32 #include "boost/format.hpp" 33 #include "boost/algorithm/string/trim.hpp" 55 ndarray::Array<T, 1>
abMagFromFlux(ndarray::Array<T const, 1>
const& flux) {
56 ndarray::Array<T, 1> out = ndarray::allocate(flux.getShape());
57 for (
std::size_t ii = 0; ii < flux.getNumElements(); ++ii) {
66 ndarray::Array<T const, 1>
const& flux) {
67 if (flux.getNumElements() != fluxErr.getNumElements()) {
69 flux.getNumElements() % fluxErr.getNumElements())
72 ndarray::Array<T, 1> out = ndarray::allocate(flux.getShape());
73 for (
std::size_t ii = 0; ii < flux.getNumElements(); ++ii) {
81 ndarray::Array<T, 1>
fluxFromABMag(ndarray::Array<T const, 1>
const& mag) {
82 ndarray::Array<T, 1> out = ndarray::allocate(mag.getShape());
83 for (
std::size_t ii = 0; ii < mag.getNumElements(); ++ii) {
92 ndarray::Array<T const, 1>
const& mag) {
93 if (mag.getNumElements() != magErr.getNumElements()) {
95 mag.getNumElements() % magErr.getNumElements())
98 ndarray::Array<T, 1> out = ndarray::allocate(mag.getShape());
99 for (
std::size_t ii = 0; ii < mag.getNumElements(); ++ii) {
105 Calib::Calib() noexcept : _fluxMag0(0.0), _fluxMag0Err(0.0) {}
106 Calib::Calib(
double fluxMag0) : _fluxMag0(fluxMag0), _fluxMag0Err(0.0) {}
108 if (calibs.empty()) {
110 "You must provide at least one input Calib");
113 double const fluxMag00 = calibs[0]->_fluxMag0;
114 double const fluxMag0Err0 = calibs[0]->_fluxMag0Err;
123 (boost::format(
"You may only combine calibs with the same fluxMag0: " 124 "%g +- %g v %g +- %g") %
126 calibs[0]->getFluxMag0().first % calibs[0]->getFluxMag0().second)
135 auto key =
"FLUXMAG0";
136 if (metadata->exists(
key)) {
137 fluxMag0 = metadata->getAsDouble(
key);
140 if (metadata->exists(
key)) {
148 bool Calib::_throwOnNegativeFlux =
true;
163 auto key =
"FLUXMAG0";
164 if (metadata->exists(
key)) {
165 metadata->remove(
key);
170 if (metadata->exists(
key)) {
171 metadata->remove(
key);
180 return _fluxMag0 == rhs._fluxMag0 && _fluxMag0Err == rhs._fluxMag0Err;
188 _fluxMag0 = fluxMag0AndErr.first;
189 _fluxMag0Err = fluxMag0AndErr.second;
196 inline void checkNegativeFlux0(
double fluxMag0) {
199 (boost::format(
"Flux of 0-mag object must be >= 0: saw %g") % fluxMag0).str());
202 inline bool isNegativeFlux(
double flux,
bool doThrow) {
206 (boost::format(
"Flux must be >= 0: saw %g") % flux).str());
212 inline double convertToFlux(
double fluxMag0,
double mag) {
return fluxMag0 * ::pow(10.0, -0.4 * mag); }
213 inline double convertToFluxErr(
double fluxMag0InvSNR,
double flux,
double magErr) {
217 double a = fluxMag0InvSNR;
218 double b = 0.4 *
std::log(10.0) * magErr;
219 if (std::abs(a) < std::abs(b)) {
224 inline double convertToMag(
double fluxMag0,
double flux) {
return -2.5 * ::log10(flux / fluxMag0); }
226 inline void convertToMagWithErr(
double* mag,
double* magErr,
double fluxMag0,
double fluxMag0Err,
double flux,
229 double const x = fluxErr / flux;
237 checkNegativeFlux0(_fluxMag0);
238 return convertToFlux(_fluxMag0, mag);
240 ndarray::Array<double, 1>
Calib::getFlux(ndarray::Array<double const, 1>
const& mag)
const {
241 checkNegativeFlux0(_fluxMag0);
242 ndarray::Array<double, 1> flux = ndarray::allocate(mag.size());
243 ndarray::Array<double const, 1>::Iterator inIter = mag.begin();
244 ndarray::Array<double, 1>::Iterator outIter = flux.begin();
245 for (; inIter != mag.end(); ++inIter, ++outIter) {
246 *outIter = convertToFlux(_fluxMag0, *inIter);
252 checkNegativeFlux0(_fluxMag0);
253 double const flux = convertToFlux(_fluxMag0, mag);
254 double const fluxErr = convertToFluxErr(_fluxMag0Err / _fluxMag0, flux, magSigma);
259 ndarray::Array<double const, 1>
const& mag, ndarray::Array<double const, 1>
const& magErr)
const {
260 checkNegativeFlux0(_fluxMag0);
261 if (mag.size() != magErr.size()) {
264 (boost::format(
"Size of mag (%d) and magErr (%d) don't match") % mag.size() % magErr.size())
268 ndarray::Array<double, 1> flux = ndarray::allocate(mag.size());
269 ndarray::Array<double, 1> fluxErr = ndarray::allocate(mag.size());
270 ndarray::Array<double const, 1>::Iterator magIter = mag.begin();
271 ndarray::Array<double const, 1>::Iterator magErrIter = magErr.begin();
272 ndarray::Array<double, 1>::Iterator fluxIter = flux.begin();
273 ndarray::Array<double, 1>::Iterator fluxErrIter = fluxErr.begin();
275 double fluxMag0InvSNR = _fluxMag0Err / _fluxMag0;
276 for (; magIter != mag.end(); ++magIter, ++magErrIter, ++fluxIter, ++fluxErrIter) {
277 *fluxIter = convertToFlux(_fluxMag0, *magIter);
278 *fluxErrIter = convertToFluxErr(fluxMag0InvSNR, *fluxIter, *magErrIter);
285 checkNegativeFlux0(_fluxMag0);
289 return convertToMag(_fluxMag0, flux);
293 checkNegativeFlux0(_fluxMag0);
300 convertToMagWithErr(&mag, &magErr, _fluxMag0, _fluxMag0Err, flux, fluxErr);
305 checkNegativeFlux0(_fluxMag0);
306 ndarray::Array<double, 1> mag = ndarray::allocate(flux.size());
307 ndarray::Array<double const, 1>::Iterator fluxIter = flux.begin();
308 ndarray::Array<double, 1>::Iterator magIter = mag.begin();
310 for (; fluxIter != flux.end(); ++fluxIter, ++magIter) {
311 if (isNegativeFlux(*fluxIter,
false)) {
316 *magIter = convertToMag(_fluxMag0, *fluxIter);
320 (boost::format(
"Flux must be >= 0: %d non-positive seen") % nonPositive).str());
326 ndarray::Array<double const, 1>
const& flux, ndarray::Array<double const, 1>
const& fluxErr)
const {
327 checkNegativeFlux0(_fluxMag0);
328 if (flux.size() != fluxErr.size()) {
330 (boost::format(
"Size of flux (%d) and fluxErr (%d) don't match") % flux.size() %
335 ndarray::Array<double, 1> mag = ndarray::allocate(flux.size());
336 ndarray::Array<double, 1> magErr = ndarray::allocate(flux.size());
337 ndarray::Array<double const, 1>::Iterator fluxIter = flux.begin();
338 ndarray::Array<double const, 1>::Iterator fluxErrIter = fluxErr.begin();
339 ndarray::Array<double, 1>::Iterator magIter = mag.begin();
340 ndarray::Array<double, 1>::Iterator magErrIter = magErr.begin();
342 for (; fluxIter != flux.end(); ++fluxIter, ++fluxErrIter, ++magIter, ++magErrIter) {
343 if (isNegativeFlux(*fluxIter,
false)) {
351 convertToMagWithErr(&f, &df, _fluxMag0, _fluxMag0Err, *fluxIter, *fluxErrIter);
357 (boost::format(
"Flux must be >= 0: %d non-positive seen") % nonPositive).str());
364 int const CALIB_TABLE_CURRENT_VERSION = 2;
376 CalibKeys(
const CalibKeys&) =
delete;
377 CalibKeys&
operator=(
const CalibKeys&) =
delete;
380 CalibKeys(CalibKeys&&) =
delete;
381 CalibKeys&
operator=(CalibKeys&&) =
delete;
383 CalibKeys(
int tableVersion = CALIB_TABLE_CURRENT_VERSION)
384 : schema(), midTime(), expTime(), fluxMag0(), fluxMag0Err() {
385 if (tableVersion == 1) {
388 "midtime",
"middle of the time of the exposure relative to Unix epoch",
"ns");
389 expTime = schema.addField<
double>(EXPTIME_FIELD_NAME,
"exposure time",
"s");
391 fluxMag0 = schema.addField<
double>(
"fluxmag0",
"flux of a zero-magnitude object",
"count");
392 fluxMag0Err = schema.addField<
double>(
"fluxmag0.err",
"1-sigma error on fluxmag0",
"count");
396 class CalibFactory :
public table::io::PersistableFactory {
399 CatalogVector
const& catalogs)
const override {
402 int tableVersion = 1;
404 catalogs.front().getSchema().find<
double>(EXPTIME_FIELD_NAME);
405 }
catch (pex::exceptions::NotFoundError) {
406 tableVersion = CALIB_TABLE_CURRENT_VERSION;
409 CalibKeys
const keys{tableVersion};
413 table::BaseRecord
const& record = catalogs.front().front();
415 result->setFluxMag0(record.get(
keys.fluxMag0), record.get(
keys.fluxMag0Err));
419 explicit CalibFactory(
std::string const&
name) : table::io::PersistableFactory(name) {}
422 std::string getCalibPersistenceName() {
return "Calib"; }
424 CalibFactory registration(getCalibPersistenceName());
431 CalibKeys
const keys{};
435 record->set(
keys.fluxMag0, fluxMag0.first);
436 record->set(
keys.fluxMag0Err, fluxMag0.second);
442 _fluxMag0Err *=
scale;
447 #define INSTANTIATE(TYPE) \ 448 template ndarray::Array<TYPE, 1> abMagFromFlux(ndarray::Array<TYPE const, 1> const& flux); \ 449 template ndarray::Array<TYPE, 1> abMagErrFromFluxErr(ndarray::Array<TYPE const, 1> const& fluxErr, \ 450 ndarray::Array<TYPE const, 1> const& flux); \ 451 template ndarray::Array<TYPE, 1> fluxFromABMag(ndarray::Array<TYPE const, 1> const& mag); \ 452 template ndarray::Array<TYPE, 1> fluxErrFromABMagErr(ndarray::Array<TYPE const, 1> const& magErr, \ 453 ndarray::Array<TYPE const, 1> const& mag);
template ndarray::Array< double, 1 > abMagErrFromFluxErr(ndarray::Array< double const, 1 > const &fluxErr, ndarray::Array< double const, 1 > const &flux)
An object passed to Persistable::write to allow it to persist itself.
#define INSTANTIATE(TYPE)
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
static void setThrowOnNegativeFlux(bool raiseException) noexcept
Set whether Calib should throw an exception when asked to convert a flux to a magnitude.
def scale(algorithm, min, max=None, frame=None)
double getFlux(double const mag) const
Return a flux (in ADUs) given a magnitude.
template ndarray::Array< double, 1 > fluxFromABMag(ndarray::Array< double const, 1 > const &mag)
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Calib & operator=(Calib const &) noexcept
table::Key< double > expTime
static bool getThrowOnNegativeFlux() noexcept
Tell me whether Calib will throw an exception if asked to convert a flux to a magnitude.
Describe an exposure's calibration.
Calib & operator*=(double const scale)
template ndarray::Array< double, 1 > abMagFromFlux(ndarray::Array< double const, 1 > const &flux)
table::Key< std::int64_t > midTime
A base class for image defects.
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
void setFluxMag0(double fluxMag0, double fluxMag0Err=0.0)
Set the flux of a zero-magnitude object.
int stripCalibKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Calib-related keywords from the metadata.
template ndarray::Array< double, 1 > fluxErrFromABMagErr(ndarray::Array< double const, 1 > const &magErr, ndarray::Array< double const, 1 > const &mag)
std::unique_ptr< SchemaItem< U > > result
double getMagnitude(double const flux) const
Return a magnitude given a flux.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
#define LSST_EXCEPT(type,...)
~Calib() noexcept override
table::Key< double > fluxMag0Err
table::Key< double > fluxMag0
std::pair< double, double > getFluxMag0() const
Return the flux, and error in flux, of a zero-magnitude object.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
bool operator==(Calib const &rhs) const noexcept
Are two Calibs identical?
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.