Debian Codegraph @

src/atlas/interpolation/method/knn/GridBox.cc

raw
1 /*
2 * (C) Copyright 1996- ECMWF.
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 * In applying this licence, ECMWF does not waive the privileges and immunities
8 * granted to it by virtue of its status as an intergovernmental organisation nor
9 * does it submit to any jurisdiction.
10 */
11
12
13 #include "atlas/interpolation/method/knn/GridBox.h"
14
15 #include <algorithm>
16 #include <ostream>
17 #include <vector>
18
19 #include "eckit/types/FloatCompare.h"
20 #include "eckit/types/Fraction.h"
21
22 #include "atlas/grid.h"
23 #include "atlas/runtime/Exception.h"
24 #include "atlas/util/Constants.h"
25 #include "atlas/util/Earth.h"
26 #include "atlas/util/GaussianLatitudes.h"
27 #include "atlas/util/Point.h"
28
29
30 static constexpr double GLOBE = 360;
31 static constexpr double NORTH_POLE = 90;
32 static constexpr double SOUTH_POLE = -90;
33 static constexpr double TWO = 2;
34
35 double normalise(double lon, double minimum) {
36 while (lon < minimum) {
37 lon += GLOBE;
38 }
39 while (lon >= minimum + GLOBE) {
40 lon -= GLOBE;
41 }
42 return lon;
43 }
44
45 #define ASSERT_BOX(n, w, s, e) \
46 ATLAS_ASSERT(SOUTH_POLE <= s && s <= n && n <= NORTH_POLE); \
47 ATLAS_ASSERT(w <= e && e <= w + GLOBE)
48
49
50 namespace atlas {
51 namespace interpolation {
52 namespace method {
53
54
55 GridBox::GridBox(double north, double west, double south, double east):
56 north_(north), west_(west), south_(south), east_(east) {
57 ASSERT_BOX(north_, west_, south_, east_);
58 }
59
60
61 double GridBox::area() const {
62 return util::Earth::area({west_, north_}, {east_, south_});
63 }
64
65
66 double GridBox::diagonal() const {
67 return util::Earth::distance({west_, north_}, {east_, south_});
68 }
69
70
71 bool GridBox::intersects(GridBox& other) const {
72 double n = std::min(north_, other.north_);
73 double s = std::max(south_, other.south_);
74
75 if (!eckit::types::is_strictly_greater(n, s)) {
76 return false;
77 }
78
79 auto intersect = [](const GridBox& a, const GridBox& b, double& w, double& e) {
80 double ref = normalise(b.west_, a.west_);
81 double w_ = std::max(a.west_, ref);
82 double e_ = std::min(a.east_, normalise(b.east_, ref));
83
84 if (eckit::types::is_strictly_greater(e_, w_)) {
85 w = w_;
86 e = e_;
87 return true;
88 }
89 return false;
90 };
91
92 double w = std::min(west_, other.west_);
93 double e = w;
94
95 if (west_ <= other.west_ ? intersect(*this, other, w, e) || intersect(other, *this, w, e)
96 : intersect(other, *this, w, e) || intersect(*this, other, w, e)) {
97 ATLAS_ASSERT(w <= e);
98 other = {n, w, s, e};
99 return true;
100 }
101 return false;
102 }
103
104
105 void GridBox::print(std::ostream& out) const {
106 out << "GridBox[north=" << north_ << ",west=" << west_ << ",south=" << south_ << ",east=" << east_ << "]";
107 }
108
109
110 GridBoxes::GridBoxes(const Grid& grid, bool gaussianWeightedLatitudes) {
111 StructuredGrid structured(grid);
112 if (!structured || grid.projection()) {
113 throw_NotImplemented("GridBoxes only support structured, unprojected/unrotated grids", Here());
114 }
115
116
117 // Bounding box and periodicity
118 RectangularDomain domain = structured.domain();
119 ATLAS_ASSERT(domain);
120
121 auto north = domain.ymax();
122 auto south = domain.ymin();
123 auto west = domain.xmin();
124 auto east = domain.xmax();
125 ASSERT_BOX(north, west, south, east);
126
127 bool periodic(ZonalBandDomain(structured.domain()));
128
129
130 // Calculate grid-box parallels (latitude midpoints, or accumulated Gaussian quadrature weights)
131 auto& y = structured.yspace();
132
133 std::vector<double> lat;
134 lat.reserve(y.size() + 1);
135
136 GaussianGrid gaussian;
137 if (gaussianWeightedLatitudes && (gaussian = grid)) {
138 auto N = [&]() {
139 auto N = gaussian.N();
140 ATLAS_ASSERT(N > 0);
141 return size_t(N);
142 }();
143
144 std::vector<double> latitudes(N * 2);
145 std::vector<double> weights(N * 2);
146 util::gaussian_quadrature_npole_spole(N, latitudes.data(), weights.data());
147
148 std::vector<double> lat_global(N * 2 + 1);
149 auto b = lat_global.rbegin();
150 auto f = lat_global.begin();
151 *(b++) = SOUTH_POLE;
152 *(f++) = NORTH_POLE;
153
154 double wacc = -1.;
155 for (size_t j = 0; j < N; ++j, ++b, ++f) {
156 wacc += TWO * weights[j];
157 double deg = util::Constants::radiansToDegrees() * std::asin(wacc);
158 *b = deg;
159 *f = -(*b);
160 }
161 lat_global[N] = 0.; // (equator)
162
163 // Grids not covering the poles need clipping
164 size_t j = 0;
165 for (; j < latitudes.size(); ++j) {
166 if (eckit::types::is_approximately_lesser_or_equal(latitudes[j], north) &&
167 eckit::types::is_approximately_lesser_or_equal(south, latitudes[j])) {
168 lat.push_back(std::min(north, lat_global[j]));
169 }
170 else if (!lat.empty()) {
171 break;
172 }
173 }
174 ATLAS_ASSERT(!lat.empty());
175 lat.push_back(std::max(south, lat_global[j]));
176 }
177 else {
178 // non-Gaussian (but structured) grids
179 lat.push_back(north);
180 for (auto b = y.begin(), a = b++; b != y.end(); a = b++) {
181 lat.push_back((*b + *a) / TWO);
182 }
183 lat.push_back(south);
184 }
185
186
187 // Calculate grid-box meridians (longitude midpoints)
188 auto& x = structured.xspace();
189 ATLAS_ASSERT(x.nx().size() == x.dx().size());
190 ATLAS_ASSERT(x.nx().size() == x.xmin().size());
191
192 auto gridSize = [&]() {
193 auto N = grid.size();
194 ATLAS_ASSERT(N >= 0);
195 return size_t(N);
196 }();
197
198 clear();
199 reserve(gridSize);
200 for (size_t j = 0; j < x.nx().size(); ++j) {
201 eckit::Fraction dx(x.dx()[j]);
202 eckit::Fraction xmin(x.xmin()[j]);
203
204 auto n = (xmin / dx).integralPart();
205 if (n * dx < xmin) {
206 n += 1; // (adjust double-fraction conversions)
207 }
208
209 // On non-periodic grids, West- and East-most grid-boxes need clipping
210 ATLAS_ASSERT(x.nx()[j] > 0);
211
212 eckit::Fraction lon0 = (n * dx) - (dx / 2);
213 eckit::Fraction lon1 = lon0;
214 for (idx_t i = 0; i < x.nx()[j]; ++i) {
215 double lon0 = lon1;
216 lon1 += dx;
217
218 double n = lat[j];
219 double s = lat[j + 1];
220 double w = periodic ? lon0 : std::max<double>(west, lon0);
221 double e = periodic ? lon1 : std::min<double>(east, lon1);
222 emplace_back(GridBox(n, w, s, e));
223 }
224
225 if (periodic) {
226 ATLAS_ASSERT(lon1 == lon0 + GLOBE);
227 }
228 }
229
230 ATLAS_ASSERT(size() == gridSize);
231 }
232
233
234 GridBoxes::GridBoxes() = default;
235
236
237 double GridBoxes::getLongestGridBoxDiagonal() const {
238 ATLAS_ASSERT(!empty());
239
240 double R = 0.;
241 for (auto& box : *this) {
242 R = std::max(R, box.diagonal());
243 }
244
245 ATLAS_ASSERT(R > 0.);
246 return R;
247 }
248
249
250 } // namespace method
251 } // namespace interpolation
252 } // namespace atlas