Lv3_duty_cycle.py
def duty_cycle_tE_dist(eventfile,tbin,segment_length,PI1,PI2,duty_cycle_bin,threshold):
    """
    To get the distribution of duty cycles over all segments, given an ObsID and
    a desired segment length!

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    tbin - size of the bins in time
    PI1 - lower energy boundary (in units of PI)
    PI2 - upper energy boundary (in units of PI)
    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)

    obsdir = Lv0_dirs.NICERSOFT_DATADIR + obsid + '_pipe/accelsearch_' + str(segment_length) + 's/'
    dat_files = sorted(glob.glob(parent_folder+'/accelsearch_' + str(segment_length) + 's/*GTI*'+str(segment_length)+'s*'+str(PI1)+'-'+str(PI2)+'*.dat')) #grab the .dat files with a specified segment length

    duty_cycle_array = []
    print('Calculating the duty cycle!')
    tbin = np.float(tbin)
    for i in tqdm(range(len(dat_files))):
        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)
        # the duty_cycle_frac is NOT a perfect metric!!! There will be data equal to the average counts (because I've binned).
        duty_cycle_frac = len(summed_data[(summed_data!=stats.mode(summed_data)[0][0])&(summed_data>0)])/len(summed_data)
        duty_cycle_array.append(duty_cycle_frac)

    fig = plt.figure(1)
    ax1 = fig.add_subplot(111)

    ax1.hist(duty_cycle_array,bins=100,log=True)
    ax1.set_xlabel('Duty cycle fraction',fontsize=12)
    ax1.set_ylabel('log10(Number of segments)',fontsize=12)

    ax2 = fig.add_subplot(111,sharex=ax1,frameon=False)
    ax2.hist(duty_cycle_array,bins=100)
    ax2.yaxis.tick_right()
    ax2.yaxis.set_label_position("right")
    ax2.set_ylabel('Number of segments',fontsize=12)

    plt.title('Segment length = ' + str(segment_length) + 's \n Number of segments here: ' + str(len(dat_files)),fontsize=12)
    plt.savefig(parent_folder + '/' +str(segment_length)+'s_dutycycle.pdf',dpi=900,format='pdf')
    plt.close()

    return np.array(duty_cycle_array)

    To get the distribution of duty cycles over all segments, given an ObsID and
    a desired segment length!

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    tbin - size of the bins in time
    PI1 - lower energy boundary (in units of PI)
    PI2 - upper energy boundary (in units of PI)
    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 get the distribution of duty cycles over all segments, given an ObsID and
a desired segment length!

path to the event file. Will extract ObsID from this for the NICER files.
To get the distribution of duty cycles over all segments given an ObsID and a desired segment length! path to the event file . Will extract ObsID from this for the NICER files .
def duty_cycle_tE_dist(eventfile,tbin,segment_length,PI1,PI2,duty_cycle_bin,threshold):
    parent_folder = str(pathlib.Path(eventfile).parent)
    obsdir = Lv0_dirs.NICERSOFT_DATADIR + obsid + '_pipe/accelsearch_' + str(segment_length) + 's/'
    dat_files = sorted(glob.glob(parent_folder+'/accelsearch_' + str(segment_length) + 's/*GTI*'+str(segment_length)+'s*'+str(PI1)+'-'+str(PI2)+'*.dat')) 
    duty_cycle_array = []
    print('Calculating the duty cycle!')
    tbin = np.float(tbin)
    for i in tqdm(range(len(dat_files))):
        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)
        duty_cycle_frac = len(summed_data[(summed_data!=stats.mode(summed_data)[0][0])&(summed_data>0)])/len(summed_data)
        duty_cycle_array.append(duty_cycle_frac)
    fig = plt.figure(1)
    ax1 = fig.add_subplot(111)
    ax1.hist(duty_cycle_array,bins=100,log=True)
    ax1.set_xlabel('Duty cycle fraction',fontsize=12)
    ax1.set_ylabel('log10(Number of segments)',fontsize=12)
    ax2 = fig.add_subplot(111,sharex=ax1,frameon=False)
    ax2.hist(duty_cycle_array,bins=100)
    ax2.yaxis.tick_right()
    ax2.yaxis.set_label_position("right")
    ax2.set_ylabel('Number of segments',fontsize=12)
    plt.title('Segment length = ' + str(segment_length) + 's \n Number of segments here: ' + str(len(dat_files)),fontsize=12)
    plt.savefig(parent_folder + '/' +str(segment_length)+'s_dutycycle.pdf',dpi=900,format='pdf')
    plt.close()
    return np.array(duty_cycle_array)
def duty_cycle_tE_dist ( eventfile , tbin , segment_length , PI1 , PI2 , duty_cycle_bin , threshold ) : parent_folder = str ( pathlib . Path ( eventfile ) . parent ) obsdir = Lv0_dirs . NICERSOFT_DATADIR + obsid + '_pipe/accelsearch_' + str ( segment_length ) + 's/' dat_files = sorted ( glob . glob ( parent_folder + '/accelsearch_' + str ( segment_length ) + 's/*GTI*' + str ( segment_length ) + 's*' + str ( PI1 ) + '-' + str ( PI2 ) + '*.dat' ) ) duty_cycle_array = [ ] print ( 'Calculating the duty cycle!' ) tbin = np . float ( tbin ) for i in tqdm ( range ( len ( dat_files ) ) ) : 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 ) duty_cycle_frac = len ( summed_data [ ( summed_data != stats . mode ( summed_data ) [ 0 ] [ 0 ] ) & ( summed_data > 0 ) ] ) / len ( summed_data ) duty_cycle_array . append ( duty_cycle_frac ) fig = plt . figure ( 1 ) ax1 = fig . add_subplot ( 111 ) ax1 . hist ( duty_cycle_array , bins = 100 , log = True ) ax1 . set_xlabel ( 'Duty cycle fraction' , fontsize = 12 ) ax1 . set_ylabel ( 'log10(Number of segments)' , fontsize = 12 ) ax2 = fig . add_subplot ( 111 , sharex = ax1 , frameon = False ) ax2 . hist ( duty_cycle_array , bins = 100 ) ax2 . yaxis . tick_right ( ) ax2 . yaxis . set_label_position ( "right" ) ax2 . set_ylabel ( 'Number of segments' , fontsize = 12 ) plt . title ( 'Segment length = ' + str ( segment_length ) + 's \n Number of segments here: ' + str ( len ( dat_files ) ) , fontsize = 12 ) plt . savefig ( parent_folder + '/' + str ( segment_length ) + 's_dutycycle.pdf' , dpi = 900 , format = 'pdf' ) plt . close ( ) return np . array ( duty_cycle_array )
To get the distribution of duty cycles over all segments, given an ObsID and
a desired segment length!
To get the distribution of duty cycles over all segments given an ObsID and a desired segment length!
"""
    To get the distribution of duty cycles over all segments, given an ObsID and
    a desired segment length!

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    tbin - size of the bins in time
    PI1 - lower energy boundary (in units of PI)
    PI2 - upper energy boundary (in units of PI)
    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 # the duty_cycle_frac is NOT a perfect metric!!! There will be data equal to the average counts (because I've binned).
eventfile
tbin
segment_length
PI1
PI2
duty_cycle_bin
threshold