Lv3_duty_cycle.py
def duty_cycle(eventfile,tbin,segment_length,duty_cycle_bin,threshold):
    """
    To determine the percentage of "data used"/total amount of data. Have two
    types of values:
    1) % of bins (of size duty_cycle_bin) with data over the ENTIRE observation
    2) % of bins with data over the GTIs

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    tbin - size of the bins in time
    segment_length - length of the individual segments
    duty_cycle_bin - binning used to calculate the duty cycle
    threshold - if amount of data in the segment is more than threshold IN PERCENTAGE, use the data
    """
    parent_folder = str(pathlib.Path(eventfile).parent)
    event_header = fits.open(eventfile)[1].header
    obj_name = event_header['OBJECT']
    obsid = event_header['OBS_ID']

    dat_files = sorted(glob.glob(parent_folder + '/accelsearch_' + str(segment_length) + 's/*GTI*'+str(segment_length)+'s*.dat')) #grab the .dat files with a specified segment length

    gtis = fits.open(eventfile)[2].data
    obs_duration = gtis[-1][1] - gtis[0][0] #to get the duration of the ENTIRE observation

    total_gti = sum([ (gtis[i][1]-gtis[i][0]) for i in range(len(gtis))])

    useful_data = 0
    for i in range(len(dat_files)):
        tbin = np.float(tbin)
        binned_data = np.fromfile(dat_files[i],dtype='<f',count=-1)
        dat_times = np.arange(0,tbin*len(binned_data),tbin)

        duty_cycle_times = np.arange(0,tbin*len(binned_data)+tbin,duty_cycle_bin)
        summed_data, binedges, binnumber = stats.binned_statistic(dat_times,binned_data,statistic='sum',bins=duty_cycle_times)
#        plt.plot(duty_cycle_times[:-1],summed_data,'bx')
#        plt.title(dat_files[i])
#        plt.axhline(y=stats.mode(summed_data)[0][0])
#        plt.show()

        usable_data = len(summed_data[(summed_data!=stats.mode(summed_data)[0][0])&(summed_data>0)])
        print(usable_data,len(summed_data))
        if usable_data/len(summed_data)*100 >= threshold:
            useful_data += usable_data
            #print(usable_data)
        #print('--')

    print('ObsID: ' + str(obsid))
    print('Segment Length: ' + str(segment_length) + 's')
    print('Useful Data: ' + str(useful_data) + 's ; threshold used: ' + str(threshold) + '%')
    print('Sum of GTIs: ' + str(total_gti) + 's')
    print('Total observation duration: ' + str(obs_duration) + 's')
    print("Percentage of bins with data (over threshold in each segment) over the ENTIRE observation: " + str(useful_data/obs_duration*100))
    print("Percentage of bins with data (over threshold in each segment) over the GTIs: " + str(useful_data/total_gti*100))

    return

    To determine the percentage of "data used"/total amount of data. Have two
    types of values:
    1) % of bins (of size duty_cycle_bin) with data over the ENTIRE observation
    2) % of bins with data over the GTIs

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    tbin - size of the bins in time
    segment_length - length of the individual segments
    duty_cycle_bin - binning used to calculate the duty cycle
    threshold - if amount of data in the segment is more than threshold IN PERCENTAGE, use the data
    
To determine the percentage of "data used"/total amount of data. Have two
types of values:
1) % of bins (of size duty_cycle_bin) with data over the ENTIRE observation
2) % of bins with data over the GTIs

