version  0.0.1
Defines the C++ API for MsPASS
Loading...
Searching...
No Matches
Ensemble.h
1#ifndef _MSPASS_ENSEMBLE_H_
2#define _MSPASS_ENSEMBLE_H_
3#include "mspass/seismic/Seismogram.h"
4#include "mspass/seismic/TimeSeries.h"
5#include "mspass/utility/memory_constants.h"
6#include <vector>
7
8namespace mspass::seismic {
9template <typename Tdata> class Ensemble : public mspass::utility::Metadata {
10public:
20 std::vector<Tdata> member;
31 Ensemble(const size_t n) { member.reserve(n); };
33 Ensemble(const mspass::utility::Metadata &md, const size_t n)
34 : mspass::utility::Metadata(md) {
35 member.reserve(n);
36 };
38 Ensemble(const Ensemble &parent)
39 : mspass::utility::Metadata(
40 dynamic_cast<const mspass::utility::Metadata &>(parent)),
41 member(parent.member) {};
43 Ensemble &operator=(const Ensemble &parent) {
44 if (this != (&parent)) {
45 this->Metadata::operator=(parent);
46 member = parent.member;
47 }
48 return *this;
49 };
50 /* \brief Indexing operator.
51
52 This is the indexing operator used to extract an ensemble member
53 with a (simpler) construct like x=e[i] as opposed to x=e.member[i].
54 The later is possible because we (intionally) make the member Container
55 public, but this operator can be used to write code that follows common
56 guidelines to not make data public. It will be used for the python
57 interface.
58
59 \param n is the member to be extracted and returned.
60 */
61 Tdata &operator[](const size_t n) const try {
62 return (this->member[n]);
63 } catch (...) {
64 throw;
65 };
79 md = dynamic_cast<mspass::utility::Metadata *>(this);
80 (*md) += newmd;
81 } catch (...) {
82 throw;
83 };
93 size_t i;
94 for (i = 0; i < this->member.size(); ++i) {
95 mspass::utility::Metadata *mdmember = &(this->member[i]);
96 (*mdmember) += dynamic_cast<mspass::utility::Metadata &>(*this);
97 }
98 };
101 void sync_metadata(std::vector<std::string> exclude) {
102 mspass::utility::Metadata sync_md(*this);
103 for (size_t i = 0; i < exclude.size(); i++) {
104 if (sync_md.is_defined(exclude[i])) {
105 sync_md.erase(exclude[i]);
106 }
107 }
108 for (size_t i = 0; i < this->member.size(); ++i) {
109 mspass::utility::Metadata *mdmember = &(this->member[i]);
110 (*mdmember) += sync_md;
111 }
112 };
113};
114
135template <typename T> class LoggingEnsemble : public Ensemble<T> {
136public:
141 LoggingEnsemble() : Ensemble<T>(), elog() { ensemble_is_live = false; };
142
143 /* The next two constructors are used to provide a parallel qpi to
144 CoreEnsemble - there might be another way to do this in the C++ code or
145 in pybind11 wrappers but this works*/
154 LoggingEnsemble(const size_t n) : Ensemble<T>(n), elog() {
155 ensemble_is_live = false;
156 }
164 : Ensemble<T>(md, n), elog() {
165 /* Might be advised to set this true, but since an object created by
166 this method has only slots but no data validate would return false.*/
167 ensemble_is_live = false;
168 }
176 const mspass::utility::ErrorLogger &elogin,
177 const size_t ndata)
178 : Ensemble<T>(md, ndata), elog(elogin) {};
181 : Ensemble<T>(parent), elog(parent.elog) {
182 ensemble_is_live = parent.ensemble_is_live;
183 };
186 LoggingEnsemble(const Ensemble<T> &parent) : Ensemble<T>(parent), elog() {
187 ensemble_is_live = true;
188 };
190 void kill() { ensemble_is_live = false; };
192 bool live() const { return ensemble_is_live; };
195 bool dead() const { return !ensemble_is_live; };
203 bool validate();
211 bool set_live() {
212 if (this->validate()) {
213 ensemble_is_live = true;
214 return true;
215 } else
216 return false;
217 };
218
221 if (&parent != this) {
223 this->member.reserve(parent.member.size());
224 this->member = parent.member;
225 elog = parent.elog;
226 ensemble_is_live = parent.ensemble_is_live;
227 }
228 return *this;
229 };
230 size_t memory_use() const {
231 size_t memuse;
232 memuse = sizeof(*this);
233 for (auto p = this->member.begin(); p != this->member.end(); ++p) {
234 memuse += p->memory_use();
235 }
236 /* Account for ensemble metadata */
237 /* We can only estimate the size of the Metadata container.
238 These constants are defined in memory_constants.h */
239 memuse +=
240 mspass::utility::memory_constants::MD_AVERAGE_SIZE * this->md.size();
241 memuse += mspass::utility::memory_constants::KEY_AVERAGE_SIZE *
242 this->changed_or_set.size();
243 /* Account for error log size */
244 memuse += mspass::utility::memory_constants::ELOG_AVERAGE_SIZE *
245 this->elog.size();
246 return memuse;
247 };
248
249private:
250 bool ensemble_is_live;
251};
252
253template <typename T> bool LoggingEnsemble<T>::validate() {
254 for (auto dptr = this->member.begin(); dptr != this->member.end(); ++dptr) {
255 if (dptr->live())
256 return true;
257 }
258 return false;
259}
260
285/* Disable temporarily - needs a revision to match new history approach
286template <typename Tdata>
287 size_t set_inputs(ProcessingHistoryRecord& rec, const
288mspass::seismic::Ensemble<Tdata>& d)
289{
290 try{
291 for(size_t i=0;i<d.member.size();++i)
292 {
293 // Ignore any data not defined as live
294 if(d.member[i].live())
295 {
296 list<ProcessingHistoryRecord> h(d.member[i].history());
297 // do nothing if the container is empty
298 if(h.size()>0)rec.inputs.push_back(h);
299 }
300 }
301 return rec.inputs.size();
302 }catch(...){throw;};
303};
304*/
305} // namespace mspass::seismic
306#endif // End guard
Definition Ensemble.h:9
Ensemble()
Definition Ensemble.h:22
void sync_metadata(std::vector< std::string > exclude)
copy ensemble metadata to all members except for the ones excluded.
Definition Ensemble.h:101
std::vector< Tdata > member
Container holding data objects.
Definition Ensemble.h:20
void sync_metadata()
copy ensemble metadata to all members.
Definition Ensemble.h:92
Ensemble(const size_t n)
Reserve space but build empty ensemble.
Definition Ensemble.h:31
void update_metadata(const mspass::utility::Metadata &newmd)
updates ensemble header (Metadata).
Definition Ensemble.h:77
Ensemble(const mspass::utility::Metadata &md, const size_t n)
Definition Ensemble.h:33
Ensemble(const Ensemble &parent)
Definition Ensemble.h:38
Ensemble & operator=(const Ensemble &parent)
Definition Ensemble.h:43
Template class that extends Ensemble to include an error log and live tests.
Definition Ensemble.h:135
LoggingEnsemble(const Ensemble< T > &parent)
Definition Ensemble.h:186
LoggingEnsemble(const mspass::utility::Metadata &md, const mspass::utility::ErrorLogger &elogin, const size_t ndata)
Definition Ensemble.h:175
LoggingEnsemble< T > & operator=(const LoggingEnsemble< T > &parent)
Definition Ensemble.h:220
LoggingEnsemble(const mspass::utility::Metadata &md, const size_t n)
Definition Ensemble.h:163
LoggingEnsemble()
Definition Ensemble.h:141
void kill()
Definition Ensemble.h:190
bool set_live()
Definition Ensemble.h:211
bool validate()
Definition Ensemble.h:253
mspass::utility::ErrorLogger elog
Definition Ensemble.h:138
LoggingEnsemble(const size_t n)
Definition Ensemble.h:154
bool live() const
Definition Ensemble.h:192
LoggingEnsemble(const LoggingEnsemble< T > &parent)
Definition Ensemble.h:180
bool dead() const
Definition Ensemble.h:195
Container to hold error logs for a data object.
Definition ErrorLogger.h:60
Definition Metadata.h:71
void erase(const std::string key)
Definition Metadata.cc:125
bool is_defined(const std::string key) const noexcept
Definition Metadata.cc:63
Metadata & operator=(const Metadata &mdold)
Definition Metadata.cc:90
Define metadata keys.
Definition BasicSpectrum.h:6
Ensemble< TimeSeries > TimeSeriesEnsemble
Definition Ensemble.h:262
Ensemble< Seismogram > ThreeComponentEnsemble
Definition Ensemble.h:264