32 #include "boost/format.hpp"
33 #include "boost/algorithm/string/trim.hpp"
36 #include "lsst/pex/exceptions.h"
37 #include "lsst/daf/base/PropertySet.h"
49 Calib::Calib(std::vector<std::shared_ptr<Calib const>>
const& calibs) : _fluxMag0(0.0), _fluxMag0Sigma(0.0) {
51 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
52 "You must provide at least one input Calib");
55 double const fluxMag00 = calibs[0]->_fluxMag0;
56 double const fluxMag0Sigma0 = calibs[0]->_fluxMag0Sigma;
58 for (std::vector<std::shared_ptr<Calib const>>::const_iterator ptr = calibs.begin(); ptr != calibs.end();
60 Calib const& calib = **ptr;
62 if (::fabs(fluxMag00 - calib._fluxMag0) > std::numeric_limits<double>::epsilon() ||
63 ::fabs(fluxMag0Sigma0 - calib._fluxMag0Sigma) > std::numeric_limits<double>::epsilon()) {
64 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
65 (boost::format(
"You may only combine calibs with the same fluxMag0: "
66 "%g +- %g v %g +- %g") %
68 calibs[0]->getFluxMag0().first % calibs[0]->getFluxMag0().second)
74 Calib::Calib(std::shared_ptr<lsst::daf::base::PropertySet const> metadata) {
77 auto key =
"FLUXMAG0";
78 if (metadata->exists(key)) {
79 fluxMag0 = metadata->getAsDouble(key);
82 if (metadata->exists(key)) {
90 bool Calib::_throwOnNegativeFlux =
true;
99 auto key =
"FLUXMAG0";
100 if (metadata->exists(key)) {
101 metadata->remove(key);
106 if (metadata->exists(key)) {
107 metadata->remove(key);
116 return _fluxMag0 == rhs._fluxMag0 && _fluxMag0Sigma == rhs._fluxMag0Sigma;
124 _fluxMag0 = fluxMag0AndSigma.first;
125 _fluxMag0Sigma = fluxMag0AndSigma.second;
128 std::pair<double, double>
Calib::getFluxMag0()
const {
return std::make_pair(_fluxMag0, _fluxMag0Sigma); }
132 inline void checkNegativeFlux0(
double fluxMag0) {
134 throw LSST_EXCEPT(lsst::pex::exceptions::DomainError,
135 (boost::format(
"Flux of 0-mag object must be >= 0: saw %g") % fluxMag0).str());
138 inline bool isNegativeFlux(
double flux,
bool doThrow) {
141 throw LSST_EXCEPT(lsst::pex::exceptions::DomainError,
142 (boost::format(
"Flux must be >= 0: saw %g") % flux).str());
148 inline double convertToFlux(
double fluxMag0,
double mag) {
return fluxMag0 * ::pow(10.0, -0.4 * mag); }
149 inline double convertToFluxErr(
double fluxMag0InvSNR,
double flux,
double magErr) {
153 double a = fluxMag0InvSNR;
154 double b = 0.4 * std::log(10.0) * magErr;
155 if (std::abs(a) < std::abs(b)) {
158 return flux * std::abs(a) * std::sqrt(1 + std::pow(b / a, 2));
160 inline double convertToMag(
double fluxMag0,
double flux) {
return -2.5 * ::log10(flux / fluxMag0); }
161 inline void convertToMagWithErr(
double* mag,
double* magErr,
double fluxMag0,
double fluxMag0InvSNR,
162 double flux,
double fluxErr) {
164 double const ratErr = ::sqrt((::pow(fluxErr, 2) + ::pow(flux * fluxMag0InvSNR, 2)) / ::pow(fluxMag0, 2));
166 *mag = -2.5 * ::log10(rat);
167 *magErr = 2.5 / ::log(10.0) * ratErr / rat;
173 checkNegativeFlux0(_fluxMag0);
174 return convertToFlux(_fluxMag0, mag);
176 ndarray::Array<double, 1>
Calib::getFlux(ndarray::Array<double const, 1>
const& mag)
const {
177 checkNegativeFlux0(_fluxMag0);
178 ndarray::Array<double, 1> flux = ndarray::allocate(mag.size());
179 ndarray::Array<double const, 1>::Iterator inIter = mag.begin();
180 ndarray::Array<double, 1>::Iterator outIter = flux.begin();
181 for (; inIter != mag.end(); ++inIter, ++outIter) {
182 *outIter = convertToFlux(_fluxMag0, *inIter);
187 std::pair<double, double>
Calib::getFlux(
double const mag,
double const magSigma)
const {
188 checkNegativeFlux0(_fluxMag0);
189 double const flux = convertToFlux(_fluxMag0, mag);
190 double const fluxErr = convertToFluxErr(_fluxMag0Sigma / _fluxMag0, flux, magSigma);
191 return std::make_pair(flux, fluxErr);
195 ndarray::Array<double const, 1>
const& mag, ndarray::Array<double const, 1>
const& magErr)
const {
196 checkNegativeFlux0(_fluxMag0);
197 if (mag.size() != magErr.size()) {
199 lsst::pex::exceptions::LengthError,
200 (boost::format(
"Size of mag (%d) and magErr (%d) don't match") % mag.size() % magErr.size())
204 ndarray::Array<double, 1> flux = ndarray::allocate(mag.size());
205 ndarray::Array<double, 1> fluxErr = ndarray::allocate(mag.size());
206 ndarray::Array<double const, 1>::Iterator magIter = mag.begin();
207 ndarray::Array<double const, 1>::Iterator magErrIter = magErr.begin();
208 ndarray::Array<double, 1>::Iterator fluxIter = flux.begin();
209 ndarray::Array<double, 1>::Iterator fluxErrIter = fluxErr.begin();
211 double fluxMag0InvSNR = _fluxMag0Sigma / _fluxMag0;
212 for (; magIter != mag.end(); ++magIter, ++magErrIter, ++fluxIter, ++fluxErrIter) {
213 *fluxIter = convertToFlux(_fluxMag0, *magIter);
214 *fluxErrIter = convertToFluxErr(fluxMag0InvSNR, *fluxIter, *magErrIter);
217 return std::make_pair(flux, fluxErr);
221 checkNegativeFlux0(_fluxMag0);
223 return std::numeric_limits<double>::quiet_NaN();
225 return convertToMag(_fluxMag0, flux);
229 checkNegativeFlux0(_fluxMag0);
231 double const NaN = std::numeric_limits<double>::quiet_NaN();
232 return std::make_pair(NaN, NaN);
236 convertToMagWithErr(&mag, &magErr, _fluxMag0, _fluxMag0Sigma / _fluxMag0, flux, fluxErr);
237 return std::make_pair(mag, magErr);
241 checkNegativeFlux0(_fluxMag0);
242 ndarray::Array<double, 1> mag = ndarray::allocate(flux.size());
243 ndarray::Array<double const, 1>::Iterator fluxIter = flux.begin();
244 ndarray::Array<double, 1>::Iterator magIter = mag.begin();
246 for (; fluxIter != flux.end(); ++fluxIter, ++magIter) {
247 if (isNegativeFlux(*fluxIter,
false)) {
249 *magIter = std::numeric_limits<double>::quiet_NaN();
252 *magIter = convertToMag(_fluxMag0, *fluxIter);
255 throw LSST_EXCEPT(lsst::pex::exceptions::DomainError,
256 (boost::format(
"Flux must be >= 0: %d non-positive seen") % nonPositive).str());
262 ndarray::Array<double const, 1>
const& flux, ndarray::Array<double const, 1>
const& fluxErr)
const {
263 checkNegativeFlux0(_fluxMag0);
264 if (flux.size() != fluxErr.size()) {
265 throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
266 (boost::format(
"Size of flux (%d) and fluxErr (%d) don't match") % flux.size() %
271 ndarray::Array<double, 1> mag = ndarray::allocate(flux.size());
272 ndarray::Array<double, 1> magErr = ndarray::allocate(flux.size());
273 ndarray::Array<double const, 1>::Iterator fluxIter = flux.begin();
274 ndarray::Array<double const, 1>::Iterator fluxErrIter = fluxErr.begin();
275 ndarray::Array<double, 1>::Iterator magIter = mag.begin();
276 ndarray::Array<double, 1>::Iterator magErrIter = magErr.begin();
278 double fluxMag0InvSNR = _fluxMag0Sigma / _fluxMag0;
279 for (; fluxIter != flux.end(); ++fluxIter, ++fluxErrIter, ++magIter, ++magErrIter) {
280 if (isNegativeFlux(*fluxIter,
false)) {
282 double const NaN = std::numeric_limits<double>::quiet_NaN();
288 convertToMagWithErr(&f, &df, _fluxMag0, fluxMag0InvSNR, *fluxIter, *fluxErrIter);
293 throw LSST_EXCEPT(lsst::pex::exceptions::DomainError,
294 (boost::format(
"Flux must be >= 0: %d non-positive seen") % nonPositive).str());
296 return std::make_pair(mag, magErr);
301 int const CALIB_TABLE_CURRENT_VERSION = 2;
302 std::string
const EXPTIME_FIELD_NAME =
"exptime";
313 CalibKeys(
const CalibKeys&) =
delete;
314 CalibKeys& operator=(
const CalibKeys&) =
delete;
317 CalibKeys(CalibKeys&&) =
delete;
318 CalibKeys& operator=(CalibKeys&&) =
delete;
320 CalibKeys(
int tableVersion = CALIB_TABLE_CURRENT_VERSION)
322 if (tableVersion == 1) {
325 "midtime",
"middle of the time of the exposure relative to Unix epoch",
"ns");
326 expTime =
schema.addField<
double>(EXPTIME_FIELD_NAME,
"exposure time",
"s");
328 fluxMag0 =
schema.addField<
double>(
"fluxmag0",
"flux of a zero-magnitude object",
"count");
329 fluxMag0Sigma =
schema.addField<
double>(
"fluxmag0.err",
"1-sigma error on fluxmag0",
"count");
333 class CalibFactory :
public table::io::PersistableFactory {
335 virtual std::shared_ptr<table::io::Persistable> read(InputArchive
const& archive,
336 CatalogVector
const& catalogs)
const {
339 int tableVersion = 1;
341 catalogs.front().getSchema().find<
double>(EXPTIME_FIELD_NAME);
342 }
catch (pex::exceptions::NotFoundError) {
343 tableVersion = CALIB_TABLE_CURRENT_VERSION;
346 CalibKeys
const keys{tableVersion};
350 table::BaseRecord
const& record = catalogs.front().front();
351 std::shared_ptr<Calib>
result(
new Calib());
352 result->setFluxMag0(record.get(keys.fluxMag0), record.get(keys.fluxMag0Sigma));
356 explicit CalibFactory(std::string
const&
name) : table::io::PersistableFactory(name) {}
359 std::string getCalibPersistenceName() {
return "Calib"; }
361 CalibFactory registration(getCalibPersistenceName());
368 CalibKeys
const keys{};
370 std::shared_ptr<table::BaseRecord> record = cat.
addNew();
371 std::pair<double, double> fluxMag0 =
getFluxMag0();
372 record->set(keys.fluxMag0, fluxMag0.first);
373 record->set(keys.fluxMag0Sigma, fluxMag0.second);
379 _fluxMag0Sigma *=
scale;
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
An object passed to Persistable::write to allow it to persist itself.
A custom container class for records, based on std::vector.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
table::Key< double > fluxMag0Sigma
Describe an exposure's calibration.
Calib & operator*=(double const scale)
bool operator==(Calib const &rhs) const
Are two Calibs identical?
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
static void setThrowOnNegativeFlux(bool raiseException)
Set whether Calib should throw an exception when asked to convert a flux to a magnitude.
table::Key< std::string > name
std::pair< double, double > getFluxMag0() const
Return the flux, and error in flux, of a zero-magnitude object.
int stripCalibKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Calib-related keywords from the metadata.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
table::Key< double > expTime
double getFlux(double const mag) const
Return a flux (in ADUs) given a magnitude.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
table::Key< std::int64_t > midTime
void setFluxMag0(double fluxMag0, double fluxMag0Sigma=0.0)
Set the flux of a zero-magnitude object.
static bool getThrowOnNegativeFlux()
Tell me whether Calib will throw an exception if asked to convert a flux to a magnitude.
table::Key< double > fluxMag0
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
afw::table::Schema schema
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
double getMagnitude(double const flux) const
Return a magnitude given a flux.