Lv2_presto_subroutines.py
def filter_accelsearch(eventfile,mode,min_freq,zmax):
    """
    Added on 8/31/2020. To filter out the ACCEL files by displaying a list of candidates
    that have met a frequency threshold (to avoid very low frequency candidates, really)

    The files will have been generated via prepfold already - perhaps in the future, I may
    only run prepfold on candidates I want! Maybe do this to avoid having too many candidates...

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    mode - "all", "t", "gtis", or "E" ; basically to tell the function where to access files to run prepfold for
    min_freq - minimum frequency to include in the list
    zmax - maximum acceleration
    """
    parent_folder = str(pathlib.Path(eventfile).parent)

    if mode == "all":
        ACCEL_files = sorted(glob.glob(parent_folder+'/*ACCEL_'+str(zmax)))
    elif mode == "t":
        ACCEL_files = sorted(glob.glob(parent_folder+'/accelsearch_'+str(segment_length)+'s/*ACCEL_'+str(zmax)))
    elif mode == "E":
        ACCEL_files = sorted(glob.glob(parent_folder+'/accelsearch_E/*ACCEL_'+str(zmax)))
    elif mode == "gtis":
        ACCEL_files = sorted(glob.glob(parent_folder+'/accelsearch_GTIs/*ACCEL_'+str(zmax)))
    else:
        raise ValueError("mode should either of 'all', 't', 'gtis', or 'E'!")

    ### below are just the headers in the *ACCEL_100 files ; the intention is just to get the indices for these lines later on
    header1 = "             Summed  Coherent  Num        Period          Frequency         FFT 'r'        Freq Deriv       FFT 'z'         Accel                           "
    header2 = "                        Power /          Raw           FFT 'r'          Pred 'r'       FFT 'z'     Pred 'z'      Phase       Centroid     Purity                        "

    output_file = open(str(pathlib.Path(ACCEL_files[0]).parent) + '/ACCEL_' + str(zmax) + '_cands_minfreq_' + str(min_freq) + '.txt','w')
    output_file.write(header1 + '\n')

    for i in range(len(ACCEL_files)):
        freqs = []
        accel_textfile = np.array(open(ACCEL_files[i],'r').read().split('\n')) #read the data from the ACCEL_$zmax files
        index_header1 = np.where(accel_textfile==header1)[0][0] #index for "Summed, Coherent, Num, Period etc
        index_header2 = np.where(accel_textfile==header2)[0][0] #index for "Power / Raw  FFT  'r'  etc
        no_cands = index_header2 - index_header1 - 5 #to obtain number of candidates
        for j in range(no_cands):
            freq = float(accel_textfile[j+3].split()[6][:-3])
            freqs.append(freq)
        if not any(np.array(freqs)>min_freq): #if none are above the minimum frequency
            continue
        else:
            output_file.write(str(pathlib.Path(ACCEL_files[i]).name) + '\n')
            for j in range(no_cands):
                freq = float(accel_textfile[j+3].split()[6][:-3])
                if freq >= min_freq:
                    output_file.write(accel_textfile[j+3] + '\n')
    output_file.close()

    Added on 8/31/2020. To filter out the ACCEL files by displaying a list of candidates
    that have met a frequency threshold (to avoid very low frequency candidates, really)

    The files will have been generated via prepfold already - perhaps in the future, I may
    only run prepfold on candidates I want! Maybe do this to avoid having too many candidates...

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    mode - "all", "t", "gtis", or "E" ; basically to tell the function where to access files to run prepfold for
    min_freq - minimum frequency to include in the list
    zmax - maximum acceleration
    


The files will have been generated via prepfold already - perhaps in the future, I may
only run prepfold on candidates I want. Maybe do this to avoid having too many candidates

