LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
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 } // namespace
39 
40 namespace lsst {
41 namespace afw {
42 namespace image {
43 
45  static int const version = 0;
46  return version;
47 }
48 
50  static std::string const versionName("EXPINFO_V");
51  return versionName;
52 }
53 
54 // Clone various components; defined here so that we don't have to expose their insides in Exposure.h
55 
57  if (calib) return std::shared_ptr<Calib>(new Calib(*calib));
58  return std::shared_ptr<Calib>();
59 }
60 
62  if (apCorrMap) {
63  return std::make_shared<ApCorrMap>(*apCorrMap);
64  }
66 }
67 
70  std::shared_ptr<Calib const> const& calib,
75  std::shared_ptr<ApCorrMap> const& apCorrMap,
78  : _wcs(wcs),
79  _psf(std::const_pointer_cast<detection::Psf>(psf)),
80  _calib(calib ? _cloneCalib(calib) : std::shared_ptr<Calib>(new Calib())),
81  _detector(detector),
82  _validPolygon(polygon),
83  _filter(filter),
84  _metadata(metadata ? metadata
85  : std::shared_ptr<daf::base::PropertySet>(new daf::base::PropertyList())),
86  _coaddInputs(coaddInputs),
87  _apCorrMap(_cloneApCorrMap(apCorrMap)),
88  _visitInfo(visitInfo),
89  _transmissionCurve(transmissionCurve) {}
90 
92  : _wcs(other._wcs),
93  _psf(other._psf),
94  _calib(_cloneCalib(other._calib)),
95  _detector(other._detector),
96  _validPolygon(other._validPolygon),
97  _filter(other._filter),
98  _metadata(other._metadata),
99  _coaddInputs(other._coaddInputs),
100  _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
101  _visitInfo(other._visitInfo),
102  _transmissionCurve(other._transmissionCurve) {}
103 
104 // Delegate to copy-constructor for backwards compatibility
106 
108  : _wcs(other._wcs),
109  _psf(other._psf),
110  _calib(_cloneCalib(other._calib)),
111  _detector(other._detector),
112  _validPolygon(other._validPolygon),
113  _filter(other._filter),
114  _metadata(other._metadata),
115  _coaddInputs(other._coaddInputs),
116  _apCorrMap(_cloneApCorrMap(other._apCorrMap)),
117  _visitInfo(other._visitInfo),
118  _transmissionCurve(other._transmissionCurve) {
119  if (copyMetadata) _metadata = _metadata->deepCopy();
120 }
121 
123  if (&other != this) {
124  _wcs = other._wcs;
125  _psf = other._psf;
126  _calib = _cloneCalib(other._calib);
127  _detector = other._detector;
128  _validPolygon = other._validPolygon;
129  _filter = other._filter;
130  _metadata = other._metadata;
131  _coaddInputs = other._coaddInputs;
132  _apCorrMap = _cloneApCorrMap(other._apCorrMap);
133  _visitInfo = other._visitInfo;
134  _transmissionCurve = other._transmissionCurve;
135  }
136  return *this;
137 }
138 // Delegate to copy-assignment for backwards compatibility
140 
141 void ExposureInfo::initApCorrMap() { _apCorrMap = std::make_shared<ApCorrMap>(); }
142 
143 ExposureInfo::~ExposureInfo() = default;
144 
145 ExposureInfo::FitsWriteData ExposureInfo::_startWriteFits(lsst::geom::Point2I const& xy0) const {
146  FitsWriteData data;
147 
148  data.metadata.reset(new daf::base::PropertyList());
149  data.imageMetadata.reset(new daf::base::PropertyList());
150  data.maskMetadata = data.imageMetadata;
151  data.varianceMetadata = data.imageMetadata;
152 
153  data.metadata->combine(getMetadata());
154 
156 
157  // In the future, we might not have exactly three image HDUs, but we always do right now,
158  // so 0=primary, 1=image, 2=mask, 3=variance, 4+=archive
159  //
160  // Historically the AR_HDU keyword was 1-indexed (see RFC-304), and to maintain file compatibility
161  // this is still the case so we're setting AR_HDU to 5 == 4 + 1
162  //
163  data.metadata->set("AR_HDU", 5, "HDU (1-indexed) containing the archive used to store ancillary objects");
164  if (hasCoaddInputs()) {
165  int coaddInputsId = data.archive.put(getCoaddInputs());
166  data.metadata->set("COADD_INPUTS_ID", coaddInputsId, "archive ID for coadd inputs catalogs");
167  }
168  if (hasApCorrMap()) {
169  int apCorrMapId = data.archive.put(getApCorrMap());
170  data.metadata->set("AP_CORR_MAP_ID", apCorrMapId, "archive ID for aperture correction map");
171  }
172  if (hasPsf() && getPsf()->isPersistable()) {
173  int psfId = data.archive.put(getPsf());
174  data.metadata->set("PSF_ID", psfId, "archive ID for the Exposure's main Psf");
175  }
176  if (hasWcs() && getWcs()->isPersistable()) {
177  int wcsId = data.archive.put(getWcs());
178  data.metadata->set("SKYWCS_ID", wcsId, "archive ID for the Exposure's main Wcs");
179  }
180  if (hasValidPolygon() && getValidPolygon()->isPersistable()) {
181  int polygonId = data.archive.put(getValidPolygon());
182  data.metadata->set("VALID_POLYGON_ID", polygonId, "archive ID for the Exposure's valid polygon");
183  }
184  if (hasTransmissionCurve() && getTransmissionCurve()->isPersistable()) {
185  int transmissionCurveId = data.archive.put(getTransmissionCurve());
186  data.metadata->set("TRANSMISSION_CURVE_ID", transmissionCurveId,
187  "archive ID for the Exposure's transmission curve");
188  }
189  if (hasDetector() && getDetector()->isPersistable()) {
190  int detectorId = data.archive.put(getDetector());
191  data.metadata->set("DETECTOR_ID", detectorId, "archive ID for the Exposure's Detector");
192  }
193 
194  // LSST convention is that Wcs is in pixel coordinates (i.e relative to bottom left
195  // corner of parent image, if any). The Wcs/Fits convention is that the Wcs is in
196  // image coordinates. When saving an image we convert from pixel to index coordinates.
197  // In the case where this image is a parent image, the reference pixels are unchanged
198  // by this transformation
199  if (hasWcs()) {
200  // Try to save the WCS as FITS-WCS metadata; if an exact representation
201  // is not possible then skip it
202  auto shift = lsst::geom::Extent2D(lsst::geom::Point2I(0, 0) - xy0);
203  auto newWcs = getWcs()->copyAtShiftedPixelOrigin(shift);
205  try {
206  wcsMetadata = newWcs->getFitsMetadata(true);
208  // cannot represent this WCS as FITS-WCS; don't write its metadata
209  }
210  if (wcsMetadata) {
211  data.imageMetadata->combine(newWcs->getFitsMetadata(true));
212  }
213  }
214 
215  // For the sake of ds9, store _x0 and _y0 as -LTV1, -LTV2.
216  // This is in addition to saving _x0 and _y0 as WCS A, which is done elsewhere
217  // and is what LSST uses to read _x0 and _y0.
218  // LTV is a convention used by STScI (see \S2.6.2 of HST Data Handbook for STIS, version 5.0
219  // http://www.stsci.edu/hst/stis/documents/handbooks/currentDHB/ch2_stis_data7.html#429287)
220  // and recognized by ds9.
221  data.imageMetadata->set("LTV1", static_cast<double>(-xy0.getX()));
222  data.imageMetadata->set("LTV2", static_cast<double>(-xy0.getY()));
223 
224  data.metadata->set("FILTER", getFilter().getName());
225  if (hasDetector()) {
226  data.metadata->set("DETNAME", getDetector()->getName());
227  data.metadata->set("DETSER", getDetector()->getSerial());
228  }
229 
230  auto visitInfoPtr = getVisitInfo();
231  if (visitInfoPtr) {
232  detail::setVisitInfoMetadata(*(data.metadata), *visitInfoPtr);
233  }
234 
235  /*
236  * We need to define these keywords properly! XXX
237  */
238  data.metadata->set("FLUXMAG0", getCalib()->getFluxMag0().first);
239  data.metadata->set("FLUXMAG0ERR", getCalib()->getFluxMag0().second);
240 
241  return data;
242 }
243 
244 void ExposureInfo::_finishWriteFits(fits::Fits& fitsfile, FitsWriteData const& data) const {
245  data.archive.writeFits(fitsfile);
246 }
247 
248 } // namespace image
249 } // namespace afw
250 } // namespace lsst
static std::string const & getFitsSerializationVersionName()
Get the version of FITS serialization version info name.
Definition: ExposureInfo.cc:49
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:688
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
static int getFitsSerializationVersion()
Get the version of FITS serialization that this ExposureInfo understands.
Definition: ExposureInfo.cc:44
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:68
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
STL class.
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.
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
table::Key< int > version
Definition: PhotoCalib.cc:272
Extent< double, 2 > Extent2D
Definition: Extent.h:400
#define LOG_GET(logger)
Returns a Log object associated with logger.
Definition: Log.h:75
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