version  0.0.1
Defines the C++ API for MsPASS
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | Friends | List of all members
mspass::utility::Metadata Class Reference
Inheritance diagram for mspass::utility::Metadata:
mspass::utility::BasicMetadata mspass::seismic::Ensemble< T > mspass::seismic::CoreSeismogram mspass::seismic::CoreTimeSeries mspass::seismic::Ensemble< Tdata > mspass::seismic::PowerSpectrum mspass::utility::AntelopePf mspass::seismic::LoggingEnsemble< T > mspass::seismic::Seismogram mspass::seismic::TimeSeries mspass::seismic::SeismogramWGaps mspass::seismic::TimeSeriesWGaps

Public Member Functions

 Metadata ()
 
 Metadata (std::ifstream &ifs, const std::string form=std::string("pf"))
 
 Metadata (const Metadata &mdold)
 
virtual ~Metadata ()
 
Metadataoperator= (const Metadata &mdold)
 
Metadataoperator+= (const Metadata &rhs) noexcept
 
const Metadata operator+ (const Metadata &other) const
 
double get_double (const std::string key) const override
 
int get_int (const std::string key) const override
 
long get_long (const std::string key) const
 
std::string get_string (const std::string key) const override
 
bool get_bool (const std::string key) const override
 
template<typename T >
get (const std::string key) const
 
template<typename T >
get (const char *key) const
 Generic get interface for C char array.
 
boost::any get_any (const std::string key) const
 
std::string type (const std::string key) const
 
template<typename T >
void put (const std::string key, T val) noexcept
 
template<typename T >
void put (const char *key, T val) noexcept
 
void put (const std::string key, const double val) override
 
void put (const std::string key, const int val) override
 
void put (const std::string key, const bool val) override
 
void put (const std::string key, const std::string val) override
 
void put (const char *key, const char *val)
 
void put (std::string key, const char *val)
 
void put_object (const std::string key, const pybind11::object val)
 
void put_int (const std::string key, const int val)
 
void put_string (const std::string key, const std::string val)
 
void put_bool (const std::string key, const bool val)
 
void put_double (const std::string key, const double val)
 
void put_long (const std::string key, const long val)
 
void append_chain (const std::string key, const std::string val, const std::string separator=std::string(":"))
 
std::set< std::string > modified () const
 
void clear_modified ()
 Mark all data as unmodified.
 
std::set< std::string > keys () const noexcept
 
bool is_defined (const std::string key) const noexcept
 
void erase (const std::string key)
 
std::size_t size () const noexcept
 
std::map< std::string, boost::any >::const_iterator begin () const noexcept
 
std::map< std::string, boost::any >::const_iterator end () const noexcept
 
void change_key (const std::string oldkey, const std::string newkey)
 Change the keyword to access an attribute.
 

Protected Attributes

std::map< std::string, boost::any > md
 
std::set< std::string > changed_or_set
 

Friends

pybind11::object serialize_metadata_py (const Metadata &md)
 
Metadata restore_serialized_metadata_py (const pybind11::object &sd)
 
std::ostringstream & operator<< (std::ostringstream &, const mspass::utility::Metadata &)
 

Constructor & Destructor Documentation

◆ Metadata() [1/3]

mspass::utility::Metadata::Metadata ( )
inline

Default constructor. Does nothing.

75{};

◆ Metadata() [2/3]

mspass::utility::Metadata::Metadata ( std::ifstream &  ifs,
const std::string  form = std::string("pf") 
)

Construct from a file.

This simple file based constructor assumes the file contains only a set lines with this format: key value type where type must be one of: real, integer, bool, or string. Note int is actually always promoted to a long. The optional format variable is there to allow alternative formats in the future.

Parameters
ifs- ifstream from which to read data
format- optional format specification. Currently only default of "text" is accepted.
Exceptions
MsPASSErrorcan be thrown for a variety of conditions.

◆ Metadata() [3/3]

mspass::utility::Metadata::Metadata ( const Metadata mdold)

Standard copy constructor.

Parameters
mdold- parent object to be copied
62 : md(parent.md), changed_or_set(parent.changed_or_set) {}
T get(const std::string key) const
Definition Metadata.h:445

◆ ~Metadata()

virtual mspass::utility::Metadata::~Metadata ( )
inlinevirtual

Destructor - has to be explicitly implemented and declared virtual for reasons found in textbooks and various web forums. A very subtle feature of C++ inheritance.

99{};

Member Function Documentation

◆ append_chain()

