Lv2_efsearch.py
def efsearch(eventfile,n_segments,dper,nphase,nbint,nper,dres,outfile_root,plot_efsearch):
    """
    Performing FTOOLS' efsearch!

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    n_segments - no. of segments to break the epoch folding search into
    dper - value for period used in the folding; input represents center of range of trial periods
    nphase - no. of phases in the folded light curve(s) ; "INDEF" uses the default value
    nbint - the number of newbins per interval used in the analysis
    nper - no. of periods over which the search is carried out
    dres - period resolution is the spacing between two contiguous periods in the search
           "INDEF" uses default value of half the Fourier resolution in the interval
    outfile_root - prefix for the end file name
    plot_efsearch - 'yes' or 'no' to plot the results from efsearch; do "exit" for the next plot!
    """

    efsearch_cmd = ['efsearch',eventfile,'window="-"','sepoch=INDEF','dper='+str(dper),'nphase='+str(nphase),'nbint='+str(nbint),'nper='+str(nper),'dres='+str(dres),'outfile='+outfile_root,'outfiletype=2','plot='+plot_efsearch]

    parent_folder = str(pathlib.Path(eventfile).parent)
    efsearch_logfile = parent_folder + '/efsearch.log'

    with open(efsearch_logfile,'w') as logtextfile:
        output = subprocess.run(efsearch_cmd,capture_output=True,text=True)
        logtextfile.write(output.stdout)
        logtextfile.write('*------------------------------* \n')
        logtextfile.write(output.stderr)
    logtextfile.close()

    subprocess.run(['mv',outfile_root+'.fes',parent_folder + '/' + outfile_root + '.fes'])

    efsearch_results = fits.open(parent_folder + '/' + outfile_root + '.fes')
    efsearch_plot = parent_folder + '/' + outfile_root + '.pdf'

    with PdfPages(efsearch_plot) as pdf:
        for i in tqdm(range(1,len(efsearch_results))):
            T_start = efsearch_results[i].header['TSTARTI'] + efsearch_results[i].header['TSTARTF']
            T_stop = efsearch_results[i].header['TSTOPI'] + efsearch_results[i].header['TSTOPF']
            T_zeroref = efsearch_results[i].header['TIMEZERI'] + efsearch_results[i].header['TIMEZERF']

            ### extracting period, chi-squared (+ error) values from the efsearch results
            period = efsearch_results[i].data['PERIOD']
            chisqrd1 = efsearch_results[i].data['CHISQRD1']
            error1 = efsearch_results[i].data['ERROR1']

            ### First find the peaks in the plot
            peak_indices = peakutils.indexes(chisqrd1,thres=0.2,min_dist=0.0001)

            ### plotting the chi-squared vs period plots and the corresponding Gaussian fits
            plt.errorbar(x=period,y=chisqrd1,yerr=error1,fmt='x')
            plt.title('TSTART: ' + str((T_start-T_zeroref)*86400) + ' ; TSTOP: ' + str((T_stop-T_zeroref)*86400))
            plt.xlabel('Period (s)',fontsize=12)
            plt.ylabel('chi-squared',fontsize=12)
            pdf.savefig()
            plt.close()

    Performing FTOOLS' efsearch!

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    n_segments - no. of segments to break the epoch folding search into
    dper - value for period used in the folding; input represents center of range of trial periods
    nphase - no. of phases in the folded light curve(s) ; "INDEF" uses the default value
    nbint - the number of newbins per interval used in the analysis
    nper - no. of periods over which the search is carried out
    dres - period resolution is the spacing between two contiguous periods in the search
           "INDEF" uses default value of half the Fourier resolution in the interval
    outfile_root - prefix for the end file name
    plot_efsearch - 'yes' or 'no' to plot the results from efsearch; do "exit" for the next plot!
    
