Lv2_presto_subroutines.py
def niextract_gti_time_energy(eventfile,segment_length,PI1,PI2):
    """
    Using niextract-events to get segmented data based on [segment_length]-length
    GTIs that were created above, AND energy range!
    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the individual segments for combining power spectra
    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
    """
    parent_folder = str(pathlib.Path(eventfile).parent)
    gtis = fits.open(eventfile)[2].data
    times = fits.open(eventfile)[1].data['TIME']

    event_header = fits.open(eventfile)[1].header
    obj_name = event_header['OBJECT']
    obsid = event_header['OBS_ID']

    Tobs_start = gtis[0][0] #MJD for the observation start time
    Tobs_end = gtis[-1][1] #MJD for the observation end time

#    Tobs_start = event[1].data['TIME'][0]
#    Tobs_end = event[1].data['TIME'][-1]

    segment_times = np.arange(Tobs_start,Tobs_end+segment_length,segment_length) #array of time values, starting
    #Jul 10: added Tobs_end + segment_length to get that final piece of data

    binned_counts, bin_edges, binnumber = stats.binned_statistic(times,np.ones(len(times)),statistic='sum',bins=segment_times)
    #bin edges defined by left boundary

    niextract_folder = parent_folder + '/accelsearch_' + str(segment_length) + 's/'
    Lv2_mkdir.makedir(niextract_folder)

    for i in tqdm(range(len(segment_times)-1)):
        if binned_counts[i] != 0: #prevents from processing GTIs with no counts
            outputfile = niextract_folder + 'ni' + obsid + '_nicersoft_bary_GTI'+str(i).zfill(6)+'_'+str(segment_length).zfill(5)+'s_' + 'E'+str(PI1).zfill(4)+'-'+str(PI2).zfill(4)+'.evt'
            if 'merged' in parent_folder:
                merged_id = str(pathlib.Path(eventfile).name)[:12]
                outputfile = niextract_folder + merged_id + '_nicersoft_bary_GTI' + str(i).zfill(6) + '_' + str(segment_length).zfill(5) + 's_E' + str(PI1).zfill(4) + '-' + str(PI2).zfill(4) + '.evt'
            if os.path.exists(outputfile)==False:
                subprocess.run(['niextract-events',eventfile+'[PI='+str(PI1)+':'+str(PI2)+']',outputfile,'timefile='+parent_folder+'/gtis/'+str(segment_length).zfill(5)+'s_GTI'+str(i).zfill(6)+'.gti'])

    return

    Using niextract-events to get segmented data based on [segment_length]-length
    GTIs that were created above, AND energy range!
    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the individual segments for combining power spectra
    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
    
Using niextract-events to get segmented data based on [segment_length]-length
GTIs that were created above, AND energy range.
eventfile - path to the event file. Will extract ObsID from this for the NICER files.
segment_length - length of the individual segments for combining power spectra
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
Using niextract - events to get segmented data based on [ segment_length ] - length GTIs that were created above AND energy range . eventfile - path to the event file . Will extract ObsID from this for the NICER files . segment_length - length of the individual segments for combining power spectra 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
def niextract_gti_time_energy(eventfile,segment_length,PI1,PI2):
    parent_folder = str(pathlib.Path(eventfile).parent)
    gtis = fits.open(eventfile)[2].data
    times = fits.open(eventfile)[1].data['TIME']
    event_header = fits.open(eventfile)[1].header
    obj_name = event_header['OBJECT']
    obsid = event_header['OBS_ID']
    Tobs_start = gtis[0][0] 
    Tobs_end = gtis[-1][1] 
    segment_times = np.arange(Tobs_start,Tobs_end+segment_length,segment_length) 
    binned_counts, bin_edges, binnumber = stats.binned_statistic(times,np.ones(len(times)),statistic='sum',bins=segment_times)
    niextract_folder = parent_folder + '/accelsearch_' + str(segment_length) + 's/'
    Lv2_mkdir.makedir(niextract_folder)
    for i in tqdm(range(len(segment_times)-1)):
        if binned_counts[i] != 0: 
            outputfile = niextract_folder + 'ni' + obsid + '_nicersoft_bary_GTI'+str(i).zfill(6)+'_'+str(segment_length).zfill(5)+'s_' + 'E'+str(PI1).zfill(4)+'-'+str(PI2).zfill(4)+'.evt'
            if 'merged' in parent_folder:
                merged_id = str(pathlib.Path(eventfile).name)[:12]
                outputfile = niextract_folder + merged_id + '_nicersoft_bary_GTI' + str(i).zfill(6) + '_' + str(segment_length).zfill(5) + 's_E' + str(PI1).zfill(4) + '-' + str(PI2).zfill(4) + '.evt'
            if os.path.exists(outputfile)==False:
                subprocess.run(['niextract-events',eventfile+'[PI='+str(PI1)+':'+str(PI2)+']',outputfile,'timefile='+parent_folder+'/gtis/'+str(segment_length).zfill(5)+'s_GTI'+str(i).zfill(6)+'.gti'])
    return
