23#include "boost/algorithm/string/trim.hpp"
31fits::Fits * nextHdu(fits::Fits * fitsFile) {
32 if (fitsFile ==
nullptr) {
36 fitsFile->setHdu(1,
true);
37 }
catch (fits::FitsError &) {
47 _imageReader(fileName, hdu),
48 _maskReader(nextHdu(_imageReader._fitsFile)),
49 _varianceReader(nextHdu(_maskReader._fitsFile))
53 _imageReader(manager, hdu),
54 _maskReader(nextHdu(_imageReader._fitsFile)),
55 _varianceReader(nextHdu(_maskReader._fitsFile))
59 _imageReader(fitsFile),
60 _maskReader(nextHdu(_imageReader._fitsFile)),
61 _varianceReader(nextHdu(_maskReader._fitsFile))
73 return _imageReader.
readBBox(origin);
85 std::string exttype = boost::algorithm::trim_right_copy(metadata->getAsString(
"EXTTYPE"));
86 if (exttype !=
"" && exttype != expected) {
88 str(boost::format(
"Reading %s (hdu %d) Expected EXTTYPE==\"%s\", saw \"%s\"") %
91 metadata->remove(
"EXTTYPE");
93 LOGL_WARN(
"lsst.afw.image.MaskedImageFitsReader",
"Expected extension type not found: %s",
101 auto fitsFile = _imageReader._fitsFile;
107 if (!_imageMetadata) {
109 checkExtType(_imageReader, _imageMetadata,
"IMAGE");
111 return _imageMetadata;
115 if (!_maskMetadata) {
117 checkExtType(_maskReader, _maskMetadata,
"MASK");
119 return _maskMetadata;
123 if (!_varianceMetadata) {
125 checkExtType(_varianceReader, _varianceMetadata,
"VARIANCE");
127 return _varianceMetadata;
130template <
typename ImagePixelT>
133 return _imageReader.
read<ImagePixelT>(
bbox, origin, allowUnsafe);
136template <
typename ImagePixelT>
140 return _imageReader.
readArray<ImagePixelT>(
bbox, origin, allowUnsafe);
143template <
typename MaskPixelT>
145 bool conformMasks,
bool allowUnsafe) {
146 return _maskReader.
read<MaskPixelT>(
bbox, origin, conformMasks, allowUnsafe);
149template <
typename MaskPixelT>
153 return _maskReader.
readArray<MaskPixelT>(
bbox, origin, allowUnsafe);
156template <
typename VariancePixelT>
159 return _varianceReader.
read<VariancePixelT>(
bbox, origin, allowUnsafe);
162template <
typename VariancePixelT>
166 return _varianceReader.
readArray<VariancePixelT>(
bbox, origin, allowUnsafe);
169template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
172 bool conformMasks,
bool needAllHdus,
bool allowUnsafe
191 enum class Hdu { Primary = 0,
Image,
Mask, Variance };
194 if (needAllHdus && _imageReader.
getHdu() >
static_cast<int>(Hdu::Image)) {
198 auto image = std::make_shared<Image<ImagePixelT>>(_imageReader.
read<ImagePixelT>(
bbox, origin,
204 if (_imageReader.
getHdu() ==
static_cast<int>(Hdu::Image)) {
206 if (_maskReader._fitsFile) {
208 mask = std::make_shared<Mask<MaskPixelT>>(
209 _maskReader.
read<MaskPixelT>(
bbox, origin, conformMasks, allowUnsafe)
216 LOGL_WARN(_log,
"Mask unreadable (%s); using default", e.
what());
218 _maskReader._fitsFile->status = 0;
220 }
else if (needAllHdus) {
223 if (_varianceReader._fitsFile) {
225 variance = std::make_shared<Image<VariancePixelT>>(
226 _varianceReader.
read<VariancePixelT>(
bbox, origin, allowUnsafe)
233 LOGL_WARN(_log,
"Variance unreadable (%s); using default", e.
what());
235 _varianceReader._fitsFile->status = 0;
237 }
else if (needAllHdus) {
244#define INSTANTIATE(ImagePixelT) \
245 template MaskedImage<ImagePixelT, MaskPixel, VariancePixel> MaskedImageFitsReader::read( \
246 lsst::geom::Box2I const &, \
250 template Image<ImagePixelT> MaskedImageFitsReader::readImage(\
251 lsst::geom::Box2I const &, \
255 template ndarray::Array<ImagePixelT, 2, 2> MaskedImageFitsReader::readImageArray(\
256 lsst::geom::Box2I const &, \
#define INSTANTIATE(FROMSYS, TOSYS)
#define LSST_EXCEPT_ADD(e, m)
Add the current location and a message to an existing exception before rethrowing it.
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
LSST DM logging module built on log4cxx.
#define LOGL_WARN(logger, message...)
Log a warn-level message using a varargs/printf style interface.
#define LOG_GET(logger)
Returns a Log object associated with logger.
An exception thrown when problems are found when reading or writing FITS files.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
RAII scoped guard for moving the HDU in a Fits object.
Lifetime-management for memory that goes into FITS memory files.
Base class for image FITS readers.
lsst::geom::Point2I readXY0(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT)
Read the image origin from the on-disk image or a subimage thereof.
std::shared_ptr< daf::base::PropertyList > readMetadata()
Read the image's FITS header.
ndarray::Array< T, 2, 2 > readArray(lsst::geom::Box2I const &bbox, ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the image's data array.
std::string getFileName() const
Return the name of the file this reader targets.
int getHdu() const noexcept
Return the HDU this reader targets.
lsst::geom::Box2I readBBox(ImageOrigin origin=PARENT)
Read the bounding box of the on-disk image.
std::string readDType() const
Read a string describing the pixel type of the on-disk image.
Image< PixelT > read(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the Image.
A class to represent a 2-dimensional array of pixels.
Mask< PixelT > read(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool allowUnsafe=false)
Read the Mask.
Represent a 2-dimensional array of bitmask pixels.
A FITS reader class for MaskedImages and their components.
std::string readMaskDType() const
Read a string describing the pixel type of the on-disk image plane.
std::shared_ptr< daf::base::PropertyList > readImageMetadata()
Read the FITS header of one of the HDUs.
std::shared_ptr< daf::base::PropertyList > readPrimaryMetadata()
Read the FITS header of one of the HDUs.
std::string readVarianceDType() const
Read a string describing the pixel type of the on-disk image plane.
ndarray::Array< MaskPixelT, 2, 2 > readMaskArray(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the mask plane.
MaskedImageFitsReader(std::string const &fileName, int hdu=fits::DEFAULT_HDU)
Construct a FITS reader object.
std::shared_ptr< daf::base::PropertyList > readMaskMetadata()
Read the FITS header of one of the HDUs.
ndarray::Array< ImagePixelT, 2, 2 > readImageArray(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the image plane.
std::shared_ptr< daf::base::PropertyList > readVarianceMetadata()
Read the FITS header of one of the HDUs.
ndarray::Array< VariancePixelT, 2, 2 > readVarianceArray(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the variance plane.
Mask< MaskPixelT > readMask(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool allowUnsafe=false)
Read the mask plane.
lsst::geom::Box2I readBBox(ImageOrigin origin=PARENT)
Read the bounding box of the on-disk image.
~MaskedImageFitsReader() noexcept
MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > read(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool needAllHdus=false, bool allowUnsafe=false)
Read the full MaskedImage.
Image< ImagePixelT > readImage(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the image plane.
Image< VariancePixelT > readVariance(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the variance plane.
lsst::geom::Point2I readXY0(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT)
Read the image origin from the on-disk image or a subimage thereof.
A class to manipulate images, masks, and variance as a single object.
An integer coordinate rectangle.
virtual char const * what(void) const noexcept
Return a character string summarizing this exception.
Reports invalid arguments.
Reports attempts to access elements using an invalid key.
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=DEFAULT_HDU, bool strip=false)
Read FITS header.
g2d::python::Image< double > Image
py::scoped_interpreter guard
g2d::python::Image< bool > Mask