lsst.afw  16.0-57-g90e7ba260+1
PhotoCalib.h
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 #ifndef LSST_AFW_IMAGE_PHOTOCALIB_H
24 #define LSST_AFW_IMAGE_PHOTOCALIB_H
25 
32 #include "boost/format.hpp"
33 
36 #include "lsst/geom/Point.h"
37 #include "lsst/geom/Box.h"
38 #include "lsst/afw/table/Source.h"
42 
43 namespace lsst {
44 namespace afw {
45 namespace image {
46 
48 struct Measurement {
49  Measurement(double value, double err) : value(value), err(err) {}
50  double const value;
51  double const err;
52 };
53 
64 inline void assertNonNegative(double value, std::string const &name) {
65  if (value < 0) {
67  (boost::format("%s must be positive: %.3g") % name % value).str());
68  }
69 }
70 
103 public:
104  // no move or copy
105  PhotoCalib(PhotoCalib const &) = delete;
106  PhotoCalib(PhotoCalib &&) = delete;
107  PhotoCalib &operator=(PhotoCalib const &) = delete;
108  PhotoCalib &operator=(PhotoCalib &&) = delete;
109 
110  ~PhotoCalib() override = default;
111 
116 
125  explicit PhotoCalib(double calibrationMean, double calibrationErr = 0,
127  : _calibrationMean(calibrationMean), _calibrationErr(calibrationErr), _isConstant(true) {
128  assertNonNegative(_calibrationMean, "Calibration mean");
129  assertNonNegative(_calibrationErr, "Calibration error");
130  ndarray::Array<double, 2, 2> coeffs = ndarray::allocate(ndarray::makeVector(1, 1));
131  coeffs[0][0] = calibrationMean;
132  _calibration = std::make_shared<afw::math::ChebyshevBoundedField>(
134  }
135 
143  : _calibration(calibration),
144  _calibrationMean(computeCalibrationMean(calibration)),
145  _calibrationErr(calibrationErr),
146  _isConstant(false) {
147  assertNonNegative(_calibrationMean, "Calibration (computed via BoundedField.mean()) mean");
148  assertNonNegative(_calibrationErr, "Calibration error");
149  }
150 
159  PhotoCalib(double calibrationMean, double calibrationErr,
161  : _calibration(calibration),
162  _calibrationMean(calibrationMean),
163  _calibrationErr(calibrationErr),
164  _isConstant(isConstant) {
165  assertNonNegative(_calibrationMean, "Calibration mean");
166  assertNonNegative(_calibrationErr, "Calibration error");
167  }
168 
179  double instFluxToMaggies(double instFlux, lsst::geom::Point<double, 2> const &point) const;
180 
182  double instFluxToMaggies(double instFlux) const;
183 
195  Measurement instFluxToMaggies(double instFlux, double instFluxErr,
196  lsst::geom::Point<double, 2> const &point) const;
197 
199  Measurement instFluxToMaggies(double instFlux, double instFluxErr) const;
200 
213  Measurement instFluxToMaggies(const afw::table::SourceRecord &sourceRecord,
214  std::string const &instFluxField) const;
215 
228  ndarray::Array<double, 2, 2> instFluxToMaggies(afw::table::SourceCatalog const &sourceCatalog,
229  std::string const &instFluxField) const;
230 
246  void instFluxToMaggies(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
247  std::string const &outField) const;
248 
259  double instFluxToMagnitude(double instFlux, lsst::geom::Point<double, 2> const &point) const;
260 
262  double instFluxToMagnitude(double instFlux) const;
263 
275  Measurement instFluxToMagnitude(double instFlux, double instFluxErr,
276  lsst::geom::Point<double, 2> const &point) const;
277 
279  Measurement instFluxToMagnitude(double instFlux, double instFluxErr) const;
280 
293  Measurement instFluxToMagnitude(afw::table::SourceRecord const &sourceRecord,
294  std::string const &instFluxField) const;
295 
308  ndarray::Array<double, 2, 2> instFluxToMagnitude(afw::table::SourceCatalog const &sourceCatalog,
309  std::string const &instFluxField) const;
310 
329  void instFluxToMagnitude(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
330  std::string const &outField) const;
331 
341  MaskedImage<float> calibrateImage(MaskedImage<float> const &maskedImage) const;
342 
350  double magnitudeToInstFlux(double magnitude) const;
351 
365  double getCalibrationMean() const { return _calibrationMean; }
366 
379  double getCalibrationErr() const { return _calibrationErr; }
380 
393  double getInstFluxMag0() const { return 1.0 / _calibrationMean; }
394 
408  std::shared_ptr<afw::math::BoundedField> computeScaledCalibration() const;
409 
432 
434  bool operator==(PhotoCalib const &rhs) const;
435 
437  bool operator!=(PhotoCalib const &rhs) const { return !(*this == rhs); }
438 
439  bool isPersistable() const noexcept override { return true; }
440 
442 
443 protected:
444  std::string getPersistenceName() const override;
445 
446  void write(OutputArchiveHandle &handle) const override;
447 
448 private:
450 
451  // The "mean" calibration, defined as the geometric mean of _calibration evaluated over _calibration's
452  // bbox. Computed on instantiation as a convinience. Also, the actual calibration for a spatially-constant
453  // calibration.
454  double _calibrationMean;
455 
456  // The standard deviation of this PhotoCalib.
457  double _calibrationErr;
458 
459  // Is this spatially-constant? Used to short-circuit getting centroids.
460  bool _isConstant;
461 
467  double evaluate(lsst::geom::Point<double, 2> const &point) const;
468 
470  double computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const;
471 
473  void instFluxToMaggiesArray(afw::table::SourceCatalog const &sourceCatalog,
474  std::string const &instFluxField, ndarray::Array<double, 2, 2> result) const;
475  void instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
476  std::string const &instFluxField,
477  ndarray::Array<double, 2, 2> result) const;
478 };
479 
480 } // namespace image
481 } // namespace afw
482 } // namespace lsst
483 
484 #endif // LSST_AFW_IMAGE_PHOTOCALIB_H
table::Key< double > calibrationErr
Definition: PhotoCalib.cc:247
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
Definition: PhotoCalib.h:439
PhotoCalib(double calibrationMean, double calibrationErr, std::shared_ptr< afw::math::BoundedField > calibration, bool isConstant)
Create a calibration with a pre-computed mean.
Definition: PhotoCalib.h:159
double getCalibrationMean() const
Get the mean photometric calibration (equal to 1 / getInstFluxMag0()).
Definition: PhotoCalib.h:365
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:248
table::Box2IKey bbox
Definition: Detector.cc:166
bool operator!=(PhotoCalib const &rhs) const
Two PhotoCalibs are equal if their component bounded fields and calibrationErr are equal...
Definition: PhotoCalib.h:437
bool operator==(CoordKey const &lhs, CoordKey const &rhs)
Compare CoordKeys for equality using the constituent Keys.
A value and its error.
Definition: PhotoCalib.h:48
ItemVariant const * other
Definition: Schema.cc:55
table::Key< double > calibrationMean
Definition: PhotoCalib.cc:246
PhotoCalib(double calibrationMean, double calibrationErr=0, lsst::geom::Box2I const &bbox=lsst::geom::Box2I())
Create a non-spatially-varying calibration.
Definition: PhotoCalib.h:125
STL class.
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
Measurement(double value, double err)
Definition: PhotoCalib.h:49
std::ostream & operator<<(std::ostream &os, VisitInfo const &visitInfo)
Definition: VisitInfo.cc:440
A base class for image defects.
PhotoCalib(std::shared_ptr< afw::math::BoundedField > calibration, double calibrationErr=0)
Create a spatially-varying calibration.
Definition: PhotoCalib.h:142
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:74
PhotoCalib()
Create a empty, zeroed calibration.
Definition: PhotoCalib.h:115
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:194
double getCalibrationErr() const
Get the mean photometric calibration error.
Definition: PhotoCalib.h:379
#define LSST_EXCEPT(type,...)
A BoundedField based on 2-d Chebyshev polynomials of the first kind.
Record class that contains measurements made on a single exposure.
Definition: Source.h:82
void assertNonNegative(double value, std::string const &name)
Raise lsst::pex::exceptions::InvalidParameterError if value is not >=0.
Definition: PhotoCalib.h:64
std::ostream * os
Definition: Schema.cc:737
double getInstFluxMag0() const
Get the magnitude zero point (equal to 1 / mean(calibration)).
Definition: PhotoCalib.h:393
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:176
STL class.