
PRO eis_update_refwvl, fitdata, refwvl, line=line

;+
; NAME
;
;     EIS_UPDATE_REFWVL
;
; PROJECT
;
;     Hinode/EIS
;
; EXPLANATION
;
;     Updates the reference wavelength values in the fit structure
;     FITDATA using the specified input value. If the fit structure
;     contains multiple Gaussians then the routine ensures that the
;     user updates the correct reference wavelength.
;
;     Consider a two Gaussian fit to the lines Fe X 184.54 and Fe VIII
;     185.21. If the REFWVl tag of FITDATA contains the values 184.537
;     and 185.209, and the new reference wavelength of the Fe VIII
;     line is 185.212, then the REFWVl tag of FITDATA will be modified
;     to [184.540,185.212]. I.e., the Fe VIII wavelength has been set
;     to the input reference wavelength, while the Fe X wavelength has
;     been adjusted by the wavelength difference between the new and
;     old wavelengths of the Fe VIII line.
;
; INPUTS
;
;     FITDATA   A fit structure produced by EIS_AUTO_FIT.
;
;     REFWVL    A wavelength in angstroms that is the new reference
;               wavelength for one of the emission lines stored in
;               FITDATA. 
;
; OPTIONAL INPUTS
;
;     LINE      If FITDATA contains multiple emission lines then LINE
;               specifies the index of the LINE to which REFWVL
;               applies. 
;
; OUTPUTS
;
;     FITDATA is returned but with REFWVL updated as described
;     above. All other tags remain the same.
;
; HISTORY
;
;     Ver.1, 21-Apr-2010, Peter Young
;-

ng=fitdata.ngauss

IF fitdata.ngauss GT 1 THEN BEGIN
  IF n_elements(line) EQ 0 THEN BEGIN
    print,'%EIS_UPDATE_REFWVL: The fit structure contains '+trim(ng)+' emission lines. Please select'
    print,'                    which line the reference wavelength applies to by using the keyword LINE='
    print,'                    (LINE takes values 0, 1, 2, ....)'
    return
  ENDIF ELSE BEGIN
    form1=trim(ng)+'f10.4'
    print,format='("Old REFWVL: ",'+form1+')',fitdata.refwvl
    offset=refwvl-fitdata.refwvl[line]
    fitdata.refwvl=fitdata.refwvl+offset
    fitdata.refwvl[line]=refwvl
    print,format='("New REFWVL: ",'+form1+')',fitdata.refwvl
  ENDELSE 
ENDIF ELSE BEGIN
  print,format='("Old REFWVL: ",f10.4)',fitdata.refwvl
  fitdata.refwvl=refwvl
  print,format='("New REFWVL: ",f10.4)',fitdata.refwvl
ENDELSE 

END
