PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
LonLatGrid.hh
Go to the documentation of this file.
1/* Copyright (C) 2025 PISM Authors
2 *
3 * This file is part of PISM.
4 *
5 * PISM is free software; you can redistribute it and/or modify it under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with PISM; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef PISM_LONLATGRID_H
21#define PISM_LONLATGRID_H
22
23#include <vector>
24#include <cmath> // M_PI
25#include <cstddef> // size_t
26
27#include "pism/util/LonLatCalculator.hh"
28
29namespace pism {
30/*!
31 * Grid definition using coordinates in radians.
32 */
33struct LonLatGrid {
34 std::vector<double> lon;
35 std::vector<double> lat;
36
37 /*!
38 *
39 * Converts a Cartesian grid in a `projection` that uses coordinates
40 * `x` and `y` in meters into the form that can be used to define a
41 * curvilinear grid in YAC.
42 *
43 * The `projection` string has to use the format compatible with PROJ.
44 */
45 LonLatGrid(const std::vector<double> &x, const std::vector<double> &y,
46 const std::string &projection) {
47
48 size_t nrow = y.size();
49 size_t ncol = x.size();
50 size_t N = nrow * ncol;
51
52 lon.resize(N);
53 lat.resize(N);
54
55 // convert from (row, col) to the linear index in "cell" arrays
56 auto C = [ncol](size_t row, size_t col) { return row * ncol + col; };
57
58 // convert from degrees to radians
59 auto deg2rad = [](double degree) { return degree * M_PI / 180; };
60
61 pism::LonLatCalculator mapping(projection);
62
63 for (size_t row = 0; row < nrow; ++row) {
64 for (size_t col = 0; col < ncol; ++col) {
65 auto coords = mapping.lonlat(x[col], y[row]);
66
67 lon[C(row, col)] = deg2rad(coords[0]);
68 lat[C(row, col)] = deg2rad(coords[1]);
69 }
70 }
71 }
72};
73
74} // namespace pism
75
76#endif /* PISM_LONLATGRID_H */
std::array< double, 2 > lonlat(double x, double y)
std::vector< double > lat
Definition LonLatGrid.hh:35
LonLatGrid(const std::vector< double > &x, const std::vector< double > &y, const std::string &projection)
Definition LonLatGrid.hh:45
std::vector< double > lon
Definition LonLatGrid.hh:34