DEPRECATED_Lv3_average_ps_segments.py
def average_ps_presto_segments(obsid,segment_length,threshold):
    """
    Do averaged power spectra from the FFT files that were generated from PRESTO!

    obsid - Observation ID of the object of interest (10-digit str)
    segment_length - length of the segments
    threshold - if data is under threshold (in percentage), then throw OUT the segment!
    """
    fft_files = presto_FFT(obsid,segment_length) #get the sorted list of .fft files
    dat_files = presto_dat(obsid,segment_length) #get the sorted list of .dat files

    counts_test = np.fromfile(dat_files[0],dtype='<f',count=-1)
    t_test = np.linspace(0,segment_length,len(counts_test))

    t_bins_threshold = np.arange(0,segment_length,1) #1 second bins
    summed_threshold, bin_edges_thres, binnumber_thres = stats.binned_statistic(t_test,counts_test,statistic='sum',bins=t_bins_threshold)

    initial_binned_data = np.fromfile(fft_files[0],dtype='complex64',count=-1) #just to initialize averaged power spectrum
    averaged_ps = np.zeros(len(initial_binned_data))
    t_bins_threshold = np.arange(0,segment_length,1) #1 second bins

    segment_no = 0
    multiplier = [0,11,12,16,17,18,1,21,22,23,24,27,28,29,33,34,38,39,40,43,44,45,46,
                49,50,51,55,56,57,5,60,61,62,66,67,68,6,71,72,73,74,77,78,79,7,82,83,
                84]
    for i in tqdm(range(len(fft_files))):
        dat_file = np.fromfile(dat_files[i],dtype='<f',count=-1)
        t_data = np.linspace(0,segment_length,len(dat_file))+(multiplier[i]*1000)
        #plt.plot(t_data,dat_file,'bx-')
        summed_threshold, bin_edges_thres, binnumber_thres = stats.binned_statistic(t_data,dat_file,statistic='sum',bins=t_bins_threshold)
        binsize = segment_length/len(dat_file)

        tbins_here = t_bins_threshold+(multiplier[i]*1000)
        plt.plot(tbins_here[:-1],summed_threshold,'bx-')
        #bin the dat_file to have the threshold!
#        print(len(summed_threshold),len(summed_threshold[summed_threshold>0])/len(summed_threshold)*100)
        if len(summed_threshold[summed_threshold>0])/len(summed_threshold)*100 >= threshold:
            binned_data = np.fromfile(fft_files[i],dtype='complex64',count=-1)
            averaged_ps += 2/sum(dat_file)*np.abs(binned_data)**2
            segment_no += 1

    freqs = np.fft.fftfreq(averaged_ps.size,binsize)
    N = len(freqs)

    print('We used ' + str(segment_no) + ' out of ' + str(len(fft_files)) + ' possible segments.')
    return freqs[1:int(N/2)], averaged_ps[1:int(N/2)]/segment_no

    Do averaged power spectra from the FFT files that were generated from PRESTO!

    obsid - Observation ID of the object of interest (10-digit str)
    segment_length - length of the segments
    threshold - if data is under threshold (in percentage), then throw OUT the segment!
    
Do averaged power spectra from the FFT files that were generated from PRESTO.
obsid - Observation ID of the object of interest (10-digit str)
segment_length - length of the segments
threshold - if data is under threshold (in percentage), then throw OUT the segment!
Do averaged power spectra from the FFT files that were generated from PRESTO . obsid - Observation ID of the object of interest ( 10 - digit str ) segment_length - length of the segments threshold - if data is under threshold ( in percentage ) then throw OUT the segment!
def average_ps_presto_segments(obsid,segment_length,threshold):
    fft_files = presto_FFT(obsid,segment_length) 
    dat_files = presto_dat(obsid,segment_length) 
    counts_test = np.fromfile(dat_files[0],dtype='<f',count=-1)
    t_test = np.linspace(0,segment_length,len(counts_test))
    t_bins_threshold = np.arange(0,segment_length,1) 
    summed_threshold, bin_edges_thres, binnumber_thres = stats.binned_statistic(t_test,counts_test,statistic='sum',bins=t_bins_threshold)
    initial_binned_data = np.fromfile(fft_files[0],dtype='complex64',count=-1) 
    averaged_ps = np.zeros(len(initial_binned_data))
    t_bins_threshold = np.arange(0,segment_length,1) 
    segment_no = 0
    multiplier = [0,11,12,16,17,18,1,21,22,23,24,27,28,29,33,34,38,39,40,43,44,45,46,
                49,50,51,55,56,57,5,60,61,62,66,67,68,6,71,72,73,74,77,78,79,7,82,83,
                84]
    for i in tqdm(range(len(fft_files))):
        dat_file = np.fromfile(dat_files[i],dtype='<f',count=-1)
        t_data = np.linspace(0,segment_length,len(dat_file))+(multiplier[i]*1000)
        summed_threshold, bin_edges_thres, binnumber_thres = stats.binned_statistic(t_data,dat_file,statistic='sum',bins=t_bins_threshold)
        binsize = segment_length/len(dat_file)
        tbins_here = t_bins_threshold+(multiplier[i]*1000)
        plt.plot(tbins_here[:-1],summed_threshold,'bx-')
        if len(summed_threshold[summed_threshold>0])/len(summed_threshold)*100 >= threshold:
            binned_data = np.fromfile(fft_files[i],dtype='complex64',count=-1)
            averaged_ps += 2/sum(dat_file)*np.abs(binned_data)**2
            segment_no += 1
    freqs = np.fft.fftfreq(averaged_ps.size,binsize)
    N = len(freqs)
    print('We used ' + str(segment_no) + ' out of ' + str(len(fft_files)) + ' possible segments.')
    return freqs[1:int(N/2)], averaged_ps[1:int(N/2)]/segment_no
