forked from telegraphic/leda_analysis_2016
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path14_output_to_hibayes.py
executable file
·137 lines (105 loc) · 3.18 KB
/
14_output_to_hibayes.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
#!/usr/bin/env python
"""
# 14_output_to_hibayes.py
Outputs data to freqs.txt, spectrum.txt and spec_errs.txt for input into hibayes package
"""
import matplotlib as mpl
import seaborn as sns
import tables as tb
from leda_cal.leda_cal import *
from leda_cal.skymodel import *
from leda_cal.dpflgr import *
from leda_cal import useful
from leda_cal.git import get_repo_fingerprint
sns.set_style('ticks')
sns.set_context("paper",font_scale=1.5)
def fit_poly(x, y, n=5, log=True):
""" Fit a polynomial to x, y data
x (np.array): x-axis of data (e.g. frequency)
y (np.array): y-axis of data (e.g temperature)
n (int): number of terms in polynomial (defaults to 5)
"""
x_g = x
x = np.ma.array(x, mask=y.mask).compressed()
y = y.compressed()
if log:
print "HERE"
yl = np.log10(y)
else:
yl = y
fit = np.polyfit(x, yl, n)
print fit
p = np.poly1d(fit)
if log:
return 10**(p(x_g))
else:
return p(x_g)
def fit_poly_log(x, y, n=5):
""" Fit a polynomial to x, y data
x (np.array): x-axis of data (e.g. frequency)
y (np.array): y-axis of data (e.g temperature)
n (int): number of terms in polynomial (defaults to 5)
"""
x_g = x
x = np.ma.array(x, mask=y.mask).compressed()
y = y.compressed()
yl = np.log10(y)
fit = np.polyfit(x, yl, n)
print fit
p = np.poly1d(fit)
return 10**(p(x_g))
def quicklook(filename):
h5 = tb.open_file(filename)
T_ant = apply_calibration(h5)
f_leda = T_ant['f']
ant_ids = ['252A', '254A', '255A']
pol_id = 'y'
print("Plotting...")
fig, ax = plt.subplots(figsize=(8, 6))
mid = T_ant["252A"].shape[0]/2
sl = 250
d0 = T_ant[ant_ids[0]][mid-sl:mid+sl]
d1 = T_ant[ant_ids[1]][mid-sl:mid+sl]
d2 = T_ant[ant_ids[2]][mid-sl:mid+sl]
n_chans = 42
d0 = rfi_flag(d0, freqs=f_leda)
d0 = useful.rebin(d0, 1, n_chans)
f_leda = useful.rebin(f_leda, n_chans)
d0_s = np.std(d0, axis=0) / np.sqrt(n_chans)**2
d0 = np.median(d0, axis=0)
#d1 = rfi_flag(d1, freqs=f_leda)
#d1 = np.median(d1, axis=0)
#
#d2 = rfi_flag(d2, freqs=f_leda)
#d2 = np.median(d2, axis=0)
#plt.imshow(d0, cmap='viridis', aspect='auto')
#plt.colorbar()
#plt.show()
#exit()
return f_leda, d0, d0_s
if __name__ == "__main__":
import sys
try:
filename = sys.argv[1]
except:
print "USAGE: ./quicklook.py filename_of_hdf5_observation"
exit()
f, d, d_s = quicklook(filename)
np.savetxt("freqs.txt", f)
fh = open("freqs.txt", "a")
fh.write("# %s\n" % get_repo_fingerprint())
fh.close()
np.savetxt("spectrum.txt", d)
fh = open("spectrum.txt", "a")
fh.write("# %s\n" % get_repo_fingerprint())
fh.close()
np.savetxt("spectrum_errs.txt", d_s)
fh = open("spectrum_errs.txt", "a")
fh.write("# %s\n" % get_repo_fingerprint())
fh.close()
plt.subplot(2,1,1)
plt.plot(f, d)
plt.subplot(2,1,2)
plt.plot(f, d_s)
plt.text(0.005, 0.005, get_repo_fingerprint(), transform=plt.gcf().transFigure, size=8)
plt.show()