forked from AaronParsons/fringerate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_sim_beam.py
More file actions
executable file
·170 lines (151 loc) · 5.24 KB
/
plot_sim_beam.py
File metadata and controls
executable file
·170 lines (151 loc) · 5.24 KB
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
#! /usr/bin/env python
import aipy as a, numpy as n, pylab as p
import capo as C, scipy
from mpl_toolkits.basemap import Basemap
import frf_conv
FQ = .151
DIM = 400
RES = .5
SIZE = DIM / RES
aa = a.cal.get_aa('psa898_v003', n.array([FQ]))
h = a.healpix.HealpixMap(nside=64)
im = a.img.Img(DIM, res=RES)
def proj_hmap(dat, mode='e'):
h.map = dat
if mode == 'e': x,y,z = im.get_eq(dec=aa.lat, center=(SIZE/2,SIZE/2))
elif mode == 't': x,y,z = im.get_top(center=(SIZE/2,SIZE/2))
else: raise ValueError(mode)
v = n.logical_not(x.mask)
x,y,z = x.flatten(), y.flatten(), z.flatten()
d = h[x,y,z]
d.shape = (SIZE,SIZE)
d = n.where(v, d, 0)
return d
#m = Basemap(projection='ortho', lat_0=aa.lat, lon_0=180, rsphere=1.)
m = Basemap(projection='ortho', lat_0=90, lon_0=180, rsphere=1.)
def plot_hmap(dat, mode='log', mx=0, drng=3):
d = proj_hmap(dat,'e')
d /= d.max()
if mode == 'log': d = n.log10(n.abs(d).clip(10**(mx-drng),n.Inf))
elif mode == 'real': d = d.real
m.imshow(10*d, vmax=10*mx, vmin=10*(mx-drng), origin='lower', interpolation='bicubic', cmap='jet')
#m.imshow(d, vmax=mx, vmin=(mx-drng), origin='lower', interpolation='bicubic', cmap='jet')
m.drawmapboundary()
m.drawmeridians(n.arange(0,360,30))
m.drawparallels(n.arange(0,90,10))
def beam_area(dat):
return n.sum(dat) * 4 * n.pi / h.npix()
xyz = h.px2crd(n.arange(h.map.size), ncrd=3)
tx,ty,tz = n.dot(aa._eq2zen, xyz)
_bmx = aa[0].bm_response((tx,ty,tz),pol='x')[0]
_bmy = aa[0].bm_response((tx,ty,tz),pol='y')[0]
bmI = 0.5 * (_bmx**2 + _bmy**2)
bmI = n.where(tz > 0, bmI, 0)
#def nos(dat, t):
# pwr_bm = beam_area(bm)
# pwr2_bm = beam_area(dat**2)
# return pwr_bm**2 / pwr2_bm / t
#print beam_area(bm), beam_area(bm**2), nos(bm, 1.), nos(bm,1.) / n.sqrt(NBINS)
#xyz = (xyz[1],xyz[0],xyz[2])
#bl = n.array([100, 0, 0])
bl = aa.get_baseline(0,16,'r') * FQ
#bl = aa.get_baseline(0,28,'r') * FQ
#bl = aa.get_baseline(0,3,'r') * 10 * FQ
print 'Baseline:', bl
fng = frf_conv.mk_fng(bl, *xyz)
#fng = mk_fng(bl, *xyz)
print fng.max()
#plot_hmap(fng, mode='real', mx=.001, drng=.001); p.colorbar(); p.show()
frf,bins,wgt,(cen,wid) = frf_conv.get_optimal_kernel_at_ref(aa, 0, (0,16))
bwfrs = frf_conv.get_beam_w_fr(aa, (0,16), ref_chan=0) # XXX check our ref_chan
tbins,firs,frbins,frfs = frf_conv.get_fringe_rate_kernels(bwfrs,42.9,403)
skypass = n.where(n.abs(bins)<0.5/42.9, 1, 0)
crosstalk = n.ones_like(frbins)
#crosstalk[14:] = 0 # 600s / 42.9s = 14
#crosstalk[3*14:] = 0 # 600s / 42.9s = 14
#crosstalk[:3*14] *= a.dsp.gen_window(3*14, 'blackman-harris')
#crosstalk[3*84:] = 0 # 3600s / 42.9s = 84
#crosstalk[:3*84] *= a.dsp.gen_window(3*84, 'blackman-harris')
crosstalk = n.fft.fftshift(n.abs(n.fft.ifft(crosstalk)))
crosstalk /= crosstalk.max()
print frfs[0].max()
frfs[0] /= frfs[0].max() # XXX shouldn't have to do this
#p.plot(frbins,1-crosstalk)
#p.plot(bins,frf)
#p.plot(frbins,frfs[0])
#frfs[0] *= (1-crosstalk)
#p.show()
wgt = scipy.interpolate.interp1d(frbins, frfs[0], kind='linear')
fng_wgt = wgt(fng)
fng_bm = fng_wgt * bmI
#print fng.max(), fng.min()
noise_ratio = n.sum(n.abs(frfs[0])**2)/n.sum(n.abs(skypass)**2)
print noise_ratio, 42.9 / noise_ratio, n.sqrt(noise_ratio)
#hist, bin_edges = n.histogram(fng, bins=bin_edges, weights=bmI**2)
#hist, bin_edges = n.histogram(fng, range=(-.01,.01), bins=NBINS, weights=bm**2) # or bm**2?
#f = 0
#for cnt,b1 in enumerate(bin_edges[:-1]):
# b2 = bin_edges[cnt+1]
# if cnt % 2 == 0: f = n.where(n.logical_and(fng >= b1, fng < b2), 1., f)
p.figure(figsize=(10,3))
p.subplot(131)
plot_hmap(bmI, mx=0, drng=2)
p.subplot(132)
#p.imshow(proj_hmap(fng,'e'), vmax=0, vmin=-.007, origin='lower')
plot_hmap(fng_wgt, mx=0, drng=2)
#
p.subplot(133)
plot_hmap(fng_bm, mx=0, drng=2)
p.subplots_adjust(left=.05,right=.87,top=.9,bottom=.1, wspace=.05)
fig = p.gcf()
cbar_ax1 = fig.add_axes([.9,.15,.02,.7])
p.colorbar(ticks=[0,-3,-6,-10,-13,-16,-20], cax=cbar_ax1)#, shrink=.5)
p.xlabel('dB', fontsize=14)
p.show()
#
#p.subplot(234)
##hist, bins = n.histogram(fng, range=(-.01,.01), bins=NBINS, weights=bm) # or bm**2?
#bins = 0.5 * (bin_edges[:-1] + bin_edges[1:])
#hist1 = hist/n.sum(hist)
#print 'Hist:', hist1.max()
##p.plot(bins, hist1/hist1.max())
#p.plot(bins, hist1/hist1.max())
#p.plot(bins, n.sqrt(hist1/hist1.max()))
#
#noise_sum, noise_wgt = 0, 0
#wgts = []
#f = 0
#for cnt,b1 in enumerate(bin_edges[:-1]):
# b2 = bin_edges[cnt+1]
# fng_wgt = n.where(n.logical_and(fng >= b1, fng < b2), 1., 0)
# fng_amp = bm * fng_wgt
# _n = nos(fng_amp,NBINS)
# w = 1. / _n**2
# wgts.append(w)
# if w > 0:
# #print cnt, b1, _n, w
# noise_sum += (_n*w)**2
# noise_wgt += w
# f = n.where(n.logical_and(fng >= b1, fng < b2), w, f)
#wgts = n.array(wgts)
#
#p.plot(bins, wgts/wgts.max())
#
#fng_amp = bm * f
#print fng_amp.max()
#fng_amp /= fng_amp.max()
bmI_area = beam_area(bmI**2)
fng_bm_area = beam_area(fng_bm**2)
print 'Whole PP beam area:', bmI_area
print 'Filtered PP beam area:', fng_bm_area
print 'Correction factor:', bmI_area / fng_bm_area
#print n.sqrt(noise_sum) / noise_wgt
#
#print 'Improvement:', (n.sqrt(noise_sum)/noise_wgt) / (nos(bm,1.) / n.sqrt(NBINS))
#
#p.subplot(235)
#p.imshow(proj_hmap(f,'e'), origin='lower')
#
#p.subplot(236)
#p.imshow(proj_hmap(fng_amp,'e'), origin='lower')
p.show()