void mspass::utility::Metadata::append_chain ( const std::string  key,
const std::string  val,
const std::string  separator = std::string(":") 
)

Create or append to a chained string.

A chain conceptually is identical to a list of string data. We implement it in Metadata because sometimes (e.g. MongoDB interaction and some constructs like the unix shell PATH variable) handling a full scale container like list<std::string> would be awkward. If more extensive capability like that is needed it would be better to add a class that inherits Metadata and does so. AntelopePf more or less does this, for example, handling Tbl sections. In any case, this usage is more for one word strings separated by a common separator: e.g. path=/usr/local/bin:/bin uses : as the separator. /usr/local/bin and /bin are the chain.

If the key related to the chain does not yet exist it is silently created. If it already exists and we append to it.

Parameters
keyis the key that defines the string.
valis the the new string to append to the chain
separatoris the string used for a separator (default ":")
Exceptions
MsPASSErrorwill be thrown if data is found in key and it is not of type string.
73 {
74 if (this->is_defined(key)) {
75 string typ = this->type(key);
76 if (typ.find("string") == string::npos)
77 throw MsPASSError("Metadata::append_chain: data for key=" + key +
78 " is not string type but " + typ +
79 "\nMust be string type to define a valid chain",
80 ErrorSeverity::Invalid);
81 string sval = this->get_string(key);
82 sval += separator;
83 sval += val;
84 this->put(key, sval);
85 } else {
86 this->put(key, val);
87 }
88 changed_or_set.insert(key);
89}
bool is_defined(const std::string key) const noexcept
Definition Metadata.cc:63
std::string get_string(const std::string key) const override
Definition Metadata.h:202

References get(), get_string(), and is_defined().

◆ begin()

std::map< string, boost::any >::const_iterator mspass::utility::Metadata::begin ( ) const
noexcept

Return iterator to beginning of internal map container.

137 {
138 return md.begin();
139}

◆ change_key()

void mspass::utility::Metadata::change_key ( const std::string  oldkey,
const std::string  newkey 
)

Change the keyword to access an attribute.

Sometimes it is useful to change the key used to access a particular piece of data. Doing so, for example, is one way to implement an alias (alternative name) for something. The entry for the old key is copied to a entry accessible by the new key. The entry for old is then deleted. This avoids downstream inconsistencies a the cost of possible failures from the translation. This method always returns and will silently do nothing if old is not defined. If the new key is already defined, its content will be replaced by the old's.

Parameters
oldkeyis the key to search for to be changed
newkeyis the new key to use for the replacement.
281 {
282 map<string, boost::any>::iterator mdptr;
283 mdptr = md.find(oldkey);
284 /* We silently do nothing if old is not found */
285 if (mdptr != md.end()) {
286 md.insert_or_assign(newkey, mdptr->second);
287 md.erase(mdptr);
288 }
289}

References get().

◆ clear_modified()

void mspass::utility::Metadata::clear_modified ( )
inline

Mark all data as unmodified.

There are situations where it is necessary to clear the data structure used to mark changed metadata. The best example know is when data objects interact with a database and try to do updates. Effort can be wasted in unnecessary updates if metadata are improperly marked as modified. This method clears the entire container that defines changed data.

376{ changed_or_set.clear(); };

◆ end()

std::map< string, boost::any >::const_iterator mspass::utility::Metadata::end ( ) const
noexcept

Return iterator to end of internal map container.

140 {
141 return md.end();
142}

◆ erase()

void mspass::utility::Metadata::erase ( const std::string  key)

Overload for C string

Clear data associated with a particular key.

125 {
126 map<string, boost::any>::iterator iptr;
127 iptr = md.find(key);
128 if (iptr != md.end())
129 md.erase(iptr);
130 /* Also need to modify this set if the key is found there */
131 set<std::string>::iterator sptr;
132 sptr = changed_or_set.find(key);
133 if (sptr != changed_or_set.end())
134 changed_or_set.erase(sptr);
135}

References get().

◆ get() [1/2]

template<typename T >
T mspass::utility::Metadata::get ( const char *  key) const
inline

Generic get interface for C char array.

This is a generic interface most useful for template procedures
that need to get a Metadata component.   Since this object only
can contain simple types the type requested must be simple.
Currently supports only int, long, short, double, float, and string.
C char* is intentionally not supported. This is largely a wrapper
on the string key version of this same generic function.

\param key is the name tag of desired component.

\exception - will throw a MetadataGetError (child of MsPASSError) for
   type mismatch or in an overflow or underflow condition.
