LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Schema.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 #ifndef AFW_TABLE_Schema_h_INCLUDED
3 #define AFW_TABLE_Schema_h_INCLUDED
4 
5 #include <memory>
6 #include <algorithm>
7 #include <vector>
8 
9 #include "ndarray.h"
10 #include "lsst/base.h"
11 #include "lsst/afw/fits.h"
12 #include "lsst/afw/fitsDefaults.h"
13 #include "lsst/afw/table/Key.h"
14 #include "lsst/afw/table/Field.h"
16 #include "lsst/afw/table/Flag.h"
18 
19 namespace lsst {
20 namespace afw {
21 namespace table {
22 
23 class SubSchema;
24 class BaseRecord;
25 
51 class Schema final {
52  using Impl = detail::SchemaImpl;
53 
54 public:
55  // This variable is defined in SchemaImpl, but is replicated here as
56  // so that it is available to Python.
58 
66  EQUAL_KEYS = 0x01,
67  EQUAL_NAMES = 0x02,
68  EQUAL_DOCS = 0x04,
69  EQUAL_UNITS = 0x08,
70  EQUAL_FIELDS = 0x0F,
71  EQUAL_ALIASES = 0x10,
72  IDENTICAL = 0x1F
73  };
74 
76  std::string join(std::string const& a, std::string const& b) const;
78  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
79  return join(join(a, b), c);
80  }
82  std::string const& d) const {
83  return join(join(a, b), join(c, d));
84  }
86 
94  template <typename T>
95  SchemaItem<T> find(std::string const& name) const;
96 
105  template <typename T>
106  SchemaItem<T> find(Key<T> const& key) const;
107 
116  template <typename F>
117  void findAndApply(std::string const& name, F&& func) const {
118  _impl->findAndApply(_aliases->apply(name), std::forward<F>(func));
119  }
120 
132  SubSchema operator[](std::string const& name) const;
133 
146  std::set<std::string> getNames(bool topOnly = false) const;
147 
149  std::size_t getRecordSize() const { return _impl->getRecordSize(); }
150 
152  std::size_t getFieldCount() const { return _impl->getFieldCount(); }
153 
155  std::size_t getFlagFieldCount() const { return _impl->getFlagFieldCount(); }
156 
159 
170  template <typename T>
171  Key<T> addField(Field<T> const& field, bool doReplace = false);
172 
180  template <typename T>
181  Key<T> addField(std::string const& name, std::string const& doc, std::string const& units = "",
182  FieldBase<T> const& base = FieldBase<T>(), bool doReplace = false) {
183  return addField(Field<T>(name, doc, units, base), doReplace);
184  }
185 
193  template <typename T>
194  Key<T> addField(std::string const& name, std::string const& doc, FieldBase<T> const& base,
195  bool doReplace = false) {
196  return addField(Field<T>(name, doc, base), doReplace);
197  }
198 
200  template <typename T>
201  void replaceField(Key<T> const& key, Field<T> const& field);
202 
204 
213  template <typename F>
214  void forEach(F & func) const {
215  for (auto const & item : _impl->getItems()) {
216  std::visit(func, item);
217  }
218  }
219  template <typename F>
220  void forEach(F const & func) const {
221  for (auto const & item : _impl->getItems()) {
222  std::visit(func, item);
223  }
224  }
226 
228 
235  bool operator==(Schema const& other) const { return compare(other, EQUAL_KEYS); }
236  bool operator!=(Schema const& other) const { return !this->operator==(other); }
238 
240  std::size_t hash_value() const noexcept;
241 
251  int compare(Schema const& other, int flags = EQUAL_KEYS) const;
252 
259  int contains(Schema const& other, int flags = EQUAL_KEYS) const;
260 
267  template <typename T>
268  int contains(SchemaItem<T> const& item, int flags = EQUAL_KEYS) const;
269 
279  std::shared_ptr<AliasMap> getAliasMap() const { return _aliases; }
280 
290 
292  void disconnectAliases();
293 
295  Schema();
296 
298  Schema(Schema const& other);
299  Schema(Schema&& other);
300 
301  Schema& operator=(Schema const& other);
304 
310  static Schema readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU);
311  static Schema readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU);
312  static Schema readFits(fits::Fits& fitsfile);
313 
319  static Schema fromFitsMetadata(daf::base::PropertyList& header, bool stripMetadata = true);
320 
323 
324 private:
325  friend class detail::Access;
326  friend class SubSchema;
327 
329  void _edit();
330 
331  std::shared_ptr<Impl> _impl;
332  std::shared_ptr<AliasMap> _aliases;
333 };
334 
368  using Impl = detail::SchemaImpl;
369 
370 public:
372  std::string join(std::string const& a, std::string const& b) const;
374  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
375  return join(join(a, b), c);
376  }
378  std::string const& d) const {
379  return join(join(a, b), join(c, d));
380  }
382 
384  template <typename T>
385  SchemaItem<T> find(std::string const& name) const;
386 
395  template <typename F>
396  void findAndApply(std::string const& name, F&& func) const {
397  _impl->findAndApply(_aliases->apply(join(_name, name)), std::forward<F>(func));
398  }
399 
411  template <typename F>
412  void apply(F&& func) const {
413  _impl->findAndApply(_aliases->apply(_name), std::forward<F>(func));
414  }
415 
417  SubSchema operator[](std::string const& name) const;
418 
420  std::string const& getPrefix() const { return _name; }
421 
428  std::set<std::string> getNames(bool topOnly = false) const;
429 
437  template <typename T>
438  operator Key<T>() const {
439  return _impl->find<T>(_aliases->apply(_name)).key;
440  }
441 
449  template <typename T>
450  operator Field<T>() const {
451  return _impl->find<T>(_aliases->apply(_name)).field;
452  }
453 
454 private:
455  friend class Schema;
456 
458 
459  std::shared_ptr<Impl> _impl;
460  std::shared_ptr<AliasMap> _aliases;
461  std::string _name;
462 };
463 
465  return SubSchema(_impl, _aliases, name);
466 }
467 } // namespace table
468 } // namespace afw
469 } // namespace lsst
470 
471 namespace std {
472 template <>
473 struct hash<lsst::afw::table::Schema> {
476  size_t operator()(argument_type const& obj) const noexcept { return obj.hash_value(); }
477 };
478 } // namespace std
479 
480 #endif // !AFW_TABLE_Schema_h_INCLUDED
table::Key< std::string > name
Definition: Amplifier.cc:116
table::Key< int > field
Definition: ApCorrMap.cc:77
std::ostream * os
Definition: Schema.cc:557
table::Key< int > b
table::Key< int > a
table::Schema schema
Definition: python.h:134
Basic LSST definitions.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
Mapping class that holds aliases for a Schema.
Definition: AliasMap.h:36
A class used as a handle to a particular field in a table.
Definition: Key.h:53
Defines the fields and offsets for a table.
Definition: Schema.h:51
void forEach(F &func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:214
static int const VERSION
Definition: Schema.h:57
Key< T > addField(std::string const &name, std::string const &doc, std::string const &units="", FieldBase< T > const &base=FieldBase< T >(), bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
Definition: Schema.h:181
Schema & operator=(Schema &&other)
Schema()
Construct an empty Schema.
Definition: Schema.cc:420
std::size_t getFieldCount() const
The total number of fields.
Definition: Schema.h:152
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Definition: Schema.cc:540
Schema & operator=(Schema const &other)
std::string join(std::string const &a, std::string const &b, std::string const &c, std::string const &d) const
Definition: Schema.h:81
Schema(Schema const &other)
Copy constructor.
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema.
Definition: Schema.cc:452
void setAliasMap(std::shared_ptr< AliasMap > aliases)
Set the alias map.
Definition: Schema.cc:533
Key< T > addField(std::string const &name, std::string const &doc, FieldBase< T > const &base, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
Definition: Schema.h:194
bool operator!=(Schema const &other) const
Definition: Schema.h:236
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
Definition: Schema.cc:464
static Schema fromFitsMetadata(daf::base::PropertyList &header, bool stripMetadata=true)
Construct from reading a FITS header.
Definition: Schema.cc:448
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
Definition: Schema.cc:479
void forEach(F const &func) const
Definition: Schema.h:220
static Schema readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Construct from reading a FITS file.
Definition: Schema.cc:430
SubSchema operator[](std::string const &name) const
Look up a (possibly incomplete) name in the Schema.
Definition: Schema.h:464
friend class SubSchema
Definition: Schema.h:326
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Schema.cc:520
void findAndApply(std::string const &name, F &&func) const
Find a SchemaItem by name and run a functor on it.
Definition: Schema.h:117
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
Definition: Schema.cc:490
std::size_t getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:149
std::string join(std::string const &a, std::string const &b, std::string const &c) const
Definition: Schema.h:78
std::size_t getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: Schema.h:158
int compare(Schema const &other, int flags=EQUAL_KEYS) const
Do a detailed equality comparison of two schemas.
Definition: Schema.cc:509
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field (name/description) for an existing Key.
Definition: Schema.cc:485
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
Definition: Schema.cc:562
bool operator==(Schema const &other) const
Equality comparison.
Definition: Schema.h:235
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition: Schema.cc:467
ComparisonFlags
Bit flags used when comparing schemas.
Definition: Schema.h:65
@ EQUAL_DOCS
Fields have the same documentation (ordered).
Definition: Schema.h:68
@ IDENTICAL
Everything is the same.
Definition: Schema.h:72
@ EQUAL_NAMES
Fields have the same names (ordered).
Definition: Schema.h:67
@ EQUAL_UNITS
Fields have the same units (ordered).
Definition: Schema.h:69
@ EQUAL_KEYS
Keys have the same types offsets, and sizes.
Definition: Schema.h:66
@ EQUAL_FIELDS
Fields are identical (but aliases may not be).
Definition: Schema.h:70
@ EQUAL_ALIASES
Schemas have identical AliasMaps.
Definition: Schema.h:71
std::size_t getFlagFieldCount() const
The number of Flag fields.
Definition: Schema.h:155
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
Definition: Schema.h:279
A proxy type for name lookups in a Schema.
Definition: Schema.h:367
std::string const & getPrefix() const
Return the prefix that defines this SubSchema relative to its parent Schema.
Definition: Schema.h:420
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema.
Definition: Schema.cc:575
std::string join(std::string const &a, std::string const &b, std::string const &c) const
Definition: Schema.h:374
void apply(F &&func) const
Run functor on the SchemaItem represented by this SubSchema.
Definition: Schema.h:412
std::string join(std::string const &a, std::string const &b, std::string const &c, std::string const &d) const
Definition: Schema.h:377
SubSchema operator[](std::string const &name) const
Return a nested proxy.
Definition: Schema.cc:588
void findAndApply(std::string const &name, F &&func) const
Find a nested SchemaItem by name and run a functor on it.
Definition: Schema.h:396
std::set< std::string > getNames(bool topOnly=false) const
Return a set of nested names that start with the SubSchema's prefix.
Definition: Schema.cc:592
SchemaItem< T > find(std::string const &name) const
Find a nested SchemaItem by name.
Definition: Schema.cc:584
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:45
ItemContainer const & getItems() const
Return the vector of SchemaItem variants.
Definition: SchemaImpl.h:133
decltype(auto) findAndApply(std::string const &name, F &&func) const
Find an item by name and run the given functor on it.
Definition: SchemaImpl.h:90
std::size_t getRecordSize() const
The size of a record in bytes.
Definition: SchemaImpl.h:66
std::size_t getFlagFieldCount() const
The number of Flag fields.
Definition: SchemaImpl.h:72
std::size_t getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: SchemaImpl.h:75
SchemaItem< T > find(std::string const &name) const
Find an item by name (used to implement Schema::find).
Definition: Schema.cc:92
std::size_t getFieldCount() const
The total number of fields.
Definition: SchemaImpl.h:69
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
class[[deprecated("Removed with no replacement (but see lsst::afw::image::TransmissionCurve). Will be " "removed after v22.")]] FilterProperty final
Describe the properties of a Filter (e.g.
Definition: Filter.h:53
A base class for image defects.
STL namespace.
Field base class default implementation (used for numeric scalars and lsst::geom::Angle).
Definition: FieldBase.h:41
A description of a field in a table.
Definition: Field.h:24
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:22
size_t operator()(argument_type const &obj) const noexcept
Definition: Schema.h:476