24 #include "lsst/pex/exceptions.h"
25 #include "lsst/log/Log.h"
36 LOG_LOGGER _log = LOG_GET(
"afw.image.ExposureInfo");
48 if (metadata.exists(name)) {
49 r = metadata.get<
int>(
name);
50 metadata.remove(name);
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>();
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>();
69 std::shared_ptr<ApCorrMap> ExposureInfo::_cloneApCorrMap(std::shared_ptr<ApCorrMap const> apCorrMap) {
71 return std::make_shared<ApCorrMap>(*apCorrMap);
73 return std::shared_ptr<ApCorrMap>();
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())),
89 _validPolygon(polygon),
93 _coaddInputs(coaddInputs),
94 _apCorrMap(_cloneApCorrMap(apCorrMap)),
95 _visitInfo(visitInfo) {}
98 : _wcs(_cloneWcs(other._wcs)),
100 _calib(_cloneCalib(other._calib)),
101 _detector(other._detector),
102 _validPolygon(other._validPolygon),
103 _filter(other._filter),
105 _coaddInputs(other._coaddInputs),
106 _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
107 _visitInfo(other._visitInfo) {}
110 : _wcs(_cloneWcs(other._wcs)),
112 _calib(_cloneCalib(other._calib)),
113 _detector(other._detector),
114 _validPolygon(other._validPolygon),
115 _filter(other._filter),
117 _coaddInputs(other._coaddInputs),
118 _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
119 _visitInfo(other._visitInfo) {
120 if (copyMetadata) _metadata = _metadata->deepCopy();
124 if (&other !=
this) {
125 _wcs = _cloneWcs(other._wcs);
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;
143 ExposureInfo::FitsWriteData ExposureInfo::_startWriteFits(
afw::geom::Point2I const& xy0)
const {
148 data.maskMetadata = data.imageMetadata;
149 data.varianceMetadata = data.imageMetadata;
159 data.metadata->set(
"AR_HDU", 5,
"HDU (1-indexed) containing the archive used to store ancillary objects");
162 data.metadata->set(
"COADD_INPUTS_ID", coaddInputsId,
"archive ID for coadd inputs catalogs");
166 data.metadata->set(
"AP_CORR_MAP_ID", apCorrMapId,
"archive ID for aperture correction map");
169 int psfId = data.archive.put(
getPsf());
170 data.metadata->set(
"PSF_ID", psfId,
"archive ID for the Exposure's main Psf");
173 int wcsId = data.archive.put(
getWcs());
174 data.metadata->set(
"WCS_ID", wcsId,
"archive ID for the Exposure's main Wcs");
178 data.metadata->set(
"VALID_POLYGON_ID", polygonId,
"archive ID for the Exposure's valid polygon");
187 std::shared_ptr<Wcs> newWcs =
getWcs()->clone();
188 newWcs->shiftReferencePixel(-xy0.getX(), -xy0.getY());
191 data.imageMetadata->combine(newWcs->getFitsMetadata());
205 data.imageMetadata->set(
"LTV1", static_cast<double>(-xy0.getX()));
206 data.imageMetadata->set(
"LTV2", static_cast<double>(-xy0.getY()));
208 data.metadata->set(
"FILTER",
getFilter().getName());
210 data.metadata->set(
"DETNAME",
getDetector()->getName());
211 data.metadata->set(
"DETSER",
getDetector()->getSerial());
222 data.metadata->set(
"FLUXMAG0",
getCalib()->getFluxMag0().
first);
223 data.metadata->set(
"FLUXMAG0ERR",
getCalib()->getFluxMag0().
second);
228 void ExposureInfo::_finishWriteFits(fits::Fits& fitsfile, FitsWriteData
const& data)
const {
229 data.archive.writeFits(fitsfile);
232 void ExposureInfo::_readFits(fits::Fits& fitsfile, std::shared_ptr<daf::base::PropertySet> metadata,
233 std::shared_ptr<daf::base::PropertySet> imageMetadata) {
235 _wcs =
makeWcs(imageMetadata,
true);
237 if (!imageMetadata->exists(
"INHERIT")) {
243 metadata = imageMetadata;
246 _filter = Filter(metadata,
true);
249 _visitInfo = std::shared_ptr<VisitInfo const>(
new VisitInfo(*metadata));
252 std::shared_ptr<Calib> newCalib(
new Calib(metadata));
256 int archiveHdu = popInt(*metadata,
"AR_HDU");
261 fitsfile.setHdu(archiveHdu);
267 int psfId = popInt(*metadata,
"PSF_ID");
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());
273 int wcsId = popInt(*metadata,
"WCS_ID");
275 auto archiveWcs = archive.get<Wcs>(wcsId);
279 LOGLS_DEBUG(_log,
"Null WCS seen in binary table");
281 }
catch (pex::exceptions::NotFoundError& err) {
282 auto msg = str(boost::format(
"Could not read WCS extension; setting to null: %s") % err.what());
284 msg +=
" ; using WCS from FITS header";
286 LOGLS_WARN(_log, msg);
288 int coaddInputsId = popInt(*metadata,
"COADD_INPUTS_ID");
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());
294 int apCorrMapId = popInt(*metadata,
"AP_CORR_MAP_ID");
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());
300 int validPolygonId = popInt(*metadata,
"VALID_POLYGON_ID");
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());
308 _metadata = metadata;
std::shared_ptr< Wcs > getWcs()
Return the coordinate system of the exposure.
std::shared_ptr< detection::Psf > getPsf() const
Return the exposure's point-spread function.
std::shared_ptr< ApCorrMap > getApCorrMap()
Return the exposure's aperture correction map (null pointer if !hasApCorrMap())
int stripFilterKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Filter-related keywords from the metadata.
bool hasValidPolygon() const
Does this exposure have a valid Polygon.
void setCalib(std::shared_ptr< Calib const > calib)
Set the Exposure's Calib object.
bool hasCoaddInputs() const
Does this exposure have coadd provenance catalogs?
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.
Describe an exposure's calibration.
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.
std::shared_ptr< daf::base::PropertySet > getMetadata() const
Return flexible metadata.
void initApCorrMap()
Set the exposure's aperture correction map to a new, empty map.
table::Key< std::string > name
int stripCalibKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Calib-related keywords from the metadata.
std::shared_ptr< cameraGeom::Detector const > getDetector() const
Return the exposure's Detector information.
Holds an integer identifier for an LSST filter.
Filter getFilter() const
Return the exposure's filter.
bool hasPsf() const
Does this exposure have a Psf?
std::shared_ptr< Calib > getCalib()
Return the exposure's photometric calibration.
bool hasWcs() const
Does this exposure have a Wcs?
lsst::daf::base::PropertySet PropertySet
int stripVisitInfoKeywords(daf::base::PropertySet &metadata)
Remove VisitInfo-related keywords from the metadata.
bool hasApCorrMap() const
Return true if the exposure has an aperture correction map.
A collection of all the things that make an Exposure different from a MaskedImage.
std::shared_ptr< geom::polygon::Polygon const > getValidPolygon() const
Return the valid Polygon.
daf::base::PropertySet & _metadata
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's visit info.
bool hasDetector() const
Does this exposure have Detector information?
lsst::daf::base::PropertyList PropertyList
std::shared_ptr< CoaddInputs > getCoaddInputs() const
Return a pair of catalogs that record the inputs, if this Exposure is a coadd (otherwise null)...