lsst.afw  14.0-42-g99f9a3040+1
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"
28 #include "lsst/afw/image/Wcs.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<Wcs> ExposureInfo::_cloneWcs(std::shared_ptr<Wcs const> wcs) {
66  if (wcs) return wcs->clone();
67  return std::shared_ptr<Wcs>();
68 }
69 
70 std::shared_ptr<ApCorrMap> ExposureInfo::_cloneApCorrMap(std::shared_ptr<ApCorrMap const> apCorrMap) {
71  if (apCorrMap) {
72  return std::make_shared<ApCorrMap>(*apCorrMap);
73  }
75 }
76 
79  std::shared_ptr<Calib const> const& calib,
84  std::shared_ptr<ApCorrMap> const& apCorrMap,
87  : _wcs(_cloneWcs(wcs)),
88  _psf(std::const_pointer_cast<detection::Psf>(psf)),
89  _calib(calib ? _cloneCalib(calib) : std::shared_ptr<Calib>(new Calib())),
90  _detector(detector),
91  _validPolygon(polygon),
92  _filter(filter),
93  _metadata(metadata ? metadata
94  : std::shared_ptr<daf::base::PropertySet>(new daf::base::PropertyList())),
95  _coaddInputs(coaddInputs),
96  _apCorrMap(_cloneApCorrMap(apCorrMap)),
97  _visitInfo(visitInfo),
98  _transmissionCurve(transmissionCurve)
99 {}
100 
102  : _wcs(_cloneWcs(other._wcs)),
103  _psf(other._psf),
104  _calib(_cloneCalib(other._calib)),
105  _detector(other._detector),
106  _validPolygon(other._validPolygon),
107  _filter(other._filter),
108  _metadata(other._metadata),
109  _coaddInputs(other._coaddInputs),
110  _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
111  _visitInfo(other._visitInfo),
112  _transmissionCurve(other._transmissionCurve) {}
113 
114 // Delegate to copy-constructor for backwards compatibility
116 
117 ExposureInfo::ExposureInfo(ExposureInfo const& other, bool copyMetadata)
118  : _wcs(_cloneWcs(other._wcs)),
119  _psf(other._psf),
120  _calib(_cloneCalib(other._calib)),
121  _detector(other._detector),
122  _validPolygon(other._validPolygon),
123  _filter(other._filter),
124  _metadata(other._metadata),
125  _coaddInputs(other._coaddInputs),
126  _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
127  _visitInfo(other._visitInfo),
128  _transmissionCurve(other._transmissionCurve) {
129  if (copyMetadata) _metadata = _metadata->deepCopy();
130 }
131 
133  if (&other != this) {
134  _wcs = _cloneWcs(other._wcs);
135  _psf = other._psf;
136  _calib = _cloneCalib(other._calib);
137  _detector = other._detector;
138  _validPolygon = other._validPolygon;
139  _filter = other._filter;
140  _metadata = other._metadata;
141  _coaddInputs = other._coaddInputs;
142  _apCorrMap = _cloneApCorrMap(other._apCorrMap);
143  _visitInfo = other._visitInfo;
144  _transmissionCurve = other._transmissionCurve;
145  }
146  return *this;
147 }
148 // Delegate to copy-assignment for backwards compatibility
149 ExposureInfo& ExposureInfo::operator=(ExposureInfo&& other) { return *this = other; }
150 
151 void ExposureInfo::initApCorrMap() { _apCorrMap = std::make_shared<ApCorrMap>(); }
152 
153 ExposureInfo::~ExposureInfo() = default;
154 
155 ExposureInfo::FitsWriteData ExposureInfo::_startWriteFits(afw::geom::Point2I const& xy0) const {
156  FitsWriteData data;
157 
158  data.metadata.reset(new daf::base::PropertyList());
159  data.imageMetadata.reset(new daf::base::PropertyList());
160  data.maskMetadata = data.imageMetadata;
161  data.varianceMetadata = data.imageMetadata;
162 
163  data.metadata->combine(getMetadata());
164 
165  // In the future, we might not have exactly three image HDUs, but we always do right now,
166  // so 0=primary, 1=image, 2=mask, 3=variance, 4+=archive
167  //
168  // Historically the AR_HDU keyword was 1-indexed (see RFC-304), and to maintain file compatibility
169  // this is still the case so we're setting AR_HDU to 5 == 4 + 1
170  //
171  data.metadata->set("AR_HDU", 5, "HDU (1-indexed) containing the archive used to store ancillary objects");
172  if (hasCoaddInputs()) {
173  int coaddInputsId = data.archive.put(getCoaddInputs());
174  data.metadata->set("COADD_INPUTS_ID", coaddInputsId, "archive ID for coadd inputs catalogs");
175  }
176  if (hasApCorrMap()) {
177  int apCorrMapId = data.archive.put(getApCorrMap());
178  data.metadata->set("AP_CORR_MAP_ID", apCorrMapId, "archive ID for aperture correction map");
179  }
180  if (hasPsf() && getPsf()->isPersistable()) {
181  int psfId = data.archive.put(getPsf());
182  data.metadata->set("PSF_ID", psfId, "archive ID for the Exposure's main Psf");
183  }
184  if (hasWcs() && getWcs()->isPersistable()) {
185  int wcsId = data.archive.put(getWcs());
186  data.metadata->set("WCS_ID", wcsId, "archive ID for the Exposure's main Wcs");
187  }
188  if (hasValidPolygon() && getValidPolygon()->isPersistable()) {
189  int polygonId = data.archive.put(getValidPolygon());
190  data.metadata->set("VALID_POLYGON_ID", polygonId, "archive ID for the Exposure's valid polygon");
191  }
192  if (hasTransmissionCurve() && getTransmissionCurve()->isPersistable()) {
193  int transmissionCurveId = data.archive.put(getTransmissionCurve());
194  data.metadata->set("TRANSMISSION_CURVE_ID", transmissionCurveId,
195  "archive ID for the Exposure's transmission curve");
196  }
197 
198  // LSST convention is that Wcs is in pixel coordinates (i.e relative to bottom left
199  // corner of parent image, if any). The Wcs/Fits convention is that the Wcs is in
200  // image coordinates. When saving an image we convert from pixel to index coordinates.
201  // In the case where this image is a parent image, the reference pixels are unchanged
202  // by this transformation
203  if (hasWcs()) {
204  std::shared_ptr<Wcs> newWcs = getWcs()->clone(); // Create a copy
205  newWcs->shiftReferencePixel(-xy0.getX(), -xy0.getY());
206 
207  // We want the WCS to appear in all HDUs
208  data.imageMetadata->combine(newWcs->getFitsMetadata());
209  }
210 
211  // Store _x0 and _y0. If this exposure is a portion of a larger image, _x0 and _y0
212  // indicate the origin (the position of the bottom left corner) of the sub-image with
213  // respect to the origin of the parent image.
214  // This is stored in the fits header using the LTV convention used by STScI
215  //(see \S2.6.2 of HST Data Handbook for STIS, version 5.0
216  // http://www.stsci.edu/hst/stis/documents/handbooks/currentDHB/ch2_stis_data7.html#429287).
217  // This is not a fits standard keyword, but is recognised by ds9
218  // LTV keywords use the opposite convention to the LSST, in that they represent
219  // the position of the origin of the parent image relative to the origin of the sub-image.
220  // _x0, _y0 >= 0, while LTV1 and LTV2 <= 0
221 
222  data.imageMetadata->set("LTV1", static_cast<double>(-xy0.getX()));
223  data.imageMetadata->set("LTV2", static_cast<double>(-xy0.getY()));
224 
225  data.metadata->set("FILTER", getFilter().getName());
226  if (hasDetector()) {
227  data.metadata->set("DETNAME", getDetector()->getName());
228  data.metadata->set("DETSER", getDetector()->getSerial());
229  }
230 
231  auto visitInfoPtr = getVisitInfo();
232  if (visitInfoPtr) {
233  detail::setVisitInfoMetadata(*(data.metadata), *visitInfoPtr);
234  }
235 
236  /*
237  * We need to define these keywords properly! XXX
238  */
239  data.metadata->set("FLUXMAG0", getCalib()->getFluxMag0().first);
240  data.metadata->set("FLUXMAG0ERR", getCalib()->getFluxMag0().second);
241 
242  return data;
243 }
244 
245 void ExposureInfo::_finishWriteFits(fits::Fits& fitsfile, FitsWriteData const& data) const {
246  data.archive.writeFits(fitsfile);
247 }
248 
249 void ExposureInfo::_readFits(fits::Fits& fitsfile, std::shared_ptr<daf::base::PropertySet> metadata,
251  // true: strip keywords that are related to the created WCS from the input metadata
252  _wcs = makeWcs(imageMetadata, true);
253 
254  if (!imageMetadata->exists("INHERIT")) {
255  // New-style exposures put everything but the Wcs in the primary HDU, use
256  // INHERIT keyword in the others. For backwards compatibility, if we don't
257  // find the INHERIT keyword, we ignore the primary HDU metadata and expect
258  // everything to be in the image HDU metadata. Note that we can't merge them,
259  // because they're probably duplicates.
260  metadata = imageMetadata;
261  }
262 
263  _filter = Filter(metadata, true);
264  detail::stripFilterKeywords(metadata);
265 
266  _visitInfo = std::shared_ptr<VisitInfo const>(new VisitInfo(*metadata));
268 
269  std::shared_ptr<Calib> newCalib(new Calib(metadata));
270  setCalib(newCalib);
271  detail::stripCalibKeywords(metadata);
272 
273  int archiveHdu = popInt(*metadata, "AR_HDU");
274 
275  if (archiveHdu) {
276  --archiveHdu; // see note above in _startWriteFits; AR_HDU is *one* indexed
277 
278  fitsfile.setHdu(archiveHdu);
280  // Load the Psf and Wcs from the archive; id=0 results in a null pointer.
281  // Note that the binary table Wcs, if present, clobbers the FITS header one,
282  // because the former might be an approximation to something we can't represent
283  // using the FITS WCS standard but can represent with binary tables.
284  int psfId = popInt(*metadata, "PSF_ID");
285  try {
286  _psf = archive.get<detection::Psf>(psfId);
287  } catch (pex::exceptions::NotFoundError& err) {
288  LOGLS_WARN(_log, "Could not read PSF; setting to null: " << err.what());
289  }
290  int wcsId = popInt(*metadata, "WCS_ID");
291  try {
292  auto archiveWcs = archive.get<Wcs>(wcsId);
293  if (archiveWcs) {
294  _wcs = archiveWcs;
295  } else {
296  LOGLS_DEBUG(_log, "Null WCS seen in binary table");
297  }
298  } catch (pex::exceptions::NotFoundError& err) {
299  auto msg = str(boost::format("Could not read WCS extension; setting to null: %s") % err.what());
300  if (_wcs) {
301  msg += " ; using WCS from FITS header";
302  }
303  LOGLS_WARN(_log, msg);
304  }
305  int coaddInputsId = popInt(*metadata, "COADD_INPUTS_ID");
306  try {
307  _coaddInputs = archive.get<CoaddInputs>(coaddInputsId);
308  } catch (pex::exceptions::NotFoundError& err) {
309  LOGLS_WARN(_log, "Could not read CoaddInputs; setting to null: " << err.what());
310  }
311  int apCorrMapId = popInt(*metadata, "AP_CORR_MAP_ID");
312  try {
313  _apCorrMap = archive.get<ApCorrMap>(apCorrMapId);
314  } catch (pex::exceptions::NotFoundError& err) {
315  LOGLS_WARN(_log, "Could not read ApCorrMap; setting to null: " << err.what());
316  }
317  int validPolygonId = popInt(*metadata, "VALID_POLYGON_ID");
318  try {
319  _validPolygon = archive.get<geom::polygon::Polygon>(validPolygonId);
320  } catch (pex::exceptions::NotFoundError& err) {
321  LOGLS_WARN(_log, "Could not read ValidPolygon; setting to null: " << err.what());
322  }
323  int transmissionCurveId = popInt(*metadata, "TRANSMISSION_CURVE_ID");
324  try {
325  _transmissionCurve = archive.get<TransmissionCurve>(transmissionCurveId);
326  } catch (pex::exceptions::NotFoundError & err) {
327  LOGLS_WARN(_log, "Could not read TransmissionCurve; setting to null: " << err.what());
328  }
329  }
330 
331  _metadata = metadata;
332 }
333 }
334 }
335 } // namespace lsst::afw::image
#define LOGLS_WARN(logger, message)
std::shared_ptr< Wcs > getWcs()
Return the coordinate system of the exposure.
Definition: ExposureInfo.h:93
bool hasTransmissionCurve() const
Does this exposure have a transmission curve?
Definition: ExposureInfo.h:195
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:126
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< Wcs > makeWcs(std::shared_ptr< lsst::daf::base::PropertySet > const &fitsMetadata, bool stripMetadata=false)
Create a Wcs object from a fits header.
Definition: makeWcs.cc:37
std::shared_ptr< image::VisitInfo const > getVisitInfo() const
Return the exposure&#39;s visit info.
Definition: ExposureInfo.h:186
bool hasPsf() const
Does this exposure have a Psf?
Definition: ExposureInfo.h:135
STL namespace.
std::shared_ptr< ApCorrMap > getApCorrMap()
Return the exposure&#39;s aperture correction map (null pointer if !hasApCorrMap())
Definition: ExposureInfo.h:160
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:102
Implementation of the WCS standard for a any projection.
Definition: Wcs.h:104
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:280
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.
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:55
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:157
#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:138
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:148
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:151
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:129
virtual char const * what(void) const
ExposureInfo(std::shared_ptr< Wcs const > const &wcs=std::shared_ptr< Wcs 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:77
std::shared_ptr< cameraGeom::Detector const > getDetector() const
Return the exposure&#39;s Detector information.
Definition: ExposureInfo.h:105
std::shared_ptr< Calib > getCalib()
Return the exposure&#39;s photometric calibration.
Definition: ExposureInfo.h:120
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:183
Filter getFilter() const
Return the exposure&#39;s filter.
Definition: ExposureInfo.h:111
bool hasCoaddInputs() const
Does this exposure have coadd provenance catalogs?
Definition: ExposureInfo.h:177
std::shared_ptr< TransmissionCurve const > getTransmissionCurve() const
Return the exposure&#39;s transmission curve.
Definition: ExposureInfo.h:198