lsst.meas.base  14.0-21-gb9e430a+3
FluxUtilities.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2015 AURA/LSST.
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 
27 
28 namespace lsst { namespace meas { namespace base {
29 
31  flux(std::numeric_limits<Flux>::quiet_NaN()),
32  fluxSigma(std::numeric_limits<FluxErrElement>::quiet_NaN())
33 {}
34 
36  afw::table::Schema & schema,
37  std::string const & name,
38  std::string const & doc
39 ) {
40  FluxResultKey result;
41  result._flux = schema.addField<Flux>(schema.join(name, "flux"), doc, "count");
42  result._fluxSigma = schema.addField<FluxErrElement>(schema.join(name, "fluxSigma"),
43  "1-sigma flux uncertainty", "count");
44  return result;
45 }
46 
48  FluxResult r;
49  r.flux = record.get(_flux);
50  r.fluxSigma = record.get(_fluxSigma);
51  return r;
52 }
53 
54 void FluxResultKey::set(afw::table::BaseRecord & record, FluxResult const & value) const {
55  record.set(_flux, value.flux);
56  record.set(_fluxSigma, value.fluxSigma);
57 }
58 
60  afw::table::Schema & schema,
61  std::string const & name
62 ) {
63  MagResultKey result;
64  result._magKey = schema.addField<Mag>(schema.join(name, "mag"), "Magnitude");
65  result._magErrKey = schema.addField<MagErrElement>(schema.join(name, "magErr"), "Error on magnitude");
66  return result;
67 }
68 
70  MagResult result = {record.get(_magKey), record.get(_magErrKey)};
71  return result;
72 }
73 
74 void MagResultKey::set(afw::table::BaseRecord & record, MagResult const & magResult) const {
75  record.set(_magKey, magResult.mag);
76  record.set(_magErrKey, magResult.magErr);
77 }
78 
79 void MagResultKey::set(afw::table::BaseRecord & record, std::pair<double,double> const & magResult) const {
80  record.set(_magKey, magResult.first);
81  record.set(_magErrKey, magResult.second);
82 }
83 
85  std::string const & name,
87 ) :
88  BaseTransform{name}
89 {
90  // Map the flag through to the output
91  mapper.addMapping(mapper.getInputSchema().find<afw::table::Flag>(name + "_flag").key);
92 
93  // Add keys for the magnitude and associated error
94  _magKey = MagResultKey::addFields(mapper.editOutputSchema(), name);
95 }
96 
98  afw::table::SourceCatalog const & inputCatalog,
99  afw::table::BaseCatalog & outputCatalog,
100  afw::geom::SkyWcs const & wcs,
101  afw::image::Calib const & calib
102 ) const {
103  checkCatalogSize(inputCatalog, outputCatalog);
104  FluxResultKey fluxKey(inputCatalog.getSchema()[_name]);
105  afw::table::SourceCatalog::const_iterator inSrc = inputCatalog.begin();
106  afw::table::BaseCatalog::iterator outSrc = outputCatalog.begin();
107  {
108  // While noThrow is in scope, converting a negative flux to a magnitude
109  // returns NaN rather than throwing.
111  for (; inSrc != inputCatalog.end() && outSrc != outputCatalog.end(); ++inSrc, ++outSrc) {
112  FluxResult fluxResult = fluxKey.get(*inSrc);
113  _magKey.set(*outSrc, calib.getMagnitude(fluxResult.flux, fluxResult.fluxSigma));
114  }
115  }
116 }
117 
119  _throwOnNegative = afw::image::Calib::getThrowOnNegativeFlux();
121 }
122 
125 }
126 
127 }}} // namespace lsst::meas::base
double FluxErrElement
Definition: constants.h:50
std::string join(std::string const &a, std::string const &b) const
double MagErrElement
Definition: constants.h:52
This defines the base of measurement transformations.
A FunctorKey for MagResult.
Temporarily replace negative fluxes with NaNs.
STL namespace.
A reusable result struct for magnitudes.
Key< T > addField(Field< T > const &field, bool doReplace=false)
static FluxResultKey addFields(afw::table::Schema &schema, std::string const &name, std::string const &doc)
Add a pair of _flux, _fluxSigma fields to a Schema, and return a FluxResultKey that points to them...
Field< T >::Value get(Key< T > const &key) const
FluxResult()
Default constructor; initializes everything to NaN.
string name
STL class.
static void setThrowOnNegativeFlux(bool raiseException)
Flux flux
Measured flux in DN.
Definition: FluxUtilities.h:40
virtual void operator()(afw::table::SourceCatalog const &inputCatalog, afw::table::BaseCatalog &outputCatalog, afw::geom::SkyWcs const &wcs, afw::image::Calib const &calib) const
A FunctorKey for FluxResult.
Definition: FluxUtilities.h:58
double getMagnitude(double const flux) const
static MagResultKey addFields(afw::table::Schema &schema, std::string const &name)
Add a pair of _mag, _magErr fields to a Schema, and return a MagResultKey that points to them...
FluxTransform(std::string const &name, afw::table::SchemaMapper &mapper)
Base::const_iterator const_iterator
virtual FluxResult get(afw::table::BaseRecord const &record) const
Get a FluxResult from the given record.
virtual void set(afw::table::BaseRecord &record, MagResult const &magResult) const
Set a MagResult in the given record.
Abstract base class for all C++ measurement transformations.
Definition: Transform.h:83
virtual void set(afw::table::BaseRecord &record, FluxResult const &other) const
Set a FluxResult in the given record.
void set(Key< T > const &key, U const &value)
static bool getThrowOnNegativeFlux()
void checkCatalogSize(afw::table::BaseCatalog const &cat1, afw::table::BaseCatalog const &cat2) const
Ensure that catalogs have the same size.
Definition: Transform.h:102
virtual MagResult get(afw::table::BaseRecord const &record) const
Get a MagResult from the given record.
FluxErrElement fluxSigma
1-Sigma error (sqrt of variance) on flux in DN.
Definition: FluxUtilities.h:41
A reusable result struct for flux measurements.
Definition: FluxUtilities.h:39