Debian Codegraph @

src/atlas/grid/detail/tiles/FV3Tiles.cc

raw
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 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include <array>
12 #include <iomanip>
13 #include <limits>
14 #include <ostream>
15 #include <string>
16 #include <utility>
17
18 #include "eckit/utils/Hash.h"
19
20 #include "atlas/grid/detail/tiles/TilesFactory.h"
21
22 #include "atlas/grid/detail/tiles/FV3Tiles.h"
23 #include "atlas/grid/detail/tiles/Tiles.h"
24 #include "atlas/projection/detail/ProjectionUtilities.h"
25 #include "atlas/runtime/Exception.h"
26 #include "atlas/runtime/Log.h"
27 #include "atlas/runtime/Trace.h"
28 #include "atlas/util/Constants.h"
29 #include "atlas/util/CoordinateEnums.h"
30 #include "atlas/util/Point.h"
31
32
33 namespace {
34
35 static constexpr bool debug = false; // constexpr so compiler can optimize `if ( debug ) { ... }` out
36 static constexpr double rad2deg = atlas::util::Constants::radiansToDegrees();
37
38 using atlas::projection::detail::ProjectionUtilities;
39
40 static bool is_tiny(const double& x) {
41 constexpr double epsilon = 1.e-12;
42 return (std::abs(x) < epsilon);
43 }
44
45 static bool is_same(const double& x, const double& y, const double& tol = 1.0) {
46 constexpr double epsilon = 1.e-12;
47 return (std::abs(x - y) < epsilon * tol);
48 }
49
50 static bool is_less(const double& lhs, const double& rhs) {
51 constexpr double epsilon = 1.e-12;
52 return lhs < rhs - epsilon;
53 }
54
55 static bool is_geq(const double& lhs, const double& rhs) {
56 constexpr double epsilon = 1.e-12;
57 return lhs >= rhs - epsilon;
58 }
59
60 void sphericalToCartesian(const double lonlat[], double xyz[]) {
61 auto crd_sys = ProjectionUtilities::CoordinateSystem::LEFT_HAND;
62 constexpr double radius = 1.;
63 ProjectionUtilities::sphericalToCartesian(lonlat, xyz, crd_sys, radius);
64 }
65
66 } // namespace
67
68 namespace atlas {
69 namespace grid {
70 namespace detail {
71
72 // constructor
73 FV3CubedSphereTiles::FV3CubedSphereTiles(const eckit::Parametrisation&) {}
74
75 std::array<std::array<double, 6>, 2> FV3CubedSphereTiles::xy2abOffsets() const {
76 return {{{0., 1., 1., 2., 3., 3.}, {1., 1., 2., 1., 1., 0.}}};
77 }
78
79 std::array<std::array<double, 6>, 2> FV3CubedSphereTiles::ab2xyOffsets() const {
80 return {{{0., 90., 90., 180., 270., 270.}, {-45., -45., 45., -45., -45., -135.}}};
81 }
82
83 static void tile0Rotate(double xyz[]) {
84 // Face 0, no rotation.
85 }
86
87 static void tile1Rotate(double xyz[]) {
88 // Face 1: rotate -90.0 degrees about z axis
89 constexpr double angle = -M_PI / 2.0;
90 ProjectionUtilities::rotate3dZ(angle, xyz);
91 }
92
93 static void tile2Rotate(double xyz[]) {
94 // Face 2: rotate -90.0 degrees about z axis
95 // rotate 90.0 degrees about x axis
96 constexpr double angle_z = -M_PI / 2.0;
97 ProjectionUtilities::rotate3dZ(angle_z, xyz);
98 constexpr double angle_x = M_PI / 2.0;
99 ProjectionUtilities::rotate3dX(angle_x, xyz);
100 }
101
102 static void tile3Rotate(double xyz[]) {
103 // Face 3: rotate -180.0 degrees about z axis
104 constexpr double angle = -M_PI;
105 ProjectionUtilities::rotate3dZ(angle, xyz);
106 }
107
108 static void tile4Rotate(double xyz[]) {
109 // Face 4: rotate 90.0 degrees about z axis
110 constexpr double angle = M_PI / 2.0;
111 ProjectionUtilities::rotate3dZ(angle, xyz);
112 }
113
114 static void tile5Rotate(double xyz[]) {
115 // Face 5: rotate 90.0 degrees about y axis
116 // rotate 90.0 degrees about z axis
117 constexpr double angle_y = M_PI / 2.0;
118 ProjectionUtilities::rotate3dY(angle_y, xyz);
119 constexpr double angle_z = M_PI / 2.0;
120 ProjectionUtilities::rotate3dZ(angle_z, xyz);
121 }
122
123 void FV3CubedSphereTiles::rotate(idx_t t, double xyz[]) const {
124 switch (t) {
125 case 0: {
126 tile0Rotate(xyz);
127 break;
128 }
129 case 1: {
130 tile1Rotate(xyz);
131 break;
132 }
133 case 2: {
134 tile2Rotate(xyz);
135 break;
136 }
137 case 3: {
138 tile3Rotate(xyz);
139 break;
140 }
141 case 4: {
142 tile4Rotate(xyz);
143 break;
144 }
145 case 5: {
146 tile5Rotate(xyz);
147 break;
148 }
149 default: {
150 throw_OutOfRange("t", t, 6);
151 }
152 }
153 }
154
155 static void tile0RotateInverse(double xyz[]) {
156 // Face 0, no rotation.
157 }
158
159 static void tile1RotateInverse(double xyz[]) {
160 // Face 1: rotate 90.0 degrees about z axis
161 constexpr double angle = M_PI / 2.0;
162 ProjectionUtilities::rotate3dZ(angle, xyz);
163 }
164
165 static void tile2RotateInverse(double xyz[]) {
166 // Face 2: rotate 90.0 degrees about x axis
167 // rotate -90.0 degrees about z axis
168 constexpr double angle_x = -M_PI / 2.0;
169 ProjectionUtilities::rotate3dX(angle_x, xyz);
170 constexpr double angle_z = M_PI / 2.0;
171 ProjectionUtilities::rotate3dZ(angle_z, xyz);
172 }
173
174 static void tile3RotateInverse(double xyz[]) {
175 // Face 3: rotate 180.0 degrees about z axis
176 constexpr double angle = M_PI;
177 ProjectionUtilities::rotate3dZ(angle, xyz);
178 }
179
180 static void tile4RotateInverse(double xyz[]) {
181 // Face 4: rotate -90.0 degrees about y axis
182 constexpr double angle = -M_PI / 2.0;
183 ProjectionUtilities::rotate3dZ(angle, xyz);
184 }
185
186 static void tile5RotateInverse(double xyz[]) {
187 // Face 5: rotate -90.0 degrees about y axis
188 // rotate -90.0 degrees about z axis
189 constexpr double angle_z = -M_PI / 2.;
190 ProjectionUtilities::rotate3dZ(angle_z, xyz);
191 constexpr double angle_y = -M_PI / 2.;
192 ProjectionUtilities::rotate3dY(angle_y, xyz);
193 }
194
195 void FV3CubedSphereTiles::unrotate(idx_t t, double xyz[]) const {
196 switch (t) {
197 case 0: {
198 tile0RotateInverse(xyz);
199 break;
200 }
201 case 1: {
202 tile1RotateInverse(xyz);
203 break;
204 }
205 case 2: {
206 tile2RotateInverse(xyz);
207 break;
208 }
209 case 3: {
210 tile3RotateInverse(xyz);
211 break;
212 }
213 case 4: {
214 tile4RotateInverse(xyz);
215 break;
216 }
217 case 5: {
218 tile5RotateInverse(xyz);
219 break;
220 }
221 default: {
222 throw_OutOfRange("t", t, 6);
223 }
224 }
225 }
226
227 idx_t FV3CubedSphereTiles::indexFromXY(const double xy[]) const {
228 // Assume one face-edge is of length 90 degrees.
229 //
230 // y ^
231 // |
232 // 135 ----------
233 // | | ^ |
234 // | | |
235 // | |=< 2 <|
236 // | | v |
237 // | | = |
238 // 45 0----------2----------3----------4----------
239 // | | ^ | ^ | = | = |
240 // | | | | ^ | ^ |
241 // | |=< 0 <|=< 1 <|=< 3 <|=< 4 <|
242 // | | v | v | | |
243 // | | = | = | v | v |
244 // -45 0 ---------1----------1----------5----------
245 // | | = |
246 // | | ^ |
247 // | |=< 5 <|
248 // | | |
249 // | | v |
250 // -135 ----------(5 for end iterator)
251 // ----0---------90--------180--------270--------360---> x
252
253
254 idx_t t{-1};
255
256 if ((xy[XX] >= 0.) && (xy[YY] >= -45.) && (xy[XX] < 90.) && (xy[YY] < 45.)) {
257 t = 0;
258 }
259 else if ((xy[XX] >= 90.) && (xy[YY] >= -45.) && (xy[XX] < 180.) && (xy[YY] < 45.)) {
260 t = 1;
261 }
262 else if ((xy[XX] >= 90.) && (xy[YY] >= 45.) && (xy[XX] < 180.) && (xy[YY] < 135.)) {
263 t = 2;
264 }
265 else if ((xy[XX] >= 180.) && (xy[YY] > -45.) && (xy[XX] < 270.) && (xy[YY] <= 45.)) {
266 t = 3;
267 }
268 else if ((xy[XX] >= 270.) && (xy[YY] > -45.) && (xy[XX] < 360.) && (xy[YY] <= 45.)) {
269 t = 4;
270 }
271 else if ((xy[XX] >= 270.) && (xy[YY] > -135.) && (xy[XX] < 360.) && (xy[YY] <= -45.)) {
272 t = 5;
273 }
274
275 // extra points
276 if (is_same(xy[XX], 0.) && is_same(xy[YY], 45.)) {
277 t = 0;
278 }
279 if (is_same(xy[XX], 180.) && is_same(xy[YY], -45.)) {
280 t = 1;
281 }
282
283 // for end iterator !!!!
284 if (is_same(xy[XX], 360.) && is_same(xy[YY], -135.)) {
285 t = 5;
286 }
287
288 ATLAS_ASSERT(t >= 0);
289
290 return t;
291 }
292
293 // Calculates the FV3 panel
294 // Input (crd) is Longitude and Latitude in Radians
295 // Output is tile number
296 idx_t FV3CubedSphereTiles::indexFromLonLat(const double crd[]) const {
297 idx_t t(-1); // tile index
298 double xyz[3];
299
300 sphericalToCartesian(crd, xyz);
301
302 static const double cornerLat = std::asin(1. / std::sqrt(3.0)) * rad2deg;
303 // the magnitude of the latitude at the corners of the cube (not including the sign)
304 // in radians.
305
306 const double& lon = crd[LON];
307 const double& lat = crd[LAT];
308
309 double zPlusAbsX = xyz[ZZ] + std::abs(xyz[XX]);
310 double zPlusAbsY = xyz[ZZ] + std::abs(xyz[YY]);
311 double zMinusAbsX = xyz[ZZ] - std::abs(xyz[XX]);
312 double zMinusAbsY = xyz[ZZ] - std::abs(xyz[YY]);
313
314 // Note that this method can lead to roundoff errors that can
315 // cause the tile selection to fail.
316 // To this end we enforce that tiny values close (in roundoff terms)
317 // to a boundary should end up exactly on the boundary.
318 if (is_tiny(zPlusAbsX)) {
319 zPlusAbsX = 0.;
320 }
321 if (is_tiny(zPlusAbsY)) {
322 zPlusAbsY = 0.;
323 }
324 if (is_tiny(zMinusAbsX)) {
325 zMinusAbsX = 0.;
326 }
327 if (is_tiny(zMinusAbsY)) {
328 zMinusAbsY = 0.;
329 }
330
331 if (is_geq(lon, 315.) || is_less(lon, 45.)) {
332 if ((zPlusAbsX <= 0.) && (zPlusAbsY <= 0.)) {
333 t = 2;
334 }
335 else if ((zMinusAbsX > 0.) && (zMinusAbsY > 0.)) {
336 t = 5;
337 }
338 else {
339 t = 0;
340 }
341 // extra point corner point (considering two different longitudes depending on
342 // whether we are on longitude points [0,360] or [-45.,45.]
343 if (is_same(lon, -45.) && is_same(lat, cornerLat)) {
344 t = 0;
345 }
346 if (is_same(lon, 315.) && is_same(lat, cornerLat)) {
347 t = 0;
348 }
349 }
350
351 if (is_geq(lon, 45.) && is_less(lon, 135.)) {
352 // interior
353 if ((zPlusAbsX <= 0.) && (zPlusAbsY <= 0.)) {
354 t = 2;
355 }
356 else if ((zMinusAbsX > 0.) && (zMinusAbsY > 0.)) {
357 t = 5;
358 }
359 else {
360 t = 1;
361 }
362 }
363
364 if (is_geq(lon, 135.) && is_less(lon, 225.)) {
365 // interior
366 if ((zPlusAbsX < 0.) && (zPlusAbsY < 0.)) {
367 t = 2;
368 }
369 else if ((zMinusAbsX >= 0.) && (zMinusAbsY >= 0.)) {
370 t = 5;
371 }
372 else {
373 t = 3;
374 }
375 // extra point corner point
376 if (is_same(lon, 135.) && is_same(lat, -cornerLat)) {
377 t = 1;
378 }
379 }
380
381 if (is_geq(lon, 225.) && is_less(lon, 315.)) {
382 // interior
383 if ((zPlusAbsX < 0.) && (zPlusAbsY < 0.)) {
384 t = 2;
385 }
386 else if ((zMinusAbsX >= 0.) && (zMinusAbsY >= 0.)) {
387 t = 5;
388 }
389 else {
390 t = 4;
391 }
392 }
393
394 if (debug) {
395 Log::info() << "tileFromLonLat:: lonlat abs xyz t = "
396 << std::setprecision(std::numeric_limits<double>::digits10 + 1) << zPlusAbsX << " " << zPlusAbsY
397 << " " << zMinusAbsX << " " << zMinusAbsY << " " << crd[LON] << " " << crd[LAT] << " " << xyz[XX]
398 << " " << xyz[YY] << " " << xyz[ZZ] << " " << t << std::endl;
399 }
400
401 return t;
402 }
403
404 void FV3CubedSphereTiles::enforceXYdomain(double xy[]) const {
405 // the conversion from lonlat to xy can involve some small errors and small errors
406 // can affect whether xy that is created within a valid space
407 // This has been tested with N = 512 with equiangular and equidistant projections.
408 const double tol{70.0};
409 constexpr double epsilon = std::numeric_limits<double>::epsilon();
410
411
412 if (debug) {
413 Log::info() << "enforceXYDomain before " << xy[XX] << " " << xy[YY] << std::endl;
414 }
415
416 xy[XX] = std::max(xy[XX], 0.0);
417 xy[XX] = std::min(xy[XX], 360.0 - epsilon);
418 xy[YY] = std::max(xy[YY], -135.0 + epsilon);
419 xy[YY] = std::min(xy[YY], 135.0 - epsilon);
420 if (is_same(xy[XX], 90.0, tol)) {
421 xy[XX] = 90.0;
422 }
423 if (is_same(xy[XX], 180.0, tol)) {
424 xy[XX] = 180.0;
425 }
426 if (is_same(xy[XX], 270.0, tol)) {
427 xy[XX] = 270.0;
428 }
429 if (is_same(xy[YY], -45.0, tol) && (xy[XX] <= 180.0)) {
430 xy[YY] = -45.0;
431 }
432 if (is_same(xy[YY], 45.0, tol) && (xy[XX] >= 180.0)) {
433 xy[YY] = 45.0;
434 }
435 if (is_same(xy[YY], 45.0, tol) && is_same(xy[XX], 0.0, tol)) {
436 xy[XX] = 0.0;
437 xy[YY] = 45.0;
438 }
439
440 if (debug) {
441 Log::info() << "enforceXYDomain after " << xy[XX] << " " << xy[YY] << std::endl;
442 }
443 }
444
445 atlas::PointXY FV3CubedSphereTiles::tileCubePeriodicity(const atlas::PointXY& xyExtended,
446 const atlas::idx_t tile) const {
447 throw_NotImplemented("tileCubePeriodicity not implemented for FV3Tiles", Here());
448 }
449
450 void FV3CubedSphereTiles::print(std::ostream& os) const {
451 os << "FV3CubedSphereTiles"
452 << "]";
453 }
454
455
456 const PointXY& FV3CubedSphereTiles::tileCentre(size_t t) const {
457 throw_NotImplemented("tileCentre not implemented for FV3Tiles", Here());
458 }
459
460 const FV3CubedSphereTiles::Jacobian& FV3CubedSphereTiles::tileJacobian(size_t t) const {
461 throw_NotImplemented("tileJacobian not implemented for FV3Tiles", Here());
462 ;
463 }
464
465
466 namespace {
467 static CubedSphereTilesBuilder<FV3CubedSphereTiles> register_builder(FV3CubedSphereTiles::static_type());
468 }
469
470
471 } // namespace detail
472 } // namespace grid
473 } // namespace atlas