| 1 |
/* |
| 2 |
* (C) Copyright 2020 UCAR |
| 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 |
#pragma once |
| 9 |
|
| 10 |
#include <string> |
| 11 |
|
| 12 |
#include "atlas/grid/Grid.h" |
| 13 |
#include "atlas/grid/Tiles.h" |
| 14 |
#include "atlas/grid/detail/grid/CubedSphere.h" |
| 15 |
|
| 16 |
namespace atlas { |
| 17 |
|
| 18 |
//--------------------------------------------------------------------------------------------------------------------- |
| 19 |
class CubedSphereGrid; |
| 20 |
|
| 21 |
/* |
| 22 |
Grid |
| 23 |
| |
| 24 |
+----------+----------+-------------------------------+ |
| 25 |
| | | |
| 26 |
StructuredGrid UnstructuredGrid CubedSphere |
| 27 |
| |
| 28 |
+--------------------+-----------------------+ |
| 29 |
| | | |
| 30 |
ReducedGrid GaussianGrid RegularGrid |
| 31 |
| | | | | |
| 32 |
+--------+--------+ +--------+--------+ +-----+ |
| 33 |
| | | |
| 34 |
ReducedGaussianGrid RegularGaussianGrid RegularLonLatGrid |
| 35 |
*/ |
| 36 |
|
| 37 |
//--------------------------------------------------------------------------------------------------------------------- |
| 38 |
|
| 39 |
/*! @class CubedSphereGrid |
| 40 |
* @brief Specialization of Grid, where the grid is a cubed sphere |
| 41 |
* @details |
| 42 |
* @copydetails Grid |
| 43 |
* @dotfile classatlas_1_1CubedSphereGrid__inherit__graph.dot |
| 44 |
*/ |
| 45 |
|
| 46 |
|
| 47 |
/* |
| 48 |
Description of indices |
| 49 |
---------------------- |
| 50 |
This class and the implementations in detail/grid/CubedSphere.h are for the structured cubed-sphere |
| 51 |
grid. |
| 52 |
|
| 53 |
In the following there are indices in the grid, denoted idx_t i, idx_t j, idx_t t and positions in |
| 54 |
the grid, denoted double x, double y and t. For the cubed-sphere each face of the tile is |
| 55 |
represented by rank 2 arrays with index in that array denoted by i and j. The face is denoted by t. |
| 56 |
|
| 57 |
For a regular like grid, translating between i, j and x, y could be done from a starting point and |
| 58 |
the grid spacing. But this is not possible for the cubed-sphere grid, where the spacing is not |
| 59 |
necessarily regular and the array not rank 2 for the entire grid, i.e. is made more complicated |
| 60 |
by the tile index. |
| 61 |
|
| 62 |
x and y represent the position on a plane with the tiles arranged in an 'unfolded manner', shown |
| 63 |
below. Tile t is carried around also as part of the array xy in order to do the projection properly. |
| 64 |
|
| 65 |
Cubed-sphere Panel Arrangement: |
| 66 |
|
| 67 |
y |
| 68 |
|
| 69 |
^ ....... |
| 70 |
| | : |
| 71 |
| | 2 : |
| 72 |
| | : |
| 73 |
| --->--- |
| 74 |
| *....... ....... ------- ------- |
| 75 |
| | :| :| :| : |
| 76 |
| | 0 :| 1 :v 3 :v 4 : |
| 77 |
| | :| :| :| : |
| 78 |
| --->--- --->--* ....... ....... |
| 79 |
| ------- |
| 80 |
| | : |
| 81 |
| v 5 : |
| 82 |
| | : |
| 83 |
| ....... |
| 84 |
-----------------------------------------------------------> x |
| 85 |
|
| 86 |
Key |
| 87 |
Solid lines: left and bottom edges of panel |
| 88 |
Dotted lines: right and top edges of panel |
| 89 |
>, <, v: direction of increasing index in first dimension |
| 90 |
*: location of two extra points (ngrid = 6 * NCube * NCube + 2) |
| 91 |
|
| 92 |
---------------------------------------------------------------------------------------------------- |
| 93 |
|
| 94 |
NCube = 3 example ( 6 * NCube * NCube + 2 = 56 grid points ) |
| 95 |
|
| 96 |
Index of each point within the grid (i,j,t): |
| 97 |
|
| 98 |
() denotes actual points in the grids |
| 99 |
<> denotes points that are duplicates (not part of the grid) but shown for completeness of the grid |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
<0,3,0>-<0,2,4>-<0,1,4>-<0,0,4> |
| 104 |
| | |
| 105 |
| | |
| 106 |
(0,1,2) (1,2,2) (2,2,2) <0,2,3> |
| 107 |
| | |
| 108 |
| | |
| 109 |
(0,1,2) (1,1,2) (2,1,2) <0,1,3> |
| 110 |
| | |
| 111 |
| | |
| 112 |
(0,3,0)-<0,1,2>-<0,1,2>-(0,0,2)-(1,0,2)-(2,0,2)-(0,0,3)-(0,1,3)-(0,2,3)-(0,0,4)-(0,1,4)-(0,2,4)-<0,3,0> |
| 113 |
| | | | | |
| 114 |
| | | | | |
| 115 |
(0,2,0) (1,2,0) (2,2,0) (0,1,1) (1,2,1) (2,2,1) (1,0,3) (1,1,3) (1,2,3) (1,0,4) (1,1,4) (1,2,4) <0,1,0> |
| 116 |
| | | | | |
| 117 |
| | | | | |
| 118 |
(0,1,0) (1,1,0) (2,1,0) (0,1,1) (1,1,1) (2,1,1) (2,0,3) (2,1,3) (2,2,3) (2,0,4) (2,1,4) (2,2,4) <0,1,0> |
| 119 |
| | | | | |
| 120 |
| | | | | |
| 121 |
(0,0,0)-(1,0,0)-(2,0,0)-(0,0,1)-(1,0,1)-(2,0,1)-(3,0,1)-<2,0,5>-<1,0,5>-(0,0,5)-(0,1,5)-(0,2,5)-<0,0,0> |
| 122 |
| | |
| 123 |
| | |
| 124 |
(1,0,5) (1,1,5) (1,2,5) <1,0,0> |
| 125 |
| | |
| 126 |
| | |
| 127 |
(2,0,5) (2,1,5) (2,2,5) <2,0,0> |
| 128 |
| | |
| 129 |
| | |
| 130 |
<3,0,1>-<2,0,1>-<1,0,1>-<0,0,1> |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
Position of each point within the grid (x,y,t): |
| 135 |
|
| 136 |
() denotes actual points in the grids |
| 137 |
+ denotes duplicate points |
| 138 |
|
| 139 |
For the xyt, t is only required in order to pass to the projection and thus know how to rotate |
| 140 |
the projection of tile 0 to obtain the other tiles |
| 141 |
|
| 142 |
|
| 143 |
+-------+-------+-------+ |
| 144 |
| | |
| 145 |
| | |
| 146 |
(3,8,2) (4,8,2) (5,8,2) + |
| 147 |
| | |
| 148 |
| | |
| 149 |
(3,7,2) (4,7,2) (5,7,2) + |
| 150 |
| | |
| 151 |
| | |
| 152 |
(0,6,0)----+-------+----(3,6,2)-(4,6,2)-(5,6,2)-(6,6,3)-(7,6,3)-(8,6,3)-(9,6,4)-(10,6,4)-(11,6,4)----+ |
| 153 |
| | | | | |
| 154 |
| | | | | |
| 155 |
(0,5,0) (1,5,0) (2,5,0) (3,5,1) (4,5,1) (5,5,1) (6,5,3) (7,5,3) (8,5,3) (9,5,4) (10,5,4) (11,5,4) + |
| 156 |
| | | | | |
| 157 |
| | | | | |
| 158 |
(0,4,0) (1,4,0) (2,4,0) (3,4,1) (4,4,1) (5,4,1) (6,4,3) (7,4,3) (8,4,3) (9,4,4) (10,4,4) (11,4,4) + |
| 159 |
| | | | | |
| 160 |
| | | | | |
| 161 |
(0,3,0)-(1,3,0)-(2,3,0)-(3,3,1)-(4,3,1)-(5,3,1)-(6,3,1)----+-------+----(9,3,5)-(10,3,5)-(11,3,5)----+ |
| 162 |
| | |
| 163 |
| | |
| 164 |
(9,2,5) (10,2,5) (11,2,5) + |
| 165 |
| | |
| 166 |
| | |
| 167 |
(9,1,5) (10,1,5) (11,1,5) + |
| 168 |
| | |
| 169 |
| | |
| 170 |
+--------+-------+--------+ |
| 171 |
|
| 172 |
|
| 173 |
Position in the grid iterator/mesh: |
| 174 |
|
| 175 |
First element of each tile: {0, 10, 20, 29, 38, 47} |
| 176 |
|
| 177 |
+---+---+---+ |
| 178 |
| | |
| 179 |
26 27 28 + |
| 180 |
| | |
| 181 |
23 24 25 + |
| 182 |
| | |
| 183 |
9---+---+--20--21--22--29--32--35---38--41--44---+ |
| 184 |
| | | | | |
| 185 |
6 7 8 17 18 19 30 33 36 39 42 45 + |
| 186 |
| | | | | |
| 187 |
3 4 5 14 15 16 31 34 37 40 43 46 + |
| 188 |
| | | | | |
| 189 |
0---1---2--10--11--12--13---+---+---47--50--53---+ |
| 190 |
| | |
| 191 |
48 51 54 + |
| 192 |
| | |
| 193 |
49 52 55 + |
| 194 |
| | |
| 195 |
+---+---+---+ |
| 196 |
|
| 197 |
---------------------------------------------------------------------------------------------------- |
| 198 |
|
| 199 |
*/ |
| 200 |
|
| 201 |
|
| 202 |
namespace temporary { |
| 203 |
|
| 204 |
class IteratorTIJ { |
| 205 |
using implementation_t = grid::detail::grid::CubedSphere::IteratorTIJ; |
| 206 |
|
| 207 |
public: |
| 208 |
using difference_type = implementation_t::difference_type; |
| 209 |
using iterator_category = implementation_t::iterator_category; |
| 210 |
using value_type = implementation_t::value_type; |
| 211 |
using pointer = implementation_t::pointer; |
| 212 |
using reference = implementation_t::reference; |
| 213 |
|
| 214 |
public: |
| 215 |
IteratorTIJ(std::unique_ptr<implementation_t> iterator): iterator_(std::move(iterator)) {} |
| 216 |
|
| 217 |
bool next(value_type& xy) { return iterator_->next(xy); } |
| 218 |
|
| 219 |
reference operator*() const { return iterator_->operator*(); } |
| 220 |
|
| 221 |
const IteratorTIJ& operator++() { |
| 222 |
iterator_->operator++(); |
| 223 |
return *this; |
| 224 |
} |
| 225 |
|
| 226 |
const IteratorTIJ& operator+=(difference_type distance) { |
| 227 |
iterator_->operator+=(distance); |
| 228 |
return *this; |
| 229 |
} |
| 230 |
|
| 231 |
friend difference_type operator-(const IteratorTIJ& last, const IteratorTIJ& first) { |
| 232 |
return first.iterator_->distance(*last.iterator_); |
| 233 |
} |
| 234 |
|
| 235 |
bool operator==(const IteratorTIJ& other) const { return iterator_->operator==(*other.iterator_); } |
| 236 |
bool operator!=(const IteratorTIJ& other) const { return iterator_->operator!=(*other.iterator_); } |
| 237 |
|
| 238 |
private: |
| 239 |
difference_type distance(const IteratorTIJ& other) const { return iterator_->distance(*other.iterator_); } |
| 240 |
|
| 241 |
private: |
| 242 |
std::unique_ptr<implementation_t> iterator_; |
| 243 |
}; |
| 244 |
|
| 245 |
class IterateTIJ { |
| 246 |
public: |
| 247 |
using iterator = IteratorTIJ; |
| 248 |
using const_iterator = iterator; |
| 249 |
using Grid = grid::detail::grid::CubedSphere; |
| 250 |
|
| 251 |
public: |
| 252 |
IterateTIJ(const Grid& grid): grid_(grid) {} |
| 253 |
iterator begin() const { return grid_.tij_begin(); } |
| 254 |
iterator end() const { return grid_.tij_end(); } |
| 255 |
|
| 256 |
private: |
| 257 |
const Grid& grid_; |
| 258 |
}; |
| 259 |
|
| 260 |
|
| 261 |
} // namespace temporary |
| 262 |
|
| 263 |
|
| 264 |
class CubedSphereGrid : public Grid { |
| 265 |
public: |
| 266 |
using grid_t = grid::detail::grid::CubedSphere; |
| 267 |
using CubedSphereProjection = projection::detail::CubedSphereProjectionBase; |
| 268 |
using CubedSphereTiles = grid::CubedSphereTiles; |
| 269 |
|
| 270 |
public: |
| 271 |
CubedSphereGrid(); |
| 272 |
CubedSphereGrid(const Grid&); |
| 273 |
CubedSphereGrid(const Grid::Implementation*); |
| 274 |
CubedSphereGrid(const std::string& name); |
| 275 |
CubedSphereGrid(const Config&); |
| 276 |
CubedSphereGrid(const int&, const Projection& = Projection()); |
| 277 |
|
| 278 |
operator bool() const { return valid(); } |
| 279 |
|
| 280 |
bool valid() const { return grid_; } |
| 281 |
|
| 282 |
using Grid::xy; |
| 283 |
void xyt(idx_t i, idx_t j, idx_t t, double xyt[]) const { grid_->xyt(i, j, t, xyt); } |
| 284 |
PointXY xyt(idx_t i, idx_t j, idx_t t) const { return grid_->xyt(i, j, t); } |
| 285 |
// Given indexes in the array (i, j, t) return position array xyt |
| 286 |
|
| 287 |
void xy(idx_t i, idx_t j, idx_t t, double xy[]) const { grid_->xy(i, j, t, xy); } |
| 288 |
PointXY xy(idx_t i, idx_t j, idx_t t) const { return grid_->xy(i, j, t); } |
| 289 |
|
| 290 |
using Grid::lonlat; |
| 291 |
// Given indexes in the array (i, j) return lat/lon array (via the projection) |
| 292 |
void lonlat(idx_t i, idx_t j, idx_t t, double lonlat[]) const { grid_->lonlat(i, j, t, lonlat); } |
| 293 |
|
| 294 |
// Given indexes in the array (i, j, t) return lat/lon as a PointLonLat object |
| 295 |
PointLonLat lonlat(idx_t i, idx_t j, idx_t t) const { return grid_->lonlat(i, j, t); } |
| 296 |
|
| 297 |
// Return the size of the cubed sphere grid, where N is the number of grid boxes along the edge of a tile |
| 298 |
inline int N() const { return grid_->N(); } |
| 299 |
|
| 300 |
/// @brief return tiles object. |
| 301 |
inline CubedSphereTiles tiles() const { return grid_->tiles(); } |
| 302 |
|
| 303 |
/// @brief return cubed sphere projection object. |
| 304 |
inline const CubedSphereProjection& cubedSphereProjection() const { |
| 305 |
return dynamic_cast<const CubedSphereProjection&>(*projection().get()); |
| 306 |
}; |
| 307 |
|
| 308 |
temporary::IterateTIJ tij() const { return temporary::IterateTIJ(*grid_); } |
| 309 |
|
| 310 |
const std::string& stagger() const { return grid_->stagger(); } |
| 311 |
|
| 312 |
private: |
| 313 |
const grid_t* grid_; |
| 314 |
}; |
| 315 |
|
| 316 |
//--------------------------------------------------------------------------------------------------------------------- |
| 317 |
|
| 318 |
} // namespace atlas |