| 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 |
#pragma once |
| 12 |
|
| 13 |
#include <functional> |
| 14 |
#include <initializer_list> |
| 15 |
#include <string> |
| 16 |
|
| 17 |
#include "atlas/grid/Grid.h" |
| 18 |
#include "atlas/grid/detail/grid/Healpix.h" |
| 19 |
#include "atlas/grid/detail/grid/Structured.h" |
| 20 |
|
| 21 |
namespace atlas { |
| 22 |
|
| 23 |
//--------------------------------------------------------------------------------------------------------------------- |
| 24 |
class StructuredGrid; |
| 25 |
class RegularGrid; |
| 26 |
class GaussianGrid; |
| 27 |
class ReducedGaussianGrid; |
| 28 |
class RegularGaussianGrid; |
| 29 |
class RegularLonLatGrid; |
| 30 |
class ShiftedLonLatGrid; |
| 31 |
class HealpixGrid; |
| 32 |
|
| 33 |
/* |
| 34 |
Grid |
| 35 |
| |
| 36 |
+----------+----------+ |
| 37 |
| | |
| 38 |
StructuredGrid UnstructuredGrid |
| 39 |
| |
| 40 |
+--------------------+-----------------------+------------------+ |
| 41 |
| | | | |
| 42 |
ReducedGrid GaussianGrid RegularGrid HealpixGrid |
| 43 |
| | | | | |
| 44 |
+--------+--------+ +--------+--------+ +-----+ |
| 45 |
| | | |
| 46 |
ReducedGaussianGrid RegularGaussianGrid RegularLonLatGrid |
| 47 |
*/ |
| 48 |
|
| 49 |
//--------------------------------------------------------------------------------------------------------------------- |
| 50 |
|
| 51 |
/*! @class StructuredGrid |
| 52 |
* @brief Specialization of Grid, where the grid can be represented by rows with uniform distribution |
| 53 |
* @details |
| 54 |
* @copydetails Grid |
| 55 |
* @dotfile classatlas_1_1StructuredGrid__inherit__graph.dot |
| 56 |
*/ |
| 57 |
class StructuredGrid : public Grid { |
| 58 |
public: |
| 59 |
using grid_t = grid::detail::grid::Structured; |
| 60 |
using XSpace = grid_t::XSpace; |
| 61 |
using YSpace = grid_t::YSpace; |
| 62 |
|
| 63 |
public: |
| 64 |
StructuredGrid(); |
| 65 |
StructuredGrid(const Grid&); |
| 66 |
StructuredGrid(const Grid::Implementation*); |
| 67 |
StructuredGrid(const std::string& name, const Domain& = Domain()); |
| 68 |
StructuredGrid(const std::string& name, const Projection&, const Domain& = Domain()); |
| 69 |
StructuredGrid(const Config&); |
| 70 |
StructuredGrid(const XSpace&, const YSpace&, const Projection& = Projection(), const Domain& = Domain()); |
| 71 |
StructuredGrid(const Grid&, const Domain&); |
| 72 |
|
| 73 |
operator bool() const { return valid(); } |
| 74 |
|
| 75 |
bool valid() const { return grid_; } |
| 76 |
|
| 77 |
inline idx_t ny() const { return grid_->ny(); } |
| 78 |
|
| 79 |
inline idx_t nx(idx_t j) const { return grid_->nx(j); } |
| 80 |
|
| 81 |
inline const std::vector<idx_t>& nx() const { return grid_->nx(); } |
| 82 |
|
| 83 |
inline idx_t nxmax() const { return grid_->nxmax(); } |
| 84 |
|
| 85 |
inline const std::vector<double>& y() const { return grid_->y(); } |
| 86 |
|
| 87 |
/// x coordinate for given grid point {i,j} |
| 88 |
inline double x(idx_t i, idx_t j) const { return grid_->x(i, j); } |
| 89 |
|
| 90 |
/// y coordinate for given grid row {j} |
| 91 |
inline double y(idx_t j) const { return grid_->y(j); } |
| 92 |
|
| 93 |
/// increment in x for a given grid row {j} |
| 94 |
inline double dx(idx_t j) const { return grid_->dx(j); } |
| 95 |
|
| 96 |
/// x coordinate of beginning of a given grid row {j} |
| 97 |
inline double xmin(idx_t j) const { return grid_->xmin(j); } |
| 98 |
|
| 99 |
|
| 100 |
using Grid::xy; |
| 101 |
void xy(idx_t i, idx_t j, double xy[]) const { grid_->xy(i, j, xy); } |
| 102 |
|
| 103 |
using Grid::lonlat; |
| 104 |
void lonlat(idx_t i, idx_t j, double lonlat[]) const { grid_->lonlat(i, j, lonlat); } |
| 105 |
|
| 106 |
PointXY xy(idx_t i, idx_t j) const { return PointXY(x(i, j), y(j)); } |
| 107 |
|
| 108 |
PointLonLat lonlat(idx_t i, idx_t j) const { return grid_->lonlat(i, j); } |
| 109 |
|
| 110 |
inline bool reduced() const { return grid_->reduced(); } |
| 111 |
|
| 112 |
inline bool regular() const { return not reduced(); } |
| 113 |
|
| 114 |
bool periodic() const { return grid_->periodic(); } |
| 115 |
|
| 116 |
const XSpace& xspace() const { return grid_->xspace(); } |
| 117 |
|
| 118 |
const YSpace& yspace() const { return grid_->yspace(); } |
| 119 |
|
| 120 |
gidx_t index(idx_t i, idx_t j) const { return grid_->index(i, j); } |
| 121 |
|
| 122 |
void index2ij(gidx_t gidx, idx_t& i, idx_t& j) const { grid_->index2ij(gidx, i, j); } |
| 123 |
|
| 124 |
private: |
| 125 |
const grid_t* grid_; |
| 126 |
}; |
| 127 |
|
| 128 |
//--------------------------------------------------------------------------------------------------------------------- |
| 129 |
|
| 130 |
/// @class ReducedGrid |
| 131 |
/// @brief Specialization of StructuredGrid, where not all rows have the same number of grid points |
| 132 |
class ReducedGrid : public StructuredGrid { |
| 133 |
public: |
| 134 |
using StructuredGrid::StructuredGrid; |
| 135 |
|
| 136 |
operator bool() const { return valid(); } |
| 137 |
|
| 138 |
bool valid() const { return StructuredGrid::valid() && reduced(); } |
| 139 |
}; |
| 140 |
|
| 141 |
//--------------------------------------------------------------------------------------------------------------------- |
| 142 |
|
| 143 |
/// @class RegularGrid |
| 144 |
/// @brief Specialization of StructuredGrid, where all rows have the same number of grid points |
| 145 |
class RegularGrid : public StructuredGrid { |
| 146 |
public: |
| 147 |
using StructuredGrid::dx; |
| 148 |
using StructuredGrid::StructuredGrid; |
| 149 |
using StructuredGrid::x; |
| 150 |
using StructuredGrid::xy; |
| 151 |
|
| 152 |
operator bool() const { return valid(); } |
| 153 |
|
| 154 |
bool valid() const { return StructuredGrid::valid() && regular(); } |
| 155 |
|
| 156 |
idx_t nx() const { return nxmax(); } |
| 157 |
|
| 158 |
inline double dx() const { return dx(0); } |
| 159 |
|
| 160 |
inline double x(idx_t i) const { return x(i, 0); } |
| 161 |
|
| 162 |
PointXY xy(idx_t i, idx_t j) const { return PointXY(x(i), y(j)); } |
| 163 |
}; |
| 164 |
|
| 165 |
//--------------------------------------------------------------------------------------------------------------------- |
| 166 |
|
| 167 |
template <class Grid> |
| 168 |
class Gaussian : public Grid { |
| 169 |
public: |
| 170 |
using Grid::Grid; |
| 171 |
|
| 172 |
idx_t N() const { return Grid::ny() / 2; } |
| 173 |
|
| 174 |
protected: |
| 175 |
bool gaussian() const { return Grid::domain().global() && Grid::yspace().type() == "gaussian"; } |
| 176 |
}; |
| 177 |
|
| 178 |
//--------------------------------------------------------------------------------------------------------------------- |
| 179 |
|
| 180 |
/// @class GaussianGrid |
| 181 |
/// @brief Specialization of StructuredGrid, where rows follow a Gaussian distribution |
| 182 |
class GaussianGrid : public Gaussian<StructuredGrid> { |
| 183 |
using grid_t = Gaussian<StructuredGrid>; |
| 184 |
|
| 185 |
public: |
| 186 |
using grid_t::grid_t; |
| 187 |
|
| 188 |
operator bool() const { return valid(); } |
| 189 |
|
| 190 |
bool valid() const { return StructuredGrid::valid() && gaussian(); } |
| 191 |
}; |
| 192 |
|
| 193 |
//--------------------------------------------------------------------------------------------------------------------- |
| 194 |
|
| 195 |
/// @class ReducedGaussianGrid |
| 196 |
/// @brief Specialization of ReducedGrid, where rows follow a Gaussian distribution |
| 197 |
class ReducedGaussianGrid : public Gaussian<ReducedGrid> { |
| 198 |
using grid_t = Gaussian<ReducedGrid>; |
| 199 |
|
| 200 |
public: |
| 201 |
using grid_t::grid_t; |
| 202 |
ReducedGaussianGrid(const std::initializer_list<idx_t>& pl); |
| 203 |
ReducedGaussianGrid(const std::vector<long>& pl, const Domain& = Domain()); |
| 204 |
ReducedGaussianGrid(const std::vector<int>& pl, const Domain& = Domain()); |
| 205 |
ReducedGaussianGrid(const std::vector<long>& pl, const Projection&); |
| 206 |
ReducedGaussianGrid(const std::vector<int>& pl, const Projection&); |
| 207 |
|
| 208 |
operator bool() const { return valid(); } |
| 209 |
|
| 210 |
bool valid() const { return ReducedGrid::valid() && gaussian(); } |
| 211 |
}; |
| 212 |
|
| 213 |
//--------------------------------------------------------------------------------------------------------------------- |
| 214 |
|
| 215 |
/// @class RegularGaussianGrid |
| 216 |
/// @brief Specialization of RegularGaussianGrid, where rows follow a Gaussian distribution |
| 217 |
class RegularGaussianGrid : public Gaussian<RegularGrid> { |
| 218 |
using grid_t = Gaussian<RegularGrid>; |
| 219 |
|
| 220 |
public: |
| 221 |
using grid_t::grid_t; |
| 222 |
RegularGaussianGrid(int N, const Domain& = Domain()); |
| 223 |
|
| 224 |
operator bool() const { return valid(); } |
| 225 |
|
| 226 |
bool valid() const { return RegularGrid::valid() && gaussian(); } |
| 227 |
}; |
| 228 |
|
| 229 |
//--------------------------------------------------------------------------------------------------------------------- |
| 230 |
|
| 231 |
/// @class RegularLonLatGrid |
| 232 |
/// @brief Specialization of RegularGrid, assuming a global domain |
| 233 |
class RegularLonLatGrid : public RegularGrid { |
| 234 |
public: |
| 235 |
using RegularGrid::RegularGrid; |
| 236 |
|
| 237 |
public: |
| 238 |
operator bool() const { return valid(); } |
| 239 |
|
| 240 |
bool valid() const { return RegularGrid::valid() && global_lonlat(); } |
| 241 |
|
| 242 |
inline double lon(idx_t i) const { return x(i); } |
| 243 |
|
| 244 |
inline double lat(idx_t j) const { return y(j); } |
| 245 |
|
| 246 |
PointLonLat lonlat(idx_t i, idx_t j) const { return xy(i, j); } |
| 247 |
|
| 248 |
bool standard() const { return standard_lon() && standard_lat(); } |
| 249 |
bool shifted() const { return shifted_lon() && shifted_lat(); } |
| 250 |
bool shiftedLon() const { return shifted_lon() && standard_lat(); } |
| 251 |
bool shiftedLat() const { return standard_lon() && shifted_lat(); } |
| 252 |
|
| 253 |
protected: |
| 254 |
bool global_lonlat() const { return domain().global() && not projection() && yspace().type() == "linear"; } |
| 255 |
|
| 256 |
bool standard_lon() const { return x(0) == 0.; } |
| 257 |
|
| 258 |
bool standard_lat() const { return y(0) == 90. && ny() % 2 == 1; } |
| 259 |
|
| 260 |
bool shifted_lon() const { return x(0) == 0.5 * 360. / nx(); } |
| 261 |
|
| 262 |
bool shifted_lat() const { return y(0) == 90. - 0.5 * 180. / ny() && ny() % 2 == 0; } |
| 263 |
}; |
| 264 |
|
| 265 |
//--------------------------------------------------------------------------------------------------------------------- |
| 266 |
|
| 267 |
/// @class HealpixGrid |
| 268 |
/// @brief Specialization of StructuredGrid, assuming a global domain |
| 269 |
class HealpixGrid : public StructuredGrid { |
| 270 |
public: |
| 271 |
using grid_t = grid::detail::grid::Healpix; |
| 272 |
|
| 273 |
public: |
| 274 |
HealpixGrid(const Grid&); |
| 275 |
HealpixGrid(int N, const std::string& ordering = "ring"); |
| 276 |
|
| 277 |
operator bool() const { return valid(); } |
| 278 |
|
| 279 |
bool valid() const { return grid_; } |
| 280 |
|
| 281 |
long N() const { return grid_->nxmax() / 4; } |
| 282 |
|
| 283 |
private: |
| 284 |
const grid_t* grid_; |
| 285 |
}; |
| 286 |
|
| 287 |
//--------------------------------------------------------------------------------------------------------------------- |
| 288 |
|
| 289 |
} // namespace atlas |