LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
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"
30 #include "lsst/afw/geom/SkyWcs.h"
32 #include "lsst/afw/detection/Psf.h"
35 #include "lsst/afw/fits.h"
36 
37 using namespace std::string_literals;
38 
39 namespace {
40 LOG_LOGGER _log = LOG_GET("afw.image.ExposureInfo");
41 
43 } // namespace
44 
45 namespace lsst {
46 namespace afw {
47 namespace image {
48 
49 // Important: do *not* change the keys' strings; doing so will break compatibility with old FITS files
50 
51 typehandling::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 
54 bool ExposureInfo::hasWcs() const { return hasComponent(KEY_WCS); }
55 std::shared_ptr<geom::SkyWcs const> ExposureInfo::getWcs() const { return getComponent(KEY_WCS); }
56 void ExposureInfo::setWcs(std::shared_ptr<geom::SkyWcs const> wcs) { setComponent(KEY_WCS, wcs); }
57 
59  typehandling::makeKey<std::shared_ptr<detection::Psf const>>("PSF"s);
60 
61 bool ExposureInfo::hasPsf() const { return hasComponent(KEY_PSF); }
62 std::shared_ptr<detection::Psf const> ExposureInfo::getPsf() const { return getComponent(KEY_PSF); }
63 void ExposureInfo::setPsf(std::shared_ptr<detection::Psf const> psf) { setComponent(KEY_PSF, psf); }
64 
65 typehandling::Key<std::string, std::shared_ptr<PhotoCalib const>> const ExposureInfo::KEY_PHOTO_CALIB =
66  typehandling::makeKey<std::shared_ptr<PhotoCalib const>>("PHOTOCALIB"s);
67 
68 bool ExposureInfo::hasPhotoCalib() const { return hasComponent(KEY_PHOTO_CALIB); }
69 std::shared_ptr<PhotoCalib const> ExposureInfo::getPhotoCalib() const {
70  return getComponent(KEY_PHOTO_CALIB);
71 }
72 void ExposureInfo::setPhotoCalib(std::shared_ptr<PhotoCalib const> photoCalib) {
73  setComponent(KEY_PHOTO_CALIB, photoCalib);
74 }
75 
77  typehandling::makeKey<std::shared_ptr<cameraGeom::Detector const>>("DETECTOR"s);
78 
79 bool ExposureInfo::hasDetector() const { return hasComponent(KEY_DETECTOR); }
80 std::shared_ptr<cameraGeom::Detector const> ExposureInfo::getDetector() const {
81  return getComponent(KEY_DETECTOR);
82 }
83 void ExposureInfo::setDetector(std::shared_ptr<cameraGeom::Detector const> detector) {
84  setComponent(KEY_DETECTOR, detector);
85 }
86 
88  ExposureInfo::KEY_VALID_POLYGON =
89  typehandling::makeKey<std::shared_ptr<geom::polygon::Polygon const>>("VALID_POLYGON"s);
90 
91 bool ExposureInfo::hasValidPolygon() const { return hasComponent(KEY_VALID_POLYGON); }
92 std::shared_ptr<geom::polygon::Polygon const> ExposureInfo::getValidPolygon() const {
93  return getComponent(KEY_VALID_POLYGON);
94 }
95 void ExposureInfo::setValidPolygon(std::shared_ptr<geom::polygon::Polygon const> polygon) {
96  setComponent(KEY_VALID_POLYGON, polygon);
97 }
98 
99 typehandling::Key<std::string, std::shared_ptr<CoaddInputs const>> const ExposureInfo::KEY_COADD_INPUTS =
100  typehandling::makeKey<std::shared_ptr<CoaddInputs const>>("COADD_INPUTS"s);
101 
102 bool ExposureInfo::hasCoaddInputs() const { return hasComponent(KEY_COADD_INPUTS); }
103 void ExposureInfo::setCoaddInputs(std::shared_ptr<CoaddInputs const> coaddInputs) {
104  setComponent(KEY_COADD_INPUTS, coaddInputs);
105 }
106 std::shared_ptr<CoaddInputs const> ExposureInfo::getCoaddInputs() const {
107  return getComponent(KEY_COADD_INPUTS);
108 }
109 
110 typehandling::Key<std::string, std::shared_ptr<ApCorrMap const>> const ExposureInfo::KEY_AP_CORR_MAP =
111  typehandling::makeKey<std::shared_ptr<ApCorrMap const>>("AP_CORR_MAP"s);
112 
113 bool ExposureInfo::hasApCorrMap() const { return hasComponent(KEY_AP_CORR_MAP); }
114 std::shared_ptr<ApCorrMap const> ExposureInfo::getApCorrMap() const { return getComponent(KEY_AP_CORR_MAP); }
115 void ExposureInfo::setApCorrMap(std::shared_ptr<ApCorrMap const> apCorrMap) {
116  setComponent(KEY_AP_CORR_MAP, apCorrMap);
117 }
118 
120  ExposureInfo::KEY_TRANSMISSION_CURVE =
121  typehandling::makeKey<std::shared_ptr<TransmissionCurve const>>("TRANSMISSION_CURVE"s);
122 
123 bool ExposureInfo::hasTransmissionCurve() const { return hasComponent(KEY_TRANSMISSION_CURVE); }
124 std::shared_ptr<TransmissionCurve const> ExposureInfo::getTransmissionCurve() const {
125  return getComponent(KEY_TRANSMISSION_CURVE);
126 }
127 void ExposureInfo::setTransmissionCurve(std::shared_ptr<TransmissionCurve const> tc) {
128  setComponent(KEY_TRANSMISSION_CURVE, tc);
129 }
130 
131 int ExposureInfo::getFitsSerializationVersion() {
132  // Version history:
133  // unversioned and 0: photometric calibration via Calib, WCS via SkyWcs using AST.
134  // 1: photometric calibration via PhotoCalib
135  static int const version = 1;
136  return version;
137 }
138 
139 std::string const& ExposureInfo::getFitsSerializationVersionName() {
140  static std::string const versionName("EXPINFO_V");
141  return versionName;
142 }
143 
144 // Clone various components; defined here so that we don't have to expose their insides in Exposure.h
145 
147  if (apCorrMap) {
148  return std::make_shared<ApCorrMap>(*apCorrMap);
149  }
151 }
152 
153 ExposureInfo::ExposureInfo(std::shared_ptr<geom::SkyWcs const> const& wcs,
159  std::shared_ptr<CoaddInputs> const& coaddInputs,
163  : _filter(filter),
164  _metadata(metadata ? metadata
165  : std::shared_ptr<daf::base::PropertySet>(new daf::base::PropertyList())),
166  _visitInfo(visitInfo),
167  _components(std::make_unique<MapClass>()) {
168  setWcs(wcs);
169  setPsf(psf);
172  setValidPolygon(polygon);
173  setCoaddInputs(coaddInputs);
174  setApCorrMap(_cloneApCorrMap(apCorrMap));
176 }
177 
179 
180 // Delegate to copy-constructor for backwards compatibility
182 
184  : _filter(other._filter),
185  _metadata(other._metadata),
186  _visitInfo(other._visitInfo),
187  // ExposureInfos can (historically) share objects, but should each have their own pointers to them
188  _components(std::make_unique<MapClass>(*(other._components))) {
189  if (copyMetadata) _metadata = _metadata->deepCopy();
190 }
191 
193  if (&other != this) {
194  _filter = other._filter;
195  _metadata = other._metadata;
196  _visitInfo = other._visitInfo;
197  // ExposureInfos can (historically) share objects, but should each have their own pointers to them
198  _components = std::make_unique<MapClass>(*(other._components));
199  }
200  return *this;
201 }
202 // Delegate to copy-assignment for backwards compatibility
204 
205 void ExposureInfo::initApCorrMap() { setApCorrMap(std::make_shared<ApCorrMap>()); }
206 
207 ExposureInfo::~ExposureInfo() = default;
208 
209 int ExposureInfo::_addToArchive(FitsWriteData& data, table::io::Persistable const& object, std::string key,
210  std::string comment) {
211  int componentId = data.archive.put(object);
212  data.metadata->set(key, componentId, comment);
213  return componentId;
214 }
215 
216 int ExposureInfo::_addToArchive(FitsWriteData& data,
218  std::string comment) {
219  // Don't delegate to Persistable const& version because OutputArchive::put
220  // has special handling of shared_ptr
221  int componentId = data.archive.put(object);
222  data.metadata->set(key, componentId, comment);
223  return componentId;
224 }
225 
226 // Standardized strings for _startWriteFits
227 namespace {
228 std::string _getOldHeaderKey(std::string mapKey) { return mapKey + "_ID"; }
229 std::string _getNewHeaderKey(std::string mapKey) { return "ARCHIVE_ID_" + mapKey; }
230 
231 std::string _getHeaderComment(std::string mapKey) {
232  return "archive ID for generic component '" + mapKey + "'";
233 }
234 } // namespace
235 
236 ExposureInfo::FitsWriteData ExposureInfo::_startWriteFits(lsst::geom::Point2I const& xy0) const {
237  FitsWriteData data;
238 
239  data.metadata.reset(new daf::base::PropertyList());
240  data.imageMetadata.reset(new daf::base::PropertyList());
241  data.maskMetadata = data.imageMetadata;
242  data.varianceMetadata = data.imageMetadata;
243 
244  data.metadata->combine(getMetadata());
245 
247 
248  // In the future, we might not have exactly three image HDUs, but we always do right now,
249  // so 0=primary, 1=image, 2=mask, 3=variance, 4+=archive
250  //
251  // Historically the AR_HDU keyword was 1-indexed (see RFC-304), and to maintain file compatibility
252  // this is still the case so we're setting AR_HDU to 5 == 4 + 1
253  //
254  data.metadata->set("AR_HDU", 5, "HDU (1-indexed) containing the archive used to store ancillary objects");
255  for (auto const& keyValue : *_components) {
256  std::string const& key = keyValue.first.getId();
257  std::shared_ptr<typehandling::Storable const> const& object = keyValue.second;
258 
259  if (object && object->isPersistable()) {
260  std::string comment = _getHeaderComment(key);
261  // Store archive ID in two header keys:
262  // - old-style key for backwards compatibility,
263  // - and new-style key because it's much safer to parse
264  int id = _addToArchive(data, object, _getOldHeaderKey(key), comment);
265  data.metadata->set(_getNewHeaderKey(key), id, comment);
266  }
267  }
268 
269  // LSST convention is that Wcs is in pixel coordinates (i.e relative to bottom left
270  // corner of parent image, if any). The Wcs/Fits convention is that the Wcs is in
271  // image coordinates. When saving an image we convert from pixel to index coordinates.
272  // In the case where this image is a parent image, the reference pixels are unchanged
273  // by this transformation
274  if (hasWcs() && getWcs()->isFits()) {
275  // Try to save the WCS as FITS-WCS metadata; if an exact representation
276  // is not possible then skip it
277  auto shift = lsst::geom::Extent2D(lsst::geom::Point2I(0, 0) - xy0);
278  auto newWcs = getWcs()->copyAtShiftedPixelOrigin(shift);
280  try {
281  wcsMetadata = newWcs->getFitsMetadata(true);
282  } catch (pex::exceptions::RuntimeError const&) {
283  // cannot represent this WCS as FITS-WCS; don't write its metadata
284  }
285  if (wcsMetadata) {
286  data.imageMetadata->combine(newWcs->getFitsMetadata(true));
287  }
288  }
289 
290  // For the sake of ds9, store _x0 and _y0 as -LTV1, -LTV2.
291  // This is in addition to saving _x0 and _y0 as WCS A, which is done elsewhere
292  // and is what LSST uses to read _x0 and _y0.
293  // LTV is a convention used by STScI (see \S2.6.2 of HST Data Handbook for STIS, version 5.0
294  // http://www.stsci.edu/hst/stis/documents/handbooks/currentDHB/ch2_stis_data7.html#429287)
295  // and recognized by ds9.
296  data.imageMetadata->set("LTV1", static_cast<double>(-xy0.getX()));
297  data.imageMetadata->set("LTV2", static_cast<double>(-xy0.getY()));
298 
299  data.metadata->set("FILTER", getFilter().getName());
300  if (hasDetector()) {
301  data.metadata->set("DETNAME", getDetector()->getName());
302  data.metadata->set("DETSER", getDetector()->getSerial());
303  }
304 
305  auto visitInfoPtr = getVisitInfo();
306  if (visitInfoPtr) {
307  detail::setVisitInfoMetadata(*(data.metadata), *visitInfoPtr);
308  }
309 
310  return data;
311 }
312 
313 void ExposureInfo::_finishWriteFits(fits::Fits& fitsfile, FitsWriteData const& data) const {
314  data.archive.writeFits(fitsfile);
315 }
316 
317 } // namespace image
318 } // namespace afw
319 } // namespace lsst
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
LOG_LOGGER
#define LOG_LOGGER
Definition: Log.h:703
lsst::afw::image::ExposureInfo::setWcs
void setWcs(std::shared_ptr< geom::SkyWcs const > wcs)
Set the WCS of the exposure.
Definition: ExposureInfo.cc:56
std::string
STL class.
std::shared_ptr
STL class.
Psf.h
lsst.pipe.tasks.assembleCoadd.filter
filter
Definition: assembleCoadd.py:342
TransmissionCurve.h
version
table::Key< int > version
Definition: PhotoCalib.cc:374
wcs
table::Key< table::Array< std::uint8_t > > wcs
Definition: SkyWcs.cc:71
lsst::afw::image::ExposureInfo::setTransmissionCurve
void setTransmissionCurve(std::shared_ptr< TransmissionCurve const > tc)
Set the exposure's transmission curve.
Definition: ExposureInfo.cc:127
transmissionCurve
Key< int > transmissionCurve
Definition: Exposure.cc:71
lsst::afw::image::detail::setVisitInfoMetadata
void setVisitInfoMetadata(daf::base::PropertyList &metadata, VisitInfo const &visitInfo)
Set FITS metadata from a VisitInfo.
Definition: VisitInfo.cc:281
lsst::afw::image::Filter
Holds an integer identifier for an LSST filter.
Definition: Filter.h:141
psf
Key< int > psf
Definition: Exposure.cc:65
lsst::afw::image::ExposureInfo::getMetadata
std::shared_ptr< daf::base::PropertySet > getMetadata() const
Return flexible metadata.
Definition: ExposureInfo.h:141
fits.h
lsst::afw
Definition: imageAlgorithm.dox:1
LOG_GET
#define LOG_GET(logger)
Definition: Log.h:75
version
Definition: version.py:1
SkyWcs.h
lsst::afw::image::ExposureInfo::hasDetector
bool hasDetector() const
Does this exposure have Detector information?
Definition: ExposureInfo.cc:79
lsst::afw::image::ExposureInfo::ExposureInfo
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.
Definition: ExposureInfo.cc:153
lsst::afw::image::ExposureInfo::getWcs
std::shared_ptr< geom::SkyWcs const > getWcs() const
Return the WCS of the exposure.
Definition: ExposureInfo.cc:55
lsst::afw::image::ExposureInfo::getFitsSerializationVersion
static int getFitsSerializationVersion()
Get the version of FITS serialization that this ExposureInfo understands.
Definition: ExposureInfo.cc:131
lsst::afw::image::ExposureInfo::setCoaddInputs
void setCoaddInputs(std::shared_ptr< CoaddInputs const > coaddInputs)
Set the exposure's coadd provenance catalogs.
Definition: ExposureInfo.cc:103
lsst::afw::image::ExposureInfo::getFilter
Filter getFilter() const
Return the exposure's filter.
Definition: ExposureInfo.h:126
data
char * data
Definition: BaseRecord.cc:62
lsst::afw::image::ExposureInfo::getDetector
std::shared_ptr< cameraGeom::Detector const > getDetector() const
Return the exposure's Detector information.
Definition: ExposureInfo.cc:80
lsst::afw::image::ExposureInfo::setPsf
void setPsf(std::shared_ptr< detection::Psf const > psf)
Set the exposure's point-spread function.
Definition: ExposureInfo.cc:63
lsst::afw::image::ExposureInfo
A collection of all the things that make an Exposure different from a MaskedImage.
Definition: ExposureInfo.h:86
other
ItemVariant const * other
Definition: Schema.cc:56
lsst::afw::image::ExposureInfo::getFitsSerializationVersionName
static std::string const & getFitsSerializationVersionName()
Get the version of FITS serialization version info name.
Definition: ExposureInfo.cc:139
lsst::afw::image::ExposureInfo::initApCorrMap
void initApCorrMap()
Set the exposure's aperture correction map to a new, empty map.
Definition: ExposureInfo.cc:205
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
Polygon.h
lsst::afw::typehandling::Key
Key for type-safe lookup in a GenericMap.
Definition: Key.h:52
lsst::afw::table::io::Persistable
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
photoCalib
Key< int > photoCalib
Definition: Exposure.cc:67
ExposureInfo.h
lsst::afw::image::ExposureInfo::setDetector
void setDetector(std::shared_ptr< cameraGeom::Detector const > detector)
Set the exposure's Detector information.
Definition: ExposureInfo.cc:83
PhotoCalib.h
Implementation of the Photometric Calibration class.
std
STL namespace.
detector
table::Key< int > detector
Definition: DetectorCollection.cc:172
key
Key< U > key
Definition: Schema.cc:281
lsst::afw::image::ExposureInfo::setValidPolygon
void setValidPolygon(std::shared_ptr< geom::polygon::Polygon const > polygon)
Set the exposure's valid Polygon.
Definition: ExposureInfo.cc:95
lsst::geom::Point< int, 2 >
lsst::afw::image::ExposureInfo::setPhotoCalib
void setPhotoCalib(std::shared_ptr< PhotoCalib const > photoCalib)
Set the Exposure's PhotoCalib object.
Definition: ExposureInfo.cc:72
lsst::geom::Extent2D
Extent< double, 2 > Extent2D
Definition: Extent.h:400
ApCorrMap.h
lsst::afw::image::ExposureInfo::~ExposureInfo
~ExposureInfo()
lsst::afw::image::ExposureInfo::operator=
ExposureInfo & operator=(ExposureInfo const &other)
Assignment; shares all components except the filter.
Definition: ExposureInfo.cc:192
Log.h
LSST DM logging module built on log4cxx.
Detector.h
visitInfo
Key< int > visitInfo
Definition: Exposure.cc:70
lsst::afw::image::ExposureInfo::setApCorrMap
void setApCorrMap(std::shared_ptr< ApCorrMap const > apCorrMap)
Set the exposure's aperture correction map (null pointer if !hasApCorrMap())
Definition: ExposureInfo.cc:115
lsst::afw::image::ExposureInfo::hasWcs
bool hasWcs() const
Does this exposure have a Wcs?
Definition: ExposureInfo.cc:54
lsst::afw::image::ExposureInfo::getVisitInfo
std::shared_ptr< image::VisitInfo const > getVisitInfo() const
Return the exposure's visit info.
Definition: ExposureInfo.h:191
exceptions.h
lsst::afw::image::detail::StorableMap
A map of Storable supporting strongly-typed access.
Definition: StorableMap.h:59
apCorrMap
Key< int > apCorrMap
Definition: Exposure.cc:68