31 #include "lsst/daf/base/PropertySet.h"
32 #include "lsst/pex/exceptions.h"
43 double pow2(
double value) {
return value * value; }
45 double toMaggies(
double instFlux,
double scale) {
return instFlux /
scale; }
47 double toMagnitude(
double instFlux,
double scale) {
return -2.5 * log10(instFlux / scale); }
49 double toMaggiesErr(
double instFlux,
double instFluxErr,
double scale,
double scaleErr,
double maggies) {
50 return maggies * sqrt(pow2(instFluxErr / instFlux) + pow2(scaleErr / scale));
53 double toMagnitudeErr(
double instFlux,
double instFluxErr,
double scale,
double scaleErr) {
54 return 2.5 / log(10.0) * sqrt(pow2(instFluxErr / instFlux) + pow2(scaleErr / scale));
63 return toMaggies(instFlux, _instFluxMag0);
65 return toMaggies(instFlux, _zeroPoint->evaluate(point));
74 instFluxMag0 = _instFluxMag0;
76 instFluxMag0 = _zeroPoint->evaluate(point);
77 maggies = toMaggies(instFlux, instFluxMag0);
78 err = toMaggiesErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err, maggies);
83 double maggies = toMaggies(instFlux, _instFluxMag0);
84 double err = toMaggiesErr(instFlux, instFluxErr, _instFluxMag0, _instFluxMag0Err, maggies);
89 std::string
const &instFluxField)
const {
91 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFlux").key;
92 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").key;
96 std::string
const &instFluxField)
const {
97 ndarray::Array<double, 2, 2>
result =
98 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.
size()), 2));
99 instFluxToMaggiesArray(sourceCatalog, instFluxField, result);
104 std::string
const &outField)
const {
105 throw LSST_EXCEPT(pex::exceptions::LogicError,
106 "Will not be implemented until RFC-322 is implemented. See DM-10155");
113 return toMagnitude(instFlux, _instFluxMag0);
115 return toMagnitude(instFlux, _zeroPoint->evaluate(point));
124 instFluxMag0 = _instFluxMag0;
126 instFluxMag0 = _zeroPoint->evaluate(point);
127 magnitude = toMagnitude(instFlux, instFluxMag0);
128 err = toMagnitudeErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err);
133 double magnitude = toMagnitude(instFlux, _instFluxMag0);
134 double err = toMagnitudeErr(instFlux, instFluxErr, _instFluxMag0, _instFluxMag0Err);
139 std::string
const &instFluxField)
const {
141 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFlux").key;
142 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").key;
147 std::string
const &instFluxField)
const {
148 ndarray::Array<double, 2, 2>
result =
149 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.
size()), 2));
150 instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
155 std::string
const &instFluxField, std::string
const &outField)
const {
156 throw LSST_EXCEPT(pex::exceptions::LogicError,
157 "Will not be implemented until RFC-322 is implemented. See DM-10155.");
163 return pow(10, magnitude / -2.5) * _instFluxMag0;
167 return *(_zeroPoint) / _instFluxMag0;
171 throw LSST_EXCEPT(pex::exceptions::LogicError,
"Not Implemented: See DM-10154.");
175 return (_instFluxMag0 == rhs._instFluxMag0 && _instFluxMag0Err == rhs._instFluxMag0Err &&
176 (*_zeroPoint) == *(rhs._zeroPoint));
179 double PhotoCalib::computeInstFluxMag0(std::shared_ptr<afw::math::BoundedField> zeroPoint)
const {
180 return zeroPoint->mean();
184 if (photoCalib._isConstant)
185 os <<
"spatially constant with ";
187 os << *(photoCalib._zeroPoint) <<
" with ";
188 return os <<
"mean: " << photoCalib._instFluxMag0 <<
" err: " << photoCalib._instFluxMag0Err;
195 class PhotoCalibSchema {
204 PhotoCalibSchema(PhotoCalibSchema
const &) =
delete;
205 PhotoCalibSchema &operator=(PhotoCalibSchema
const &) =
delete;
207 PhotoCalibSchema(PhotoCalibSchema &&) =
delete;
208 PhotoCalibSchema &operator=(PhotoCalibSchema &&) =
delete;
210 static PhotoCalibSchema
const &
get() {
211 static PhotoCalibSchema
const instance;
218 instFluxMag0(
schema.addField<double>(
"instFluxMag0",
"instFlux of a zero-magnitude object",
221 schema.addField<double>(
"instFluxMag0Err",
"1-err error on instFluxmag0",
"count")),
222 isConstant(
schema.addField<table::Flag>(
"isConstant",
"Is this spatially-constant?")),
223 field(
schema.addField<int>(
"field",
"archive ID of the BoundedField object")) {
224 schema.getCitizen().markPersistent();
228 class PhotoCalibFactory :
public table::io::PersistableFactory {
230 virtual PTR(table::io::Persistable)
231 read(InputArchive const &archive, CatalogVector const &catalogs)
const {
232 table::BaseRecord
const &record = catalogs.front().front();
233 PhotoCalibSchema
const &keys = PhotoCalibSchema::get();
234 return std::make_shared<PhotoCalib>(record.get(keys.instFluxMag0), record.get(keys.instFluxMag0Err),
235 archive.get<afw::math::BoundedField>(record.get(keys.field)),
236 record.get(keys.isConstant));
239 PhotoCalibFactory(std::string
const &
name) : afw::table::io::PersistableFactory(name) {}
242 std::string getPhotoCalibPersistenceName() {
return "PhotoCalib"; }
244 PhotoCalibFactory registration(getPhotoCalibPersistenceName());
251 PhotoCalibSchema
const &keys = PhotoCalibSchema::get();
253 auto record = catalog.
addNew();
254 record->set(keys.instFluxMag0, _instFluxMag0);
255 record->set(keys.instFluxMag0Err, _instFluxMag0Err);
256 record->set(keys.isConstant, _isConstant);
257 record->set(keys.field, handle.
put(_zeroPoint));
264 std::string
const &instFluxField,
265 ndarray::Array<double, 2, 2>
result)
const {
267 auto instFluxKey = sourceCatalog.
getSchema().
find<
double>(instFluxField +
"_instFlux").key;
268 auto instFluxErrKey = sourceCatalog.
getSchema().
find<
double>(instFluxField +
"_instFluxErr").key;
269 auto iter = result.begin();
270 for (
auto const &rec : sourceCatalog) {
271 instFlux = rec.get(instFluxKey);
272 instFluxErr = rec.get(instFluxErrKey);
274 instFluxMag0 = _instFluxMag0;
276 instFluxMag0 = _zeroPoint->evaluate(rec.getCentroid());
277 maggies = toMaggies(instFlux, instFluxMag0);
278 (*iter)[0] = maggies;
279 (*iter)[1] = toMaggiesErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err, maggies);
285 std::string
const &instFluxField,
286 ndarray::Array<double, 2, 2> result)
const {
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 instFluxMag0 = _instFluxMag0;
297 instFluxMag0 = _zeroPoint->evaluate(rec.getCentroid());
298 (*iter)[0] = toMagnitude(instFlux, instFluxMag0);
299 (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err);
table::Key< double > instFluxMag0Err
int put(Persistable const *obj, bool permissive=false)
Save a nested Persistable to the same archive.
bool operator==(PhotoCalib const &rhs) const
Two PhotoCalibs are equal if their component bounded fields and instFluxMag0Err are equal...
An object passed to Persistable::write to allow it to persist itself.
The photometric calibration of an exposure.
A custom container class for records, based on std::vector.
size_type size() const
Return the number of elements in the catalog.
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
table::Key< double > instFluxMag0
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
std::shared_ptr< afw::math::BoundedField > computeScaledZeroPoint() const
Calculates the spatially-variable zero point, normalized by the mean in the valid domain...
table::Key< std::string > name
double magnitudeToInstFlux(double magnitude) const
Convert AB magnitude to instFlux (ADU), using the mean instFlux/magnitude scaling factor...
double instFluxToMagnitude(double instFlux, afw::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to AB magnitude.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
std::ostream & operator<<(std::ostream &os, PhotoCalib const &photoCalib)
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
std::shared_ptr< afw::math::BoundedField > computeScalingTo(std::shared_ptr< PhotoCalib > other) const
Calculates the scaling between this PhotoCalib and another PhotoCalib.
double instFluxToMaggies(double instFlux, afw::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to maggies at a point in the BoundedField.
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
Record class that contains measurements made on a single exposure.
table::Key< table::Flag > isConstant
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
Implementation of the Photometric Calibration class.
Schema getSchema() const
Return the schema associated with the catalog's table.
Schema getSchema() const
Return the Schema that holds this record's fields and keys.
afw::table::Schema schema
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.