UFO
RoundingEquispacedBinSelector.h
Go to the documentation of this file.
1 /*
2  * (C) Crown copyright 2021, Met Office
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #ifndef UFO_UTILS_ROUNDINGEQUISPACEDBINSELECTOR_H_
9 #define UFO_UTILS_ROUNDINGEQUISPACEDBINSELECTOR_H_
10 
11 #include <algorithm>
12 #include <cmath>
13 
14 #include "eckit/exception/Exceptions.h"
16 
17 namespace ufo
18 {
19 
20 /// \brief Represents an infinite set of consecutive intervals (_bins_) of the same width.
21 ///
22 /// Bin 0 is open; bins 1, 2 etc. are right-open; bins -1, -2 etc. are left-open.
23 ///
24 /// Call the bin() function to find the bin containing a particular value.
26  public:
27  // If necessary, these could be made template parameters.
28  typedef float ValueType;
29  typedef int IndexType;
30 
31  /// \brief Partition the real axis into bins of width \p binWidth, with the center of bin 0
32  /// located at \p bin0Center.
36  bin0Center_(bin0Center)
37  {
38  ASSERT_MSG(binWidth > 0, "Bin width must be positive");
39  }
40 
41  IndexType bin(ValueType value) const override {
42  IndexType binIndex = std::lround((value - bin0Center_) * inverseBinWidth_);
43  return binIndex;
44  }
45 
46  boost::optional<IndexType> numBins() const override {
47  return boost::none;
48  }
49 
50  ValueType binWidth() const override {
51  return binWidth_;
52  }
53 
54  ValueType inverseBinWidth() const override {
55  return inverseBinWidth_;
56  }
57 
58  ValueType binCenter(IndexType bin) const override {
59  return bin0Center_ + binWidth_ * bin;
60  }
61 
62  private:
66 };
67 
68 } // namespace ufo
69 
70 #endif // UFO_UTILS_ROUNDINGEQUISPACEDBINSELECTOR_H_
A finite or infinite collection of non-overlapping intervals (bins) of the same width.
Represents an infinite set of consecutive intervals (bins) of the same width.
ValueType binCenter(IndexType bin) const override
Return the value lying at the center of the bin with index bin.
ValueType binWidth() const override
Return the width of each bin.
ValueType inverseBinWidth() const override
Return the inverse of the width of each bin.
IndexType bin(ValueType value) const override
Return the index of the bin containing value, or the nearest bin if value lies outside all bins.
RoundingEquispacedBinSelector(ValueType binWidth, ValueType bin0Center=0)
Partition the real axis into bins of width binWidth, with the center of bin 0 located at bin0Center.
boost::optional< IndexType > numBins() const override
Return the number of bins or boost::none if the bin collection is infinite.
Definition: RunCRTM.h:27