lsst.afw  16.0-60-g0d536d8fc
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 fromMagnitude(double magnitude, double scale) { return pow(10, magnitude / -2.5) / scale; }
53 
54 double toMaggiesErr(double instFlux, double instFluxErr, double scale, double scaleErr, double maggies) {
55  return maggies * hypot(instFluxErr / instFlux, scaleErr / scale);
56 }
57 
71 void toMaggiesVariance(ndarray::Array<float const, 2, 1> const &instFlux,
72  ndarray::Array<float const, 2, 1> const &instFluxVar, float scaleErr,
73  ndarray::Array<float const, 2, 1> const &maggies, ndarray::Array<float, 2, 1> out) {
74  auto eigenMaggies = ndarray::asEigen<Eigen::ArrayXpr>(maggies);
75  auto eigenInstFluxVar = ndarray::asEigen<Eigen::ArrayXpr>(instFluxVar);
76  auto eigenInstFlux = ndarray::asEigen<Eigen::ArrayXpr>(instFlux);
77  auto eigenOut = ndarray::asEigen<Eigen::ArrayXpr>(out);
78  eigenOut = eigenMaggies.square() * (eigenInstFluxVar / eigenInstFlux.square() +
79  (scaleErr / eigenMaggies * eigenInstFlux).square());
80 }
81 
82 double toMagnitudeErr(double instFlux, double instFluxErr, double scale, double scaleErr) {
83  return 2.5 / log(10.0) * hypot(instFluxErr / instFlux, scaleErr / scale);
84 }
85 
86 } // anonymous namespace
87 
88 // ------------------- Conversions to Maggies -------------------
89 
90 double PhotoCalib::instFluxToMaggies(double instFlux, lsst::geom::Point<double, 2> const &point) const {
91  return toMaggies(instFlux, evaluate(point));
92 }
93 
94 double PhotoCalib::instFluxToMaggies(double instFlux) const { return toMaggies(instFlux, _calibrationMean); }
95 
96 Measurement PhotoCalib::instFluxToMaggies(double instFlux, double instFluxErr,
97  lsst::geom::Point<double, 2> const &point) const {
98  double calibration, err, maggies;
99  calibration = evaluate(point);
100  maggies = toMaggies(instFlux, calibration);
101  err = toMaggiesErr(instFlux, instFluxErr, calibration, _calibrationErr, maggies);
102  return Measurement(maggies, err);
103 }
104 
105 Measurement PhotoCalib::instFluxToMaggies(double instFlux, double instFluxErr) const {
106  double maggies = toMaggies(instFlux, _calibrationMean);
107  double err = toMaggiesErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr, maggies);
108  return Measurement(maggies, err);
109 }
110 
111 Measurement PhotoCalib::instFluxToMaggies(afw::table::SourceRecord const &sourceRecord,
112  std::string const &instFluxField) const {
113  auto position = sourceRecord.getCentroid();
114  auto instFluxKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFlux").key;
115  auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFluxErr").key;
116  return instFluxToMaggies(sourceRecord.get(instFluxKey), sourceRecord.get(instFluxErrKey), position);
117 }
118 ndarray::Array<double, 2, 2> PhotoCalib::instFluxToMaggies(afw::table::SourceCatalog const &sourceCatalog,
119  std::string const &instFluxField) const {
120  ndarray::Array<double, 2, 2> result =
121  ndarray::allocate(ndarray::makeVector(int(sourceCatalog.size()), 2));
122  instFluxToMaggiesArray(sourceCatalog, instFluxField, result);
123  return result;
124 }
125 
126 void PhotoCalib::instFluxToMaggies(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
127  std::string const &outField) const {
128  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
129  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
130  auto maggiesKey = sourceCatalog.getSchema().find<double>(outField + "_instFlux").key;
131  auto maggiesErrKey = sourceCatalog.getSchema().find<double>(outField + "_instFluxErr").key;
132  for (auto &record : sourceCatalog) {
133  auto result =
134  instFluxToMaggies(record.get(instFluxKey), record.get(instFluxErrKey), record.getCentroid());
135  record.set(maggiesKey, result.value);
136  record.set(maggiesErrKey, result.err);
137  }
138 }
139 
140 // ------------------- Conversions to Magnitudes -------------------
141 
142 double PhotoCalib::instFluxToMagnitude(double instFlux, lsst::geom::Point<double, 2> const &point) const {
143  return toMagnitude(instFlux, evaluate(point));
144 }
145 
146 double PhotoCalib::instFluxToMagnitude(double instFlux) const {
147  return toMagnitude(instFlux, _calibrationMean);
148 }
149 
150 Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr,
151  lsst::geom::Point<double, 2> const &point) const {
152  double calibration, err, magnitude;
153  calibration = evaluate(point);
154  magnitude = toMagnitude(instFlux, calibration);
155  err = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
156  return Measurement(magnitude, err);
157 }
158 
159 Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr) const {
160  double magnitude = toMagnitude(instFlux, _calibrationMean);
161  double err = toMagnitudeErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr);
162  return Measurement(magnitude, err);
163 }
164 
165 Measurement PhotoCalib::instFluxToMagnitude(afw::table::SourceRecord const &sourceRecord,
166  std::string const &instFluxField) const {
167  auto position = sourceRecord.getCentroid();
168  auto instFluxKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFlux").key;
169  auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFluxErr").key;
170  return instFluxToMagnitude(sourceRecord.get(instFluxKey), sourceRecord.get(instFluxErrKey), position);
171 }
172 
173 ndarray::Array<double, 2, 2> PhotoCalib::instFluxToMagnitude(afw::table::SourceCatalog const &sourceCatalog,
174  std::string const &instFluxField) const {
175  ndarray::Array<double, 2, 2> result =
176  ndarray::allocate(ndarray::makeVector(int(sourceCatalog.size()), 2));
177  instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
178  return result;
179 }
180 
181 void PhotoCalib::instFluxToMagnitude(afw::table::SourceCatalog &sourceCatalog,
182  std::string const &instFluxField, std::string const &outField) const {
183  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
184  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
185  auto magKey = sourceCatalog.getSchema().find<double>(outField + "_mag").key;
186  auto magErrKey = sourceCatalog.getSchema().find<double>(outField + "_magErr").key;
187  for (auto &record : sourceCatalog) {
188  auto result = instFluxToMagnitude(record.get(instFluxKey), record.get(instFluxErrKey),
189  record.getCentroid());
190  record.set(magKey, result.value);
191  record.set(magErrKey, result.err);
192  }
193 }
194 
195 // ------------------- other utility methods -------------------
196 
197 double PhotoCalib::magnitudeToInstFlux(double magnitude) const {
198  return fromMagnitude(magnitude, _calibrationMean);
199 }
200 
201 double PhotoCalib::magnitudeToInstFlux(double magnitude, lsst::geom::Point<double, 2> const &point) const {
202  return fromMagnitude(magnitude, evaluate(point));
203 }
204 
205 std::shared_ptr<math::BoundedField> PhotoCalib::computeScaledCalibration() const {
206  return *(_calibration) / _calibrationMean;
207 }
208 
210  throw LSST_EXCEPT(pex::exceptions::LogicError, "Not Implemented: See DM-10154.");
211 }
212 
213 bool PhotoCalib::operator==(PhotoCalib const &rhs) const {
214  return (_calibrationMean == rhs._calibrationMean && _calibrationErr == rhs._calibrationErr &&
215  (*_calibration) == *(rhs._calibration));
216 }
217 
218 double PhotoCalib::computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const {
219  return calibration->mean();
220 }
221 
223  if (photoCalib._isConstant)
224  os << "spatially constant with ";
225  else
226  os << *(photoCalib._calibration) << " with ";
227  return os << "mean: " << photoCalib._calibrationMean << " err: " << photoCalib._calibrationErr;
228 }
229 
230 MaskedImage<float> PhotoCalib::calibrateImage(MaskedImage<float> const &maskedImage) const {
231  // Deep copy construct, as we're mutiplying in-place.
232  auto result = MaskedImage<float>(maskedImage, true);
233 
234  if (_isConstant) {
235  *(result.getImage()) *= _calibrationMean;
236  } else {
237  _calibration->multiplyImage(*(result.getImage()), true); // only in the overlap region
238  }
239  toMaggiesVariance(maskedImage.getImage()->getArray(), maskedImage.getVariance()->getArray(),
240  _calibrationErr, result.getImage()->getArray(), result.getVariance()->getArray());
241 
242  return result;
243 }
244 
245 // ------------------- persistence -------------------
246 
247 namespace {
248 
249 class PhotoCalibSchema {
250 public:
251  table::Schema schema;
252  table::Key<double> calibrationMean;
253  table::Key<double> calibrationErr;
254  table::Key<table::Flag> isConstant;
255  table::Key<int> field;
256 
257  // No copying
258  PhotoCalibSchema(PhotoCalibSchema const &) = delete;
259  PhotoCalibSchema &operator=(PhotoCalibSchema const &) = delete;
260  // No moving
261  PhotoCalibSchema(PhotoCalibSchema &&) = delete;
262  PhotoCalibSchema &operator=(PhotoCalibSchema &&) = delete;
263 
264  static PhotoCalibSchema const &get() {
265  static PhotoCalibSchema const instance;
266  return instance;
267  }
268 
269 private:
270  PhotoCalibSchema()
271  : schema(),
272  calibrationMean(schema.addField<double>(
273  "calibrationMean", "mean calibration on this PhotoCalib's domain", "count")),
275  schema.addField<double>("calibrationErr", "1-sigma error on calibrationMean", "count")),
276  isConstant(schema.addField<table::Flag>("isConstant", "Is this spatially-constant?")),
277  field(schema.addField<int>("field", "archive ID of the BoundedField object")) {
278  schema.getCitizen().markPersistent();
279  }
280 };
281 
282 class PhotoCalibFactory : public table::io::PersistableFactory {
283 public:
284  PTR(table::io::Persistable)
285  read(InputArchive const &archive, CatalogVector const &catalogs) const override {
286  table::BaseRecord const &record = catalogs.front().front();
287  PhotoCalibSchema const &keys = PhotoCalibSchema::get();
288  return std::make_shared<PhotoCalib>(record.get(keys.calibrationMean), record.get(keys.calibrationErr),
289  archive.get<afw::math::BoundedField>(record.get(keys.field)),
290  record.get(keys.isConstant));
291  }
292 
293  PhotoCalibFactory(std::string const &name) : afw::table::io::PersistableFactory(name) {}
294 };
295 
296 std::string getPhotoCalibPersistenceName() { return "PhotoCalib"; }
297 
298 PhotoCalibFactory registration(getPhotoCalibPersistenceName());
299 
300 } // anonymous namespace
301 
302 std::string PhotoCalib::getPersistenceName() const { return getPhotoCalibPersistenceName(); }
303 
304 void PhotoCalib::write(OutputArchiveHandle &handle) const {
305  PhotoCalibSchema const &keys = PhotoCalibSchema::get();
306  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
307  auto record = catalog.addNew();
308  record->set(keys.calibrationMean, _calibrationMean);
309  record->set(keys.calibrationErr, _calibrationErr);
310  record->set(keys.isConstant, _isConstant);
311  record->set(keys.field, handle.put(_calibration));
312  handle.saveCatalog(catalog);
313 }
314 
315 // ------------------- private/protected helpers -------------------
316 
317 double PhotoCalib::evaluate(lsst::geom::Point<double, 2> const &point) const {
318  if (_isConstant)
319  return _calibrationMean;
320  else
321  return _calibration->evaluate(point);
322 }
323 
324 void PhotoCalib::instFluxToMaggiesArray(afw::table::SourceCatalog const &sourceCatalog,
325  std::string const &instFluxField,
326  ndarray::Array<double, 2, 2> result) const {
327  double instFlux, instFluxErr, maggies, calibration;
328  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
329  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
330  auto iter = result.begin();
331  for (auto const &rec : sourceCatalog) {
332  instFlux = rec.get(instFluxKey);
333  instFluxErr = rec.get(instFluxErrKey);
334  calibration = evaluate(rec.getCentroid());
335  maggies = toMaggies(instFlux, calibration);
336  (*iter)[0] = maggies;
337  (*iter)[1] = toMaggiesErr(instFlux, instFluxErr, calibration, _calibrationErr, maggies);
338  iter++;
339  }
340 }
341 
342 void PhotoCalib::instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
343  std::string const &instFluxField,
344  ndarray::Array<double, 2, 2> result) const {
345  double instFlux, instFluxErr, calibration;
346  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
347  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
348  auto iter = result.begin();
349  for (auto const &rec : sourceCatalog) {
350  instFlux = rec.get(instFluxKey);
351  instFluxErr = rec.get(instFluxErrKey);
352  calibration = evaluate(rec.getCentroid());
353  (*iter)[0] = toMagnitude(instFlux, calibration);
354  (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
355  iter++;
356  }
357 }
358 
359 } // namespace image
360 } // namespace afw
361 } // namespace lsst
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage&#39;s variance.
Definition: MaskedImage.h:1091
table::Key< double > calibrationErr
Definition: PhotoCalib.cc:253
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:102
table::Key< table::Flag > isConstant
Definition: PhotoCalib.cc:254
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
A value and its error.
Definition: PhotoCalib.h:48
#define PTR(...)
ItemVariant const * other
Definition: Schema.cc:56
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:252
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:1058
STL class.
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition: Schema.cc:656
A base class for image defects.
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:74
log
Key< U > key
Definition: Schema.cc:281
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:195
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
table::Schema schema
Definition: PhotoCalib.cc:251
std::ostream & operator<<(std::ostream &os, PhotoCalib const &photoCalib)
Definition: PhotoCalib.cc:222
T hypot(T... args)
#define LSST_EXCEPT(type,...)
T pow(T... args)
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:746
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:255