forked from vhaasteren/qtip
/
pint_temp.py
177 lines (139 loc) · 4.93 KB
/
pint_temp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vim: tabstop=4:softtabstop=4:shiftwidth=4:expandtab
"""
pint_temp: Temporary functions, useful for interacting with PINT
"""
import numpy
import subprocess
def general2(parfile, timfile, params):
"""
general2(parfile, timfile, params):
Calls tempo2 with the general2 plugin, and reads the output.
Inputs:
parfile = string, name of parfile
timfile = string, name of tim file
params = list of general2 values to return
Outputs:
dict of numpy arrays for each requested param.
Notes:
Assumes each parameter results in a single text column in the general2
output. A few params (for example 'posPulsar') output multiple columns,
using these will currently break things.
Also currently assumes all outputs can be interpreted as floating
point numbers.
"""
id_str = 'ABCD'
s_arg = id_str
for p in params:
s_arg += " {%s}" % p
s_arg += "\\n"
t2output = subprocess.check_output(["tempo2", "-output", "general2",
"-f", parfile, timfile, "-s", s_arg])
goodlines = [x for x in t2output.split('\n') if x.startswith(id_str)]
nline = len(goodlines)
result = {}
for p in params:
# Note, assumes single output column per requested param
# and that all values are numerical
result[p] = numpy.zeros(nline)
for i in range(nline):
vals = goodlines[i].split()
for ip in range(len(params)):
result[params[ip]][i] = vals[ip+1]
return result
def chi2(parfile,timfile):
"""
Run tempo2, get chi2 (as reported by 'general' plugin)
"""
id_str = 'ABCD'
t2output = subprocess.check_output(["tempo2", "-nobs", "20000", "-output", "general",
"-s", id_str+' ', "-f", parfile, timfile])
goodlines = [x for x in t2output.split('\n') if x.startswith(id_str)]
chi2 = 0.0
for l in goodlines:
vals = l.split()
if vals[1]=='chisq' and vals[2]=='=':
chi2 = float(vals[3])
return chi2
if __name__ == "__main__":
#! /usr/bin/env python
import time, sys, os
import pint.models as tm
from pint.phase import Phase
from pint import toa
import matplotlib.pyplot as plt
import numpy
import tempo2_utils
parfile = 'tests/J1744-1134.basic.par'
t1_parfile = 'tests/J1744-1134.t1.par'
timfile = 'tests/J1744-1134.Rcvr1_2.GASP.8y.x.tim'
m = tm.StandardTimingModel()
m.read_parfile(parfile)
print "model.as_parfile():"
print m.as_parfile()
try:
planet_ephems = m.PLANET_SHAPIRO.value
except AttributeError:
planet_ephems = False
t0 = time.time()
t = toa.get_TOAs(timfile)
time_toa = time.time() - t0
sys.stderr.write("Read/corrected TOAs in %.3f sec\n" % time_toa)
mjds = t.get_mjds()
d_tdbs = numpy.array([x.tdb.delta_tdb_tt for x in t.table['mjd']])
errs = t.get_errors()
resids = numpy.zeros_like(mjds)
ss_roemer = numpy.zeros_like(mjds)
ss_shapiro = numpy.zeros_like(mjds)
sys.stderr.write("Computing residuals...\n")
t0 = time.time()
for ii, tt in enumerate(t.table):
p = m.phase(tt)
resids[ii] = p.frac
ss_roemer[ii] = m.solar_system_geometric_delay(tt)
ss_shapiro[ii] = m.solar_system_shapiro_delay(tt)
time_phase = time.time() - t0
sys.stderr.write("Computed phases in %.3f sec\n" % time_phase)
# resids in (approximate) us:
resids_us = resids / float(m.F0.value) * 1e6
sys.stderr.write("RMS PINT residuals are %.3f us\n" % resids_us.std())
# Get some general2 stuff
tempo2_vals = tempo2_utils.general2(parfile, timfile,
['tt2tb', 'roemer', 'post_phase',
'shapiro', 'shapiroJ'])
t2_resids = tempo2_vals['post_phase'] / float(m.F0.value) * 1e6
diff_t2 = resids_us - t2_resids
diff_t2 -= diff_t2.mean()
# run tempo1 also, if the tempo_utils module is available
try:
import tempo_utils
t1_toas = tempo_utils.read_toa_file(timfile)
tempo_utils.run_tempo(t1_toas, t1_parfile)
t1_resids = t1_toas.get_resids(units='phase') / float(m.F0.value) * 1e6
diff_t1 = resids_us - t1_resids
diff_t1 -= diff_t1.mean()
diff_t2_t1 = t2_resids - t1_resids
diff_t2_t1 -= diff_t2_t1.mean()
except:
pass
def do_plot():
plt.clf()
plt.subplot(211)
plt.hold(False)
plt.errorbar(mjds,resids_us,errs,fmt=None,label='PINT')
plt.title("J1744-1134 GBT/GASP timing")
plt.xlabel('MJD')
plt.ylabel('Residual (us)')
plt.legend()
plt.grid()
plt.subplot(212)
plt.plot(mjds,diff_t2*1e3,label='PINT - T2')
plt.hold(True)
# plt.plot(mjds,diff_t1*1e3,label='PINT - T1')
plt.grid()
plt.xlabel('MJD')
plt.ylabel('Residual diff (ns)')
plt.legend()
do_plot()
plt.show()