Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-44404: add nanojanskyToInstFlux to PhotoCalib #737

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/lsst/afw/image/PhotoCalib.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,23 @@ class PhotoCalib final : public table::io::PersistableFacade<PhotoCalib>, public
/// @overload magnitudeToInstFlux(double, lsst::geom::Point<double, 2> const &) const;
double magnitudeToInstFlux(double magnitude) const;

/**
* Convert nanojansky to instFlux (ADU).
*
* If passed point, use the exact calculation at that point, otherwise, use the mean scaling factor.
*
* Useful for computing expected instrumental flux from reference catalog sources.
*
* @param[in] nanojansky The flux in nanojanksies to convert.
* @param[in] point The position that flux is to be converted at.
*
* @returns Source instFlux in ADU.
*/
double nanojanskyToInstFlux(double nanojansky, lsst::geom::Point<double, 2> const &point) const;

/// @overload nanojanskyToInstFlux(double, lsst::geom::Point<double, 2> const &) const;
double nanojanskyToInstFlux(double nanojansky) const;

/**
* Get the mean photometric calibration.
*
Expand Down
13 changes: 11 additions & 2 deletions python/lsst/afw/image/_photoCalib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,19 @@ void declarePhotoCalib(lsst::utils::python::WrapperCollection &wrappers) {
cls.def("magnitudeToInstFlux",
py::overload_cast<double, lsst::geom::Point<double, 2> const &>(
&PhotoCalib::magnitudeToInstFlux, py::const_),
"instFlux"_a, "point"_a);
"magnitude"_a, "point"_a);
cls.def("magnitudeToInstFlux",
py::overload_cast<double>(&PhotoCalib::magnitudeToInstFlux, py::const_),
"instFlux"_a);
"magnitude"_a);

/* from nanojansky. */
cls.def("nanojanskyToInstFlux",
py::overload_cast<double, lsst::geom::Point<double, 2> const &>(
&PhotoCalib::nanojanskyToInstFlux, py::const_),
"nanojansky"_a, "point"_a);
cls.def("nanojanskyToInstFlux",
py::overload_cast<double>(&PhotoCalib::nanojanskyToInstFlux, py::const_),
"nanojansky"_a);

/* utilities */
cls.def("getCalibrationMean", &PhotoCalib::getCalibrationMean);
Expand Down
10 changes: 10 additions & 0 deletions src/image/PhotoCalib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ double toInstFluxFromMagnitude(double magnitude, double scale) {
return utils::ABMagnitudeToNanojansky(magnitude) / scale;
}

double toInstFluxFromNanojansky(double nanojansky, double scale) { return nanojansky / scale; }

double toNanojanskyErr(double instFlux, double instFluxErr, double scale, double scaleErr,
double nanojansky) {
return std::abs(nanojansky) * hypot(instFluxErr / instFlux, scaleErr / scale);
Expand Down Expand Up @@ -219,6 +221,14 @@ double PhotoCalib::magnitudeToInstFlux(double magnitude, lsst::geom::Point<doubl
return toInstFluxFromMagnitude(magnitude, evaluate(point));
}

double PhotoCalib::nanojanskyToInstFlux(double nanojansky) const {
return toInstFluxFromNanojansky(nanojansky, _calibrationMean);
}

double PhotoCalib::nanojanskyToInstFlux(double nanojansky, lsst::geom::Point<double, 2> const &point) const {
return toInstFluxFromNanojansky(nanojansky, evaluate(point));
}

std::shared_ptr<math::BoundedField> PhotoCalib::computeScaledCalibration() const {
return *(_calibration) / _calibrationMean;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/test_photoCalib.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,20 @@ def _testPhotoCalibCenter(self, photoCalib, calibrationErr):
photoCalib.magnitudeToInstFlux(mag2, self.pointXShift),
rtol=1e-15)

# test reverse conversion: nanojansky to instFlux (no position specified)
self.assertFloatsAlmostEqual(self.instFlux1, photoCalib.nanojanskyToInstFlux(self.flux1))
self.assertFloatsAlmostEqual(self.instFlux2, photoCalib.nanojanskyToInstFlux(self.flux2), rtol=1e-15)

# test round-tripping instFlux->nanojansky->instFlux (position specified)
flux = photoCalib.instFluxToNanojansky(self.instFlux1, self.pointXShift)
self.assertFloatsAlmostEqual(self.instFlux1,
photoCalib.nanojanskyToInstFlux(flux, self.pointXShift),
rtol=1e-15)
flux2 = photoCalib.instFluxToNanojansky(self.instFlux2, self.pointXShift)
self.assertFloatsAlmostEqual(self.instFlux2,
photoCalib.nanojanskyToInstFlux(flux2, self.pointXShift),
rtol=1e-15)

# test round-tripping arrays (position specified)
instFlux1Array = np.full(10, self.instFlux1)
instFlux2Array = np.full(10, self.instFlux2)
Expand Down
Loading