24 #ifndef LSST_AFW_IMAGE_fits_io_h_INCLUDED
25 #define LSST_AFW_IMAGE_fits_io_h_INCLUDED
29 #include "boost/format.hpp"
30 #include "lsst/pex/exceptions.h"
40 template <
typename PixelT>
48 ndarray::Vector<ndarray::Size, 2> shape;
51 }
else if (nAxis == 3) {
52 ndarray::Vector<ndarray::Size, 3> shape3 = fitsfile.
getImageShape<3>();
55 boost::str(boost::format(
"3rd dimension %d is not 1") % shape3[0]));
57 shape = shape3.last<2>();
63 const int hdu = fitsfile.
getHdu();
64 std::shared_ptr<daf::base::PropertyList> header(
readMetadata(fitsfile,
true));
65 metadata.combine(header);
74 if (!bbox.isEmpty()) {
76 bbox.shift(-xyOffset);
80 if (bbox.getMinX() < 0 || bbox.getMinY() < 0 || bbox.getWidth() > dimensions.getX() ||
81 bbox.getHeight() > dimensions.getY()) {
82 throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
83 (boost::format(
"BBox (%d,%d) %dx%d doesn't fit in image %dx%d") %
84 bbox.getMinX() % bbox.getMinY() % bbox.getWidth() % bbox.getHeight() %
85 dimensions.getX() % dimensions.getY())
88 dimensions = bbox.getDimensions();
91 array = ndarray::allocate(dimensions.getY(), dimensions.getX());
93 fitsfile.
readImage(array, ndarray::makeVector(xy0.getY(), xy0.getX()));
98 template <
typename ImageT>
100 std::shared_ptr<daf::base::PropertySet const> metadata =
101 std::shared_ptr<daf::base::PropertySet const>()) {
102 fitsfile.
createImage<
typename ImageT::Pixel>(image.getArray().getShape());
112 #endif // !LSST_AFW_IMAGE_fits_io_h_INCLUDED
int getImageDim()
Return the number of dimensions in the current HDU.
afw::table::PointKey< int > dimensions
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
void fits_write_image(fits::Fits &fitsfile, const ImageT &image, std::shared_ptr< daf::base::PropertySet const > metadata=std::shared_ptr< daf::base::PropertySet const >())
bool checkImageType()
Return true if the current HDU has the given pixel type.
Extent< int, 2 > Extent2I
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
An integer coordinate rectangle.
void readImage(ndarray::Array< T, N, N > const &array, ndarray::Vector< int, N > const &offset)
Read an array from a FITS image.
An exception thrown when problems are found when reading or writing FITS files.
ndarray::Vector< ndarray::Size, N > getImageShape()
Return the shape of the current (image) HDU.
void fits_read_array(fits::Fits &fitsfile, ndarray::Array< PixelT, 2, 2 > &array, geom::Point2I &xy0, lsst::daf::base::PropertySet &metadata, geom::Box2I bbox=geom::Box2I(), ImageOrigin origin=PARENT)
void setHdu(int hdu, bool relative=false)
Set the current HDU.
lsst::daf::base::PropertySet PropertySet
An exception thrown when a FITS file has the wrong type.
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...
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=INT_MIN, bool strip=false)
Return the metadata (header entries) from a FITS file.
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.
#define LSST_FITS_EXCEPT(type, fitsObj,...)
A FITS-related replacement for LSST_EXCEPT that takes an additional Fits object and uses makeErrorMes...
std::string const wcsNameForXY0
geom::Point2I getImageXY0FromMetadata(std::string const &wcsName, lsst::daf::base::PropertySet *metadata)