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;
39 
40  public:
41  /// \brief Partition a sphere into bins whose centers lie on a reduced Gaussian grid.
42  ///
43  /// \param numLatitudeBins
44  /// The number of zonal bands of bins into which the sphere is split.
45  /// \param roundingMode
46  /// - If set to NEAREST, the number of bins in each zonal band is chosen so that the bin width
47  /// in the zonal direction is as close as possible to that in the meridional direction.
48  /// - If set to DOWN, the number of bins is chosen so that the bin width in the zonal direction
49  /// is as small as possible, but no smaller than in the meridional direction.
50  /// \param metOfficeOpsCompatibilityMode
51  /// If true, the compatibility mode with the Met Office OPS system will be activated.
52  /// This will stop longitudeBin() from clamping longitudes to the interval [0, 360] deg.
53  ///
54  /// Activation of this mode in new code is discouraged; even if input longitudes are e.g. in
55  /// the range [-180, 180] deg, it is normally better to wrap them to the [0, 360] deg range
56  /// before passing them to longitudeBin(). Otherwise there's a risk that points lying exactly
57  /// at -180 or 180 deg will be put into a bin of their own.
58  SpatialBinSelector(IndexType numLatitudeBins, SpatialBinCountRoundingMode roundingMode,
59  bool metOfficeOpsCompatibilityMode = false);
60 
61  /// \brief Partition a sphere into bins whose centers lie on a regular Gaussian grid.
62  ///
63  /// \param numLatitudeBins
64  /// The number of zonal bands of bins into which the sphere is split.
65  /// \param numLongitudeBins
66  /// The number of meridional bands of bins into which the sphere is split.
67  SpatialBinSelector(IndexType numLatitudeBins, IndexType numLongitudeBins,
68  bool metOfficeOpsCompatibilityMode = false);
69 
70  /// \brief Return the index of the zonal band of bins containing points with a given latitude
71  /// (in degrees, assumed to lie in the interval [-90, 90]).
72  IndexType latitudeBin(ValueType latitude) const {
73  return latitudeBinSelector_.bin(latitude);
74  }
75 
76  /// \brief Return the index of the bin within the zonal band of index \p latitudeBin
77  /// containing points with a given longitude (in degrees).
78  ///
79  /// The longitude is assumed to lie in the interval [0, 360] unless the compatibility mode with
80  /// the Met Office OPS system is in effect.
82  return longitudeBinSelectors_[latitudeBin].bin(longitude);
83  }
84 
85  /// \brief Return the latitude at the center of the zonal band of bins with index \p latitudeBin.
88  }
89 
90  /// \brief Return the longitude at the center of the bin with index \p latitudeBin lying in the
91  /// zonal band of bins with index \p latitudeBin.
94  }
95 
96  /// \brief Return the number of bins into which the sphere is split.
97  IndexType totalNumBins() const;
98 
99  /// \brief Return the width of each zonal band of bins.
102  }
103 
104  /// \brief Return the zonal width of each bin in the band of bins with index \p latitudeBin.
106  return longitudeBinSelectors_[latitudeBin].binWidth();
107  }
108 
109  /// \brief Return the inverse of the width of each zonal band of bins.
112  }
113 
114  /// \brief Return the inverse of the zonal width of each bin in the band of bins with index
115  /// \p latitudeBin.
117  return longitudeBinSelectors_[latitudeBin].inverseBinWidth();
118  }
119 
120  /// \brief Return \p idealNumBins rounded to a positive integer according to the rounding
121  /// strategy \p roundingMode.
122  static IndexType roundNumBins(float idealNumBins, SpatialBinCountRoundingMode roundingMode);
123 
124  private:
127  std::vector<TruncatingEquispacedBinSelector> longitudeBinSelectors_;
128 };
129 
130 } // namespace ufo
131 
132 #endif // UFO_UTILS_SPATIALBINSELECTOR_H_
Represents a partition of a sphere into a number of subsets (bins).
static constexpr ValueType longitudeUpperBound_
IndexType totalNumBins() const
Return the number of bins into which the sphere is split.
ValueType latitudeBinCenter(IndexType latitudeBin) const
Return the latitude at the center of the zonal band of bins with index latitudeBin.
TruncatingEquispacedBinSelector latitudeBinSelector_
static constexpr int opsCompatibilityModeRelativeLongitudeRange_
ValueType latitudeBinWidth() const
Return the width of each zonal band of bins.
std::vector< TruncatingEquispacedBinSelector > longitudeBinSelectors_
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...
static constexpr ValueType longitudeLowerBound_
static constexpr ValueType latitudeLowerBound_
static constexpr ValueType opsCompatibilityModeLongitudeUpperBound_
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 ...
static constexpr ValueType latitudeUpperBound_
SpatialBinSelector(IndexType numLatitudeBins, SpatialBinCountRoundingMode roundingMode, bool metOfficeOpsCompatibilityMode=false)
Partition a sphere into bins whose centers lie on a reduced Gaussian grid.
IndexType latitudeBin(ValueType latitude) const
Return the index of the zonal band of bins containing points with a given latitude (in degrees,...
static constexpr ValueType opsCompatibilityModeLongitudeLowerBound_
ValueType longitudeBinWidth(IndexType latitudeBin) const
Return the zonal width of each bin in the band of bins with index latitudeBin.
ValueType inverseLongitudeBinWidth(IndexType latitudeBin) const
Return the inverse of the zonal width of each bin in the band of bins with index latitudeBin.
ValueType inverseLatitudeBinWidth() const
Return the inverse of the width of each zonal band of bins.
static IndexType roundNumBins(float idealNumBins, SpatialBinCountRoundingMode roundingMode)
Return idealNumBins rounded to a positive integer according to the rounding strategy roundingMode.
Represents a finite set of consecutive intervals (bins) of the same width, each closed from the left ...
ValueType binWidth() const override
Return the width of each bin.
ValueType binCenter(IndexType bin) const override
Return the value lying at the center of the bin with index 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.
Definition: RunCRTM.h:27
SpatialBinCountRoundingMode