lsst.afw  13.0-73-gb2a4e38
 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 + "_instFlux").key;
92  auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFluxErr").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  throw LSST_EXCEPT(pex::exceptions::LogicError,
106  "Will not be implemented until RFC-322 is implemented. See DM-10155");
107 }
108 
109 // ------------------- Conversions to Magnitudes -------------------
110 
111 double PhotoCalib::instFluxToMagnitude(double instFlux, afw::geom::Point<double, 2> const &point) const {
112  if (_isConstant)
113  return toMagnitude(instFlux, _instFluxMag0);
114  else
115  return toMagnitude(instFlux, _zeroPoint->evaluate(point));
116 }
117 
118 double PhotoCalib::instFluxToMagnitude(double instFlux) const { return toMagnitude(instFlux, _instFluxMag0); }
119 
120 Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr,
121  afw::geom::Point<double, 2> const &point) const {
122  double instFluxMag0, err, magnitude;
123  if (_isConstant)
124  instFluxMag0 = _instFluxMag0;
125  else
126  instFluxMag0 = _zeroPoint->evaluate(point);
127  magnitude = toMagnitude(instFlux, instFluxMag0);
128  err = toMagnitudeErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err);
129  return Measurement(magnitude, err);
130 }
131 
132 Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr) const {
133  double magnitude = toMagnitude(instFlux, _instFluxMag0);
134  double err = toMagnitudeErr(instFlux, instFluxErr, _instFluxMag0, _instFluxMag0Err);
135  return Measurement(magnitude, err);
136 }
137 
139  std::string const &instFluxField) const {
140  auto position = sourceRecord.getCentroid();
141  auto instFluxKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFlux").key;
142  auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFluxErr").key;
143  return instFluxToMagnitude(sourceRecord.get(instFluxKey), sourceRecord.get(instFluxErrKey), position);
144 }
145 
146 ndarray::Array<double, 2, 2> PhotoCalib::instFluxToMagnitude(afw::table::SourceCatalog const &sourceCatalog,
147  std::string const &instFluxField) const {
148  ndarray::Array<double, 2, 2> result =
149  ndarray::allocate(ndarray::makeVector(int(sourceCatalog.size()), 2));
150  instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
151  return result;
152 }
153 
155  std::string const &instFluxField, std::string const &outField) const {
156  throw LSST_EXCEPT(pex::exceptions::LogicError,
157  "Will not be implemented until RFC-322 is implemented. See DM-10155.");
158 }
159 
160 // ------------------- other utility methods -------------------
161 
162 double PhotoCalib::magnitudeToInstFlux(double magnitude) const {
163  return pow(10, magnitude / -2.5) * _instFluxMag0;
164 }
165 
166 std::shared_ptr<math::BoundedField> PhotoCalib::computeScaledZeroPoint() const {
167  return *(_zeroPoint) / _instFluxMag0;
168 }
169 
170 std::shared_ptr<math::BoundedField> PhotoCalib::computeScalingTo(std::shared_ptr<PhotoCalib> other) const {
171  throw LSST_EXCEPT(pex::exceptions::LogicError, "Not Implemented: See DM-10154.");
172 }
173 
174 bool PhotoCalib::operator==(PhotoCalib const &rhs) const {
175  return (_instFluxMag0 == rhs._instFluxMag0 && _instFluxMag0Err == rhs._instFluxMag0Err &&
176  (*_zeroPoint) == *(rhs._zeroPoint));
177 }
178 
179 double PhotoCalib::computeInstFluxMag0(std::shared_ptr<afw::math::BoundedField> zeroPoint) const {
180  return zeroPoint->mean();
181 }
182 
183 std::ostream &operator<<(std::ostream &os, PhotoCalib const &photoCalib) {
184  if (photoCalib._isConstant)
185  os << "spatially constant with ";
186  else
187  os << *(photoCalib._zeroPoint) << " with ";
188  return os << "mean: " << photoCalib._instFluxMag0 << " err: " << photoCalib._instFluxMag0Err;
189 }
190 
191 // ------------------- persistence -------------------
192 
193 namespace {
194 
195 class PhotoCalibSchema {
196 public:
197  table::Schema schema;
198  table::Key<double> instFluxMag0;
199  table::Key<double> instFluxMag0Err;
200  table::Key<table::Flag> isConstant;
201  table::Key<int> field;
202 
203  // No copying
204  PhotoCalibSchema(PhotoCalibSchema const &) = delete;
205  PhotoCalibSchema &operator=(PhotoCalibSchema const &) = delete;
206  // No moving
207  PhotoCalibSchema(PhotoCalibSchema &&) = delete;
208  PhotoCalibSchema &operator=(PhotoCalibSchema &&) = delete;
209 
210  static PhotoCalibSchema const &get() {
211  static PhotoCalibSchema const instance;
212  return instance;
213  }
214 
215 private:
216  PhotoCalibSchema()
217  : schema(),
218  instFluxMag0(schema.addField<double>("instFluxMag0", "instFlux of a zero-magnitude object",
219  "count")),
221  schema.addField<double>("instFluxMag0Err", "1-err error on instFluxmag0", "count")),
222  isConstant(schema.addField<table::Flag>("isConstant", "Is this spatially-constant?")),
223  field(schema.addField<int>("field", "archive ID of the BoundedField object")) {
224  schema.getCitizen().markPersistent();
225  }
226 };
227 
228 class PhotoCalibFactory : public table::io::PersistableFactory {
229 public:
230  virtual PTR(table::io::Persistable)
231  read(InputArchive const &archive, CatalogVector const &catalogs) const {
232  table::BaseRecord const &record = catalogs.front().front();
233  PhotoCalibSchema const &keys = PhotoCalibSchema::get();
234  return std::make_shared<PhotoCalib>(record.get(keys.instFluxMag0), record.get(keys.instFluxMag0Err),
235  archive.get<afw::math::BoundedField>(record.get(keys.field)),
236  record.get(keys.isConstant));
237  }
238 
239  PhotoCalibFactory(std::string const &name) : afw::table::io::PersistableFactory(name) {}
240 };
241 
242 std::string getPhotoCalibPersistenceName() { return "PhotoCalib"; }
243 
244 PhotoCalibFactory registration(getPhotoCalibPersistenceName());
245 
246 } // anonymous namespace
247 
248 std::string PhotoCalib::getPersistenceName() const { return getPhotoCalibPersistenceName(); }
249 
251  PhotoCalibSchema const &keys = PhotoCalibSchema::get();
252  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
253  auto record = catalog.addNew();
254  record->set(keys.instFluxMag0, _instFluxMag0);
255  record->set(keys.instFluxMag0Err, _instFluxMag0Err);
256  record->set(keys.isConstant, _isConstant);
257  record->set(keys.field, handle.put(_zeroPoint));
258  handle.saveCatalog(catalog);
259 }
260 
261 // ------------------- private/protected helpers -------------------
262 
263 void PhotoCalib::instFluxToMaggiesArray(afw::table::SourceCatalog const &sourceCatalog,
264  std::string const &instFluxField,
265  ndarray::Array<double, 2, 2> result) const {
266  double instFlux, instFluxErr, maggies, instFluxMag0;
267  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
268  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
269  auto iter = result.begin();
270  for (auto const &rec : sourceCatalog) {
271  instFlux = rec.get(instFluxKey);
272  instFluxErr = rec.get(instFluxErrKey);
273  if (_isConstant)
274  instFluxMag0 = _instFluxMag0;
275  else
276  instFluxMag0 = _zeroPoint->evaluate(rec.getCentroid());
277  maggies = toMaggies(instFlux, instFluxMag0);
278  (*iter)[0] = maggies;
279  (*iter)[1] = toMaggiesErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err, maggies);
280  iter++;
281  }
282 }
283 
284 void PhotoCalib::instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
285  std::string const &instFluxField,
286  ndarray::Array<double, 2, 2> result) const {
287  double instFlux, instFluxErr, instFluxMag0;
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  instFluxMag0 = _instFluxMag0;
296  else
297  instFluxMag0 = _zeroPoint->evaluate(rec.getCentroid());
298  (*iter)[0] = toMagnitude(instFlux, instFluxMag0);
299  (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err);
300  iter++;
301  }
302 }
303 }
304 }
305 }
table::Key< double > instFluxMag0Err
Definition: PhotoCalib.cc:199
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:174
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:198
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:166
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:162
double instFluxToMagnitude(double instFlux, afw::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to AB magnitude.
Definition: PhotoCalib.cc:111
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:248
std::ostream & operator<<(std::ostream &os, PhotoCalib const &photoCalib)
Definition: PhotoCalib.cc:183
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: PhotoCalib.cc:250
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:170
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:200
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