;Basic Structure for the analysis of the H4RG full frame images.
;Contains directories and keywords to be used in the 
;
;KEYWORDS:
;   ROOTDIR: 
;         The Directory holding the directories of Raw files
;   DATE:
;         The Date during which they were taken.  Also the subdirectory that 
;         the files are in.
;   REDUCEDDIR:
;         The directory where the processed images and plots will be placed in.
;   BLOCKLENGTH:
;         An offset for files that were written improperly.  The readu 
;         function reads the values with this offset in place.
;   OBJECTNAME:
;         The string that indicates what files are being sought.  The filenames
;         should begin with this string ('Dark', 'Flat', 'M16', etc.).
;   NREADS:
;         The number of reads (NAXIS3) that are present in the file.  This
;         is also a string in the filename that will be used as a regular
;         expression in searching for matched files.
;   THISFILTER:
;         The filter through which the files were taken. ('I', 'G', or 'Y')
;   OVERRIDE: 
;         Do the processing even if the output file already exists
;
;   USEBADMASK:
;	  Apply the mask that contains the leaky, hot and dead pixels
;   TVFLAG:     0 - Default, Don't plot anything
;               1 - Plot to the X window
;               2 - Plot to postscript
;               3 - Plot to a .png
;
;   TVPPT:      Tv Plot Pixel Type - Designates the type of pixel to be plotted 
;               1 - Plot all slopes
;		2 - Plot cosmic rays 
;		3 - Plot the negative voltage spikes 
;               4 - Plot the first read cosmics or potentially hot pixels
;
;*************************************************************************
;Make this a common block
Common KeyParams, KeyStr

;Get Path delimiter based on Windows or Unix
PathDelim = Path_Sep()

;Directory and FileName Keywords
If N_Elements(RootDir)    eq 0 then RootDir = '/nfs/slac/g/ki/ki03/lances/H4RG-10-007/'
If N_Elements(Month)      eq 0 then Month   = 'Apr'
If N_Elements(Year)       eq 0 then Year    = '07'
If N_Elements(Date)       eq 0 then Date='07Apr30'
If N_Elements(ReducedDir) eq 0 then begin 
   If stregex(RootDir, 'Reduced', /boolean) then begin
     ReducedDir=RootDir
   EndIf else begin
     ReducedDir = $
     RootDir+PathDelim+'Reduced'+PathDelim+Date+PathDelim
   EndElse
EndIf

;Make the directories if this is the first time running
If not keyword_set(MakeDirs) then MakeDirs = 0

;Once the above directories are set, these follow
DarkDir    = ReducedDir+PathDelim+'Darks'+PathDelim
FlatDir    = ReducedDir+PathDelim+'Flats'+PathDelim
BadPixDir  = ReducedDir+PathDelim+'BadPix'+PathDelim
LeakyDir   = ReducedDir+PathDelim+'LeakyPix'+PathDelim
HotDir     = ReducedDir+PathDelim+'HotPix'+PathDelim
DeadDir    = ReducedDir+PathDelim+'DeadPix'+PathDelim
CosmicsDir = ReducedDir+PathDelim+'Cosmics'+PathDelim
FourierDir = ReducedDir+PathDelim+'FourierSpectra'+PathDelim
PlotDir    = '/nfs/slac/g/ki/ki09/lances/Plots/'

If MakeDirs then begin
   File_Mkdir, DarkDir    & File_MkDir, FlatDir & File_MkDir, LeakyDir
   File_Mkdir, HotDir     & File_MkDir, DeadDir & File_MkDir, CosmicsDir
   File_Mkdir, FourierDir & File_Mkdir, PlotDir
Endif

;Array for the Nights and the Filters
ObsDates = ['07Apr25', '07Apr26', '07Apr28', '07Apr30']
FilterSet = ['G','I','Y']

;The temporary masks that count th number of times a pixel was bad
DeadPixelMaskPath  = DeadDir+'DeadPixelMap_Darks.fits'
LeakyPixelMaskPath = LeakyDir+'LeakyPixelMaskFor_13_Flat_I_Files__2Reads.fits'
HotPixelMaskPath   = HotDir+'HotPixelMask_13_Flat_I_Files__2Reads.fits'
DarkSlopeDir       = '/nfs/slac/g/ki/ki09/lances/Reduced/DarkSlopes/'

