| 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 "CubedSphereEquiAnglProjection.h" |
| 9 |
|
| 10 |
#include <cmath> |
| 11 |
#include <iostream> |
| 12 |
|
| 13 |
#include "eckit/config/Parametrisation.h" |
| 14 |
#include "eckit/types/FloatCompare.h" |
| 15 |
#include "eckit/utils/Hash.h" |
| 16 |
|
| 17 |
#include "atlas/projection/detail/ProjectionFactory.h" |
| 18 |
#include "atlas/projection/detail/ProjectionUtilities.h" |
| 19 |
#include "atlas/runtime/Exception.h" |
| 20 |
#include "atlas/runtime/Log.h" |
| 21 |
#include "atlas/util/Config.h" |
| 22 |
#include "atlas/util/Constants.h" |
| 23 |
#include "atlas/util/CoordinateEnums.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
namespace atlas { |
| 28 |
namespace projection { |
| 29 |
namespace detail { |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
static constexpr bool debug = false; // constexpr so compiler can optimize `if ( debug ) { ... }` out |
| 34 |
static constexpr double deg2rad = atlas::util::Constants::degreesToRadians(); |
| 35 |
static constexpr double rad2deg = atlas::util::Constants::radiansToDegrees(); |
| 36 |
|
| 37 |
// Define small number relative to 360. |
| 38 |
constexpr double epsilon = std::numeric_limits<double>::epsilon() * 360.; |
| 39 |
|
| 40 |
// Define some "fuzzy" comparison operators. |
| 41 |
|
| 42 |
// a is approximately equal to b. |
| 43 |
bool equal(double a, double b) { |
| 44 |
return std::abs(a - b) <= epsilon; |
| 45 |
} |
| 46 |
// a is less than b. |
| 47 |
bool lessThan(double a, double b) { |
| 48 |
return a < b && !equal(a, b); |
| 49 |
} |
| 50 |
// a is greater than b. |
| 51 |
bool greaterThan(double a, double b) { |
| 52 |
return a > b && !equal(a, b); |
| 53 |
} |
| 54 |
// a is less than or approximately equal to b. |
| 55 |
bool lessEqual(double a, double b) { |
| 56 |
return a < b || equal(a, b); |
| 57 |
} |
| 58 |
// a is greater than or approximately equal to b. |
| 59 |
bool greaterEqual(double a, double b) { |
| 60 |
return a > b || equal(a, b); |
| 61 |
} |
| 62 |
|
| 63 |
// Sine/cosine helper struct. |
| 64 |
struct SineCosine { |
| 65 |
double sinLambda{}; |
| 66 |
double cosLambda{}; |
| 67 |
double sinPhi{}; |
| 68 |
double cosPhi{}; |
| 69 |
explicit SineCosine(const PointLonLat& lonlat) { |
| 70 |
const double lambda = lonlat.lon() * deg2rad; |
| 71 |
const double phi = lonlat.lat() * deg2rad; |
| 72 |
sinLambda = std::sin(lambda); |
| 73 |
cosLambda = std::cos(lambda); |
| 74 |
sinPhi = std::sin(phi); |
| 75 |
cosPhi = std::sqrt(1. - sinPhi * sinPhi); // Safe, as phi is between +/- 90 degrees |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
} // namespace |
| 80 |
|
| 81 |
// ------------------------------------------------------------------------------------------------- |
| 82 |
|
| 83 |
CubedSphereEquiAnglProjection::CubedSphereEquiAnglProjection(const eckit::Parametrisation& params): |
| 84 |
CubedSphereProjectionBase(params) {} |
| 85 |
|
| 86 |
// ------------------------------------------------------------------------------------------------- |
| 87 |
|
| 88 |
void CubedSphereEquiAnglProjection::xy2alphabeta(double crd[], idx_t t) const { |
| 89 |
// Get tile centre. |
| 90 |
const auto& xyCentre = getCubedSphereTiles().tileCentre(static_cast<size_t>(t)); |
| 91 |
|
| 92 |
// Check that xy coordinate is within valid "+" shaped halo region. |
| 93 |
const auto inCross = [&](const double crd[]) -> bool { |
| 94 |
return (greaterEqual(crd[XX], xyCentre[XX] - 45.) && lessEqual(crd[XX], xyCentre[XX] + 45.)) || |
| 95 |
(greaterEqual(crd[YY], xyCentre[YY] - 45.) && lessEqual(crd[YY], xyCentre[YY] + 45.)); |
| 96 |
}; |
| 97 |
if (!inCross(crd)) { |
| 98 |
std::stringstream sStream; |
| 99 |
sStream << "xy coordinate (" << crd[0] << ", " << crd[1] << ") is not in range for tile " << t << "."; |
| 100 |
ATLAS_THROW_EXCEPTION(sStream.str()); |
| 101 |
} |
| 102 |
|
| 103 |
// Get alphaBeta Jacobian. |
| 104 |
const auto abJacobian = getCubedSphereTiles().tileJacobian(static_cast<size_t>(t)).inverse(); |
| 105 |
|
| 106 |
// Set (alpha, beta) coord. |
| 107 |
const Point2 alphabeta = abJacobian * (Point2(crd) - xyCentre); |
| 108 |
crd[0] = alphabeta[0]; |
| 109 |
crd[1] = alphabeta[1]; |
| 110 |
|
| 111 |
// Define correction. |
| 112 |
const auto correction = [](const double crd[]) -> double { |
| 113 |
return rad2deg * std::atan(std::tan(crd[0] * deg2rad) * std::tan(crd[1] * deg2rad)); |
| 114 |
}; |
| 115 |
|
| 116 |
// Correct halo (alpha, beta) coord. |
| 117 |
if (lessThan(crd[0], -45.)) { |
| 118 |
// Left. |
| 119 |
crd[1] = -correction(crd); |
| 120 |
} |
| 121 |
else if (greaterThan(crd[0], 45.)) { |
| 122 |
// Right. |
| 123 |
crd[1] = correction(crd); |
| 124 |
} |
| 125 |
else if (lessThan(crd[1], -45.)) { |
| 126 |
// Bottom. |
| 127 |
crd[0] = -correction(crd); |
| 128 |
} |
| 129 |
else if (greaterThan(crd[1], 45.)) { |
| 130 |
// Top. |
| 131 |
crd[0] = correction(crd); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// ------------------------------------------------------------------------------------------------- |
| 136 |
|
| 137 |
void CubedSphereEquiAnglProjection::alphabeta2xy(double crd[], idx_t t) const { |
| 138 |
// Define correction. |
| 139 |
const auto correction1 = [](const double crd[]) -> double { |
| 140 |
return rad2deg * std::atan(std::tan(crd[1] * deg2rad) / std::tan(crd[0] * deg2rad)); |
| 141 |
}; |
| 142 |
const auto correction2 = [](const double crd[]) -> double { |
| 143 |
return rad2deg * std::atan(std::tan(crd[0] * deg2rad) / std::tan(crd[1] * deg2rad)); |
| 144 |
}; |
| 145 |
|
| 146 |
// Correct halo (alpha, beta) coord. |
| 147 |
if (lessThan(crd[0], -45.) && greaterThan(crd[1], crd[0]) && lessEqual(crd[1], -crd[0])) { |
| 148 |
// Left trapezium. |
| 149 |
crd[1] = -correction1(crd); |
| 150 |
} |
| 151 |
else if (greaterThan(crd[0], 45.) && greaterEqual(crd[1], -crd[0]) && lessThan(crd[1], crd[0])) { |
| 152 |
// Right trapezium. |
| 153 |
crd[1] = correction1(crd); |
| 154 |
} |
| 155 |
else if (lessThan(crd[1], -45.) && greaterEqual(crd[0], crd[1]) && lessThan(crd[0], -crd[1])) { |
| 156 |
// Bottom trapezium. |
| 157 |
crd[0] = -correction2(crd); |
| 158 |
} |
| 159 |
else if (greaterThan(crd[1], 45.) && greaterThan(crd[0], -crd[1]) && lessEqual(crd[0], crd[1])) { |
| 160 |
// Top trapezium. |
| 161 |
crd[0] = correction2(crd); |
| 162 |
} |
| 163 |
|
| 164 |
// Get tile centre. |
| 165 |
const auto& xyCentre = getCubedSphereTiles().tileCentre(static_cast<size_t>(t)); |
| 166 |
|
| 167 |
// Get xy Jacobian. |
| 168 |
const auto xyJacobian = getCubedSphereTiles().tileJacobian(static_cast<size_t>(t)); |
| 169 |
|
| 170 |
// Set xy coord. |
| 171 |
const Point2 xy = xyJacobian * Point2(crd) + xyCentre; |
| 172 |
crd[XX] = xy[XX]; |
| 173 |
crd[YY] = xy[YY]; |
| 174 |
} |
| 175 |
|
| 176 |
// ------------------------------------------------------------------------------------------------- |
| 177 |
|
| 178 |
void CubedSphereEquiAnglProjection::lonlat2alphabeta(double crd[], idx_t t) const { |
| 179 |
|
| 180 |
const auto angles = SineCosine(PointLonLat(crd)); |
| 181 |
|
| 182 |
// Convert lonlat to xyz on unit sphere. |
| 183 |
auto xyz = PointXYZ{angles.cosLambda * angles.cosPhi, angles.sinLambda * angles.cosPhi, -angles.sinPhi}; |
| 184 |
|
| 185 |
const auto& tiles = getCubedSphereTiles(); |
| 186 |
tiles.unrotate(t, xyz.data()); |
| 187 |
|
| 188 |
// project into xy and rotate into alphabeta coordinates. |
| 189 |
auto alphaBeta = tiles.tileJacobian(t).inverse() * |
| 190 |
Point2{std::atan2(xyz.y(), xyz.x()), -std::atan2(xyz.z(), xyz.x())}; |
| 191 |
|
| 192 |
alphaBeta = alphaBeta * rad2deg; |
| 193 |
|
| 194 |
crd[0] = alphaBeta[0]; |
| 195 |
crd[1] = alphaBeta[1]; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
// ------------------------------------------------------------------------------------------------- |
| 200 |
|
| 201 |
void CubedSphereEquiAnglProjection::alphabeta2lonlat(double crd[], idx_t t) const { |
| 202 |
|
| 203 |
// Rotate alphabeta into xy direction. |
| 204 |
const auto& tiles = getCubedSphereTiles(); |
| 205 |
auto alphaBeta = tiles.tileJacobian(t) * Point2{crd}; |
| 206 |
alphaBeta = alphaBeta * deg2rad; |
| 207 |
|
| 208 |
// project alphabeta into xyz space. |
| 209 |
auto xyz = PointXYZ{-1., -std::tan(alphaBeta[0]), std::tan(alphaBeta[1])}; |
| 210 |
|
| 211 |
tiles.rotate(t, xyz.data()); |
| 212 |
|
| 213 |
// Honestly, I figured out some of the -1 factors by trial and error. |
| 214 |
const auto r = PointXYZ::norm(xyz); |
| 215 |
|
| 216 |
auto lonlat = PointLonLat{}; |
| 217 |
if (equal(xyz.x(), 0.) && equal(xyz.y(), 0.)) { |
| 218 |
lonlat.lon() = 0.; |
| 219 |
} |
| 220 |
else { |
| 221 |
lonlat.lon() = std::atan2(-xyz.y(), -xyz.x()); |
| 222 |
} |
| 223 |
lonlat.lat() = std::asin(xyz.z() / r); |
| 224 |
|
| 225 |
lonlat = lonlat * rad2deg; |
| 226 |
lonlat.normalise(); |
| 227 |
|
| 228 |
crd[0] = lonlat.lon(); |
| 229 |
crd[1] = lonlat.lat(); |
| 230 |
} |
| 231 |
|
| 232 |
// ------------------------------------------------------------------------------------------------- |
| 233 |
|
| 234 |
Jacobian CubedSphereEquiAnglProjection::jacobian(const PointLonLat& lonlat, idx_t t) const { |
| 235 |
// Note: angular units cancel, so we leave all values in radians. |
| 236 |
|
| 237 |
const auto angles = SineCosine(lonlat); |
| 238 |
|
| 239 |
// Convert lonlat to xyz on unit sphere. |
| 240 |
auto xyz = PointXYZ{angles.cosLambda * angles.cosPhi, angles.sinLambda * angles.cosPhi, -angles.sinPhi}; |
| 241 |
|
| 242 |
// Get derivatives of xyz with respect to lambda and phi. |
| 243 |
auto dxyz_by_dlambda = PointXYZ{-angles.sinLambda * angles.cosPhi, angles.cosLambda * angles.cosPhi, 0.}; |
| 244 |
auto dxyz_by_dphi = PointXYZ{-angles.cosLambda * angles.sinPhi, -angles.sinLambda * angles.sinPhi, -angles.cosPhi}; |
| 245 |
|
| 246 |
// Rotate vectors. |
| 247 |
const auto& tiles = getCubedSphereTiles(); |
| 248 |
tiles.unrotate(t, xyz.data()); |
| 249 |
tiles.unrotate(t, dxyz_by_dlambda.data()); |
| 250 |
tiles.unrotate(t, dxyz_by_dphi.data()); |
| 251 |
|
| 252 |
// Get derivatives of a and b with respect to xyz. |
| 253 |
// Note: a and b are xy displacements from the tile centre, *not* the |
| 254 |
// (alpha, beta) coordinates defined in the tileJacobian method. |
| 255 |
|
| 256 |
const double inv_x2y2 = 1. / (xyz.x() * xyz.x() + xyz.y() * xyz.y()); |
| 257 |
const double inv_x2z2 = 1. / (xyz.x() * xyz.x() + xyz.z() * xyz.z()); |
| 258 |
|
| 259 |
const auto da_by_dxyz = PointXYZ{-xyz.y() * inv_x2y2, xyz.x() * inv_x2y2, 0.}; |
| 260 |
const auto db_by_dxyz = PointXYZ{xyz.z() * inv_x2z2, 0., -xyz.x() * inv_x2z2}; |
| 261 |
|
| 262 |
// Use chain rule to get Jacobian. |
| 263 |
const auto& dot = eckit::geometry::Point3::dot; |
| 264 |
return Jacobian{{dot(da_by_dxyz, dxyz_by_dlambda), dot(da_by_dxyz, dxyz_by_dphi)}, |
| 265 |
{dot(db_by_dxyz, dxyz_by_dlambda), dot(db_by_dxyz, dxyz_by_dphi)}}; |
| 266 |
} |
| 267 |
|
| 268 |
Jacobian CubedSphereEquiAnglProjection::alphabetaJacobian(const PointLonLat& lonlat, idx_t t) const { |
| 269 |
const auto& tiles = getCubedSphereTiles(); |
| 270 |
return tiles.tileJacobian(t).inverse() * jacobian(lonlat, t); |
| 271 |
} |
| 272 |
|
| 273 |
// ------------------------------------------------------------------------------------------------- |
| 274 |
|
| 275 |
void CubedSphereEquiAnglProjection::lonlat2xy(double crd[]) const { |
| 276 |
if (debug) { |
| 277 |
Log::info() << "equiangular lonlat2xy start : lonlat = " << crd[LON] << " " << crd[LAT] << std::endl; |
| 278 |
} |
| 279 |
|
| 280 |
idx_t t; |
| 281 |
double ab[2]; // alpha-beta coordinate |
| 282 |
double xyz[3]; // on Cartesian grid |
| 283 |
|
| 284 |
CubedSphereProjectionBase::lonlat2xy_pre(crd, t, xyz); |
| 285 |
|
| 286 |
// should be between -45.0 and 45.0 |
| 287 |
// now calculate (alpha, beta) in radians. |
| 288 |
ab[0] = std::atan2(xyz[YY], xyz[XX]) * rad2deg; |
| 289 |
ab[1] = std::atan2(-xyz[ZZ], xyz[XX]) * rad2deg; // I think the minus is here due to the |
| 290 |
// left coordinate system |
| 291 |
|
| 292 |
if (debug) { |
| 293 |
Log::debug() << "equiangular lonlat2xy xyz ab : " << xyz[XX] << " " << xyz[YY] << " " << xyz[ZZ] << " " |
| 294 |
<< ab[LON] << " " << ab[LAT] << std::endl; |
| 295 |
} |
| 296 |
|
| 297 |
CubedSphereProjectionBase::alphabetat2xy(t, ab, crd); |
| 298 |
|
| 299 |
if (debug) { |
| 300 |
Log::debug() << "equiangular lonlat2xy end : xy = " << crd[LON] << " " << crd[LAT] << std::endl; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
// ------------------------------------------------------------------------------------------------- |
| 305 |
// input should be Willems xy coordinate in degrees |
| 306 |
// |
| 307 |
void CubedSphereEquiAnglProjection::xy2lonlat(double crd[]) const { |
| 308 |
if (debug) { |
| 309 |
Log::debug() << "xy2lonlat start xy = " << crd[LON] << " " << crd[LAT] << std::endl; |
| 310 |
} |
| 311 |
|
| 312 |
static const double rsq3 = 1.0 / std::sqrt(3.0); |
| 313 |
double xyz[3]; |
| 314 |
double ab[2]; // alpha-beta coordinate |
| 315 |
idx_t t; // tile index |
| 316 |
|
| 317 |
// calculate xy (in degrees) to alpha beta (in radians) and t - tile index. |
| 318 |
CubedSphereProjectionBase::xy2alphabetat(crd, t, ab); |
| 319 |
|
| 320 |
if (debug) { |
| 321 |
Log::debug() << "equiangular xy2lonlat:: crd t ab : " << crd[0] << " " << crd[1] << " " << t << " " << ab[0] |
| 322 |
<< " " << ab[1] << std::endl; |
| 323 |
} |
| 324 |
|
| 325 |
xyz[0] = -rsq3; |
| 326 |
xyz[1] = -rsq3 * std::tan(ab[0] * deg2rad); |
| 327 |
xyz[2] = -rsq3 * std::tan(ab[1] * deg2rad); |
| 328 |
|
| 329 |
CubedSphereProjectionBase::xy2lonlat_post(xyz, t, crd); |
| 330 |
|
| 331 |
if (debug) { |
| 332 |
Log::debug() << "end of equiangular xy2lonlat lonlat = " << crd[LON] << " " << crd[LAT] << std::endl; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
// ------------------------------------------------------------------------------------------------- |
| 337 |
|
| 338 |
Jacobian CubedSphereEquiAnglProjection::jacobian(const PointLonLat& lonlat) const { |
| 339 |
const auto& tiles = getCubedSphereTiles(); |
| 340 |
const idx_t t = tiles.indexFromLonLat(lonlat.data()); |
| 341 |
return jacobian(lonlat, t); |
| 342 |
} |
| 343 |
|
| 344 |
// ------------------------------------------------------------------------------------------------- |
| 345 |
|
| 346 |
CubedSphereEquiAnglProjection::Spec CubedSphereEquiAnglProjection::spec() const { |
| 347 |
// Fill projection specification |
| 348 |
Spec proj; |
| 349 |
proj.set("type", static_type()); |
| 350 |
return proj; |
| 351 |
} |
| 352 |
|
| 353 |
// ------------------------------------------------------------------------------------------------- |
| 354 |
|
| 355 |
void CubedSphereEquiAnglProjection::hash(eckit::Hash& h) const { |
| 356 |
// Add to hash |
| 357 |
h.add(static_type()); |
| 358 |
CubedSphereProjectionBase::hash(h); |
| 359 |
} |
| 360 |
|
| 361 |
// ------------------------------------------------------------------------------------------------- |
| 362 |
|
| 363 |
namespace { |
| 364 |
static ProjectionBuilder<CubedSphereEquiAnglProjection> register_1(CubedSphereEquiAnglProjection::static_type()); |
| 365 |
} |
| 366 |
|
| 367 |
} // namespace detail |
| 368 |
} // namespace projection |
| 369 |
} // namespace atlas |