Lv2_presto_subroutines.py
def prepfold(eventfile,segment_length,mode,zmax):
    """
    Performing PRESTO's prepfold on the pulsation candidates.
    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the individual segments
    mode - "all", "t", "gtis", or "E" ; basically to tell the function where to access files to run prepfold for
    zmax - maximum acceleration
    """
    parent_folder = str(pathlib.Path(eventfile).parent)

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

    cand_files = [ACCEL_files[i] + '.cand' for i in range(len(ACCEL_files))]
    if zmax < 10:
        events_files = [cand_files[i][:-13]+'.events' for i in range(len(cand_files))]
    if (zmax >= 10) & (zmax < 100):
        events_files = [cand_files[i][:-14]+'.events' for i in range(len(cand_files))]
    if (zmax >= 100) & (zmax < 999):
        events_files = [cand_files[i][:-15]+'.events' for i in range(len(cand_files))]
    else:
        events_files = [cand_files[i][:-16]+'.events' for i in range(len(cand_files))]

    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                        "

    with open(logfile,'w') as logtextfile:
        for i in range(len(ACCEL_files)): #for each successful ACCEL_zmax file:
            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
            cand_relpath = relpath(cand_files[i],parent_folder) #relative path of .cand file ; PRESTO doesn't like using absolute paths
            events_relpath = relpath(events_files[i],parent_folder) #relative path of .events file ; PRESTO doesn't like using absolute paths

            for j in range(min(50,no_cands)): #to control the number of outputs! Sometimes there can be thousands of candidates - insane!
                command = 'cd ' + parent_folder + ' ; prepfold -double -events -noxwin -n 20 -accelcand ' + str(j+1) + ' -accelfile ' + cand_relpath + ' ' + events_relpath
                output = subprocess.run(command,shell=True,capture_output=True,text=True)
                logtextfile.write(output.stdout)
                logtextfile.write('*------------------------------* \n')
                logtextfile.write(output.stderr)
        logtextfile.close()

    return

    Performing PRESTO's prepfold on the pulsation candidates.
    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the individual segments
    mode - "all", "t", "gtis", or "E" ; basically to tell the function where to access files to run prepfold for
    zmax - maximum acceleration
    
Performing PRESTO's prepfold on the pulsation candidates.
eventfile - path to the event file. Will extract ObsID from this for the NICER files.
Performing PRESTO ' s prepfold on the pulsation candidates . eventfile - path to the event file . Will extract ObsID from this for the NICER files .
def prepfold(eventfile,segment_length,mode,zmax):
    parent_folder = str(pathlib.Path(eventfile).parent)
    if mode == "all":
        ACCEL_files = sorted(glob.glob(parent_folder+'/*ACCEL_'+str(zmax)))
        logfile = parent_folder + '/prepfold_all.log'
    elif mode == "t":
        ACCEL_files = sorted(glob.glob(parent_folder+'/accelsearch_'+str(segment_length)+'s/*ACCEL_'+str(zmax)))
        logfile = parent_folder + '/accelsearch_' + str(segment_length) + 's/prepfold_t.log'
    elif mode == "E":
        ACCEL_files = sorted(glob.glob(parent_folder+'/accelsearch_E/*ACCEL_'+str(zmax)))
        logfile = parent_folder + '/accelsearch_E/prepfold.log'
    elif mode == "gtis":
        ACCEL_files = sorted(glob.glob(parent_folder+'/accelsearch_GTIs/*ACCEL_'+str(zmax)))
        logfile = parent_folder + '/accelsearch_GTIs/prepfold.log'
    else:
        raise ValueError("mode should either of 'all', 't', 'gtis', or 'E'!")
    cand_files = [ACCEL_files[i] + '.cand' for i in range(len(ACCEL_files))]
    if zmax < 10:
        events_files = [cand_files[i][:-13]+'.events' for i in range(len(cand_files))]
    if (zmax >= 10) & (zmax < 100):
        events_files = [cand_files[i][:-14]+'.events' for i in range(len(cand_files))]
    if (zmax >= 100) & (zmax < 999):
        events_files = [cand_files[i][:-15]+'.events' for i in range(len(cand_files))]
    else:
        events_files = [cand_files[i][:-16]+'.events' for i in range(len(cand_files))]
    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                        "
    with open(logfile,'w') as logtextfile:
        for i in range(len(ACCEL_files)): 
            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 
            cand_relpath = relpath(cand_files[i],parent_folder) 
            events_relpath = relpath(events_files[i],parent_folder) 
            for j in range(min(50,no_cands)): 
                command = 'cd ' + parent_folder + ' ; prepfold -double -events -noxwin -n 20 -accelcand ' + str(j+1) + ' -accelfile ' + cand_relpath + ' ' + events_relpath
                output = subprocess.run(command,shell=True,capture_output=True,text=True)
                logtextfile.write(output.stdout)
                logtextfile.write('*------------------------------* \n')
                logtextfile.write(output.stderr)
        logtextfile.close()
    return
