Debian Codegraph @

src/atlas/projection/detail/LonLatProjection.cc

raw
1 /*
2 * (C) Copyright 2013 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 * 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 <memory>
12
13 #include "eckit/geometry/GreatCircle.h"
14 #include "eckit/types/FloatCompare.h"
15 #include "eckit/utils/Hash.h"
16
17 #include "atlas/domain.h"
18 #include "atlas/projection/detail/LonLatProjection.h"
19 #include "atlas/projection/detail/ProjectionFactory.h"
20 #include "atlas/runtime/Exception.h"
21 #include "atlas/runtime/Log.h"
22 #include "atlas/util/Config.h"
23
24 namespace atlas {
25 namespace projection {
26 namespace detail {
27
28 template <typename Rotation>
29 LonLatProjectionT<Rotation>::LonLatProjectionT(const eckit::Parametrisation& config):
30 ProjectionImpl(), rotation_(config) {}
31
32 template <>
33 void LonLatProjectionT<NotRotated>::xy2lonlat(double[]) const {}
34
35 template <>
36 void LonLatProjectionT<NotRotated>::lonlat2xy(double[]) const {}
37
38 template <>
39 ProjectionImpl::Jacobian LonLatProjectionT<NotRotated>::jacobian(const PointLonLat&) const {
40 Jacobian jac;
41 jac[0] = {1.0, 0.0};
42 jac[1] = {0.0, 1.0};
43 return jac;
44 }
45
46 template <typename Rotation>
47 ProjectionImpl::Jacobian LonLatProjectionT<Rotation>::jacobian(const PointLonLat&) const {
48 throw_NotImplemented("LonLatProjectionT::jacobian", Here());
49 }
50
51 template <>
52 RectangularLonLatDomain LonLatProjectionT<NotRotated>::lonlatBoundingBox(const Domain& domain) const {
53 return domain;
54 }
55
56 template <typename Rotation>
57 RectangularLonLatDomain LonLatProjectionT<Rotation>::lonlatBoundingBox(const Domain& domain) const {
58 using eckit::types::is_strictly_greater;
59
60
61 // 0. setup
62
63 if (domain.global()) {
64 return domain;
65 }
66 RectangularDomain rect(domain);
67 ATLAS_ASSERT(rect);
68
69
70 const std::string derivative = "central";
71 constexpr double h = 0.001; // precision to millidegrees
72 constexpr size_t Niter = 100;
73
74
75 // 1. determine box from projected corners
76
77 const std::vector<PointXY> corners{
78 {rect.xmin(), rect.ymax()}, {rect.xmax(), rect.ymax()}, {rect.xmax(), rect.ymin()}, {rect.xmin(), rect.ymin()}};
79
80 BoundLonLat bounds;
81 for (auto& p : corners) {
82 bounds.extend(lonlat(p), PointLonLat{h, h});
83 }
84
85
86 // 2. locate latitude extrema by checking if poles are included (in the un-projected frame) and if not, find extrema
87 // not at the corners by refining iteratively
88
89 PointXY NP{xy({0., 90.})};
90 PointXY SP{xy({0., -90.})};
91
92 bounds.includesNorthPole(rect.contains(NP));
93 bounds.includesSouthPole(rect.contains(SP));
94
95 for (size_t i = 0; i < corners.size(); ++i) {
96 if (!bounds.includesNorthPole() || !bounds.includesSouthPole()) {
97 PointXY A = corners[i];
98 PointXY B = corners[(i + 1) % corners.size()];
99
100 std::unique_ptr<Derivate> derivate(DerivateFactory::build(derivative, *this, A, B, h));
101 double dAdy = derivate->d(A).lat();
102 double dBdy = derivate->d(B).lat();
103
104 if (!is_strictly_greater(dAdy * dBdy, 0.)) {
105 for (size_t cnt = 0; cnt < Niter; ++cnt) {
106 PointXY M = PointXY::middle(A, B);
107 double dMdy = derivate->d(M).lat();
108 if (is_strictly_greater(dAdy * dMdy, 0.)) {
109 A = M;
110 dAdy = dMdy;
111 }
112 else if (is_strictly_greater(dBdy * dMdy, 0.)) {
113 B = M;
114 dBdy = dMdy;
115 }
116 else {
117 break;
118 }
119 }
120
121 // update extrema, extended by 'a small amount' (arbitrary)
122 bounds.extend(lonlat(PointXY::middle(A, B)), PointLonLat{0, h});
123 }
124 }
125 }
126
127
128 // 3. locate longitude extrema by checking if date line is crossed (in the un-projected frame), in which case we
129 // assume periodicity and if not, find extrema not at the corners by refining iteratively
130
131 if (!bounds.crossesDateLine()) {
132 PointLonLat A{xy({180., -10.})};
133 PointLonLat B{xy({180., 10.})};
134 eckit::geometry::GreatCircle DL(A, B);
135
136 for (auto lon : {rect.xmin(), rect.xmax()}) {
137 if (!bounds.crossesDateLine()) {
138 for (auto lat : DL.latitude(lon)) {
139 if ((bounds.crossesDateLine(domain.contains(lon, lat)))) {
140 break;
141 }
142 }
143 }
144 }
145
146 for (auto lat : {rect.ymin(), rect.ymax()}) {
147 if (!bounds.crossesDateLine()) {
148 for (auto lon : DL.longitude(lat)) {
149 if ((bounds.crossesDateLine(domain.contains(lon, lat)))) {
150 break;
151 }
152 }
153 }
154 }
155 }
156
157 for (size_t i = 0; i < corners.size(); ++i) {
158 if (!bounds.crossesDateLine()) {
159 PointXY A = corners[i];
160 PointXY B = corners[(i + 1) % corners.size()];
161
162 std::unique_ptr<Derivate> derivate(DerivateFactory::build(derivative, *this, A, B, h));
163 double dAdx = derivate->d(A).lon();
164 double dBdx = derivate->d(B).lon();
165
166 if (!is_strictly_greater(dAdx * dBdx, 0.)) {
167 for (size_t cnt = 0; cnt < Niter; ++cnt) {
168 PointXY M = PointXY::middle(A, B);
169 double dMdx = derivate->d(M).lon();
170 if (is_strictly_greater(dAdx * dMdx, 0.)) {
171 A = M;
172 dAdx = dMdx;
173 }
174 else if (is_strictly_greater(dBdx * dMdx, 0.)) {
175 B = M;
176 dBdx = dMdx;
177 }
178 else {
179 break;
180 }
181 }
182
183 // update extrema, extended by 'a small amount' (arbitrary)
184 bounds.extend(lonlat(PointXY::middle(A, B)), PointLonLat{h, 0});
185 }
186 }
187 }
188
189
190 // 4. return bounding box
191 return bounds;
192 }
193
194 template <typename Rotation>
195 typename LonLatProjectionT<Rotation>::Spec LonLatProjectionT<Rotation>::spec() const {
196 Spec proj_spec;
197 proj_spec.set("type", static_type());
198 rotation_.spec(proj_spec);
199 return proj_spec;
200 }
201
202 template <typename Rotation>
203 void LonLatProjectionT<Rotation>::hash(eckit::Hash& hsh) const {
204 hsh.add(static_type());
205 rotation_.hash(hsh);
206 }
207
208 template class LonLatProjectionT<NotRotated>;
209 template class LonLatProjectionT<Rotated>;
210
211 namespace {
212 static ProjectionBuilder<LonLatProjection> register_1(LonLatProjection::static_type());
213 static ProjectionBuilder<RotatedLonLatProjection> register_2(RotatedLonLatProjection::static_type());
214 } // namespace
215
216 // namespace
217
218 } // namespace detail
219 } // namespace projection
220 } // namespace atlas