UFO
SpatialBinSelector.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_SPATIALBINSELECTOR_H_
9 #define UFO_UTILS_SPATIALBINSELECTOR_H_
10 
11 #include <cmath>
12 #include <vector>
13 
14 #include "ufo/utils/Constants.h"
16 
17 namespace ufo {
18 
20  DOWN, //< Round down
21  NEAREST //< Round to the nearest integer
22 };
23 
24 /// \brief Represents a partition of a sphere into a number of subsets (_bins_).
26  public:
27  // If necessary, these could be made template parameters.
28  typedef float ValueType;
29  typedef int IndexType;
30 
31  private:
32  static constexpr ValueType latitudeLowerBound_ = -90;
33  static constexpr ValueType latitudeUpperBound_ = 90;
34  static constexpr ValueType longitudeLowerBound_ = 0;
35  static constexpr ValueType longitudeUpperBound_ = 360;
36 
37  public:
38  /// \brief Partition a sphere into bins whose centers lie on a reduced Gaussian grid.
39  ///
40  /// \param numLatitudeBins
41  /// The number of zonal bands of bins into which the sphere is split.
42  /// \param roundingMode
43  /// - If set to NEAREST, the number of bins in each zonal band is chosen so that the bin width
44  /// in the zonal direction is as close as possible to that in the meridional direction.
45  /// - If set to DOWN, the number of bins is chosen so that the bin width in the zonal direction
46  /// is as small as possible, but no smaller than in the meridional direction.
47  SpatialBinSelector(IndexType numLatitudeBins, SpatialBinCountRoundingMode roundingMode);
48 
49  /// \brief Partition a sphere into bins whose centers lie on a regular Gaussian grid.
50  ///
51  /// \param numLatitudeBins
52  /// The number of zonal bands of bins into which the sphere is split.
53  /// \param numLongitudeBins
54  /// The number of meridional bands of bins into which the sphere is split.
55  SpatialBinSelector(IndexType numLatitudeBins, IndexType numLongitudeBins);
56 
57  /// \brief Return the index of the zonal band of bins containing points with a given latitude
58  /// (in degrees, assumed to lie in the interval [-90, 90]).
59  IndexType latitudeBin(ValueType latitude) const {
60  return latitudeBinSelector_.bin(latitude);
61  }
62 
63  /// \brief Return the index of the bin within the zonal band of index \p latitudeBin
64  /// containing points with a given latitude (in degrees, assumed to lie in the interval [0, 360)).
66  return longitudeBinSelectors_[latitudeBin].bin(longitude);
67  }
68 
69  /// \brief Return the latitude at the center of the zonal band of bins with index \p latitudeBin.
72  }
73 
74  /// \brief Return the longitude at the center of the bin with index \p latitudeBin lying in the
75  /// zonal band of bins with index \p latitudeBin.
78  }
79 
80  /// \brief Return the number of bins into which the sphere is split.
81  IndexType totalNumBins() const;
82 
83  /// \brief Return the width of each zonal band of bins.
86  }
87 
88  /// \brief Return the zonal width of each bin in the band of bins with index \p latitudeBin.
90  return longitudeBinSelectors_[latitudeBin].binWidth();
91  }
92 
93  /// \brief Return the inverse of the width of each zonal band of bins.
96  }
97 
98  /// \brief Return the inverse of the zonal width of each bin in the band of bins with index
99  /// \p latitudeBin.
101  return longitudeBinSelectors_[latitudeBin].inverseBinWidth();
102  }
103 
104  /// \brief Return \p idealNumBins rounded to a positive integer according to the rounding
105  /// strategy \p roundingMode.
106  static IndexType roundNumBins(float idealNumBins, SpatialBinCountRoundingMode roundingMode);
107 
108  private:
110  std::vector<EquispacedBinSelector> longitudeBinSelectors_;
111 };
112 
113 } // namespace ufo
114 
115 #endif // UFO_UTILS_SPATIALBINSELECTOR_H_
ufo::SpatialBinSelector
Represents a partition of a sphere into a number of subsets (bins).
Definition: SpatialBinSelector.h:25
ufo::SpatialBinSelector::SpatialBinSelector
SpatialBinSelector(IndexType numLatitudeBins, SpatialBinCountRoundingMode roundingMode)
Partition a sphere into bins whose centers lie on a reduced Gaussian grid.
Definition: SpatialBinSelector.cc:18
ufo::SpatialBinSelector::latitudeBin
IndexType latitudeBin(ValueType latitude) const
Return the index of the zonal band of bins containing points with a given latitude (in degrees,...
Definition: SpatialBinSelector.h:59
ufo::SpatialBinSelector::inverseLongitudeBinWidth
ValueType inverseLongitudeBinWidth(IndexType latitudeBin) const
Return the inverse of the zonal width of each bin in the band of bins with index latitudeBin.
Definition: SpatialBinSelector.h:100
ufo::SpatialBinSelector::roundNumBins
static IndexType roundNumBins(float idealNumBins, SpatialBinCountRoundingMode roundingMode)
Return idealNumBins rounded to a positive integer according to the rounding strategy roundingMode.
Definition: SpatialBinSelector.cc:51
ufo::SpatialBinSelector::latitudeUpperBound_
static constexpr ValueType latitudeUpperBound_
Definition: SpatialBinSelector.h:33
ufo::SpatialBinSelector::longitudeLowerBound_
static constexpr ValueType longitudeLowerBound_
Definition: SpatialBinSelector.h:34
ufo::SpatialBinSelector::latitudeLowerBound_
static constexpr ValueType latitudeLowerBound_
Definition: SpatialBinSelector.h:32
EquispacedBinSelector.h
ufo::SpatialBinSelector::longitudeBinSelectors_
std::vector< EquispacedBinSelector > longitudeBinSelectors_
Definition: SpatialBinSelector.h:110
ufo
Definition: RunCRTM.h:27
ufo::SpatialBinSelector::longitudeBinWidth
ValueType longitudeBinWidth(IndexType latitudeBin) const
Return the zonal width of each bin in the band of bins with index latitudeBin.
Definition: SpatialBinSelector.h:89
ufo::SpatialBinCountRoundingMode::NEAREST
@ NEAREST
ufo::SpatialBinSelector::latitudeBinWidth
ValueType latitudeBinWidth() const
Return the width of each zonal band of bins.
Definition: SpatialBinSelector.h:84
ufo::SpatialBinSelector::longitudeBin
IndexType longitudeBin(IndexType latitudeBin, ValueType longitude) const
Return the index of the bin within the zonal band of index latitudeBin containing points with a given...
Definition: SpatialBinSelector.h:65
ufo::SpatialBinSelector::latitudeBinSelector_
EquispacedBinSelector latitudeBinSelector_
Definition: SpatialBinSelector.h:109
ufo::EquispacedBinSelector::binWidth
ValueType binWidth() const
Return the width of each bin.
Definition: EquispacedBinSelector.h:55
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::SpatialBinSelector::longitudeBinCenter
ValueType longitudeBinCenter(IndexType latitudeBin, IndexType longitudeBin) const
Return the longitude at the center of the bin with index latitudeBin lying in the zonal band of bins ...
Definition: SpatialBinSelector.h:76
ufo::SpatialBinSelector::ValueType
float ValueType
Definition: SpatialBinSelector.h:28
ufo::EquispacedBinSelector::inverseBinWidth
ValueType inverseBinWidth() const
Return the inverse of the width of each bin.
Definition: EquispacedBinSelector.h:60
ufo::SpatialBinCountRoundingMode::DOWN
@ DOWN
ufo::SpatialBinSelector::IndexType
int IndexType
Definition: SpatialBinSelector.h:29
ufo::EquispacedBinSelector
Represents a set of consecutive intervals (bins) of the same width.
Definition: EquispacedBinSelector.h:22
ufo::SpatialBinSelector::latitudeBinCenter
ValueType latitudeBinCenter(IndexType latitudeBin) const
Return the latitude at the center of the zonal band of bins with index latitudeBin.
Definition: SpatialBinSelector.h:70
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
Constants.h
ufo::SpatialBinSelector::longitudeUpperBound_
static constexpr ValueType longitudeUpperBound_
Definition: SpatialBinSelector.h:35
ufo::SpatialBinSelector::inverseLatitudeBinWidth
ValueType inverseLatitudeBinWidth() const
Return the inverse of the width of each zonal band of bins.
Definition: SpatialBinSelector.h:94
ufo::SpatialBinSelector::totalNumBins
IndexType totalNumBins() const
Return the number of bins into which the sphere is split.
Definition: SpatialBinSelector.cc:44
ufo::SpatialBinCountRoundingMode
SpatialBinCountRoundingMode
Definition: SpatialBinSelector.h:19