;The master pixel masks that contain either 0's or 1's
DeadMasterDir       = '/nfs/slac/g/ki/ki09/lances/Reduced/DeadPix/'
MasterDeadMapPath   = DeadMasterDir+'DeadMask.fits' 

LeakyMasterDir       = '/nfs/slac/g/ki/ki09/lances/Reduced/LeakyPix/'
LeakyFileList        = LeakyMasterDir+'LeakyFiles.txt'
LeakyValsPath	     = LeakyMasterDir+'LeakyPixelValues.fits'
MasterLeakyMapPath   = LeakyMasterDir+'LeakyMask.fits'
MasterLeakyNNMapPath = LeakyMasterDir+'LeakyNNMask.fits'
HotMasterDir         = '/nfs/slac/g/ki/ki09/lances/Reduced/HotPix/'
HotMasterMapPath     = HotMasterDir+'HotMask.fits'
DarkSlopeList 	     = DarkSlopeDir + 'DarkSlope.lst'

;Parameters that are modified at top level script
If N_Elements(Naxis) 		eq 0 	then Naxis = 3
If N_Elements(Naxis1) 		eq 0  	then Naxis1 = (Naxis2 = 4096)  
If N_Elements(Offset)	 	eq 0    then Offset = 0
If N_Elements(ObjectName) 	eq 0  	then ObjectName = 'Object'
If N_Elements(NReads) 		eq 0  	then NReads  = 20
If N_Elements(NDrops) 		eq 0	then NDrops  = 0
If N_Elements(NGroups) 		eq 0 	then NGroups = 1
If N_Elements(FirstRead) 	eq 0  	then FirstRead = 0
If N_Elements(LastRead) 	eq 0  	then LastRead = NReads - 1
If N_Elements(ThisFilter) 	eq 0  	then ThisFilter = 'I'
If N_Elements(LAT)		eq 0 	then LAT = 31.968889
If N_Elements(ITIME)		eq 0	then ITIME = 1.
If N_Elements(ThisNDrops) 	eq 0 	then ThisNDrops = 0
If N_Elements(Rem60Hz) 		eq 0  	then Rem60Hz = 0
If N_Elements(SubtractDark)	eq 0 	then SubtractDark = 0
If N_Elements(OverRide) 	eq 0  	then OverRide = 0
If N_Elements(AllFiles) 	eq 0  	then AllFiles = 1
If N_Elements(FilterStr) 	eq 0  	then FilterStr = ''
If N_Elements(MaskFiles) 	eq 0 	then MaskFiles = 'Darks'
If N_Elements(FlatFielded) 	eq 0  	then FlatFielded = 1
If N_Elements(AvgEntireFrame) 	eq 0  	then AvgEntireFrame = 0
If N_Elements(RemovalMode) 	eq 0 	then RemovalMode = 1
If N_Elements(FromMask) 	eq 0  	then FromMask = 0
If N_Elements(TvFlag) 		eq 0  	then TvFlag = 0
If N_Elements(TvPPT)		eq 0    then TvPPT  = 0
If N_Elements(Avgs) 		eq 0  	then Avgs = 0
If N_Elements(BiasSub) 		eq 0	then BiasSub = 1
If N_Elements(UseBadMask) 	eq 0 	then UseBadMask = 1
If N_Elements(RejectPersist)    eq 0 	then RejectPersist = 0
If N_Elements(Verbose)		eq 0	then Verbose = 0
If N_Elements(NOUT)		eq 0	then NOUT = 32
If N_Elements(WindowMode)	eq 0 	then WindowMode = 0
;Some reasonable tags should appear together
If Rem60Hz then FilterStr = '_60HzRemoved'

;Values for the H4RG
Ref          = 8		;Number of reference pixels in a column and row
Ch0W         = 128		;Width of Channel Zero

;Get the screen size in case the plots are requested
ScreenSize = Get_Screen_Size()
XScreenSize = ScreenSize(0)
YScreenSize = ScreenSize(1)

