DP3
Axis.h
Go to the documentation of this file.
1 // Axis.h: Classes representing a regular or irregular axis.
2 //
3 // Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
9 
10 #ifndef LOFAR_PARMDB_AXIS_H
11 #define LOFAR_PARMDB_AXIS_H
12 
13 #include "blob/BlobStreamable.h"
14 
15 #include <cstdint>
16 #include <memory>
17 #include <utility>
18 #include <vector>
19 
20 namespace dp3 {
21 namespace parmdb {
22 
25 
27 
29 class Axis : public blob::BlobStreamable {
30  public:
32  typedef std::shared_ptr<Axis> ShPtr;
33 
35  Axis();
36 
37  ~Axis() override {}
38 
40  virtual Axis::ShPtr clone() const = 0;
41 
44  bool operator==(const Axis& that) const;
45  bool operator!=(const Axis& that) const { return !operator==(that); }
47 
49  unsigned int getId() const { return itsId; }
50 
53  double center(size_t n) const { return itsCenter[n]; }
54  double width(size_t n) const { return itsWidth[n]; }
55  double upper(size_t n) const { return itsUpper[n]; }
56  double lower(size_t n) const { return itsLower[n]; }
58 
61  const std::vector<double>& centers() const { return itsCenter; }
62  const std::vector<double>& widths() const { return itsWidth; }
63  const std::vector<double>& uppers() const { return itsUpper; }
64  const std::vector<double>& lowers() const { return itsLower; }
66 
68  bool isRegular() const { return itsIsRegular; }
69 
71  size_t size() const { return itsCenter.size(); }
72 
75  double start() const { return itsLower[0]; }
76  double end() const { return itsUpper[itsUpper.size() - 1]; }
78 
80  std::pair<double, double> range() const {
81  return std::make_pair(start(), end());
82  }
83 
91  std::pair<size_t, bool> find(double x, bool biasRight = true,
92  size_t start = 0) const;
93 
95  size_t locate(double x, bool biasRight = true, size_t start = 0) const {
96  std::pair<size_t, bool> res = find(x, biasRight, start);
97  if (!res.second) throwNotFound(x);
98  return res.first;
99  }
100 
102  bool checkIntervals(const Axis& that) const;
103 
107  Axis::ShPtr subset(double start, double end, size_t& index) const;
108  Axis::ShPtr subset(double start, double end) const;
110 
112  Axis::ShPtr subset(size_t start, size_t end) const {
113  return doSubset(start, end);
114  }
115 
117  virtual Axis::ShPtr compress(size_t factor) const = 0;
118 
127  Axis::ShPtr combine(const Axis& that, int& s1, int& e1, int& s2,
128  int& e2) const;
129 
131  const std::string& classType() const override = 0;
132 
134  void write(blob::BlobOStream& bos) const override = 0;
135 
137  void read(blob::BlobIStream& bis) override = 0;
138 
144  static Axis::ShPtr makeAxis(const std::vector<double>& low,
145  const std::vector<double>& high);
146 
147  private:
149  Axis::ShPtr add(const Axis& that) const;
150 
151  virtual Axis::ShPtr doSubset(size_t start, size_t end) const = 0;
152 
154  void throwNotFound(double x) const;
155 
156  protected:
158  void setup(double start, double width, unsigned int count);
160  void setup(const std::vector<double>& v1, const std::vector<double>& v2,
161  bool asStartEnd);
162 
164  static unsigned int theirId;
165 
166  unsigned int itsId;
168  std::vector<double> itsCenter;
169  std::vector<double> itsWidth;
170  std::vector<double> itsLower;
171  std::vector<double> itsUpper;
172 };
173 
175 class RegularAxis : public Axis {
176  public:
179 
181  RegularAxis(double begin, double cellWidth, unsigned int count,
182  bool asStartEnd = false);
183 
184  ~RegularAxis() override;
185 
187  Axis::ShPtr clone() const override;
188 
189  Axis::ShPtr doSubset(size_t start, size_t end) const override;
190  Axis::ShPtr compress(size_t factor) const override;
191 
193  void write(blob::BlobOStream& bos) const override;
194 
196  void read(blob::BlobIStream& bis) override;
197 
199  const std::string& classType() const override;
200 
201  private:
202  double itsStart;
203  double itsWidth;
204  uint32_t itsCount;
205 };
206 
210 class OrderedAxis : public Axis {
211  public:
214 
219  OrderedAxis(const std::vector<double>& v1, const std::vector<double>& v2,
220  bool asStartEnd = false);
221 
222  ~OrderedAxis() override;
223 
225  Axis::ShPtr clone() const override;
226 
227  Axis::ShPtr doSubset(size_t start, size_t end) const override;
228  Axis::ShPtr compress(size_t factor) const override;
229 
231  void write(blob::BlobOStream& bos) const override;
232 
234  void read(blob::BlobIStream& bis) override;
235 
237  const std::string& classType() const override;
238 };
239 
241 
242 } // namespace parmdb
243 } // namespace dp3
244 
245 #endif
Input stream for a blob.
Definition: BlobIStream.h:43
Output stream for a blob.
Definition: BlobOStream.h:40
Interface for classes that can be streamed using blobs.
Definition: BlobStreamable.h:25
Classes representing a regular or irregular axis.
Definition: Axis.h:29
std::vector< double > itsCenter
Definition: Axis.h:168
void setup(const std::vector< double > &v1, const std::vector< double > &v2, bool asStartEnd)
Set up the object for an irregular axis.
std::shared_ptr< Axis > ShPtr
Define a shared_ptr for this class.
Definition: Axis.h:32
Axis::ShPtr combine(const Axis &that, int &s1, int &e1, int &s2, int &e2) const
Axis::ShPtr subset(size_t start, size_t end) const
Make a subset of the axis for the given start/end index.
Definition: Axis.h:112
double upper(size_t n) const
Definition: Axis.h:55
Axis::ShPtr subset(double start, double end, size_t &index) const
Axis()
The constructor sets the unique id.
double center(size_t n) const
Definition: Axis.h:53
static unsigned int theirId
Unique seqnr of an Axis object. Used in class AxisMapping.
Definition: Axis.h:164
void read(blob::BlobIStream &bis) override=0
Read the contents from the blob input stream bis into *this.
const std::vector< double > & centers() const
Definition: Axis.h:61
const std::vector< double > & uppers() const
Definition: Axis.h:63
bool operator==(const Axis &that) const
unsigned int itsId
Definition: Axis.h:166
~Axis() override
Definition: Axis.h:37
double start() const
Definition: Axis.h:75
size_t size() const
Get nr of cells.
Definition: Axis.h:71
std::pair< double, double > range() const
Get the total range of the axis.
Definition: Axis.h:80
void write(blob::BlobOStream &bos) const override=0
Write the contents of *this into the blob output stream bos.
Axis::ShPtr subset(double start, double end) const
const std::string & classType() const override=0
Return the type of *this as a string.
virtual Axis::ShPtr clone() const =0
Clone the object.
virtual Axis::ShPtr compress(size_t factor) const =0
Compress the axis.
unsigned int getId() const
Get the unique axis id.
Definition: Axis.h:49
std::vector< double > itsUpper
Definition: Axis.h:171
std::vector< double > itsWidth
Definition: Axis.h:169
static Axis::ShPtr makeAxis(const std::vector< double > &low, const std::vector< double > &high)
bool isRegular() const
Is the axis regular?
Definition: Axis.h:68
double lower(size_t n) const
Definition: Axis.h:56
bool operator!=(const Axis &that) const
Definition: Axis.h:45
bool checkIntervals(const Axis &that) const
Check if the corresponding intervals in this and that axis are the same.
double end() const
Definition: Axis.h:76
const std::vector< double > & widths() const
Definition: Axis.h:62
double width(size_t n) const
Definition: Axis.h:54
std::vector< double > itsLower
Definition: Axis.h:170
bool itsIsRegular
Definition: Axis.h:167
const std::vector< double > & lowers() const
Definition: Axis.h:64
void setup(double start, double width, unsigned int count)
Set up the object for a regular axis.
size_t locate(double x, bool biasRight=true, size_t start=0) const
Get the cellnr as above, but throw an exception if not found.
Definition: Axis.h:95
std::pair< size_t, bool > find(double x, bool biasRight=true, size_t start=0) const
Ordered irregularly strided cell centered axis. The cells are ordered and disjoint,...
Definition: Axis.h:210
OrderedAxis()
Default constructor creates one cell from -1e30 till 1e30.
OrderedAxis(const std::vector< double > &v1, const std::vector< double > &v2, bool asStartEnd=false)
void read(blob::BlobIStream &bis) override
Read the contents from the blob input stream bis into *this.
const std::string & classType() const override
Return the type of *this as a string.
Axis::ShPtr doSubset(size_t start, size_t end) const override
Axis::ShPtr clone() const override
Clone the object.
void write(blob::BlobOStream &bos) const override
Write the contents of *this into the blob output stream bos.
Axis::ShPtr compress(size_t factor) const override
Compress the axis.
Regularly strided cell centered axis.
Definition: Axis.h:175
Axis::ShPtr compress(size_t factor) const override
Compress the axis.
Axis::ShPtr clone() const override
Clone the object.
const std::string & classType() const override
Return the type of *this as a string.
RegularAxis(double begin, double cellWidth, unsigned int count, bool asStartEnd=false)
Construct giving the beginning of the axis and the width of each cell.
RegularAxis()
Default constructor creates one cell from -1e30 till 1e30.
Axis::ShPtr doSubset(size_t start, size_t end) const override
void write(blob::BlobOStream &bos) const override
Write the contents of *this into the blob output stream bos.
void read(blob::BlobIStream &bis) override
Read the contents from the blob input stream bis into *this.
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53