atlas
Grid.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 <algorithm>
14 #include <iterator>
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 #include "atlas/domain/Domain.h"
20 #include "atlas/library/config.h"
21 #include "atlas/projection/Projection.h"
22 #include "atlas/util/Object.h"
23 
24 namespace eckit {
25 class Hash;
26 }
27 namespace atlas {
28 class PointXY;
29 class PointLonLat;
30 namespace util {
31 class Config;
32 };
33 } // namespace atlas
34 
35 namespace atlas {
36 namespace grid {
37 namespace detail {
38 namespace grid {
39 
40 class GridObserver;
41 
42 class Grid : public util::Object {
43 public: // types
45  using Domain = atlas::Domain;
47  using Spec = atlas::util::Config;
48  using uid_t = std::string;
49  using hash_t = std::string;
50 
51  template <typename Derived, typename Point>
52  class IteratorT {
53  public:
54  using difference_type = size_t;
55  using iterator_category = std::random_access_iterator_tag;
56  using value_type = Point;
57  using pointer = const Point*;
58  using reference = const Point&;
59 
60  public:
61  virtual bool next( value_type& ) = 0;
62  virtual reference operator*() const = 0;
63  virtual const Derived& operator++() = 0;
64  virtual const Derived& operator+=( difference_type ) = 0;
65  virtual bool operator==( const Derived& ) const = 0;
66  virtual bool operator!=( const Derived& ) const = 0;
67  virtual difference_type distance( const Derived& ) const = 0;
68  virtual std::unique_ptr<Derived> clone() const = 0;
69  virtual ~IteratorT() {}
70  };
71  class IteratorXY : public IteratorT<IteratorXY, PointXY> {};
72  class IteratorLonLat : public IteratorT<IteratorLonLat, PointLonLat> {};
73 
74 public: // methods
75  static const Grid* create( const Config& );
76 
77  static const Grid* create( const std::string& name );
78 
79  static const Grid* create( const std::string& name, const Config& );
80 
81  static const Grid* create( const Grid&, const Domain& );
82 
84  Grid();
85 
87  virtual ~Grid();
88 
90  virtual std::string name() const = 0;
91  virtual std::string type() const = 0;
92 
95  virtual uid_t uid() const;
96 
98  virtual void hash( eckit::Hash& ) const = 0;
99 
101  std::string hash() const;
102 
104  const Domain& domain() const { return domain_; }
105 
107  virtual RectangularLonLatDomain lonlatBoundingBox() const = 0;
108 
109  virtual size_t footprint() const { return 0; }
110 
113  const Projection& projection() const { return projection_; }
114 
117  // classes should compute it at construction
118  virtual idx_t size() const = 0;
119 
120  virtual Spec spec() const = 0;
121 
122  virtual std::unique_ptr<IteratorXY> xy_begin() const = 0;
123  virtual std::unique_ptr<IteratorXY> xy_end() const = 0;
124  virtual std::unique_ptr<IteratorLonLat> lonlat_begin() const = 0;
125  virtual std::unique_ptr<IteratorLonLat> lonlat_end() const = 0;
126 
127  void attachObserver( GridObserver& ) const;
128  void detachObserver( GridObserver& ) const;
129 
130  virtual Config meshgenerator() const;
131  virtual Config partitioner() const;
132 
133 protected: // methods
135  virtual void print( std::ostream& ) const = 0;
136 
137 private: // methods
138  friend std::ostream& operator<<( std::ostream& s, const Grid& p ) {
139  p.print( s );
140  return s;
141  }
142 
143 private: // members
145  mutable uid_t uid_;
146 
148  mutable hash_t hash_;
149 
150 protected: // members
151  Projection projection_;
152  Domain domain_;
153 
154  mutable std::vector<GridObserver*> grid_observers_;
155 };
156 
158 private:
159  std::vector<const Grid*> registered_grids_;
160 
161 public:
162  void registerGrid( const Grid& grid ) {
163  if ( std::find( registered_grids_.begin(), registered_grids_.end(), &grid ) == registered_grids_.end() ) {
164  registered_grids_.push_back( &grid );
165  grid.attachObserver( *this );
166  }
167  }
168  void unregisterGrid( const Grid& grid ) {
169  auto found = std::find( registered_grids_.begin(), registered_grids_.end(), &grid );
170  if ( found != registered_grids_.end() ) {
171  registered_grids_.erase( found );
172  grid.detachObserver( *this );
173  }
174  }
175  virtual ~GridObserver() {
176  for ( auto grid : registered_grids_ ) {
177  grid->detachObserver( *this );
178  }
179  }
180  virtual void onGridDestruction( Grid& ) = 0;
181 };
182 
183 //----------------------------------------------------------------------------------------------------------------------
184 
185 extern "C" {
186 idx_t atlas__grid__Grid__size( Grid* This );
187 Grid::Spec* atlas__grid__Grid__spec( Grid* This );
188 void atlas__grid__Grid__uid( const Grid* This, char*& uid, int& size );
189 Grid::Domain::Implementation* atlas__grid__Grid__lonlat_bounding_box( const Grid* This );
190 }
191 
192 } // namespace grid
193 } // namespace detail
194 } // namespace grid
195 } // namespace atlas
const Domain & domain() const
Definition: Grid.h:104
Definition: Projection.h:49
const Projection & projection() const
Definition: Grid.h:113
Definition: Domain.h:130
virtual void print(std::ostream &) const =0
Fill provided me.
Definition: Object.h:18
Definition: Domain.h:19
Definition: Grid.h:42
Definition: Domain.h:48
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
Configuration class used to construct various atlas components.
Definition: Config.h:27