;pro h4rg_pipeline
;
;PURPOSE:
;	To Form a tri-color image from a set of darkcurrent frames, flat 
;	fields, sky dithered images, and object dithered images.
;
;INPUTS:
;
;	OBJECTNAME: 
;		The name of the object.  This is included in the file name 
;		as well as the OBJECT keyword of the fitsheader
;	SKYNAME: 
;		The name of the object used for the sky.  This is included 
;		in the filename as well as the OBJECT keyword of the fitsheader.
;
;
pro h4rg_pipeline

 ;Directories
 if not(keyword_set(date)) then date='07Apr30'
 If not(keyword_set(RootDir)) then Rootdir='/Volumes/SEA_DISC/'
 If not(keyword_set(ReducedDir)) then ReducedDir = '~/Desktop/LSST/KPNO/Images/Reduced/'+Date+'/'
 
 cd,rootdir

 ;Block Length for Offset
 If not(keyword_set(BlockLength)) then BlockLength = 0
 If not(keyword_set(skipsky)) then skipsky=0
 If not(keyword_set(skipflat)) then skipflat=0
 If not(keyword_set(ThisFilter)) then ThisFilter = 'I'
 If not(keyword_set(ObjectName)) then ObjectName = 'M63'
 If not(keyword_set(DarkName)) then DarkName = 'Dark'
 If not(keyword_set(SkyName)) then SkyName = 'PG1525-071'
 If not(keyword_set(NReads)) then NReads = 20
 If not(keyword_set(DarkSub)) then DarkSub = 1
 If not(keyword_set(Cadence)) then Cadence = [15,20,30]
 If not(keyword_set(Filters)) then Filters = ['I', 'G','Y']

 ;Loop Through Filters
 for FilterNum = 0, N_elements(Filters) - 1 do begin

   ;STEP 1 - Get Median Darks for each Cadence
   DarkExists = Dark_Median(RootDir = RootDir, Date = '07Apr30', $
	  NReads = Cadence(FilterNum), ThisFilter = Filters(FilterNum),$
	  OverRide=0)
 
   ;STEP 2 - Get Median Flats for the current filter 
   FlatExists = Flat_Median(RootDir = RootDir , Date = Date, $
	  NReads = Cadence(FilterNum), ThisFilter = Filters(FilterNum), $
	  OverRide=0)

   ;STEP 3 - Get the Median Sky
   SkyExists = sky_median(RootDir = RootDir, Date = Date, $
	  NReads = Cadence(FilterNum), ThisFilter = Filters(FilterNum), $
	  SkyName = SkyName, /DivideByFlat, OverRide=0)

   ;STEP 4 - Get the Median Object 
   ObjectExists = object_median(RootDir = RootDir, Date = Date, $
	  NReads = Cadence(FilterNum), ThisFilter = Filters(FilterNum), $
	  ObjectName = ObjectName, $
	  BlockLength = 0, /DivideByFlat, /SkySub, /OverRide)


 endfor

End
