lsst.afw  14.0-23-g38c323faa+1
FitsReader.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 #ifndef AFW_TABLE_IO_FitsReader_h_INCLUDED
3 #define AFW_TABLE_IO_FitsReader_h_INCLUDED
4 
5 #include <type_traits>
6 
7 #include "lsst/afw/fits.h"
13 
14 namespace lsst {
15 namespace afw {
16 namespace table {
17 namespace io {
18 
34 class FitsReader {
35 public:
43  explicit FitsReader(std::string const& persistedClassName);
44 
65  template <typename ContainerT>
66  static ContainerT apply(afw::fits::Fits& fits, int ioFlags,
68  std::shared_ptr<daf::base::PropertyList> metadata = std::make_shared<daf::base::PropertyList>();
69  fits.readMetadata(*metadata, true);
70  FitsReader const* reader = _lookupFitsReader(*metadata);
71  FitsSchemaInputMapper mapper(*metadata, true);
72  reader->_setupArchive(fits, mapper, archive, ioFlags);
73  std::shared_ptr<BaseTable> table = reader->makeTable(mapper, metadata, ioFlags, true);
74  ContainerT container(std::dynamic_pointer_cast<typename ContainerT::Table>(table));
75  if (!container.getTable()) {
76  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Invalid table class for catalog.");
77  }
78  std::size_t nRows = fits.countRows();
79  container.reserve(nRows);
80  for (std::size_t row = 0; row < nRows; ++row) {
81  mapper.readRecord(
82  // We need to be able to support reading Catalog<T const>, since it shares the same
83  // template
84  // as Catalog<T> (which invokes this method in readFits).
86  *container.addNew()),
87  fits, row);
88  }
89  return container;
90  }
91 
98  template <typename ContainerT, typename SourceT>
99  static ContainerT apply(SourceT& source, int hdu, int ioFlags,
102  fits.setHdu(hdu);
103  return apply<ContainerT>(fits, ioFlags, archive);
104  }
105 
127  int ioFlags, bool stripMetadata) const;
128 
133  virtual bool usesArchive(int ioFlags) const { return false; }
134 
135  virtual ~FitsReader() {}
136 
137 private:
138  static FitsReader const* _lookupFitsReader(daf::base::PropertyList const& metadata);
139 
140  void _setupArchive(afw::fits::Fits& fits, FitsSchemaInputMapper& mapper,
141  std::shared_ptr<InputArchive> archive, int ioFlags) const;
142 };
143 }
144 }
145 }
146 } // namespace lsst::afw::table::io
147 
148 #endif // !AFW_TABLE_IO_FitsReader_h_INCLUDED
std::size_t countRows()
Return the number of row in a table.
static ContainerT apply(SourceT &source, int hdu, int ioFlags, std::shared_ptr< InputArchive > archive=std::shared_ptr< InputArchive >())
Create a new Catalog by reading a FITS file.
Definition: FitsReader.h:99
A utility class for reading FITS binary tables.
Definition: FitsReader.h:34
FitsReader(std::string const &persistedClassName)
Construct a FitsReader, registering it to be used for all persisted tables with the given tag...
A class that describes a mapping from a FITS binary table to an afw::table Schema.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:279
STL class.
virtual std::shared_ptr< BaseTable > makeTable(FitsSchemaInputMapper &mapper, std::shared_ptr< daf::base::PropertyList > metadata, int ioFlags, bool stripMetadata) const
Callback to create a Table object from a FITS binary table schema.
A base class for image defects.
void setHdu(int hdu, bool relative=false)
Set the current HDU.
#define LSST_EXCEPT(type,...)
static ContainerT apply(afw::fits::Fits &fits, int ioFlags, std::shared_ptr< InputArchive > archive=std::shared_ptr< InputArchive >())
Create a new Catalog by reading a FITS binary table.
Definition: FitsReader.h:66
virtual bool usesArchive(int ioFlags) const
Callback that should return true if the FitsReader subclass makes use of an InputArchive to read firs...
Definition: FitsReader.h:133
void readMetadata(daf::base::PropertySet &metadata, bool strip=false)
Read a FITS header into a PropertySet or PropertyList.
void readRecord(BaseRecord &record, afw::fits::Fits &fits, std::size_t row)
Fill a record from a FITS binary table row.