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
CorrectFluxes.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 "lsst/utils/ieee.h"
26 #include "lsst/afw/detection/Psf.h"
27 
28 namespace lsst { namespace meas { namespace algorithms {
29 
30 namespace {
31 
32 typedef std::pair<afw::table::KeyTuple<afw::table::Flux>,ScaledFlux::KeyTuple> ScaledFluxKeys;
33 
34 // Helper function to get a value from a record and return a default value when the key is invalid.
35 template <typename T>
36 inline typename afw::table::Field<T>::Value get(
37  afw::table::SourceRecord const & record,
38  afw::table::Key<T> const & key,
39  typename afw::table::Field<T>::Value default_ = false
40 ) {
41  return key.isValid() ? record.get(key) : default_;
42 }
43 
44 // Helper functor to set the general flag for a flux measurement
45 struct SetMainFlag {
46 
47  explicit SetMainFlag(afw::table::SourceRecord * record_) : record(record_) {}
48 
49  void operator()(ScaledFluxKeys const & keys) const {
50  if (keys.first.flag.isValid()) {
51  record->set(keys.first.flag, true);
52  }
53  }
54 
55  afw::table::SourceRecord * record;
56 };
57 
58 // Helper functor to apply the aperture correction to a flux measurement
59 struct ApplyApCorr {
60 
61  ApplyApCorr(double apCorr_, afw::table::SourceRecord * record_) : apCorr(apCorr_), record(record_) {}
62 
63  void operator()(ScaledFluxKeys const & keys) const {
64  (*record)[keys.first.meas] *= apCorr;
65  if (keys.first.err.isValid()) {
66  (*record)[keys.first.err] *= apCorr;
67  }
68  }
69 
70  double apCorr;
71  afw::table::SourceRecord * record;
72 };
73 
74 
75 class CorrectFluxes : public Algorithm {
76 public:
77 
78  CorrectFluxes(
79  CorrectFluxesControl const & ctrl,
80  afw::table::Schema & schema,
81  AlgorithmMap const & others
82  );
83 
84 private:
85 
86  void _apply(
87  afw::table::SourceRecord & source,
88  PTR(afw::detection::Psf const) psf,
89  afw::geom::Point2D const & center
90  ) const;
91 
92  template <typename PixelT>
93  void _apply(
94  afw::table::SourceRecord & source,
95  afw::image::Exposure<PixelT> const & exposure,
96  afw::geom::Point2D const & center
97  ) const {
98  return this->_apply(source, exposure.getPsf(), center);
99  }
100 
102 
103  afw::table::Key<float> _apCorrKey;
104  afw::table::Key<afw::table::Flag> _apCorrFlagKey;
105  ScaledFluxKeys _canonical;
106  std::vector<ScaledFluxKeys> _others;
107 };
108 
109 CorrectFluxes::CorrectFluxes(
110  CorrectFluxesControl const & ctrl,
111  afw::table::Schema & schema,
112  AlgorithmMap const & others
113 ) : Algorithm(ctrl) {
114 
115  if (ctrl.doApCorr) {
116  _apCorrKey = schema.addField<float>(
117  ctrl.name + ".apcorr",
118  (boost::format("correction applied to model fluxes to match to %f-pixel aperture")
119  % ctrl.apCorrRadius).str()
120  );
121  _apCorrFlagKey = schema.addField<afw::table::Flag>(
122  ctrl.name + ".apcorr.flags",
123  "flag set if aperture correction failed"
124  );
125  }
126 
127  for (AlgorithmMap::const_iterator i = others.begin(); i != others.end(); ++i) {
128  CONST_PTR(ScaledFlux) asScaledFlux = boost::dynamic_pointer_cast<ScaledFlux const>(i->second);
129  if (i->second->getControl().name == ctrl.canonicalFluxName) {
130  if (!asScaledFlux) {
131  throw LSST_EXCEPT(
132  pex::exceptions::InvalidParameterError,
133  (boost::format("Canonical flux (%s) is not an instance of ScaledFlux")
134  % ctrl.canonicalFluxName).str()
135  );
136  }
137  int fluxCount = asScaledFlux->getFluxCount();
138  if (ctrl.canonicalFluxIndex < 0 || ctrl.canonicalFluxIndex >= fluxCount) {
139  throw LSST_EXCEPT(
140  pex::exceptions::InvalidParameterError,
141  (boost::format("Invalid index (%d) for canonical flux (must be between 0 and %d)")
142  % ctrl.canonicalFluxIndex % (fluxCount - 1)).str()
143  );
144  }
145  for (int i = 0; i < fluxCount; ++i) {
146  if (i == ctrl.canonicalFluxIndex) {
147  _canonical =
148  ScaledFluxKeys(
149  asScaledFlux->getFluxKeys(i),
150  asScaledFlux->getFluxCorrectionKeys(i)
151  );
152  } else {
153  _others.push_back(
154  ScaledFluxKeys(
155  asScaledFlux->getFluxKeys(i),
156  asScaledFlux->getFluxCorrectionKeys(i)
157  )
158  );
159  }
160  }
161  } else if (asScaledFlux) {
162  int fluxCount = asScaledFlux->getFluxCount();
163  for (int i = 0; i < fluxCount; ++i) {
164  _others.push_back(
165  ScaledFluxKeys(
166  asScaledFlux->getFluxKeys(i),
167  asScaledFlux->getFluxCorrectionKeys(i)
168  )
169  );
170  }
171  }
172  } // for i in others
173 
174  if (ctrl.doTieScaledFluxes && !_canonical.first.meas.isValid()) {
175  throw LSST_EXCEPT(
176  pex::exceptions::LogicError,
177  "Cannot tie scaled fluxes without a canonical flux measurement."
178  );
179  }
180 }
181 
182 void CorrectFluxes::_apply(
183  afw::table::SourceRecord & source,
184  PTR(afw::detection::Psf const) psf,
185  afw::geom::Point2D const & center
186 ) const {
187  CorrectFluxesControl const & ctrl = static_cast<CorrectFluxesControl const &>(this->getControl());
188 
189  // We'll call this functor with a ScaledFluxKeys to set the general flag.
190  // Useful to make it a functor so we can pass it to std::for_each.
191  SetMainFlag setMainFlag(&source);
192 
193  if (!ctrl.doApCorr && !ctrl.doTieScaledFluxes) return;
194 
195  if (ctrl.doApCorr) {
196  double apCorr = 1.0;
197  try {
198  source.set(_apCorrFlagKey, true);
199  if (!psf) {
200  throw LSST_EXCEPT(
201  pex::exceptions::LogicError,
202  "Aperture correction requested but no PSF provided"
203  );
204  }
205  double canonicalPsfFactor = get(source, _canonical.second.psfFactor, 1.0);
206  apCorr = psf->computeApertureFlux(ctrl.apCorrRadius, center) / canonicalPsfFactor;
207  if (!lsst::utils::isfinite(apCorr)) {
208  throw LSST_EXCEPT(
209  pex::exceptions::RuntimeError,
210  "Aperture correction is unexpectedly non-finite"
211  );
212  }
213  source.set(_apCorrKey, apCorr);
214  source.set(_apCorrFlagKey, false);
215  } catch (...) {
216  if (ctrl.doFlagApCorrFailures) {
217  setMainFlag(_canonical);
218  std::for_each(_others.begin(), _others.end(), setMainFlag);
219  }
220  throw;
221  }
222  // the next few lines cannot throw, so we don't need to put them in the try block above
223  // (note that this means either all measurements fail aperture correction or none do)
224  ApplyApCorr applyApCorr(apCorr, &source);
225  applyApCorr(_canonical);
226  std::for_each(_others.begin(), _others.end(), applyApCorr);
227  }
228 
229  if (ctrl.doTieScaledFluxes) {
230  bool badCanonicalFlux = get(source, _canonical.first.flag, false);
231  bool badCanonicalPsfFactor = get(source, _canonical.second.psfFactorFlag, false);
232  double canonicalPsfFactor = get(source, _canonical.second.psfFactor, 1.0);
233  if (badCanonicalFlux || badCanonicalPsfFactor) {
234  if (ctrl.doFlagTieFailures) {
235  std::for_each(_others.begin(), _others.end(), setMainFlag);
236  }
237  } else {
238  for (std::vector<ScaledFluxKeys>::const_iterator i = _others.begin(); i != _others.end(); ++i) {
239  // If assertion fails, somebody implemented a ScaledFlux that didn't meet requirements.
240  assert(i->first.meas.isValid());
241  if (i->second.psfFactorFlag.isValid() && source.get(i->second.psfFactorFlag)) {
242  if (ctrl.doFlagTieFailures) {
243  setMainFlag(*i);
244  }
245  } else {
246  double psfFactor = get(source, i->second.psfFactor, 1.0);
247  double scaling = canonicalPsfFactor / psfFactor;
248  source[i->first.meas] *= scaling;
249  if (i->first.err.isValid()) {
250  source[i->first.err] *= scaling;
251  }
252  }
253  }
254  }
255  }
256 }
257 
259 
260 } // anonymous
261 
263  return boost::make_shared<CorrectFluxesControl>(*this);
264 }
265 
267  afw::table::Schema & schema,
268  PTR(daf::base::PropertyList) const & metadata,
269  AlgorithmMap const & others
270 ) const {
271  return boost::make_shared<CorrectFluxes>(*this, boost::ref(schema), others);
272 }
273 
274 }}} // namespace lsst::meas::algorithms
afw::table::Key< afw::table::Flag > _apCorrFlagKey
double apCorr
#define PTR(...)
Definition: base.h:41
#define LSST_MEAS_ALGORITHM_PRIVATE_INTERFACE(CLASS)
Declare Algorithm::_applyT virtual function overloads with the correct types.
Definition: Algorithm.h:60
Class for storing ordered metadata with comments.
Definition: PropertyList.h:81
A control object for a pluggable algorithm that scales fluxes to a common system. ...
Definition: CorrectFluxes.h:52
Point< double, 2 > Point2D
Definition: Point.h:277
ScaledFluxKeys _canonical
afw::table::Key< float > _apCorrKey
Base class for source measurement algorithms.
Definition: Algorithm.h:106
Base class for measurement algorithm control objects.
Definition: Algorithm.h:168
tbl::Schema schema
Definition: CoaddPsf.cc:324
#define LSST_MEAS_ALGORITHM_PRIVATE_IMPLEMENTATION(CLASS)
Implement Algorithm::_applyT virtual function overloads with the correct types.
Definition: Algorithm.h:71
#define CONST_PTR(...)
Definition: base.h:47
int isfinite(T t)
Definition: ieee.h:100
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
std::vector< ScaledFluxKeys > _others
std::map< std::string, boost::shared_ptr< Algorithm const > > AlgorithmMap
Definition: Algorithm.h:79
afw::table::SourceRecord * record