| 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 "CubedSphereEquiDistProjection.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 |
namespace atlas { |
| 26 |
namespace projection { |
| 27 |
namespace detail { |
| 28 |
|
| 29 |
static constexpr bool debug = false; // constexpr so compiler can optimize `if ( debug ) { ... }` out |
| 30 |
|
| 31 |
// ------------------------------------------------------------------------------------------------- |
| 32 |
|
| 33 |
CubedSphereEquiDistProjection::CubedSphereEquiDistProjection(const eckit::Parametrisation& params): |
| 34 |
CubedSphereProjectionBase(params) {} |
| 35 |
|
| 36 |
// ------------------------------------------------------------------------------------------------- |
| 37 |
|
| 38 |
void CubedSphereEquiDistProjection::xy2alphabeta(double crd[], idx_t t) const { |
| 39 |
throw_NotImplemented("xy2alphabeta not implemented for CubedSphereEquiDistProjection", Here()); |
| 40 |
} |
| 41 |
|
| 42 |
// ------------------------------------------------------------------------------------------------- |
| 43 |
|
| 44 |
void CubedSphereEquiDistProjection::alphabeta2xy(double crd[], idx_t t) const { |
| 45 |
throw_NotImplemented("alphabeta2xy not implemented for CubedSphereEquiDistProjection", Here()); |
| 46 |
} |
| 47 |
|
| 48 |
// ------------------------------------------------------------------------------------------------- |
| 49 |
|
| 50 |
void CubedSphereEquiDistProjection::lonlat2alphabeta(double crd[], idx_t t) const { |
| 51 |
throw_NotImplemented("lonlat2alphabeta not implemented for CubedSphereEquiDistProjection", Here()); |
| 52 |
} |
| 53 |
|
| 54 |
// ------------------------------------------------------------------------------------------------- |
| 55 |
|
| 56 |
void CubedSphereEquiDistProjection::alphabeta2lonlat(double crd[], idx_t t) const { |
| 57 |
throw_NotImplemented("alphabeta2lonlat not implemented for CubedSphereEquiDistProjection", Here()); |
| 58 |
} |
| 59 |
|
| 60 |
// ------------------------------------------------------------------------------------------------- |
| 61 |
|
| 62 |
Jacobian CubedSphereEquiDistProjection::jacobian(const PointLonLat& lonlat, idx_t t) const { |
| 63 |
throw_NotImplemented("jacobian not implemented for CubedSphereEquiDistProjection", Here()); |
| 64 |
} |
| 65 |
|
| 66 |
// ------------------------------------------------------------------------------------------------- |
| 67 |
|
| 68 |
Jacobian CubedSphereEquiDistProjection::alphabetaJacobian(const PointLonLat& lonlat, idx_t t) const { |
| 69 |
throw_NotImplemented("alphabetaJacobian not implemented for CubedSphereEquiDistProjection", Here()); |
| 70 |
} |
| 71 |
|
| 72 |
// ------------------------------------------------------------------------------------------------- |
| 73 |
|
| 74 |
void CubedSphereEquiDistProjection::lonlat2xy(double crd[]) const { |
| 75 |
if (debug) { |
| 76 |
Log::info() << "equidist lonlat2xy start : lonlat = " << crd[LON] << " " << crd[LAT] << std::endl; |
| 77 |
} |
| 78 |
idx_t t; |
| 79 |
double ab[2]; // alpha-beta coordinate |
| 80 |
double xyz[3]; // on Cartesian grid |
| 81 |
|
| 82 |
CubedSphereProjectionBase::lonlat2xy_pre(crd, t, xyz); |
| 83 |
|
| 84 |
//now should be tile 0 - now calculate (alpha, beta) in radians. |
| 85 |
// should be between - 45.0 and 45.0 |
| 86 |
ab[0] = 45.0 * xyz[YY] / xyz[XX]; |
| 87 |
ab[1] = -45.0 * xyz[ZZ] / xyz[XX]; |
| 88 |
|
| 89 |
if (debug) { |
| 90 |
Log::info() << "equidist lonlat2xy xyz ab : " << xyz[0] << " " << xyz[1] << " " << xyz[2] << " " << ab[0] << " " |
| 91 |
<< ab[1] << std::endl; |
| 92 |
} |
| 93 |
|
| 94 |
CubedSphereProjectionBase::alphabetat2xy(t, ab, crd); |
| 95 |
|
| 96 |
if (debug) { |
| 97 |
Log::info() << "equidist lonlat2xy end : xy = " << crd[LON] << " " << crd[LAT] << std::endl; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
// ------------------------------------------------------------------------------------------------- |
| 103 |
|
| 104 |
void CubedSphereEquiDistProjection::xy2lonlat(double crd[]) const { |
| 105 |
static const double rsq3 = 1.0 / sqrt(3.0); |
| 106 |
double xyz[3]; |
| 107 |
double ab[2]; // alpha-beta coordinate |
| 108 |
idx_t t; // tile index |
| 109 |
|
| 110 |
// calculate xy (in degrees) to alpha beta (in radians) and t - tile index. |
| 111 |
CubedSphereProjectionBase::xy2alphabetat(crd, t, ab); |
| 112 |
|
| 113 |
if (debug) { |
| 114 |
Log::info() << "equidist xy2lonlat:: crd t ab : " << crd[LON] << " " << crd[1] << " " << t << " " << ab[0] |
| 115 |
<< " " << ab[1] << std::endl; |
| 116 |
} |
| 117 |
|
| 118 |
xyz[0] = -rsq3; |
| 119 |
xyz[1] = -rsq3 * ab[0] / 45.; |
| 120 |
xyz[2] = -rsq3 * ab[1] / 45.; |
| 121 |
|
| 122 |
CubedSphereProjectionBase::xy2lonlat_post(xyz, t, crd); |
| 123 |
|
| 124 |
if (debug) { |
| 125 |
Log::info() << "end of equidistant xy2lonlat lonlat = " << crd[LON] << " " << crd[LAT] << std::endl; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
// ------------------------------------------------------------------------------------------------- |
| 131 |
|
| 132 |
Jacobian CubedSphereEquiDistProjection::jacobian(const PointLonLat&) const { |
| 133 |
ATLAS_NOTIMPLEMENTED; |
| 134 |
} |
| 135 |
|
| 136 |
// ------------------------------------------------------------------------------------------------- |
| 137 |
|
| 138 |
// ------------------------------------------------------------------------------------------------- |
| 139 |
|
| 140 |
CubedSphereEquiDistProjection::Spec CubedSphereEquiDistProjection::spec() const { |
| 141 |
// Fill projection specification |
| 142 |
Spec proj; |
| 143 |
proj.set("type", static_type()); |
| 144 |
return proj; |
| 145 |
} |
| 146 |
|
| 147 |
// ------------------------------------------------------------------------------------------------- |
| 148 |
|
| 149 |
void CubedSphereEquiDistProjection::hash(eckit::Hash& h) const { |
| 150 |
// Add to hash |
| 151 |
h.add(static_type()); |
| 152 |
CubedSphereProjectionBase::hash(h); |
| 153 |
} |
| 154 |
|
| 155 |
// ------------------------------------------------------------------------------------------------- |
| 156 |
|
| 157 |
namespace { |
| 158 |
static ProjectionBuilder<CubedSphereEquiDistProjection> register_1(CubedSphereEquiDistProjection::static_type()); |
| 159 |
} |
| 160 |
|
| 161 |
} // namespace detail |
| 162 |
} // namespace projection |
| 163 |
} // namespace atlas |