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 +
"_flux").key;
92 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_fluxSigma").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 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_flux").key;
106 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_fluxSigma").key;
107 auto maggiesKey = sourceCatalog.getSchema().find<
double>(outField +
"_calFlux").key;
108 auto maggiesErrKey = sourceCatalog.getSchema().find<
double>(outField +
"_calFluxErr").key;
109 for (
auto & record : sourceCatalog) {
111 record.getCentroid());
112 record.set(maggiesKey,
result.value);
113 record.set(maggiesErrKey,
result.err);
121 return toMagnitude(instFlux, _instFluxMag0);
123 return toMagnitude(instFlux, _zeroPoint->evaluate(point));
132 instFluxMag0 = _instFluxMag0;
134 instFluxMag0 = _zeroPoint->evaluate(point);
135 magnitude = toMagnitude(instFlux, instFluxMag0);
136 err = toMagnitudeErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err);
141 double magnitude = toMagnitude(instFlux, _instFluxMag0);
142 double err = toMagnitudeErr(instFlux, instFluxErr, _instFluxMag0, _instFluxMag0Err);
147 std::string
const &instFluxField)
const {
149 auto instFluxKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_flux").key;
150 auto instFluxErrKey = sourceRecord.
getSchema().
find<
double>(instFluxField +
"_fluxSigma").key;
155 std::string
const &instFluxField)
const {
156 ndarray::Array<double, 2, 2>
result =
157 ndarray::allocate(ndarray::makeVector(
int(sourceCatalog.size()), 2));
158 instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
163 std::string
const &instFluxField, std::string
const &outField)
const {
164 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_flux").key;
165 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_fluxSigma").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) * _instFluxMag0;
183 return *(_zeroPoint) / _instFluxMag0;
187 throw LSST_EXCEPT(pex::exceptions::LogicError,
"Not Implemented: See DM-10154.");
191 return (_instFluxMag0 == rhs._instFluxMag0 && _instFluxMag0Err == rhs._instFluxMag0Err &&
192 (*_zeroPoint) == *(rhs._zeroPoint));
195 double PhotoCalib::computeInstFluxMag0(std::shared_ptr<afw::math::BoundedField> zeroPoint)
const {
196 return zeroPoint->mean();
200 if (photoCalib._isConstant)
201 os <<
"spatially constant with ";
203 os << *(photoCalib._zeroPoint) <<
" with ";
204 return os <<
"mean: " << photoCalib._instFluxMag0 <<
" err: " << photoCalib._instFluxMag0Err;
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;
234 instFluxMag0(schema.addField<
double>(
"instFluxMag0",
"instFlux of a zero-magnitude object",
237 schema.addField<
double>(
"instFluxMag0Err",
"1-err error on instFluxmag0",
"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 virtual PTR(table::io::Persistable)
247 read(InputArchive
const &archive, CatalogVector
const &catalogs)
const {
248 table::BaseRecord
const &record = catalogs.front().front();
250 return std::make_shared<PhotoCalib>(record.get(keys.instFluxMag0), record.get(keys.instFluxMag0Err),
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());
269 auto record = catalog.
addNew();
270 record->set(keys.instFluxMag0, _instFluxMag0);
271 record->set(keys.instFluxMag0Err, _instFluxMag0Err);
272 record->set(keys.isConstant, _isConstant);
273 record->set(keys.field, handle.
put(_zeroPoint));
280 std::string
const &instFluxField,
281 ndarray::Array<double, 2, 2>
result)
const {
283 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_flux").key;
284 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_fluxSigma").key;
285 auto iter = result.begin();
286 for (
auto const &rec : sourceCatalog) {
287 instFlux = rec.get(instFluxKey);
288 instFluxErr = rec.get(instFluxErrKey);
290 instFluxMag0 = _instFluxMag0;
292 instFluxMag0 = _zeroPoint->evaluate(rec.getCentroid());
293 maggies = toMaggies(instFlux, instFluxMag0);
294 (*iter)[0] = maggies;
295 (*iter)[1] = toMaggiesErr(instFlux, instFluxErr, instFluxMag0, _instFluxMag0Err, maggies);
301 std::string
const &instFluxField,
302 ndarray::Array<double, 2, 2> result)
const {
304 auto instFluxKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_flux").key;
305 auto instFluxErrKey = sourceCatalog.getSchema().find<
double>(instFluxField +
"_fluxSigma").key;
306 auto iter = result.begin();
307 for (
auto const &rec : sourceCatalog) {
308 instFlux = rec.get(instFluxKey);
309 instFluxErr = rec.get(instFluxErrKey);
311 instFluxMag0 = _instFluxMag0;
313 instFluxMag0 = _zeroPoint->evaluate(rec.getCentroid());
314 (*iter)[0] = toMagnitude(instFlux, instFluxMag0);
315 (*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.
double instFluxToMagnitude(double instFlux, afw::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to AB magnitude.
The photometric calibration of an exposure.
A custom container class for records, based on std::vector.
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.
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
double instFluxToMaggies(double instFlux, afw::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to maggies at a point in the BoundedField.
table::Key< double > instFluxMag0
A base class for image defects.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Schema getSchema() const
Return the Schema that holds this record's fields and keys.
table::Key< std::string > name
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
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.
table::Key< table::Flag > isConstant
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Implementation of the Photometric Calibration class.
std::shared_ptr< afw::math::BoundedField > computeScaledZeroPoint() const
Calculates the spatially-variable zero point, 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.