<commit_before>'''oscaar v2.0 
    Module for differential photometry
    Developed by <NAME>, 2011-2013 & minor modifications by <NAME> 
    '''
import numpy as np
import pyfits
from matplotlib import pyplot as plt
from scipy import optimize
from glob import glob

import os
import re
import oscaar
import mathMethods
import IO
oscaarpath = os.path.dirname(os.path.abspath(oscaar.__file__))
oscaarpathplus = os.path.join(oscaarpath,'extras')

class dataBank:
    '''
        Methods for storing information from each star in Python dictionaries.
        
        Core Developer: <NAME>
        '''
    def __init__(self):
        '''
            Run oscaar.parseRegionsFile() to get the inital guesses for the 
            initial centroids of the stars from the DS9 regions file, create
            dictionaries in which to store all of the data collected
            for each star. Allocate the memory for these arrays wherever possible.
            Parse the init.par file to grab the paths and initial parameters for 
            the run.
            INPUTS: None.
            '''

        self.dict = {}
        self.parseInit()
        
        self.flatPath = self.dict["flatPath"]
        self.rawRegionsList = self.dict["regPaths"]
        self.ingress = self.dict["ingress"]
        self.egress = self.dict["egress"]
        self.apertureRadii = self.dict["apertureRadius"]
        self.trackingZoom = self.dict["trackingZoom"]
        self.ccdGain = self.dict["ccdGain"]
        self.trackPlots = self.dict["trackPlots"]
        self.photPlots = self.dict["photPlots"]
        self.smoothConst = self.dict ["smoothConst"]
        self.darksPath = self.dict["darksPath"]
        self.imagesPaths = self.dict["imagesPaths"]
        self.timeKeyword = self.dict["timeKeyword"]
        
        if self.timeKeyword == 'JD': 
            self.convertToJD = lambda x: x
        elif self.timeKeyword == 'DATE-OBS':
            self.convertToJD = mathMethods.ut2jdSplitAtT
        
        assert len(self.imagesPaths) > 1, 'Must have at least two data images'
        if self.flatPath != '':
            self.masterFlat = pyfits.getdata(self.flatPath)
            self.masterFlatPath = self.flatPath
        else:
            print 'Using an isotropic ("placebo") master-flat (array of ones)'
            dim1,dim2 = np.shape(pyfits.getdata(self.imagesPaths[0]))
            self.masterFlat = np.ones([dim1,dim2])
        self.allStarsDict = {}
        
        self.regionsFileList, self.regionsFITSrefsList = self.parseRawRegionsList(self.rawRegionsList)
        init_x_list,init_y_list = self.parseRegionsFile(self.regionsFileList[0])
        zeroArray = np.zeros_like(self.imagesPaths,dtype=np.float32)
        self.times = np.zeros_like(self.imagesPaths,dtype=np.float64)
        self.keys = []
        self.targetKey = '000'

        Nradii = len(self.apertureRadii)
        
        for i in range(0,len(init_x_list)):

            self.allStarsDict[str(i).zfill(3)] = {'x-pos':np.copy(zeroArray), 'y-pos':np.copy(zeroArray),\
                'rawFlux':[np.copy(zeroArray) for j in range(Nradii)], 'rawError':[np.copy(zeroArray) for j in range(Nradii)],'flag':False,\
                'scaledFlux':[np.copy(zeroArray) for j in range(Nradii)], 'scaledError':[np.copy(zeroArray) for j in range(Nradii)], 'chisq':np.zeros_like(self.apertureRadii)}
            self.allStarsDict[str(i).zfill(3)]['x-pos'][0] = init_x_list[i]
            self.allStarsDict[str(i).zfill(3)]['y-pos'][0] = init_y_list[i]
            self.keys.append(str(i).zfill(3))   
    def getDict(self):
        '''Return master dictionary of all star data'''
        return self.allStarsDict
    
    def centroidInitialGuess(self,expNumber,star):
        if expNumber == 0:
            est_x = self.allStarsDict[star]['x-pos'][0]  ## Use DS9 regions file's estimate for the 
            est_y = self.allStarsDict[star]['y-pos'][0]  ##    stellar centroid for the first exposure
        elif self.imagesPaths[expNumber] in self.regionsFITSrefsList:
            refIndex = self.regionsFITSrefsList.index(self.imagesPaths[expNumber])
            init_x_list, init_y_list = self.parseRegionsFile(self.regionsFileList[refIndex])
            est_x = init_x_list[int(star)]
            est_y = init_y_list[int(star)]
        else: 
            est_x = self.allStarsDict[star]['x-pos'][expNumber-1]    ## All other exposures use the
            est_y = self.allStarsDict[star]['y-pos'][expNumber-1]    ##    previous exposure centroid as estimate
        return est_x, est_y
    
    def storeCentroid(self,star,exposureNumber,xCentroid,yCentroid):
        '''Store the centroid data collected by oscaar.trackSmooth()
            INPUTS: star - Key for the star for which the centroid has been measured
            
            exposureNumber - Index of exposure being considered
            
            xCentroid - x-centroid of the star
            
            yCentroid - y-centroid of the star
            '''
        self.allStarsDict[star]['x-pos'][exposureNumber] = xCentroid
        self.allStarsDict[star]['y-pos'][exposureNumber] = yCentroid   
    
    def storeFlux(self,star,exposureNumber,rawFlux,rawError):
        '''Store the flux and error data collected by oscaar.phot()
            INPUTS: star - Key for the star for which the centroid has been measured
            
            exposureNumber - Index of exposure being considered
            
            rawFlux - flux measured, to be stored
            
            rawError - photon noise measured, to be stored
            '''
        self.allStarsDict[star]['rawFlux'][exposureNumber] = rawFlux
        self.allStarsDict[star]['rawError'][exposureNumber] = rawError
    def storeFluxes(self,star,exposureNumber,rawFluxes,rawErrors):
        '''Store the flux and error data collected by oscaar.phot()
            INPUTS: star - Key for the star for which the centroid has been measured
            
            exposureNumber - Index of exposure being considered
            
            rawFlux - flux measured, to be stored
            
            rawError - photon noise measured, to be stored
            '''
        for apertureRadiusIndex in range(len(self.apertureRadii)):
            self.allStarsDict[star]['rawFlux'][apertureRadiusIndex][exposureNumber] = rawFluxes[apertureRadiusIndex]
            self.allStarsDict[star]['rawError'][apertureRadiusIndex][exposureNumber] = rawErrors[apertureRadiusIndex]
    
    def getPaths(self):
        '''Return the paths to the raw images used'''
        return self.imagesPaths
    
    def getFluxes(self,star):
        '''Return the fluxes for one star, where the star parameter is the key for the
            star of interest.'''
        return self.allStarsDict[star]['rawFlux']
    
    def getErrors(self,star):
        '''Return the errors for one star, where the star parameter is the key for the
            star of interest.'''
        return self.allStarsDict[star]['rawError']
    
    def storeTime(self,expNumber):
        '''Store the time in JD from the FITS header.
            INPUTS: exposureNumber - Index of exposure being considered
            
            time - Time as read-in from the FITS header
            '''
        #try:
        timeStamp = pyfits.getheader(self.getPaths()[expNumber])[self.timeKeyword]
        #except KeyError: 
        #    print 'Input Error: The Exposure Time Keyword indicated in observatory.par is not a valid key: ',self.timeKeyword
        #finally: 
        self.times[expNumber] = self.convertToJD(timeStamp)
    
    def getTimes(self):
        '''Return all times collected with dataBank.storeTime()'''
        return self.times
    
    def getFlag(self,star):
        '''Return the flag for the star with key "star" '''
        return self.allStarsDict[star]['flag']
    <commit_msg>Updated docstrings for sphinx<commit_after>'''oscaar v2.0 
    Module for differential photometry
    Developed by <NAME>, 2011-2013 & minor modifications by <NAME> 
    '''
