| 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 |
#pragma once |
| 12 |
|
| 13 |
#include <iostream> |
| 14 |
#include <memory> |
| 15 |
#include <string> |
| 16 |
|
| 17 |
#include "atlas/projection/Jacobian.h" |
| 18 |
#include "atlas/util/Factory.h" |
| 19 |
#include "atlas/util/NormaliseLongitude.h" |
| 20 |
#include "atlas/util/Object.h" |
| 21 |
#include "atlas/util/Point.h" |
| 22 |
#include "atlas/util/Rotation.h" |
| 23 |
|
| 24 |
namespace eckit { |
| 25 |
class Parametrisation; |
| 26 |
class Hash; |
| 27 |
} // namespace eckit |
| 28 |
|
| 29 |
namespace atlas { |
| 30 |
class Domain; |
| 31 |
class RectangularLonLatDomain; |
| 32 |
namespace util { |
| 33 |
class Config; |
| 34 |
} |
| 35 |
} // namespace atlas |
| 36 |
|
| 37 |
namespace atlas::projection::detail { |
| 38 |
|
| 39 |
class ProjectionImpl : public util::Object { |
| 40 |
public: |
| 41 |
using Spec = atlas::util::Config; |
| 42 |
using Jacobian = projection::Jacobian; |
| 43 |
|
| 44 |
public: |
| 45 |
static const ProjectionImpl* create(const eckit::Parametrisation& p); |
| 46 |
static const ProjectionImpl* create(const std::string& type, const eckit::Parametrisation& p); |
| 47 |
|
| 48 |
ProjectionImpl() = default; |
| 49 |
|
| 50 |
virtual std::string type() const = 0; |
| 51 |
|
| 52 |
virtual void xy2lonlat(double crd[]) const = 0; |
| 53 |
virtual void lonlat2xy(double crd[]) const = 0; |
| 54 |
|
| 55 |
virtual Jacobian jacobian(const PointLonLat&) const = 0; |
| 56 |
|
| 57 |
void xy2lonlat(Point2&) const; |
| 58 |
void lonlat2xy(Point2&) const; |
| 59 |
|
| 60 |
PointLonLat lonlat(const PointXY&) const; |
| 61 |
PointXY xy(const PointLonLat&) const; |
| 62 |
virtual PointXYZ xyz(const PointLonLat&) const; |
| 63 |
|
| 64 |
virtual bool strictlyRegional() const = 0; |
| 65 |
virtual RectangularLonLatDomain lonlatBoundingBox(const Domain&) const = 0; |
| 66 |
|
| 67 |
virtual Spec spec() const = 0; |
| 68 |
|
| 69 |
virtual std::string units() const = 0; |
| 70 |
|
| 71 |
virtual operator bool() const { return true; } |
| 72 |
|
| 73 |
virtual void hash(eckit::Hash&) const = 0; |
| 74 |
|
| 75 |
struct BoundLonLat { |
| 76 |
operator RectangularLonLatDomain() const; |
| 77 |
void extend(PointLonLat p, PointLonLat eps); |
| 78 |
|
| 79 |
bool crossesDateLine(bool); |
| 80 |
bool includesNorthPole(bool); |
| 81 |
bool includesSouthPole(bool); |
| 82 |
|
| 83 |
bool crossesDateLine() const { return crossesDateLine_; } |
| 84 |
bool includesNorthPole() const { return includesNorthPole_; } |
| 85 |
bool includesSouthPole() const { return includesSouthPole_; } |
| 86 |
|
| 87 |
private: |
| 88 |
PointLonLat min_; |
| 89 |
PointLonLat max_; |
| 90 |
bool crossesDateLine_ = false; |
| 91 |
bool includesNorthPole_ = false; |
| 92 |
bool includesSouthPole_ = false; |
| 93 |
bool first_ = true; |
| 94 |
}; |
| 95 |
|
| 96 |
struct Normalise { |
| 97 |
Normalise(const eckit::Parametrisation&); |
| 98 |
Normalise(double west); |
| 99 |
void hash(eckit::Hash&) const; |
| 100 |
void spec(Spec&) const; |
| 101 |
void operator()(double crd[]) const { |
| 102 |
if (normalise_) { |
| 103 |
crd[0] = (*normalise_)(crd[0]); |
| 104 |
} |
| 105 |
} |
| 106 |
operator bool() const { return bool(normalise_); } |
| 107 |
|
| 108 |
private: |
| 109 |
std::unique_ptr<util::NormaliseLongitude> normalise_; |
| 110 |
std::vector<double> values_; |
| 111 |
}; |
| 112 |
|
| 113 |
struct Derivate { |
| 114 |
Derivate(const ProjectionImpl& p, PointXY A, PointXY B, double h, double refLongitude = 0.); |
| 115 |
virtual ~Derivate(); |
| 116 |
virtual PointLonLat d(PointXY) const = 0; |
| 117 |
|
| 118 |
protected: |
| 119 |
const ProjectionImpl& projection_; |
| 120 |
const PointXY H_; |
| 121 |
const double invnH_; |
| 122 |
const double refLongitude_; |
| 123 |
PointLonLat xy2lonlat(const PointXY& p) const; |
| 124 |
}; |
| 125 |
|
| 126 |
struct DerivateFactory : public util::Factory<DerivateFactory> { |
| 127 |
static std::string className() { return "DerivateFactory"; } |
| 128 |
static ProjectionImpl::Derivate* build(const std::string& type, const ProjectionImpl& p, PointXY A, PointXY B, |
| 129 |
double h, double refLongitude = 0.); |
| 130 |
|
| 131 |
protected: |
| 132 |
using Factory::Factory; |
| 133 |
virtual ~DerivateFactory(); |
| 134 |
virtual ProjectionImpl::Derivate* make(const ProjectionImpl& p, PointXY A, PointXY B, double h, |
| 135 |
double refLongitude = 0.) = 0; |
| 136 |
}; |
| 137 |
}; |
| 138 |
|
| 139 |
inline void ProjectionImpl::xy2lonlat(Point2& point) const { |
| 140 |
xy2lonlat(point.data()); |
| 141 |
} |
| 142 |
|
| 143 |
inline void ProjectionImpl::lonlat2xy(Point2& point) const { |
| 144 |
lonlat2xy(point.data()); |
| 145 |
} |
| 146 |
|
| 147 |
inline PointLonLat ProjectionImpl::lonlat(const PointXY& xy) const { |
| 148 |
PointLonLat lonlat(xy); |
| 149 |
xy2lonlat(lonlat.data()); |
| 150 |
return lonlat; |
| 151 |
} |
| 152 |
|
| 153 |
inline PointXY ProjectionImpl::xy(const PointLonLat& lonlat) const { |
| 154 |
PointXY xy(lonlat); |
| 155 |
lonlat2xy(xy.data()); |
| 156 |
return xy; |
| 157 |
} |
| 158 |
|
| 159 |
//--------------------------------------------------------------------------------------------------------------------- |
| 160 |
|
| 161 |
class Rotated : public util::Rotation { |
| 162 |
public: |
| 163 |
using Spec = ProjectionImpl::Spec; |
| 164 |
|
| 165 |
Rotated(const PointLonLat& south_pole, double rotation_angle = 0.); |
| 166 |
Rotated(const eckit::Parametrisation&); |
| 167 |
virtual ~Rotated() = default; |
| 168 |
|
| 169 |
static std::string classNamePrefix() { return "Rotated"; } |
| 170 |
static std::string typePrefix() { return "rotated_"; } |
| 171 |
|
| 172 |
void spec(Spec&) const; |
| 173 |
|
| 174 |
void hash(eckit::Hash&) const; |
| 175 |
}; |
| 176 |
|
| 177 |
class NotRotated { |
| 178 |
public: |
| 179 |
using Spec = ProjectionImpl::Spec; |
| 180 |
|
| 181 |
NotRotated() = default; |
| 182 |
NotRotated(const eckit::Parametrisation&) {} |
| 183 |
virtual ~NotRotated() = default; |
| 184 |
|
| 185 |
static std::string classNamePrefix() { return ""; } // deliberately empty |
| 186 |
static std::string typePrefix() { return ""; } // deliberately empty |
| 187 |
|
| 188 |
void rotate(double*) const { /* do nothing */ } |
| 189 |
void unrotate(double*) const { /* do nothing */ } |
| 190 |
|
| 191 |
bool rotated() const { return false; } |
| 192 |
|
| 193 |
void spec(Spec&) const {} |
| 194 |
|
| 195 |
void hash(eckit::Hash&) const {} |
| 196 |
}; |
| 197 |
|
| 198 |
extern "C" { |
| 199 |
const ProjectionImpl* atlas__Projection__ctor_config(const eckit::Parametrisation* config); |
| 200 |
void atlas__Projection__type(const ProjectionImpl* This, char*& type, int& size); |
| 201 |
void atlas__Projection__hash(const ProjectionImpl* This, char*& hash, int& size); |
| 202 |
ProjectionImpl::Spec* atlas__Projection__spec(const ProjectionImpl* This); |
| 203 |
void atlas__Projection__xy2lonlat(const ProjectionImpl* This, const double x, const double y, double& lon, double& lat); |
| 204 |
void atlas__Projection__lonlat2xy(const ProjectionImpl* This, const double lon, const double lat, double& x, double& y); |
| 205 |
} |
| 206 |
|
| 207 |
} // namespace atlas::projection::detail |