
PRO eis_write_template, outfile, template

;+
; NAME
;
;    EIS_WRITE_TEMPLATE
;
; PROJECT
;
;    Hinode/EIS
;
; EXPLANATION
;
;    Creates a text file containing the full specification of the
;    TEMPLATE structure that is one of the inputs to the routine
;    EIS_AUTO_FIT.
;
;    Generally the user will not use this routine directly, instead it
;    will be called from EIS_FIT_TEMPLATE.
;
; INPUTS
;
;    OUTFILE      A string giving the name of the file to be output.
;
;    TEMPLATE     An EIS fit template structure - see
;                 eis_read_template for details of tags.
;
; OUTPUTS
;
;    A text file called OUTFILE is written to the user's directory.
;
; CALLS
;
;    None.
;
; MODIFICATION HISTORY:
;
;    Ver.1, 1-Feb-2010, Peter Young
;    Ver.2, 21-Aug-2020, Peter Young
;      refpix was not being written, so fixed this.
;-


n=template.ngauss
nback=template.nback
null_value=template.null_value

line_str=template.lines

refpix=template.refpix
;IF n_elements(refpix) EQ 0 THEN refpix=[0,0]

IF n_elements(cent_range) EQ 0 THEN cent_range=null_value
IF n_elements(width_range) NE 2 THEN width_range=[null_value,null_value]

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,'Null value: '+string(format='(f12.3)',null_value)
printf,lout,'Reference pixel: '+string(format='(2i5)',refpix)
;
FOR i=0,n-1 DO BEGIN
  printf,lout,'Gaussian '+string(format='(i3)',i)
  printf,lout,'Initial guess for wavelength:     '+string(format='(f12.3)',line_str[i].centroid)
  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)',line_str[i].width)
 ;
  printf,lout,format='("Allowed range for wavelength:",2f12.3)', $
         line_str[i].cen_lim
  printf,lout,format='("Allowed range for peak:      ",2f12.3)', $
         line_str[i].peak_lim
  printf,lout,format='("Allowed range for width:     ",2f12.3)', $
         line_str[i].wid_lim
  printf,lout,'Parameter tying:'
  printf,lout,format='("  Amplitude: ",i3,f12.3)',line_str[i].peak_tie, $
         line_str[i].peak_tie_val
  printf,lout,format='("  Centroid:  ",i3,f12.3)',line_str[i].cen_tie, $
         line_str[i].cen_tie_val
  printf,lout,format='("  Width:     ",i3,f12.3)',line_str[i].wid_tie
ENDFOR
free_lun,lout

END
