LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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 "lsst/afw/fits.h"
11 
12 namespace lsst { namespace afw { namespace table { namespace io {
13 
29 class FitsReader {
30 public:
31 
39  explicit FitsReader(std::string const & persistedClassName);
40 
61  template <typename ContainerT>
62  static ContainerT apply(afw::fits::Fits & fits, int ioFlags, PTR(InputArchive) archive=PTR(InputArchive)()) {
63  PTR(daf::base::PropertyList) metadata = boost::make_shared<daf::base::PropertyList>();
64  fits.readMetadata(*metadata, true);
65  FitsReader const * reader = _lookupFitsReader(*metadata);
66  FitsSchemaInputMapper mapper(*metadata, true);
67  reader->_setupArchive(fits, mapper, archive, ioFlags);
68  PTR(BaseTable) table = reader->makeTable(mapper, metadata, ioFlags, true);
69  ContainerT container(boost::dynamic_pointer_cast<typename ContainerT::Table>(table));
70  if (!container.getTable()) {
71  throw LSST_EXCEPT(
72  pex::exceptions::RuntimeError,
73  "Invalid table class for catalog."
74  );
75  }
76  std::size_t nRows = fits.countRows();
77  container.reserve(nRows);
78  for (std::size_t row = 0; row < nRows; ++row) {
79  mapper.readRecord(
80  // We need to be able to support reading Catalog<T const>, since it shares the same template
81  // as Catalog<T> (which invokes this method in readFits).
82  const_cast<typename boost::remove_const<typename ContainerT::Record>::type&>(
83  *container.addNew()
84  ),
85  fits, row
86  );
87  }
88  return container;
89  }
90 
97  template <typename ContainerT, typename SourceT>
98  static ContainerT apply(SourceT & source, int hdu, int ioFlags, PTR(InputArchive) archive=PTR(InputArchive)()) {
100  fits.setHdu(hdu);
101  return apply<ContainerT>(fits, ioFlags, archive);
102  }
103 
123  virtual PTR(BaseTable) makeTable(
124  FitsSchemaInputMapper & mapper,
125  PTR(daf::base::PropertyList) metadata,
126  int ioFlags,
127  bool stripMetadata
128  ) const;
129 
134  virtual bool usesArchive(int ioFlags) const { return false; }
135 
136  virtual ~FitsReader() {}
137 
138 private:
139 
140  static FitsReader const * _lookupFitsReader(daf::base::PropertyList const & metadata);
141 
142  void _setupArchive(
143  afw::fits::Fits & fits,
144  FitsSchemaInputMapper & mapper,
145  PTR(InputArchive) archive,
146  int ioFlags
147  ) const;
148 
149 };
150 
151 }}}} // namespace lsst::afw::table::io
152 
153 #endif // !AFW_TABLE_IO_FitsReader_h_INCLUDED
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
A utility class for reading FITS binary tables.
Definition: FitsReader.h:29
static ContainerT apply(SourceT &source, int hdu, int ioFlags, boost::shared_ptr< InputArchive > archive=boost::shared_ptr< InputArchive >())
Definition: FitsReader.h:98
Class for storing ordered metadata with comments.
Definition: PropertyList.h:81
FitsReader(std::string const &persistedClassName)
#define PTR(...)
Definition: base.h:41
static ContainerT apply(afw::fits::Fits &fits, int ioFlags, boost::shared_ptr< InputArchive > archive=boost::shared_ptr< InputArchive >())
Definition: FitsReader.h:62
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:194
virtual bool usesArchive(int ioFlags) const
Definition: FitsReader.h:134
void _setupArchive(afw::fits::Fits &fits, FitsSchemaInputMapper &mapper, boost::shared_ptr< InputArchive > archive, int ioFlags) const
void setHdu(int hdu, bool relative=false)
Set the current HDU.
int row
Definition: CR.cc:153
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:28
Base class for all tables.
Definition: BaseTable.h:44
void readMetadata(daf::base::PropertySet &metadata, bool strip=false)
Read a FITS header into a PropertySet or PropertyList.
static FitsReader const * _lookupFitsReader(daf::base::PropertyList const &metadata)
void readRecord(BaseRecord &record, afw::fits::Fits &fits, std::size_t row)