Lv1_barycorr.py
def barycorr(eventfile,outfile,refframe,orbit_file,parfile,output_folder,custom_coords):
    """
    General function to perform the barycenter corrections for an event file

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    outfile - path to the output event file with barycenter corrections applied
    refframe - reference frame for barycenter corrections (usually ICRS)
    orbit_file - path to the orbit file of the observation
    parfile - name of the .par file
    output_folder - path to the folder where the outfile will be
    custom_coords - either an empty list/array or a list/array with two elements (RA/DEC)
    """
    if refframe != 'ICRS' and refframe != 'FK5':
        raise ValueError("refframe should either be ICRS or FK5! Otherwise, update Lv1_barycorr.py if there are options I was unaware of.")

    TIMEZERO = fits.open(eventfile)[1].header['TIMEZERO']
    logfile = output_folder + 'barycorr_notes.txt'

    if parfile[-4:] == '.par': #i.e., if we have a par file:
        posepoch,old_ra,old_dec,PMRA,PMDEC = read_par(parfile)
        #get epoch of position, old RA, old DEC, and PMRA/PMDEC proper motion corrections

        gti_card = fits.open(eventfile)[2]
        gtis = gti_card.data #aim is to get start/stop times of observation to get time of middle of observation
        MJDREFI = gti_card.header['MJDREFI']
        MJDREFF = gti_card.header['MJDREFF']
        centroid_time = (gtis[-1][1]-gtis[0][0])/2 #not going to take TIMEZERO into account because difference is small? Plus it's a -1 - (-1) thing

        centroid_MJD = (MJDREFI+MJDREF) + (TIMEZERO+centroid_time)/86400 #convert centroid MET to MJD
        time_elapsed = centroid_MJD-POSEPOCH #centroid_MJD is later than POSEPOCH!

        new_ra = old_ra + (PMRA/np.cos(old_dec*np.pi/180) * 1E-3/3600) * time_elapsed/365.2425 #365.2425 days in a year
        new_dec = old_dec + (PMDEC*1E-3/3600) * time_elapsed/365.2425

        with open(logfile,'w') as logtextfile:
            if len(custom_coords) == 2:
                new_ra = custom_coords[0]
                new_dec = custom_coords[1]
            output = subprocess.run(['barycorr',eventfile,'outfile='+outfile,'orbitfiles='+orbit_file,'ra='+str(new_ra),'dec='+str(new_dec),'refframe='+str(refframe),'clobber=YES'],capture_output=True,text=True)
            logtextfile.write(output.stdout)
            logtextfile.write('*------------------------------* \n')
            logtextfile.write(output.stderr)
            logtextfile.close()

    elif parfile == '':
        ra,dec = get_ra_dec(eventfile)
        if len(custom_coords) == 2:
            ra = custom_coords[0]
            dec = custom_coords[1]
        with open(logfile,'w') as logtextfile:
            output = subprocess.run(['barycorr',eventfile,'outfile='+outfile,'orbitfiles='+orbit_file,'ra='+str(ra),'dec='+str(dec),'refframe='+str(refframe),'clobber=YES'],capture_output=True,text=True)
            logtextfile.write(output.stdout)
            logtextfile.write('*------------------------------* \n')
            logtextfile.write(output.stderr)
            logtextfile.close()

    else:
        raise ValueError("parfile is neither an empty string nor a .par file. Is this right?")

    General function to perform the barycenter corrections for an event file

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    outfile - path to the output event file with barycenter corrections applied
    refframe - reference frame for barycenter corrections (usually ICRS)
    orbit_file - path to the orbit file of the observation
    parfile - name of the .par file
    output_folder - path to the folder where the outfile will be
    custom_coords - either an empty list/array or a list/array with two elements (RA/DEC)
    
General function to perform the barycenter corrections for an event file
eventfile - path to the event file. Will extract ObsID from this for the NICER files.
General function to perform the barycenter corrections for an event file eventfile - path to the event file . Will extract ObsID from this for the NICER files .
def barycorr(eventfile,outfile,refframe,orbit_file,parfile,output_folder,custom_coords):
    if refframe != 'ICRS' and refframe != 'FK5':
        raise ValueError("refframe should either be ICRS or FK5! Otherwise, update Lv1_barycorr.py if there are options I was unaware of.")
    TIMEZERO = fits.open(eventfile)[1].header['TIMEZERO']
    logfile = output_folder + 'barycorr_notes.txt'
    if parfile[-4:] == '.par': 
        posepoch,old_ra,old_dec,PMRA,PMDEC = read_par(parfile)
        gti_card = fits.open(eventfile)[2]
        gtis = gti_card.data 
        MJDREFI = gti_card.header['MJDREFI']
        MJDREFF = gti_card.header['MJDREFF']
        centroid_time = (gtis[-1][1]-gtis[0][0])/2 
        centroid_MJD = (MJDREFI+MJDREF) + (TIMEZERO+centroid_time)/86400 
        time_elapsed = centroid_MJD-POSEPOCH 
        new_ra = old_ra + (PMRA/np.cos(old_dec*np.pi/180) * 1E-3/3600) * time_elapsed/365.2425 
        new_dec = old_dec + (PMDEC*1E-3/3600) * time_elapsed/365.2425
        with open(logfile,'w') as logtextfile:
            if len(custom_coords) == 2:
                new_ra = custom_coords[0]
                new_dec = custom_coords[1]
            output = subprocess.run(['barycorr',eventfile,'outfile='+outfile,'orbitfiles='+orbit_file,'ra='+str(new_ra),'dec='+str(new_dec),'refframe='+str(refframe),'clobber=YES'],capture_output=True,text=True)
            logtextfile.write(output.stdout)
            logtextfile.write('*------------------------------* \n')
            logtextfile.write(output.stderr)
            logtextfile.close()
    elif parfile == '':
        ra,dec = get_ra_dec(eventfile)
        if len(custom_coords) == 2:
            ra = custom_coords[0]
            dec = custom_coords[1]
        with open(logfile,'w') as logtextfile:
            output = subprocess.run(['barycorr',eventfile,'outfile='+outfile,'orbitfiles='+orbit_file,'ra='+str(ra),'dec='+str(dec),'refframe='+str(refframe),'clobber=YES'],capture_output=True,text=True)
            logtextfile.write(output.stdout)
            logtextfile.write('*------------------------------* \n')
            logtextfile.write(output.stderr)
            logtextfile.close()
    else:
        raise ValueError("parfile is neither an empty string nor a .par file. Is this right?")