;Structure to Hold these things
KeyStr = { $
	PathDelim: 		PathDelim,	$
	RootDir: 		RootDir, 	$
        Date: 			Date, 		$
	Year: 			Year,		$
	Month: 			Month,		$
	ReducedDir: 		ReducedDir, 	$
        PlotDir: 		PlotDir, 	$
        LeakyDir: 		LeakyDir, 	$
        DarkDir: 		DarkDir, 	$
        FlatDir: 		FlatDir, 	$
	DarkSlopeDir: 		DarkSlopeDir, 	$
	MasterDeadMapPath: 	MasterDeadMapPath, $
	MasterLeakyMapPath: 	MasterLeakyMapPath,$
	MasterLeakyNNMapPath: 	MasterLeakyNNMapPath, $
	LeakyValsPath: 		LeakyValsPath, $
	HotMasterMapPath: 	HotMasterMapPath, $
	HotMasterDir: 		HotMasterDir, $
 	ObjectDir:		'', $
        DarkName: 	  	'', $
        DarkKey: 	  	'', $
        FlatName:	  	'', $
	FlatKey: 	  	'', $
	RawFullPath: 	  	'', $
        RawName:	  	'', $
	RawKey:		  	'', $
	WindowMode:		WindowMode, $
	ObjectName:	  	ObjectName, 	$
	NReads:		  	NReads, 	$
	NDrops:		  	NDrops,		$
	NGroups:	  	NGroups,	$
	TotalReads:	  	NReads,		$
	ThisFilter:	  	ThisFilter,	$
	Naxis:		  	Naxis,		$
	Naxis1:	 	  	Naxis1,		$
	Naxis2:		  	Naxis2,		$
	Naxis3:		  	NReads,		$
	Offset:		  	Offset,		$
	NOUT:			NOUT,		$
	Naxis2NoCh0:      	Naxis2-128,	$
	Ref:			Ref,		$
	Ch0W:		  	Ch0W,		$
	BiasSub:	 	BiasSub,	$
	Rem60Hz:	  	Rem60Hz,	$
	Rem60HzStr:	  	'',		$
	RejectPersist:    	RejectPersist,  $
	RejectPersistStr: 	'', $
	UseBadMask:		UseBadMask,	$
	TvFlag:			TvFlag, 	$
	TvPPT:			TvPPT,		$
	Verbose:		Verbose,	$
	FlatFieldStr:		'', 		$
	SubtractDark:		SubtractDark,	$
	DarkSubStr:		'',		$
	SlopeSigThresh: 	3.5,		$
	DarkSlope:      	0B,		$
	RA:			0D,		$
	DEC:			0D,		$
	ZD:			0D,		$
	AZ:			0D,		$
	LST:			0D,		$
	UT:			0D,		$
	HA:			0D,		$
	LAT:			LAT,		$
	AIRMASS:		0D,		$
	ITIME:			ITIME,		$
        CDELT1:			0D,		$
	CDELT2:			0D,		$
	CRVAL1:			0D,		$
	CRVAL2:			0D,		$
	CTYPE1:			'',		$
	CTYPE2:			'',		$
        CRPIX1:			0D,		$
	CRPIX2:			0D,		$	
	DitherFiles:		StrArr(12),	$
	NumDithers:		0B,		$
	RAs:			Intarr(12,3), 	$
	DECs:			Intarr(12,3),	$	
	AirMasses:		Dblarr(12),	$
	UTs:			Dblarr(12),	$
	XOffs:			IntArr(12),	$
	YOffs:			IntArr(12),	$	
	ExpTime:		5.45,		$
	MCDFile:		'',		$
	Time:			'',		$
	OverRide:		OverRide, 	$
	ReadNoiseMax:  		0L, 		$
	LowerLinLim:   		0L,	    	$
	UpperLinLim:    	0L,		$    	
        UpperADCRange:  	0L,		$
        LowerADCRange:  	0L,  		$
        UpperThresh:    	10000, 		$
        LowerThresh:    	-7000, 		$
	UpperADCThreshold: 	0L,		$
	LowerADCThreshold: 	0L		$
}

