32 #include "boost/format.hpp" 33 #include "boost/algorithm/string/trim.hpp" 50 ndarray::Array<T, 1>
abMagFromFlux(ndarray::Array<T const, 1>
const& flux) {
51 ndarray::Array<T, 1> out = ndarray::allocate(flux.getShape());
52 for (
std::size_t ii = 0; ii < flux.getNumElements(); ++ii) {
61 ndarray::Array<T const, 1>
const& flux) {
62 if (flux.getNumElements() != fluxErr.getNumElements()) {
64 flux.getNumElements() % fluxErr.getNumElements())
67 ndarray::Array<T, 1> out = ndarray::allocate(flux.getShape());
68 for (
std::size_t ii = 0; ii < flux.getNumElements(); ++ii) {
76 ndarray::Array<T, 1>
fluxFromABMag(ndarray::Array<T const, 1>
const& mag) {
77 ndarray::Array<T, 1> out = ndarray::allocate(mag.getShape());
78 for (
std::size_t ii = 0; ii < mag.getNumElements(); ++ii) {
87 ndarray::Array<T const, 1>
const& mag) {
88 if (mag.getNumElements() != magErr.getNumElements()) {
90 mag.getNumElements() % magErr.getNumElements())
93 ndarray::Array<T, 1> out = ndarray::allocate(mag.getShape());
94 for (
std::size_t ii = 0; ii < mag.getNumElements(); ++ii) {
103 if (calibs.empty()) {
105 "You must provide at least one input Calib");
108 double const fluxMag00 = calibs[0]->_fluxMag0;
109 double const fluxMag0Sigma0 = calibs[0]->_fluxMag0Sigma;
118 (boost::format(
"You may only combine calibs with the same fluxMag0: " 119 "%g +- %g v %g +- %g") %
121 calibs[0]->getFluxMag0().first % calibs[0]->getFluxMag0().second)
130 auto key =
"FLUXMAG0";
131 if (metadata->exists(
key)) {
132 fluxMag0 = metadata->getAsDouble(
key);
135 if (metadata->exists(
key)) {
143 bool Calib::_throwOnNegativeFlux =
true;
158 auto key =
"FLUXMAG0";
159 if (metadata->exists(
key)) {
160 metadata->remove(
key);
165 if (metadata->exists(
key)) {
166 metadata->remove(
key);
175 return _fluxMag0 == rhs._fluxMag0 && _fluxMag0Sigma == rhs._fluxMag0Sigma;
183 _fluxMag0 = fluxMag0AndSigma.first;
184 _fluxMag0Sigma = fluxMag0AndSigma.second;
191 inline void checkNegativeFlux0(
double fluxMag0) {
194 (boost::format(
"Flux of 0-mag object must be >= 0: saw %g") % fluxMag0).str());
197 inline bool isNegativeFlux(
double flux,
bool doThrow) {
201 (boost::format(
"Flux must be >= 0: saw %g") % flux).str());
207 inline double convertToFlux(
double fluxMag0,
double mag) {
return fluxMag0 * ::pow(10.0, -0.4 * mag); }
208 inline double convertToFluxErr(
double fluxMag0InvSNR,
double flux,
double magErr) {
212 double a = fluxMag0InvSNR;
213 double b = 0.4 *
std::log(10.0) * magErr;
214 if (std::abs(a) < std::abs(b)) {
219 inline double convertToMag(
double fluxMag0,
double flux) {
return -2.5 * ::log10(flux / fluxMag0); }
221 inline void convertToMagWithErr(
double* mag,
double* magErr,
double fluxMag0,
double fluxMag0Err,
double flux,
224 double const x = fluxErr / flux;
232 checkNegativeFlux0(_fluxMag0);
233 return convertToFlux(_fluxMag0, mag);
235 ndarray::Array<double, 1>
Calib::getFlux(ndarray::Array<double const, 1>
const& mag)
const {
236 checkNegativeFlux0(_fluxMag0);
237 ndarray::Array<double, 1> flux = ndarray::allocate(mag.size());
238 ndarray::Array<double const, 1>::Iterator inIter = mag.begin();
239 ndarray::Array<double, 1>::Iterator outIter = flux.begin();
240 for (; inIter != mag.end(); ++inIter, ++outIter) {
241 *outIter = convertToFlux(_fluxMag0, *inIter);
247 checkNegativeFlux0(_fluxMag0);
248 double const flux = convertToFlux(_fluxMag0, mag);
249 double const fluxErr = convertToFluxErr(_fluxMag0Sigma / _fluxMag0, flux, magSigma);
254 ndarray::Array<double const, 1>
const& mag, ndarray::Array<double const, 1>
const& magErr)
const {
255 checkNegativeFlux0(_fluxMag0);
256 if (mag.size() != magErr.size()) {
259 (boost::format(
"Size of mag (%d) and magErr (%d) don't match") % mag.size() % magErr.size())
263 ndarray::Array<double, 1> flux = ndarray::allocate(mag.size());
264 ndarray::Array<double, 1> fluxErr = ndarray::allocate(mag.size());
265 ndarray::Array<double const, 1>::Iterator magIter = mag.begin();
266 ndarray::Array<double const, 1>::Iterator magErrIter = magErr.begin();
267 ndarray::Array<double, 1>::Iterator fluxIter = flux.begin();
268 ndarray::Array<double, 1>::Iterator fluxErrIter = fluxErr.begin();
270 double fluxMag0InvSNR = _fluxMag0Sigma / _fluxMag0;
271 for (; magIter != mag.end(); ++magIter, ++magErrIter, ++fluxIter, ++fluxErrIter) {
272 *fluxIter = convertToFlux(_fluxMag0, *magIter);
273 *fluxErrIter = convertToFluxErr(fluxMag0InvSNR, *fluxIter, *magErrIter);
280 checkNegativeFlux0(_fluxMag0);
284 return convertToMag(_fluxMag0, flux);
288 checkNegativeFlux0(_fluxMag0);
295 convertToMagWithErr(&mag, &magErr, _fluxMag0, _fluxMag0Sigma, flux, fluxErr);
300 checkNegativeFlux0(_fluxMag0);
301 ndarray::Array<double, 1> mag = ndarray::allocate(flux.size());
302 ndarray::Array<double const, 1>::Iterator fluxIter = flux.begin();
303 ndarray::Array<double, 1>::Iterator magIter = mag.begin();
305 for (; fluxIter != flux.end(); ++fluxIter, ++magIter) {
306 if (isNegativeFlux(*fluxIter,
false)) {
311 *magIter = convertToMag(_fluxMag0, *fluxIter);
315 (boost::format(
"Flux must be >= 0: %d non-positive seen") % nonPositive).str());
321 ndarray::Array<double const, 1>
const& flux, ndarray::Array<double const, 1>
const& fluxErr)
const {
322 checkNegativeFlux0(_fluxMag0);
323 if (flux.size() != fluxErr.size()) {
325 (boost::format(
"Size of flux (%d) and fluxErr (%d) don't match") % flux.size() %
330 ndarray::Array<double, 1> mag = ndarray::allocate(flux.size());
331 ndarray::Array<double, 1> magErr = ndarray::allocate(flux.size());
332 ndarray::Array<double const, 1>::Iterator fluxIter = flux.begin();
333 ndarray::Array<double const, 1>::Iterator fluxErrIter = fluxErr.begin();
334 ndarray::Array<double, 1>::Iterator magIter = mag.begin();
335 ndarray::Array<double, 1>::Iterator magErrIter = magErr.begin();
337 for (; fluxIter != flux.end(); ++fluxIter, ++fluxErrIter, ++magIter, ++magErrIter) {
338 if (isNegativeFlux(*fluxIter,
false)) {
346 convertToMagWithErr(&f, &df, _fluxMag0, _fluxMag0Sigma, *fluxIter, *fluxErrIter);
352 (boost::format(
"Flux must be >= 0: %d non-positive seen") % nonPositive).str());
359 int const CALIB_TABLE_CURRENT_VERSION = 2;
371 CalibKeys(
const CalibKeys&) =
delete;
372 CalibKeys&
operator=(
const CalibKeys&) =
delete;
375 CalibKeys(CalibKeys&&) =
delete;
376 CalibKeys&
operator=(CalibKeys&&) =
delete;
378 CalibKeys(
int tableVersion = CALIB_TABLE_CURRENT_VERSION)
379 : schema(), midTime(), expTime(), fluxMag0(), fluxMag0Sigma() {
380 if (tableVersion == 1) {
383 "midtime",
"middle of the time of the exposure relative to Unix epoch",
"ns");
384 expTime = schema.addField<
double>(EXPTIME_FIELD_NAME,
"exposure time",
"s");
386 fluxMag0 = schema.addField<
double>(
"fluxmag0",
"flux of a zero-magnitude object",
"count");
387 fluxMag0Sigma = schema.addField<
double>(
"fluxmag0.err",
"1-sigma error on fluxmag0",
"count");
391 class CalibFactory :
public table::io::PersistableFactory {
394 CatalogVector
const& catalogs)
const {
397 int tableVersion = 1;
399 catalogs.front().getSchema().find<
double>(EXPTIME_FIELD_NAME);
400 }
catch (pex::exceptions::NotFoundError) {
401 tableVersion = CALIB_TABLE_CURRENT_VERSION;
404 CalibKeys
const keys{tableVersion};
408 table::BaseRecord
const& record = catalogs.front().front();
410 result->setFluxMag0(record.get(
keys.fluxMag0), record.get(
keys.fluxMag0Sigma));
414 explicit CalibFactory(
std::string const&
name) : table::io::PersistableFactory(name) {}
417 std::string getCalibPersistenceName() {
return "Calib"; }
419 CalibFactory registration(getCalibPersistenceName());
426 CalibKeys
const keys{};
430 record->set(
keys.fluxMag0, fluxMag0.first);
431 record->set(
keys.fluxMag0Sigma, fluxMag0.second);
437 _fluxMag0Sigma *=
scale;
442 #define INSTANTIATE(TYPE) \ 443 template ndarray::Array<TYPE, 1> abMagFromFlux(ndarray::Array<TYPE const, 1> const& flux); \ 444 template ndarray::Array<TYPE, 1> abMagErrFromFluxErr(ndarray::Array<TYPE const, 1> const& fluxErr, \ 445 ndarray::Array<TYPE const, 1> const& flux); \ 446 template ndarray::Array<TYPE, 1> fluxFromABMag(ndarray::Array<TYPE const, 1> const& mag); \ 447 template ndarray::Array<TYPE, 1> fluxErrFromABMagErr(ndarray::Array<TYPE const, 1> const& magErr, \ 448 ndarray::Array<TYPE const, 1> const& mag); double fluxErrFromABMagErr(double magErr, double mag) noexcept
Compute flux error in Janskys from AB magnitude error and AB magnitude.
table::Key< double > fluxMag0Sigma
An object passed to Persistable::write to allow it to persist itself.
double abMagErrFromFluxErr(double fluxErr, double flux)
Compute AB magnitude error from flux and flux error in Janskys.
#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.
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.
double abMagFromFlux(double flux)
Compute AB magnitude from flux in Janskys.
Describe an exposure's calibration.
Calib & operator*=(double const scale)
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
table::Key< std::int64_t > midTime
A base class for image defects.
double fluxFromABMag(double mag) noexcept
Compute flux in Janskys from AB magnitude.
int stripCalibKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Calib-related keywords from the metadata.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
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,...)
void setFluxMag0(double fluxMag0, double fluxMag0Sigma=0.0)
Set the flux of a zero-magnitude object.
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?
virtual ~Calib() noexcept
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.