LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
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 <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>
193  Key<T> addField(std::string const& name, std::string const& doc, FieldBase<T> const& base,
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 
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
table::Key< std::string > name
Definition: Amplifier.cc:116
table::Key< int > field
Definition: ApCorrMap.cc:77
ItemVariant const * other
Definition: Schema.cc:56
Key< U > key
Definition: Schema.cc:281
std::ostream * os
Definition: Schema.cc:746
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:50
static int const VERSION
Definition: Schema.h:56
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
int getFlagFieldCount() const
The number of Flag fields.
Definition: Schema.h:154
Schema & operator=(Schema &&other)
Schema()
Construct an empty Schema.
Definition: Schema.cc:609
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Definition: Schema.cc:729
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:80
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: Schema.h:157
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
void setAliasMap(std::shared_ptr< AliasMap > aliases)
Set the alias map.
Definition: Schema.cc:722
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
bool operator!=(Schema const &other) const
Definition: Schema.h:226
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
Definition: Schema.cc:653
static Schema fromFitsMetadata(daf::base::PropertyList &header, bool stripMetadata=true)
Construct from reading a FITS header.
Definition: Schema.cc:637
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
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:212
static Schema readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Construct from reading a FITS file.
Definition: Schema.cc:619
SubSchema operator[](std::string const &name) const
Look up a (possibly incomplete) name in the Schema.
Definition: Schema.h:454
int getFieldCount() const
The total number of fields.
Definition: Schema.h:151
friend class SubSchema
Definition: Schema.h:316
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Schema.cc:709
void findAndApply(std::string const &name, F &&func) const
Find a SchemaItem by name and run a functor on it.
Definition: Schema.h:116
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
Definition: Schema.cc:679
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:148
std::string join(std::string const &a, std::string const &b, std::string const &c) const
Definition: Schema.h:77
int compare(Schema const &other, int flags=EQUAL_KEYS) const
Do a detailed equality comparison of two schemas.
Definition: Schema.cc:698
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field (name/description) for an existing Key.
Definition: Schema.cc:674
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
Definition: Schema.cc:751
bool operator==(Schema const &other) const
Equality comparison.
Definition: Schema.h:225
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition: Schema.cc:656
ComparisonFlags
Bit flags used when comparing schemas.
Definition: Schema.h:64
@ EQUAL_DOCS
Fields have the same documentation (ordered).
Definition: Schema.h:67
@ IDENTICAL
Everything is the same.
Definition: Schema.h:71
@ EQUAL_NAMES
Fields have the same names (ordered).
Definition: Schema.h:66
@ EQUAL_UNITS
Fields have the same units (ordered).
Definition: Schema.h:68
@ EQUAL_KEYS
Keys have the same types offsets, and sizes.
Definition: Schema.h:65
@ EQUAL_FIELDS
Fields are identical (but aliases may not be).
Definition: Schema.h:69
@ EQUAL_ALIASES
Schemas have identical AliasMaps.
Definition: Schema.h:70
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
Definition: Schema.h:269
A proxy type for name lookups in a Schema.
Definition: Schema.h:357
std::string const & getPrefix() const
Return the prefix that defines this SubSchema relative to its parent Schema.
Definition: Schema.h:410
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
std::string join(std::string const &a, std::string const &b, std::string const &c) const
Definition: Schema.h:364
void apply(F &&func) const
Run functor on the SchemaItem represented by this SubSchema.
Definition: Schema.h:402
std::string join(std::string const &a, std::string const &b, std::string const &c, std::string const &d) const
Definition: Schema.h:367
SubSchema operator[](std::string const &name) const
Return a nested proxy.
Definition: Schema.cc:777
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
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
SchemaItem< T > find(std::string const &name) const
Find a nested SchemaItem by name.
Definition: Schema.cc:773
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:48
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
T for_each(T... args)
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:43
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:25
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:158
size_t operator()(argument_type const &obj) const noexcept
Definition: Schema.h:466