LSSTApplications  19.0.0-10-g4a5fae6+3,19.0.0-10-g920eed2,19.0.0-11-g48a0200+2,19.0.0-18-gfc4e62b+16,19.0.0-2-g3b2f90d+2,19.0.0-2-gd671419+6,19.0.0-20-g5a5a17ab+14,19.0.0-21-g2644856+17,19.0.0-24-g0913cb1,19.0.0-24-g878c510+4,19.0.0-25-g6c8df7140+1,19.0.0-25-gb330496+4,19.0.0-3-g2b32d65+6,19.0.0-3-g8227491+15,19.0.0-3-g9c54d0d+15,19.0.0-3-gca68e65+11,19.0.0-3-gcfc5f51+6,19.0.0-3-ge110943+14,19.0.0-3-ge74d124,19.0.0-30-g9c3fd16+5,19.0.0-4-g06f5963+6,19.0.0-4-g10df615,19.0.0-4-g3d16501+17,19.0.0-4-g4a9c019+6,19.0.0-4-g5a8b323,19.0.0-4-g66397f0+1,19.0.0-4-g8557e14,19.0.0-4-g8964aba+16,19.0.0-4-ge404a01+15,19.0.0-5-g40f3a5a,19.0.0-5-g4db63b3,19.0.0-5-gb9eeb60,19.0.0-5-gfb03ce7+16,19.0.0-6-gbaebbfb+15,19.0.0-61-gec4c6e08+5,19.0.0-7-g039c0b5+15,19.0.0-7-gbea9075+4,19.0.0-7-gc567de5+16,19.0.0-72-g37abf38+2,19.0.0-9-g463f923+15,v20.0.0.rc1
LSSTDataManagementBasePackage
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 <vector>
7 
8 #include "ndarray.h"
9 #include "lsst/base.h"
10 #include "lsst/afw/fits.h"
11 #include "lsst/afw/fitsDefaults.h"
12 #include "lsst/afw/table/Key.h"
13 #include "lsst/afw/table/Field.h"
15 #include "lsst/afw/table/Flag.h"
17 
18 namespace lsst {
19 namespace afw {
20 namespace table {
21 
22 class SubSchema;
23 class BaseRecord;
24 
50 class Schema final {
51  typedef detail::SchemaImpl Impl;
52 
53 public:
54  // This variable is defined in SchemaImpl, but is replicated here as
55  // so that it is available to Python.
57 
65  EQUAL_KEYS = 0x01,
66  EQUAL_NAMES = 0x02,
67  EQUAL_DOCS = 0x04,
68  EQUAL_UNITS = 0x08,
69  EQUAL_FIELDS = 0x0F,
70  EQUAL_ALIASES = 0x10,
71  IDENTICAL = 0x1F
72  };
73 
75  std::string join(std::string const& a, std::string const& b) const;
77  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
78  return join(join(a, b), c);
79  }
81  std::string const& d) const {
82  return join(join(a, b), join(c, d));
83  }
85 
93  template <typename T>
94  SchemaItem<T> find(std::string const& name) const;
95 
104  template <typename T>
105  SchemaItem<T> find(Key<T> const& key) const;
106 
115  template <typename F>
116  void findAndApply(std::string const& name, F&& func) const {
117  _impl->findAndApply(_aliases->apply(name), std::forward<F>(func));
118  }
119 
131  SubSchema operator[](std::string const& name) const;
132 
145  std::set<std::string> getNames(bool topOnly = false) const;
146 
148  int getRecordSize() const { return _impl->getRecordSize(); }
149 
151  int getFieldCount() const { return _impl->getFieldCount(); }
152 
154  int getFlagFieldCount() const { return _impl->getFlagFieldCount(); }
155 
157  int getNonFlagFieldCount() const { return _impl->getNonFlagFieldCount(); }
158 
169  template <typename T>
170  Key<T> addField(Field<T> const& field, bool doReplace = false);
171 
179  template <typename T>
180  Key<T> addField(std::string const& name, std::string const& doc, std::string const& units = "",
181  FieldBase<T> const& base = FieldBase<T>(), bool doReplace = false) {
182  return addField(Field<T>(name, doc, units, base), doReplace);
183  }
184 
192  template <typename T>
194  bool doReplace = false) {
195  return addField(Field<T>(name, doc, base), doReplace);
196  }
197 
199  template <typename T>
200  void replaceField(Key<T> const& key, Field<T> const& field);
201 
211  template <typename F>
212  void forEach(F&& func) const {
213  Impl::VisitorWrapper<F> visitor(std::forward<F>(func));
214  std::for_each(_impl->getItems().begin(), _impl->getItems().end(), visitor);
215  }
216 
218 
225  bool operator==(Schema const& other) const { return compare(other, EQUAL_KEYS); }
226  bool operator!=(Schema const& other) const { return !this->operator==(other); }
228 
230  std::size_t hash_value() const noexcept;
231 
241  int compare(Schema const& other, int flags = EQUAL_KEYS) const;
242 
249  int contains(Schema const& other, int flags = EQUAL_KEYS) const;
250 
257  template <typename T>
258  int contains(SchemaItem<T> const& item, int flags = EQUAL_KEYS) const;
259 
269  std::shared_ptr<AliasMap> getAliasMap() const { return _aliases; }
270 
280 
282  void disconnectAliases();
283 
285  Schema();
286 
288  Schema(Schema const& other);
289  Schema(Schema&& other);
290 
294 
300  static Schema readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU);
301  static Schema readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU);
302  static Schema readFits(fits::Fits& fitsfile);
303 
309  static Schema fromFitsMetadata(daf::base::PropertyList& header, bool stripMetadata = true);
310 
313 
314 private:
315  friend class detail::Access;
316  friend class SubSchema;
317 
319  void _edit();
320 
321  std::shared_ptr<Impl> _impl;
322  std::shared_ptr<AliasMap> _aliases;
323 };
324 
357 class SubSchema final {
358  typedef detail::SchemaImpl Impl;
359 
360 public:
362  std::string join(std::string const& a, std::string const& b) const;
364  std::string join(std::string const& a, std::string const& b, std::string const& c) const {
365  return join(join(a, b), c);
366  }
368  std::string const& d) const {
369  return join(join(a, b), join(c, d));
370  }
372 
374  template <typename T>
375  SchemaItem<T> find(std::string const& name) const;
376 
385  template <typename F>
386  void findAndApply(std::string const& name, F&& func) const {
387  _impl->findAndApply(_aliases->apply(join(_name, name)), std::forward<F>(func));
388  }
389 
401  template <typename F>
402  void apply(F&& func) const {
403  _impl->findAndApply(_aliases->apply(_name), std::forward<F>(func));
404  }
405 
407  SubSchema operator[](std::string const& name) const;
408 
410  std::string const& getPrefix() const { return _name; }
411 
418  std::set<std::string> getNames(bool topOnly = false) const;
419 
427  template <typename T>
428  operator Key<T>() const {
429  return _impl->find<T>(_aliases->apply(_name)).key;
430  }
431 
439  template <typename T>
440  operator Field<T>() const {
441  return _impl->find<T>(_aliases->apply(_name)).field;
442  }
443 
444 private:
445  friend class Schema;
446 
448 
449  std::shared_ptr<Impl> _impl;
450  std::shared_ptr<AliasMap> _aliases;
451  std::string _name;
452 };
453 
455  return SubSchema(_impl, _aliases, name);
456 }
457 } // namespace table
458 } // namespace afw
459 } // namespace lsst
460 
461 namespace std {
462 template <>
463 struct hash<lsst::afw::table::Schema> {
466  size_t operator()(argument_type const& obj) const noexcept { return obj.hash_value(); }
467 };
468 } // namespace std
469 
470 #endif // !AFW_TABLE_Schema_h_INCLUDED
schema
table::Schema schema
Definition: Amplifier.cc:115
lsst::afw::table::Schema::addField
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:193
lsst::afw::table::SubSchema::find
SchemaItem< T > find(std::string const &name) const
Find a nested SchemaItem by name.
Definition: Schema.cc:773
std::for_each
T for_each(T... args)
Field.h
std::string
STL class.
std::shared_ptr
STL class.
lsst::afw::table::Schema::addField
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:668
lsst::afw::fits::DEFAULT_HDU
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
base
Definition: __init__.py:1
lsst::afw::fits::Fits
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
lsst::afw::table::Schema::join
std::string join(std::string const &a, std::string const &b, std::string const &c, std::string const &d) const
Definition: Schema.h:80
lsst::afw::table::Schema::compare
int compare(Schema const &other, int flags=EQUAL_KEYS) const
Do a detailed equality comparison of two schemas.
Definition: Schema.cc:698
lsst::afw::table::Schema::getRecordSize
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:148
lsst::afw::table::SubSchema::operator[]
SubSchema operator[](std::string const &name) const
Return a nested proxy.
Definition: Schema.cc:777
lsst::afw::table::detail::SchemaImpl
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:48
lsst::afw::table::Schema::findAndApply
void findAndApply(std::string const &name, F &&func) const
Find a SchemaItem by name and run a functor on it.
Definition: Schema.h:116
fits.h
lsst::afw::table::SubSchema::join
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:764
lsst::afw
Definition: imageAlgorithm.dox:1
lsst::afw::table::Schema::addField
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:180
lsst::afw::table::Schema::operator=
Schema & operator=(Schema const &other)
lsst::daf::base::PropertyList
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
lsst::afw::table::Schema::ComparisonFlags
ComparisonFlags
Bit flags used when comparing schemas.
Definition: Schema.h:64
lsst::afw::table::Schema
Defines the fields and offsets for a table.
Definition: Schema.h:50
lsst::afw::table::SubSchema::getNames
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:781
lsst::afw::fits::MemFileManager
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
lsst::afw::table::Schema::operator!=
bool operator!=(Schema const &other) const
Definition: Schema.h:226
lsst::afw::table::Schema::forEach
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:212
lsst::afw::table::Schema::EQUAL_KEYS
@ EQUAL_KEYS
Keys have the same types offsets, and sizes.
Definition: Schema.h:65
lsst::afw::table::Schema::operator==
bool operator==(Schema const &other) const
Equality comparison.
Definition: Schema.h:225
lsst::afw::table::Schema::find
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition: Schema.cc:656
lsst::afw::table::Schema::Schema
Schema()
Construct an empty Schema.
Definition: Schema.cc:609
lsst::afw::table::Schema::hash_value
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Schema.cc:709
lsst::afw::table::Schema::operator[]
SubSchema operator[](std::string const &name) const
Look up a (possibly incomplete) name in the Schema.
Definition: Schema.h:454
lsst::afw::table::Schema::getNames
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
Definition: Schema.cc:653
lsst::afw::table::AliasMap
Mapping class that holds aliases for a Schema.
Definition: AliasMap.h:36
lsst::afw::table::SubSchema::join
std::string join(std::string const &a, std::string const &b, std::string const &c) const
Definition: Schema.h:364
std::ostream
STL class.
fitsDefaults.h
other
ItemVariant const * other
Definition: Schema.cc:56
field
table::Key< int > field
Definition: ApCorrMap.cc:77
lsst::afw::table::Schema::readFits
static Schema readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Construct from reading a FITS file.
Definition: Schema.cc:619
lsst::afw::table::Schema::setAliasMap
void setAliasMap(std::shared_ptr< AliasMap > aliases)
Set the alias map.
Definition: Schema.cc:722
lsst::afw::table::SchemaItem
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:25
lsst::afw::table::Schema::join
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:641
lsst::afw::table::Schema::~Schema
~Schema()
lsst::afw::table::Key
A class used as a handle to a particular field in a table.
Definition: fwd.h:45
lsst::afw::table::SubSchema::findAndApply
void findAndApply(std::string const &name, F &&func) const
Find a nested SchemaItem by name and run a functor on it.
Definition: Schema.h:386
lsst::afw::table::Schema::EQUAL_ALIASES
@ EQUAL_ALIASES
Schemas have identical AliasMaps.
Definition: Schema.h:70
base.h
lsst::afw::table::Schema::EQUAL_DOCS
@ EQUAL_DOCS
Fields have the same documentation (ordered).
Definition: Schema.h:67
b
table::Key< int > b
Definition: TransmissionCurve.cc:467
lsst::afw::table::Schema::IDENTICAL
@ IDENTICAL
Everything is the same.
Definition: Schema.h:71
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::afw::table::Schema::operator<<
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
Definition: Schema.cc:751
lsst::afw::table::Schema::getNonFlagFieldCount
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: Schema.h:157
lsst::afw::table::Schema::EQUAL_UNITS
@ EQUAL_UNITS
Fields have the same units (ordered).
Definition: Schema.h:68
AliasMap.h
os
std::ostream * os
Definition: Schema.cc:746
lsst::afw::table::SubSchema::apply
void apply(F &&func) const
Run functor on the SchemaItem represented by this SubSchema.
Definition: Schema.h:402
lsst::afw::table::SubSchema::join
std::string join(std::string const &a, std::string const &b, std::string const &c, std::string const &d) const
Definition: Schema.h:367
lsst::afw::table::FieldBase
Field base class default implementation (used for numeric scalars and lsst::geom::Angle).
Definition: FieldBase.h:43
Key.h
lsst::afw::table::Schema::VERSION
static int const VERSION
Definition: Schema.h:56
lsst::afw::table::Schema::getFieldCount
int getFieldCount() const
The total number of fields.
Definition: Schema.h:151
lsst::afw::table::Field
A description of a field in a table.
Definition: Field.h:24
lsst::afw::table::detail::Access
Definition: Access.h:31
a
table::Key< int > a
Definition: TransmissionCurve.cc:466
SchemaImpl.h
lsst::afw::table::SubSchema
A proxy type for name lookups in a Schema.
Definition: Schema.h:357
std
STL namespace.
lsst::afw::table::Schema::fromFitsMetadata
static Schema fromFitsMetadata(daf::base::PropertyList &header, bool stripMetadata=true)
Construct from reading a FITS header.
Definition: Schema.cc:637
key
Key< U > key
Definition: Schema.cc:281
lsst::afw::table::Schema::SubSchema
friend class SubSchema
Definition: Schema.h:316
lsst::afw::table::Schema::getFlagFieldCount
int getFlagFieldCount() const
The number of Flag fields.
Definition: Schema.h:154
std::hash< lsst::afw::table::Schema >::operator()
size_t operator()(argument_type const &obj) const noexcept
Definition: Schema.h:466
std::size_t
lsst::afw::table::Schema::disconnectAliases
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Definition: Schema.cc:729
lsst::afw::table::Schema::join
std::string join(std::string const &a, std::string const &b, std::string const &c) const
Definition: Schema.h:77
lsst::afw::table::Schema::EQUAL_NAMES
@ EQUAL_NAMES
Fields have the same names (ordered).
Definition: Schema.h:66
lsst::afw::table::detail::SchemaImpl::VERSION
static int const VERSION
Definition: SchemaImpl.h:59
lsst::afw::table::Schema::getAliasMap
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
Definition: Schema.h:269
lsst::afw::table::detail::SchemaImpl::VisitorWrapper
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:158
lsst::afw::table::Schema::contains
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
Definition: Schema.cc:679
lsst::afw::table::Schema::EQUAL_FIELDS
@ EQUAL_FIELDS
Fields are identical (but aliases may not be).
Definition: Schema.h:69
std::set
STL class.
lsst::afw::table::Schema::replaceField
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field (name/description) for an existing Key.
Definition: Schema.cc:674
std::hash
lsst::afw::table::SubSchema::getPrefix
std::string const & getPrefix() const
Return the prefix that defines this SubSchema relative to its parent Schema.
Definition: Schema.h:410
Flag.h
lsst::afw::table::Schema::operator=
Schema & operator=(Schema &&other)