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
fits.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 #ifndef LSST_AFW_fits_h_INCLUDED
3 #define LSST_AFW_fits_h_INCLUDED
4 
18 #include <string>
19 
20 #include <boost/format.hpp>
21 
22 #include "lsst/base.h"
23 #include "lsst/pex/exceptions.h"
24 #include "lsst/daf/base.h"
25 #include "ndarray.h"
26 
27 namespace lsst { namespace afw { namespace fits {
28 
32 LSST_EXCEPTION_TYPE(FitsError, lsst::pex::exceptions::IoError, lsst::afw::fits::FitsError)
33 
34 
37 LSST_EXCEPTION_TYPE(FitsTypeError, lsst::afw::fits::FitsError, lsst::afw::fits::FitsTypeError)
38 
39 #ifndef SWIG // only want SWIG to see the exceptions; everything else is too low-level for Python.
40 
48 public:
49 
50  virtual void operator()(
51  std::string const & key,
52  std::string const & value,
53  std::string const & comment
54  ) = 0;
55 
57 
58 };
59 
68 std::string makeErrorMessage(std::string const & fileName="", int status=0, std::string const & msg="");
69 inline std::string makeErrorMessage(std::string const & fileName, int status, boost::format const & msg) {
70  return makeErrorMessage(fileName, status, msg.str());
71 }
72 
82 std::string makeErrorMessage(void * fptr, int status=0, std::string const & msg="");
83 inline std::string makeErrorMessage(void * fptr, int status, boost::format const & msg) {
84  return makeErrorMessage(fptr, status, msg.str());
85 }
86 
91 #define LSST_FITS_EXCEPT(type, fitsObj, ...) \
92  type(LSST_EXCEPT_HERE, lsst::afw::fits::makeErrorMessage((fitsObj).fptr, (fitsObj).status, __VA_ARGS__))
93 
97 #define LSST_FITS_CHECK_STATUS(fitsObj, ...) \
98  if ((fitsObj).status != 0) throw LSST_FITS_EXCEPT(lsst::afw::fits::FitsError, fitsObj, __VA_ARGS__)
99 
101 template <typename T> int getBitPix();
102 
107 public:
108 
115  MemFileManager() : _ptr(0), _len(0), _managed(true) {}
116 
123  explicit MemFileManager(std::size_t len) : _ptr(0), _len(0), _managed(true) { reset(len); }
124 
132  MemFileManager(void * ptr, std::size_t len) : _ptr(ptr), _len(len), _managed(false) {}
133 
139  void reset();
140 
149  void reset(std::size_t len);
150 
159  void reset(void * ptr, std::size_t len) { reset(); _ptr = ptr; _len = len; _managed = false; }
160 
162 
163  // No copying
164  MemFileManager (const MemFileManager&) = delete;
165  MemFileManager& operator=(const MemFileManager&) = delete;
166 
167  // No moving
168  MemFileManager (MemFileManager&&) = delete;
170 
172  void* getData() const { return _ptr; }
173 
175  std::size_t getLength() const { return _len; }
176 
177 private:
178 
179  friend class Fits;
180 
181  void * _ptr;
182  std::size_t _len;
183  bool _managed;
184 };
185 
202 class Fits {
203  template <typename T> void createImageImpl(int nAxis, long * nAxes);
204  template <typename T> void writeImageImpl(T const * data, int nElements);
205  template <typename T> void readImageImpl(int nAxis, T * data, long * begin, long * end, long * increment);
206  void getImageShapeImpl(int maxDim, long * nAxes);
207 public:
208 
210  AUTO_CLOSE = 0x01, // Close files when the Fits object goes out of scope if fptr != NULL
211  AUTO_CHECK = 0x02 // Call LSST_FITS_CHECK_STATUS after every cfitsio call
212  };
213 
215  std::string getFileName() const;
216 
218  int getHdu();
219 
229  void setHdu(int hdu, bool relative=false);
230 
232  int countHdus();
233 
235  template <typename T>
237  void updateKey(std::string const & key, T const & value, std::string const & comment);
238  void updateKey(std::string const & key, char const * value, std::string const & comment) {
239  updateKey(key, std::string(value), comment);
240  }
241  template <typename T>
242  void updateKey(std::string const & key, T const & value);
243  void updateKey(std::string const & key, char const * value) {
244  updateKey(key, std::string(value));
245  }
247 
249 
256  template <typename T>
257  void writeKey(std::string const & key, T const & value, std::string const & comment);
258  void writeKey(std::string const & key, char const * value, std::string const & comment) {
259  writeKey(key, std::string(value), comment);
260  }
261  template <typename T>
262  void writeKey(std::string const & key, T const & value);
263  void writeKey(std::string const & key, char const * value) {
264  writeKey(key, std::string(value));
265  }
267 
269  template <typename T>
271  void updateColumnKey(std::string const & prefix, int n, T const & value, std::string const & comment);
272  void updateColumnKey(std::string const & prefix, int n, char const * value, std::string const & comment) {
273  updateColumnKey(prefix, n, std::string(value), comment);
274  }
275  template <typename T>
276  void updateColumnKey(std::string const & prefix, int n, T const & value);
277  void updateColumnKey(std::string const & prefix, int n, char const * value) {
278  updateColumnKey(prefix, n, std::string(value));
279  }
281 
283  template <typename T>
285  void writeColumnKey(std::string const & prefix, int n, T const & value, std::string const & comment);
286  void writeColumnKey(std::string const & prefix, int n, char const * value, std::string const & comment) {
287  writeColumnKey(prefix, n, std::string(value), comment);
288  }
289  template <typename T>
290  void writeColumnKey(std::string const & prefix, int n, T const & value);
291  void writeColumnKey(std::string const & prefix, int n, char const * value) {
292  writeColumnKey(prefix, n, std::string(value));
293  }
295 
305  void writeMetadata(daf::base::PropertySet const & metadata);
306 
316  void readMetadata(daf::base::PropertySet & metadata, bool strip=false);
317 
319  template <typename T>
320  void readKey(std::string const & key, T & value);
321 
330  void forEachKey(HeaderIterationFunctor & functor);
331 
338  void createEmpty();
339 
350  template <typename PixelT, int N>
351  void createImage(ndarray::Vector<ndarray::Size,N> const & shape) {
352  ndarray::Vector<long,N> nAxes(shape.reverse());
353  createImageImpl<PixelT>(N, nAxes.elems);
354  }
355 
362  template <typename PixelT>
363  void createImage(long x, long y) {
364  long naxes[2] = { x, y };
365  createImageImpl<PixelT>(2, naxes);
366  }
367 
375  template <typename T, int N, int C>
376  void writeImage(ndarray::Array<T const,N,C> const & array) {
377  ndarray::Array<T const,N,N> contiguous = ndarray::dynamic_dimension_cast<2>(array);
378  if (contiguous.empty()) contiguous = ndarray::copy(array);
379  writeImageImpl(contiguous.getData(), contiguous.getNumElements());
380  }
381 
383  int getImageDim();
384 
393  template <int N>
394  ndarray::Vector<ndarray::Size,N> getImageShape() {
395  ndarray::Vector<long,N> nAxes(1);
396  getImageShapeImpl(N, nAxes.elems);
397  ndarray::Vector<ndarray::Size,N> shape;
398  for (int i = 0; i < N; ++i) shape[i] = nAxes[N-i-1];
399  return shape;
400  }
401 
408  template <typename T>
409  bool checkImageType();
410 
417  template <typename T, int N>
418  void readImage(
419  ndarray::Array<T,N,N> const & array,
420  ndarray::Vector<int,N> const & offset
421  ) {
422  ndarray::Vector<long,N> begin(offset.reverse());
423  ndarray::Vector<long,N> end(begin);
424  end += array.getShape().reverse();
425  ndarray::Vector<long,N> increment(1);
426  begin += increment; // first FITS pixel is 1, not 0
427  readImageImpl(N, array.getData(), begin.elems, end.elems, increment.elems);
428  }
429 
431  void createTable();
432 
439  template <typename T>
440  int addColumn(std::string const & ttype, int size, std::string const & comment);
441 
448  template <typename T>
449  int addColumn(std::string const & ttype, int size);
450 
452  std::size_t addRows(std::size_t nRows);
453 
455  std::size_t countRows();
456 
458  template <typename T>
459  void writeTableArray(std::size_t row, int col, int nElements, T const * value);
460 
462  template <typename T>
463  void writeTableScalar(std::size_t row, int col, T value) { writeTableArray(row, col, 1, &value); }
464 
466  void writeTableScalar(std::size_t row, int col, std::string const & value);
467 
469  template <typename T>
470  void readTableArray(std::size_t row, int col, int nElements, T * value);
471 
473  template <typename T>
474  void readTableScalar(std::size_t row, int col, T & value) { readTableArray(row, col, 1, &value); }
475 
477  void readTableScalar(std::size_t row, int col, std::string & value);
478 
480  long getTableArraySize(int col);
481 
483  long getTableArraySize(std::size_t row, int col);
484 
486  Fits() : fptr(0), status(0), behavior(0) {}
487 
489  Fits(std::string const & filename, std::string const & mode, int behavior);
490 
492  Fits(MemFileManager & manager, std::string const & mode, int behavior);
493 
495  void closeFile();
496 
497  ~Fits() { if ((fptr) && (behavior & AUTO_CLOSE)) closeFile(); }
498 
499  // No copying
500  Fits (const Fits&) = delete;
501  Fits& operator=(const Fits&) = delete;
502 
503  // No moving
504  Fits (Fits&&) = delete;
505  Fits& operator=(Fits&&) = delete;
506 
507  void * fptr; // the actual cfitsio fitsfile pointer; void to avoid including fitsio.h here.
508  int status; // the cfitsio status indicator that gets passed to every cfitsio call.
509  int behavior; // bitwise OR of BehaviorFlags
510 };
511 
512 #endif // !SWIG
513 
515 PTR(daf::base::PropertyList) readMetadata(std::string const & fileName, int hdu=0, bool strip=false);
523 PTR(daf::base::PropertyList) readMetadata(fits::MemFileManager & manager, int hdu=0, bool strip=false);
524 PTR(daf::base::PropertyList) readMetadata(fits::Fits & fitsfile, bool strip=false);
526 
527 }}}
528 
529 #endif // !LSST_AFW_fits_h_INCLUDED
int y
void createTable()
Create a new binary table extension.
void writeKey(std::string const &key, char const *value)
Add a FITS header key to the bottom of the header.
Definition: fits.h:263
void readTableArray(std::size_t row, int col, int nElements, T *value)
Read an array value from a binary table.
void writeColumnKey(std::string const &prefix, int n, char const *value, std::string const &comment)
Write a key of the form XXXXXnnn, where XXXXX is the prefix and nnn is a column number.
Definition: fits.h:286
int getHdu()
Return the current HDU (1-indexed; 1 is the Primary HDU).
void readImage(ndarray::Array< T, N, N > const &array, ndarray::Vector< int, N > const &offset)
Read an array from a FITS image.
Definition: fits.h:418
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
void forEachKey(HeaderIterationFunctor &functor)
Call a polymorphic functor for every key in the header.
Class for storing ordered metadata with comments.
Definition: PropertyList.h:82
void updateColumnKey(std::string const &prefix, int n, char const *value, std::string const &comment)
Update a key of the form XXXXXnnn, where XXXXX is the prefix and nnn is a column number.
Definition: fits.h:272
Include files required for standard LSST Exception handling.
void readTableScalar(std::size_t row, int col, T &value)
Read an array scalar from a binary table.
Definition: fits.h:474
void updateColumnKey(std::string const &prefix, int n, char const *value)
Update a key of the form XXXXXnnn, where XXXXX is the prefix and nnn is a column number.
Definition: fits.h:277
int getBitPix()
Return the cfitsio integer BITPIX code for the given data type.
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
Definition: fits.h:376
void createImage(long x, long y)
Create a 2-d image with pixel type provided by the given explicit PixelT template parameter...
Definition: fits.h:363
MemFileManager(std::size_t len)
Construct a MemFileManager with (len) bytes of initial memory.
Definition: fits.h:123
void updateColumnKey(std::string const &prefix, int n, T const &value, std::string const &comment)
Update a key of the form XXXXXnnn, where XXXXX is the prefix and nnn is a column number.
boost::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=0, bool strip=false)
Read FITS header.
Definition: Utils.h:62
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:202
void reset(void *ptr, std::size_t len)
Set the internal memory buffer to an manually-managed external block.
Definition: fits.h:159
int countHdus()
Return the number of HDUs in the file.
void writeImageImpl(T const *data, int nElements)
Fits()
Default constructor; set all data members to 0.
Definition: fits.h:486
std::size_t addRows(std::size_t nRows)
Append rows to a table, and return the index of the first new row.
metadata import lsst afw display as afwDisplay n
int getImageDim()
Return the number of dimensions in the current HDU.
void createImageImpl(int nAxis, long *nAxes)
void writeKey(std::string const &key, T const &value, std::string const &comment)
Add a FITS header key to the bottom of the header.
ndarray::Vector< ndarray::Size, N > getImageShape()
Return the shape of the current (image) HDU.
Definition: fits.h:394
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:106
void * getData() const
Return the buffer.
Definition: fits.h:172
void writeTableArray(std::size_t row, int col, int nElements, T const *value)
Write an array value to a binary table.
void readImageImpl(int nAxis, T *data, long *begin, long *end, long *increment)
Fits & operator=(const Fits &)=delete
double x
void writeKey(std::string const &key, char const *value, std::string const &comment)
Add a FITS header key to the bottom of the header.
Definition: fits.h:258
std::size_t countRows()
Return the number of row in a table.
void readKey(std::string const &key, T &value)
Read a FITS header key into the given reference.
void setHdu(int hdu, bool relative=false)
Set the current HDU.
bool checkImageType()
Return true if the current HDU has the given pixel type.
int row
Definition: CR.cc:158
void createImage(ndarray::Vector< ndarray::Size, N > const &shape)
Create an image with pixel type provided by the given explicit PixelT template parameter and shape de...
Definition: fits.h:351
MemFileManager & operator=(const MemFileManager &)=delete
MemFileManager()
Construct a MemFileManager with no initial memory buffer.
Definition: fits.h:115
void writeTableScalar(std::size_t row, int col, T value)
Write an scalar value to a binary table.
Definition: fits.h:463
void writeColumnKey(std::string const &prefix, int n, T const &value, std::string const &comment)
Write a key of the form XXXXXnnn, where XXXXX is the prefix and nnn is a column number.
int status
#define PTR(...)
Definition: base.h:41
Class for storing generic metadata.
Definition: PropertySet.h:82
std::size_t getLength() const
Return the buffer length.
Definition: fits.h:175
void createEmpty()
Create an empty image HDU with NAXIS=0 at the end of the file.
void readMetadata(daf::base::PropertySet &metadata, bool strip=false)
Read a FITS header into a PropertySet or PropertyList.
void writeColumnKey(std::string const &prefix, int n, char const *value)
Write a key of the form XXXXXnnn, where XXXXX is the prefix and nnn is a column number.
Definition: fits.h:291
void getImageShapeImpl(int maxDim, long *nAxes)
void updateKey(std::string const &key, T const &value, std::string const &comment)
Set a FITS header key, editing if it already exists and appending it if not.
void closeFile()
Close a FITS file.
void reset()
Return the manager to the same state it would be if default-constructed.
Basic LSST definitions.
virtual void operator()(std::string const &key, std::string const &value, std::string const &comment)=0
std::string getFileName() const
Return the file name associated with the FITS object or &quot;&lt;unknown&gt;&quot; if there is none.
#define LSST_EXCEPTION_TYPE(t, b, c)
Macro used to define new types of exceptions without additional data.
Definition: Exception.h:68
std::string makeErrorMessage(std::string const &fileName="", int status=0, std::string const &msg="")
Return an error message reflecting FITS I/O errors.
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:47
int col
Definition: CR.cc:157
void updateKey(std::string const &key, char const *value)
Set a FITS header key, editing if it already exists and appending it if not.
Definition: fits.h:243
void updateKey(std::string const &key, char const *value, std::string const &comment)
Set a FITS header key, editing if it already exists and appending it if not.
Definition: fits.h:238
MemFileManager(void *ptr, std::size_t len)
Construct a MemFileManager that references and does not manage external memory.
Definition: fits.h:132
long getTableArraySize(int col)
Return the size of an array column.
int addColumn(std::string const &ttype, int size, std::string const &comment)
Add a column to a table.