| 1 |
/* |
| 2 |
* (C) Copyright 1996- 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 |
|
| 12 |
#include "LambertConformalConicProjection.h" |
| 13 |
|
| 14 |
#include <cmath> |
| 15 |
|
| 16 |
#include "eckit/config/Parametrisation.h" |
| 17 |
#include "eckit/types/FloatCompare.h" |
| 18 |
#include "eckit/utils/Hash.h" |
| 19 |
|
| 20 |
#include "atlas/projection/detail/ProjectionFactory.h" |
| 21 |
#include "atlas/runtime/Exception.h" |
| 22 |
#include "atlas/util/Config.h" |
| 23 |
#include "atlas/util/Constants.h" |
| 24 |
#include "atlas/util/CoordinateEnums.h" |
| 25 |
#include "atlas/util/Earth.h" |
| 26 |
|
| 27 |
|
| 28 |
namespace atlas { |
| 29 |
namespace projection { |
| 30 |
namespace detail { |
| 31 |
|
| 32 |
namespace { |
| 33 |
|
| 34 |
inline double normalise(double a, double minimum, double globe) { |
| 35 |
while (a >= minimum + globe) { |
| 36 |
a -= globe; |
| 37 |
} |
| 38 |
while (a < minimum) { |
| 39 |
a += globe; |
| 40 |
} |
| 41 |
return a; |
| 42 |
} |
| 43 |
|
| 44 |
inline double tan_d(double lat_d) { |
| 45 |
return std::tan(util::Constants::degreesToRadians() * (45. + lat_d * 0.5)); |
| 46 |
} |
| 47 |
|
| 48 |
inline double sin_d(double theta_d) { |
| 49 |
return std::sin(util::Constants::degreesToRadians() * theta_d); |
| 50 |
} |
| 51 |
|
| 52 |
inline double cos_d(double theta_d) { |
| 53 |
return std::cos(util::Constants::degreesToRadians() * theta_d); |
| 54 |
} |
| 55 |
|
| 56 |
static ProjectionBuilder<LambertConformalConicProjection> register_1(LambertConformalConicProjection::static_type()); |
| 57 |
|
| 58 |
} // namespace |
| 59 |
|
| 60 |
LambertConformalConicProjection::LambertConformalConicProjection(const eckit::Parametrisation& params) { |
| 61 |
ATLAS_ASSERT(params.get("longitude0", lon0_ = 0)); |
| 62 |
ATLAS_ASSERT(params.get("latitude0", lat0_ = 0)); |
| 63 |
if (not params.get("latitude1", lat1_)) { |
| 64 |
lat1_ = lat0_; |
| 65 |
} |
| 66 |
if (not params.get("latitude2", lat2_)) { |
| 67 |
lat2_ = lat0_; |
| 68 |
} |
| 69 |
params.get("radius", radius_ = util::Earth::radius()); |
| 70 |
|
| 71 |
n_ = eckit::types::is_approximately_equal(lat1_, lat2_) |
| 72 |
? sin_d(lat2_) |
| 73 |
: std::log(cos_d(lat1_) / cos_d(lat2_)) / std::log(tan_d(lat2_) / tan_d(lat1_)); |
| 74 |
inv_n_ = 1. / n_; |
| 75 |
sign_ = n_ < 0. ? -1. : 1.; // n < 0: adjustment for southern hemisphere |
| 76 |
|
| 77 |
F_ = (cos_d(lat1_) * std::pow(tan_d(lat1_), n_)) / n_; |
| 78 |
rho0_ = sign_ * radius_ * F_ * std::pow(tan_d(lat0_), -n_); |
| 79 |
} |
| 80 |
|
| 81 |
void LambertConformalConicProjection::lonlat2xy(double crd[]) const { |
| 82 |
double rho = radius_ * F_ * std::pow(tan_d(crd[1]), -n_); |
| 83 |
double theta = n_ * normalise(crd[0] - lon0_, -180, 360); |
| 84 |
|
| 85 |
crd[XX] = rho * sin_d(theta); |
| 86 |
crd[YY] = rho0_ - rho * cos_d(theta); |
| 87 |
} |
| 88 |
|
| 89 |
void LambertConformalConicProjection::xy2lonlat(double crd[]) const { |
| 90 |
double x = sign_ * crd[XX]; |
| 91 |
double y = rho0_ - sign_ * crd[YY]; |
| 92 |
|
| 93 |
double rho = sign_ * std::sqrt(x * x + y * y); |
| 94 |
double theta = std::atan2(x, y) * inv_n_; |
| 95 |
|
| 96 |
crd[LON] = util::Constants::radiansToDegrees() * theta + lon0_; |
| 97 |
crd[LAT] = eckit::types::is_approximately_equal(rho, 0.) |
| 98 |
? 90 * sign_ |
| 99 |
: util::Constants::radiansToDegrees() * 2. * std::atan(std::pow(radius_ * F_ / rho, inv_n_)) - 90.; |
| 100 |
} |
| 101 |
|
| 102 |
ProjectionImpl::Jacobian LambertConformalConicProjection::jacobian(const PointLonLat& lonlat) const { |
| 103 |
ProjectionImpl::Jacobian jac; |
| 104 |
|
| 105 |
const double deg2rad = util::Constants::degreesToRadians(); |
| 106 |
|
| 107 |
double lat = lonlat.lat(), lon = lonlat.lon(); |
| 108 |
double tanlat = tan_d(lat), coslat = cos_d(45. + lat * 0.5); |
| 109 |
|
| 110 |
double rho = radius_ * F_ * std::pow(tanlat, -n_); |
| 111 |
double theta = n_ * normalise(lon - lon0_, -180, 360); |
| 112 |
|
| 113 |
double costheta = cos_d(theta), sintheta = sin_d(theta); |
| 114 |
|
| 115 |
// double x = rho * sintheta; |
| 116 |
// double y = rho0_ - rho * costheta; |
| 117 |
|
| 118 |
auto cpj = [&](const double dlon, const double dlat, double& dx, double& dy) { |
| 119 |
double drho = deg2rad * radius_ * F_ * (-n_) * std::pow(tanlat, -n_ - 1) / (coslat * coslat) * dlat * 0.5; |
| 120 |
double dtheta = +n_ * dlon; |
| 121 |
dx = +drho * sintheta + rho * costheta * deg2rad * dtheta; |
| 122 |
dy = -drho * costheta + rho * sintheta * deg2rad * dtheta; |
| 123 |
}; |
| 124 |
|
| 125 |
const double dlat = 1.0f; |
| 126 |
const double dlon = 1.0f; |
| 127 |
|
| 128 |
cpj(dlon, 0, jac[0][0], jac[1][0]); |
| 129 |
cpj(0, dlat, jac[0][1], jac[1][1]); |
| 130 |
|
| 131 |
return jac; |
| 132 |
} |
| 133 |
|
| 134 |
LambertConformalConicProjection::Spec LambertConformalConicProjection::spec() const { |
| 135 |
Spec spec; |
| 136 |
spec.set("type", static_type()); |
| 137 |
spec.set("longitude0", lon0_); |
| 138 |
spec.set("latitude0", lat0_); |
| 139 |
spec.set("latitude1", lat1_); |
| 140 |
spec.set("latitude2", lat2_); |
| 141 |
if (!eckit::types::is_approximately_equal(radius_, util::Earth::radius())) { |
| 142 |
spec.set("radius", radius_); |
| 143 |
} |
| 144 |
return spec; |
| 145 |
} |
| 146 |
|
| 147 |
void LambertConformalConicProjection::hash(eckit::Hash& h) const { |
| 148 |
h.add(static_type()); |
| 149 |
h.add(lat1_); |
| 150 |
h.add(lat2_); |
| 151 |
h.add(lat0_); |
| 152 |
h.add(lon0_); |
| 153 |
h.add(radius_); |
| 154 |
} |
| 155 |
|
| 156 |
} // namespace detail |
| 157 |
} // namespace projection |
| 158 |
} // namespace atlas |