import numpy as np
import pyfits
from matplotlib import pyplot as plt
from scipy import optimize
from glob import glob

import os
import re
import oscaar
import mathMethods
import IO
oscaarpath = os.path.dirname(os.path.abspath(oscaar.__file__))
oscaarpathplus = os.path.join(oscaarpath,'extras')

class dataBank:
    '''
        Methods for easily storing and accessing information from the entire 
        differential photometry process with OSCAAR.
        
        Core Developer: <NAME>
        '''
    def __init__(self):
        '''
            Get the inital guesses for the initial centroids of the stars from the DS9 regions file, 
            create dictionaries in which to store all of the data collected for each star, and for each 
            aperture radius. Allocate the memory for these arrays wherever possible. Parse the init.par 
            file to grab the paths and initial parameters for the run.
            
            Parameters
			----------
			    None
			    
			Returns
			-------
				Instance of the ``dataBank`` object.
            '''

        self.dict = {}
        self.parseInit()
        
        self.flatPath = self.dict["flatPath"]
        self.rawRegionsList = self.dict["regPaths"]
        self.ingress = self.dict["ingress"]
        self.egress = self.dict["egress"]
        self.apertureRadii = self.dict["apertureRadius"]
        self.trackingZoom = self.dict["trackingZoom"]
        self.ccdGain = self.dict["ccdGain"]
        self.trackPlots = self.dict["trackPlots"]
        self.photPlots = self.dict["photPlots"]
        self.smoothConst = self.dict ["smoothConst"]
        self.darksPath = self.dict["darksPath"]
        self.imagesPaths = self.dict["imagesPaths"]
        self.timeKeyword = self.dict["timeKeyword"]
        
        if self.timeKeyword == 'JD': 
            self.convertToJD = lambda x: x
        elif self.timeKeyword == 'DATE-OBS':
            self.convertToJD = mathMethods.ut2jdSplitAtT
        
        assert len(self.imagesPaths) > 1, 'Must have at least two data images'
        if self.flatPath != '':
            self.masterFlat = pyfits.getdata(self.flatPath)
            self.masterFlatPath = self.flatPath
        else:
            print 'Using an isotropic ("placebo") master-flat (array of ones)'
            dim1,dim2 = np.shape(pyfits.getdata(self.imagesPaths[0]))
            self.masterFlat = np.ones([dim1,dim2])
        self.allStarsDict = {}
        
        self.regionsFileList, self.regionsFITSrefsList = self.parseRawRegionsList(self.rawRegionsList)
        init_x_list,init_y_list = self.parseRegionsFile(self.regionsFileList[0])
        zeroArray = np.zeros_like(self.imagesPaths,dtype=np.float32)
        self.times = np.zeros_like(self.imagesPaths,dtype=np.float64)
        self.keys = []
        self.targetKey = '000'

        Nradii = len(self.apertureRadii)
        
        for i in range(0,len(init_x_list)):

            self.allStarsDict[str(i).zfill(3)] = {'x-pos':np.copy(zeroArray), 'y-pos':np.copy(zeroArray),\
                'rawFlux':[np.copy(zeroArray) for j in range(Nradii)], 'rawError':[np.copy(zeroArray) for j in range(Nradii)],'flag':False,\
                'scaledFlux':[np.copy(zeroArray) for j in range(Nradii)], 'scaledError':[np.copy(zeroArray) for j in range(Nradii)], 'chisq':np.zeros_like(self.apertureRadii)}
            self.allStarsDict[str(i).zfill(3)]['x-pos'][0] = init_x_list[i]
            self.allStarsDict[str(i).zfill(3)]['y-pos'][0] = init_y_list[i]
            self.keys.append(str(i).zfill(3))   
    def getDict(self):
        '''Return dictionary of all star data called ``allStarsDict`.'''
        return self.allStarsDict
    
    def centroidInitialGuess(self,expNumber,star):
    	'''
    	
    	Gets called for each exposure. If called on the first exposure, it will return
    	the intial centroid guesses input by the DS9 regions file. If any other image
    	and only one regions file has been submitted, it will return the previous centroid
    	as the initial guess for subsequent exposures. If multiple regions files have been
    	submitted, it will return the initial guesses in those regions files when the image path
    	with index ``expNumber`` is equivalent to the path stored for that regions file's 
    	"Reference FITS image".
    	
    	Parameters
	    ----------
	    expNumber : int
	        The index of the exposure currently being analyzed. The image gets called
	        by its index from the list of image paths returned by getPaths().
	    star : str
	        The key from ``allStarsDict`` that corresponds to the star for which you'd
	        like a centroid initial guess.
	
	    Returns
	    -------
	    est_x : float
	        Estimated centroid position of the star ``star`` along the *x*-axis of pixels for
	        exposure index ``expNumber``
	    
	    est_y : float
	        Estimated centroid position of the star ``star`` along the *y*-axis of pixels for
	        exposure index ``expNumber``
    	'''
        if expNumber == 0:
            est_x = self.allStarsDict[star]['x-pos'][0]  ## Use DS9 regions file's estimate for the 
            est_y = self.allStarsDict[star]['y-pos'][0]  ##    stellar centroid for the first exposure
        elif self.imagesPaths[expNumber] in self.regionsFITSrefsList:
            refIndex = self.regionsFITSrefsList.index(self.imagesPaths[expNumber])
            init_x_list, init_y_list = self.parseRegionsFile(self.regionsFileList[refIndex])
            est_x = init_x_list[int(star)]
            est_y = init_y_list[int(star)]
        else: 
            est_x = self.allStarsDict[star]['x-pos'][expNumber-1]    ## All other exposures use the
            est_y = self.allStarsDict[star]['y-pos'][expNumber-1]    ##    previous exposure centroid as estimate
        return est_x, est_y
    
    def storeCentroid(self,star,exposureNumber,xCentroid,yCentroid):
        '''Store the centroid data collected by oscaar.trackSmooth()
            Parameters
            ----------
            star : str
            	Key for the star for which the centroid has been measured
            
            exposureNumber : int
            	Index of exposure being considered
            
            xCentroid : float
            	*x*-centroid of the star
            
            yCentroid : float
            	*y*-centroid of the star
            '''
        self.allStarsDict[star]['x-pos'][exposureNumber] = xCentroid
        self.allStarsDict[star]['y-pos'][exposureNumber] = yCentroid   
    
    def storeFlux(self,star,exposureNumber,rawFlux,rawError):
        '''Store the flux and error data collected by oscaar.phot()
			Parameters
			----------
			star : str
				Key for the star from the ``allStarsDict`` dictionary
			
			exposureNumber : int
				Index of exposure being considered
			
            rawFlux : float
            	flux measured, to be stored
            
            rawError : 
            	photon noise measured, to be stored
            '''
        self.allStarsDict[star]['rawFlux'][exposureNumber] = rawFlux
        self.allStarsDict[star]['rawError'][exposureNumber] = rawError
    def storeFluxes(self,star,exposureNumber,rawFluxes,rawErrors):
		'''Store the flux and error data collected by oscaar.phot()
			Parameters
			----------
			star : str
				Key for the star from the ``allStarsDict`` dictionary
			
			exposureNumber : int
				Index of exposure being considered
			
			rawFluxes : list of floats
				flux measured, to be stored
			
			rawErrors : list of floats
				photon noise measured, to be stored
			'''
        for apertureRadiusIndex in range(len(self.apertureRadii)):
            self.allStarsDict[star]['rawFlux'][apertureRadiusIndex][exposureNumber] = rawFluxes[apertureRadiusIndex]
            self.allStarsDict[star]['rawError'][apertureRadiusIndex][exposureNumber] = rawErrors[apertureRadiusIndex]
    
    def getPaths(self):
        '''Return the paths to the raw images to be used'''
        return self.imagesPaths
    
    def getFluxes(self,star):
    	'''
    		Return list of fluxes for the star with key ``star``
    		
			Parameters
			----------
			star : str
				Key for the star from the ``allStarsDict`` dictionary
			
			Returns
			_______
			fluxes : list
				List of fluxes for each aperture radius
		'''
        return self.allStarsDict[star]['rawFlux']
    
    def getErrors(self,star):
        '''Return the errors for one star, where the star parameter is the key for the
            star of interest.'''
        return self.allStarsDict[star]['rawError']
    
    def storeTime(self,expNumber):
        '''Store the time in JD from the FITS header.
            INPUTS: exposureNumber - Index of exposure being considered
            
            time - Time as read-in from the FITS header
            '''
        #try:
        timeStamp = pyfits.getheader(self.getPaths()[expNumber])[self.timeKeyword]
        #except KeyError: 
        #    print 'Input Error: The Exposure Time Keyword indicated in observatory.par is not a valid key: ',self.timeKeyword
        #finally: 
        self.times[expNumber] = self.convertToJD(timeStamp)
    
    def getTimes(self):
        '''Return all times collected with dataBank.storeTime()'''
        return self.times
    
    def getFlag(self,star):
        '''Return the flag for the star with key "star" '''
        return self.allStarsDict[star]['flag']
    
{'fraction_non_alphanumeric': 0.10008608321377331, 'fraction_numerical': 0.004533715925394548, 'mean_word_length': 2.385003885003885, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3664867', 'n_tokens_mistral': 5288, 'n_tokens_neox': 5015, 'n_words': 1358}