Lv2_average_ps_methods.py
def do_demodulate(eventfile,segment_length,mode,par_file):
    """
    Do orbital demodulation on the original events.

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the segments
    par_file - orbital parameter file for input into binary_psr
    mode - "all", "t" or "E" ; basically to tell the function where to access files to run do_demodulate
    """
    TIMEZERO = -1
    if mode == "all":
        parent_folder = str(pathlib.Path(eventfile).parent) + '/'
    elif mode == "t":
        parent_folder = str(pathlib.Path(eventfile).parent) + '/accelsearch_' + str(segment_length) + 's/'
    elif mode == "E":
        parent_folder = str(pathlib.Path(eventfile).parent) + '/accelsearch_E/'
    else:
        raise ValueError("mode should either of 'all', 't', or 'E'!")

    eventfiles = sorted(glob.glob(parent_folder + '*.evt')) #get absolute paths of all event FITS files
    for i in range(len(eventfiles)): #for every event file (e.g., for each segment)
        oldfile = eventfiles[i] #old event FITS file
        if len(fits.open(oldfile)[1].data['TIME']) == 0:
            continue
        newfile = eventfiles[i][:-4]+'_demod.evt' #new event FITS file, to be demodulated
        subprocess.run(['cp',oldfile,newfile])
        with fits.open(newfile,mode='update') as fitsfile_demod:
            MJDREFI = fitsfile_demod[1].header['MJDREFI'] #integer for MJD reference
            MJDREFF = fitsfile_demod[1].header['MJDREFF'] #float decimal for MJD reference

            times = fitsfile_demod[1].data['TIME'] #original time series
            gtis_start = fitsfile_demod[2].data['START'] #original GTI start times
            gtis_stop = fitsfile_demod[2].data['STOP'] #original GTI end times

            times_MJD = MJDREFI + MJDREFF + (TIMEZERO+times)/86400 #converting METs to MJD
            gtis_start_MJD = MJDREFI + MJDREFF + (TIMEZERO+gtis_start)/86400 #converting GTIs in METs to MJD
            gtis_stop_MJD = MJDREFI + MJDREFF + (TIMEZERO+gtis_stop)/86400 #converting GTIs in METs to MJD

            times_demod = binary_psr.binary_psr(par_file).demodulate_TOAs(times_MJD) #demodulated event times
            gtis_start_demod = binary_psr.binary_psr(par_file).demodulate_TOAs(gtis_start_MJD) #demodulated GTI start times
            gtis_stop_demod = binary_psr.binary_psr(par_file).demodulate_TOAs(gtis_stop_MJD) #demodulated GTI end times

            fitsfile_demod[1].data['TIME'] = (times_demod - MJDREFI - MJDREFF) * 86400 #convert back to METs
            fitsfile_demod[2].data['START'] = (gtis_start_demod - MJDREFI - MJDREFF) * 86400 #convert back to METs
            fitsfile_demod[2].data['STOP'] = (gtis_stop_demod - MJDREFI - MJDREFF) * 86400 #convert back to METs

            fitsfile_demod.flush()

    return

    Do orbital demodulation on the original events.

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the segments
    par_file - orbital parameter file for input into binary_psr
    mode - "all", "t" or "E" ; basically to tell the function where to access files to run do_demodulate
    
Do orbital demodulation on the original events.
eventfile - path to the event file. Will extract ObsID from this for the NICER files.
Do orbital demodulation on the original events . eventfile - path to the event file . Will extract ObsID from this for the NICER files .
def do_demodulate(eventfile,segment_length,mode,par_file):
    TIMEZERO = -1
    if mode == "all":
        parent_folder = str(pathlib.Path(eventfile).parent) + '/'
    elif mode == "t":
        parent_folder = str(pathlib.Path(eventfile).parent) + '/accelsearch_' + str(segment_length) + 's/'
    elif mode == "E":
        parent_folder = str(pathlib.Path(eventfile).parent) + '/accelsearch_E/'
    else:
        raise ValueError("mode should either of 'all', 't', or 'E'!")
    eventfiles = sorted(glob.glob(parent_folder + '*.evt')) 
    for i in range(len(eventfiles)): 
        oldfile = eventfiles[i] 
        if len(fits.open(oldfile)[1].data['TIME']) == 0:
            continue
        newfile = eventfiles[i][:-4]+'_demod.evt' 
        subprocess.run(['cp',oldfile,newfile])
        with fits.open(newfile,mode='update') as fitsfile_demod:
            MJDREFI = fitsfile_demod[1].header['MJDREFI'] 
            MJDREFF = fitsfile_demod[1].header['MJDREFF'] 
            times = fitsfile_demod[1].data['TIME'] 
            gtis_start = fitsfile_demod[2].data['START'] 
            gtis_stop = fitsfile_demod[2].data['STOP'] 
            times_MJD = MJDREFI + MJDREFF + (TIMEZERO+times)/86400 
            gtis_start_MJD = MJDREFI + MJDREFF + (TIMEZERO+gtis_start)/86400 
            gtis_stop_MJD = MJDREFI + MJDREFF + (TIMEZERO+gtis_stop)/86400 
            times_demod = binary_psr.binary_psr(par_file).demodulate_TOAs(times_MJD) 
            gtis_start_demod = binary_psr.binary_psr(par_file).demodulate_TOAs(gtis_start_MJD) 
            gtis_stop_demod = binary_psr.binary_psr(par_file).demodulate_TOAs(gtis_stop_MJD) 
            fitsfile_demod[1].data['TIME'] = (times_demod - MJDREFI - MJDREFF) * 86400 
            fitsfile_demod[2].data['START'] = (gtis_start_demod - MJDREFI - MJDREFF) * 86400 
            fitsfile_demod[2].data['STOP'] = (gtis_stop_demod - MJDREFI - MJDREFF) * 86400 
            fitsfile_demod.flush()
    return
