lsst.afw  16.0-9-gdf7162dbc+1
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"
42 #include "lsst/afw/fits.h"
43 
44 namespace lsst {
45 namespace afw {
46 namespace image {
47 
48 // CLASS CONSTRUCTORS and DESTRUCTOR
49 
50 template <typename ImageT, typename MaskT, typename VarianceT>
51 Exposure<ImageT, MaskT, VarianceT>::Exposure(unsigned int width, unsigned int height,
53  : daf::base::Citizen(typeid(this)), _maskedImage(width, height), _info(new ExposureInfo(wcs)) {}
54 
55 template <typename ImageT, typename MaskT, typename VarianceT>
58  : daf::base::Citizen(typeid(this)), _maskedImage(dimensions), _info(new ExposureInfo(wcs)) {}
59 
60 template <typename ImageT, typename MaskT, typename VarianceT>
63  : daf::base::Citizen(typeid(this)), _maskedImage(bbox), _info(new ExposureInfo(wcs)) {}
64 
65 template <typename ImageT, typename MaskT, typename VarianceT>
68  : daf::base::Citizen(typeid(this)), _maskedImage(maskedImage), _info(new ExposureInfo(wcs)) {}
69 
70 template <typename ImageT, typename MaskT, typename VarianceT>
72  : daf::base::Citizen(typeid(this)),
73  _maskedImage(maskedImage),
74  _info(info ? info : std::make_shared<ExposureInfo>()) {}
75 
76 template <typename ImageT, typename MaskT, typename VarianceT>
78  : daf::base::Citizen(typeid(this)),
79  _maskedImage(src.getMaskedImage(), deep),
80  _info(new ExposureInfo(*src.getInfo(), deep)) {}
81 // Delegate to copy-constructor for backwards compatibility
82 template <typename ImageT, typename MaskT, typename VarianceT>
84 
85 template <typename ImageT, typename MaskT, typename VarianceT>
87  ImageOrigin const origin, bool const deep)
88  : daf::base::Citizen(typeid(this)),
89  _maskedImage(src.getMaskedImage(), bbox, origin, deep),
90  _info(new ExposureInfo(*src.getInfo(), deep)) {}
91 
92 template <typename ImageT, typename MaskT, typename VarianceT>
94  ImageOrigin origin, bool conformMasks)
95  : daf::base::Citizen(typeid(this)), _maskedImage(), _info(new ExposureInfo()) {
96  fits::Fits fitsfile(fileName, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
97  _readFits(fitsfile, bbox, origin, conformMasks);
98 }
99 
100 template <typename ImageT, typename MaskT, typename VarianceT>
102  ImageOrigin origin, bool conformMasks)
103  : daf::base::Citizen(typeid(this)), _maskedImage(), _info(new ExposureInfo()) {
104  fits::Fits fitsfile(manager, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
105  _readFits(fitsfile, bbox, origin, conformMasks);
106 }
107 
108 template <typename ImageT, typename MaskT, typename VarianceT>
110  ImageOrigin origin, bool conformMasks)
111  : daf::base::Citizen(typeid(this)) {
112  _readFits(fitsfile, bbox, origin, conformMasks);
113 }
114 
115 template <typename ImageT, typename MaskT, typename VarianceT>
117  ImageOrigin origin, bool conformMasks) {
120  _maskedImage = MaskedImageT(fitsfile, metadata, bbox, origin, conformMasks, false, imageMetadata);
121  _info->_readFits(fitsfile, metadata, imageMetadata);
122 }
123 
124 template <typename ImageT, typename MaskT, typename VarianceT>
126 
127 // SET METHODS
128 
129 template <typename ImageT, typename MaskT, typename VarianceT>
131  _maskedImage = maskedImage;
132 }
133 
134 template <typename ImageT, typename MaskT, typename VarianceT>
136  lsst::geom::Point2I old(_maskedImage.getXY0());
137  if (_info->hasWcs()) {
138  auto shift = lsst::geom::Extent2D(origin - old);
139  auto newWcs = _info->getWcs()->copyAtShiftedPixelOrigin(shift);
140  _info->setWcs(newWcs);
141  }
142  _maskedImage.setXY0(origin);
143 }
144 
145 template <typename ImageT, typename MaskT, typename VarianceT>
147 template <typename ImageT, typename MaskT, typename VarianceT>
149 
150 // Write FITS
151 
152 template <typename ImageT, typename MaskT, typename VarianceT>
154  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
155  writeFits(fitsfile);
156 }
157 
158 template <typename ImageT, typename MaskT, typename VarianceT>
160  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
161  writeFits(fitsfile);
162 }
163 
164 template <typename ImageT, typename MaskT, typename VarianceT>
166  writeFits(fitsfile, fits::ImageWriteOptions(*_maskedImage.getImage()),
167  fits::ImageWriteOptions(*_maskedImage.getMask()),
168  fits::ImageWriteOptions(*_maskedImage.getVariance()));
169 }
170 
171 template <typename ImageT, typename MaskT, typename VarianceT>
173  fits::ImageWriteOptions const &imageOptions,
174  fits::ImageWriteOptions const &maskOptions,
175  fits::ImageWriteOptions const &varianceOptions) const {
176  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
177  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
178 }
179 
180 template <typename ImageT, typename MaskT, typename VarianceT>
182  fits::ImageWriteOptions const &imageOptions,
183  fits::ImageWriteOptions const &maskOptions,
184  fits::ImageWriteOptions const &varianceOptions) const {
185  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
186  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
187 }
188 
189 template <typename ImageT, typename MaskT, typename VarianceT>
191  fits::ImageWriteOptions const &imageOptions,
192  fits::ImageWriteOptions const &maskOptions,
193  fits::ImageWriteOptions const &varianceOptions) const {
194  ExposureInfo::FitsWriteData data = _info->_startWriteFits(getXY0());
195  _maskedImage.writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, data.metadata,
196  data.imageMetadata, data.maskMetadata, data.varianceMetadata);
197  _info->_finishWriteFits(fitsfile, data);
198 }
199 
200 namespace {
209 template <class ExposureT>
210 void _copyCommonPixels(ExposureT &destination, ExposureT const &source) {
211  lsst::geom::Box2I overlapBox = destination.getBBox();
212  overlapBox.clip(source.getBBox());
213 
214  // MaskedImage::assign interprets empty bounding box as "whole image"
215  if (!overlapBox.isEmpty()) {
216  typename ExposureT::MaskedImageT overlapPixels(source.getMaskedImage(), overlapBox);
217  destination.getMaskedImage().assign(overlapPixels, overlapBox);
218  }
219 }
220 } // namespace
221 
222 template <typename ImageT, typename MaskT, typename VarianceT>
224  lsst::geom::SpherePoint const &center, lsst::geom::Extent2I const &size) const {
225  if (!hasWcs()) {
226  throw LSST_EXCEPT(pex::exceptions::LogicError, "Cannot look up source position without WCS.");
227  }
228  lsst::geom::Point2D pixelCenter = getWcs()->skyToPixel(center);
229 
230  if (!lsst::geom::Box2D(getBBox()).contains(pixelCenter)) {
231  std::stringstream buffer;
232  buffer << "Point " << center << " lies at pixel " << pixelCenter << ", which lies outside Exposure "
233  << getBBox();
235  }
236  if (size[0] <= 0 || size[1] <= 0) {
237  std::stringstream buffer;
238  buffer << "Cannot create bounding box with dimensions " << size;
240  }
241  lsst::geom::Box2I bbox = lsst::geom::Box2I::makeCenteredBox(pixelCenter, size);
242 
243  // cutout must have independent ExposureInfo
244  auto copyInfo = std::make_shared<ExposureInfo>(*getInfo());
245  MaskedImageT blank(bbox); // Can't initialize Exposure with a temporary
246  Exposure cutout(blank, copyInfo);
247 
248  _copyCommonPixels(cutout, *this);
249  return cutout;
250 }
251 
252 // Explicit instantiations
254 template class Exposure<std::uint16_t>;
255 template class Exposure<int>;
256 template class Exposure<float>;
257 template class Exposure<double>;
258 template class Exposure<std::uint64_t>;
260 } // namespace image
261 } // namespace afw
262 } // namespace lsst
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage&#39;s variance.
Definition: MaskedImage.h:1067
Key< int > wcs
Definition: Exposure.cc:61
virtual ~Exposure()
Destructor.
char * data
Definition: BaseTable.cc:205
bool isEmpty() const
void setXY0(lsst::geom::Point2I const &origin)
Set the Exposure&#39;s origin (including correcting the Wcs)
Definition: Exposure.cc:135
A class to contain the data, WCS, and other information needed to describe an image of the sky...
Definition: Exposure.h:78
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:300
STL namespace.
lsst::geom::Point2I getXY0() const
Return the Exposure&#39;s origin.
Definition: Exposure.h:253
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:80
lsst::geom::Point2I getXY0() const
Return the image&#39;s origin.
Definition: MaskedImage.h:1098
lsst::geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
Definition: Exposure.h:255
bool hasWcs() const
Does this Exposure have a Wcs?
Definition: Exposure.h:297
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:1034
STL class.
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
A base class for image defects.
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:210
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:223
void setMaskedImage(MaskedImageT &maskedImage)
Set the MaskedImage of the Exposure.
Definition: Exposure.cc:130
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:1046
#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:51
void setXY0(int const x0, int const y0)
Set the MaskedImage&#39;s origin.
Definition: MaskedImage.h:1108
std::shared_ptr< geom::SkyWcs const > getWcs() const
Definition: Exposure.h:214
table::Box2IKey bbox
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:153
Extent< double, 2 > Extent2D
A collection of all the things that make an Exposure different from a MaskedImage.
Definition: ExposureInfo.h:87
void clip(Box2I const &other)
Exposure & operator=(Exposure const &)