Loading [MathJax]/extensions/tex2jax.js
LSST Applications g0fba68d861+83433b07ee,g16d25e1f1b+23bc9e47ac,g1ec0fe41b4+3ea9d11450,g1fd858c14a+9be2b0f3b9,g2440f9efcc+8c5ae1fdc5,g35bb328faa+8c5ae1fdc5,g4a4af6cd76+d25431c27e,g4d2262a081+c74e83464e,g53246c7159+8c5ae1fdc5,g55585698de+1e04e59700,g56a49b3a55+92a7603e7a,g60b5630c4e+1e04e59700,g67b6fd64d1+3fc8cb0b9e,g78460c75b0+7e33a9eb6d,g786e29fd12+668abc6043,g8352419a5c+8c5ae1fdc5,g8852436030+60e38ee5ff,g89139ef638+3fc8cb0b9e,g94187f82dc+1e04e59700,g989de1cb63+3fc8cb0b9e,g9d31334357+1e04e59700,g9f33ca652e+0a83e03614,gabe3b4be73+8856018cbb,gabf8522325+977d9fabaf,gb1101e3267+8b4b9c8ed7,gb89ab40317+3fc8cb0b9e,gc0af124501+57ccba3ad1,gcf25f946ba+60e38ee5ff,gd6cbbdb0b4+1cc2750d2e,gd794735e4e+7be992507c,gdb1c4ca869+be65c9c1d7,gde0f65d7ad+c7f52e58fe,ge278dab8ac+6b863515ed,ge410e46f29+3fc8cb0b9e,gf35d7ec915+97dd712d81,gf5e32f922b+8c5ae1fdc5,gf618743f1b+747388abfa,gf67bdafdda+3fc8cb0b9e,w.2025.18
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PhotoCalib.cc
Go to the documentation of this file.
1/*
2 * LSST Data Management System
3 * Copyright 2017 LSST Corporation.
4 *
5 * This product includes software developed by the
6 * LSST Project (http://www.lsst.org/).
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the LSST License Statement and
19 * the GNU General Public License along with this program. If not,
20 * see <http://www.lsstcorp.org/LegalNotices/>.
21 */
22
23#include <cmath>
24#include <iostream>
25#include <iomanip>
26
27#include "lsst/geom/Point.h"
33#include "lsst/pex/exceptions.h"
34#include "ndarray.h"
37
38namespace lsst {
39namespace afw {
40
41template std::shared_ptr<image::PhotoCalib> table::io::PersistableFacade<image::PhotoCalib>::dynamicCast(
42 std::shared_ptr<table::io::Persistable> const &);
43
44namespace image {
45
46// ------------------- helpers -------------------
47
51 s << "value=" << measurement.value << ", error=" << measurement.error;
52 return os << s.str();
53}
54
55namespace {
56
57int const SERIALIZATION_VERSION = 1;
58
59double toNanojansky(double instFlux, double scale) { return instFlux * scale; }
60
61double toMagnitude(double instFlux, double scale) {
62 return cpputils::nanojanskyToABMagnitude(instFlux * scale);
63}
64
65double toInstFluxFromMagnitude(double magnitude, double scale) {
66 // Note: flux[nJy] / scale = instFlux[counts]
67 return cpputils::ABMagnitudeToNanojansky(magnitude) / scale;
68}
69
70double toNanojanskyErr(double instFluxErr, double scale) {
71 return instFluxErr * scale;
72}
73
74double toMagnitudeErr(double instFlux, double instFluxErr) {
75 return 2.5 / std::log(10.0) * (instFluxErr / instFlux);
76}
77
78} // anonymous namespace
79
80// ------------------- Conversions to nanojansky -------------------
81
82double PhotoCalib::instFluxToNanojansky(double instFlux, lsst::geom::Point<double, 2> const &point) const {
83 return toNanojansky(instFlux, evaluate(point));
84}
85
86double PhotoCalib::instFluxToNanojansky(double instFlux) const {
87 return toNanojansky(instFlux, _calibrationMean);
88}
89
90Measurement PhotoCalib::instFluxToNanojansky(double instFlux, double instFluxErr,
91 lsst::geom::Point<double, 2> const &point) const {
92 double calibration, error, nanojansky;
93 calibration = evaluate(point);
94 nanojansky = toNanojansky(instFlux, calibration);
95 error = toNanojanskyErr(instFluxErr, calibration);
96 return Measurement(nanojansky, error);
97}
98
99Measurement PhotoCalib::instFluxToNanojansky(double instFlux, double instFluxErr) const {
100 double nanojansky = toNanojansky(instFlux, _calibrationMean);
101 double error = toNanojanskyErr(instFluxErr, _calibrationMean);
102 return Measurement(nanojansky, error);
103}
104
106 std::string const &instFluxField) const {
107 auto position = sourceRecord.getCentroid();
108 auto instFluxKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFlux").key;
109 auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFluxErr").key;
110 return instFluxToNanojansky(sourceRecord.get(instFluxKey), sourceRecord.get(instFluxErrKey), position);
111}
112ndarray::Array<double, 2, 2> PhotoCalib::instFluxToNanojansky(afw::table::SourceCatalog const &sourceCatalog,
113 std::string const &instFluxField) const {
114 ndarray::Array<double, 2, 2> result =
115 ndarray::allocate(ndarray::makeVector(int(sourceCatalog.size()), 2));
116 instFluxToNanojanskyArray(sourceCatalog, instFluxField, result);
117 return result;
118}
119
121 std::string const &instFluxField, std::string const &outField) const {
122 auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
123 auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
124 auto nanojanskyKey = sourceCatalog.getSchema().find<double>(outField + "_flux").key;
125 auto nanojanskyErrKey = sourceCatalog.getSchema().find<double>(outField + "_fluxErr").key;
126 for (auto &record : sourceCatalog) {
127 auto result = instFluxToNanojansky(record.get(instFluxKey), record.get(instFluxErrKey),
128 record.getCentroid());
129 record.set(nanojanskyKey, result.value);
130 record.set(nanojanskyErrKey, result.error);
131 }
132}
133
134// ------------------- Conversions to Magnitudes -------------------
135
136double PhotoCalib::instFluxToMagnitude(double instFlux, lsst::geom::Point<double, 2> const &point) const {
137 return toMagnitude(instFlux, evaluate(point));
138}
139
140double PhotoCalib::instFluxToMagnitude(double instFlux) const {
141 return toMagnitude(instFlux, _calibrationMean);
142}
143
144Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr,
145 lsst::geom::Point<double, 2> const &point) const {
146 double calibration, error, magnitude;
147 calibration = evaluate(point);
148 magnitude = toMagnitude(instFlux, calibration);
149 error = toMagnitudeErr(instFlux, instFluxErr);
150 return Measurement(magnitude, error);
151}
152
153Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr) const {
154 double magnitude = toMagnitude(instFlux, _calibrationMean);
155 double error = toMagnitudeErr(instFlux, instFluxErr);
156 return Measurement(magnitude, error);
157}
158
160 std::string const &instFluxField) const {
161 auto position = sourceRecord.getCentroid();
162 auto instFluxKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFlux").key;
163 auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFluxErr").key;
164 return instFluxToMagnitude(sourceRecord.get(instFluxKey), sourceRecord.get(instFluxErrKey), position);
165}
166
167ndarray::Array<double, 2, 2> PhotoCalib::instFluxToMagnitude(afw::table::SourceCatalog const &sourceCatalog,
168 std::string const &instFluxField) const {
169 ndarray::Array<double, 2, 2> result =
170 ndarray::allocate(ndarray::makeVector(int(sourceCatalog.size()), 2));
171 instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
172 return result;
173}
174
176 std::string const &instFluxField, std::string const &outField) const {
177 auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
178 auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
179 auto magKey = sourceCatalog.getSchema().find<double>(outField + "_mag").key;
180 auto magErrKey = sourceCatalog.getSchema().find<double>(outField + "_magErr").key;
181 for (auto &record : sourceCatalog) {
182 auto result = instFluxToMagnitude(record.get(instFluxKey), record.get(instFluxErrKey),
183 record.getCentroid());
184 record.set(magKey, result.value);
185 record.set(magErrKey, result.error);
186 }
187}
188
189// ------------------- other utility methods -------------------
190
191double PhotoCalib::magnitudeToInstFlux(double magnitude) const {
192 return toInstFluxFromMagnitude(magnitude, _calibrationMean);
193}
194
195double PhotoCalib::magnitudeToInstFlux(double magnitude, lsst::geom::Point<double, 2> const &point) const {
196 return toInstFluxFromMagnitude(magnitude, evaluate(point));
197}
198
200 return *(_calibration) / _calibrationMean;
201}
202
206
207bool PhotoCalib::operator==(PhotoCalib const &rhs) const {
208 return (_calibrationMean == rhs._calibrationMean && _calibrationErr == rhs._calibrationErr &&
209 (*_calibration) == *(rhs._calibration));
210}
211
212double PhotoCalib::computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const {
213 return calibration->mean();
214}
215
217 return std::make_unique<PhotoCalib>(*this);
218}
219
221 std::stringstream buffer;
222 if (_isConstant)
223 buffer << "spatially constant with ";
224 else
225 buffer << *_calibration << " with ";
226 buffer << "mean: " << _calibrationMean << " error: " << _calibrationErr;
227 return buffer.str();
228}
229
230bool PhotoCalib::equals(typehandling::Storable const &other) const noexcept {
231 return singleClassEquals(*this, other);
232}
233
235 return os << photoCalib.toString();
236}
237
239 // Deep copy construct, as we're multiplying in-place.
240 auto result = MaskedImage<float>(maskedImage, true);
241 if (_isConstant) {
242 result *= _calibrationMean;
243 } else {
244 _calibration->multiplyImage(result, true); // only in the overlap region
245 }
246 return result;
247}
248
249
251 bool includeScaleUncertainty) const {
252 return calibrateImage(maskedImage);
253}
254
256 // Deep copy construct, as we're multiplying in-place.
257 auto result = MaskedImage<float>(maskedImage, true);
258 if (_isConstant) {
259 result /= _calibrationMean;
260 } else {
261 _calibration->divideImage(result, true); // only in the overlap region
262 }
263 return result;
264}
265
267 bool includeScaleUncertainty) const {
268 return uncalibrateImage(maskedImage);
269}
270
272 std::vector<std::string> const &instFluxFields) const {
273 auto const &inSchema = catalog.getSchema();
274 afw::table::SchemaMapper mapper(inSchema, true); // true: share the alias map
275 mapper.addMinimalSchema(inSchema);
276
277 using FieldD = afw::table::Field<double>;
278
279 struct Keys {
280 table::Key<double> instFlux;
281 table::Key<double> instFluxErr;
283 table::Key<double> fluxErr;
285 table::Key<double> magErr;
286 };
287
289 keys.reserve(instFluxFields.size());
290 for (auto const &field : instFluxFields) {
291 Keys newKey;
292 newKey.instFlux = inSchema[inSchema.join(field, "instFlux")];
293 newKey.flux =
294 mapper.addOutputField(FieldD(inSchema.join(field, "flux"), "calibrated flux", "nJy"), true);
295 newKey.mag = mapper.addOutputField(
296 FieldD(inSchema.join(field, "mag"), "calibrated magnitude", "mag(AB)"), true);
297 try {
298 newKey.instFluxErr = inSchema.find<double>(inSchema.join(field, "instFluxErr")).key;
299 newKey.fluxErr = mapper.addOutputField(
300 FieldD(inSchema.join(field, "fluxErr"), "calibrated flux uncertainty", "nJy"), true);
301 newKey.magErr = mapper.addOutputField(
302 FieldD(inSchema.join(field, "magErr"), "calibrated magnitude uncertainty", "mag(AB)"),
303 true);
305 ; // Keys struct defaults to invalid keys; that marks the error as missing.
306 }
307 keys.emplace_back(newKey);
308 }
309
310 // Create the new catalog
312 output.insert(mapper, output.begin(), catalog.begin(), catalog.end());
313
314 auto calibration = evaluateCatalog(output);
315
316 // fill in the catalog values
317 int iRec = 0;
318 for (auto &rec : output) {
319 for (auto &key : keys) {
320 double instFlux = rec.get(key.instFlux);
321 double nanojansky = toNanojansky(instFlux, calibration[iRec]);
322 rec.set(key.flux, nanojansky);
323 rec.set(key.mag, toMagnitude(instFlux, calibration[iRec]));
324 if (key.instFluxErr.isValid()) {
325 double instFluxErr = rec.get(key.instFluxErr);
326 rec.set(key.fluxErr, toNanojanskyErr(instFluxErr, calibration[iRec]));
327 rec.set(key.magErr, toMagnitudeErr(instFlux, instFluxErr));
328 }
329 }
330 ++iRec;
331 }
332
333 return output;
334}
335
337 std::vector<std::string> instFluxFields;
338 static std::string const SUFFIX = "_instFlux";
339 for (auto const &name : catalog.getSchema().getNames()) {
340 // Pick every field ending in "_instFlux", grabbing everything before that prefix.
341 if (name.size() > SUFFIX.size() + 1 &&
342 name.compare(name.size() - SUFFIX.size(), SUFFIX.size(), SUFFIX) == 0) {
343 instFluxFields.emplace_back(name.substr(0, name.size() - 9));
344 }
345 }
346 return calibrateCatalog(catalog, instFluxFields);
347}
348
349// ------------------- persistence -------------------
350
351namespace {
352
353class PhotoCalibSchema {
354public:
355 table::Schema schema;
356 table::Key<double> calibrationMean;
357 table::Key<double> calibrationErr;
358 table::Key<table::Flag> isConstant;
359 table::Key<int> field;
361
362 // No copying
363 PhotoCalibSchema(PhotoCalibSchema const &) = delete;
364 PhotoCalibSchema &operator=(PhotoCalibSchema const &) = delete;
365 // No moving
366 PhotoCalibSchema(PhotoCalibSchema &&) = delete;
367 PhotoCalibSchema &operator=(PhotoCalibSchema &&) = delete;
368
369 static PhotoCalibSchema const &get() {
370 static PhotoCalibSchema const instance;
371 return instance;
372 }
373
374private:
375 PhotoCalibSchema()
376 : schema(),
377 calibrationMean(schema.addField<double>(
378 "calibrationMean", "mean calibration on this PhotoCalib's domain", "count")),
379 calibrationErr(
380 schema.addField<double>("calibrationErr", "1-sigma error on calibrationMean", "count")),
381 isConstant(schema.addField<table::Flag>("isConstant", "Is this spatially-constant?")),
382 field(schema.addField<int>("field", "archive ID of the BoundedField object")),
383 version(schema.addField<int>("version", "version of this PhotoCalib")) {}
384};
385
386class PhotoCalibFactory : public table::io::PersistableFactory {
387public:
388 std::shared_ptr<table::io::Persistable> read(InputArchive const &archive,
389 CatalogVector const &catalogs) const override {
390 table::BaseRecord const &record = catalogs.front().front();
391 PhotoCalibSchema const &keys = PhotoCalibSchema::get();
392 int version = getVersion(record);
393 if (version < 1) {
394 throw(pex::exceptions::RuntimeError("Unsupported version (version 0 was defined in maggies): " +
395 std::to_string(version)));
396 }
397 return std::make_shared<PhotoCalib>(record.get(keys.calibrationMean), record.get(keys.calibrationErr),
398 archive.get<afw::math::BoundedField>(record.get(keys.field)),
399 record.get(keys.isConstant));
400 }
401
402 PhotoCalibFactory(std::string const &name) : afw::table::io::PersistableFactory(name) {}
403
404protected:
405 int getVersion(table::BaseRecord const &record) const {
406 int version = -1;
407 try {
408 std::string versionName = "version";
409 auto versionKey = record.getSchema().find<int>(versionName);
410 version = record.get(versionKey.key);
411 } catch (const pex::exceptions::NotFoundError &) {
412 // un-versioned files are version 0
413 version = 0;
414 }
415 return version;
416 }
417};
418
419std::string getPhotoCalibPersistenceName() { return "PhotoCalib"; }
420
421PhotoCalibFactory registration(getPhotoCalibPersistenceName());
422
423} // namespace
424
425/*
426 * Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
427 */
428
429namespace {
430int const CALIB_TABLE_CURRENT_VERSION = 2; // final version of Calib in ExposureTable
431std::string const EXPTIME_FIELD_NAME = "exptime"; // name of exposure time field (no longer used)
432
433class CalibKeys {
434public:
437 table::Key<double> expTime;
438 table::Key<double> fluxMag0;
439 table::Key<double> fluxMag0Err;
440
441 // No copying
442 CalibKeys(const CalibKeys &) = delete;
443 CalibKeys &operator=(const CalibKeys &) = delete;
444
445 // No moving
446 CalibKeys(CalibKeys &&) = delete;
447 CalibKeys &operator=(CalibKeys &&) = delete;
448
449 CalibKeys(int tableVersion = CALIB_TABLE_CURRENT_VERSION)
450 : schema(), midTime(), expTime(), fluxMag0(), fluxMag0Err() {
451 if (tableVersion == 1) {
452 // obsolete fields
453 midTime = schema.addField<std::int64_t>(
454 "midtime", "middle of the time of the exposure relative to Unix epoch", "ns");
455 expTime = schema.addField<double>(EXPTIME_FIELD_NAME, "exposure time", "s");
456 }
457 fluxMag0 = schema.addField<double>("fluxmag0", "flux of a zero-magnitude object", "count");
458 fluxMag0Err = schema.addField<double>("fluxmag0.err", "1-sigma error on fluxmag0", "count");
459 }
460};
461
462class CalibFactory : public table::io::PersistableFactory {
463public:
464 std::shared_ptr<table::io::Persistable> read(InputArchive const &archive,
465 CatalogVector const &catalogs) const override {
466 // table version is not persisted, so we don't have a clean way to determine the version;
467 // the hack is version = 1 if exptime found, else current
468 int tableVersion = 1;
469 try {
470 catalogs.front().getSchema().find<double>(EXPTIME_FIELD_NAME);
471 } catch (pex::exceptions::NotFoundError const &) {
472 tableVersion = CALIB_TABLE_CURRENT_VERSION;
473 }
474
475 CalibKeys const keys{tableVersion};
476 LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
477 LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
478 LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys.schema);
479 table::BaseRecord const &record = catalogs.front().front();
480
481 double calibration = cpputils::referenceFlux / record.get(keys.fluxMag0);
482 double calibrationErr = cpputils::referenceFlux * record.get(keys.fluxMag0Err) /
483 std::pow(record.get(keys.fluxMag0), 2);
484 return std::make_shared<PhotoCalib>(calibration, calibrationErr);
485 }
486
487 explicit CalibFactory(std::string const &name) : table::io::PersistableFactory(name) {}
488};
489
490std::string getCalibPersistenceName() { return "Calib"; }
491
492CalibFactory calibRegistration(getCalibPersistenceName());
493
494} // namespace
495
496std::string PhotoCalib::getPersistenceName() const { return getPhotoCalibPersistenceName(); }
497
499 PhotoCalibSchema const &keys = PhotoCalibSchema::get();
500 table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
501 auto record = catalog.addNew();
502 record->set(keys.calibrationMean, _calibrationMean);
503 record->set(keys.calibrationErr, _calibrationErr);
504 record->set(keys.isConstant, _isConstant);
505 record->set(keys.field, handle.put(_calibration));
506 record->set(keys.version, SERIALIZATION_VERSION);
507 handle.saveCatalog(catalog);
508}
509
510// ------------------- private/protected helpers -------------------
511
512double PhotoCalib::evaluate(lsst::geom::Point<double, 2> const &point) const {
513 if (_isConstant)
514 return _calibrationMean;
515 else
516 return _calibration->evaluate(point);
517}
518
519ndarray::Array<double, 1> PhotoCalib::evaluateArray(ndarray::Array<double, 1> const &xx,
520 ndarray::Array<double, 1> const &yy) const {
521 if (_isConstant) {
522 ndarray::Array<double, 1> result = ndarray::allocate(ndarray::makeVector(xx.size()));
523 result.deep() = _calibrationMean;
524 return result;
525 } else {
526 return _calibration->evaluate(xx, yy);
527 }
528}
529
530ndarray::Array<double, 1> PhotoCalib::evaluateCatalog(afw::table::SourceCatalog const &sourceCatalog) const {
531 ndarray::Array<double, 1> xx = ndarray::allocate(ndarray::makeVector(sourceCatalog.size()));
532 ndarray::Array<double, 1> yy = ndarray::allocate(ndarray::makeVector(sourceCatalog.size()));
533 size_t i = 0;
534 for (auto const &rec : sourceCatalog) {
535 auto point = rec.getCentroid();
536 xx[i] = point.getX();
537 yy[i] = point.getY();
538 ++i;
539 }
540 return evaluateArray(xx, yy);
541}
542
543void PhotoCalib::instFluxToNanojanskyArray(afw::table::SourceCatalog const &sourceCatalog,
544 std::string const &instFluxField,
545 ndarray::Array<double, 2, 2> result) const {
546 auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
547 auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
548
549 auto calibration = evaluateCatalog(sourceCatalog);
550 int i = 0;
551 auto iter = result.begin();
552 for (auto const &rec : sourceCatalog) {
553 double instFlux = rec.get(instFluxKey);
554 double instFluxErr = rec.get(instFluxErrKey);
555 double nanojansky = toNanojansky(instFlux, calibration[i]);
556 (*iter)[0] = nanojansky;
557 (*iter)[1] = toNanojanskyErr(instFluxErr, calibration[i]);
558 ++iter;
559 ++i;
560 }
561}
562
563void PhotoCalib::instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
564 std::string const &instFluxField,
565 ndarray::Array<double, 2, 2> result) const {
566 auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
567 auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
568
569 auto calibration = evaluateCatalog(sourceCatalog);
570 auto iter = result.begin();
571 int i = 0;
572 for (auto const &rec : sourceCatalog) {
573 double instFlux = rec.get(instFluxKey);
574 double instFluxErr = rec.get(instFluxErrKey);
575 (*iter)[0] = toMagnitude(instFlux, calibration[i]);
576 (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr);
577 ++iter;
578 ++i;
579 }
580}
581
583 auto key = "FLUXMAG0";
584 if (metadata.exists(key)) {
585 double instFluxMag0 = metadata.getAsDouble(key);
586 if (strip) metadata.remove(key);
587
588 double instFluxMag0Err = 0.0;
589 key = "FLUXMAG0ERR";
590 if (metadata.exists(key)) {
591 instFluxMag0Err = metadata.getAsDouble(key);
592 if (strip) metadata.remove(key);
593 }
594 return makePhotoCalibFromCalibZeroPoint(instFluxMag0, instFluxMag0Err);
595 } else {
596 return nullptr;
597 }
598}
599
600std::shared_ptr<PhotoCalib> makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err) {
601 double calibration = cpputils::referenceFlux / instFluxMag0;
602 double calibrationErr = cpputils::referenceFlux * instFluxMag0Err / std::pow(instFluxMag0, 2);
603 return std::make_shared<PhotoCalib>(calibration, calibrationErr);
604}
605
606} // namespace image
607} // namespace afw
608} // namespace lsst
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
Utilities for converting between flux and magnitude in C++.
Implementation of the Photometric Calibration class.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition Persistable.h:48
A class to manipulate images, masks, and variance as a single object.
Definition MaskedImage.h:74
The photometric calibration of an exposure.
Definition PhotoCalib.h:114
afw::table::SourceCatalog calibrateCatalog(afw::table::SourceCatalog const &catalog, std::vector< std::string > const &instFluxFields) const
Return a flux calibrated catalog, with new _flux, _fluxErr, _mag, and _magErr fields.
bool operator==(PhotoCalib const &rhs) const
Two PhotoCalibs are equal if their component bounded fields and calibrationErr are equal.
double instFluxToNanojansky(double instFlux, lsst::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to nJy at a point in the BoundedField.
Definition PhotoCalib.cc:82
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
PhotoCalib(PhotoCalib const &)=default
double instFluxToMagnitude(double instFlux, lsst::geom::Point< double, 2 > const &point) const
Convert instFlux in ADU to AB magnitude.
std::string toString() const override
Create a string representation of this object.
bool equals(typehandling::Storable const &other) const noexcept override
Compare this object to another Storable.
MaskedImage< float > uncalibrateImage(MaskedImage< float > const &maskedImage) const
Return a un-calibrated image, with pixel values in ADU (or whatever the original input to this photoC...
std::shared_ptr< typehandling::Storable > cloneStorable() const override
Create a new PhotoCalib that is a copy of this one.
std::shared_ptr< afw::math::BoundedField > computeScalingTo(std::shared_ptr< PhotoCalib > other) const
Calculates the scaling between this PhotoCalib and another PhotoCalib.
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
std::shared_ptr< afw::math::BoundedField > computeScaledCalibration() const
Calculates the spatially-variable calibration, normalized by the mean in the valid domain.
MaskedImage< float > calibrateImage(MaskedImage< float > const &maskedImage) const
Return a flux calibrated image, with pixel values in nJy.
double magnitudeToInstFlux(double magnitude, lsst::geom::Point< double, 2 > const &point) const
Convert AB magnitude to instFlux (ADU).
Base class for all records.
Definition BaseRecord.h:31
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
Definition BaseRecord.h:151
Schema getSchema() const
Return the Schema that holds this record's fields and keys.
Definition BaseRecord.h:80
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Definition Catalog.h:489
A class used as a handle to a particular field in a table.
Definition Key.h:53
Defines the fields and offsets for a table.
Definition Schema.h:51
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition Schema.cc:467
A mapping between the keys of two Schemas, used to copy data between them.
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
Key< T > addOutputField(Field< T > const &newField, bool doReplace=false)
Add a new field to the output Schema that is not connected to the input Schema.
void addMinimalSchema(Schema const &minimal, bool doMap=true)
Add the given minimal schema to the output schema.
Record class that contains measurements made on a single exposure.
Definition Source.h:78
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
Definition Source.h:569
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
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...
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
A base class for factory classes used to reconstruct objects from records.
io::OutputArchiveHandle OutputArchiveHandle
Interface supporting iteration over heterogenous containers.
Definition Storable.h:58
static bool singleClassEquals(T const &lhs, Storable const &rhs)
Test if a Storable is of a particular class and equal to another object.
Definition Storable.h:151
Class for storing generic metadata.
Definition PropertySet.h:67
virtual void remove(std::string const &name)
Remove all values for a property name (possibly hierarchical).
bool exists(std::string const &name) const
Determine if a name (possibly hierarchical) exists.
double getAsDouble(std::string const &name) const
Get the last value for any arithmetic property name (possibly hierarchical).
A coordinate class intended to represent absolute positions.
Definition Point.h:169
Reports errors in the logical structure of the program.
Definition Runtime.h:46
Reports attempts to access elements using an invalid key.
Definition Runtime.h:151
T emplace_back(T... args)
T get(T... args)
T log(T... args)
T make_shared(T... args)
scale(algorithm, min, max=None, frame=None)
Definition ds9.py:108
std::shared_ptr< PhotoCalib > makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err)
Construct a PhotoCalib from the deprecated Calib-style instFluxMag0/instFluxMag0Err values.
std::shared_ptr< PhotoCalib > makePhotoCalibFromMetadata(daf::base::PropertySet &metadata, bool strip=false)
Construct a PhotoCalib from FITS FLUXMAG0/FLUXMAG0ERR keywords.
std::ostream & operator<<(std::ostream &os, Measurement const &measurement)
Definition PhotoCalib.cc:48
CatalogT< BaseRecord > BaseCatalog
Definition fwd.h:72
SortedCatalogT< SourceRecord > SourceCatalog
Definition fwd.h:85
const double referenceFlux
The Oke & Gunn (1983) AB magnitude reference flux, in nJy (often approximated as 3631....
Definition Magnitude.h:46
double ABMagnitudeToNanojansky(double magnitude)
Convert an AB magnitude to a flux in nanojansky.
Definition Magnitude.cc:32
double nanojanskyToABMagnitude(double flux)
Convert a flux in nanojansky to AB magnitude.
Definition Magnitude.cc:30
T pow(T... args)
T setprecision(T... args)
T size(T... args)
T str(T... args)
A value and its error.
Definition PhotoCalib.h:51
A description of a field in a table.
Definition Field.h:24
T to_string(T... args)
std::shared_ptr< table::io::Persistable > read(table::io::InputArchive const &archive, table::io::CatalogVector const &catalogs) const override