lsst.afw  16.0-43-gaa65a4573+2
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 } // namespace
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 
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 
96  : _wcs(other._wcs),
97  _psf(other._psf),
98  _calib(_cloneCalib(other._calib)),
99  _detector(other._detector),
100  _validPolygon(other._validPolygon),
101  _filter(other._filter),
102  _metadata(other._metadata),
103  _coaddInputs(other._coaddInputs),
104  _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
105  _visitInfo(other._visitInfo),
106  _transmissionCurve(other._transmissionCurve) {}
107 
108 // Delegate to copy-constructor for backwards compatibility
110 
112  : _wcs(other._wcs),
113  _psf(other._psf),
114  _calib(_cloneCalib(other._calib)),
115  _detector(other._detector),
116  _validPolygon(other._validPolygon),
117  _filter(other._filter),
118  _metadata(other._metadata),
119  _coaddInputs(other._coaddInputs),
120  _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
121  _visitInfo(other._visitInfo),
122  _transmissionCurve(other._transmissionCurve) {
123  if (copyMetadata) _metadata = _metadata->deepCopy();
124 }
125 
127  if (&other != this) {
128  _wcs = other._wcs;
129  _psf = other._psf;
130  _calib = _cloneCalib(other._calib);
131  _detector = other._detector;
132  _validPolygon = other._validPolygon;
133  _filter = other._filter;
134  _metadata = other._metadata;
135  _coaddInputs = other._coaddInputs;
136  _apCorrMap = _cloneApCorrMap(other._apCorrMap);
137  _visitInfo = other._visitInfo;
138  _transmissionCurve = other._transmissionCurve;
139  }
140  return *this;
141 }
142 // Delegate to copy-assignment for backwards compatibility
144 
145 void ExposureInfo::initApCorrMap() { _apCorrMap = std::make_shared<ApCorrMap>(); }
146 
147 ExposureInfo::~ExposureInfo() = default;
148 
149 ExposureInfo::FitsWriteData ExposureInfo::_startWriteFits(lsst::geom::Point2I const& xy0) const {
150  FitsWriteData data;
151 
152  data.metadata.reset(new daf::base::PropertyList());
153  data.imageMetadata.reset(new daf::base::PropertyList());
154  data.maskMetadata = data.imageMetadata;
155  data.varianceMetadata = data.imageMetadata;
156 
157  data.metadata->combine(getMetadata());
158 
159  // In the future, we might not have exactly three image HDUs, but we always do right now,
160  // so 0=primary, 1=image, 2=mask, 3=variance, 4+=archive
161  //
162  // Historically the AR_HDU keyword was 1-indexed (see RFC-304), and to maintain file compatibility
163  // this is still the case so we're setting AR_HDU to 5 == 4 + 1
164  //
165  data.metadata->set("AR_HDU", 5, "HDU (1-indexed) containing the archive used to store ancillary objects");
166  if (hasCoaddInputs()) {
167  int coaddInputsId = data.archive.put(getCoaddInputs());
168  data.metadata->set("COADD_INPUTS_ID", coaddInputsId, "archive ID for coadd inputs catalogs");
169  }
170  if (hasApCorrMap()) {
171  int apCorrMapId = data.archive.put(getApCorrMap());
172  data.metadata->set("AP_CORR_MAP_ID", apCorrMapId, "archive ID for aperture correction map");
173  }
174  if (hasPsf() && getPsf()->isPersistable()) {
175  int psfId = data.archive.put(getPsf());
176  data.metadata->set("PSF_ID", psfId, "archive ID for the Exposure's main Psf");
177  }
178  if (hasWcs() && getWcs()->isPersistable()) {
179  int wcsId = data.archive.put(getWcs());
180  data.metadata->set("SKYWCS_ID", wcsId, "archive ID for the Exposure's main Wcs");
181  }
182  if (hasValidPolygon() && getValidPolygon()->isPersistable()) {
183  int polygonId = data.archive.put(getValidPolygon());
184  data.metadata->set("VALID_POLYGON_ID", polygonId, "archive ID for the Exposure's valid polygon");
185  }
186  if (hasTransmissionCurve() && getTransmissionCurve()->isPersistable()) {
187  int transmissionCurveId = data.archive.put(getTransmissionCurve());
188  data.metadata->set("TRANSMISSION_CURVE_ID", transmissionCurveId,
189  "archive ID for the Exposure's transmission curve");
190  }
191 
192  // LSST convention is that Wcs is in pixel coordinates (i.e relative to bottom left
193  // corner of parent image, if any). The Wcs/Fits convention is that the Wcs is in
194  // image coordinates. When saving an image we convert from pixel to index coordinates.
195  // In the case where this image is a parent image, the reference pixels are unchanged
196  // by this transformation
197  if (hasWcs()) {
198  // Try to save the WCS as FITS-WCS metadata; if an exact representation
199  // is not possible then skip it
200  auto shift = lsst::geom::Extent2D(lsst::geom::Point2I(0, 0) - xy0);
201  auto newWcs = getWcs()->copyAtShiftedPixelOrigin(shift);
203  try {
204  wcsMetadata = newWcs->getFitsMetadata(true);
206  // cannot represent this WCS as FITS-WCS; don't write its metadata
207  }
208  if (wcsMetadata) {
209  data.imageMetadata->combine(newWcs->getFitsMetadata(true));
210  }
211  }
212 
213  // For the sake of ds9, store _x0 and _y0 as -LTV1, -LTV2.
214  // This is in addition to saving _x0 and _y0 as WCS A, which is done elsewhere
215  // and is what LSST uses to read _x0 and _y0.
216  // LTV is a convention used by STScI (see \S2.6.2 of HST Data Handbook for STIS, version 5.0
217  // http://www.stsci.edu/hst/stis/documents/handbooks/currentDHB/ch2_stis_data7.html#429287)
218  // and recognized by ds9.
219  data.imageMetadata->set("LTV1", static_cast<double>(-xy0.getX()));
220  data.imageMetadata->set("LTV2", static_cast<double>(-xy0.getY()));
221 
222  data.metadata->set("FILTER", getFilter().getName());
223  if (hasDetector()) {
224  data.metadata->set("DETNAME", getDetector()->getName());
225  data.metadata->set("DETSER", getDetector()->getSerial());
226  }
227 
228  auto visitInfoPtr = getVisitInfo();
229  if (visitInfoPtr) {
230  detail::setVisitInfoMetadata(*(data.metadata), *visitInfoPtr);
231  }
232 
233  /*
234  * We need to define these keywords properly! XXX
235  */
236  data.metadata->set("FLUXMAG0", getCalib()->getFluxMag0().first);
237  data.metadata->set("FLUXMAG0ERR", getCalib()->getFluxMag0().second);
238 
239  return data;
240 }
241 
242 void ExposureInfo::_finishWriteFits(fits::Fits& fitsfile, FitsWriteData const& data) const {
243  data.archive.writeFits(fitsfile);
244 }
245 
246 void ExposureInfo::_readFits(fits::Fits& fitsfile, std::shared_ptr<daf::base::PropertySet> metadata,
248  // Try to read WCS from image metadata, and if found, strip the keywords used
249  try {
250  _wcs = geom::makeSkyWcs(*imageMetadata, true);
252  LOGLS_DEBUG(_log, "No WCS found in FITS metadata");
253  }
254 
255  // Strip LTV1, LTV2 from imageMetadata, because we don't use it internally
256  imageMetadata->remove("LTV1");
257  imageMetadata->remove("LTV2");
258 
259  if (!imageMetadata->exists("INHERIT")) {
260  // New-style exposures put everything but the Wcs in the primary HDU, use
261  // INHERIT keyword in the others. For backwards compatibility, if we don't
262  // find the INHERIT keyword, we ignore the primary HDU metadata and expect
263  // everything to be in the image HDU metadata. Note that we can't merge them,
264  // because they're probably duplicates.
265  metadata = imageMetadata;
266  }
267 
268  _filter = Filter(metadata, true);
269  detail::stripFilterKeywords(metadata);
270 
271  _visitInfo = std::shared_ptr<VisitInfo const>(new VisitInfo(*metadata));
273 
274  std::shared_ptr<Calib> newCalib(new Calib(metadata));
275  setCalib(newCalib);
276  detail::stripCalibKeywords(metadata);
277 
278  int archiveHdu = popInt(*metadata, "AR_HDU");
279 
280  if (archiveHdu) {
281  --archiveHdu; // see note above in _startWriteFits; AR_HDU is *one* indexed
282 
283  fitsfile.setHdu(archiveHdu);
285  // Load the Psf and Wcs from the archive; id=0 results in a null pointer.
286  // Note that the binary table Wcs, if present, clobbers the FITS header one,
287  // because the former might be an approximation to something we can't represent
288  // using the FITS WCS standard but can represent with binary tables.
289  int psfId = popInt(*metadata, "PSF_ID");
290  try {
291  _psf = archive.get<detection::Psf>(psfId);
292  } catch (pex::exceptions::NotFoundError& err) {
293  LOGLS_WARN(_log, "Could not read PSF; setting to null: " << err.what());
294  }
295  int wcsId = popInt(*metadata, "SKYWCS_ID");
296  try {
297  auto archiveWcs = archive.get<geom::SkyWcs>(wcsId);
298  if (archiveWcs) {
299  _wcs = archiveWcs;
300  } else {
301  LOGLS_DEBUG(_log, "Null WCS seen in binary table");
302  }
303  } catch (pex::exceptions::NotFoundError& err) {
304  auto msg = str(boost::format("Could not read WCS extension; setting to null: %s") % err.what());
305  if (_wcs) {
306  msg += " ; using WCS from FITS header";
307  }
308  LOGLS_WARN(_log, msg);
309  }
310  int coaddInputsId = popInt(*metadata, "COADD_INPUTS_ID");
311  try {
312  _coaddInputs = archive.get<CoaddInputs>(coaddInputsId);
313  } catch (pex::exceptions::NotFoundError& err) {
314  LOGLS_WARN(_log, "Could not read CoaddInputs; setting to null: " << err.what());
315  }
316  int apCorrMapId = popInt(*metadata, "AP_CORR_MAP_ID");
317  try {
318  _apCorrMap = archive.get<ApCorrMap>(apCorrMapId);
319  } catch (pex::exceptions::NotFoundError& err) {
320  LOGLS_WARN(_log, "Could not read ApCorrMap; setting to null: " << err.what());
321  }
322  int validPolygonId = popInt(*metadata, "VALID_POLYGON_ID");
323  try {
324  _validPolygon = archive.get<geom::polygon::Polygon>(validPolygonId);
325  } catch (pex::exceptions::NotFoundError& err) {
326  LOGLS_WARN(_log, "Could not read ValidPolygon; setting to null: " << err.what());
327  }
328  int transmissionCurveId = popInt(*metadata, "TRANSMISSION_CURVE_ID");
329  try {
330  _transmissionCurve = archive.get<TransmissionCurve>(transmissionCurveId);
331  } catch (pex::exceptions::NotFoundError& err) {
332  LOGLS_WARN(_log, "Could not read TransmissionCurve; setting to null: " << err.what());
333  }
334  }
335 
336  _metadata = metadata;
337 }
338 } // namespace image
339 } // namespace afw
340 } // namespace lsst
#define LOGLS_WARN(logger, message)
char * data
Definition: BaseTable.cc:205
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:115
A spatially-varying transmission curve as a function of wavelength.
void setCalib(std::shared_ptr< Calib const > calib)
Set the Exposure&#39;s Calib object.
Definition: ExposureInfo.h:123
virtual char const * what(void) const noexcept
A thin wrapper around std::map to allow aperture corrections to be attached to Exposures.
Definition: ApCorrMap.h:44
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:138
bool hasDetector() const
Does this exposure have Detector information?
Definition: ExposureInfo.h:99
Key< int > apCorrMap
Definition: Exposure.cc:64
ItemVariant const * other
Definition: Schema.cc:55
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:296
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:95
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:282
bool hasApCorrMap() const
Return true if the exposure has an aperture correction map.
Definition: ExposureInfo.h:154
table::Key< table::Array< std::uint8_t > > wcs
Definition: SkyWcs.cc:71
#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
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:160
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:153
void setHdu(int hdu, bool relative=false)
Set the current HDU.
Definition: fits.cc:478
std::shared_ptr< daf::base::PropertySet > getMetadata() const
Return flexible metadata.
Definition: ExposureInfo.h:126
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
static InputArchive readFits(fits::Fits &fitsfile)
Read an object from an already open FITS object.
std::shared_ptr< SkyWcs > makeSkyWcs(daf::base::PropertySet &metadata, bool strip=false)
Construct a SkyWcs from FITS keywords.
Definition: SkyWcs.cc:476
int stripVisitInfoKeywords(daf::base::PropertySet &metadata)
Remove VisitInfo-related keywords from the metadata.
Definition: VisitInfo.cc:266
Cartesian polygons.
Definition: Polygon.h:58
A multi-catalog archive object used to load table::io::Persistable objects.
Definition: InputArchive.h:31
Extent< double, 2 > Extent2D
#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:75
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
std::shared_ptr< Persistable > get(int id) const
Load the Persistable with the given ID and return it.
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