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();
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); }
162 inline void convertToMagWithErr(
double* mag,
double* magErr,
double fluxMag0,
double fluxMag0Err,
163 double flux,
double fluxErr) {
164 *mag = -2.5*std::log10(flux/fluxMag0);
165 double const x = fluxErr/flux;
166 double const y = fluxMag0Err/
fluxMag0;
167 *magErr = (2.5/std::log(10.0))*std::sqrt(x*x + y*y);
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, 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 for (; fluxIter != flux.end(); ++fluxIter, ++fluxErrIter, ++magIter, ++magErrIter) {
279 if (isNegativeFlux(*fluxIter,
false)) {
281 double const NaN = std::numeric_limits<double>::quiet_NaN();
287 convertToMagWithErr(&f, &df, _fluxMag0, _fluxMag0Sigma, *fluxIter, *fluxErrIter);
292 throw LSST_EXCEPT(lsst::pex::exceptions::DomainError,
293 (boost::format(
"Flux must be >= 0: %d non-positive seen") % nonPositive).str());
295 return std::make_pair(mag, magErr);
300 int const CALIB_TABLE_CURRENT_VERSION = 2;
301 std::string
const EXPTIME_FIELD_NAME =
"exptime";
312 CalibKeys(
const CalibKeys&) =
delete;
313 CalibKeys&
operator=(
const CalibKeys&) =
delete;
316 CalibKeys(CalibKeys&&) =
delete;
317 CalibKeys&
operator=(CalibKeys&&) =
delete;
319 CalibKeys(
int tableVersion = CALIB_TABLE_CURRENT_VERSION)
320 : schema(), midTime(), expTime(), fluxMag0(), fluxMag0Sigma() {
321 if (tableVersion == 1) {
323 midTime = schema.addField<std::int64_t>(
324 "midtime",
"middle of the time of the exposure relative to Unix epoch",
"ns");
325 expTime = schema.addField<
double>(EXPTIME_FIELD_NAME,
"exposure time",
"s");
327 fluxMag0 = schema.addField<
double>(
"fluxmag0",
"flux of a zero-magnitude object",
"count");
328 fluxMag0Sigma = schema.addField<
double>(
"fluxmag0.err",
"1-sigma error on fluxmag0",
"count");
332 class CalibFactory :
public table::io::PersistableFactory {
334 virtual std::shared_ptr<table::io::Persistable> read(InputArchive
const& archive,
335 CatalogVector
const& catalogs)
const {
338 int tableVersion = 1;
340 catalogs.front().getSchema().find<
double>(EXPTIME_FIELD_NAME);
341 }
catch (pex::exceptions::NotFoundError) {
342 tableVersion = CALIB_TABLE_CURRENT_VERSION;
345 CalibKeys
const keys{tableVersion};
349 table::BaseRecord
const& record = catalogs.front().front();
351 result->setFluxMag0(record.get(keys.fluxMag0), record.get(keys.fluxMag0Sigma));
355 explicit CalibFactory(std::string
const&
name) : table::io::PersistableFactory(name) {}
358 std::string getCalibPersistenceName() {
return "Calib"; }
360 CalibFactory registration(getCalibPersistenceName());
367 CalibKeys
const keys{};
369 std::shared_ptr<table::BaseRecord> record = cat.
addNew();
371 record->set(keys.fluxMag0, fluxMag0.first);
372 record->set(keys.fluxMag0Sigma, fluxMag0.second);
378 _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.
def scale(algorithm, min, max=None, frame=None)
double getFlux(double const mag) const
Return a flux (in ADUs) given a magnitude.
table::Key< double > fluxMag0Sigma
Describe an exposure's calibration.
Calib & operator*=(double const scale)
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
A base class for image defects.
void operator=(Persistable const &other)
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
int stripCalibKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Calib-related keywords from the metadata.
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.
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.
table::Key< double > expTime
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
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.
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.