lsst.afw  17.0.1-8-g7c0af834f
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 
256  void instFluxToNanojansky(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
257  std::string const &outField) const;
258 
269  double instFluxToMagnitude(double instFlux, lsst::geom::Point<double, 2> const &point) const;
270 
272  double instFluxToMagnitude(double instFlux) const;
273 
285  Measurement instFluxToMagnitude(double instFlux, double instFluxErr,
286  lsst::geom::Point<double, 2> const &point) const;
287 
289  Measurement instFluxToMagnitude(double instFlux, double instFluxErr) const;
290 
302  Measurement instFluxToMagnitude(afw::table::SourceRecord const &sourceRecord,
303  std::string const &instFluxField) const;
304 
316  ndarray::Array<double, 2, 2> instFluxToMagnitude(afw::table::SourceCatalog const &sourceCatalog,
317  std::string const &instFluxField) const;
318 
336  void instFluxToMagnitude(afw::table::SourceCatalog &sourceCatalog, std::string const &instFluxField,
337  std::string const &outField) const;
338 
349  MaskedImage<float> calibrateImage(MaskedImage<float> const &maskedImage,
350  bool includeScaleUncertainty = true) const;
351 
364  double magnitudeToInstFlux(double magnitude, lsst::geom::Point<double, 2> const &point) const;
366  double magnitudeToInstFlux(double magnitude) const;
367 
380  double getCalibrationMean() const { return _calibrationMean; }
381 
394  double getCalibrationErr() const { return _calibrationErr; }
395 
408  double getInstFluxAtZeroMagnitude() const { return utils::referenceFlux / _calibrationMean; }
409 
423  std::shared_ptr<afw::math::BoundedField> computeScaledCalibration() const;
424 
447 
449  bool operator==(PhotoCalib const &rhs) const;
450 
452  bool operator!=(PhotoCalib const &rhs) const { return !(*this == rhs); }
453 
454  bool isPersistable() const noexcept override { return true; }
455 
457 
458  /* Backwards compatibility with old Calib object */
459 
461  [[deprecated("No-op: PhotoCalib never throws on negative instFlux. Will remove after v18.")]] static void
462  setThrowOnNegativeFlux(bool raiseException) noexcept {
463  ; // do nothing!
464  }
466  [[deprecated("No-op: PhotoCalib never throws on negative instFlux. Will remove after v18.")]] static bool
468  return false;
469  }
470 
474  [
475  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
476  "removed after v18.")]] double
477  getMagnitude(double instFlux) const {
478  return instFluxToMagnitude(instFlux);
479  }
480 
484  [
485  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
486  "removed after v18.")]] ndarray::Array<double, 1>
487  getMagnitude(ndarray::Array<double const, 1> const &instFlux) const;
488  [
489  [deprecated("For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
490  "removed after v18.")]] std::pair<double, double>
491  getMagnitude(double instFlux, double instFluxErr) const {
492  auto result = instFluxToMagnitude(instFlux, instFluxErr);
493  return std::make_pair<const double &, const double &>(result.value, result.error);
494  };
495  [[deprecated(
496  "For backwards compatibility with Calib; use `instFluxToMagnitude` instead. To be "
497  "removed after v18.")]] std::pair<ndarray::Array<double, 1>, ndarray::Array<double, 1>>
498  getMagnitude(ndarray::Array<double const, 1> const &instFlux,
499  ndarray::Array<double const, 1> const &instFluxErr) const;
504  [[deprecated(
505  "For backwards compatibility with Calib; use `magnitudeToInstFlux` instead. To be removed "
506  "after v18.")]] double
507  getFlux(double magnitude) const {
508  return magnitudeToInstFlux(magnitude);
509  }
514  [[deprecated(
515  "For backwards compatibility with Calib: use `getCalibrationMean`, `getCalibrationErr`, or "
516  "`getInstFluxAtZeroMagnitude. To be removed after v18.")]] std::pair<double, double>
517  getFluxMag0() const {
518  return std::make_pair<double, double>(getInstFluxAtZeroMagnitude(),
520  }
522  [[deprecated(
523  "PhotoCalib is immutable: create a new one with the calibration factor and calibration error,"
524  " or create it like an old Calib object with makePhotoCalibFromCalibZeroPoint.")]] void
525  setFluxMag0(double, double = 0) const {
526  std::string msg =
527  "PhotoCalib is immutable: create a new `PhotoCalib` with the calibration"
528  " factor and error, or create it like an old Calib object with "
529  "`makePhotoCalibFromCalibZeroPoint`.";
531  }
532 
533 protected:
534  std::string getPersistenceName() const override;
535 
536  void write(OutputArchiveHandle &handle) const override;
537 
538 private:
540 
541  // The "mean" calibration, defined as the geometric mean of _calibration evaluated over _calibration's
542  // bbox. Computed on instantiation as a convinience. Also, the actual calibration for a spatially-constant
543  // calibration.
544  double _calibrationMean;
545 
546  // The standard deviation of this PhotoCalib.
547  double _calibrationErr;
548 
549  // Is this spatially-constant? Used to short-circuit getting centroids.
550  bool _isConstant;
551 
557  double evaluate(lsst::geom::Point<double, 2> const &point) const;
558 
560  double computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const;
561 
563  void instFluxToNanojanskyArray(afw::table::SourceCatalog const &sourceCatalog,
564  std::string const &instFluxField,
565  ndarray::Array<double, 2, 2> result) const;
566  void instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
567  std::string const &instFluxField,
568  ndarray::Array<double, 2, 2> result) const;
569 };
570 
583 
597 std::shared_ptr<PhotoCalib> makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err);
598 
599 } // namespace image
600 } // namespace afw
601 } // namespace lsst
602 
603 #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:504
table::Key< double > calibrationErr
Definition: PhotoCalib.cc:304
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
Definition: PhotoCalib.h:454
std::shared_ptr< PhotoCalib > makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err)
Construct a PhotoCalib from the deprecated Calib-style instFluxMag0/instFluxMag0Err values...
Definition: PhotoCalib.cc:522
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:467
std::ostream & operator<<(std::ostream &os, Measurement const &measurement)
Definition: PhotoCalib.cc:47
double getCalibrationMean() const
Get the mean photometric calibration.
Definition: PhotoCalib.h:380
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:305
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:452
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:303
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:462
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:525
Measurement(double value, double error)
Definition: PhotoCalib.h:53
std::pair< double, double > getMagnitude(double instFlux, double instFluxErr) const
Definition: PhotoCalib.h:491
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:195
double getCalibrationErr() const
Get the mean photometric calibration error.
Definition: PhotoCalib.h:394
#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:517
double getMagnitude(double instFlux) const
Definition: PhotoCalib.h:477
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:507
double getInstFluxAtZeroMagnitude() const
Get the magnitude zero point (the instrumental flux corresponding to 0 magnitude).
Definition: PhotoCalib.h:408