path to the event file. Will extract ObsID from this for the NICER files.
tbin - size of the bins in time
segment_length - length of the individual segments
duty_cycle_bin - binning used to calculate the duty cycle
threshold - if amount of data in the segment is more than threshold IN PERCENTAGE, use the data
To determine the percentage of " data used " / total amount of data . Have two types of values : 1 ) % of bins ( of size duty_cycle_bin ) with data over the ENTIRE observation 2 ) % of bins with data over the GTIs path to the event file . Will extract ObsID from this for the NICER files . tbin - size of the bins in time segment_length - length of the individual segments duty_cycle_bin - binning used to calculate the duty cycle threshold - if amount of data in the segment is more than threshold IN PERCENTAGE use the data
def duty_cycle(eventfile,tbin,segment_length,duty_cycle_bin,threshold):
    parent_folder = str(pathlib.Path(eventfile).parent)
    event_header = fits.open(eventfile)[1].header
    obj_name = event_header['OBJECT']
    obsid = event_header['OBS_ID']
    dat_files = sorted(glob.glob(parent_folder + '/accelsearch_' + str(segment_length) + 's/*GTI*'+str(segment_length)+'s*.dat')) 
    gtis = fits.open(eventfile)[2].data
    obs_duration = gtis[-1][1] - gtis[0][0] 
    total_gti = sum([ (gtis[i][1]-gtis[i][0]) for i in range(len(gtis))])
    useful_data = 0
    for i in range(len(dat_files)):
        tbin = np.float(tbin)
        binned_data = np.fromfile(dat_files[i],dtype='<f',count=-1)
        dat_times = np.arange(0,tbin*len(binned_data),tbin)
        duty_cycle_times = np.arange(0,tbin*len(binned_data)+tbin,duty_cycle_bin)
        summed_data, binedges, binnumber = stats.binned_statistic(dat_times,binned_data,statistic='sum',bins=duty_cycle_times)
        usable_data = len(summed_data[(summed_data!=stats.mode(summed_data)[0][0])&(summed_data>0)])
        print(usable_data,len(summed_data))
        if usable_data/len(summed_data)*100 >= threshold:
            useful_data += usable_data
    print('ObsID: ' + str(obsid))
    print('Segment Length: ' + str(segment_length) + 's')
    print('Useful Data: ' + str(useful_data) + 's ; threshold used: ' + str(threshold) + '%')
    print('Sum of GTIs: ' + str(total_gti) + 's')
    print('Total observation duration: ' + str(obs_duration) + 's')
    print("Percentage of bins with data (over threshold in each segment) over the ENTIRE observation: " + str(useful_data/obs_duration*100))
    print("Percentage of bins with data (over threshold in each segment) over the GTIs: " + str(useful_data/total_gti*100))
    return
def duty_cycle ( eventfile , tbin , segment_length , duty_cycle_bin , threshold ) : parent_folder = str ( pathlib . Path ( eventfile ) . parent ) event_header = fits . open ( eventfile ) [ 1 ] . header obj_name = event_header [ 'OBJECT' ] obsid = event_header [ 'OBS_ID' ] dat_files = sorted ( glob . glob ( parent_folder + '/accelsearch_' + str ( segment_length ) + 's/*GTI*' + str ( segment_length ) + 's*.dat' ) ) gtis = fits . open ( eventfile ) [ 2 ] . data obs_duration = gtis [ - 1 ] [ 1 ] - gtis [ 0 ] [ 0 ] total_gti = sum ( [ ( gtis [ i ] [ 1 ] - gtis [ i ] [ 0 ] ) for i in range ( len ( gtis ) ) ] ) useful_data = 0 for i in range ( len ( dat_files ) ) : tbin = np . float ( tbin ) binned_data = np . fromfile ( dat_files [ i ] , dtype = '<f' , count = - 1 ) dat_times = np . arange ( 0 , tbin * len ( binned_data ) , tbin ) duty_cycle_times = np . arange ( 0 , tbin * len ( binned_data ) + tbin , duty_cycle_bin ) summed_data , binedges , binnumber = stats . binned_statistic ( dat_times , binned_data , statistic = 'sum' , bins = duty_cycle_times ) usable_data = len ( summed_data [ ( summed_data != stats . mode ( summed_data ) [ 0 ] [ 0 ] ) & ( summed_data > 0 ) ] ) print ( usable_data , len ( summed_data ) ) if usable_data / len ( summed_data ) * 100 >= threshold : useful_data += usable_data print ( 'ObsID: ' + str ( obsid ) ) print ( 'Segment Length: ' + str ( segment_length ) + 's' ) print ( 'Useful Data: ' + str ( useful_data ) + 's ; threshold used: ' + str ( threshold ) + '%' ) print ( 'Sum of GTIs: ' + str ( total_gti ) + 's' ) print ( 'Total observation duration: ' + str ( obs_duration ) + 's' ) print ( "Percentage of bins with data (over threshold in each segment) over the ENTIRE observation: " + str ( useful_data / obs_duration * 100 ) ) print ( "Percentage of bins with data (over threshold in each segment) over the GTIs: " + str ( useful_data / total_gti * 100 ) ) return
To determine the percentage of "data used"/total amount of data.
To determine the percentage of " data used " / total amount of data .
"""
    To determine the percentage of "data used"/total amount of data. Have two
    types of values:
    1) % of bins (of size duty_cycle_bin) with data over the ENTIRE observation
    2) % of bins with data over the GTIs

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    tbin - size of the bins in time
    segment_length - length of the individual segments
    duty_cycle_bin - binning used to calculate the duty cycle
    threshold - if amount of data in the segment is more than threshold IN PERCENTAGE, use the data
    """ #grab the .dat files with a specified segment length #to get the duration of the ENTIRE observation #        plt.plot(duty_cycle_times[:-1],summed_data,'bx') #        plt.title(dat_files[i]) #        plt.axhline(y=stats.mode(summed_data)[0][0]) #        plt.show() #print(usable_data) #print('--')
eventfile
tbin
segment_length
duty_cycle_bin
threshold