def barycorr ( eventfile , outfile , refframe , orbit_file , parfile , output_folder , custom_coords ) : if refframe != 'ICRS' and refframe != 'FK5' : raise ValueError ( "refframe should either be ICRS or FK5! Otherwise, update Lv1_barycorr.py if there are options I was unaware of." ) TIMEZERO = fits . open ( eventfile ) [ 1 ] . header [ 'TIMEZERO' ] logfile = output_folder + 'barycorr_notes.txt' if parfile [ - 4 : ] == '.par' : posepoch , old_ra , old_dec , PMRA , PMDEC = read_par ( parfile ) gti_card = fits . open ( eventfile ) [ 2 ] gtis = gti_card . data MJDREFI = gti_card . header [ 'MJDREFI' ] MJDREFF = gti_card . header [ 'MJDREFF' ] centroid_time = ( gtis [ - 1 ] [ 1 ] - gtis [ 0 ] [ 0 ] ) / 2 centroid_MJD = ( MJDREFI + MJDREF ) + ( TIMEZERO + centroid_time ) / 86400 time_elapsed = centroid_MJD - POSEPOCH new_ra = old_ra + ( PMRA / np . cos ( old_dec * np . pi / 180 ) * 1E-3 / 3600 ) * time_elapsed / 365.2425 new_dec = old_dec + ( PMDEC * 1E-3 / 3600 ) * time_elapsed / 365.2425 with open ( logfile , 'w' ) as logtextfile : if len ( custom_coords ) == 2 : new_ra = custom_coords [ 0 ] new_dec = custom_coords [ 1 ] output = subprocess . run ( [ 'barycorr' , eventfile , 'outfile=' + outfile , 'orbitfiles=' + orbit_file , 'ra=' + str ( new_ra ) , 'dec=' + str ( new_dec ) , 'refframe=' + str ( refframe ) , 'clobber=YES' ] , capture_output = True , text = True ) logtextfile . write ( output . stdout ) logtextfile . write ( '*------------------------------* \n' ) logtextfile . write ( output . stderr ) logtextfile . close ( ) elif parfile == '' : ra , dec = get_ra_dec ( eventfile ) if len ( custom_coords ) == 2 : ra = custom_coords [ 0 ] dec = custom_coords [ 1 ] with open ( logfile , 'w' ) as logtextfile : output = subprocess . run ( [ 'barycorr' , eventfile , 'outfile=' + outfile , 'orbitfiles=' + orbit_file , 'ra=' + str ( ra ) , 'dec=' + str ( dec ) , 'refframe=' + str ( refframe ) , 'clobber=YES' ] , capture_output = True , text = True ) logtextfile . write ( output . stdout ) logtextfile . write ( '*------------------------------* \n' ) logtextfile . write ( output . stderr ) logtextfile . close ( ) else : raise ValueError ( "parfile is neither an empty string nor a .par file. Is this right?" )
General function to perform the barycenter corrections for an event file
eventfile - path to the event file.
General function to perform the barycenter corrections for an event file eventfile - path to the event file .
"""
    General function to perform the barycenter corrections for an event file

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    outfile - path to the output event file with barycenter corrections applied
    refframe - reference frame for barycenter corrections (usually ICRS)
    orbit_file - path to the orbit file of the observation
    parfile - name of the .par file
    output_folder - path to the folder where the outfile will be
    custom_coords - either an empty list/array or a list/array with two elements (RA/DEC)
    """ #i.e., if we have a par file: #get epoch of position, old RA, old DEC, and PMRA/PMDEC proper motion corrections #aim is to get start/stop times of observation to get time of middle of observation #not going to take TIMEZERO into account because difference is small? Plus it's a -1 - (-1) thing #convert centroid MET to MJD #centroid_MJD is later than POSEPOCH! #365.2425 days in a year
eventfile
outfile
refframe
orbit_file
parfile
output_folder
custom_coords