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
FitsWriter.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 #ifndef AFW_TABLE_IO_FitsWriter_h_INCLUDED
3 #define AFW_TABLE_IO_FitsWriter_h_INCLUDED
4 
5 #include <set>
6 
7 #include "lsst/base.h"
8 #include "lsst/pex/exceptions.h"
10 #include "lsst/afw/fits.h"
11 
12 namespace lsst { namespace afw { namespace table { namespace io {
13 
22 class FitsWriter {
23 public:
24 
26 
34  template <typename OutputT, typename ContainerT>
35  static void apply(OutputT & output, std::string const & mode, ContainerT const & container, int flags) {
36  Fits fits(output, mode, Fits::AUTO_CLOSE | Fits::AUTO_CHECK);
37  apply(fits, container, flags);
38  }
39 
41  template <typename ContainerT>
42  static void apply(Fits & fits, ContainerT const & container, int flags) {
43  PTR(FitsWriter) writer
44  = boost::static_pointer_cast<BaseTable const>(container.getTable())->makeFitsWriter(&fits, flags);
45  writer->write(container);
46  }
47 
55  template <typename ContainerT>
56  void write(ContainerT const & container) {
57  std::set<PTR(BaseTable const)> tables;
58  for (typename ContainerT::const_iterator i = container.begin(); i != container.end(); ++i) {
59  if (i->getTable() != container.getTable()) tables.insert(i->getTable());
60  }
61  for (std::set<PTR(BaseTable const)>::iterator j = tables.begin(); j != tables.end(); ++j) {
62  if (
63  (**j).getSchema().compare(container.getTable()->getSchema(), Schema::IDENTICAL)
65  ) {
66  throw LSST_EXCEPT(
67  pex::exceptions::LogicError,
68  "Cannot save Catalog with heterogenous schemas"
69  );
70  }
71  }
72  _writeTable(container.getTable(), container.size());
73  for (typename ContainerT::const_iterator i = container.begin(); i != container.end(); ++i) {
74  _writeRecord(*i);
75  }
76  _finish();
77  }
78 
80  explicit FitsWriter(Fits * fits, int flags) : _fits(fits), _flags(flags) {}
81 
82 protected:
83 
85  virtual void _writeTable(CONST_PTR(BaseTable) const & table, std::size_t nRows);
86 
88  virtual void _writeRecord(BaseRecord const & source);
89 
91  virtual void _finish() {}
92 
93  Fits * _fits; // wrapped cfitsio pointer
94  int _flags; // subclass-defined flags to control writing
95  std::size_t _row; // which row we're currently processing
96 
97 private:
98 
99  struct ProcessRecords;
100 
101 
102  PTR(ProcessRecords) _processor; // a private Schema::forEach functor that write records
103 
104 };
105 
106 }}}} // namespace lsst::afw::table::io
107 
108 #endif // !AFW_TABLE_IO_FitsWriter_h_INCLUDED
Writer object for FITS binary tables.
Definition: FitsWriter.h:22
Include files required for standard LSST Exception handling.
void write(ContainerT const &container)
Write records in a container to disk.
Definition: FitsWriter.h:56
#define PTR(...)
Definition: base.h:41
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:194
virtual void _writeRecord(BaseRecord const &source)
Write an individual record.
static void apply(Fits &fits, ContainerT const &container, int flags)
Low-level driver for writing FITS files, operating on an open FITS file.
Definition: FitsWriter.h:42
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
Base class for all records.
Definition: BaseRecord.h:27
static void apply(OutputT &output, std::string const &mode, ContainerT const &container, int flags)
Driver for writing FITS files.
Definition: FitsWriter.h:35
#define CONST_PTR(...)
Definition: base.h:47
virtual void _writeTable(boost::shared_ptr< BaseTable const > const &table, std::size_t nRows)
Write a table and its schema.
virtual void _finish()
Finish writing a catalog.
Definition: FitsWriter.h:91
Base class for all tables.
Definition: BaseTable.h:44
boost::shared_ptr< ProcessRecords > _processor
Definition: FitsWriter.h:99
Everything is the same.
Definition: Schema.h:67
FitsWriter(Fits *fits, int flags)
Construct from a wrapped cfitsio pointer.
Definition: FitsWriter.h:80