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;
153 auto key =
"FLUXMAG0";
154 if (metadata->exists(key)) {
155 metadata->remove(key);
160 if (metadata->exists(key)) {
161 metadata->remove(key);
170 return _fluxMag0 == rhs._fluxMag0 && _fluxMag0Sigma == rhs._fluxMag0Sigma;
178 _fluxMag0 = fluxMag0AndSigma.first;
179 _fluxMag0Sigma = fluxMag0AndSigma.second;
186 inline void checkNegativeFlux0(
double fluxMag0) {
189 (boost::format(
"Flux of 0-mag object must be >= 0: saw %g") % fluxMag0).str());
192 inline bool isNegativeFlux(
double flux,
bool doThrow) {
196 (boost::format(
"Flux must be >= 0: saw %g") % flux).str());
202 inline double convertToFlux(
double fluxMag0,
double mag) {
return fluxMag0 * ::pow(10.0, -0.4 * mag); }
203 inline double convertToFluxErr(
double fluxMag0InvSNR,
double flux,
double magErr) {
207 double a = fluxMag0InvSNR;
208 double b = 0.4 *
std::log(10.0) * magErr;
209 if (std::abs(a) < std::abs(b)) {
214 inline double convertToMag(
double fluxMag0,
double flux) {
return -2.5 * ::log10(flux / fluxMag0); }
216 inline void convertToMagWithErr(
double* mag,
double* magErr,
double fluxMag0,
double fluxMag0Err,
217 double flux,
double fluxErr) {
219 double const x = fluxErr/flux;
220 double const y = fluxMag0Err/
fluxMag0;
227 checkNegativeFlux0(_fluxMag0);
228 return convertToFlux(_fluxMag0, mag);
230 ndarray::Array<double, 1>
Calib::getFlux(ndarray::Array<double const, 1>
const& mag)
const {
231 checkNegativeFlux0(_fluxMag0);
232 ndarray::Array<double, 1> flux = ndarray::allocate(mag.size());
233 ndarray::Array<double const, 1>::Iterator inIter = mag.begin();
234 ndarray::Array<double, 1>::Iterator outIter = flux.begin();
235 for (; inIter != mag.end(); ++inIter, ++outIter) {
236 *outIter = convertToFlux(_fluxMag0, *inIter);
242 checkNegativeFlux0(_fluxMag0);
243 double const flux = convertToFlux(_fluxMag0, mag);
244 double const fluxErr = convertToFluxErr(_fluxMag0Sigma / _fluxMag0, flux, magSigma);
249 ndarray::Array<double const, 1>
const& mag, ndarray::Array<double const, 1>
const& magErr)
const {
250 checkNegativeFlux0(_fluxMag0);
251 if (mag.size() != magErr.size()) {
254 (boost::format(
"Size of mag (%d) and magErr (%d) don't match") % mag.size() % magErr.size())
258 ndarray::Array<double, 1> flux = ndarray::allocate(mag.size());
259 ndarray::Array<double, 1> fluxErr = ndarray::allocate(mag.size());
260 ndarray::Array<double const, 1>::Iterator magIter = mag.begin();
261 ndarray::Array<double const, 1>::Iterator magErrIter = magErr.begin();
262 ndarray::Array<double, 1>::Iterator fluxIter = flux.begin();
263 ndarray::Array<double, 1>::Iterator fluxErrIter = fluxErr.begin();
265 double fluxMag0InvSNR = _fluxMag0Sigma / _fluxMag0;
266 for (; magIter != mag.end(); ++magIter, ++magErrIter, ++fluxIter, ++fluxErrIter) {
267 *fluxIter = convertToFlux(_fluxMag0, *magIter);
268 *fluxErrIter = convertToFluxErr(fluxMag0InvSNR, *fluxIter, *magErrIter);
275 checkNegativeFlux0(_fluxMag0);
279 return convertToMag(_fluxMag0, flux);
283 checkNegativeFlux0(_fluxMag0);
290 convertToMagWithErr(&mag, &magErr, _fluxMag0, _fluxMag0Sigma, flux, fluxErr);
295 checkNegativeFlux0(_fluxMag0);
296 ndarray::Array<double, 1> mag = ndarray::allocate(flux.size());
297 ndarray::Array<double const, 1>::Iterator fluxIter = flux.begin();
298 ndarray::Array<double, 1>::Iterator magIter = mag.begin();
300 for (; fluxIter != flux.end(); ++fluxIter, ++magIter) {
301 if (isNegativeFlux(*fluxIter,
false)) {
306 *magIter = convertToMag(_fluxMag0, *fluxIter);
310 (boost::format(
"Flux must be >= 0: %d non-positive seen") % nonPositive).str());
316 ndarray::Array<double const, 1>
const& flux, ndarray::Array<double const, 1>
const& fluxErr)
const {
317 checkNegativeFlux0(_fluxMag0);
318 if (flux.size() != fluxErr.size()) {
320 (boost::format(
"Size of flux (%d) and fluxErr (%d) don't match") % flux.size() %
325 ndarray::Array<double, 1> mag = ndarray::allocate(flux.size());
326 ndarray::Array<double, 1> magErr = ndarray::allocate(flux.size());
327 ndarray::Array<double const, 1>::Iterator fluxIter = flux.begin();
328 ndarray::Array<double const, 1>::Iterator fluxErrIter = fluxErr.begin();
329 ndarray::Array<double, 1>::Iterator magIter = mag.begin();
330 ndarray::Array<double, 1>::Iterator magErrIter = magErr.begin();
332 for (; fluxIter != flux.end(); ++fluxIter, ++fluxErrIter, ++magIter, ++magErrIter) {
333 if (isNegativeFlux(*fluxIter,
false)) {
341 convertToMagWithErr(&f, &df, _fluxMag0, _fluxMag0Sigma, *fluxIter, *fluxErrIter);
347 (boost::format(
"Flux must be >= 0: %d non-positive seen") % nonPositive).str());
354 int const CALIB_TABLE_CURRENT_VERSION = 2;
366 CalibKeys(
const CalibKeys&) =
delete;
367 CalibKeys&
operator=(
const CalibKeys&) =
delete;
370 CalibKeys(CalibKeys&&) =
delete;
371 CalibKeys&
operator=(CalibKeys&&) =
delete;
373 CalibKeys(
int tableVersion = CALIB_TABLE_CURRENT_VERSION)
374 : schema(), midTime(), expTime(), fluxMag0(), fluxMag0Sigma() {
375 if (tableVersion == 1) {
378 "midtime",
"middle of the time of the exposure relative to Unix epoch",
"ns");
379 expTime = schema.addField<
double>(EXPTIME_FIELD_NAME,
"exposure time",
"s");
381 fluxMag0 = schema.addField<
double>(
"fluxmag0",
"flux of a zero-magnitude object",
"count");
382 fluxMag0Sigma = schema.addField<
double>(
"fluxmag0.err",
"1-sigma error on fluxmag0",
"count");
386 class CalibFactory :
public table::io::PersistableFactory {
389 CatalogVector
const& catalogs)
const {
392 int tableVersion = 1;
394 catalogs.front().getSchema().find<
double>(EXPTIME_FIELD_NAME);
395 }
catch (pex::exceptions::NotFoundError) {
396 tableVersion = CALIB_TABLE_CURRENT_VERSION;
399 CalibKeys
const keys{tableVersion};
403 table::BaseRecord
const& record = catalogs.front().front();
405 result->setFluxMag0(record.get(keys.fluxMag0), record.get(keys.fluxMag0Sigma));
409 explicit CalibFactory(
std::string const&
name) : table::io::PersistableFactory(name) {}
412 std::string getCalibPersistenceName() {
return "Calib"; }
414 CalibFactory registration(getCalibPersistenceName());
421 CalibKeys
const keys{};
425 record->set(keys.fluxMag0, fluxMag0.first);
426 record->set(keys.fluxMag0Sigma, fluxMag0.second);
432 _fluxMag0Sigma *=
scale;
438 #define INSTANTIATE(TYPE) \ 439 template ndarray::Array<TYPE, 1> abMagFromFlux(ndarray::Array<TYPE const, 1> const& flux); \ 440 template ndarray::Array<TYPE, 1> abMagErrFromFluxErr(ndarray::Array<TYPE const, 1> const& fluxErr, \ 441 ndarray::Array<TYPE const, 1> const& flux); \ 442 template ndarray::Array<TYPE, 1> fluxFromABMag(ndarray::Array<TYPE const, 1> const& mag); \ 443 template ndarray::Array<TYPE, 1> fluxErrFromABMagErr(ndarray::Array<TYPE const, 1> const& magErr, \ 444 ndarray::Array<TYPE const, 1> const& mag);
table::Key< std::string > name
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)
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.
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.
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.
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.
#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.