def do_demodulate ( eventfile , segment_length , mode , par_file ) : TIMEZERO = - 1 if mode == "all" : parent_folder = str ( pathlib . Path ( eventfile ) . parent ) + '/' elif mode == "t" : parent_folder = str ( pathlib . Path ( eventfile ) . parent ) + '/accelsearch_' + str ( segment_length ) + 's/' elif mode == "E" : parent_folder = str ( pathlib . Path ( eventfile ) . parent ) + '/accelsearch_E/' else : raise ValueError ( "mode should either of 'all', 't', or 'E'!" ) eventfiles = sorted ( glob . glob ( parent_folder + '*.evt' ) ) for i in range ( len ( eventfiles ) ) : oldfile = eventfiles [ i ] if len ( fits . open ( oldfile ) [ 1 ] . data [ 'TIME' ] ) == 0 : continue newfile = eventfiles [ i ] [ : - 4 ] + '_demod.evt' subprocess . run ( [ 'cp' , oldfile , newfile ] ) with fits . open ( newfile , mode = 'update' ) as fitsfile_demod : MJDREFI = fitsfile_demod [ 1 ] . header [ 'MJDREFI' ] MJDREFF = fitsfile_demod [ 1 ] . header [ 'MJDREFF' ] times = fitsfile_demod [ 1 ] . data [ 'TIME' ] gtis_start = fitsfile_demod [ 2 ] . data [ 'START' ] gtis_stop = fitsfile_demod [ 2 ] . data [ 'STOP' ] times_MJD = MJDREFI + MJDREFF + ( TIMEZERO + times ) / 86400 gtis_start_MJD = MJDREFI + MJDREFF + ( TIMEZERO + gtis_start ) / 86400 gtis_stop_MJD = MJDREFI + MJDREFF + ( TIMEZERO + gtis_stop ) / 86400 times_demod = binary_psr . binary_psr ( par_file ) . demodulate_TOAs ( times_MJD ) gtis_start_demod = binary_psr . binary_psr ( par_file ) . demodulate_TOAs ( gtis_start_MJD ) gtis_stop_demod = binary_psr . binary_psr ( par_file ) . demodulate_TOAs ( gtis_stop_MJD ) fitsfile_demod [ 1 ] . data [ 'TIME' ] = ( times_demod - MJDREFI - MJDREFF ) * 86400 fitsfile_demod [ 2 ] . data [ 'START' ] = ( gtis_start_demod - MJDREFI - MJDREFF ) * 86400 fitsfile_demod [ 2 ] . data [ 'STOP' ] = ( gtis_stop_demod - MJDREFI - MJDREFF ) * 86400 fitsfile_demod . flush ( ) return
Do orbital demodulation on the original events.
Do orbital demodulation on the original events .
"""
    Do orbital demodulation on the original events.

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the segments
    par_file - orbital parameter file for input into binary_psr
    mode - "all", "t" or "E" ; basically to tell the function where to access files to run do_demodulate
    """ #get absolute paths of all event FITS files #for every event file (e.g., for each segment) #old event FITS file #new event FITS file, to be demodulated #integer for MJD reference #float decimal for MJD reference #original time series #original GTI start times #original GTI end times #converting METs to MJD #converting GTIs in METs to MJD #converting GTIs in METs to MJD #demodulated event times #demodulated GTI start times #demodulated GTI end times #convert back to METs #convert back to METs #convert back to METs
eventfile
segment_length
mode
par_file