
FUNCTION eis_fit_FUNCTION, template, FUNCTION_name=FUNCTION_name

;+
; NAME
;
;     EIS_FIT_FUNCTION
;
; PROJECT
;
;     Hinode/EIS
;
; EXPLANATION
;
;     Creates the fit function that will be used by EIS_AUTO_FIT
;     using the input TEMPLATE structure. The fit function is a linear
;     combination of GAUSS_SG() and LINE_SG() functions. By making use
;     of the parameter tying information (stored in TEMPLATE.LINES) it
;     is possible to make the parameters of one Gaussian to be
;     dependent on the parameters of another Gaussian (e.g., fixing
;     the width of two Gaussians to be the same).
;
; INPUTS
;
;     TEMPLATE   A fit template structure created by the routine
;                EIS_FIT_TEMPLATE. 
;
; OPTIONAL INPUTS
;
;     FUNCTION_NAME  By default, the routine creates a fit function
;                    that is a linear combination of Gaussians
;                    (defined by the function GAUSS_SG.PRO). An
;                    alternative function can be specified with this
;                    keyword, simply by giving the string of the
;                    function name, e.g., 'user_function'. An example
;                    of how the keyword is used is given in the
;                    PROGRAMMING NOTES section below.
;
; OUTPUTS
;
;     A text string giving the fit function in the format expected by
;     MPFITEXPR. 
;
; PROGRAMMING NOTES
;
;     Using FUNCTION_NAME -
;     The user should define a function that takes inputs X and P,
;     where X is interpreted as wavelength, and P contains the
;     parameters for the function. P must contain 3 elements, with
;     P[0] corresponding to the peak, P[1] the centroid, and P[2] the
;     width. See the GAUSS_SG.PRO function in Solarsoft for how to
;     define the function. An example is the Voigt function which can
;     be defined with these parameters. Another is the modified
;     Gaussian function used for the SOHO/CDS mission
;
; HISTORY
;
;     Ver.1, 14-Jan-2010, Peter Young
;
;     Ver.2, 25-Jul-2012, Peter Young
;       added FUNCTION_NAME keyword
;-

IF n_params() LT 1 THEN BEGIN
  print,'Use:  IDL> fn=eis_fit_function(template [, function_name=])'
  return,-1
ENDIF 

IF n_elements(FUNCTION_name) EQ 0 THEN FUNCTION_name='gauss_sg'

ngauss=template.ngauss
nback=template.nback
nparam=ngauss*3+nback
null_value=template.null_value

fn=''
FOR i=0,ngauss-1 DO BEGIN
  fn=fn+FUNCTION_name+'(x,['
 
 ;
 ; Add line peak parameters to fit function
 ;
  peak_tie=template.lines[i].peak_tie
  peak_tie_val=template.lines[i].peak_tie_val

  IF peak_tie EQ -1 THEN BEGIN
    add_string='p['+trim(i*3)+'],'
    peak_string=''
  ENDIF ELSE BEGIN
    add_string='p['+trim(peak_tie*3)+'],'
    peak_string=',peak_factor='+string(format='(e12.3)',peak_tie_val)
  ENDELSE 
  fn=fn+add_string

 ;
 ; Add line centroid parameters to fit function
 ;
  cen_tie=template.lines[i].cen_tie
  cen_tie_val=template.lines[i].cen_tie_val

  IF cen_tie EQ -1 THEN BEGIN
    add_string='p['+trim(i*3+1)+'],'
    cen_string=''
  ENDIF ELSE BEGIN
    add_string='p['+trim(cen_tie*3+1)+'],'
    cen_string=',cen_offset='+string(format='(e12.3)',cen_tie_val)
  ENDELSE
  fn=fn+add_string

 ;
 ; Add line width parameters to fit function
 ;
  wid_tie=template.lines[i].wid_tie

  IF wid_tie EQ -1 THEN BEGIN
    add_string='p['+trim(i*3+2)+']]'
  ENDIF ELSE BEGIN
    add_string='p['+trim(wid_tie*3+2)+']]'
  ENDELSE 
  fn=fn+add_string

  IF peak_string NE '' THEN fn=fn+peak_string
  IF cen_string NE '' THEN fn=fn+cen_string

  fn=fn+') + '
ENDFOR 

IF nback EQ 2 THEN fn=fn+' line_sg(x,p['+trim(nparam-2)+':'+trim(nparam-1)+'])' $
                      ELSE fn=fn+' p['+trim(nparam-1)+']'

return,fn

END
