version  0.0.1
Defines the C++ API for MsPASS
Ensemble.h
1 #ifndef _MSPASS_ENSEMBLE_H_
2 #define _MSPASS_ENSEMBLE_H_
3 #include <vector>
4 #include "mspass/seismic/TimeSeries.h"
5 #include "mspass/seismic/Seismogram.h"
6 #include "mspass/utility/memory_constants.h"
7 
8 namespace mspass::seismic{
9 template <typename Tdata> class Ensemble : public mspass::utility::Metadata
10 {
11 public:
21  std::vector<Tdata> member;
23  Ensemble(){};
32  Ensemble(const size_t n){member.reserve(n);};
34  Ensemble(const mspass::utility::Metadata& md,const size_t n) : mspass::utility::Metadata(md)
35  {
36  member.reserve(n);
37  };
39  Ensemble(const Ensemble& parent) : mspass::utility::Metadata(dynamic_cast<const mspass::utility::Metadata&>(parent)),
40  member(parent.member){};
42  Ensemble& operator=(const Ensemble& parent)
43  {
44  if(this!=(&parent))
45  {
46  this->Metadata::operator=(parent);
47  member=parent.member;
48  }
49  return *this;
50  };
51  /* \brief Indexing operator.
52 
53  This is the indexing operator used to extract an ensemble member
54  with a (simpler) construct like x=e[i] as opposed to x=e.member[i].
55  The later is possible because we (intionally) make the member Container
56  public, but this operator can be used to write code that follows common
57  guidelines to not make data public. It will be used for the python
58  interface.
59 
60  \param n is the member to be extracted and returned.
61  */
62  Tdata& operator[](const size_t n) const
63  try{
64  return(this->member[n]);
65  }catch(...){throw;};
78  try{
80  md=dynamic_cast<mspass::utility::Metadata*>(this);
81  (*md) += newmd;
82  }catch(...){throw;};
92  {
93  size_t i;
94  for(i=0;i<this->member.size();++i)
95  {
96  mspass::utility::Metadata *mdmember=&(this->member[i]);
97  (*mdmember)+=dynamic_cast<mspass::utility::Metadata&>(*this);
98  }
99  };
102  void sync_metadata(std::vector<std::string> exclude)
103  {
104  mspass::utility::Metadata sync_md(*this);
105  for (size_t i = 0; i < exclude.size(); i++)
106  {
107  if(sync_md.is_defined(exclude[i])) {
108  sync_md.erase(exclude[i]);
109  }
110  }
111  for (size_t i = 0; i < this->member.size(); ++i)
112  {
113  mspass::utility::Metadata *mdmember = &(this->member[i]);
114  (*mdmember) += sync_md;
115  }
116  };
117 
118 };
119 
120 
140 template <typename T> class LoggingEnsemble : public Ensemble<T>
141 {
142 public:
148  {
149  ensemble_is_live=false;
150  };
151 
152  /* The next two constructors are used to provide a parallel qpi to
153  CoreEnsemble - there might be another way to do this in the C++ code or
154  in pybind11 wrappers but this works*/
163  LoggingEnsemble(const size_t n) : Ensemble<T>(n),elog()
164  {
165  ensemble_is_live=false;
166  }
173  LoggingEnsemble(const mspass::utility::Metadata& md,const size_t n)
174  : Ensemble<T>(md,n),elog()
175  {
176  /* Might be advised to set this true, but since an object created by
177  this method has only slots but no data validate would return false.*/
178  ensemble_is_live=false;
179  }
187  const mspass::utility::ErrorLogger& elogin, const size_t ndata)
188  : Ensemble<T>(md,ndata),elog(elogin)
189  {
190  };
193  : Ensemble<T>(parent),elog(parent.elog)
194  {
195  ensemble_is_live=parent.ensemble_is_live;
196  };
199  : Ensemble<T>(parent),elog()
200  {
201  ensemble_is_live=true;
202  };
204  void kill(){ensemble_is_live=false;};
206  bool live() const {return ensemble_is_live;};
208  bool dead() const {return !ensemble_is_live;};
216  bool validate();
224  bool set_live(){
225  if(this->validate())
226  {
227  ensemble_is_live=true;
228  return true;
229  }
230  else
231  return false;
232  };
233 
236  {
237  if(&parent != this)
238  {
240  this->member.reserve(parent.member.size());
241  this->member=parent.member;
242  elog=parent.elog;
243  ensemble_is_live=parent.ensemble_is_live;
244  }
245  return *this;
246  };
247  size_t memory_use() const
248  {
249  size_t memuse;
250  memuse = sizeof(*this);
251  for(auto p=this->member.begin();p!=this->member.end();++p)
252  {
253  memuse += p->memory_use();
254  }
255  /* Account for ensemble metadata */
256  /* We can only estimate the size of the Metadata container.
257  These constants are defined in memory_constants.h */
258  memuse += mspass::utility::memory_constants::MD_AVERAGE_SIZE*this->md.size();
259  memuse += mspass::utility::memory_constants::KEY_AVERAGE_SIZE*this->changed_or_set.size();
260  /* Account for error log size */
261  memuse += mspass::utility::memory_constants::ELOG_AVERAGE_SIZE*this->elog.size();
262  return memuse;
263  };
264 private:
265 
266  bool ensemble_is_live;
267 };
268 
269 template <typename T> bool LoggingEnsemble<T>::validate()
270 {
271  for(auto dptr=this->member.begin();dptr!=this->member.end();++dptr)
272  {
273  if(dptr->live()) return true;
274  }
275  return false;
276 }
277 
302 /* Disable temporarily - needs a revision to match new history approach
303 template <typename Tdata>
304  size_t set_inputs(ProcessingHistoryRecord& rec, const mspass::seismic::Ensemble<Tdata>& d)
305 {
306  try{
307  for(size_t i=0;i<d.member.size();++i)
308  {
309  // Ignore any data not defined as live
310  if(d.member[i].live())
311  {
312  list<ProcessingHistoryRecord> h(d.member[i].history());
313  // do nothing if the container is empty
314  if(h.size()>0)rec.inputs.push_back(h);
315  }
316  }
317  return rec.inputs.size();
318  }catch(...){throw;};
319 };
320 */
321 } // End mspass::seismic namespace encapsulation
322 #endif // End guard
Definition: Ensemble.h:10
Ensemble()
Definition: Ensemble.h:23
void sync_metadata(std::vector< std::string > exclude)
copy ensemble metadata to all members except for the ones excluded.
Definition: Ensemble.h:102
std::vector< Tdata > member
Container holding data objects.
Definition: Ensemble.h:21
void update_metadata(const mspass::utility::Metadata &newmd) try
updates ensemble header (Metadata).
Definition: Ensemble.h:77
void sync_metadata()
copy ensemble metadata to all members.
Definition: Ensemble.h:91
Ensemble & operator=(const Ensemble &parent)
Definition: Ensemble.h:42
Ensemble(const size_t n)
Reserve space but build empty ensemble.
Definition: Ensemble.h:32
Ensemble(const mspass::utility::Metadata &md, const size_t n)
Definition: Ensemble.h:34
Ensemble(const Ensemble &parent)
Definition: Ensemble.h:39
Template class that extends Ensemble to include an error log and live tests.
Definition: Ensemble.h:141
LoggingEnsemble(const Ensemble< T > &parent)
Definition: Ensemble.h:198
LoggingEnsemble(const mspass::utility::Metadata &md, const mspass::utility::ErrorLogger &elogin, const size_t ndata)
Definition: Ensemble.h:186
LoggingEnsemble(const mspass::utility::Metadata &md, const size_t n)
Definition: Ensemble.h:173
LoggingEnsemble()
Definition: Ensemble.h:147
void kill()
Definition: Ensemble.h:204
bool set_live()
Definition: Ensemble.h:224
bool validate()
Definition: Ensemble.h:269
mspass::utility::ErrorLogger elog
Definition: Ensemble.h:144
LoggingEnsemble(const size_t n)
Definition: Ensemble.h:163
bool live() const
Definition: Ensemble.h:206
LoggingEnsemble(const LoggingEnsemble< T > &parent)
Definition: Ensemble.h:192
LoggingEnsemble< T > & operator=(const LoggingEnsemble< T > &parent)
Definition: Ensemble.h:235
bool dead() const
Definition: Ensemble.h:208
Container to hold error logs for a data object.
Definition: ErrorLogger.h:61
Definition: Metadata.h:76
void erase(const std::string key)
Definition: Metadata.cc:151
bool is_defined(const std::string key) const noexcept
Definition: Metadata.cc:73
Metadata & operator=(const Metadata &mdold)
Definition: Metadata.cc:108
Define metadata keys.
Definition: BasicSpectrum.h:6
Ensemble< TimeSeries > TimeSeriesEnsemble
Definition: Ensemble.h:279
Ensemble< Seismogram > ThreeComponentEnsemble
Definition: Ensemble.h:281