
PRO spec_gauss_eis, specstr, shift=shift, xrange=xrange, yrange=yrange, $
                    int_negative=int_negative, continuum=continuum

;+
; NAME:
;    SPEC_GAUSS_EIS
;
; PURPOSE:
;    This routine is a 'wrapper' for calling the general purpose
;    line fitting routine SPEC_GAUSS_WIDGET. For creating the input
;    spectrum in the correct format, try using the routine
;    EIS_MASK_SPECTRUM. 
;
; CATEGORY:
;    Hinode/EIS; Gaussian fitting.
;
; CALLING SEQUENCE:
;    SPEC_GAUSS_EIS, SpecStr
;
; INPUTS:
;    SpecStr:  A structure containing 1D spectra in the form produced by
;             EIS_SLIT_SUM. The structure must have the following
;             tags:
;             .wvl     Wavelength vector (Angstroms)
;             .int     Intensity vector (erg cm-2 s-1 sr-1 Ang-1)
;             .err     Error vector (erg cm-2 s-1 sr-1 Ang-1)
;             .qual    Quality vector
;
;             Optional tags are:
;             .qual_max The maximum allowed value of the quality vector.
;
; OPTIONAL INPUTS:
;    Shift:  If the spectrum is known to have a systematic velocity
;            shift, then this will affect the routine's
;            ability to automatically identify emission
;            lines. Specifying the shift with SHIFT will remove the
;            offset from the line identification process. E.g., if
;            the spectrum is know to be red-shifted by 30 km/s, then
;            specify SHIFT=30.
;    Xrange: A 2-element array allowing the user to manually specify
;            the X-range for the plot. Note that clicking 'Unzoom' in
;            the widget defaults back to XRANGE.
;    Yrange: A 2-element array allowing the user to manually specify
;            the Y-range for the plot. Note that clicking 'Unzoom' in
;            the widget defaults back to YRANGE.
;
; KEYWORD PARAMETERS:
;    INT_NEGATIVE:  By default the routine displays the spectrum with
;                   only positive values shown. Use this keyword to
;                   show all values (may be needed if there's a
;                   problem with the dark frame subtraction).
;    CONTINUUM:  Implements the /continuum option in spec_gauss_widget,
;                which allows average intensity values to be written to
;                the average_intensity.txt file in the working directory.
;                This file can be read with sg_read_avg_int.pro.
;	
; OUTPUTS:
;    As the user fits lines in the spectrum, a text file is created in
;    the current working directory and called
;    spec_gauss_fits.txt. This file contains the fit parameters for
;    each of the fitted lines. S
;
; CALLS:
;    SPEC_GAUSS_WIDGET
;
; EXAMPLE:
;    IDL> eis_mask_spectrum,file,mask,swspec=swspec,lwspec=lwspec
;    IDL> spec_gauss_eis,swspec
;
; MODIFICATION HISTORY:
;    Ver.1, 4-Sep-2008, Peter Young
;    Ver.2, 5-Sep-2008, Peter Young
;       added /qqset in call to spec_gauss_widget
;    Ver.3, 24-Oct-2008, Peter Young
;       now points to Solarsoft file for EIS line IDs
;    Ver.4, 31-Oct-2008, Peter Young
;       added xrange and yrange calls to spec_gauss_widget
;    Ver.5, 18-Dec-2008, Peter Young
;       added optional inputs xrange= and yrange=
;    Ver.6, 16-Jun-2010, Peter Young
;       modified def_width value
;    Ver.7, 4-May-2011, Peter Young
;       added parinfo_wvl_step=0.005 in call to spec_gauss_widget
;    Ver.8, 12-Dec-2011, Peter Young
;       now sends keyword /INT_POSITIVE to SPEC_GAUSS_WIDGET in
;       order to force intensities to be positive.
;    Ver.9, 29-Jan-2019, Peter Young
;       added /INT_NEGATIVE; re-formatted and expanded header; added
;       parameter check.
;    Ver.10, 14-Oct-2025, Peter Young
;       added /CONTINUUM.
;    Ver.11, 02-Apr-2026, Peter Young
;       if intensity units are DN or photons, then I set angpix=0 when
;       calling spec_gauss_widget
;-

IF n_params() LT 1 THEN BEGIN
  print,'Use:  IDL> spec_gauss_eis, spec [, shift=, xrange=, yrange=, /int_negative, /continuum]'
  return
ENDIF 

IF n_elements(shift) EQ 0 THEN shift=0.

xx=specstr.wvl
yy=specstr.int
ee=specstr.err
qq=specstr.qual
IF tag_exist(specstr,'qual_max') THEN qual_max=specstr.qual_max

;
; This is the default width assumed for all EIS emission lines. If the
; user does not specify an initial guess for the width of the line,
; then def_width will be used.
;
def_width=0.065   ; angstroms

fname=getenv('SSW')+'/hinode/eis/idl/atest/pyoung/eis_line_ids.txt'
IF file_exist(fname) EQ 1 THEN BEGIN
  line_list={fname: fname, width:def_width, shift: shift}
ENDIF

i=where(yy NE -100.,ni)
IF n_elements(xrange) EQ 0 THEN xrange=[min(xx[i]),max(xx[i])]
IF n_elements(yrange) EQ 0 THEN BEGIN
  yrange=[min(yy[i]),max(yy[i])*1.1]
  IF NOT keyword_set(int_negative) THEN BEGIN
    yrange[0]=0
    int_positive=1
  ENDIF
ENDIF

;
; If the intensity units are DN or photon-events, then I need to set angpix=0,
; i.e., the intensities are in per-pixel units rather than per-angstrom units.
;
IF specstr.units EQ 'DN' OR specstr.units EQ 'Photon-Events' THEN angpix=0 ELSE angpix=1

spec_gauss_widget,xx,yy,ee,qq, $
                  def_width=def_width, qqmax=qual_max, $
                  line_list=line_list,angpix=angpix,/qqset, $
                  xrange=xrange, yrange=yrange, $
                  set_width_range=0, width_range=[0.05,0.15], $
                  parinfo_wvl_step=0.005, int_positive=int_positive, $
                  continuum=continuum

END
