lsst.afw  17.0.1-4-g41c8d5dc0+2
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"
45 
46 namespace lsst {
47 namespace afw {
48 namespace image {
49 
50 // CLASS CONSTRUCTORS and DESTRUCTOR
51 
52 template <typename ImageT, typename MaskT, typename VarianceT>
53 Exposure<ImageT, MaskT, VarianceT>::Exposure(unsigned int width, unsigned int height,
55  : daf::base::Citizen(typeid(this)), _maskedImage(width, height), _info(new ExposureInfo(wcs)) {}
56 
57 template <typename ImageT, typename MaskT, typename VarianceT>
60  : daf::base::Citizen(typeid(this)), _maskedImage(dimensions), _info(new ExposureInfo(wcs)) {}
61 
62 template <typename ImageT, typename MaskT, typename VarianceT>
65  : daf::base::Citizen(typeid(this)), _maskedImage(bbox), _info(new ExposureInfo(wcs)) {}
66 
67 template <typename ImageT, typename MaskT, typename VarianceT>
70  : daf::base::Citizen(typeid(this)), _maskedImage(maskedImage), _info(new ExposureInfo(wcs)) {}
71 
72 template <typename ImageT, typename MaskT, typename VarianceT>
74  : daf::base::Citizen(typeid(this)),
75  _maskedImage(maskedImage),
76  _info(info ? info : std::make_shared<ExposureInfo>()) {}
77 
78 template <typename ImageT, typename MaskT, typename VarianceT>
80  : daf::base::Citizen(typeid(this)),
81  _maskedImage(src.getMaskedImage(), deep),
82  _info(new ExposureInfo(*src.getInfo(), deep)) {}
83 // Delegate to copy-constructor for backwards compatibility
84 template <typename ImageT, typename MaskT, typename VarianceT>
86 
87 template <typename ImageT, typename MaskT, typename VarianceT>
89  ImageOrigin const origin, bool const deep)
90  : daf::base::Citizen(typeid(this)),
91  _maskedImage(src.getMaskedImage(), bbox, origin, deep),
92  _info(new ExposureInfo(*src.getInfo(), deep)) {}
93 
94 template <typename ImageT, typename MaskT, typename VarianceT>
96  ImageOrigin origin, bool conformMasks, bool allowUnsafe)
97  : daf::base::Citizen(typeid(this)), _maskedImage(), _info(new ExposureInfo()) {
98  ExposureFitsReader reader(fileName);
99  *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
100 }
101 
102 template <typename ImageT, typename MaskT, typename VarianceT>
104  ImageOrigin origin, bool conformMasks, bool allowUnsafe)
105  : daf::base::Citizen(typeid(this)), _maskedImage(), _info(new ExposureInfo()) {
106  ExposureFitsReader reader(manager);
107  *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
108 }
109 
110 template <typename ImageT, typename MaskT, typename VarianceT>
112  ImageOrigin origin, bool conformMasks, bool allowUnsafe)
113  : daf::base::Citizen(typeid(this)) {
114  ExposureFitsReader reader(&fitsFile);
115  *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
116 }
117 
118 template <typename ImageT, typename MaskT, typename VarianceT>
120 
121 // SET METHODS
122 
123 template <typename ImageT, typename MaskT, typename VarianceT>
125  _maskedImage = maskedImage;
126 }
127 
128 template <typename ImageT, typename MaskT, typename VarianceT>
130  lsst::geom::Point2I old(_maskedImage.getXY0());
131  if (_info->hasWcs()) {
132  auto shift = lsst::geom::Extent2D(origin - old);
133  auto newWcs = _info->getWcs()->copyAtShiftedPixelOrigin(shift);
134  _info->setWcs(newWcs);
135  }
136  _maskedImage.setXY0(origin);
137 }
138 
139 template <typename ImageT, typename MaskT, typename VarianceT>
141 template <typename ImageT, typename MaskT, typename VarianceT>
143 
144 // Write FITS
145 
146 template <typename ImageT, typename MaskT, typename VarianceT>
148  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
149  writeFits(fitsfile);
150 }
151 
152 template <typename ImageT, typename MaskT, typename VarianceT>
154  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
155  writeFits(fitsfile);
156 }
157 
158 template <typename ImageT, typename MaskT, typename VarianceT>
160  writeFits(fitsfile, fits::ImageWriteOptions(*_maskedImage.getImage()),
161  fits::ImageWriteOptions(*_maskedImage.getMask()),
162  fits::ImageWriteOptions(*_maskedImage.getVariance()));
163 }
164 
165 template <typename ImageT, typename MaskT, typename VarianceT>
167  fits::ImageWriteOptions const &imageOptions,
168  fits::ImageWriteOptions const &maskOptions,
169  fits::ImageWriteOptions const &varianceOptions) const {
170  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
171  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
172 }
173 
174 template <typename ImageT, typename MaskT, typename VarianceT>
176  fits::ImageWriteOptions const &imageOptions,
177  fits::ImageWriteOptions const &maskOptions,
178  fits::ImageWriteOptions const &varianceOptions) const {
179  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
180  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
181 }
182 
183 template <typename ImageT, typename MaskT, typename VarianceT>
185  fits::ImageWriteOptions const &imageOptions,
186  fits::ImageWriteOptions const &maskOptions,
187  fits::ImageWriteOptions const &varianceOptions) const {
188  ExposureInfo::FitsWriteData data = _info->_startWriteFits(getXY0());
189  _maskedImage.writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, data.metadata,
190  data.imageMetadata, data.maskMetadata, data.varianceMetadata);
191  _info->_finishWriteFits(fitsfile, data);
192 }
193 
194 namespace {
203 template <class ExposureT>
204 void _copyCommonPixels(ExposureT &destination, ExposureT const &source) {
205  lsst::geom::Box2I overlapBox = destination.getBBox();
206  overlapBox.clip(source.getBBox());
207 
208  // MaskedImage::assign interprets empty bounding box as "whole image"
209  if (!overlapBox.isEmpty()) {
210  typename ExposureT::MaskedImageT overlapPixels(source.getMaskedImage(), overlapBox);
211  destination.getMaskedImage().assign(overlapPixels, overlapBox);
212  }
213 }
214 } // namespace
215 
216 template <typename ImageT, typename MaskT, typename VarianceT>
218  lsst::geom::SpherePoint const &center, lsst::geom::Extent2I const &size) const {
219  if (!hasWcs()) {
220  throw LSST_EXCEPT(pex::exceptions::LogicError, "Cannot look up source position without WCS.");
221  }
222  lsst::geom::Point2D pixelCenter = getWcs()->skyToPixel(center);
223 
224  if (!lsst::geom::Box2D(getBBox()).contains(pixelCenter)) {
225  std::stringstream buffer;
226  buffer << "Point " << center << " lies at pixel " << pixelCenter << ", which lies outside Exposure "
227  << getBBox();
229  }
230  if (size[0] <= 0 || size[1] <= 0) {
231  std::stringstream buffer;
232  buffer << "Cannot create bounding box with dimensions " << size;
234  }
236 
237  // cutout must have independent ExposureInfo
238  auto copyInfo = std::make_shared<ExposureInfo>(*getInfo());
239  MaskedImageT blank(bbox); // Can't initialize Exposure with a temporary
240  blank = math::edgePixel<MaskedImageT>(
242  Exposure cutout(blank, copyInfo);
243 
244  _copyCommonPixels(cutout, *this);
245  return cutout;
246 }
247 
248 // Explicit instantiations
250 template class Exposure<std::uint16_t>;
251 template class Exposure<int>;
252 template class Exposure<float>;
253 template class Exposure<double>;
254 template class Exposure<std::uint64_t>;
256 } // namespace image
257 } // namespace afw
258 } // namespace lsst
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage&#39;s variance.
Definition: MaskedImage.h:1091
Key< int > wcs
Definition: Exposure.cc:63
virtual ~Exposure()
Destructor.
char * data
Definition: BaseTable.cc:205
table::Box2IKey bbox
Definition: Detector.cc:166
void setXY0(lsst::geom::Point2I const &origin)
Set the Exposure&#39;s origin (including correcting the Wcs)
Definition: Exposure.cc:129
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:320
STL namespace.
lsst::geom::Point2I getXY0() const
Return the Exposure&#39;s origin.
Definition: Exposure.h:273
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:296
lsst::geom::Point2I getXY0() const
Return the image&#39;s origin.
Definition: MaskedImage.h:1122
lsst::geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
Definition: Exposure.h:275
bool hasWcs() const
Does this Exposure have a Wcs?
Definition: Exposure.h:317
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage&#39;s image.
Definition: MaskedImage.h:1058
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:230
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:217
void setMaskedImage(MaskedImageT &maskedImage)
Set the MaskedImage of the Exposure.
Definition: Exposure.cc:124
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:421
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage&#39;s mask.
Definition: MaskedImage.h:1070
#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:53
void setXY0(int const x0, int const y0)
Set the MaskedImage&#39;s origin.
Definition: MaskedImage.h:1132
void clip(Box2I const &other) noexcept
std::shared_ptr< geom::SkyWcs const > getWcs() const
Definition: Exposure.h:234
ImageT::image_category image_category
Definition: ImageBase.h:68
A FITS reader class for Exposures and their components.
static Box2I makeCenteredBox(Point2D const &center, Extent const &size)
Exposure< ImagePixelT, MaskPixelT, VariancePixelT > read(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool allowUnsafe=false)
Read the full Exposure.
void writeFits(std::string const &fileName) const
Write an Exposure to a regular multi-extension FITS file.
Definition: Exposure.cc:147
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 &)