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 (boost::format(
"Length mismatch: %d vs %d") % flux.getNumElements() %
65 fluxErr.getNumElements()).str());
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 (boost::format(
"Length mismatch: %d vs %d") % mag.getNumElements() %
91 magErr.getNumElements()).str());
93 ndarray::Array<T, 1> out = ndarray::allocate(mag.getShape());
94 for (
std::size_t ii = 0; ii < mag.getNumElements(); ++ii) {
104 if (calibs.empty()) {
106 "You must provide at least one input Calib");
109 double const fluxMag00 = calibs[0]->_fluxMag0;
110 double const fluxMag0Sigma0 = calibs[0]->_fluxMag0Sigma;
119 (boost::format(
"You may only combine calibs with the same fluxMag0: " 120 "%g +- %g v %g +- %g") %
122 calibs[0]->getFluxMag0().first % calibs[0]->getFluxMag0().second)
131 auto key =
"FLUXMAG0";
132 if (metadata->exists(
key)) {
133 fluxMag0 = metadata->getAsDouble(
key);
136 if (metadata->exists(
key)) {
144 bool Calib::_throwOnNegativeFlux =
true;
159 auto key =
"FLUXMAG0";
160 if (metadata->exists(
key)) {
161 metadata->remove(
key);
166 if (metadata->exists(
key)) {
167 metadata->remove(
key);
176 return _fluxMag0 == rhs._fluxMag0 && _fluxMag0Sigma == rhs._fluxMag0Sigma;
184 _fluxMag0 = fluxMag0AndSigma.first;
185 _fluxMag0Sigma = fluxMag0AndSigma.second;
192 inline void checkNegativeFlux0(
double fluxMag0) {
195 (boost::format(
"Flux of 0-mag object must be >= 0: saw %g") % fluxMag0).str());
198 inline bool isNegativeFlux(
double flux,
bool doThrow) {
202 (boost::format(
"Flux must be >= 0: saw %g") % flux).str());
208 inline double convertToFlux(
double fluxMag0,
double mag) {
return fluxMag0 * ::pow(10.0, -0.4 * mag); }
209 inline double convertToFluxErr(
double fluxMag0InvSNR,
double flux,
double magErr) {
213 double a = fluxMag0InvSNR;
214 double b = 0.4 *
std::log(10.0) * magErr;
215 if (std::abs(a) < std::abs(b)) {
220 inline double convertToMag(
double fluxMag0,
double flux) {
return -2.5 * ::log10(flux / fluxMag0); }
222 inline void convertToMagWithErr(
double* mag,
double* magErr,
double fluxMag0,
double fluxMag0Err,
223 double flux,
double fluxErr) {
225 double const x = fluxErr/flux;
233 checkNegativeFlux0(_fluxMag0);
234 return convertToFlux(_fluxMag0, mag);
236 ndarray::Array<double, 1>
Calib::getFlux(ndarray::Array<double const, 1>
const& mag)
const {
237 checkNegativeFlux0(_fluxMag0);
238 ndarray::Array<double, 1> flux = ndarray::allocate(mag.size());
239 ndarray::Array<double const, 1>::Iterator inIter = mag.begin();
240 ndarray::Array<double, 1>::Iterator outIter = flux.begin();
241 for (; inIter != mag.end(); ++inIter, ++outIter) {
242 *outIter = convertToFlux(_fluxMag0, *inIter);
248 checkNegativeFlux0(_fluxMag0);
249 double const flux = convertToFlux(_fluxMag0, mag);
250 double const fluxErr = convertToFluxErr(_fluxMag0Sigma / _fluxMag0, flux, magSigma);
255 ndarray::Array<double const, 1>
const& mag, ndarray::Array<double const, 1>
const& magErr)
const {
256 checkNegativeFlux0(_fluxMag0);
257 if (mag.size() != magErr.size()) {
260 (boost::format(
"Size of mag (%d) and magErr (%d) don't match") % mag.size() % magErr.size())
264 ndarray::Array<double, 1> flux = ndarray::allocate(mag.size());
265 ndarray::Array<double, 1> fluxErr = ndarray::allocate(mag.size());
266 ndarray::Array<double const, 1>::Iterator magIter = mag.begin();
267 ndarray::Array<double const, 1>::Iterator magErrIter = magErr.begin();
268 ndarray::Array<double, 1>::Iterator fluxIter = flux.begin();
269 ndarray::Array<double, 1>::Iterator fluxErrIter = fluxErr.begin();
271 double fluxMag0InvSNR = _fluxMag0Sigma / _fluxMag0;
272 for (; magIter != mag.end(); ++magIter, ++magErrIter, ++fluxIter, ++fluxErrIter) {
273 *fluxIter = convertToFlux(_fluxMag0, *magIter);
274 *fluxErrIter = convertToFluxErr(fluxMag0InvSNR, *fluxIter, *magErrIter);
281 checkNegativeFlux0(_fluxMag0);
285 return convertToMag(_fluxMag0, flux);
289 checkNegativeFlux0(_fluxMag0);
296 convertToMagWithErr(&mag, &magErr, _fluxMag0, _fluxMag0Sigma, flux, fluxErr);
301 checkNegativeFlux0(_fluxMag0);
302 ndarray::Array<double, 1> mag = ndarray::allocate(flux.size());
303 ndarray::Array<double const, 1>::Iterator fluxIter = flux.begin();
304 ndarray::Array<double, 1>::Iterator magIter = mag.begin();
306 for (; fluxIter != flux.end(); ++fluxIter, ++magIter) {
307 if (isNegativeFlux(*fluxIter,
false)) {
312 *magIter = convertToMag(_fluxMag0, *fluxIter);
316 (boost::format(
"Flux must be >= 0: %d non-positive seen") % nonPositive).str());
322 ndarray::Array<double const, 1>
const& flux, ndarray::Array<double const, 1>
const& fluxErr)
const {
323 checkNegativeFlux0(_fluxMag0);
324 if (flux.size() != fluxErr.size()) {
326 (boost::format(
"Size of flux (%d) and fluxErr (%d) don't match") % flux.size() %
331 ndarray::Array<double, 1> mag = ndarray::allocate(flux.size());
332 ndarray::Array<double, 1> magErr = ndarray::allocate(flux.size());
333 ndarray::Array<double const, 1>::Iterator fluxIter = flux.begin();
334 ndarray::Array<double const, 1>::Iterator fluxErrIter = fluxErr.begin();
335 ndarray::Array<double, 1>::Iterator magIter = mag.begin();
336 ndarray::Array<double, 1>::Iterator magErrIter = magErr.begin();
338 for (; fluxIter != flux.end(); ++fluxIter, ++fluxErrIter, ++magIter, ++magErrIter) {
339 if (isNegativeFlux(*fluxIter,
false)) {
347 convertToMagWithErr(&f, &df, _fluxMag0, _fluxMag0Sigma, *fluxIter, *fluxErrIter);
353 (boost::format(
"Flux must be >= 0: %d non-positive seen") % nonPositive).str());
360 int const CALIB_TABLE_CURRENT_VERSION = 2;
372 CalibKeys(
const CalibKeys&) =
delete;
373 CalibKeys&
operator=(
const CalibKeys&) =
delete;
376 CalibKeys(CalibKeys&&) =
delete;
377 CalibKeys&
operator=(CalibKeys&&) =
delete;
379 CalibKeys(
int tableVersion = CALIB_TABLE_CURRENT_VERSION)
380 : schema(), midTime(), expTime(), fluxMag0(), fluxMag0Sigma() {
381 if (tableVersion == 1) {
384 "midtime",
"middle of the time of the exposure relative to Unix epoch",
"ns");
385 expTime = schema.addField<
double>(EXPTIME_FIELD_NAME,
"exposure time",
"s");
387 fluxMag0 = schema.addField<
double>(
"fluxmag0",
"flux of a zero-magnitude object",
"count");
388 fluxMag0Sigma = schema.addField<
double>(
"fluxmag0.err",
"1-sigma error on fluxmag0",
"count");
392 class CalibFactory :
public table::io::PersistableFactory {
395 CatalogVector
const& catalogs)
const {
398 int tableVersion = 1;
400 catalogs.front().getSchema().find<
double>(EXPTIME_FIELD_NAME);
401 }
catch (pex::exceptions::NotFoundError) {
402 tableVersion = CALIB_TABLE_CURRENT_VERSION;
405 CalibKeys
const keys{tableVersion};
409 table::BaseRecord
const& record = catalogs.front().front();
411 result->setFluxMag0(record.get(
keys.fluxMag0), record.get(
keys.fluxMag0Sigma));
415 explicit CalibFactory(
std::string const&
name) : table::io::PersistableFactory(name) {}
418 std::string getCalibPersistenceName() {
return "Calib"; }
420 CalibFactory registration(getCalibPersistenceName());
427 CalibKeys
const keys{};
431 record->set(
keys.fluxMag0, fluxMag0.first);
432 record->set(
keys.fluxMag0Sigma, fluxMag0.second);
438 _fluxMag0Sigma *=
scale;
444 #define INSTANTIATE(TYPE) \ 445 template ndarray::Array<TYPE, 1> abMagFromFlux(ndarray::Array<TYPE const, 1> const& flux); \ 446 template ndarray::Array<TYPE, 1> abMagErrFromFluxErr(ndarray::Array<TYPE const, 1> const& fluxErr, \ 447 ndarray::Array<TYPE const, 1> const& flux); \ 448 template ndarray::Array<TYPE, 1> fluxFromABMag(ndarray::Array<TYPE const, 1> const& mag); \ 449 template ndarray::Array<TYPE, 1> fluxErrFromABMagErr(ndarray::Array<TYPE const, 1> const& magErr, \ 450 ndarray::Array<TYPE const, 1> const& mag);
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.
double fluxErrFromABMagErr(double magErr, double mag)
Compute flux error in Janskys from AB magnitude error and AB magnitude.
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 > expTime
double abMagFromFlux(double flux)
Compute AB magnitude from flux in Janskys.
Describe an exposure's calibration.
Calib & operator*=(double const scale)
double fluxFromABMag(double mag)
Compute flux in Janskys from AB magnitude.
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.
static void setThrowOnNegativeFlux(bool raiseException)
Set whether Calib should throw an exception when asked to convert a flux to a magnitude.
Calib & operator=(Calib const &)
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.
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.
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.