lsst.afw  16.0-43-gaa65a4573+1
PhotoCalib.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2017 LSST Corporation.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #include <cmath>
24 
25 #include "lsst/geom/Point.h"
28 #include "lsst/afw/table/Source.h"
32 #include "lsst/pex/exceptions.h"
33 #include "ndarray.h"
35 
36 namespace lsst {
37 namespace afw {
38 
41 
42 namespace image {
43 
44 // ------------------- helpers -------------------
45 
46 namespace {
47 
48 double toMaggies(double instFlux, double scale) { return instFlux * scale; }
49 
50 double toMagnitude(double instFlux, double scale) { return -2.5 * log10(instFlux * scale); }
51 
52 double toMaggiesErr(double instFlux, double instFluxErr, double scale, double scaleErr, double maggies) {
53  return maggies * hypot(instFluxErr / instFlux, scaleErr / scale);
54 }
55 
56 double toMagnitudeErr(double instFlux, double instFluxErr, double scale, double scaleErr) {
57  return 2.5 / log(10.0) * hypot(instFluxErr / instFlux, scaleErr / scale);
58 }
59 
60 } // anonymous namespace
61 
62 // ------------------- Conversions to Maggies -------------------
63 
64 double PhotoCalib::instFluxToMaggies(double instFlux, lsst::geom::Point<double, 2> const &point) const {
65  if (_isConstant)
66  return toMaggies(instFlux, _calibrationMean);
67  else
68  return toMaggies(instFlux, _calibration->evaluate(point));
69 }
70 
71 double PhotoCalib::instFluxToMaggies(double instFlux) const { return toMaggies(instFlux, _calibrationMean); }
72 
73 Measurement PhotoCalib::instFluxToMaggies(double instFlux, double instFluxErr,
74  lsst::geom::Point<double, 2> const &point) const {
75  double calibration, err, maggies;
76  if (_isConstant)
77  calibration = _calibrationMean;
78  else
79  calibration = _calibration->evaluate(point);
80  maggies = toMaggies(instFlux, calibration);
81  err = toMaggiesErr(instFlux, instFluxErr, calibration, _calibrationErr, maggies);
82  return Measurement(maggies, err);
83 }
84 
85 Measurement PhotoCalib::instFluxToMaggies(double instFlux, double instFluxErr) const {
86  double maggies = toMaggies(instFlux, _calibrationMean);
87  double err = toMaggiesErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr, maggies);
88  return Measurement(maggies, err);
89 }
90 
91 Measurement PhotoCalib::instFluxToMaggies(afw::table::SourceRecord const &sourceRecord,
92  std::string const &instFluxField) const {
93  auto position = sourceRecord.getCentroid();
94  auto instFluxKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFlux").key;
95  auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFluxErr").key;
96  return instFluxToMaggies(sourceRecord.get(instFluxKey), sourceRecord.get(instFluxErrKey), position);
97 }
98 ndarray::Array<double, 2, 2> PhotoCalib::instFluxToMaggies(afw::table::SourceCatalog const &sourceCatalog,
99  std::string const &instFluxField) const {
100  ndarray::Array<double, 2, 2> result =
101  ndarray::allocate(ndarray::makeVector(int(sourceCatalog.size()), 2));
102  instFluxToMaggiesArray(sourceCatalog, instFluxField, result);
103  return result;
104 }
105 
106 void PhotoCalib::instFluxToMaggies(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
107  std::string const &outField) const {
108  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
109  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
110  auto maggiesKey = sourceCatalog.getSchema().find<double>(outField + "_instFlux").key;
111  auto maggiesErrKey = sourceCatalog.getSchema().find<double>(outField + "_instFluxErr").key;
112  for (auto &record : sourceCatalog) {
113  auto result =
114  instFluxToMaggies(record.get(instFluxKey), record.get(instFluxErrKey), record.getCentroid());
115  record.set(maggiesKey, result.value);
116  record.set(maggiesErrKey, result.err);
117  }
118 }
119 
120 // ------------------- Conversions to Magnitudes -------------------
121 
122 double PhotoCalib::instFluxToMagnitude(double instFlux, lsst::geom::Point<double, 2> const &point) const {
123  if (_isConstant)
124  return toMagnitude(instFlux, _calibrationMean);
125  else
126  return toMagnitude(instFlux, _calibration->evaluate(point));
127 }
128 
129 double PhotoCalib::instFluxToMagnitude(double instFlux) const {
130  return toMagnitude(instFlux, _calibrationMean);
131 }
132 
133 Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr,
134  lsst::geom::Point<double, 2> const &point) const {
135  double calibration, err, magnitude;
136  if (_isConstant)
137  calibration = _calibrationMean;
138  else
139  calibration = _calibration->evaluate(point);
140  magnitude = toMagnitude(instFlux, calibration);
141  err = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
142  return Measurement(magnitude, err);
143 }
144 
145 Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr) const {
146  double magnitude = toMagnitude(instFlux, _calibrationMean);
147  double err = toMagnitudeErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr);
148  return Measurement(magnitude, err);
149 }
150 
151 Measurement PhotoCalib::instFluxToMagnitude(afw::table::SourceRecord const &sourceRecord,
152  std::string const &instFluxField) const {
153  auto position = sourceRecord.getCentroid();
154  auto instFluxKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFlux").key;
155  auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFluxErr").key;
156  return instFluxToMagnitude(sourceRecord.get(instFluxKey), sourceRecord.get(instFluxErrKey), position);
157 }
158 
159 ndarray::Array<double, 2, 2> PhotoCalib::instFluxToMagnitude(afw::table::SourceCatalog const &sourceCatalog,
160  std::string const &instFluxField) const {
161  ndarray::Array<double, 2, 2> result =
162  ndarray::allocate(ndarray::makeVector(int(sourceCatalog.size()), 2));
163  instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
164  return result;
165 }
166 
167 void PhotoCalib::instFluxToMagnitude(afw::table::SourceCatalog &sourceCatalog,
168  std::string const &instFluxField, std::string const &outField) const {
169  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
170  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
171  auto magKey = sourceCatalog.getSchema().find<double>(outField + "_mag").key;
172  auto magErrKey = sourceCatalog.getSchema().find<double>(outField + "_magErr").key;
173  for (auto &record : sourceCatalog) {
174  auto result = instFluxToMagnitude(record.get(instFluxKey), record.get(instFluxErrKey),
175  record.getCentroid());
176  record.set(magKey, result.value);
177  record.set(magErrKey, result.err);
178  }
179 }
180 
181 // ------------------- other utility methods -------------------
182 
183 double PhotoCalib::magnitudeToInstFlux(double magnitude) const {
184  return pow(10, magnitude / -2.5) / _calibrationMean;
185 }
186 
187 std::shared_ptr<math::BoundedField> PhotoCalib::computeScaledCalibration() const {
188  return *(_calibration) / _calibrationMean;
189 }
190 
192  throw LSST_EXCEPT(pex::exceptions::LogicError, "Not Implemented: See DM-10154.");
193 }
194 
195 bool PhotoCalib::operator==(PhotoCalib const &rhs) const {
196  return (_calibrationMean == rhs._calibrationMean && _calibrationErr == rhs._calibrationErr &&
197  (*_calibration) == *(rhs._calibration));
198 }
199 
200 double PhotoCalib::computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const {
201  return calibration->mean();
202 }
203 
205  if (photoCalib._isConstant)
206  os << "spatially constant with ";
207  else
208  os << *(photoCalib._calibration) << " with ";
209  return os << "mean: " << photoCalib._calibrationMean << " err: " << photoCalib._calibrationErr;
210 }
211 
212 // ------------------- persistence -------------------
213 
214 namespace {
215 
216 class PhotoCalibSchema {
217 public:
218  table::Schema schema;
219  table::Key<double> calibrationMean;
220  table::Key<double> calibrationErr;
221  table::Key<table::Flag> isConstant;
222  table::Key<int> field;
223 
224  // No copying
225  PhotoCalibSchema(PhotoCalibSchema const &) = delete;
226  PhotoCalibSchema &operator=(PhotoCalibSchema const &) = delete;
227  // No moving
228  PhotoCalibSchema(PhotoCalibSchema &&) = delete;
229  PhotoCalibSchema &operator=(PhotoCalibSchema &&) = delete;
230 
231  static PhotoCalibSchema const &get() {
232  static PhotoCalibSchema const instance;
233  return instance;
234  }
235 
236 private:
237  PhotoCalibSchema()
238  : schema(),
239  calibrationMean(schema.addField<double>(
240  "calibrationMean", "mean calibration on this PhotoCalib's domain", "count")),
242  schema.addField<double>("calibrationErr", "1-sigma error on calibrationMean", "count")),
243  isConstant(schema.addField<table::Flag>("isConstant", "Is this spatially-constant?")),
244  field(schema.addField<int>("field", "archive ID of the BoundedField object")) {
245  schema.getCitizen().markPersistent();
246  }
247 };
248 
249 class PhotoCalibFactory : public table::io::PersistableFactory {
250 public:
251  PTR(table::io::Persistable)
252  read(InputArchive const &archive, CatalogVector const &catalogs) const override {
253  table::BaseRecord const &record = catalogs.front().front();
254  PhotoCalibSchema const &keys = PhotoCalibSchema::get();
255  return std::make_shared<PhotoCalib>(record.get(keys.calibrationMean), record.get(keys.calibrationErr),
256  archive.get<afw::math::BoundedField>(record.get(keys.field)),
257  record.get(keys.isConstant));
258  }
259 
260  PhotoCalibFactory(std::string const &name) : afw::table::io::PersistableFactory(name) {}
261 };
262 
263 std::string getPhotoCalibPersistenceName() { return "PhotoCalib"; }
264 
265 PhotoCalibFactory registration(getPhotoCalibPersistenceName());
266 
267 } // anonymous namespace
268 
269 std::string PhotoCalib::getPersistenceName() const { return getPhotoCalibPersistenceName(); }
270 
271 void PhotoCalib::write(OutputArchiveHandle &handle) const {
272  PhotoCalibSchema const &keys = PhotoCalibSchema::get();
273  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
274  auto record = catalog.addNew();
275  record->set(keys.calibrationMean, _calibrationMean);
276  record->set(keys.calibrationErr, _calibrationErr);
277  record->set(keys.isConstant, _isConstant);
278  record->set(keys.field, handle.put(_calibration));
279  handle.saveCatalog(catalog);
280 }
281 
282 // ------------------- private/protected helpers -------------------
283 
284 void PhotoCalib::instFluxToMaggiesArray(afw::table::SourceCatalog const &sourceCatalog,
285  std::string const &instFluxField,
286  ndarray::Array<double, 2, 2> result) const {
287  double instFlux, instFluxErr, maggies, calibration;
288  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
289  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
290  auto iter = result.begin();
291  for (auto const &rec : sourceCatalog) {
292  instFlux = rec.get(instFluxKey);
293  instFluxErr = rec.get(instFluxErrKey);
294  if (_isConstant)
295  calibration = _calibrationMean;
296  else
297  calibration = _calibration->evaluate(rec.getCentroid());
298  maggies = toMaggies(instFlux, calibration);
299  (*iter)[0] = maggies;
300  (*iter)[1] = toMaggiesErr(instFlux, instFluxErr, calibration, _calibrationErr, maggies);
301  iter++;
302  }
303 }
304 
305 void PhotoCalib::instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
306  std::string const &instFluxField,
307  ndarray::Array<double, 2, 2> result) const {
308  double instFlux, instFluxErr, calibration;
309  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
310  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
311  auto iter = result.begin();
312  for (auto const &rec : sourceCatalog) {
313  instFlux = rec.get(instFluxKey);
314  instFluxErr = rec.get(instFluxErrKey);
315  if (_isConstant)
316  calibration = _calibrationMean;
317  else
318  calibration = _calibration->evaluate(rec.getCentroid());
319  (*iter)[0] = toMagnitude(instFlux, calibration);
320  (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
321  iter++;
322  }
323 }
324 } // namespace image
325 } // namespace afw
326 } // namespace lsst
table::Key< double > calibrationErr
Definition: PhotoCalib.cc:220
def keys(self)
int put(Persistable const *obj, bool permissive=false)
Save an object to the archive and return a unique ID that can be used to retrieve it from an InputArc...
An object passed to Persistable::write to allow it to persist itself.
The photometric calibration of an exposure.
Definition: PhotoCalib.h:81
table::Key< table::Flag > isConstant
Definition: PhotoCalib.cc:221
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:114
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18
bool operator==(CoordKey const &lhs, CoordKey const &rhs)
Compare CoordKeys for equality using the constituent Keys.
A value and its error.
Definition: PhotoCalib.h:44
#define PTR(...)
ItemVariant const * other
Definition: Schema.cc:55
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
Definition: BaseRecord.h:125
T log10(T... args)
table::Key< double > calibrationMean
Definition: PhotoCalib.cc:219
STL class.
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition: Schema.cc:655
A base class for image defects.
log
Key< U > key
Definition: Schema.cc:280
Schema getSchema() const
Return the Schema that holds this record&#39;s fields and keys.
Definition: BaseRecord.h:54
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:194
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
table::Schema schema
Definition: PhotoCalib.cc:218
std::ostream & operator<<(std::ostream &os, PhotoCalib const &photoCalib)
Definition: PhotoCalib.cc:204
T hypot(T... args)
#define LSST_EXCEPT(type,...)
Record class that contains measurements made on a single exposure.
Definition: Source.h:82
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
std::ostream * os
Definition: Schema.cc:737
STL class.
Implementation of the Photometric Calibration class.
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
Definition: Source.h:644
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Definition: Catalog.h:472
table::Key< int > field
Definition: PhotoCalib.cc:222