| 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. and Interpolation |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "eckit/config/Resource.h" |
| 12 |
#include "eckit/log/TraceTimer.h" |
| 13 |
|
| 14 |
#include "atlas/array.h" |
| 15 |
#include "atlas/functionspace/PointCloud.h" |
| 16 |
#include "atlas/functionspace/NodeColumns.h" |
| 17 |
#include "atlas/functionspace/CellColumns.h" |
| 18 |
#include "atlas/functionspace/StructuredColumns.h" |
| 19 |
#include "atlas/interpolation/method/knn/KNearestNeighboursBase.h" |
| 20 |
#include "atlas/library/Library.h" |
| 21 |
#include "atlas/mesh/Nodes.h" |
| 22 |
#include "atlas/mesh/actions/BuildXYZField.h" |
| 23 |
#include "atlas/runtime/Trace.h" |
| 24 |
#include "atlas/util/CoordinateEnums.h" |
| 25 |
|
| 26 |
namespace atlas { |
| 27 |
namespace interpolation { |
| 28 |
namespace method { |
| 29 |
|
| 30 |
void KNearestNeighboursBase::buildPointSearchTree(Mesh& meshSource, const mesh::Halo& _halo) { |
| 31 |
ATLAS_TRACE(); |
| 32 |
eckit::TraceTimer<Atlas> tim("KNearestNeighboursBase::buildPointSearchTree()"); |
| 33 |
|
| 34 |
auto lonlat = array::make_view<double, 2>(meshSource.nodes().lonlat()); |
| 35 |
auto halo = array::make_view<int, 1>(meshSource.nodes().halo()); |
| 36 |
int h = _halo.size(); |
| 37 |
|
| 38 |
static bool fastBuildKDTrees = eckit::Resource<bool>("$ATLAS_FAST_BUILD_KDTREES", true); |
| 39 |
|
| 40 |
if (fastBuildKDTrees) { |
| 41 |
pTree_.reserve(lonlat.shape(0)); |
| 42 |
} |
| 43 |
for (idx_t ip = 0; ip < lonlat.shape(0); ++ip) { |
| 44 |
if (halo(ip) <= h) { |
| 45 |
pTree_.insert(PointLonLat(lonlat(ip, LON), lonlat(ip, LAT)), ip); |
| 46 |
} |
| 47 |
} |
| 48 |
pTree_.build(); |
| 49 |
|
| 50 |
// // generate 3D point coordinates |
| 51 |
// mesh::actions::BuildXYZField("xyz")(meshSource); |
| 52 |
} |
| 53 |
|
| 54 |
namespace { |
| 55 |
|
| 56 |
template <typename FunctionSpace_type> |
| 57 |
void insert_tree(util::IndexKDTree& tree, const FunctionSpace_type& functionspace) { |
| 58 |
size_t ip{0}; |
| 59 |
for (auto p : functionspace.iterate().lonlat()) { |
| 60 |
tree.insert(p, ip++); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
void insert_tree(util::IndexKDTree& tree, const functionspace::NodeColumns& functionspace) { |
| 65 |
auto lonlat = array::make_view<double, 2>(functionspace.lonlat()); |
| 66 |
auto halo = array::make_view<int, 1>(functionspace.nodes().halo()); |
| 67 |
int h = functionspace.halo().size(); |
| 68 |
|
| 69 |
for (idx_t ip = 0; ip < lonlat.shape(0); ++ip) { |
| 70 |
if (halo(ip) <= h) { |
| 71 |
tree.insert(PointLonLat(lonlat(ip, LON), lonlat(ip, LAT)), ip); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
void insert_tree(util::IndexKDTree& tree, const functionspace::CellColumns& functionspace) { |
| 77 |
auto lonlat = array::make_view<double, 2>(functionspace.lonlat()); |
| 78 |
auto halo = array::make_view<int, 1>(functionspace.cells().halo()); |
| 79 |
int h = functionspace.halo().size(); |
| 80 |
|
| 81 |
for (idx_t ip = 0; ip < lonlat.shape(0); ++ip) { |
| 82 |
if (halo(ip) <= h) { |
| 83 |
tree.insert(PointLonLat(lonlat(ip, LON), lonlat(ip, LAT)), ip); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
void insert_tree(util::IndexKDTree& tree, const functionspace::StructuredColumns& functionspace) { |
| 89 |
auto lonlat = array::make_view<double, 2>(functionspace.lonlat()); |
| 90 |
|
| 91 |
for (idx_t ip = 0; ip < lonlat.shape(0); ++ip) { |
| 92 |
tree.insert(PointLonLat(lonlat(ip, LON), lonlat(ip, LAT)), ip); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
} // namespace |
| 97 |
|
| 98 |
void KNearestNeighboursBase::buildPointSearchTree(const FunctionSpace& functionspace) { |
| 99 |
ATLAS_TRACE(); |
| 100 |
eckit::TraceTimer<Atlas> timer("KNearestNeighboursBase::buildPointSearchTree()"); |
| 101 |
|
| 102 |
static bool fastBuildKDTrees = eckit::Resource<bool>("$ATLAS_FAST_BUILD_KDTREES", true); |
| 103 |
|
| 104 |
if (fastBuildKDTrees) { |
| 105 |
pTree_.reserve(functionspace.size()); |
| 106 |
} |
| 107 |
|
| 108 |
if (functionspace::PointCloud fs = functionspace) { |
| 109 |
insert_tree(pTree_, fs); |
| 110 |
} |
| 111 |
else if (functionspace::NodeColumns fs = functionspace) { |
| 112 |
insert_tree(pTree_, fs); |
| 113 |
} |
| 114 |
else if (functionspace::CellColumns fs = functionspace) { |
| 115 |
insert_tree(pTree_, fs); |
| 116 |
} |
| 117 |
else if (functionspace::StructuredColumns fs = functionspace) { |
| 118 |
insert_tree(pTree_, fs); |
| 119 |
} |
| 120 |
else { |
| 121 |
ATLAS_NOTIMPLEMENTED; |
| 122 |
} |
| 123 |
pTree_.build(); |
| 124 |
} |
| 125 |
|
| 126 |
bool KNearestNeighboursBase::extractTreeFromCache(const Cache& c) { |
| 127 |
IndexKDTreeCache cache(c); |
| 128 |
if (cache) { |
| 129 |
pTree_ = cache.tree(); |
| 130 |
return true; |
| 131 |
} |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
} // namespace method |
| 137 |
} // namespace interpolation |
| 138 |
} // namespace atlas |