48 double toMaggies(
double instFlux,
double scale) {
return instFlux *
scale; }
50 double toMagnitude(
double instFlux,
double scale) {
return -2.5 *
log10(instFlux * scale); }
52 double toMaggiesErr(
double instFlux,
double instFluxErr,
double scale,
double scaleErr,
double maggies) {
53 return maggies *
hypot(instFluxErr / instFlux, scaleErr / scale);
56 double toMagnitudeErr(
double instFlux,
double instFluxErr,
double scale,
double scaleErr) {
57 return 2.5 /
log(10.0) *
hypot(instFluxErr / instFlux, scaleErr / scale);
66 return toMaggies(instFlux, _calibrationMean);
68 return toMaggies(instFlux, _calibration->evaluate(point));
71 double PhotoCalib::instFluxToMaggies(
double instFlux)
const {
return toMaggies(instFlux, _calibrationMean); }
73 Measurement PhotoCalib::instFluxToMaggies(
double instFlux,
double instFluxErr,
75 double calibration, err, maggies;
77 calibration = _calibrationMean;
79 calibration = _calibration->evaluate(point);
80 maggies = toMaggies(instFlux, calibration);
81 err = toMaggiesErr(instFlux, instFluxErr, calibration, _calibrationErr, maggies);
85 Measurement PhotoCalib::instFluxToMaggies(
double instFlux,
double instFluxErr)
const {
86 double maggies = toMaggies(instFlux, _calibrationMean);
87 double err = toMaggiesErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr, maggies);
94 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFlux").
key;
95 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").
key;
96 return instFluxToMaggies(sourceRecord.
get(instFluxKey), sourceRecord.
get(instFluxErrKey), position);
100 ndarray::Array<double, 2, 2>
result =
101 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
102 instFluxToMaggiesArray(sourceCatalog, instFluxField, result);
108 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
109 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
110 auto maggiesKey = sourceCatalog.getSchema().find<
double>(outField +
"_instFlux").
key;
111 auto maggiesErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_instFluxErr").
key;
112 for (
auto &record : sourceCatalog) {
114 instFluxToMaggies(record.get(instFluxKey), record.get(instFluxErrKey), record.getCentroid());
115 record.set(maggiesKey,
result.value);
116 record.set(maggiesErrKey,
result.err);
124 return toMagnitude(instFlux, _calibrationMean);
126 return toMagnitude(instFlux, _calibration->evaluate(point));
129 double PhotoCalib::instFluxToMagnitude(
double instFlux)
const {
130 return toMagnitude(instFlux, _calibrationMean);
133 Measurement PhotoCalib::instFluxToMagnitude(
double instFlux,
double instFluxErr,
135 double calibration, err, magnitude;
137 calibration = _calibrationMean;
139 calibration = _calibration->evaluate(point);
140 magnitude = toMagnitude(instFlux, calibration);
141 err = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
145 Measurement PhotoCalib::instFluxToMagnitude(
double instFlux,
double instFluxErr)
const {
146 double magnitude = toMagnitude(instFlux, _calibrationMean);
147 double err = toMagnitudeErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr);
154 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFlux").
key;
155 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").
key;
156 return instFluxToMagnitude(sourceRecord.
get(instFluxKey), sourceRecord.
get(instFluxErrKey), position);
161 ndarray::Array<double, 2, 2>
result =
162 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
163 instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
169 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
170 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
171 auto magKey = sourceCatalog.getSchema().find<
double>(outField +
"_mag").
key;
172 auto magErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_magErr").
key;
173 for (
auto &record : sourceCatalog) {
174 auto result = instFluxToMagnitude(record.get(instFluxKey), record.get(instFluxErrKey),
175 record.getCentroid());
176 record.set(magKey,
result.value);
177 record.set(magErrKey,
result.err);
183 double PhotoCalib::magnitudeToInstFlux(
double magnitude)
const {
184 return pow(10, magnitude / -2.5) / _calibrationMean;
188 return *(_calibration) / _calibrationMean;
196 return (_calibrationMean == rhs._calibrationMean && _calibrationErr == rhs._calibrationErr &&
197 (*_calibration) == *(rhs._calibration));
201 return calibration->mean();
205 if (photoCalib._isConstant)
206 os <<
"spatially constant with ";
208 os << *(photoCalib._calibration) <<
" with ";
209 return os <<
"mean: " << photoCalib._calibrationMean <<
" err: " << photoCalib._calibrationErr;
216 class PhotoCalibSchema {
225 PhotoCalibSchema(PhotoCalibSchema
const &) =
delete;
226 PhotoCalibSchema &operator=(PhotoCalibSchema
const &) =
delete;
228 PhotoCalibSchema(PhotoCalibSchema &&) =
delete;
229 PhotoCalibSchema &operator=(PhotoCalibSchema &&) =
delete;
231 static PhotoCalibSchema
const &
get() {
232 static PhotoCalibSchema
const instance;
240 "calibrationMean",
"mean calibration on this PhotoCalib's domain",
"count")),
242 schema.addField<
double>(
"calibrationErr",
"1-sigma error on calibrationMean",
"count")),
243 isConstant(schema.addField<table::Flag>(
"isConstant",
"Is this spatially-constant?")),
244 field(schema.addField<
int>(
"field",
"archive ID of the BoundedField object")) {
245 schema.getCitizen().markPersistent();
249 class PhotoCalibFactory :
public table::io::PersistableFactory {
251 PTR(table::io::Persistable)
252 read(InputArchive
const &archive, CatalogVector
const &catalogs)
const override {
253 table::BaseRecord
const &record = catalogs.front().front();
254 PhotoCalibSchema
const &
keys = PhotoCalibSchema::get();
255 return std::make_shared<PhotoCalib>(record.get(keys.calibrationMean), record.get(keys.calibrationErr),
256 archive.get<afw::math::BoundedField>(record.get(keys.field)),
257 record.get(keys.isConstant));
260 PhotoCalibFactory(
std::string const &
name) : afw::table::io::PersistableFactory(name) {}
263 std::string getPhotoCalibPersistenceName() {
return "PhotoCalib"; }
265 PhotoCalibFactory registration(getPhotoCalibPersistenceName());
269 std::string PhotoCalib::getPersistenceName()
const {
return getPhotoCalibPersistenceName(); }
272 PhotoCalibSchema
const &
keys = PhotoCalibSchema::get();
274 auto record = catalog.
addNew();
275 record->set(keys.calibrationMean, _calibrationMean);
276 record->set(keys.calibrationErr, _calibrationErr);
277 record->set(keys.isConstant, _isConstant);
278 record->set(keys.field, handle.
put(_calibration));
286 ndarray::Array<double, 2, 2>
result)
const {
287 double instFlux, instFluxErr, maggies, calibration;
288 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
289 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
290 auto iter = result.begin();
291 for (
auto const &rec : sourceCatalog) {
292 instFlux = rec.get(instFluxKey);
293 instFluxErr = rec.get(instFluxErrKey);
295 calibration = _calibrationMean;
297 calibration = _calibration->evaluate(rec.getCentroid());
298 maggies = toMaggies(instFlux, calibration);
299 (*iter)[0] = maggies;
300 (*iter)[1] = toMaggiesErr(instFlux, instFluxErr, calibration, _calibrationErr, maggies);
307 ndarray::Array<double, 2, 2> result)
const {
308 double instFlux, instFluxErr, calibration;
309 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFlux").
key;
310 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_instFluxErr").
key;
311 auto iter = result.begin();
312 for (
auto const &rec : sourceCatalog) {
313 instFlux = rec.get(instFluxKey);
314 instFluxErr = rec.get(instFluxErrKey);
316 calibration = _calibrationMean;
318 calibration = _calibration->evaluate(rec.getCentroid());
319 (*iter)[0] = toMagnitude(instFlux, calibration);
320 (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
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.
bool operator==(CoordKey const &lhs, CoordKey const &rhs)
Compare CoordKeys for equality using the constituent Keys.
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
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
A base class for image defects.
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.