lsst.afw  14.0-49-g535656444+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/afw/geom/Point.h"
28 #include "lsst/afw/table/Source.h"
32 #include "lsst/pex/exceptions.h"
33 #include "ndarray.h"
34 
35 namespace lsst {
36 namespace afw {
37 namespace image {
38 
39 // ------------------- helpers -------------------
40 
41 namespace {
42 
43 double toMaggies(double instFlux, double scale) { return instFlux * scale; }
44 
45 double toMagnitude(double instFlux, double scale) { return -2.5 * log10(instFlux * scale); }
46 
47 double toMaggiesErr(double instFlux, double instFluxErr, double scale, double scaleErr, double maggies) {
48  return maggies * hypot(instFluxErr / instFlux, scaleErr / scale);
49 }
50 
51 double toMagnitudeErr(double instFlux, double instFluxErr, double scale, double scaleErr) {
52  return 2.5 / log(10.0) * hypot(instFluxErr / instFlux, scaleErr / scale);
53 }
54 
55 } // anonymous namespace
56 
57 // ------------------- Conversions to Maggies -------------------
58 
59 double PhotoCalib::instFluxToMaggies(double instFlux, afw::geom::Point<double, 2> const &point) const {
60  if (_isConstant)
61  return toMaggies(instFlux, _calibrationMean);
62  else
63  return toMaggies(instFlux, _calibration->evaluate(point));
64 }
65 
66 double PhotoCalib::instFluxToMaggies(double instFlux) const { return toMaggies(instFlux, _calibrationMean); }
67 
68 Measurement PhotoCalib::instFluxToMaggies(double instFlux, double instFluxErr,
69  afw::geom::Point<double, 2> const &point) const {
70  double calibration, err, maggies;
71  if (_isConstant)
72  calibration = _calibrationMean;
73  else
74  calibration = _calibration->evaluate(point);
75  maggies = toMaggies(instFlux, calibration);
76  err = toMaggiesErr(instFlux, instFluxErr, calibration, _calibrationErr, maggies);
77  return Measurement(maggies, err);
78 }
79 
80 Measurement PhotoCalib::instFluxToMaggies(double instFlux, double instFluxErr) const {
81  double maggies = toMaggies(instFlux, _calibrationMean);
82  double err = toMaggiesErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr, maggies);
83  return Measurement(maggies, err);
84 }
85 
87  std::string const &instFluxField) const {
88  auto position = sourceRecord.getCentroid();
89  auto instFluxKey = sourceRecord.getSchema().find<double>(instFluxField + "_flux").key;
90  auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_fluxSigma").key;
91  return instFluxToMaggies(sourceRecord.get(instFluxKey), sourceRecord.get(instFluxErrKey), position);
92 }
93 ndarray::Array<double, 2, 2> PhotoCalib::instFluxToMaggies(afw::table::SourceCatalog const &sourceCatalog,
94  std::string const &instFluxField) const {
95  ndarray::Array<double, 2, 2> result =
96  ndarray::allocate(ndarray::makeVector(int(sourceCatalog.size()), 2));
97  instFluxToMaggiesArray(sourceCatalog, instFluxField, result);
98  return result;
99 }
100 
101 void PhotoCalib::instFluxToMaggies(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
102  std::string const &outField) const {
103  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_flux").key;
104  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_fluxSigma").key;
105  auto maggiesKey = sourceCatalog.getSchema().find<double>(outField + "_flux").key;
106  auto maggiesErrKey = sourceCatalog.getSchema().find<double>(outField + "_fluxSigma").key;
107  for (auto &record : sourceCatalog) {
108  auto result =
109  instFluxToMaggies(record.get(instFluxKey), record.get(instFluxErrKey), record.getCentroid());
110  record.set(maggiesKey, result.value);
111  record.set(maggiesErrKey, result.err);
112  }
113 }
114 
115 // ------------------- Conversions to Magnitudes -------------------
116 
117 double PhotoCalib::instFluxToMagnitude(double instFlux, afw::geom::Point<double, 2> const &point) const {
118  if (_isConstant)
119  return toMagnitude(instFlux, _calibrationMean);
120  else
121  return toMagnitude(instFlux, _calibration->evaluate(point));
122 }
123 
124 double PhotoCalib::instFluxToMagnitude(double instFlux) const {
125  return toMagnitude(instFlux, _calibrationMean);
126 }
127 
128 Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr,
129  afw::geom::Point<double, 2> const &point) const {
130  double calibration, err, magnitude;
131  if (_isConstant)
132  calibration = _calibrationMean;
133  else
134  calibration = _calibration->evaluate(point);
135  magnitude = toMagnitude(instFlux, calibration);
136  err = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
137  return Measurement(magnitude, err);
138 }
139 
140 Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr) const {
141  double magnitude = toMagnitude(instFlux, _calibrationMean);
142  double err = toMagnitudeErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr);
143  return Measurement(magnitude, err);
144 }
145 
147  std::string const &instFluxField) const {
148  auto position = sourceRecord.getCentroid();
149  auto instFluxKey = sourceRecord.getSchema().find<double>(instFluxField + "_flux").key;
150  auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_fluxSigma").key;
151  return instFluxToMagnitude(sourceRecord.get(instFluxKey), sourceRecord.get(instFluxErrKey), position);
152 }
153 
154 ndarray::Array<double, 2, 2> PhotoCalib::instFluxToMagnitude(afw::table::SourceCatalog const &sourceCatalog,
155  std::string const &instFluxField) const {
156  ndarray::Array<double, 2, 2> result =
157  ndarray::allocate(ndarray::makeVector(int(sourceCatalog.size()), 2));
158  instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
159  return result;
160 }
161 
163  std::string const &instFluxField, std::string const &outField) const {
164  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_flux").key;
165  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_fluxSigma").key;
166  auto magKey = sourceCatalog.getSchema().find<double>(outField + "_mag").key;
167  auto magErrKey = sourceCatalog.getSchema().find<double>(outField + "_magErr").key;
168  for (auto &record : sourceCatalog) {
169  auto result = instFluxToMagnitude(record.get(instFluxKey), record.get(instFluxErrKey),
170  record.getCentroid());
171  record.set(magKey, result.value);
172  record.set(magErrKey, result.err);
173  }
174 }
175 
176 // ------------------- other utility methods -------------------
177 
178 double PhotoCalib::magnitudeToInstFlux(double magnitude) const {
179  return pow(10, magnitude / -2.5) / _calibrationMean;
180 }
181 
183  return *(_calibration) / _calibrationMean;
184 }
185 
187  throw LSST_EXCEPT(pex::exceptions::LogicError, "Not Implemented: See DM-10154.");
188 }
189 
190 bool PhotoCalib::operator==(PhotoCalib const &rhs) const {
191  return (_calibrationMean == rhs._calibrationMean && _calibrationErr == rhs._calibrationErr &&
192  (*_calibration) == *(rhs._calibration));
193 }
194 
195 double PhotoCalib::computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const {
196  return calibration->mean();
197 }
198 
200  if (photoCalib._isConstant)
201  os << "spatially constant with ";
202  else
203  os << *(photoCalib._calibration) << " with ";
204  return os << "mean: " << photoCalib._calibrationMean << " err: " << photoCalib._calibrationErr;
205 }
206 
207 // ------------------- persistence -------------------
208 
209 namespace {
210 
211 class PhotoCalibSchema {
212 public:
213  table::Schema schema;
214  table::Key<double> calibrationMean;
215  table::Key<double> calibrationErr;
216  table::Key<table::Flag> isConstant;
217  table::Key<int> field;
218 
219  // No copying
220  PhotoCalibSchema(PhotoCalibSchema const &) = delete;
221  PhotoCalibSchema &operator=(PhotoCalibSchema const &) = delete;
222  // No moving
223  PhotoCalibSchema(PhotoCalibSchema &&) = delete;
224  PhotoCalibSchema &operator=(PhotoCalibSchema &&) = delete;
225 
226  static PhotoCalibSchema const &get() {
227  static PhotoCalibSchema const instance;
228  return instance;
229  }
230 
231 private:
232  PhotoCalibSchema()
233  : schema(),
234  calibrationMean(schema.addField<double>(
235  "calibrationMean", "mean calibration on this PhotoCalib's domain", "count")),
237  schema.addField<double>("calibrationErr", "1-sigma error on calibrationMean", "count")),
238  isConstant(schema.addField<table::Flag>("isConstant", "Is this spatially-constant?")),
239  field(schema.addField<int>("field", "archive ID of the BoundedField object")) {
240  schema.getCitizen().markPersistent();
241  }
242 };
243 
244 class PhotoCalibFactory : public table::io::PersistableFactory {
245 public:
246  virtual PTR(table::io::Persistable)
247  read(InputArchive const &archive, CatalogVector const &catalogs) const {
248  table::BaseRecord const &record = catalogs.front().front();
249  PhotoCalibSchema const &keys = PhotoCalibSchema::get();
250  return std::make_shared<PhotoCalib>(record.get(keys.calibrationMean), record.get(keys.calibrationErr),
251  archive.get<afw::math::BoundedField>(record.get(keys.field)),
252  record.get(keys.isConstant));
253  }
254 
255  PhotoCalibFactory(std::string const &name) : afw::table::io::PersistableFactory(name) {}
256 };
257 
258 std::string getPhotoCalibPersistenceName() { return "PhotoCalib"; }
259 
260 PhotoCalibFactory registration(getPhotoCalibPersistenceName());
261 
262 } // anonymous namespace
263 
264 std::string PhotoCalib::getPersistenceName() const { return getPhotoCalibPersistenceName(); }
265 
267  PhotoCalibSchema const &keys = PhotoCalibSchema::get();
268  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
269  auto record = catalog.addNew();
270  record->set(keys.calibrationMean, _calibrationMean);
271  record->set(keys.calibrationErr, _calibrationErr);
272  record->set(keys.isConstant, _isConstant);
273  record->set(keys.field, handle.put(_calibration));
274  handle.saveCatalog(catalog);
275 }
276 
277 // ------------------- private/protected helpers -------------------
278 
279 void PhotoCalib::instFluxToMaggiesArray(afw::table::SourceCatalog const &sourceCatalog,
280  std::string const &instFluxField,
281  ndarray::Array<double, 2, 2> result) const {
282  double instFlux, instFluxErr, maggies, calibration;
283  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_flux").key;
284  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_fluxSigma").key;
285  auto iter = result.begin();
286  for (auto const &rec : sourceCatalog) {
287  instFlux = rec.get(instFluxKey);
288  instFluxErr = rec.get(instFluxErrKey);
289  if (_isConstant)
290  calibration = _calibrationMean;
291  else
292  calibration = _calibration->evaluate(rec.getCentroid());
293  maggies = toMaggies(instFlux, calibration);
294  (*iter)[0] = maggies;
295  (*iter)[1] = toMaggiesErr(instFlux, instFluxErr, calibration, _calibrationErr, maggies);
296  iter++;
297  }
298 }
299 
300 void PhotoCalib::instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
301  std::string const &instFluxField,
302  ndarray::Array<double, 2, 2> result) const {
303  double instFlux, instFluxErr, calibration;
304  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_flux").key;
305  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_fluxSigma").key;
306  auto iter = result.begin();
307  for (auto const &rec : sourceCatalog) {
308  instFlux = rec.get(instFluxKey);
309  instFluxErr = rec.get(instFluxErrKey);
310  if (_isConstant)
311  calibration = _calibrationMean;
312  else
313  calibration = _calibration->evaluate(rec.getCentroid());
314  (*iter)[0] = toMagnitude(instFlux, calibration);
315  (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
316  iter++;
317  }
318 }
319 } // namespace image
320 } // namespace afw
321 } // namespace lsst
table::Key< double > calibrationErr
Definition: PhotoCalib.cc:215
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...
table::Key< std::string > name
Definition: ApCorrMap.cc:71
bool operator==(PhotoCalib const &rhs) const
Two PhotoCalibs are equal if their component bounded fields and calibrationErr are equal...
Definition: PhotoCalib.cc:190
An object passed to Persistable::write to allow it to persist itself.
double instFluxToMagnitude(double instFlux, afw::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to AB magnitude.
Definition: PhotoCalib.cc:117
The photometric calibration of an exposure.
Definition: PhotoCalib.h:81
A custom container class for records, based on std::vector.
Definition: Catalog.h:97
table::Key< table::Flag > isConstant
Definition: PhotoCalib.cc:216
T log(T... args)
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:117
std::shared_ptr< afw::math::BoundedField > computeScalingTo(std::shared_ptr< PhotoCalib > other) const
Calculates the scaling between this PhotoCalib and another PhotoCalib.
Definition: PhotoCalib.cc:186
A value and its error.
Definition: PhotoCalib.h:44
#define PTR(...)
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
Definition: BaseRecord.h:123
T log10(T... args)
double instFluxToMaggies(double instFlux, afw::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to maggies at a point in the BoundedField.
Definition: PhotoCalib.cc:59
table::Key< double > calibrationMean
Definition: PhotoCalib.cc:214
STL class.
A base class for image defects.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: PhotoCalib.cc:264
Schema getSchema() const
Return the Schema that holds this record&#39;s fields and keys.
Definition: BaseRecord.h:52
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
table::Schema schema
Definition: PhotoCalib.cc:213
T hypot(T... args)
#define LSST_EXCEPT(type,...)
friend std::ostream & operator<<(std::ostream &os, PhotoCalib const &photoCalib)
Definition: PhotoCalib.cc:199
PhotoCalib & operator=(PhotoCalib const &)=delete
Record class that contains measurements made on a single exposure.
Definition: Source.h:80
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: PhotoCalib.cc:266
STL class.
Implementation of the Photometric Calibration class.
std::shared_ptr< afw::math::BoundedField > computeScaledCalibration() const
Calculates the spatially-variable calibration, normalized by the mean in the valid domain...
Definition: PhotoCalib.cc:182
double magnitudeToInstFlux(double magnitude) const
Convert AB magnitude to instFlux (ADU), using the mean instFlux/magnitude scaling factor...
Definition: PhotoCalib.cc:178
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
Definition: Source.h:916
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:217