lsst.afw  16.0-9-gdf7162dbc+1
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 
5 /*
6  * Utilities for working with FITS files. These are mostly thin wrappers around
7  * cfitsio calls, and their main purpose is to transform functions signatures from
8  * void pointers and cfitsio's preprocessor type enums to a more type-safe and
9  * convenient interface using overloads and templates.
10  *
11  * This was written as part of implementing the afw/table library. Someday
12  * the afw/image FITS I/O should be modified to use some of these with the goal
13  * of eliminating a lot of code between the two.
14  */
15 
16 #include <climits>
17 #include <string>
18 
19 #include <boost/format.hpp>
20 
21 #include "lsst/base.h"
22 #include "lsst/pex/exceptions.h"
23 #include "lsst/daf/base.h"
24 #include "ndarray.h"
26 #include "lsst/afw/fitsDefaults.h"
27 
28 namespace lsst {
29 namespace afw {
30 namespace fits {
31 
36 
37 
40 LSST_EXCEPTION_TYPE(FitsTypeError, lsst::afw::fits::FitsError, lsst::afw::fits::FitsTypeError)
41 
49 public:
50  virtual void operator()(std::string const& key, std::string const& value, std::string const& comment) = 0;
51 
53 };
54 
63 std::string makeErrorMessage(std::string const& fileName = "", int status = 0, std::string const& msg = "");
64 inline std::string makeErrorMessage(std::string const& fileName, int status, boost::format const& msg) {
65  return makeErrorMessage(fileName, status, msg.str());
66 }
67 
77 std::string makeErrorMessage(void* fptr, int status = 0, std::string const& msg = "");
78 inline std::string makeErrorMessage(void* fptr, int status, boost::format const& msg) {
79  return makeErrorMessage(fptr, status, msg.str());
80 }
81 
98  std::set<std::string> const& excludeNames = {});
99 
104 #define LSST_FITS_EXCEPT(type, fitsObj, ...) \
105  type(LSST_EXCEPT_HERE, lsst::afw::fits::makeErrorMessage((fitsObj).fptr, (fitsObj).status, __VA_ARGS__))
106 
110 #define LSST_FITS_CHECK_STATUS(fitsObj, ...) \
111  if ((fitsObj).status != 0) throw LSST_FITS_EXCEPT(lsst::afw::fits::FitsError, fitsObj, __VA_ARGS__)
112 
114 template <typename T>
115 int getBitPix();
116 
121 public:
128  MemFileManager() : _ptr(0), _len(0), _managed(true) {}
129 
136  explicit MemFileManager(std::size_t len) : _ptr(0), _len(0), _managed(true) { reset(len); }
137 
145  MemFileManager(void* ptr, std::size_t len) : _ptr(ptr), _len(len), _managed(false) {}
146 
152  void reset();
153 
162  void reset(std::size_t len);
163 
172  void reset(void* ptr, std::size_t len) {
173  reset();
174  _ptr = ptr;
175  _len = len;
176  _managed = false;
177  }
178 
180 
181  // No copying
182  MemFileManager(const MemFileManager&) = delete;
183  MemFileManager& operator=(const MemFileManager&) = delete;
184 
185  // No moving
186  MemFileManager(MemFileManager&&) = delete;
188 
190  void* getData() const { return _ptr; }
191 
193  std::size_t getLength() const { return _len; }
194 
195 private:
196  friend class Fits;
197 
198  void* _ptr;
199  std::size_t _len;
200  bool _managed;
201 };
202 
206 template <typename T, int N, int C>
207 ndarray::Array<T const, N, N> const makeContiguousArray(ndarray::Array<T, N, C> const& array) {
208  ndarray::Array<T const, N, N> contiguous = ndarray::dynamic_dimension_cast<N>(array);
209  if (contiguous.empty()) contiguous = ndarray::copy(array);
210  return contiguous;
211 }
212 
221 
223  template <typename T>
224  explicit ImageWriteOptions(image::Image<T> const& image) : compression(image) {}
225 
227  template <typename T>
228  explicit ImageWriteOptions(image::Mask<T> const& mask) : compression(mask) {}
229 
231  explicit ImageWriteOptions(ImageCompressionOptions const& compression_ =
233  ImageScalingOptions const& scaling_ = ImageScalingOptions())
234  : compression(compression_), scaling(scaling_) {}
235 
237  explicit ImageWriteOptions(ImageScalingOptions const& scaling_)
238  : compression(ImageCompressionOptions::NONE), scaling(scaling_) {}
239 
265 
278 };
279 
296 class Fits {
297  void createImageImpl(int bitpix, int nAxis, long const* nAxes);
298  template <typename T>
299  void writeImageImpl(T const* data, int nElements);
300  template <typename T>
301  void readImageImpl(int nAxis, T* data, long* begin, long* end, long* increment);
302  void getImageShapeImpl(int maxDim, long* nAxes);
303 
304 public:
306  AUTO_CLOSE = 0x01, // Close files when the Fits object goes out of scope if fptr != NULL
307  AUTO_CHECK = 0x02 // Call LSST_FITS_CHECK_STATUS after every cfitsio call
308  };
309 
311  std::string getFileName() const;
312 
314  int getHdu();
315 
325  void setHdu(int hdu, bool relative = false);
326 
328  int countHdus();
329 
331  template <typename T>
333  void updateKey(std::string const& key, T const& value, std::string const& comment);
334  void updateKey(std::string const& key, char const* value, std::string const& comment) {
335  updateKey(key, std::string(value), comment);
336  }
337  template <typename T>
338  void updateKey(std::string const& key, T const& value);
339  void updateKey(std::string const& key, char const* value) { updateKey(key, std::string(value)); }
341 
343 
350  template <typename T>
351  void writeKey(std::string const& key, T const& value, std::string const& comment);
352  void writeKey(std::string const& key, char const* value, std::string const& comment) {
353  writeKey(key, std::string(value), comment);
354  }
355  template <typename T>
356  void writeKey(std::string const& key, T const& value);
357  void writeKey(std::string const& key, char const* value) { writeKey(key, std::string(value)); }
359 
361  template <typename T>
363  void updateColumnKey(std::string const& prefix, int n, T const& value, std::string const& comment);
364  void updateColumnKey(std::string const& prefix, int n, char const* value, std::string const& comment) {
365  updateColumnKey(prefix, n, std::string(value), comment);
366  }
367  template <typename T>
368  void updateColumnKey(std::string const& prefix, int n, T const& value);
369  void updateColumnKey(std::string const& prefix, int n, char const* value) {
370  updateColumnKey(prefix, n, std::string(value));
371  }
373 
375  template <typename T>
377  void writeColumnKey(std::string const& prefix, int n, T const& value, std::string const& comment);
378  void writeColumnKey(std::string const& prefix, int n, char const* value, std::string const& comment) {
379  writeColumnKey(prefix, n, std::string(value), comment);
380  }
381  template <typename T>
382  void writeColumnKey(std::string const& prefix, int n, T const& value);
383  void writeColumnKey(std::string const& prefix, int n, char const* value) {
384  writeColumnKey(prefix, n, std::string(value));
385  }
387 
397  void writeMetadata(daf::base::PropertySet const& metadata);
398 
408  void readMetadata(daf::base::PropertySet& metadata, bool strip = false);
409 
411  template <typename T>
412  void readKey(std::string const& key, T& value);
413 
422  void forEachKey(HeaderIterationFunctor& functor);
423 
430  void createEmpty();
431 
442  template <typename PixelT, int N>
443  void createImage(ndarray::Vector<ndarray::Size, N> const& shape) {
444  ndarray::Vector<long, N> nAxes(shape.reverse());
445  createImageImpl(detail::Bitpix<PixelT>::value, N, nAxes.elems);
446  }
447 
448  template <int N>
449  void createImage(int bitpix, ndarray::Vector<ndarray::Size, N> const& shape) {
450  ndarray::Vector<long, N> nAxes(shape.reverse());
451  createImageImpl(bitpix, N, nAxes.elems);
452  }
453 
460  template <typename PixelT>
461  void createImage(long x, long y) {
462  long naxes[2] = {x, y};
463  createImageImpl(detail::Bitpix<PixelT>::value, 2, naxes);
464  }
465 
475  template <typename T, int N, int C>
476  void writeImage(ndarray::Array<T const, N, C> const& array) {
477  writeImageImpl(makeContiguousArray(array).getData(), array.getNumElements());
478  }
479 
492  template <typename T>
493  void writeImage(
498 
500  int getImageDim();
501 
510  template <int N>
511  ndarray::Vector<ndarray::Size, N> getImageShape() {
512  ndarray::Vector<long, N> nAxes(1);
513  getImageShapeImpl(N, nAxes.elems);
514  ndarray::Vector<ndarray::Size, N> shape;
515  for (int i = 0; i < N; ++i) shape[i] = nAxes[N - i - 1];
516  return shape;
517  }
518 
525  template <typename T>
526  bool checkImageType();
527 
534  template <typename T, int N>
535  void readImage(ndarray::Array<T, N, N> const& array, ndarray::Vector<int, N> const& offset) {
536  ndarray::Vector<long, N> begin(offset.reverse());
537  ndarray::Vector<long, N> end(begin);
538  end += array.getShape().reverse();
539  ndarray::Vector<long, N> increment(1);
540  begin += increment; // first FITS pixel is 1, not 0
541  readImageImpl(N, array.getData(), begin.elems, end.elems, increment.elems);
542  }
543 
545  void createTable();
546 
553  template <typename T>
554  int addColumn(std::string const& ttype, int size, std::string const& comment);
555 
562  template <typename T>
563  int addColumn(std::string const& ttype, int size);
564 
566  std::size_t addRows(std::size_t nRows);
567 
569  std::size_t countRows();
570 
572  template <typename T>
573  void writeTableArray(std::size_t row, int col, int nElements, T const* value);
574 
576  template <typename T>
577  void writeTableScalar(std::size_t row, int col, T value) {
578  writeTableArray(row, col, 1, &value);
579  }
581  void writeTableScalar(std::size_t row, int col, std::string const& value);
582 
584  template <typename T>
585  void readTableArray(std::size_t row, int col, int nElements, T* value);
586 
588  template <typename T>
589  void readTableScalar(std::size_t row, int col, T& value) {
590  readTableArray(row, col, 1, &value);
591  }
592 
594  void readTableScalar(std::size_t row, int col, std::string& value, bool isVariableLength);
595 
597  long getTableArraySize(int col);
598 
600  long getTableArraySize(std::size_t row, int col);
601 
603  Fits() : fptr(0), status(0), behavior(0) {}
604 
606  Fits(std::string const& filename, std::string const& mode, int behavior);
607 
609  Fits(MemFileManager& manager, std::string const& mode, int behavior);
610 
612  void closeFile();
613 
617  void setImageCompression(ImageCompressionOptions const& options);
618 
620  ImageCompressionOptions getImageCompression();
621 
629  bool checkCompressedImagePhu();
630 
631  ~Fits() {
632  if ((fptr) && (behavior & AUTO_CLOSE)) closeFile();
633  }
634 
635  // No copying
636  Fits(const Fits&) = delete;
637  Fits& operator=(const Fits&) = delete;
638 
639  // No moving
640  Fits(Fits&&) = delete;
641  Fits& operator=(Fits&&) = delete;
642 
643  void* fptr; // the actual cfitsio fitsfile pointer; void to avoid including fitsio.h here.
644  int status; // the cfitsio status indicator that gets passed to every cfitsio call.
645  int behavior; // bitwise OR of BehaviorFlags
646 };
647 
668 
680  bool strip = false);
692  bool strip = false);
703 
704 void setAllowImageCompression(bool allow);
706 
707 } // namespace fits
708 } // namespace afw
709 } // namespace lsst
710 
711 #endif // !LSST_AFW_fits_h_INCLUDED
void createImage(int bitpix, ndarray::Vector< ndarray::Size, N > const &shape)
Definition: fits.h:449
void writeKey(std::string const &key, char const *value)
Add a FITS header key to the bottom of the header.
Definition: fits.h:357
MemFileManager(std::size_t len)
Construct a MemFileManager with (len) bytes of initial memory.
Definition: fits.h:136
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:339
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:383
std::shared_ptr< daf::base::PropertyList > combineMetadata(std::shared_ptr< const daf::base::PropertyList > first, std::shared_ptr< const daf::base::PropertyList > second)
Combine two sets of metadata in a FITS-appropriate fashion.
Definition: fits.cc:1470
char * data
Definition: BaseTable.cc:205
ImageCompressionOptions compression
Options controlling compression.
Definition: fits.h:219
afw::table::Key< afw::table::Array< MaskPixelT > > mask
Options for writing an image to FITS.
Definition: fits.h:218
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:352
int getBitPix()
Return the cfitsio integer BITPIX code for the given data type.
Definition: fits.cc:455
void readTableScalar(std::size_t row, int col, T &value)
Read an array scalar from a binary table.
Definition: fits.h:589
MemFileManager(void *ptr, std::size_t len)
Construct a MemFileManager that references and does not manage external memory.
Definition: fits.h:145
ImageWriteOptions(ImageCompressionOptions const &compression_=ImageCompressionOptions(ImageCompressionOptions::NONE), ImageScalingOptions const &scaling_=ImageScalingOptions())
Construct with specific compression and scaling options.
Definition: fits.h:231
ImageWriteOptions(image::Mask< T > const &mask)
Construct with default options for masks.
Definition: fits.h:228
bool strip
Definition: fits.cc:817
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:378
tuple options
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:296
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:104
FITS BITPIX header value by C++ type.
Fits * fits
Definition: FitsWriter.cc:90
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:334
STL class.
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:535
MemFileManager()
Construct a MemFileManager with no initial memory buffer.
Definition: fits.h:128
ndarray::Array< T const, N, N > const makeContiguousArray(ndarray::Array< T, N, C > const &array)
Construct a contiguous ndarray.
Definition: fits.h:207
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:35
ndarray::Vector< ndarray::Size, N > getImageShape()
Return the shape of the current (image) HDU.
Definition: fits.h:511
Options for tile compression of image pixels.
A base class for image defects.
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:461
int end
Definition: BoundedField.cc:99
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:84
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:120
validate
void * getData() const
Return the buffer.
Definition: fits.h:190
Fits()
Default constructor; set all data members to 0.
Definition: fits.h:603
Key< U > key
Definition: Schema.cc:280
void reset(void *ptr, std::size_t len)
Set the internal memory buffer to an manually-managed external block.
Definition: fits.h:172
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:369
Options for scaling image pixels.
double x
ImageWriteOptions(ImageScalingOptions const &scaling_)
Construct with specific scaling options.
Definition: fits.h:237
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:364
STL class.
ImageScalingOptions scaling
Options controlling scaling.
Definition: fits.h:220
std::size_t getLength() const
Return the buffer length.
Definition: fits.h:193
void setAllowImageCompression(bool allow)
Definition: fits.cc:1388
An exception thrown when a FITS file has the wrong type.
Definition: fits.h:40
void reset()
Return the manager to the same state it would be if default-constructed.
Definition: fits.cc:440
std::string prefix
Definition: SchemaMapper.cc:79
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:443
MemFileManager & operator=(const MemFileManager &)=delete
bool getAllowImageCompression()
Definition: fits.cc:1390
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
Definition: fits.h:476
#define LSST_EXCEPTION_TYPE(t, b, c)
std::string makeErrorMessage(std::string const &fileName="", int status=0, std::string const &msg="")
Return an error message reflecting FITS I/O errors.
Definition: fits.cc:391
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:67
int y
Definition: SpanSet.cc:44
Base class for polymorphic functors used to iterator over FITS key headers.
Definition: fits.h:48
void writeTableScalar(std::size_t row, int col, T value)
Write a scalar value to a binary table.
Definition: fits.h:577
std::string makeLimitedFitsHeader(lsst::daf::base::PropertySet const &metadata, std::set< std::string > const &excludeNames={})
Format a PropertySet into an FITS header string in a simplistic fashion.
Definition: fits.cc:422
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=DEFAULT_HDU, bool strip=false)
Read FITS header.
Definition: fits.cc:1499
ImageWriteOptions(image::Image< T > const &image)
Construct with default options for images.
Definition: fits.h:224