
FUNCTION EIS_TEMPLATE_PARINFO, TEMPLATE

;+
; NAME
;
;     EIS_TEMPLATE_PARINFO()
;
; PROJECT
;
;     Hinode/EIS
;
; EXPLANATION
;
;     Takes an EIS fit template and uses the line parameter constraint
;     information to produce a PARINFO structure suitable for input to
;     the MPFIT procedures.
;
; INPUTS
;
;     TEMPLATE   A fit template produced by eis_fit_template.
;
; OUTPUTS
;
;     A PARINFO structure in the format used by the MPFIT procedures.
;
; HISTORY
;
;     Ver.1, 29-Jan-2010, Peter Young
;     Ver.2, 4-May-2011, Peter Young
;       I've introduced the tag STEP to PARINFO in order to
;       manually specify the step sizes used for the centroid and line
;       width. This seems to give better fits.
;-

ngauss=template.ngauss
nback=template.nback
nparams=ngauss*3+nback

;
; I use STEP rather than RELSTEP because the wavelength pixel size is
; constant across the EIS spectra, and because line widths are
; approximately the same for most lines. STEP will only be implemented
; for centroids and width (a value of 0 implies that the step size is
; automatically calculated by the MPFIT routines).
;
parinfo=replicate({fixed: 0, limited: [0,0], limits:[0.d,0.d], step: 0.},nparams)

null_value=template.null_value

str1=''
FOR i=0,ngauss-1 DO BEGIN
  iparam=i*3
  peak_lim=template.lines[i].peak_lim
  FOR k=0,1 DO BEGIN 
    IF peak_lim[k] NE null_value THEN BEGIN
      parinfo[iparam].limited[k]=1
      parinfo[iparam].limits[k]=peak_lim[k]
    ENDIF
  ENDFOR
 ;
  iparam=i*3+1
  parinfo[iparam].step=0.005   ; step size for centroids
  cen_lim=template.lines[i].cen_lim
  FOR k=0,1 DO BEGIN 
    IF cen_lim[k] NE null_value THEN BEGIN
      parinfo[iparam].limited[k]=1
      parinfo[iparam].limits[k]=cen_lim[k]
    ENDIF
  ENDFOR
 ;
  iparam=i*3+2
  parinfo[iparam].step=0.005   ; step size for widths
  wid_lim=template.lines[i].wid_lim
  FOR k=0,1 DO BEGIN 
    IF wid_lim[k] NE null_value THEN BEGIN
      parinfo[iparam].limited[k]=1
      parinfo[iparam].limits[k]=wid_lim[k]
    ENDIF
  ENDFOR
ENDFOR

return,parinfo

END
