42 std::shared_ptr<table::io::Persistable>
const &);
51 s <<
"value=" << measurement.value <<
", error=" << measurement.error;
57int const SERIALIZATION_VERSION = 1;
59double toNanojansky(
double instFlux,
double scale) {
return instFlux * scale; }
61double toMagnitude(
double instFlux,
double scale) {
65double toInstFluxFromMagnitude(
double magnitude,
double scale) {
70double toNanojanskyErr(
double instFluxErr,
double scale) {
71 return instFluxErr *
scale;
74double toMagnitudeErr(
double instFlux,
double instFluxErr) {
75 return 2.5 /
std::log(10.0) * (instFluxErr / instFlux);
83 return toNanojansky(instFlux, evaluate(point));
87 return toNanojansky(instFlux, _calibrationMean);
92 double calibration, error, nanojansky;
93 calibration = evaluate(point);
94 nanojansky = toNanojansky(instFlux, calibration);
95 error = toNanojanskyErr(instFluxErr, calibration);
100 double nanojansky = toNanojansky(instFlux, _calibrationMean);
101 double error = toNanojanskyErr(instFluxErr, _calibrationMean);
108 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFlux").key;
109 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").key;
114 ndarray::Array<double, 2, 2> result =
115 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
116 instFluxToNanojanskyArray(sourceCatalog, instFluxField, result);
122 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").key;
123 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").key;
124 auto nanojanskyKey = sourceCatalog.getSchema().find<
double>(outField +
"_flux").key;
125 auto nanojanskyErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_fluxErr").key;
126 for (
auto &record : sourceCatalog) {
128 record.getCentroid());
129 record.set(nanojanskyKey, result.value);
130 record.set(nanojanskyErrKey, result.error);
137 return toMagnitude(instFlux, evaluate(point));
141 return toMagnitude(instFlux, _calibrationMean);
146 double calibration, error, magnitude;
147 calibration = evaluate(point);
148 magnitude = toMagnitude(instFlux, calibration);
149 error = toMagnitudeErr(instFlux, instFluxErr);
154 double magnitude = toMagnitude(instFlux, _calibrationMean);
155 double error = toMagnitudeErr(instFlux, instFluxErr);
162 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFlux").key;
163 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").key;
169 ndarray::Array<double, 2, 2> result =
170 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
171 instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
177 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").key;
178 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").key;
179 auto magKey = sourceCatalog.getSchema().find<
double>(outField +
"_mag").key;
180 auto magErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_magErr").key;
181 for (
auto &record : sourceCatalog) {
183 record.getCentroid());
184 record.set(magKey, result.value);
185 record.set(magErrKey, result.error);
192 return toInstFluxFromMagnitude(magnitude, _calibrationMean);
196 return toInstFluxFromMagnitude(magnitude, evaluate(point));
200 return *(_calibration) / _calibrationMean;
208 return (_calibrationMean == rhs._calibrationMean && _calibrationErr == rhs._calibrationErr &&
209 (*_calibration) == *(rhs._calibration));
213 return calibration->mean();
217 return std::make_unique<PhotoCalib>(*
this);
223 buffer <<
"spatially constant with ";
225 buffer << *_calibration <<
" with ";
226 buffer <<
"mean: " << _calibrationMean <<
" error: " << _calibrationErr;
235 return os << photoCalib.toString();
242 result *= _calibrationMean;
244 _calibration->multiplyImage(result,
true);
251 bool includeScaleUncertainty)
const {
259 result /= _calibrationMean;
261 _calibration->divideImage(result,
true);
267 bool includeScaleUncertainty)
const {
273 auto const &inSchema = catalog.getSchema();
289 keys.reserve(instFluxFields.
size());
290 for (
auto const &field : instFluxFields) {
292 newKey.instFlux = inSchema[inSchema.join(field,
"instFlux")];
294 mapper.
addOutputField(FieldD(inSchema.join(field,
"flux"),
"calibrated flux",
"nJy"),
true);
296 FieldD(inSchema.join(field,
"mag"),
"calibrated magnitude",
"mag(AB)"),
true);
298 newKey.instFluxErr = inSchema.find<
double>(inSchema.join(field,
"instFluxErr")).key;
300 FieldD(inSchema.join(field,
"fluxErr"),
"calibrated flux uncertainty",
"nJy"),
true);
302 FieldD(inSchema.join(field,
"magErr"),
"calibrated magnitude uncertainty",
"mag(AB)"),
307 keys.emplace_back(newKey);
312 output.insert(mapper, output.begin(), catalog.begin(), catalog.end());
314 auto calibration = evaluateCatalog(output);
318 for (
auto &rec : output) {
319 for (
auto &key : keys) {
320 double instFlux = rec.get(key.instFlux);
321 double nanojansky = toNanojansky(instFlux, calibration[iRec]);
322 rec.set(key.flux, nanojansky);
323 rec.set(key.mag, toMagnitude(instFlux, calibration[iRec]));
324 if (key.instFluxErr.isValid()) {
325 double instFluxErr = rec.get(key.instFluxErr);
326 rec.set(key.fluxErr, toNanojanskyErr(instFluxErr, calibration[iRec]));
327 rec.set(key.magErr, toMagnitudeErr(instFlux, instFluxErr));
339 for (
auto const &name : catalog.getSchema().getNames()) {
341 if (name.size() > SUFFIX.
size() + 1 &&
342 name.compare(name.size() - SUFFIX.
size(), SUFFIX.
size(), SUFFIX) == 0) {
343 instFluxFields.
emplace_back(name.substr(0, name.size() - 9));
353class PhotoCalibSchema {
363 PhotoCalibSchema(PhotoCalibSchema
const &) =
delete;
364 PhotoCalibSchema &operator=(PhotoCalibSchema
const &) =
delete;
366 PhotoCalibSchema(PhotoCalibSchema &&) =
delete;
367 PhotoCalibSchema &operator=(PhotoCalibSchema &&) =
delete;
369 static PhotoCalibSchema
const &get() {
370 static PhotoCalibSchema
const instance;
377 calibrationMean(schema.addField<double>(
378 "calibrationMean",
"mean calibration on this PhotoCalib's domain",
"count")),
380 schema.addField<double>(
"calibrationErr",
"1-sigma error on calibrationMean",
"count")),
381 isConstant(schema.addField<
table::Flag>(
"isConstant",
"Is this spatially-constant?")),
382 field(schema.addField<int>(
"field",
"archive ID of the BoundedField object")),
383 version(schema.addField<int>(
"version",
"version of this PhotoCalib")) {}
386class PhotoCalibFactory :
public table::io::PersistableFactory {
389 CatalogVector
const &catalogs)
const override {
390 table::BaseRecord
const &record = catalogs.front().front();
391 PhotoCalibSchema
const &keys = PhotoCalibSchema::get();
392 int version = getVersion(record);
394 throw(pex::exceptions::RuntimeError(
"Unsupported version (version 0 was defined in maggies): " +
398 archive.get<afw::math::BoundedField>(record.get(
keys.field)),
399 record.get(
keys.isConstant));
402 PhotoCalibFactory(std::string
const &name) : afw::
table::io::PersistableFactory(
name) {}
408 std::string versionName =
"version";
409 auto versionKey = record.getSchema().find<
int>(versionName);
410 version = record.get(versionKey.key);
411 }
catch (
const pex::exceptions::NotFoundError &) {
419std::string getPhotoCalibPersistenceName() {
return "PhotoCalib"; }
421PhotoCalibFactory registration(getPhotoCalibPersistenceName());
430int const CALIB_TABLE_CURRENT_VERSION = 2;
431std::string
const EXPTIME_FIELD_NAME =
"exptime";
442 CalibKeys(
const CalibKeys &) =
delete;
443 CalibKeys &operator=(
const CalibKeys &) =
delete;
446 CalibKeys(CalibKeys &&) =
delete;
447 CalibKeys &operator=(CalibKeys &&) =
delete;
449 CalibKeys(
int tableVersion = CALIB_TABLE_CURRENT_VERSION)
450 :
schema(), midTime(), expTime(), fluxMag0(), fluxMag0Err() {
451 if (tableVersion == 1) {
453 midTime =
schema.addField<std::int64_t>(
454 "midtime",
"middle of the time of the exposure relative to Unix epoch",
"ns");
455 expTime =
schema.addField<
double>(EXPTIME_FIELD_NAME,
"exposure time",
"s");
457 fluxMag0 =
schema.addField<
double>(
"fluxmag0",
"flux of a zero-magnitude object",
"count");
458 fluxMag0Err =
schema.addField<
double>(
"fluxmag0.err",
"1-sigma error on fluxmag0",
"count");
464 std::shared_ptr<table::io::Persistable>
read(InputArchive
const &archive,
465 CatalogVector
const &catalogs)
const override {
468 int tableVersion = 1;
470 catalogs.front().getSchema().find<
double>(EXPTIME_FIELD_NAME);
471 }
catch (pex::exceptions::NotFoundError
const &) {
472 tableVersion = CALIB_TABLE_CURRENT_VERSION;
475 CalibKeys
const keys{tableVersion};
487 explicit CalibFactory(std::string
const &name) :
table::io::PersistableFactory(
name) {}
490std::string getCalibPersistenceName() {
return "Calib"; }
492CalibFactory calibRegistration(getCalibPersistenceName());
499 PhotoCalibSchema
const &keys = PhotoCalibSchema::get();
501 auto record = catalog.
addNew();
502 record->set(keys.calibrationMean, _calibrationMean);
503 record->set(keys.calibrationErr, _calibrationErr);
504 record->set(keys.isConstant, _isConstant);
505 record->set(keys.field, handle.
put(_calibration));
506 record->set(keys.version, SERIALIZATION_VERSION);
514 return _calibrationMean;
516 return _calibration->evaluate(point);
519ndarray::Array<double, 1> PhotoCalib::evaluateArray(ndarray::Array<double, 1>
const &xx,
520 ndarray::Array<double, 1>
const &yy)
const {
522 ndarray::Array<double, 1> result = ndarray::allocate(ndarray::makeVector(xx.size()));
523 result.deep() = _calibrationMean;
526 return _calibration->evaluate(xx, yy);
531 ndarray::Array<double, 1> xx = ndarray::allocate(ndarray::makeVector(sourceCatalog.size()));
532 ndarray::Array<double, 1> yy = ndarray::allocate(ndarray::makeVector(sourceCatalog.size()));
534 for (
auto const &rec : sourceCatalog) {
535 auto point = rec.getCentroid();
536 xx[i] = point.getX();
537 yy[i] = point.getY();
540 return evaluateArray(xx, yy);
544 std::string
const &instFluxField,
545 ndarray::Array<double, 2, 2> result)
const {
546 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").key;
547 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").key;
549 auto calibration = evaluateCatalog(sourceCatalog);
552 for (
auto const &rec : sourceCatalog) {
553 double instFlux = rec.get(instFluxKey);
554 double instFluxErr = rec.get(instFluxErrKey);
555 double nanojansky = toNanojansky(instFlux, calibration[i]);
556 (*iter)[0] = nanojansky;
557 (*iter)[1] = toNanojanskyErr(instFluxErr, calibration[i]);
564 std::string
const &instFluxField,
565 ndarray::Array<double, 2, 2> result)
const {
566 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").key;
567 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").key;
569 auto calibration = evaluateCatalog(sourceCatalog);
572 for (
auto const &rec : sourceCatalog) {
573 double instFlux = rec.get(instFluxKey);
574 double instFluxErr = rec.get(instFluxErrKey);
575 (*iter)[0] = toMagnitude(instFlux, calibration[i]);
576 (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr);
583 auto key =
"FLUXMAG0";
584 if (metadata.
exists(key)) {
586 if (strip) metadata.
remove(key);
588 double instFluxMag0Err = 0.0;
590 if (metadata.
exists(key)) {
592 if (strip) metadata.
remove(key);
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Utilities for converting between flux and magnitude in C++.
Implementation of the Photometric Calibration class.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
A class to manipulate images, masks, and variance as a single object.
The photometric calibration of an exposure.
afw::table::SourceCatalog calibrateCatalog(afw::table::SourceCatalog const &catalog, std::vector< std::string > const &instFluxFields) const
Return a flux calibrated catalog, with new _flux, _fluxErr, _mag, and _magErr fields.
bool operator==(PhotoCalib const &rhs) const
Two PhotoCalibs are equal if their component bounded fields and calibrationErr are equal.
double instFluxToNanojansky(double instFlux, lsst::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to nJy at a point in the BoundedField.
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
PhotoCalib(PhotoCalib const &)=default
double instFluxToMagnitude(double instFlux, lsst::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to AB magnitude.
std::string toString() const override
Create a string representation of this object.
bool equals(typehandling::Storable const &other) const noexcept override
Compare this object to another Storable.
MaskedImage< float > uncalibrateImage(MaskedImage< float > const &maskedImage) const
Return a un-calibrated image, with pixel values in ADU (or whatever the original input to this photoC...
std::shared_ptr< typehandling::Storable > cloneStorable() const override
Create a new PhotoCalib that is a copy of this one.
std::shared_ptr< afw::math::BoundedField > computeScalingTo(std::shared_ptr< PhotoCalib > other) const
Calculates the scaling between this PhotoCalib and another PhotoCalib.
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
std::shared_ptr< afw::math::BoundedField > computeScaledCalibration() const
Calculates the spatially-variable calibration, normalized by the mean in the valid domain.
MaskedImage< float > calibrateImage(MaskedImage< float > const &maskedImage) const
Return a flux calibrated image, with pixel values in nJy.
double magnitudeToInstFlux(double magnitude, lsst::geom::Point< double, 2 > const &point) const
Convert AB magnitude to instFlux (ADU).
Base class for all records.
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
Schema getSchema() const
Return the Schema that holds this record's fields and keys.
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
A class used as a handle to a particular field in a table.
Defines the fields and offsets for a table.
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
A mapping between the keys of two Schemas, used to copy data between them.
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
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.
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.
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
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...
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
A base class for factory classes used to reconstruct objects from records.
io::OutputArchiveHandle OutputArchiveHandle
Interface supporting iteration over heterogenous containers.
static bool singleClassEquals(T const &lhs, Storable const &rhs)
Test if a Storable is of a particular class and equal to another object.
Class for storing generic metadata.
virtual void remove(std::string const &name)
Remove all values for a property name (possibly hierarchical).
bool exists(std::string const &name) const
Determine if a name (possibly hierarchical) exists.
double getAsDouble(std::string const &name) const
Get the last value for any arithmetic property name (possibly hierarchical).
A coordinate class intended to represent absolute positions.
Reports errors in the logical structure of the program.
Reports attempts to access elements using an invalid key.
T emplace_back(T... args)
scale(algorithm, min, max=None, frame=None)
std::shared_ptr< PhotoCalib > makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err)
Construct a PhotoCalib from the deprecated Calib-style instFluxMag0/instFluxMag0Err values.
std::shared_ptr< PhotoCalib > makePhotoCalibFromMetadata(daf::base::PropertySet &metadata, bool strip=false)
Construct a PhotoCalib from FITS FLUXMAG0/FLUXMAG0ERR keywords.
std::ostream & operator<<(std::ostream &os, Measurement const &measurement)
CatalogT< BaseRecord > BaseCatalog
SortedCatalogT< SourceRecord > SourceCatalog
const double referenceFlux
The Oke & Gunn (1983) AB magnitude reference flux, in nJy (often approximated as 3631....
double ABMagnitudeToNanojansky(double magnitude)
Convert an AB magnitude to a flux in nanojansky.
double nanojanskyToABMagnitude(double flux)
Convert a flux in nanojansky to AB magnitude.
T setprecision(T... args)
A description of a field in a table.
std::shared_ptr< table::io::Persistable > read(table::io::InputArchive const &archive, table::io::CatalogVector const &catalogs) const override