LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
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/pex/exceptions.h"
23 #include "lsst/daf/base.h"
24 #include "ndarray.h"
25 
26 namespace lsst { namespace afw { namespace fits {
27 
31 LSST_EXCEPTION_TYPE(FitsError, lsst::pex::exceptions::IoError, lsst::afw::fits::FitsError)
32 
33 
36 LSST_EXCEPTION_TYPE(FitsTypeError, lsst::afw::fits::FitsError, lsst::afw::fits::FitsTypeError)
37 
38 #ifndef SWIG // only want SWIG to see the exceptions; everything else is too low-level for Python.
39 
47 public:
48 
49  virtual void operator()(
50  std::string const & key,
51  std::string const & value,
52  std::string const & comment
53  ) = 0;
54 
56 
57 };
58 
67 std::string makeErrorMessage(std::string const & fileName="", int status=0, std::string const & msg="");
68 inline std::string makeErrorMessage(std::string const & fileName, int status, boost::format const & msg) {
69  return makeErrorMessage(fileName, status, msg.str());
70 }
71 
81 std::string makeErrorMessage(void * fptr, int status=0, std::string const & msg="");
82 inline std::string makeErrorMessage(void * fptr, int status, boost::format const & msg) {
83  return makeErrorMessage(fptr, status, msg.str());
84 }
85 
90 #define LSST_FITS_EXCEPT(type, fitsObj, ...) \
91  type(LSST_EXCEPT_HERE, lsst::afw::fits::makeErrorMessage((fitsObj).fptr, (fitsObj).status, __VA_ARGS__))
92 
96 #define LSST_FITS_CHECK_STATUS(fitsObj, ...) \
97  if ((fitsObj).status != 0) throw LSST_FITS_EXCEPT(lsst::afw::fits::FitsError, fitsObj, __VA_ARGS__)
98 
100 template <typename T> int getBitPix();
101 
106 public:
107 
114  MemFileManager() : _ptr(0), _len(0), _managed(true) {}
115 
122  explicit MemFileManager(std::size_t len) : _ptr(0), _len(0), _managed(true) { reset(len); }
123 
131  MemFileManager(void * ptr, std::size_t len) : _ptr(ptr), _len(len), _managed(false) {}
132 
138  void reset();
139 
148  void reset(std::size_t len);
149 
158  void reset(void * ptr, std::size_t len) { reset(); _ptr = ptr; _len = len; _managed = false; }
159 
161 
162  // No copying
163  MemFileManager (const MemFileManager&) = delete;
164  MemFileManager& operator=(const MemFileManager&) = delete;
165 
166  // No moving
167  MemFileManager (MemFileManager&&) = delete;
169 
171  void* getData() const { return _ptr; }
172 
174  std::size_t getLength() const { return _len; }
175 
176 private:
177 
178  friend class Fits;
179 
180  void * _ptr;
181  std::size_t _len;
182  bool _managed;
183 };
184 
201 class Fits {
202  template <typename T> void createImageImpl(int nAxis, long * nAxes);
203  template <typename T> void writeImageImpl(T const * data, int nElements);
204  template <typename T> void readImageImpl(int nAxis, T * data, long * begin, long * end, long * increment);
205  void getImageShapeImpl(int maxDim, long * nAxes);
206 public:
207 
209  AUTO_CLOSE = 0x01, // Close files when the Fits object goes out of scope if fptr != NULL
210  AUTO_CHECK = 0x02 // Call LSST_FITS_CHECK_STATUS after every cfitsio call
211  };
212 
214  std::string getFileName() const;
215 
217  int getHdu();
218 
228  void setHdu(int hdu, bool relative=false);
229 
231  int countHdus();
232 
234  template <typename T>
236  void updateKey(std::string const & key, T const & value, std::string const & comment);
237  void updateKey(std::string const & key, char const * value, std::string const & comment) {
238  updateKey(key, std::string(value), comment);
239  }
240  template <typename T>
241  void updateKey(std::string const & key, T const & value);
242  void updateKey(std::string const & key, char const * value) {
243  updateKey(key, std::string(value));
244  }
246 
248 
255  template <typename T>
256  void writeKey(std::string const & key, T const & value, std::string const & comment);
257  void writeKey(std::string const & key, char const * value, std::string const & comment) {
258  writeKey(key, std::string(value), comment);
259  }
260  template <typename T>
261  void writeKey(std::string const & key, T const & value);
262  void writeKey(std::string const & key, char const * value) {
263  writeKey(key, std::string(value));
264  }
266 
268  template <typename T>
270  void updateColumnKey(std::string const & prefix, int n, T const & value, std::string const & comment);
271  void updateColumnKey(std::string const & prefix, int n, char const * value, std::string const & comment) {
272  updateColumnKey(prefix, n, std::string(value), comment);
273  }
274  template <typename T>
275  void updateColumnKey(std::string const & prefix, int n, T const & value);
276  void updateColumnKey(std::string const & prefix, int n, char const * value) {
277  updateColumnKey(prefix, n, std::string(value));
278  }
280 
282  template <typename T>
284  void writeColumnKey(std::string const & prefix, int n, T const & value, std::string const & comment);
285  void writeColumnKey(std::string const & prefix, int n, char const * value, std::string const & comment) {
286  writeColumnKey(prefix, n, std::string(value), comment);
287  }
288  template <typename T>
289  void writeColumnKey(std::string const & prefix, int n, T const & value);
290  void writeColumnKey(std::string const & prefix, int n, char const * value) {
291  writeColumnKey(prefix, n, std::string(value));
292  }
294 
304  void writeMetadata(daf::base::PropertySet const & metadata);
305 
315  void readMetadata(daf::base::PropertySet & metadata, bool strip=false);
316 
318  template <typename T>
319  void readKey(std::string const & key, T & value);
320 
329  void forEachKey(HeaderIterationFunctor & functor);
330 
337  void createEmpty();
338 
349  template <typename PixelT, int N>
350  void createImage(ndarray::Vector<ndarray::Size,N> const & shape) {
351  ndarray::Vector<long,N> nAxes(shape.reverse());
352  createImageImpl<PixelT>(N, nAxes.elems);
353  }
354 
361  template <typename PixelT>
362  void createImage(long x, long y) {
363  long naxes[2] = { x, y };
364  createImageImpl<PixelT>(2, naxes);
365  }
366 
374  template <typename T, int N, int C>
375  void writeImage(ndarray::Array<T const,N,C> const & array) {
376  ndarray::Array<T const,N,N> contiguous = ndarray::dynamic_dimension_cast<2>(array);
377  if (contiguous.empty()) contiguous = ndarray::copy(array);
378  writeImageImpl(contiguous.getData(), contiguous.getNumElements());
379  }
380 
382  int getImageDim();
383 
392  template <int N>
393  ndarray::Vector<ndarray::Size,N> getImageShape() {
394  ndarray::Vector<long,N> nAxes(1);
395  getImageShapeImpl(N, nAxes.elems);
396  ndarray::Vector<ndarray::Size,N> shape;
397  for (int i = 0; i < N; ++i) shape[i] = nAxes[N-i-1];
398  return shape;
399  }
400 
407  template <typename T>
408  bool checkImageType();
409 
416  template <typename T, int N>
417  void readImage(
418  ndarray::Array<T,N,N> const & array,
419  ndarray::Vector<int,N> const & offset
420  ) {
421  ndarray::Vector<long,N> begin(offset.reverse());
422  ndarray::Vector<long,N> end(begin);
423  end += array.getShape().reverse();
424  ndarray::Vector<long,N> increment(1);
425  begin += increment; // first FITS pixel is 1, not 0
426  readImageImpl(N, array.getData(), begin.elems, end.elems, increment.elems);
427  }
428 
430  void createTable();
431 
438  template <typename T>
439  int addColumn(std::string const & ttype, int size, std::string const & comment);
440 
447  template <typename T>
448  int addColumn(std::string const & ttype, int size);
449 
451  std::size_t addRows(std::size_t nRows);
452 
454  std::size_t countRows();
455 
457  template <typename T>
458  void writeTableArray(std::size_t row, int col, int nElements, T const * value);
459 
461  template <typename T>
462  void writeTableScalar(std::size_t row, int col, T value) { writeTableArray(row, col, 1, &value); }
463 
465  void writeTableScalar(std::size_t row, int col, std::string const & value);
466 
468  template <typename T>
469  void readTableArray(std::size_t row, int col, int nElements, T * value);
470 
472  template <typename T>
473  void readTableScalar(std::size_t row, int col, T & value) { readTableArray(row, col, 1, &value); }
474 
476  void readTableScalar(std::size_t row, int col, std::string & value);
477 
479  long getTableArraySize(int col);
480 
482  long getTableArraySize(std::size_t row, int col);
483 
485  Fits() : fptr(0), status(0), behavior(0) {}
486 
488  Fits(std::string const & filename, std::string const & mode, int behavior);
489 
491  Fits(MemFileManager & manager, std::string const & mode, int behavior);
492 
494  void closeFile();
495 
496  ~Fits() { if ((fptr) && (behavior & AUTO_CLOSE)) closeFile(); }
497 
498  // No copying
499  Fits (const Fits&) = delete;
500  Fits& operator=(const Fits&) = delete;
501 
502  // No moving
503  Fits (Fits&&) = delete;
504  Fits& operator=(Fits&&) = delete;
505 
506  void * fptr; // the actual cfitsio fitsfile pointer; void to avoid including fitsio.h here.
507  int status; // the cfitsio status indicator that gets passed to every cfitsio call.
508  int behavior; // bitwise OR of BehaviorFlags
509 };
510 
511 #endif // !SWIG
512 
513 }}}
514 
515 #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:262
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:285
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:417
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.
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:271
void readTableScalar(std::size_t row, int col, T &value)
Read an array scalar from a binary table.
Definition: fits.h:473
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:276
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:375
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:362
MemFileManager(std::size_t len)
Construct a MemFileManager with (len) bytes of initial memory.
Definition: fits.h:122
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.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:201
void reset(void *ptr, std::size_t len)
Set the internal memory buffer to an manually-managed external block.
Definition: fits.h:158
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:485
std::size_t addRows(std::size_t nRows)
Append rows to a table, and return the index of the first new row.
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:393
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:105
void * getData() const
Return the buffer.
Definition: fits.h:171
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:257
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:159
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:350
MemFileManager & operator=(const MemFileManager &)=delete
MemFileManager()
Construct a MemFileManager with no initial memory buffer.
Definition: fits.h:114
void writeTableScalar(std::size_t row, int col, T value)
Write an scalar value to a binary table.
Definition: fits.h:462
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
Class for storing generic metadata.
Definition: PropertySet.h:82
std::size_t getLength() const
Return the buffer length.
Definition: fits.h:174
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:290
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.
Include files required for standard LSST Exception handling.
void reset()
Return the manager to the same state it would be if default-constructed.
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)
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:46
int col
Definition: CR.cc:158
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:242
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:237
MemFileManager(void *ptr, std::size_t len)
Construct a MemFileManager that references and does not manage external memory.
Definition: fits.h:131
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.