Lv2_average_ps_methods.py
def average_ps(eventfile,segment_length,demod,tbin_size,threshold,PI1,PI2,t1,t2,starting_freq,W):
    """
    Given the full list of .dat and .fft files, and the indices where the PRESTO-binned
    data is beyond some threshold, return the averaged power spectrum!

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the segments
    demod - whether we're dealing with demodulated data or not!
    tbin_size - size of the time bin
    threshold - if data is under threshold (in percentage), then don't use the segment!
    PI1 - lower bound of PI (not energy in keV!) desired for the energy range
    PI2 - upper bound of PI (not energy in keV!) desired for the energy range
    t1 - starting time for calculation of averaged power spectra
    t2 - ending time for calculation of averaged power spectra
    (note that t=0 corresponds to the MET of the FIRST event in the eventfile, so will need to inspect light curve with Lv2_lc.py to get times)
    starting_freq - frequency to start constructing the histogram of powers from
    W - number of consecutive frequency bins to AVERAGE over
    """
    if demod != True and demod != False:
        raise ValueError("demod should either be True or False!")

    dat_files = presto_dat(eventfile,segment_length,demod,PI1,PI2,t1,t2) #sorted array of .dat files
    fft_files = presto_fft(eventfile,segment_length,demod,PI1,PI2,t1,t2) #sorted array of .fft files
    passed_threshold,M = segment_threshold(eventfile,segment_length,demod,tbin_size,threshold,PI1,PI2,t1,t2)
    #list of indices where the rebinned .dat files are beyond the threshold

    if len(passed_threshold) == 0:
        freqs = np.fft.fftfreq(int(segment_length/tbin_size),tbin_size)
        N = len(freqs)

        f = freqs[1:int(N/2)]
        average_ps = np.ones(int(segment_length/(2*tbin_size)))*2
        ps = average_ps[1:]

        ps_to_use = ps[f>starting_freq]
        ps_bins = np.linspace(min(ps_to_use),max(ps_to_use),1000)
        N_greaterthanP = []
        print('Creating the noise histogram [N(>P)]...')
        for i in tqdm(range(len(ps_bins))):
            array_greaterthan = ps_to_use[ps_to_use>ps_bins[i]]
            N_greaterthanP.append(len(array_greaterthan))

        return f,ps,ps_bins,N_greaterthanP,M

    dat_threshold = dat_files[passed_threshold] #.dat files that passed the threshold
    fft_threshold = fft_files[passed_threshold] #corresponding .fft files that passed the threshold

    freqs = np.fft.fftfreq(int(segment_length/tbin_size),tbin_size)
    N = len(freqs)

    average_ps = np.zeros(int(segment_length/(2*tbin_size)))
    print('Calculating the averaged spectrum...')
    for i in tqdm(range(len(dat_threshold))):
        dat_threshold_data = np.fromfile(dat_threshold[i],dtype='<f',count=-1)
        no_photons = sum(dat_threshold_data)

        fft_threshold_data = np.fromfile(fft_threshold[i],dtype='complex64',count=-1)
        ps = 2/no_photons * np.abs(fft_threshold_data)**2
        average_ps += ps

    print('The mean Leahy power of the latter 90% of the power spectrum is ' + str(np.mean(average_ps[np.int(0.1*len(average_ps)):])/len(passed_threshold)))

    if W == 1:
        f = freqs[1:int(N/2)]
        ps = average_ps[1:]/len(passed_threshold)

        ps_to_use = ps[f>starting_freq]
        ps_bins = np.linspace(min(ps_to_use),max(ps_to_use),1000)
        N_greaterthanP = []
        print('Creating the noise histogram [N(>P)]...')
        for i in tqdm(range(len(ps_bins))):
            array_greaterthan = ps_to_use[ps_to_use>ps_bins[i]]
            N_greaterthanP.append(len(array_greaterthan))

        return f,ps,ps_bins,N_greaterthanP,M

    else:
        pre_f = freqs[1:int(N/2)] #frequency array corresponding to W = 1
        pre_ps = average_ps[1:]/len(passed_threshold) #power array corresponding to W = 1

        consec_f = pre_f[::W] #frequency bins AFTER averaging W consecutive frequency bins
        consec_ps,consec_edges,consec_binnumber = stats.binned_statistic(pre_f,pre_ps,statistic='mean',bins=consec_f)

        f = consec_f[:-1]
        ps = consec_ps

        ps_to_use = ps[f>starting_freq]
        ps_bins = np.linspace(min(ps_to_use),max(ps_to_use),1000)
        N_greaterthanP = []
        print('Creating the noise histogram [N(>P)]...')
        for i in tqdm(range(len(ps_bins))):
            array_greaterthan = ps_to_use[ps_to_use>ps_bins[i]]
            N_greaterthanP.append(len(array_greaterthan))

        return f,ps,ps_bins,N_greaterthanP,M

    Given the full list of .dat and .fft files, and the indices where the PRESTO-binned
    data is beyond some threshold, return the averaged power spectrum!

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the segments
    demod - whether we're dealing with demodulated data or not!
    tbin_size - size of the time bin
    threshold - if data is under threshold (in percentage), then don't use the segment!
    PI1 - lower bound of PI (not energy in keV!) desired for the energy range
    PI2 - upper bound of PI (not energy in keV!) desired for the energy range
    t1 - starting time for calculation of averaged power spectra
    t2 - ending time for calculation of averaged power spectra
    (note that t=0 corresponds to the MET of the FIRST event in the eventfile, so will need to inspect light curve with Lv2_lc.py to get times)
    starting_freq - frequency to start constructing the histogram of powers from
    W - number of consecutive frequency bins to AVERAGE over
    
