atlas
StructuredGrid.h
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 
22 namespace atlas {
23 
24 //---------------------------------------------------------------------------------------------------------------------
25 class StructuredGrid;
26 class RegularGrid;
27 class GaussianGrid;
28 class ReducedGaussianGrid;
29 class RegularGaussianGrid;
30 class RegularLonLatGrid;
31 class ShiftedLonLatGrid;
32 class HealpixGrid;
33 
34 /*
35  Grid
36  |
37  +----------+----------+
38  | |
39  StructuredGrid UnstructuredGrid
40  |
41  +--------------------+-----------------------+------------------+
42  | | | |
43  ReducedGrid GaussianGrid RegularGrid HealpixGrid
44  | | | | |
45  +--------+--------+ +--------+--------+ +-----+
46  | | |
47  ReducedGaussianGrid RegularGaussianGrid RegularLonLatGrid
48 */
49 
50 //---------------------------------------------------------------------------------------------------------------------
51 
58 class StructuredGrid : public Grid {
59 public:
61  using XSpace = grid_t::XSpace;
62  using YSpace = grid_t::YSpace;
63 
64 public:
66  StructuredGrid( const Grid& );
67  StructuredGrid( const Grid::Implementation* );
68  StructuredGrid( const std::string& name, const Domain& = Domain() );
69  StructuredGrid( const std::string& name, const Projection&, const Domain& = Domain() );
70  StructuredGrid( const Config& );
71  StructuredGrid( const XSpace&, const YSpace&, const Projection& = Projection(), const Domain& = Domain() );
72  StructuredGrid( const Grid&, const Domain& );
73 
74  operator bool() const { return valid(); }
75 
76  bool valid() const { return grid_; }
77 
78  inline idx_t ny() const { return grid_->ny(); }
79 
80  inline idx_t nx( idx_t j ) const { return grid_->nx( j ); }
81 
82  inline const std::vector<idx_t>& nx() const { return grid_->nx(); }
83 
84  inline idx_t nxmax() const { return grid_->nxmax(); }
85 
86  inline const std::vector<double>& y() const { return grid_->y(); }
87 
89  inline double x( idx_t i, idx_t j ) const { return grid_->x( i, j ); }
90 
92  inline double y( idx_t j ) const { return grid_->y( j ); }
93 
95  inline double dx( idx_t j ) const { return grid_->dx( j ); }
96 
98  inline double xmin( idx_t j ) const { return grid_->xmin( j ); }
99 
100 
101  using Grid::xy;
102  void xy( idx_t i, idx_t j, double xy[] ) const { grid_->xy( i, j, xy ); }
103 
104  using Grid::lonlat;
105  void lonlat( idx_t i, idx_t j, double lonlat[] ) const { grid_->lonlat( i, j, lonlat ); }
106 
107  PointXY xy( idx_t i, idx_t j ) const { return PointXY( x( i, j ), y( j ) ); }
108 
109  PointLonLat lonlat( idx_t i, idx_t j ) const { return grid_->lonlat( i, j ); }
110 
111  inline bool reduced() const { return grid_->reduced(); }
112 
113  inline bool regular() const { return not reduced(); }
114 
115  bool periodic() const { return grid_->periodic(); }
116 
117  const XSpace& xspace() const { return grid_->xspace(); }
118 
119  const YSpace& yspace() const { return grid_->yspace(); }
120 
121  gidx_t index( idx_t i, idx_t j ) const { return grid_->index( i, j ); }
122 
123  void index2ij( gidx_t gidx, idx_t& i, idx_t& j ) const { grid_->index2ij( gidx, i, j ); }
124 
125 private:
126  const grid_t* grid_;
127 };
128 
129 //---------------------------------------------------------------------------------------------------------------------
130 
133 class ReducedGrid : public StructuredGrid {
134 public:
135  using StructuredGrid::StructuredGrid;
136 
137  operator bool() const { return valid(); }
138 
139  bool valid() const { return StructuredGrid::valid() && reduced(); }
140 };
141 
142 //---------------------------------------------------------------------------------------------------------------------
143 
146 class RegularGrid : public StructuredGrid {
147 public:
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 x( idx_t i ) const { return x( i, 0 ); }
159 
160  PointXY xy( idx_t i, idx_t j ) const { return PointXY( x( i ), y( j ) ); }
161 };
162 
163 //---------------------------------------------------------------------------------------------------------------------
164 
165 template <class Grid>
166 class Gaussian : public Grid {
167 public:
168  using Grid::Grid;
169 
170  idx_t N() const { return Grid::ny() / 2; }
171 
172 protected:
173  bool gaussian() const { return Grid::domain().global() && Grid::yspace().type() == "gaussian"; }
174 };
175 
176 //---------------------------------------------------------------------------------------------------------------------
177 
180 class GaussianGrid : public Gaussian<StructuredGrid> {
182 
183 public:
184  using grid_t::grid_t;
185 
186  operator bool() const { return valid(); }
187 
188  bool valid() const { return StructuredGrid::valid() && gaussian(); }
189 };
190 
191 //---------------------------------------------------------------------------------------------------------------------
192 
195 class ReducedGaussianGrid : public Gaussian<ReducedGrid> {
197 
198 public:
199  using grid_t::grid_t;
200  ReducedGaussianGrid( const std::initializer_list<idx_t>& pl );
201  ReducedGaussianGrid( const std::vector<long>& pl, const Domain& = Domain() );
202  ReducedGaussianGrid( const std::vector<int>& pl, const Domain& = Domain() );
203  ReducedGaussianGrid( const std::vector<long>& pl, const Projection& );
204  ReducedGaussianGrid( const std::vector<int>& pl, const Projection& );
205 
206  operator bool() const { return valid(); }
207 
208  bool valid() const { return ReducedGrid::valid() && gaussian(); }
209 };
210 
211 //---------------------------------------------------------------------------------------------------------------------
212 
215 class RegularGaussianGrid : public Gaussian<RegularGrid> {
217 
218 public:
219  using grid_t::grid_t;
220  RegularGaussianGrid( int N, const Domain& = Domain() );
221 
222  operator bool() const { return valid(); }
223 
224  bool valid() const { return RegularGrid::valid() && gaussian(); }
225 };
226 
227 //---------------------------------------------------------------------------------------------------------------------
228 
232 public:
233  using RegularGrid::RegularGrid;
234 
235 public:
236  operator bool() const { return valid(); }
237 
238  bool valid() const { return RegularGrid::valid() && global_lonlat(); }
239 
240  inline double lon( idx_t i ) const { return x( i ); }
241 
242  inline double lat( idx_t j ) const { return y( j ); }
243 
244  PointLonLat lonlat( idx_t i, idx_t j ) const { return xy( i, j ); }
245 
246  bool standard() const { return standard_lon() && standard_lat(); }
247  bool shifted() const { return shifted_lon() && shifted_lat(); }
248  bool shiftedLon() const { return shifted_lon() && standard_lat(); }
249  bool shiftedLat() const { return standard_lon() && shifted_lat(); }
250 
251 protected:
252  bool global_lonlat() const { return domain().global() && not projection() && yspace().type() == "linear"; }
253 
254  bool standard_lon() const { return x( 0 ) == 0.; }
255 
256  bool standard_lat() const { return y( 0 ) == 90. && ny() % 2 == 1; }
257 
258  bool shifted_lon() const { return x( 0 ) == 0.5 * 360. / nx(); }
259 
260  bool shifted_lat() const { return y( 0 ) == 90. - 0.5 * 180. / ny() && ny() % 2 == 0; }
261 };
262 
263 //---------------------------------------------------------------------------------------------------------------------
264 
267 class HealpixGrid : public StructuredGrid {
268 public:
270 
271 public:
272  HealpixGrid( const Grid& );
273  HealpixGrid( int N );
274 
275  bool valid() { return grid_; }
276 
277 private:
278  const grid_t* grid_;
279 };
280 
281 //---------------------------------------------------------------------------------------------------------------------
282 
283 } // namespace atlas
double xmin(idx_t j) const
x coordinate of beginning of a given grid row {j}
Definition: StructuredGrid.h:98
Specialization of RegularGaussianGrid, where rows follow a Gaussian distribution. ...
Definition: StructuredGrid.h:215
Specialization of RegularGrid, assuming a global domain.
Definition: StructuredGrid.h:231
Structured Grid.
Definition: Structured.h:39
Definition: Spacing.h:44
Specialization of StructuredGrid, where rows follow a Gaussian distribution.
Definition: StructuredGrid.h:180
Definition: StructuredGrid.h:166
Specialization of StructuredGrid, where not all rows have the same number of grid points...
Definition: StructuredGrid.h:133
Specialization of ReducedGrid, where rows follow a Gaussian distribution.
Definition: StructuredGrid.h:195
double y(idx_t j) const
y coordinate for given grid row {j}
Definition: StructuredGrid.h:92
Point in longitude-latitude coordinate system.
Definition: Point.h:103
Definition: Projection.h:49
Specialization of StructuredGrid, where all rows have the same number of grid points.
Definition: StructuredGrid.h:146
Most general grid container.
Definition: Grid.h:64
Definition: Domain.h:48
long gidx_t
Integer type for global indices.
Definition: config.h:34
double x(idx_t i, idx_t j) const
x coordinate for given grid point {i,j}
Definition: StructuredGrid.h:89
double dx(idx_t j) const
increment in x for a given grid row {j}
Definition: StructuredGrid.h:95
Specialization of Grid, where the grid can be represented by rows with uniform distribution.
Definition: StructuredGrid.h:58
Point in arbitrary XY-coordinate system.
Definition: Point.h:40
Contains all atlas classes and methods.
Definition: atlas-grids.cc:33
long idx_t
Integer type for indices in connectivity tables.
Definition: config.h:42
Specialization of StructuredGrid, assuming a global domain.
Definition: StructuredGrid.h:267
Configuration class used to construct various atlas components.
Definition: Config.h:27
Definition: Healpix.h:20