def niextract_gti_time_energy ( eventfile , segment_length , PI1 , PI2 ) : parent_folder = str ( pathlib . Path ( eventfile ) . parent ) gtis = fits . open ( eventfile ) [ 2 ] . data times = fits . open ( eventfile ) [ 1 ] . data [ 'TIME' ] event_header = fits . open ( eventfile ) [ 1 ] . header obj_name = event_header [ 'OBJECT' ] obsid = event_header [ 'OBS_ID' ] Tobs_start = gtis [ 0 ] [ 0 ] Tobs_end = gtis [ - 1 ] [ 1 ] segment_times = np . arange ( Tobs_start , Tobs_end + segment_length , segment_length ) binned_counts , bin_edges , binnumber = stats . binned_statistic ( times , np . ones ( len ( times ) ) , statistic = 'sum' , bins = segment_times ) niextract_folder = parent_folder + '/accelsearch_' + str ( segment_length ) + 's/' Lv2_mkdir . makedir ( niextract_folder ) for i in tqdm ( range ( len ( segment_times ) - 1 ) ) : if binned_counts [ i ] != 0 : outputfile = niextract_folder + 'ni' + obsid + '_nicersoft_bary_GTI' + str ( i ) . zfill ( 6 ) + '_' + str ( segment_length ) . zfill ( 5 ) + 's_' + 'E' + str ( PI1 ) . zfill ( 4 ) + '-' + str ( PI2 ) . zfill ( 4 ) + '.evt' if 'merged' in parent_folder : merged_id = str ( pathlib . Path ( eventfile ) . name ) [ : 12 ] outputfile = niextract_folder + merged_id + '_nicersoft_bary_GTI' + str ( i ) . zfill ( 6 ) + '_' + str ( segment_length ) . zfill ( 5 ) + 's_E' + str ( PI1 ) . zfill ( 4 ) + '-' + str ( PI2 ) . zfill ( 4 ) + '.evt' if os . path . exists ( outputfile ) == False : subprocess . run ( [ 'niextract-events' , eventfile + '[PI=' + str ( PI1 ) + ':' + str ( PI2 ) + ']' , outputfile , 'timefile=' + parent_folder + '/gtis/' + str ( segment_length ) . zfill ( 5 ) + 's_GTI' + str ( i ) . zfill ( 6 ) + '.gti' ] ) return
Using niextract-events to get segmented data based on [segment_length]-length
GTIs that were created above, AND energy range!
Using niextract - events to get segmented data based on [ segment_length ] - length GTIs that were created above AND energy range!
"""
    Using niextract-events to get segmented data based on [segment_length]-length
    GTIs that were created above, AND energy range!
    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the individual segments for combining power spectra
    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
    """ #MJD for the observation start time #MJD for the observation end time #    Tobs_start = event[1].data['TIME'][0] #    Tobs_end = event[1].data['TIME'][-1] #array of time values, starting #Jul 10: added Tobs_end + segment_length to get that final piece of data #bin edges defined by left boundary #prevents from processing GTIs with no counts
eventfile
segment_length
PI1
PI2