-
Notifications
You must be signed in to change notification settings - Fork 2
/
pan_mod.py
374 lines (309 loc) · 10.4 KB
/
pan_mod.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
"""
Panel definitions of the SO LAT.
Grace E. Chesmore
May 2021
"""
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from DEFAULTS import PARENT_PATH
n_per_m1 = [7, 9, 9, 9, 9, 9, 9, 9, 7] # Num panels per row
n_per_m2 = [5, 7, 9, 9, 9, 9, 9, 7, 5] # Num panels per row
# Initializes the pattern of panels in
# the primary and secondary mirrors.
def initialize_panel_model():
out_m1 = np.ones((10, sum(n_per_m1)))
out_m2 = np.ones((10, sum(n_per_m2)))
i_out = 0
i_row = 0
while i_row < 9:
i = 0
while i < n_per_m1[i_row]:
out_m1[0, i_out] = i_row + 1 # row
out_m1[1, i_out] = i + 1 # column
i_out += 1
i += 1
i_row += 1
i_out = 0
i_row = 0
while i_row < 9:
i = 0
while i < n_per_m2[i_row]:
out_m2[0, i_out] = i_row + 1 # row
out_m2[1, i_out] = i + 1 # column
i_out += 1
i += 1
i_row += 1
# The central row of adjusters in the adjuster positions .csv
# file are out of order (for both the primary and secondary),
# so here I re-order them to match the rest of the panels in the mirrors.
out_m1[1, 0:7] += 1
out_m1[1, 70:77] += 1
out_m2[1, 0:5] += 2
out_m2[1, 5:12] += 1
out_m2[1, 57:64] += 1
out_m2[1, 64:69] += 2
return out_m1, out_m2
def find_nearest(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return idx
# Given an x and y position, the function determines
# which panel that point sits on.
def identify_panel(x, y, x_adj, y_adj, col, row):
n_pts = len(x)
out = np.ones((2, n_pts, n_pts))
for ii in range(n_pts):
for jj in range(n_pts):
indx_x = find_nearest(x_adj, x[ii, jj])
indx_y = find_nearest(y_adj, y[ii, jj])
if x[ii, jj] < np.min(x_adj):
out[1, ii, jj] = 0
elif x[ii, jj] > np.max(x_adj):
out[1, ii, jj] = 10
elif y[ii, jj] < np.min(y_adj):
out[0, ii, jj] = 0
elif y[ii, jj] > np.max(y_adj):
out[0, ii, jj] = 10
else:
out[0, ii, jj] = row[indx_x]
out[1, ii, jj] = col[indx_y]
return out
# Defines the panel model given specified
# adjuster offsets.
def panel_model_from_adjuster_offsets(mirror, err, num, save):
if num == 0:
delta_z = np.random.randn(1092) * err / 1e3
else:
delta_z = err / 1e3
# If you want to save the specific adjuster offsets
# define the path of this saved file here:
if save != 0:
import os
path = PARENT_PATH + "/data/chesmore/sim_out/rx00600/"
if not os.path.exists(path):
os.makedirs(path)
np.savetxt(
path
+ "adj_offsets_m"
+ str(mirror)
+ "_"
+ str(num)
+ ".txt",
np.c_[delta_z],
)
pan_mod1, pan_mod2 = initialize_panel_model()
adj_pos_m1, adj_pos_m2 = get_single_vert_adj_positions()
if mirror == 1:
npan = sum(n_per_m1)
pan_mod = pan_mod1
col = adj_pos_m1[0]
row = adj_pos_m1[1]
x_adj = adj_pos_m1[2]
y_adj = adj_pos_m1[3]
z_adj = adj_pos_m1[4]
else:
npan = sum(n_per_m2)
pan_mod = pan_mod2
col = adj_pos_m2[0]
row = adj_pos_m2[1]
x_adj = adj_pos_m2[4]
y_adj = adj_pos_m2[3]
z_adj = adj_pos_m2[2]
i = 0
while i < npan:
i_row = pan_mod[0, i] # Row number
i_col = pan_mod[1, i] # Column number
# The calculation of the model parameters
cur_adj = np.where((row == i_row) & (col == i_col))
if len(cur_adj[0]) == 0:
i += 1
continue
else:
n_adj = len(cur_adj[0])
dz_cur = delta_z[cur_adj] # dz error
x_cur = x_adj[cur_adj]
y_cur = y_adj[cur_adj]
mean_x = np.mean(x_cur)
mean_y = np.mean(y_cur)
x_cur = x_cur - mean_x
y_cur = y_cur - mean_y
if i_col == 5:
x_cur = np.array([x_cur[0], x_cur[3], x_cur[1], x_cur[4], x_cur[2]])
y_cur = np.array([y_cur[0], y_cur[3], y_cur[1], y_cur[4], y_cur[2]])
M = np.zeros((5, 5))
M[:, 0] = 1.0
M[:, 1] = x_cur
M[:, 2] = y_cur
M[:, 3] = x_cur ** 2 + y_cur ** 2
M[:, 4] = y_cur * x_cur
coeffs = np.matmul(np.linalg.inv(M), dz_cur)
a = coeffs[0]
b = coeffs[1]
c = coeffs[2]
d = coeffs[3]
e = coeffs[4]
f = 0
# The output of the model
pan_mod[2, i] = a
pan_mod[3, i] = b
pan_mod[4, i] = c
pan_mod[5, i] = d
pan_mod[6, i] = e
pan_mod[7, i] = f
pan_mod[8, i] = mean_x
pan_mod[9, i] = mean_y
i += 1
return pan_mod
# Constructs the z offset given adjuster offsets.
def reconstruct_z_from_pan_model(x, y, x_adj, y_adj, col, row, panm, mirror):
de_z = np.zeros(np.shape(x))
print("Starting panel identification...")
pan_id = identify_panel(x, y, x_adj, y_adj, col, row) # returns row and column #'s
print("Panel identification finished.")
print("Starting panel reconstruction...")
i = 0
while i < len(x):
j = 0
while j < len(x):
cur_pan = np.where(
(panm[0, :] == pan_id[0, i, j]) & (panm[1, :] == pan_id[1, i, j])
)
if len(cur_pan[0]) == 0:
de_z[i, j] = np.nan
else:
if mirror == 2:
x_edge = x_edge_m2[int(cur_pan[0])]
y_edge = y_edge_m2[int(cur_pan[0])]
else:
x_edge = x_edge_m1[int(cur_pan[0])]
y_edge = y_edge_m1[int(cur_pan[0])]
if (
x[i, j] > (np.max(x_adj) + x_edge)
or x[i, j] < (np.min(x_adj) - x_edge)
or y[i, j] > (np.max(y_adj) + y_edge)
or y[i, j] < (np.min(y_adj) - y_edge)
):
de_z[i, j] = np.nan
else:
xc = panm[8, cur_pan]
yc = panm[9, cur_pan]
xi = x[i, j] - xc
yi = y[i, j] - yc
a = panm[2, cur_pan]
b = panm[3, cur_pan]
c = panm[4, cur_pan]
d = panm[5, cur_pan]
e = panm[6, cur_pan]
de_z[i, j] = (
a
+ (b * xi)
+ (c * yi)
+ d * (xi ** 2 + yi ** 2)
+ (e * xi * yi)
)
j += 1
i += 1
print("Finished panel reconstruction.")
return de_z
# Vertex adjuster positions are read in
# from .csv file.
def get_single_vert_adj_positions():
out_m1 = []
out_m2 = []
# Primary mirror adjuster positions
df_m1 = pd.read_csv(
PARENT_PATH + "/pans-adjs/Mirror-M1-vertical-adjuster-points_r1-1.csv",
skiprows=2,
na_values=["<-- ", "--> ", "<--", "-->"],
)
# Secondary mirror adjuster positions
df_m2 = pd.read_csv(
PARENT_PATH + "/pans-adjs/Mirror-M2-vertical-adjuster-points_r1-1.csv",
skiprows=2,
na_values=["<-- ", "--> ", "<--", "-->"],
)
# Read in adjuster positions from columns
x_adj_m1 = df_m1["X2"]
y_adj_m1 = df_m1["Y2"]
z_adj_m1 = df_m1["Z2"]
x_adj_m1_2 = df_m1["x "]
y_adj_m1_2 = df_m1["y "]
z_adj_m1_2 = df_m1["z "]
x_adj_m2 = df_m2["X2"]
y_adj_m2 = df_m2["Y2"]
z_adj_m2 = df_m2["Z2"]
x_adj_m2_2 = df_m2["x"]
y_adj_m2_2 = df_m2["y"]
z_adj_m2_2 = df_m2["z"]
xx = x_adj_m1[x_adj_m1.notnull()]
yy = y_adj_m1[x_adj_m1.notnull()]
zz = z_adj_m1[x_adj_m1.notnull()]
xx_2 = x_adj_m1_2[x_adj_m1_2.notnull()]
yy_2 = y_adj_m1_2[x_adj_m1_2.notnull()]
zz_2 = z_adj_m1_2[x_adj_m1_2.notnull()]
##################################
pan_id = df_m1["Panel2"]
pan_id_2 = df_m1["Panel "]
pp = pan_id[x_adj_m1.notnull()]
pp_2 = pan_id_2[x_adj_m1_2.notnull()]
pp = ["%.0f" % number for number in pp]
pan_id_m1 = np.concatenate((pp_2, pp))
##################################
x_adj_m1 = np.array(xx)
y_adj_m1 = np.array(yy)
z_adj_m1 = np.array(zz)
x_adj_m1_2 = np.array(xx_2)
y_adj_m1_2 = np.array(yy_2)
z_adj_m1_2 = np.array(zz_2)
x_adj_m1 = np.concatenate((x_adj_m1_2, x_adj_m1))
y_adj_m1 = np.concatenate((y_adj_m1_2, y_adj_m1))
z_adj_m1 = np.concatenate((z_adj_m1_2, z_adj_m1))
xx = x_adj_m2[x_adj_m2.notnull()]
yy = y_adj_m2[x_adj_m2.notnull()]
zz = z_adj_m2[x_adj_m2.notnull()]
xx_2 = x_adj_m2_2[x_adj_m2_2.notnull()]
yy_2 = y_adj_m2_2[x_adj_m2_2.notnull()]
zz_2 = z_adj_m2_2[x_adj_m2_2.notnull()]
##################################
pan_id = df_m2["Panel2"]
pan_id_2 = df_m2["Panel"]
pp = pan_id[x_adj_m2.notnull()]
pp_2 = pan_id_2[x_adj_m2_2.notnull()]
pan_id_m2 = np.concatenate((pp_2, pp))
pan_id_m2 = ["%.0f" % number for number in pan_id_m2]
##################################
x_adj_m2 = np.array(xx)
y_adj_m2 = np.array(yy)
z_adj_m2 = np.array(zz)
x_adj_m2_2 = np.array(xx_2)
y_adj_m2_2 = np.array(yy_2)
z_adj_m2_2 = np.array(zz_2)
x_adj_m2 = np.concatenate((x_adj_m2_2, x_adj_m2))
y_adj_m2 = np.concatenate((y_adj_m2_2, y_adj_m2))
z_adj_m2 = np.concatenate((z_adj_m2_2, z_adj_m2))
# Primary column and row numbers
col_m1 = np.zeros(len(pan_id_m1))
row_m1 = np.zeros(len(pan_id_m1))
for kk in range(len(pan_id_m1)):
pan = pan_id_m1[kk]
col_m1[kk] = pan[3]
row_m1[kk] = pan[2]
# Secondary column and row numbers
col_m2 = np.zeros(len(pan_id_m2))
row_m2 = np.zeros(len(pan_id_m2))
for kk in range(len(pan_id_m2)):
pan = pan_id_m2[kk]
col_m2[kk] = pan[3]
row_m2[kk] = pan[2]
out_m1.append(col_m1)
out_m1.append(row_m1)
out_m1.append(x_adj_m1)
out_m1.append(y_adj_m1)
out_m1.append(z_adj_m1)
out_m2.append(col_m2)
out_m2.append(row_m2)
out_m2.append(x_adj_m2)
out_m2.append(y_adj_m2)
out_m2.append(z_adj_m2)
return out_m1, out_m2