-
Notifications
You must be signed in to change notification settings - Fork 6
/
LofarMetaDataUtil.cc
336 lines (278 loc) · 11.3 KB
/
LofarMetaDataUtil.cc
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
//# LofarMetaDataUtil.cc: Utility functions to read the meta data relevant for
//# simulating the beam from LOFAR observations stored in MS format.
//#
//# Copyright (C) 2013
//# ASTRON (Netherlands Institute for Radio Astronomy)
//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
//#
//# This file is part of the LOFAR software suite.
//# The LOFAR software suite is free software: you can redistribute it and/or
//# modify it under the terms of the GNU General Public License as published
//# by the Free Software Foundation, either version 3 of the License, or
//# (at your option) any later version.
//#
//# The LOFAR software suite is distributed in the hope that it will be useful,
//# but WITHOUT ANY WARRANTY; without even the implied warranty of
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//# GNU General Public License for more details.
//#
//# You should have received a copy of the GNU General Public License along
//# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
//#
//# $Id$
#include "LofarMetaDataUtil.h"
#include "AntennaFieldLBA.h"
#include "AntennaFieldHBA.h"
#include "MathUtil.h"
#include "TileAntenna.h"
#include "DualDipoleAntenna.h"
#include <casacore/measures/Measures/MDirection.h>
#include <casacore/measures/Measures/MPosition.h>
#include <casacore/measures/Measures/MCDirection.h>
#include <casacore/measures/Measures/MCPosition.h>
#include <casacore/measures/Measures/MeasTable.h>
#include <casacore/measures/Measures/MeasConvert.h>
#include <casacore/measures/TableMeasures/ScalarMeasColumn.h>
#include <cassert>
#include <stdexcept>
#include <casacore/ms/MeasurementSets/MSAntenna.h>
#include <casacore/ms/MSSel/MSSelection.h>
#include <casacore/ms/MSSel/MSAntennaParse.h>
#include <casacore/ms/MeasurementSets/MSAntennaColumns.h>
#include <casacore/ms/MeasurementSets/MSDataDescription.h>
#include <casacore/ms/MeasurementSets/MSDataDescColumns.h>
#include <casacore/ms/MeasurementSets/MSField.h>
#include <casacore/ms/MeasurementSets/MSFieldColumns.h>
#include <casacore/ms/MeasurementSets/MSObservation.h>
#include <casacore/ms/MeasurementSets/MSObsColumns.h>
#include <casacore/ms/MeasurementSets/MSPolarization.h>
#include <casacore/ms/MeasurementSets/MSPolColumns.h>
#include <casacore/ms/MeasurementSets/MSSpectralWindow.h>
#include <casacore/ms/MeasurementSets/MSSpWindowColumns.h>
namespace LOFAR
{
namespace StationResponse
{
using namespace casacore;
using namespace std;
bool hasColumn(const Table &table, const string &column)
{
return table.tableDesc().isColumn(column);
}
bool hasSubTable(const Table &table, const string &name)
{
return table.keywordSet().isDefined(name);
}
Table getSubTable(const Table &table, const string &name)
{
return table.keywordSet().asTable(name);
}
TileAntenna::TileConfig readTileConfig(const Table &table, unsigned int row)
{
ROArrayQuantColumn<Double> c_tile_offset(table, "TILE_ELEMENT_OFFSET", "m");
// Read tile configuration for HBA antenna fields.
Matrix<Quantity> aips_offset = c_tile_offset(row);
assert(aips_offset.ncolumn() == TileAntenna::TileConfig::size());
TileAntenna::TileConfig config;
for(unsigned int i = 0; i < config.size(); ++i)
{
config[i][0] = aips_offset(0, i).getValue();
config[i][1] = aips_offset(1, i).getValue();
config[i][2] = aips_offset(2, i).getValue();
}
return config;
}
void transformToFieldCoordinates(TileAntenna::TileConfig &config,
const AntennaField::CoordinateSystem::Axes &axes)
{
for(unsigned int i = 0; i < config.size(); ++i)
{
const vector3r_t position = config[i];
config[i][0] = dot(position, axes.p);
config[i][1] = dot(position, axes.q);
config[i][2] = dot(position, axes.r);
}
}
AntennaField::CoordinateSystem readCoordinateSystemAartfaac(
const Table &table, unsigned int id)
{
ROArrayQuantColumn<Double> c_position(table, "POSITION", "m");
// Read antenna field center (ITRF).
Vector<Quantity> aips_position = c_position(id);
assert(aips_position.size() == 3);
vector3r_t position = {{aips_position(0).getValue(),
aips_position(1).getValue(), aips_position(2).getValue()}};
TableRecord keywordset = table.keywordSet();
Matrix<double> aips_axes;
keywordset.get("AARTFAAC_COORDINATE_AXES", aips_axes);
assert(aips_axes.shape().isEqual(IPosition(2, 3, 3)));
vector3r_t p = {{aips_axes(0, 0), aips_axes(1, 0), aips_axes(2, 0)}};
vector3r_t q = {{aips_axes(0, 1), aips_axes(1, 1), aips_axes(2, 1)}};
vector3r_t r = {{aips_axes(0, 2), aips_axes(1, 2), aips_axes(2, 2)}};
AntennaField::CoordinateSystem system = {position, {p, q, r}};
return system;
}
AntennaField::CoordinateSystem readCoordinateSystem(const Table &table,
unsigned int id)
{
ROArrayQuantColumn<Double> c_position(table, "POSITION", "m");
ROArrayQuantColumn<Double> c_axes(table, "COORDINATE_AXES", "m");
// Read antenna field center (ITRF).
Vector<Quantity> aips_position = c_position(id);
assert(aips_position.size() == 3);
vector3r_t position = {{aips_position(0).getValue(),
aips_position(1).getValue(), aips_position(2).getValue()}};
// Read antenna field coordinate axes (ITRF).
Matrix<Quantity> aips_axes = c_axes(id);
assert(aips_axes.shape().isEqual(IPosition(2, 3, 3)));
vector3r_t p = {{aips_axes(0, 0).getValue(), aips_axes(1, 0).getValue(),
aips_axes(2, 0).getValue()}};
vector3r_t q = {{aips_axes(0, 1).getValue(), aips_axes(1, 1).getValue(),
aips_axes(2, 1).getValue()}};
vector3r_t r = {{aips_axes(0, 2).getValue(), aips_axes(1, 2).getValue(),
aips_axes(2, 2).getValue()}};
AntennaField::CoordinateSystem system = {position, {p, q, r}};
return system;
}
void readAntennae(const Table &table, unsigned int id,
const AntennaField::Ptr &field)
{
ROArrayQuantColumn<Double> c_offset(table, "ELEMENT_OFFSET", "m");
ROArrayColumn<Bool> c_flag(table, "ELEMENT_FLAG");
// Read element offsets and flags.
Matrix<Quantity> aips_offset = c_offset(id);
assert(aips_offset.shape().isEqual(IPosition(2, 3, aips_offset.ncolumn())));
Matrix<Bool> aips_flag = c_flag(id);
assert(aips_flag.shape().isEqual(IPosition(2, 2, aips_offset.ncolumn())));
for(size_t i = 0; i < aips_offset.ncolumn(); ++i)
{
AntennaField::Antenna antenna;
antenna.position[0] = aips_offset(0, i).getValue();
antenna.position[1] = aips_offset(1, i).getValue();
antenna.position[2] = aips_offset(2, i).getValue();
antenna.enabled[0] = !aips_flag(0, i);
antenna.enabled[1] = !aips_flag(1, i);
field->addAntenna(antenna);
}
}
AntennaField::Ptr readAntennaField(const Table &table, unsigned int id)
{
AntennaField::Ptr field;
AntennaField::CoordinateSystem system = readCoordinateSystem(table, id);
ROScalarColumn<String> c_name(table, "NAME");
const string &name = c_name(id);
if(name == "LBA")
{
DualDipoleAntenna::Ptr model(new DualDipoleAntenna());
field = AntennaField::Ptr(new AntennaFieldLBA(name, system, model));
}
else // HBA, HBA0, HBA1
{
TileAntenna::TileConfig config = readTileConfig(table, id);
transformToFieldCoordinates(config, system.axes);
TileAntenna::Ptr model(new TileAntenna(config));
field = AntennaField::Ptr(new AntennaFieldHBA(name, system, model));
}
readAntennae(table, id, field);
return field;
}
AntennaField::Ptr readAntennaFieldAartfaac(const Table &table, const string &ant_type,
unsigned int id)
{
AntennaField::Ptr field;
AntennaField::CoordinateSystem system = readCoordinateSystemAartfaac(table, id);
if (ant_type == "LBA")
{
DualDipoleAntenna::Ptr model(new DualDipoleAntenna());
field = AntennaField::Ptr(new AntennaFieldLBA(ant_type, system, model));
}
else // HBA
{
// TODO: implement this
throw std::runtime_error("HBA for Aartfaac is not implemented yet.");
}
// Add only one antenna to the field (no offset, always enabled)
AntennaField::Antenna antenna;
antenna.position[0] = 0.;
antenna.position[1] = 0.;
antenna.position[2] = 0.;
antenna.enabled[0] = true;
antenna.enabled[1] = true;
field->addAntenna(antenna);
return field;
}
void readStationPhaseReference(const Table &table, unsigned int id,
const Station::Ptr &station)
{
const string columnName("LOFAR_PHASE_REFERENCE");
if(hasColumn(table, columnName))
{
ROScalarMeasColumn<MPosition> c_reference(table, columnName);
MPosition mReference = MPosition::Convert(c_reference(id),
MPosition::ITRF)();
MVPosition mvReference = mReference.getValue();
vector3r_t reference = {{mvReference(0), mvReference(1),
mvReference(2)}};
station->setPhaseReference(reference);
}
}
Station::Ptr readStation(const MeasurementSet &ms, unsigned int id)
{
ROMSAntennaColumns antenna(ms.antenna());
assert(antenna.nrow() > id && !antenna.flagRow()(id));
// Get station name.
const string name(antenna.name()(id));
// Get station position (ITRF).
MPosition mPosition = MPosition::Convert(antenna.positionMeas()(id),
MPosition::ITRF)();
MVPosition mvPosition = mPosition.getValue();
const vector3r_t position = {{mvPosition(0), mvPosition(1), mvPosition(2)}};
// Create station.
Station::Ptr station(new Station(name, position));
// Read phase reference position (if available).
readStationPhaseReference(ms.antenna(), id, station);
// Read antenna field information.
ROScalarColumn<String> telescope_name_col(getSubTable(ms, "OBSERVATION"),
"TELESCOPE_NAME");
string telescope_name = telescope_name_col(0);
if (telescope_name == "LOFAR")
{
Table tab_field = getSubTable(ms, "LOFAR_ANTENNA_FIELD");
tab_field = tab_field(tab_field.col("ANTENNA_ID") == static_cast<Int>(id));
for(size_t i = 0; i < tab_field.nrow(); ++i)
{
station->addField(readAntennaField(tab_field, i));
}
}
else if (telescope_name == "AARTFAAC")
{
ROScalarColumn<String> ant_type_col(getSubTable(ms, "OBSERVATION"),
"AARTFAAC_ANTENNA_TYPE");
string ant_type = ant_type_col(0);
Table tab_field = getSubTable(ms, "ANTENNA");
station -> addField(readAntennaFieldAartfaac(tab_field, ant_type, id));
}
return station;
}
MDirection readTileBeamDirection(const casacore::MeasurementSet &ms) {
MDirection tileBeamDir;
Table fieldTable = getSubTable(ms, "FIELD");
if (fieldTable.nrow() != 1) {
throw std::runtime_error("MS has multiple fields, this does not work with the LOFAR beam library.");
}
if (hasColumn(fieldTable, "LOFAR_TILE_BEAM_DIR"))
{
ROArrayMeasColumn<MDirection> tileBeamCol(fieldTable,
"LOFAR_TILE_BEAM_DIR");
tileBeamDir = *(tileBeamCol(0).data());
}
else
{
ROArrayMeasColumn<MDirection> tileBeamCol(fieldTable,
"DELAY_DIR");
tileBeamDir = *(tileBeamCol(0).data());
}
return tileBeamDir;
}
} //# namespace StationResponse
} //# namespace LOFAR