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_dataset.h"
5 #include "teca_shared_object.h"
6 #include "teca_variant_array.h"
7 #include <map>
8 #include <vector>
9 #include <string>
10 
11 TECA_SHARED_OBJECT_FORWARD_DECL(teca_array_collection)
12 
13 /// A collection of named arrays.
14 /**
15  * The array collection is used internally in other mesh based datasets. It
16  * can also be used to process more general data where the arrays have
17  * differing lengths or a non-geometric association.
18  */
20 {
21 public:
22  TECA_DATASET_STATIC_NEW(teca_array_collection)
23  TECA_DATASET_NEW_INSTANCE()
24  TECA_DATASET_NEW_COPY()
25 
26  // set/get temporal metadata
27  TECA_DATASET_METADATA(time, double, 1)
28  TECA_DATASET_METADATA(calendar, std::string, 1)
29  TECA_DATASET_METADATA(time_units, std::string, 1)
30  TECA_DATASET_METADATA(time_step, unsigned long, 1)
31 
32  // set/get attribute metadata
33  TECA_DATASET_METADATA(attributes, teca_metadata, 1)
34 
35  /// reset to empty state
36  void clear();
37 
38  /** declare a set of arrays. requires name,type pairs for ex.
39  * define("c1",int(),"c2",float()) creates 2 arrays in the collection the
40  * first storing int the second storing float.
41  */
42  template<typename nT, typename aT, typename... oT>
43  void declare_set(nT &&a_name, aT a_type, oT &&...args);
44 
45  /// declare a single array
46  template<typename nT, typename aT>
47  void declare(nT &&a_name, aT a_type);
48 
49  /** add, return the index of the new entry, or -1 if the array name already
50  * exists.
51  */
52  int append(p_teca_variant_array array);
53 
54  /** add, return the index of the new entry, or -1 if the array name already
55  * exists.
56  */
57  int append(const std::string &name, p_teca_variant_array array);
58 
59  /** replace the ith array, return 0 on success. the name of the array is
60  * not changed.
61  */
62  int set(unsigned int i, p_teca_variant_array array);
63 
64  /// add or replace the named array, returns 0 on success.
65  int set(const std::string &name, p_teca_variant_array array);
66 
67  /// remove the ith array
68  int remove(unsigned int i);
69 
70  /// remove the named array
71  int remove(const std::string &name);
72 
73  /// Return the number of arrays
74  unsigned int size() const noexcept
75  { return m_arrays.size(); }
76 
77  /// access an array by its by id
78  p_teca_variant_array get(unsigned int i)
79  { return m_arrays[i]; }
80 
81  /// access an array by its by id
82  const_p_teca_variant_array get(unsigned int i) const
83  { return m_arrays[i]; }
84 
85  /// test for array
86  bool has(const std::string &name) const;
87 
88  /// access an array by name
89  p_teca_variant_array get(const std::string &name);
90 
91  /// access an array by name
92  const_p_teca_variant_array get(const std::string &name) const;
93 
94  /// access an array by name
95  p_teca_variant_array operator[](const std::string &name)
96  { return this->get(name); }
97 
98  /// access an array by name
99  const_p_teca_variant_array operator[](const std::string &name) const
100  { return this->get(name); }
101 
102  // Get the name of the ith array
103  std::string &get_name(unsigned int i)
104  { return m_names[i]; }
105 
106  // Get the name of the ith array
107  const std::string &get_name(unsigned int i) const
108  { return m_names[i]; }
109 
110  // Get the list of names
111  std::vector<std::string> &get_names()
112  { return m_names; }
113 
114  // Get the list of names
115  const std::vector<std::string> &get_names() const
116  { return m_names; }
117 
118  /// Return the name of the class
119  std::string get_class_name() const override
120  { return "teca_array_collection"; }
121 
122  /// return an integer identifier uniquely naming the dataset type
123  int get_type_code() const override;
124 
125  /// copy
126  void copy(const const_p_teca_dataset &other) override;
127 
128  /// shallow copy
129  void shallow_copy(const p_teca_dataset &other) override;
130 
131  /// append
132  int append(const const_p_teca_array_collection &other);
133 
134  /// shallow append
135  int shallow_append(const p_teca_array_collection &other);
136 
137  /// swap
138  void swap(const p_teca_dataset &other) override;
139 
140  /// serialize the data to the given stream for I/O or communication
141  int to_stream(teca_binary_stream &s) const override;
142 
143  /// serialize the data from the given stream for I/O or communication
144  int from_stream(teca_binary_stream &s) override;
145 
146  /// stream to a human readable representation
147  int to_stream(std::ostream &) const override;
148 
149 protected:
150  teca_array_collection() = default;
152  teca_array_collection(const teca_array_collection &&) = delete;
153  void operator=(const teca_array_collection &) = delete;
154  void operator=(const teca_array_collection &&) = delete;
155  void declare_set(){}
156 
157 private:
158  using name_vector_t = std::vector<std::string>;
159  using array_vector_t = std::vector<p_teca_variant_array>;
160  using name_array_map_t = std::map<std::string,unsigned int>;
161 
162  name_vector_t m_names;
163  array_vector_t m_arrays;
164  name_array_map_t m_name_array_map;
165 };
166 
167 // --------------------------------------------------------------------------
168 template<typename nT, typename aT, typename... oT>
169 void teca_array_collection::declare_set(nT &&a_name, aT a_type, oT &&... args)
170 {
171  this->declare(std::forward<nT>(a_name), a_type);
172  this->declare_set(args...);
173 }
174 
175 // --------------------------------------------------------------------------
176 template<typename nT, typename aT>
177 void teca_array_collection::declare(nT &&a_name, aT)
178 {
179  unsigned int id = m_arrays.size();
180  m_names.emplace_back(std::forward<nT>(a_name));
181  m_arrays.emplace_back(teca_variant_array_impl<aT>::New());
182  m_name_array_map.emplace(std::forward<nT>(a_name), id);
183 }
184 
185 #endif
teca_array_collection::get
p_teca_variant_array get(unsigned int i)
access an array by its by id
Definition: teca_array_collection.h:78
teca_variant_array.h
teca_array_collection::declare_set
void declare_set(nT &&a_name, aT a_type, oT &&...args)
Definition: teca_array_collection.h:169
teca_binary_stream
Serialize objects into a binary stream.
Definition: teca_binary_stream.h:15
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:18
teca_variant_array_impl
The concrete implementation of our type agnostic container for contiguous arrays.
Definition: teca_variant_array.h:23
teca_array_collection::get
const_p_teca_variant_array get(unsigned int i) const
access an array by its by id
Definition: teca_array_collection.h:82
teca_dataset
Interface for TECA datasets.
Definition: teca_dataset.h:216
teca_array_collection::get_class_name
std::string get_class_name() const override
Return the name of the class.
Definition: teca_array_collection.h:119
teca_array_collection::operator[]
p_teca_variant_array operator[](const std::string &name)
access an array by name
Definition: teca_array_collection.h:95
teca_array_collection
A collection of named arrays.
Definition: teca_array_collection.h:19
const_p_teca_variant_array
std::shared_ptr< const teca_variant_array > const_p_teca_variant_array
Definition: teca_variant_array.h:22
teca_array_collection::declare
void declare(nT &&a_name, aT a_type)
declare a single array
Definition: teca_array_collection.h:177
teca_array_collection::size
unsigned int size() const noexcept
Return the number of arrays.
Definition: teca_array_collection.h:74
teca_shared_object.h
p_teca_variant_array
std::shared_ptr< teca_variant_array > p_teca_variant_array
Definition: teca_variant_array.h:22
teca_py_array::set
bool set(teca_variant_array *varr, unsigned long i, PyObject *obj)
Set i'th element of the variant array to the value of the object.
Definition: teca_py_array.h:284
teca_py_array::append
bool append(teca_variant_array *varr, PyObject *obj)
Append values from the object to the variant array.
Definition: teca_py_array.h:213
teca_array_collection::operator[]
const_p_teca_variant_array operator[](const std::string &name) const
access an array by name
Definition: teca_array_collection.h:99
teca_py_array::copy
bool copy(teca_variant_array *varr, PyObject *obj)
Copy values from the object into variant array.
Definition: teca_py_array.h:270