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
PsfFlux.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 "boost/array.hpp"
25 
26 #include "ndarray/eigen.h"
27 
28 #include "lsst/afw/table/Source.h"
29 #include "lsst/afw/detection/Psf.h"
31 #include "lsst/afw/detection/FootprintArray.cc"
32 #include "lsst/meas/base/PsfFlux.h"
33 
34 namespace lsst { namespace meas { namespace base {
35 
37  Control const & ctrl,
38  std::string const & name,
40 ) : _ctrl(ctrl),
41  _fluxResultKey(
42  FluxResultKey::addFields(schema, name, "flux derived from linear least-squares fit of PSF model")
43  ),
44  _centroidExtractor(schema, name)
45 {
46  static boost::array<FlagDefinition,N_FLAGS> const flagDefs = {{
47  {"flag", "general failure flag"},
48  {"flag_noGoodPixels", "not enough non-rejected pixels in data to attempt the fit"},
49  {"flag_edge", "object was too close to the edge of the image to use the full PSF model"}
50  }};
51  _flagHandler = FlagHandler::addFields(schema, name, flagDefs.begin(), flagDefs.end());
52 }
53 
55  afw::table::SourceRecord & measRecord,
56  afw::image::Exposure<float> const & exposure
57 ) const {
58  PTR(afw::detection::Psf const) psf = exposure.getPsf();
59  if (!psf) {
60  throw LSST_EXCEPT(
61  FatalAlgorithmError,
62  "PsfFlux algorithm requires a Psf with every exposure"
63  );
64  }
65  afw::geom::Point2D position = _centroidExtractor(measRecord, _flagHandler);
66  PTR(afw::detection::Psf::Image) psfImage = psf->computeImage(position);
67  afw::geom::Box2I fitBBox = psfImage->getBBox();
68  fitBBox.clip(exposure.getBBox());
69  if (fitBBox != psfImage->getBBox()) {
70  _flagHandler.setValue(measRecord, FAILURE, true); // if we had a suspect flag, we'd set that instead
71  _flagHandler.setValue(measRecord, EDGE, true);
72  }
73  afw::detection::Footprint fitRegion(fitBBox);
74  if (!_ctrl.badMaskPlanes.empty()) {
75  afw::image::MaskPixel badBits = 0x0;
76  for (
77  std::vector<std::string>::const_iterator i = _ctrl.badMaskPlanes.begin();
78  i != _ctrl.badMaskPlanes.end();
79  ++i
80  ) {
81  badBits |= exposure.getMaskedImage().getMask()->getPlaneBitMask(*i);
82  }
83  fitRegion.intersectMask(*exposure.getMaskedImage().getMask(), badBits);
84  }
85  if (fitRegion.getArea() == 0) {
86  throw LSST_EXCEPT(
90  );
91  }
92  typedef afw::detection::Psf::Pixel PsfPixel;
96  fitRegion,
97  psfImage->getArray(),
98  psfImage->getXY0()
99  )
100  );
103  fitRegion,
104  exposure.getMaskedImage().getImage()->getArray(),
105  exposure.getXY0()
106  )
107  );
110  fitRegion,
111  exposure.getMaskedImage().getVariance()->getArray(),
112  exposure.getXY0()
113  )
114  );
115  PsfPixel alpha = model.matrix().squaredNorm();
116  FluxResult result;
117  result.flux = model.matrix().dot(data.matrix().cast<PsfPixel>()) / alpha;
118  // If we're not using per-pixel weights to compute the flux, we'll still want to compute the
119  // variance as if we had, so we'll apply the weights to the model vector now, and update alpha.
120  result.fluxSigma = std::sqrt(model.square().matrix().dot(variance.matrix().cast<PsfPixel>()))
121  / alpha;
122  if (!utils::isfinite(result.flux) || !utils::isfinite(result.fluxSigma)) {
123  throw LSST_EXCEPT(PixelValueError, "Invalid pixel value detected in image.");
124  }
125  measRecord.set(_fluxResultKey, result);
126 }
127 
129  _flagHandler.handleFailure(measRecord, error);
130 }
131 
132 
133 }}} // namespace lsst::meas::base
134 
Defines the fields and offsets for a table.
Definition: Schema.h:46
std::vector< std::string > badMaskPlanes
&quot;Mask planes that indicate pixels that should be excluded from the fit&quot; ;
Definition: PsfFlux.h:49
void flattenArray(Footprint const &fp, ndarray::Array< T, N, C > const &src, ndarray::Array< U, N-1, D > const &dest, lsst::afw::geom::Point2I const &xy0=lsst::afw::geom::Point2I())
Flatten the first two dimensions of an array.
geom::Point2I getXY0() const
Definition: Exposure.h:192
geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
Definition: Exposure.h:194
#define PTR(...)
Definition: base.h:41
Eigen matrix objects that present a view into an ndarray::Array.
Eigen3 view into an ndarray::Array.
Definition: eigen.h:232
A class to contain the data, WCS, and other information needed to describe an image of the sky...
Definition: Exposure.h:48
boost::uint16_t MaskPixel
PixelT Pixel
A pixel in this ImageBase.
Definition: Image.h:133
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=NULL) const
Definition: PsfFlux.cc:128
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
An integer coordinate rectangle.
Definition: Box.h:53
FlagDefinition getDefinition(int i) const
Definition: FlagHandler.h:66
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:150
tbl::Schema schema
Definition: CoaddPsf.cc:324
Matrix alpha
math::Kernel::Pixel Pixel
Pixel type of Image returned by computeImage.
Definition: Psf.h:78
Flux flux
Measured flux in DN.
Definition: FluxUtilities.h:37
PsfFluxAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
Definition: PsfFlux.cc:36
void setValue(afw::table::BaseRecord &record, int i, bool value) const
Definition: FlagHandler.h:72
A FunctorKey for FluxResult.
Definition: FluxUtilities.h:55
A set of pixels in an Image.
Definition: Footprint.h:73
int isfinite(T t)
Definition: ieee.h:100
void intersectMask(image::Mask< MaskPixelT > const &mask, MaskPixelT bitmask=~0x0)
Intersect the Footprint with a Mask.
boost::shared_ptr< lsst::afw::detection::Psf > getPsf()
Return the Exposure&#39;s Psf object.
Definition: Exposure.h:224
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Definition: PsfFlux.cc:54
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
SafeCentroidExtractor _centroidExtractor
Definition: PsfFlux.h:102
lsst::afw::image::VariancePixel VarPixel
Definition: convCUDA.h:44
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
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
A polymorphic base class for representing an image&#39;s Point Spread Function.
Definition: Psf.h:68
FluxErrElement fluxSigma
1-Sigma error (sqrt of variance) on flux in DN.
Definition: FluxUtilities.h:38
A C++ control class to handle PsfFluxAlgorithm&#39;s configuration.
Definition: PsfFlux.h:45
static FlagHandler addFields(afw::table::Schema &schema, std::string const &prefix, FlagDefinition const *begin, FlagDefinition const *end)
Definition: FlagHandler.cc:28
A reusable result struct for flux measurements.
Definition: FluxUtilities.h:36