lsst.afw  17.0.1-19-gee9b810f8+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 <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 
225  Measurement instFluxToNanojansky(const afw::table::SourceRecord &sourceRecord,
226  std::string const &instFluxField) const;
227 
239  ndarray::Array<double, 2, 2> instFluxToNanojansky(afw::table::SourceCatalog const &sourceCatalog,
240  std::string const &instFluxField) const;
241 
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 
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 
364  afw::table::SourceCatalog calibrateCatalog(afw::table::SourceCatalog const &catalog,
365  std::vector<std::string> const &instFluxFields) const;
366 
368  afw::table::SourceCatalog calibrateCatalog(afw::table::SourceCatalog const &catalog) const;
369 
382  double magnitudeToInstFlux(double magnitude, lsst::geom::Point<double, 2> const &point) const;
383 
385  double magnitudeToInstFlux(double magnitude) const;
386 
399  double getCalibrationMean() const { return _calibrationMean; }
400 
413  double getCalibrationErr() const { return _calibrationErr; }
414 
427  double getInstFluxAtZeroMagnitude() const { return utils::referenceFlux / _calibrationMean; }
428 
442  std::shared_ptr<afw::math::BoundedField> computeScaledCalibration() const;
443 
466 
468  bool operator==(PhotoCalib const &rhs) const;
469 
471  bool operator!=(PhotoCalib const &rhs) const { return !(*this == rhs); }
472 
473  bool isPersistable() const noexcept override { return true; }
474 
476 
477  /* Backwards compatibility with old Calib object */
478 
480  [[deprecated("No-op: PhotoCalib never throws on negative instFlux. Will remove after v18.")]] static void
481  setThrowOnNegativeFlux(bool raiseException) noexcept {
482  ; // do nothing!
483  }
485  [[deprecated("No-op: PhotoCalib never throws on negative instFlux. Will remove after v18.")]] static bool
487  return false;
488  }
489 
493  [
494  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
495  "removed after v18.")]] double
496  getMagnitude(double instFlux) const {
497  return instFluxToMagnitude(instFlux);
498  }
499 
503  [
504  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
505  "removed after v18.")]] ndarray::Array<double, 1>
506  getMagnitude(ndarray::Array<double const, 1> const &instFlux) const;
507  [
508  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
509  "removed after v18.")]] std::pair<double, double>
510  getMagnitude(double instFlux, double instFluxErr) const {
511  auto result = instFluxToMagnitude(instFlux, instFluxErr);
512  return std::make_pair<const double &, const double &>(result.value, result.error);
513  };
514  [[deprecated(
515  "For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
516  "removed after v18.")]] std::pair<ndarray::Array<double, 1>, ndarray::Array<double, 1>>
517  getMagnitude(ndarray::Array<double const, 1> const &instFlux,
518  ndarray::Array<double const, 1> const &instFluxErr) const;
523  [[deprecated(
524  "For backwards compatibility with Calib; use `magnitudeToInstFlux` instead. To be removed "
525  "after v18.")]] double
526  getFlux(double magnitude) const {
527  return magnitudeToInstFlux(magnitude);
528  }
533  [[deprecated(
534  "For backwards compatibility with Calib: use `getCalibrationMean`, `getCalibrationErr`, or "
535  "`getInstFluxAtZeroMagnitude. To be removed after v18.")]] std::pair<double, double>
536  getFluxMag0() const {
537  return std::make_pair<double, double>(getInstFluxAtZeroMagnitude(),
539  }
541  [[deprecated(
542  "PhotoCalib is immutable: create a new one with the calibration factor and calibration error,"
543  " or create it like an old Calib object with makePhotoCalibFromCalibZeroPoint.")]] void
544  setFluxMag0(double, double = 0) const {
545  std::string msg =
546  "PhotoCalib is immutable: create a new `PhotoCalib` with the calibration"
547  " factor and error, or create it like an old Calib object with "
548  "`makePhotoCalibFromCalibZeroPoint`.";
550  }
551 
552 protected:
553  std::string getPersistenceName() const override;
554 
555  void write(OutputArchiveHandle &handle) const override;
556 
557 private:
559 
560  // The "mean" calibration, defined as the geometric mean of _calibration evaluated over _calibration's
561  // bbox. Computed on instantiation as a convinience. Also, the actual calibration for a spatially-constant
562  // calibration.
563  double _calibrationMean;
564 
565  // The standard deviation of this PhotoCalib.
566  double _calibrationErr;
567 
568  // Is this spatially-constant? Used to short-circuit getting centroids.
569  bool _isConstant;
570 
576  double evaluate(lsst::geom::Point<double, 2> const &point) const;
580  ndarray::Array<double, 1> evaluateArray(ndarray::Array<double, 1> const &xx,
581  ndarray::Array<double, 1> const &yy) const;
585  ndarray::Array<double, 1> evaluateCatalog(afw::table::SourceCatalog const &sourceCatalog) const;
586 
588  double computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const;
589 
591  void instFluxToNanojanskyArray(afw::table::SourceCatalog const &sourceCatalog,
592  std::string const &instFluxField,
593  ndarray::Array<double, 2, 2> result) const;
594  void instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
595  std::string const &instFluxField,
596  ndarray::Array<double, 2, 2> result) const;
597 };
598 
611 
625 std::shared_ptr<PhotoCalib> makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err);
626 
627 } // namespace image
628 } // namespace afw
629 } // namespace lsst
630 
631 #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:473
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:486
std::ostream & operator<<(std::ostream &os, Measurement const &measurement)
Definition: PhotoCalib.cc:47
double getCalibrationMean() const
Get the mean photometric calibration.
Definition: PhotoCalib.h:399
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:471
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:481
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:544
Measurement(double value, double error)
Definition: PhotoCalib.h:53
std::pair< double, double > getMagnitude(double instFlux, double instFluxErr) const
Definition: PhotoCalib.h:510
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:195
double getCalibrationErr() const
Get the mean photometric calibration error.
Definition: PhotoCalib.h:413
#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:536
double getMagnitude(double instFlux) const
Definition: PhotoCalib.h:496
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:526
double getInstFluxAtZeroMagnitude() const
Get the magnitude zero point (the instrumental flux corresponding to 0 magnitude).
Definition: PhotoCalib.h:427