Line data Source code
1 : /*
2 : * DataRecord.h
3 : *
4 : * Created on: Jan 27, 2016
5 : * Author: nakazato
6 : */
7 :
8 : #ifndef SINGLEDISH_FILLER_DATARECORD_H_
9 : #define SINGLEDISH_FILLER_DATARECORD_H_
10 :
11 : #include <memory>
12 : #include <stdlib.h>
13 :
14 : #include <casacore/casa/BasicSL/String.h>
15 : #include <casacore/casa/Arrays/Matrix.h>
16 : #include <casacore/casa/Arrays/Vector.h>
17 : #include <casacore/measures/Measures/Stokes.h>
18 : #include <singledishfiller/Filler/FillerUtil.h>
19 :
20 : namespace {
21 : template<class T>
22 0 : void copyStorage(size_t n, T const *src, T *dst) {
23 0 : for (size_t i = n; i < n; ++i) {
24 0 : dst[i] = src[i];
25 : }
26 0 : }
27 :
28 : template<class T>
29 0 : void copyStorage(size_t n, size_t m, size_t stride, T const *src, T *dst) {
30 0 : for (size_t i = 0; i < m; ++i) {
31 0 : copyStorage(n, src + stride * m, dst + stride * m);
32 : }
33 0 : }
34 :
35 : }
36 :
37 : namespace casa { //# NAMESPACE CASA - BEGIN
38 : namespace sdfiller { //# NAMESPACE SDFILLER - BEGIN
39 :
40 : using casacore::SHARE;
41 :
42 : struct DataRecord {
43 7 : DataRecord() :
44 7 : block_size_(16384u), num_data_storage_(block_size_),
45 7 : num_tsys_storage_(block_size_), num_tcal_storage_(block_size_),
46 7 : data_shape_(1, 0), tsys_shape_(1, 0), tcal_shape_(1, 0),
47 7 : data_storage_(new casacore::Float[num_data_storage_]),
48 7 : flag_storage_(new casacore::Bool[num_data_storage_]),
49 7 : tsys_storage_(new casacore::Float[num_tsys_storage_]),
50 14 : tcal_storage_(new casacore::Float[num_tcal_storage_]), direction(2, 2, 0.0),
51 7 : direction_slice(casacore::IPosition(2, 2, 1), direction.data(), SHARE),
52 7 : direction_vector(direction.column(0)), scan_rate(direction.column(1)),
53 7 : data(data_shape_, data_storage_.get(), SHARE),
54 7 : flag(data_shape_, flag_storage_.get(), SHARE),
55 7 : tsys(tsys_shape_, tsys_storage_.get(), SHARE),
56 14 : tcal(tcal_shape_, tcal_storage_.get(), SHARE) {
57 : // std::cout << "DataRecord::DataRecord()" << std::endl;
58 7 : clear();
59 7 : }
60 :
61 7 : ~DataRecord() {
62 7 : }
63 :
64 : // method
65 7 : void clear() {
66 : // std::cout << "clear" << std::endl;
67 7 : time = -1.0;
68 7 : interval = -1.0;
69 7 : antenna_id = -1;
70 7 : field_id = -1;
71 7 : spw_id = -1;
72 7 : feed_id = -1;
73 7 : scan = -1;
74 7 : subscan = -1;
75 7 : pol = casacore::Stokes::Undefined;
76 7 : intent = "";
77 7 : pol_type = "";
78 7 : direction = 0.0;
79 7 : setDataSize(0);
80 7 : setTsysSize(0);
81 7 : setTcalSize(0);
82 7 : flag_row = true;
83 :
84 7 : temperature = 0.0f;
85 7 : pressure = 0.0f;
86 7 : rel_humidity = 0.0f;
87 7 : wind_speed = 0.0f;
88 7 : wind_direction = 0.0f;
89 7 : }
90 :
91 28165 : void setDataSize(size_t n) {
92 28165 : casacore::Bool redirect = false;
93 28165 : if (data_shape_[0] != (ssize_t) n) {
94 : // std::cout << "resize data to " << n << std::endl;
95 537 : data_shape_[0] = n;
96 537 : redirect = true;
97 : }
98 28165 : if (num_data_storage_ < n) {
99 0 : size_t new_num_storage = num_data_storage_ + block_size_;
100 0 : while (new_num_storage < n) {
101 0 : new_num_storage += block_size_;
102 : }
103 : // std::cout << "resize data storage to " << new_num_storage << std::endl;
104 0 : casacore::Float *new_data_storage = new casacore::Float[new_num_storage];
105 0 : copyStorage(data_shape_[0], data_storage_.get(), new_data_storage);
106 0 : data_storage_.reset(new_data_storage);
107 0 : casacore::Bool *new_flag_storage = new casacore::Bool[new_num_storage];
108 0 : copyStorage(data_shape_[0], flag_storage_.get(), new_flag_storage);
109 0 : flag_storage_.reset(new_flag_storage);
110 0 : num_data_storage_ = new_num_storage;
111 0 : redirect = true;
112 : }
113 28165 : if (redirect) {
114 537 : data.takeStorage(data_shape_, data_storage_.get(), SHARE);
115 537 : flag.takeStorage(data_shape_, flag_storage_.get(), SHARE);
116 : }
117 28165 : }
118 :
119 28165 : void setTsysSize(size_t n) {
120 28165 : casacore::Bool redirect = false;
121 28165 : if (tsys_shape_[0] != (ssize_t) n) {
122 : // std::cout << "resize tsys to " << n << std::endl;
123 207 : tsys_shape_[0] = n;
124 207 : redirect = true;
125 : }
126 28165 : if (num_tsys_storage_ < n) {
127 0 : size_t new_num_storage = num_tsys_storage_ + block_size_;
128 0 : while (new_num_storage < n) {
129 0 : new_num_storage += block_size_;
130 : }
131 : // std::cout << "resize tsys storage to " << new_num_storage << std::endl;
132 0 : casacore::Float *new_tsys_storage = new casacore::Float[new_num_storage];
133 0 : copyStorage(tsys_shape_[0], tsys_storage_.get(), new_tsys_storage);
134 0 : tsys_storage_.reset(new_tsys_storage);
135 0 : num_tsys_storage_ = new_num_storage;
136 0 : redirect = true;
137 : }
138 28165 : if (redirect) {
139 207 : tsys.takeStorage(tsys_shape_, tsys_storage_.get(), SHARE);
140 : }
141 28165 : }
142 :
143 997 : void setTcalSize(size_t n) {
144 997 : casacore::Bool redirect = false;
145 997 : if (tcal_shape_[0] != (ssize_t) n) {
146 : // std::cout << "resize tcal to " << n << std::endl;
147 535 : tcal_shape_[0] = n;
148 535 : redirect = true;
149 : }
150 997 : if (num_tcal_storage_ < n) {
151 0 : size_t new_num_storage = num_tcal_storage_ + block_size_;
152 0 : while (new_num_storage < n) {
153 0 : new_num_storage += block_size_;
154 : }
155 : // std::cout << "resize tcal storage to " << new_num_storage << std::endl;
156 0 : casacore::Float *new_tcal_storage = new casacore::Float[new_num_storage];
157 0 : copyStorage(tcal_shape_[0], tcal_storage_.get(), new_tcal_storage);
158 0 : tcal_storage_.reset(new_tcal_storage);
159 0 : num_tcal_storage_ = new_num_storage;
160 0 : redirect = true;
161 : }
162 997 : if (redirect) {
163 535 : tcal.takeStorage(tcal_shape_, tcal_storage_.get(), SHARE);
164 : }
165 997 : }
166 :
167 : DataRecord(DataRecord const &other) :
168 : DataRecord() {
169 : *this = other;
170 : }
171 : DataRecord &operator=(DataRecord const &other) {
172 : time = other.time;
173 : interval = other.interval;
174 : antenna_id = other.antenna_id;
175 : field_id = other.field_id;
176 : spw_id = other.spw_id;
177 : feed_id = other.feed_id;
178 : scan = other.scan;
179 : subscan = other.subscan;
180 : pol = other.pol;
181 : intent = other.intent;
182 : pol_type = other.pol_type;
183 : direction = other.direction;
184 : direction_slice.takeStorage(direction_slice.shape(), direction.data(),
185 : SHARE);
186 : direction_vector.reference(direction.column(0));
187 : scan_rate.reference(direction.column(1));
188 : flag_row = other.flag_row;
189 : setDataSize(other.data_shape_[0]);
190 : data = other.data;
191 : flag = other.flag;
192 : setTsysSize(other.tsys_shape_[0]);
193 : tsys = other.tsys;
194 : setTcalSize(other.tcal_shape_[0]);
195 : tcal = other.tcal;
196 :
197 : temperature = other.temperature;
198 : pressure = other.pressure;
199 : rel_humidity = other.rel_humidity;
200 : wind_speed = other.wind_speed;
201 : wind_direction = other.wind_direction;
202 :
203 : return *this;
204 : }
205 :
206 : private:
207 : size_t const block_size_;
208 : size_t num_data_storage_;
209 : size_t num_tsys_storage_;
210 : size_t num_tcal_storage_;
211 : casacore::IPosition data_shape_;
212 : casacore::IPosition tsys_shape_;
213 : casacore::IPosition tcal_shape_;
214 : std::unique_ptr<casacore::Float[]> data_storage_;
215 : std::unique_ptr<casacore::Bool[]> flag_storage_;
216 : std::unique_ptr<casacore::Float[]> tsys_storage_;
217 : std::unique_ptr<casacore::Float[]> tcal_storage_;
218 :
219 : public:
220 : // mandatory
221 : casacore::Double time;
222 : casacore::Double interval;
223 : casacore::Int antenna_id;
224 : casacore::Int field_id;
225 : casacore::Int spw_id;
226 : casacore::Int feed_id;
227 : casacore::Int scan;
228 : casacore::Int subscan;
229 : casacore::Stokes::StokesTypes pol;
230 : casacore::String intent;
231 : casacore::String pol_type;
232 : casacore::Matrix<casacore::Double> direction;
233 : casacore::Matrix<casacore::Double> direction_slice;
234 : casacore::Vector<casacore::Double> direction_vector;
235 : casacore::Vector<casacore::Double> scan_rate;
236 : casacore::Vector<casacore::Float> data;
237 : casacore::Vector<casacore::Bool> flag;
238 : casacore::Bool flag_row;
239 :
240 : // optional
241 : casacore::Vector<casacore::Float> tsys;
242 : casacore::Vector<casacore::Float> tcal;
243 :
244 : casacore::Float temperature;
245 : casacore::Float pressure;
246 : casacore::Float rel_humidity;
247 : casacore::Float wind_speed;
248 : casacore::Float wind_direction;
249 : };
250 :
251 : struct MSDataRecord {
252 7 : MSDataRecord() :
253 7 : block_size_(131072u),
254 7 : num_data_storage_(block_size_),
255 7 : num_tsys_storage_(block_size_),
256 7 : num_tcal_storage_(block_size_),
257 7 : data_shape_(2, 0, 0),
258 7 : tsys_shape_(2, 0, 0),
259 7 : tcal_shape_(2, 0, 0),
260 7 : corr_type_shape_(1, 0),
261 7 : corr_type_storage_(4),
262 7 : data_storage_(malloc(num_data_storage_ * sizeof(casacore::Complex))),
263 7 : flag_storage_(malloc(num_data_storage_ * sizeof(casacore::Bool))),
264 7 : tsys_storage_(malloc(num_tsys_storage_ * sizeof(casacore::Float))),
265 7 : tcal_storage_(malloc(num_tcal_storage_ * sizeof(casacore::Float))),
266 7 : sigma_storage_(new casacore::Float[4]),
267 7 : is_float_(false),
268 7 : corr_type(corr_type_shape_, corr_type_storage_.storage(), SHARE),
269 7 : direction(2, 2, 0.0),
270 7 : direction_slice(casacore::IPosition(2, 2, 1), direction.data(), SHARE),
271 7 : float_data(data_shape_, reinterpret_cast<casacore::Float *>(data_storage_.get()),
272 : SHARE),
273 7 : complex_data(data_shape_,
274 7 : reinterpret_cast<casacore::Complex *>(data_storage_.get()), SHARE),
275 7 : flag(data_shape_, reinterpret_cast<casacore::Bool *>(flag_storage_.get()), SHARE),
276 7 : sigma(corr_type_shape_, sigma_storage_.get(), SHARE), weight(sigma),
277 7 : tsys(tsys_shape_, reinterpret_cast<casacore::Float *>(tsys_storage_.get()), SHARE),
278 14 : tcal(tcal_shape_, reinterpret_cast<casacore::Float *>(tcal_storage_.get()), SHARE) {
279 7 : if (!data_storage_ || !flag_storage_ || !tsys_storage_ || !tcal_storage_) {
280 0 : throw casacore::AipsError("Failed to allocate memory.");
281 : }
282 35 : for (size_t i = 0; i < 4; ++i) {
283 28 : sigma_storage_[i] = 1.0f;
284 : }
285 7 : clear();
286 7 : }
287 :
288 7 : ~MSDataRecord() {
289 7 : }
290 :
291 : // method
292 15775 : void clear() {
293 15775 : time = -1.0;
294 15775 : interval = -1.0;
295 15775 : antenna_id = -1;
296 15775 : field_id = -1;
297 15775 : spw_id = -1;
298 15775 : feed_id = -1;
299 15775 : scan = -1;
300 15775 : subscan = -1;
301 15775 : intent = "";
302 15775 : pol_type = "";
303 15775 : num_pol = 0;
304 15775 : direction = 0.0;
305 15775 : setDataSize(0, 0);
306 15775 : setTsysSize(0, 0);
307 15775 : setTcalSize(0, 0);
308 15775 : flag_row = true;
309 15775 : is_float_ = false;
310 :
311 15775 : temperature = 0.0f;
312 15775 : pressure = 0.0f;
313 15775 : rel_humidity = 0.0f;
314 15775 : wind_speed = 0.0f;
315 15775 : wind_direction = 0.0f;
316 15775 : }
317 :
318 14174 : casacore::Bool isFloat() const {
319 14174 : return is_float_;
320 : }
321 :
322 14174 : void setFloat() {
323 14174 : is_float_ = true;
324 14174 : }
325 :
326 0 : void setComplex() {
327 0 : is_float_ = false;
328 0 : }
329 :
330 29949 : void setDataSize(size_t n, size_t m) {
331 29949 : casacore::Bool redirect = false;
332 29949 : if (data_shape_[0] != (ssize_t) n) {
333 : // std::cout << "resize data to " << n << std::endl;
334 4139 : data_shape_[0] = n;
335 4139 : redirect = true;
336 : }
337 29949 : if (data_shape_[1] != (ssize_t) m) {
338 4339 : data_shape_[1] = m;
339 4339 : redirect = true;
340 : }
341 29949 : if (num_data_storage_ < n * m) {
342 0 : size_t new_num_storage = num_data_storage_ + block_size_;
343 0 : while (new_num_storage < n * m) {
344 0 : new_num_storage += block_size_;
345 : }
346 : // std::cout << "resize data storage to " << new_num_storage << std::endl;
347 0 : void *new_data_storage = malloc(new_num_storage * sizeof(casacore::Complex));
348 0 : if (!new_data_storage) {
349 0 : throw casacore::AipsError("Failed to allocate memory.");
350 : }
351 0 : if (is_float_) {
352 0 : copyStorage(data_shape_[0], data_shape_[1], 4,
353 0 : reinterpret_cast<casacore::Float *>(data_storage_.get()),
354 : reinterpret_cast<casacore::Float *>(new_data_storage));
355 : } else {
356 0 : copyStorage(data_shape_[0], data_shape_[1], 4,
357 0 : reinterpret_cast<casacore::Complex *>(data_storage_.get()),
358 : reinterpret_cast<casacore::Complex *>(new_data_storage));
359 : }
360 0 : data_storage_.reset(new_data_storage);
361 0 : void *new_flag_storage = malloc(new_num_storage * sizeof(casacore::Bool));
362 0 : if (!new_flag_storage) {
363 0 : throw casacore::AipsError("Failed to allocate memory.");
364 : }
365 0 : copyStorage(data_shape_[0], data_shape_[1], 4,
366 0 : reinterpret_cast<casacore::Bool *>(flag_storage_.get()),
367 : reinterpret_cast<casacore::Bool *>(new_flag_storage));
368 0 : flag_storage_.reset(new_flag_storage);
369 0 : num_data_storage_ = new_num_storage;
370 0 : redirect = true;
371 : }
372 29949 : if (redirect) {
373 4339 : corr_type_shape_[0] = data_shape_[0];
374 4339 : corr_type.takeStorage(corr_type_shape_, corr_type_storage_.storage(),
375 : SHARE);
376 4339 : sigma.takeStorage(corr_type_shape_, sigma_storage_.get(), SHARE);
377 4339 : float_data.takeStorage(data_shape_,
378 4339 : reinterpret_cast<casacore::Float *>(data_storage_.get()), SHARE);
379 4339 : complex_data.takeStorage(data_shape_,
380 4339 : reinterpret_cast<casacore::Complex *>(data_storage_.get()), SHARE);
381 4339 : flag.takeStorage(data_shape_,
382 4339 : reinterpret_cast<casacore::Bool *>(flag_storage_.get()), SHARE);
383 : }
384 29949 : }
385 :
386 40525 : void setTsysSize(size_t n, size_t m) {
387 40525 : casacore::Bool redirect = false;
388 40525 : if (tsys_shape_[0] != (ssize_t) n) {
389 : // std::cout << "resize tsys to " << n << std::endl;
390 21507 : tsys_shape_[0] = n;
391 21507 : redirect = true;
392 : }
393 40525 : if (tsys_shape_[1] != (ssize_t) m) {
394 : // std::cout << "resize tsys to " << n << std::endl;
395 21482 : tsys_shape_[1] = m;
396 21482 : redirect = true;
397 : }
398 40525 : if (num_tsys_storage_ < n * m) {
399 0 : size_t new_num_storage = num_tsys_storage_ + block_size_;
400 0 : while (new_num_storage < n * m) {
401 0 : new_num_storage += block_size_;
402 : }
403 : // std::cout << "resize tsys storage to " << new_num_storage << std::endl;
404 0 : void *new_tsys_storage = malloc(new_num_storage * sizeof(casacore::Float));
405 0 : copyStorage(tsys_shape_[0], tsys_shape_[1], 4,
406 0 : reinterpret_cast<casacore::Float *>(tsys_storage_.get()),
407 : reinterpret_cast<casacore::Float *>(new_tsys_storage));
408 0 : tsys_storage_.reset(new_tsys_storage);
409 0 : num_tsys_storage_ = new_num_storage;
410 0 : redirect = true;
411 : }
412 40525 : if (redirect) {
413 21507 : tsys.takeStorage(tsys_shape_,
414 21507 : reinterpret_cast<casacore::Float *>(tsys_storage_.get()), SHARE);
415 : }
416 40525 : }
417 :
418 30349 : void setTcalSize(size_t n, size_t m) {
419 30349 : casacore::Bool redirect = false;
420 30349 : if (tcal_shape_[0] != (ssize_t) n) {
421 : // std::cout << "resize tcal to " << n << std::endl;
422 1155 : tcal_shape_[0] = n;
423 1155 : redirect = true;
424 : }
425 30349 : if (tcal_shape_[1] != (ssize_t) m) {
426 : // std::cout << "resize tcal to " << n << std::endl;
427 1155 : tcal_shape_[1] = m;
428 1155 : redirect = true;
429 : }
430 30349 : if (num_tcal_storage_ < n * m) {
431 0 : size_t new_num_storage = num_tcal_storage_ + block_size_;
432 0 : while (new_num_storage < n * m) {
433 0 : new_num_storage += block_size_;
434 : }
435 : // std::cout << "resize tcal storage to " << new_num_storage << std::endl;
436 0 : void *new_tcal_storage = malloc(new_num_storage * sizeof(casacore::Float));
437 0 : copyStorage(tcal_shape_[0], tcal_shape_[1], 4,
438 0 : reinterpret_cast<casacore::Float *>(tcal_storage_.get()),
439 : reinterpret_cast<casacore::Float *>(new_tcal_storage));
440 0 : tcal_storage_.reset(new_tcal_storage);
441 0 : num_tcal_storage_ = new_num_storage;
442 0 : redirect = true;
443 : }
444 30349 : if (redirect) {
445 1155 : tcal.takeStorage(tcal_shape_,
446 1155 : reinterpret_cast<casacore::Float *>(tcal_storage_.get()), SHARE);
447 : }
448 30349 : }
449 :
450 : MSDataRecord(MSDataRecord const &other) :
451 : MSDataRecord() {
452 : *this = other;
453 : }
454 : MSDataRecord &operator=(MSDataRecord const &other) {
455 : time = other.time;
456 : interval = other.interval;
457 : antenna_id = other.antenna_id;
458 : field_id = other.field_id;
459 : spw_id = other.spw_id;
460 : feed_id = other.feed_id;
461 : scan = other.scan;
462 : subscan = other.subscan;
463 : intent = other.intent;
464 : pol_type = other.pol_type;
465 : num_pol = other.num_pol;
466 : direction = other.direction;
467 : direction_slice.takeStorage(direction_slice.shape(), direction.data(),
468 : SHARE);
469 : flag_row = other.flag_row;
470 : setDataSize(other.data_shape_[0], other.data_shape_[1]);
471 : corr_type = other.corr_type;
472 : if (other.is_float_) {
473 : float_data = other.float_data;
474 : } else {
475 : complex_data = other.complex_data;
476 : }
477 : flag = other.flag;
478 : setTsysSize(other.tsys_shape_[0], other.tsys_shape_[1]);
479 : tsys = other.tsys;
480 : setTcalSize(other.tcal_shape_[0], other.tcal_shape_[1]);
481 : tcal = other.tcal;
482 :
483 : temperature = other.temperature;
484 : pressure = other.pressure;
485 : rel_humidity = other.rel_humidity;
486 : wind_speed = other.wind_speed;
487 : wind_direction = other.wind_direction;
488 :
489 : return *this;
490 : }
491 :
492 : private:
493 : size_t const block_size_;
494 : size_t num_data_storage_;
495 : size_t num_tsys_storage_;
496 : size_t num_tcal_storage_;
497 : casacore::IPosition data_shape_;
498 : casacore::IPosition tsys_shape_;
499 : casacore::IPosition tcal_shape_;
500 : casacore::IPosition corr_type_shape_;
501 : casacore::Block<casacore::Int> corr_type_storage_;
502 : std::unique_ptr<void, sdfiller::Deleter> data_storage_;
503 : std::unique_ptr<void, sdfiller::Deleter> flag_storage_;
504 : std::unique_ptr<void, sdfiller::Deleter> tsys_storage_;
505 : std::unique_ptr<void, sdfiller::Deleter> tcal_storage_;
506 : std::unique_ptr<casacore::Float[]> sigma_storage_;
507 : casacore::Bool is_float_;
508 :
509 : public:
510 : // mandatory
511 : casacore::Double time;
512 : casacore::Double interval;
513 : casacore::Int antenna_id;
514 : casacore::Int field_id;
515 : casacore::Int spw_id;
516 : casacore::Int feed_id;
517 : casacore::Int scan;
518 : casacore::Int subscan;
519 : casacore::Int num_pol;
520 : casacore::String intent;
521 : casacore::String pol_type;
522 : casacore::Vector<casacore::Int> corr_type;
523 : casacore::Matrix<casacore::Double> direction;
524 : casacore::Matrix<casacore::Double> direction_slice;
525 : casacore::Matrix<casacore::Float> float_data;
526 : casacore::Matrix<casacore::Complex> complex_data;
527 : casacore::Matrix<casacore::Bool> flag;
528 : casacore::Bool flag_row;
529 : casacore::Vector<casacore::Float> sigma;
530 : casacore::Vector<casacore::Float> &weight;
531 :
532 : // optional
533 : casacore::Matrix<casacore::Float> tsys;
534 : casacore::Matrix<casacore::Float> tcal;
535 :
536 : casacore::Float temperature;
537 : casacore::Float pressure;
538 : casacore::Float rel_humidity;
539 : casacore::Float wind_speed;
540 : casacore::Float wind_direction;
541 :
542 : };
543 :
544 : } //# NAMESPACE SDFILLER - END
545 : } //# NAMESPACE CASA - END
546 :
547 : #endif /* SINGLEDISH_FILLER_DATARECORD_H_ */
|