lsst.afw  13.0-91-g2a3e2ee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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"
33 #include "lsst/afw/fits.h"
34 
35 namespace {
36 LOG_LOGGER _log = LOG_GET("afw.image.ExposureInfo");
37 }
38 
39 namespace lsst {
40 namespace afw {
41 namespace image {
42 
43 namespace {
44 
45 // Return an int value from a PropertySet if it exists and remove it, or return 0.
46 int popInt(daf::base::PropertySet& metadata, std::string const& name) {
47  int r = 0;
48  if (metadata.exists(name)) {
49  r = metadata.get<int>(name);
50  metadata.remove(name);
51  }
52  return r;
53 }
54 
55 } // anonymous
56 
57 // Clone various components; defined here so that we don't have to expose their insides in Exposure.h
58 
59 std::shared_ptr<Calib> ExposureInfo::_cloneCalib(std::shared_ptr<Calib const> calib) {
60  if (calib) return std::shared_ptr<Calib>(new Calib(*calib));
61  return std::shared_ptr<Calib>();
62 }
63 
64 std::shared_ptr<Wcs> ExposureInfo::_cloneWcs(std::shared_ptr<Wcs const> wcs) {
65  if (wcs) return wcs->clone();
66  return std::shared_ptr<Wcs>();
67 }
68 
69 std::shared_ptr<ApCorrMap> ExposureInfo::_cloneApCorrMap(std::shared_ptr<ApCorrMap const> apCorrMap) {
70  if (apCorrMap) {
71  return std::make_shared<ApCorrMap>(*apCorrMap);
72  }
73  return std::shared_ptr<ApCorrMap>();
74 }
75 
76 ExposureInfo::ExposureInfo(std::shared_ptr<Wcs const> const& wcs,
77  std::shared_ptr<detection::Psf const> const& psf,
78  std::shared_ptr<Calib const> const& calib,
79  std::shared_ptr<cameraGeom::Detector const> const& detector,
80  std::shared_ptr<geom::polygon::Polygon const> const& polygon, Filter const& filter,
81  std::shared_ptr<daf::base::PropertySet> const& metadata,
82  std::shared_ptr<CoaddInputs> const& coaddInputs,
83  std::shared_ptr<ApCorrMap> const& apCorrMap,
84  std::shared_ptr<image::VisitInfo const> const& visitInfo)
85  : _wcs(_cloneWcs(wcs)),
86  _psf(std::const_pointer_cast<detection::Psf>(psf)),
87  _calib(calib ? _cloneCalib(calib) : std::shared_ptr<Calib>(new Calib())),
88  _detector(detector),
89  _validPolygon(polygon),
90  _filter(filter),
91  _metadata(metadata ? metadata
92  : std::shared_ptr<daf::base::PropertySet>(new daf::base::PropertyList())),
93  _coaddInputs(coaddInputs),
94  _apCorrMap(_cloneApCorrMap(apCorrMap)),
95  _visitInfo(visitInfo) {}
96 
98  : _wcs(_cloneWcs(other._wcs)),
99  _psf(other._psf),
100  _calib(_cloneCalib(other._calib)),
101  _detector(other._detector),
102  _validPolygon(other._validPolygon),
103  _filter(other._filter),
104  _metadata(other._metadata),
105  _coaddInputs(other._coaddInputs),
106  _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
107  _visitInfo(other._visitInfo) {}
108 
109 ExposureInfo::ExposureInfo(ExposureInfo const& other, bool copyMetadata)
110  : _wcs(_cloneWcs(other._wcs)),
111  _psf(other._psf),
112  _calib(_cloneCalib(other._calib)),
113  _detector(other._detector),
114  _validPolygon(other._validPolygon),
115  _filter(other._filter),
116  _metadata(other._metadata),
117  _coaddInputs(other._coaddInputs),
118  _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
119  _visitInfo(other._visitInfo) {
120  if (copyMetadata) _metadata = _metadata->deepCopy();
121 }
122 
124  if (&other != this) {
125  _wcs = _cloneWcs(other._wcs);
126  _psf = other._psf;
127  _calib = _cloneCalib(other._calib);
128  _detector = other._detector;
129  _validPolygon = other._validPolygon;
130  _filter = other._filter;
131  _metadata = other._metadata;
132  _coaddInputs = other._coaddInputs;
133  _apCorrMap = _cloneApCorrMap(other._apCorrMap);
134  _visitInfo = other._visitInfo;
135  }
136  return *this;
137 }
138 
139 void ExposureInfo::initApCorrMap() { _apCorrMap = std::make_shared<ApCorrMap>(); }
140 
142 
143 ExposureInfo::FitsWriteData ExposureInfo::_startWriteFits(afw::geom::Point2I const& xy0) const {
144  FitsWriteData data;
145 
146  data.metadata.reset(new daf::base::PropertyList());
147  data.imageMetadata.reset(new daf::base::PropertyList());
148  data.maskMetadata = data.imageMetadata;
149  data.varianceMetadata = data.imageMetadata;
150 
151  data.metadata->combine(getMetadata());
152 
153  // In the future, we might not have exactly three image HDUs, but we always do right now,
154  // so 0=primary, 1=image, 2=mask, 3=variance, 4+=archive
155  //
156  // Historically the AR_HDU keyword was 1-indexed (see RFC-304), and to maintain file compatibility
157  // this is still the case so we're setting AR_HDU to 5 == 4 + 1
158  //
159  data.metadata->set("AR_HDU", 5, "HDU (1-indexed) containing the archive used to store ancillary objects");
160  if (hasCoaddInputs()) {
161  int coaddInputsId = data.archive.put(getCoaddInputs());
162  data.metadata->set("COADD_INPUTS_ID", coaddInputsId, "archive ID for coadd inputs catalogs");
163  }
164  if (hasApCorrMap()) {
165  int apCorrMapId = data.archive.put(getApCorrMap());
166  data.metadata->set("AP_CORR_MAP_ID", apCorrMapId, "archive ID for aperture correction map");
167  }
168  if (hasPsf() && getPsf()->isPersistable()) {
169  int psfId = data.archive.put(getPsf());
170  data.metadata->set("PSF_ID", psfId, "archive ID for the Exposure's main Psf");
171  }
172  if (hasWcs() && getWcs()->isPersistable()) {
173  int wcsId = data.archive.put(getWcs());
174  data.metadata->set("WCS_ID", wcsId, "archive ID for the Exposure's main Wcs");
175  }
176  if (hasValidPolygon() && getValidPolygon()->isPersistable()) {
177  int polygonId = data.archive.put(getValidPolygon());
178  data.metadata->set("VALID_POLYGON_ID", polygonId, "archive ID for the Exposure's valid polygon");
179  }
180 
181  // LSST convention is that Wcs is in pixel coordinates (i.e relative to bottom left
182  // corner of parent image, if any). The Wcs/Fits convention is that the Wcs is in
183  // image coordinates. When saving an image we convert from pixel to index coordinates.
184  // In the case where this image is a parent image, the reference pixels are unchanged
185  // by this transformation
186  if (hasWcs()) {
187  std::shared_ptr<Wcs> newWcs = getWcs()->clone(); // Create a copy
188  newWcs->shiftReferencePixel(-xy0.getX(), -xy0.getY());
189 
190  // We want the WCS to appear in all HDUs
191  data.imageMetadata->combine(newWcs->getFitsMetadata());
192  }
193 
194  // Store _x0 and _y0. If this exposure is a portion of a larger image, _x0 and _y0
195  // indicate the origin (the position of the bottom left corner) of the sub-image with
196  // respect to the origin of the parent image.
197  // This is stored in the fits header using the LTV convention used by STScI
198  //(see \S2.6.2 of HST Data Handbook for STIS, version 5.0
199  // http://www.stsci.edu/hst/stis/documents/handbooks/currentDHB/ch2_stis_data7.html#429287).
200  // This is not a fits standard keyword, but is recognised by ds9
201  // LTV keywords use the opposite convention to the LSST, in that they represent
202  // the position of the origin of the parent image relative to the origin of the sub-image.
203  // _x0, _y0 >= 0, while LTV1 and LTV2 <= 0
204 
205  data.imageMetadata->set("LTV1", static_cast<double>(-xy0.getX()));
206  data.imageMetadata->set("LTV2", static_cast<double>(-xy0.getY()));
207 
208  data.metadata->set("FILTER", getFilter().getName());
209  if (hasDetector()) {
210  data.metadata->set("DETNAME", getDetector()->getName());
211  data.metadata->set("DETSER", getDetector()->getSerial());
212  }
213 
214  auto visitInfoPtr = getVisitInfo();
215  if (visitInfoPtr) {
216  detail::setVisitInfoMetadata(*(data.metadata), *visitInfoPtr);
217  }
218 
219  /*
220  * We need to define these keywords properly! XXX
221  */
222  data.metadata->set("FLUXMAG0", getCalib()->getFluxMag0().first);
223  data.metadata->set("FLUXMAG0ERR", getCalib()->getFluxMag0().second);
224 
225  return data;
226 }
227 
228 void ExposureInfo::_finishWriteFits(fits::Fits& fitsfile, FitsWriteData const& data) const {
229  data.archive.writeFits(fitsfile);
230 }
231 
232 void ExposureInfo::_readFits(fits::Fits& fitsfile, std::shared_ptr<daf::base::PropertySet> metadata,
233  std::shared_ptr<daf::base::PropertySet> imageMetadata) {
234  // true: strip keywords that are related to the created WCS from the input metadata
235  _wcs = makeWcs(imageMetadata, true);
236 
237  if (!imageMetadata->exists("INHERIT")) {
238  // New-style exposures put everything but the Wcs in the primary HDU, use
239  // INHERIT keyword in the others. For backwards compatibility, if we don't
240  // find the INHERIT keyword, we ignore the primary HDU metadata and expect
241  // everything to be in the image HDU metadata. Note that we can't merge them,
242  // because they're probably duplicates.
243  metadata = imageMetadata;
244  }
245 
246  _filter = Filter(metadata, true);
247  detail::stripFilterKeywords(metadata);
248 
249  _visitInfo = std::shared_ptr<VisitInfo const>(new VisitInfo(*metadata));
251 
252  std::shared_ptr<Calib> newCalib(new Calib(metadata));
253  setCalib(newCalib);
254  detail::stripCalibKeywords(metadata);
255 
256  int archiveHdu = popInt(*metadata, "AR_HDU");
257 
258  if (archiveHdu) {
259  --archiveHdu; // see note above in _startWriteFits; AR_HDU is *one* indexed
260 
261  fitsfile.setHdu(archiveHdu);
262  table::io::InputArchive archive = table::io::InputArchive::readFits(fitsfile);
263  // Load the Psf and Wcs from the archive; id=0 results in a null pointer.
264  // Note that the binary table Wcs, if present, clobbers the FITS header one,
265  // because the former might be an approximation to something we can't represent
266  // using the FITS WCS standard but can represent with binary tables.
267  int psfId = popInt(*metadata, "PSF_ID");
268  try {
269  _psf = archive.get<detection::Psf>(psfId);
270  } catch (pex::exceptions::NotFoundError& err) {
271  LOGLS_WARN(_log, "Could not read PSF; setting to null: " << err.what());
272  }
273  int wcsId = popInt(*metadata, "WCS_ID");
274  try {
275  auto archiveWcs = archive.get<Wcs>(wcsId);
276  if (archiveWcs) {
277  _wcs = archiveWcs;
278  } else {
279  LOGLS_DEBUG(_log, "Null WCS seen in binary table");
280  }
281  } catch (pex::exceptions::NotFoundError& err) {
282  auto msg = str(boost::format("Could not read WCS extension; setting to null: %s") % err.what());
283  if (_wcs) {
284  msg += " ; using WCS from FITS header";
285  }
286  LOGLS_WARN(_log, msg);
287  }
288  int coaddInputsId = popInt(*metadata, "COADD_INPUTS_ID");
289  try {
290  _coaddInputs = archive.get<CoaddInputs>(coaddInputsId);
291  } catch (pex::exceptions::NotFoundError& err) {
292  LOGLS_WARN(_log, "Could not read CoaddInputs; setting to null: " << err.what());
293  }
294  int apCorrMapId = popInt(*metadata, "AP_CORR_MAP_ID");
295  try {
296  _apCorrMap = archive.get<ApCorrMap>(apCorrMapId);
297  } catch (pex::exceptions::NotFoundError& err) {
298  LOGLS_WARN(_log, "Could not read ApCorrMap; setting to null: " << err.what());
299  }
300  int validPolygonId = popInt(*metadata, "VALID_POLYGON_ID");
301  try {
302  _validPolygon = archive.get<geom::polygon::Polygon>(validPolygonId);
303  } catch (pex::exceptions::NotFoundError& err) {
304  LOGLS_WARN(_log, "Could not read ValidPolygon; setting to null: " << err.what());
305  }
306  }
307 
308  _metadata = metadata;
309 }
310 }
311 }
312 } // namespace lsst::afw::image
std::shared_ptr< Wcs > getWcs()
Return the coordinate system of the exposure.
Definition: ExposureInfo.h:92
std::shared_ptr< detection::Psf > getPsf() const
Return the exposure&#39;s point-spread function.
Definition: ExposureInfo.h:137
static InputArchive readFits(fits::Fits &fitsfile)
Read an object from an already open FITS object.
std::shared_ptr< ApCorrMap > getApCorrMap()
Return the exposure&#39;s aperture correction map (null pointer if !hasApCorrMap())
Definition: ExposureInfo.h:159
int stripFilterKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Filter-related keywords from the metadata.
Definition: Filter.cc:123
bool hasValidPolygon() const
Does this exposure have a valid Polygon.
Definition: ExposureInfo.h:147
void setCalib(std::shared_ptr< Calib const > calib)
Set the Exposure&#39;s Calib object.
Definition: ExposureInfo.h:125
bool hasCoaddInputs() const
Does this exposure have coadd provenance catalogs?
Definition: ExposureInfo.h:176
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 >())
Construct an ExposureInfo from its various components.
Definition: ExposureInfo.cc:76
Describe an exposure&#39;s calibration.
Definition: Calib.h:76
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
std::shared_ptr< daf::base::PropertySet > getMetadata() const
Return flexible metadata.
Definition: ExposureInfo.h:128
void initApCorrMap()
Set the exposure&#39;s aperture correction map to a new, empty map.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
int stripCalibKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Calib-related keywords from the metadata.
Definition: Calib.cc:96
std::shared_ptr< cameraGeom::Detector const > getDetector() const
Return the exposure&#39;s Detector information.
Definition: ExposureInfo.h:104
Holds an integer identifier for an LSST filter.
Definition: Filter.h:134
Filter getFilter() const
Return the exposure&#39;s filter.
Definition: ExposureInfo.h:110
bool hasPsf() const
Does this exposure have a Psf?
Definition: ExposureInfo.h:134
std::shared_ptr< Calib > getCalib()
Return the exposure&#39;s photometric calibration.
Definition: ExposureInfo.h:119
bool hasWcs() const
Does this exposure have a Wcs?
Definition: ExposureInfo.h:89
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:55
afw::table::Key< afw::table::Array< ImagePixelT > > image
int stripVisitInfoKeywords(daf::base::PropertySet &metadata)
Remove VisitInfo-related keywords from the metadata.
Definition: VisitInfo.cc:259
bool hasApCorrMap() const
Return true if the exposure has an aperture correction map.
Definition: ExposureInfo.h:156
A collection of all the things that make an Exposure different from a MaskedImage.
Definition: ExposureInfo.h:86
std::shared_ptr< geom::polygon::Polygon const > getValidPolygon() const
Return the valid Polygon.
Definition: ExposureInfo.h:150
daf::base::PropertySet & _metadata
Definition: fits_io_mpl.h:83
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.
std::shared_ptr< image::VisitInfo const > getVisitInfo() const
Return the exposure&#39;s visit info.
Definition: ExposureInfo.h:185
bool hasDetector() const
Does this exposure have Detector information?
Definition: ExposureInfo.h:101
lsst::daf::base::PropertyList PropertyList
Definition: Wcs.cc:56
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:182