
FUNCTION eis_replace_errors, wvl, int, f, noabs=noabs, photon=photon

;+
; NAME:
;     EIS_REPLACE_ERRORS
;
; PURPOSE:
;     Derives error values for interpolated intensity pixels. The
;     error is derived using the fit parameters, F, obtained from
;     EIS_INT_ERR_FIT, and the values of the interpolated pixels.
;
; CATEGORY:
;     Hinode/EIS; imputation.
;
; CALLING SEQUENCE:
;     Result = EIS_REPLACE_ERRORS( Wvl, Int, F )
;
; INPUTS:
;     WVL   The wavelength array corresponding to the intensity array
;           INT. Normally this will be WINDATA.WVL.
;     INT   Intensity array containing interpolated intensity values.
;     F     A two-element array produced by EIS_INT_ERR_FIT which
;           gives the parameters of the straight line fit performed by
;           EIS_INT_ERR_FIT.
;
; KEYWORD PARAMETERS:
;     NOABS:  If set, then the intensity array is assumed to have been
;             derived with the /noabs keyword to eis_prep. It is
;             recommended that you use the output noabs= from
;             eis_int_err_fit and then input it to this routine.
;     PHOTON: If set, then the intensity array is assumed to have been
;             derived with the /photon keyword to eis_prep. It is
;             recommended that you use the output photon= from
;             eis_int_err_fit and then input it to this routine.
;
; OUTPUTS:
;     An array of same size as INT containing the interpolated
;     errors. Note that the errors are interpolated for all pixels,
;     not just the ones with interpolated intensities.
;
; EXAMPLE:
;     IDL> wd=eis_getwindata(file,195.12)
;     IDL> f=eis_int_err_fit(wd,noabs=noabs,photon=photon)
;     IDL> err=eis_replace_errors(wd.wvl,wd.int,f,noabs=noabs,photon=photon)
;
; MODIFICATION HISTORY:
;     Ver.1, 16-Apr-2009, Peter Young
;     Ver.2, 13-May-2025, Peter Young
;       Added the /noabs keyword; updated header.
;     Ver.3, 06-Aug-2025, Peter Young
;       Added /photon keyword; corrected expressions for g and h; now calls
;       eis_replicate_vector to allow array operations to be used.
;-

ea=eis_eff_area(wvl,/quiet)

;
; Create effective area and wavelength arrays that match the
; dimensions of int.
;
siz=size(int,/dim)
ea_arr=eis_replicate_vector(ea,siz[1:*])
wvl_arr=eis_replicate_vector(wvl,siz[1:*])

;
; Compute the error for the three cases.
; Note that the formulae below must match those in eis_int_err_fit!
;
CASE 1 OF
  keyword_set(noabs): BEGIN
    g=(int > 0.) / wvl_arr
    err_out = sqrt( f[1]*g + f[0] )
  END
  ;
  keyword_set(photon): BEGIN
    g=(int > 0.) / wvl_arr^2
    err_out = sqrt( f[1]*g + f[0] ) * wvl_arr
  END
  ;
  ELSE: BEGIN
    g=(int > 0.) * ea_arr / wvl_arr
    err_out=sqrt( f[1]*g + f[0] ) / ea_arr
  END 
ENDCASE 

return,err_out

END
