lsst.afw  16.0-66-g44ffc98c2+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 
355  double magnitudeToInstFlux(double magnitude, lsst::geom::Point<double, 2> const &point) const;
357  double magnitudeToInstFlux(double magnitude) const;
358 
372  double getCalibrationMean() const { return _calibrationMean; }
373 
386  double getCalibrationErr() const { return _calibrationErr; }
387 
400  double getInstFluxMag0() const { return 1.0 / _calibrationMean; }
401 
415  std::shared_ptr<afw::math::BoundedField> computeScaledCalibration() const;
416 
439 
441  bool operator==(PhotoCalib const &rhs) const;
442 
444  bool operator!=(PhotoCalib const &rhs) const { return !(*this == rhs); }
445 
446  bool isPersistable() const noexcept override { return true; }
447 
449 
450 protected:
451  std::string getPersistenceName() const override;
452 
453  void write(OutputArchiveHandle &handle) const override;
454 
455 private:
457 
458  // The "mean" calibration, defined as the geometric mean of _calibration evaluated over _calibration's
459  // bbox. Computed on instantiation as a convinience. Also, the actual calibration for a spatially-constant
460  // calibration.
461  double _calibrationMean;
462 
463  // The standard deviation of this PhotoCalib.
464  double _calibrationErr;
465 
466  // Is this spatially-constant? Used to short-circuit getting centroids.
467  bool _isConstant;
468 
474  double evaluate(lsst::geom::Point<double, 2> const &point) const;
475 
477  double computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const;
478 
480  void instFluxToMaggiesArray(afw::table::SourceCatalog const &sourceCatalog,
481  std::string const &instFluxField, ndarray::Array<double, 2, 2> result) const;
482  void instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
483  std::string const &instFluxField,
484  ndarray::Array<double, 2, 2> result) const;
485 };
486 
487 } // namespace image
488 } // namespace afw
489 } // namespace lsst
490 
491 #endif // LSST_AFW_IMAGE_PHOTOCALIB_H
table::Key< double > calibrationErr
Definition: PhotoCalib.cc:255
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
Definition: PhotoCalib.h:446
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:372
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:256
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:444
A value and its error.
Definition: PhotoCalib.h:48
ItemVariant const * other
Definition: Schema.cc:56
table::Key< double > calibrationMean
Definition: PhotoCalib.cc:254
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:448
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:195
double getCalibrationErr() const
Get the mean photometric calibration error.
Definition: PhotoCalib.h:386
#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:746
double getInstFluxMag0() const
Get the magnitude zero point (equal to 1 / mean(calibration)).
Definition: PhotoCalib.h:400
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:176
STL class.