Spring2020/PythonPhotometry/Sarah_DiffPhotometry.py
def detrend(photometry, ephotometry, nstars):
    '''
    detrend
    
    detrend the background for each night so we don't have to worry about changes in background noise levels
    
    inputs
    -------
    photometry: list - list of flux values from aperture photometry
    ephotometry: list - list of flux errors from aperture photometry
    nstars: float - number of stars in the field
    
    outputs
    --------
    finalPhot: list - final aperture photometry of sources with bad sources replaced with nans.  
                        << this is the list you want to use from now on. >> 
    cPhotometry: list - detrended aperture photometry
    
    '''
    
    # replace all flux negative values with nans
    photometry = np.array(photometry)
    photometry[photometry<=0] = np.nan

    #create empty lists
    cPhotometry = np.zeros_like(photometry)
    finalPhot = np.zeros_like(photometry)

    checkstars = np.linspace(0,nstars,10).astype(int)


    
    # get median flux value for each star (find percent change)
    for star in np.arange(nstars):

        if star in checkstars:
            print('starting detrending on star number ', star, '/', nstars)
        ## pull out each star's photometry + error Photometry for each night and place in list 
        starPhotometry = np.array([starPhot[star] for starPhot in photometry])
        starPhotometryerr = np.array([starPhoterr[star] for starPhoterr in ephotometry])
    
        #find signal to noise 
        SNval = starPhotometry /starPhotometryerr
  
        #find all low S/N
        low_sn = np.where(SNval < 3.)
        
        if len(low_sn[0]) > 0:
            print(star, ': low S/N, night', low_sn[0])
        if star == 135:
            print(low_sn)
        
        # blank out bad photometry
        starPhotometry[low_sn] = np.nan
        
        # add final photometric values to list
        finalPhot[:,star] = starPhotometry

        # now find the median
       
        med_val = np.nanmedian(starPhotometry)
        if star in checkstars:
            print(f'median photometry on star {star}: {med_val}')
       
        if med_val <= 0.0: # known bad photometry
            cPhotometry[:,star] = starPhotometry*np.nan
        else:
            cPhotometry[:,star] = starPhotometry/med_val

    # do a check for outlier photometry?
    
    for night in np.arange(len(cPhotometry)):
        # remove large-scale image-to-image variation to find best stars
        cPhotometry[night] = cPhotometry[night]/np.nanmedian(cPhotometry[night])
      
    cPhotometry = np.array(cPhotometry) 
    # eliminate stars with outliers from consideration
    w = np.where( (cPhotometry < 0.5) | (cPhotometry > 1.5))
    cPhotometry[w] = np.nan
    
    return finalPhot, cPhotometry

    detrend
    
    detrend the background for each night so we don't have to worry about changes in background noise levels
    
    inputs
    -------
    photometry: list - list of flux values from aperture photometry
    ephotometry: list - list of flux errors from aperture photometry
    nstars: float - number of stars in the field
    
    outputs
    --------
    finalPhot: list - final aperture photometry of sources with bad sources replaced with nans.  
                        << this is the list you want to use from now on. >> 
    cPhotometry: list - detrended aperture photometry
    
    
detrend
detrend the background for each night so we don't have to worry about changes in background noise levels

inputs

list of flux values from aperture photometry
ephotometry: list - list of flux errors from aperture photometry
nstars: float - number of stars in the field

outputs

