pro PLOT_EXP_FACTOR,Tel,dtea,dteb,mjdout,exp_factors,ps=ps, DIR=dir, LOUD=loud
;
;+
; $Id: plot_exp_factor.pro,v 1.4 2011/01/25 17:03:03 nathan Exp $
; NAME:
;	PLOT_EXP_FACTOR
;
; PURPOSE:
;	This procedure reads all the files for the given date range
;	and plots the exposure time factors
;
; CATEGORY:
;	EXP_FAC
;
; CALLING SEQUENCE:
;	PLOT_EXP_FACTOR,Tel,Dtea
;
; INPUTS:
;	Tel:	The telesope designation (string):  c1, c2, c3, c4/eit
;		The default is all three LASCO telescopes.
;	Dtea:	Gives the date to be processed in one of the following formats:
;			YYMMDD,	6 character string
;			YYYY/MM/DD, 10 character string
;			CDS Date Structure
;			Long Word of the modified julian date
;
; OPTIONAL INPUTS:
;	Dteb:	Gives the final date to be processed in the same format as DteA
;
; KEYWORD PARAMETERS:
;	PS:	If set then the plot is sent to the printer, else it is sent to the current
;		graphics device
;   	DIR= desired input directory (before month); defaults to $NRL_LIB/lasco/expfac/data
;
;
; OPTIONAL OUTPUTS:
;	mjdout 	    Array of time in CDS structure format
;   	exp_factors FLTARR  plotted exp correction factors
;
; RESTRICTIONS:
;
; PROCEDURE:
;
; EXAMPLE:
;
; MODIFICATION HISTORY:
; $Log: plot_exp_factor.pro,v $
; Revision 1.4  2011/01/25 17:03:03  nathan
; comments
;
; Revision 1.3  2011/01/24 19:58:03  nathan
; add plot of smooth median
;
; Revision 1.2  2011/01/18 22:52:14  nathan
; Do not CD to source directory; DIR= specify source directory; extend common
; block; optional output parameters plotted
;
; 	Written by:	R.A. Howard, NRL, 10/7/97
;
; %H% %W% :LASCO IDL LIBRARY
;
; NOTE: this pro is committed in dev/lasco/expfac; copy to lasco/idl/expfac.
;-
;
;  Find out which telescope is requested
;
common exp_factor_array,telescope,filename,fname,factor,bias,mjd, $  
                   time,filter,polar,waveleng,nregion,sigma, filedate
 
ttel = STRLOWCASE(tel)
IF (ttel EQ 'eit')  THEN ttel='c4'
IF (STRPOS('c1c2c3c4',ttel) LT 0)    THEN BEGIN
   PRINT,'ERROR:  Unrecognized telescope'
   RETURN
ENDIF
exp_factors=[-1]
blank = '               '		; a 15 character blank string
;
;  Generate the modified julian date from the input date after
;  finding out what format the input date is in
;
FOR i=2,N_PARAMS() DO BEGIN
    IF (i EQ 2)  THEN date = dtea ELSE date=dteb
    CASE datatype(date) OF
     'STR':  BEGIN
                IF (STRLEN(date) EQ 6) THEN BEGIN
                   d = STRMID(date,0,2)
                   IF (FIX(d) LT 99)  THEN pre='19' ELSE pre='20'
                   d = pre+d+'/'+STRMID(date,2,2)+'/'+STRMID(date,4,2)
                ENDIF ELSE d = date
                mjd = STR2UTC(d)
              END
      'LON':  BEGIN
                 get_utc,mjd
                 mjd.mjd = date
                 mjd.time = 0
              END
      'STC':  mjd = date
    ENDCASE
    IF (i EQ 2)  THEN BEGIN
       mjda = mjd(0)
       mjdb = mjd(0)
    ENDIF ELSE mjdb = mjd(0)
ENDFOR

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

;CD,expfacdir,curr=old
;CD,curr=top
FOR imjd=mjda.mjd,mjdb.mjd DO BEGIN
    ;CD,top
    amjd = mjda
    amjd.mjd = imjd
    dd = UTC2YYMMDD(amjd)
    PRINT,'Processing date = '+dd
    ;CD,STRMID(dd,0,4)
    ok = READ_EXP_FACTOR(tel,dd,LOUD=loud, DIR=expfacdir)
    IF (ok EQ 0)  THEN BEGIN
       IF (exp_factors(0) EQ -1)  THEN BEGIN
          exp_factors = factor
          ef_mjd = mjd
          ef_time = time
       ENDIF ELSE BEGIN
          exp_factors = [exp_factors,factor]
          ef_mjd = [ef_mjd,mjd]
          ef_time = [ef_time,time]
       ENDELSE
    ENDIF
ENDFOR
n = N_ELEMENTS(exp_factors)
mjd = REPLICATE({CDS_INT_TIME},n)
mjd.mjd = ef_mjd
mjd.time = ef_time
mjdout=mjd
IF (KEYWORD_SET(ps))  THEN PS_SETUP,0
sig = STDEV(exp_factors,mn)
w = where(ABS(exp_factors-mn) LT 5*sig,nw)
sig = STDEV(exp_factors(w))
!p.title=tel+' Exposure Time Factors (sigma='+STRING(sig,format='(f8.5)')+')'
UTPLOT,mjd,exp_factors,yrange=[0.98,1.02]
outplot,mjd,smooth(median(exp_factors,7),23,/edge),color=4
!p.title=''
IF (KEYWORD_SET(ps))  THEN PS_SETUP,1
;cd,old
RETURN
END

