lsst.afw  17.0.1-28-gdd72be969
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 
58 std::ostream &operator<<(std::ostream &os, Measurement const &measurement);
59 
70 inline void assertNonNegative(double value, std::string const &name) {
71  if (value < 0) {
73  (boost::format("%s must be positive: %.3g") % name % value).str());
74  }
75 }
76 
116 public:
117  // Allow move, but no copy
118  PhotoCalib(PhotoCalib const &) = default;
119  PhotoCalib(PhotoCalib &&) = default;
120  PhotoCalib &operator=(PhotoCalib const &) = delete;
121  PhotoCalib &operator=(PhotoCalib &&) = delete;
122 
123  ~PhotoCalib() override = default;
124 
129 
138  explicit PhotoCalib(double calibrationMean, double calibrationErr = 0,
140  : _calibrationMean(calibrationMean), _calibrationErr(calibrationErr), _isConstant(true) {
141  assertNonNegative(_calibrationMean, "Calibration mean");
142  assertNonNegative(_calibrationErr, "Calibration error");
143  ndarray::Array<double, 2, 2> coeffs = ndarray::allocate(ndarray::makeVector(1, 1));
144  coeffs[0][0] = calibrationMean;
145  _calibration = std::make_shared<afw::math::ChebyshevBoundedField>(
147  }
148 
156  : _calibration(calibration),
157  _calibrationMean(computeCalibrationMean(calibration)),
158  _calibrationErr(calibrationErr),
159  _isConstant(false) {
160  assertNonNegative(_calibrationMean, "Calibration (computed via BoundedField.mean()) mean");
161  assertNonNegative(_calibrationErr, "Calibration error");
162  }
163 
172  PhotoCalib(double calibrationMean, double calibrationErr,
174  : _calibration(calibration),
175  _calibrationMean(calibrationMean),
176  _calibrationErr(calibrationErr),
177  _isConstant(isConstant) {
178  assertNonNegative(_calibrationMean, "Calibration mean");
179  assertNonNegative(_calibrationErr, "Calibration error");
180  }
181 
192  double instFluxToNanojansky(double instFlux, lsst::geom::Point<double, 2> const &point) const;
193 
195  double instFluxToNanojansky(double instFlux) const;
196 
208  Measurement instFluxToNanojansky(double instFlux, double instFluxErr,
209  lsst::geom::Point<double, 2> const &point) const;
210 
212  Measurement instFluxToNanojansky(double instFlux, double instFluxErr) const;
213 
226  Measurement instFluxToNanojansky(const afw::table::SourceRecord &sourceRecord,
227  std::string const &instFluxField) const;
228 
241  ndarray::Array<double, 2, 2> instFluxToNanojansky(afw::table::SourceCatalog const &sourceCatalog,
242  std::string const &instFluxField) const;
243 
257  void instFluxToNanojansky(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
258  std::string const &outField) const;
259 
270  double instFluxToMagnitude(double instFlux, lsst::geom::Point<double, 2> const &point) const;
271 
273  double instFluxToMagnitude(double instFlux) const;
274 
286  Measurement instFluxToMagnitude(double instFlux, double instFluxErr,
287  lsst::geom::Point<double, 2> const &point) const;
288 
290  Measurement instFluxToMagnitude(double instFlux, double instFluxErr) const;
291 
304  Measurement instFluxToMagnitude(afw::table::SourceRecord const &sourceRecord,
305  std::string const &instFluxField) const;
306 
319  ndarray::Array<double, 2, 2> instFluxToMagnitude(afw::table::SourceCatalog const &sourceCatalog,
320  std::string const &instFluxField) const;
321 
338  void instFluxToMagnitude(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
339  std::string const &outField) const;
340 
351  MaskedImage<float> calibrateImage(MaskedImage<float> const &maskedImage,
352  bool includeScaleUncertainty = true) const;
353 
370  afw::table::SourceCatalog calibrateCatalog(afw::table::SourceCatalog const &catalog,
371  std::vector<std::string> const &instFluxFields) const;
372 
374  afw::table::SourceCatalog calibrateCatalog(afw::table::SourceCatalog const &catalog) const;
375 
388  double magnitudeToInstFlux(double magnitude, lsst::geom::Point<double, 2> const &point) const;
389 
391  double magnitudeToInstFlux(double magnitude) const;
392 
405  double getCalibrationMean() const { return _calibrationMean; }
406 
419  double getCalibrationErr() const { return _calibrationErr; }
420 
433  double getInstFluxAtZeroMagnitude() const { return utils::referenceFlux / _calibrationMean; }
434 
448  std::shared_ptr<afw::math::BoundedField> computeScaledCalibration() const;
449 
472 
474  bool operator==(PhotoCalib const &rhs) const;
475 
477  bool operator!=(PhotoCalib const &rhs) const { return !(*this == rhs); }
478 
479  bool isPersistable() const noexcept override { return true; }
480 
482 
483  /* Backwards compatibility with old Calib object */
484 
486  [[deprecated("No-op: PhotoCalib never throws on negative instFlux. Will remove after v18.")]] static void
487  setThrowOnNegativeFlux(bool raiseException) noexcept {
488  ; // do nothing!
489  }
491  [[deprecated("No-op: PhotoCalib never throws on negative instFlux. Will remove after v18.")]] static bool
493  return false;
494  }
495 
499  [
500  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
501  "removed after v18.")]] double
502  getMagnitude(double instFlux) const {
503  return instFluxToMagnitude(instFlux);
504  }
505 
509  [
510  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
511  "removed after v18.")]] ndarray::Array<double, 1>
512  getMagnitude(ndarray::Array<double const, 1> const &instFlux) const;
513  [
514  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
515  "removed after v18.")]] std::pair<double, double>
516  getMagnitude(double instFlux, double instFluxErr) const {
517  auto result = instFluxToMagnitude(instFlux, instFluxErr);
518  return std::make_pair<const double &, const double &>(result.value, result.error);
519  };
520  [[deprecated(
521  "For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
522  "removed after v18.")]] std::pair<ndarray::Array<double, 1>, ndarray::Array<double, 1>>
523  getMagnitude(ndarray::Array<double const, 1> const &instFlux,
524  ndarray::Array<double const, 1> const &instFluxErr) const;
529  [[deprecated(
530  "For backwards compatibility with Calib; use `magnitudeToInstFlux` instead. To be removed "
531  "after v18.")]] double
532  getFlux(double magnitude) const {
533  return magnitudeToInstFlux(magnitude);
534  }
539  [[deprecated(
540  "For backwards compatibility with Calib: use `getCalibrationMean`, `getCalibrationErr`, or "
541  "`getInstFluxAtZeroMagnitude. To be removed after v18.")]] std::pair<double, double>
542  getFluxMag0() const {
543  return std::make_pair<double, double>(getInstFluxAtZeroMagnitude(),
545  }
547  [[deprecated(
548  "PhotoCalib is immutable: create a new one with the calibration factor and calibration error,"
549  " or create it like an old Calib object with makePhotoCalibFromCalibZeroPoint.")]] void
550  setFluxMag0(double, double = 0) const {
551  std::string msg =
552  "PhotoCalib is immutable: create a new `PhotoCalib` with the calibration"
553  " factor and error, or create it like an old Calib object with "
554  "`makePhotoCalibFromCalibZeroPoint`.";
556  }
557 
558 protected:
559  std::string getPersistenceName() const override;
560 
561  void write(OutputArchiveHandle &handle) const override;
562 
563 private:
565 
566  // The "mean" calibration, defined as the geometric mean of _calibration evaluated over _calibration's
567  // bbox. Computed on instantiation as a convinience. Also, the actual calibration for a spatially-constant
568  // calibration.
569  double _calibrationMean;
570 
571  // The standard deviation of this PhotoCalib.
572  double _calibrationErr;
573 
574  // Is this spatially-constant? Used to short-circuit getting centroids.
575  bool _isConstant;
576 
582  double evaluate(lsst::geom::Point<double, 2> const &point) const;
586  ndarray::Array<double, 1> evaluateArray(ndarray::Array<double, 1> const &xx,
587  ndarray::Array<double, 1> const &yy) const;
591  ndarray::Array<double, 1> evaluateCatalog(afw::table::SourceCatalog const &sourceCatalog) const;
592 
594  double computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const;
595 
597  void instFluxToNanojanskyArray(afw::table::SourceCatalog const &sourceCatalog,
598  std::string const &instFluxField,
599  ndarray::Array<double, 2, 2> result) const;
600  void instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
601  std::string const &instFluxField,
602  ndarray::Array<double, 2, 2> result) const;
603 };
604 
617 
631 std::shared_ptr<PhotoCalib> makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err);
632 
633 } // namespace image
634 } // namespace afw
635 } // namespace lsst
636 
637 #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:611
table::Key< double > calibrationErr
Definition: PhotoCalib.cc:384
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
Definition: PhotoCalib.h:479
std::shared_ptr< PhotoCalib > makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err)
Construct a PhotoCalib from the deprecated Calib-style instFluxMag0/instFluxMag0Err values...
Definition: PhotoCalib.cc:629
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:172
static bool getThrowOnNegativeFlux() noexcept
No-op: for backwards compatibility with Calib (always returns false).
Definition: PhotoCalib.h:492
std::ostream & operator<<(std::ostream &os, Measurement const &measurement)
Definition: PhotoCalib.cc:47
double getCalibrationMean() const
Get the mean photometric calibration.
Definition: PhotoCalib.h:405
An object passed to Persistable::write to allow it to persist itself.
The photometric calibration of an exposure.
Definition: PhotoCalib.h:115
table::Key< table::Flag > isConstant
Definition: PhotoCalib.cc:385
table::Box2IKey bbox
Definition: Detector.cc:169
bool operator!=(PhotoCalib const &rhs) const
Two PhotoCalibs are equal if their component bounded fields and calibrationErr are equal...
Definition: PhotoCalib.h:477
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:383
PhotoCalib(double calibrationMean, double calibrationErr=0, lsst::geom::Box2I const &bbox=lsst::geom::Box2I())
Create a non-spatially-varying calibration.
Definition: PhotoCalib.h:138
STL class.
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
A base class for image defects.
static void setThrowOnNegativeFlux(bool raiseException) noexcept
No-op: for backwards compatibility with Calib.
Definition: PhotoCalib.h:487
PhotoCalib(std::shared_ptr< afw::math::BoundedField > calibration, double calibrationErr=0)
Create a spatially-varying calibration.
Definition: PhotoCalib.h:155
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:128
void setFluxMag0(double, double=0) const
Invalid for PhotoCalib: this only exists to provide the user an informative error message...
Definition: PhotoCalib.h:550
Measurement(double value, double error)
Definition: PhotoCalib.h:53
std::pair< double, double > getMagnitude(double instFlux, double instFluxErr) const
Definition: PhotoCalib.h:516
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:195
double getCalibrationErr() const
Get the mean photometric calibration error.
Definition: PhotoCalib.h:419
#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:542
double getMagnitude(double instFlux) const
Definition: PhotoCalib.h:502
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:70
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
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:532
double getInstFluxAtZeroMagnitude() const
Get the magnitude zero point (the instrumental flux corresponding to 0 magnitude).
Definition: PhotoCalib.h:433