
FUNCTION eis_subtract_mask_spectra, spec1, spec2, xrange=xrange, plot=plot

;+
; NAME:
;	EIS_SUBTRACT_MASK_SPECTRA
;
; PURPOSE:
;       Takes two spectra derived from the routine eis_mask_spectrum,
;       and subtracts one from the other. The typical usage is when
;       spec2 is a background spectrum to be subtracted from spec1.
;
; CATEGORY:
;       Hinode/EIS; spectral analysis.
;
; CALLING SEQUENCE:
;	Result = EIS_SUBTRACT_MASK_SPECTRA(Spec1, Spec2)
;
; INPUTS:
;	Spec1:	A spectrum structure in the format produced by
;        	eis_mask_spectrum.
;	Spec2:	A spectrum structure in the format produced by
;        	eis_mask_spectrum which will be subtracted from
;        	Spec1. 
;
; OPTIONAL INPUTS:
;       Xrange: A 2 element array specifying the wavelength range to
;               plot (only works if /plot set).
;
; KEYWORD PARAMETERS:
;       PLOT:   If set, then spec1 is plotted, with spec2 over-plotted
;               as a dashed line.
;
; OUTPUTS:
;	A spectrum structure in the format produced by
;	eis_mask_spectrum. The structure will be identical to Spec1,
;	except that the .int and .err tags are modified by subtracting
;	the spectrum in Spec2.
;
; EXAMPLE:
;       eis_mask_spectrum, file, mask, sw=sw, lw=lw
;       eis_mask_spectrum, file, mask_bg, sw=swbg, lw=lwbg
;       outsw=eis_subtract_mask_spectra(sw,swbg)
;
; MODIFICATION HISTORY:
;       Ver.1, 17-Oct-2013, Peter Young
;       Ver.2, 18-Oct-2013, Peter Young
;         Added XRANGE= and /PLOT.
;-

outspec=spec1
outspec.int=-100
outspec.err=-100


k=where(spec1.int NE -100. AND spec2.int NE -100.,nk)
outspec[k].int=spec1[k].int-spec2[k].int
outspec[k].err=sqrt(spec1[k].err^2 + spec2[k].err^2)

IF keyword_set(plot) THEN BEGIN
  plot,spec1.wvl,spec1.int>0,psym=10,/xsty,xrange=xrange
  oplot,spec2.wvl,spec2.int,psym=10,line=2
ENDIF 

return,outspec

END
