Debian Codegraph @

src/atlas/interpolation/method/PointIndex2.cc

raw
1 /*
2 * (C) Crown Copyright 2021 Met Office
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 */
7
8 #include "eckit/config/Resource.h"
9
10 #include "atlas/array/ArrayView.h"
11 #include "atlas/array/MakeView.h"
12 #include "atlas/interpolation/method/PointIndex2.h"
13 #include "atlas/mesh/HybridElements.h"
14 #include "atlas/runtime/Trace.h"
15 #include "atlas/util/Topology.h"
16
17 namespace atlas {
18 namespace interpolation {
19 namespace method {
20
21 ElemIndex2* create_element2D_kdtree(const Mesh& mesh, const Field& field_centres) {
22 ATLAS_TRACE();
23 const array::ArrayView<const double, 2> centres = array::make_view<double, 2>(field_centres);
24 const array::ArrayView<const int, 1> flags = array::make_view<int, 1>(mesh.cells().flags());
25 auto include_element = [&](unsigned int e) {
26 using util::Topology;
27 return not Topology::view(flags(e)).check(Topology::INVALID);
28 };
29
30 static bool fastBuildKDTrees = eckit::Resource<bool>("$ATLAS_FAST_BUILD_KDTREES", true);
31
32 ElemIndex2* tree = new ElemIndex2();
33 const size_t nb_cells = centres.shape(0);
34
35 if (fastBuildKDTrees) {
36 std::vector<ElemIndex2::Value> p;
37 p.reserve(nb_cells);
38
39 for (unsigned int j = 0; j < nb_cells; ++j) {
40 if (include_element(j)) {
41 p.emplace_back(ElemIndex2::Point(centres(j, LON), centres(j, LAT)), ElemIndex2::Payload(j));
42 }
43 }
44
45 tree->build(p.begin(), p.end());
46 }
47 else {
48 for (unsigned int j = 0; j < nb_cells; ++j) {
49 if (include_element(j)) {
50 tree->insert(
51 ElemIndex2::Value(ElemIndex2::Point(centres(j, LON), centres(j, LAT)), ElemIndex2::Payload(j)));
52 }
53 }
54 }
55 return tree;
56 }
57
58 } // namespace method
59 } // namespace interpolation
60 } // namespace atlas