-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps_utils_new.py
238 lines (184 loc) · 8.86 KB
/
ps_utils_new.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import numpy as np
from numpy import pi
import scipy.signal
from astropy.io import fits
#from scipy.optimize import curve_fit
import scipy.optimize
class IRImage:
def __init__(self,fits_path,poly_order=4,run_poly_fit=False,fov_deg=5.,dont_crop=False):
print('loading '+fits_path)
hdulist = fits.open(fits_path)
self.header = hdulist[0].header
self.full_ADU = hdulist[0].data
self.n_full = self.full_ADU.shape[0]
hdulist.close()
self.MAGZPT = self.header['MAGZPT']
self.exp_time_sec = self.header['EXPTIME']
#self.filter_name = self.header['FILTER']
self.dtheta_deg = float(np.abs(self.header['CD1_1']))
self.dtheta_amin = 60.*self.dtheta_deg
self.dtheta_rad = self.dtheta_deg*np.pi/180.
self.domega_sr = self.dtheta_rad**2
self.ADU_to_kjy_per_sr = 3.631*(10**(-self.MAGZPT/2.5))/self.exp_time_sec/self.domega_sr
self.full_kjy_per_sr = self.full_ADU * self.ADU_to_kjy_per_sr
if dont_crop:
self.frame_ADU = self.full_ADU
self.n = self.n_full
else:
self.fov_deg = fov_deg
self.n = int(np.round(self.fov_deg/self.dtheta_deg))
self.frame_ADU = self.crop_around_frame(self.full_ADU)
self.ADU_to_rawADU = (1.86/60**2*np.pi/180.)**2/(self.domega_sr)
self.frame_rawADU = self.frame_ADU*self.ADU_to_rawADU
self.frame_kjy_per_sr = self.frame_ADU * self.ADU_to_kjy_per_sr
if run_poly_fit: self.fit_2D_poly(poly_order)
def crop_around_frame(self,full_img):
x,y = np.where(full_img != 0)
self.x_med, self.y_med = np.median(x),np.median(y)
x1 = int(self.x_med-self.n/2+1)
x2 = x1+self.n
y1 = int(self.y_med-self.n/2+1)
y2 = y1+self.n
return full_img[x1:x2,y1:y2]
def fit_2D_poly(self,order):
def func(ind, *c):
xi = 1.*(ind % self.n)/self.n-.5
yi = 1.*(np.int32(ind/self.n))/self.n-.5
xcoefs = c[1:1+order]
ycoefs = c[1+order:]
xpoly = 1 + np.sum([xcoefs[i]*(xi**(i+1)) for i in range(order)],axis=0)
ypoly = 1 + np.sum([ycoefs[i]*(yi**(i+1)) for i in range(order)],axis=0)
return xpoly*ypoly * c[0]
inds = np.arange(self.n**2)
#popt, pcov = curve_fit(func, inds, self.frame_kjy_per_sr.flatten(), p0 = np.append(self.frame_kjy_per_sr.mean(),np.zeros(2*order)),method='trf')
#np.sqrt(np.diag(pcov))
chisq = lambda p0: np.sum((func(inds,*p0)-self.frame_rawADU.flatten())**2)
bg = self.frame_rawADU.mean()
print('bg = '+str(bg))
poly_term_param_bounds = [(-.1,.1),(-.1,.1),(-.3,.3),(-.5,.5),(-.6,.6),(-1,1),(-2,2),(-2,2)]
param_bounds = [(bg-10,bg+10)]+poly_term_param_bounds[:order] + poly_term_param_bounds[:order]
print(param_bounds)
result = scipy.optimize.differential_evolution(chisq, param_bounds, polish=True)
popt = result['x']
print(popt)
self.model_frame_rawADU = np.reshape(func(inds,*popt),(self.n,self.n))
self.model_frame_kjy_per_sr = self.model_frame_rawADU / self.ADU_to_rawADU * self.ADU_to_kjy_per_sr
self.res_frame_kjy_per_sr = self.frame_kjy_per_sr - self.model_frame_kjy_per_sr
def image2PS(image,nbins,lmax,backsub=False,hann=False,exclude_lx_or_ly_zero=False,use_res_image=False,use_model_image=False):
n = image.n
dang = image.dtheta_rad
if use_res_image:
img = image.res_frame_kjy_per_sr
elif use_model_image:
img = image.model_frame_kjy_per_sr
else:
img = image.frame_kjy_per_sr
lvals = np.fft.fftfreq(n)*2*pi/dang
dl = np.abs(lvals[1]-lvals[0])
lx,ly = np.meshgrid(lvals,lvals)
lmag = np.sqrt(lx**2+ly**2)
w2 = np.ones((n,n))
norm = 1
if hann:
w = scipy.signal.hann(n)
wx,wy = np.meshgrid(w,w)
w2 = wx*wy
norm = np.sqrt(np.mean(w2**2))
sub = 0
if backsub: sub = img.mean()
print 'img.mean() = '+str(img.mean())
img_ft = np.fft.fft2((img-sub)*w2)/norm
lbinedges = np.linspace(0,lmax,nbins+1)
lbincenters = .5*(lbinedges[0:nbins]+lbinedges[1:nbins+1])
img_ft_binned = np.zeros(nbins)
counts = np.zeros(nbins)
for bini in range(nbins):
inbin = (lmag>lbinedges[bini])&(lmag<lbinedges[bini+1])
if exclude_lx_or_ly_zero: inbin &= (np.abs(lx)>3*dl) & (np.abs(ly)>3*dl)
img_ft_binned[bini] = np.sum(np.abs(img_ft[inbin])**2)/np.sum(inbin)
counts[bini] = np.sum(inbin)
return lbincenters,img_ft_binned*(dang**2)/(n**2),counts
def make_hann(n):
w = scipy.signal.hann(n)
wx,wy = np.meshgrid(w,w)
w2 = wx*wy
return w2, np.sqrt(np.mean(w2**2))
def ir_and_radio_xspec(ir_image,ir_label,mwa_image,mwa_label,nbins,lmax,useirhann=True,usemwahann=False):
assert ir_image.dtheta_rad == mwa_image.dtheta_rad
assert ir_image.n == mwa_image.n
if ir_label == 'res':
ir_img = ir_image.res_frame_kjy_per_sr
else:
ir_img = ir_image.frame_kjy_per_sr
mwa_weights_img = mwa_image.weights_xx0
if mwa_label == 'res':
mwa_dirty_img = mwa_image.dirty_xx_u0-mwa_image.model_xx_u0
else:
mwa_dirty_img = mwa_image.dirty_xx_u0
n,dang = ir_image.n, ir_image.dtheta_rad
hann2D,hann2Drms = make_hann(n)
lvals = np.fft.fftfreq(n)*2*pi/dang
lx,ly = np.meshgrid(lvals,lvals)
lmag = np.sqrt(lx**2+ly**2)
print(np.max(lmag))
irwind,mwawind = np.ones(ir_img.shape),np.ones(ir_img.shape)
irnorm,mwanorm = 1.,1.
if useirhann:
irwind,irnorm = hann2D,hann2Drms
if usemwahann:
mwawind,mwanorm = hann2D,hann2Drms
# FFT the (MWA dirty image) and (IR image)
ir_ft = np.fft.fft2(irwind*(ir_img-ir_img.mean()))/irnorm
mwa_dirty_ft = np.fft.fft2(mwawind*(mwa_dirty_img-mwa_dirty_img.mean()))/mwanorm
mwa_weights_ft = np.abs(np.fft.fft2((mwa_weights_img-mwa_weights_img.mean())))/mwanorm
lbinedges = np.linspace(0,lmax,nbins+1)
lbincenters = .5*(lbinedges[0:nbins]+lbinedges[1:nbins+1])
xspec_binned = np.zeros(nbins)
mwaspec_binned = np.zeros(nbins)
irspec_binned = np.zeros(nbins)
bin_counts = np.zeros(nbins)
bin_sum_weights = np.zeros(nbins)
bin_sum_squared_weights = np.zeros(nbins)
for bini in range(nbins):
inbin = (lmag>lbinedges[bini])&(lmag<lbinedges[bini+1])
bin_counts[bini] = np.sum(inbin)
xspec_binned[bini] = np.sum(ir_ft[inbin]*np.conj(mwa_dirty_ft[inbin])*mwa_weights_ft[inbin])/np.sum(mwa_weights_ft[inbin])
mwaspec_binned[bini] = np.sum(np.abs(mwa_dirty_ft[inbin])**2*mwa_weights_ft[inbin]**2)/np.sum(mwa_weights_ft[inbin]**2)
irspec_binned[bini] = np.mean(np.abs(ir_ft[inbin])**2)
bin_sum_weights[bini] = np.sum(mwa_weights_ft[inbin])
bin_sum_squared_weights[bini] = np.sum(mwa_weights_ft[inbin]**2)
pspec_norm = (dang**2)/(n**2)
return lbincenters,irspec_binned*pspec_norm,mwaspec_binned*pspec_norm,xspec_binned*pspec_norm,bin_counts,bin_sum_weights,bin_sum_squared_weights
def ir_and_ir_full_xspec(ir_image1,ir_image2,nbins,lmin,lmax,flip=False,uselogbins=False):
ir_img1 = ir_image1.frame_rawADU-ir_image1ir_and_radio_xspec.model_frame_rawADU
ir_img2 = ir_image2.frame_rawADU-ir_image2.model_frame_rawADU
if flip: ir_img2 = np.fliplr(np.flipud(ir_img2))
n,dang = ir_img1.shape[0], ir_image1.dtheta_rad
hann2D,hann2Drms = make_hann(n)
lvals = np.fft.fftfreq(n)*2*pi/dang
lx,ly = np.meshgrid(lvals,lvals)
lmag = np.sqrt(lx**2+ly**2)
print(np.max(lmag))
# FFT the (MWA dirty image) and (IR image)
ir_ft1 = np.fft.fft2((ir_img1-ir_img1.mean())*hann2D)/hann2Drms
ir_ft2 = np.fft.fft2((ir_img2-ir_img2.mean())*hann2D)/hann2Drms
if uselogbins:
lbinedges = 10.**np.linspace(np.log10(lmin),np.log10(lmax),nbins+1)
else:
lbinedges = np.linspace(lmin,lmax,nbins+1)
lbincenters = .5*(lbinedges[0:nbins]+lbinedges[1:nbins+1])
xspec_binned = np.zeros(nbins)
irspec1_binned = np.zeros(nbins)
irspec2_binned = np.zeros(nbins)
bin_counts = np.zeros(nbins)
bin_sum_weights = np.zeros(nbins)
bin_sum_squared_weights = np.zeros(nbins)
for bini in range(nbins):
inbin = (lmag>lbinedges[bini])&(lmag<lbinedges[bini+1])
bin_counts[bini] = np.sum(inbin)
xspec_binned[bini] = np.sum(ir_ft1[inbin]*np.conj(ir_ft2[inbin]))/np.sum(inbin)
irspec1_binned[bini] = np.mean(np.abs(ir_ft1[inbin])**2)
irspec2_binned[bini] = np.mean(np.abs(ir_ft2[inbin])**2)
bin_counts[bini] = np.sum(inbin)
pspec_norm = (dang**2)/(n**2)
return lbincenters,xspec_binned*pspec_norm,irspec1_binned*pspec_norm,irspec2_binned*pspec_norm,bin_counts