Given the full list of .dat and .fft files, and the indices where the PRESTO-binned
data is beyond some threshold, return the averaged power spectrum!

path to the event file. Will extract ObsID from this for the NICER files.
segment_length - length of the segments
demod - whether we're dealing with demodulated data or not.
tbin_size - size of the time bin
threshold - if data is under threshold (in percentage), then don't use the segment.
Given the full list of . dat and . fft files and the indices where the PRESTO - binned data is beyond some threshold return the averaged power spectrum! path to the event file . Will extract ObsID from this for the NICER files . segment_length - length of the segments demod - whether we ' re dealing with demodulated data or not . tbin_size - size of the time bin threshold - if data is under threshold ( in percentage ) then don ' t use the segment .
def average_ps(eventfile,segment_length,demod,tbin_size,threshold,PI1,PI2,t1,t2,starting_freq,W):
    if demod != True and demod != False:
        raise ValueError("demod should either be True or False!")
    dat_files = presto_dat(eventfile,segment_length,demod,PI1,PI2,t1,t2) 
    fft_files = presto_fft(eventfile,segment_length,demod,PI1,PI2,t1,t2) 
    passed_threshold,M = segment_threshold(eventfile,segment_length,demod,tbin_size,threshold,PI1,PI2,t1,t2)
    if len(passed_threshold) == 0:
        freqs = np.fft.fftfreq(int(segment_length/tbin_size),tbin_size)
        N = len(freqs)
        f = freqs[1:int(N/2)]
        average_ps = np.ones(int(segment_length/(2*tbin_size)))*2
        ps = average_ps[1:]
        ps_to_use = ps[f>starting_freq]
        ps_bins = np.linspace(min(ps_to_use),max(ps_to_use),1000)
        N_greaterthanP = []
        print('Creating the noise histogram [N(>P)]...')
        for i in tqdm(range(len(ps_bins))):
            array_greaterthan = ps_to_use[ps_to_use>ps_bins[i]]
            N_greaterthanP.append(len(array_greaterthan))
        return f,ps,ps_bins,N_greaterthanP,M
    dat_threshold = dat_files[passed_threshold] 
    fft_threshold = fft_files[passed_threshold] 
    freqs = np.fft.fftfreq(int(segment_length/tbin_size),tbin_size)
    N = len(freqs)
    average_ps = np.zeros(int(segment_length/(2*tbin_size)))
    print('Calculating the averaged spectrum...')
    for i in tqdm(range(len(dat_threshold))):
        dat_threshold_data = np.fromfile(dat_threshold[i],dtype='<f',count=-1)
        no_photons = sum(dat_threshold_data)
        fft_threshold_data = np.fromfile(fft_threshold[i],dtype='complex64',count=-1)
        ps = 2/no_photons * np.abs(fft_threshold_data)**2
        average_ps += ps
    print('The mean Leahy power of the latter 90% of the power spectrum is ' + str(np.mean(average_ps[np.int(0.1*len(average_ps)):])/len(passed_threshold)))
    if W == 1:
        f = freqs[1:int(N/2)]
        ps = average_ps[1:]/len(passed_threshold)
        ps_to_use = ps[f>starting_freq]
        ps_bins = np.linspace(min(ps_to_use),max(ps_to_use),1000)
        N_greaterthanP = []
        print('Creating the noise histogram [N(>P)]...')
        for i in tqdm(range(len(ps_bins))):
            array_greaterthan = ps_to_use[ps_to_use>ps_bins[i]]
            N_greaterthanP.append(len(array_greaterthan))
        return f,ps,ps_bins,N_greaterthanP,M
    else:
        pre_f = freqs[1:int(N/2)] 
        pre_ps = average_ps[1:]/len(passed_threshold) 
        consec_f = pre_f[::W] 
        consec_ps,consec_edges,consec_binnumber = stats.binned_statistic(pre_f,pre_ps,statistic='mean',bins=consec_f)
        f = consec_f[:-1]
        ps = consec_ps
        ps_to_use = ps[f>starting_freq]
        ps_bins = np.linspace(min(ps_to_use),max(ps_to_use),1000)
        N_greaterthanP = []
        print('Creating the noise histogram [N(>P)]...')
        for i in tqdm(range(len(ps_bins))):
            array_greaterthan = ps_to_use[ps_to_use>ps_bins[i]]
            N_greaterthanP.append(len(array_greaterthan))
        return f,ps,ps_bins,N_greaterthanP,M
