43 double toMaggies(
double instFlux,
double scale) {
return instFlux *
scale; }
45 double toMagnitude(
double instFlux,
double scale) {
return -2.5 *
log10(instFlux * scale); }
47 double toMaggiesErr(
double instFlux,
double instFluxErr,
double scale,
double scaleErr,
double maggies) {
48 return maggies *
hypot(instFluxErr / instFlux, scaleErr / scale);
51 double toMagnitudeErr(
double instFlux,
double instFluxErr,
double scale,
double scaleErr) {
52 return 2.5 /
log(10.0) *
hypot(instFluxErr / instFlux, scaleErr / scale);
61 return toMaggies(instFlux, _calibrationMean);
63 return toMaggies(instFlux, _calibration->evaluate(point));
70 double calibration, err, maggies;
72 calibration = _calibrationMean;
74 calibration = _calibration->evaluate(point);
75 maggies = toMaggies(instFlux, calibration);
76 err = toMaggiesErr(instFlux, instFluxErr, calibration, _calibrationErr, maggies);
81 double maggies = toMaggies(instFlux, _calibrationMean);
82 double err = toMaggiesErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr, maggies);
89 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_flux").
key;
90 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_fluxErr").
key;
95 ndarray::Array<double, 2, 2>
result =
96 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
97 instFluxToMaggiesArray(sourceCatalog, instFluxField, result);
103 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_flux").
key;
104 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_fluxErr").
key;
105 auto maggiesKey = sourceCatalog.getSchema().find<
double>(outField +
"_flux").
key;
106 auto maggiesErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_fluxErr").
key;
107 for (
auto &record : sourceCatalog) {
109 instFluxToMaggies(record.get(instFluxKey), record.get(instFluxErrKey), record.getCentroid());
110 record.set(maggiesKey,
result.value);
111 record.set(maggiesErrKey,
result.err);
119 return toMagnitude(instFlux, _calibrationMean);
121 return toMagnitude(instFlux, _calibration->evaluate(point));
125 return toMagnitude(instFlux, _calibrationMean);
130 double calibration, err, magnitude;
132 calibration = _calibrationMean;
134 calibration = _calibration->evaluate(point);
135 magnitude = toMagnitude(instFlux, calibration);
136 err = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
141 double magnitude = toMagnitude(instFlux, _calibrationMean);
142 double err = toMagnitudeErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr);
149 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_flux").
key;
150 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_fluxErr").
key;
156 ndarray::Array<double, 2, 2>
result =
157 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
158 instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
164 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_flux").
key;
165 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_fluxErr").
key;
166 auto magKey = sourceCatalog.getSchema().find<
double>(outField +
"_mag").
key;
167 auto magErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_magErr").
key;
168 for (
auto &record : sourceCatalog) {
170 record.getCentroid());
171 record.set(magKey,
result.value);
172 record.set(magErrKey,
result.err);
179 return pow(10, magnitude / -2.5) / _calibrationMean;
183 return *(_calibration) / _calibrationMean;
191 return (_calibrationMean == rhs._calibrationMean && _calibrationErr == rhs._calibrationErr &&
192 (*_calibration) == *(rhs._calibration));
196 return calibration->mean();
200 if (photoCalib._isConstant)
201 os <<
"spatially constant with ";
203 os << *(photoCalib._calibration) <<
" with ";
204 return os <<
"mean: " << photoCalib._calibrationMean <<
" err: " << photoCalib._calibrationErr;
211 class PhotoCalibSchema {
220 PhotoCalibSchema(PhotoCalibSchema
const &) =
delete;
221 PhotoCalibSchema &
operator=(PhotoCalibSchema
const &) =
delete;
223 PhotoCalibSchema(PhotoCalibSchema &&) =
delete;
224 PhotoCalibSchema &
operator=(PhotoCalibSchema &&) =
delete;
226 static PhotoCalibSchema
const &
get() {
227 static PhotoCalibSchema
const instance;
235 "calibrationMean",
"mean calibration on this PhotoCalib's domain",
"count")),
237 schema.addField<
double>(
"calibrationErr",
"1-sigma error on calibrationMean",
"count")),
238 isConstant(schema.addField<table::Flag>(
"isConstant",
"Is this spatially-constant?")),
239 field(schema.addField<
int>(
"field",
"archive ID of the BoundedField object")) {
240 schema.getCitizen().markPersistent();
244 class PhotoCalibFactory :
public table::io::PersistableFactory {
246 PTR(table::io::Persistable)
247 read(InputArchive
const &archive, CatalogVector
const &catalogs)
const override {
248 table::BaseRecord
const &record = catalogs.front().front();
249 PhotoCalibSchema
const &
keys = PhotoCalibSchema::get();
250 return std::make_shared<PhotoCalib>(record.get(keys.calibrationMean), record.get(keys.calibrationErr),
251 archive.get<afw::math::BoundedField>(record.get(keys.field)),
252 record.get(keys.isConstant));
255 PhotoCalibFactory(
std::string const &
name) : afw::table::io::PersistableFactory(name) {}
258 std::string getPhotoCalibPersistenceName() {
return "PhotoCalib"; }
260 PhotoCalibFactory registration(getPhotoCalibPersistenceName());
267 PhotoCalibSchema
const &
keys = PhotoCalibSchema::get();
269 auto record = catalog.
addNew();
270 record->set(keys.calibrationMean, _calibrationMean);
271 record->set(keys.calibrationErr, _calibrationErr);
272 record->set(keys.isConstant, _isConstant);
273 record->set(keys.field, handle.
put(_calibration));
281 ndarray::Array<double, 2, 2>
result)
const {
282 double instFlux, instFluxErr, maggies, calibration;
283 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_flux").
key;
284 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_fluxErr").
key;
285 auto iter = result.begin();
286 for (
auto const &rec : sourceCatalog) {
287 instFlux = rec.get(instFluxKey);
288 instFluxErr = rec.get(instFluxErrKey);
290 calibration = _calibrationMean;
292 calibration = _calibration->evaluate(rec.getCentroid());
293 maggies = toMaggies(instFlux, calibration);
294 (*iter)[0] = maggies;
295 (*iter)[1] = toMaggiesErr(instFlux, instFluxErr, calibration, _calibrationErr, maggies);
302 ndarray::Array<double, 2, 2> result)
const {
303 double instFlux, instFluxErr, calibration;
304 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_flux").
key;
305 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_fluxErr").
key;
306 auto iter = result.begin();
307 for (
auto const &rec : sourceCatalog) {
308 instFlux = rec.get(instFluxKey);
309 instFluxErr = rec.get(instFluxErrKey);
311 calibration = _calibrationMean;
313 calibration = _calibration->evaluate(rec.getCentroid());
314 (*iter)[0] = toMagnitude(instFlux, calibration);
315 (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
table::Key< double > calibrationErr
double instFluxToMaggies(double instFlux, lsst::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to maggies at a point in the BoundedField.
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...
bool operator==(PhotoCalib const &rhs) const
Two PhotoCalibs are equal if their component bounded fields and calibrationErr are equal...
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)
std::shared_ptr< afw::math::BoundedField > computeScalingTo(std::shared_ptr< PhotoCalib > other) const
Calculates the scaling between this PhotoCalib and another PhotoCalib.
double instFluxToMagnitude(double instFlux, lsst::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to AB magnitude.
ItemVariant const * other
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
table::Key< double > calibrationMean
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
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.
#define LSST_EXCEPT(type,...)
friend std::ostream & operator<<(std::ostream &os, PhotoCalib const &photoCalib)
PhotoCalib & operator=(PhotoCalib const &)=delete
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.
std::shared_ptr< afw::math::BoundedField > computeScaledCalibration() const
Calculates the spatially-variable calibration, normalized by the mean in the valid domain...
double magnitudeToInstFlux(double magnitude) const
Convert AB magnitude to instFlux (ADU), using the mean instFlux/magnitude scaling factor...
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.