##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
##MedianOfAllReads.py
##
##PURPOSE: 
##  To form the median of a group of files, read by read.  The median is taken 
##  as a simple means of rejecting cosmic rays and trying to determine the 
##  best values of a pixel in exposures for which all of the pixels should have the
##  same values.
##
##  The default behavior is to search for files matching the Object, NDrops, 
##  NReads, NGroups, etc.  Optionally, [FileList] can be specified as a keyword.
##  This file should contain a list of files to include in the median.
##
##INPUTS: 
##      DetStr       - 'H1RG-022','H2RG-32-147', or 'H4RG-10-007'
##	ElecSt       - 'ASIC' or 'LEACH'
##      Date         - A string containing the date in the path (ex. '07Nov16')
##      NReads       - A string with the number of reads in the dark images
##	Row	     - The Row of the pixel desired
##	Col	     - The Column of the pixel desired
##
##KEYWORDS:
##	FirstFile     - The index of the first read to be plotted
##	LastFile      - The index of the last read to be plotted
##	UseRef	      - A Boolean; 0- Don't subtract reference  1- Do subtract
##	DarkSub       - A Boolean; 0- Don't subtract dark frame 1- Do subtract
##      BiasSub       - A Boolean; 0- Don't subtract bias frame from all reads
##                                 1- Do subtract bias frame from all reads
##      ObjectText    - An extra string to identify the cadence of LEACH files
##      Object        - A string specifiying the type of object for which the 
##			median should be formed ('Dark', 'Flat', etc.)
##      FileList      - A file containing a list of files to include in the median
##CALLING SEQUENCE:
##	run MedianOfAllReads.py [HxRG] [ElecSt] [Date] [NReads] [Row] [Col]
##
##EXAMPLES:
##________
##H2RG 
##  run MedianOfAllReads.py 'H2RG-32-147' 'ASIC' '07Dec18' 30 
##H1RG: 
##  run MedianOfAllReads.py 'H1RG-022' 'ASIC' '07Nov16' 100 --Object='Dark'
##  run MedianOfAllReads.py 'H1RG-022' 'ASIC' '07Nov16' 100 100 200 
##
##/////////////////////////////////////////////////////////////////////////
##SETUP
execfile('/afs/slac/u/ki/lances/python/python_H2RG/HxRG_Setup.py')
from HxRG_Class import *

#Get the filename and options from command line
DetStr       = sys.argv[1]
ElecStr      = sys.argv[2]
Date	     = sys.argv[3]
NReads       = sys.argv[4]

##PARSE KEYWORDS
keywords    = ['FirstFile=','LastFile=','UseRef='    , 'DarkSub=',
               'BiasSub='  ,'Object='  ,'ObjectText=', 'FileList=']
FirstFile   = 0
LastFile    = 0
UseRef      = 0
DarkSub     = 0
BiasSub     = 1
Object      = ''
ObjectText  = ''
FileList    = ''

opts, extraparams = getopt.getopt(sys.argv[5:],'',keywords)
for o,p in opts:
  if o in ['--FirstFile']:
    FirstFile = int(p)
  elif o in ['--LastFile']:
    LastFile  = int(p)
  elif o in ['--UseRef']:
    UseRef  = int(p)
  elif o in ['--DarkSub']:
    DarkSub = int(p)
  elif o in ['--BiasSub']:
    BiasSub = int(p)
  elif o in ['--Object']: 
    Object  = p 
  elif o in ['--ObjectText']:
    ObjectText = p
  elif o in ['--FileList']:    
    FileList= p  

#Form the class that will contain the necessary info
HxRG = HxRG_C(DetStr=DetStr, ElecStr=ElecStr)
HxRG.Get_Date(Date)
HxRG.Specify_Object(Object)

##FIND THE DARK FILES THAT MATCH THE SEQUENCE IN MIND
if FileList == '':
   FilesForMed  = sort_files_by_date(HxRG.ObjectDir+Object+'*'+'_'+NReads+'*.fits')
   if LastFile == 0 : LastFile = len(FilesForMed)
   NumFilesForMed = LastFile-FirstFile
else:
   FilesForMed  = ''
  
##Begin the file 
