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