| 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/GridBoxAverage.h" |
| 13 |
|
| 14 |
#include <vector> |
| 15 |
|
| 16 |
#include "eckit/log/ProgressTimer.h" |
| 17 |
|
| 18 |
#include "atlas/array.h" |
| 19 |
#include "atlas/functionspace/PointCloud.h" |
| 20 |
#include "atlas/interpolation/method/MethodFactory.h" |
| 21 |
#include "atlas/runtime/Exception.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace atlas { |
| 25 |
namespace interpolation { |
| 26 |
namespace method { |
| 27 |
|
| 28 |
|
| 29 |
namespace { |
| 30 |
MethodBuilder<GridBoxAverage> __builder("grid-box-average"); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
void GridBoxAverage::do_execute(const FieldSet& source, FieldSet& target, Metadata& metadata) const { |
| 35 |
ATLAS_ASSERT(source.size() == target.size()); |
| 36 |
|
| 37 |
// Matrix-based interpolation is handled by base (Method) class |
| 38 |
// TODO: exploit sparse/dense matrix multiplication |
| 39 |
for (idx_t i = 0; i < source.size(); ++i) { |
| 40 |
if (matrixFree_) { |
| 41 |
GridBoxAverage::do_execute(source[i], target[i], metadata); |
| 42 |
} |
| 43 |
else { |
| 44 |
Method::do_execute(source[i], target[i], metadata); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
void GridBoxAverage::do_execute(const Field& source, Field& target, Metadata& metadata) const { |
| 51 |
ATLAS_TRACE("atlas::interpolation::method::GridBoxAverage::do_execute()"); |
| 52 |
|
| 53 |
// Matrix-based interpolation is handled by base (Method) class |
| 54 |
if (!matrixFree_) { |
| 55 |
Method::do_execute(source, target, metadata); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
// ensure GridBoxMethod::setup() |
| 61 |
functionspace::PointCloud tgt = target_; |
| 62 |
ATLAS_ASSERT(tgt); |
| 63 |
|
| 64 |
ATLAS_ASSERT(searchRadius_ > 0.); |
| 65 |
ATLAS_ASSERT(!sourceBoxes_.empty()); |
| 66 |
ATLAS_ASSERT(!targetBoxes_.empty()); |
| 67 |
|
| 68 |
|
| 69 |
// set arrays |
| 70 |
ATLAS_ASSERT(source.rank() == 1); |
| 71 |
ATLAS_ASSERT(target.rank() == 1); |
| 72 |
|
| 73 |
auto xarray = atlas::array::make_view<double, 1>(source); |
| 74 |
auto yarray = atlas::array::make_view<double, 1>(target); |
| 75 |
ATLAS_ASSERT(xarray.size() == sourceBoxes_.size()); |
| 76 |
ATLAS_ASSERT(yarray.size() == targetBoxes_.size()); |
| 77 |
|
| 78 |
yarray.assign(0.); |
| 79 |
failures_.clear(); |
| 80 |
|
| 81 |
|
| 82 |
// interpolate |
| 83 |
eckit::ProgressTimer progress("Intersecting", targetBoxes_.size(), "grid box", double(5.)); |
| 84 |
|
| 85 |
std::vector<Triplet> triplets; |
| 86 |
size_t i = 0; |
| 87 |
for (auto p : tgt.iterate().lonlat()) { |
| 88 |
++progress; |
| 89 |
|
| 90 |
if (intersect(i, targetBoxes_.at(i), pTree_.closestPointsWithinRadius(p, searchRadius_), triplets)) { |
| 91 |
auto& y = yarray[i]; |
| 92 |
for (auto& t : triplets) { |
| 93 |
y += xarray[t.col()] * t.value(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
++i; |
| 98 |
} |
| 99 |
|
| 100 |
if (!failures_.empty()) { |
| 101 |
giveUp(failures_); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
} // namespace method |
| 107 |
} // namespace interpolation |
| 108 |
} // namespace atlas |