lsst.afw  17.0.1-24-g0055ebd3c+3
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 
25 #include "lsst/geom/Point.h"
28 #include "lsst/afw/table.h"
32 #include "lsst/pex/exceptions.h"
33 #include "ndarray.h"
35 #include "lsst/utils/Magnitude.h"
36 
37 namespace lsst {
38 namespace afw {
39 
42 
43 namespace image {
44 
45 // ------------------- helpers -------------------
46 
49  s << std::setprecision(std::numeric_limits<long double>::digits10 + 1);
50  s << "value=" << measurement.value << ", error=" << measurement.error;
51  return os << s.str();
52 }
53 
54 namespace {
55 
56 int const SERIALIZATION_VERSION = 1;
57 
58 double toNanojansky(double instFlux, double scale) { return instFlux * scale; }
59 
60 double toMagnitude(double instFlux, double scale) { return utils::nanojanskyToABMagnitude(instFlux * scale); }
61 
62 double toInstFluxFromMagnitude(double magnitude, double scale) {
63  // Note: flux[nJy] / scale = instFlux[counts]
64  return utils::ABMagnitudeToNanojansky(magnitude) / scale;
65 }
66 
67 double toNanojanskyErr(double instFlux, double instFluxErr, double scale, double scaleErr,
68  double nanojansky) {
69  return std::abs(nanojansky) * hypot(instFluxErr / instFlux, scaleErr / scale);
70 }
71 
85 void toNanojanskyVariance(ndarray::Array<float const, 2, 1> const &instFlux,
86  ndarray::Array<float const, 2, 1> const &instFluxVar, float scaleErr,
87  ndarray::Array<float const, 2, 1> const &flux, ndarray::Array<float, 2, 1> out) {
88  auto eigenFlux = ndarray::asEigen<Eigen::ArrayXpr>(flux);
89  auto eigenInstFluxVar = ndarray::asEigen<Eigen::ArrayXpr>(instFluxVar);
90  auto eigenInstFlux = ndarray::asEigen<Eigen::ArrayXpr>(instFlux);
91  auto eigenOut = ndarray::asEigen<Eigen::ArrayXpr>(out);
92  eigenOut = eigenFlux.square() *
93  (eigenInstFluxVar / eigenInstFlux.square() + (scaleErr / eigenFlux * eigenInstFlux).square());
94 }
95 
96 double toMagnitudeErr(double instFlux, double instFluxErr, double scale, double scaleErr) {
97  return 2.5 / log(10.0) * hypot(instFluxErr / instFlux, scaleErr / scale);
98 }
99 
100 } // anonymous namespace
101 
102 // ------------------ Deprecated Calib interface -------------------
103 
104 ndarray::Array<double, 1> PhotoCalib::getMagnitude(ndarray::Array<double const, 1> const &instFlux) const {
105  ndarray::Array<double, 1> result = ndarray::allocate(ndarray::makeVector(int(instFlux.size())));
106  auto iter = result.begin();
107  for (auto const &i : instFlux) {
108  *iter = toMagnitude(i, _calibrationMean);
109  iter++;
110  }
111  return result;
112 }
113 
114 std::pair<ndarray::Array<double, 1>, ndarray::Array<double, 1>> PhotoCalib::getMagnitude(
115  ndarray::Array<double const, 1> const &instFlux,
116  ndarray::Array<double const, 1> const &instFluxErr) const {
117  ndarray::Array<double, 1> mag = ndarray::allocate(ndarray::makeVector(int(instFlux.size())));
118  ndarray::Array<double, 1> magErr = ndarray::allocate(ndarray::makeVector(int(instFlux.size())));
119  auto iMag = mag.begin();
120  auto iMagErr = magErr.begin();
121  for (auto i = instFlux.begin(), iErr = instFluxErr.begin(); i != instFlux.end(); ++i, ++iErr) {
122  *iMag = toMagnitude(*i, _calibrationMean);
123  *iMagErr = toMagnitudeErr(*i, *iErr, _calibrationMean, _calibrationErr);
124  iMag++;
125  iMagErr++;
126  }
127  return std::make_pair(mag, magErr);
128 }
129 
130 // ------------------- Conversions to nanojansky -------------------
131 
132 double PhotoCalib::instFluxToNanojansky(double instFlux, lsst::geom::Point<double, 2> const &point) const {
133  return toNanojansky(instFlux, evaluate(point));
134 }
135 
136 double PhotoCalib::instFluxToNanojansky(double instFlux) const {
137  return toNanojansky(instFlux, _calibrationMean);
138 }
139 
140 Measurement PhotoCalib::instFluxToNanojansky(double instFlux, double instFluxErr,
141  lsst::geom::Point<double, 2> const &point) const {
142  double calibration, error, nanojansky;
143  calibration = evaluate(point);
144  nanojansky = toNanojansky(instFlux, calibration);
145  error = toNanojanskyErr(instFlux, instFluxErr, calibration, _calibrationErr, nanojansky);
146  return Measurement(nanojansky, error);
147 }
148 
149 Measurement PhotoCalib::instFluxToNanojansky(double instFlux, double instFluxErr) const {
150  double nanojansky = toNanojansky(instFlux, _calibrationMean);
151  double error = toNanojanskyErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr, nanojansky);
152  return Measurement(nanojansky, error);
153 }
154 
155 Measurement PhotoCalib::instFluxToNanojansky(afw::table::SourceRecord const &sourceRecord,
156  std::string const &instFluxField) const {
157  auto position = sourceRecord.getCentroid();
158  auto instFluxKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFlux").key;
159  auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFluxErr").key;
160  return instFluxToNanojansky(sourceRecord.get(instFluxKey), sourceRecord.get(instFluxErrKey), position);
161 }
162 ndarray::Array<double, 2, 2> PhotoCalib::instFluxToNanojansky(afw::table::SourceCatalog const &sourceCatalog,
163  std::string const &instFluxField) const {
164  ndarray::Array<double, 2, 2> result =
165  ndarray::allocate(ndarray::makeVector(int(sourceCatalog.size()), 2));
166  instFluxToNanojanskyArray(sourceCatalog, instFluxField, result);
167  return result;
168 }
169 
170 void PhotoCalib::instFluxToNanojansky(afw::table::SourceCatalog &sourceCatalog,
171  std::string const &instFluxField, std::string const &outField) const {
172  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
173  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
174  auto nanojanskyKey = sourceCatalog.getSchema().find<double>(outField + "_flux").key;
175  auto nanojanskyErrKey = sourceCatalog.getSchema().find<double>(outField + "_fluxErr").key;
176  for (auto &record : sourceCatalog) {
177  auto result = instFluxToNanojansky(record.get(instFluxKey), record.get(instFluxErrKey),
178  record.getCentroid());
179  record.set(nanojanskyKey, result.value);
180  record.set(nanojanskyErrKey, result.error);
181  }
182 }
183 
184 // ------------------- Conversions to Magnitudes -------------------
185 
186 double PhotoCalib::instFluxToMagnitude(double instFlux, lsst::geom::Point<double, 2> const &point) const {
187  return toMagnitude(instFlux, evaluate(point));
188 }
189 
190 double PhotoCalib::instFluxToMagnitude(double instFlux) const {
191  return toMagnitude(instFlux, _calibrationMean);
192 }
193 
194 Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr,
195  lsst::geom::Point<double, 2> const &point) const {
196  double calibration, error, magnitude;
197  calibration = evaluate(point);
198  magnitude = toMagnitude(instFlux, calibration);
199  error = toMagnitudeErr(instFlux, instFluxErr, calibration, _calibrationErr);
200  return Measurement(magnitude, error);
201 }
202 
203 Measurement PhotoCalib::instFluxToMagnitude(double instFlux, double instFluxErr) const {
204  double magnitude = toMagnitude(instFlux, _calibrationMean);
205  double error = toMagnitudeErr(instFlux, instFluxErr, _calibrationMean, _calibrationErr);
206  return Measurement(magnitude, error);
207 }
208 
209 Measurement PhotoCalib::instFluxToMagnitude(afw::table::SourceRecord const &sourceRecord,
210  std::string const &instFluxField) const {
211  auto position = sourceRecord.getCentroid();
212  auto instFluxKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFlux").key;
213  auto instFluxErrKey = sourceRecord.getSchema().find<double>(instFluxField + "_instFluxErr").key;
214  return instFluxToMagnitude(sourceRecord.get(instFluxKey), sourceRecord.get(instFluxErrKey), position);
215 }
216 
217 ndarray::Array<double, 2, 2> PhotoCalib::instFluxToMagnitude(afw::table::SourceCatalog const &sourceCatalog,
218  std::string const &instFluxField) const {
219  ndarray::Array<double, 2, 2> result =
220  ndarray::allocate(ndarray::makeVector(int(sourceCatalog.size()), 2));
221  instFluxToMagnitudeArray(sourceCatalog, instFluxField, result);
222  return result;
223 }
224 
225 void PhotoCalib::instFluxToMagnitude(afw::table::SourceCatalog &sourceCatalog,
226  std::string const &instFluxField, std::string const &outField) const {
227  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
228  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
229  auto magKey = sourceCatalog.getSchema().find<double>(outField + "_mag").key;
230  auto magErrKey = sourceCatalog.getSchema().find<double>(outField + "_magErr").key;
231  for (auto &record : sourceCatalog) {
232  auto result = instFluxToMagnitude(record.get(instFluxKey), record.get(instFluxErrKey),
233  record.getCentroid());
234  record.set(magKey, result.value);
235  record.set(magErrKey, result.error);
236  }
237 }
238 
239 // ------------------- other utility methods -------------------
240 
241 double PhotoCalib::magnitudeToInstFlux(double magnitude) const {
242  return toInstFluxFromMagnitude(magnitude, _calibrationMean);
243 }
244 
245 double PhotoCalib::magnitudeToInstFlux(double magnitude, lsst::geom::Point<double, 2> const &point) const {
246  return toInstFluxFromMagnitude(magnitude, evaluate(point));
247 }
248 
249 std::shared_ptr<math::BoundedField> PhotoCalib::computeScaledCalibration() const {
250  return *(_calibration) / _calibrationMean;
251 }
252 
254  throw LSST_EXCEPT(pex::exceptions::LogicError, "Not Implemented: See DM-10154.");
255 }
256 
257 bool PhotoCalib::operator==(PhotoCalib const &rhs) const {
258  return (_calibrationMean == rhs._calibrationMean && _calibrationErr == rhs._calibrationErr &&
259  (*_calibration) == *(rhs._calibration));
260 }
261 
262 double PhotoCalib::computeCalibrationMean(std::shared_ptr<afw::math::BoundedField> calibration) const {
263  return calibration->mean();
264 }
265 
267  if (photoCalib._isConstant)
268  os << "spatially constant with ";
269  else
270  os << *(photoCalib._calibration) << " with ";
271  return os << "mean: " << photoCalib._calibrationMean << " error: " << photoCalib._calibrationErr;
272 }
273 
274 MaskedImage<float> PhotoCalib::calibrateImage(MaskedImage<float> const &maskedImage,
275  bool includeScaleUncertainty) const {
276  // Deep copy construct, as we're mutiplying in-place.
277  auto result = MaskedImage<float>(maskedImage, true);
278 
279  if (_isConstant) {
280  *(result.getImage()) *= _calibrationMean;
281  } else {
282  _calibration->multiplyImage(*(result.getImage()), true); // only in the overlap region
283  }
284  if (includeScaleUncertainty) {
285  toNanojanskyVariance(maskedImage.getImage()->getArray(), maskedImage.getVariance()->getArray(),
286  _calibrationErr, result.getImage()->getArray(),
287  result.getVariance()->getArray());
288  } else {
289  toNanojanskyVariance(maskedImage.getImage()->getArray(), maskedImage.getVariance()->getArray(), 0,
290  result.getImage()->getArray(), result.getVariance()->getArray());
291  }
292 
293  return result;
294 }
295 
296 afw::table::SourceCatalog PhotoCalib::calibrateCatalog(afw::table::SourceCatalog const &catalog,
297  std::vector<std::string> const &instFluxFields) const {
298  auto const &inSchema = catalog.getSchema();
299  afw::table::SchemaMapper mapper(inSchema, true); // true: share the alias map
300  mapper.addMinimalSchema(inSchema);
301 
302  using FieldD = afw::table::Field<double>;
303 
304  struct Keys {
305  table::Key<double> instFlux;
306  table::Key<double> instFluxErr;
307  table::Key<double> flux;
308  table::Key<double> fluxErr;
309  table::Key<double> mag;
310  table::Key<double> magErr;
311  };
312 
314  keys.reserve(instFluxFields.size());
315  for (auto const &field : instFluxFields) {
316  Keys newKey;
317  newKey.instFlux = inSchema[inSchema.join(field, "instFlux")];
318  newKey.flux =
319  mapper.addOutputField(FieldD(inSchema.join(field, "flux"), "calibrated flux", "nJy"), true);
320  newKey.mag = mapper.addOutputField(
321  FieldD(inSchema.join(field, "mag"), "calibrated magnitude", "mag(AB)"), true);
322  try {
323  newKey.instFluxErr = inSchema.find<double>(inSchema.join(field, "instFluxErr")).key;
324  newKey.fluxErr = mapper.addOutputField(
325  FieldD(inSchema.join(field, "fluxErr"), "calibrated flux uncertainty", "nJy"), true);
326  newKey.magErr = mapper.addOutputField(
327  FieldD(inSchema.join(field, "magErr"), "calibrated magnitude uncertainty", "mag(AB)"),
328  true);
329  } catch (pex::exceptions::NotFoundError &) {
330  ; // Keys struct defaults to invalid keys; that marks the error as missing.
331  }
332  keys.emplace_back(newKey);
333  }
334 
335  // Create the new catalog
337  output.insert(mapper, output.begin(), catalog.begin(), catalog.end());
338 
339  auto calibration = evaluateCatalog(output);
340 
341  // fill in the catalog values
342  int iRec = 0;
343  for (auto &rec : output) {
344  for (auto &key : keys) {
345  double instFlux = rec.get(key.instFlux);
346  double nanojansky = toNanojansky(instFlux, calibration[iRec]);
347  rec.set(key.flux, nanojansky);
348  rec.set(key.mag, toMagnitude(instFlux, calibration[iRec]));
349  if (key.instFluxErr.isValid()) {
350  double instFluxErr = rec.get(key.instFluxErr);
351  rec.set(key.fluxErr, toNanojanskyErr(instFlux, instFluxErr, calibration[iRec],
352  _calibrationErr, nanojansky));
353  rec.set(key.magErr,
354  toMagnitudeErr(instFlux, instFluxErr, calibration[iRec], _calibrationErr));
355  }
356  }
357  ++iRec;
358  }
359 
360  return output;
361 }
362 
363 afw::table::SourceCatalog PhotoCalib::calibrateCatalog(afw::table::SourceCatalog const &catalog) const {
364  std::vector<std::string> instFluxFields;
365  static std::string const SUFFIX = "_instFlux";
366  for (auto const &name : catalog.getSchema().getNames()) {
367  // Pick every field ending in "_instFlux", grabbing everything before that prefix.
368  if (name.size() > SUFFIX.size() + 1 &&
369  name.compare(name.size() - SUFFIX.size(), SUFFIX.size(), SUFFIX) == 0) {
370  instFluxFields.emplace_back(name.substr(0, name.size() - 9));
371  }
372  }
373  return calibrateCatalog(catalog, instFluxFields);
374 }
375 
376 // ------------------- persistence -------------------
377 
378 namespace {
379 
380 class PhotoCalibSchema {
381 public:
382  table::Schema schema;
383  table::Key<double> calibrationMean;
384  table::Key<double> calibrationErr;
385  table::Key<table::Flag> isConstant;
386  table::Key<int> field;
387  table::Key<int> version;
388 
389  // No copying
390  PhotoCalibSchema(PhotoCalibSchema const &) = delete;
391  PhotoCalibSchema &operator=(PhotoCalibSchema const &) = delete;
392  // No moving
393  PhotoCalibSchema(PhotoCalibSchema &&) = delete;
394  PhotoCalibSchema &operator=(PhotoCalibSchema &&) = delete;
395 
396  static PhotoCalibSchema const &get() {
397  static PhotoCalibSchema const instance;
398  return instance;
399  }
400 
401 private:
402  PhotoCalibSchema()
403  : schema(),
404  calibrationMean(schema.addField<double>(
405  "calibrationMean", "mean calibration on this PhotoCalib's domain", "count")),
407  schema.addField<double>("calibrationErr", "1-sigma error on calibrationMean", "count")),
408  isConstant(schema.addField<table::Flag>("isConstant", "Is this spatially-constant?")),
409  field(schema.addField<int>("field", "archive ID of the BoundedField object")),
410  version(schema.addField<int>("version", "version of this PhotoCalib")) {
411  schema.getCitizen().markPersistent();
412  }
413 };
414 
415 class PhotoCalibFactory : public table::io::PersistableFactory {
416 public:
417  PTR(table::io::Persistable)
418  read(InputArchive const &archive, CatalogVector const &catalogs) const override {
419  table::BaseRecord const &record = catalogs.front().front();
420  PhotoCalibSchema const &keys = PhotoCalibSchema::get();
421  int version = getVersion(record);
422  if (version < 1) {
423  throw(pex::exceptions::RuntimeError("Unsupported version (version 0 was defined in maggies): " +
424  std::to_string(version)));
425  }
426  return std::make_shared<PhotoCalib>(record.get(keys.calibrationMean), record.get(keys.calibrationErr),
427  archive.get<afw::math::BoundedField>(record.get(keys.field)),
428  record.get(keys.isConstant));
429  }
430 
431  PhotoCalibFactory(std::string const &name) : afw::table::io::PersistableFactory(name) {}
432 
433 protected:
434  int getVersion(table::BaseRecord const &record) const {
435  int version = -1;
436  try {
437  std::string versionName = "version";
438  auto versionKey = record.getSchema().find<int>(versionName);
439  version = record.get(versionKey.key);
440  } catch (const pex::exceptions::NotFoundError &) {
441  // un-versioned files are version 0
442  version = 0;
443  }
444  return version;
445  }
446 };
447 
448 std::string getPhotoCalibPersistenceName() { return "PhotoCalib"; }
449 
450 PhotoCalibFactory registration(getPhotoCalibPersistenceName());
451 
452 } // namespace
453 
458 namespace {
459 int const CALIB_TABLE_CURRENT_VERSION = 2; // final version of Calib in ExposureTable
460 std::string const EXPTIME_FIELD_NAME = "exptime"; // name of exposure time field (no longer used)
461 
462 class CalibKeys {
463 public:
464  table::Schema schema;
465  table::Key<std::int64_t> midTime;
466  table::Key<double> expTime;
467  table::Key<double> fluxMag0;
468  table::Key<double> fluxMag0Err;
469 
470  // No copying
471  CalibKeys(const CalibKeys &) = delete;
472  CalibKeys &operator=(const CalibKeys &) = delete;
473 
474  // No moving
475  CalibKeys(CalibKeys &&) = delete;
476  CalibKeys &operator=(CalibKeys &&) = delete;
477 
478  CalibKeys(int tableVersion = CALIB_TABLE_CURRENT_VERSION)
479  : schema(), midTime(), expTime(), fluxMag0(), fluxMag0Err() {
480  if (tableVersion == 1) {
481  // obsolete fields
482  midTime = schema.addField<std::int64_t>(
483  "midtime", "middle of the time of the exposure relative to Unix epoch", "ns");
484  expTime = schema.addField<double>(EXPTIME_FIELD_NAME, "exposure time", "s");
485  }
486  fluxMag0 = schema.addField<double>("fluxmag0", "flux of a zero-magnitude object", "count");
487  fluxMag0Err = schema.addField<double>("fluxmag0.err", "1-sigma error on fluxmag0", "count");
488  }
489 };
490 
491 class CalibFactory : public table::io::PersistableFactory {
492 public:
493  std::shared_ptr<table::io::Persistable> read(InputArchive const &archive,
494  CatalogVector const &catalogs) const override {
495  // table version is not persisted, so we don't have a clean way to determine the version;
496  // the hack is version = 1 if exptime found, else current
497  int tableVersion = 1;
498  try {
499  catalogs.front().getSchema().find<double>(EXPTIME_FIELD_NAME);
500  } catch (pex::exceptions::NotFoundError) {
501  tableVersion = CALIB_TABLE_CURRENT_VERSION;
502  }
503 
504  CalibKeys const keys{tableVersion};
505  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
506  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
507  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys.schema);
508  table::BaseRecord const &record = catalogs.front().front();
509 
510  double calibration = utils::referenceFlux / record.get(keys.fluxMag0);
511  double calibrationErr =
512  utils::referenceFlux * record.get(keys.fluxMag0Err) / std::pow(record.get(keys.fluxMag0), 2);
513  return std::make_shared<PhotoCalib>(calibration, calibrationErr);
514  }
515 
516  explicit CalibFactory(std::string const &name) : table::io::PersistableFactory(name) {}
517 };
518 
519 std::string getCalibPersistenceName() { return "Calib"; }
520 
521 CalibFactory calibRegistration(getCalibPersistenceName());
522 
523 } // namespace
524 
525 std::string PhotoCalib::getPersistenceName() const { return getPhotoCalibPersistenceName(); }
526 
527 void PhotoCalib::write(OutputArchiveHandle &handle) const {
528  PhotoCalibSchema const &keys = PhotoCalibSchema::get();
529  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
530  auto record = catalog.addNew();
531  record->set(keys.calibrationMean, _calibrationMean);
532  record->set(keys.calibrationErr, _calibrationErr);
533  record->set(keys.isConstant, _isConstant);
534  record->set(keys.field, handle.put(_calibration));
535  record->set(keys.version, SERIALIZATION_VERSION);
536  handle.saveCatalog(catalog);
537 }
538 
539 // ------------------- private/protected helpers -------------------
540 
541 double PhotoCalib::evaluate(lsst::geom::Point<double, 2> const &point) const {
542  if (_isConstant)
543  return _calibrationMean;
544  else
545  return _calibration->evaluate(point);
546 }
547 
548 ndarray::Array<double, 1> PhotoCalib::evaluateArray(ndarray::Array<double, 1> const &xx,
549  ndarray::Array<double, 1> const &yy) const {
550  if (_isConstant) {
551  ndarray::Array<double, 1> result = ndarray::allocate(ndarray::makeVector(xx.size()));
552  result.deep() = _calibrationMean;
553  return result;
554  } else {
555  return _calibration->evaluate(xx, yy);
556  }
557 }
558 
559 ndarray::Array<double, 1> PhotoCalib::evaluateCatalog(afw::table::SourceCatalog const &sourceCatalog) const {
560  ndarray::Array<double, 1> xx = ndarray::allocate(ndarray::makeVector(sourceCatalog.size()));
561  ndarray::Array<double, 1> yy = ndarray::allocate(ndarray::makeVector(sourceCatalog.size()));
562  size_t i = 0;
563  for (auto const &rec : sourceCatalog) {
564  auto point = rec.getCentroid();
565  xx[i] = point.getX();
566  yy[i] = point.getY();
567  ++i;
568  }
569  return evaluateArray(xx, yy);
570 }
571 
572 void PhotoCalib::instFluxToNanojanskyArray(afw::table::SourceCatalog const &sourceCatalog,
573  std::string const &instFluxField,
574  ndarray::Array<double, 2, 2> result) const {
575  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
576  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
577 
578  auto calibration = evaluateCatalog(sourceCatalog);
579  int i = 0;
580  auto iter = result.begin();
581  for (auto const &rec : sourceCatalog) {
582  double instFlux = rec.get(instFluxKey);
583  double instFluxErr = rec.get(instFluxErrKey);
584  double nanojansky = toNanojansky(instFlux, calibration[i]);
585  (*iter)[0] = nanojansky;
586  (*iter)[1] = toNanojanskyErr(instFlux, instFluxErr, calibration[i], _calibrationErr, nanojansky);
587  ++iter;
588  ++i;
589  }
590 }
591 
592 void PhotoCalib::instFluxToMagnitudeArray(afw::table::SourceCatalog const &sourceCatalog,
593  std::string const &instFluxField,
594  ndarray::Array<double, 2, 2> result) const {
595  auto instFluxKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFlux").key;
596  auto instFluxErrKey = sourceCatalog.getSchema().find<double>(instFluxField + "_instFluxErr").key;
597 
598  auto calibration = evaluateCatalog(sourceCatalog);
599  auto iter = result.begin();
600  int i = 0;
601  for (auto const &rec : sourceCatalog) {
602  double instFlux = rec.get(instFluxKey);
603  double instFluxErr = rec.get(instFluxErrKey);
604  (*iter)[0] = toMagnitude(instFlux, calibration[i]);
605  (*iter)[1] = toMagnitudeErr(instFlux, instFluxErr, calibration[i], _calibrationErr);
606  ++iter;
607  ++i;
608  }
609 }
610 
612  auto key = "FLUXMAG0";
613  if (metadata.exists(key)) {
614  double instFluxMag0 = metadata.getAsDouble(key);
615  if (strip) metadata.remove(key);
616 
617  double instFluxMag0Err = 0.0;
618  key = "FLUXMAG0ERR";
619  if (metadata.exists(key)) {
620  instFluxMag0Err = metadata.getAsDouble(key);
621  if (strip) metadata.remove(key);
622  }
623  return makePhotoCalibFromCalibZeroPoint(instFluxMag0, instFluxMag0Err);
624  } else {
625  return nullptr;
626  }
627 }
628 
629 std::shared_ptr<PhotoCalib> makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err) {
630  double calibration = utils::referenceFlux / instFluxMag0;
631  double calibrationErr = utils::referenceFlux * instFluxMag0Err / std::pow(instFluxMag0, 2);
632  return std::make_shared<PhotoCalib>(calibration, calibrationErr);
633 }
634 
635 } // namespace image
636 } // namespace afw
637 } // namespace lsst
double getAsDouble(std::string const &name) const
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage&#39;s variance.
Definition: MaskedImage.h:1091
std::shared_ptr< PhotoCalib > makePhotoCalibFromMetadata(daf::base::PropertySet &metadata, bool strip=false)
Construct a PhotoCalib from FITS FLUXMAG0/FLUXMAG0ERR keywords.
Definition: PhotoCalib.cc:611
table::Key< double > calibrationErr
Definition: PhotoCalib.cc:384
std::shared_ptr< PhotoCalib > makePhotoCalibFromCalibZeroPoint(double instFluxMag0, double instFluxMag0Err)
Construct a PhotoCalib from the deprecated Calib-style instFluxMag0/instFluxMag0Err values...
Definition: PhotoCalib.cc:629
def keys(self)
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.
Definition: PhotoCalib.h:115
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
table::Key< table::Flag > isConstant
Definition: PhotoCalib.cc:385
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
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.
Definition: SchemaMapper.h:34
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:109
T to_string(T... args)
Schema const getOutputSchema() const
Return the output schema (copy-on-write).
Definition: SchemaMapper.h:27
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18
A value and its error.
Definition: PhotoCalib.h:52
bool strip
Definition: fits.cc:883
table::Key< double > fluxMag0Err
Definition: PhotoCalib.cc:468
#define PTR(...)
virtual void remove(std::string const &name)
ItemVariant const * other
Definition: Schema.cc:56
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
table::Key< double > expTime
Definition: PhotoCalib.cc:466
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
Definition: BaseRecord.h:151
table::Key< double > calibrationMean
Definition: PhotoCalib.cc:383
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:1058
STL class.
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition: Schema.cc:656
A base class for image defects.
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:74
A description of a field in a table.
Definition: Field.h:24
log
Key< U > key
Definition: Schema.cc:281
T str(T... args)
T make_pair(T... args)
Schema getSchema() const
Return the Schema that holds this record&#39;s fields and keys.
Definition: BaseRecord.h:80
bool exists(std::string const &name) const
table::Key< double > fluxMag0
Definition: PhotoCalib.cc:467
table::Key< std::int64_t > midTime
Definition: PhotoCalib.cc:465
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:195
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
table::Schema schema
Definition: PhotoCalib.cc:382
std::ostream & operator<<(std::ostream &os, PhotoCalib const &photoCalib)
Definition: PhotoCalib.cc:266
T find(T... args)
T size(T... args)
#define LSST_EXCEPT(type,...)
double nanojanskyToABMagnitude(double flux)
void addMinimalSchema(Schema const &minimal, bool doMap=true)
Add the given minimal schema to the output schema.
T pow(T... args)
Record class that contains measurements made on a single exposure.
Definition: Source.h:82
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
std::ostream * os
Definition: Schema.cc:746
const double referenceFlux
STL class.
double ABMagnitudeToNanojansky(double magnitude)
Implementation of the Photometric Calibration class.
T reserve(T... args)
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
Definition: Source.h:656
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:485
table::Key< int > version
Definition: PhotoCalib.cc:387
T emplace_back(T... args)
table::Key< int > field
Definition: PhotoCalib.cc:386