28#include "ndarray/eigen.h"
41template <
typename MaskedImageT>
44 explicit FootprintBits() : _anyBits(0), _allBits(~static_cast<typename MaskedImageT::Mask::Pixel>(0x0)) {}
49 _allBits = ~static_cast<typename MaskedImageT::Mask::Pixel>(0x0);
52 void operator()(
geom::Point2I const& point,
typename MaskedImageT::Mask::Pixel
const& value) {
58 typename MaskedImageT::Mask::Pixel getAnyBits()
const {
return _anyBits; }
60 typename MaskedImageT::Mask::Pixel getAllBits()
const {
return _allBits; }
63 typename MaskedImageT::Mask::Pixel _anyBits;
64 typename MaskedImageT::Mask::Pixel _allBits;
67typedef afw::image::MaskedImage<float> MaskedImageF;
71 const FootprintBits<MaskedImageF>& func, afw::table::SourceRecord& measRecord) {
72 for (
auto const& i : maskFlagToPixelFlag) {
74 if (func.getAnyBits() & MaskedImageF::Mask::getPlaneBitMask(i.first)) {
75 measRecord.set(i.second,
true);
77 }
catch (pex::exceptions::InvalidParameterError& err) {
85 const FootprintBits<MaskedImageF>& func, afw::table::SourceRecord& measRecord) {
86 for (
auto const& i : maskFlagToPixelFlag) {
88 if (func.getAllBits() & MaskedImageF::Mask::getPlaneBitMask(i.first)) {
89 measRecord.set(i.second,
true);
91 }
catch (pex::exceptions::InvalidParameterError& err) {
103 _generalFailureKey =
schema.addField<afw::table::Flag>(
104 name +
"_flag",
"General failure flag, set if anything went wrong");
106 schema.addField<afw::table::Flag>(name +
"_flag" +
"_offimage",
"Source center is off image");
108 _anyKeys[
"EDGE"] =
schema.addField<afw::table::Flag>(
110 "Source is outside usable exposure region (masked EDGE or NO_DATA, or centroid off image).");
111 _anyKeys[
"NO_DATA"] = _anyKeys.
at(
"EDGE");
112 _anyKeys[
"INTRP"] =
schema.addField<afw::table::Flag>(name +
"_flag_interpolated",
113 "Interpolated pixel in the Source footprint");
114 _anyKeys[
"SAT"] =
schema.addField<afw::table::Flag>(name +
"_flag_saturated",
115 "Saturated pixel in the Source footprint");
117 schema.addField<afw::table::Flag>(name +
"_flag_cr",
"Cosmic ray in the Source footprint");
119 schema.addField<afw::table::Flag>(name +
"_flag_bad",
"Bad pixel in the Source footprint");
120 _anyKeys[
"SUSPECT"] =
schema.addField<afw::table::Flag>(name +
"_flag_suspect",
121 "Source's footprint includes suspect pixels");
123 _centerKeys[
"EDGE"] =
schema.addField<afw::table::Flag>(
124 name +
"_flag_edgeCenter",
"EDGE or NO_DATA Pixel in the 3x3 region around the centroid.");
125 _centerKeys[
"NO_DATA"] = _centerKeys.
at(
"EDGE");
126 _centerKeys[
"INTRP"] =
schema.addField<afw::table::Flag>(
127 name +
"_flag_interpolatedCenter",
"Interpolated pixel in the 3x3 region around the centroid.");
128 _centerKeys[
"SAT"] =
schema.addField<afw::table::Flag>(
129 name +
"_flag_saturatedCenter",
"Saturated pixel in the 3x3 region around the centroid.");
130 _centerKeys[
"CR"] =
schema.addField<afw::table::Flag>(
131 name +
"_flag_crCenter",
"Cosmic ray in the 3x3 region around the centroid.");
132 _centerKeys[
"BAD"] =
schema.addField<afw::table::Flag>(name +
"_flag_badCenter",
133 "Bad pixel in the 3x3 region around the centroid");
134 _centerKeys[
"SUSPECT"] =
schema.addField<afw::table::Flag>(
135 name +
"_flag_suspectCenter",
"Suspect pixel in the 3x3 region around the centroid.");
138 _centerAllKeys[
"EDGE"] =
schema.addField<afw::table::Flag>(
139 name +
"_flag_edgeCenterAll",
140 "All pixels in the 3x3 region around the centroid are marked EDGE or NO_DATA.");
141 _centerAllKeys[
"NO_DATA"] = _centerAllKeys.
at(
"EDGE");
142 _centerAllKeys[
"INTRP"] =
schema.addField<afw::table::Flag>(
143 name +
"_flag_interpolatedCenterAll",
144 "All pixels in the 3x3 region around the centroid are interpolated.");
145 _centerAllKeys[
"SAT"] =
schema.addField<afw::table::Flag>(
146 name +
"_flag_saturatedCenterAll",
147 "All pixels in the 3x3 region around the centroid are saturated.");
148 _centerAllKeys[
"CR"] =
schema.addField<afw::table::Flag>(
149 name +
"_flag_crCenterAll",
150 "All pixels in the 3x3 region around the centroid have the cosmic ray mask bit.");
151 _centerAllKeys[
"BAD"] =
schema.addField<afw::table::Flag>(
152 name +
"_flag_badCenterAll",
"All pixels in the 3x3 region around the centroid are bad.");
153 _centerAllKeys[
"SUSPECT"] =
schema.addField<afw::table::Flag>(
154 name +
"_flag_suspectCenterAll",
"All pixels in the 3x3 region around the centroid are suspect.");
158 std::string maskName(i);
159 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
160 _centerKeys[i] = schema.addField<afw::table::Flag>(
161 name +
"_flag_" + maskName +
"Center",
"3x3 region around the centroid has " + i +
" pixels");
164 std::string maskName(i);
165 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
166 _centerAllKeys[i] = schema.addField<afw::table::Flag>(
167 name +
"_flag_" + maskName +
"CenterAll",
168 "All pixels in the 3x3 region around the source centroid are " + i +
" pixels");
172 std::string maskName(i);
173 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
174 _anyKeys[i] = schema.addField<afw::table::Flag>(name +
"_flag_" + maskName,
175 "Source footprint includes " + i +
" pixels");
182 FootprintBits<MaskedImageF> func;
186 if (measRecord.
getTable()->getCentroidSlot().getMeasKey().isValid()) {
191 measRecord.
set(_generalFailureKey,
true);
200 if (!footprint || footprint->getPeaks().empty()) {
203 center.setX(footprint->getPeaks().front().getFx());
204 center.setY(footprint->getPeaks().front().getFy());
211 if (!
bbox.contains(center)) {
212 measRecord.
set(_offImageKey,
true);
213 measRecord.
set(_anyKeys.
at(
"EDGE"),
true);
214 measRecord.
set(_centerKeys.
at(
"EDGE"),
true);
215 measRecord.
set(_centerAllKeys.
at(
"EDGE"),
true);
222 (boost::format(
"Source id %d has no footprint.") % measRecord.
getId()).str());
224 auto fullSpans = footprint->getSpans();
228 (boost::format(
"Source id %d has no spans in footprint.") % measRecord.
getId()).str());
230 fullSpans->clippedTo(mimage.getBBox())->applyFunctor(func, *(mimage.getMask()));
233 updateFlags(_anyKeys, func, measRecord);
237 (boost::format(
"Centroid of source id %d passed to PixelFlags is non-finite; "
238 "footprint-based flags have been set, but centroid-based flags will not be.") %
250 middle.
getSpans()->clippedTo(mimage.getBBox())->applyFunctor(func, *(mimage.getMask()));
253 updateFlags(_centerKeys, func, measRecord);
254 updateFlagsAll(_centerAllKeys, func, measRecord);
259 measRecord.
set(_generalFailureKey,
true);
#define LSST_EXCEPT(type,...)
This is the algorithm for PixelFlags.
MaskedImageT getMaskedImage()
void set(Key< T > const &key, U const &value)
CentroidSlotDefinition::MeasValue getCentroid() const
std::shared_ptr< SourceTable const > getTable() const
std::shared_ptr< Footprint > getFootprint() const
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.
PixelFlagsAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Called to measure a single child source in an image.
std::map< std::string, afw::table::Key< afw::table::Flag > > KeyMap
A C++ control class to handle PixelFlagsAlgorithm's configuration.
std::vector< std::string > masksFpAnywhere
"List of mask planes to be searched for which occur anywhere within a footprint. " "If any of the pla...
std::vector< std::string > masksFpCenter
"List of mask planes to be searched for which occur in the center of a footprint. " "If any of the pl...
int positionToIndex(double pos)