258 {
259 try {
260 T val;
261 val = get<T>(std::string(key));
262 return val;
263 } catch (...) {
264 throw;
265 };
266 }

References get().

◆ get() [2/2]

template<typename T >
T mspass::utility::Metadata::get ( const std::string  key) const

Generic get interface.

This is a generic interface most useful for template procedures that need to get a Metadata component. Since this object only can contain simple types the type requested must be simple. Currently supports only int, long, short, double, float, and string. C char* is intentionally not supported. Calls to anything but the supported types will throw an exception.

Parameters
keyis the name tag of desired component.
Exceptions
-will throw a MetadataGetError (child of MsPASSError) for type mismatch or in an overflow or underflow condition.
445 {
446 T result;
447 std::map<std::string, boost::any>::const_iterator iptr;
448 iptr = md.find(key);
449 if (iptr == md.end()) {
450 throw MetadataGetError(key, typeid(T).name());
451 }
452 boost::any aval = iptr->second;
453 try {
454 result = boost::any_cast<T>(aval);
455 } catch (boost::bad_any_cast &err) {
456 const std::type_info &ti = aval.type();
457 throw MetadataGetError(err.what(), key, typeid(T).name(), ti.name());
458 };
459 return result;
460}

References get().

◆ get_any()

boost::any mspass::utility::Metadata::get_any ( const std::string  key) const
inline

Get the boost::any container from the Metadata object.

This method is mostly for Python bindings so that a generic get method can work in Python.

Parameters
keyis the name tag of desired component.
Exceptions
-MetadataGetError if requested parameter is not found.
277 {
278 std::map<std::string, boost::any>::const_iterator iptr;
279 iptr = md.find(key);
280 if (iptr == md.end()) {
281 throw MetadataGetError(key, typeid(boost::any).name());
282 }
283 return iptr->second;
284 };

References get().

◆ get_bool()

bool mspass::utility::Metadata::get_bool ( const std::string  key) const
inlineoverridevirtual

Get a boolean parameter from the Metadata object.

This method never throws an exception assuming that if the requested parameter is not found it is false.

Parameters
keykeyword associated with requested metadata member.

Implements mspass::utility::BasicMetadata.

219 {
220 try {
221 bool val;
222 val = get<bool>(key);
223 return val;
224 } catch (...) {
225 throw;
226 };
227 };

References get().

◆ get_double()

double mspass::utility::Metadata::get_double ( const std::string  key) const
inlineoverridevirtual

Get a real number from the Metadata object.

Exceptions
MetadataGetErrorif requested parameter is not found or there is a type mismatch.
Parameters
keykeyword associated with requested metadata member.

Implements mspass::utility::BasicMetadata.

131 {
132 try {
133 double val;
135 return val;
136 } catch (MetadataGetError &merr) {
137 /* Try a float if that failed */
138 try {
139 float fval;
141 return fval;
142 } catch (MetadataGetError &merr) {
143 throw merr;
144 }
145 }
146 };

References get().

◆ get_int()

int mspass::utility::Metadata::get_int ( const std::string  key) const
inlineoverridevirtual

Get an integer from the Metadata object.

Exceptions
MetadataGetErrorif requested parameter is not found or there is a type mismatch.
Parameters
keykeyword associated with requested metadata member.

Implements mspass::utility::BasicMetadata.

154 {
155 try {
156 int val;
157 val = get<int>(key);
158 return val;
159 } catch (MetadataGetError &merr) {
160 try {
161 long lval;
162 lval = get<long>(key);
163 return static_cast<int>(lval);
164 } catch (MetadataGetError &merr) {
165 throw merr;
166 }
167 }
168 };

References get().

◆ get_long()

long mspass::utility::Metadata::get_long ( const std::string  key) const
inline

Get a long integer from the Metadata object.

Exceptions
MetadataGetErrorif requested parameter is not found or there is a type mismatch.
Parameters
keykeyword associated with requested metadata member.
176 {
177 try {
178 long val;
179 val = get<long>(key);
180 return val;
181 } catch (MetadataGetError &merr) {
182 try {
183 int ival;
184 ival = get<int>(key);
185 return static_cast<long>(ival);
186 } catch (MetadataGetError &merr) {
187 throw merr;
188 }
189 }
190 };

References get().

◆ get_string()

std::string mspass::utility::Metadata::get_string ( const std::string  key) const
inlineoverridevirtual

Get a string from the Metadata object.

