| 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/mesh/actions/BuildXYZField.h" |
| 12 |
|
| 13 |
#include "atlas/array/MakeView.h" |
| 14 |
#include "atlas/field/Field.h" |
| 15 |
#include "atlas/mesh/Mesh.h" |
| 16 |
#include "atlas/mesh/Nodes.h" |
| 17 |
#include "atlas/runtime/Trace.h" |
| 18 |
#include "atlas/util/Earth.h" |
| 19 |
#include "atlas/util/Point.h" |
| 20 |
|
| 21 |
namespace atlas { |
| 22 |
namespace mesh { |
| 23 |
namespace actions { |
| 24 |
|
| 25 |
//---------------------------------------------------------------------------------------------------------------------- |
| 26 |
|
| 27 |
BuildXYZField::BuildXYZField(const std::string& name, bool force_recompute): |
| 28 |
name_(name), force_recompute_(force_recompute) {} |
| 29 |
|
| 30 |
Field& BuildXYZField::operator()(Mesh& mesh) const { |
| 31 |
return operator()(mesh.nodes()); |
| 32 |
} |
| 33 |
|
| 34 |
Field& BuildXYZField::operator()(mesh::Nodes& nodes) const { |
| 35 |
bool recompute = force_recompute_; |
| 36 |
if (!nodes.has_field(name_)) { |
| 37 |
nodes.add(Field(name_, array::make_datatype<double>(), array::make_shape(nodes.size(), 3))); |
| 38 |
recompute = true; |
| 39 |
} |
| 40 |
if (recompute) { |
| 41 |
ATLAS_TRACE("BuildXYZField"); |
| 42 |
array::ArrayView<double, 2> lonlat = array::make_view<double, 2>(nodes.lonlat()); |
| 43 |
array::ArrayView<double, 2> xyz = array::make_view<double, 2>(nodes.field(name_)); |
| 44 |
|
| 45 |
PointXYZ p2; |
| 46 |
for (idx_t n = 0; n < nodes.size(); ++n) { |
| 47 |
const PointLonLat p1(lonlat(n, 0), lonlat(n, 1)); |
| 48 |
util::Earth::convertSphericalToCartesian(p1, p2); |
| 49 |
xyz(n, 0) = p2.x(); |
| 50 |
xyz(n, 1) = p2.y(); |
| 51 |
xyz(n, 2) = p2.z(); |
| 52 |
} |
| 53 |
} |
| 54 |
return nodes.field(name_); |
| 55 |
} |
| 56 |
|
| 57 |
//---------------------------------------------------------------------------------------------------------------------- |
| 58 |
|
| 59 |
} // namespace actions |
| 60 |
} // namespace mesh |
| 61 |
} // namespace atlas |