lsst.afw 23.0.0+60ce4897b0
ExposureInfo.cc
Go to the documentation of this file.
1// -*- LSST-C++ -*- // fixed format comment for emacs
2/*
3 * This file is part of afw.
4 *
5 * Developed for the LSST Data Management System.
6 * This product includes software developed by the LSST Project
7 * (https://www.lsst.org).
8 * See the COPYRIGHT file at the top-level directory of this distribution
9 * for details of code ownership.
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25#include "lsst/pex/exceptions.h"
26#include "lsst/log/Log.h"
35#include "lsst/afw/fits.h"
36
37using namespace std::string_literals;
38
39namespace {
40LOG_LOGGER _log = LOG_GET("afw.image.ExposureInfo");
41
43} // namespace
44
45namespace lsst {
46namespace afw {
47namespace image {
48
49// Important: do *not* change the keys' strings; doing so will break compatibility with old FITS files
50
51typehandling::Key<std::string, std::shared_ptr<geom::SkyWcs const>> const ExposureInfo::KEY_WCS =
52 typehandling::makeKey<std::shared_ptr<geom::SkyWcs const>>("SKYWCS"s);
53
54bool ExposureInfo::hasWcs() const { return hasComponent(KEY_WCS); }
57
59 typehandling::makeKey<std::shared_ptr<detection::Psf const>>("PSF"s);
60
61bool ExposureInfo::hasPsf() const { return hasComponent(KEY_PSF); }
64
66 typehandling::makeKey<std::shared_ptr<PhotoCalib const>>("PHOTOCALIB"s);
67
71}
74}
75
77 typehandling::makeKey<std::shared_ptr<cameraGeom::Detector const>>("DETECTOR"s);
78
82}
85}
86
89 typehandling::makeKey<std::shared_ptr<geom::polygon::Polygon const>>("VALID_POLYGON"s);
90
94}
97}
98
100 typehandling::makeKey<std::shared_ptr<CoaddInputs const>>("COADD_INPUTS"s);
101
104 setComponent(KEY_COADD_INPUTS, coaddInputs);
105}
108}
109
111 typehandling::makeKey<std::shared_ptr<ApCorrMap const>>("AP_CORR_MAP"s);
112
117}
118
121 typehandling::makeKey<std::shared_ptr<TransmissionCurve const>>("TRANSMISSION_CURVE"s);
122
126}
129}
130
131// Compatibility code defined inside ExposureFitsReader.cc, to be removed in DM-27177
133Filter makeFilter(FilterLabel const& label);
134
137 if (label) {
138 return makeFilter(*label);
139 } else {
140 // Old exposures always had a Filter, even if only the default
141 return Filter();
142 }
143}
144
146
148 typehandling::makeKey<std::shared_ptr<FilterLabel const>>("FILTER"s);
152 setComponent(KEY_FILTER, label);
153}
154
156 // Version history:
157 // unversioned and 0: photometric calibration via Calib, WCS via SkyWcs using AST.
158 // 1: photometric calibration via PhotoCalib, generic components
159 // 2: remove Filter, replaced with (generic) FilterLabel
160 static int const version = 2;
161 return version;
162}
163
165 static std::string const versionName("EXPINFO_V");
166 return versionName;
167}
168
169// Clone various components; defined here so that we don't have to expose their insides in Exposure.h
170
172 if (apCorrMap) {
173 return std::make_shared<ApCorrMap>(*apCorrMap);
174 }
176}
177
182 std::shared_ptr<geom::polygon::Polygon const> const& polygon, Filter const& filter,
184 std::shared_ptr<CoaddInputs> const& coaddInputs,
188 : _metadata(metadata ? metadata
189 : std::shared_ptr<daf::base::PropertySet>(new daf::base::PropertyList())),
190 _visitInfo(visitInfo),
191 _components(std::make_unique<MapClass>()) {
192 // setFilter guards against default filters
193 setFilter(filter);
194 setWcs(wcs);
195 setPsf(psf);
198 setValidPolygon(polygon);
199 setCoaddInputs(coaddInputs);
200 setApCorrMap(_cloneApCorrMap(apCorrMap));
202}
203
205
206// Delegate to copy-constructor for backwards compatibility
208
209ExposureInfo::ExposureInfo(ExposureInfo const& other, bool copyMetadata)
210 : _metadata(other._metadata),
211 _visitInfo(other._visitInfo),
212 // ExposureInfos can (historically) share objects, but should each have their own pointers to them
213 _components(std::make_unique<MapClass>(*(other._components))) {
214 if (copyMetadata) _metadata = _metadata->deepCopy();
215}
216
218 if (&other != this) {
219 _metadata = other._metadata;
220 _visitInfo = other._visitInfo;
221 // ExposureInfos can (historically) share objects, but should each have their own pointers to them
222 _components = std::make_unique<MapClass>(*(other._components));
223 }
224 return *this;
225}
226// Delegate to copy-assignment for backwards compatibility
227ExposureInfo& ExposureInfo::operator=(ExposureInfo&& other) { return *this = other; }
228
229void ExposureInfo::initApCorrMap() { setApCorrMap(std::make_shared<ApCorrMap>()); }
230
232
233int ExposureInfo::_addToArchive(FitsWriteData& data, table::io::Persistable const& object, std::string key,
234 std::string comment) {
235 int componentId = data.archive.put(object);
236 data.metadata->set(key, componentId, comment);
237 return componentId;
238}
239
240int ExposureInfo::_addToArchive(FitsWriteData& data,
242 std::string comment) {
243 // Don't delegate to Persistable const& version because OutputArchive::put
244 // has special handling of shared_ptr
245 int componentId = data.archive.put(object);
246 data.metadata->set(key, componentId, comment);
247 return componentId;
248}
249
250// Standardized strings for _startWriteFits
251namespace {
252std::string _getOldHeaderKey(std::string mapKey) { return mapKey + "_ID"; }
253std::string _getNewHeaderKey(std::string mapKey) { return "ARCHIVE_ID_" + mapKey; }
254
255std::string _getHeaderComment(std::string mapKey) {
256 return "archive ID for generic component '" + mapKey + "'";
257}
258} // namespace
259
260ExposureInfo::FitsWriteData ExposureInfo::_startWriteFits(lsst::geom::Point2I const& xy0) const {
261 FitsWriteData data;
262
263 data.metadata.reset(new daf::base::PropertyList());
264 data.imageMetadata.reset(new daf::base::PropertyList());
265 data.maskMetadata = data.imageMetadata;
266 data.varianceMetadata = data.imageMetadata;
267
268 data.metadata->combine(getMetadata());
269
271
272 // In the future, we might not have exactly three image HDUs, but we always do right now,
273 // so 0=primary, 1=image, 2=mask, 3=variance, 4+=archive
274 //
275 // Historically the AR_HDU keyword was 1-indexed (see RFC-304), and to maintain file compatibility
276 // this is still the case so we're setting AR_HDU to 5 == 4 + 1
277 //
278 data.metadata->set("AR_HDU", 5, "HDU (1-indexed) containing the archive used to store ancillary objects");
279 for (auto const& keyValue : *_components) {
280 std::string const& key = keyValue.first.getId();
281 std::shared_ptr<typehandling::Storable const> const& object = keyValue.second;
282
283 if (object && object->isPersistable()) {
284 std::string comment = _getHeaderComment(key);
285 // Store archive ID in two header keys:
286 // - old-style key for backwards compatibility,
287 // - and new-style key because it's much safer to parse
288 int id = _addToArchive(data, object, _getOldHeaderKey(key), comment);
289 data.metadata->set(_getNewHeaderKey(key), id, comment);
290 }
291 }
292
293 // LSST convention is that Wcs is in pixel coordinates (i.e relative to bottom left
294 // corner of parent image, if any). The Wcs/Fits convention is that the Wcs is in
295 // image coordinates. When saving an image we convert from pixel to index coordinates.
296 // In the case where this image is a parent image, the reference pixels are unchanged
297 // by this transformation
298 if (hasWcs() && getWcs()->isFits()) {
299 // Try to save the WCS as FITS-WCS metadata; if an exact representation
300 // is not possible then skip it
301 auto shift = lsst::geom::Extent2D(lsst::geom::Point2I(0, 0) - xy0);
302 auto newWcs = getWcs()->copyAtShiftedPixelOrigin(shift);
304 try {
305 wcsMetadata = newWcs->getFitsMetadata(true);
306 } catch (pex::exceptions::RuntimeError const&) {
307 // cannot represent this WCS as FITS-WCS; don't write its metadata
308 }
309 if (wcsMetadata) {
310 data.imageMetadata->combine(newWcs->getFitsMetadata(true));
311 }
312 }
313
314 // For the sake of ds9, store _x0 and _y0 as -LTV1, -LTV2.
315 // This is in addition to saving _x0 and _y0 as WCS A, which is done elsewhere
316 // and is what LSST uses to read _x0 and _y0.
317 // LTV is a convention used by STScI (see \S2.6.2 of HST Data Handbook for STIS, version 5.0
318 // http://www.stsci.edu/hst/stis/documents/handbooks/currentDHB/ch2_stis_data7.html#429287)
319 // and recognized by ds9.
320 data.imageMetadata->set("LTV1", static_cast<double>(-xy0.getX()));
321 data.imageMetadata->set("LTV2", static_cast<double>(-xy0.getY()));
322
323 if (hasDetector()) {
324 data.metadata->set("DETNAME", getDetector()->getName());
325 data.metadata->set("DETSER", getDetector()->getSerial());
326 }
327
328 auto visitInfoPtr = getVisitInfo();
329 if (visitInfoPtr) {
330 detail::setVisitInfoMetadata(*(data.metadata), *visitInfoPtr);
331 }
332
333 return data;
334}
335
336void ExposureInfo::_finishWriteFits(fits::Fits& fitsfile, FitsWriteData const& data) const {
337 data.archive.writeFits(fitsfile);
338}
339
340} // namespace image
341} // namespace afw
342} // namespace lsst
char * data
Definition: BaseRecord.cc:61
table::Key< int > detector
#define LOG_GET(logger)
Implementation of the Photometric Calibration class.
table::Key< table::Array< std::uint8_t > > wcs
Definition: SkyWcs.cc:66
A collection of all the things that make an Exposure different from a MaskedImage.
Definition: ExposureInfo.h:86
bool hasWcs() const
Does this exposure have a Wcs?
Definition: ExposureInfo.cc:54
static int getFitsSerializationVersion()
Get the version of FITS serialization that this ExposureInfo understands.
bool hasCoaddInputs() const
Does this exposure have coadd provenance catalogs?
void setFilterLabel(std::shared_ptr< FilterLabel const > filterLabel)
Set the exposure's filter information.
static typehandling::Key< std::string, std::shared_ptr< geom::SkyWcs const > > const KEY_WCS
Standard key for looking up the Wcs.
Definition: ExposureInfo.h:89
ExposureInfo & operator=(ExposureInfo const &other)
Assignment; shares all components.
void setFilter(Filter const &filter)
Set the exposure's filter.
static typehandling::Key< std::string, std::shared_ptr< cameraGeom::Detector const > > const KEY_DETECTOR
Standard key for looking up the detector information.
Definition: ExposureInfo.h:95
bool hasTransmissionCurve() const
Does this exposure have a transmission curve?
bool hasPsf() const
Does this exposure have a Psf?
Definition: ExposureInfo.cc:61
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< PhotoCalib const > const &photoCalib=std::shared_ptr< PhotoCalib 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.
void setValidPolygon(std::shared_ptr< geom::polygon::Polygon const > polygon)
Set the exposure's valid Polygon.
Definition: ExposureInfo.cc:95
bool hasPhotoCalib() const
Does this exposure have a photometric calibration?
Definition: ExposureInfo.cc:68
bool hasApCorrMap() const
Return true if the exposure has an aperture correction map.
void setWcs(std::shared_ptr< geom::SkyWcs const > wcs)
Set the WCS of the exposure.
Definition: ExposureInfo.cc:56
std::shared_ptr< PhotoCalib const > getPhotoCalib() const
Return the exposure's photometric calibration.
Definition: ExposureInfo.cc:69
bool hasComponent(typehandling::Key< std::string, T > const &key) const
Test whether a generic component is defined.
Definition: ExposureInfo.h:275
std::shared_ptr< ApCorrMap const > getApCorrMap() const
Return the exposure's aperture correction map (null pointer if !hasApCorrMap())
std::shared_ptr< FilterLabel const > getFilterLabel() const
Return the exposure's filter information.
void setApCorrMap(std::shared_ptr< ApCorrMap const > apCorrMap)
Set the exposure's aperture correction map (null pointer if !hasApCorrMap())
void setPhotoCalib(std::shared_ptr< PhotoCalib const > photoCalib)
Set the Exposure's PhotoCalib object.
Definition: ExposureInfo.cc:72
Filter getFilter() const
Return the exposure's filter.
static typehandling::Key< std::string, std::shared_ptr< geom::polygon::Polygon const > > const KEY_VALID_POLYGON
Standard key for looking up the valid polygon.
Definition: ExposureInfo.h:98
void setComponent(typehandling::Key< std::string, std::shared_ptr< T > > const &key, std::shared_ptr< T > const &object)
Add a generic component to the ExposureInfo.
Definition: ExposureInfo.h:255
bool hasValidPolygon() const
Does this exposure have a valid Polygon.
Definition: ExposureInfo.cc:91
std::shared_ptr< geom::SkyWcs const > getWcs() const
Return the WCS of the exposure.
Definition: ExposureInfo.cc:55
std::shared_ptr< T > getComponent(typehandling::Key< std::string, std::shared_ptr< T > > const &key) const
Retrieve a generic component from the ExposureInfo.
Definition: ExposureInfo.h:289
static typehandling::Key< std::string, std::shared_ptr< PhotoCalib const > > const KEY_PHOTO_CALIB
Standard key for looking up the photometric calibration.
Definition: ExposureInfo.h:93
void setCoaddInputs(std::shared_ptr< CoaddInputs const > coaddInputs)
Set the exposure's coadd provenance catalogs.
std::shared_ptr< cameraGeom::Detector const > getDetector() const
Return the exposure's Detector information.
Definition: ExposureInfo.cc:80
bool hasFilterLabel() const
Does this exposure have filter information?
void setDetector(std::shared_ptr< cameraGeom::Detector const > detector)
Set the exposure's Detector information.
Definition: ExposureInfo.cc:83
void setPsf(std::shared_ptr< detection::Psf const > psf)
Set the exposure's point-spread function.
Definition: ExposureInfo.cc:63
static typehandling::Key< std::string, std::shared_ptr< TransmissionCurve const > > const KEY_TRANSMISSION_CURVE
Standard key for looking up the transmission curve.
Definition: ExposureInfo.h:105
void initApCorrMap()
Set the exposure's aperture correction map to a new, empty map.
static typehandling::Key< std::string, std::shared_ptr< CoaddInputs const > > const KEY_COADD_INPUTS
Standard key for looking up coadd provenance catalogs.
Definition: ExposureInfo.h:100
static typehandling::Key< std::string, std::shared_ptr< detection::Psf const > > const KEY_PSF
Standard key for looking up the point-spread function.
Definition: ExposureInfo.h:91
std::shared_ptr< CoaddInputs const > getCoaddInputs() const
Return a pair of catalogs that record the inputs, if this Exposure is a coadd (otherwise null).
std::shared_ptr< geom::polygon::Polygon const > getValidPolygon() const
Return the valid Polygon.
Definition: ExposureInfo.cc:92
std::shared_ptr< daf::base::PropertySet > getMetadata() const
Return flexible metadata.
Definition: ExposureInfo.h:163
bool hasDetector() const
Does this exposure have Detector information?
Definition: ExposureInfo.cc:79
static typehandling::Key< std::string, std::shared_ptr< ApCorrMap const > > const KEY_AP_CORR_MAP
Standard key for looking up the aperture correction map.
Definition: ExposureInfo.h:102
static std::string const & getFitsSerializationVersionName()
Get the version of FITS serialization version info name.
std::shared_ptr< image::VisitInfo const > getVisitInfo() const
Return the exposure's visit info.
Definition: ExposureInfo.h:213
static typehandling::Key< std::string, std::shared_ptr< FilterLabel const > > const KEY_FILTER
Standard key for looking up filter information.
Definition: ExposureInfo.h:107
void setTransmissionCurve(std::shared_ptr< TransmissionCurve const > tc)
Set the exposure's transmission curve.
std::shared_ptr< detection::Psf const > getPsf() const
Return the exposure's point-spread function.
Definition: ExposureInfo.cc:62
std::shared_ptr< TransmissionCurve const > getTransmissionCurve() const
Return the exposure's transmission curve.
A group of labels for a filter in an exposure or coadd.
Definition: FilterLabel.h:58
A map of Storable supporting strongly-typed access.
Definition: StorableMap.h:59
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
Key for type-safe lookup in a GenericMap.
Definition: Key.h:52
void setVisitInfoMetadata(daf::base::PropertyList &metadata, VisitInfo const &visitInfo)
Set FITS metadata from a VisitInfo.
Definition: VisitInfo.cc:356
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Filter makeFilter(FilterLabel const &label)
Convert a FilterLabel back to an old-style Filter.
std::shared_ptr< FilterLabel > makeFilterLabel(Filter const &filter)
Convert an old-style Filter to a FilterLabel.
std::string const & getName() const noexcept
Return a filter's name.
Definition: Filter.h:78
Extent< double, 2 > Extent2D
A base class for image defects.
STL namespace.
Key< int > psf
Definition: Exposure.cc:65
Key< int > visitInfo
Definition: Exposure.cc:70
Key< int > photoCalib
Definition: Exposure.cc:67
Key< int > transmissionCurve
Definition: Exposure.cc:71
Key< int > apCorrMap
Definition: Exposure.cc:68