2 #ifndef LSST_AFW_fits_h_INCLUDED
3 #define LSST_AFW_fits_h_INCLUDED
19 #include <boost/format.hpp>
21 #include "lsst/base.h"
22 #include "lsst/pex/exceptions.h"
23 #include "lsst/daf/base.h"
49 virtual void operator()(std::string
const& key, std::string
const& value, std::string
const& comment) = 0;
62 std::string
makeErrorMessage(std::string
const& fileName =
"",
int status = 0, std::string
const& msg =
"");
63 inline std::string
makeErrorMessage(std::string
const& fileName,
int status, boost::format
const& msg) {
76 std::string
makeErrorMessage(
void* fptr,
int status = 0, std::string
const& msg =
"");
77 inline std::string
makeErrorMessage(
void* fptr,
int status, boost::format
const& msg) {
85 #define LSST_FITS_EXCEPT(type, fitsObj, ...) \
86 type(LSST_EXCEPT_HERE, lsst::afw::fits::makeErrorMessage((fitsObj).fptr, (fitsObj).status, __VA_ARGS__))
91 #define LSST_FITS_CHECK_STATUS(fitsObj, ...) \
92 if ((fitsObj).status != 0) throw LSST_FITS_EXCEPT(lsst::afw::fits::FitsError, fitsObj, __VA_ARGS__)
126 MemFileManager(
void* ptr, std::size_t len) : _ptr(ptr), _len(len), _managed(false) {}
143 void reset(std::size_t len);
153 void reset(
void* ptr, std::size_t len) {
188 template <
typename T,
int N,
int C>
190 ndarray::Array<T const, N, N> contiguous = ndarray::dynamic_dimension_cast<N>(array);
191 if (contiguous.empty()) contiguous = ndarray::copy(array);
206 template <
typename T>
210 template <
typename T>
280 void createImageImpl(
int bitpix,
int nAxis,
long const* nAxes);
281 template <
typename T>
282 void writeImageImpl(T
const* data,
int nElements);
283 template <
typename T>
284 void readImageImpl(
int nAxis, T* data,
long* begin,
long*
end,
long* increment);
285 void getImageShapeImpl(
int maxDim,
long* nAxes);
308 void setHdu(
int hdu,
bool relative =
false);
314 template <
typename T>
316 void updateKey(std::string
const& key, T
const& value, std::string
const& comment);
317 void updateKey(std::string
const& key,
char const* value, std::string
const& comment) {
318 updateKey(key, std::string(value), comment);
320 template <
typename T>
321 void updateKey(std::string
const& key, T
const& value);
333 template <
typename T>
334 void writeKey(std::string
const& key, T
const& value, std::string
const& comment);
335 void writeKey(std::string
const& key,
char const* value, std::string
const& comment) {
336 writeKey(key, std::string(value), comment);
338 template <
typename T>
339 void writeKey(std::string
const& key, T
const& value);
340 void writeKey(std::string
const& key,
char const* value) {
writeKey(key, std::string(value)); }
344 template <
typename T>
346 void updateColumnKey(std::string
const& prefix,
int n, T
const& value, std::string
const& comment);
347 void updateColumnKey(std::string
const& prefix,
int n,
char const* value, std::string
const& comment) {
350 template <
typename T>
358 template <
typename T>
360 void writeColumnKey(std::string
const& prefix,
int n, T
const& value, std::string
const& comment);
361 void writeColumnKey(std::string
const& prefix,
int n,
char const* value, std::string
const& comment) {
364 template <
typename T>
394 template <
typename T>
395 void readKey(std::string
const& key, T& value);
425 template <
typename PixelT,
int N>
426 void createImage(ndarray::Vector<ndarray::Size, N>
const& shape) {
427 ndarray::Vector<long, N> nAxes(shape.reverse());
432 void createImage(
int bitpix, ndarray::Vector<ndarray::Size, N>
const& shape) {
433 ndarray::Vector<long, N> nAxes(shape.reverse());
434 createImageImpl(bitpix, N, nAxes.elems);
443 template <
typename PixelT>
445 long naxes[2] = {
x, y};
458 template <
typename T,
int N,
int C>
459 void writeImage(ndarray::Array<T const, N, C>
const& array) {
475 template <
typename T>
479 std::shared_ptr<daf::base::PropertySet const> header=std::shared_ptr<daf::base::PropertyList>(),
497 ndarray::Vector<long, N> nAxes(1);
498 getImageShapeImpl(N, nAxes.elems);
499 ndarray::Vector<ndarray::Size, N> shape;
500 for (
int i = 0; i < N; ++i) shape[i] = nAxes[N - i - 1];
510 template <
typename T>
519 template <
typename T,
int N>
520 void readImage(ndarray::Array<T, N, N>
const& array, ndarray::Vector<int, N>
const& offset) {
521 ndarray::Vector<long, N> begin(offset.reverse());
522 ndarray::Vector<long, N>
end(begin);
523 end += array.getShape().reverse();
524 ndarray::Vector<long, N> increment(1);
526 readImageImpl(N, array.getData(), begin.elems, end.elems, increment.elems);
538 template <
typename T>
539 int addColumn(std::string
const& ttype,
int size, std::string
const& comment);
547 template <
typename T>
548 int addColumn(std::string
const& ttype,
int size);
551 std::size_t
addRows(std::size_t nRows);
557 template <
typename T>
558 void writeTableArray(std::size_t row,
int col,
int nElements, T
const* value);
561 template <
typename T>
569 template <
typename T>
570 void readTableArray(std::size_t row,
int col,
int nElements, T* value);
573 template <
typename T>
579 void readTableScalar(std::size_t row,
int col, std::string& value,
bool isVariableLength);
591 Fits(std::string
const& filename, std::string
const& mode,
int behavior);
643 std::shared_ptr<daf::base::PropertyList>
readMetadata(std::string
const& fileName,
int hdu = INT_MIN,
676 #endif // !LSST_AFW_fits_h_INCLUDED
void createImage(int bitpix, ndarray::Vector< ndarray::Size, N > const &shape)
int getImageDim()
Return the number of dimensions in the current HDU.
std::size_t countRows()
Return the number of row in a table.
void writeKey(std::string const &key, char const *value)
Add a FITS header key to the bottom of the header.
MemFileManager(std::size_t len)
Construct a MemFileManager with (len) bytes of initial memory.
void updateKey(std::string const &key, char const *value)
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)
Write a key of the form XXXXXnnn, where XXXXX is the prefix and nnn is a column number.
void forEachKey(HeaderIterationFunctor &functor)
Call a polymorphic functor for every key in the header.
ImageCompressionOptions getImageCompression()
Return the current image compression settings.
ImageCompressionOptions compression
Options controlling compression.
static std::shared_ptr< daf::base::PropertySet > validate(daf::base::PropertySet const &config)
Validate a PropertySet.
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
void readKey(std::string const &key, T &value)
Read a FITS header key into the given reference.
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.
Options for writing an image to FITS.
void writeKey(std::string const &key, char const *value, std::string const &comment)
Add a FITS header key to the bottom of the header.
int getBitPix()
Return the cfitsio integer BITPIX code for the given data type.
void readTableScalar(std::size_t row, int col, T &value)
Read an array scalar from a binary table.
MemFileManager(void *ptr, std::size_t len)
Construct a MemFileManager that references and does not manage external memory.
bool checkImageType()
Return true if the current HDU is compatible with the given pixel type.
ImageWriteOptions(ImageCompressionOptions const &compression_=ImageCompressionOptions(ImageCompressionOptions::NONE), ImageScalingOptions const &scaling_=ImageScalingOptions())
Construct with specific compression and scaling options.
ImageWriteOptions(image::Mask< T > const &mask)
Construct with default options for masks.
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.
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.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
The base class for all image classed (Image, Mask, MaskedImage, ...)
long getTableArraySize(int col)
Return the size of an array column.
FITS BITPIX header value by C++ type.
void closeFile()
Close a FITS file.
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=INT_MIN, bool strip=false)
Read FITS header.
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.
void createEmpty()
Create an empty image HDU with NAXIS=0 at the end of the file.
std::size_t getLength() const
Return the buffer length.
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 readImage(ndarray::Array< T, N, N > const &array, ndarray::Vector< int, N > const &offset)
Read an array from a FITS image.
MemFileManager()
Construct a MemFileManager with no initial memory buffer.
ndarray::Array< T const, N, N > const makeContiguousArray(ndarray::Array< T, N, C > const &array)
Construct a contiguous ndarray.
An exception thrown when problems are found when reading or writing FITS files.
metadata import lsst afw display as afwDisplay n
ndarray::Vector< ndarray::Size, N > getImageShape()
Return the shape of the current (image) HDU.
Options for tile compression of image pixels.
void createImage(long x, long y)
Create a 2-d image with pixel type provided by the given explicit PixelT template parameter...
Represent a 2-dimensional array of bitmask pixels.
Lifetime-management for memory that goes into FITS memory files.
std::size_t addRows(std::size_t nRows)
Append rows to a table, and return the index of the first new row.
Fits()
Default constructor; set all data members to 0.
void reset(void *ptr, std::size_t len)
Set the internal memory buffer to an manually-managed external block.
void createTable()
Create a new binary table extension.
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.
Options for scaling image pixels.
ImageWriteOptions(ImageScalingOptions const &scaling_)
Construct with specific scaling options.
void setHdu(int hdu, bool relative=false)
Set the current HDU.
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.
ImageScalingOptions scaling
Options controlling scaling.
bool checkCompressedImagePhu()
Go to the first image header in the FITS file.
std::string getFileName() const
Return the file name associated with the FITS object or "<unknown>" if there is none.
lsst::daf::base::PropertySet PropertySet
void setAllowImageCompression(bool allow)
void writeTableArray(std::size_t row, int col, int nElements, T const *value)
Write an array value to a binary table.
An exception thrown when a FITS file has the wrong type.
void reset()
Return the manager to the same state it would be if default-constructed.
void readTableArray(std::size_t row, int col, int nElements, T *value)
Read an array value from a binary table.
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...
void setImageCompression(ImageCompressionOptions const &options)
Set compression options for writing FITS images.
void writeKey(std::string const &key, T const &value, std::string const &comment)
Add a FITS header key to the bottom of the header.
MemFileManager & operator=(const MemFileManager &)=delete
Fits & operator=(const Fits &)=delete
bool getAllowImageCompression()
int getHdu()
Return the current HDU (0-indexed; 0 is the Primary HDU).
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
int addColumn(std::string const &ttype, int size, std::string const &comment)
Add a column to a table.
std::string makeErrorMessage(std::string const &fileName="", int status=0, std::string const &msg="")
Return an error message reflecting FITS I/O errors.
void * getData() const
Return the buffer.
A class to represent a 2-dimensional array of pixels.
void writeTableScalar(std::size_t row, int col, T value)
Write a scalar value to a binary table.
void readMetadata(daf::base::PropertySet &metadata, bool strip=false)
Read a FITS header into a PropertySet or PropertyList.
ImageWriteOptions(image::Image< T > const &image)
Construct with default options for images.
int countHdus()
Return the number of HDUs in the file.