LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
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>
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 
291  Schema& operator=(Schema const& other);
292  Schema& operator=(Schema&& other);
293  ~Schema();
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  }
367  std::string join(std::string const& a, std::string const& b, std::string const& c,
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 
454 inline SubSchema Schema::operator[](std::string const& name) const {
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
Defines the fields and offsets for a table.
Definition: Schema.h:50
friend class SubSchema
Definition: Schema.h:316
Field base class default implementation (used for numeric scalars and lsst::geom::Angle).
Definition: FieldBase.h:43
void setAliasMap(std::shared_ptr< AliasMap > aliases)
Set the alias map.
Definition: Schema.cc:722
Schema()
Construct an empty Schema.
Definition: Schema.cc:609
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:148
A proxy type for name lookups in a Schema.
Definition: Schema.h:357
ComparisonFlags
Bit flags used when comparing schemas.
Definition: Schema.h:64
Basic LSST definitions.
int compare(Schema const &other, int flags=EQUAL_KEYS) const
Do a detailed equality comparison of two schemas.
Definition: Schema.cc:698
Keys have the same types offsets, and sizes.
Definition: Schema.h:65
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
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
table::Key< int > b
static Schema fromFitsMetadata(daf::base::PropertyList &header, bool stripMetadata=true)
Construct from reading a FITS header.
Definition: Schema.cc:637
table::Key< int > a
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Definition: Schema.cc:729
STL namespace.
static Schema readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Construct from reading a FITS file.
Definition: Schema.cc:619
Schemas have identical AliasMaps.
Definition: Schema.h:70
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::string join(std::string const &a, std::string const &b, std::string const &c) const
Join strings using the field delimiter appropriate for this Schema.
Definition: Schema.h:77
ItemVariant const * other
Definition: Schema.cc:56
void apply(F &&func) const
Run functor on the SchemaItem represented by this SubSchema.
Definition: Schema.h:402
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
bool operator!=(Schema const &other) const
Equality comparison.
Definition: Schema.h:226
std::string const & getPrefix() const
Return the prefix that defines this SubSchema relative to its parent Schema.
Definition: Schema.h:410
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:212
STL class.
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
Fields have the same units (ordered).
Definition: Schema.h:68
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition: Schema.cc:656
Fields have the same names (ordered).
Definition: Schema.h:66
A base class for image defects.
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
int contains(Schema const &other, int flags=EQUAL_KEYS) const
Test whether the given schema is a subset of this.
Definition: Schema.cc:679
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
table::Schema schema
Definition: Amplifier.cc:115
A description of a field in a table.
Definition: Field.h:24
Key< U > key
Definition: Schema.cc:281
int getFlagFieldCount() const
The number of Flag fields.
Definition: Schema.h:154
std::string join(std::string const &a, std::string const &b, std::string const &c) const
Join strings using the field delimiter appropriate for this Schema.
Definition: Schema.h:364
int getFieldCount() const
The total number of fields.
Definition: Schema.h:151
A private implementation class to hide the messy details of Schema.
Definition: SchemaImpl.h:48
Definition: __init__.py:1
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Schema.cc:709
STL class.
A functor-wrapper used in the implementation of Schema::forEach.
Definition: SchemaImpl.h:158
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
Definition: Schema.h:269
friend std::ostream & operator<<(std::ostream &os, Schema const &schema)
Stringification.
Definition: Schema.cc:751
SubSchema operator[](std::string const &name) const
Look up a (possibly incomplete) name in the Schema.
Definition: Schema.h:454
static int const VERSION
Definition: Schema.h:56
A class used as a handle to a particular field in a table.
Definition: fwd.h:45
bool operator==(Schema const &other) const
Equality comparison.
Definition: Schema.h:225
void replaceField(Key< T > const &key, Field< T > const &field)
Replace the Field (name/description) for an existing Key.
Definition: Schema.cc:674
size_t operator()(argument_type const &obj) const noexcept
Definition: Schema.h:466
std::ostream * os
Definition: Schema.cc:746
T for_each(T... args)
std::string join(std::string const &a, std::string const &b, std::string const &c, std::string const &d) const
Join strings using the field delimiter appropriate for this Schema.
Definition: Schema.h:80
std::string join(std::string const &a, std::string const &b, std::string const &c, std::string const &d) const
Join strings using the field delimiter appropriate for this Schema.
Definition: Schema.h:367
Fields have the same documentation (ordered).
Definition: Schema.h:67
STL class.
std::set< std::string > getNames(bool topOnly=false) const
Return a set of field names in the schema.
Definition: Schema.cc:653
Schema & operator=(Schema const &other)
Fields are identical (but aliases may not be).
Definition: Schema.h:69
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
int getNonFlagFieldCount() const
The number of non-Flag fields.
Definition: Schema.h:157
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
Everything is the same.
Definition: Schema.h:71
void findAndApply(std::string const &name, F &&func) const
Find a SchemaItem by name and run a functor on it.
Definition: Schema.h:116
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:25