| 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 "atlas/grid/detail/spacing/CustomSpacing.h" |
| 12 |
|
| 13 |
#include <algorithm> |
| 14 |
|
| 15 |
#include "eckit/config/Parametrisation.h" |
| 16 |
|
| 17 |
#include "atlas/grid/detail/spacing/SpacingFactory.h" |
| 18 |
#include "atlas/runtime/Exception.h" |
| 19 |
|
| 20 |
namespace atlas { |
| 21 |
namespace grid { |
| 22 |
namespace spacing { |
| 23 |
|
| 24 |
CustomSpacing::CustomSpacing(long N, const double values[], const Interval& interval) { |
| 25 |
x_.assign(values, values + N); |
| 26 |
min_ = std::min(interval[0], interval[1]); |
| 27 |
max_ = std::max(interval[0], interval[1]); |
| 28 |
} |
| 29 |
|
| 30 |
CustomSpacing::CustomSpacing(const eckit::Parametrisation& params) { |
| 31 |
params.get("values", x_); |
| 32 |
|
| 33 |
size_t N; |
| 34 |
if (params.get("N", N)) { |
| 35 |
ATLAS_ASSERT(x_.size() == N); |
| 36 |
} |
| 37 |
N = x_.size(); |
| 38 |
|
| 39 |
std::vector<double> interval; |
| 40 |
if (params.get("interval", interval)) { |
| 41 |
min_ = std::min(interval[0], interval[1]); |
| 42 |
max_ = std::max(interval[0], interval[1]); |
| 43 |
} |
| 44 |
else { |
| 45 |
min_ = x_.front(); |
| 46 |
max_ = x_.front(); |
| 47 |
for (size_t j = 1; j < N; ++j) { |
| 48 |
min_ = std::min(min_, x_[j]); |
| 49 |
max_ = std::max(max_, x_[j]); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
CustomSpacing::Spec CustomSpacing::spec() const { |
| 55 |
Spec spacing_specs; |
| 56 |
spacing_specs.set("type", static_type()); |
| 57 |
spacing_specs.set("values", x_); |
| 58 |
spacing_specs.set("interval", std::vector<double>{min(), max()}); |
| 59 |
return spacing_specs; |
| 60 |
} |
| 61 |
|
| 62 |
namespace { |
| 63 |
static SpacingBuilder<CustomSpacing> __builder(CustomSpacing::static_type()); |
| 64 |
} |
| 65 |
|
| 66 |
} // namespace spacing |
| 67 |
} // namespace grid |
| 68 |
} // namespace atlas |