| 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 "atlas/projection/detail/ProjectionImpl.h" |
| 12 |
|
| 13 |
#include <algorithm> |
| 14 |
#include <cstddef> |
| 15 |
#include <cstring> |
| 16 |
#include <memory> |
| 17 |
#include <utility> |
| 18 |
|
| 19 |
#include "eckit/types/FloatCompare.h" |
| 20 |
#include "eckit/utils/Hash.h" |
| 21 |
#include "eckit/utils/MD5.h" |
| 22 |
|
| 23 |
#include "atlas/projection/detail/ProjectionFactory.h" |
| 24 |
#include "atlas/runtime/Exception.h" |
| 25 |
#include "atlas/util/Config.h" |
| 26 |
|
| 27 |
namespace atlas::projection::detail { |
| 28 |
|
| 29 |
// -------------------------------------------------------------------------------------------------------------------- |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
void longitude_in_range(double reference, double& lon) { |
| 34 |
// keep longitude difference (to reference) range below +-180 degree |
| 35 |
while (lon > reference + 180.) { |
| 36 |
lon -= 360.; |
| 37 |
} |
| 38 |
while (lon <= reference - 180.) { |
| 39 |
lon += 360.; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
template <class T> |
| 44 |
struct DerivateBuilder : public ProjectionImpl::DerivateFactory { |
| 45 |
using DerivateFactory::DerivateFactory; |
| 46 |
ProjectionImpl::Derivate* make(const ProjectionImpl& p, PointXY A, PointXY B, double h, |
| 47 |
double refLongitude) override { |
| 48 |
return new T(p, A, B, h, refLongitude); |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
struct DerivateForwards final : ProjectionImpl::Derivate { |
| 53 |
using Derivate::Derivate; |
| 54 |
PointLonLat d(PointXY P) const override { return (xy2lonlat(P + H_) - xy2lonlat(P)) * invnH_; } |
| 55 |
}; |
| 56 |
|
| 57 |
struct DerivateBackwards final : ProjectionImpl::Derivate { |
| 58 |
using Derivate::Derivate; |
| 59 |
PointLonLat d(PointXY P) const override { return (xy2lonlat(P) - xy2lonlat(P - H_)) * invnH_; } |
| 60 |
}; |
| 61 |
|
| 62 |
struct DerivateCentral final : ProjectionImpl::Derivate { |
| 63 |
DerivateCentral(const ProjectionImpl& p, PointXY A, PointXY B, double h, double refLongitude): |
| 64 |
Derivate(p, A, B, h, refLongitude), H2_{H_ * 0.5} {} |
| 65 |
const PointXY H2_; |
| 66 |
PointLonLat d(PointXY P) const override { return (xy2lonlat(P + H2_) - xy2lonlat(P - H2_)) * invnH_; } |
| 67 |
}; |
| 68 |
|
| 69 |
} // namespace |
| 70 |
|
| 71 |
ProjectionImpl::Derivate::Derivate(const ProjectionImpl& p, PointXY A, PointXY B, double h, double refLongitude): |
| 72 |
projection_(p), |
| 73 |
H_{PointXY::mul(PointXY::normalize(PointXY::sub(B, A)), h)}, |
| 74 |
invnH_(1. / PointXY::norm(H_)), |
| 75 |
refLongitude_(refLongitude) {} |
| 76 |
|
| 77 |
ProjectionImpl::Derivate::~Derivate() = default; |
| 78 |
|
| 79 |
ProjectionImpl::Derivate* ProjectionImpl::DerivateFactory::build(const std::string& type, const ProjectionImpl& p, |
| 80 |
PointXY A, PointXY B, double h, double refLongitude) { |
| 81 |
ATLAS_ASSERT(0. < h); |
| 82 |
|
| 83 |
// force_link |
| 84 |
static DerivateBuilder<DerivateForwards> __derivate1("forwards"); |
| 85 |
static DerivateBuilder<DerivateBackwards> __derivate2("backwards"); |
| 86 |
static DerivateBuilder<DerivateCentral> __derivate3("central"); |
| 87 |
|
| 88 |
if (A.distance2(B) < h * h) { |
| 89 |
struct DerivateDegenerate final : Derivate { |
| 90 |
using Derivate::Derivate; |
| 91 |
PointLonLat d(PointXY) const override { return {}; } |
| 92 |
}; |
| 93 |
return new DerivateDegenerate(p, A, B, h, refLongitude); |
| 94 |
} |
| 95 |
|
| 96 |
auto* factory = get(type); |
| 97 |
return factory->make(p, A, B, h); |
| 98 |
} |
| 99 |
|
| 100 |
ProjectionImpl::DerivateFactory::~DerivateFactory() = default; |
| 101 |
|
| 102 |
PointLonLat ProjectionImpl::Derivate::xy2lonlat(const PointXY& p) const { |
| 103 |
PointLonLat q(p); |
| 104 |
projection_.xy2lonlat(q.data()); |
| 105 |
longitude_in_range(refLongitude_, q.lon()); |
| 106 |
return q; |
| 107 |
} |
| 108 |
|
| 109 |
// -------------------------------------------------------------------------------------------------------------------- |
| 110 |
|
| 111 |
ProjectionImpl::BoundLonLat::operator RectangularLonLatDomain() const { |
| 112 |
return RectangularLonLatDomain({min_[0], max_[0]}, {min_[1], max_[1]}); |
| 113 |
} |
| 114 |
|
| 115 |
bool ProjectionImpl::BoundLonLat::crossesDateLine(bool yes) { |
| 116 |
if ((crossesDateLine_ = crossesDateLine_ || yes)) { |
| 117 |
max_.lon() = min_.lon() + 360.; |
| 118 |
} |
| 119 |
return crossesDateLine_; |
| 120 |
} |
| 121 |
|
| 122 |
bool ProjectionImpl::BoundLonLat::includesNorthPole(bool yes) { |
| 123 |
if ((includesNorthPole_ = includesNorthPole_ || yes)) { |
| 124 |
max_.lat() = 90.; |
| 125 |
} |
| 126 |
crossesDateLine(includesNorthPole_); |
| 127 |
return includesNorthPole_; |
| 128 |
} |
| 129 |
|
| 130 |
bool ProjectionImpl::BoundLonLat::includesSouthPole(bool yes) { |
| 131 |
if ((includesSouthPole_ = includesSouthPole_ || yes)) { |
| 132 |
min_.lat() = -90.; |
| 133 |
} |
| 134 |
crossesDateLine(includesSouthPole_); |
| 135 |
return includesSouthPole_; |
| 136 |
} |
| 137 |
|
| 138 |
void ProjectionImpl::BoundLonLat::extend(PointLonLat p, PointLonLat eps) { |
| 139 |
ATLAS_ASSERT(0. <= eps.lon() && 0. <= eps.lat()); |
| 140 |
|
| 141 |
auto sub = PointLonLat::sub(p, eps); |
| 142 |
auto add = PointLonLat::add(p, eps); |
| 143 |
min_ = first_ ? sub : PointLonLat::componentsMin(min_, sub); |
| 144 |
max_ = first_ ? add : PointLonLat::componentsMax(max_, add); |
| 145 |
first_ = false; |
| 146 |
|
| 147 |
min_.lat() = std::max(min_.lat(), -90.); |
| 148 |
max_.lat() = std::min(max_.lat(), 90.); |
| 149 |
max_.lon() = std::min(max_.lon(), min_.lon() + 360.); |
| 150 |
ATLAS_ASSERT(min_.lon() <= max_.lon() && min_.lat() <= max_.lat()); |
| 151 |
|
| 152 |
includesSouthPole(eckit::types::is_approximately_equal(min_.lat(), -90.)); |
| 153 |
includesNorthPole(eckit::types::is_approximately_equal(max_.lat(), 90.)); |
| 154 |
crossesDateLine(eckit::types::is_approximately_equal(max_.lon() - min_.lon(), 360.)); |
| 155 |
} |
| 156 |
|
| 157 |
// -------------------------------------------------------------------------------------------------------------------- |
| 158 |
|
| 159 |
ProjectionImpl::Normalise::Normalise(const eckit::Parametrisation& p) { |
| 160 |
values_.resize(2); |
| 161 |
bool provided = false; |
| 162 |
if (p.get("normalise", values_)) { |
| 163 |
provided = true; |
| 164 |
} |
| 165 |
else if (p.get("normalize", values_)) { |
| 166 |
provided = true; |
| 167 |
} |
| 168 |
else if (p.get("west", values_[0])) { |
| 169 |
values_[1] = values_[0] + 360.; |
| 170 |
provided = true; |
| 171 |
} |
| 172 |
if (provided) { |
| 173 |
normalise_ = std::make_unique<util::NormaliseLongitude>(values_[0], values_[1]); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
ProjectionImpl::Normalise::Normalise(double west) { |
| 178 |
values_.resize(2); |
| 179 |
values_[0] = west; |
| 180 |
values_[1] = values_[0] + 360.; |
| 181 |
normalise_ = std::make_unique<util::NormaliseLongitude>(values_[0], values_[1]); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
void ProjectionImpl::Normalise::hash(eckit::Hash& hash) const { |
| 186 |
if (normalise_) { |
| 187 |
hash.add(values_[0]); |
| 188 |
hash.add(values_[1]); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void ProjectionImpl::Normalise::spec(Spec& s) const { |
| 193 |
if (normalise_) { |
| 194 |
s.set("normalise", values_); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
// -------------------------------------------------------------------------------------------------------------------- |
| 200 |
|
| 201 |
const ProjectionImpl* ProjectionImpl::create(const eckit::Parametrisation& p) { |
| 202 |
std::string projectionType; |
| 203 |
if (p.get("type", projectionType)) { |
| 204 |
return ProjectionFactory::build(projectionType, p); |
| 205 |
} |
| 206 |
|
| 207 |
// should return error here |
| 208 |
throw_Exception("type missing in Params", Here()); |
| 209 |
} |
| 210 |
|
| 211 |
const ProjectionImpl* ProjectionImpl::create(const std::string& type, const eckit::Parametrisation& p) { |
| 212 |
return ProjectionFactory::build(type, p); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
PointXYZ ProjectionImpl::xyz(const PointLonLat& lonlat) const { |
| 217 |
atlas::PointXYZ xyz; |
| 218 |
atlas::util::Earth::convertSphericalToCartesian(lonlat, xyz); |
| 219 |
return xyz; |
| 220 |
} |
| 221 |
|
| 222 |
RectangularLonLatDomain ProjectionImpl::lonlatBoundingBox(const Domain& domain) const { |
| 223 |
using eckit::types::is_strictly_greater; |
| 224 |
|
| 225 |
|
| 226 |
// 0. setup |
| 227 |
|
| 228 |
if (domain.global()) { |
| 229 |
return domain; |
| 230 |
} |
| 231 |
RectangularDomain rect(domain); |
| 232 |
ATLAS_ASSERT(rect); |
| 233 |
|
| 234 |
// use central longitude as absolute reference (keep points within +-180 longitude range) |
| 235 |
const PointXY centre_xy{(rect.xmin() + rect.xmax()) / 2., (rect.ymin() + rect.ymax()) / 2.}; |
| 236 |
const auto centre_lon = lonlat(centre_xy).lon(); |
| 237 |
|
| 238 |
const std::string derivative = "central"; |
| 239 |
constexpr double h_deg = 0.5e-6; // precision to microdegrees |
| 240 |
constexpr double h_meters = 0.5e-1; // precision to decimeters |
| 241 |
constexpr size_t Niter = 100; |
| 242 |
|
| 243 |
const double h = units() == "degrees" ? h_deg : h_meters; |
| 244 |
|
| 245 |
|
| 246 |
// 1. determine box from projected corners |
| 247 |
|
| 248 |
const std::pair<PointXY, PointXY> segments[] = {{{rect.xmin(), rect.ymax()}, {rect.xmax(), rect.ymax()}}, |
| 249 |
{{rect.xmax(), rect.ymax()}, {rect.xmax(), rect.ymin()}}, |
| 250 |
{{rect.xmax(), rect.ymin()}, {rect.xmin(), rect.ymin()}}, |
| 251 |
{{rect.xmin(), rect.ymin()}, {rect.xmin(), rect.ymax()}}}; |
| 252 |
|
| 253 |
BoundLonLat bounds; |
| 254 |
for (const auto& [p, dummy] : segments) { |
| 255 |
auto q = lonlat(p); |
| 256 |
longitude_in_range(centre_lon, q.lon()); |
| 257 |
bounds.extend(q, PointLonLat{h_deg, h_deg}); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
// 2. locate latitude extrema by checking if poles are included (in the un-projected frame) and if not, find extrema |
| 262 |
// not at the corners by refining iteratively |
| 263 |
|
| 264 |
bounds.includesNorthPole(bounds.includesNorthPole() || rect.contains(xy(PointLonLat{0, 90 - h_deg}))); |
| 265 |
bounds.includesSouthPole(bounds.includesSouthPole() || rect.contains(xy(PointLonLat{0, -90 + h_deg}))); |
| 266 |
|
| 267 |
for (auto [A, B] : segments) { |
| 268 |
if (!bounds.includesNorthPole() || !bounds.includesSouthPole()) { |
| 269 |
std::unique_ptr<Derivate> derivate(DerivateFactory::build(derivative, *this, A, B, h, centre_lon)); |
| 270 |
double dAdy = derivate->d(A).lat(); |
| 271 |
double dBdy = derivate->d(B).lat(); |
| 272 |
|
| 273 |
if (!is_strictly_greater(dAdy * dBdy, 0.)) { |
| 274 |
for (size_t cnt = 0; cnt < Niter; ++cnt) { |
| 275 |
PointXY M = PointXY::middle(A, B); |
| 276 |
double dMdy = derivate->d(M).lat(); |
| 277 |
if (is_strictly_greater(dAdy * dMdy, 0.)) { |
| 278 |
A = M; |
| 279 |
dAdy = dMdy; |
| 280 |
} |
| 281 |
else if (is_strictly_greater(dBdy * dMdy, 0.)) { |
| 282 |
B = M; |
| 283 |
dBdy = dMdy; |
| 284 |
} |
| 285 |
else { |
| 286 |
break; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
// update extrema, extended by 'a small amount' (arbitrary) |
| 291 |
bounds.extend(lonlat(PointXY::middle(A, B)), PointLonLat{0, h_deg}); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
// 3. locate longitude extrema not at the corners by refining iteratively |
| 298 |
|
| 299 |
for (auto [A, B] : segments) { |
| 300 |
if (!bounds.crossesDateLine()) { |
| 301 |
std::unique_ptr<Derivate> derivate(DerivateFactory::build(derivative, *this, A, B, h, centre_lon)); |
| 302 |
double dAdx = derivate->d(A).lon(); |
| 303 |
double dBdx = derivate->d(B).lon(); |
| 304 |
|
| 305 |
if (!is_strictly_greater(dAdx * dBdx, 0.)) { |
| 306 |
for (size_t cnt = 0; cnt < Niter; ++cnt) { |
| 307 |
PointXY M = PointXY::middle(A, B); |
| 308 |
double dMdx = derivate->d(M).lon(); |
| 309 |
if (is_strictly_greater(dAdx * dMdx, 0.)) { |
| 310 |
A = M; |
| 311 |
dAdx = dMdx; |
| 312 |
} |
| 313 |
else if (is_strictly_greater(dBdx * dMdx, 0.)) { |
| 314 |
B = M; |
| 315 |
dBdx = dMdx; |
| 316 |
} |
| 317 |
else { |
| 318 |
break; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
// update extrema, extended by 'a small amount' (arbitrary) |
| 323 |
bounds.extend(lonlat(PointXY::middle(A, B)), PointLonLat{h_deg, 0}); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
// 4. return bounding box |
| 330 |
return bounds; |
| 331 |
} |
| 332 |
|
| 333 |
//--------------------------------------------------------------------------------------------------------------------- |
| 334 |
|
| 335 |
Rotated::Rotated(const PointLonLat& south_pole, double rotation_angle): util::Rotation(south_pole, rotation_angle) {} |
| 336 |
|
| 337 |
Rotated::Rotated(const eckit::Parametrisation& p): util::Rotation(p) {} |
| 338 |
|
| 339 |
void Rotated::spec(Spec& s) const { |
| 340 |
std::vector<double> npole{northPole().lon(), northPole().lat()}; |
| 341 |
std::vector<double> spole{southPole().lon(), southPole().lat()}; |
| 342 |
s.set("north_pole", npole); |
| 343 |
s.set("south_pole", spole); |
| 344 |
s.set("rotation_angle", rotationAngle()); |
| 345 |
} |
| 346 |
|
| 347 |
void Rotated::hash(eckit::Hash& hsh) const { |
| 348 |
hsh.add("rotated"); |
| 349 |
hsh.add(southPole().lon()); |
| 350 |
hsh.add(southPole().lat()); |
| 351 |
hsh.add(rotationAngle()); |
| 352 |
} |
| 353 |
|
| 354 |
//--------------------------------------------------------------------------------------------------------------------- |
| 355 |
|
| 356 |
extern "C" { |
| 357 |
const ProjectionImpl* atlas__Projection__ctor_config(const eckit::Parametrisation* config) { |
| 358 |
return ProjectionImpl::create(*config); |
| 359 |
} |
| 360 |
void atlas__Projection__type(const ProjectionImpl* This, char*& type, int& size) { |
| 361 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Projection"); |
| 362 |
std::string s = This->type(); |
| 363 |
size = static_cast<int>(s.size()); |
| 364 |
type = new char[size + 1]; |
| 365 |
std::strncpy(type, s.c_str(), size + 1); |
| 366 |
} |
| 367 |
void atlas__Projection__hash(const ProjectionImpl* This, char*& hash, int& size) { |
| 368 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Projection"); |
| 369 |
eckit::MD5 md5; |
| 370 |
This->hash(md5); |
| 371 |
std::string s = md5.digest(); |
| 372 |
size = static_cast<int>(s.size()); |
| 373 |
hash = new char[size + 1]; |
| 374 |
std::strncpy(hash, s.c_str(), size + 1); |
| 375 |
} |
| 376 |
ProjectionImpl::Spec* atlas__Projection__spec(const ProjectionImpl* This) { |
| 377 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Projection"); |
| 378 |
return new ProjectionImpl::Spec(This->spec()); |
| 379 |
} |
| 380 |
void atlas__Projection__xy2lonlat(const ProjectionImpl* This, const double x, const double y, double& lon, |
| 381 |
double& lat) { |
| 382 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Projection"); |
| 383 |
Point2 p(x, y); |
| 384 |
This->xy2lonlat(p); |
| 385 |
lon = p.x(); |
| 386 |
lat = p.y(); |
| 387 |
} |
| 388 |
void atlas__Projection__lonlat2xy(const ProjectionImpl* This, const double lon, const double lat, double& x, |
| 389 |
double& y) { |
| 390 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Projection"); |
| 391 |
Point2 p(lon, lat); |
| 392 |
This->lonlat2xy(p); |
| 393 |
x = p.x(); |
| 394 |
y = p.y(); |
| 395 |
} |
| 396 |
|
| 397 |
} // extern "C" |
| 398 |
|
| 399 |
} // namespace atlas::projection::detail |