LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
InputUtilities.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2014 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 "lsst/utils/ieee.h"
25 #include "lsst/afw/table/Source.h"
29 
30 namespace lsst { namespace meas { namespace base {
31 
34  std::string const & name,
35  bool isCentroider
36 ) :
37  _name(name),
38  _isCentroider(isCentroider)
39 {
40  // Instead of aliasing e.g. MyAlgorithm_flag_badCentroid->slot_Centroid_flag, we actually
41  // look up the target of slot_Centroid_flag, and alias that to MyAlgorithm_flag_badCentroid.
42  // That way, if someone changes the slots later, after we've already done the measurement,
43  // this alias still points to the right thing.
44  std::string slotFlagName = schema.getAliasMap()->apply(schema.join("slot", "Centroid", "flag"));
45  if (_isCentroider) {
46  if (slotFlagName != schema.join(name, "flag")) {
47  // only setup the alias if this isn't the slot algorithm itself (otherwise it'd be circular)
48  schema.getAliasMap()->set(schema.join(name, "flag", "badInitialCentroid"), slotFlagName);
49  }
50  } else {
51  schema.getAliasMap()->set(schema.join(name, "flag", "badCentroid"), slotFlagName);
52  }
53 }
54 
55 namespace {
56 
57 afw::geom::Point2D extractPeak(afw::table::SourceRecord const & record, std::string const & name) {
58  afw::geom::Point2D result;
59  PTR(afw::detection::Footprint) footprint = record.getFootprint();
60  if (!footprint) {
61  throw LSST_EXCEPT(
62  pex::exceptions::RuntimeError,
63  (boost::format("%s: Centroid slot value is NaN, but no Footprint attached to record")
64  % name).str()
65  );
66  }
67  if (footprint->getPeaks().empty()) {
68  throw LSST_EXCEPT(
69  pex::exceptions::RuntimeError,
70  (boost::format("%s: Centroid slot value is NaN, but Footprint has no Peaks")
71  % name).str()
72  );
73  }
74  result.setX(footprint->getPeaks().front()->getFx());
75  result.setY(footprint->getPeaks().front()->getFy());
76  return result;
77 }
78 
79 } // anonymous
80 
82  afw::table::SourceRecord & record,
83  FlagHandler const & flags
84 ) const {
85  if (!record.getTable()->getCentroidKey().isValid()) {
86  if (_isCentroider) {
87  return extractPeak(record, _name);
88  } else {
89  throw LSST_EXCEPT(
90  FatalAlgorithmError,
91  (boost::format("%s requires a centroid, but the centroid slot is not defined") % _name).str()
92  );
93  }
94  }
95  afw::geom::Point2D result = record.getCentroid();
96  if (utils::isnan(result.getX()) || utils::isnan(result.getY())) {
97  if (!record.getTable()->getCentroidFlagKey().isValid()) {
98  if (_isCentroider) {
99  return extractPeak(record, _name);
100  } else {
101  throw LSST_EXCEPT(
102  pex::exceptions::RuntimeError,
103  (boost::format("%s: Centroid slot value is NaN, but there is no Centroid slot flag "
104  "(is the executionOrder for %s lower than that of the slot Centroid?)")
105  % _name % _name).str()
106  );
107  }
108  }
109  if (!record.getCentroidFlag() && !_isCentroider) {
110  throw LSST_EXCEPT(
111  pex::exceptions::RuntimeError,
112  (boost::format("%s: Centroid slot value is NaN, but the Centroid slot flag is not set "
113  "(is the executionOrder for %s lower than that of the slot Centroid?)")
114  % _name % _name).str()
115  );
116  }
117  result = extractPeak(record, _name);
118  if (!_isCentroider) {
119  // set the general flag, because using the Peak might affect the current measurement
120  flags.setValue(record, FlagHandler::FAILURE, true);
121  }
122  } else if (!_isCentroider && record.getTable()->getCentroidFlagKey().isValid()
123  && record.getCentroidFlag()) {
124  // we got a usable value, but the centroid flag is still be set, and that might affect
125  // the current measurement
126  flags.setValue(record, FlagHandler::FAILURE, true);
127  }
128  return result;
129 }
130 
132  _name(name)
133 {
134  // Instead of aliasing e.g. MyAlgorithm_flag_badShape->slot_Shape_flag, we actually
135  // look up the target of slot_Shape_flag, and alias that to MyAlgorithm_flag_badCentroid.
136  // That way, if someone changes the slots later, after we've already done the measurement,
137  // this alias still points to the right thing.
138  std::string slotFlagName = schema.getAliasMap()->apply(schema.join("slot", "Shape", "flag"));
139  schema.getAliasMap()->set(schema.join(name, "flag", "badShape"), slotFlagName);
140 }
141 
143  afw::table::SourceRecord & record,
144  FlagHandler const & flags
145 ) const {
146  if (!record.getTable()->getShapeKey().isValid()) {
147  throw LSST_EXCEPT(
148  FatalAlgorithmError,
149  (boost::format("%s requires a shape, but the shape slot is not defined") % _name).str()
150  );
151  }
152  afw::geom::ellipses::Quadrupole result = record.getShape();
153  if (utils::isnan(result.getIxx()) || utils::isnan(result.getIyy()) || utils::isnan(result.getIxy())
154  || result.getDeterminant() < 0
155  ) {
156  if (!record.getTable()->getShapeFlagKey().isValid()) {
157  throw LSST_EXCEPT(
158  pex::exceptions::RuntimeError,
159  (boost::format("%s: Shape slot value is NaN, but there is no Shape slot flag "
160  "(is the executionOrder for %s lower than that of the slot Shape?)")
161  % _name % _name).str()
162  );
163  }
164  if (!record.getShapeFlag()) {
165  throw LSST_EXCEPT(
166  pex::exceptions::RuntimeError,
167  (boost::format("%s: Shape slot value is NaN, but the Shape slot flag is not set "
168  "(is the executionOrder for %s lower than that of the slot Shape?)")
169  % _name % _name).str()
170  );
171  }
172  throw LSST_EXCEPT(
174  (boost::format("%s: Shape needed, and Shape slot measurement failed.") % _name).str(),
176  );
177  } else if (record.getTable()->getShapeFlagKey().isValid() && record.getShapeFlag()) {
178  // we got a usable value, but the shape flag might still be set, and that might affect
179  // the current measurement
180  flags.setValue(record, FlagHandler::FAILURE, true);
181  }
182  return result;
183 }
184 
185 }}} // lsst::meas::base
An ellipse core with quadrupole moments as parameters.
Definition: Quadrupole.h:45
Defines the fields and offsets for a table.
Definition: Schema.h:46
bool getShapeFlag() const
Return true if the measurement in the Shape slot failed.
Definition: Source.h:829
afw::geom::ellipses::Quadrupole operator()(afw::table::SourceRecord &record, FlagHandler const &flags) const
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
Definition: Source.h:809
Represent a set of pixels of an arbitrary shape and size.
boost::shared_ptr< SourceTable const > getTable() const
Definition: Source.h:93
std::string const & _name
Definition: Mask.cc:677
boost::shared_ptr< AliasMap > getAliasMap() const
Definition: Schema.h:258
double const getIyy() const
Definition: Quadrupole.h:59
#define PTR(...)
Definition: base.h:41
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
A coordinate class intended to represent absolute positions.
Definition: PSF.h:39
bool getCentroidFlag() const
Return true if the measurement in the Centroid slot failed.
Definition: Source.h:817
tbl::Schema schema
Definition: CoaddPsf.cc:324
SafeShapeExtractor(afw::table::Schema &schema, std::string const &name)
if(width!=gim.getWidth()||height!=gim.getHeight()||x0!=gim.getX0()||y0!=gim.getY0())
Definition: saturated.cc:47
void setValue(afw::table::BaseRecord &record, int i, bool value) const
Definition: FlagHandler.h:72
double const getIxx() const
Definition: Quadrupole.h:56
ShapeSlotDefinition::MeasValue getShape() const
Get the value of the Shape slot measurement.
Definition: Source.h:821
A set of pixels in an Image.
Definition: Footprint.h:70
SafeCentroidExtractor(afw::table::Schema &schema, std::string const &name, bool isCentroider=false)
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema&#39;s version.
afw::geom::Point2D operator()(afw::table::SourceRecord &record, FlagHandler const &flags) const
double getDeterminant() const
Return the determinant of the matrix representation.
Definition: Quadrupole.h:85
double const getIxy() const
Definition: Quadrupole.h:62
Record class that contains measurements made on a single exposure.
Definition: Source.h:81