34 #include "lsst/afw/table/io/Persistable.cc" 49 int const SERIALIZATION_VERSION = 1;
51 double toNanojansky(
double instFlux,
double scale) {
return instFlux *
scale; }
55 double toInstFluxFromMagnitude(
double magnitude,
double scale) {
60 double toNanojanskyErr(
double instFlux,
double instFluxErr,
double scale,
double scaleErr,
62 return std::abs(nanojansky) *
hypot(instFluxErr / instFlux, scaleErr / scale);
78 void toNanojanskyVariance(ndarray::Array<float const, 2, 1>
const &instFlux,
79 ndarray::Array<float const, 2, 1>
const &instFluxVar,
float scaleErr,
80 ndarray::Array<float const, 2, 1>
const &flux, ndarray::Array<float, 2, 1> out) {
81 auto eigenFlux = ndarray::asEigen<Eigen::ArrayXpr>(flux);
82 auto eigenInstFluxVar = ndarray::asEigen<Eigen::ArrayXpr>(instFluxVar);
83 auto eigenInstFlux = ndarray::asEigen<Eigen::ArrayXpr>(instFlux);
84 auto eigenOut = ndarray::asEigen<Eigen::ArrayXpr>(out);
85 eigenOut = eigenFlux.square() *
86 (eigenInstFluxVar / eigenInstFlux.square() + (scaleErr / eigenFlux * eigenInstFlux).square());
89 double toMagnitudeErr(
double instFlux,
double instFluxErr,
double scale,
double scaleErr) {
90 return 2.5 /
log(10.0) *
hypot(instFluxErr / instFlux, scaleErr / scale);
98 return toNanojansky(instFlux, evaluate(point));
101 double PhotoCalib::instFluxToNanojansky(
double instFlux)
const {
102 return toNanojansky(instFlux, _calibrationMean);
105 Measurement PhotoCalib::instFluxToNanojansky(
double instFlux,
double instFluxErr,
107 double calibration,
error, nanojansky;
108 calibration = evaluate(point);
109 nanojansky = toNanojansky(instFlux, calibration);
110 error = toNanojanskyErr(instFlux, instFluxErr, calibration, _calibrationErr, nanojansky);
114 Measurement PhotoCalib::instFluxToNanojansky(
double instFlux,
double instFluxErr)
const {
115 double nanojansky = toNanojansky(instFlux, _calibrationMean);
116 double error = toNanojanskyErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr, nanojansky);
123 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFlux").
key;
124 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").
key;
125 return instFluxToNanojansky(sourceRecord.
get(instFluxKey), sourceRecord.
get(instFluxErrKey), position);
129 ndarray::Array<double, 2, 2>
result =
130 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
131 instFluxToNanojanskyArray(sourceCatalog, instFluxField, result);
137 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
138 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
139 auto nanojanskyKey = sourceCatalog.getSchema().find<
double>(outField +
"_flux").
key;
140 auto nanojanskyErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_fluxErr").
key;
141 for (
auto &record : sourceCatalog) {
142 auto result = instFluxToNanojansky(record.get(instFluxKey), record.get(instFluxErrKey),
143 record.getCentroid());
144 record.set(nanojanskyKey,
result.value);
145 record.set(nanojanskyErrKey,
result.error);
152 return toMagnitude(instFlux, evaluate(point));
155 double PhotoCalib::instFluxToMagnitude(
double instFlux)
const {
156 return toMagnitude(instFlux, _calibrationMean);
159 Measurement PhotoCalib::instFluxToMagnitude(
double instFlux,
double instFluxErr,
161 double calibration,
error, magnitude;
162 calibration = evaluate(point);
163 magnitude = toMagnitude(instFlux, calibration);
164 error = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
168 Measurement PhotoCalib::instFluxToMagnitude(
double instFlux,
double instFluxErr)
const {
169 double magnitude = toMagnitude(instFlux, _calibrationMean);
170 double error = toMagnitudeErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr);
177 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFlux").
key;
178 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").
key;
179 return instFluxToMagnitude(sourceRecord.
get(instFluxKey), sourceRecord.
get(instFluxErrKey), position);
184 ndarray::Array<double, 2, 2>
result =
185 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
186 instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
192 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
193 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
194 auto magKey = sourceCatalog.getSchema().find<
double>(outField +
"_mag").
key;
195 auto magErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_magErr").
key;
196 for (
auto &record : sourceCatalog) {
197 auto result = instFluxToMagnitude(record.get(instFluxKey), record.get(instFluxErrKey),
198 record.getCentroid());
199 record.set(magKey,
result.value);
200 record.set(magErrKey,
result.error);
206 double PhotoCalib::magnitudeToInstFlux(
double magnitude)
const {
207 return toInstFluxFromMagnitude(magnitude, _calibrationMean);
211 return toInstFluxFromMagnitude(magnitude, evaluate(point));
215 return *(_calibration) / _calibrationMean;
223 return (_calibrationMean == rhs._calibrationMean && _calibrationErr == rhs._calibrationErr &&
224 (*_calibration) == *(rhs._calibration));
228 return calibration->mean();
232 if (photoCalib._isConstant)
233 os <<
"spatially constant with ";
235 os << *(photoCalib._calibration) <<
" with ";
236 return os <<
"mean: " << photoCalib._calibrationMean <<
" error: " << photoCalib._calibrationErr;
240 bool includeScaleUncertainty)
const {
245 *(
result.getImage()) *= _calibrationMean;
247 _calibration->multiplyImage(*(
result.getImage()),
true);
249 if (includeScaleUncertainty) {
250 toNanojanskyVariance(maskedImage.
getImage()->getArray(), maskedImage.
getVariance()->getArray(),
251 _calibrationErr,
result.getImage()->getArray(),
252 result.getVariance()->getArray());
254 toNanojanskyVariance(maskedImage.
getImage()->getArray(), maskedImage.
getVariance()->getArray(), 0,
255 result.getImage()->getArray(),
result.getVariance()->getArray());
265 class PhotoCalibSchema {
275 PhotoCalibSchema(PhotoCalibSchema
const &) =
delete;
276 PhotoCalibSchema &operator=(PhotoCalibSchema
const &) =
delete;
278 PhotoCalibSchema(PhotoCalibSchema &&) =
delete;
279 PhotoCalibSchema &operator=(PhotoCalibSchema &&) =
delete;
281 static PhotoCalibSchema
const &
get() {
282 static PhotoCalibSchema
const instance;
290 "calibrationMean",
"mean calibration on this PhotoCalib's domain",
"count")),
292 schema.addField<
double>(
"calibrationErr",
"1-sigma error on calibrationMean",
"count")),
293 isConstant(schema.addField<table::Flag>(
"isConstant",
"Is this spatially-constant?")),
294 field(schema.addField<
int>(
"field",
"archive ID of the BoundedField object")),
295 version(schema.addField<
int>(
"version",
"version of this PhotoCalib")) {
296 schema.getCitizen().markPersistent();
300 class PhotoCalibFactory :
public table::io::PersistableFactory {
302 PTR(table::io::Persistable)
303 read(InputArchive
const &archive, CatalogVector
const &catalogs)
const override {
304 table::BaseRecord
const &record = catalogs.front().front();
305 PhotoCalibSchema
const &
keys = PhotoCalibSchema::get();
306 int version = getVersion(record);
308 throw(pex::exceptions::RuntimeError(
"Unsupported version (version 0 was defined in maggies): " +
311 return std::make_shared<PhotoCalib>(record.get(keys.calibrationMean), record.get(keys.calibrationErr),
312 archive.get<afw::math::BoundedField>(record.get(keys.field)),
313 record.get(keys.isConstant));
316 PhotoCalibFactory(
std::string const &
name) : afw::table::io::PersistableFactory(name) {}
319 int getVersion(table::BaseRecord
const &record)
const {
323 auto versionKey = record.getSchema().
find<
int>(versionName);
324 version = record.get(versionKey.key);
325 }
catch (
const pex::exceptions::NotFoundError &) {
333 std::string getPhotoCalibPersistenceName() {
return "PhotoCalib"; }
335 PhotoCalibFactory registration(getPhotoCalibPersistenceName());
339 std::string PhotoCalib::getPersistenceName()
const {
return getPhotoCalibPersistenceName(); }
342 PhotoCalibSchema
const &
keys = PhotoCalibSchema::get();
344 auto record = catalog.
addNew();
345 record->set(keys.calibrationMean, _calibrationMean);
346 record->set(keys.calibrationErr, _calibrationErr);
347 record->set(keys.isConstant, _isConstant);
348 record->set(keys.field, handle.
put(_calibration));
349 record->set(keys.version, SERIALIZATION_VERSION);
357 return _calibrationMean;
359 return _calibration->evaluate(point);
364 ndarray::Array<double, 2, 2>
result)
const {
365 double instFlux, instFluxErr, nanojansky, calibration;
366 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
367 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
368 auto iter = result.begin();
369 for (
auto const &rec : sourceCatalog) {
370 instFlux = rec.get(instFluxKey);
371 instFluxErr = rec.get(instFluxErrKey);
372 calibration = evaluate(rec.getCentroid());
373 nanojansky = toNanojansky(instFlux, calibration);
374 (*iter)[0] = nanojansky;
375 (*iter)[1] = toNanojanskyErr(instFlux, instFluxErr, calibration, _calibrationErr, nanojansky);
382 ndarray::Array<double, 2, 2> result)
const {
383 double instFlux, instFluxErr, calibration;
384 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
385 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
386 auto iter = result.begin();
387 for (
auto const &rec : sourceCatalog) {
388 instFlux = rec.get(instFluxKey);
389 instFluxErr = rec.get(instFluxErrKey);
390 calibration = evaluate(rec.getCentroid());
391 (*iter)[0] = toMagnitude(instFlux, calibration);
392 (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
400 auto key =
"FLUXMAG0";
double getAsDouble(std::string const &name) const
Get the last value for any arithmetic property name (possibly hierarchical).
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
Angle abs(Angle const &a)
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
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.
def scale(algorithm, min, max=None, frame=None)
virtual void remove(std::string const &name)
Remove all values for a property name (possibly hierarchical).
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
std::shared_ptr< PhotoCalib > makePhotoCalib(daf::base::PropertySet &metadata, bool strip=false)
Construct a PhotoCalib from FITS FLUXMAG0/FLUXMAG0ERR keywords.
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.
Utilities for converting between flux and magnitude in C++.
Schema getSchema() const
Return the Schema that holds this record's fields and keys.
bool exists(std::string const &name) const
Determine if a name (possibly hierarchical) exists.
table::Key< double > calibrationMean
Reports errors in the logical structure of the program.
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,...)
Create an exception with a given type.
double nanojanskyToABMagnitude(double flux)
Convert a flux in nanojansky to AB magnitude.
Class for storing generic metadata.
ItemVariant const * other
Record class that contains measurements made on a single exposure.
table::Key< table::Flag > isConstant
table::Key< double > fluxMag0
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
table::Key< int > version
const double referenceFlux
The Oke & Gunn (1983) AB magnitude reference flux, in nJy (often approximated as 3631.0).
table::Key< double > fluxMag0Err
double ABMagnitudeToNanojansky(double magnitude)
Convert an AB magnitude to a flux in nanojansky.
Implementation of the Photometric Calibration class.
table::Key< double > calibrationErr
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.