Debian Codegraph @

src/atlas/mesh/actions/GetCubedSphereNodalArea.cc

raw
1 /*
2 * (C) Crown Copyright 2024 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
9 #include "atlas/array/MakeView.h"
10 #include "atlas/grid/CubedSphereGrid.h"
11 #include "atlas/mesh/actions/GetCubedSphereNodalArea.h"
12 #include "atlas/util/NormaliseLongitude.h"
13 #include "atlas/util/Point.h"
14
15
16 namespace atlas {
17 namespace mesh {
18 namespace actions {
19
20
21 Field& GetCubedSphereNodalArea::operator()(Mesh& mesh) {
22 if (mesh.nodes().has_field("grid_cell_areas")) {
23 return mesh.nodes().field("grid_cell_areas");
24 }
25 else {
26 constexpr auto deg2rad = M_PI / 180.;
27 const auto& proj = mesh.projection();
28 auto lonlat = array::make_view<double, 2>(mesh.nodes().lonlat());
29
30 auto gcell_area_field = Field("grid_cell_areas",
31 make_datatype<double>(),
32 array::make_shape(mesh.nodes().size()));
33 auto gcell_area_fview = array::make_view<double, 1>(gcell_area_field);
34
35 double gcell_area_cs = [&] {
36 ATLAS_ASSERT(CubedSphereGrid(mesh.grid()));
37 // (grid_res * grid_res) = no. of cells on a tile
38 auto grid_res = CubedSphereGrid(mesh.grid()).N();
39 // area of a grid cell (cubed-sphere coord. system)
40 return M_PI/(2*grid_res) * M_PI/(2*grid_res);
41 }();
42
43 for (size_t i = 0; i < gcell_area_fview.size(); ++i) {
44 PointLonLat loc = PointLonLat(lonlat(i, atlas::LON), lonlat(i, atlas::LAT));
45 double cos_lat = std::cos(deg2rad * loc.lat());
46 double grid_jac_det = 1/proj.jacobian(loc).determinant();
47 // area of a grid cell (geographic coord. system)
48 gcell_area_fview(i) = grid_jac_det * gcell_area_cs * cos_lat;
49 }
50
51 mesh.nodes().add(gcell_area_field);
52
53 return mesh.nodes().field("grid_cell_areas");
54 }
55 }
56
57 } // namespace actions
58 } // namespace mesh
59 } // namespace atlas