Debian Codegraph @

src/atlas/projection/detail/SchmidtProjection.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 #include <cmath>
11
12 #include "eckit/config/Parametrisation.h"
13 #include "eckit/utils/Hash.h"
14
15 #include "atlas/projection/detail/ProjectionFactory.h"
16 #include "atlas/projection/detail/SchmidtProjection.h"
17 #include "atlas/runtime/Exception.h"
18 #include "atlas/util/Config.h"
19 #include "atlas/util/Constants.h"
20 #include "atlas/util/UnitSphere.h"
21
22 namespace {
23 static double D2R(const double x) {
24 return atlas::util::Constants::degreesToRadians() * x;
25 }
26 static double R2D(const double x) {
27 return atlas::util::Constants::radiansToDegrees() * x;
28 }
29 } // namespace
30
31 namespace atlas {
32 namespace projection {
33 namespace detail {
34
35 PointLonLat rotation_north_pole(const Rotated& rotation) {
36 return rotation.northPole();
37 }
38
39 PointLonLat rotation_north_pole(const NotRotated& rotation) {
40 return PointLonLat{0., 90.};
41 }
42
43 // constructor
44 template <typename Rotation>
45 SchmidtProjectionT<Rotation>::SchmidtProjectionT(const eckit::Parametrisation& params):
46 ProjectionImpl(), rotation_(params) {
47 if (!params.get("stretching_factor", c_)) {
48 throw_Exception("stretching_factor missing in Params", Here());
49 }
50 ATLAS_ASSERT(c_ != 0.);
51
52 north0_ = {0.0, 0.0, 1.0};
53
54 atlas::util::UnitSphere::convertSphericalToCartesian(rotation_north_pole(rotation_), north1_);
55 north1_ = PointXYZ::normalize(north1_);
56 }
57
58 // constructor
59 template <typename Rotation>
60 SchmidtProjectionT<Rotation>::SchmidtProjectionT(): ProjectionImpl(), rotation_(util::NoConfig()) {}
61
62 template <typename Rotation>
63 void SchmidtProjectionT<Rotation>::xy2lonlat(double crd[]) const {
64 // stretch
65 crd[1] = R2D(std::asin(std::cos(2. * std::atan(1 / c_ * std::tan(std::acos(std::sin(D2R(crd[1]))) * 0.5)))));
66
67 // perform rotation
68 rotation_.rotate(crd);
69 }
70
71 template <typename Rotation>
72 void SchmidtProjectionT<Rotation>::lonlat2xy(double crd[]) const {
73 // inverse rotation
74 rotation_.unrotate(crd);
75
76 // unstretch
77 crd[1] = R2D(std::asin(std::cos(2. * std::atan(c_ * std::tan(std::acos(std::sin(D2R(crd[1]))) * 0.5)))));
78 }
79
80 template <>
81 ProjectionImpl::Jacobian SchmidtProjectionT<NotRotated>::jacobian(const PointLonLat&) const {
82 throw_NotImplemented("SchmidtProjectionT<NotRotated>::jacobian", Here());
83 }
84
85 template <typename Rotation>
86 ProjectionImpl::Jacobian SchmidtProjectionT<Rotation>::jacobian(const PointLonLat& lonlat) const {
87 double xy[2] = {lonlat.lon(), lonlat.lat()};
88
89 lonlat2xy(xy);
90
91 PointXYZ xyz;
92 atlas::util::UnitSphere::convertSphericalToCartesian(lonlat, xyz);
93
94
95 double zomc2 = 1.0 - 1.0 / (c_ * c_);
96 double zopc2 = 1.0 + 1.0 / (c_ * c_);
97
98 double zcosy = std::cos(D2R(xy[1]));
99 double zsiny = std::sin(D2R(xy[1]));
100 double zcoslat = std::cos(D2R(lonlat.lat()));
101
102 double zfactor = std::sqrt((zopc2 + zsiny * zomc2) * (zopc2 + zsiny * zomc2) / (zopc2 * zopc2 - zomc2 * zomc2));
103
104 // Base vectors in unrotated frame
105 auto u0 = PointXYZ::normalize(PointXYZ::cross(north0_, xyz));
106 auto v0 = PointXYZ::normalize(PointXYZ::cross(xyz, u0));
107
108 // Base vectors in rotated frame
109 auto u1 = PointXYZ::normalize(PointXYZ::cross(north1_, xyz));
110 auto v1 = PointXYZ::normalize(PointXYZ::cross(xyz, u1));
111
112 double u0u1 = PointXYZ::dot(u0, u1);
113 double v0u1 = PointXYZ::dot(v0, u1);
114
115 double u0v1 = PointXYZ::dot(u0, v1);
116 double v0v1 = PointXYZ::dot(v0, v1);
117
118 Jacobian jac;
119 jac[0] = {zcoslat * u0u1 * zfactor / zcosy, v0u1 * zfactor / zcosy};
120 jac[1] = {zcoslat * u0v1 * zfactor, v0v1 * zfactor};
121
122 return jac;
123 }
124
125 // specification
126 template <typename Rotation>
127 typename SchmidtProjectionT<Rotation>::Spec SchmidtProjectionT<Rotation>::spec() const {
128 Spec proj_spec;
129 proj_spec.set("type", static_type());
130 proj_spec.set("stretching_factor", c_);
131 rotation_.spec(proj_spec);
132 return proj_spec;
133 }
134
135 template <typename Rotation>
136 void SchmidtProjectionT<Rotation>::hash(eckit::Hash& hsh) const {
137 hsh.add(static_type());
138 rotation_.hash(hsh);
139 hsh.add(c_);
140 }
141
142 template class SchmidtProjectionT<NotRotated>;
143 template class SchmidtProjectionT<Rotated>;
144
145 namespace {
146 static ProjectionBuilder<SchmidtProjection> register_1(SchmidtProjection::static_type());
147 static ProjectionBuilder<RotatedSchmidtProjection> register_2(RotatedSchmidtProjection::static_type());
148 } // namespace
149
150 } // namespace detail
151 } // namespace projection
152 } // namespace atlas