lsst.afw  17.0.1-7-gf7766dbc3
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 <cmath> // For quiet_nan in the deprecated block
33 
34 #include "boost/format.hpp"
35 
38 #include "lsst/geom/Point.h"
39 #include "lsst/geom/Box.h"
40 #include "lsst/afw/table/Source.h"
45 #include "lsst/utils/Magnitude.h"
46 
47 namespace lsst {
48 namespace afw {
49 namespace image {
50 
52 struct Measurement {
53  Measurement(double value, double error) : value(value), error(error) {}
54  double const value;
55  double const error;
56 };
57 
68 inline void assertNonNegative(double value, std::string const &name) {
69  if (value < 0) {
71  (boost::format("%s must be positive: %.3g") % name % value).str());
72  }
73 }
74 
114 public:
115  // no move or copy
116  PhotoCalib(PhotoCalib const &) = delete;
117  PhotoCalib(PhotoCalib &&) = delete;
118  PhotoCalib &operator=(PhotoCalib const &) = delete;
119  PhotoCalib &operator=(PhotoCalib &&) = delete;
120 
121  ~PhotoCalib() override = default;
122 
127 
136  explicit PhotoCalib(double calibrationMean, double calibrationErr = 0,
138  : _calibrationMean(calibrationMean), _calibrationErr(calibrationErr), _isConstant(true) {
139  assertNonNegative(_calibrationMean, "Calibration mean");
140  assertNonNegative(_calibrationErr, "Calibration error");
141  ndarray::Array<double, 2, 2> coeffs = ndarray::allocate(ndarray::makeVector(1, 1));
142  coeffs[0][0] = calibrationMean;
143  _calibration = std::make_shared<afw::math::ChebyshevBoundedField>(
145  }
146 
154  : _calibration(calibration),
155  _calibrationMean(computeCalibrationMean(calibration)),
156  _calibrationErr(calibrationErr),
157  _isConstant(false) {
158  assertNonNegative(_calibrationMean, "Calibration (computed via BoundedField.mean()) mean");
159  assertNonNegative(_calibrationErr, "Calibration error");
160  }
161 
170  PhotoCalib(double calibrationMean, double calibrationErr,
172  : _calibration(calibration),
173  _calibrationMean(calibrationMean),
174  _calibrationErr(calibrationErr),
175  _isConstant(isConstant) {
176  assertNonNegative(_calibrationMean, "Calibration mean");
177  assertNonNegative(_calibrationErr, "Calibration error");
178  }
179 
190  double instFluxToNanojansky(double instFlux, lsst::geom::Point<double, 2> const &point) const;
191 
193  double instFluxToNanojansky(double instFlux) const;
194 
206  Measurement instFluxToNanojansky(double instFlux, double instFluxErr,
207  lsst::geom::Point<double, 2> const &point) const;
208 
210  Measurement instFluxToNanojansky(double instFlux, double instFluxErr) const;
211 
223  Measurement instFluxToNanojansky(const afw::table::SourceRecord &sourceRecord,
224  std::string const &instFluxField) const;
225 
237  ndarray::Array<double, 2, 2> instFluxToNanojansky(afw::table::SourceCatalog const &sourceCatalog,
238  std::string const &instFluxField) const;
239 
254  void instFluxToNanojansky(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
255  std::string const &outField) const;
256 
267  double instFluxToMagnitude(double instFlux, lsst::geom::Point<double, 2> const &point) const;
268 
270  double instFluxToMagnitude(double instFlux) const;
271 
283  Measurement instFluxToMagnitude(double instFlux, double instFluxErr,
284  lsst::geom::Point<double, 2> const &point) const;
285 
287  Measurement instFluxToMagnitude(double instFlux, double instFluxErr) const;
288 
300  Measurement instFluxToMagnitude(afw::table::SourceRecord const &sourceRecord,
301  std::string const &instFluxField) const;
302 
314  ndarray::Array<double, 2, 2> instFluxToMagnitude(afw::table::SourceCatalog const &sourceCatalog,
315  std::string const &instFluxField) const;
316 
334  void instFluxToMagnitude(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
335  std::string const &outField) const;
336 
347  MaskedImage<float> calibrateImage(MaskedImage<float> const &maskedImage,
348  bool includeScaleUncertainty = true) const;
349 
362  double magnitudeToInstFlux(double magnitude, lsst::geom::Point<double, 2> const &point) const;
364  double magnitudeToInstFlux(double magnitude) const;
365 
378  double getCalibrationMean() const { return _calibrationMean; }
379 
392  double getCalibrationErr() const { return _calibrationErr; }
393 
406  double getInstFluxAtZeroMagnitude() const { return utils::referenceFlux / _calibrationMean; }
407 
421  std::shared_ptr<afw::math::BoundedField> computeScaledCalibration() const;
422 
445 
447  bool operator==(PhotoCalib const &rhs) const;
448 
450  bool operator!=(PhotoCalib const &rhs) const { return !(*this == rhs); }
451 
452  bool isPersistable() const noexcept override { return true; }
453 
455 
456  /* Backwards compatibility with old Calib object */
457 
459  [[deprecated("No-op: PhotoCalib never throws on negative instFlux. Will remove after v18.")]] static void
460  setThrowOnNegativeFlux(bool raiseException) noexcept {
461  ; // do nothing!
462  }
464  [[deprecated("No-op: PhotoCalib never throws on negative instFlux. Will remove after v18.")]] static bool
466  return false;
467  }
468 
472  [
473  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
474  "removed after v18.")]] double
475  getMagnitude(double instFlux) const {
476  return instFluxToMagnitude(instFlux);
477  }
478 
482  [
483  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
484  "removed after v18.")]] ndarray::Array<double, 1>
485  getMagnitude(ndarray::Array<double const, 1> const &instFlux) const;
486  [
487  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
488  "removed after v18.")]] std::pair<double, double>
489  getMagnitude(double instFlux, double instFluxErr) const {
490  auto result = instFluxToMagnitude(instFlux, instFluxErr);
491  return std::make_pair<const double &, const double &>(result.value, result.error);
492  };
493  [[deprecated(
494  "For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
495  "removed after v18.")]] std::pair<ndarray::Array<double, 1>, ndarray::Array<double, 1>>
496  getMagnitude(ndarray::Array<double const, 1> const &instFlux,
497  ndarray::Array<double const, 1> const &instFluxErr) const;
502  [[deprecated(
503  "For backwards compatibility with Calib; use `magnitudeToInstFlux` instead. To be removed "
504  "after v18.")]] double
505  getFlux(double magnitude) const {
506  return magnitudeToInstFlux(magnitude);
507  }
512  [[deprecated(
513  "For backwards compatibility with Calib: use `getCalibrationMean`, `getCalibrationErr`, or "
514  "`getInstFluxAtZeroMagnitude. To be removed after v18.")]] std::pair<double, double>
515  getFluxMag0() const {
516  return std::make_pair<double, double>(getInstFluxAtZeroMagnitude(),
518  }
520  [[deprecated(
521  "PhotoCalib is immutable: create a new one with the calibration factor and calibration error,"
522  " or create it like an old Calib object with makePhotoCalibFromCalibZeroPoint.")]] void
523  setFluxMag0(double, double = 0) const {
524  std::string msg =
525  "PhotoCalib is immutable: create a new `PhotoCalib` with the calibration"
526  " factor and error, or create it like an old Calib object with "
527  "`makePhotoCalibFromCalibZeroPoint`.";
529  }
530 
531 protected:
532  std::string getPersistenceName() const override;
533 
534  void write(OutputArchiveHandle &handle) const override;
535 
536 private:
538 
539  // The "mean" calibration, defined as the geometric mean of _calibration evaluated over _calibration's
540  // bbox. Computed on instantiation as a convinience. Also, the actual calibration for a spatially-constant
541  // calibration.
542  double _calibrationMean;
543 
544  // The standard deviation of this PhotoCalib.
545  double _calibrationErr;
546 
547  // Is this spatially-constant? Used to short-circuit getting centroids.
548  bool _isConstant;
549 
555  double evaluate(lsst::geom::Point<double, 2> const &point) const;
556 
558  double computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const;
559 
561  void instFluxToNanojanskyArray(afw::table::SourceCatalog const &sourceCatalog,
562  std::string const &instFluxField,
563  ndarray::Array<double, 2, 2> result) const;
564  void instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
565  std::string const &instFluxField,
566  ndarray::Array<double, 2, 2> result) const;
567 };
568 
581 
595 std::shared_ptr<PhotoCalib> makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err);
596 
597 } // namespace image
598 } // namespace afw
599 } // namespace lsst
600 
601 #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:425
table::Key< double > calibrationErr
Definition: PhotoCalib.cc:297
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
Definition: PhotoCalib.h:452
std::shared_ptr< PhotoCalib > makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err)
Construct a PhotoCalib from the deprecated Calib-style instFluxMag0/instFluxMag0Err values...
Definition: PhotoCalib.cc:443
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:170
static bool getThrowOnNegativeFlux() noexcept
No-op: for backwards compatibility with Calib (always returns false).
Definition: PhotoCalib.h:465
double getCalibrationMean() const
Get the mean photometric calibration.
Definition: PhotoCalib.h:378
An object passed to Persistable::write to allow it to persist itself.
The photometric calibration of an exposure.
Definition: PhotoCalib.h:113
table::Key< table::Flag > isConstant
Definition: PhotoCalib.cc:298
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:450
A value and its error.
Definition: PhotoCalib.h:52
bool strip
Definition: fits.cc:883
ItemVariant const * other
Definition: Schema.cc:56
table::Key< double > calibrationMean
Definition: PhotoCalib.cc:296
PhotoCalib(double calibrationMean, double calibrationErr=0, lsst::geom::Box2I const &bbox=lsst::geom::Box2I())
Create a non-spatially-varying calibration.
Definition: PhotoCalib.h:136
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.
static void setThrowOnNegativeFlux(bool raiseException) noexcept
No-op: for backwards compatibility with Calib.
Definition: PhotoCalib.h:460
PhotoCalib(std::shared_ptr< afw::math::BoundedField > calibration, double calibrationErr=0)
Create a spatially-varying calibration.
Definition: PhotoCalib.h:153
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:126
void setFluxMag0(double, double=0) const
Invalid for PhotoCalib: this only exists to provide the user an informative error message...
Definition: PhotoCalib.h:523
Measurement(double value, double error)
Definition: PhotoCalib.h:53
std::pair< double, double > getMagnitude(double instFlux, double instFluxErr) const
Definition: PhotoCalib.h:489
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:195
double getCalibrationErr() const
Get the mean photometric calibration error.
Definition: PhotoCalib.h:392
#define LSST_EXCEPT(type,...)
A BoundedField based on 2-d Chebyshev polynomials of the first kind.
std::pair< double, double > getFluxMag0() const
Get the magnitude zero point (the instrumental flux corresponding to 0 magnitude).
Definition: PhotoCalib.h:515
double getMagnitude(double instFlux) const
Definition: PhotoCalib.h:475
Record class that contains measurements made on a single exposure.
Definition: Source.h:82
T quiet_NaN(T... args)
void assertNonNegative(double value, std::string const &name)
Raise lsst::pex::exceptions::InvalidParameterError if value is not >=0.
Definition: PhotoCalib.h:68
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 getFlux(double magnitude) const
Definition: PhotoCalib.h:505
double getInstFluxAtZeroMagnitude() const
Get the magnitude zero point (the instrumental flux corresponding to 0 magnitude).
Definition: PhotoCalib.h:406