48 int const SERIALIZATION_VERSION = 0;
50 double toMaggies(
double instFlux,
double scale) {
return instFlux *
scale; }
52 double toMagnitude(
double instFlux,
double scale) {
return -2.5 *
log10(instFlux * scale); }
54 double fromMagnitude(
double magnitude,
double scale) {
return pow(10, magnitude / -2.5) /
scale; }
56 double toMaggiesErr(
double instFlux,
double instFluxErr,
double scale,
double scaleErr,
double maggies) {
57 return maggies *
hypot(instFluxErr / instFlux, scaleErr / scale);
73 void toMaggiesVariance(ndarray::Array<float const, 2, 1>
const &instFlux,
74 ndarray::Array<float const, 2, 1>
const &instFluxVar,
float scaleErr,
75 ndarray::Array<float const, 2, 1>
const &maggies, ndarray::Array<float, 2, 1> out) {
76 auto eigenMaggies = ndarray::asEigen<Eigen::ArrayXpr>(maggies);
77 auto eigenInstFluxVar = ndarray::asEigen<Eigen::ArrayXpr>(instFluxVar);
78 auto eigenInstFlux = ndarray::asEigen<Eigen::ArrayXpr>(instFlux);
79 auto eigenOut = ndarray::asEigen<Eigen::ArrayXpr>(out);
80 eigenOut = eigenMaggies.square() * (eigenInstFluxVar / eigenInstFlux.square() +
81 (scaleErr / eigenMaggies * eigenInstFlux).square());
84 double toMagnitudeErr(
double instFlux,
double instFluxErr,
double scale,
double scaleErr) {
85 return 2.5 /
log(10.0) *
hypot(instFluxErr / instFlux, scaleErr / scale);
93 return toMaggies(instFlux, evaluate(point));
96 double PhotoCalib::instFluxToMaggies(
double instFlux)
const {
return toMaggies(instFlux, _calibrationMean); }
98 Measurement PhotoCalib::instFluxToMaggies(
double instFlux,
double instFluxErr,
100 double calibration, err, maggies;
101 calibration = evaluate(point);
102 maggies = toMaggies(instFlux, calibration);
103 err = toMaggiesErr(instFlux, instFluxErr, calibration, _calibrationErr, maggies);
107 Measurement PhotoCalib::instFluxToMaggies(
double instFlux,
double instFluxErr)
const {
108 double maggies = toMaggies(instFlux, _calibrationMean);
109 double err = toMaggiesErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr, maggies);
116 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFlux").
key;
117 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").
key;
118 return instFluxToMaggies(sourceRecord.
get(instFluxKey), sourceRecord.
get(instFluxErrKey), position);
122 ndarray::Array<double, 2, 2>
result =
123 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
124 instFluxToMaggiesArray(sourceCatalog, instFluxField, result);
130 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
131 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
132 auto maggiesKey = sourceCatalog.getSchema().find<
double>(outField +
"_instFlux").
key;
133 auto maggiesErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_instFluxErr").
key;
134 for (
auto &record : sourceCatalog) {
136 instFluxToMaggies(record.get(instFluxKey), record.get(instFluxErrKey), record.getCentroid());
137 record.set(maggiesKey,
result.value);
138 record.set(maggiesErrKey,
result.err);
145 return toMagnitude(instFlux, evaluate(point));
148 double PhotoCalib::instFluxToMagnitude(
double instFlux)
const {
149 return toMagnitude(instFlux, _calibrationMean);
152 Measurement PhotoCalib::instFluxToMagnitude(
double instFlux,
double instFluxErr,
154 double calibration, err, magnitude;
155 calibration = evaluate(point);
156 magnitude = toMagnitude(instFlux, calibration);
157 err = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
161 Measurement PhotoCalib::instFluxToMagnitude(
double instFlux,
double instFluxErr)
const {
162 double magnitude = toMagnitude(instFlux, _calibrationMean);
163 double err = toMagnitudeErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr);
170 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFlux").
key;
171 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").
key;
172 return instFluxToMagnitude(sourceRecord.
get(instFluxKey), sourceRecord.
get(instFluxErrKey), position);
177 ndarray::Array<double, 2, 2>
result =
178 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
179 instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
185 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
186 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
187 auto magKey = sourceCatalog.getSchema().find<
double>(outField +
"_mag").
key;
188 auto magErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_magErr").
key;
189 for (
auto &record : sourceCatalog) {
190 auto result = instFluxToMagnitude(record.get(instFluxKey), record.get(instFluxErrKey),
191 record.getCentroid());
192 record.set(magKey,
result.value);
193 record.set(magErrKey,
result.err);
199 double PhotoCalib::magnitudeToInstFlux(
double magnitude)
const {
200 return fromMagnitude(magnitude, _calibrationMean);
204 return fromMagnitude(magnitude, evaluate(point));
208 return *(_calibration) / _calibrationMean;
216 return (_calibrationMean == rhs._calibrationMean && _calibrationErr == rhs._calibrationErr &&
217 (*_calibration) == *(rhs._calibration));
221 return calibration->mean();
225 if (photoCalib._isConstant)
226 os <<
"spatially constant with ";
228 os << *(photoCalib._calibration) <<
" with ";
229 return os <<
"mean: " << photoCalib._calibrationMean <<
" err: " << photoCalib._calibrationErr;
237 *(
result.getImage()) *= _calibrationMean;
239 _calibration->multiplyImage(*(
result.getImage()),
true);
241 toMaggiesVariance(maskedImage.
getImage()->getArray(), maskedImage.
getVariance()->getArray(),
242 _calibrationErr,
result.getImage()->getArray(),
result.getVariance()->getArray());
251 class PhotoCalibSchema {
261 PhotoCalibSchema(PhotoCalibSchema
const &) =
delete;
262 PhotoCalibSchema &operator=(PhotoCalibSchema
const &) =
delete;
264 PhotoCalibSchema(PhotoCalibSchema &&) =
delete;
265 PhotoCalibSchema &operator=(PhotoCalibSchema &&) =
delete;
267 static PhotoCalibSchema
const &
get() {
268 static PhotoCalibSchema
const instance;
276 "calibrationMean",
"mean calibration on this PhotoCalib's domain",
"count")),
278 schema.addField<
double>(
"calibrationErr",
"1-sigma error on calibrationMean",
"count")),
279 isConstant(schema.addField<table::Flag>(
"isConstant",
"Is this spatially-constant?")),
280 field(schema.addField<
int>(
"field",
"archive ID of the BoundedField object")),
281 version(schema.addField<
int>(
"version",
"version of this PhotoCalib")) {
282 schema.getCitizen().markPersistent();
286 class PhotoCalibFactory :
public table::io::PersistableFactory {
288 PTR(table::io::Persistable)
289 read(InputArchive
const &archive, CatalogVector
const &catalogs)
const override {
290 table::BaseRecord
const &record = catalogs.front().front();
291 PhotoCalibSchema
const &
keys = PhotoCalibSchema::get();
292 int version = getVersion(record);
294 throw(pex::exceptions::TypeError(
"Unsupported version: " +
std::to_string(version)));
296 return std::make_shared<PhotoCalib>(record.get(keys.calibrationMean), record.get(keys.calibrationErr),
297 archive.get<afw::math::BoundedField>(record.get(keys.field)),
298 record.get(keys.isConstant));
301 PhotoCalibFactory(
std::string const &
name) : afw::table::io::PersistableFactory(name) {}
304 int getVersion(table::BaseRecord
const &record)
const {
308 auto versionKey = record.getSchema().
find<
int>(versionName);
309 version = record.get(versionKey.key);
310 }
catch (
const pex::exceptions::NotFoundError &) {
318 std::string getPhotoCalibPersistenceName() {
return "PhotoCalib"; }
320 PhotoCalibFactory registration(getPhotoCalibPersistenceName());
324 std::string PhotoCalib::getPersistenceName()
const {
return getPhotoCalibPersistenceName(); }
327 PhotoCalibSchema
const &
keys = PhotoCalibSchema::get();
329 auto record = catalog.
addNew();
330 record->set(keys.calibrationMean, _calibrationMean);
331 record->set(keys.calibrationErr, _calibrationErr);
332 record->set(keys.isConstant, _isConstant);
333 record->set(keys.field, handle.
put(_calibration));
334 record->set(keys.version, SERIALIZATION_VERSION);
342 return _calibrationMean;
344 return _calibration->evaluate(point);
349 ndarray::Array<double, 2, 2>
result)
const {
350 double instFlux, instFluxErr, maggies, calibration;
351 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
352 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
353 auto iter = result.begin();
354 for (
auto const &rec : sourceCatalog) {
355 instFlux = rec.get(instFluxKey);
356 instFluxErr = rec.get(instFluxErrKey);
357 calibration = evaluate(rec.getCentroid());
358 maggies = toMaggies(instFlux, calibration);
359 (*iter)[0] = maggies;
360 (*iter)[1] = toMaggiesErr(instFlux, instFluxErr, calibration, _calibrationErr, maggies);
367 ndarray::Array<double, 2, 2> result)
const {
368 double instFlux, instFluxErr, calibration;
369 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
370 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
371 auto iter = result.begin();
372 for (
auto const &rec : sourceCatalog) {
373 instFlux = rec.get(instFluxKey);
374 instFluxErr = rec.get(instFluxErrKey);
375 calibration = evaluate(rec.getCentroid());
376 (*iter)[0] = toMagnitude(instFlux, calibration);
377 (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
table::Key< double > calibrationErr
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.
table::Key< table::Flag > isConstant
def scale(algorithm, min, max=None, frame=None)
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
ItemVariant const * other
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.
Schema getSchema() const
Return the Schema that holds this record's fields and keys.
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,...)
Record class that contains measurements made on a single exposure.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
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