lsst.afw  16.0-43-gaa65a4573
Exposure.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*- // fixed format comment for emacs
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #include <memory>
26 #include <stdexcept>
27 #include <sstream>
28 #include <cstdint>
29 
30 #include "boost/format.hpp"
31 #include "boost/algorithm/string/trim.hpp"
32 
35 #include "lsst/pex/exceptions.h"
37 #include "lsst/afw/geom/SkyWcs.h"
39 #include "lsst/afw/detection/Psf.h"
40 #include "lsst/afw/image/Calib.h"
43 #include "lsst/afw/fits.h"
44 
45 namespace lsst {
46 namespace afw {
47 namespace image {
48 
49 // CLASS CONSTRUCTORS and DESTRUCTOR
50 
51 template <typename ImageT, typename MaskT, typename VarianceT>
52 Exposure<ImageT, MaskT, VarianceT>::Exposure(unsigned int width, unsigned int height,
54  : daf::base::Citizen(typeid(this)), _maskedImage(width, height), _info(new ExposureInfo(wcs)) {}
55 
56 template <typename ImageT, typename MaskT, typename VarianceT>
59  : daf::base::Citizen(typeid(this)), _maskedImage(dimensions), _info(new ExposureInfo(wcs)) {}
60 
61 template <typename ImageT, typename MaskT, typename VarianceT>
64  : daf::base::Citizen(typeid(this)), _maskedImage(bbox), _info(new ExposureInfo(wcs)) {}
65 
66 template <typename ImageT, typename MaskT, typename VarianceT>
69  : daf::base::Citizen(typeid(this)), _maskedImage(maskedImage), _info(new ExposureInfo(wcs)) {}
70 
71 template <typename ImageT, typename MaskT, typename VarianceT>
73  : daf::base::Citizen(typeid(this)),
74  _maskedImage(maskedImage),
75  _info(info ? info : std::make_shared<ExposureInfo>()) {}
76 
77 template <typename ImageT, typename MaskT, typename VarianceT>
79  : daf::base::Citizen(typeid(this)),
80  _maskedImage(src.getMaskedImage(), deep),
81  _info(new ExposureInfo(*src.getInfo(), deep)) {}
82 // Delegate to copy-constructor for backwards compatibility
83 template <typename ImageT, typename MaskT, typename VarianceT>
85 
86 template <typename ImageT, typename MaskT, typename VarianceT>
88  ImageOrigin const origin, bool const deep)
89  : daf::base::Citizen(typeid(this)),
90  _maskedImage(src.getMaskedImage(), bbox, origin, deep),
91  _info(new ExposureInfo(*src.getInfo(), deep)) {}
92 
93 template <typename ImageT, typename MaskT, typename VarianceT>
95  ImageOrigin origin, bool conformMasks)
96  : daf::base::Citizen(typeid(this)), _maskedImage(), _info(new ExposureInfo()) {
97  fits::Fits fitsfile(fileName, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
98  _readFits(fitsfile, bbox, origin, conformMasks);
99 }
100 
101 template <typename ImageT, typename MaskT, typename VarianceT>
103  ImageOrigin origin, bool conformMasks)
104  : daf::base::Citizen(typeid(this)), _maskedImage(), _info(new ExposureInfo()) {
105  fits::Fits fitsfile(manager, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
106  _readFits(fitsfile, bbox, origin, conformMasks);
107 }
108 
109 template <typename ImageT, typename MaskT, typename VarianceT>
111  ImageOrigin origin, bool conformMasks)
112  : daf::base::Citizen(typeid(this)) {
113  _readFits(fitsfile, bbox, origin, conformMasks);
114 }
115 
116 template <typename ImageT, typename MaskT, typename VarianceT>
118  ImageOrigin origin, bool conformMasks) {
121  _maskedImage = MaskedImageT(fitsfile, metadata, bbox, origin, conformMasks, false, imageMetadata);
122  _info->_readFits(fitsfile, metadata, imageMetadata);
123 }
124 
125 template <typename ImageT, typename MaskT, typename VarianceT>
127 
128 // SET METHODS
129 
130 template <typename ImageT, typename MaskT, typename VarianceT>
132  _maskedImage = maskedImage;
133 }
134 
135 template <typename ImageT, typename MaskT, typename VarianceT>
137  lsst::geom::Point2I old(_maskedImage.getXY0());
138  if (_info->hasWcs()) {
139  auto shift = lsst::geom::Extent2D(origin - old);
140  auto newWcs = _info->getWcs()->copyAtShiftedPixelOrigin(shift);
141  _info->setWcs(newWcs);
142  }
143  _maskedImage.setXY0(origin);
144 }
145 
146 template <typename ImageT, typename MaskT, typename VarianceT>
148 template <typename ImageT, typename MaskT, typename VarianceT>
150 
151 // Write FITS
152 
153 template <typename ImageT, typename MaskT, typename VarianceT>
155  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
156  writeFits(fitsfile);
157 }
158 
159 template <typename ImageT, typename MaskT, typename VarianceT>
161  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
162  writeFits(fitsfile);
163 }
164 
165 template <typename ImageT, typename MaskT, typename VarianceT>
167  writeFits(fitsfile, fits::ImageWriteOptions(*_maskedImage.getImage()),
168  fits::ImageWriteOptions(*_maskedImage.getMask()),
169  fits::ImageWriteOptions(*_maskedImage.getVariance()));
170 }
171 
172 template <typename ImageT, typename MaskT, typename VarianceT>
174  fits::ImageWriteOptions const &imageOptions,
175  fits::ImageWriteOptions const &maskOptions,
176  fits::ImageWriteOptions const &varianceOptions) const {
177  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
178  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
179 }
180 
181 template <typename ImageT, typename MaskT, typename VarianceT>
183  fits::ImageWriteOptions const &imageOptions,
184  fits::ImageWriteOptions const &maskOptions,
185  fits::ImageWriteOptions const &varianceOptions) const {
186  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
187  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
188 }
189 
190 template <typename ImageT, typename MaskT, typename VarianceT>
192  fits::ImageWriteOptions const &imageOptions,
193  fits::ImageWriteOptions const &maskOptions,
194  fits::ImageWriteOptions const &varianceOptions) const {
195  ExposureInfo::FitsWriteData data = _info->_startWriteFits(getXY0());
196  _maskedImage.writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, data.metadata,
197  data.imageMetadata, data.maskMetadata, data.varianceMetadata);
198  _info->_finishWriteFits(fitsfile, data);
199 }
200 
201 namespace {
210 template <class ExposureT>
211 void _copyCommonPixels(ExposureT &destination, ExposureT const &source) {
212  lsst::geom::Box2I overlapBox = destination.getBBox();
213  overlapBox.clip(source.getBBox());
214 
215  // MaskedImage::assign interprets empty bounding box as "whole image"
216  if (!overlapBox.isEmpty()) {
217  typename ExposureT::MaskedImageT overlapPixels(source.getMaskedImage(), overlapBox);
218  destination.getMaskedImage().assign(overlapPixels, overlapBox);
219  }
220 }
221 } // namespace
222 
223 template <typename ImageT, typename MaskT, typename VarianceT>
225  lsst::geom::SpherePoint const &center, lsst::geom::Extent2I const &size) const {
226  if (!hasWcs()) {
227  throw LSST_EXCEPT(pex::exceptions::LogicError, "Cannot look up source position without WCS.");
228  }
229  lsst::geom::Point2D pixelCenter = getWcs()->skyToPixel(center);
230 
231  if (!lsst::geom::Box2D(getBBox()).contains(pixelCenter)) {
232  std::stringstream buffer;
233  buffer << "Point " << center << " lies at pixel " << pixelCenter << ", which lies outside Exposure "
234  << getBBox();
236  }
237  if (size[0] <= 0 || size[1] <= 0) {
238  std::stringstream buffer;
239  buffer << "Cannot create bounding box with dimensions " << size;
241  }
242  lsst::geom::Box2I bbox = lsst::geom::Box2I::makeCenteredBox(pixelCenter, size);
243 
244  // cutout must have independent ExposureInfo
245  auto copyInfo = std::make_shared<ExposureInfo>(*getInfo());
246  MaskedImageT blank(bbox); // Can't initialize Exposure with a temporary
247  blank = math::edgePixel<MaskedImageT>(
249  Exposure cutout(blank, copyInfo);
250 
251  _copyCommonPixels(cutout, *this);
252  return cutout;
253 }
254 
255 // Explicit instantiations
257 template class Exposure<std::uint16_t>;
258 template class Exposure<int>;
259 template class Exposure<float>;
260 template class Exposure<double>;
261 template class Exposure<std::uint64_t>;
263 } // namespace image
264 } // namespace afw
265 } // namespace lsst
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage&#39;s variance.
Definition: MaskedImage.h:1082
Key< int > wcs
Definition: Exposure.cc:61
virtual ~Exposure()
Destructor.
char * data
Definition: BaseTable.cc:205
void setXY0(lsst::geom::Point2I const &origin)
Set the Exposure&#39;s origin (including correcting the Wcs)
Definition: Exposure.cc:136
A class to contain the data, WCS, and other information needed to describe an image of the sky...
Definition: Exposure.h:72
bool isEmpty() const noexcept
Options for writing an image to FITS.
Definition: fits.h:218
std::shared_ptr< ExposureInfo > getInfo()
Get the ExposureInfo that aggregates all the non-image components. Never null.
Definition: Exposure.h:314
STL namespace.
lsst::geom::Point2I getXY0() const
Return the Exposure&#39;s origin.
Definition: Exposure.h:267
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:296
MaskedImage< ImageT, MaskT, VarianceT > MaskedImageT
Definition: Exposure.h:74
lsst::geom::Point2I getXY0() const
Return the image&#39;s origin.
Definition: MaskedImage.h:1113
lsst::geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
Definition: Exposure.h:269
bool hasWcs() const
Does this Exposure have a Wcs?
Definition: Exposure.h:311
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:1049
STL class.
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
A base class for image defects.
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:224
std::shared_ptr< RecordT > src
Definition: Match.cc:48
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:120
Exposure getCutout(lsst::geom::SpherePoint const &center, lsst::geom::Extent2I const &size) const
Return an Exposure that is a small cutout of the original.
Definition: Exposure.cc:224
void setMaskedImage(MaskedImageT &maskedImage)
Set the MaskedImage of the Exposure.
Definition: Exposure.cc:131
T str(T... args)
const char * source()
void writeFits(std::string const &fileName, std::shared_ptr< daf::base::PropertySet const > metadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > imageMetadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > maskMetadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > varianceMetadata=std::shared_ptr< daf::base::PropertySet const >()) const
Write a MaskedImage to a regular FITS file.
Definition: MaskedImage.cc:486
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage&#39;s mask.
Definition: MaskedImage.h:1061
#define LSST_EXCEPT(type,...)
Citizen(const std::type_info &)
Exposure(unsigned int width, unsigned int height, std::shared_ptr< geom::SkyWcs const > wcs=std::shared_ptr< geom::SkyWcs const >())
Construct an Exposure with a blank MaskedImage of specified size (default 0x0) and a SkyWcs (which ma...
Definition: Exposure.cc:52
void setXY0(int const x0, int const y0)
Set the MaskedImage&#39;s origin.
Definition: MaskedImage.h:1123
void clip(Box2I const &other) noexcept
std::shared_ptr< geom::SkyWcs const > getWcs() const
Definition: Exposure.h:228
table::Box2IKey bbox
ImageT::image_category image_category
Definition: ImageBase.h:68
static Box2I makeCenteredBox(Point2D const &center, Extent const &size)
void writeFits(std::string const &fileName) const
Write an Exposure to a regular multi-extension FITS file.
Definition: Exposure.cc:154
Extent< double, 2 > Extent2D
A collection of all the things that make an Exposure different from a MaskedImage.
Definition: ExposureInfo.h:87
Exposure & operator=(Exposure const &)