| 1 |
/* |
| 2 |
* (C) Copyright 1996- 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. and Interpolation |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
#include "atlas/interpolation/method/knn/GridBoxMaximum.h" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <limits> |
| 16 |
#include <vector> |
| 17 |
|
| 18 |
#include "eckit/log/ProgressTimer.h" |
| 19 |
#include "eckit/types/FloatCompare.h" |
| 20 |
|
| 21 |
#include "atlas/array.h" |
| 22 |
#include "atlas/functionspace/PointCloud.h" |
| 23 |
#include "atlas/interpolation/method/MethodFactory.h" |
| 24 |
#include "atlas/runtime/Exception.h" |
| 25 |
#include "atlas/linalg/sparse/MakeEckitSparseMatrix.h" |
| 26 |
|
| 27 |
|
| 28 |
namespace atlas { |
| 29 |
namespace interpolation { |
| 30 |
namespace method { |
| 31 |
|
| 32 |
|
| 33 |
namespace { |
| 34 |
MethodBuilder<GridBoxMaximum> __builder("grid-box-maximum"); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
void GridBoxMaximum::do_execute(const FieldSet& source, FieldSet& target, Metadata& metadata) const { |
| 39 |
ATLAS_ASSERT(source.size() == target.size()); |
| 40 |
|
| 41 |
for (idx_t i = 0; i < source.size(); ++i) { |
| 42 |
do_execute(source[i], target[i], metadata); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
void GridBoxMaximum::do_execute(const Field& source, Field& target, Metadata&) const { |
| 48 |
ATLAS_TRACE("atlas::interpolation::method::GridBoxMaximum::do_execute()"); |
| 49 |
|
| 50 |
// set arrays |
| 51 |
ATLAS_ASSERT(source.rank() == 1); |
| 52 |
ATLAS_ASSERT(target.rank() == 1); |
| 53 |
|
| 54 |
auto xarray = atlas::array::make_view<double, 1>(source); |
| 55 |
auto yarray = atlas::array::make_view<double, 1>(target); |
| 56 |
ATLAS_ASSERT(xarray.size() == sourceBoxes_.size()); |
| 57 |
ATLAS_ASSERT(yarray.size() == targetBoxes_.size()); |
| 58 |
|
| 59 |
yarray.assign(0.); |
| 60 |
failures_.clear(); |
| 61 |
|
| 62 |
|
| 63 |
if (!matrixFree_) { |
| 64 |
const auto m = atlas::linalg::make_non_owning_eckit_sparse_matrix(matrix()); |
| 65 |
auto k = m.begin(); |
| 66 |
|
| 67 |
for (decltype(m.rows()) i = 0, j = 0; i < m.rows(); ++i) { |
| 68 |
double max = std::numeric_limits<double>::lowest(); |
| 69 |
bool found = false; |
| 70 |
|
| 71 |
for (; k != m.end(i); ++k) { |
| 72 |
ATLAS_ASSERT(k.col() < size_t(xarray.shape(0))); |
| 73 |
auto value = xarray[k.col()]; |
| 74 |
if (max < value) { |
| 75 |
max = value; |
| 76 |
j = k.col(); |
| 77 |
} |
| 78 |
found = true; |
| 79 |
} |
| 80 |
|
| 81 |
ATLAS_ASSERT(found); |
| 82 |
yarray[i] = xarray[j]; |
| 83 |
} |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
// ensure GridBoxMethod::setup() |
| 89 |
functionspace::PointCloud tgt = target_; |
| 90 |
ATLAS_ASSERT(tgt); |
| 91 |
|
| 92 |
ATLAS_ASSERT(searchRadius_ > 0.); |
| 93 |
ATLAS_ASSERT(!sourceBoxes_.empty()); |
| 94 |
ATLAS_ASSERT(!targetBoxes_.empty()); |
| 95 |
|
| 96 |
|
| 97 |
// interpolate |
| 98 |
eckit::ProgressTimer progress("Intersecting", targetBoxes_.size(), "grid box", double(5.)); |
| 99 |
|
| 100 |
std::vector<Triplet> triplets; |
| 101 |
size_t i = 0; |
| 102 |
for (auto p : tgt.iterate().lonlat()) { |
| 103 |
++progress; |
| 104 |
|
| 105 |
if (intersect(i, targetBoxes_.at(i), pTree_.closestPointsWithinRadius(p, searchRadius_), triplets)) { |
| 106 |
auto triplet = std::max_element(triplets.begin(), triplets.end(), [](const Triplet& a, const Triplet& b) { |
| 107 |
return !eckit::types::is_approximately_greater_or_equal(a.value(), b.value()); |
| 108 |
}); |
| 109 |
|
| 110 |
yarray[i] = xarray[triplet->col()]; |
| 111 |
} |
| 112 |
|
| 113 |
++i; |
| 114 |
} |
| 115 |
|
| 116 |
if (!failures_.empty()) { |
| 117 |
giveUp(failures_); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
} // namespace method |
| 123 |
} // namespace interpolation |
| 124 |
} // namespace atlas |