LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
addToCoadd.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
30 #include <limits>
31 
32 #include "boost/format.hpp"
33 
34 #include "lsst/pex/exceptions.h"
35 #include "lsst/afw/geom.h"
37 
38 namespace pexExcept = lsst::pex::exceptions;
39 namespace afwGeom = lsst::afw::geom;
40 namespace afwImage = lsst::afw::image;
41 namespace coaddUtils = lsst::coadd::utils;
42 
43 namespace {
44  /*
45  * A boolean functor to test if a MaskedImage pixel is valid (mask & badPixel == 0)
46  */
47  struct CheckMask {
48  CheckMask(lsst::afw::image::MaskPixel badPixel) : _badPixel(badPixel) {}
49 
50  template<typename T>
51  bool operator()(T val) const {
52  return ((val.mask() & _badPixel) == 0) ? true : false;
53  }
54  private:
56  };
57 
58  /*
59  * A boolean functor to test if an Image pixel has known value (not NaN)
60  */
61  struct CheckKnownValue {
62  CheckKnownValue(lsst::afw::image::MaskPixel) {}
63 
64  template<typename T>
65  bool operator()(T val) const {
66  return !std::isnan(static_cast<float>(*val));
67  }
68  };
69 
70  /*
71  * Implementation of addToCoadd
72  *
73  * The template parameter isValidPixel is a functor with operator()
74  * which returns true if a given image pixel is valid.
75  * This allows us to support multiple image types including
76  * MaskedImage with a mask bit and Image with a check for NaN.
77  *
78  * @return overlapping bounding box, relative to parent image
79  */
80  template <typename CoaddT, typename WeightPixelT, typename isValidPixel>
81  static lsst::afw::geom::Box2I addToCoaddImpl(
82  CoaddT &coadd,
84  CoaddT const &image,
85  lsst::afw::image::MaskPixel const badPixelMask,
86  WeightPixelT weight
87  ) {
88  typedef typename afwImage::Image<WeightPixelT> WeightMapT;
89 
90  if (coadd.getBBox() != weightMap.getBBox()) {
91  throw LSST_EXCEPT(pexExcept::InvalidParameterError,
92  (boost::format("coadd and weightMap parent bboxes differ: %s != %s") %
93  coadd.getBBox() % weightMap.getBBox()).str());
94  }
95 
96  afwGeom::Box2I overlapBBox = coadd.getBBox();
97  overlapBBox.clip(image.getBBox());
98  if (overlapBBox.isEmpty()) {
99  return overlapBBox;
100  }
101 
102  CoaddT coaddView(coadd, overlapBBox, afwImage::PARENT, false);
103  WeightMapT weightMapView(weightMap, overlapBBox, afwImage::PARENT, false);
104  CoaddT imageView(image, overlapBBox, afwImage::PARENT, false);
105 
106  isValidPixel const isValid(badPixelMask); // functor to check if a pixel is good
107  for (int y = 0, endY = imageView.getHeight(); y != endY; ++y) {
108  typename CoaddT::const_x_iterator imageIter = imageView.row_begin(y);
109  typename CoaddT::const_x_iterator const imageEndIter = imageView.row_end(y);
110  typename CoaddT::x_iterator coaddIter = coaddView.row_begin(y);
111  typename WeightMapT::x_iterator weightMapIter = weightMapView.row_begin(y);
112  for (; imageIter != imageEndIter; ++imageIter, ++coaddIter, ++weightMapIter) {
113  if (isValid(imageIter)) {
114  typename CoaddT::SinglePixel pix = *imageIter;
115  pix *= typename CoaddT::Pixel(weight);
116  *coaddIter += pix;
117  *weightMapIter += weight;
118  }
119  }
120  }
121  return overlapBBox;
122  }
123 } // anonymous namespace
124 
125 template <typename CoaddPixelT, typename WeightPixelT>
127  // spell out lsst:afw::image to make Doxygen happy
131  WeightPixelT weight
132 ) {
134  return addToCoaddImpl<Image, WeightPixelT, CheckKnownValue>(coadd, weightMap, image, 0x0, weight);
135 }
136 
137 template <typename CoaddPixelT, typename WeightPixelT>
139  // spell out lsst:afw::image to make Doxygen happy
144  lsst::afw::image::VariancePixel> const &maskedImage,
145  lsst::afw::image::MaskPixel const badPixelMask,
146  WeightPixelT weight
147 ) {
149  return addToCoaddImpl<Image,WeightPixelT,CheckMask>(coadd, weightMap, maskedImage, badPixelMask, weight);
150 }
151 
152 // Explicit instantiations
153 
155 #define MASKEDIMAGE(IMAGEPIXEL) afwImage::MaskedImage<IMAGEPIXEL, \
156  afwImage::MaskPixel, afwImage::VariancePixel>
157 #define INSTANTIATE(COADDPIXEL, WEIGHTPIXEL) \
158  template lsst::afw::geom::Box2I coaddUtils::addToCoadd<COADDPIXEL, WEIGHTPIXEL>( \
159  afwImage::Image<COADDPIXEL> &coadd, \
160  afwImage::Image<WEIGHTPIXEL> &weightMap, \
161  afwImage::Image<COADDPIXEL> const &image, \
162  WEIGHTPIXEL weight \
163  ); \
164  \
165  template lsst::afw::geom::Box2I coaddUtils::addToCoadd<COADDPIXEL, WEIGHTPIXEL>( \
166  MASKEDIMAGE(COADDPIXEL) &coadd, \
167  afwImage::Image<WEIGHTPIXEL> &weightMap, \
168  MASKEDIMAGE(COADDPIXEL) const &image, \
169  afwImage::MaskPixel const badPixelMask, \
170  WEIGHTPIXEL weight \
171  );
172 
173 INSTANTIATE(double, double);
174 INSTANTIATE(double, float);
175 INSTANTIATE(double, int);
176 INSTANTIATE(double, boost::uint16_t);
177 INSTANTIATE(float, double);
178 INSTANTIATE(float, float);
179 INSTANTIATE(float, int);
180 INSTANTIATE(float, boost::uint16_t);
int y
An include file to include the header files for lsst::afw::geom.
tbl::Key< double > weight
Include files required for standard LSST Exception handling.
boost::uint16_t MaskPixel
#define INSTANTIATE(MATCH)
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: Image.h:377
int isnan(T t)
Definition: ieee.h:110
bool val
An integer coordinate rectangle.
Definition: Box.h:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:77
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:166
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
lsst::afw::geom::Box2I addToCoadd(lsst::afw::image::Image< CoaddPixelT > &coadd, lsst::afw::image::Image< WeightPixelT > &weightMap, lsst::afw::image::Image< CoaddPixelT > const &image, WeightPixelT weight)
add good pixels from an image to a coadd and associated weight map
Definition: addToCoadd.cc:126
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
float VariancePixel
! default type for Masks and MaskedImage Masks
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:415