function get_exp_factor,header,exp_factor,exp_bias,nreg,exp_sig, silent=silent, DIR=dir
;+
; NAME:
;	GET_EXP_FACTOR
;
; PURPOSE:
;	This function returns the exposure factor and bias for the
;	given image.
;
; CATEGORY:
;	EXPFAC
;
; CALLING SEQUENCE:
;	Result = GET_EXP_FACTOR (Hdr,Exp_factor,Exp_bias)
;
; INPUTS:
;	Header:	Image header for which the factor and bias are wanted.
;
; OUTPUTS:
;	Result:		0 for success,
;			-1 for file not found,
;			-2 for time not found.
;	Exp_factor:	The exposure correction factor. It should be
;			multiplied with the exposure time to get the
;			correct time.
;	Exp_bias:	The offset bias that should be subtracted from the
;			image.
;
; OPTIONAL OUTPUTS:
;	Nreg:		Number of regions that particpated in the calculation.
;	Exp_sig:	The standard deviation in the fit.
;
; KEYWORDS:
;   	DIR=	    Use different expfac/data directory than default.
;	FITS_HDR	STRARR = a FITS header, returned modified if set
;
; COMMON BLOCKS:
;	EXP_FACTOR_ARRAY:	The exposure facotor information for a given
;			date.
;
; PROCEDURE:
;	The date in the image header is tested to see if the exposure factor
;	data in the common block are for that date.  If not, the data are
;	then read in to the common block using READ_EXP_FACTOR.  The data
;	are searched for the time of the exposure.  If no time matches
;	the exposure, then the factor is set to 1 and the standard bias from
;	OFFSET_BIAS is used.
;
; MODIFICATION HISTORY:
; 	Written by:	RA Howard, NRL, 21 September 1997
;	6 Oct 1997	RAH, Split away from READ_EXP_FACTOR
;	20 Feb 1998	RAH, Added the fits names to common
;	19 Jul 2000	NBR - Allow FITS header input, and add HISTORY to FITS header
;	 1 Aug 2000	NBR - Add FITS_HDR keyword
;	 7 Aug 2000	NBR - Remove FITS_HDR keyword
;	17 Sep 2002	NBR - Only use OFFSET_BIAS.pro if no expfactor; add filedate
;			to common block and FITS header
;	19 Sep 2002	NBR - Properly account for no expfac file found (2nd time through)
;	 9 Jan 2003	NBR - Use fn instead of filenamenopath in header
;   07 Oct 2006 F Auchère - Added the silent keyword
;   25 Jan 2011 NBR - Add DIR= keyword; make more noise if there is no expfac correction
;
; EXAMPLE:
;	To obtain the exposure factor and bias information and then
;	convert the image counts to DN/sec:
;		success = GET_EXP_FACTOR(hdr,expfac,bias)
;		IF (success NE 0)  THEN BEGIN
;			PRINT,'Exposure factor not found for image ',hdr.filename
;			PRINT,'Exposure factor is assumed to be 1.0'
;               ENDIF
;		img = (img-bias)/(hdr.exptime*expfac)
;
;      W   H
ver= '@(#)get_exp_factor.pro	1.20 07/26/11' ;NRL LASCO IDL LIBRARY
;
;-

common exp_factor_array,telescope,filename,fname,factor,bias,mjd, $
                   time,filter,polar,waveleng,nregion,sigma, filedate

version = strcompress(STRMID(ver,4,strlen(ver)))
hdr = header
;IF keyword_set(FITS_HDR) THEN hdrflag=1 ELSE hdrflag=0
IF (DATATYPE(hdr) NE 'STC')  THEN hdr=LASCO_FITSHDR2STRUCT(hdr)

;
;  Test to see if the exposure factor data have already
;  been read in for this telescope
;  First test to see if the array has been defined and
;  if it has then if it is the right telescope
;
readflag=0		; assume to not read the data file
tel  = STRLOWCASE(hdr.detector)
get_utc,dte
dte.mjd  = hdr.mid_date
yymm = STRMID(UTC2YYMMDD(dte),0,6)
dobs=utc2str(dte,/ecs)
fn   = tel+'_expfactor_'+yymm+'.dat'
;
;  Is the filename in the common block defined?
;  If it is  defined, then is it the same as the one
;  we want?
;  If either of these are not true then set the
;  flag to read in the exposure factor data.
;
s    = SIZE(filename)
IF (s(1) EQ 0) THEN readflag=1 $	; filename is undefined
	ELSE BEGIN
		break_file,filename,lo,fdir,filenamenopath,ext
		filenamenopath = filenamenopath+ext
		IF (fn EQ filenamenopath)  THEN BEGIN 	; filename already read
			readflag=0

		ENDIF ELSE readflag=1
	ENDELSE
;
;  If required read in the exposure factors
;

IF keyword_set(DIR) THEN readflag=1
loud = not keyword_set(SILENT)
if (loud) then help,readflag, tel, yymm
IF (readflag EQ 1)  THEN result = READ_EXP_FACTOR(tel,yymm, DIR=dir, LOUD=loud) $
                    ELSE result=0
;
;  Test for the status of the read
;
exp_factor  = 1.
exp_sig     = 0.
nreg        = 0
IF datatype(mjd) EQ 'UND' THEN BEGIN
   exp_bias    = OFFSET_BIAS(header,/sum)
	; HISTORY is added in offset_bias.pro
   message,'No dates match input date '+dobs+' '+hdr.filename, /info
   RETURN,-1  		; Return a failure code
ENDIF
wd = WHERE(hdr.mid_date EQ mjd,nw)
IF (result EQ -1) or nw EQ 0 THEN BEGIN
   exp_bias    = OFFSET_BIAS(header,/sum)
	; HISTORY is added in offset_bias.pro
   message,hdr.filename+' '+dobs+' not found in expfac file.',/info
   RETURN, -1	; Return a failure code
ENDIF



deltime = long(1000.d00*hdr.mid_time)-time(wd)
wt = WHERE((ABS(deltime) LT 100),nw)
IF (nw EQ 0)  THEN BEGIN
   exp_factor  = 1.
   exp_bias    = OFFSET_BIAS(header,/sum)
   exp_sig     = 0.
   nreg        = 0
   wneg=where(deltime LT 0,nneg)
;stop
    IF nneg EQ 0 THEN BEGIN
    	message,'No times after '+trim(hdr.mid_time)+' for '+trim(hdr.mid_date),/info
	return,-3
    ENDIF ELSE BEGIN
    	message,'No times in '+fn+' match input: '+dobs,/info
   
    	RETURN,-2
    ENDELSE
ENDIF
exp_factor = factor(wd(wt(0)))
exp_bias   = bias(wd(wt(0)))
nreg       = nregion(wd(wt(0)))
exp_sig    = sigma[wd[wt[0]]]

IF nreg EQ 0 or nreg EQ 97 THEN BEGIN
    nregmsg=' NO EXPOSURE CORRECTION'
    print,'WARNING: ',hdr.filename,' ',dobs,' -- ',nregmsg
ENDIF ELSE BEGIN
    nregmsg=' Nreg='+trim(nreg)
ENDELSE
IF datatype(header) EQ 'STR' THEN BEGIN
   	FXADDPAR,header,'HISTORY',' '+version+', '+TRIM(exp_factor)+','+nregmsg+', Sig='+strcompress(TRIM(STRING(exp_sig)))
	FXADDPAR, header, 'HISTORY', ' Bias ('+strcompress(trim(string(exp_bias)))+') from '+strcompress(fn)+' (wr.'+strcompress(filedate)+')'
ENDIF

RETURN,0
END
