LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
PixelFlags.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"
28 #include "lsst/afw/table/Source.h"
30 
31 namespace lsst { namespace meas { namespace base {
32 namespace {
33 
34 template <typename MaskedImageT>
35 class FootprintBits : public afw::detection::FootprintFunctor<MaskedImageT> {
36 public:
37  explicit FootprintBits(MaskedImageT const& mimage) :
38  afw::detection::FootprintFunctor<MaskedImageT>(mimage), _bits(0)
39  {}
40 
42  void reset() {
43  _bits = 0x0;
44  }
45  virtual void reset(afw::detection::Footprint const&) {}
46 
48  void operator()(typename MaskedImageT::xy_locator loc,
49  int x,
50  int y
51  ) {
52  _bits |= loc.mask(0, 0);
53  }
54 
56  typename MaskedImageT::Mask::Pixel getBits() const { return _bits; }
57 private:
58  typename MaskedImageT::Mask::Pixel _bits;
59 };
60 } // end anonymous namespace
62  Control const & ctrl,
63  std::string const & name,
65 ) : _ctrl(ctrl),
66  _centroidExtractor(schema, name)
67 {
68  static boost::array<FlagDefinition,N_FLAGS> const flagDefs = {{
69  {"flag", "general failure flag, set if anything went wrong"},
70  {"flag_edge", "Source is outside usable exposure region (masked EDGE or NO_DATA)"},
71  {"flag_interpolated", "Interpolated pixel in the source footprint"},
72  {"flag_interpolatedCenter", "Interpolated pixel in the source center"},
73  {"flag_saturated", "Saturated pixel in the source footprint"},
74  {"flag_saturatedCenter", "Saturated pixel in the source center"},
75  {"flag_cr", "Cosmic ray in the source footprint"},
76  {"flag_crCenter", "Cosmic ray in the source center"},
77  {"flag_bad", "Bad pixel in the source footprint"}
78  }};
79  _flagHandler = FlagHandler::addFields(schema, name, flagDefs.begin(), flagDefs.end());
80 }
81 
83  afw::table::SourceRecord & measRecord,
84  afw::image::Exposure<float> const & exposure
85 ) const {
86 
88  typedef afw::image::MaskedImage<float> MaskedImageT;
89  MaskedImageT mimage = exposure.getMaskedImage();
90 
91  FootprintBits<MaskedImageT> func(mimage);
92 
93 // Catch NAN in centroid estimate
94  if (lsst::utils::isnan(center.getX()) || lsst::utils::isnan(center.getY())) {
95  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
96  "Center point passed to PixelFlagsALgorithm is NaN");
97  }
98 // Catch centroids off the image
99  if (!mimage.getBBox().contains(afw::geom::Point2I(center))) {
100  _flagHandler.setValue(measRecord, EDGE, true);
101  }
102  // Check for bits set in the source's Footprint
103  afw::detection::Footprint const & footprint(*measRecord.getFootprint());
104  func.apply(footprint);
105  if (func.getBits() & (MaskedImageT::Mask::getPlaneBitMask("EDGE") |
106  MaskedImageT::Mask::getPlaneBitMask("NO_DATA"))) {
107  _flagHandler.setValue(measRecord, EDGE, true);
108  }
109  if (func.getBits() & MaskedImageT::Mask::getPlaneBitMask("BAD")) {
110  _flagHandler.setValue(measRecord, BAD, true);
111  }
112  if (func.getBits() & MaskedImageT::Mask::getPlaneBitMask("INTRP")) {
113  _flagHandler.setValue(measRecord, INTERPOLATED, true);
114  }
115  if (func.getBits() & MaskedImageT::Mask::getPlaneBitMask("SAT")) {
116  _flagHandler.setValue(measRecord, SATURATED, true);
117  }
118  if (func.getBits() & MaskedImageT::Mask::getPlaneBitMask("CR")) {
119  _flagHandler.setValue(measRecord, CR, true);
120  }
121 
122  // Check for bits set in the 3x3 box around the center
123  afw::geom::Point2I llc(afw::image::positionToIndex(center.getX())-1, afw::image::positionToIndex(center.getY()) - 1);
124 
125  afw::detection::Footprint const middle(afw::geom::BoxI(llc, afw::geom::ExtentI(3))); // central 3x3
126  func.apply(middle);
127  if (func.getBits() & MaskedImageT::Mask::getPlaneBitMask("INTRP")) {
128  _flagHandler.setValue(measRecord, INTERPOLATED_CENTER, true);
129  }
130  if (func.getBits() & MaskedImageT::Mask::getPlaneBitMask("SAT")) {
131  _flagHandler.setValue(measRecord, SATURATED_CENTER, true);
132  }
133  if (func.getBits() & MaskedImageT::Mask::getPlaneBitMask("CR")) {
134  _flagHandler.setValue(measRecord, CR_CENTER, true);
135  }
136  _flagHandler.setValue(measRecord, FAILURE, false);
137 }
138 
140  _flagHandler.handleFailure(measRecord, error);
141 }
142 
143 }}} // namespace lsst::meas::base
144 
int y
Defines the fields and offsets for a table.
Definition: Schema.h:46
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:150
table::Key< std::string > name
Definition: ApCorrMap.cc:71
Eigen matrix objects that present a view into an ndarray::Array.
int positionToIndex(double pos)
Convert image position to nearest integer index.
Definition: ImageUtils.h:69
MaskedImageT::Mask::Pixel _bits
Definition: PixelFlags.cc:58
lsst::afw::detection::Footprint Footprint
Definition: Source.h:61
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
int isnan(T t)
Definition: ieee.h:110
An integer coordinate rectangle.
Definition: Box.h:53
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Definition: PixelFlags.cc:82
def error
Definition: log.py:108
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=NULL) const
Definition: PixelFlags.cc:139
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:77
PixelFlagsAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
Definition: PixelFlags.cc:61
void setValue(afw::table::BaseRecord &record, int i, bool value) const
Definition: FlagHandler.h:72
tbl::Schema schema
A set of pixels in an Image.
Definition: Footprint.h:62
int x
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
A C++ control class to handle PixelFlagsAlgorithm&#39;s configuration.
Definition: PixelFlags.h:43
boost::shared_ptr< Footprint > getFootprint() const
Definition: Source.h:89
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
static FlagHandler addFields(afw::table::Schema &schema, std::string const &prefix, FlagDefinition const *begin, FlagDefinition const *end)
Definition: FlagHandler.cc:28
SafeCentroidExtractor _centroidExtractor
Definition: PixelFlags.h:100