def average_ps ( eventfile , segment_length , demod , tbin_size , threshold , PI1 , PI2 , t1 , t2 , starting_freq , W ) : if demod != True and demod != False : raise ValueError ( "demod should either be True or False!" ) dat_files = presto_dat ( eventfile , segment_length , demod , PI1 , PI2 , t1 , t2 ) fft_files = presto_fft ( eventfile , segment_length , demod , PI1 , PI2 , t1 , t2 ) passed_threshold , M = segment_threshold ( eventfile , segment_length , demod , tbin_size , threshold , PI1 , PI2 , t1 , t2 ) if len ( passed_threshold ) == 0 : freqs = np . fft . fftfreq ( int ( segment_length / tbin_size ) , tbin_size ) N = len ( freqs ) f = freqs [ 1 : int ( N / 2 ) ] average_ps = np . ones ( int ( segment_length / ( 2 * tbin_size ) ) ) * 2 ps = average_ps [ 1 : ] ps_to_use = ps [ f > starting_freq ] ps_bins = np . linspace ( min ( ps_to_use ) , max ( ps_to_use ) , 1000 ) N_greaterthanP = [ ] print ( 'Creating the noise histogram [N(>P)]...' ) for i in tqdm ( range ( len ( ps_bins ) ) ) : array_greaterthan = ps_to_use [ ps_to_use > ps_bins [ i ] ] N_greaterthanP . append ( len ( array_greaterthan ) ) return f , ps , ps_bins , N_greaterthanP , M dat_threshold = dat_files [ passed_threshold ] fft_threshold = fft_files [ passed_threshold ] freqs = np . fft . fftfreq ( int ( segment_length / tbin_size ) , tbin_size ) N = len ( freqs ) average_ps = np . zeros ( int ( segment_length / ( 2 * tbin_size ) ) ) print ( 'Calculating the averaged spectrum...' ) for i in tqdm ( range ( len ( dat_threshold ) ) ) : dat_threshold_data = np . fromfile ( dat_threshold [ i ] , dtype = '<f' , count = - 1 ) no_photons = sum ( dat_threshold_data ) fft_threshold_data = np . fromfile ( fft_threshold [ i ] , dtype = 'complex64' , count = - 1 ) ps = 2 / no_photons * np . abs ( fft_threshold_data ) ** 2 average_ps += ps print ( 'The mean Leahy power of the latter 90% of the power spectrum is ' + str ( np . mean ( average_ps [ np . int ( 0.1 * len ( average_ps ) ) : ] ) / len ( passed_threshold ) ) ) if W == 1 : f = freqs [ 1 : int ( N / 2 ) ] ps = average_ps [ 1 : ] / len ( passed_threshold ) ps_to_use = ps [ f > starting_freq ] ps_bins = np . linspace ( min ( ps_to_use ) , max ( ps_to_use ) , 1000 ) N_greaterthanP = [ ] print ( 'Creating the noise histogram [N(>P)]...' ) for i in tqdm ( range ( len ( ps_bins ) ) ) : array_greaterthan = ps_to_use [ ps_to_use > ps_bins [ i ] ] N_greaterthanP . append ( len ( array_greaterthan ) ) return f , ps , ps_bins , N_greaterthanP , M else : pre_f = freqs [ 1 : int ( N / 2 ) ] pre_ps = average_ps [ 1 : ] / len ( passed_threshold ) consec_f = pre_f [ : : W ] consec_ps , consec_edges , consec_binnumber = stats . binned_statistic ( pre_f , pre_ps , statistic = 'mean' , bins = consec_f ) f = consec_f [ : - 1 ] ps = consec_ps ps_to_use = ps [ f > starting_freq ] ps_bins = np . linspace ( min ( ps_to_use ) , max ( ps_to_use ) , 1000 ) N_greaterthanP = [ ] print ( 'Creating the noise histogram [N(>P)]...' ) for i in tqdm ( range ( len ( ps_bins ) ) ) : array_greaterthan = ps_to_use [ ps_to_use > ps_bins [ i ] ] N_greaterthanP . append ( len ( array_greaterthan ) ) return f , ps , ps_bins , N_greaterthanP , M
Given the full list of .dat and .fft files, and the indices where the PRESTO-binned
data is beyond some threshold, return the averaged power spectrum!
Given the full list of . dat and . fft files and the indices where the PRESTO - binned data is beyond some threshold return the averaged power spectrum!
"""
    Given the full list of .dat and .fft files, and the indices where the PRESTO-binned
    data is beyond some threshold, return the averaged power spectrum!

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the segments
    demod - whether we're dealing with demodulated data or not!
    tbin_size - size of the time bin
    threshold - if data is under threshold (in percentage), then don't use the segment!
    PI1 - lower bound of PI (not energy in keV!) desired for the energy range
    PI2 - upper bound of PI (not energy in keV!) desired for the energy range
    t1 - starting time for calculation of averaged power spectra
    t2 - ending time for calculation of averaged power spectra
    (note that t=0 corresponds to the MET of the FIRST event in the eventfile, so will need to inspect light curve with Lv2_lc.py to get times)
    starting_freq - frequency to start constructing the histogram of powers from
    W - number of consecutive frequency bins to AVERAGE over
    """ #sorted array of .dat files #sorted array of .fft files #list of indices where the rebinned .dat files are beyond the threshold #.dat files that passed the threshold #corresponding .fft files that passed the threshold #frequency array corresponding to W = 1 #power array corresponding to W = 1 #frequency bins AFTER averaging W consecutive frequency bins
eventfile
segment_length
demod
tbin_size
threshold
PI1
PI2
t1
t2
starting_freq
W