LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
NaiveFlux.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2013 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #include "ndarray/eigen.h"
25 
26 #include "lsst/afw/detection/Psf.h"
27 #include "lsst/afw/geom/Box.h"
29 #include "lsst/afw/table/Source.h"
31 
32 namespace lsst { namespace meas { namespace base {
33 
34 namespace { //anonymous
35 
36 template <typename MaskedImageT>
37 class FootprintFlux : public lsst::afw::detection::FootprintFunctor<MaskedImageT> {
38 public:
39  explicit FootprintFlux(MaskedImageT const& mimage
40  ) : lsst::afw::detection::FootprintFunctor<MaskedImageT>(mimage),
41  _sum(0.0), _sumVar(0.0) {}
42 
44  void reset() {
45  _sum = _sumVar = 0.0;
46  }
47  void reset(lsst::afw::detection::Footprint const&) {}
48 
50  void operator()(typename MaskedImageT::xy_locator loc,
51  int,
52  int
53  ) {
54  typename MaskedImageT::Image::Pixel ival = loc.image(0, 0);
55  typename MaskedImageT::Variance::Pixel vval = loc.variance(0, 0);
56  _sum += ival;
57  _sumVar += vval;
58  }
59 
61  double getSum() const { return _sum; }
62 
64  double getSumVar() const { return _sumVar; }
65 
66 private:
67  double _sum;
68  double _sumVar;
69 };
70 
71 template <typename MaskedImageT, typename WeightImageT>
72 class FootprintWeightFlux : public lsst::afw::detection::FootprintFunctor<MaskedImageT> {
73 public:
74  FootprintWeightFlux(MaskedImageT const& mimage,
75  typename WeightImageT::Ptr wimage
77  _wimage(wimage),
78  _sum(0.0), _sumVar(0.0), _x0(0), _y0(0) {}
79 
81  void reset(lsst::afw::detection::Footprint const& foot) {
82  _sum = _sumVar = 0.0;
83 
84  lsst::afw::geom::BoxI const& bbox(foot.getBBox());
85  _x0 = bbox.getMinX();
86  _y0 = bbox.getMinY();
87 
88  if (bbox.getDimensions() != _wimage->getDimensions()) {
89  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
90  (boost::format("Footprint at %d,%d -- %d,%d is wrong size for "
91  "%d x %d weight image") %
92  bbox.getMinX() % bbox.getMinY() % bbox.getMaxX() % bbox.getMaxY() %
93  _wimage->getWidth() % _wimage->getHeight()).str());
94  }
95  }
96  void reset() {}
97 
99  void operator()(typename MaskedImageT::xy_locator iloc,
100  int x,
101  int y
102  ) {
103  typename MaskedImageT::Image::Pixel ival = iloc.image(0, 0);
104  typename MaskedImageT::Variance::Pixel vval = iloc.variance(0, 0);
105  typename WeightImageT::Pixel wval = (*_wimage)(x - _x0, y - _y0);
106  _sum += wval*ival;
107  _sumVar += wval*wval*vval;
108  }
109 
111  double getSum() const { return _sum; }
113  double getSumVar() const { return _sumVar; }
114 
115 private:
116  typename WeightImageT::Ptr const& _wimage; // The weight image
117  double _sum; // our desired sum
118  double _sumVar; // The variance of our desired sum
119  int _x0, _y0; // the origin of the current Footprint
120 };
121 
122 
123 /*****************************************************************************************************/
127 template<typename T>
128 struct getSum2 {
129  getSum2() : sum(0.0), sum2(0.0) {}
130 
131  getSum2& operator+(T x) {
132  sum += x;
133  sum2 += x*x;
134  return *this;
135  }
136 
137  double sum; // \sum_i(x_i)
138  double sum2; // \sum_i(x_i^2)
139 };
140 } // end anonymous namespace
141 
143  Control const & ctrl,
144  std::string const & name,
146 ) : _ctrl(ctrl),
147  _fluxResultKey(
148  FluxResultKey::addFields(schema, name, "flux from Naive Flux algorithm")
149  ),
150  _centroidExtractor(schema, name)
151 {
152  static boost::array<FlagDefinition,N_FLAGS> const flagDefs = {{
153  {"flag", "general failure flag, set if anything went wrong"},
154  {"flag_edge", "source is too close to the edge of the field to compute the given aperture"}
155  }};
156  _flagHandler = FlagHandler::addFields(schema, name, flagDefs.begin(), flagDefs.end());
157 }
158 
160  afw::table::SourceRecord & measRecord,
161  afw::image::Exposure<float> const & exposure
162 ) const {
163  FluxResult result;
164  // Get the centroid from a previous centroid measurement
165  afw::geom::Point2D center = _centroidExtractor(measRecord, _flagHandler);
167 
168  double const xcen = center.getX();
169  double const ycen = center.getY();
170 
171  int const ixcen = afw::image::positionToIndex(xcen);
172  int const iycen = afw::image::positionToIndex(ycen);
173 
174  // BBox for data image
175  afw::geom::BoxI imageBBox(mimage.getBBox());
176 
177  /* ******************************************************* */
178  // Aperture flux
179  FootprintFlux<afw::image::Exposure<float>::MaskedImageT> fluxFunctor(mimage);
180  afw::detection::Footprint const foot(
181  afw::geom::PointI(ixcen, iycen),
182  _ctrl.radius,
183  imageBBox
184  );
185  try {
186  fluxFunctor.apply(foot);
187  } catch (pex::exceptions::LengthError &) {
188  throw LSST_EXCEPT(
191  EDGE
192  );
193  }
194 
195  result.flux = fluxFunctor.getSum();
196  result.fluxSigma = ::sqrt(fluxFunctor.getSumVar());
197  measRecord.set(_fluxResultKey, result);
198  _flagHandler.setValue(measRecord, FAILURE, false);
199 }
200 
201 
203  _flagHandler.handleFailure(measRecord, error);
204 }
205 
206 }}} // namespace lsst::meas::base
Defines the fields and offsets for a table.
Definition: Schema.h:46
SafeCentroidExtractor _centroidExtractor
Definition: NaiveFlux.h:108
WeightImageT::Ptr const & _wimage
Definition: NaiveFlux.cc:116
Eigen matrix objects that present a view into an ndarray::Array.
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Definition: NaiveFlux.cc:159
int positionToIndex(double pos)
Convert image position to nearest integer index.
Definition: ImageUtils.h:69
double _sum
Definition: NaiveFlux.cc:67
int y
A class to contain the data, WCS, and other information needed to describe an image of the sky...
Definition: Exposure.h:48
geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
Definition: MaskedImage.h:905
double _sumVar
Definition: NaiveFlux.cc:68
int _y0
Definition: NaiveFlux.cc:119
< unspecified-expression-type > operator+(ExpressionBase< Operand > const &operand, Scalar const &scalar)
Definition: operators.h:365
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=NULL) const
Definition: NaiveFlux.cc:202
An integer coordinate rectangle.
Definition: Box.h:53
FlagDefinition getDefinition(int i) const
Definition: FlagHandler.h:66
double sum2
Definition: NaiveFlux.cc:138
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:150
tbl::Schema schema
Definition: CoaddPsf.cc:324
int x
Flux flux
Measured flux in DN.
Definition: FluxUtilities.h:37
void setValue(afw::table::BaseRecord &record, int i, bool value) const
Definition: FlagHandler.h:72
double sum
Definition: NaiveFlux.cc:137
int _x0
Definition: NaiveFlux.cc:119
A FunctorKey for FluxResult.
Definition: FluxUtilities.h:55
A set of pixels in an Image.
Definition: Footprint.h:73
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
A C++ control class to handle NaiveFluxAlgorithm&#39;s configuration.
Definition: NaiveFlux.h:47
NaiveFluxAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
Definition: NaiveFlux.cc:142
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition: BaseRecord.h:136
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=NULL) const
Definition: FlagHandler.cc:59
Record class that contains measurements made on a single exposure.
Definition: Source.h:81
FluxErrElement fluxSigma
1-Sigma error (sqrt of variance) on flux in DN.
Definition: FluxUtilities.h:38
A functor class to allow users to process all the pixels in a Footprint.
static FlagHandler addFields(afw::table::Schema &schema, std::string const &prefix, FlagDefinition const *begin, FlagDefinition const *end)
Definition: FlagHandler.cc:28
double radius
&quot;Size of the circular aperture, in pixels&quot; ;
Definition: NaiveFlux.h:49
A reusable result struct for flux measurements.
Definition: FluxUtilities.h:36
geom::Box2I getBBox() const
Return the Footprint&#39;s bounding box.
Definition: Footprint.h:194