lsst.afw  14.0-56-gfbf220870
ExposureInfo.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*- // fixed format comment for emacs
2 /*
3  * LSST Data Management System
4  * Copyright 2008, 2009, 2010 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #include "lsst/pex/exceptions.h"
25 #include "lsst/log/Log.h"
27 #include "lsst/afw/image/Calib.h"
29 #include "lsst/afw/geom/SkyWcs.h"
31 #include "lsst/afw/detection/Psf.h"
34 #include "lsst/afw/fits.h"
35 
36 namespace {
37 LOG_LOGGER _log = LOG_GET("afw.image.ExposureInfo");
38 }
39 
40 namespace lsst {
41 namespace afw {
42 namespace image {
43 
44 namespace {
45 
46 // Return an int value from a PropertySet if it exists and remove it, or return 0.
47 int popInt(daf::base::PropertySet& metadata, std::string const& name) {
48  int r = 0;
49  if (metadata.exists(name)) {
50  r = metadata.get<int>(name);
51  metadata.remove(name);
52  }
53  return r;
54 }
55 
56 } // anonymous
57 
58 // Clone various components; defined here so that we don't have to expose their insides in Exposure.h
59 
61  if (calib) return std::shared_ptr<Calib>(new Calib(*calib));
62  return std::shared_ptr<Calib>();
63 }
64 
65 std::shared_ptr<ApCorrMap> ExposureInfo::_cloneApCorrMap(std::shared_ptr<ApCorrMap const> apCorrMap) {
66  if (apCorrMap) {
67  return std::make_shared<ApCorrMap>(*apCorrMap);
68  }
70 }
71 
74  std::shared_ptr<Calib const> const& calib,
79  std::shared_ptr<ApCorrMap> const& apCorrMap,
82  : _wcs(wcs),
83  _psf(std::const_pointer_cast<detection::Psf>(psf)),
84  _calib(calib ? _cloneCalib(calib) : std::shared_ptr<Calib>(new Calib())),
85  _detector(detector),
86  _validPolygon(polygon),
87  _filter(filter),
88  _metadata(metadata ? metadata
89  : std::shared_ptr<daf::base::PropertySet>(new daf::base::PropertyList())),
90  _coaddInputs(coaddInputs),
91  _apCorrMap(_cloneApCorrMap(apCorrMap)),
92  _visitInfo(visitInfo),
93  _transmissionCurve(transmissionCurve)
94 {}
95 
97  : _wcs(other._wcs),
98  _psf(other._psf),
99  _calib(_cloneCalib(other._calib)),
100  _detector(other._detector),
101  _validPolygon(other._validPolygon),
102  _filter(other._filter),
103  _metadata(other._metadata),
104  _coaddInputs(other._coaddInputs),
105  _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
106  _visitInfo(other._visitInfo),
107  _transmissionCurve(other._transmissionCurve) {}
108 
109 // Delegate to copy-constructor for backwards compatibility
111 
112 ExposureInfo::ExposureInfo(ExposureInfo const& other, bool copyMetadata)
113  : _wcs(other._wcs),
114  _psf(other._psf),
115  _calib(_cloneCalib(other._calib)),
116  _detector(other._detector),
117  _validPolygon(other._validPolygon),
118  _filter(other._filter),
119  _metadata(other._metadata),
120  _coaddInputs(other._coaddInputs),
121  _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
122  _visitInfo(other._visitInfo),
123  _transmissionCurve(other._transmissionCurve) {
124  if (copyMetadata) _metadata = _metadata->deepCopy();
125 }
126 
128  if (&other != this) {
129  _wcs = other._wcs;
130  _psf = other._psf;
131  _calib = _cloneCalib(other._calib);
132  _detector = other._detector;
133  _validPolygon = other._validPolygon;
134  _filter = other._filter;
135  _metadata = other._metadata;
136  _coaddInputs = other._coaddInputs;
137  _apCorrMap = _cloneApCorrMap(other._apCorrMap);
138  _visitInfo = other._visitInfo;
139  _transmissionCurve = other._transmissionCurve;
140  }
141  return *this;
142 }
143 // Delegate to copy-assignment for backwards compatibility
144 ExposureInfo& ExposureInfo::operator=(ExposureInfo&& other) { return *this = other; }
145 
146 void ExposureInfo::initApCorrMap() { _apCorrMap = std::make_shared<ApCorrMap>(); }
147 
148 ExposureInfo::~ExposureInfo() = default;
149 
150 ExposureInfo::FitsWriteData ExposureInfo::_startWriteFits(afw::geom::Point2I const& xy0) const {
151  FitsWriteData data;
152 
153  data.metadata.reset(new daf::base::PropertyList());
154  data.imageMetadata.reset(new daf::base::PropertyList());
155  data.maskMetadata = data.imageMetadata;
156  data.varianceMetadata = data.imageMetadata;
157 
158  data.metadata->combine(getMetadata());
159 
160  // In the future, we might not have exactly three image HDUs, but we always do right now,
161  // so 0=primary, 1=image, 2=mask, 3=variance, 4+=archive
162  //
163  // Historically the AR_HDU keyword was 1-indexed (see RFC-304), and to maintain file compatibility
164  // this is still the case so we're setting AR_HDU to 5 == 4 + 1
165  //
166  data.metadata->set("AR_HDU", 5, "HDU (1-indexed) containing the archive used to store ancillary objects");
167  if (hasCoaddInputs()) {
168  int coaddInputsId = data.archive.put(getCoaddInputs());
169  data.metadata->set("COADD_INPUTS_ID", coaddInputsId, "archive ID for coadd inputs catalogs");
170  }
171  if (hasApCorrMap()) {
172  int apCorrMapId = data.archive.put(getApCorrMap());
173  data.metadata->set("AP_CORR_MAP_ID", apCorrMapId, "archive ID for aperture correction map");
174  }
175  if (hasPsf() && getPsf()->isPersistable()) {
176  int psfId = data.archive.put(getPsf());
177  data.metadata->set("PSF_ID", psfId, "archive ID for the Exposure's main Psf");
178  }
179  if (hasWcs() && getWcs()->isPersistable()) {
180  int wcsId = data.archive.put(getWcs());
181  data.metadata->set("SKYWCS_ID", wcsId, "archive ID for the Exposure's main Wcs");
182  }
183  if (hasValidPolygon() && getValidPolygon()->isPersistable()) {
184  int polygonId = data.archive.put(getValidPolygon());
185  data.metadata->set("VALID_POLYGON_ID", polygonId, "archive ID for the Exposure's valid polygon");
186  }
187  if (hasTransmissionCurve() && getTransmissionCurve()->isPersistable()) {
188  int transmissionCurveId = data.archive.put(getTransmissionCurve());
189  data.metadata->set("TRANSMISSION_CURVE_ID", transmissionCurveId,
190  "archive ID for the Exposure's transmission curve");
191  }
192 
193  // LSST convention is that Wcs is in pixel coordinates (i.e relative to bottom left
194  // corner of parent image, if any). The Wcs/Fits convention is that the Wcs is in
195  // image coordinates. When saving an image we convert from pixel to index coordinates.
196  // In the case where this image is a parent image, the reference pixels are unchanged
197  // by this transformation
198  if (hasWcs()) {
199  // Try to save the WCS as FITS-WCS metadata; if an exact representation
200  // is not possible then skip it
201  auto shift = geom::Extent2D(geom::Point2I(0, 0) - xy0);
202  auto newWcs = getWcs()->copyAtShiftedPixelOrigin(shift);
204  try {
205  wcsMetadata = newWcs->getFitsMetadata(true);
207  // cannot represent this WCS as FITS-WCS; don't write its metadata
208  }
209  if (wcsMetadata) {
210  data.imageMetadata->combine(newWcs->getFitsMetadata(true));
211  }
212  }
213 
214  // For the sake of ds9, store _x0 and _y0 as -LTV1, -LTV2.
215  // This is in addition to saving _x0 and _y0 as WCS A, which is done elsewhere
216  // and is what LSST uses to read _x0 and _y0.
217  // LTV is a convention used by STScI (see \S2.6.2 of HST Data Handbook for STIS, version 5.0
218  // http://www.stsci.edu/hst/stis/documents/handbooks/currentDHB/ch2_stis_data7.html#429287)
219  // and recognized by ds9.
220  data.imageMetadata->set("LTV1", static_cast<double>(-xy0.getX()));
221  data.imageMetadata->set("LTV2", static_cast<double>(-xy0.getY()));
222 
223  data.metadata->set("FILTER", getFilter().getName());
224  if (hasDetector()) {
225  data.metadata->set("DETNAME", getDetector()->getName());
226  data.metadata->set("DETSER", getDetector()->getSerial());
227  }
228 
229  auto visitInfoPtr = getVisitInfo();
230  if (visitInfoPtr) {
231  detail::setVisitInfoMetadata(*(data.metadata), *visitInfoPtr);
232  }
233 
234  /*
235  * We need to define these keywords properly! XXX
236  */
237  data.metadata->set("FLUXMAG0", getCalib()->getFluxMag0().first);
238  data.metadata->set("FLUXMAG0ERR", getCalib()->getFluxMag0().second);
239 
240  return data;
241 }
242 
243 void ExposureInfo::_finishWriteFits(fits::Fits& fitsfile, FitsWriteData const& data) const {
244  data.archive.writeFits(fitsfile);
245 }
246 
247 void ExposureInfo::_readFits(fits::Fits& fitsfile, std::shared_ptr<daf::base::PropertySet> metadata,
249  // Try to read WCS from image metadata, and if found, strip the keywords used
250  try {
251  _wcs = geom::makeSkyWcs(*imageMetadata, true);
253  LOGLS_DEBUG(_log, "No WCS found in FITS metadata");
254  }
255 
256  // Strip LTV1, LTV2 from imageMetadata, because we don't use it internally
257  imageMetadata->remove("LTV1");
258  imageMetadata->remove("LTV2");
259 
260  if (!imageMetadata->exists("INHERIT")) {
261  // New-style exposures put everything but the Wcs in the primary HDU, use
262  // INHERIT keyword in the others. For backwards compatibility, if we don't
263  // find the INHERIT keyword, we ignore the primary HDU metadata and expect
264  // everything to be in the image HDU metadata. Note that we can't merge them,
265  // because they're probably duplicates.
266  metadata = imageMetadata;
267  }
268 
269  _filter = Filter(metadata, true);
270  detail::stripFilterKeywords(metadata);
271 
272  _visitInfo = std::shared_ptr<VisitInfo const>(new VisitInfo(*metadata));
274 
275  std::shared_ptr<Calib> newCalib(new Calib(metadata));
276  setCalib(newCalib);
277  detail::stripCalibKeywords(metadata);
278 
279  int archiveHdu = popInt(*metadata, "AR_HDU");
280 
281  if (archiveHdu) {
282  --archiveHdu; // see note above in _startWriteFits; AR_HDU is *one* indexed
283 
284  fitsfile.setHdu(archiveHdu);
286  // Load the Psf and Wcs from the archive; id=0 results in a null pointer.
287  // Note that the binary table Wcs, if present, clobbers the FITS header one,
288  // because the former might be an approximation to something we can't represent
289  // using the FITS WCS standard but can represent with binary tables.
290  int psfId = popInt(*metadata, "PSF_ID");
291  try {
292  _psf = archive.get<detection::Psf>(psfId);
293  } catch (pex::exceptions::NotFoundError& err) {
294  LOGLS_WARN(_log, "Could not read PSF; setting to null: " << err.what());
295  }
296  int wcsId = popInt(*metadata, "SKYWCS_ID");
297  try {
298  auto archiveWcs = archive.get<geom::SkyWcs>(wcsId);
299  if (archiveWcs) {
300  _wcs = archiveWcs;
301  } else {
302  LOGLS_DEBUG(_log, "Null WCS seen in binary table");
303  }
304  } catch (pex::exceptions::NotFoundError& err) {
305  auto msg = str(boost::format("Could not read WCS extension; setting to null: %s") % err.what());
306  if (_wcs) {
307  msg += " ; using WCS from FITS header";
308  }
309  LOGLS_WARN(_log, msg);
310  }
311  int coaddInputsId = popInt(*metadata, "COADD_INPUTS_ID");
312  try {
313  _coaddInputs = archive.get<CoaddInputs>(coaddInputsId);
314  } catch (pex::exceptions::NotFoundError& err) {
315  LOGLS_WARN(_log, "Could not read CoaddInputs; setting to null: " << err.what());
316  }
317  int apCorrMapId = popInt(*metadata, "AP_CORR_MAP_ID");
318  try {
319  _apCorrMap = archive.get<ApCorrMap>(apCorrMapId);
320  } catch (pex::exceptions::NotFoundError& err) {
321  LOGLS_WARN(_log, "Could not read ApCorrMap; setting to null: " << err.what());
322  }
323  int validPolygonId = popInt(*metadata, "VALID_POLYGON_ID");
324  try {
325  _validPolygon = archive.get<geom::polygon::Polygon>(validPolygonId);
326  } catch (pex::exceptions::NotFoundError& err) {
327  LOGLS_WARN(_log, "Could not read ValidPolygon; setting to null: " << err.what());
328  }
329  int transmissionCurveId = popInt(*metadata, "TRANSMISSION_CURVE_ID");
330  try {
331  _transmissionCurve = archive.get<TransmissionCurve>(transmissionCurveId);
332  } catch (pex::exceptions::NotFoundError & err) {
333  LOGLS_WARN(_log, "Could not read TransmissionCurve; setting to null: " << err.what());
334  }
335  }
336 
337  _metadata = metadata;
338 }
339 }
340 }
341 } // namespace lsst::afw::image
#define LOGLS_WARN(logger, message)
bool hasTransmissionCurve() const
Does this exposure have a transmission curve?
Definition: ExposureInfo.h:192
A 2-dimensional celestial WCS that transform pixels to ICRS RA/Dec, using the LSST standard for pixel...
Definition: SkyWcs.h:114
A spatially-varying transmission curve as a function of wavelength.
std::shared_ptr< Persistable > get(int id) const
Load the Persistable with the given ID and return it.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
void setCalib(std::shared_ptr< Calib const > calib)
Set the Exposure&#39;s Calib object.
Definition: ExposureInfo.h:123
Extent< double, 2 > Extent2D
Definition: Extent.h:383
A thin wrapper around std::map to allow aperture corrections to be attached to Exposures.
Definition: ApCorrMap.h:44
static InputArchive readFits(fits::Fits &fitsfile)
Read an object from an already open FITS object.
Information about a single exposure of an imaging camera.
Definition: VisitInfo.h:67
std::shared_ptr< image::VisitInfo const > getVisitInfo() const
Return the exposure&#39;s visit info.
Definition: ExposureInfo.h:183
bool hasPsf() const
Does this exposure have a Psf?
Definition: ExposureInfo.h:132
STL namespace.
std::shared_ptr< ApCorrMap > getApCorrMap()
Return the exposure&#39;s aperture correction map (null pointer if !hasApCorrMap())
Definition: ExposureInfo.h:157
int stripFilterKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Filter-related keywords from the metadata.
Definition: Filter.cc:123
bool hasDetector() const
Does this exposure have Detector information?
Definition: ExposureInfo.h:99
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:298
ExposureInfo(std::shared_ptr< geom::SkyWcs const > const &wcs=std::shared_ptr< geom::SkyWcs const >(), std::shared_ptr< detection::Psf const > const &psf=std::shared_ptr< detection::Psf const >(), std::shared_ptr< Calib const > const &calib=std::shared_ptr< Calib const >(), std::shared_ptr< cameraGeom::Detector const > const &detector=std::shared_ptr< cameraGeom::Detector const >(), std::shared_ptr< geom::polygon::Polygon const > const &polygon=std::shared_ptr< geom::polygon::Polygon const >(), Filter const &filter=Filter(), std::shared_ptr< daf::base::PropertySet > const &metadata=std::shared_ptr< daf::base::PropertySet >(), std::shared_ptr< CoaddInputs > const &coaddInputs=std::shared_ptr< CoaddInputs >(), std::shared_ptr< ApCorrMap > const &apCorrMap=std::shared_ptr< ApCorrMap >(), std::shared_ptr< image::VisitInfo const > const &visitInfo=std::shared_ptr< image::VisitInfo const >(), std::shared_ptr< TransmissionCurve const > const &transmissionCurve=std::shared_ptr< TransmissionCurve >())
Construct an ExposureInfo from its various components.
Definition: ExposureInfo.cc:72
bool hasWcs() const
Does this exposure have a Wcs?
Definition: ExposureInfo.h:90
Describe an exposure&#39;s calibration.
Definition: Calib.h:96
A simple Persistable struct containing ExposureCatalogs that record the inputs to a coadd...
Definition: CoaddInputs.h:48
STL class.
ExposureInfo & operator=(ExposureInfo const &other)
Assignment; deep-copies all components except the metadata.
void setVisitInfoMetadata(daf::base::PropertyList &metadata, VisitInfo const &visitInfo)
Set FITS metadata from a VisitInfo.
Definition: VisitInfo.cc:275
bool hasApCorrMap() const
Return true if the exposure has an aperture correction map.
Definition: ExposureInfo.h:154
#define LOGLS_DEBUG(logger, message)
A base class for image defects.
std::shared_ptr< detection::Psf > getPsf() const
Return the exposure&#39;s point-spread function.
Definition: ExposureInfo.h:135
std::shared_ptr< SkyWcs > makeSkyWcs(daf::base::PropertySet &metadata, bool strip=false)
Construct a SkyWcs from FITS keywords.
void initApCorrMap()
Set the exposure&#39;s aperture correction map to a new, empty map.
bool hasValidPolygon() const
Does this exposure have a valid Polygon.
Definition: ExposureInfo.h:145
int stripCalibKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Calib-related keywords from the metadata.
Definition: Calib.cc:156
std::shared_ptr< geom::polygon::Polygon const > getValidPolygon() const
Return the valid Polygon.
Definition: ExposureInfo.h:148
Holds an integer identifier for an LSST filter.
Definition: Filter.h:141
void setHdu(int hdu, bool relative=false)
Set the current HDU.
std::shared_ptr< daf::base::PropertySet > getMetadata() const
Return flexible metadata.
Definition: ExposureInfo.h:126
virtual char const * what(void) const
std::shared_ptr< cameraGeom::Detector const > getDetector() const
Return the exposure&#39;s Detector information.
Definition: ExposureInfo.h:102
std::shared_ptr< Calib > getCalib()
Return the exposure&#39;s photometric calibration.
Definition: ExposureInfo.h:117
int stripVisitInfoKeywords(daf::base::PropertySet &metadata)
Remove VisitInfo-related keywords from the metadata.
Definition: VisitInfo.cc:259
Cartesian polygons.
Definition: Polygon.h:59
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
#define LOG_GET(logger)
A collection of all the things that make an Exposure different from a MaskedImage.
Definition: ExposureInfo.h:87
A polymorphic base class for representing an image&#39;s Point Spread Function.
Definition: Psf.h:70
std::shared_ptr< CoaddInputs > getCoaddInputs() const
Return a pair of catalogs that record the inputs, if this Exposure is a coadd (otherwise null)...
Definition: ExposureInfo.h:180
std::shared_ptr< geom::SkyWcs const > getWcs() const
Return the WCS of the exposure.
Definition: ExposureInfo.h:93
Filter getFilter() const
Return the exposure&#39;s filter.
Definition: ExposureInfo.h:108
bool hasCoaddInputs() const
Does this exposure have coadd provenance catalogs?
Definition: ExposureInfo.h:174
std::shared_ptr< TransmissionCurve const > getTransmissionCurve() const
Return the exposure&#39;s transmission curve.
Definition: ExposureInfo.h:195