DataRecord.h

Go to the documentation of this file.
00001 /*
00002  * DataRecord.h
00003  *
00004  *  Created on: Jan 27, 2016
00005  *      Author: nakazato
00006  */
00007 
00008 #ifndef SINGLEDISH_FILLER_DATARECORD_H_
00009 #define SINGLEDISH_FILLER_DATARECORD_H_
00010 
00011 #include <singledish/Filler/FillerUtil.h>
00012 
00013 #include <memory>
00014 #include <stdlib.h>
00015 
00016 #include <casacore/casa/BasicSL/String.h>
00017 #include <casacore/casa/Arrays/Matrix.h>
00018 #include <casacore/casa/Arrays/Vector.h>
00019 
00020 namespace {
00021 template<class T>
00022 void copyStorage(size_t n, T const *src, T *dst) {
00023   for (size_t i = n; i < n; ++i) {
00024     dst[i] = src[i];
00025   }
00026 }
00027 
00028 template<class T>
00029 void copyStorage(size_t n, size_t m, size_t stride, T const *src, T *dst) {
00030   for (size_t i = 0; i < m; ++i) {
00031     copyStorage(n, src + stride * m, dst + stride * m);
00032   }
00033 }
00034 
00035 }
00036 
00037 namespace casa { //# NAMESPACE CASA - BEGIN
00038 namespace sdfiller { //# NAMESPACE SDFILLER - BEGIN
00039 
00040 struct DataRecord {
00041   DataRecord() :
00042       block_size_(16384u), num_data_storage_(block_size_),
00043       num_tsys_storage_(block_size_), num_tcal_storage_(block_size_),
00044       data_shape_(1, 0), tsys_shape_(1, 0), tcal_shape_(1, 0),
00045       data_storage_(new Float[num_data_storage_]),
00046       flag_storage_(new Bool[num_data_storage_]),
00047       tsys_storage_(new Float[num_tsys_storage_]),
00048       tcal_storage_(new Float[num_tcal_storage_]), direction(2, 2, 0.0),
00049       direction_slice(IPosition(2, 2, 1), direction.data(), SHARE),
00050       direction_vector(direction.column(0)), scan_rate(direction.column(1)),
00051       data(data_shape_, data_storage_.get(), SHARE),
00052       flag(data_shape_, flag_storage_.get(), SHARE),
00053       tsys(tsys_shape_, tsys_storage_.get(), SHARE),
00054       tcal(tcal_shape_, tcal_storage_.get(), SHARE) {
00055 //    std::cout << "DataRecord::DataRecord()" << std::endl;
00056     clear();
00057   }
00058 
00059   ~DataRecord() {
00060   }
00061 
00062   // method
00063   void clear() {
00064 //    std::cout << "clear" << std::endl;
00065     time = -1.0;
00066     interval = -1.0;
00067     antenna_id = -1;
00068     field_id = -1;
00069     spw_id = -1;
00070     feed_id = -1;
00071     scan = -1;
00072     subscan = -1;
00073     polno = 0;
00074     intent = "";
00075     pol_type = "";
00076     direction = 0.0;
00077     setDataSize(0);
00078     setTsysSize(0);
00079     setTcalSize(0);
00080     flag_row = True;
00081 
00082     temperature = 0.0f;
00083     pressure = 0.0f;
00084     rel_humidity = 0.0f;
00085     wind_speed = 0.0f;
00086     wind_direction = 0.0f;
00087   }
00088 
00089   void setDataSize(size_t n) {
00090     Bool redirect = False;
00091     if (data_shape_[0] != (ssize_t) n) {
00092 //      std::cout << "resize data to " << n << std::endl;
00093       data_shape_[0] = n;
00094       redirect = True;
00095     }
00096     if (num_data_storage_ < n) {
00097       size_t new_num_storage = num_data_storage_ + block_size_;
00098       while (new_num_storage < n) {
00099         new_num_storage += block_size_;
00100       }
00101 //      std::cout << "resize data storage to " << new_num_storage << std::endl;
00102       Float *new_data_storage = new Float[new_num_storage];
00103       copyStorage(data_shape_[0], data_storage_.get(), new_data_storage);
00104       data_storage_.reset(new_data_storage);
00105       Bool *new_flag_storage = new Bool[new_num_storage];
00106       copyStorage(data_shape_[0], flag_storage_.get(), new_flag_storage);
00107       flag_storage_.reset(new_flag_storage);
00108       num_data_storage_ = new_num_storage;
00109       redirect = True;
00110     }
00111     if (redirect) {
00112       data.takeStorage(data_shape_, data_storage_.get(), SHARE);
00113       flag.takeStorage(data_shape_, flag_storage_.get(), SHARE);
00114     }
00115   }
00116 
00117   void setTsysSize(size_t n) {
00118     Bool redirect = False;
00119     if (tsys_shape_[0] != (ssize_t) n) {
00120 //      std::cout << "resize tsys to " << n << std::endl;
00121       tsys_shape_[0] = n;
00122       redirect = True;
00123     }
00124     if (num_tsys_storage_ < n) {
00125       size_t new_num_storage = num_tsys_storage_ + block_size_;
00126       while (new_num_storage < n) {
00127         new_num_storage += block_size_;
00128       }
00129 //      std::cout << "resize tsys storage to " << new_num_storage << std::endl;
00130       Float *new_tsys_storage = new Float[new_num_storage];
00131       copyStorage(tsys_shape_[0], tsys_storage_.get(), new_tsys_storage);
00132       tsys_storage_.reset(new_tsys_storage);
00133       num_tsys_storage_ = new_num_storage;
00134       redirect = True;
00135     }
00136     if (redirect) {
00137       tsys.takeStorage(tsys_shape_, tsys_storage_.get(), SHARE);
00138     }
00139   }
00140 
00141   void setTcalSize(size_t n) {
00142     Bool redirect = False;
00143     if (tcal_shape_[0] != (ssize_t) n) {
00144 //      std::cout << "resize tcal to " << n << std::endl;
00145       tcal_shape_[0] = n;
00146       redirect = True;
00147     }
00148     if (num_tcal_storage_ < n) {
00149       size_t new_num_storage = num_tcal_storage_ + block_size_;
00150       while (new_num_storage < n) {
00151         new_num_storage += block_size_;
00152       }
00153 //      std::cout << "resize tcal storage to " << new_num_storage << std::endl;
00154       Float *new_tcal_storage = new Float[new_num_storage];
00155       copyStorage(tcal_shape_[0], tcal_storage_.get(), new_tcal_storage);
00156       tcal_storage_.reset(new_tcal_storage);
00157       num_tcal_storage_ = new_num_storage;
00158       redirect = True;
00159     }
00160     if (redirect) {
00161       tcal.takeStorage(tcal_shape_, tcal_storage_.get(), SHARE);
00162     }
00163   }
00164 
00165   DataRecord(DataRecord const &other) :
00166       DataRecord() {
00167     *this = other;
00168   }
00169   DataRecord &operator=(DataRecord const &other) {
00170     time = other.time;
00171     interval = other.interval;
00172     antenna_id = other.antenna_id;
00173     field_id = other.field_id;
00174     spw_id = other.spw_id;
00175     feed_id = other.feed_id;
00176     scan = other.scan;
00177     subscan = other.subscan;
00178     polno = other.polno;
00179     intent = other.intent;
00180     pol_type = other.pol_type;
00181     direction = other.direction;
00182     direction_slice.takeStorage(direction_slice.shape(), direction.data(),
00183         SHARE);
00184     direction_vector.reference(direction.column(0));
00185     scan_rate.reference(direction.column(1));
00186     flag_row = other.flag_row;
00187     setDataSize(other.data_shape_[0]);
00188     data = other.data;
00189     flag = other.flag;
00190     setTsysSize(other.tsys_shape_[0]);
00191     tsys = other.tsys;
00192     setTcalSize(other.tcal_shape_[0]);
00193     tcal = other.tcal;
00194 
00195     temperature = other.temperature;
00196     pressure = other.pressure;
00197     rel_humidity = other.rel_humidity;
00198     wind_speed = other.wind_speed;
00199     wind_direction = other.wind_direction;
00200 
00201     return *this;
00202   }
00203 
00204 private:
00205   size_t const block_size_;
00206   size_t num_data_storage_;
00207   size_t num_tsys_storage_;
00208   size_t num_tcal_storage_;
00209   IPosition data_shape_;
00210   IPosition tsys_shape_;
00211   IPosition tcal_shape_;
00212   std::unique_ptr<Float[]> data_storage_;
00213   std::unique_ptr<Bool[]> flag_storage_;
00214   std::unique_ptr<Float[]> tsys_storage_;
00215   std::unique_ptr<Float[]> tcal_storage_;
00216 
00217 public:
00218   // mandatory
00219   Double time;
00220   Double interval;
00221   Int antenna_id;
00222   Int field_id;
00223   Int spw_id;
00224   Int feed_id;
00225   Int scan;
00226   Int subscan;
00227   uInt polno;
00228   String intent;
00229   String pol_type;
00230   Matrix<Double> direction;
00231   Matrix<Double> direction_slice;
00232   Vector<Double> direction_vector;
00233   Vector<Double> scan_rate;
00234   Vector<Float> data;
00235   Vector<Bool> flag;
00236   Bool flag_row;
00237 
00238   // optional
00239   Vector<Float> tsys;
00240   Vector<Float> tcal;
00241 
00242   Float temperature;
00243   Float pressure;
00244   Float rel_humidity;
00245   Float wind_speed;
00246   Float wind_direction;
00247 };
00248 
00249 struct MSDataRecord {
00250   MSDataRecord() :
00251       block_size_(131072u),
00252       num_data_storage_(block_size_),
00253       num_tsys_storage_(block_size_),
00254       num_tcal_storage_(block_size_),
00255       data_shape_(2, 0, 0),
00256       tsys_shape_(2, 0, 0),
00257       tcal_shape_(2, 0, 0),
00258       corr_type_shape_(1, 0),
00259       corr_type_storage_(4),
00260       data_storage_(malloc(num_data_storage_ * sizeof(Complex))),
00261       flag_storage_(malloc(num_data_storage_ * sizeof(Bool))),
00262       tsys_storage_(malloc(num_tsys_storage_ * sizeof(Float))),
00263       tcal_storage_(malloc(num_tcal_storage_ * sizeof(Float))),
00264       sigma_storage_(new Float[4]),
00265       is_float_(False),
00266       corr_type(corr_type_shape_, corr_type_storage_.storage(), SHARE),
00267       direction(2, 2, 0.0),
00268       direction_slice(IPosition(2, 2, 1), direction.data(), SHARE),
00269       float_data(data_shape_, reinterpret_cast<Float *>(data_storage_.get()),
00270           SHARE),
00271       complex_data(data_shape_,
00272           reinterpret_cast<Complex *>(data_storage_.get()), SHARE),
00273       flag(data_shape_, reinterpret_cast<Bool *>(flag_storage_.get()), SHARE),
00274       sigma(corr_type_shape_, sigma_storage_.get(), SHARE), weight(sigma),
00275       tsys(tsys_shape_, reinterpret_cast<Float *>(tsys_storage_.get()), SHARE),
00276       tcal(tcal_shape_, reinterpret_cast<Float *>(tcal_storage_.get()), SHARE) {
00277     if (!data_storage_ || !flag_storage_ || !tsys_storage_ || !tcal_storage_) {
00278       throw AipsError("Failed to allocate memory.");
00279     }
00280     for (size_t i = 0; i < 4; ++i) {
00281       sigma_storage_[i] = 1.0f;
00282     }
00283     clear();
00284   }
00285 
00286   ~MSDataRecord() {
00287   }
00288 
00289   // method
00290   void clear() {
00291     time = -1.0;
00292     interval = -1.0;
00293     antenna_id = -1;
00294     field_id = -1;
00295     spw_id = -1;
00296     feed_id = -1;
00297     scan = -1;
00298     subscan = -1;
00299     intent = "";
00300     pol_type = "";
00301     num_pol = 0;
00302     direction = 0.0;
00303     setDataSize(0, 0);
00304     setTsysSize(0, 0);
00305     setTcalSize(0, 0);
00306     flag_row = True;
00307     is_float_ = False;
00308 
00309     temperature = 0.0f;
00310     pressure = 0.0f;
00311     rel_humidity = 0.0f;
00312     wind_speed = 0.0f;
00313     wind_direction = 0.0f;
00314   }
00315 
00316   Bool isFloat() const {
00317     return is_float_;
00318   }
00319 
00320   void setFloat() {
00321     is_float_ = True;
00322   }
00323 
00324   void setComplex() {
00325     is_float_ = False;
00326   }
00327 
00328   void setDataSize(size_t n, size_t m) {
00329     Bool redirect = False;
00330     if (data_shape_[0] != (ssize_t) n) {
00331 //      std::cout << "resize data to " << n << std::endl;
00332       data_shape_[0] = n;
00333       redirect = True;
00334     }
00335     if (data_shape_[1] != (ssize_t) m) {
00336       data_shape_[1] = m;
00337       redirect = True;
00338     }
00339     if (num_data_storage_ < n * m) {
00340       size_t new_num_storage = num_data_storage_ + block_size_;
00341       while (new_num_storage < n * m) {
00342         new_num_storage += block_size_;
00343       }
00344 //      std::cout << "resize data storage to " << new_num_storage << std::endl;
00345       void *new_data_storage = malloc(new_num_storage * sizeof(Complex));
00346       if (!new_data_storage) {
00347         throw AipsError("Failed to allocate memory.");
00348       }
00349       if (is_float_) {
00350         copyStorage(data_shape_[0], data_shape_[1], 4,
00351             reinterpret_cast<Float *>(data_storage_.get()),
00352             reinterpret_cast<Float *>(new_data_storage));
00353       } else {
00354         copyStorage(data_shape_[0], data_shape_[1], 4,
00355             reinterpret_cast<Complex *>(data_storage_.get()),
00356             reinterpret_cast<Complex *>(new_data_storage));
00357       }
00358       data_storage_.reset(new_data_storage);
00359       void *new_flag_storage = malloc(new_num_storage * sizeof(Bool));
00360       if (!new_flag_storage) {
00361         throw AipsError("Failed to allocate memory.");
00362       }
00363       copyStorage(data_shape_[0], data_shape_[1], 4,
00364           reinterpret_cast<Bool *>(flag_storage_.get()),
00365           reinterpret_cast<Bool *>(new_flag_storage));
00366       flag_storage_.reset(new_flag_storage);
00367       num_data_storage_ = new_num_storage;
00368       redirect = True;
00369     }
00370     if (redirect) {
00371       corr_type_shape_[0] = data_shape_[0];
00372       corr_type.takeStorage(corr_type_shape_, corr_type_storage_.storage(),
00373           SHARE);
00374       sigma.takeStorage(corr_type_shape_, sigma_storage_.get(), SHARE);
00375       float_data.takeStorage(data_shape_,
00376           reinterpret_cast<Float *>(data_storage_.get()), SHARE);
00377       complex_data.takeStorage(data_shape_,
00378           reinterpret_cast<Complex *>(data_storage_.get()), SHARE);
00379       flag.takeStorage(data_shape_,
00380           reinterpret_cast<Bool *>(flag_storage_.get()), SHARE);
00381     }
00382   }
00383 
00384   void setTsysSize(size_t n, size_t m) {
00385     Bool redirect = False;
00386     if (tsys_shape_[0] != (ssize_t) n) {
00387 //      std::cout << "resize tsys to " << n << std::endl;
00388       tsys_shape_[0] = n;
00389       redirect = True;
00390     }
00391     if (tsys_shape_[1] != (ssize_t) m) {
00392 //      std::cout << "resize tsys to " << n << std::endl;
00393       tsys_shape_[1] = m;
00394       redirect = True;
00395     }
00396     if (num_tsys_storage_ < n * m) {
00397       size_t new_num_storage = num_tsys_storage_ + block_size_;
00398       while (new_num_storage < n * m) {
00399         new_num_storage += block_size_;
00400       }
00401 //      std::cout << "resize tsys storage to " << new_num_storage << std::endl;
00402       void *new_tsys_storage = malloc(new_num_storage * sizeof(Float));
00403       copyStorage(tsys_shape_[0], tsys_shape_[1], 4,
00404           reinterpret_cast<Float *>(tsys_storage_.get()),
00405           reinterpret_cast<Float *>(new_tsys_storage));
00406       tsys_storage_.reset(new_tsys_storage);
00407       num_tsys_storage_ = new_num_storage;
00408       redirect = True;
00409     }
00410     if (redirect) {
00411       tsys.takeStorage(tsys_shape_,
00412           reinterpret_cast<Float *>(tsys_storage_.get()), SHARE);
00413     }
00414   }
00415 
00416   void setTcalSize(size_t n, size_t m) {
00417     Bool redirect = False;
00418     if (tcal_shape_[0] != (ssize_t) n) {
00419 //      std::cout << "resize tcal to " << n << std::endl;
00420       tcal_shape_[0] = n;
00421       redirect = True;
00422     }
00423     if (tcal_shape_[1] != (ssize_t) m) {
00424 //      std::cout << "resize tcal to " << n << std::endl;
00425       tcal_shape_[1] = m;
00426       redirect = True;
00427     }
00428     if (num_tcal_storage_ < n * m) {
00429       size_t new_num_storage = num_tcal_storage_ + block_size_;
00430       while (new_num_storage < n * m) {
00431         new_num_storage += block_size_;
00432       }
00433 //      std::cout << "resize tcal storage to " << new_num_storage << std::endl;
00434       void *new_tcal_storage = malloc(new_num_storage * sizeof(Float));
00435       copyStorage(tcal_shape_[0], tcal_shape_[1], 4,
00436           reinterpret_cast<Float *>(tcal_storage_.get()),
00437           reinterpret_cast<Float *>(new_tcal_storage));
00438       tcal_storage_.reset(new_tcal_storage);
00439       num_tcal_storage_ = new_num_storage;
00440       redirect = True;
00441     }
00442     if (redirect) {
00443       tcal.takeStorage(tcal_shape_,
00444           reinterpret_cast<Float *>(tcal_storage_.get()), SHARE);
00445     }
00446   }
00447 
00448   MSDataRecord(MSDataRecord const &other) :
00449       MSDataRecord() {
00450     *this = other;
00451   }
00452   MSDataRecord &operator=(MSDataRecord const &other) {
00453     time = other.time;
00454     interval = other.interval;
00455     antenna_id = other.antenna_id;
00456     field_id = other.field_id;
00457     spw_id = other.spw_id;
00458     feed_id = other.feed_id;
00459     scan = other.scan;
00460     subscan = other.subscan;
00461     intent = other.intent;
00462     pol_type = other.pol_type;
00463     num_pol = other.num_pol;
00464     direction = other.direction;
00465     direction_slice.takeStorage(direction_slice.shape(), direction.data(),
00466         SHARE);
00467     flag_row = other.flag_row;
00468     setDataSize(other.data_shape_[0], other.data_shape_[1]);
00469     corr_type = other.corr_type;
00470     if (other.is_float_) {
00471       float_data = other.float_data;
00472     } else {
00473       complex_data = other.complex_data;
00474     }
00475     flag = other.flag;
00476     setTsysSize(other.tsys_shape_[0], other.tsys_shape_[1]);
00477     tsys = other.tsys;
00478     setTcalSize(other.tcal_shape_[0], other.tcal_shape_[1]);
00479     tcal = other.tcal;
00480 
00481     temperature = other.temperature;
00482     pressure = other.pressure;
00483     rel_humidity = other.rel_humidity;
00484     wind_speed = other.wind_speed;
00485     wind_direction = other.wind_direction;
00486 
00487     return *this;
00488   }
00489 
00490 private:
00491   size_t const block_size_;
00492   size_t num_data_storage_;
00493   size_t num_tsys_storage_;
00494   size_t num_tcal_storage_;
00495   IPosition data_shape_;
00496   IPosition tsys_shape_;
00497   IPosition tcal_shape_;
00498   IPosition corr_type_shape_;
00499   Block<Int> corr_type_storage_;
00500   std::unique_ptr<void, sdfiller::Deleter> data_storage_;
00501   std::unique_ptr<void, sdfiller::Deleter> flag_storage_;
00502   std::unique_ptr<void, sdfiller::Deleter> tsys_storage_;
00503   std::unique_ptr<void, sdfiller::Deleter> tcal_storage_;
00504   std::unique_ptr<Float[]> sigma_storage_;
00505   Bool is_float_;
00506 
00507 public:
00508 // mandatory
00509   Double time;
00510   Double interval;
00511   Int antenna_id;
00512   Int field_id;
00513   Int spw_id;
00514   Int feed_id;
00515   Int scan;
00516   Int subscan;
00517   Int num_pol;
00518   String intent;
00519   String pol_type;
00520   Vector<Int> corr_type;
00521   Matrix<Double> direction;
00522   Matrix<Double> direction_slice;
00523   Matrix<Float> float_data;
00524   Matrix<Complex> complex_data;
00525   Matrix<Bool> flag;
00526   Bool flag_row;
00527   Vector<Float> sigma;
00528   Vector<Float> &weight;
00529 
00530   // optional
00531   Matrix<Float> tsys;
00532   Matrix<Float> tcal;
00533 
00534   Float temperature;
00535   Float pressure;
00536   Float rel_humidity;
00537   Float wind_speed;
00538   Float wind_direction;
00539 
00540 };
00541 
00542 } //# NAMESPACE SDFILLER - END
00543 } //# NAMESPACE CASA - END
00544 
00545 #endif /* SINGLEDISH_FILLER_DATARECORD_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1