Debian Codegraph @

src/atlas/projection/detail/CubedSphereProjectionBase.cc

raw
1 /*
2 * (C) Copyright 2020 UCAR
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 #include "CubedSphereProjectionBase.h"
9
10 #include <array>
11 #include <cmath>
12 #include <functional>
13 #include <iomanip>
14 #include <iostream>
15 #include <limits>
16
17 #include "atlas/projection/detail/ProjectionUtilities.h"
18 #include "atlas/runtime/Exception.h"
19 #include "atlas/runtime/Log.h"
20 #include "atlas/runtime/Trace.h"
21 #include "atlas/util/Config.h"
22 #include "atlas/util/Constants.h"
23 #include "atlas/util/CoordinateEnums.h"
24
25 namespace atlas {
26 namespace projection {
27 namespace detail {
28
29 // -------------------------------------------------------------------------------------------------
30 // Helper functions and variables local to this translation unit
31 namespace {
32
33 static constexpr double deg2rad = util::Constants::degreesToRadians();
34
35 static void schmidtTransform(double stretchFac, double targetLon, double targetLat, double lonlat[]) {
36 double c2p1 = 1.0 + stretchFac * stretchFac;
37 double c2m1 = 1.0 - stretchFac * stretchFac;
38
39 double sin_p = std::sin(targetLat * deg2rad);
40 double cos_p = std::cos(targetLat * deg2rad);
41
42 double sin_lat;
43 double cos_lat;
44 double lat_t;
45
46 if (std::abs(c2m1) > 1.0e-7) {
47 sin_lat = std::sin(lonlat[LAT] * deg2rad);
48 lat_t = std::asin((c2m1 + c2p1 * sin_lat) / (c2p1 + c2m1 * sin_lat));
49 }
50 else { // no stretching
51 lat_t = lonlat[LAT];
52 }
53
54 sin_lat = std::sin(lat_t);
55 cos_lat = std::cos(lat_t);
56 double sin_o = -(sin_p * sin_lat + cos_p * cos_lat * cos(lonlat[LON] * deg2rad));
57
58 if ((1. - std::abs(sin_o)) < 1.0e-7) { // poles
59 lonlat[LON] = 0.0;
60 lonlat[LAT] = std::copysign(90.0, sin_o);
61 }
62 else {
63 lonlat[LAT] = std::asin(sin_o);
64 lonlat[LON] = targetLon + atan2(-cos_lat * std::sin(lonlat[LON] * deg2rad),
65 -sin_lat * cos_p + cos_lat * sin_p * std::cos(lonlat[LON] * deg2rad));
66 if (lonlat[LON] < 0.0) {
67 lonlat[LON] += 360.;
68 }
69 else if (lonlat[LON] >= 360.) {
70 lonlat[LON] -= 360.;
71 }
72 }
73 }
74
75 void sphericalToCartesian(const double lonlat[], double xyz[]) {
76 auto crd_sys = ProjectionUtilities::CoordinateSystem::LEFT_HAND;
77 constexpr double radius = 1.;
78 ProjectionUtilities::sphericalToCartesian(lonlat, xyz, crd_sys, radius);
79 }
80
81 void cartesianToSpherical(const double xyz[], double lonlat[]) {
82 auto crd_sys = ProjectionUtilities::CoordinateSystem::LEFT_HAND;
83 constexpr double radius = 0.; // --> equivalent to radius = norm(xyz)
84 ProjectionUtilities::cartesianToSpherical(xyz, lonlat, crd_sys, radius);
85 }
86
87 std::string getTileType(const eckit::Parametrisation& params) {
88 std::string tileStr;
89 params.get("tile.type", tileStr);
90 return tileStr;
91 }
92
93 } // namespace
94
95
96 // -------------------------------------------------------------------------------------------------
97
98 CubedSphereProjectionBase::CubedSphereProjectionBase(const eckit::Parametrisation& params):
99 tiles_(getTileType(params)),
100 tiles_offsets_ab2xy_(tiles_.ab2xyOffsets()),
101 tiles_offsets_xy2ab_(tiles_.xy2abOffsets()) {
102 ATLAS_TRACE("CubedSphereProjectionBase::CubedSphereProjectionBase");
103
104 // Shift projection by a longitude
105 shiftLon_ = 0.0;
106 if (params.has("ShiftLon")) {
107 params.get("ShiftLon", shiftLon_);
108 ATLAS_ASSERT(shiftLon_ <= 90.0, "ShiftLon should be <= 90.0 degrees");
109 ATLAS_ASSERT(shiftLon_ >= -90.0, "ShiftLon should be >= -90.0 degrees");
110 }
111
112 // Apply a Schmidt transform
113 doSchmidt_ = false;
114 stretchFac_ = 0.0;
115 targetLon_ = 0.0;
116 targetLat_ = 0.0;
117 if (params.has("DoSchmidt")) {
118 params.get("DoSchmidt", doSchmidt_);
119 if (doSchmidt_) {
120 params.get("StretchFac", stretchFac_);
121 params.get("TargetLon", targetLon_);
122 params.get("TargetLat", targetLat_);
123 }
124 }
125 }
126
127 // -------------------------------------------------------------------------------------------------
128
129 void CubedSphereProjectionBase::hash(eckit::Hash& h) const {
130 // Add stretching options to hash
131 h.add(shiftLon_);
132 h.add(doSchmidt_);
133 h.add(stretchFac_);
134 h.add(targetLon_);
135 h.add(targetLat_);
136 }
137
138 // -------------------------------------------------------------------------------------------------
139
140 void CubedSphereProjectionBase::xy2lonlat_post(double xyz[], const idx_t t, double crd[]) const {
141 cartesianToSpherical(xyz, crd);
142
143 if (crd[LON] < 0.) {
144 crd[LON] += 360.;
145 }
146 crd[LON] -= 180.;
147
148 // Convert to cartesian
149 sphericalToCartesian(crd, xyz);
150
151 // Perform tile specific rotation
152 tiles_.rotate(t, xyz);
153
154 // Back to lonlat
155 cartesianToSpherical(xyz, crd);
156
157 // Shift longitude
158 if (shiftLon_ != 0.0) {
159 crd[LON] = crd[LON] + shiftLon_;
160 if (crd[LON] < -180.) {
161 crd[LON] = 360. + crd[LON];
162 }
163 if (crd[LON] > 180.) {
164 crd[LON] = -360. + crd[LON];
165 }
166 }
167
168 // To 0, 360
169 if (crd[LON] < 0.0) {
170 crd[LON] += 360.;
171 }
172
173 // Schmidt transform
174 if (doSchmidt_) {
175 schmidtTransform(stretchFac_, targetLon_, targetLat_, crd);
176 }
177
178 // longitude does not make sense at the poles - set to 0.
179 if (std::abs(std::abs(crd[LAT]) - 90.) < 1e-15) {
180 crd[LON] = 0.;
181 }
182 }
183
184 // -------------------------------------------------------------------------------------------------
185
186 void CubedSphereProjectionBase::lonlat2xy_pre(double crd[], idx_t& t, double xyz[]) const {
187 if (std::abs(crd[LON]) < 1e-15) {
188 crd[LON] = 0.;
189 }
190 if (std::abs(crd[LAT]) < 1e-15) {
191 crd[LAT] = 0.;
192 }
193
194 // To [-45.0, 315)
195 if (crd[LON] >= 315.0) {
196 crd[LON] -= 360.;
197 }
198
199 // find tile which this lonlat is linked to
200 // works [-45, 315.0)
201 t = tiles_.indexFromLonLat(crd);
202
203 sphericalToCartesian(crd, xyz);
204 tiles_.unrotate(t, xyz);
205 }
206
207 // -------------------------------------------------------------------------------------------------
208
209
210 void CubedSphereProjectionBase::xy2alphabetat(const double xy[], idx_t& t, double ab[]) const {
211 // xy is in degrees while ab is in degree
212 // ab are the (alpha, beta) coordinates and t is the tile index.
213
214 t = tiles_.indexFromXY(xy);
215 double normalisedX = xy[XX] / 90.;
216 double normalisedY = (xy[YY] + 135.) / 90.;
217 ab[LON] = (normalisedX - tiles_offsets_xy2ab_[XX][size_t(t)]) * 90.0 - 45.0;
218 ab[LAT] = (normalisedY - tiles_offsets_xy2ab_[YY][size_t(t)]) * 90.0 - 45.0;
219 }
220
221 // -------------------------------------------------------------------------------------------------
222
223 void CubedSphereProjectionBase::alphabetat2xy(const idx_t t, const double ab[], double xy[]) const {
224 // xy and ab are in degrees
225 // (alpha, beta) and tiles.
226
227 xy[XX] = ab[LON] + 45.0 + tiles_offsets_ab2xy_[LON][size_t(t)];
228 xy[YY] = ab[LAT] + 45.0 + tiles_offsets_ab2xy_[LAT][size_t(t)];
229
230 tiles_.enforceXYdomain(xy);
231 }
232
233 // -------------------------------------------------------------------------------------------------
234
235 Point2 CubedSphereProjectionBase::xy2alphabeta(const Point2& xy, idx_t t) const {
236 auto alphabeta = Point2(xy);
237 xy2alphabeta(alphabeta.data(), t);
238 return alphabeta;
239 }
240
241 // -------------------------------------------------------------------------------------------------
242
243 Point2 CubedSphereProjectionBase::alphabeta2xy(const Point2& alphabeta, idx_t t) const {
244 auto xy = Point2(alphabeta);
245 alphabeta2xy(xy.data(), t);
246 return xy;
247 }
248
249 // -------------------------------------------------------------------------------------------------
250
251 Point2 CubedSphereProjectionBase::lonlat2alphabeta(const Point2& lonlat, idx_t t) const {
252 auto alphabeta = Point2(lonlat);
253 lonlat2alphabeta(alphabeta.data(), t);
254 return alphabeta;
255 }
256
257 // -------------------------------------------------------------------------------------------------
258
259 Point2 CubedSphereProjectionBase::alphabeta2lonlat(const Point2& alphabeta, idx_t t) const {
260 auto lonlat = Point2(alphabeta);
261 alphabeta2lonlat(lonlat.data(), t);
262 return lonlat;
263 }
264
265 // -------------------------------------------------------------------------------------------------
266
267 } // namespace detail
268 } // namespace projection
269 } // namespace atlas