UFO
EquispacedBinSelector.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2019 Met Office UK
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_EQUISPACEDBINSELECTOR_H_
9 #define UFO_UTILS_EQUISPACEDBINSELECTOR_H_
10 
11 #include <algorithm>
12 
13 #include "eckit/exception/Exceptions.h"
14 
15 namespace ufo
16 {
17 
18 /// \brief Represents a set of consecutive intervals (_bins_) of the same width.
19 ///
20 /// Call the bin() function to find the bin containing a particular value.
22 {
23  public:
24  // If necessary, these could be made template parameters.
25  typedef float ValueType;
26  typedef int IndexType;
27 
28  /// \brief Partition the interval [\p lowerBound, \p upperBound] into \p numBins
29  /// bins of the same width.
31  : lowerBound_(lowerBound),
32  binWidth_((upperBound - lowerBound) / numBins),
35  {
36  ASSERT_MSG(upperBound > lowerBound, "Upper bound must be larger than lower bound");
37  ASSERT_MSG(numBins > 0, "Number of bins must be positive");
38  }
39 
40  /// \brief Return the (0-based) index of the bin containing \p value, or the nearest bin
41  /// if \p value lies outside all bins.
42  IndexType bin(ValueType value) const {
43  IndexType binIndex = static_cast<IndexType>((value - lowerBound_) * inverseBinWidth_);
44  binIndex = std::max(IndexType(0), binIndex);
45  binIndex = std::min(numBins_ - 1, binIndex);
46  return binIndex;
47  }
48 
49  /// \brief Return the number of bins.
50  IndexType numBins() const {
51  return numBins_;
52  }
53 
54  /// \brief Return the width of each bin.
55  ValueType binWidth() const {
56  return binWidth_;
57  }
58 
59  /// \brief Return the inverse of the width of each bin.
61  return inverseBinWidth_;
62  }
63 
64  /// \brief Return the value lying at the center of the bin with index \p bin.
66  return lowerBound_ + binWidth_ * (bin + ValueType(0.5));
67  }
68 
69  private:
74 };
75 
76 } // namespace ufo
77 
78 #endif // UFO_UTILS_EQUISPACEDBINSELECTOR_H_
ufo::EquispacedBinSelector::IndexType
int IndexType
Definition: EquispacedBinSelector.h:26
ufo::EquispacedBinSelector::numBins
IndexType numBins() const
Return the number of bins.
Definition: EquispacedBinSelector.h:50
ufo
Definition: RunCRTM.h:27
ufo::EquispacedBinSelector::binWidth_
ValueType binWidth_
Definition: EquispacedBinSelector.h:71
ufo::EquispacedBinSelector::binWidth
ValueType binWidth() const
Return the width of each bin.
Definition: EquispacedBinSelector.h:55
ufo::EquispacedBinSelector::EquispacedBinSelector
EquispacedBinSelector(ValueType lowerBound, ValueType upperBound, IndexType numBins)
Partition the interval [lowerBound, upperBound] into numBins bins of the same width.
Definition: EquispacedBinSelector.h:30
ufo::EquispacedBinSelector::binCenter
ValueType binCenter(IndexType bin) const
Return the value lying at the center of the bin with index bin.
Definition: EquispacedBinSelector.h:65
ufo::EquispacedBinSelector::ValueType
float ValueType
Definition: EquispacedBinSelector.h:25
ufo::EquispacedBinSelector::inverseBinWidth
ValueType inverseBinWidth() const
Return the inverse of the width of each bin.
Definition: EquispacedBinSelector.h:60
ufo::EquispacedBinSelector::numBins_
IndexType numBins_
Definition: EquispacedBinSelector.h:73
ufo::EquispacedBinSelector
Represents a set of consecutive intervals (bins) of the same width.
Definition: EquispacedBinSelector.h:22
ufo::EquispacedBinSelector::inverseBinWidth_
ValueType inverseBinWidth_
Definition: EquispacedBinSelector.h:72
ufo::EquispacedBinSelector::bin
IndexType bin(ValueType value) const
Return the (0-based) index of the bin containing value, or the nearest bin if value lies outside all ...
Definition: EquispacedBinSelector.h:42
ufo::EquispacedBinSelector::lowerBound_
ValueType lowerBound_
Definition: EquispacedBinSelector.h:70