49 s << std::setprecision(std::numeric_limits<long double>::digits10 + 1);
50 s <<
"value=" << measurement.
value <<
", error=" << measurement.
error;
56 int const SERIALIZATION_VERSION = 1;
58 double toNanojansky(
double instFlux,
double scale) {
return instFlux *
scale; }
62 double toInstFluxFromMagnitude(
double magnitude,
double scale) {
67 double toNanojanskyErr(
double instFlux,
double instFluxErr,
double scale,
double scaleErr,
69 return std::abs(nanojansky) * hypot(instFluxErr / instFlux, scaleErr / scale);
85 void toNanojanskyVariance(ndarray::Array<float const, 2, 1>
const &instFlux,
86 ndarray::Array<float const, 2, 1>
const &instFluxVar,
float scaleErr,
87 ndarray::Array<float const, 2, 1>
const &flux, ndarray::Array<float, 2, 1> out) {
88 auto eigenFlux = ndarray::asEigen<Eigen::ArrayXpr>(flux);
89 auto eigenInstFluxVar = ndarray::asEigen<Eigen::ArrayXpr>(instFluxVar);
90 auto eigenInstFlux = ndarray::asEigen<Eigen::ArrayXpr>(instFlux);
91 auto eigenOut = ndarray::asEigen<Eigen::ArrayXpr>(out);
92 eigenOut = eigenFlux.square() *
93 (eigenInstFluxVar / eigenInstFlux.square() + (scaleErr / eigenFlux * eigenInstFlux).square());
96 double toMagnitudeErr(
double instFlux,
double instFluxErr,
double scale,
double scaleErr) {
97 return 2.5 /
log(10.0) * hypot(instFluxErr / instFlux, scaleErr / scale);
104 ndarray::Array<double, 1> PhotoCalib::getMagnitude(ndarray::Array<double const, 1>
const &instFlux)
const {
105 ndarray::Array<double, 1>
result = ndarray::allocate(ndarray::makeVector(
int(instFlux.size())));
106 auto iter = result.begin();
107 for (
auto const &i : instFlux) {
108 *iter = toMagnitude(i, _calibrationMean);
115 ndarray::Array<double const, 1>
const &instFlux,
116 ndarray::Array<double const, 1>
const &instFluxErr)
const {
117 ndarray::Array<double, 1> mag = ndarray::allocate(ndarray::makeVector(
int(instFlux.size())));
118 ndarray::Array<double, 1> magErr = ndarray::allocate(ndarray::makeVector(
int(instFlux.size())));
119 auto iMag = mag.begin();
120 auto iMagErr = magErr.begin();
121 for (
auto i = instFlux.begin(), iErr = instFluxErr.begin(); i != instFlux.end(); ++i, ++iErr) {
122 *iMag = toMagnitude(*i, _calibrationMean);
123 *iMagErr = toMagnitudeErr(*i, *iErr, _calibrationMean, _calibrationErr);
133 return toNanojansky(instFlux, evaluate(point));
136 double PhotoCalib::instFluxToNanojansky(
double instFlux)
const {
137 return toNanojansky(instFlux, _calibrationMean);
140 Measurement PhotoCalib::instFluxToNanojansky(
double instFlux,
double instFluxErr,
142 double calibration, error, nanojansky;
143 calibration = evaluate(point);
144 nanojansky = toNanojansky(instFlux, calibration);
145 error = toNanojanskyErr(instFlux, instFluxErr, calibration, _calibrationErr, nanojansky);
149 Measurement PhotoCalib::instFluxToNanojansky(
double instFlux,
double instFluxErr)
const {
150 double nanojansky = toNanojansky(instFlux, _calibrationMean);
151 double error = toNanojanskyErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr, nanojansky);
158 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFlux").
key;
159 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").
key;
160 return instFluxToNanojansky(sourceRecord.
get(instFluxKey), sourceRecord.
get(instFluxErrKey), position);
164 ndarray::Array<double, 2, 2>
result =
165 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
166 instFluxToNanojanskyArray(sourceCatalog, instFluxField, result);
172 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
173 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
174 auto nanojanskyKey = sourceCatalog.getSchema().find<
double>(outField +
"_flux").
key;
175 auto nanojanskyErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_fluxErr").
key;
176 for (
auto &record : sourceCatalog) {
177 auto result = instFluxToNanojansky(record.get(instFluxKey), record.get(instFluxErrKey),
178 record.getCentroid());
179 record.set(nanojanskyKey,
result.value);
180 record.set(nanojanskyErrKey,
result.error);
187 return toMagnitude(instFlux, evaluate(point));
190 double PhotoCalib::instFluxToMagnitude(
double instFlux)
const {
191 return toMagnitude(instFlux, _calibrationMean);
194 Measurement PhotoCalib::instFluxToMagnitude(
double instFlux,
double instFluxErr,
196 double calibration, error, magnitude;
197 calibration = evaluate(point);
198 magnitude = toMagnitude(instFlux, calibration);
199 error = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
203 Measurement PhotoCalib::instFluxToMagnitude(
double instFlux,
double instFluxErr)
const {
204 double magnitude = toMagnitude(instFlux, _calibrationMean);
205 double error = toMagnitudeErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr);
212 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFlux").
key;
213 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").
key;
214 return instFluxToMagnitude(sourceRecord.
get(instFluxKey), sourceRecord.
get(instFluxErrKey), position);
219 ndarray::Array<double, 2, 2>
result =
220 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
221 instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
227 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
228 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
229 auto magKey = sourceCatalog.getSchema().find<
double>(outField +
"_mag").
key;
230 auto magErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_magErr").
key;
231 for (
auto &record : sourceCatalog) {
232 auto result = instFluxToMagnitude(record.get(instFluxKey), record.get(instFluxErrKey),
233 record.getCentroid());
234 record.set(magKey,
result.value);
235 record.set(magErrKey,
result.error);
241 double PhotoCalib::magnitudeToInstFlux(
double magnitude)
const {
242 return toInstFluxFromMagnitude(magnitude, _calibrationMean);
246 return toInstFluxFromMagnitude(magnitude, evaluate(point));
250 return *(_calibration) / _calibrationMean;
258 return (_calibrationMean == rhs._calibrationMean && _calibrationErr == rhs._calibrationErr &&
259 (*_calibration) == *(rhs._calibration));
263 return calibration->mean();
267 if (photoCalib._isConstant)
268 os <<
"spatially constant with ";
270 os << *(photoCalib._calibration) <<
" with ";
271 return os <<
"mean: " << photoCalib._calibrationMean <<
" error: " << photoCalib._calibrationErr;
275 bool includeScaleUncertainty)
const {
280 *(
result.getImage()) *= _calibrationMean;
282 _calibration->multiplyImage(*(
result.getImage()),
true);
284 if (includeScaleUncertainty) {
285 toNanojanskyVariance(maskedImage.
getImage()->getArray(), maskedImage.
getVariance()->getArray(),
286 _calibrationErr,
result.getImage()->getArray(),
287 result.getVariance()->getArray());
289 toNanojanskyVariance(maskedImage.
getImage()->getArray(), maskedImage.
getVariance()->getArray(), 0,
290 result.getImage()->getArray(),
result.getVariance()->getArray());
298 auto const &inSchema = catalog.getSchema();
315 for (
auto const &
field : instFluxFields) {
317 newKey.instFlux = inSchema[inSchema.join(
field,
"instFlux")];
319 mapper.
addOutputField(FieldD(inSchema.join(
field,
"flux"),
"calibrated flux",
"nJy"),
true);
321 FieldD(inSchema.join(
field,
"mag"),
"calibrated magnitude",
"mag(AB)"),
true);
323 newKey.instFluxErr = inSchema.find<
double>(inSchema.join(
field,
"instFluxErr")).
key;
325 FieldD(inSchema.join(
field,
"fluxErr"),
"calibrated flux uncertainty",
"nJy"),
true);
327 FieldD(inSchema.join(
field,
"magErr"),
"calibrated magnitude uncertainty",
"mag(AB)"),
337 output.insert(mapper, output.begin(), catalog.begin(), catalog.end());
339 auto calibration = evaluateCatalog(output);
343 for (
auto &rec : output) {
344 for (
auto &
key : keys) {
345 double instFlux = rec.get(
key.instFlux);
346 double nanojansky = toNanojansky(instFlux, calibration[iRec]);
347 rec.set(
key.flux, nanojansky);
348 rec.set(
key.mag, toMagnitude(instFlux, calibration[iRec]));
349 if (
key.instFluxErr.isValid()) {
350 double instFluxErr = rec.get(
key.instFluxErr);
351 rec.set(
key.fluxErr, toNanojanskyErr(instFlux, instFluxErr, calibration[iRec],
352 _calibrationErr, nanojansky));
354 toMagnitudeErr(instFlux, instFluxErr, calibration[iRec], _calibrationErr));
366 for (
auto const &
name : catalog.getSchema().getNames()) {
368 if (
name.size() > SUFFIX.
size() + 1 &&
373 return calibrateCatalog(catalog, instFluxFields);
380 class PhotoCalibSchema {
390 PhotoCalibSchema(PhotoCalibSchema
const &) =
delete;
391 PhotoCalibSchema &operator=(PhotoCalibSchema
const &) =
delete;
393 PhotoCalibSchema(PhotoCalibSchema &&) =
delete;
394 PhotoCalibSchema &operator=(PhotoCalibSchema &&) =
delete;
396 static PhotoCalibSchema
const &
get() {
397 static PhotoCalibSchema
const instance;
405 "calibrationMean",
"mean calibration on this PhotoCalib's domain",
"count")),
407 schema.addField<
double>(
"calibrationErr",
"1-sigma error on calibrationMean",
"count")),
408 isConstant(schema.addField<table::Flag>(
"isConstant",
"Is this spatially-constant?")),
409 field(schema.addField<
int>(
"field",
"archive ID of the BoundedField object")),
410 version(schema.addField<
int>(
"version",
"version of this PhotoCalib")) {
411 schema.getCitizen().markPersistent();
415 class PhotoCalibFactory :
public table::io::PersistableFactory {
417 PTR(table::io::Persistable)
418 read(InputArchive
const &archive, CatalogVector
const &catalogs)
const override {
419 table::BaseRecord
const &record = catalogs.front().front();
420 PhotoCalibSchema
const &
keys = PhotoCalibSchema::get();
421 int version = getVersion(record);
423 throw(pex::exceptions::RuntimeError(
"Unsupported version (version 0 was defined in maggies): " +
426 return std::make_shared<PhotoCalib>(record.get(keys.calibrationMean), record.get(keys.calibrationErr),
427 archive.get<afw::math::BoundedField>(record.get(keys.field)),
428 record.get(keys.isConstant));
431 PhotoCalibFactory(
std::string const &
name) : afw::table::io::PersistableFactory(name) {}
434 int getVersion(table::BaseRecord
const &record)
const {
438 auto versionKey = record.getSchema().
find<
int>(versionName);
439 version = record.get(versionKey.key);
440 }
catch (
const pex::exceptions::NotFoundError &) {
448 std::string getPhotoCalibPersistenceName() {
return "PhotoCalib"; }
450 PhotoCalibFactory registration(getPhotoCalibPersistenceName());
459 int const CALIB_TABLE_CURRENT_VERSION = 2;
471 CalibKeys(
const CalibKeys &) =
delete;
472 CalibKeys &operator=(
const CalibKeys &) =
delete;
475 CalibKeys(CalibKeys &&) =
delete;
476 CalibKeys &operator=(CalibKeys &&) =
delete;
478 CalibKeys(
int tableVersion = CALIB_TABLE_CURRENT_VERSION)
479 : schema(), midTime(), expTime(), fluxMag0(), fluxMag0Err() {
480 if (tableVersion == 1) {
483 "midtime",
"middle of the time of the exposure relative to Unix epoch",
"ns");
484 expTime = schema.addField<
double>(EXPTIME_FIELD_NAME,
"exposure time",
"s");
486 fluxMag0 = schema.addField<
double>(
"fluxmag0",
"flux of a zero-magnitude object",
"count");
487 fluxMag0Err = schema.addField<
double>(
"fluxmag0.err",
"1-sigma error on fluxmag0",
"count");
491 class CalibFactory :
public table::io::PersistableFactory {
494 CatalogVector
const &catalogs)
const override {
497 int tableVersion = 1;
499 catalogs.front().getSchema().find<
double>(EXPTIME_FIELD_NAME);
500 }
catch (pex::exceptions::NotFoundError) {
501 tableVersion = CALIB_TABLE_CURRENT_VERSION;
504 CalibKeys
const keys{tableVersion};
508 table::BaseRecord
const &record = catalogs.front().front();
516 explicit CalibFactory(
std::string const &
name) : table::io::PersistableFactory(name) {}
519 std::string getCalibPersistenceName() {
return "Calib"; }
521 CalibFactory calibRegistration(getCalibPersistenceName());
525 std::string PhotoCalib::getPersistenceName()
const {
return getPhotoCalibPersistenceName(); }
528 PhotoCalibSchema
const &
keys = PhotoCalibSchema::get();
530 auto record = catalog.
addNew();
531 record->set(keys.calibrationMean, _calibrationMean);
532 record->set(keys.calibrationErr, _calibrationErr);
533 record->set(keys.isConstant, _isConstant);
534 record->set(keys.field, handle.
put(_calibration));
535 record->set(keys.version, SERIALIZATION_VERSION);
543 return _calibrationMean;
545 return _calibration->evaluate(point);
548 ndarray::Array<double, 1> PhotoCalib::evaluateArray(ndarray::Array<double, 1>
const &xx,
549 ndarray::Array<double, 1>
const &yy)
const {
551 ndarray::Array<double, 1>
result = ndarray::allocate(ndarray::makeVector(xx.size()));
552 result.deep() = _calibrationMean;
555 return _calibration->evaluate(xx, yy);
560 ndarray::Array<double, 1> xx = ndarray::allocate(ndarray::makeVector(sourceCatalog.size()));
561 ndarray::Array<double, 1> yy = ndarray::allocate(ndarray::makeVector(sourceCatalog.size()));
563 for (
auto const &rec : sourceCatalog) {
564 auto point = rec.getCentroid();
565 xx[i] = point.getX();
566 yy[i] = point.getY();
569 return evaluateArray(xx, yy);
574 ndarray::Array<double, 2, 2>
result)
const {
575 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
576 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
578 auto calibration = evaluateCatalog(sourceCatalog);
580 auto iter = result.begin();
581 for (
auto const &rec : sourceCatalog) {
582 double instFlux = rec.get(instFluxKey);
583 double instFluxErr = rec.get(instFluxErrKey);
584 double nanojansky = toNanojansky(instFlux, calibration[i]);
585 (*iter)[0] = nanojansky;
586 (*iter)[1] = toNanojanskyErr(instFlux, instFluxErr, calibration[i], _calibrationErr, nanojansky);
594 ndarray::Array<double, 2, 2> result)
const {
595 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
596 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
598 auto calibration = evaluateCatalog(sourceCatalog);
599 auto iter = result.begin();
601 for (
auto const &rec : sourceCatalog) {
602 double instFlux = rec.get(instFluxKey);
603 double instFluxErr = rec.get(instFluxErrKey);
604 (*iter)[0] = toMagnitude(instFlux, calibration[i]);
605 (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr, calibration[i], _calibrationErr);
612 auto key =
"FLUXMAG0";
617 double instFluxMag0Err = 0.0;
double getAsDouble(std::string const &name) const
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
std::shared_ptr< PhotoCalib > makePhotoCalibFromMetadata(daf::base::PropertySet &metadata, bool strip=false)
Construct a PhotoCalib from FITS FLUXMAG0/FLUXMAG0ERR keywords.
table::Key< double > calibrationErr
std::shared_ptr< PhotoCalib > makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err)
Construct a PhotoCalib from the deprecated Calib-style instFluxMag0/instFluxMag0Err values...
int put(Persistable const *obj, bool permissive=false)
Save an object to the archive and return a unique ID that can be used to retrieve it from an InputArc...
An object passed to Persistable::write to allow it to persist itself.
The photometric calibration of an exposure.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
table::Key< table::Flag > isConstant
A mapping between the keys of two Schemas, used to copy data between them.
Key< T > addOutputField(Field< T > const &newField, bool doReplace=false)
Add a new field to the output Schema that is not connected to the input Schema.
def scale(algorithm, min, max=None, frame=None)
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
table::Key< double > fluxMag0Err
virtual void remove(std::string const &name)
ItemVariant const * other
table::Key< double > expTime
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
table::Key< double > calibrationMean
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage's image.
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
A base class for image defects.
A class to manipulate images, masks, and variance as a single object.
A description of a field in a table.
Schema getSchema() const
Return the Schema that holds this record's fields and keys.
bool exists(std::string const &name) const
table::Key< double > fluxMag0
table::Key< std::int64_t > midTime
std::unique_ptr< SchemaItem< U > > result
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
std::ostream & operator<<(std::ostream &os, PhotoCalib const &photoCalib)
#define LSST_EXCEPT(type,...)
double nanojanskyToABMagnitude(double flux)
void addMinimalSchema(Schema const &minimal, bool doMap=true)
Add the given minimal schema to the output schema.
Record class that contains measurements made on a single exposure.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
const double referenceFlux
double ABMagnitudeToNanojansky(double magnitude)
Implementation of the Photometric Calibration class.
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
table::Key< int > version
T emplace_back(T... args)