

PRO eis_make_fit_template, spec, wrange=wrange, outfile=outfile, $
                           cen_bound=cen_bound

;+
; NAME
;
;     EIS_MAKE_FIT_TEMPLATE
;
; PROJECT
;
;     Hinode/EIS
;
; EXPLANATION
;
;     The aim of this routine is to create a template that defines a
;     multi-Gaussian function that can be fit to a portion of an EIS
;     spectrum. This template can then be input to eis_auto_fit (or
;     equivalent).
;
; INPUTS
;
;     SPEC    A structure with the following tags:
;              .wvl
;              .int
;             which define a spectrum. If the .int tag is an array of
;             spectra (e.g., it's the output from
;             eis_getwindata), then the spectrum is averaged over the
;             array.
;
; OPTIONAL INPUTS
;
;     OUTFILE  The name of the text file that will contain the fit
;              template information. If not set then the template will
;              be written to the file 'fit_template.txt' in the
;              current working directory.
;
;     WRANGE   A two element array. Use this to specify a reduced
;              wavelength range for the display.
;
; HISTORY
;
;     Beta 1, 6-Nov-2008, Peter Young
;-

IF n_elements(outfile) EQ 0 THEN outfile='fit_template.txt'

IF n_elements(cen_bound) EQ 0 THEN cen_bound=0.15

;
; If spec.int is not a 1D array (e.g., if it is the intensity tag of
; the eis_getwindata structure) then average int over the spatial/time
; dimensions.
;
siz=size(spec.int)
IF siz[0] GT 1 THEN BEGIN
  int=spec.int
  dimen=indgen(siz[0]-1)+2
  int=average(int,dimen)
ENDIF ELSE BEGIN
  int=spec.int
ENDELSE 

IF n_elements(wrange) EQ 0 THEN wrange=[min(spec.wvl),max(spec.wvl)]

!p.multi=0
plot,spec.wvl,int,psym=10,/xsty,xrange=wrange, $
     xtit='Wavelength / '+string(197b), $
     ytit='Intensity / erg cm!u-2!n s!u-1!n sr!u-1!n '+string(197b)+'!u-1!n'

print,''
print,'** Click once on plot to select background estimate **'
cursor,x,y,/up
print,format='("    Background level:",f8.1)',y
oplot,[-1e5,1e5],[1,1]*y,th=2
backg=y

print,''
print,'** Click once for each line to choose the peak amplitude of each line. **'
print,'** When you are finished, click once in the margin of the plot window  **'
print,'** to exit the routine and write the template file.                    **'
;
swtch=0
;
xr=!x.crange
yr=!y.crange
;
str={wvl: 0., peak: 0.}
line_str=0
WHILE swtch EQ 0 DO BEGIN
  cursor,x,y,/up
  IF (x LT xr[0] OR x GT xr[1]) OR (y LT yr[0] OR y GT yr[1]) THEN BEGIN
    swtch=1
  ENDIF ELSE BEGIN
    str.wvl=x
    str.peak=y-backg
    oplot,th=2,[1,1]*x,[backg,y]
    IF n_tags(line_str) EQ 0 THEN line_str=str ELSE line_str=[line_str,str]
  ENDELSE
ENDWHILE

n=n_elements(line_str)

nback=2

openw,lout,outfile,/get_lun
printf,lout,'No. of Gaussians: '+string(format='(i3)',n)
printf,lout,'No. of background parameters: '+string(format='(i3)',nback)
printf,lout,'Wavelength range: '+string(format='(2f12.3)',wrange)
printf,lout,'Initial guess for background level: '+string(format='(f8.1)',backg)
;
FOR i=0,n-1 DO BEGIN
  printf,lout,'Gaussian '+string(format='(i3)',i+1)
  printf,lout,'Initial guess for wavelength:     '+string(format='(f12.3)',line_str[i].wvl)
  printf,lout,'Initial guess for peak:           '+string(format='(f10.1)',line_str[i].peak)
  printf,lout,'Initial guess for Gaussian width: '+string(format='(f12.3)',0.030)
 ;
  printf,lout,format='("Allowed range for wavelength:",2f12.3)', $
         line_str[i].wvl-cen_bound,line_str[i].wvl+cen_bound
;  printf,lout,format='("Allowed range for peak:      ",2f12.3)', $
;         -1,-1
  printf,lout,format='("Allowed range for width:     ",2f12.3)', $
         0.02,0.06
ENDFOR
free_lun,lout

END
