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  */
57  int append(p_teca_variant_array array);
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 by id
83  p_teca_variant_array get(unsigned int i)
84  { return m_arrays[i]; }
85 
86  /// access an array by its by id
87  const_p_teca_variant_array get(unsigned int i) const
88  { return m_arrays[i]; }
89 
90  /// test for array
91  bool has(const std::string &name) const;
92 
93  /// access an array by name
94  p_teca_variant_array get(const std::string &name);
95 
96  /// access an array by name
97  const_p_teca_variant_array get(const std::string &name) const;
98 
99  /// access an array by name
100  p_teca_variant_array operator[](const std::string &name)
101  { return this->get(name); }
102 
103  /// access an array by name
104  const_p_teca_variant_array operator[](const std::string &name) const
105  { return this->get(name); }
106 
107  // Get the name of the ith array
108  std::string &get_name(unsigned int i)
109  { return m_names[i]; }
110 
111  // Get the name of the ith array
112  const std::string &get_name(unsigned int i) const
113  { return m_names[i]; }
114 
115  // Get the list of names
116  std::vector<std::string> &get_names()
117  { return m_names; }
118 
119  // Get the list of names
120  const std::vector<std::string> &get_names() const
121  { return m_names; }
122 
123  /// Return the name of the class
124  std::string get_class_name() const override
125  { return "teca_array_collection"; }
126 
127  /// return an integer identifier uniquely naming the dataset type
128  int get_type_code() const override;
129 
130  /// @copydoc teca_dataset::copy(const const_p_teca_dataset &,allocator)
131  void copy(const const_p_teca_dataset &other,
132  allocator alloc = allocator::malloc) override;
133 
134  /// @copydoc teca_dataset::shallow_copy(const p_teca_dataset &)
135  void shallow_copy(const p_teca_dataset &other) override;
136 
137  /// append
138  int append(const const_p_teca_array_collection &other);
139 
140  /// shallow append
141  int shallow_append(const p_teca_array_collection &other);
142 
143  /// swap
144  void swap(const p_teca_dataset &other) override;
145 
146  /// serialize the data to the given stream for I/O or communication
147  int to_stream(teca_binary_stream &s) const override;
148 
149  /// serialize the data from the given stream for I/O or communication
150  int from_stream(teca_binary_stream &s) override;
151 
152  /// stream to a human readable representation
153  int to_stream(std::ostream &) const override;
154  using teca_dataset::from_stream;
155 
156 #if defined(SWIG)
157 protected:
158 #else
159 public:
160 #endif
161  // NOTE: constructors are public to enable std::make_shared. do not use.
162  teca_array_collection() = default;
163 
164 protected:
166  teca_array_collection(const teca_array_collection &&) = delete;
167  void operator=(const teca_array_collection &) = delete;
168  void operator=(const teca_array_collection &&) = delete;
169  void declare_set(){}
170 
171 private:
172  using name_vector_t = std::vector<std::string>;
173  using array_vector_t = std::vector<p_teca_variant_array>;
174  using name_array_map_t = std::map<std::string,unsigned int>;
175 
176  name_vector_t m_names;
177  array_vector_t m_arrays;
178  name_array_map_t m_name_array_map;
179  allocator default_allocator;
180 };
181 
182 // --------------------------------------------------------------------------
183 template<typename nT, typename aT, typename... oT>
184 void teca_array_collection::declare_set(nT &&a_name, aT a_type, oT &&... args)
185 {
186  this->declare(std::forward<nT>(a_name), a_type);
187  this->declare_set(args...);
188 }
189 
190 // --------------------------------------------------------------------------
191 template<typename nT, typename aT>
192 void teca_array_collection::declare(nT &&a_name, aT)
193 {
194  unsigned int id = m_arrays.size();
195  m_names.emplace_back(std::forward<nT>(a_name));
196  m_arrays.emplace_back(teca_variant_array_impl<aT>::New(this->default_allocator));
197  m_name_array_map.emplace(std::forward<nT>(a_name), id);
198 }
199 
200 #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:83
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:184
teca_binary_stream
Serialize objects into a binary stream.
Definition: teca_binary_stream.h:16
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:21
teca_variant_array_impl
The concrete implementation of our type agnostic container for contiguous arrays.
Definition: teca_variant_array_impl.h:39
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:87
teca_dataset
Interface for TECA datasets.
Definition: teca_dataset.h:231
teca_py_array::set
TECA_EXPORT 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:305
teca_array_collection::get_class_name
std::string get_class_name() const override
Return the name of the class.
Definition: teca_array_collection.h:124
teca_array_collection::operator[]
p_teca_variant_array operator[](const std::string &name)
access an array by name
Definition: teca_array_collection.h:100
teca_array_collection
A collection of named arrays.
Definition: teca_array_collection.h:20
const_p_teca_variant_array
std::shared_ptr< const teca_variant_array > const_p_teca_variant_array
Definition: teca_variant_array.h:27
teca_array_collection::declare
void declare(nT &&a_name, aT a_type)
declare a single array
Definition: teca_array_collection.h:192
teca_array_collection::size
unsigned int size() const noexcept
Return the number of arrays.
Definition: teca_array_collection.h:79
teca_py_array::copy
TECA_EXPORT bool copy(teca_variant_array *varr, PyObject *obj)
Copy values from the object into variant array.
Definition: teca_py_array.h:290
teca_shared_object.h
p_teca_variant_array
std::shared_ptr< teca_variant_array > p_teca_variant_array
Definition: teca_variant_array.h:27
teca_error::TECA_EXPORT
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
teca_py_array::append
TECA_EXPORT bool append(teca_variant_array *varr, PyObject *obj)
Append values from the object to the variant array.
Definition: teca_py_array.h:221
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:104