FUNCTION read_exp_factor,tel,date, DIR=dir, LOUD=loud
;+
; $Id: read_exp_factor.pro,v 1.9 2011/01/25 17:03:03 nathan Exp $
; NAME:
;	READ_EXP_FACTOR
;
; PURPOSE:
;	This function reads the exposure factor file for a given date
;
; CATEGORY:
;	EXPFAC
;
; CALLING SEQUENCE:
;	Result = READ_EXP_FACTOR(Date)
;
; INPUTS:
;	Tel:		Telescope (string: 'c1','c2','c3')
;	Date:		Date for which the exposure factors are wanted, either
;			in YYMMDD, MJD, or CDS time structure formate
; Optional Inputs:
;   	DIR=	directory to search instead of $NRL_LIB/idl/expfac/data
;   	/LOUD	Print filename being read
;
; OUTPUTS:
;	Result:		0 = Successful read
;			-1 = File not found
;
; COMMON BLOCKS:
;	EXP_FACTOR_ARRAY:	The exposure factor information for a given date.
;
; PROCEDURE:
;	Called by GET_EXP_FACTOR
;
;	The file for the image date specified in the image header is 
;	read into the common block.
;  
;	The format in the exposure factor file is:
;      fname       tsnnnnnn.fts    (a12)  
;      factor      number          (f10.6)  
;      bias        number          (f10.1)  
;      date        YYMMDD          (a6)  
;      time        SEC.MM          (12.2)  
;      Filter      string          (a12)  
;      Polarizer   string          (a12)  
;      Wavelength  NNNN.NNNN       (a9)  
;      Nregion     NNNN            (i4)
;      Sigma       NNNN.NNNN       (f10.2)
;
;	Checks for duplicate entries and only saves the last one read in.
;
; MODIFICATION HISTORY:
; $Log: read_exp_factor.pro,v $
; Revision 1.9  2011/01/25 17:03:03  nathan
; comments
;
; Revision 1.8  2011/01/18 22:57:47  nathan
; fix syntax and add /loud
;
; Revision 1.7  2011/01/03 21:56:49  nathan
; From $NRL_LIB/lasco/expfac, Added DIR= and COMMON write_exp
; (to incorporate functionality of read_exp_factor_data3.pro)
;
; 	Written by:	RA Howard, NRL, 21 September 1997
;	6 October 1997	RAH, Split read away from GET_EXP_FACTOR
;	20 Feb 98	RAH, added fits filenames to the common block
;			and changed the variable name to be nregion instead
;			of nzero
;	22 Feb 98	RAH, check for duplicate entries, and permit any format
;			for date
; @(#)read_exp_factor.pro	1.10 10/02/99 :LASCO IDL LIBRARY
;	 4 Dec 01 	Use FILEPATH for filename
;	17 Sep 02	Add filedate to common block, from file_date_mod
;       K Battams 051021    Add ,/swap_if_little_endian keyword to OPEN calls
;	N.Rich, 2009-05-12  Use brackets for sigma array so not confused with sigma.pro
;   	N.Rich, 2010-11-05  Added DIR= and COMMON write_exp (to incorporate functionality of read_exp_factor_data3.pro)
;
; %W%, %H% :LASCO IDL LIBRARY
;
; NOTE: this pro is committed in dev/lasco/expfac; copy to lasco/idl/expfac.
;-

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

IF keyword_set(DIR) THEN expfacdir=dir
IF datatype(expfacdir) EQ 'UND' THEN BEGIN
    expfacdir=FILEPATH('data',ROOT_D=getenv('NRL_LIB'), SUBD=['idl','expfac']) 
    message,'Reading expfac correction files from '+expfacdir,/info
    wait,2
ENDIF ;ELSE message,'Reading expfac correction files from '+expfacdir,/info

;  
;  Read in the exposure factor and offset bias data  
;  from the file for the month and store in the common block  
;  
   yymmdd = UTC2YYMMDD(YYMMDD2UTC(Date))
   ;subdir = GETENV('NRL_LIB')+'/lasco/expfac/data/'+STRMID(yymmdd,0,4)+'/'
   ttel = STRLOWCASE(tel)
   fn   = ttel+'_expfactor_'+yymmdd+'.dat'
   filename = FILEPATH(fn,ROOT_D=expfacdir,subd=strmid(yymmdd,0,4))
   ;ff = findfile(subdir+fn)
   ff = file_search(filename)
   IF (ff(0) EQ '')  THEN BEGIN
      message,'File not found: '+filename,/info
      RETURN,-1  		; Return failure
   ENDIF
   ;filename = fn
   telescope = ttel
   ;OPENR,lu,subdir+fn,/GET_LUN
   IF keyword_set(LOUD) THEN message,'Reading '+filename,/info
   OPENR,lu,filename,/GET_LUN,/swap_if_little_endian
;  
;  define all the arrays  
;  
   nsize = 1000     ; allow for 1,000 images in a single day!
   fname = STRARR(nsize)  
   factor   = FLTARR(nsize)  
   bias     = FLTARR(nsize)  
   mjd      = LONARR(nsize)  
   time     = LONARR(nsize)  
   filter   = STRARR(nsize)  
   polar    = STRARR(nsize)  
   waveleng = STRARR(nsize)  
   nregion  = INTARR(nsize)  
   sigma    = FLTARR(nsize)  
;  
;  read in the data until the end of file  
;  
   n = 0  
   fn=''
   dt=''
   fi=''
   po=''
   wl=''
   np=0
   sg=0.
    REPEAT BEGIN  
    	READF,lu,fn,fa,bi,dt,ti,fi,po,wl,np,sg, $
	   FORMAT='(a12,f10.6,f10.1,2x,a6,2x,f10.2,2x,a12,a12,a9,i4,e10.2)'
    	IF n eq 2 and keyword_set(LOUD) THEN BEGIN
	    help,fn,fa,bi,dt,ti,fi,po,wl,np,sg
	    wait,abs(loud)
	ENDIF
       fname(n)   = fn  
       factor[n]  = fa  
       bias(n)    = bi  
       dte        = YYMMDD2UTC(dt)  	; the date is stored as YYMMDD
       mjd(n)     = dte.mjd  		; convert to mjd
       time(n)    = LONG(ti*1000)
       filter(n)  = fi  
       polar(n)   = po  
       waveleng(n)= wl  
       nregion(n) = np
       sigma[n]   = sg
;
;   Check for duplicate entry
;
       IF (n GT 0)  THEN BEGIN
          w = WHERE (time(n) EQ time(0:n-1),nw)
          IF (nw GT 0)  THEN BEGIN	; dup found
message,'found dup. Replacing '+fname[w[0]]+' with '+fname[n], /info
             fname(w(0)) = fname(n)
             factor[w[0]] = factor[n]
             bias(w(0)) = bias(n)
             mjd(w(0)) = mjd(n)
             time(w(0)) = time(n)
             filter(w(0)) = filter(n)
             polar(w(0)) = polar(n)
             waveleng(w(0)) = waveleng(n)
             nregion(w(0)) = nregion(n)
             sigma[w[0]] = sigma[n]
          ENDIF ELSE BEGIN		; dup not found
             n = n+1
          ENDELSE
       ENDIF ELSE n = n+1		; first time through
   ENDREP UNTIL(EOF(lu) OR (n GT 1000))  
   IF (NOT EOF(lu))  THEN BEGIN  
      PRINT,'Warning:  Reading in Exposure factor file ' $ 
            +'ended before end of file'  
   ENDIF  
   FREE_LUN,lu  
;
;  Now close up the arrays to the number of images
;
   n = n-1  
   fname    = fname(0:n)  
   factor   = factor[0:n]  
   bias     = bias(0:n)  
   mjd      = mjd(0:n)  
   time     = time(0:n)  
   filter   = filter(0:n)  
   polar    = polar(0:n)  
   waveleng = waveleng(0:n)  
   nregion  = nregion(0:n)
   sigma    = sigma[0:n]  

filedate = file_date_mod(filename,/date_only)

RETURN,0			; Return Success
END  
