function eis_read_template, infile, empty=empty

;+
; NAME
;
;     EIS_READ_TEMPLATE()
;
; PROJECT
;
;     Hinode/EIS
;
; EXPLANATION
;
;     This routine reads the fit template file created by
;     eis_fit_template into an IDL structure.
;
; INPUTS
;
;     INFILE   The name of the fit template text file.
;
; KEYWORDS
;
;     EMPTY    If set, then an "empty" structure containing space for
;              20 emission lines is created. Note that /EMPTY
;              over-rides any file specified by INFILE.
;
; OUTPUTS
;
;     Creates a structure with the following tags
;      .lines      Structure that defines the fit parameters (peak,
;                  centroid, width) for each emission line, and
;                  parameter tying information.
;      .refpix     2 element array. The reference pixel in the spatial
;                  area for which the template is valid.
;      .ngauss     Number of emission lines.
;      .nback      Number of background parameters.
;      .null_value  The value used to identify null data.
;      .init       A vector that is directly input to MPFITEXPR for
;                  specifying the initial parameters of the fit. 
;
; PROGRAMMING NOTES
;
;     The tags for TEMPLATE.LINES are:
;     .peak   Value for line peak.
;     .centroid  Value for line centroid
;     .width  Value for line width (Gaussian, not FWHM)
;     .peak_tie  Parameter index to which line peak is tied.
;     .peak_tie_val  Value for the ratio of the current line peak to
;                    the 'tied' line peak
;     .cen_tie   Parameter index to which line centroid is tied.
;     .cen_tie_val   Value for the offset of the current line centroid to
;                    the 'tied' line centroid.
;     .wid_tie   Parameter index to which line width is tied.
;     .peak_lim  2 element array giving lower and upper bounds on the
;                line peak. A null value indicates no bound is set.
;     .cen_lim  2 element array giving lower and upper bounds on the
;               line centroid. A null value indicates no bound is set.
;     .wid_lim  2 element array giving lower and upper bounds on the
;               line width. A null value indicates no bound is set.
;
; MODIFICATION HISTORY:
;
;     Ver. 1, 2-Feb-2010, Peter Young
;     Ver. 2, 21-Aug-2020, Peter Young
;       Changed refpix to an integer array.
;-


IF NOT keyword_set(empty) THEN BEGIN
  IF n_elements(infile) EQ '' THEN BEGIN
    print,'Use:  IDL> template = eis_read_template( infile [, /empty ] )'
    return,-1
  ENDIF 
  chck=file_search(infile)
  IF chck EQ '' THEN return,-1
ENDIF 

null_value=-100.

linestr={peak: 0., $
         centroid: 0., $
         width: 0., $
         peak_lim: fltarr(2)+null_value, $
         cen_lim: fltarr(2)+null_value, $
         wid_lim: fltarr(2)+null_value, $
         peak_tie: -1, $
         peak_tie_val: 0., $
         cen_tie: -1, $
         cen_tie_val: 0., $
         wid_tie: -1}

refpix=intarr(2)
ngauss=20
nback=2
IF NOT keyword_set(empty) THEN BEGIN
  openr,lin,infile,/get_lun
  readf,lin,format='(18x,i3)',ngauss
  readf,lin,format='(30x,i3)',nback
  readf,lin,format='(12x,f12.3)',null_value
  readf,lin,format='(17x,2i5)',refpix
ENDIF

nparams=3*ngauss+nback

;
; Create the output TEMPLATE structure
; ------------------------------------
;
template={lines: replicate(linestr,ngauss), $
          refpix: refpix, $
          ngauss: ngauss, $
          nback: nback, $
          null_value: null_value, $
          init: fltarr(nparams)}
IF nback EQ 2 THEN template.init[ngauss*3:ngauss*3+1]=0. ELSE $
     template.init[ngauss*3]=0.

IF keyword_set(empty) THEN return,template

str1=''
FOR i=0,ngauss-1 DO BEGIN
  readf,lin,str1    ; skip this line
 ;
  readf,lin,format='(34x,f12.0)',wvl
  template.lines[i].centroid=wvl
  template.init[i*3+1]=wvl
 ;
  readf,lin,format='(34x,f10.0)',peak
  template.lines[i].peak=peak
  template.init[i*3]=peak
 ;
  readf,lin,format='(34x,f12.0)',width
  template.lines[i].width=width
  template.init[i*3+2]=width
 ;
  readf,lin,format='(29x,2f12.0)',wvl1,wvl2
  template.lines[i].cen_lim=[wvl1,wvl2]
 ;
  readf,lin,format='(29x,2f12.0)',peak1,peak2
  template.lines[i].peak_lim=[peak1,peak2]
 ;
  readf,lin,format='(29x,2f12.0)',wid1,wid2
  template.lines[i].wid_lim=[wid1,wid2]
 ;
  readf,lin,str1   ; read 'Parameter tying' string
  readf,lin,format='(13x,i3,f12.0)',k,val
  template.lines[i].peak_tie=k
  template.lines[i].peak_tie_val=val
  readf,lin,format='(13x,i3,f12.0)',k,val
  template.lines[i].cen_tie=k
  template.lines[i].cen_tie_val=val
  readf,lin,format='(13x,i3)',k
  template.lines[i].wid_tie=k
ENDFOR

free_lun,lin

return,template

END
