| 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 |
#include <ostream> |
| 12 |
#include <utility> |
| 13 |
|
| 14 |
#include "eckit/utils/Hash.h" |
| 15 |
|
| 16 |
#include "atlas/domain/detail/DomainFactory.h" |
| 17 |
#include "atlas/domain/detail/RectangularDomain.h" |
| 18 |
#include "atlas/runtime/Exception.h" |
| 19 |
|
| 20 |
namespace atlas { |
| 21 |
namespace domain { |
| 22 |
|
| 23 |
using Interval = RectangularDomain::Interval; |
| 24 |
|
| 25 |
namespace { |
| 26 |
|
| 27 |
static std::array<double, 2> get_interval_x(const eckit::Parametrisation& params) { |
| 28 |
double xmin, xmax; |
| 29 |
|
| 30 |
if (!params.get("xmin", xmin)) { |
| 31 |
throw_Exception("xmin missing in Params", Here()); |
| 32 |
} |
| 33 |
|
| 34 |
if (!params.get("xmax", xmax)) { |
| 35 |
throw_Exception("xmax missing in Params", Here()); |
| 36 |
} |
| 37 |
|
| 38 |
return {xmin, xmax}; |
| 39 |
} |
| 40 |
|
| 41 |
static std::array<double, 2> get_interval_y(const eckit::Parametrisation& params) { |
| 42 |
double ymin, ymax; |
| 43 |
|
| 44 |
if (!params.get("ymin", ymin)) { |
| 45 |
throw_Exception("ymin missing in Params", Here()); |
| 46 |
} |
| 47 |
|
| 48 |
if (!params.get("ymax", ymax)) { |
| 49 |
throw_Exception("ymax missing in Params", Here()); |
| 50 |
} |
| 51 |
|
| 52 |
return {ymin, ymax}; |
| 53 |
} |
| 54 |
|
| 55 |
static std::string get_units(const eckit::Parametrisation& params) { |
| 56 |
std::string units; |
| 57 |
if (!params.get("units", units)) { |
| 58 |
throw_Exception("units missing in Params", Here()); |
| 59 |
} |
| 60 |
return units; |
| 61 |
} |
| 62 |
|
| 63 |
} // namespace |
| 64 |
|
| 65 |
bool RectangularDomain::is_global(const Interval& x, const Interval& y, const std::string& units) { |
| 66 |
if (units != "degrees") { |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
const double eps = 1.e-12; |
| 71 |
return std::abs((x[1] - x[0]) - 360.) < eps && std::abs(std::abs(y[1] - y[0]) - 180.) < eps; |
| 72 |
} |
| 73 |
|
| 74 |
bool RectangularDomain::is_zonal_band(const Interval& x, const std::string& units) { |
| 75 |
if (units != "degrees") { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
const double eps = 1.e-12; |
| 80 |
return std::abs((x[1] - x[0]) - 360.) < eps; |
| 81 |
} |
| 82 |
|
| 83 |
RectangularDomain::RectangularDomain(const eckit::Parametrisation& params): |
| 84 |
RectangularDomain(get_interval_x(params), get_interval_y(params), get_units(params)) {} |
| 85 |
|
| 86 |
RectangularDomain::RectangularDomain(const Interval& x, const Interval& y, const std::string& units): |
| 87 |
xmin_(x[0]), xmax_(x[1]), ymin_(y[0]), ymax_(y[1]), units_(units) { |
| 88 |
unit_degrees_ = (units_ == "degrees") ? true : false; |
| 89 |
|
| 90 |
// Make sure xmax>=xmin and ymax>=ymin |
| 91 |
if (xmin_ > xmax_) { |
| 92 |
std::swap(xmin_, xmax_); |
| 93 |
} |
| 94 |
if (ymin_ > ymax_) { |
| 95 |
std::swap(ymin_, ymax_); |
| 96 |
} |
| 97 |
global_ = is_global({xmin_, xmax_}, {ymin_, ymax_}, units_); |
| 98 |
|
| 99 |
const double tol = 1.e-6; |
| 100 |
xmin_tol_ = xmin_ - tol; |
| 101 |
ymin_tol_ = ymin_ - tol; |
| 102 |
xmax_tol_ = xmax_ + tol; |
| 103 |
ymax_tol_ = ymax_ + tol; |
| 104 |
} |
| 105 |
|
| 106 |
bool RectangularDomain::contains(double x, double y) const { |
| 107 |
return contains_x(x) and contains_y(y); |
| 108 |
} |
| 109 |
|
| 110 |
RectangularDomain::Spec RectangularDomain::spec() const { |
| 111 |
Spec domain_spec; |
| 112 |
domain_spec.set("type", type()); |
| 113 |
domain_spec.set("xmin", xmin()); |
| 114 |
domain_spec.set("xmax", xmax()); |
| 115 |
domain_spec.set("ymin", ymin()); |
| 116 |
domain_spec.set("ymax", ymax()); |
| 117 |
domain_spec.set("units", units()); |
| 118 |
return domain_spec; |
| 119 |
} |
| 120 |
|
| 121 |
void RectangularDomain::print(std::ostream& os) const { |
| 122 |
os << "RectangularDomain[" |
| 123 |
<< "xmin=" << xmin() << ",xmax=" << xmax() << ",ymin=" << ymin() << ",ymax=" << ymax() << ",units=" << units() |
| 124 |
<< "]"; |
| 125 |
} |
| 126 |
|
| 127 |
void RectangularDomain::hash(eckit::Hash& h) const { |
| 128 |
double multiplier = units() == "meters" ? 1e2 : 1e8; |
| 129 |
auto add_double = [&](const double& x) { h.add(std::round(x * multiplier)); }; |
| 130 |
h.add(type()); |
| 131 |
h.add(units()); |
| 132 |
add_double(xmin()); |
| 133 |
add_double(xmax()); |
| 134 |
add_double(ymin()); |
| 135 |
add_double(ymax()); |
| 136 |
} |
| 137 |
|
| 138 |
bool RectangularDomain::containsNorthPole() const { |
| 139 |
return unit_degrees_ && ymax_tol_ >= 90.; |
| 140 |
} |
| 141 |
|
| 142 |
bool RectangularDomain::containsSouthPole() const { |
| 143 |
return unit_degrees_ && ymin_tol_ <= -90.; |
| 144 |
} |
| 145 |
|
| 146 |
namespace { |
| 147 |
static DomainBuilder<RectangularDomain> register_builder(RectangularDomain::static_type()); |
| 148 |
} |
| 149 |
|
| 150 |
extern "C" { |
| 151 |
double atlas__LonLatRectangularDomain__north(const RectangularDomain* This) { |
| 152 |
return This->ymax(); |
| 153 |
} |
| 154 |
double atlas__LonLatRectangularDomain__west(const RectangularDomain* This) { |
| 155 |
return This->xmin(); |
| 156 |
} |
| 157 |
double atlas__LonLatRectangularDomain__south(const RectangularDomain* This) { |
| 158 |
return This->ymin(); |
| 159 |
} |
| 160 |
double atlas__LonLatRectangularDomain__east(const RectangularDomain* This) { |
| 161 |
return This->xmax(); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
} // namespace domain |
| 166 |
} // namespace atlas |