Repository navigation
Expand file tree
/
Copy pathtest_export_ms.py
More file actions
388 lines (310 loc) · 14.4 KB
/
Copy pathtest_export_ms.py
File metadata and controls
388 lines (310 loc) · 14.4 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
# -*- coding: utf-8 -*-
"""Unit test for the measurementset module."""
import os
import time
import shutil
import tempfile
import unittest
import numpy
from data_models.memory_data_models import Configuration
from data_models.polarisation import ReceptorFrame
from astropy.coordinates import EarthLocation
try:
import casacore
from casacore.tables import table # pylint: disable=import-error
from processing_components.visibility import msv2
from processing_components.visibility.msv2fund import Stand, Antenna
run_ms_tests = True
# except ModuleNotFoundError:
except:
run_ms_tests = False
class measurementset_tests(unittest.TestCase):
"""A unittest.TestCase collection of unit tests for the lsl.writer.measurementset.Ms
class."""
testPath = None
def setUp(self):
"""Turn off all numpy warnings and create the temporary file directory."""
numpy.seterr(all='ignore')
self.testPath = tempfile.mkdtemp(prefix='test-measurementset-', suffix='.tmp')
def __initData(self):
"""Private function to generate a random set of data for writing a UVFITS
file. The data is returned as a dictionary with keys:
* freq - frequency array in Hz
* site - Observatory object
* stands - array of stand numbers
* bl - list of baseline pairs in real stand numbers
* vis - array of visibility data in baseline x freq format
"""
if run_ms_tests == False:
return
# Frequency range
freq = numpy.arange(0,512)*20e6/512 + 40e6
channel_width = numpy.full_like(freq,20e6/512.)
# Site and stands
obs = EarthLocation(lon="116.76444824", lat="-26.824722084", height=300.0)
mount = numpy.array(['equat','equat','equat','equat','equat','equat','equat','equat','equat','equat'])
names = numpy.array(['ak02','ak04','ak05','ak12','ak13','ak14','ak16','ak24','ak28','ak30'])
diameter = numpy.array([12.,12.,12.,12.,12.,12.,12.,12.,12.,12.])
xyz = numpy.array([[-2556109.98244348,5097388.70050131,-2848440.1332423 ],
[-2556087.396082 , 5097423.589662 , -2848396.867933 ],
[-2556028.60254059, 5097451.46195695, -2848399.83113161],
[-2556496.23893101, 5097333.71466669, -2848187.33832738],
[-2556407.35299627, 5097064.98390756, -2848756.02069474],
[-2555972.78456557, 5097233.65481756, -2848839.88915184],
[-2555592.88867802, 5097835.02121109, -2848098.26409648],
[-2555959.34313275, 5096979.52802882, -2849303.57702486],
[-2556552.97431815, 5097767.23612874, -2847354.29540396],
[-2557348.40370367, 5097170.17682775, -2847716.21368966]])
site_config = Configuration(name='ASKAP', data=None, location= obs,
names=names, xyz=xyz, mount=mount, frame=None,
receptor_frame=ReceptorFrame("linear"),
diameter=diameter)
antennas = []
for i in range(len(names)):
antennas.append(Antenna(i,Stand(names[i],xyz[i,0],xyz[i,1],xyz[i,2])))
# Set baselines and data
blList = []
N = len(antennas)
antennas2 = antennas
for i in range(0, N - 1):
for j in range(i + 1, N):
blList.append((antennas[i], antennas2[j]))
visData = numpy.random.rand(len(blList), len(freq))
visData = visData.astype(numpy.complex64)
return {'freq': freq, 'channel_width':channel_width, 'site': site_config, 'antennas': antennas, 'bl': blList, 'vis': visData}
def __initData_WGS84(self):
"""Private function to generate a random set of data for writing a Measurements
file. The data is returned as a dictionary with keys:
* freq - frequency array in Hz
* site - observatory object
* stands - array of stand numbers
* bl - list of baseline pairs in real stand numbers
* vis - array of visibility data in baseline x freq format
"""
if run_ms_tests == False:
return
# Frequency range
freq = numpy.arange(0, 512) * 20e6 / 512 + 40e6
channel_width = numpy.full_like(freq, 20e6 / 512.)
# Site and stands
obs = EarthLocation(lon="+116.6356824", lat="-26.70130064", height=377.0)
names = numpy.array(['A%02d' % i for i in range(36)])
mount = numpy.array(['equat' for i in range(36)])
diameter = numpy.array([12 for i in range(36)])
xyz = numpy.array([[-175.233429, +1673.460938, 0.0000],
[+261.119019, +796.922119, 0.0000],
[-29.2005200, +744.432068, 0.0000],
[-289.355286, +586.936035, 0.0000],
[-157.031570, +815.570068, 0.0000],
[-521.311646, +754.674927, 0.0000],
[-1061.114258, +840.541443, 0.0000],
[-921.829407, +997.627686, 0.0000],
[-818.293579, +1142.272095, 0.0000],
[-531.752808, +850.726257, 0.0000],
[+81.352448, +790.245117, 0.0000],
[+131.126358, +1208.831909, 0.0000],
[-1164.709351, +316.779236, 0.0000],
[-686.248901, +590.285278, 0.0000],
[-498.987305, +506.338226, 0.0000],
[-182.249146, +365.113464, 0.0000],
[+420.841858, +811.081543, 0.0000],
[+804.107910, +1273.328369, 0.0000],
[-462.810394, +236.353790, 0.0000],
[-449.772339, +15.039755, 0.0000],
[+13.791821, +110.971809, 0.0000],
[-425.687317, -181.908752, 0.0000],
[-333.404053, -503.603394, 0.0000],
[-1495.472412, +1416.063232, 0.0000],
[-1038.578857, +1128.367920, 0.0000],
[-207.151749, +956.312561, 0.0000],
[-389.051880, +482.405670, 0.0000],
[-434.000000, +510.000000, 0.0000],
[-398.000000, +462.000000, 0.0000],
[-425.000000, +475.000000, 0.0000],
[-400.000000, +3664.000000, 0.0000],
[+1796.000000, +1468.000000, 0.0000],
[+2600.000000, -1532.000000, 0.0000],
[-400.000000, -2336.000000, 0.0000],
[-3400.00000, -1532.000000, 0.0000],
[-2596.000000, +1468.000000, 0.0000]])
site_config = Configuration(name='ASKAP', data=None, location=obs,
names=names, xyz=xyz, mount=mount, frame=None,
receptor_frame=ReceptorFrame("linear"),
diameter=diameter)
antennas = []
for i in range(len(names)):
antennas.append(Antenna(i, Stand(names[i], xyz[i, 0], xyz[i, 1], xyz[i, 2])))
# Set baselines and data
blList = []
N = len(antennas)
antennas2 = antennas
for i in range(0, N - 1):
for j in range(i + 1, N):
blList.append((antennas[i], antennas2[j]))
visData = numpy.random.rand(len(blList), len(freq))
visData = visData.astype(numpy.complex64)
return {'freq': freq, 'channel_width': channel_width, 'site': site_config, 'antennas': antennas, 'bl': blList,
'vis': visData}
def test_write_tables(self):
"""Test if the MeasurementSet writer writes all of the tables."""
if run_ms_tests==False:
return
testTime = time.time()
testFile = os.path.join(self.testPath, 'ms-test-W.ms')
# Get some data
data = self.__initData()
# Start the table
tbl = msv2.Ms(testFile, ref_time=testTime)
tbl.set_stokes(['xx'])
tbl.set_frequency(data['freq'],data['channel_width'])
tbl.set_geometry(data['site'], data['antennas'])
tbl.add_data_set(testTime, 2.0, data['bl'], data['vis'])
tbl.write()
# Make sure everyone is there
self.assertTrue(os.path.exists(testFile))
for tbl in ('ANTENNA', 'DATA_DESCRIPTION', 'FEED', 'FIELD', 'FLAG_CMD', 'HISTORY',
'OBSERVATION', 'POINTING', 'POLARIZATION', 'PROCESSOR', 'SOURCE',
'SPECTRAL_WINDOW', 'STATE'):
self.assertTrue(os.path.exists(os.path.join(testFile, tbl)))
def test_write_tables_wgs84(self):
"""Test if the MeasurementSet writer writes all of the tables."""
if run_ms_tests==False:
return
testTime = time.time()
testFile = os.path.join(self.testPath, 'ms-test-WGS.ms')
# Get some data
data = self.__initData_WGS84()
# Start the table
tbl = msv2.Ms(testFile, ref_time=testTime, frame='wgs84')
tbl.set_stokes(['xx'])
tbl.set_frequency(data['freq'],data['channel_width'])
tbl.set_geometry(data['site'], data['antennas'])
tbl.add_data_set(testTime, 2.0, data['bl'], data['vis'])
tbl.write()
# Make sure everyone is there
self.assertTrue(os.path.exists(testFile))
for tbl in ('ANTENNA', 'DATA_DESCRIPTION', 'FEED', 'FIELD', 'FLAG_CMD', 'HISTORY',
'OBSERVATION', 'POINTING', 'POLARIZATION', 'PROCESSOR', 'SOURCE',
'SPECTRAL_WINDOW', 'STATE'):
self.assertTrue(os.path.exists(os.path.join(testFile, tbl)))
def test_main_table(self):
"""Test the primary data table."""
if run_ms_tests==False:
return
testTime = time.time()
testFile = os.path.join(self.testPath, 'ms-test-UV.ms')
# Get some data
data = self.__initData()
# Start the file
fits = msv2.Ms(testFile, ref_time=testTime)
fits.set_stokes(['xx'])
fits.set_frequency(data['freq'],data['channel_width'])
fits.set_geometry(data['site'], data['antennas'])
fits.add_data_set(testTime, 6.0, data['bl'], data['vis'])
fits.write()
# Open the table and examine
ms = casacore.tables.table(testFile, ack=False)
uvw = ms.getcol('UVW')
ant1 = ms.getcol('ANTENNA1')
ant2 = ms.getcol('ANTENNA2')
vis = ms.getcol('DATA')
ms2 = casacore.tables.table(os.path.join(testFile, 'ANTENNA'), ack=False)
mapper = ms2.getcol('NAME')
# Correct number of visibilities
self.assertEqual(uvw.shape[0], data['vis'].shape[0])
self.assertEqual(vis.shape[0], data['vis'].shape[0])
# Correct number of uvw coordinates
self.assertEqual(uvw.shape[1], 3)
# Correct number of frequencies
self.assertEqual(vis.shape[1], data['freq'].size)
# Correct values
for row in range(uvw.shape[0]):
stand1 = ant1[row]
stand2 = ant2[row]
visData = vis[row,:,0]
# Find out which visibility set in the random data corresponds to the
# current visibility
i = 0
for a1,a2 in data['bl']:
if a1.stand.id == mapper[stand1] and a2.stand.id == mapper[stand2]:
break
else:
i = i + 1
# Run the comparison
for vd, sd in zip(visData, data['vis'][i,:]):
self.assertAlmostEqual(vd, sd, 8)
i = i + 1
ms.close()
ms2.close()
def test_multi_if(self):
"""writing more than one spectral window to a MeasurementSet."""
if run_ms_tests==False:
return
testTime = time.time()
testFile = os.path.join(self.testPath, 'ms-test-MultiIF.ms')
# Get some data
data = self.__initData()
# Start the file
fits = msv2.Ms(testFile, ref_time=testTime)
fits.set_stokes(['xx'])
fits.set_frequency(data['freq'],data['channel_width'])
fits.set_frequency(data['freq']+10e6,data['channel_width'])
fits.set_geometry(data['site'], data['antennas'])
fits.add_data_set(testTime, 6.0, data['bl'],
numpy.concatenate([data['vis'], 10*data['vis']], axis=1))
fits.write()
# Open the table and examine
ms = casacore.tables.table(testFile, ack=False)
uvw = ms.getcol('UVW')
ant1 = ms.getcol('ANTENNA1')
ant2 = ms.getcol('ANTENNA2')
ddsc = ms.getcol('DATA_DESC_ID')
vis = ms.getcol('DATA')
ms2 = casacore.tables.table(os.path.join(testFile, 'ANTENNA'), ack=False)
mapper = ms2.getcol('NAME')
ms3 = casacore.tables.table(os.path.join(testFile, 'DATA_DESCRIPTION'), ack=False)
spw = [i for i in ms3.getcol('SPECTRAL_WINDOW_ID')]
# Correct number of visibilities
self.assertEqual(uvw.shape[0], 2*data['vis'].shape[0])
self.assertEqual(vis.shape[0], 2*data['vis'].shape[0])
# Correct number of uvw coordinates
self.assertEqual(uvw.shape[1], 3)
# Correct number of frequencies
self.assertEqual(vis.shape[1], data['freq'].size)
# Correct values
for row in range(uvw.shape[0]):
stand1 = ant1[row]
stand2 = ant2[row]
descid = ddsc[row]
visData = vis[row,:,0]
# Find out which visibility set in the random data corresponds to the
# current visibility
i = 0
for a1,a2 in data['bl']:
if a1.stand.id == mapper[stand1] and a2.stand.id == mapper[stand2]:
break
else:
i = i + 1
# Find out which spectral window this corresponds to
if spw[descid] == 0:
compData = data['vis']
else:
compData = 10*data['vis']
# Run the comparison
for vd, sd in zip(visData, compData[i,:]):
self.assertAlmostEqual(vd, sd, 8)
ms.close()
ms2.close()
ms3.close()
def tearDown(self):
"""Remove the test path directory and its contents"""
shutil.rmtree(self.testPath, ignore_errors=True)
class measurementset_test_suite(unittest.TestSuite):
"""A unittest.TestSuite class which contains all of the lsl.reader units
tests."""
def __init__(self):
unittest.TestSuite.__init__(self)
loader = unittest.TestLoader()
self.addTests(loader.loadTestsFromTestCase(measurementset_tests))
if __name__ == '__main__':
unittest.main()