##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
##Persistence_Get_Dark_Times
##
##PURPOSE:
##To get the times from the header of the darks used and return an array 
##with the time relative to the first dark
##
##INPUTS:
##  FileList: str
##    The full path to the list of the darks
##  FirstDark: int
##    The index of the first dark to be used
##  LastDark: int
##    The index of the last dark to be included
##OUTPUTS:
##  Times: int array
##    The times that each dark were taken relative to 0 at the first
##
from ReadCol import *
import pdb
from HxRG_Class import *
from numpy import *

def Persistence_Get_Dark_Times(FileList, FirstDark=1, LastDark=20):
  
  ##READ THE FILES IN FROM FILELIST
  DarkFiles=ReadFileList(FileList)
  if LastDark !=0:
     DarkFiles = DarkFiles[FirstDark:LastDark]
  NumDarkFiles = len(DarkFiles)

  #Get the initial time in seconds
  DarkHxRG = HxRG_C(FitsFileName=DarkFiles[0])
  DarkHxRG.Get_Raw_Header(DarkFiles[0])
  InitialTime = 3600.*float(DarkHxRG.Hour)+60.*float(DarkHxRG.Minutes)+\
                float(DarkHxRG.Seconds)
 
  #Form an array for the times
  Times = zeros(NumDarkFiles,int16)
  del DarkHxRG

  #Get all of the times
  for DarkNum, DarkFileName in \
  zip(arange(NumDarkFiles-1)+1, DarkFiles[1:LastDark]):
    DarkHxRG = HxRG_C(FitsFileName=DarkFileName)
    DarkHxRG.Get_Raw_Header(DarkFileName)
    ThisTime = 3600.*float(DarkHxRG.Hour)+60.*float(DarkHxRG.Minutes)+\
               float(DarkHxRG.Seconds)

    #Account for rollover of midnight 0 Hours - 23 Hours is negative
    RelTime = ThisTime-InitialTime
    if RelTime < 0 :
       ThisTime = ThisTime+24*3600.
       RelTime = ThisTime-InitialTime
    Times[DarkNum] = RelTime
    del DarkHxRG

  return Times

##Persistence_Get_Dark_Times
##
##PURPOSE:
##To get the times of individual reads from the header of the darks used and 
##return an array with the time relative to the first dark
##
##INPUTS:
##  FileList: str
##    The full path to the list of the darks
##  FirstDark: int
##    The index of the first dark to be used
##  LastDark: int
##    The index of the last dark to be included
##OUTPUTS:
##  Times: int array
##    The times that each dark were taken relative to 0 at the first
##
def Persistence_Get_Dark_Read_Times(FileList, FirstDark=1, LastDark=20):

  ##READ THE FILES IN FROM FILELIST
  DarkFiles=ReadFileList(FileList)
  if LastDark !=0:
     DarkFiles = DarkFiles[FirstDark:LastDark]
  NumDarkFiles = len(DarkFiles)

  #Get the initial time in seconds
  DarkHxRG = HxRG_C(FitsFileName=DarkFiles[0])
  DarkHxRG.Get_Raw_Header(DarkFiles[0])
  InitialStartTime = 3600.*float(DarkHxRG.Hour)+60.*float(DarkHxRG.Minutes)+\
                float(DarkHxRG.Seconds)
  NReadsPerDark = DarkHxRG.NReads

  #Form an array for the times
  Times = zeros(NumDarkFiles*(NReadsPerDark-1),float32)
  del DarkHxRG

  #Get all of the times
  for DarkNum, DarkFileName in \
  zip(arange(NumDarkFiles)+1, DarkFiles[0:LastDark]):
    DarkHxRG = HxRG_C(FitsFileName=DarkFileName)
    DarkHxRG.Get_Raw_Header(DarkFileName)
    ThisStartTime = 3600.*float(DarkHxRG.Hour)+60.*float(DarkHxRG.Minutes)+\
                    float(DarkHxRG.Seconds)

    #Account for rollover of midnight 0 Hours - 23 Hours is negative
    RelStartTime = ThisStartTime-InitialStartTime
    if RelStartTime < 0 :
       ThisStartTime = ThisStartTime+24*3600.
       RelStartTime = ThisStartTime-InitiaStartlTime
    for ReadNum in arange(NReadsPerDark-1):
      print RelStartTime+\
                                                 ReadNum*DarkHxRG.FrameTime
      Times[(DarkNum-1)*(NReadsPerDark-1)+ReadNum] = RelStartTime+\
                                                 ReadNum*DarkHxRG.FrameTime
    del DarkHxRG

  return Times

