DP3
BdaBuffer.h
Go to the documentation of this file.
1 // BdaBuffer.h: Buffer holding BDA data
2 // Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 
8 
9 #ifndef DP3_BASE_BDABUFFER_H_
10 #define DP3_BASE_BDABUFFER_H_
11 
12 #include <complex>
13 #include <map>
14 #include <string>
15 #include <vector>
16 
17 #include <aocommon/uvector.h>
18 
19 #include "common/Fields.h"
20 #include "common/Types.h"
21 
22 namespace dp3 {
23 namespace base {
24 
25 class BdaBuffer {
26  public:
27  struct Row {
28  Row(double time, double interval, double exposure, common::rownr_t row_nr,
29  std::size_t baseline_nr, std::size_t n_channels,
30  std::size_t n_correlations, std::size_t offset, const double* uvw);
31  std::size_t GetDataSize() const { return n_channels * n_correlations; }
32  bool IsMetadataEqual(const BdaBuffer::Row& other) const;
33  const double time;
34  const double interval;
35  const double exposure;
37  const std::size_t baseline_nr;
38  const std::size_t n_channels;
39  const std::size_t n_correlations;
40  const std::size_t offset;
41  double uvw[3];
42  };
43 
52  explicit BdaBuffer(std::size_t pool_size, const common::Fields& fields);
53 
59  BdaBuffer(const BdaBuffer& other) = delete;
60  BdaBuffer& operator=(const BdaBuffer& other) = delete;
73  BdaBuffer(const BdaBuffer& other, const common::Fields& fields);
74 
83  void AddData(const std::string& name = "") {
84  aocommon::UVector<std::complex<float>>& new_data = data_[name];
85  new_data.reserve(original_capacity_);
86  new_data.resize(GetNumberOfElements());
87  }
88 
97  void MoveData(BdaBuffer& source, const std::string& source_name = "",
98  const std::string& target_name = "");
99 
107  void RemoveData(const std::string& name = "") { data_.erase(name); }
108 
112  bool HasData(const std::string& name = "") const {
113  return data_.find(name) != data_.end();
114  }
115 
117  std::vector<std::string> GetDataNames() const {
118  std::vector<std::string> names;
119  names.reserve(data_.size());
120  for (const auto& name_vector : data_) {
121  names.push_back(name_vector.first);
122  }
123  return names;
124  }
125 
146  bool AddRow(double time, double interval, double exposure,
147  std::size_t baseline_nr, std::size_t n_channels,
148  std::size_t n_correlations, common::rownr_t row_nr,
149  const std::complex<float>* data = nullptr,
150  const bool* flags = nullptr, const float* weights = nullptr,
151  const double* uvw = nullptr);
152 
159  void Clear();
160 
165  std::size_t GetNumberOfElements() const {
166  return original_capacity_ - remaining_capacity_;
167  }
168 
173  std::size_t GetRemainingCapacity() const { return remaining_capacity_; }
174 
175  const std::complex<float>* GetData(const std::string& name = "") const;
176  std::complex<float>* GetData(const std::string& name = "");
177 
178  const std::complex<float>* GetData(std::size_t row,
179  const std::string& name = "") const;
180  std::complex<float>* GetData(std::size_t row, const std::string& name = "");
181 
182  const bool* GetFlags() const {
183  return flags_.empty() ? nullptr : flags_.data();
184  }
185  bool* GetFlags() { return flags_.empty() ? nullptr : flags_.data(); }
186 
187  const bool* GetFlags(std::size_t row) const {
188  return flags_.empty() ? nullptr : flags_.data() + rows_[row].offset;
189  }
190  bool* GetFlags(std::size_t row) {
191  return flags_.empty() ? nullptr : flags_.data() + rows_[row].offset;
192  }
193 
194  const float* GetWeights() const {
195  return weights_.empty() ? nullptr : weights_.data();
196  }
197  float* GetWeights() { return weights_.empty() ? nullptr : weights_.data(); }
198 
199  const float* GetWeights(std::size_t row) const {
200  return weights_.empty() ? nullptr : weights_.data() + rows_[row].offset;
201  }
202  float* GetWeights(std::size_t row) {
203  return weights_.empty() ? nullptr : weights_.data() + rows_[row].offset;
204  }
205 
206  std::vector<Row>& GetRows() { return rows_; }
207  const std::vector<Row>& GetRows() const { return rows_; }
208 
209  static constexpr bool TimeIsLess(double x, double y) {
210  return x < (y - kTimeTolerance);
211  }
212  static constexpr bool TimeIsLessEqual(double x, double y) {
213  return x < (y + kTimeTolerance);
214  }
215  static constexpr bool TimeIsGreaterEqual(double x, double y) {
216  return x > (y - kTimeTolerance);
217  }
218  static constexpr bool TimeIsEqual(double x, double y) {
219  // Don't use std::abs, since it is not a constexpr.
220  return ((x > y) ? (x - y) : (y - x)) < kTimeTolerance;
221  }
222 
223  bool IsMetadataEqual(const BdaBuffer& other) const;
224 
225  // Tolerance value for comparing measurement timestamps. It should be larger
226  // than the rounding errors that occur due to BDA averaging and expanding.
227  static constexpr double kTimeTolerance = 2.0e-6;
228 
229  private:
233  std::map<std::string, aocommon::UVector<std::complex<float>>> data_;
234  aocommon::UVector<bool> flags_;
235  aocommon::UVector<float> weights_;
238  std::vector<Row> rows_;
239  std::size_t original_capacity_;
240  std::size_t remaining_capacity_;
241 };
242 
243 } // namespace base
244 } // namespace dp3
245 
246 #endif // DP3_BASE_BDABUFFER_H_
Define common types.
Definition: BdaBuffer.h:25
std::size_t GetRemainingCapacity() const
Definition: BdaBuffer.h:173
static constexpr bool TimeIsGreaterEqual(double x, double y)
Definition: BdaBuffer.h:215
void AddData(const std::string &name="")
Definition: BdaBuffer.h:83
void RemoveData(const std::string &name="")
Definition: BdaBuffer.h:107
void MoveData(BdaBuffer &source, const std::string &source_name="", const std::string &target_name="")
static constexpr double kTimeTolerance
Definition: BdaBuffer.h:227
static constexpr bool TimeIsLess(double x, double y)
Definition: BdaBuffer.h:209
bool HasData(const std::string &name="") const
Definition: BdaBuffer.h:112
bool * GetFlags(std::size_t row)
Definition: BdaBuffer.h:190
bool AddRow(double time, double interval, double exposure, std::size_t baseline_nr, std::size_t n_channels, std::size_t n_correlations, common::rownr_t row_nr, const std::complex< float > *data=nullptr, const bool *flags=nullptr, const float *weights=nullptr, const double *uvw=nullptr)
const bool * GetFlags(std::size_t row) const
Definition: BdaBuffer.h:187
float * GetWeights()
Definition: BdaBuffer.h:197
BdaBuffer(const BdaBuffer &other, const common::Fields &fields)
static constexpr bool TimeIsLessEqual(double x, double y)
Definition: BdaBuffer.h:212
std::complex< float > * GetData(const std::string &name="")
const std::complex< float > * GetData(std::size_t row, const std::string &name="") const
BdaBuffer(std::size_t pool_size, const common::Fields &fields)
float * GetWeights(std::size_t row)
Definition: BdaBuffer.h:202
std::vector< std::string > GetDataNames() const
Definition: BdaBuffer.h:117
BdaBuffer(const BdaBuffer &other)=delete
const bool * GetFlags() const
Definition: BdaBuffer.h:182
const float * GetWeights() const
Definition: BdaBuffer.h:194
std::vector< Row > & GetRows()
Definition: BdaBuffer.h:206
static constexpr bool TimeIsEqual(double x, double y)
Definition: BdaBuffer.h:218
std::size_t GetNumberOfElements() const
Definition: BdaBuffer.h:165
BdaBuffer & operator=(const BdaBuffer &other)=delete
const float * GetWeights(std::size_t row) const
Definition: BdaBuffer.h:199
bool IsMetadataEqual(const BdaBuffer &other) const
const std::vector< Row > & GetRows() const
Definition: BdaBuffer.h:207
bool * GetFlags()
Definition: BdaBuffer.h:185
std::complex< float > * GetData(std::size_t row, const std::string &name="")
const std::complex< float > * GetData(const std::string &name="") const
Definition: Fields.h:16
unsigned int rownr_t
Definition: Types.h:19
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53
Definition: BdaBuffer.h:27
const double exposure
Exposure time for the measurements in seconds.
Definition: BdaBuffer.h:35
const std::size_t n_correlations
Definition: BdaBuffer.h:39
const double time
Centroid time for the measurements in MJD seconds.
Definition: BdaBuffer.h:33
const std::size_t baseline_nr
Definition: BdaBuffer.h:37
bool IsMetadataEqual(const BdaBuffer::Row &other) const
std::size_t GetDataSize() const
Definition: BdaBuffer.h:31
const std::size_t n_channels
Definition: BdaBuffer.h:38
const double interval
Duration time for the measurements in seconds.
Definition: BdaBuffer.h:34
double uvw[3]
Definition: BdaBuffer.h:41
Row(double time, double interval, double exposure, common::rownr_t row_nr, std::size_t baseline_nr, std::size_t n_channels, std::size_t n_correlations, std::size_t offset, const double *uvw)
const std::size_t offset
Relative position in BdaBuffer vectors.
Definition: BdaBuffer.h:40
common::rownr_t row_nr
Definition: BdaBuffer.h:36