Note the string in this case can be quite large. If the string was parsed from an Antelope Pf nested Tbl and Arrs can be extracted this way and parsed with pf routines.

Exceptions
MetadataGetErrorif requested parameter is not found or there is a type mismatch.
Parameters
keykeyword associated with requested metadata member.

Implements mspass::utility::BasicMetadata.

202 {
203 try {
204 std::string val;
206 return val;
207 } catch (...) {
208 throw;
209 };
210 };

References get().

◆ is_defined()

bool mspass::utility::Metadata::is_defined ( const std::string  key) const
noexcept

Test if a key has an associated value. Returns true if a value is defined.

63 {
64 map<string, boost::any>::const_iterator mptr;
65 mptr = md.find(key);
66 if (mptr != md.end()) {
67 return true;
68 } else {
69 return false;
70 }
71}

◆ keys()

set< string > mspass::utility::Metadata::keys ( ) const
noexcept

Return all keys without any type information.

116 {
117 set<string> result;
118 map<string, boost::any>::const_iterator mptr;
119 for (mptr = md.begin(); mptr != md.end(); ++mptr) {
120 string key(mptr->first);
121 result.insert(key);
122 }
123 return result;
124}

References get().

◆ modified()

std::set< std::string > mspass::utility::Metadata::modified ( ) const
inline

Return the keys of all altered Metadata values.

365{ return changed_or_set; };

◆ operator+()

Add two Metadata objects. Uses operator+=

111 {
112 Metadata result(*this);
113 result += other;
114 return result;
115}
Metadata()
Definition Metadata.h:75

References get().

◆ operator+=()

Append additional metadata with replacement.

A plus operator implies addition, but this overloading does something very different. A simple way to describe the effect is that on completion the left hand side Metadata object will contain a duplicate of the right hand side plus any attributes in the rhs that were not present on the lhs. Another way to clarify this is to describe the algorithm. We take each attribute on the right and search for it in the lhs. If it is not in the lhs it will be added. If it is there already, the rhs value will replace the old value on the lhs. This is most useful when an algorithm creates a new set of attributes that we want to use in downstream processing but retain all the other attributes.

Parameters
rhsis the new metadata to be insert/replace on the lhs.
98 {
99 if (this != (&rhs)) {
100 /* We depend here upon the map container replacing values associated with
101 existing keys. We mark all entries changes anyway. This is a vastly
102 simpler algorithm than the old SEISPP::Metadata. */
103 map<string, boost::any>::const_iterator rhsptr;
104 for (rhsptr = rhs.md.begin(); rhsptr != rhs.md.end(); ++rhsptr) {
105 md[rhsptr->first] = rhsptr->second;
106 changed_or_set.insert(rhsptr->first);
107 }
108 }
109 return *this;
110}

◆ operator=()

Standard assignment operator.

Parameters
mdold- parent object to copy
90 {
91 if (this != (&parent)) {
92 md = parent.md;
93 changed_or_set = parent.changed_or_set;
94 }
95 return *this;
96}

References get().

◆ put() [1/8]

void mspass::utility::Metadata::put ( const char key,
const char val 
)
inline

