lsst.afw  13.0-98-g5a654715e+2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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"
31 #include "lsst/daf/base/PropertySet.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 pow2(double value) { return value * value; }
44 
45 double toMaggies(double instFlux, double scale) { return instFlux / scale; }
46 
47 double toMagnitude(double instFlux, double scale) { return -2.5 * log10(instFlux / scale); }
48 
49 double toMaggiesErr(double instFlux, double instFluxErr, double scale, double scaleErr, double maggies) {
50  return maggies * sqrt(pow2(instFluxErr / instFlux) + pow2(scaleErr / scale));
51 }
52 
53 double toMagnitudeErr(double instFlux, double instFluxErr, double scale, double scaleErr) {
54  return 2.5 / log(10.0) * sqrt(pow2(instFluxErr / instFlux) + pow2(scaleErr / scale));
55 }
56 
57 } // anonymous namespace
58 
59 // ------------------- Conversions to Maggies -------------------
60 
61 double PhotoCalib::instFluxToMaggies(double instFlux, afw::geom::Point<double, 2> const &point) const {
62  if (_isConstant)
63  return toMaggies(instFlux, _instFluxMag0);
64  else
65  return toMaggies(instFlux, _zeroPoint->evaluate(point));
66 }
67 
68 double PhotoCalib::instFluxToMaggies(double instFlux) const { return toMaggies(instFlux, _instFluxMag0); }
69 
70 Measurement PhotoCalib::instFluxToMaggies(double instFlux, double instFluxErr,
71  afw::geom::Point<double, 2> const &point) const {
72  double instFluxMag0, err, maggies;
73  if (_isConstant)
74  instFluxMag0 = _instFluxMag0;
75  else
76  instFluxMag0 = _zeroPoint->evaluate(point);
77  maggies = toMaggies(instFlux, instFluxMag0);
78  err = toMaggiesErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err, maggies);
79  return Measurement(maggies, err);
80 }
81 
82 Measurement PhotoCalib::instFluxToMaggies(double instFlux, double instFluxErr) const {
83  double maggies = toMaggies(instFlux, _instFluxMag0);
84  double err = toMaggiesErr(instFlux, instFluxErr, _instFluxMag0, _instFluxMag0Err, maggies);
85  return Measurement(maggies, err);
86 }
87 
89  std::string const &instFluxField) const {
90  auto position = sourceRecord.getCentroid();
91  auto instFluxKey = sourceRecord.getSchema().find<double>(instFluxField + "_flux").key;
92  auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_fluxSigma").key;
93  return instFluxToMaggies(sourceRecord.get(instFluxKey), sourceRecord.get(instFluxErrKey), position);
94 }
95 ndarray::Array<double, 2, 2> PhotoCalib::instFluxToMaggies(afw::table::SourceCatalog const &sourceCatalog,
96  std::string const &instFluxField) const {
97  ndarray::Array<double, 2, 2> result =
98  ndarray::allocate(ndarray::makeVector(int(sourceCatalog.size()), 2));
99  instFluxToMaggiesArray(sourceCatalog, instFluxField, result);
100  return result;
101 }
102 
103 void PhotoCalib::instFluxToMaggies(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
104  std::string const &outField) const {
105  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_flux").key;
106  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_fluxSigma").key;
107  auto maggiesKey = sourceCatalog.getSchema().find<double>(outField + "_calFlux").key;
108  auto maggiesErrKey = sourceCatalog.getSchema().find<double>(outField + "_calFluxErr").key;
109  for (auto & record : sourceCatalog) {
110  auto result = instFluxToMaggies(record.get(instFluxKey), record.get(instFluxErrKey),
111  record.getCentroid());
112  record.set(maggiesKey, result.value);
113  record.set(maggiesErrKey, result.err);
114  }
115 }
116 
117 // ------------------- Conversions to Magnitudes -------------------
118 
119 double PhotoCalib::instFluxToMagnitude(double instFlux, afw::geom::Point<double, 2> const &point) const {
120  if (_isConstant)
121  return toMagnitude(instFlux, _instFluxMag0);
122  else
123  return toMagnitude(instFlux, _zeroPoint->evaluate(point));
124 }
125 
126 double PhotoCalib::instFluxToMagnitude(double instFlux) const { return toMagnitude(instFlux, _instFluxMag0); }
127 
128 Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr,
129  afw::geom::Point<double, 2> const &point) const {
130  double instFluxMag0, err, magnitude;
131  if (_isConstant)
132  instFluxMag0 = _instFluxMag0;
133  else
134  instFluxMag0 = _zeroPoint->evaluate(point);
135  magnitude = toMagnitude(instFlux, instFluxMag0);
136  err = toMagnitudeErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err);
137  return Measurement(magnitude, err);
138 }
139 
140 Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr) const {
141  double magnitude = toMagnitude(instFlux, _instFluxMag0);
142  double err = toMagnitudeErr(instFlux, instFluxErr, _instFluxMag0, _instFluxMag0Err);
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) * _instFluxMag0;
180 }
181 
182 std::shared_ptr<math::BoundedField> PhotoCalib::computeScaledZeroPoint() const {
183  return *(_zeroPoint) / _instFluxMag0;
184 }
185 
186 std::shared_ptr<math::BoundedField> PhotoCalib::computeScalingTo(std::shared_ptr<PhotoCalib> other) const {
187  throw LSST_EXCEPT(pex::exceptions::LogicError, "Not Implemented: See DM-10154.");
188 }
189 
190 bool PhotoCalib::operator==(PhotoCalib const &rhs) const {
191  return (_instFluxMag0 == rhs._instFluxMag0 && _instFluxMag0Err == rhs._instFluxMag0Err &&
192  (*_zeroPoint) == *(rhs._zeroPoint));
193 }
194 
195 double PhotoCalib::computeInstFluxMag0(std::shared_ptr<afw::math::BoundedField> zeroPoint) const {
196  return zeroPoint->mean();
197 }
198 
199 std::ostream &operator<<(std::ostream &os, PhotoCalib const &photoCalib) {
200  if (photoCalib._isConstant)
201  os << "spatially constant with ";
202  else
203  os << *(photoCalib._zeroPoint) << " with ";
204  return os << "mean: " << photoCalib._instFluxMag0 << " err: " << photoCalib._instFluxMag0Err;
205 }
206 
207 // ------------------- persistence -------------------
208 
209 namespace {
210 
211 class PhotoCalibSchema {
212 public:
213  table::Schema schema;
214  table::Key<double> instFluxMag0;
215  table::Key<double> instFluxMag0Err;
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  instFluxMag0(schema.addField<double>("instFluxMag0", "instFlux of a zero-magnitude object",
235  "count")),
237  schema.addField<double>("instFluxMag0Err", "1-err error on instFluxmag0", "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.instFluxMag0), record.get(keys.instFluxMag0Err),
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.instFluxMag0, _instFluxMag0);
271  record->set(keys.instFluxMag0Err, _instFluxMag0Err);
272  record->set(keys.isConstant, _isConstant);
273  record->set(keys.field, handle.put(_zeroPoint));
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, instFluxMag0;
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  instFluxMag0 = _instFluxMag0;
291  else
292  instFluxMag0 = _zeroPoint->evaluate(rec.getCentroid());
293  maggies = toMaggies(instFlux, instFluxMag0);
294  (*iter)[0] = maggies;
295  (*iter)[1] = toMaggiesErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err, 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, instFluxMag0;
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  instFluxMag0 = _instFluxMag0;
312  else
313  instFluxMag0 = _zeroPoint->evaluate(rec.getCentroid());
314  (*iter)[0] = toMagnitude(instFlux, instFluxMag0);
315  (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err);
316  iter++;
317  }
318 }
319 }
320 }
321 }
table::Key< double > instFluxMag0Err
Definition: PhotoCalib.cc:215
int put(Persistable const *obj, bool permissive=false)
Save a nested Persistable to the same archive.
bool operator==(PhotoCalib const &rhs) const
Two PhotoCalibs are equal if their component bounded fields and instFluxMag0Err are equal...
Definition: PhotoCalib.cc:190
An object passed to Persistable::write to allow it to persist itself.
The photometric calibration of an exposure.
Definition: PhotoCalib.h:82
table::Key< int > field
Definition: ApCorrMap.cc:72
A custom container class for records, based on std::vector.
Definition: Catalog.h:96
A value and its error.
Definition: PhotoCalib.h:44
size_type size() const
Return the number of elements in the catalog.
Definition: Catalog.h:399
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
Definition: Source.h:900
table::Key< double > instFluxMag0
Definition: PhotoCalib.cc:214
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
Definition: fwd.h:63
std::shared_ptr< afw::math::BoundedField > computeScaledZeroPoint() const
Calculates the spatially-variable zero point, normalized by the mean in the valid domain...
Definition: PhotoCalib.cc:182
table::Key< std::string > name
Definition: ApCorrMap.cc:71
double magnitudeToInstFlux(double magnitude) const
Convert AB magnitude to instFlux (ADU), using the mean instFlux/magnitude scaling factor...
Definition: PhotoCalib.cc:178
double instFluxToMagnitude(double instFlux, afw::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to AB magnitude.
Definition: PhotoCalib.cc:119
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: PhotoCalib.cc:264
std::ostream & operator<<(std::ostream &os, PhotoCalib const &photoCalib)
Definition: PhotoCalib.cc:199
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: PhotoCalib.cc:266
afw::table::Key< afw::table::Array< ImagePixelT > > image
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
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:61
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
Definition: BaseRecord.h:123
Record class that contains measurements made on a single exposure.
Definition: Source.h:80
table::Key< table::Flag > isConstant
Definition: PhotoCalib.cc:216
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
py::object result
Definition: schema.cc:290
Implementation of the Photometric Calibration class.
Schema getSchema() const
Return the schema associated with the catalog&#39;s table.
Definition: Catalog.h:116
Schema getSchema() const
Return the Schema that holds this record&#39;s fields and keys.
Definition: BaseRecord.h:52
afw::table::Schema schema
Definition: GaussianPsf.cc:43
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:463