path to the event file. Will extract ObsID from this for the NICER files.
The files will have been generated via prepfold already - perhaps in the future I may only run prepfold on candidates I want . Maybe do this to avoid having too many candidates path to the event file . Will extract ObsID from this for the NICER files .
def filter_accelsearch(eventfile,mode,min_freq,zmax):
    parent_folder = str(pathlib.Path(eventfile).parent)
    if mode == "all":
        ACCEL_files = sorted(glob.glob(parent_folder+'/*ACCEL_'+str(zmax)))
    elif mode == "t":
        ACCEL_files = sorted(glob.glob(parent_folder+'/accelsearch_'+str(segment_length)+'s/*ACCEL_'+str(zmax)))
    elif mode == "E":
        ACCEL_files = sorted(glob.glob(parent_folder+'/accelsearch_E/*ACCEL_'+str(zmax)))
    elif mode == "gtis":
        ACCEL_files = sorted(glob.glob(parent_folder+'/accelsearch_GTIs/*ACCEL_'+str(zmax)))
    else:
        raise ValueError("mode should either of 'all', 't', 'gtis', or 'E'!")
    header1 = "             Summed  Coherent  Num        Period          Frequency         FFT 'r'        Freq Deriv       FFT 'z'         Accel                           "
    header2 = "                        Power /          Raw           FFT 'r'          Pred 'r'       FFT 'z'     Pred 'z'      Phase       Centroid     Purity                        "
    output_file = open(str(pathlib.Path(ACCEL_files[0]).parent) + '/ACCEL_' + str(zmax) + '_cands_minfreq_' + str(min_freq) + '.txt','w')
    output_file.write(header1 + '\n')
    for i in range(len(ACCEL_files)):
        freqs = []
        accel_textfile = np.array(open(ACCEL_files[i],'r').read().split('\n')) 
        index_header1 = np.where(accel_textfile==header1)[0][0] 
        index_header2 = np.where(accel_textfile==header2)[0][0] 
        no_cands = index_header2 - index_header1 - 5 
        for j in range(no_cands):
            freq = float(accel_textfile[j+3].split()[6][:-3])
            freqs.append(freq)
        if not any(np.array(freqs)>min_freq): 
            continue
        else:
            output_file.write(str(pathlib.Path(ACCEL_files[i]).name) + '\n')
            for j in range(no_cands):
                freq = float(accel_textfile[j+3].split()[6][:-3])
                if freq >= min_freq:
                    output_file.write(accel_textfile[j+3] + '\n')
    output_file.close()
def filter_accelsearch ( eventfile , mode , min_freq , zmax ) : parent_folder = str ( pathlib . Path ( eventfile ) . parent ) if mode == "all" : ACCEL_files = sorted ( glob . glob ( parent_folder + '/*ACCEL_' + str ( zmax ) ) ) elif mode == "t" : ACCEL_files = sorted ( glob . glob ( parent_folder + '/accelsearch_' + str ( segment_length ) + 's/*ACCEL_' + str ( zmax ) ) ) elif mode == "E" : ACCEL_files = sorted ( glob . glob ( parent_folder + '/accelsearch_E/*ACCEL_' + str ( zmax ) ) ) elif mode == "gtis" : ACCEL_files = sorted ( glob . glob ( parent_folder + '/accelsearch_GTIs/*ACCEL_' + str ( zmax ) ) ) else : raise ValueError ( "mode should either of 'all', 't', 'gtis', or 'E'!" ) header1 = "             Summed  Coherent  Num        Period          Frequency         FFT 'r'        Freq Deriv       FFT 'z'         Accel                           " header2 = "                        Power /          Raw           FFT 'r'          Pred 'r'       FFT 'z'     Pred 'z'      Phase       Centroid     Purity                        " output_file = open ( str ( pathlib . Path ( ACCEL_files [ 0 ] ) . parent ) + '/ACCEL_' + str ( zmax ) + '_cands_minfreq_' + str ( min_freq ) + '.txt' , 'w' ) output_file . write ( header1 + '\n' ) for i in range ( len ( ACCEL_files ) ) : freqs = [ ] accel_textfile = np . array ( open ( ACCEL_files [ i ] , 'r' ) . read ( ) . split ( '\n' ) ) index_header1 = np . where ( accel_textfile == header1 ) [ 0 ] [ 0 ] index_header2 = np . where ( accel_textfile == header2 ) [ 0 ] [ 0 ] no_cands = index_header2 - index_header1 - 5 for j in range ( no_cands ) : freq = float ( accel_textfile [ j + 3 ] . split ( ) [ 6 ] [ : - 3 ] ) freqs . append ( freq ) if not any ( np . array ( freqs ) > min_freq ) : continue else : output_file . write ( str ( pathlib . Path ( ACCEL_files [ i ] ) . name ) + '\n' ) for j in range ( no_cands ) : freq = float ( accel_textfile [ j + 3 ] . split ( ) [ 6 ] [ : - 3 ] ) if freq >= min_freq : output_file . write ( accel_textfile [ j + 3 ] + '\n' ) output_file . close ( )
Added on 8/31/2020.
Added on 8 / 31 / 2020 .
"""
    Added on 8/31/2020. To filter out the ACCEL files by displaying a list of candidates
    that have met a frequency threshold (to avoid very low frequency candidates, really)

    The files will have been generated via prepfold already - perhaps in the future, I may
    only run prepfold on candidates I want! Maybe do this to avoid having too many candidates...

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    mode - "all", "t", "gtis", or "E" ; basically to tell the function where to access files to run prepfold for
    min_freq - minimum frequency to include in the list
    zmax - maximum acceleration
    """ ### below are just the headers in the *ACCEL_100 files ; the intention is just to get the indices for these lines later on #read the data from the ACCEL_$zmax files #index for "Summed, Coherent, Num, Period etc #index for "Power / Raw  FFT  'r'  etc #to obtain number of candidates #if none are above the minimum frequency
eventfile
mode
min_freq
zmax