def average_ps_presto_segments ( obsid , segment_length , threshold ) : fft_files = presto_FFT ( obsid , segment_length ) dat_files = presto_dat ( obsid , segment_length ) counts_test = np . fromfile ( dat_files [ 0 ] , dtype = '<f' , count = - 1 ) t_test = np . linspace ( 0 , segment_length , len ( counts_test ) ) t_bins_threshold = np . arange ( 0 , segment_length , 1 ) summed_threshold , bin_edges_thres , binnumber_thres = stats . binned_statistic ( t_test , counts_test , statistic = 'sum' , bins = t_bins_threshold ) initial_binned_data = np . fromfile ( fft_files [ 0 ] , dtype = 'complex64' , count = - 1 ) averaged_ps = np . zeros ( len ( initial_binned_data ) ) t_bins_threshold = np . arange ( 0 , segment_length , 1 ) segment_no = 0 multiplier = [ 0 , 11 , 12 , 16 , 17 , 18 , 1 , 21 , 22 , 23 , 24 , 27 , 28 , 29 , 33 , 34 , 38 , 39 , 40 , 43 , 44 , 45 , 46 , 49 , 50 , 51 , 55 , 56 , 57 , 5 , 60 , 61 , 62 , 66 , 67 , 68 , 6 , 71 , 72 , 73 , 74 , 77 , 78 , 79 , 7 , 82 , 83 , 84 ] for i in tqdm ( range ( len ( fft_files ) ) ) : dat_file = np . fromfile ( dat_files [ i ] , dtype = '<f' , count = - 1 ) t_data = np . linspace ( 0 , segment_length , len ( dat_file ) ) + ( multiplier [ i ] * 1000 ) summed_threshold , bin_edges_thres , binnumber_thres = stats . binned_statistic ( t_data , dat_file , statistic = 'sum' , bins = t_bins_threshold ) binsize = segment_length / len ( dat_file ) tbins_here = t_bins_threshold + ( multiplier [ i ] * 1000 ) plt . plot ( tbins_here [ : - 1 ] , summed_threshold , 'bx-' ) if len ( summed_threshold [ summed_threshold > 0 ] ) / len ( summed_threshold ) * 100 >= threshold : binned_data = np . fromfile ( fft_files [ i ] , dtype = 'complex64' , count = - 1 ) averaged_ps += 2 / sum ( dat_file ) * np . abs ( binned_data ) ** 2 segment_no += 1 freqs = np . fft . fftfreq ( averaged_ps . size , binsize ) N = len ( freqs ) print ( 'We used ' + str ( segment_no ) + ' out of ' + str ( len ( fft_files ) ) + ' possible segments.' ) return freqs [ 1 : int ( N / 2 ) ] , averaged_ps [ 1 : int ( N / 2 ) ] / segment_no
Do averaged power spectra from the FFT files that were generated from PRESTO!
Do averaged power spectra from the FFT files that were generated from PRESTO!
"""
    Do averaged power spectra from the FFT files that were generated from PRESTO!

    obsid - Observation ID of the object of interest (10-digit str)
    segment_length - length of the segments
    threshold - if data is under threshold (in percentage), then throw OUT the segment!
    """ #get the sorted list of .fft files #get the sorted list of .dat files #1 second bins #just to initialize averaged power spectrum #1 second bins #plt.plot(t_data,dat_file,'bx-') #bin the dat_file to have the threshold! #        print(len(summed_threshold),len(summed_threshold[summed_threshold>0])/len(summed_threshold)*100)
obsid
segment_length
threshold