-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_example.py
More file actions
502 lines (470 loc) · 11.9 KB
/
main_example.py
File metadata and controls
502 lines (470 loc) · 11.9 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import numpy as np
try:
from astropy import units, \
constants
from astropy.io import fits
astropy_is_imported = True
except:
astropy_is_imported = False
def getcol_wrapper(ms, table, colname):
if os.path.isdir(ms):
tb.open(
"{}/{}".format(ms, table)
)
col = np.squeeze(
tb.getcol(colname)
)
tb.close()
else:
raise IOError(
"{} does not exist".format(ms)
)
return col
def get_num_chan(ms):
return getcol_wrapper(
ms=ms,
table="SPECTRAL_WINDOW",
colname="NUM_CHAN"
)
def get_spw_ids(ms):
return getcol_wrapper(
ms=ms,
table="DATA_DESCRIPTION",
colname="SPECTRAL_WINDOW_ID"
)
def get_visibilities(ms):
if os.path.isdir(ms):
data = getcol_wrapper(
ms=ms,
table="",
colname="DATA"
)
else:
raise IOError(
"{} does not exisxt".format(ms)
)
visibilities = np.stack(
arrays=(data.real, data.imag),
axis=-1
)
return visibilities
def export_visibilities(ms, filename):
if os.path.isfile(filename):
print(
"{} already exists".format(filename)
)
else:
visibilities = get_visibilities(ms=ms)
print(
"shape (visibilities):", visibilities.shape
)
if astropy_is_imported:
fits.writeto(
filename=filename + ".fits",
data=visibilities,
overwrite=True
)
else:
with open(filename + ".numpy", 'wb') as file:
np.save(file, visibilities)
def convert_array_to_wavelengths(array, frequency):
if astropy_is_imported:
array_converted = (
(array * units.m) * (frequency * units.Hz) / constants.c
).decompose().value
else:
array_converted = array * frequency / 299792458.0
return array_converted
def get_uv_wavelengths(ms):
if os.path.isdir(ms):
uvw = getcol_wrapper(
ms=ms,
table="",
colname="UVW"
)
else:
raise IOError(
"{} does not exisxt".format(ms)
)
chan_freq = getcol_wrapper(
ms=ms,
table="SPECTRAL_WINDOW",
colname="CHAN_FREQ"
)
chan_freq_shape = np.shape(chan_freq)
if np.shape(chan_freq):
u_wavelengths, v_wavelengths = np.zeros(
shape=(
2,
chan_freq_shape[0],
uvw.shape[1]
)
)
for i in range(chan_freq_shape[0]):
u_wavelengths[i, :] = convert_array_to_wavelengths(array=uvw[0, :], frequency=chan_freq[i])
v_wavelengths[i, :] = convert_array_to_wavelengths(array=uvw[1, :], frequency=chan_freq[i])
else:
u_wavelengths = convert_array_to_wavelengths(array=uvw[0, :], frequency=chan_freq)
v_wavelengths = convert_array_to_wavelengths(array=uvw[1, :], frequency=chan_freq)
uv_wavelengths = np.stack(
arrays=(u_wavelengths, v_wavelengths),
axis=-1
)
return uv_wavelengths
def export_uv_wavelengths(ms, filename):
if os.path.isfile(filename):
print(
"{} already exists".format(filename)
)
else:
uv_wavelengths = get_uv_wavelengths(ms=ms)
print(
"shape (uv_wavelengths):", uv_wavelengths.shape
)
if astropy_is_imported:
fits.writeto(
filename=filename + ".fits",
data=uv_wavelengths,
overwrite=True
)
else:
with open(filename + ".numpy", 'wb') as file:
np.save(file, uv_wavelengths)
# def get_sigma(ms):
#
# if os.path.isdir(ms):
# sigma = getcol_wrapper(
# ms=ms,
# table="",
# colname="SIGMA"
# )
# else:
# raise IOError(
# "{} does not exisxt".format(ms)
# )
#
# return sigma
#
#
# def export_sigma(ms, width, spw, contsub):
#
# filename = "width_{}/sigma_spw_{}{}.fits".format(
# width,
# spw,
# ".contsub" if contsub else ''
# )
# if os.path.isfile(filename):
# print(
# "{} already exists".format(filename)
# )
# else:
# sigma = get_sigma(
# ms="width_{}/{}".format(
# width,
# ms if ms.endswith(".statwt") else "{}.statwt".format(ms)
# )
# )
#
# # sigma = np.average(
# # a=sigma,
# # axis=0
# # )
#
# num_chan = getcol_wrapper(
# ms=ms,
# table="SPECTRAL_WINDOW",
# colname="NUM_CHAN"
# )
#
# if num_chan == width:
# pass
# else:
# sigma = np.tile(
# A=sigma,
# reps=(
# int(num_chan / width), 1
# )
# )
#
# sigma = np.stack(
# arrays=(sigma, sigma),
# axis=-1
# )
#
# print(
# "shape (sigma):", sigma.shape
# )
# fits.writeto(
# filename=filename,
# data=sigma,
# overwrite=True
# )
def get_frequencies(uid, field, spw):
ms = "{}_field_{}_spw_{}.ms.split.cal".format(
uid,
field,
spw
)
if os.path.isdir(ms):
chan_freq = getcol_wrapper(
ms=ms,
table="SPECTRAL_WINDOW",
colname="CHAN_FREQ"
)
else:
raise IOError(
"The directory {} does not exist".format(ms)
)
return chan_freq
def export_frequencies(uid, field, spw):
chan_freq = get_frequencies(
uid=uid,
field=field,
spw=spw
)
filename = "./{}_spw_{}_frequencies".format(
uid,
spw
)
if astropy_is_imported:
fits.writeto(
filename="{}.fits".format(filename),
data=chan_freq
)
else:
with open("{}.numpy".format(filename), 'wb') as file:
np.save(file, chan_freq)
def get_antennas(ms):
antenna1 = getcol_wrapper(
ms=ms,
table="",
colname="ANTENNA1"
)
antenna2 = getcol_wrapper(
ms=ms,
table="",
colname="ANTENNA2"
)
return np.array([
antenna1,
antenna2
])
def export_antennas(ms, filename):
antennas = get_antennas(
ms=ms
)
if astropy_is_imported:
filename += ".fits"
else:
filename += ".numpy"
if filename.endswith(".fits"):
fits.writeto(
filename=filename,
data=antennas,
overwrite=True
)
else:
with open(filename, 'wb') as file:
np.save(file, antennas)
# def get_time(ms):
# time = getcol_wrapper(
# ms=ms,
# table="",
# colname="TIME"
# )
# return np.asarray(time)
# def export_time(ms, filename):
#
# time = get_time(
# ms=ms
# )
#
# # NOTE:
# if astropy_is_imported:
# filename += ".fits"
# else:
# filename += ".numpy"
# if filename.endswith(".fits"):
# fits.writeto(
# filename=filename,
# data=time,
# overwrite=True
# )
# else:
# with open(filename, 'wb') as file:
# np.save(file, time)
def get_scans(ms):
scans = getcol_wrapper(
ms=ms,
table="",
colname="SCAN_NUMBER"
)
return np.asarray(scans)
def export_scans(ms, filename):
scans = get_scans(
ms=ms
)
if astropy_is_imported:
filename += ".fits"
else:
filename += ".numpy"
if filename.endswith(".fits"):
fits.writeto(
filename=filename,
data=scans,
overwrite=True
)
else:
with open(filename, 'wb') as file:
np.save(file, scans)
if __name__ == "__main__":
#uid = "A002_X11adad7_Xd8c1"
uid = "A002_X11adad7_Xdfdb"
field = "SPT0314-44"
if not os.path.isdir(
"uid___{}_{}.ms.split.cal".format(uid, field)
):
if not os.path.isdir(
"uid___{}.ms.split.cal".format(uid)
):
raise NotImplementedError()
else:
split(
vis="uid___{}.ms.split.cal".format(
uid
),
outputvis="uid___{}_{}.ms.split.cal".format(
uid,
field
),
keepmms=True,
field=field,
spw="",
datacolumn="data",
keepflags=False
)
spws = [
"25",
"27",
"29",
#"31",
]
# NOTE:
width = 960
for spw in spws:
if not os.path.isdir(
"{}_{}_spw_{}_width_{}.ms.split.cal".format(
"uid___" + uid,
field,
spw,
width
)
):
split(
vis="uid___{}_{}.ms.split.cal".format(
uid,
field
),
outputvis="uid___{}_{}_spw_{}_width_{}.ms.split.cal".format(
uid,
field,
spw,
width
),
keepmms=True,
field=field,
spw=spw,
datacolumn="data",
width=width,
keepflags=False
)
# ========== #
# NOTE: ...
# ========== #
filename_uv_wavelengths = "uv_wavelengths_{}_{}_spw_{}_width_{}".format(
uid,
field,
spw,
width,
)
if os.path.isfile(filename_uv_wavelengths + ".fits") or os.path.isfile(filename_uv_wavelengths + ".numpy"):
pass
else:
export_uv_wavelengths(
ms="uid___{}_{}_spw_{}_width_{}.ms.split.cal".format(
uid,
field,
spw,
width
),
filename=filename_uv_wavelengths
)
# ========== #
# END
# ========== #
# ========== #
# NOTE: ...
# ========== #
filename_visibilities = "visibilities_{}_{}_spw_{}_width_{}".format(
uid,
field,
spw,
width,
)
if os.path.isfile(filename_visibilities + ".fits") or os.path.isfile(filename_visibilities + ".numpy"):
pass
else:
export_visibilities(
ms="uid___{}_{}_spw_{}_width_{}.ms.split.cal".format(
uid,
field,
spw,
width
),
filename=filename_visibilities
)
# ========== #
# END
# ========== #
# ========== #
# NOTE: ...
# ========== #
filename = "antennas_{}_{}_spw_{}_width_{}".format(
uid,
field,
spw,
width
)
export_antennas(
ms="uid___{}_{}_spw_{}_width_{}.ms.split.cal".format(
uid,
field,
spw,
width
),
filename=filename
)
# ========== #
# END
# ========== #
# ========== #
# NOTE: ...
# ========== #
filename = "scans_{}_{}_spw_{}_width_{}".format(
uid,
field,
spw,
width
)
export_scans(
ms="uid___{}_{}_spw_{}_width_{}.ms.split.cal".format(
uid,
field,
spw,
width
),
filename=filename
)
# ========== #
# END
# ========== #