final aperture photometry of sources with bad sources replaced with nans.
<< this is the list you want to use from now on. >>
cPhotometry: list - detrended aperture photometry
detrend detrend the background for each night so we don ' t have to worry about changes in background noise levels inputs list of flux values from aperture photometry ephotometry : list - list of flux errors from aperture photometry nstars : float - number of stars in the field outputs final aperture photometry of sources with bad sources replaced with nans . << this is the list you want to use from now on . >> cPhotometry : list - detrended aperture photometry
def detrend(photometry, ephotometry, nstars):
    photometry = np.array(photometry)
    photometry[photometry<=0] = np.nan
    cPhotometry = np.zeros_like(photometry)
    finalPhot = np.zeros_like(photometry)
    checkstars = np.linspace(0,nstars,10).astype(int)
    for star in np.arange(nstars):
        if star in checkstars:
            print('starting detrending on star number ', star, '/', nstars)
        starPhotometry = np.array([starPhot[star] for starPhot in photometry])
        starPhotometryerr = np.array([starPhoterr[star] for starPhoterr in ephotometry])
        SNval = starPhotometry /starPhotometryerr
        low_sn = np.where(SNval < 3.)
        if len(low_sn[0]) > 0:
            print(star, ': low S/N, night', low_sn[0])
        if star == 135:
            print(low_sn)
        starPhotometry[low_sn] = np.nan
        finalPhot[:,star] = starPhotometry
        med_val = np.nanmedian(starPhotometry)
        if star in checkstars:
            print(f'median photometry on star {star}: {med_val}')
        if med_val <= 0.0: 
            cPhotometry[:,star] = starPhotometry*np.nan
        else:
            cPhotometry[:,star] = starPhotometry/med_val
    for night in np.arange(len(cPhotometry)):
        cPhotometry[night] = cPhotometry[night]/np.nanmedian(cPhotometry[night])
    cPhotometry = np.array(cPhotometry) 
    w = np.where( (cPhotometry < 0.5) | (cPhotometry > 1.5))
    cPhotometry[w] = np.nan
    return finalPhot, cPhotometry
def detrend ( photometry , ephotometry , nstars ) : photometry = np . array ( photometry ) photometry [ photometry <= 0 ] = np . nan cPhotometry = np . zeros_like ( photometry ) finalPhot = np . zeros_like ( photometry ) checkstars = np . linspace ( 0 , nstars , 10 ) . astype ( int ) for star in np . arange ( nstars ) : if star in checkstars : print ( 'starting detrending on star number ' , star , '/' , nstars ) starPhotometry = np . array ( [ starPhot [ star ] for starPhot in photometry ] ) starPhotometryerr = np . array ( [ starPhoterr [ star ] for starPhoterr in ephotometry ] ) SNval = starPhotometry / starPhotometryerr low_sn = np . where ( SNval < 3. ) if len ( low_sn [ 0 ] ) > 0 : print ( star , ': low S/N, night' , low_sn [ 0 ] ) if star == 135 : print ( low_sn ) starPhotometry [ low_sn ] = np . nan finalPhot [ : , star ] = starPhotometry med_val = np . nanmedian ( starPhotometry ) if star in checkstars : print ( f'median photometry on star {star}: {med_val}' ) if med_val <= 0.0 : cPhotometry [ : , star ] = starPhotometry * np . nan else : cPhotometry [ : , star ] = starPhotometry / med_val for night in np . arange ( len ( cPhotometry ) ) : cPhotometry [ night ] = cPhotometry [ night ] / np . nanmedian ( cPhotometry [ night ] ) cPhotometry = np . array ( cPhotometry ) w = np . where ( ( cPhotometry < 0.5 ) | ( cPhotometry > 1.5 ) ) cPhotometry [ w ] = np . nan return finalPhot , cPhotometry
detrend
detrend the background for each night so we don't have to worry about changes in background noise levels
detrend detrend the background for each night so we don ' t have to worry about changes in background noise levels
'''
    detrend
    
    detrend the background for each night so we don't have to worry about changes in background noise levels
    
    inputs
    -------
    photometry: list - list of flux values from aperture photometry
    ephotometry: list - list of flux errors from aperture photometry
    nstars: float - number of stars in the field
    
    outputs
    --------
    finalPhot: list - final aperture photometry of sources with bad sources replaced with nans.  
                        << this is the list you want to use from now on. >> 
    cPhotometry: list - detrended aperture photometry
    
    ''' # replace all flux negative values with nans #create empty lists # get median flux value for each star (find percent change) ## pull out each star's photometry + error Photometry for each night and place in list  #find signal to noise  #find all low S/N # blank out bad photometry # add final photometric values to list # now find the median # known bad photometry # do a check for outlier photometry? # remove large-scale image-to-image variation to find best stars # eliminate stars with outliers from consideration
photometry
ephotometry
nstars