LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
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 
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 #include <set>
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"
27 #include "lsst/afw/fitsDefaults.h"
28 
29 namespace lsst {
30 namespace afw {
31 namespace fits {
32 
37 
38 
42 
50 public:
51  virtual void operator()(std::string const& key, std::string const& value, std::string const& comment) = 0;
52 
54 };
55 
64 std::string makeErrorMessage(std::string const& fileName = "", int status = 0, std::string const& msg = "");
65 inline std::string makeErrorMessage(std::string const& fileName, int status, boost::format const& msg) {
66  return makeErrorMessage(fileName, status, msg.str());
67 }
68 
78 std::string makeErrorMessage(void* fptr, int status = 0, std::string const& msg = "");
79 inline std::string makeErrorMessage(void* fptr, int status, boost::format const& msg) {
80  return makeErrorMessage(fptr, status, msg.str());
81 }
82 
99  std::set<std::string> const& excludeNames = {});
100 
105 #define LSST_FITS_EXCEPT(type, fitsObj, ...) \
106  type(LSST_EXCEPT_HERE, lsst::afw::fits::makeErrorMessage((fitsObj).fptr, (fitsObj).status, __VA_ARGS__))
107 
111 #define LSST_FITS_CHECK_STATUS(fitsObj, ...) \
112  if ((fitsObj).status != 0) throw LSST_FITS_EXCEPT(lsst::afw::fits::FitsError, fitsObj, __VA_ARGS__)
113 
115 template <typename T>
116 int getBitPix();
117 
122 public:
129  MemFileManager() : _ptr(0), _len(0), _managed(true) {}
130 
137  explicit MemFileManager(std::size_t len) : _ptr(0), _len(0), _managed(true) { reset(len); }
138 
146  MemFileManager(void* ptr, std::size_t len) : _ptr(ptr), _len(len), _managed(false) {}
147 
153  void reset();
154 
163  void reset(std::size_t len);
164 
173  void reset(void* ptr, std::size_t len) {
174  reset();
175  _ptr = ptr;
176  _len = len;
177  _managed = false;
178  }
179 
181 
182  // No copying
183  MemFileManager(const MemFileManager&) = delete;
184  MemFileManager& operator=(const MemFileManager&) = delete;
185 
186  // No moving
187  MemFileManager(MemFileManager&&) = delete;
189 
191  void* getData() const { return _ptr; }
192 
194  std::size_t getLength() const { return _len; }
195 
196 private:
197  friend class Fits;
198 
199  void* _ptr;
200  std::size_t _len;
201  bool _managed;
202 };
203 
207 template <typename T, int N, int C>
208 ndarray::Array<T const, N, N> const makeContiguousArray(ndarray::Array<T, N, C> const& array) {
209  ndarray::Array<T const, N, N> contiguous = ndarray::dynamic_dimension_cast<N>(array);
210  if (contiguous.empty()) contiguous = ndarray::copy(array);
211  return contiguous;
212 }
213 
222 
224  template <typename T>
225  explicit ImageWriteOptions(image::Image<T> const& image) : compression(image) {}
226 
228  template <typename T>
229  explicit ImageWriteOptions(image::Mask<T> const& mask) : compression(mask) {}
230 
232  explicit ImageWriteOptions(ImageCompressionOptions const& compression_ =
234  ImageScalingOptions const& scaling_ = ImageScalingOptions())
235  : compression(compression_), scaling(scaling_) {}
236 
238  explicit ImageWriteOptions(ImageScalingOptions const& scaling_)
239  : compression(ImageCompressionOptions::NONE), scaling(scaling_) {}
240 
266 
279 };
280 
297 class Fits {
298  void createImageImpl(int bitpix, int nAxis, long const* nAxes);
299  template <typename T>
300  void writeImageImpl(T const* data, int nElements);
301  template <typename T>
302  void readImageImpl(int nAxis, T* data, long* begin, long* end, long* increment);
303  void getImageShapeImpl(int maxDim, long* nAxes);
304 
305 public:
307  AUTO_CLOSE = 0x01, // Close files when the Fits object goes out of scope if fptr != NULL
308  AUTO_CHECK = 0x02 // Call LSST_FITS_CHECK_STATUS after every cfitsio call
309  };
310 
312  std::string getFileName() const;
313 
315  int getHdu();
316 
326  void setHdu(int hdu, bool relative = false);
327 
329  int countHdus();
330 
332  template <typename T>
334  void updateKey(std::string const& key, T const& value, std::string const& comment);
335  void updateKey(std::string const& key, char const* value, std::string const& comment) {
336  updateKey(key, std::string(value), comment);
337  }
338  template <typename T>
339  void updateKey(std::string const& key, T const& value);
340  void updateKey(std::string const& key, char const* value) { updateKey(key, std::string(value)); }
342 
344 
351  template <typename T>
352  void writeKey(std::string const& key, T const& value, std::string const& comment);
353  void writeKey(std::string const& key, char const* value, std::string const& comment) {
354  writeKey(key, std::string(value), comment);
355  }
356  template <typename T>
357  void writeKey(std::string const& key, T const& value);
358  void writeKey(std::string const& key, char const* value) { writeKey(key, std::string(value)); }
360 
362  template <typename T>
364  void updateColumnKey(std::string const& prefix, int n, T const& value, std::string const& comment);
365  void updateColumnKey(std::string const& prefix, int n, char const* value, std::string const& comment) {
366  updateColumnKey(prefix, n, std::string(value), comment);
367  }
368  template <typename T>
369  void updateColumnKey(std::string const& prefix, int n, T const& value);
370  void updateColumnKey(std::string const& prefix, int n, char const* value) {
371  updateColumnKey(prefix, n, std::string(value));
372  }
374 
376  template <typename T>
378  void writeColumnKey(std::string const& prefix, int n, T const& value, std::string const& comment);
379  void writeColumnKey(std::string const& prefix, int n, char const* value, std::string const& comment) {
380  writeColumnKey(prefix, n, std::string(value), comment);
381  }
382  template <typename T>
383  void writeColumnKey(std::string const& prefix, int n, T const& value);
384  void writeColumnKey(std::string const& prefix, int n, char const* value) {
385  writeColumnKey(prefix, n, std::string(value));
386  }
388 
398  void writeMetadata(daf::base::PropertySet const& metadata);
399 
409  void readMetadata(daf::base::PropertySet& metadata, bool strip = false);
410 
412  template <typename T>
413  void readKey(std::string const& key, T& value);
414 
423  void forEachKey(HeaderIterationFunctor& functor);
424 
431  void createEmpty();
432 
443  template <typename PixelT, int N>
444  void createImage(ndarray::Vector<ndarray::Size, N> const& shape) {
445  ndarray::Vector<long, N> nAxes(shape.reverse());
446  createImageImpl(detail::Bitpix<PixelT>::value, N, nAxes.elems);
447  }
448 
449  template <int N>
450  void createImage(int bitpix, ndarray::Vector<ndarray::Size, N> const& shape) {
451  ndarray::Vector<long, N> nAxes(shape.reverse());
452  createImageImpl(bitpix, N, nAxes.elems);
453  }
454 
461  template <typename PixelT>
462  void createImage(long x, long y) {
463  long naxes[2] = {x, y};
464  createImageImpl(detail::Bitpix<PixelT>::value, 2, naxes);
465  }
466 
476  template <typename T, int N, int C>
477  void writeImage(ndarray::Array<T const, N, C> const& array) {
478  writeImageImpl(makeContiguousArray(array).getData(), array.getNumElements());
479  }
480 
493  template <typename T>
494  void writeImage(
499 
501  int getImageDim();
502 
511  template <int N>
512  ndarray::Vector<ndarray::Size, N> getImageShape() {
513  ndarray::Vector<long, N> nAxes(1);
514  getImageShapeImpl(N, nAxes.elems);
515  ndarray::Vector<ndarray::Size, N> shape;
516  for (int i = 0; i < N; ++i) shape[i] = nAxes[N - i - 1];
517  return shape;
518  }
519 
526  template <typename T>
527  bool checkImageType();
528 
532  std::string getImageDType();
533 
540  template <typename T, int N>
541  void readImage(ndarray::Array<T, N, N> const& array, ndarray::Vector<int, N> const& offset) {
542  ndarray::Vector<long, N> begin(offset.reverse());
543  ndarray::Vector<long, N> end(begin);
544  end += array.getShape().reverse();
545  ndarray::Vector<long, N> increment(1);
546  begin += increment; // first FITS pixel is 1, not 0
547  readImageImpl(N, array.getData(), begin.elems, end.elems, increment.elems);
548  }
549 
551  void createTable();
552 
559  template <typename T>
560  int addColumn(std::string const& ttype, int size, std::string const& comment);
561 
568  template <typename T>
569  int addColumn(std::string const& ttype, int size);
570 
572  std::size_t addRows(std::size_t nRows);
573 
575  std::size_t countRows();
576 
578  template <typename T>
579  void writeTableArray(std::size_t row, int col, int nElements, T const* value);
580 
582  template <typename T>
583  void writeTableScalar(std::size_t row, int col, T value) {
584  writeTableArray(row, col, 1, &value);
585  }
587  void writeTableScalar(std::size_t row, int col, std::string const& value);
588 
590  template <typename T>
591  void readTableArray(std::size_t row, int col, int nElements, T* value);
592 
594  template <typename T>
595  void readTableScalar(std::size_t row, int col, T& value) {
596  readTableArray(row, col, 1, &value);
597  }
598 
600  void readTableScalar(std::size_t row, int col, std::string& value, bool isVariableLength);
601 
603  long getTableArraySize(int col);
604 
606  long getTableArraySize(std::size_t row, int col);
607 
609  Fits() : fptr(0), status(0), behavior(0) {}
610 
612  Fits(std::string const& filename, std::string const& mode, int behavior);
613 
615  Fits(MemFileManager& manager, std::string const& mode, int behavior);
616 
618  void closeFile();
619 
623  void setImageCompression(ImageCompressionOptions const& options);
624 
626  ImageCompressionOptions getImageCompression();
627 
635  bool checkCompressedImagePhu();
636 
637  ~Fits() {
638  if ((fptr) && (behavior & AUTO_CLOSE)) closeFile();
639  }
640 
641  // No copying
642  Fits(const Fits&) = delete;
643  Fits& operator=(const Fits&) = delete;
644 
645  // No moving
646  Fits(Fits&&) = delete;
647  Fits& operator=(Fits&&) = delete;
648 
649  void* fptr; // the actual cfitsio fitsfile pointer; void to avoid including fitsio.h here.
650  int status; // the cfitsio status indicator that gets passed to every cfitsio call.
651  int behavior; // bitwise OR of BehaviorFlags
652 };
653 
674 
686  bool strip = false);
698  bool strip = false);
709 
710 void setAllowImageCompression(bool allow);
712 
713 
714 
725 public:
726 
727  HduMoveGuard() = delete;
728 
729  HduMoveGuard(HduMoveGuard const &) = delete;
730  HduMoveGuard(HduMoveGuard &&) = delete;
731 
732  HduMoveGuard & operator=(HduMoveGuard const &) = delete;
733  HduMoveGuard & operator=(HduMoveGuard &&) = delete;
734 
746  HduMoveGuard(Fits & fits, int hdu, bool relative=false);
747 
748  ~HduMoveGuard();
749 
751  void disable() { _enabled = false; }
752 
753 private:
754  Fits & _fits;
755  int _oldHdu;
756  bool _enabled;
757 };
758 
759 
760 } // namespace fits
761 } // namespace afw
762 } // namespace lsst
763 
764 #endif // !LSST_AFW_fits_h_INCLUDED
int col
Definition: CR.cc:144
void createImage(int bitpix, ndarray::Vector< ndarray::Size, N > const &shape)
Definition: fits.h:450
void writeKey(std::string const &key, char const *value)
Add a FITS header key to the bottom of the header.
Definition: fits.h:358
MemFileManager(std::size_t len)
Construct a MemFileManager with (len) bytes of initial memory.
Definition: fits.h:137
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:340
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:384
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:1587
uint64_t * ptr
Definition: RangeSet.cc:88
ImageCompressionOptions compression
Options controlling compression.
Definition: fits.h:220
Basic LSST definitions.
Options for writing an image to FITS.
Definition: fits.h:219
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:353
int getBitPix()
Return the cfitsio integer BITPIX code for the given data type.
Definition: fits.cc:471
void readTableScalar(std::size_t row, int col, T &value)
Read an array scalar from a binary table.
Definition: fits.h:595
MemFileManager(void *ptr, std::size_t len)
Construct a MemFileManager that references and does not manage external memory.
Definition: fits.h:146
ImageWriteOptions(ImageCompressionOptions const &compression_=ImageCompressionOptions(ImageCompressionOptions::NONE), ImageScalingOptions const &scaling_=ImageScalingOptions())
Construct with specific compression and scaling options.
Definition: fits.h:232
ImageWriteOptions(image::Mask< T > const &mask)
Construct with default options for masks.
Definition: fits.h:229
int y
Definition: SpanSet.cc:49
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:379
tuple options
Definition: lsstimport.py:47
std::string prefix
Definition: SchemaMapper.cc:79
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:103
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:335
Reports errors in external input/output operations.
Definition: Runtime.h:160
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:541
MemFileManager()
Construct a MemFileManager with no initial memory buffer.
Definition: fits.h:129
ndarray::Array< T const, N, N > const makeContiguousArray(ndarray::Array< T, N, C > const &array)
Construct a contiguous ndarray.
Definition: fits.h:208
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:36
ndarray::Vector< ndarray::Size, N > getImageShape()
Return the shape of the current (image) HDU.
Definition: fits.h:512
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:462
char * data
Definition: BaseRecord.cc:62
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:78
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
void * getData() const
Return the buffer.
Definition: fits.h:191
Fits()
Default constructor; set all data members to 0.
Definition: fits.h:609
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
void reset(void *ptr, std::size_t len)
Set the internal memory buffer to an manually-managed external block.
Definition: fits.h:173
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:370
Options for scaling image pixels.
ImageWriteOptions(ImageScalingOptions const &scaling_)
Construct with specific scaling options.
Definition: fits.h:238
double x
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:365
STL class.
afw::table::Key< afw::table::Array< MaskPixelT > > mask
ImageScalingOptions scaling
Options controlling scaling.
Definition: fits.h:221
void disable()
Disable the guard, leaving the HDU at its current state at destruction.
Definition: fits.h:751
Key< U > key
Definition: Schema.cc:281
std::size_t getLength() const
Return the buffer length.
Definition: fits.h:194
void setAllowImageCompression(bool allow)
Definition: fits.cc:1505
Class for storing generic metadata.
Definition: PropertySet.h:68
An exception thrown when a FITS file has the wrong type.
Definition: fits.h:41
void reset()
Return the manager to the same state it would be if default-constructed.
Definition: fits.cc:456
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:444
MemFileManager & operator=(const MemFileManager &)=delete
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
bool getAllowImageCompression()
Definition: fits.cc:1507
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
Definition: fits.h:477
#define LSST_EXCEPTION_TYPE(t, b, c)
Macro used to define new types of exceptions without additional data.
Definition: Exception.h:69
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:407
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:59
RAII scoped guard for moving the HDU in a Fits object.
Definition: fits.h:724
Base class for polymorphic functors used to iterator over FITS key headers.
Definition: fits.h:49
int end
bool strip
Definition: fits.cc:883
void writeTableScalar(std::size_t row, int col, T value)
Write a scalar value to a binary table.
Definition: fits.h:583
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:438
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
int row
Definition: CR.cc:145
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=DEFAULT_HDU, bool strip=false)
Read FITS header.
Definition: fits.cc:1616
ImageWriteOptions(image::Image< T > const &image)
Construct with default options for images.
Definition: fits.h:225