tabsim 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tabsim/__init__.py +0 -0
- tabsim/config.py +904 -0
- tabsim/dask/__init__.py +0 -0
- tabsim/dask/coordinates.py +469 -0
- tabsim/dask/extras.py +60 -0
- tabsim/dask/interferometry.py +572 -0
- tabsim/dask/observation.py +969 -0
- tabsim/data/EDA2.itrf.txt +256 -0
- tabsim/data/MWA.itrf.txt +128 -0
- tabsim/data/MeerKAT.itrf.txt +64 -0
- tabsim/data/__init__.py +0 -0
- tabsim/data/aux_data/calibrator.skymodel +2 -0
- tabsim/data/aux_data/norad_satellite.rfimodel +61711 -0
- tabsim/data/aux_data/satellite.ole +3 -0
- tabsim/data/aux_data/satellite.rfimodel +3 -0
- tabsim/data/aux_data/stationary.loc +4 -0
- tabsim/data/aux_data/stationary.rfimodel +4 -0
- tabsim/data/config_files/sim_config_base.yaml +172 -0
- tabsim/data/config_files/tab_config_base.yaml +86 -0
- tabsim/jax/__init__.py +0 -0
- tabsim/jax/coordinates.py +1082 -0
- tabsim/jax/interferometry.py +667 -0
- tabsim/jax/utils.py +90 -0
- tabsim/plot.py +323 -0
- tabsim/scripts/__init__.py +1 -0
- tabsim/scripts/ast_region.py +56 -0
- tabsim/scripts/flag_data.py +195 -0
- tabsim/scripts/sim_vis.py +136 -0
- tabsim/scripts/tle_sat_region.py +191 -0
- tabsim/sky.py +137 -0
- tabsim/tle.py +753 -0
- tabsim/tools.py +111 -0
- tabsim/write.py +715 -0
- tabsim-0.0.1.dist-info/METADATA +991 -0
- tabsim-0.0.1.dist-info/RECORD +39 -0
- tabsim-0.0.1.dist-info/WHEEL +5 -0
- tabsim-0.0.1.dist-info/entry_points.txt +5 -0
- tabsim-0.0.1.dist-info/licenses/LICENSE +674 -0
- tabsim-0.0.1.dist-info/top_level.txt +1 -0
tabsim/write.py
ADDED
|
@@ -0,0 +1,715 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from tabsim.dask.observation import Observation
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
import shutil
|
|
10
|
+
|
|
11
|
+
import dask
|
|
12
|
+
import dask.array as da
|
|
13
|
+
import numpy as np
|
|
14
|
+
import xarray as xr
|
|
15
|
+
from daskms import Dataset, xds_to_table
|
|
16
|
+
import numpy as np
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def rm_dir(path: str, overwrite: bool = True):
|
|
20
|
+
"""Remove a directory but check for existence and overwrite flag before.
|
|
21
|
+
|
|
22
|
+
Parameters
|
|
23
|
+
----------
|
|
24
|
+
path : str
|
|
25
|
+
Directory path to remove.
|
|
26
|
+
overwrite : bool, optional
|
|
27
|
+
Whether to remove the directory, by default True
|
|
28
|
+
|
|
29
|
+
Raises
|
|
30
|
+
------
|
|
31
|
+
FileExistsError
|
|
32
|
+
If the directory exists and the overwrite flag is not enabled.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
if os.path.exists(path):
|
|
36
|
+
if overwrite:
|
|
37
|
+
shutil.rmtree(path)
|
|
38
|
+
else:
|
|
39
|
+
raise FileExistsError(
|
|
40
|
+
f"File {path} already exists.\n\nConsider using the 'overwrite' flag."
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def mk_obs_dir(output_path: str, obs_name: str, overwrite: bool = True) -> tuple:
|
|
45
|
+
"""Construct an observation simulation directory and return save paths for MS and zarr files.
|
|
46
|
+
|
|
47
|
+
Parameters
|
|
48
|
+
----------
|
|
49
|
+
output_path : str
|
|
50
|
+
Path to where to make simulation directory.
|
|
51
|
+
obs_name : str
|
|
52
|
+
Simulation observation name.
|
|
53
|
+
overwrite : bool, optional
|
|
54
|
+
Whether to overwrite a previous simulation directory, by default True.
|
|
55
|
+
|
|
56
|
+
Returns
|
|
57
|
+
-------
|
|
58
|
+
tuple
|
|
59
|
+
Path to the simulation directory and and the save paths for the zarr and MS files.
|
|
60
|
+
|
|
61
|
+
Raises
|
|
62
|
+
------
|
|
63
|
+
FileExistsError
|
|
64
|
+
If the simulation directory already exists and the overwrite flag is not True.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
save_path = os.path.join(output_path, obs_name)
|
|
68
|
+
rm_dir(save_path, overwrite)
|
|
69
|
+
os.makedirs(save_path, exist_ok=True)
|
|
70
|
+
zarr_path = os.path.join(save_path, obs_name + ".zarr")
|
|
71
|
+
ms_path = os.path.join(save_path, obs_name + ".ms")
|
|
72
|
+
|
|
73
|
+
return save_path, zarr_path, ms_path
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def mk_obs_name(prefix: str, obs: Observation, suffix: str = None) -> str:
|
|
77
|
+
"""Construct an observation name based on the parameters of the observation and an additional prefix.
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
----------
|
|
81
|
+
prefix : str
|
|
82
|
+
Prefix for the observation name.
|
|
83
|
+
obs : Observation
|
|
84
|
+
Observation class instance.
|
|
85
|
+
|
|
86
|
+
Returns
|
|
87
|
+
-------
|
|
88
|
+
str
|
|
89
|
+
Observation name
|
|
90
|
+
"""
|
|
91
|
+
|
|
92
|
+
obs_name = (
|
|
93
|
+
f"{prefix}_obs_{obs.n_ant:0>2}A_{obs.n_time:0>3}T-{int(obs.times[0]):0>4}-{int(obs.times[-1]):0>4}"
|
|
94
|
+
+ f"_{obs.n_int_samples:0>3}I_{obs.n_freq:0>3}F-{float(obs.freqs[0]):.3e}-{float(obs.freqs[-1]):.3e}"
|
|
95
|
+
+ f"_{obs.n_p_ast:0>3}PAST_{obs.n_g_ast:0>3}GAST_{obs.n_e_ast:0>3}EAST_{obs.n_rfi_tle_satellite}SAT_{obs.n_rfi_stationary}GRD"
|
|
96
|
+
)
|
|
97
|
+
if suffix is not None:
|
|
98
|
+
obs_name = obs_name + "_" + suffix
|
|
99
|
+
|
|
100
|
+
return obs_name
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def construct_observation_ds(obs: Observation):
|
|
104
|
+
"""Construct a dataset for a single observation."""
|
|
105
|
+
|
|
106
|
+
visibility_data = get_visibility_data(obs)
|
|
107
|
+
optional_data = get_optional_data(obs)
|
|
108
|
+
|
|
109
|
+
astromonical_source_data = get_astromonical_source_data(obs)
|
|
110
|
+
rfi_satellite_data = get_satellite_rfi_data(obs)
|
|
111
|
+
rfi_stationary_data = get_stationary_rfi_data(obs)
|
|
112
|
+
|
|
113
|
+
coordinates = get_coordinates(obs)
|
|
114
|
+
attributes = get_observation_attributes(obs)
|
|
115
|
+
|
|
116
|
+
data = {
|
|
117
|
+
**visibility_data,
|
|
118
|
+
**optional_data,
|
|
119
|
+
**astromonical_source_data,
|
|
120
|
+
**rfi_satellite_data,
|
|
121
|
+
**rfi_stationary_data,
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
ds = xr.Dataset(data_vars=data, coords=coordinates, attrs=attributes)
|
|
125
|
+
|
|
126
|
+
return ds
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def get_visibility_data(obs: Observation):
|
|
130
|
+
vis_data = {
|
|
131
|
+
"vis_ast": (["time", "bl", "freq"], obs.vis_ast),
|
|
132
|
+
"vis_rfi": (["time", "bl", "freq"], obs.vis_rfi),
|
|
133
|
+
"vis_obs": (["time", "bl", "freq"], obs.vis_obs),
|
|
134
|
+
"vis_calibrated": (["time", "bl", "freq"], obs.vis_cal),
|
|
135
|
+
"flags": (["time", "bl", "freq"], obs.flags),
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return vis_data
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def get_optional_data(obs: Observation):
|
|
142
|
+
opt_data = {
|
|
143
|
+
# Antenna indices
|
|
144
|
+
"antenna1": (["bl"], obs.a1),
|
|
145
|
+
"antenna2": (["bl"], obs.a2),
|
|
146
|
+
# Antenna positions
|
|
147
|
+
# "ants_enu": (["ant", "enu"], obs.ENU),
|
|
148
|
+
"ants_itrf": (["ant", "itrf"], obs.ITRF),
|
|
149
|
+
"ants_xyz": (["time_fine", "ant", "xyz"], obs.ants_xyz),
|
|
150
|
+
"ants_uvw": (["time_fine", "ant", "uvw"], obs.ants_uvw),
|
|
151
|
+
"bl_uvw": (["time_fine", "bl", "uvw"], obs.bl_uvw),
|
|
152
|
+
# Noise parameters
|
|
153
|
+
"SEFD": (["freq"], obs.SEFD),
|
|
154
|
+
"noise_std": (["freq"], obs.noise_std),
|
|
155
|
+
"noise_data": (["time", "bl", "freq"], obs.noise_data),
|
|
156
|
+
# Gain parameters
|
|
157
|
+
"gains_ants": (["time", "ant", "freq"], obs.gains_ants),
|
|
158
|
+
# Time index from time_fine to time
|
|
159
|
+
"time_idx": (["time"], obs.t_idx),
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return opt_data
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def get_observation_attributes(obs: Observation):
|
|
166
|
+
attrs = {
|
|
167
|
+
"tel_name": obs.tel_name,
|
|
168
|
+
"tel_latitude": obs.latitude,
|
|
169
|
+
"tel_longitude": obs.longitude,
|
|
170
|
+
"tel_elevation": obs.elevation,
|
|
171
|
+
"target_name": obs.target_name,
|
|
172
|
+
"target_ra": obs.ra,
|
|
173
|
+
"target_dec": obs.dec,
|
|
174
|
+
"int_time": obs.int_time,
|
|
175
|
+
"chan_width": obs.chan_width,
|
|
176
|
+
"dish_diameter": obs.dish_d,
|
|
177
|
+
"n_ant": obs.n_ant,
|
|
178
|
+
"n_freq": obs.n_freq,
|
|
179
|
+
"n_bl": obs.n_bl,
|
|
180
|
+
"n_time": obs.n_time,
|
|
181
|
+
"n_time_fine": obs.n_time_fine,
|
|
182
|
+
"n_int_samples": obs.n_int_samples,
|
|
183
|
+
"n_sat_src": obs.n_rfi_satellite,
|
|
184
|
+
"n_stat_src": obs.n_rfi_stationary,
|
|
185
|
+
"n_ast_src": obs.n_ast,
|
|
186
|
+
"n_ast_p_src": obs.n_p_ast,
|
|
187
|
+
"n_ast_g_src": obs.n_g_ast,
|
|
188
|
+
"n_ast_e_src": obs.n_e_ast,
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
attrs = {k: v.compute() if isinstance(v, da.Array) else v for k, v in attrs.items()}
|
|
192
|
+
|
|
193
|
+
return attrs
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def get_coordinates(obs: Observation):
|
|
197
|
+
coords = {
|
|
198
|
+
"time": obs.times,
|
|
199
|
+
"time_fine": obs.times_fine,
|
|
200
|
+
"time_mjd": obs.times_mjd,
|
|
201
|
+
"time_mjd_fine": obs.times_mjd_fine,
|
|
202
|
+
"freq": obs.freqs,
|
|
203
|
+
"bl": np.arange(obs.n_bl),
|
|
204
|
+
"ant": np.arange(obs.n_ant),
|
|
205
|
+
"uvw": np.array(["u", "v", "w"]),
|
|
206
|
+
"lmn": np.array(["l", "m", "n"]),
|
|
207
|
+
"itrf": np.array(["x0", "y0", "z0"]),
|
|
208
|
+
"xyz": np.array(["x", "y", "z"]),
|
|
209
|
+
"enu": np.array(["east", "north", "up"]),
|
|
210
|
+
"radec": np.array(["ra", "dec"]),
|
|
211
|
+
"geo": np.array(["latitude", "longitude", "elevation"]),
|
|
212
|
+
"orbit": np.array(["elevation", "inclination", "lon_asc_node", "periapsis"]),
|
|
213
|
+
"tle": np.array(["tle_line1", "tle_line2"]),
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return coords
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def get_astromonical_source_data(obs: Observation):
|
|
220
|
+
if obs.n_p_ast > 0:
|
|
221
|
+
ast_p_data = {
|
|
222
|
+
# Astronomical point source parameters
|
|
223
|
+
"ast_p_I": (
|
|
224
|
+
["ast_p_src", "time", "freq"],
|
|
225
|
+
da.concatenate(obs.ast_p_I, axis=0).rechunk("auto"),
|
|
226
|
+
),
|
|
227
|
+
"ast_p_lmn": (
|
|
228
|
+
["ast_p_src", "lmn"],
|
|
229
|
+
da.concatenate(obs.ast_p_lmn, axis=0).rechunk("auto"),
|
|
230
|
+
),
|
|
231
|
+
"ast_p_radec": (
|
|
232
|
+
["ast_p_src", "radec"],
|
|
233
|
+
da.concatenate(obs.ast_p_radec, axis=1).rechunk("auto").T,
|
|
234
|
+
),
|
|
235
|
+
}
|
|
236
|
+
else:
|
|
237
|
+
ast_p_data = {}
|
|
238
|
+
|
|
239
|
+
if obs.n_g_ast > 0:
|
|
240
|
+
ast_g_data = {
|
|
241
|
+
# Astronomical point source parameters
|
|
242
|
+
"ast_g_I": (
|
|
243
|
+
["ast_g_src", "time", "freq"],
|
|
244
|
+
da.concatenate(obs.ast_g_I, axis=0).rechunk("auto"),
|
|
245
|
+
),
|
|
246
|
+
"ast_g_lmn": (
|
|
247
|
+
["ast_g_src", "lmn"],
|
|
248
|
+
da.concatenate(obs.ast_g_lmn, axis=0).rechunk("auto"),
|
|
249
|
+
),
|
|
250
|
+
"ast_g_radec": (
|
|
251
|
+
["ast_g_src", "radec"],
|
|
252
|
+
da.concatenate(obs.ast_g_radec, axis=1).rechunk("auto").T,
|
|
253
|
+
),
|
|
254
|
+
"ast_g_major": (
|
|
255
|
+
["ast_g_src"],
|
|
256
|
+
da.concatenate(obs.ast_g_major, axis=0).rechunk("auto"),
|
|
257
|
+
),
|
|
258
|
+
"ast_g_minor": (
|
|
259
|
+
["ast_g_src"],
|
|
260
|
+
da.concatenate(obs.ast_g_minor, axis=0).rechunk("auto"),
|
|
261
|
+
),
|
|
262
|
+
"ast_g_pos_angle": (
|
|
263
|
+
["ast_g_src"],
|
|
264
|
+
da.concatenate(obs.ast_g_pos_angle, axis=0).rechunk("auto"),
|
|
265
|
+
),
|
|
266
|
+
}
|
|
267
|
+
else:
|
|
268
|
+
ast_g_data = {}
|
|
269
|
+
|
|
270
|
+
if obs.n_e_ast > 0:
|
|
271
|
+
ast_e_data = {
|
|
272
|
+
# Astronomical point source parameters
|
|
273
|
+
"ast_e_I": (
|
|
274
|
+
["ast_e_src", "time", "freq"],
|
|
275
|
+
da.concatenate(obs.ast_e_I, axis=0).rechunk("auto"),
|
|
276
|
+
),
|
|
277
|
+
"ast_e_lmn": (
|
|
278
|
+
["ast_e_src", "lmn"],
|
|
279
|
+
da.concatenate(obs.ast_e_lmn, axis=0).rechunk("auto"),
|
|
280
|
+
),
|
|
281
|
+
"ast_e_radec": (
|
|
282
|
+
["ast_e_src", "radec"],
|
|
283
|
+
da.concatenate(obs.ast_e_radec, axis=1).rechunk("auto").T,
|
|
284
|
+
),
|
|
285
|
+
"ast_e_major": (
|
|
286
|
+
["ast_e_src"],
|
|
287
|
+
da.concatenate(obs.ast_e_major, axis=0).rechunk("auto"),
|
|
288
|
+
),
|
|
289
|
+
}
|
|
290
|
+
else:
|
|
291
|
+
ast_e_data = {}
|
|
292
|
+
|
|
293
|
+
ast_data = {**ast_p_data, **ast_g_data, **ast_e_data}
|
|
294
|
+
|
|
295
|
+
return ast_data
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def get_satellite_rfi_data(obs: Observation):
|
|
299
|
+
if obs.n_rfi_satellite > 0:
|
|
300
|
+
# Circular Satellite RFI parameters
|
|
301
|
+
rfi_sat = {
|
|
302
|
+
"rfi_sat_A": (
|
|
303
|
+
["sat_src", "time_fine", "ant", "freq"],
|
|
304
|
+
da.concatenate(obs.rfi_satellite_A_app, axis=0).rechunk("auto"),
|
|
305
|
+
),
|
|
306
|
+
"rfi_sat_xyz": (
|
|
307
|
+
["sat_src", "time_fine", "xyz"],
|
|
308
|
+
da.concatenate(obs.rfi_satellite_xyz, axis=0).rechunk("auto"),
|
|
309
|
+
),
|
|
310
|
+
"rfi_sat_ang_sep": (
|
|
311
|
+
["sat_src", "time_fine", "ant"],
|
|
312
|
+
da.concatenate(obs.rfi_satellite_ang_sep, axis=0).rechunk("auto"),
|
|
313
|
+
),
|
|
314
|
+
"rfi_sat_orbit": (
|
|
315
|
+
["sat_src", "orbit"],
|
|
316
|
+
da.concatenate(obs.rfi_satellite_orbit, axis=0).rechunk("auto"),
|
|
317
|
+
),
|
|
318
|
+
}
|
|
319
|
+
else:
|
|
320
|
+
rfi_sat = {}
|
|
321
|
+
|
|
322
|
+
if obs.n_rfi_tle_satellite > 0:
|
|
323
|
+
# TLE Satellite RFI parameters
|
|
324
|
+
rfi_tle_sat = {
|
|
325
|
+
"rfi_tle_sat_A": (
|
|
326
|
+
["tle_sat_src", "time_fine", "ant", "freq"],
|
|
327
|
+
da.concatenate(obs.rfi_tle_satellite_A_app, axis=0).rechunk("auto"),
|
|
328
|
+
),
|
|
329
|
+
"rfi_tle_sat_xyz": (
|
|
330
|
+
["sat_src", "time_fine", "xyz"],
|
|
331
|
+
da.concatenate(obs.rfi_tle_satellite_xyz, axis=0).rechunk("auto"),
|
|
332
|
+
),
|
|
333
|
+
"rfi_tle_sat_ang_sep": (
|
|
334
|
+
["sat_src", "time_fine", "ant"],
|
|
335
|
+
da.concatenate(obs.rfi_tle_satellite_ang_sep, axis=0).rechunk("auto"),
|
|
336
|
+
),
|
|
337
|
+
"rfi_tle_sat_orbit": (
|
|
338
|
+
["sat_src", "tle"],
|
|
339
|
+
da.asarray(
|
|
340
|
+
np.concatenate(obs.rfi_tle_satellite_orbit, axis=0).astype("<U69")
|
|
341
|
+
).rechunk("auto"),
|
|
342
|
+
),
|
|
343
|
+
"norad_ids": (
|
|
344
|
+
["sat_src"],
|
|
345
|
+
da.asarray(np.concatenate(obs.norad_ids, axis=0).astype(int)).rechunk(
|
|
346
|
+
"auto"
|
|
347
|
+
),
|
|
348
|
+
),
|
|
349
|
+
}
|
|
350
|
+
else:
|
|
351
|
+
rfi_tle_sat = {}
|
|
352
|
+
|
|
353
|
+
return {**rfi_sat, **rfi_tle_sat}
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def get_stationary_rfi_data(obs: Observation):
|
|
357
|
+
|
|
358
|
+
if obs.n_rfi_stationary > 0:
|
|
359
|
+
rfi_stat = {
|
|
360
|
+
# Stationary RFI parameters
|
|
361
|
+
"rfi_stat_A": (
|
|
362
|
+
["stat_src", "time_fine", "ant", "freq"],
|
|
363
|
+
da.concatenate(obs.rfi_stationary_A_app, axis=0).rechunk("auto"),
|
|
364
|
+
),
|
|
365
|
+
"rfi_stat_xyz": (
|
|
366
|
+
["stat_src", "time_fine", "xyz"],
|
|
367
|
+
da.concatenate(obs.rfi_stationary_xyz, axis=0).rechunk("auto"),
|
|
368
|
+
),
|
|
369
|
+
"rfi_stat_ang_sep": (
|
|
370
|
+
["stat_src", "time_fine", "ant"],
|
|
371
|
+
da.concatenate(obs.rfi_stationary_ang_sep, axis=0).rechunk("auto"),
|
|
372
|
+
),
|
|
373
|
+
"rfi_stat_geo": (
|
|
374
|
+
["stat_src", "geo"],
|
|
375
|
+
da.concatenate(obs.rfi_stationary_geo, axis=0).rechunk("auto"),
|
|
376
|
+
),
|
|
377
|
+
}
|
|
378
|
+
else:
|
|
379
|
+
rfi_stat = {}
|
|
380
|
+
|
|
381
|
+
return rfi_stat
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def write_ms(
|
|
385
|
+
ds: Dataset,
|
|
386
|
+
ms_path: str,
|
|
387
|
+
overwrite: bool = False,
|
|
388
|
+
vis_corr: dask.Array = None,
|
|
389
|
+
flags: dask.Array = None,
|
|
390
|
+
):
|
|
391
|
+
"""Write a dataset to a Measurement Set."""
|
|
392
|
+
rm_dir(ms_path, overwrite)
|
|
393
|
+
|
|
394
|
+
tables = [
|
|
395
|
+
construct_ms_data_table(ds, ms_path, vis_corr=vis_corr, flags=flags),
|
|
396
|
+
construct_ms_antenna_table(ds, ms_path),
|
|
397
|
+
construct_ms_direction_table(ds, ms_path),
|
|
398
|
+
construct_ms_observation_table(ds, ms_path),
|
|
399
|
+
construct_ms_field_table(ds, ms_path),
|
|
400
|
+
construct_ms_data_desc_table(ds, ms_path),
|
|
401
|
+
construct_ms_spectral_window_table(ds, ms_path),
|
|
402
|
+
construct_ms_feed_table(ds, ms_path),
|
|
403
|
+
construct_ms_polarization_table(ds, ms_path),
|
|
404
|
+
]
|
|
405
|
+
for table in tables:
|
|
406
|
+
dask.compute(table)
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def construct_ms_data_table(
|
|
410
|
+
ds: Dataset,
|
|
411
|
+
ms_path: str,
|
|
412
|
+
vis_corr: dask.Array = None,
|
|
413
|
+
flags: dask.Array = None,
|
|
414
|
+
extras: bool = True,
|
|
415
|
+
):
|
|
416
|
+
"""Get the data table for a Measurement Set."""
|
|
417
|
+
n_time = ds.attrs["n_time"]
|
|
418
|
+
n_freq = ds.attrs["n_freq"]
|
|
419
|
+
n_corr = 1
|
|
420
|
+
n_bl = ds.attrs["n_bl"]
|
|
421
|
+
n_row = n_time * n_bl
|
|
422
|
+
|
|
423
|
+
noise_std = ds.noise_std.data.mean() * da.ones(shape=(n_row, 1))
|
|
424
|
+
|
|
425
|
+
vis_obs = ds.vis_obs.data.reshape(n_row, n_freq, n_corr)
|
|
426
|
+
vis_model = ds.vis_ast.data.reshape(n_row, n_freq, n_corr)
|
|
427
|
+
|
|
428
|
+
vis_cal = ds.vis_calibrated.data.reshape(n_row, n_freq, n_corr)
|
|
429
|
+
vis_rfi = ds.vis_rfi.data.reshape(n_row, n_freq, n_corr)
|
|
430
|
+
noise_data = ds.noise_data.data.reshape(n_row, n_freq, n_corr)
|
|
431
|
+
rfi_resid = vis_rfi + noise_data
|
|
432
|
+
no_rfi = vis_model + noise_data
|
|
433
|
+
|
|
434
|
+
if vis_corr is None:
|
|
435
|
+
vis_corr = da.zeros((n_row, n_freq, n_corr), dtype=np.complex64)
|
|
436
|
+
else:
|
|
437
|
+
vis_corr = da.asarray(vis_corr).reshape(n_row, n_freq, n_corr)
|
|
438
|
+
|
|
439
|
+
if flags is None:
|
|
440
|
+
flags = ds.flags.data.reshape(n_row, n_freq, n_corr)
|
|
441
|
+
else:
|
|
442
|
+
flags = da.asarray(flags).reshape(n_row, n_freq, n_corr)
|
|
443
|
+
|
|
444
|
+
row_times = da.asarray(
|
|
445
|
+
(ds.coords["time_mjd"].data[:, None] * da.ones(shape=(1, n_bl))).flatten()
|
|
446
|
+
)
|
|
447
|
+
ant1 = (
|
|
448
|
+
(ds.antenna1.data[None, :] * da.ones(shape=(n_time, 1)))
|
|
449
|
+
.flatten()
|
|
450
|
+
.astype(np.int32)
|
|
451
|
+
)
|
|
452
|
+
ant2 = (
|
|
453
|
+
(ds.antenna2.data[None, :] * da.ones(shape=(n_time, 1)))
|
|
454
|
+
.flatten()
|
|
455
|
+
.astype(np.int32)
|
|
456
|
+
)
|
|
457
|
+
|
|
458
|
+
uvw = ds.bl_uvw.data[ds.time_idx].reshape(-1, 3)
|
|
459
|
+
|
|
460
|
+
weight = da.ones(shape=(n_row, 1))
|
|
461
|
+
a_id = da.zeros(n_row, dtype=np.int32)
|
|
462
|
+
|
|
463
|
+
interval = ds.int_time * da.ones(n_row)
|
|
464
|
+
|
|
465
|
+
data_vars = {
|
|
466
|
+
"DATA": (("row", "chan", "corr"), vis_obs),
|
|
467
|
+
"ANTENNA1": (("row"), ant1),
|
|
468
|
+
"ANTENNA2": (("row"), ant2),
|
|
469
|
+
"TIME": (("row"), row_times),
|
|
470
|
+
"TIME_CENTROID": (("row"), row_times),
|
|
471
|
+
"UVW": (("row", "uvw"), uvw),
|
|
472
|
+
"CORRECTED_DATA": (("row", "chan", "corr"), vis_corr),
|
|
473
|
+
"MODEL_DATA": (
|
|
474
|
+
("row", "chan", "corr"),
|
|
475
|
+
da.zeros((n_row, n_freq, n_corr), dtype=np.complex64),
|
|
476
|
+
),
|
|
477
|
+
"SIGMA": (("row", "corr"), noise_std),
|
|
478
|
+
"WEIGHT": (("row", "corr"), weight),
|
|
479
|
+
"ARRAY_ID": (("row"), a_id),
|
|
480
|
+
"FLAG": (("row", "chan", "corr"), flags),
|
|
481
|
+
"INTERVAL": (("row"), interval),
|
|
482
|
+
"FLAG_CATEGORY": (
|
|
483
|
+
("row", "chan", "corr", "flagcat"),
|
|
484
|
+
da.expand_dims(flags, axis=3),
|
|
485
|
+
),
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if extras:
|
|
489
|
+
data_vars = {
|
|
490
|
+
**data_vars,
|
|
491
|
+
"CAL_DATA": (("row", "chan", "corr"), vis_cal.astype(np.complex64)),
|
|
492
|
+
"RFI_MODEL_DATA": (("row", "chan", "corr"), vis_rfi.astype(np.complex64)),
|
|
493
|
+
"AST_MODEL_DATA": (("row", "chan", "corr"), vis_model.astype(np.complex64)),
|
|
494
|
+
"NOISE_DATA": (("row", "chan", "corr"), noise_data.astype(np.complex64)),
|
|
495
|
+
"RFI_DATA": (("row", "chan", "corr"), rfi_resid.astype(np.complex64)),
|
|
496
|
+
"AST_DATA": (("row", "chan", "corr"), no_rfi.astype(np.complex64)),
|
|
497
|
+
"3S_FLAGS": (("row", "chan", "corr"), flags),
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
dims = ("row", "chan", "corr")
|
|
501
|
+
chunks = {k: v for k, v in zip(dims, vis_obs.chunksize)}
|
|
502
|
+
|
|
503
|
+
col_kw = {
|
|
504
|
+
"CAL_DATA": {"UNIT": "Jy"},
|
|
505
|
+
"RFI_MODEL_DATA": {"UNIT": "Jy"},
|
|
506
|
+
"NOISE_DATA": {"UNIT": "Jy"},
|
|
507
|
+
"RFI_DATA": {"UNIT": "Jy"},
|
|
508
|
+
"AST_DATA": {"UNIT": "Jy"},
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
return xds_to_table(
|
|
512
|
+
[Dataset(data_vars).chunk(chunks)],
|
|
513
|
+
ms_path,
|
|
514
|
+
columns="ALL",
|
|
515
|
+
column_keywords=col_kw,
|
|
516
|
+
)
|
|
517
|
+
|
|
518
|
+
#####################
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
def construct_ms_antenna_table(ds: Dataset, ms_path: str):
|
|
522
|
+
n_ant = ds.attrs["n_ant"]
|
|
523
|
+
|
|
524
|
+
# ants_xyz = ds.ants_xyz.data[0]
|
|
525
|
+
ants_itrf = ds.ants_itrf.data
|
|
526
|
+
dish_d = ds.attrs["dish_diameter"] * da.ones(n_ant)
|
|
527
|
+
mount = da.asarray(["ALT-AZ" for _ in range(n_ant)])
|
|
528
|
+
dish_type = da.asarray(["GROUND-BASED" for _ in range(n_ant)])
|
|
529
|
+
dish_name = da.asarray([f"m{i:0>3}" for i in range(n_ant)])
|
|
530
|
+
offset = da.zeros((n_ant, 3))
|
|
531
|
+
flag = da.zeros(n_ant)
|
|
532
|
+
|
|
533
|
+
data_vars = {
|
|
534
|
+
"POSITION": (("row", "xyz"), ants_itrf),
|
|
535
|
+
"DISH_DIAMETER": (("row"), dish_d),
|
|
536
|
+
"MOUNT": (("row"), mount),
|
|
537
|
+
"TYPE": (("row"), dish_type),
|
|
538
|
+
"NAME": (("row"), dish_name),
|
|
539
|
+
"STATION": (("row"), dish_name),
|
|
540
|
+
"OFFSET": (("row", "xyz"), offset),
|
|
541
|
+
"FLAG_ROW": (("row"), flag),
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
return xds_to_table([Dataset(data_vars)], ms_path + "::ANTENNA", columns="ALL")
|
|
545
|
+
|
|
546
|
+
#####################
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
def construct_ms_direction_table(ds: Dataset, ms_path: str):
|
|
550
|
+
target = da.deg2rad(
|
|
551
|
+
da.asarray([ds.attrs["target_ra"], ds.attrs["target_dec"]])
|
|
552
|
+
).reshape(1, 2)
|
|
553
|
+
|
|
554
|
+
data_vars = {"DIRECTION": (("row", "radec"), target)}
|
|
555
|
+
|
|
556
|
+
return xds_to_table([Dataset(data_vars)], ms_path + "::SOURCE", columns="ALL")
|
|
557
|
+
|
|
558
|
+
#####################
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
def construct_ms_observation_table(ds: Dataset, ms_path: str):
|
|
562
|
+
flag = da.zeros(1)
|
|
563
|
+
# log = da.zeros(1,1)
|
|
564
|
+
observer = da.array(["Chris Finlay"])
|
|
565
|
+
project = da.array(["tabascal"])
|
|
566
|
+
release = da.zeros(1)
|
|
567
|
+
tel_name = da.array([ds.attrs["tel_name"]])
|
|
568
|
+
time_range = da.asarray(ds.coords["time"].data[da.array([0, -1])].reshape(1, 2))
|
|
569
|
+
|
|
570
|
+
data_vars = {
|
|
571
|
+
"FLAG_ROW": (("row"), flag),
|
|
572
|
+
# 'LOG':(('row', 'log'), log),
|
|
573
|
+
"OBSERVER": (("row"), observer),
|
|
574
|
+
"PROJECT": (("row"), project),
|
|
575
|
+
"RELEASE_DATE": (("row"), release),
|
|
576
|
+
# 'SCHEDULE': (('row', 'schedule'), schedule),
|
|
577
|
+
# 'SCHEDULE_TYPE': (('row', 'xyz'), schedule_type),
|
|
578
|
+
"TELESCOPE_NAME": (("row"), tel_name),
|
|
579
|
+
"TIME_RANGE": (("row", "obs-exts"), time_range),
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
return xds_to_table([Dataset(data_vars)], ms_path + "::OBSERVATION", columns="ALL")
|
|
583
|
+
|
|
584
|
+
#####################
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
def construct_ms_field_table(ds: Dataset, ms_path: str):
|
|
588
|
+
code = da.array(["T"])
|
|
589
|
+
target = da.deg2rad(
|
|
590
|
+
da.asarray([ds.attrs["target_ra"], ds.attrs["target_dec"]])
|
|
591
|
+
).reshape(1, 1, 2)
|
|
592
|
+
flag = da.zeros(1)
|
|
593
|
+
num_poly = da.zeros(1)
|
|
594
|
+
source_id = da.zeros(1)
|
|
595
|
+
time = da.asarray(ds.coords["time"].data[:1])
|
|
596
|
+
name = da.array([ds.target_name])
|
|
597
|
+
|
|
598
|
+
data_vars = {
|
|
599
|
+
"CODE": (("row"), code),
|
|
600
|
+
"DELAY_DIR": (("row", "field-poly", "field-dir"), target),
|
|
601
|
+
"FLAG_ROW": (("row"), flag),
|
|
602
|
+
"NUM_POLY": (("row"), num_poly),
|
|
603
|
+
"PHASE_DIR": (("row", "field-poly", "field-dir"), target),
|
|
604
|
+
"REFERENCE_DIR": (("row", "field-poly", "field-dir"), target),
|
|
605
|
+
"SOURCE_ID": (("row"), source_id),
|
|
606
|
+
"TIME": (("row"), time),
|
|
607
|
+
"NAME": (("row"), name),
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
return xds_to_table([Dataset(data_vars)], ms_path + "::FIELD", columns="ALL")
|
|
611
|
+
|
|
612
|
+
#####################
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
def construct_ms_data_desc_table(ds: Dataset, ms_path: str):
|
|
616
|
+
flag = da.zeros(1, dtype=bool)
|
|
617
|
+
pol_id = da.zeros(1)
|
|
618
|
+
spec_id = da.zeros(1)
|
|
619
|
+
|
|
620
|
+
data_vars = {
|
|
621
|
+
"FLAG_ROW": (("row"), flag),
|
|
622
|
+
"POLARIZATION_ID": (("row"), pol_id),
|
|
623
|
+
"SPECTRAL_WINDOW_ID": (("row"), spec_id),
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
return xds_to_table(
|
|
627
|
+
[Dataset(data_vars)], ms_path + "::DATA_DESCRIPTION", columns="ALL"
|
|
628
|
+
)
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
def construct_ms_spectral_window_table(ds: Dataset, ms_path: str):
|
|
632
|
+
n_freq = ds.attrs["n_freq"]
|
|
633
|
+
chan_freq = da.asarray(ds.coords["freq"].data.reshape(1, n_freq))
|
|
634
|
+
chan_width = ds.attrs["chan_width"] * da.ones((1, n_freq))
|
|
635
|
+
ref_freq = chan_freq[:, 0]
|
|
636
|
+
total_bw = chan_width.sum(axis=1)
|
|
637
|
+
zero = da.zeros(1)
|
|
638
|
+
one = da.ones(1)
|
|
639
|
+
|
|
640
|
+
data_vars = {
|
|
641
|
+
"CHAN_FREQ": (("row", "chan"), chan_freq),
|
|
642
|
+
"CHAN_WIDTH": (("row", "chan"), chan_width),
|
|
643
|
+
"EFFECTIVE_BW": (("row", "chan"), chan_width),
|
|
644
|
+
"EFFECTIVE_BW": (("row", "chan"), n_freq * chan_width),
|
|
645
|
+
"FLAG_ROW": (("row"), zero),
|
|
646
|
+
"FREQ_GROUP": (("row"), zero),
|
|
647
|
+
# 'FREQ_GROUP_NAME': (('row'), zero),
|
|
648
|
+
"IF_CONV_CHAIN": (("row"), zero),
|
|
649
|
+
"MEAS_FREQ_REF": (("row"), 5 * one),
|
|
650
|
+
# 'NAME': (('row'), zero),
|
|
651
|
+
"NET_SIDEBAND": (("row"), one),
|
|
652
|
+
"NUM_CHAN": (("row"), one),
|
|
653
|
+
"REF_FREQUENCY": (("row"), ref_freq),
|
|
654
|
+
"RESOLUTION": (("row", "chan"), chan_width),
|
|
655
|
+
"TOTAL_BANDWIDTH": (("row"), total_bw),
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
return xds_to_table(
|
|
659
|
+
[Dataset(data_vars)], ms_path + "::SPECTRAL_WINDOW", columns="ALL"
|
|
660
|
+
)
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
def construct_ms_feed_table(ds: Dataset, ms_path: str):
|
|
664
|
+
n_ant = ds.attrs["n_ant"]
|
|
665
|
+
ant_id = da.arange(n_ant)
|
|
666
|
+
beam_id = da.ones(n_ant)
|
|
667
|
+
beam_offset = da.zeros(shape=(n_ant, 2, 2))
|
|
668
|
+
feed_id = da.zeros(n_ant)
|
|
669
|
+
interval = da.zeros(n_ant)
|
|
670
|
+
n_recep = 2 * da.ones(n_ant)
|
|
671
|
+
pol_type = da.array([["X", "Y"] for _ in range(n_ant)])
|
|
672
|
+
pol_resp = da.array([da.eye(2, dtype=np.complex64) for _ in range(n_ant)]).rechunk(
|
|
673
|
+
n_ant, 2, 2
|
|
674
|
+
)
|
|
675
|
+
pos = da.zeros(shape=(n_ant, 3))
|
|
676
|
+
rec_ang = -0.5 * np.pi * da.ones(shape=(n_ant, 2))
|
|
677
|
+
spec_id = -1 * da.ones(n_ant)
|
|
678
|
+
time = da.zeros(n_ant)
|
|
679
|
+
|
|
680
|
+
data_vars = {
|
|
681
|
+
"ANTENNA_ID": (("row"), ant_id),
|
|
682
|
+
"BEAM_ID": (("row"), beam_id),
|
|
683
|
+
"BEAM_OFFSET": (("row", "receptors", "radec"), beam_offset),
|
|
684
|
+
"FEED_ID": (("row"), feed_id),
|
|
685
|
+
"INTERVAL": (("row"), interval),
|
|
686
|
+
"NUM_RECEPTORS": (("row"), n_recep),
|
|
687
|
+
"POLARIZATION_TYPE": (("row", "receptors"), pol_type),
|
|
688
|
+
"POL_RESPONSE": (("row", "receptors", "receptors-2"), pol_resp),
|
|
689
|
+
"POSITION": (("row", "xyz"), pos),
|
|
690
|
+
"RECEPTOR_ANGLE": (("row", "receptors"), rec_ang),
|
|
691
|
+
"SPECTRAL_WINDOW_ID": (("row"), spec_id),
|
|
692
|
+
"TIME": (("row"), time),
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
return xds_to_table([Dataset(data_vars)], ms_path + "::FEED", columns="ALL")
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
def construct_ms_polarization_table(ds: Dataset, ms_path: str):
|
|
699
|
+
# corr_prod = da.zeros(shape=(1, 2, 2))
|
|
700
|
+
# corr_type = da.array([[9, 12]]) # XX, YY
|
|
701
|
+
# num_corr = 2 * da.ones(1)
|
|
702
|
+
corr_prod = da.zeros(shape=(1, 1, 1))
|
|
703
|
+
corr_type = 9 * da.ones((1, 1)) # XX
|
|
704
|
+
|
|
705
|
+
num_corr = da.ones(1)
|
|
706
|
+
flag_row = da.zeros(1)
|
|
707
|
+
|
|
708
|
+
data_vars = {
|
|
709
|
+
"CORR_PRODUCT": (("row", "corr", "corrprod_idx"), corr_prod),
|
|
710
|
+
"CORR_TYPE": (("row", "corr"), corr_type),
|
|
711
|
+
"FLAG_ROW": (("row"), flag_row),
|
|
712
|
+
"NUM_CORR": (("row"), num_corr),
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
return xds_to_table([Dataset(data_vars)], ms_path + "::POLARIZATION", columns="ALL")
|