forked from McMahonCosmologyGroup/holosim-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ap_fitting.py
410 lines (335 loc) · 13.5 KB
/
ap_fitting.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"""
Aperture field analysis.
Grace E. Chesmore
May 2021
"""
import ap_field as af
import far_field as ff
import matplotlib.pyplot as plt
import numpy as np
import optics_analyze as oa
import pan_fitting as pf
import pan_mod as pm
import scipy
from scipy.spatial import distance
imu = np.complex(0, 1)
# Optional function to remove gradient
# term from aperture fields. We don't use
# in this work, but provide as an optional
# function.
def remove_grad(x, y, phi, fftd):
x = x[np.where(np.isnan(phi) == False)]
y = y[np.where(np.isnan(phi) == False)]
fftd = fftd[np.where(np.isnan(phi) == False)]
phi = phi[np.where(np.isnan(phi) == False)]
def grad(x, y, a, b):
return a * x + b * y
def rms_grad(p, x, y):
a = p[0]
b = p[1]
phi_grad = grad(x, y, a, b)
return np.sum(np.sqrt((phi - phi_grad) ** 2))
p0 = [0.1, 0.1]
p = scipy.optimize.minimize(rms_grad, p0, args=(x, y))
phi_model = grad(x, y, p.x[0], p.x[1])
return x, y, phi, fftd
# Applying reference plane and receiver feed
# corrections.
def apply_ref_pl_and_rx_corrections(beam_in, az, el, xyz_tow, xyz_ref, xyz_ap, k):
pl_ap = np.zeros(len(az))
pl_rp = np.zeros(len(az))
i = int(0)
while i < len(az):
# Rotated location of aperture
loc_ap = oa.rotate_azel(xyz_ap, az[i], el[i])
pl_ap[i] = distance.euclidean(loc_ap, xyz_tow)
# Rotated location of reference receiver
loc_rp = oa.rotate_azel(xyz_ref, az[i], el[i])
pl_rp[i] = distance.euclidean(loc_rp, xyz_tow)
i += 1
pl_ap = np.reshape(pl_ap, (int(np.sqrt(len(az))), int(np.sqrt(len(az)))))
pl_rp = np.reshape(pl_rp, (int(np.sqrt(len(az))), int(np.sqrt(len(az)))))
# Convert the path length to a phase correction
ap_phase_cor = np.exp(-imu * pl_ap * k)
rp_phase_cor = np.exp(imu * pl_rp * k)
# Apply the correction
out = beam_in * ap_phase_cor * rp_phase_cor
return out
# Main function which takes in a far-field measurement
# and analyzes, turning measurement into aperture field.
def analyze_holography(
dat, geo_struct, plotting, reference_motion_correction, epsilon_terms, shift
):
pan_mod2 = pm.panel_model_from_adjuster_offsets(2, 0, 0, 0) # mm
pan_mod1 = pm.panel_model_from_adjuster_offsets(1, 0, 0, 0) # mm
# constants
lambda_ = geo_struct.lambda_
k = 2.0 * np.pi / lambda_
diam = 6 # diameter of mirrors
# Position of tower
rx = np.array([geo_struct.rx_x, geo_struct.rx_y, geo_struct.rx_z])
xyz_tow = np.array([geo_struct.x_tow, geo_struct.y_tow, geo_struct.z_tow])
# Position of reference receiver
xyz_ref = np.array([geo_struct.x_phref, geo_struct.y_phref, geo_struct.z_phref])
# Position of aperture
xyz_ap = np.array([geo_struct.x_ap, geo_struct.y_ap, geo_struct.z_ap])
# Scale correction due to parralax
plx_cor_x = 1.0 + (xyz_ap[2] / np.sqrt(np.sum(xyz_tow ** 2)))
plx_cor_y = 1.0 + (xyz_ap[2] / np.sqrt(np.sum(xyz_tow ** 2)))
## Break out the data (which is complex)
nx = len(dat[:, 0])
ny = len(dat[:, 1])
ndat = len(dat[0, :])
azi = dat[:, 0]
az = azi
eli = dat[:, 1]
el = eli
beam = dat[:, 2] + imu * dat[:, 3]
AZ = np.reshape(az, (int(np.sqrt(len(el))), int(np.sqrt(len(el)))))
EL = np.reshape(el, (int(np.sqrt(len(el))), int(np.sqrt(len(el)))))
beam = np.reshape(beam, (int(np.sqrt(len(el))), int(np.sqrt(len(el)))))
geo_struct.de_ang = abs(AZ[0, 0] - AZ[1, 0])
geo_struct.N_scan = int(len(AZ) / 2)
if plotting == 1:
plt.figure(figsize=(3, 3))
plt.title("Az vs. El")
plt.plot(az, el, ".")
plt.axis("equal")
plt.xlim(np.min(el), np.max(el))
plt.xlim(np.min(az), np.max(az))
plt.axis("equal")
plt.xlabel("Elevation [rad]")
plt.ylabel("Azimuthal [rad]")
plt.show()
plt.figure(figsize=(10, 3))
plt.subplot(1, 2, 1)
plt.title("Amplitude [dB]")
plt.pcolormesh(
AZ, EL, 20 * np.log10(abs(beam) / np.max(abs(beam))), shading="auto"
)
plt.colorbar()
plt.axis("equal")
plt.xlabel("Elevation [rad]")
plt.ylabel("Azimuthal [rad]")
plt.xlim(np.min(AZ), np.max(AZ))
plt.ylim(np.min(EL), np.max(EL))
plt.subplot(1, 2, 2)
plt.title("Phase [rad]")
Z = np.arctan2(np.imag(beam), np.real(beam))
plt.pcolormesh(AZ, EL, Z, shading="auto")
plt.colorbar()
plt.axis("equal")
plt.xlabel("Elevation [rad]")
plt.ylabel("Azimuthal [rad]")
plt.xlim(np.min(AZ), np.max(AZ))
plt.ylim(np.min(EL), np.max(EL))
plt.show()
# Geometric corrections for motion of telescope
# and reference receiver.
if reference_motion_correction != 0:
beam_ref_rx_cor = apply_ref_pl_and_rx_corrections(
beam, az, el, xyz_tow, xyz_ref, xyz_ap, k
)
if plotting == 1:
plt.figure(figsize=(3, 3))
plt.title("Phase after RX Correction")
Z = np.arctan2(np.imag(beam_ref_rx_cor), np.real(beam_ref_rx_cor))
plt.pcolormesh(AZ, EL, Z, shading="auto")
plt.xlabel("Elevation [rad]")
plt.ylabel("Azimuthal [rad]")
plt.colorbar()
plt.axis("equal")
plt.show()
else:
beam_ref_rx_cor = beam
# Get Aperture Fields: FFT beam to aperture fields
fftd = np.fft.fftshift(np.fft.ifft2(np.fft.fftshift(beam_ref_rx_cor)))
if epsilon_terms != 0:
u_fun = AZ - np.mean(AZ)
v_fun = EL - np.mean(EL)
## u_integral
temp = np.zeros(np.shape(AZ), dtype=complex)
temp = beam_ref_rx_cor * u_fun
fftd_temp = np.fft.fftshift(np.fft.ifft2(np.fft.fftshift(temp)))
fftd_temp = np.transpose(fftd_temp)
fftd_temp = np.flip(fftd_temp, axis=0)
u_integral = fftd_temp
## v_integral
temp = np.zeros(np.shape(AZ), dtype=complex)
temp = beam_ref_rx_cor * v_fun
fftd_temp = np.fft.fftshift(np.fft.ifft2(np.fft.fftshift(temp)))
fftd_temp = np.transpose(fftd_temp)
fftd_temp = np.flip(fftd_temp, axis=0)
v_integral = fftd_temp
## uu_integral
temp = np.zeros(np.shape(AZ), dtype=complex)
temp = beam_ref_rx_cor * u_fun * u_fun
fftd_temp = np.fft.fftshift(np.fft.ifft2(np.fft.fftshift(temp)))
fftd_temp = np.transpose(fftd_temp)
fftd_temp = np.flip(fftd_temp, axis=0)
uu_integral = fftd_temp
## vv_integral
temp = np.zeros(np.shape(AZ), dtype=complex)
temp = beam_ref_rx_cor * v_fun * v_fun
fftd_temp = np.fft.fftshift(np.fft.ifft2(np.fft.fftshift(temp)))
fftd_temp = np.transpose(fftd_temp)
fftd_temp = np.flip(fftd_temp, axis=0)
vv_integral = fftd_temp
## uv_integral
temp = np.zeros(np.shape(AZ), dtype=complex)
temp = beam_ref_rx_cor * u_fun * v_fun
fftd_temp = np.fft.fftshift(np.fft.ifft2(np.fft.fftshift(temp)))
fftd_temp = np.transpose(fftd_temp)
fftd_temp = np.flip(fftd_temp, axis=0)
uv_integral = fftd_temp
# Get spatial coordinates
delta_th = abs(np.max(az) - np.min(az)) / len(az) # increment in azimuthal angle
alpha = (lambda_ / diam) / delta_th # increment in x
delta_x = alpha * diam / nx # spatial coordinates conversion
delta_th = abs(np.max(el) - np.min(el)) / len(el) # increment in azimuthal angle
beta = (lambda_ / diam) / delta_th
delta_y = beta * diam / ny
x = np.linspace(0, int(np.sqrt(len(az))), int(np.sqrt(len(az)))) * delta_x
y = np.linspace(0, int(np.sqrt(len(az))), int(np.sqrt(len(az)))) * delta_y
y = y - np.mean(y)
x = x - np.mean(x)
x = x / plx_cor_x
y = y / plx_cor_y
x, y = np.meshgrid(x, y)
tow_di = np.sqrt((xyz_tow[0]) ** 2.0 + (xyz_tow[1]) ** 2.0 + (xyz_tow[2]) ** 2.0)
tow_th = np.arctan(xyz_tow[0] / xyz_tow[2])
phi = oa.do_unwrap(np.arctan2(np.imag(fftd), np.real(fftd)))
x_temp = x * np.cos(np.pi / 2) - y * np.sin(np.pi / 2)
y_temp = x * np.sin(np.pi / 2) + y * np.cos(np.pi / 2)
x = x_temp
y = y_temp
# Optional for including epsilon terms
if epsilon_terms != 0:
print("Using epsilon terms.")
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.pcolormesh(
x, y, 20 * np.log10(abs(fftd) / np.max(abs(fftd))), shading="auto"
)
plt.colorbar()
plt.axis("equal")
plt.subplot(1, 2, 2)
plt.pcolormesh(
x, y, oa.do_unwrap(np.arctan2(np.imag(fftd), np.real(fftd))), shading="auto"
)
plt.colorbar()
plt.axis("equal")
evaluated_epsilon_correcton = (
-imu
* k
* (
(u_integral / (2.0 * tow_di ** 2.0) * x * (x ** 2.0 + y ** 2.0))
+ (v_integral / (2.0 * tow_di ** 2.0) * y * (x ** 2.0 + y ** 2.0))
- (uu_integral / (2.0 * tow_di) * x ** 2.0)
- (vv_integral / (2.0 * tow_di) * y ** 2.0)
- (uv_integral / (2.0 * tow_di) * x * y)
)
)
plt.figure(figsize=(12, 7))
plt.subplot(2, 3, 1)
plt.title(r"$ -\kappa (u_{int}/ 2 d_{tow}^2) x(x^2 + y^2)$")
z = -imu * k * (u_integral / (2.0 * tow_di ** 2.0) * x * (x ** 2.0 + y ** 2.0))
plt.pcolormesh(x, y, np.arctan2(np.imag(z), np.real(z)), shading="auto")
plt.colorbar()
plt.axis("equal")
plt.subplot(2, 3, 2)
plt.title(r"$ -\kappa (v_{int}/ 2 d_{tow}^2) y(x^2 + y^2)$")
z = -imu * k * (v_integral / (2.0 * tow_di ** 2.0) * y * (x ** 2.0 + y ** 2.0))
plt.pcolormesh(x, y, np.arctan2(np.imag(z), np.real(z)), shading="auto")
plt.colorbar()
plt.axis("equal")
plt.subplot(2, 3, 3)
plt.title(r"$ -\kappa (uu_{int}/ 2 d_{tow}) x^2 $")
z = -imu * k * (uu_integral / (2.0 * tow_di) * x ** 2.0)
plt.pcolormesh(x, y, np.arctan2(np.imag(z), np.real(z)), shading="auto")
plt.colorbar()
plt.axis("equal")
plt.subplot(2, 3, 4)
plt.title(r"$ -\kappa (vv_{int}/ 2 d_{tow}) y^2$")
z = -imu * k * (vv_integral / (2.0 * tow_di) * y ** 2.0)
plt.pcolormesh(x, y, np.arctan2(np.imag(z), np.real(z)), shading="auto")
plt.colorbar()
plt.axis("equal")
plt.subplot(2, 3, 5)
plt.title(r"$ -\kappa (uv_{int}/ 2 d_{tow}) x y$")
z = -imu * k * (uv_integral / (2.0 * tow_di) * x * y)
plt.pcolormesh(x, y, np.arctan2(np.imag(z), np.real(z)), shading="auto")
plt.colorbar()
plt.axis("equal")
plt.subplot(2, 3, 6)
plt.title(r"Sum of all terms")
z = evaluated_epsilon_correcton
plt.pcolormesh(x, y, np.arctan2(np.imag(z), np.real(z)), shading="auto")
plt.colorbar()
plt.axis("equal")
plt.show()
fftd += evaluated_epsilon_correcton
# This class is an optional output
# for the user to return a series of
# specific parameters (if desired).
class guessed_geometery:
x0 = 0
y0 = 0
rx_x = 0
rx_y = 0
rx_z = 0
tower_dist = 1e6
k = 2.0 * np.pi / geo_struct.lambda_
th = np.linspace(-np.pi / 2 - 0.3, -np.pi / 2 + 0.3, 150)
ph = np.linspace(np.pi / 2 - 0.3, np.pi / 2 + 0.3, 150)
rxmirror = af.ray_mirror_pts(rx, geo_struct, th, ph)
out = af.aperature_fields_from_panel_model(
pan_mod1, pan_mod2, rx, geo_struct, th, ph, rxmirror
)
if shift[0] == "y":
x_temp, y_temp, phi = pf.pannel_mask(x, y + shift[1], phi, out, 0)
x_temp, y_temp, fftd = pf.pannel_mask(x, y + shift[1], fftd, out, 0)
elif shift[0] == "x":
x_temp, y_temp, phi = pf.pannel_mask(x + shift[1], y, phi, out, 0)
x_temp, y_temp, fftd = pf.pannel_mask(x + shift[1], y, fftd, out, 0)
else:
x_temp, y_temp, phi = pf.pannel_mask(x + shift[1], y + shift[2], phi, out, 0)
x_temp, y_temp, fftd = pf.pannel_mask(x + shift[1], y + shift[2], fftd, out, 0)
x, y, phi, fftd = remove_grad(x_temp, y_temp, phi, fftd)
x, y, phi, fftd = remove_grad(x, y, phi, fftd)
return x, y, phi / 2, fftd, guessed_geometery
# Function takes a measurement with specified
# adjuster offsets
def take_measurement(adj_off1, adj_off2, rep, tele_geo, rxmirror):
# Define telescope geometry and adjuster positions on each mirror:
rx = np.array([tele_geo.rx_x, tele_geo.rx_y, tele_geo.rx_z])
# Define panels on M1 and M2. Here you can define the
# magnitude of the adjuster offsets on each mirror:
pan_mod2 = pm.panel_model_from_adjuster_offsets(
2, adj_off2 * 1e3, 1, 0
) # Panel Model on M2
pan_mod1 = pm.panel_model_from_adjuster_offsets(
1, adj_off1 * 1e3, 1, 0
) # Panel Model on M1
# Set offsets of Receiver Feed (RX):
# Define FOV of RX. In other words, define directions of
# outgoing rays from the RX.
th = np.linspace(-np.pi / 2 - 0.28, -np.pi / 2 + 0.28, tele_geo.N_scan)
ph = np.linspace(np.pi / 2 - 0.28, np.pi / 2 + 0.28, tele_geo.N_scan)
# Define the path of the rays from the RX to the aperture plane
rxmirror = af.ray_mirror_pts(rx, tele_geo, th, ph)
out = af.aperature_fields_from_panel_model(
pan_mod1, pan_mod2, rx, tele_geo, th, ph, rxmirror
)
beam = ff.far_field_sim(out, tele_geo, rx)
meas_file = "/data/chesmore/sim_out/sim_err_rep" + str(rep) + "_.txt"
np.savetxt(
meas_file,
np.c_[
np.real(beam[0, :]),
np.real(beam[1, :]),
np.real(beam[2, :]),
np.imag(beam[2, :]),
],
)
return meas_file