Put a C string literal. Requires special handling because it is not copy constructable (see warning in boost docs: https://theboostcpplibraries.com/boost.any

315 {
316 std::string sval(val);
317 this->put<std::string>(key, val);
318 }

References get().

◆ put() [2/8]

template<typename T >
void mspass::utility::Metadata::put ( const char key,
T  val 
)
inlinenoexcept
291 {
292 /* could do this as put(string(key),val) but this is so trivial duplicating
293 the code for the string method is more efficient than an added function
294 call.*/
295 boost::any aval = val;
296 md[std::string(key)] = aval;
297 changed_or_set.insert(std::string(key));
298 }

◆ put() [3/8]

void mspass::utility::Metadata::put ( const std::string  key,
const bool  val 
)
inlineoverridevirtual

Implements mspass::utility::BasicMetadata.

305 {
306 this->put<bool>(key, val);
307 };

◆ put() [4/8]

void mspass::utility::Metadata::put ( const std::string  key,
const double  val 
)
inlineoverridevirtual

Implements mspass::utility::BasicMetadata.

299 {
300 this->put<double>(key, val);
301 };

◆ put() [5/8]

void mspass::utility::Metadata::put ( const std::string  key,
const int  val 
)
inlineoverridevirtual

Implements mspass::utility::BasicMetadata.

302 {
303 this->put<int>(key, val);
304 };

◆ put() [6/8]

void mspass::utility::Metadata::put ( const std::string  key,
const std::string  val 
)
inlineoverridevirtual

Implements mspass::utility::BasicMetadata.

308 {
309 this->put<std::string>(key, val);
310 };

◆ put() [7/8]

template<typename T >
void mspass::utility::Metadata::put ( const std::string  key,
T  val 
)
inlinenoexcept
286 {
287 boost::any aval = val;
288 md[key] = aval;
289 changed_or_set.insert(key);
290 }

◆ put() [8/8]

void mspass::utility::Metadata::put ( std::string  key,
const char val 
)
inline
319{ this->put(key.c_str(), val); }

◆ put_bool()

void mspass::utility::Metadata::put_bool ( const std::string  key,
const bool  val 
)
inline
329 {
330 this->put<bool>(key, val);
331 };

◆ put_double()

void mspass::utility::Metadata::put_double ( const std::string  key,
const double  val 
)
inline
332 {
333 this->put<double>(key, val);
334 };

◆ put_int()

void mspass::utility::Metadata::put_int ( const std::string  key,
const int  val 
)
inline
323 {
324 this->put<int>(key, val);
325 };

◆ put_long()

void mspass::utility::Metadata::put_long ( const std::string  key,
const long  val 
)
inline
335 {
336 this->put<long>(key, val);
337 };

◆ put_object()

void mspass::utility::Metadata::put_object ( const std::string  key,
const pybind11::object  val 
)
inline
320 {
322 }

◆ put_string()

void mspass::utility::Metadata::put_string ( const std::string  key,
const std::string  val 
)
inline
326 {
327 this->put<std::string>(key, val);
328 };

◆ size()

std::size_t mspass::utility::Metadata::size ( ) const
noexcept

Overload for C string

Return the size of the internal map container.

136{ return md.size(); }

◆ type()

std::string mspass::utility::Metadata::type ( const std::string  key) const
155 {
156 try {
157 boost::any a = this->get_any(key);
158 return demangled_name(a);
159 } catch (...) {
160 throw;
161 };
162}
boost::any get_any(const std::string key) const
Definition Metadata.h:277

Friends And Related Symbol Documentation

◆ operator<<

std::ostringstream & operator<< ( std::ostringstream &  ,
const mspass::utility::Metadata  
)
friend

Standard operator for overloading output to a stringstream

◆ restore_serialized_metadata_py

Metadata restore_serialized_metadata_py ( const pybind11::object &  sd)
friend

Unpack serialized Metadata.

This function is the inverse of the serialize function. It recreates a Metadata object serialized previously with the serialize function.

Parameters
sdis the serialized data to be unpacked
Returns
Metadata derived from sd
261 {
262 pybind11::gil_scoped_acquire acquire;
263 try {
264 pybind11::module pickle = pybind11::module::import("pickle");
265 pybind11::object loads = pickle.attr("loads");
266 pybind11::tuple md_dump = loads(s);
267 pybind11::dict md_dict = md_dump[0];
268 pybind11::object md_py = pybind11::module_::import("mspasspy.ccore.utility")
269 .attr("Metadata")(md_dict);
270 Metadata md = md_py.cast<Metadata>();
271 md.changed_or_set = md_dump[1].cast<std::set<std::string>>();
272 pybind11::gil_scoped_release release;
273 return md;
274 } catch (...) {
275 pybind11::gil_scoped_release release;
276 throw;
277 };
278}

◆ serialize_metadata_py

pybind11::object serialize_metadata_py ( const Metadata md)
friend

Serialize Metadata to a python bytes object. This function is needed to support pickle in the python interface. It cast the C++ object to a Python dict and calls pickle against that dict directly to generate a Python bytes object. This may not be the most elegant approach, but it should be bombproof.

Parameters
mdis the Metadata object to be serialized
Returns
pickle serialized data object.
244 {
245 pybind11::gil_scoped_acquire acquire;
246 try {
247 pybind11::dict md_dict = pybind11::cast(md);
248 pybind11::set changed_or_set = pybind11::cast(md.changed_or_set);
249 pybind11::module pickle = pybind11::module::import("pickle");
250 pybind11::object dumps = pickle.attr("dumps");
251 pybind11::object md_dump =
252 dumps(pybind11::make_tuple(md_dict, changed_or_set));
253 pybind11::gil_scoped_release release;
254 return md_dump;
255 } catch (...) {
256 pybind11::gil_scoped_release release;
257 throw;
258 };
259}

The documentation for this class was generated from the following files: