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
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  if (!record.getTable()->getShapeFlagKey().isValid()) {
155  throw LSST_EXCEPT(
156  pex::exceptions::RuntimeError,
157  (boost::format("%s: Shape slot value is NaN, but there is no Shape slot flag "
158  "(is the executionOrder for %s lower than that of the slot Shape?)")
159  % _name % _name).str()
160  );
161  }
162  if (!record.getShapeFlag()) {
163  throw LSST_EXCEPT(
164  pex::exceptions::RuntimeError,
165  (boost::format("%s: Shape slot value is NaN, but the Shape slot flag is not set "
166  "(is the executionOrder for %s lower than that of the slot Shape?)")
167  % _name % _name).str()
168  );
169  }
170  throw LSST_EXCEPT(
172  (boost::format("%s: Shape needed, and Shape slot measurement failed.") % _name).str(),
174  );
175  } else if (record.getTable()->getShapeFlagKey().isValid() && record.getShapeFlag()) {
176  // we got a usable value, but the shape flag might still be set, and that might affect
177  // the current measurement
178  flags.setValue(record, FlagHandler::FAILURE, true);
179  }
180  return result;
181 }
182 
183 }}} // 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
afw::geom::ellipses::Quadrupole operator()(afw::table::SourceRecord &record, FlagHandler const &flags) const
#define PTR(...)
Definition: base.h:41
Represent a set of pixels of an arbitrary shape and size.
boost::shared_ptr< AliasMap > getAliasMap() const
Definition: Schema.h:256
std::string const & _name
Definition: Mask.cc:677
double const getIxy() const
Definition: Quadrupole.h:62
ShapeSlotDefinition::MeasValue getShape() const
Get the value of the Shape slot measurement.
Definition: Source.h:821
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.
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
Definition: Source.h:809
tbl::Schema schema
Definition: CoaddPsf.cc:324
SafeShapeExtractor(afw::table::Schema &schema, std::string const &name)
double const getIyy() const
Definition: Quadrupole.h:59
void setValue(afw::table::BaseRecord &record, int i, bool value) const
Definition: FlagHandler.h:72
A set of pixels in an Image.
Definition: Footprint.h:73
SafeCentroidExtractor(afw::table::Schema &schema, std::string const &name, bool isCentroider=false)
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
boost::shared_ptr< SourceTable const > getTable() const
Definition: Source.h:93
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.
bool getCentroidFlag() const
Return true if the measurement in the Centroid slot failed.
Definition: Source.h:817
afw::geom::Point2D operator()(afw::table::SourceRecord &record, FlagHandler const &flags) const
Record class that contains measurements made on a single exposure.
Definition: Source.h:81
double const getIxx() const
Definition: Quadrupole.h:56
afw::table::SourceRecord * record
bool getShapeFlag() const
Return true if the measurement in the Shape slot failed.
Definition: Source.h:829