32 #pragma clang diagnostic push
33 #pragma clang diagnostic ignored "-Wunused-variable"
34 #pragma clang diagnostic pop
35 #include "boost/regex.hpp"
36 #include "boost/filesystem/path.hpp"
39 #include "boost/algorithm/string/trim.hpp"
51 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
57 lsst::daf::base::Citizen(typeid(this)),
58 _image(new
Image(width, height)),
59 _mask(new
Mask(width, height, planeDict)),
60 _variance(new
Variance(width, height)) {
70 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
75 lsst::daf::base::Citizen(typeid(this)),
76 _image(new
Image(dimensions)),
77 _mask(new
Mask(dimensions, planeDict)),
78 _variance(new
Variance(dimensions)) {
92 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
97 lsst::daf::base::Citizen(typeid(this)),
98 _image(new
Image(bbox)),
99 _mask(new
Mask(bbox, planeDict)),
106 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
113 ) : lsst::daf::base::Citizen(typeid(this)),
114 _image(), _mask(), _variance()
117 *
this =
MaskedImage(fitsfile, metadata, bbox, origin, conformMasks, needAllHdus,
118 imageMetadata, maskMetadata, varianceMetadata);
121 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
128 ) : lsst::daf::base::Citizen(typeid(this)),
129 _image(), _mask(), _variance()
132 *
this =
MaskedImage(fitsfile, metadata, bbox, origin, conformMasks, needAllHdus,
133 imageMetadata, maskMetadata, varianceMetadata);
143 std::string
const & expected
146 std::string exttype = boost::algorithm::trim_right_copy(metadata->
getAsString(
"EXTTYPE"));
147 if (exttype !=
"" && exttype != expected) {
148 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
149 (
boost::format(
"Reading %s (hdu %d) Expected EXTTYPE==\"%s\", saw \"%s\"") %
152 metadata->
remove(
"EXTTYPE");
153 }
catch(lsst::pex::exceptions::NotFoundError) {
154 LOGLS_WARN(
"afw.image.MaskedImage",
"Expected extension type not found: " << expected);
167 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
174 ) : lsst::daf::base::Citizen(typeid(this)),
175 _image(), _mask(), _variance()
202 if (needAllHdus && fitsfile.
getHdu() >
static_cast<int>(Hdu::Image)) {
204 "Cannot read all HDUs starting from non-default");
210 auto prevHdu = fitsfile.
getHdu();
211 fitsfile.
setHdu(static_cast<int>(Hdu::Primary));
219 ensureMetadata(imageMetadata);
220 _image.reset(
new Image(fitsfile, imageMetadata, bbox, origin));
221 checkExtType(fitsfile, imageMetadata,
"IMAGE");
223 if (fitsfile.
getHdu() !=
static_cast<int>(Hdu::Image)) {
230 fitsfile.
setHdu(static_cast<int>(Hdu::Mask));
231 ensureMetadata(maskMetadata);
232 _mask.reset(
new Mask(fitsfile, maskMetadata, bbox, origin, conformMasks));
233 checkExtType(fitsfile, maskMetadata,
"MASK");
234 }
catch(fits::FitsError &e) {
239 LOGLS_WARN(_log,
"Mask unreadable; using default");
246 fitsfile.
setHdu(static_cast<int>(Hdu::Variance));
247 ensureMetadata(varianceMetadata);
249 checkExtType(fitsfile, varianceMetadata,
"VARIANCE");
250 }
catch(fits::FitsError &e) {
255 LOGLS_WARN(_log,
"Variance unreadable; using default");
266 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
272 lsst::daf::base::Citizen(typeid(this)),
275 _variance(variance) {
282 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
287 lsst::daf::base::Citizen(typeid(this)),
288 _image(rhs._image), _mask(rhs._mask), _variance(rhs._variance) {
300 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
308 lsst::daf::base::Citizen(typeid(this)),
309 _image(new
Image(*rhs.getImage(), bbox, origin, deep)),
310 _mask(rhs._mask ? new
Mask(*rhs.getMask(), bbox, origin, deep) : static_cast<
Mask *>(NULL)),
311 _variance(rhs._variance ? new
Variance(*rhs.getVariance(), bbox, origin, deep) : static_cast<
Variance *>(NULL)) {
323 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
328 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
333 _mask.swap(rhs.
_mask);
344 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
347 *_image = rhs.
image();
357 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
360 *_image = rhs.
image();
374 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
390 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
393 _image->assign(*rhs.
getImage(), bbox, origin);
394 _mask->assign(*rhs.
getMask(), bbox, origin);
395 _variance->assign(*rhs.
getVariance(), bbox, origin);
405 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
419 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
422 (*_image).scaledPlus(c, *rhs.
getImage());
428 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
438 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
450 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
453 (*_image).scaledMinus(c, *rhs.
getImage());
459 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
466 template<
typename ImagePixelT,
typename VariancePixelT>
467 struct productVariance {
468 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
469 return lhs*lhs*varRhs + rhs*rhs*varLhs;
474 template<
typename ImagePixelT,
typename VariancePixelT>
475 struct scaledProductVariance {
477 scaledProductVariance(
double const c) : _c(c) {}
478 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
479 return _c*_c*(lhs*lhs*varRhs + rhs*rhs*varLhs);
484 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
487 transform_pixels(_image->_getRawView(),
488 rhs.
_image->_getRawView(),
489 _variance->_getRawView(),
491 _variance->_getRawView(),
492 productVariance<ImagePixelT, VariancePixelT>());
498 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
502 transform_pixels(_image->_getRawView(),
503 rhs.
_image->_getRawView(),
504 _variance->_getRawView(),
506 _variance->_getRawView(),
507 scaledProductVariance<ImagePixelT, VariancePixelT>(c));
509 (*_image).scaledMultiplies(c, *rhs.
getImage());
513 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
516 *_variance *= rhs*rhs;
522 template<
typename ImagePixelT,
typename VariancePixelT>
523 struct quotientVariance {
524 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
525 ImagePixelT
const rhs2 = rhs*rhs;
526 return (lhs*lhs*varRhs + rhs2*varLhs)/(rhs2*rhs2);
530 template<
typename ImagePixelT,
typename VariancePixelT>
531 struct scaledQuotientVariance {
533 scaledQuotientVariance(
double c) : _c(c) {}
534 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
535 ImagePixelT
const rhs2 = rhs*rhs;
536 return (lhs*lhs*varRhs + rhs2*varLhs)/(_c*_c*rhs2*rhs2);
541 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
544 transform_pixels(_image->_getRawView(),
545 rhs.
_image->_getRawView(),
546 _variance->_getRawView(),
548 _variance->_getRawView(),
549 quotientVariance<ImagePixelT, VariancePixelT>());
555 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
559 transform_pixels(_image->_getRawView(),
560 rhs.
_image->_getRawView(),
561 _variance->_getRawView(),
563 _variance->_getRawView(),
564 scaledQuotientVariance<ImagePixelT, VariancePixelT>(c));
566 (*_image).scaledDivides(c, *rhs.
getImage());
567 *_mask |= *rhs.
_mask;
570 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
573 *_variance /= rhs*rhs;
576 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
578 std::string
const& fileName,
585 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
588 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
597 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
602 void processPlaneMetadata(
612 hdr->
set(
"INHERIT",
true);
613 hdr->
set(
"EXTTYPE", exttype);
618 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
636 pex::exceptions::LogicError,
637 "MaskedImage::writeFits can only write to an empty file"
640 if (fitsfile.
getHdu() <= 1) {
648 processPlaneMetadata(imageMetadata, hdr,
"IMAGE");
649 _image->writeFits(fitsfile, hdr);
651 processPlaneMetadata(maskMetadata, hdr,
"MASK");
652 _mask->writeFits(fitsfile, hdr);
654 processPlaneMetadata(varianceMetadata, hdr,
"VARIANCE");
655 _variance->writeFits(fitsfile, hdr);
663 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
666 if (!_mask || _mask->getWidth() == 0 || _mask->getHeight() == 0) {
670 if (_mask->getDimensions() != _image->getDimensions()) {
672 lsst::pex::exceptions::LengthError,
673 (
boost::format(
"Dimension mismatch: Image %dx%d v. Mask %dx%d") %
674 _image->getWidth() % _image->getHeight() %
675 _mask->getWidth() % _mask->getHeight()
681 if (!_variance || _variance->getWidth() == 0 || _variance->getHeight() == 0) {
685 if (_variance->getDimensions() != _image->getDimensions()) {
687 lsst::pex::exceptions::LengthError,
688 (
boost::format(
"Dimension mismatch: Image %dx%d v. Variance %dx%d") %
689 _image->getWidth() % _image->getHeight() %
690 _variance->getWidth() % _variance->getHeight()
702 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
704 #if 0 // this doesn't compile; why?
705 return iterator(_image->begin(), _mask->begin(), _variance->begin());
711 return iterator(imageBegin, maskBegin, varianceBegin);
716 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
722 return iterator(imageEnd, maskEnd, varianceEnd);
726 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
732 return iterator(imageEnd, maskEnd, varianceEnd);
736 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
746 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
756 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
762 return x_iterator(imageBegin, maskBegin, varianceBegin);
766 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
772 return x_iterator(imageEnd, maskEnd, varianceEnd);
778 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
784 return y_iterator(imageBegin, maskBegin, varianceBegin);
788 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
794 return y_iterator(imageEnd, maskEnd, varianceEnd);
805 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
822 template<
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
#define LOGLS_WARN(logger, message)
Log a warn-level message using an iostream-based interface.
x_iterator row_begin(int y) const
Return an x_iterator to the start of the image.
VariancePixelT variance() const
Return the variance part of a Pixel.
Image< VariancePixelT >::Ptr VariancePtr
shared pointer to the variance Image
void scaledDivides(double const c, MaskedImage const &rhs)
int getHdu()
Return the current HDU (1-indexed; 1 is the Primary HDU).
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
_view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
iterator begin() const
Return an iterator to the start of the image.
void set(std::string const &name, T const &value)
Replace all values for a property name (possibly hierarchical) with a new value.
virtual Ptr deepCopy(void) const
Copy the PropertySet and all of its contents.
void operator<<=(MaskedImage const &rhs)
Copy the pixels from the rhs to the lhs.
Class for storing ordered metadata with comments.
VariancePixelT variance() const
Include files required for standard LSST Exception handling.
void operator/=(ImagePixelT const rhs)
void scaledPlus(double const c, MaskedImage const &rhs)
Add a scaled MaskedImage c*rhs to a MaskedImage.
void writeFits(std::string const &fileName, boost::shared_ptr< daf::base::PropertySet const > metadata=boost::shared_ptr< daf::base::PropertySet const >(), boost::shared_ptr< daf::base::PropertySet const > imageMetadata=boost::shared_ptr< daf::base::PropertySet const >(), boost::shared_ptr< daf::base::PropertySet const > maskMetadata=boost::shared_ptr< daf::base::PropertySet const >(), boost::shared_ptr< daf::base::PropertySet const > varianceMetadata=boost::shared_ptr< daf::base::PropertySet const >()) const
Write a MaskedImage to a regular FITS file.
iterator at(int const x, int const y) const
Return an iterator at the point (x, y)
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
x_iterator row_end(int y) const
Return an x_iterator to the end of the image.
ImagePtr getImage(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's image.
A single pixel of the same type as a MaskedImage.
MaskPixelT mask() const
Return the mask part of a Pixel.
void swap(Mask< PixelT > &a, Mask< PixelT > &b)
void swap(MaskedImage &rhs)
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
lsst::afw::image::Mask< MaskPixelT > Mask
int countHdus()
Return the number of HDUs in the file.
MaskedImage(unsigned int width, unsigned int height, MaskPlaneDict const &planeDict=MaskPlaneDict())
Constructors.
void operator-=(ImagePixelT const rhs)
Subtract a scalar rhs from a MaskedImage.
void operator+=(ImagePixelT const rhs)
Add a scalar rhs to a MaskedImage.
An integer coordinate rectangle.
Mask< MaskPixelT >::MaskPlaneDict MaskPlaneDict
The Mask's MaskPlaneDict.
VariancePtr getVariance(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's variance.
table::Key< table::Array< Kernel::Pixel > > image
LSST DM logging module built on log4cxx.
An iterator to the MaskedImage.
_view_t::iterator iterator
An STL compliant iterator.
afw::table::PointKey< int > dimensions
reverse_iterator rbegin() const
Return a reverse_iterator to the start of the image.
void ImageT ImageT int float saturatedPixelValue int const width
Represent a 2-dimensional array of bitmask pixels.
Lifetime-management for memory that goes into FITS memory files.
iterator end() const
Return an iterator to the end of the image.
MaskPtr getMask(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's mask.
A class to manipulate images, masks, and variance as a single object.
_view_t::y_iterator y_iterator
An iterator for traversing the pixels in a column.
ImagePixelT image() const
Return the image part of a Pixel.
std::shared_ptr< Image< ImagePixelT > > Ptr
reverse_iterator rend() const
Return a reverse_iterator to the end of the image.
y_iterator col_end(int x) const
Return an y_iterator to the end of the image.
ImagePixelT image() const
void scaledMultiplies(double const c, MaskedImage const &rhs)
void setHdu(int hdu, bool relative=false)
Set the current HDU.
void ImageT ImageT int float saturatedPixelValue int const height
Image< ImagePixelT >::Ptr ImagePtr
shared pointer to the Image
Utilities for working with FITS files.
lsst::afw::image::Image< VariancePixelT > Variance
std::shared_ptr< Mask > Ptr
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
A pixel of a MaskedImage.
Mask< MaskPixelT >::Ptr MaskPtr
shared pointer to the Mask
void assign(MaskedImage const &rsh, geom::Box2I const &bbox=geom::Box2I(), ImageOrigin origin=PARENT)
Copy pixels from another masked image to a specified subregion of this masked image.
Class for storing generic metadata.
void createEmpty()
Create an empty image HDU with NAXIS=0 at the end of the file.
void readMetadata(daf::base::PropertySet &metadata, bool strip=false)
Read a FITS header into a PropertySet or PropertyList.
void operator*=(ImagePixelT const rhs)
y_iterator col_begin(int x) const
Return an y_iterator to the start of the image.
Implementation of the Class MaskedImage.
virtual void remove(std::string const &name)
Removes all values for a property name (possibly hierarchical).
lsst::afw::image::Image< ImagePixelT > Image
#define CONST_PTR(...)
A shared pointer to a const object.
#define LOG_GET(logger)
Returns a Log object associated with logger.
std::string getFileName() const
Return the file name associated with the FITS object or "<unknown>" if there is none.
#define LSST_EXCEPT_ADD(e, m)
Add the current location and a message to an existing exception before rethrowing it...
MaskedImage & operator=(MaskedImage const &rhs)
Make the lhs use the rhs's pixels.
std::string getAsString(std::string const &name) const
Get the last value for a string property name (possibly hierarchical).
void scaledMinus(double const c, MaskedImage const &rhs)
Subtract a scaled MaskedImage c*rhs from a MaskedImage.