Skip to content

Commit

Permalink
implement the object oriented approach to read the pulsar Ephemeris
Browse files Browse the repository at this point in the history
  • Loading branch information
tuoyl committed Jul 20, 2023
1 parent 6c106af commit d597a98
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions tatpulsar/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .readpar import readpar
69 changes: 69 additions & 0 deletions tatpulsar/utils/readpar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
Class to read the TEMPO2 parameter file (.par)
"""
import numpy as np

freq_pars = [f'F{int(i)}' for i in range(20)]
pars_we_care = ['PEPOCH', 'START', 'FINISH', 'PHI0'] +\
freq_pars

class readpar:
"""
Class to parse the TEMPO2 parameter file (.par)
The parameters in the '.par' file might be capitalized but those parameters
stored in this object are case INSENSITIVE (see examples below).
Example
-------
>>> eph = readpar('test.par')
>>> print("F0 = ", eph.F0.value, eph.f0.value)
>>> F0 = 29.636679699921209437
>>> print("F0 error = ", eph.F0.error)
>>> F0 error = 1.7247236495e-09
>>> print("PEPOCH = ", eph.PEPOCH.value)
>>> PEPOCH = 58066.18087539147382
>>> print("PEPOCH error = ", eph.PEPOCH.error)
>>> PEPOCH error = None
"""
def __init__(self, filepath):
with open(filepath, 'r') as file:
lines = file.readlines()

for line in lines:
parts = line.split()
if parts:
key = parts[0]
values = parts[1:]
setattr(self, key, values)
setattr(self, key.lower(), values)

for par in pars_we_care:
if hasattr(self, par):
value_list = getattr(self, par)

setattr(self, par, type("timingpar", (object,), {})())
setattr(self, par.lower(), type("timingpar", (object,), {})()) # Lower case

setattr(getattr(self, par), 'value', np.float128(value_list[0]))
setattr(getattr(self, par.lower()), 'value', np.float128(value_list[0])) # Lower case

if len(value_list) == 3:
setattr(getattr(self, par), 'error', np.float128(value_list[2]))
setattr(getattr(self, par.lower()), 'error', np.float128(value_list[2]))
else:
setattr(getattr(self, par), 'error', None)
setattr(getattr(self, par.lower()), 'error', None)

if __name__ == "__main__":
eph = readpar('../../tests/test.par')
print("F0 = ", eph.F0.value)
print("F0 error = ", eph.F0.error)
print("PEPOCH = ", eph.PEPOCH.value)
print("PEPOCH error = ", eph.PEPOCH.error)

print("F0 = ", eph.f0.value)
print("F0 error = ", eph.f0.error)
print("PEPOCH = ", eph.pepoch.value)
print("PEPOCH error = ", eph.pepoch.error)
34 changes: 34 additions & 0 deletions tests/test.par
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
PSRJ J0534+2200
RAJ 05:34:31.973 8.1267817736559476629e-4895
DECJ +22:00:52.06 3.1242079228685550361e-4884
F0 29.636679699921209437 1 0.00000000172472364950
F1 -3.6964161189285486447e-10 1 2.1200751036944221771e-14
F2 -4.4100506357621299713e-18 1 1.3732220303612194014e-19
F3 8.9920447272833230825e-24 1 4.3488380894326679931e-25
F4 -8.1618753666848709648e-30 1 5.5557310601785298942e-31
PEPOCH 58066.180875391473819
POSEPOCH 54760 4.2026289288901168828e-4933
DMEPOCH 54760
START 58066.177875391477251 1
FINISH 58116.307778270281737 1
TZRMJD 58088.399365291575503
TZRFRQ 1000
TZRSITE bat
TRES 39.799
EPHVER 5
CLK TT(TAI)
MODE 1
UNITS TCB
TIMEEPH IF99
DILATEFREQ Y
PLANET_SHAPIRO Y
T2CMETHOD IAU2000B
NE_SW 4.000
CORRECT_TROPOSPHERE N
EPHEM DE405
NITS 1
NTOA 249
CHI2R 1.5028 241
JUMP freq 999 1001 -0.0008124159772934 1
JUMP freq 1999 2001 -1.4639986440894e-05 1
JUMP NAME LE -0.00088 1 1

0 comments on commit d597a98

Please sign in to comment.