LSST Applications g0b6bd0c080+a72a5dd7e6,g1182afd7b4+2a019aa3bb,g17e5ecfddb+2b8207f7de,g1d67935e3f+06cf436103,g38293774b4+ac198e9f13,g396055baef+6a2097e274,g3b44f30a73+6611e0205b,g480783c3b1+98f8679e14,g48ccf36440+89c08d0516,g4b93dc025c+98f8679e14,g5c4744a4d9+a302e8c7f0,g613e996a0d+e1c447f2e0,g6c8d09e9e7+25247a063c,g7271f0639c+98f8679e14,g7a9cd813b8+124095ede6,g9d27549199+a302e8c7f0,ga1cf026fa3+ac198e9f13,ga32aa97882+7403ac30ac,ga786bb30fb+7a139211af,gaa63f70f4e+9994eb9896,gabf319e997+ade567573c,gba47b54d5d+94dc90c3ea,gbec6a3398f+06cf436103,gc6308e37c7+07dd123edb,gc655b1545f+ade567573c,gcc9029db3c+ab229f5caf,gd01420fc67+06cf436103,gd877ba84e5+06cf436103,gdb4cecd868+6f279b5b48,ge2d134c3d5+cc4dbb2e3f,ge448b5faa6+86d1ceac1d,gecc7e12556+98f8679e14,gf3ee170dca+25247a063c,gf4ac96e456+ade567573c,gf9f5ea5b4d+ac198e9f13,gff490e6085+8c2580be5c,w.2022.27
LSST Data Management Base Package
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("lsst.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
131bool ExposureInfo::hasId() const noexcept { return _exposureId.has_value(); }
132
134 if (_exposureId) {
135 return *_exposureId;
136 } else {
137 throw LSST_EXCEPT(lsst::pex::exceptions::NotFoundError, "Exposure does not have an ID.");
138 }
139}
140
142 _exposureId = id;
143 // Ensure consistency with VisitInfo::getExposureId() until the latter is removed in DM-32138.
145 if (oldVisitInfo) {
146 auto newVisitInfo = std::make_shared<VisitInfo>(
147 *_exposureId, oldVisitInfo->getExposureTime(), oldVisitInfo->getDarkTime(),
148 oldVisitInfo->getDate(), oldVisitInfo->getUt1(), oldVisitInfo->getEra(),
149 oldVisitInfo->getBoresightRaDec(), oldVisitInfo->getBoresightAzAlt(),
150 oldVisitInfo->getBoresightAirmass(), oldVisitInfo->getBoresightRotAngle(),
151 oldVisitInfo->getRotType(), oldVisitInfo->getObservatory(), oldVisitInfo->getWeather(),
152 oldVisitInfo->getInstrumentLabel(), oldVisitInfo->getId());
153 // Do not call setVisitInfo, to avoid recursion
154 _visitInfo = newVisitInfo;
155 }
156}
157
158void ExposureInfo::clearId() noexcept { _exposureId.reset(); }
159
160
162 typehandling::makeKey<std::shared_ptr<FilterLabel const>>("FILTER"s);
163bool ExposureInfo::hasFilterLabel() const { return hasFilter(); }
166 setFilter(label);
167}
171 setComponent(KEY_FILTER, label);
172}
173
175 // Version history:
176 // unversioned and 0: photometric calibration via Calib, WCS via SkyWcs using AST.
177 // 1: photometric calibration via PhotoCalib, generic components
178 // 2: remove Filter, replaced with (generic) FilterLabel
179 static int const version = 2;
180 return version;
181}
182
184 static std::string const versionName("EXPINFO_V");
185 return versionName;
186}
187
188// Clone various components; defined here so that we don't have to expose their insides in Exposure.h
189
191 if (apCorrMap) {
192 return std::make_shared<ApCorrMap>(*apCorrMap);
193 }
195}
196
203 std::shared_ptr<CoaddInputs> const& coaddInputs,
207 : _exposureId(),
208 _metadata(metadata ? metadata
209 : std::shared_ptr<daf::base::PropertySet>(new daf::base::PropertyList())),
210 _visitInfo(visitInfo),
211 _components(std::make_unique<MapClass>()) {
212 setWcs(wcs);
213 setPsf(psf);
216 setValidPolygon(polygon);
217 setCoaddInputs(coaddInputs);
218 setApCorrMap(_cloneApCorrMap(apCorrMap));
220}
221
223
224// Delegate to copy-constructor for backwards compatibility
226
227ExposureInfo::ExposureInfo(ExposureInfo const& other, bool copyMetadata)
228 : _exposureId(other._exposureId),
229 _metadata(other._metadata),
230 _visitInfo(other._visitInfo),
231 // ExposureInfos can (historically) share objects, but should each have their own pointers to them
232 _components(std::make_unique<MapClass>(*(other._components))) {
233 if (copyMetadata) _metadata = _metadata->deepCopy();
234}
235
237 if (&other != this) {
238 _exposureId = other._exposureId;
239 _metadata = other._metadata;
240 _visitInfo = other._visitInfo;
241 // ExposureInfos can (historically) share objects, but should each have their own pointers to them
242 _components = std::make_unique<MapClass>(*(other._components));
243 }
244 return *this;
245}
246// Delegate to copy-assignment for backwards compatibility
247ExposureInfo& ExposureInfo::operator=(ExposureInfo&& other) { return *this = other; }
248
249void ExposureInfo::initApCorrMap() { setApCorrMap(std::make_shared<ApCorrMap>()); }
250
252
253int ExposureInfo::_addToArchive(FitsWriteData& data, table::io::Persistable const& object, std::string key,
254 std::string comment) {
255 int componentId = data.archive.put(object);
256 data.metadata->set(key, componentId, comment);
257 return componentId;
258}
259
260int ExposureInfo::_addToArchive(FitsWriteData& data,
262 std::string comment) {
263 // Don't delegate to Persistable const& version because OutputArchive::put
264 // has special handling of shared_ptr
265 int componentId = data.archive.put(object);
266 data.metadata->set(key, componentId, comment);
267 return componentId;
268}
269
270// Standardized strings for _startWriteFits
271namespace {
272std::string _getOldHeaderKey(std::string mapKey) { return mapKey + "_ID"; }
273std::string _getNewHeaderKey(std::string mapKey) { return "ARCHIVE_ID_" + mapKey; }
274
275std::string _getHeaderComment(std::string mapKey) {
276 return "archive ID for generic component '" + mapKey + "'";
277}
278} // namespace
279
280ExposureInfo::FitsWriteData ExposureInfo::_startWriteFits(lsst::geom::Point2I const& xy0) const {
281 FitsWriteData data;
282
283 data.metadata.reset(new daf::base::PropertyList());
284 data.imageMetadata.reset(new daf::base::PropertyList());
285 data.maskMetadata = data.imageMetadata;
286 data.varianceMetadata = data.imageMetadata;
287
288 data.metadata->combine(*getMetadata());
289
291
292 // In the future, we might not have exactly three image HDUs, but we always do right now,
293 // so 0=primary, 1=image, 2=mask, 3=variance, 4+=archive
294 //
295 // Historically the AR_HDU keyword was 1-indexed (see RFC-304), and to maintain file compatibility
296 // this is still the case so we're setting AR_HDU to 5 == 4 + 1
297 //
298 data.metadata->set("AR_HDU", 5, "HDU (1-indexed) containing the archive used to store ancillary objects");
299 for (auto const& keyValue : *_components) {
300 std::string const& key = keyValue.first.getId();
301 std::shared_ptr<typehandling::Storable const> const& object = keyValue.second;
302
303 if (object && object->isPersistable()) {
304 std::string comment = _getHeaderComment(key);
305 // Store archive ID in two header keys:
306 // - old-style key for backwards compatibility,
307 // - and new-style key because it's much safer to parse
308 int id = _addToArchive(data, object, _getOldHeaderKey(key), comment);
309 data.metadata->set(_getNewHeaderKey(key), id, comment);
310 }
311 }
312
313 // LSST convention is that Wcs is in pixel coordinates (i.e relative to bottom left
314 // corner of parent image, if any). The Wcs/Fits convention is that the Wcs is in
315 // image coordinates. When saving an image we convert from pixel to index coordinates.
316 // In the case where this image is a parent image, the reference pixels are unchanged
317 // by this transformation
318 if (hasWcs() && getWcs()->isFits()) {
319 // Try to save the WCS as FITS-WCS metadata; if an exact representation
320 // is not possible then skip it
321 auto shift = lsst::geom::Extent2D(lsst::geom::Point2I(0, 0) - xy0);
322 auto newWcs = getWcs()->copyAtShiftedPixelOrigin(shift);
324 try {
325 wcsMetadata = newWcs->getFitsMetadata(true);
326 } catch (pex::exceptions::RuntimeError const&) {
327 // cannot represent this WCS as FITS-WCS; don't write its metadata
328 }
329 if (wcsMetadata) {
330 data.imageMetadata->combine(*newWcs->getFitsMetadata(true));
331 }
332 }
333
334 // For the sake of ds9, store _x0 and _y0 as -LTV1, -LTV2.
335 // This is in addition to saving _x0 and _y0 as WCS A, which is done elsewhere
336 // and is what LSST uses to read _x0 and _y0.
337 // LTV is a convention used by STScI (see \S2.6.2 of HST Data Handbook for STIS, version 5.0
338 // http://www.stsci.edu/hst/stis/documents/handbooks/currentDHB/ch2_stis_data7.html#429287)
339 // and recognized by ds9.
340 data.imageMetadata->set("LTV1", static_cast<double>(-xy0.getX()));
341 data.imageMetadata->set("LTV2", static_cast<double>(-xy0.getY()));
342
343 if (hasDetector()) {
344 data.metadata->set("DETNAME", getDetector()->getName());
345 data.metadata->set("DETSER", getDetector()->getSerial());
346 }
347
348 auto visitInfoPtr = getVisitInfo();
349 if (visitInfoPtr) {
350 detail::setVisitInfoMetadata(*(data.metadata), *visitInfoPtr);
351 }
352
353 // So long as VisitInfo also writes exposureId (until DM-32138), let ours take precedence.
354 if (hasId()) {
355 data.metadata->set("EXPID", getId());
356 }
357
358 return data;
359}
360
361void ExposureInfo::_finishWriteFits(fits::Fits& fitsfile, FitsWriteData const& data) const {
362 data.archive.writeFits(fitsfile);
363}
364
365} // namespace image
366} // namespace afw
367} // namespace lsst
char * data
Definition: BaseRecord.cc:61
table::Key< int > id
Definition: Detector.cc:162
table::Key< int > detector
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
LSST DM logging module built on log4cxx.
#define LOG_GET(logger)
Returns a Log object associated with logger.
Definition: Log.h:75
#define LOG_LOGGER
Definition: Log.h:714
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:88
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:91
ExposureInfo & operator=(ExposureInfo const &other)
Assignment; shares all components.
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:97
bool hasTransmissionCurve() const
Does this exposure have a transmission curve?
bool hasPsf() const
Does this exposure have a Psf?
Definition: ExposureInfo.cc:61
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:319
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
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:100
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:299
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:333
std::shared_ptr< FilterLabel const > getFilter() const
Return the exposure's filter information.
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:95
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 clearId() noexcept
Unset the exposure ID, if any.
void setDetector(std::shared_ptr< cameraGeom::Detector const > detector)
Set the exposure's Detector information.
Definition: ExposureInfo.cc:83
void setFilter(std::shared_ptr< FilterLabel const > filter)
Set the exposure's filter information.
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:107
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:102
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:93
std::shared_ptr< CoaddInputs const > getCoaddInputs() const
Return a pair of catalogs that record the inputs, if this Exposure is a coadd (otherwise null).
bool hasFilter() const
Does this exposure have filter information?
table::RecordId getId() const
Return the exposure ID.
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:198
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 >(), 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.
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:104
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:248
static typehandling::Key< std::string, std::shared_ptr< FilterLabel const > > const KEY_FILTER
Standard key for looking up filter information.
Definition: ExposureInfo.h:109
void setId(table::RecordId id)
Set the exposure ID.
bool hasId() const noexcept
Does this Exposure have an exposure id?
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 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
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
void setVisitInfoMetadata(daf::base::PropertyList &metadata, VisitInfo const &visitInfo)
Set FITS metadata from a VisitInfo.
Definition: VisitInfo.cc:363
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Extent< double, 2 > Extent2D
Definition: Extent.h:400
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