LSSTApplications  16.0-11-g09ed895+2,16.0-11-g12e47bd,16.0-11-g9bb73b2+6,16.0-12-g5c924a4+6,16.0-14-g9a974b3+1,16.0-15-g1417920+1,16.0-15-gdd5ca33+1,16.0-16-gf0259e2,16.0-17-g31abd91+7,16.0-17-g7d7456e+7,16.0-17-ga3d2e9f+13,16.0-18-ga4d4bcb+1,16.0-18-gd06566c+1,16.0-2-g0febb12+21,16.0-2-g9d5294e+69,16.0-2-ga8830df+6,16.0-20-g21842373+7,16.0-24-g3eae5ec,16.0-28-gfc9ea6c+4,16.0-29-ge8801f9,16.0-3-ge00e371+34,16.0-4-g18f3627+13,16.0-4-g5f3a788+20,16.0-4-ga3eb747+10,16.0-4-gabf74b7+29,16.0-4-gb13d127+6,16.0-49-g42e581f7+6,16.0-5-g27fb78a+7,16.0-5-g6a53317+34,16.0-5-gb3f8a4b+87,16.0-6-g9321be7+4,16.0-6-gcbc7b31+42,16.0-6-gf49912c+29,16.0-7-gd2eeba5+51,16.0-71-ge89f8615e,16.0-8-g21fd5fe+29,16.0-8-g3a9f023+20,16.0-8-g4734f7a+1,16.0-8-g5858431+3,16.0-9-gf5c1f43+8,master-gd73dc1d098+1,w.2019.01
LSSTDataManagementBasePackage
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 // Clone various components; defined here so that we don't have to expose their insides in Exposure.h
45 
47  if (calib) return std::shared_ptr<Calib>(new Calib(*calib));
48  return std::shared_ptr<Calib>();
49 }
50 
52  if (apCorrMap) {
53  return std::make_shared<ApCorrMap>(*apCorrMap);
54  }
56 }
57 
60  std::shared_ptr<Calib const> const& calib,
65  std::shared_ptr<ApCorrMap> const& apCorrMap,
68  : _wcs(wcs),
69  _psf(std::const_pointer_cast<detection::Psf>(psf)),
70  _calib(calib ? _cloneCalib(calib) : std::shared_ptr<Calib>(new Calib())),
71  _detector(detector),
72  _validPolygon(polygon),
73  _filter(filter),
74  _metadata(metadata ? metadata
75  : std::shared_ptr<daf::base::PropertySet>(new daf::base::PropertyList())),
76  _coaddInputs(coaddInputs),
77  _apCorrMap(_cloneApCorrMap(apCorrMap)),
78  _visitInfo(visitInfo),
79  _transmissionCurve(transmissionCurve) {}
80 
82  : _wcs(other._wcs),
83  _psf(other._psf),
84  _calib(_cloneCalib(other._calib)),
85  _detector(other._detector),
86  _validPolygon(other._validPolygon),
87  _filter(other._filter),
88  _metadata(other._metadata),
89  _coaddInputs(other._coaddInputs),
90  _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
91  _visitInfo(other._visitInfo),
92  _transmissionCurve(other._transmissionCurve) {}
93 
94 // Delegate to copy-constructor for backwards compatibility
96 
97 ExposureInfo::ExposureInfo(ExposureInfo const& other, bool copyMetadata)
98  : _wcs(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  _transmissionCurve(other._transmissionCurve) {
109  if (copyMetadata) _metadata = _metadata->deepCopy();
110 }
111 
113  if (&other != this) {
114  _wcs = other._wcs;
115  _psf = other._psf;
116  _calib = _cloneCalib(other._calib);
117  _detector = other._detector;
118  _validPolygon = other._validPolygon;
119  _filter = other._filter;
120  _metadata = other._metadata;
121  _coaddInputs = other._coaddInputs;
122  _apCorrMap = _cloneApCorrMap(other._apCorrMap);
123  _visitInfo = other._visitInfo;
124  _transmissionCurve = other._transmissionCurve;
125  }
126  return *this;
127 }
128 // Delegate to copy-assignment for backwards compatibility
130 
131 void ExposureInfo::initApCorrMap() { _apCorrMap = std::make_shared<ApCorrMap>(); }
132 
133 ExposureInfo::~ExposureInfo() = default;
134 
135 ExposureInfo::FitsWriteData ExposureInfo::_startWriteFits(lsst::geom::Point2I const& xy0) const {
136  FitsWriteData data;
137 
138  data.metadata.reset(new daf::base::PropertyList());
139  data.imageMetadata.reset(new daf::base::PropertyList());
140  data.maskMetadata = data.imageMetadata;
141  data.varianceMetadata = data.imageMetadata;
142 
143  data.metadata->combine(getMetadata());
144 
145  // In the future, we might not have exactly three image HDUs, but we always do right now,
146  // so 0=primary, 1=image, 2=mask, 3=variance, 4+=archive
147  //
148  // Historically the AR_HDU keyword was 1-indexed (see RFC-304), and to maintain file compatibility
149  // this is still the case so we're setting AR_HDU to 5 == 4 + 1
150  //
151  data.metadata->set("AR_HDU", 5, "HDU (1-indexed) containing the archive used to store ancillary objects");
152  if (hasCoaddInputs()) {
153  int coaddInputsId = data.archive.put(getCoaddInputs());
154  data.metadata->set("COADD_INPUTS_ID", coaddInputsId, "archive ID for coadd inputs catalogs");
155  }
156  if (hasApCorrMap()) {
157  int apCorrMapId = data.archive.put(getApCorrMap());
158  data.metadata->set("AP_CORR_MAP_ID", apCorrMapId, "archive ID for aperture correction map");
159  }
160  if (hasPsf() && getPsf()->isPersistable()) {
161  int psfId = data.archive.put(getPsf());
162  data.metadata->set("PSF_ID", psfId, "archive ID for the Exposure's main Psf");
163  }
164  if (hasWcs() && getWcs()->isPersistable()) {
165  int wcsId = data.archive.put(getWcs());
166  data.metadata->set("SKYWCS_ID", wcsId, "archive ID for the Exposure's main Wcs");
167  }
168  if (hasValidPolygon() && getValidPolygon()->isPersistable()) {
169  int polygonId = data.archive.put(getValidPolygon());
170  data.metadata->set("VALID_POLYGON_ID", polygonId, "archive ID for the Exposure's valid polygon");
171  }
172  if (hasTransmissionCurve() && getTransmissionCurve()->isPersistable()) {
173  int transmissionCurveId = data.archive.put(getTransmissionCurve());
174  data.metadata->set("TRANSMISSION_CURVE_ID", transmissionCurveId,
175  "archive ID for the Exposure's transmission curve");
176  }
177  if (hasDetector() && getDetector()->isPersistable()) {
178  int detectorId = data.archive.put(getDetector());
179  data.metadata->set("DETECTOR_ID", detectorId, "archive ID for the Exposure's Detector");
180  }
181 
182  // LSST convention is that Wcs is in pixel coordinates (i.e relative to bottom left
183  // corner of parent image, if any). The Wcs/Fits convention is that the Wcs is in
184  // image coordinates. When saving an image we convert from pixel to index coordinates.
185  // In the case where this image is a parent image, the reference pixels are unchanged
186  // by this transformation
187  if (hasWcs()) {
188  // Try to save the WCS as FITS-WCS metadata; if an exact representation
189  // is not possible then skip it
190  auto shift = lsst::geom::Extent2D(lsst::geom::Point2I(0, 0) - xy0);
191  auto newWcs = getWcs()->copyAtShiftedPixelOrigin(shift);
193  try {
194  wcsMetadata = newWcs->getFitsMetadata(true);
196  // cannot represent this WCS as FITS-WCS; don't write its metadata
197  }
198  if (wcsMetadata) {
199  data.imageMetadata->combine(newWcs->getFitsMetadata(true));
200  }
201  }
202 
203  // For the sake of ds9, store _x0 and _y0 as -LTV1, -LTV2.
204  // This is in addition to saving _x0 and _y0 as WCS A, which is done elsewhere
205  // and is what LSST uses to read _x0 and _y0.
206  // LTV is a convention used by STScI (see \S2.6.2 of HST Data Handbook for STIS, version 5.0
207  // http://www.stsci.edu/hst/stis/documents/handbooks/currentDHB/ch2_stis_data7.html#429287)
208  // and recognized by ds9.
209  data.imageMetadata->set("LTV1", static_cast<double>(-xy0.getX()));
210  data.imageMetadata->set("LTV2", static_cast<double>(-xy0.getY()));
211 
212  data.metadata->set("FILTER", getFilter().getName());
213  if (hasDetector()) {
214  data.metadata->set("DETNAME", getDetector()->getName());
215  data.metadata->set("DETSER", getDetector()->getSerial());
216  }
217 
218  auto visitInfoPtr = getVisitInfo();
219  if (visitInfoPtr) {
220  detail::setVisitInfoMetadata(*(data.metadata), *visitInfoPtr);
221  }
222 
223  /*
224  * We need to define these keywords properly! XXX
225  */
226  data.metadata->set("FLUXMAG0", getCalib()->getFluxMag0().first);
227  data.metadata->set("FLUXMAG0ERR", getCalib()->getFluxMag0().second);
228 
229  return data;
230 }
231 
232 void ExposureInfo::_finishWriteFits(fits::Fits& fitsfile, FitsWriteData const& data) const {
233  data.archive.writeFits(fitsfile);
234 }
235 
236 } // namespace image
237 } // namespace afw
238 } // namespace lsst
char * data
Definition: BaseTable.cc:205
bool hasTransmissionCurve() const
Does this exposure have a transmission curve?
Definition: ExposureInfo.h:192
#define LOG_LOGGER
Definition: Log.h:712
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
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
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: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:58
bool hasWcs() const
Does this exposure have a Wcs?
Definition: ExposureInfo.h:90
Describe an exposure&#39;s calibration.
Definition: Calib.h:95
table::Key< table::Array< std::uint8_t > > wcs
Definition: SkyWcs.cc:71
LSST DM logging module built on log4cxx.
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:283
bool hasApCorrMap() const
Return true if the exposure has an aperture correction map.
Definition: ExposureInfo.h:154
A base class for image defects.
Definition: cameraGeom.dox:3
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
table::Key< int > detector
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
std::shared_ptr< daf::base::PropertySet > getMetadata() const
Return flexible metadata.
Definition: ExposureInfo.h:126
Key< int > apCorrMap
Definition: Exposure.cc:66
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
ItemVariant const * other
Definition: Schema.cc:56
Extent< double, 2 > Extent2D
Definition: Extent.h:400
#define LOG_GET(logger)
Returns a Log object associated with logger.
Definition: Log.h:83
A collection of all the things that make an Exposure different from a MaskedImage.
Definition: ExposureInfo.h:87
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
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104
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