

FUNCTION eis_mask2windata, wd, swspec=swspec, lwspec=lwspec

;+
; NAME:
;     EIS_MASK2WINDATA
;
; PURPOSE:
;     Inserts a mask spectrum into an EIS windata structure, which is
;     a necessary step for using an auto_fit template for the mask
;     spectrum (see eis_mask_auto_fit.pro).
;
; CATEGORY:
;     Hinode/EIS; windata; mask.
;
; CALLING SEQUENCE:
;     Result = EIS_MASK2WINDATA( Wd )
;
; INPUTS:
;     Wd:    A windata structure in the format produced by
;            eis_getwindata.pro. Either SWSPEC, or LWSPEC must also be
;            specified. 
;
; OPTIONAL INPUTS:
;     SwSpec:  A mask spectrum for the EIS short-wave channel, as
;              produced by eis_mask_spectrum.pro. It must be from the
;              same file as WD.
;     LwSpec:  A mask spectrum for the EIS long-wave channel, as
;              produced by eis_mask_spectrum.pro. It must be from the
;              same file as WD.
;	
; OUTPUTS:
;     A windata structure identical to WD, except for the following
;     changes:
;     * the int tag has been filled with the part of the mask spectrum
;       that corresponds to the original WD structure. The inserted
;       spectrum is identical for each spatial pixel.
;     * the err tag has also been replaced using the error array from
;       the mask spectrum.
;     * the three wave_corr tag arrays have been set to zero.
;     * the time_stamp tag is updated to the current time.
;
;     If a problem is found, then WD is returned.
;
; MODIFICATION HISTORY:
;     Ver.1, 22-Aug-2020, Peter Young
;     Ver.2, 26-Jul-2023, Peter Young
;       The routine wasn't working properly for windows that had been joined
;       with eis_join_windata, so I've fixed this now.
;-


IF n_params() LT 1 THEN BEGIN
  print,'Use:  IDL> wdout=eis_mask2windata(wd, swspec= or lwspec= )'
  print,''
  print,'  It is necessary to specify one of swspec or lwspec.'
  return,wd
ENDIF 

wdout=wd

ntsw=n_tags(swspec)
ntlw=n_tags(lwspec)

IF ntsw NE 0 AND ntlw NE 0 THEN BEGIN
  print,'% EIS_MASK2WINDATA: please specify either SWSPEC or LWSPEC, but not both. Returning...'
  return,wdout
ENDIF

IF ntsw EQ 0 AND ntlw EQ 0 THEN BEGIN
  print,'% EIS_MASK2WINDATA: please specify either SWSPEC= or LWSPEC=. Returning...'
  return,wdout
ENDIF 

;
; The original code (commented out) did not work for windows joined
; with eis_join_windata.
;
IF ntsw NE 0 THEN BEGIN
  nw=n_elements(wd.wvl)
  wpix=intarr(nw)
  FOR i=0,nw-1 DO BEGIN
    getmin=min(abs(swspec.wvl-wd.wvl[i]),imin)
    IF getmin GE 0.02 THEN message,/info,/cont,'Warning - wavelength arrays seem to be mis-matched.'
    wpix[i]=imin
  ENDFOR 
;  wpix=indgen(wd.nl)+wd.hdr.tdetx-50
  int=swspec.int[wpix]
  err=swspec.err[wpix]
ENDIF ELSE BEGIN
  nw=n_elements(wd.wvl)
  wpix=intarr(nw)
  FOR i=0,nw-1 DO BEGIN
    getmin=min(abs(lwspec.wvl-wd.wvl[i]),imin)
    IF getmin GE 0.02 THEN message,/info,/cont,'Warning - wavelength arrays seem to be mis-matched.'
    wpix[i]=imin
  ENDFOR 
;  wpix=indgen(wd.nl)+wd.hdr.tdetx-50-2148
  int=lwspec.int[wpix]
  err=lwspec.err[wpix]
ENDELSE 


nx=wd.nx
ny=wd.ny

FOR i=0,nx-1 DO BEGIN
  FOR j=0,ny-1 DO BEGIN
    wdout.int[*,i,j]=int
    wdout.err[*,i,j]=err
  ENDFOR 
ENDFOR

wdout.wave_corr=0.
wdout.wave_corr_tilt=0.
wdout.wave_corr_t=0.

wdout.time_stamp=systime()


return,wdout

END

