
FUNCTION eis_fitdata_deblend, fitdata, bfitdata, fraction=fraction, line=line, bline=bline

;+
; NAME:
;    EIS_FITDATA_DEBLEND()
;
; PURPOSE:
;    This routine modifies the INT and INTERR tags in a FITDATA
;    structure, by subtracting an amount contained in another FITDATA
;    structure. It is intended for de-blending a line intensity prior
;    to input to EIS_DENSITY. A particular example is the Mg VII
;    278.39 that is blended with Si VII 278.44, and can be de-blended
;    by subtracting a fraction of the Si VII 275.36 intensity.
;
;    Note that the wavelength-dependent Y-offset between wavelengths
;    is corrected for.
;
; CATEGORY:
;    Gaussian line fitting.
;
; CALLING SEQUENCE:
;	Result = EIS_FITDATA_DEBLEND(Fitdata, Bfitdata)
;
; INPUTS:
;    FITDATA - A structure in the format returned by EIS_AUTO_FIT
;              containing the emission line to be de-blended.
;
;    BFITDATA - A structure in the format returned by EIS_AUTO_FIT
;               containing the line to be used for the correction.
;
; OPTIONAL INPUTS:
;    FRACTION - A constant by which the correcting line intensity will
;               be multipled by before being subtracted from the
;               blended line.
;
;    LINE     - If FITDATA contains more than one line, then LINE is
;               an integer specifying which of the lines is the one to
;               be corrected.
;
;    BLINE    - If BFITDATA contains more than one line, then BLINE is
;               an integer specifying which of the lines is the one to
;               use.
;
; OUTPUTS:
;    Returns a FITDATA structure identical to the input FITDATA, but
;    with the .INT and .INTERR tags modified by subtracting off the
;    blending line contained in the BFITDATA structure. Pixels that
;    are missing in either of FITDATA or BFITDATA will be set to
;    missing.
;
;    If there is a problem then -1 will be returned.
;
; EXAMPLES:
;    Correct the Mg VII 278.39 for the Si VII blend using Si VII
;    275.35:
;    newfit=eis_fitdata_deblend(fit278,fit275,fraction=0.32)
;
; SIDE EFFECTS:
;    The new INT and INTERR tags will not be consistent with the AA
;    and SIGMAA tags that contain the Gaussian fit parameters, i.e.,
;    multiplying the line amplitude and width from AA will not yield the
;    intensity contained in INT.
;
; MODIFICATION HISTORY:
;    Ver.1, 10-Jul-2015, Peter Young
;-


IF n_params() LT 2 THEN BEGIN
  print,'Use:  IDL> newfit=eis_fitdata_deblend(fitdata,bfitdata [,line=, bline=, fraction=])'
  return,-1
ENDIF 

IF n_elements(fraction) EQ 0 THEN fraction=1.0

int=eis_get_fitdata(fitdata, line=line, /int, err=err)
yshift_wvl=fitdata.refwvl[line]

IF int[0] EQ -1 THEN BEGIN
  print,'% EIS_FITDATA_DEBLEND: problem with inputs. Returning...'
  return,-1
ENDIF

bint=eis_get_fitdata(bfitdata, line=bline, /int, err=berr, yshift_wvl=yshift_wvl)

IF bint[0] EQ -1 THEN BEGIN
  print,'% EIS_FITDATA_DEBLEND: problem with inputs. Returning...'
  return,-1
ENDIF

k=where(int EQ fitdata.missing OR bint EQ fitdata.missing)
int=int-(bint*fraction)
int[k]=fitdata.missing

err=sqrt(err^2 + (fraction*berr)^2 )
err[k]=fitdata.missing

outdata=fitdata
outdata.int=int
outdata.interr=err

return,outdata

END

