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 0 : DataRecord() :
44 0 : block_size_(16384u), num_data_storage_(block_size_),
45 0 : num_tsys_storage_(block_size_), num_tcal_storage_(block_size_),
46 0 : data_shape_(1, 0), tsys_shape_(1, 0), tcal_shape_(1, 0),
47 0 : data_storage_(new casacore::Float[num_data_storage_]),
48 0 : flag_storage_(new casacore::Bool[num_data_storage_]),
49 0 : tsys_storage_(new casacore::Float[num_tsys_storage_]),
50 0 : tcal_storage_(new casacore::Float[num_tcal_storage_]), direction(2, 2, 0.0),
51 0 : direction_slice(casacore::IPosition(2, 2, 1), direction.data(), SHARE),
52 0 : direction_vector(direction.column(0)), scan_rate(direction.column(1)),
53 0 : data(data_shape_, data_storage_.get(), SHARE),
54 0 : flag(data_shape_, flag_storage_.get(), SHARE),
55 0 : tsys(tsys_shape_, tsys_storage_.get(), SHARE),
56 0 : tcal(tcal_shape_, tcal_storage_.get(), SHARE) {
57 : // std::cout << "DataRecord::DataRecord()" << std::endl;
58 0 : clear();
59 0 : }
60 :
61 0 : ~DataRecord() {
62 0 : }
63 :
64 : // method
65 0 : void clear() {
66 : // std::cout << "clear" << std::endl;
67 0 : time = -1.0;
68 0 : interval = -1.0;
69 0 : antenna_id = -1;
70 0 : field_id = -1;
71 0 : spw_id = -1;
72 0 : feed_id = -1;
73 0 : scan = -1;
74 0 : subscan = -1;
75 0 : pol = casacore::Stokes::Undefined;
76 0 : intent = "";
77 0 : pol_type = "";
78 0 : direction = 0.0;
79 0 : setDataSize(0);
80 0 : setTsysSize(0);
81 0 : setTcalSize(0);
82 0 : flag_row = true;
83 :
84 0 : temperature = 0.0f;
85 0 : pressure = 0.0f;
86 0 : rel_humidity = 0.0f;
87 0 : wind_speed = 0.0f;
88 0 : wind_direction = 0.0f;
89 0 : }
90 :
91 0 : void setDataSize(size_t n) {
92 0 : casacore::Bool redirect = false;
93 0 : if (data_shape_[0] != (ssize_t) n) {
94 : // std::cout << "resize data to " << n << std::endl;
95 0 : data_shape_[0] = n;
96 0 : redirect = true;
97 : }
98 0 : 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 0 : if (redirect) {
114 0 : data.takeStorage(data_shape_, data_storage_.get(), SHARE);
115 0 : flag.takeStorage(data_shape_, flag_storage_.get(), SHARE);
116 : }
117 0 : }
118 :
119 0 : void setTsysSize(size_t n) {
120 0 : casacore::Bool redirect = false;
121 0 : if (tsys_shape_[0] != (ssize_t) n) {
122 : // std::cout << "resize tsys to " << n << std::endl;
123 0 : tsys_shape_[0] = n;
124 0 : redirect = true;
125 : }
126 0 : 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 0 : if (redirect) {
139 0 : tsys.takeStorage(tsys_shape_, tsys_storage_.get(), SHARE);
140 : }
141 0 : }
142 :
143 0 : void setTcalSize(size_t n) {
144 0 : casacore::Bool redirect = false;
145 0 : if (tcal_shape_[0] != (ssize_t) n) {
146 : // std::cout << "resize tcal to " << n << std::endl;
147 0 : tcal_shape_[0] = n;
148 0 : redirect = true;
149 : }
150 0 : 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 0 : if (redirect) {
163 0 : tcal.takeStorage(tcal_shape_, tcal_storage_.get(), SHARE);
164 : }
165 0 : }
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 0 : MSDataRecord() :
253 0 : block_size_(131072u),
254 0 : num_data_storage_(block_size_),
255 0 : num_tsys_storage_(block_size_),
256 0 : num_tcal_storage_(block_size_),
257 0 : data_shape_(2, 0, 0),
258 0 : tsys_shape_(2, 0, 0),
259 0 : tcal_shape_(2, 0, 0),
260 0 : corr_type_shape_(1, 0),
261 0 : corr_type_storage_(4),
262 0 : data_storage_(malloc(num_data_storage_ * sizeof(casacore::Complex))),
263 0 : flag_storage_(malloc(num_data_storage_ * sizeof(casacore::Bool))),
264 0 : tsys_storage_(malloc(num_tsys_storage_ * sizeof(casacore::Float))),
265 0 : tcal_storage_(malloc(num_tcal_storage_ * sizeof(casacore::Float))),
266 0 : sigma_storage_(new casacore::Float[4]),
267 0 : is_float_(false),
268 0 : corr_type(corr_type_shape_, corr_type_storage_.storage(), SHARE),
269 0 : direction(2, 2, 0.0),
270 0 : direction_slice(casacore::IPosition(2, 2, 1), direction.data(), SHARE),
271 0 : float_data(data_shape_, reinterpret_cast<casacore::Float *>(data_storage_.get()),
272 : SHARE),
273 0 : complex_data(data_shape_,
274 0 : reinterpret_cast<casacore::Complex *>(data_storage_.get()), SHARE),
275 0 : flag(data_shape_, reinterpret_cast<casacore::Bool *>(flag_storage_.get()), SHARE),
276 0 : sigma(corr_type_shape_, sigma_storage_.get(), SHARE), weight(sigma),
277 0 : tsys(tsys_shape_, reinterpret_cast<casacore::Float *>(tsys_storage_.get()), SHARE),
278 0 : tcal(tcal_shape_, reinterpret_cast<casacore::Float *>(tcal_storage_.get()), SHARE) {
279 0 : if (!data_storage_ || !flag_storage_ || !tsys_storage_ || !tcal_storage_) {
280 0 : throw casacore::AipsError("Failed to allocate memory.");
281 : }
282 0 : for (size_t i = 0; i < 4; ++i) {
283 0 : sigma_storage_[i] = 1.0f;
284 : }
285 0 : clear();
286 0 : }
287 :
288 0 : ~MSDataRecord() {
289 0 : }
290 :
291 : // method
292 0 : void clear() {
293 0 : time = -1.0;
294 0 : interval = -1.0;
295 0 : antenna_id = -1;
296 0 : field_id = -1;
297 0 : spw_id = -1;
298 0 : feed_id = -1;
299 0 : scan = -1;
300 0 : subscan = -1;
301 0 : intent = "";
302 0 : pol_type = "";
303 0 : num_pol = 0;
304 0 : direction = 0.0;
305 0 : setDataSize(0, 0);
306 0 : setTsysSize(0, 0);
307 0 : setTcalSize(0, 0);
308 0 : flag_row = true;
309 0 : is_float_ = false;
310 :
311 0 : temperature = 0.0f;
312 0 : pressure = 0.0f;
313 0 : rel_humidity = 0.0f;
314 0 : wind_speed = 0.0f;
315 0 : wind_direction = 0.0f;
316 0 : }
317 :
318 0 : casacore::Bool isFloat() const {
319 0 : return is_float_;
320 : }
321 :
322 0 : void setFloat() {
323 0 : is_float_ = true;
324 0 : }
325 :
326 0 : void setComplex() {
327 0 : is_float_ = false;
328 0 : }
329 :
330 0 : void setDataSize(size_t n, size_t m) {
331 0 : casacore::Bool redirect = false;
332 0 : if (data_shape_[0] != (ssize_t) n) {
333 : // std::cout << "resize data to " << n << std::endl;
334 0 : data_shape_[0] = n;
335 0 : redirect = true;
336 : }
337 0 : if (data_shape_[1] != (ssize_t) m) {
338 0 : data_shape_[1] = m;
339 0 : redirect = true;
340 : }
341 0 : 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 0 : if (redirect) {
373 0 : corr_type_shape_[0] = data_shape_[0];
374 0 : corr_type.takeStorage(corr_type_shape_, corr_type_storage_.storage(),
375 : SHARE);
376 0 : sigma.takeStorage(corr_type_shape_, sigma_storage_.get(), SHARE);
377 0 : float_data.takeStorage(data_shape_,
378 0 : reinterpret_cast<casacore::Float *>(data_storage_.get()), SHARE);
379 0 : complex_data.takeStorage(data_shape_,
380 0 : reinterpret_cast<casacore::Complex *>(data_storage_.get()), SHARE);
381 0 : flag.takeStorage(data_shape_,
382 0 : reinterpret_cast<casacore::Bool *>(flag_storage_.get()), SHARE);
383 : }
384 0 : }
385 :
386 0 : void setTsysSize(size_t n, size_t m) {
387 0 : casacore::Bool redirect = false;
388 0 : if (tsys_shape_[0] != (ssize_t) n) {
389 : // std::cout << "resize tsys to " << n << std::endl;
390 0 : tsys_shape_[0] = n;
391 0 : redirect = true;
392 : }
393 0 : if (tsys_shape_[1] != (ssize_t) m) {
394 : // std::cout << "resize tsys to " << n << std::endl;
395 0 : tsys_shape_[1] = m;
396 0 : redirect = true;
397 : }
398 0 : 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 0 : if (redirect) {
413 0 : tsys.takeStorage(tsys_shape_,
414 0 : reinterpret_cast<casacore::Float *>(tsys_storage_.get()), SHARE);
415 : }
416 0 : }
417 :
418 0 : void setTcalSize(size_t n, size_t m) {
419 0 : casacore::Bool redirect = false;
420 0 : if (tcal_shape_[0] != (ssize_t) n) {
421 : // std::cout << "resize tcal to " << n << std::endl;
422 0 : tcal_shape_[0] = n;
423 0 : redirect = true;
424 : }
425 0 : if (tcal_shape_[1] != (ssize_t) m) {
426 : // std::cout << "resize tcal to " << n << std::endl;
427 0 : tcal_shape_[1] = m;
428 0 : redirect = true;
429 : }
430 0 : 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 0 : if (redirect) {
445 0 : tcal.takeStorage(tcal_shape_,
446 0 : reinterpret_cast<casacore::Float *>(tcal_storage_.get()), SHARE);
447 : }
448 0 : }
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_ */
|