def prepfold ( eventfile , segment_length , mode , zmax ) : parent_folder = str ( pathlib . Path ( eventfile ) . parent ) if mode == "all" : ACCEL_files = sorted ( glob . glob ( parent_folder + '/*ACCEL_' + str ( zmax ) ) ) logfile = parent_folder + '/prepfold_all.log' elif mode == "t" : ACCEL_files = sorted ( glob . glob ( parent_folder + '/accelsearch_' + str ( segment_length ) + 's/*ACCEL_' + str ( zmax ) ) ) logfile = parent_folder + '/accelsearch_' + str ( segment_length ) + 's/prepfold_t.log' elif mode == "E" : ACCEL_files = sorted ( glob . glob ( parent_folder + '/accelsearch_E/*ACCEL_' + str ( zmax ) ) ) logfile = parent_folder + '/accelsearch_E/prepfold.log' elif mode == "gtis" : ACCEL_files = sorted ( glob . glob ( parent_folder + '/accelsearch_GTIs/*ACCEL_' + str ( zmax ) ) ) logfile = parent_folder + '/accelsearch_GTIs/prepfold.log' else : raise ValueError ( "mode should either of 'all', 't', 'gtis', or 'E'!" ) cand_files = [ ACCEL_files [ i ] + '.cand' for i in range ( len ( ACCEL_files ) ) ] if zmax < 10 : events_files = [ cand_files [ i ] [ : - 13 ] + '.events' for i in range ( len ( cand_files ) ) ] if ( zmax >= 10 ) & ( zmax < 100 ) : events_files = [ cand_files [ i ] [ : - 14 ] + '.events' for i in range ( len ( cand_files ) ) ] if ( zmax >= 100 ) & ( zmax < 999 ) : events_files = [ cand_files [ i ] [ : - 15 ] + '.events' for i in range ( len ( cand_files ) ) ] else : events_files = [ cand_files [ i ] [ : - 16 ] + '.events' for i in range ( len ( cand_files ) ) ] 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                        " with open ( logfile , 'w' ) as logtextfile : for i in range ( len ( ACCEL_files ) ) : 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 cand_relpath = relpath ( cand_files [ i ] , parent_folder ) events_relpath = relpath ( events_files [ i ] , parent_folder ) for j in range ( min ( 50 , no_cands ) ) : command = 'cd ' + parent_folder + ' ; prepfold -double -events -noxwin -n 20 -accelcand ' + str ( j + 1 ) + ' -accelfile ' + cand_relpath + ' ' + events_relpath output = subprocess . run ( command , shell = True , capture_output = True , text = True ) logtextfile . write ( output . stdout ) logtextfile . write ( '*------------------------------* \n' ) logtextfile . write ( output . stderr ) logtextfile . close ( ) return
Performing PRESTO's prepfold on the pulsation candidates.
Performing PRESTO ' s prepfold on the pulsation candidates .
"""
    Performing PRESTO's prepfold on the pulsation candidates.
    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the individual segments
    mode - "all", "t", "gtis", or "E" ; basically to tell the function where to access files to run prepfold for
    zmax - maximum acceleration
    """ #for each successful ACCEL_zmax file: #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 #relative path of .cand file ; PRESTO doesn't like using absolute paths #relative path of .events file ; PRESTO doesn't like using absolute paths #to control the number of outputs! Sometimes there can be thousands of candidates - insane!
eventfile
segment_length
mode
zmax