Performing FTOOLS' efsearch.
eventfile - path to the event file. Will extract ObsID from this for the NICER files.
Performing FTOOLS ' efsearch . eventfile - path to the event file . Will extract ObsID from this for the NICER files .
def efsearch(eventfile,n_segments,dper,nphase,nbint,nper,dres,outfile_root,plot_efsearch):
    efsearch_cmd = ['efsearch',eventfile,'window="-"','sepoch=INDEF','dper='+str(dper),'nphase='+str(nphase),'nbint='+str(nbint),'nper='+str(nper),'dres='+str(dres),'outfile='+outfile_root,'outfiletype=2','plot='+plot_efsearch]
    parent_folder = str(pathlib.Path(eventfile).parent)
    efsearch_logfile = parent_folder + '/efsearch.log'
    with open(efsearch_logfile,'w') as logtextfile:
        output = subprocess.run(efsearch_cmd,capture_output=True,text=True)
        logtextfile.write(output.stdout)
        logtextfile.write('*------------------------------* \n')
        logtextfile.write(output.stderr)
    logtextfile.close()
    subprocess.run(['mv',outfile_root+'.fes',parent_folder + '/' + outfile_root + '.fes'])
    efsearch_results = fits.open(parent_folder + '/' + outfile_root + '.fes')
    efsearch_plot = parent_folder + '/' + outfile_root + '.pdf'
    with PdfPages(efsearch_plot) as pdf:
        for i in tqdm(range(1,len(efsearch_results))):
            T_start = efsearch_results[i].header['TSTARTI'] + efsearch_results[i].header['TSTARTF']
            T_stop = efsearch_results[i].header['TSTOPI'] + efsearch_results[i].header['TSTOPF']
            T_zeroref = efsearch_results[i].header['TIMEZERI'] + efsearch_results[i].header['TIMEZERF']
            period = efsearch_results[i].data['PERIOD']
            chisqrd1 = efsearch_results[i].data['CHISQRD1']
            error1 = efsearch_results[i].data['ERROR1']
            peak_indices = peakutils.indexes(chisqrd1,thres=0.2,min_dist=0.0001)
            plt.errorbar(x=period,y=chisqrd1,yerr=error1,fmt='x')
            plt.title('TSTART: ' + str((T_start-T_zeroref)*86400) + ' ; TSTOP: ' + str((T_stop-T_zeroref)*86400))
            plt.xlabel('Period (s)',fontsize=12)
            plt.ylabel('chi-squared',fontsize=12)
            pdf.savefig()
            plt.close()
def efsearch ( eventfile , n_segments , dper , nphase , nbint , nper , dres , outfile_root , plot_efsearch ) : efsearch_cmd = [ 'efsearch' , eventfile , 'window="-"' , 'sepoch=INDEF' , 'dper=' + str ( dper ) , 'nphase=' + str ( nphase ) , 'nbint=' + str ( nbint ) , 'nper=' + str ( nper ) , 'dres=' + str ( dres ) , 'outfile=' + outfile_root , 'outfiletype=2' , 'plot=' + plot_efsearch ] parent_folder = str ( pathlib . Path ( eventfile ) . parent ) efsearch_logfile = parent_folder + '/efsearch.log' with open ( efsearch_logfile , 'w' ) as logtextfile : output = subprocess . run ( efsearch_cmd , capture_output = True , text = True ) logtextfile . write ( output . stdout ) logtextfile . write ( '*------------------------------* \n' ) logtextfile . write ( output . stderr ) logtextfile . close ( ) subprocess . run ( [ 'mv' , outfile_root + '.fes' , parent_folder + '/' + outfile_root + '.fes' ] ) efsearch_results = fits . open ( parent_folder + '/' + outfile_root + '.fes' ) efsearch_plot = parent_folder + '/' + outfile_root + '.pdf' with PdfPages ( efsearch_plot ) as pdf : for i in tqdm ( range ( 1 , len ( efsearch_results ) ) ) : T_start = efsearch_results [ i ] . header [ 'TSTARTI' ] + efsearch_results [ i ] . header [ 'TSTARTF' ] T_stop = efsearch_results [ i ] . header [ 'TSTOPI' ] + efsearch_results [ i ] . header [ 'TSTOPF' ] T_zeroref = efsearch_results [ i ] . header [ 'TIMEZERI' ] + efsearch_results [ i ] . header [ 'TIMEZERF' ] period = efsearch_results [ i ] . data [ 'PERIOD' ] chisqrd1 = efsearch_results [ i ] . data [ 'CHISQRD1' ] error1 = efsearch_results [ i ] . data [ 'ERROR1' ] peak_indices = peakutils . indexes ( chisqrd1 , thres = 0.2 , min_dist = 0.0001 ) plt . errorbar ( x = period , y = chisqrd1 , yerr = error1 , fmt = 'x' ) plt . title ( 'TSTART: ' + str ( ( T_start - T_zeroref ) * 86400 ) + ' ; TSTOP: ' + str ( ( T_stop - T_zeroref ) * 86400 ) ) plt . xlabel ( 'Period (s)' , fontsize = 12 ) plt . ylabel ( 'chi-squared' , fontsize = 12 ) pdf . savefig ( ) plt . close ( )
Performing FTOOLS' efsearch!
Performing FTOOLS ' efsearch!
"""
    Performing FTOOLS' efsearch!

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    n_segments - no. of segments to break the epoch folding search into
    dper - value for period used in the folding; input represents center of range of trial periods
    nphase - no. of phases in the folded light curve(s) ; "INDEF" uses the default value
    nbint - the number of newbins per interval used in the analysis
    nper - no. of periods over which the search is carried out
    dres - period resolution is the spacing between two contiguous periods in the search
           "INDEF" uses default value of half the Fourier resolution in the interval
    outfile_root - prefix for the end file name
    plot_efsearch - 'yes' or 'no' to plot the results from efsearch; do "exit" for the next plot!
    """ ### extracting period, chi-squared (+ error) values from the efsearch results ### First find the peaks in the plot ### plotting the chi-squared vs period plots and the corresponding Gaussian fits
eventfile
n_segments
dper
nphase
nbint
nper
dres
outfile_root
plot_efsearch