28 #include "ndarray/eigen.h" 30 #include "lsst/afw/table/Source.h" 31 #include "lsst/afw/detection/Psf.h" 32 #include "lsst/log/Log.h" 33 #include "lsst/afw/geom/SpanSet.h" 36 namespace lsst {
namespace meas {
namespace base {
38 FlagDefinitionList flagDefinitions;
42 FlagDefinition
const PsfFluxAlgorithm::NO_GOOD_PIXELS = flagDefinitions.add(
"flag_noGoodPixels",
"not enough non-rejected pixels in data to attempt the fit");
43 FlagDefinition
const PsfFluxAlgorithm::EDGE = flagDefinitions.add(
"flag_edge",
"object was too close to the edge of the image to use the full PSF model");
46 return flagDefinitions;
57 std::string
const & name,
58 afw::table::Schema & schema,
59 std::string
const & logName
62 FluxResultKey::addFields(schema, name,
"flux derived from linear least-squares fit of PSF model")
64 _centroidExtractor(schema, name)
66 _logName = logName.size() ? logName : name;
72 afw::table::SourceRecord & measRecord,
73 afw::image::Exposure<float>
const & exposure
75 PTR(afw::detection::Psf
const) psf = exposure.getPsf();
77 LOGL_ERROR(
getLogName(),
"PsfFlux: no psf attached to exposure");
80 "PsfFlux algorithm requires a Psf with every exposure" 83 afw::geom::Point2D position = _centroidExtractor(measRecord, _flagHandler);
84 PTR(afw::detection::Psf::Image) psfImage = psf->computeImage(position);
85 afw::geom::Box2I fitBBox = psfImage->getBBox();
86 fitBBox.clip(exposure.getBBox());
87 if (fitBBox != psfImage->getBBox()) {
91 auto fitRegionSpans = std::make_shared<afw::geom::SpanSet>(fitBBox);
92 afw::detection::Footprint fitRegion(fitRegionSpans);
94 afw::image::MaskPixel badBits = 0x0;
96 std::vector<std::string>::const_iterator i = _ctrl.
badMaskPlanes.begin();
100 badBits |= exposure.getMaskedImage().getMask()->getPlaneBitMask(*i);
102 fitRegion.setSpans(fitRegion.getSpans()->intersectNot(*exposure.getMaskedImage().getMask(),
103 badBits)->clippedTo(exposure.getMaskedImage().getMask()->getBBox()));
105 if (fitRegion.getArea() == 0) {
112 typedef afw::detection::Psf::Pixel PsfPixel;
113 typedef afw::image::MaskedImage<float>::Variance::Pixel VarPixel;
114 ndarray::EigenView<PsfPixel,1,1,Eigen::ArrayXpr> model(
115 fitRegion.getSpans()->flatten(psfImage->getArray(), psfImage->getXY0())
117 ndarray::EigenView<float,1,1,Eigen::ArrayXpr> data(
118 fitRegion.getSpans()->flatten(exposure.getMaskedImage().getImage()->getArray(), exposure.getXY0())
120 ndarray::EigenView<VarPixel,1,1,Eigen::ArrayXpr> variance(
121 fitRegion.getSpans()->flatten(exposure.getMaskedImage().getVariance()->getArray(),
124 PsfPixel
alpha = model.matrix().squaredNorm();
126 result.
flux = model.matrix().dot(data.matrix().cast<PsfPixel>()) /
alpha;
129 result.
fluxSigma = std::sqrt(model.square().matrix().dot(variance.matrix().cast<PsfPixel>()))
131 if (!std::isfinite(result.
flux) || !std::isfinite(result.
fluxSigma)) {
132 throw LSST_EXCEPT(
PixelValueError,
"Invalid pixel value detected in image.");
134 measRecord.set(_fluxResultKey, result);
143 std::string
const & name,
144 afw::table::SchemaMapper & mapper
150 if (flag == PsfFluxAlgorithm::FAILURE)
continue;
151 if (mapper.getInputSchema().getNames().count(
152 mapper.getInputSchema().join(name, flag.
name)) == 0)
continue;
153 afw::table::Key<afw::table::Flag> key = mapper.getInputSchema().find<afw::table::Flag>(
154 name +
"_" + flag.
name).key;
155 mapper.addMapping(key);
std::size_t size() const
return the current size (number of defined elements) of the collection
std::vector< std::string > badMaskPlanes
"Mask planes that indicate pixels that should be excluded from the fit" ;
static FlagDefinition const NO_GOOD_PIXELS
static FlagDefinitionList const & getFlagDefinitions()
Exception to be thrown when a measurement algorithm encounters a NaN or infinite pixel.
PsfFluxAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema, std::string const &logName="")
static FlagDefinition const EDGE
Simple class used to define and document flags The name and doc constitute the identity of the FlagDe...
void setValue(afw::table::BaseRecord &record, std::size_t i, bool value) const
Set the flag field corresponding to the given flag index.
Exception to be thrown when a measurement algorithm experiences a known failure mode.
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=nullptr) const
Handle an exception thrown by the current algorithm by setting flags in the given record...
Exception to be thrown when a measurement algorithm experiences a fatal error.
static FlagDefinition const FAILURE
std::string getLogName() const
Flux flux
Measured flux in DN.
A FunctorKey for FluxResult.
static FlagHandler addFields(afw::table::Schema &schema, std::string const &prefix, FlagDefinitionList const &flagDefs, FlagDefinitionList const &exclDefs=FlagDefinitionList::getEmptyList())
Add Flag fields to a schema, creating a FlagHandler object to manage them.
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=nullptr) const
Handle an expected or unexpected Exception thrown by a measurement algorithm.
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Called to measure a single child source in an image.
vector-type utility class to build a collection of FlagDefinitions
FluxErrElement fluxSigma
1-Sigma error (sqrt of variance) on flux in DN.
A C++ control class to handle PsfFluxAlgorithm's configuration.
A reusable result struct for flux measurements.