TECA
The Toolkit for Extreme Climate Analysis
teca_array_collection.h
1 #ifndef teca_array_collection_h
2 #define teca_array_collection_h
3 
4 #include "teca_config.h"
5 #include "teca_dataset.h"
6 #include "teca_shared_object.h"
7 #include "teca_variant_array.h"
8 #include <map>
9 #include <vector>
10 #include <string>
11 
12 TECA_SHARED_OBJECT_FORWARD_DECL(teca_array_collection)
13 
14 /// A collection of named arrays.
15 /**
16  * The array collection is used internally in other mesh based datasets. It
17  * can also be used to process more general data where the arrays have
18  * differing lengths or a non-geometric association.
19  */
21 {
22 public:
23  TECA_DATASET_STATIC_NEW(teca_array_collection)
24  TECA_DATASET_NEW_INSTANCE()
25  TECA_DATASET_NEW_COPY()
26 
27  // set/get temporal metadata
28  TECA_DATASET_METADATA(time, double, 1)
29  TECA_DATASET_METADATA(calendar, std::string, 1)
30  TECA_DATASET_METADATA(time_units, std::string, 1)
31  TECA_DATASET_METADATA(time_step, unsigned long, 1)
32 
33  // set/get attribute metadata
34  TECA_DATASET_METADATA(attributes, teca_metadata, 1)
35 
36  /// reset to empty state
37  void clear();
38 
39  /** declare a set of arrays. requires name,type pairs for ex.
40  * define("c1",int(),"c2",float()) creates 2 arrays in the collection the
41  * first storing int the second storing float.
42  */
43  template<typename nT, typename aT, typename... oT>
44  void declare_set(nT &&a_name, aT a_type, oT &&...args);
45 
46  /// declare a single array
47  template<typename nT, typename aT>
48  void declare(nT &&a_name, aT a_type);
49 
50  /// set the allocator to use with ::declare
51  void set_default_allocator(allocator alloc)
52  { this->default_allocator = alloc; }
53 
54  /** add, return the index of the new entry, or -1 if the array name already
55  * exists.
56  */
58 
59  /** add, return the index of the new entry, or -1 if the array name already
60  * exists.
61  */
62  int append(const std::string &name, p_teca_variant_array array);
63 
64  /** replace the ith array, return 0 on success. the name of the array is
65  * not changed.
66  */
67  int set(unsigned int i, p_teca_variant_array array);
68 
69  /// add or replace the named array, returns 0 on success.
70  int set(const std::string &name, p_teca_variant_array array);
71 
72  /// remove the ith array
73  int remove(unsigned int i);
74 
75  /// remove the named array
76  int remove(const std::string &name);
77 
78  /// Return the number of arrays
79  unsigned int size() const noexcept
80  { return m_arrays.size(); }
81 
82  /// access an array by its id
83  p_teca_variant_array get(unsigned int i)
84  { return m_arrays[i]; }
85 
86  /// access an array by its id
87  const_p_teca_variant_array get(unsigned int i) const
88  { return m_arrays[i]; }
89 
90  /// access a typed array by id
91  template <typename array_t>
92  std::shared_ptr<array_t> get_as(unsigned int i)
93  { return std::dynamic_pointer_cast<array_t>(m_arrays[i]); }
94 
95  /// access a typed array by id
96  template <typename array_t>
97  std::shared_ptr<const array_t> get_as(unsigned int i) const
98  { return std::dynamic_pointer_cast<const array_t>(m_arrays[i]); }
99 
100  /// test for array
101  bool has(const std::string &name) const;
102 
103  /// access a typed array by name
104  template <typename array_t>
105  std::shared_ptr<array_t> get_as(const std::string &name)
106  { return std::dynamic_pointer_cast<array_t>(this->get(name)); }
107 
108  /// access a typed array by name
109  template <typename array_t>
110  std::shared_ptr<const array_t> get_as(const std::string &name) const
111  { return std::dynamic_pointer_cast<const array_t>(this->get(name)); }
112 
113  /// access an array by name
114  p_teca_variant_array get(const std::string &name);
115 
116  /// access an array by name
117  const_p_teca_variant_array get(const std::string &name) const;
118 
119  /// access an array by name
120  p_teca_variant_array operator[](const std::string &name)
121  { return this->get(name); }
122 
123  /// access an array by name
124  const_p_teca_variant_array operator[](const std::string &name) const
125  { return this->get(name); }
126 
127  // Get the name of the ith array
128  std::string &get_name(unsigned int i)
129  { return m_names[i]; }
130 
131  // Get the name of the ith array
132  const std::string &get_name(unsigned int i) const
133  { return m_names[i]; }
134 
135  // Get the list of names
136  std::vector<std::string> &get_names()
137  { return m_names; }
138 
139  // Get the list of names
140  const std::vector<std::string> &get_names() const
141  { return m_names; }
142 
143  /// Return the name of the class
144  std::string get_class_name() const override
145  { return "teca_array_collection"; }
146 
147  /// return an integer identifier uniquely naming the dataset type
148  int get_type_code() const override;
149 
150  /// @copydoc teca_dataset::copy(const const_p_teca_dataset &,allocator)
151  void copy(const const_p_teca_dataset &other,
152  allocator alloc = allocator::malloc) override;
153 
154  /// @copydoc teca_dataset::shallow_copy(const p_teca_dataset &)
155  void shallow_copy(const p_teca_dataset &other) override;
156 
157  /// append
158  int append(const const_p_teca_array_collection &other);
159 
160  /// shallow append
161  int shallow_append(const p_teca_array_collection &other);
162 
163  /// swap
164  void swap(const p_teca_dataset &other) override;
165 
166  /// serialize the data to the given stream for I/O or communication
167  int to_stream(teca_binary_stream &s) const override;
168 
169  /// serialize the data from the given stream for I/O or communication
170  int from_stream(teca_binary_stream &s) override;
171 
172  /// stream to a human readable representation
173  int to_stream(std::ostream &) const override;
175 
176 #if defined(SWIG)
177 protected:
178 #else
179 public:
180 #endif
181  // NOTE: constructors are public to enable std::make_shared. do not use.
182  teca_array_collection() = default;
183 
184 protected:
186  teca_array_collection(const teca_array_collection &&) = delete;
187  void operator=(const teca_array_collection &) = delete;
188  void operator=(const teca_array_collection &&) = delete;
189  void declare_set(){}
190 
191 private:
192  using name_vector_t = std::vector<std::string>;
193  using array_vector_t = std::vector<p_teca_variant_array>;
194  using name_array_map_t = std::map<std::string,unsigned int>;
195 
196  name_vector_t m_names;
197  array_vector_t m_arrays;
198  name_array_map_t m_name_array_map;
199  allocator default_allocator;
200 };
201 
202 // --------------------------------------------------------------------------
203 template<typename nT, typename aT, typename... oT>
204 void teca_array_collection::declare_set(nT &&a_name, aT a_type, oT &&... args)
205 {
206  this->declare(std::forward<nT>(a_name), a_type);
207  this->declare_set(args...);
208 }
209 
210 // --------------------------------------------------------------------------
211 template<typename nT, typename aT>
212 void teca_array_collection::declare(nT &&a_name, aT)
213 {
214  unsigned int id = m_arrays.size();
215  m_names.push_back(a_name);
216  m_arrays.push_back(teca_variant_array_impl<aT>::New(this->default_allocator));
217  m_name_array_map.emplace(std::forward<nT>(a_name), id);
218 }
219 
220 #endif
A collection of named arrays.
Definition: teca_array_collection.h:21
std::shared_ptr< array_t > get_as(unsigned int i)
access a typed array by id
Definition: teca_array_collection.h:92
void shallow_copy(const p_teca_dataset &other) override
int remove(unsigned int i)
remove the ith array
void swap(const p_teca_dataset &other) override
swap
std::shared_ptr< const array_t > get_as(unsigned int i) const
access a typed array by id
Definition: teca_array_collection.h:97
p_teca_variant_array operator[](const std::string &name)
access an array by name
Definition: teca_array_collection.h:120
int set(const std::string &name, p_teca_variant_array array)
add or replace the named array, returns 0 on success.
void declare_set(nT &&a_name, aT a_type, oT &&...args)
Definition: teca_array_collection.h:204
void declare(nT &&a_name, aT a_type)
declare a single array
Definition: teca_array_collection.h:212
int to_stream(std::ostream &) const override
stream to a human readable representation
unsigned int size() const noexcept
Return the number of arrays.
Definition: teca_array_collection.h:79
const_p_teca_variant_array operator[](const std::string &name) const
access an array by name
Definition: teca_array_collection.h:124
std::string get_class_name() const override
Return the name of the class.
Definition: teca_array_collection.h:144
std::shared_ptr< array_t > get_as(const std::string &name)
access a typed array by name
Definition: teca_array_collection.h:105
int get_type_code() const override
return an integer identifier uniquely naming the dataset type
const_p_teca_variant_array get(unsigned int i) const
access an array by its id
Definition: teca_array_collection.h:87
p_teca_variant_array get(unsigned int i)
access an array by its id
Definition: teca_array_collection.h:83
int to_stream(teca_binary_stream &s) const override
serialize the data to the given stream for I/O or communication
bool has(const std::string &name) const
test for array
int append(const std::string &name, p_teca_variant_array array)
const_p_teca_variant_array get(const std::string &name) const
access an array by name
int shallow_append(const p_teca_array_collection &other)
shallow append
int append(const const_p_teca_array_collection &other)
append
int remove(const std::string &name)
remove the named array
int append(p_teca_variant_array array)
void copy(const const_p_teca_dataset &other, allocator alloc=allocator::malloc) override
std::shared_ptr< const array_t > get_as(const std::string &name) const
access a typed array by name
Definition: teca_array_collection.h:110
p_teca_variant_array get(const std::string &name)
access an array by name
int set(unsigned int i, p_teca_variant_array array)
int from_stream(teca_binary_stream &s) override
serialize the data from the given stream for I/O or communication
Serialize objects into a binary stream.
Definition: teca_binary_stream.h:17
Interface for TECA datasets.
Definition: teca_dataset.h:232
virtual int from_stream(teca_binary_stream &)
deserialize the dataset from the given stream for I/O or communication
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:22
The concrete implementation of our type agnostic container for contiguous arrays.
Definition: teca_variant_array_impl.h:380
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
std::shared_ptr< teca_variant_array > p_teca_variant_array
Definition: teca_variant_array.h:27
std::shared_ptr< const teca_variant_array > const_p_teca_variant_array
Definition: teca_variant_array.h:27