LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 #include <boost/scoped_array.hpp>
22 
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 
106 class MemFileManager : private boost::noncopyable {
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 
164  void* getData() const { return _ptr; }
165 
167  std::size_t getLength() const { return _len; }
168 
169 private:
170 
171  friend class Fits;
172 
173  void * _ptr;
174  std::size_t _len;
175  bool _managed;
176 };
177 
194 class Fits : private boost::noncopyable {
195  template <typename T> void createImageImpl(int nAxis, long * nAxes);
196  template <typename T> void writeImageImpl(T const * data, int nElements);
197  template <typename T> void readImageImpl(int nAxis, T * data, long * begin, long * end, long * increment);
198  void getImageShapeImpl(int nAxis, long * nAxes);
199 public:
200 
202  AUTO_CLOSE = 0x01, // Close files when the Fits object goes out of scope if fptr != NULL
203  AUTO_CHECK = 0x02 // Call LSST_FITS_CHECK_STATUS after every cfitsio call
204  };
205 
207  std::string getFileName() const;
208 
210  int getHdu();
211 
221  void setHdu(int hdu, bool relative=false);
222 
224  int countHdus();
225 
227  template <typename T>
229  void updateKey(std::string const & key, T const & value, std::string const & comment);
230  void updateKey(std::string const & key, char const * value, std::string const & comment) {
231  updateKey(key, std::string(value), comment);
232  }
233  template <typename T>
234  void updateKey(std::string const & key, T const & value);
235  void updateKey(std::string const & key, char const * value) {
236  updateKey(key, std::string(value));
237  }
239 
241 
248  template <typename T>
249  void writeKey(std::string const & key, T const & value, std::string const & comment);
250  void writeKey(std::string const & key, char const * value, std::string const & comment) {
251  writeKey(key, std::string(value), comment);
252  }
253  template <typename T>
254  void writeKey(std::string const & key, T const & value);
255  void writeKey(std::string const & key, char const * value) {
256  writeKey(key, std::string(value));
257  }
259 
261  template <typename T>
263  void updateColumnKey(std::string const & prefix, int n, T const & value, std::string const & comment);
264  void updateColumnKey(std::string const & prefix, int n, char const * value, std::string const & comment) {
265  updateColumnKey(prefix, n, std::string(value), comment);
266  }
267  template <typename T>
268  void updateColumnKey(std::string const & prefix, int n, T const & value);
269  void updateColumnKey(std::string const & prefix, int n, char const * value) {
270  updateColumnKey(prefix, n, std::string(value));
271  }
273 
275  template <typename T>
277  void writeColumnKey(std::string const & prefix, int n, T const & value, std::string const & comment);
278  void writeColumnKey(std::string const & prefix, int n, char const * value, std::string const & comment) {
279  writeColumnKey(prefix, n, std::string(value), comment);
280  }
281  template <typename T>
282  void writeColumnKey(std::string const & prefix, int n, T const & value);
283  void writeColumnKey(std::string const & prefix, int n, char const * value) {
284  writeColumnKey(prefix, n, std::string(value));
285  }
287 
297  void writeMetadata(daf::base::PropertySet const & metadata);
298 
308  void readMetadata(daf::base::PropertySet & metadata, bool strip=false);
309 
311  template <typename T>
312  void readKey(std::string const & key, T & value);
313 
322  void forEachKey(HeaderIterationFunctor & functor);
323 
330  void createEmpty();
331 
342  template <typename PixelT, int N>
343  void createImage(ndarray::Vector<int,N> const & shape) {
344  ndarray::Vector<long,N> nAxes(shape.reverse());
345  createImageImpl<PixelT>(N, nAxes.elems);
346  }
347 
354  template <typename PixelT>
355  void createImage(long x, long y) {
356  long naxes[2] = { x, y };
357  createImageImpl<PixelT>(2, naxes);
358  }
359 
367  template <typename T, int N, int C>
370  if (contiguous.empty()) contiguous = ndarray::copy(array);
371  writeImageImpl(contiguous.getData(), contiguous.getNumElements());
372  }
373 
375  int getImageDim();
376 
385  template <int N>
387  ndarray::Vector<long,N> nAxes(1);
388  getImageShapeImpl(N, nAxes.elems);
390  for (int i = 0; i < N; ++i) shape[i] = nAxes[N-i-1];
391  return shape;
392  }
393 
400  template <typename T>
401  bool checkImageType();
402 
409  template <typename T, int N>
410  void readImage(
411  ndarray::Array<T,N,N> const & array,
412  ndarray::Vector<int,N> const & offset
413  ) {
414  ndarray::Vector<long,N> begin(offset.reverse());
415  ndarray::Vector<long,N> end(begin);
416  end += array.getShape().reverse();
417  ndarray::Vector<long,N> increment(1);
418  begin += increment; // first FITS pixel is 1, not 0
419  readImageImpl(N, array.getData(), begin.elems, end.elems, increment.elems);
420  }
421 
423  void createTable();
424 
431  template <typename T>
432  int addColumn(std::string const & ttype, int size, std::string const & comment);
433 
440  template <typename T>
441  int addColumn(std::string const & ttype, int size);
442 
444  std::size_t addRows(std::size_t nRows);
445 
447  std::size_t countRows();
448 
450  template <typename T>
451  void writeTableArray(std::size_t row, int col, int nElements, T const * value);
452 
454  template <typename T>
455  void writeTableScalar(std::size_t row, int col, T value) { writeTableArray(row, col, 1, &value); }
456 
458  void writeTableScalar(std::size_t row, int col, std::string const & value);
459 
461  template <typename T>
462  void readTableArray(std::size_t row, int col, int nElements, T * value);
463 
465  template <typename T>
466  void readTableScalar(std::size_t row, int col, T & value) { readTableArray(row, col, 1, &value); }
467 
469  void readTableScalar(std::size_t row, int col, std::string & value);
470 
472  long getTableArraySize(int col);
473 
475  long getTableArraySize(std::size_t row, int col);
476 
478  Fits() : fptr(0), status(0), behavior(0) {}
479 
481  Fits(std::string const & filename, std::string const & mode, int behavior);
482 
484  Fits(MemFileManager & manager, std::string const & mode, int behavior);
485 
487  void closeFile();
488 
489  ~Fits() { if ((fptr) && (behavior & AUTO_CLOSE)) closeFile(); }
490 
491  void * fptr; // the actual cfitsio fitsfile pointer; void to avoid including fitsio.h here.
492  int status; // the cfitsio status indicator that gets passed to every cfitsio call.
493  int behavior; // bitwise OR of BehaviorFlags
494 };
495 
496 #endif // !SWIG
497 
498 }}}
499 
500 #endif // !LSST_AFW_fits_h_INCLUDED
int y
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:230
std::size_t getLength() const
Return the buffer length.
Definition: fits.h:167
void createEmpty()
Create an empty image HDU with NAXIS=0 at the end of the file.
void writeTableScalar(std::size_t row, int col, T value)
Write an scalar value to a binary table.
Definition: fits.h:455
void reset(void *ptr, std::size_t len)
Set the internal memory buffer to an manually-managed external block.
Definition: fits.h:159
void writeKey(std::string const &key, T const &value, std::string const &comment)
Add a FITS header key to the bottom of the header.
Fits()
Default constructor; set all data members to 0.
Definition: fits.h:478
void setHdu(int hdu, bool relative=false)
Set the current HDU.
void readKey(std::string const &key, T &value)
Read a FITS header key into the given reference.
MemFileManager()
Construct a MemFileManager with no initial memory buffer.
Definition: fits.h:115
void readMetadata(daf::base::PropertySet &metadata, bool strip=false)
Read a FITS header into a PropertySet or PropertyList.
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:235
int getBitPix()
Return the cfitsio integer BITPIX code for the given data type.
Array< T, N, C_ > dynamic_dimension_cast(Array< T, N, C > const &array)
Definition: casts.h:105
void getImageShapeImpl(int nAxis, long *nAxes)
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
Definition: eigen.h:390
void reset()
Return the manager to the same state it would be if default-constructed.
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:283
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:194
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.
Vector reverse() const
Return a Vector with the elements reversed.
Definition: Vector.h:223
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:264
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
Definition: fits.h:368
std::string getFileName() const
Return the file name associated with the FITS object or &quot;&lt;unknown&gt;&quot; if there is none.
std::size_t countRows()
Return the number of row in a table.
void readTableScalar(std::size_t row, int col, T &value)
Read an array scalar from a binary table.
Definition: fits.h:466
Index getShape() const
Return a Vector of the sizes of all dimensions.
Definition: ArrayBase.h:136
int getNumElements() const
Return the total number of elements in the array.
Definition: ArrayBase.h:142
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:106
int getHdu()
Return the current HDU (1-indexed; 1 is the Primary HDU).
MemFileManager(std::size_t len)
Construct a MemFileManager with (len) bytes of initial memory.
Definition: fits.h:123
void writeImageImpl(T const *data, int nElements)
void readTableArray(std::size_t row, int col, int nElements, T *value)
Read an array value from a binary table.
void writeTableArray(std::size_t row, int col, int nElements, T const *value)
Write an array value to a binary table.
int getImageDim()
Return the number of dimensions in the current HDU.
Element * getData() const
Return a raw pointer to the first element of the array.
Definition: ArrayBase.h:117
virtual void operator()(std::string const &key, std::string const &value, std::string const &comment)=0
int addColumn(std::string const &ttype, int size, std::string const &comment)
Add a column to a table.
int x
std::size_t addRows(std::size_t nRows)
Append rows to a table, and return the index of the first new row.
int row
Definition: CR.cc:153
bool empty() const
Return true if the first dimension has no elements.
A multidimensional strided array.
Definition: Array.h:47
bool checkImageType()
Return true if the current HDU has the given pixel type..
void closeFile()
Close a FITS file.
int status
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:269
ndarray::Vector< int, N > getImageShape()
Return the shape of the current (image) HDU.
Definition: fits.h:386
void createImageImpl(int nAxis, long *nAxes)
void * getData() const
Return the buffer.
Definition: fits.h:164
Class for storing generic metadata.
Definition: PropertySet.h:82
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.
void readImageImpl(int nAxis, T *data, long *begin, long *end, long *increment)
int countHdus()
Return the number of HDUs in the file.
long getTableArraySize(int col)
Return the size of an array column.
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:410
void writeKey(std::string const &key, char const *value)
Add a FITS header key to the bottom of the header.
Definition: fits.h:255
void forEachKey(HeaderIterationFunctor &functor)
Call a polymorphic functor for every key in the header.
MemFileManager(void *ptr, std::size_t len)
Construct a MemFileManager that references and does not manage external memory.
Definition: fits.h:132
void createTable()
Create a new binary table extension.
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:355
#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.
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:47
void createImage(ndarray::Vector< int, N > const &shape)
Create an image with pixel type provided by the given explicit PixelT template parameter and shape de...
Definition: fits.h:343
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:250
int col
Definition: CR.cc:152
Include files required for standard LSST Exception handling.
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 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:278