LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
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 { namespace afw { namespace table { namespace io {
15 
31 class FitsReader {
32 public:
33 
41  explicit FitsReader(std::string const & persistedClassName);
42 
63  template <typename ContainerT>
64  static ContainerT apply(afw::fits::Fits & fits, int ioFlags, PTR(InputArchive) archive=PTR(InputArchive)()) {
65  PTR(daf::base::PropertyList) metadata = std::make_shared<daf::base::PropertyList>();
66  fits.readMetadata(*metadata, true);
67  FitsReader const * reader = _lookupFitsReader(*metadata);
68  FitsSchemaInputMapper mapper(*metadata, true);
69  reader->_setupArchive(fits, mapper, archive, ioFlags);
70  PTR(BaseTable) table = reader->makeTable(mapper, metadata, ioFlags, true);
71  ContainerT container(std::dynamic_pointer_cast<typename ContainerT::Table>(table));
72  if (!container.getTable()) {
73  throw LSST_EXCEPT(
74  pex::exceptions::RuntimeError,
75  "Invalid table class for catalog."
76  );
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 template
83  // as Catalog<T> (which invokes this method in readFits).
84  const_cast<typename std::remove_const<typename ContainerT::Record>::type&>(
85  *container.addNew()
86  ),
87  fits, row
88  );
89  }
90  return container;
91  }
92 
99  template <typename ContainerT, typename SourceT>
100  static ContainerT apply(SourceT & source, int hdu, int ioFlags, PTR(InputArchive) archive=PTR(InputArchive)()) {
102  fits.setHdu(hdu);
103  return apply<ContainerT>(fits, ioFlags, archive);
104  }
105 
125  virtual PTR(BaseTable) makeTable(
126  FitsSchemaInputMapper & mapper,
127  PTR(daf::base::PropertyList) metadata,
128  int ioFlags,
129  bool stripMetadata
130  ) const;
131 
136  virtual bool usesArchive(int ioFlags) const { return false; }
137 
138  virtual ~FitsReader() {}
139 
140 private:
141 
142  static FitsReader const * _lookupFitsReader(daf::base::PropertyList const & metadata);
143 
144  void _setupArchive(
145  afw::fits::Fits & fits,
146  FitsSchemaInputMapper & mapper,
147  PTR(InputArchive) archive,
148  int ioFlags
149  ) const;
150 
151 };
152 
153 }}}} // namespace lsst::afw::table::io
154 
155 #endif // !AFW_TABLE_IO_FitsReader_h_INCLUDED
static FitsReader const * _lookupFitsReader(daf::base::PropertyList const &metadata)
A utility class for reading FITS binary tables.
Definition: FitsReader.h:31
Class for storing ordered metadata with comments.
Definition: PropertyList.h:82
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:202
void _setupArchive(afw::fits::Fits &fits, FitsSchemaInputMapper &mapper, boost::shared_ptr< InputArchive > archive, int ioFlags) const
void readRecord(BaseRecord &record, afw::fits::Fits &fits, std::size_t row)
Fill a record from a FITS binary table row.
static ContainerT apply(SourceT &source, int hdu, int ioFlags, boost::shared_ptr< InputArchive > archive=boost::shared_ptr< InputArchive >())
Create a new Catalog by reading a FITS file.
Definition: FitsReader.h:100
std::size_t countRows()
Return the number of row in a table.
virtual boost::shared_ptr< BaseTable > makeTable(FitsSchemaInputMapper &mapper, boost::shared_ptr< daf::base::PropertyList > metadata, int ioFlags, bool stripMetadata) const
Callback to create a Table object from a FITS binary table schema.
void setHdu(int hdu, bool relative=false)
Set the current HDU.
static ContainerT apply(afw::fits::Fits &fits, int ioFlags, boost::shared_ptr< InputArchive > archive=boost::shared_ptr< InputArchive >())
Create a new Catalog by reading a FITS binary table.
Definition: FitsReader.h:64
Utilities for working with FITS files.
int row
Definition: CR.cc:158
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
#define PTR(...)
Definition: base.h:41
void readMetadata(daf::base::PropertySet &metadata, bool strip=false)
Read a FITS header into a PropertySet or PropertyList.
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:28
FitsReader(std::string const &persistedClassName)
Construct a FitsReader, registering it to be used for all persisted tables with the given tag...
Base class for all tables.
Definition: BaseTable.h:43
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:136