lsst.afw  17.0.1-4-g41c8d5dc0+2
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"
43 #include "lsst/utils/Magnitude.h"
44 
45 namespace lsst {
46 namespace afw {
47 namespace image {
48 
50 struct Measurement {
51  Measurement(double value, double error) : value(value), error(error) {}
52  double const value;
53  double const error;
54 };
55 
66 inline void assertNonNegative(double value, std::string const &name) {
67  if (value < 0) {
69  (boost::format("%s must be positive: %.3g") % name % value).str());
70  }
71 }
72 
112 public:
113  // no move or copy
114  PhotoCalib(PhotoCalib const &) = delete;
115  PhotoCalib(PhotoCalib &&) = delete;
116  PhotoCalib &operator=(PhotoCalib const &) = delete;
117  PhotoCalib &operator=(PhotoCalib &&) = delete;
118 
119  ~PhotoCalib() override = default;
120 
125 
134  explicit PhotoCalib(double calibrationMean, double calibrationErr = 0,
136  : _calibrationMean(calibrationMean), _calibrationErr(calibrationErr), _isConstant(true) {
137  assertNonNegative(_calibrationMean, "Calibration mean");
138  assertNonNegative(_calibrationErr, "Calibration error");
139  ndarray::Array<double, 2, 2> coeffs = ndarray::allocate(ndarray::makeVector(1, 1));
140  coeffs[0][0] = calibrationMean;
141  _calibration = std::make_shared<afw::math::ChebyshevBoundedField>(
143  }
144 
152  : _calibration(calibration),
153  _calibrationMean(computeCalibrationMean(calibration)),
154  _calibrationErr(calibrationErr),
155  _isConstant(false) {
156  assertNonNegative(_calibrationMean, "Calibration (computed via BoundedField.mean()) mean");
157  assertNonNegative(_calibrationErr, "Calibration error");
158  }
159 
168  PhotoCalib(double calibrationMean, double calibrationErr,
170  : _calibration(calibration),
171  _calibrationMean(calibrationMean),
172  _calibrationErr(calibrationErr),
173  _isConstant(isConstant) {
174  assertNonNegative(_calibrationMean, "Calibration mean");
175  assertNonNegative(_calibrationErr, "Calibration error");
176  }
177 
188  double instFluxToNanojansky(double instFlux, lsst::geom::Point<double, 2> const &point) const;
189 
191  double instFluxToNanojansky(double instFlux) const;
192 
204  Measurement instFluxToNanojansky(double instFlux, double instFluxErr,
205  lsst::geom::Point<double, 2> const &point) const;
206 
208  Measurement instFluxToNanojansky(double instFlux, double instFluxErr) const;
209 
221  Measurement instFluxToNanojansky(const afw::table::SourceRecord &sourceRecord,
222  std::string const &instFluxField) const;
223 
235  ndarray::Array<double, 2, 2> instFluxToNanojansky(afw::table::SourceCatalog const &sourceCatalog,
236  std::string const &instFluxField) const;
237 
252  void instFluxToNanojansky(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
253  std::string const &outField) const;
254 
265  double instFluxToMagnitude(double instFlux, lsst::geom::Point<double, 2> const &point) const;
266 
268  double instFluxToMagnitude(double instFlux) const;
269 
281  Measurement instFluxToMagnitude(double instFlux, double instFluxErr,
282  lsst::geom::Point<double, 2> const &point) const;
283 
285  Measurement instFluxToMagnitude(double instFlux, double instFluxErr) const;
286 
298  Measurement instFluxToMagnitude(afw::table::SourceRecord const &sourceRecord,
299  std::string const &instFluxField) const;
300 
312  ndarray::Array<double, 2, 2> instFluxToMagnitude(afw::table::SourceCatalog const &sourceCatalog,
313  std::string const &instFluxField) const;
314 
332  void instFluxToMagnitude(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
333  std::string const &outField) const;
334 
345  MaskedImage<float> calibrateImage(MaskedImage<float> const &maskedImage,
346  bool includeScaleUncertainty = true) const;
347 
360  double magnitudeToInstFlux(double magnitude, lsst::geom::Point<double, 2> const &point) const;
362  double magnitudeToInstFlux(double magnitude) const;
363 
376  double getCalibrationMean() const { return _calibrationMean; }
377 
390  double getCalibrationErr() const { return _calibrationErr; }
391 
404  double getInstFluxAtZeroMagnitude() const { return utils::referenceFlux / _calibrationMean; }
405 
419  std::shared_ptr<afw::math::BoundedField> computeScaledCalibration() const;
420 
443 
445  bool operator==(PhotoCalib const &rhs) const;
446 
448  bool operator!=(PhotoCalib const &rhs) const { return !(*this == rhs); }
449 
450  bool isPersistable() const noexcept override { return true; }
451 
453 
454 protected:
455  std::string getPersistenceName() const override;
456 
457  void write(OutputArchiveHandle &handle) const override;
458 
459 private:
461 
462  // The "mean" calibration, defined as the geometric mean of _calibration evaluated over _calibration's
463  // bbox. Computed on instantiation as a convinience. Also, the actual calibration for a spatially-constant
464  // calibration.
465  double _calibrationMean;
466 
467  // The standard deviation of this PhotoCalib.
468  double _calibrationErr;
469 
470  // Is this spatially-constant? Used to short-circuit getting centroids.
471  bool _isConstant;
472 
478  double evaluate(lsst::geom::Point<double, 2> const &point) const;
479 
481  double computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const;
482 
484  void instFluxToNanojanskyArray(afw::table::SourceCatalog const &sourceCatalog,
485  std::string const &instFluxField,
486  ndarray::Array<double, 2, 2> result) const;
487  void instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
488  std::string const &instFluxField,
489  ndarray::Array<double, 2, 2> result) const;
490 };
491 
504 
518 std::shared_ptr<PhotoCalib> makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err);
519 
520 } // namespace image
521 } // namespace afw
522 } // namespace lsst
523 
524 #endif // LSST_AFW_IMAGE_PHOTOCALIB_H
std::shared_ptr< PhotoCalib > makePhotoCalibFromMetadata(daf::base::PropertySet &metadata, bool strip=false)
Construct a PhotoCalib from FITS FLUXMAG0/FLUXMAG0ERR keywords.
Definition: PhotoCalib.cc:397
table::Key< double > calibrationErr
Definition: PhotoCalib.cc:269
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
Definition: PhotoCalib.h:450
std::shared_ptr< PhotoCalib > makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err)
Construct a PhotoCalib from the deprecated Calib-style instFluxMag0/instFluxMag0Err values...
Definition: PhotoCalib.cc:415
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:168
double getCalibrationMean() const
Get the mean photometric calibration.
Definition: PhotoCalib.h:376
An object passed to Persistable::write to allow it to persist itself.
The photometric calibration of an exposure.
Definition: PhotoCalib.h:111
table::Key< table::Flag > isConstant
Definition: PhotoCalib.cc:270
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:448
A value and its error.
Definition: PhotoCalib.h:50
bool strip
Definition: fits.cc:831
ItemVariant const * other
Definition: Schema.cc:56
table::Key< double > calibrationMean
Definition: PhotoCalib.cc:268
PhotoCalib(double calibrationMean, double calibrationErr=0, lsst::geom::Box2I const &bbox=lsst::geom::Box2I())
Create a non-spatially-varying calibration.
Definition: PhotoCalib.h:134
STL class.
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
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:151
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:124
Measurement(double value, double error)
Definition: PhotoCalib.h:51
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:195
double getCalibrationErr() const
Get the mean photometric calibration error.
Definition: PhotoCalib.h:390
#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:66
std::ostream * os
Definition: Schema.cc:746
const double referenceFlux
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:176
STL class.
double getInstFluxAtZeroMagnitude() const
Get the magnitude zero point (the instrumental flux corresponding to 0 magnitude).
Definition: PhotoCalib.h:404