lsst.afw  14.0-38-g278b05818
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 ExposureInfo::ExposureInfo(ExposureInfo const& other, bool copyMetadata)
115  : _wcs(_cloneWcs(other._wcs)),
116  _psf(other._psf),
117  _calib(_cloneCalib(other._calib)),
118  _detector(other._detector),
119  _validPolygon(other._validPolygon),
120  _filter(other._filter),
121  _metadata(other._metadata),
122  _coaddInputs(other._coaddInputs),
123  _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
124  _visitInfo(other._visitInfo),
125  _transmissionCurve(other._transmissionCurve) {
126  if (copyMetadata) _metadata = _metadata->deepCopy();
127 }
128 
130  if (&other != this) {
131  _wcs = _cloneWcs(other._wcs);
132  _psf = other._psf;
133  _calib = _cloneCalib(other._calib);
134  _detector = other._detector;
135  _validPolygon = other._validPolygon;
136  _filter = other._filter;
137  _metadata = other._metadata;
138  _coaddInputs = other._coaddInputs;
139  _apCorrMap = _cloneApCorrMap(other._apCorrMap);
140  _visitInfo = other._visitInfo;
141  _transmissionCurve = other._transmissionCurve;
142  }
143  return *this;
144 }
145 
146 void ExposureInfo::initApCorrMap() { _apCorrMap = std::make_shared<ApCorrMap>(); }
147 
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("WCS_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  std::shared_ptr<Wcs> newWcs = getWcs()->clone(); // Create a copy
200  newWcs->shiftReferencePixel(-xy0.getX(), -xy0.getY());
201 
202  // We want the WCS to appear in all HDUs
203  data.imageMetadata->combine(newWcs->getFitsMetadata());
204  }
205 
206  // Store _x0 and _y0. If this exposure is a portion of a larger image, _x0 and _y0
207  // indicate the origin (the position of the bottom left corner) of the sub-image with
208  // respect to the origin of the parent image.
209  // This is stored in the fits header using the LTV convention used by STScI
210  //(see \S2.6.2 of HST Data Handbook for STIS, version 5.0
211  // http://www.stsci.edu/hst/stis/documents/handbooks/currentDHB/ch2_stis_data7.html#429287).
212  // This is not a fits standard keyword, but is recognised by ds9
213  // LTV keywords use the opposite convention to the LSST, in that they represent
214  // the position of the origin of the parent image relative to the origin of the sub-image.
215  // _x0, _y0 >= 0, while LTV1 and LTV2 <= 0
216 
217  data.imageMetadata->set("LTV1", static_cast<double>(-xy0.getX()));
218  data.imageMetadata->set("LTV2", static_cast<double>(-xy0.getY()));
219 
220  data.metadata->set("FILTER", getFilter().getName());
221  if (hasDetector()) {
222  data.metadata->set("DETNAME", getDetector()->getName());
223  data.metadata->set("DETSER", getDetector()->getSerial());
224  }
225 
226  auto visitInfoPtr = getVisitInfo();
227  if (visitInfoPtr) {
228  detail::setVisitInfoMetadata(*(data.metadata), *visitInfoPtr);
229  }
230 
231  /*
232  * We need to define these keywords properly! XXX
233  */
234  data.metadata->set("FLUXMAG0", getCalib()->getFluxMag0().first);
235  data.metadata->set("FLUXMAG0ERR", getCalib()->getFluxMag0().second);
236 
237  return data;
238 }
239 
240 void ExposureInfo::_finishWriteFits(fits::Fits& fitsfile, FitsWriteData const& data) const {
241  data.archive.writeFits(fitsfile);
242 }
243 
244 void ExposureInfo::_readFits(fits::Fits& fitsfile, std::shared_ptr<daf::base::PropertySet> metadata,
246  // true: strip keywords that are related to the created WCS from the input metadata
247  _wcs = makeWcs(imageMetadata, true);
248 
249  if (!imageMetadata->exists("INHERIT")) {
250  // New-style exposures put everything but the Wcs in the primary HDU, use
251  // INHERIT keyword in the others. For backwards compatibility, if we don't
252  // find the INHERIT keyword, we ignore the primary HDU metadata and expect
253  // everything to be in the image HDU metadata. Note that we can't merge them,
254  // because they're probably duplicates.
255  metadata = imageMetadata;
256  }
257 
258  _filter = Filter(metadata, true);
259  detail::stripFilterKeywords(metadata);
260 
261  _visitInfo = std::shared_ptr<VisitInfo const>(new VisitInfo(*metadata));
263 
264  std::shared_ptr<Calib> newCalib(new Calib(metadata));
265  setCalib(newCalib);
266  detail::stripCalibKeywords(metadata);
267 
268  int archiveHdu = popInt(*metadata, "AR_HDU");
269 
270  if (archiveHdu) {
271  --archiveHdu; // see note above in _startWriteFits; AR_HDU is *one* indexed
272 
273  fitsfile.setHdu(archiveHdu);
275  // Load the Psf and Wcs from the archive; id=0 results in a null pointer.
276  // Note that the binary table Wcs, if present, clobbers the FITS header one,
277  // because the former might be an approximation to something we can't represent
278  // using the FITS WCS standard but can represent with binary tables.
279  int psfId = popInt(*metadata, "PSF_ID");
280  try {
281  _psf = archive.get<detection::Psf>(psfId);
282  } catch (pex::exceptions::NotFoundError& err) {
283  LOGLS_WARN(_log, "Could not read PSF; setting to null: " << err.what());
284  }
285  int wcsId = popInt(*metadata, "WCS_ID");
286  try {
287  auto archiveWcs = archive.get<Wcs>(wcsId);
288  if (archiveWcs) {
289  _wcs = archiveWcs;
290  } else {
291  LOGLS_DEBUG(_log, "Null WCS seen in binary table");
292  }
293  } catch (pex::exceptions::NotFoundError& err) {
294  auto msg = str(boost::format("Could not read WCS extension; setting to null: %s") % err.what());
295  if (_wcs) {
296  msg += " ; using WCS from FITS header";
297  }
298  LOGLS_WARN(_log, msg);
299  }
300  int coaddInputsId = popInt(*metadata, "COADD_INPUTS_ID");
301  try {
302  _coaddInputs = archive.get<CoaddInputs>(coaddInputsId);
303  } catch (pex::exceptions::NotFoundError& err) {
304  LOGLS_WARN(_log, "Could not read CoaddInputs; setting to null: " << err.what());
305  }
306  int apCorrMapId = popInt(*metadata, "AP_CORR_MAP_ID");
307  try {
308  _apCorrMap = archive.get<ApCorrMap>(apCorrMapId);
309  } catch (pex::exceptions::NotFoundError& err) {
310  LOGLS_WARN(_log, "Could not read ApCorrMap; setting to null: " << err.what());
311  }
312  int validPolygonId = popInt(*metadata, "VALID_POLYGON_ID");
313  try {
314  _validPolygon = archive.get<geom::polygon::Polygon>(validPolygonId);
315  } catch (pex::exceptions::NotFoundError& err) {
316  LOGLS_WARN(_log, "Could not read ValidPolygon; setting to null: " << err.what());
317  }
318  int transmissionCurveId = popInt(*metadata, "TRANSMISSION_CURVE_ID");
319  try {
320  _transmissionCurve = archive.get<TransmissionCurve>(transmissionCurveId);
321  } catch (pex::exceptions::NotFoundError & err) {
322  LOGLS_WARN(_log, "Could not read TransmissionCurve; setting to null: " << err.what());
323  }
324  }
325 
326  _metadata = metadata;
327 }
328 }
329 }
330 } // 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:150
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:134
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