lsst.afw  18.1.0-18-gf57f4888d
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"
46 #include "lsst/utils/Magnitude.h"
47 
48 namespace lsst {
49 namespace afw {
50 namespace image {
51 
53 struct Measurement {
54  Measurement(double value, double error) : value(value), error(error) {}
55  double const value;
56  double const error;
57 };
58 
59 std::ostream &operator<<(std::ostream &os, Measurement const &measurement);
60 
71 inline void assertNonNegative(double value, std::string const &name) {
72  if (value < 0) {
74  (boost::format("%s must be positive: %.3g") % name % value).str());
75  }
76 }
77 
116 class PhotoCalib final : public table::io::PersistableFacade<PhotoCalib>, public typehandling::Storable {
117 public:
118  // Allow move, but no copy
119  PhotoCalib(PhotoCalib const &) = default;
120  PhotoCalib(PhotoCalib &&) = default;
121  PhotoCalib &operator=(PhotoCalib const &) = delete;
122  PhotoCalib &operator=(PhotoCalib &&) = delete;
123 
124  ~PhotoCalib() override = default;
125 
130 
139  explicit PhotoCalib(double calibrationMean, double calibrationErr = 0,
141  : _calibrationMean(calibrationMean), _calibrationErr(calibrationErr), _isConstant(true) {
142  assertNonNegative(_calibrationMean, "Calibration mean");
143  assertNonNegative(_calibrationErr, "Calibration error");
144  ndarray::Array<double, 2, 2> coeffs = ndarray::allocate(ndarray::makeVector(1, 1));
145  coeffs[0][0] = calibrationMean;
146  _calibration = std::make_shared<afw::math::ChebyshevBoundedField>(
148  }
149 
157  : _calibration(calibration),
158  _calibrationMean(computeCalibrationMean(calibration)),
159  _calibrationErr(calibrationErr),
160  _isConstant(false) {
161  assertNonNegative(_calibrationMean, "Calibration (computed via BoundedField.mean()) mean");
162  assertNonNegative(_calibrationErr, "Calibration error");
163  }
164 
173  PhotoCalib(double calibrationMean, double calibrationErr,
175  : _calibration(calibration),
176  _calibrationMean(calibrationMean),
177  _calibrationErr(calibrationErr),
178  _isConstant(isConstant) {
179  assertNonNegative(_calibrationMean, "Calibration mean");
180  assertNonNegative(_calibrationErr, "Calibration error");
181  }
182 
193  double instFluxToNanojansky(double instFlux, lsst::geom::Point<double, 2> const &point) const;
194 
196  double instFluxToNanojansky(double instFlux) const;
197 
209  Measurement instFluxToNanojansky(double instFlux, double instFluxErr,
210  lsst::geom::Point<double, 2> const &point) const;
211 
213  Measurement instFluxToNanojansky(double instFlux, double instFluxErr) const;
214 
227  Measurement instFluxToNanojansky(const afw::table::SourceRecord &sourceRecord,
228  std::string const &instFluxField) const;
229 
242  ndarray::Array<double, 2, 2> instFluxToNanojansky(afw::table::SourceCatalog const &sourceCatalog,
243  std::string const &instFluxField) const;
244 
258  void instFluxToNanojansky(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
259  std::string const &outField) const;
260 
271  double instFluxToMagnitude(double instFlux, lsst::geom::Point<double, 2> const &point) const;
272 
274  double instFluxToMagnitude(double instFlux) const;
275 
287  Measurement instFluxToMagnitude(double instFlux, double instFluxErr,
288  lsst::geom::Point<double, 2> const &point) const;
289 
291  Measurement instFluxToMagnitude(double instFlux, double instFluxErr) const;
292 
305  Measurement instFluxToMagnitude(afw::table::SourceRecord const &sourceRecord,
306  std::string const &instFluxField) const;
307 
320  ndarray::Array<double, 2, 2> instFluxToMagnitude(afw::table::SourceCatalog const &sourceCatalog,
321  std::string const &instFluxField) const;
322 
339  void instFluxToMagnitude(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
340  std::string const &outField) const;
341 
352  MaskedImage<float> calibrateImage(MaskedImage<float> const &maskedImage,
353  bool includeScaleUncertainty = true) const;
354 
371  afw::table::SourceCatalog calibrateCatalog(afw::table::SourceCatalog const &catalog,
372  std::vector<std::string> const &instFluxFields) const;
373 
375  afw::table::SourceCatalog calibrateCatalog(afw::table::SourceCatalog const &catalog) const;
376 
389  double magnitudeToInstFlux(double magnitude, lsst::geom::Point<double, 2> const &point) const;
390 
392  double magnitudeToInstFlux(double magnitude) const;
393 
406  double getCalibrationMean() const { return _calibrationMean; }
407 
420  double getCalibrationErr() const { return _calibrationErr; }
421 
434  double getInstFluxAtZeroMagnitude() const { return utils::referenceFlux / _calibrationMean; }
435 
452  double getLocalCalibration(lsst::geom::Point<double, 2> const &point) const { return evaluate(point); }
453 
467  std::shared_ptr<afw::math::BoundedField> computeScaledCalibration() const;
468 
491 
493  bool operator==(PhotoCalib const &rhs) const;
494 
496  bool operator!=(PhotoCalib const &rhs) const { return !(*this == rhs); }
497 
498  bool isPersistable() const noexcept override { return true; }
499 
500  /* Backwards compatibility with old Calib object */
501 
503  [[deprecated("No-op: PhotoCalib never throws on negative instFlux. Will remove after v18.")]] static void
504  setThrowOnNegativeFlux(bool raiseException) noexcept {
505  ; // do nothing!
506  }
508  [[deprecated("No-op: PhotoCalib never throws on negative instFlux. Will remove after v18.")]] static bool
510  return false;
511  }
512 
516  [
517  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
518  "removed after v18.")]] double
519  getMagnitude(double instFlux) const {
520  return instFluxToMagnitude(instFlux);
521  }
522 
526  [
527  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
528  "removed after v18.")]] ndarray::Array<double, 1>
529  getMagnitude(ndarray::Array<double const, 1> const &instFlux) const;
530  [
531  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
532  "removed after v18.")]] std::pair<double, double>
533  getMagnitude(double instFlux, double instFluxErr) const {
534  auto result = instFluxToMagnitude(instFlux, instFluxErr);
535  return std::make_pair<const double &, const double &>(result.value, result.error);
536  };
537  [[deprecated(
538  "For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
539  "removed after v18.")]] std::pair<ndarray::Array<double, 1>, ndarray::Array<double, 1>>
540  getMagnitude(ndarray::Array<double const, 1> const &instFlux,
541  ndarray::Array<double const, 1> const &instFluxErr) const;
546  [[deprecated(
547  "For backwards compatibility with Calib; use `magnitudeToInstFlux` instead. To be removed "
548  "after v18.")]] double
549  getFlux(double magnitude) const {
550  return magnitudeToInstFlux(magnitude);
551  }
556  [[deprecated(
557  "For backwards compatibility with Calib: use `getCalibrationMean`, `getCalibrationErr`, or "
558  "`getInstFluxAtZeroMagnitude. To be removed after v18.")]] std::pair<double, double>
559  getFluxMag0() const {
560  return std::make_pair<double, double>(getInstFluxAtZeroMagnitude(),
562  }
564  [[deprecated(
565  "PhotoCalib is immutable: create a new one with the calibration factor and calibration error,"
566  " or create it like an old Calib object with makePhotoCalibFromCalibZeroPoint.")]] void
567  setFluxMag0(double, double = 0) const {
568  std::string msg =
569  "PhotoCalib is immutable: create a new `PhotoCalib` with the calibration"
570  " factor and error, or create it like an old Calib object with "
571  "`makePhotoCalibFromCalibZeroPoint`.";
573  }
574 
576  std::shared_ptr<typehandling::Storable> cloneStorable() const override;
577 
579  std::string toString() const override;
580 
586  bool equals(typehandling::Storable const &other) const noexcept override;
587  // PhotoCalib equality comparable but intentionally not hashable
588 
589 protected:
590  std::string getPersistenceName() const override;
591 
592  void write(OutputArchiveHandle &handle) const override;
593 
594 private:
596 
597  // The "mean" calibration, defined as the geometric mean of _calibration evaluated over _calibration's
598  // bbox. Computed on instantiation as a convinience. Also, the actual calibration for a spatially-constant
599  // calibration.
600  double _calibrationMean;
601 
602  // The standard deviation of this PhotoCalib.
603  double _calibrationErr;
604 
605  // Is this spatially-constant? Used to short-circuit getting centroids.
606  bool _isConstant;
607 
613  double evaluate(lsst::geom::Point<double, 2> const &point) const;
617  ndarray::Array<double, 1> evaluateArray(ndarray::Array<double, 1> const &xx,
618  ndarray::Array<double, 1> const &yy) const;
622  ndarray::Array<double, 1> evaluateCatalog(afw::table::SourceCatalog const &sourceCatalog) const;
623 
625  double computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const;
626 
628  void instFluxToNanojanskyArray(afw::table::SourceCatalog const &sourceCatalog,
629  std::string const &instFluxField,
630  ndarray::Array<double, 2, 2> result) const;
631  void instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
632  std::string const &instFluxField,
633  ndarray::Array<double, 2, 2> result) const;
634 };
635 
648 
662 std::shared_ptr<PhotoCalib> makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err);
663 
664 } // namespace image
665 } // namespace afw
666 } // namespace lsst
667 
668 #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:624
table::Key< double > calibrationErr
Definition: PhotoCalib.cc:399
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
Definition: PhotoCalib.h:498
std::shared_ptr< PhotoCalib > makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err)
Construct a PhotoCalib from the deprecated Calib-style instFluxMag0/instFluxMag0Err values...
Definition: PhotoCalib.cc:642
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:173
static bool getThrowOnNegativeFlux() noexcept
No-op: for backwards compatibility with Calib (always returns false).
Definition: PhotoCalib.h:509
std::ostream & operator<<(std::ostream &os, Measurement const &measurement)
Definition: PhotoCalib.cc:48
double getCalibrationMean() const
Get the mean photometric calibration.
Definition: PhotoCalib.h:406
An object passed to Persistable::write to allow it to persist itself.
The photometric calibration of an exposure.
Definition: PhotoCalib.h:116
table::Key< table::Flag > isConstant
Definition: PhotoCalib.cc:400
table::Box2IKey bbox
Definition: Detector.cc:169
Interface supporting iteration over heterogenous containers.
Definition: Storable.h:56
bool operator!=(PhotoCalib const &rhs) const
Two PhotoCalibs are equal if their component bounded fields and calibrationErr are equal...
Definition: PhotoCalib.h:496
A value and its error.
Definition: PhotoCalib.h:53
bool strip
Definition: fits.cc:901
ItemVariant const * other
Definition: Schema.cc:56
table::Key< double > calibrationMean
Definition: PhotoCalib.cc:398
PhotoCalib(double calibrationMean, double calibrationErr=0, lsst::geom::Box2I const &bbox=lsst::geom::Box2I())
Create a non-spatially-varying calibration.
Definition: PhotoCalib.h:139
STL class.
A base class for image defects.
static void setThrowOnNegativeFlux(bool raiseException) noexcept
No-op: for backwards compatibility with Calib.
Definition: PhotoCalib.h:504
PhotoCalib(std::shared_ptr< afw::math::BoundedField > calibration, double calibrationErr=0)
Create a spatially-varying calibration.
Definition: PhotoCalib.h:156
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
PhotoCalib()
Create a empty, zeroed calibration.
Definition: PhotoCalib.h:129
void setFluxMag0(double, double=0) const
Invalid for PhotoCalib: this only exists to provide the user an informative error message...
Definition: PhotoCalib.h:567
Measurement(double value, double error)
Definition: PhotoCalib.h:54
std::pair< double, double > getMagnitude(double instFlux, double instFluxErr) const
Definition: PhotoCalib.h:533
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:195
double getCalibrationErr() const
Get the mean photometric calibration error.
Definition: PhotoCalib.h:420
#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:559
double getMagnitude(double instFlux) const
Definition: PhotoCalib.h:519
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:71
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
double getLocalCalibration(lsst::geom::Point< double, 2 > const &point) const
Get the local calibration at a point.
Definition: PhotoCalib.h:452
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:549
double getInstFluxAtZeroMagnitude() const
Get the magnitude zero point (the instrumental flux corresponding to 0 magnitude).
Definition: PhotoCalib.h:434