| 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 |
|
| 13 |
#include "atlas/domain/detail/DomainFactory.h" |
| 14 |
#include "atlas/domain/detail/ZonalBandDomain.h" |
| 15 |
#include "atlas/runtime/Exception.h" |
| 16 |
|
| 17 |
namespace atlas { |
| 18 |
namespace domain { |
| 19 |
|
| 20 |
namespace { |
| 21 |
|
| 22 |
static bool _is_global(double ymin, double ymax) { |
| 23 |
const double eps = 1.e-12; |
| 24 |
return std::abs((ymax - ymin) - 180.) < eps; |
| 25 |
} |
| 26 |
|
| 27 |
static std::array<double, 2> get_interval_y(const eckit::Parametrisation& params) { |
| 28 |
constexpr double invalid = std::numeric_limits<double>::max(); |
| 29 |
auto is_valid = [](double y) { |
| 30 |
// deepcode ignore FloatingPointEquals: We want exact comparison |
| 31 |
return (y != invalid); |
| 32 |
}; |
| 33 |
double ymax = invalid; |
| 34 |
double ymin = invalid; |
| 35 |
|
| 36 |
if (params.get("south", ymin) || params.get("north", ymax)) { |
| 37 |
ymax = is_valid(ymax) ? ymax : 90.; |
| 38 |
ymin = is_valid(ymin) ? ymin : -90.; |
| 39 |
} |
| 40 |
else { |
| 41 |
if (!params.get("ymin", ymin)) { |
| 42 |
throw_Exception("ymin missing in Params", Here()); |
| 43 |
} |
| 44 |
if (!params.get("ymax", ymax)) { |
| 45 |
throw_Exception("ymax missing in Params", Here()); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return {ymin, ymax}; |
| 50 |
} |
| 51 |
|
| 52 |
static double get_west(const eckit::Parametrisation& params) { |
| 53 |
double west = 0.; |
| 54 |
params.get("west", west); |
| 55 |
return west; |
| 56 |
} |
| 57 |
|
| 58 |
/* |
| 59 |
constexpr std::array<double, 2> interval_x() { |
| 60 |
return {0., 360.}; |
| 61 |
} |
| 62 |
*/ |
| 63 |
} // namespace |
| 64 |
|
| 65 |
constexpr char ZonalBandDomain::units_[]; |
| 66 |
|
| 67 |
bool ZonalBandDomain::is_global(const Interval& y) { |
| 68 |
const double eps = 1.e-12; |
| 69 |
return std::abs(std::abs(y[1] - y[0]) - 180.) < eps; |
| 70 |
} |
| 71 |
|
| 72 |
ZonalBandDomain::ZonalBandDomain(const eckit::Parametrisation& params): |
| 73 |
ZonalBandDomain(get_interval_y(params), get_west(params)) {} |
| 74 |
|
| 75 |
ZonalBandDomain::ZonalBandDomain(const Interval& interval_y): ZonalBandDomain(interval_y, /*west*/ 0.) {} |
| 76 |
|
| 77 |
ZonalBandDomain::ZonalBandDomain(const Interval& interval_y, const double west): |
| 78 |
RectangularLonLatDomain({west, west + 360.}, interval_y) { |
| 79 |
global_ = _is_global(ymin(), ymax()); |
| 80 |
ymin_tol_ = ymin() - 1.e-6; |
| 81 |
ymax_tol_ = ymax() + 1.e-6; |
| 82 |
} |
| 83 |
|
| 84 |
bool ZonalBandDomain::contains(double /*x*/, double y) const { |
| 85 |
return contains_y(y); |
| 86 |
} |
| 87 |
|
| 88 |
ZonalBandDomain::Spec ZonalBandDomain::spec() const { |
| 89 |
Spec domain_spec; |
| 90 |
domain_spec.set("type", type()); |
| 91 |
domain_spec.set("ymin", ymin()); |
| 92 |
domain_spec.set("ymax", ymax()); |
| 93 |
if (xmin() != 0.) { |
| 94 |
domain_spec.set("west", xmin()); |
| 95 |
} |
| 96 |
return domain_spec; |
| 97 |
} |
| 98 |
|
| 99 |
void ZonalBandDomain::hash(eckit::Hash& h) const { |
| 100 |
spec().hash(h); |
| 101 |
} |
| 102 |
|
| 103 |
void ZonalBandDomain::print(std::ostream& os) const { |
| 104 |
os << "ZonalBandDomain[" |
| 105 |
<< "ymin=" << ymin() << ",ymax=" << ymax() << ",west=" << xmin() << "]"; |
| 106 |
} |
| 107 |
|
| 108 |
bool ZonalBandDomain::containsNorthPole() const { |
| 109 |
return ymax_tol_ >= 90.; |
| 110 |
} |
| 111 |
|
| 112 |
bool ZonalBandDomain::containsSouthPole() const { |
| 113 |
return ymin_tol_ <= -90.; |
| 114 |
} |
| 115 |
|
| 116 |
namespace { |
| 117 |
static DomainBuilder<ZonalBandDomain> register_builder(ZonalBandDomain::static_type()); |
| 118 |
} |
| 119 |
|
| 120 |
} // namespace domain |
| 121 |
} // namespace atlas |