Lv2_presto_subroutines.py
def edit_binary(eventfile,tbin,segment_length):
    """
    To pad the binary file so that it will be as long as the desired segment length.
    The value to pad with for each time bin, is the average count rate in THAT segment!
    Jul 10: Do zero-padding instead... so that number of counts is consistent!
    Again, this is only for when we make segments by time!
    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
    """
    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).zfill(5) + 's*.dat')) #not that order matters here I think, but just in case
    #dat_files = sorted(glob.glob(parent_folder + '/accelsearch_GTIs/*GTI*.dat')) #not that order matters here I think, but just in case

    for i in tqdm(range(len(dat_files))):
        bins = np.fromfile(dat_files[i],dtype='<f',count=-1) #reads the binary file ; converts to little endian, count=-1 means grab everything
#        bins_with_data = len(bins[bins>0]) #number of bins with data (NOT the ones with averaged count rate!)
#        average_count_rate = sum(bins)/len(bins)

        no_desired_bins = float(segment_length)/float(tbin) #TOTAL number of desired bins for the segment
        no_padded = int(no_desired_bins - len(bins)) #number of bins needed to reach the TOTAL number of desired bins
        if no_padded >= 0:
            #padding = np.ones(no_padded,dtype=np.float32)*average_count_rate #generate the array of (averaged) counts needed to pad the original segment
            padding = np.zeros(no_padded,dtype=np.float32) #just in case this is ever needed...
            new_bins = np.array(list(bins) + list(padding))
            new_bins.tofile(dat_files[i]) #don't need to do mv since obsdir already has absolute path to the SSD
        else:
            new_bins = bins[:int(no_desired_bins)] #truncate the original series; say we had a 1000s segment, but
            #nicerfits2presto went up to 1008s, so take that last 8s away because there's no data in it anyways...
            new_bins.tofile(dat_files[i])

    return

    To pad the binary file so that it will be as long as the desired segment length.
    The value to pad with for each time bin, is the average count rate in THAT segment!
    Jul 10: Do zero-padding instead... so that number of counts is consistent!
    Again, this is only for when we make segments by time!
    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
    
To pad the binary file so that it will be as long as the desired segment length.
The value to pad with for each time bin, is the average count rate in THAT segment.
Jul 10: Do zero-padding instead... so that number of counts is consistent.
Again, this is only for when we make segments by time.
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
To pad the binary file so that it will be as long as the desired segment length . The value to pad with for each time bin is the average count rate in THAT segment . Jul 10 : Do zero - padding instead ... so that number of counts is consistent . Again this is only for when we make segments by time . 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
def edit_binary(eventfile,tbin,segment_length):
    parent_folder = str(pathlib.Path(eventfile).parent)
    event_header = fits.open(eventfile)[1].header
    dat_files = sorted(glob.glob(parent_folder + '/accelsearch_' + str(segment_length) + 's/*GTI*' + str(segment_length).zfill(5) + 's*.dat')) 
    #dat_files = sorted(glob.glob(parent_folder + '/accelsearch_GTIs/*GTI*.dat')) 
    for i in tqdm(range(len(dat_files))):
        bins = np.fromfile(dat_files[i],dtype='<f',count=-1) 
        no_desired_bins = float(segment_length)/float(tbin) 
        no_padded = int(no_desired_bins - len(bins)) 
        if no_padded >= 0:
            padding = np.zeros(no_padded,dtype=np.float32) 
            new_bins = np.array(list(bins) + list(padding))
            new_bins.tofile(dat_files[i]) 
        else:
            new_bins = bins[:int(no_desired_bins)] 
            new_bins.tofile(dat_files[i])
    return
def edit_binary ( eventfile , tbin , segment_length ) : parent_folder = str ( pathlib . Path ( eventfile ) . parent ) event_header = fits . open ( eventfile ) [ 1 ] . header dat_files = sorted ( glob . glob ( parent_folder + '/accelsearch_' + str ( segment_length ) + 's/*GTI*' + str ( segment_length ) . zfill ( 5 ) + 's*.dat' ) ) for i in tqdm ( range ( len ( dat_files ) ) ) : bins = np . fromfile ( dat_files [ i ] , dtype = '<f' , count = - 1 ) no_desired_bins = float ( segment_length ) / float ( tbin ) no_padded = int ( no_desired_bins - len ( bins ) ) if no_padded >= 0 : padding = np . zeros ( no_padded , dtype = np . float32 ) new_bins = np . array ( list ( bins ) + list ( padding ) ) new_bins . tofile ( dat_files [ i ] ) else : new_bins = bins [ : int ( no_desired_bins ) ] new_bins . tofile ( dat_files [ i ] ) return
To pad the binary file so that it will be as long as the desired segment length.
To pad the binary file so that it will be as long as the desired segment length .
"""
    To pad the binary file so that it will be as long as the desired segment length.
    The value to pad with for each time bin, is the average count rate in THAT segment!
    Jul 10: Do zero-padding instead... so that number of counts is consistent!
    Again, this is only for when we make segments by time!
    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
    """ #obj_name = event_header['OBJECT'] #obsid = event_header['OBS_ID'] #not that order matters here I think, but just in case #dat_files = sorted(glob.glob(parent_folder + '/accelsearch_GTIs/*GTI*.dat')) #not that order matters here I think, but just in case #reads the binary file ; converts to little endian, count=-1 means grab everything #        bins_with_data = len(bins[bins>0]) #number of bins with data (NOT the ones with averaged count rate!) #        average_count_rate = sum(bins)/len(bins) #TOTAL number of desired bins for the segment #number of bins needed to reach the TOTAL number of desired bins #padding = np.ones(no_padded,dtype=np.float32)*average_count_rate #generate the array of (averaged) counts needed to pad the original segment #just in case this is ever needed... #don't need to do mv since obsdir already has absolute path to the SSD #truncate the original series; say we had a 1000s segment, but #nicerfits2presto went up to 1008s, so take that last 8s away because there's no data in it anyways...
eventfile
tbin
segment_length