LSSTApplications  20.0.0
LSSTDataManagementBasePackage
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"
42 #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  : _maskedImage(width, height), _info(new ExposureInfo(wcs)) {}
55 
56 template <typename ImageT, typename MaskT, typename VarianceT>
59  : _maskedImage(dimensions), _info(new ExposureInfo(wcs)) {}
60 
61 template <typename ImageT, typename MaskT, typename VarianceT>
64  : _maskedImage(bbox), _info(new ExposureInfo(wcs)) {}
65 
66 template <typename ImageT, typename MaskT, typename VarianceT>
69  : _maskedImage(maskedImage), _info(new ExposureInfo(wcs)) {}
70 
71 template <typename ImageT, typename MaskT, typename VarianceT>
73  : _maskedImage(maskedImage), _info(info ? info : std::make_shared<ExposureInfo>()) {}
74 
75 template <typename ImageT, typename MaskT, typename VarianceT>
77  : _maskedImage(src.getMaskedImage(), deep), _info(new ExposureInfo(*src.getInfo(), deep)) {}
78 // Delegate to copy-constructor for backwards compatibility
79 template <typename ImageT, typename MaskT, typename VarianceT>
81 
82 template <typename ImageT, typename MaskT, typename VarianceT>
84  ImageOrigin const origin, bool const deep)
85  : _maskedImage(src.getMaskedImage(), bbox, origin, deep),
86  _info(new ExposureInfo(*src.getInfo(), deep)) {}
87 
88 template <typename ImageT, typename MaskT, typename VarianceT>
90  ImageOrigin origin, bool conformMasks, bool allowUnsafe)
91  : _maskedImage(), _info(new ExposureInfo()) {
92  ExposureFitsReader reader(fileName);
93  *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
94 }
95 
96 template <typename ImageT, typename MaskT, typename VarianceT>
98  ImageOrigin origin, bool conformMasks, bool allowUnsafe)
99  : _maskedImage(), _info(new ExposureInfo()) {
100  ExposureFitsReader reader(manager);
101  *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
102 }
103 
104 template <typename ImageT, typename MaskT, typename VarianceT>
106  ImageOrigin origin, bool conformMasks, bool allowUnsafe) {
107  ExposureFitsReader reader(&fitsFile);
108  *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
109 }
110 
111 template <typename ImageT, typename MaskT, typename VarianceT>
113 
114 // SET METHODS
115 
116 template <typename ImageT, typename MaskT, typename VarianceT>
118  _maskedImage = maskedImage;
119 }
120 
121 template <typename ImageT, typename MaskT, typename VarianceT>
123  lsst::geom::Point2I old(_maskedImage.getXY0());
124  if (_info->hasWcs()) {
125  auto shift = lsst::geom::Extent2D(origin - old);
126  auto newWcs = _info->getWcs()->copyAtShiftedPixelOrigin(shift);
127  _info->setWcs(newWcs);
128  }
129  _maskedImage.setXY0(origin);
130 }
131 
132 template <typename ImageT, typename MaskT, typename VarianceT>
134 template <typename ImageT, typename MaskT, typename VarianceT>
136 
137 // Write FITS
138 
139 template <typename ImageT, typename MaskT, typename VarianceT>
141  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
142  writeFits(fitsfile);
143 }
144 
145 template <typename ImageT, typename MaskT, typename VarianceT>
147  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
148  writeFits(fitsfile);
149 }
150 
151 template <typename ImageT, typename MaskT, typename VarianceT>
153  writeFits(fitsfile, fits::ImageWriteOptions(*_maskedImage.getImage()),
154  fits::ImageWriteOptions(*_maskedImage.getMask()),
155  fits::ImageWriteOptions(*_maskedImage.getVariance()));
156 }
157 
158 template <typename ImageT, typename MaskT, typename VarianceT>
160  fits::ImageWriteOptions const &imageOptions,
161  fits::ImageWriteOptions const &maskOptions,
162  fits::ImageWriteOptions const &varianceOptions) const {
163  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
164  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
165 }
166 
167 template <typename ImageT, typename MaskT, typename VarianceT>
169  fits::ImageWriteOptions const &imageOptions,
170  fits::ImageWriteOptions const &maskOptions,
171  fits::ImageWriteOptions const &varianceOptions) const {
172  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
173  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
174 }
175 
176 template <typename ImageT, typename MaskT, typename VarianceT>
178  fits::ImageWriteOptions const &imageOptions,
179  fits::ImageWriteOptions const &maskOptions,
180  fits::ImageWriteOptions const &varianceOptions) const {
181  ExposureInfo::FitsWriteData data = _info->_startWriteFits(getXY0());
182  _maskedImage.writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, data.metadata,
183  data.imageMetadata, data.maskMetadata, data.varianceMetadata);
184  _info->_finishWriteFits(fitsfile, data);
185 }
186 
187 namespace {
196 template <class ExposureT>
197 void _copyCommonPixels(ExposureT &destination, ExposureT const &source) {
198  lsst::geom::Box2I overlapBox = destination.getBBox();
199  overlapBox.clip(source.getBBox());
200 
201  // MaskedImage::assign interprets empty bounding box as "whole image"
202  if (!overlapBox.isEmpty()) {
203  typename ExposureT::MaskedImageT overlapPixels(source.getMaskedImage(), overlapBox);
204  destination.getMaskedImage().assign(overlapPixels, overlapBox);
205  }
206 }
207 } // namespace
208 
209 template <typename ImageT, typename MaskT, typename VarianceT>
211  lsst::geom::SpherePoint const &center, lsst::geom::Extent2I const &size) const {
212  if (!hasWcs()) {
213  throw LSST_EXCEPT(pex::exceptions::LogicError, "Cannot look up source position without WCS.");
214  }
215  lsst::geom::Point2D pixelCenter = getWcs()->skyToPixel(center);
216 
217  if (!lsst::geom::Box2D(getBBox()).contains(pixelCenter)) {
218  std::stringstream buffer;
219  buffer << "Point " << center << " lies at pixel " << pixelCenter << ", which lies outside Exposure "
220  << getBBox();
222  }
223  if (size[0] <= 0 || size[1] <= 0) {
224  std::stringstream buffer;
225  buffer << "Cannot create bounding box with dimensions " << size;
227  }
229 
230  // cutout must have independent ExposureInfo
231  auto copyInfo = std::make_shared<ExposureInfo>(*getInfo());
232  MaskedImageT blank(bbox); // Can't initialize Exposure with a temporary
233  blank = math::edgePixel<MaskedImageT>(
235  Exposure cutout(blank, copyInfo);
236 
237  _copyCommonPixels(cutout, *this);
238  return cutout;
239 }
240 
241 // Explicit instantiations
243 template class Exposure<std::uint16_t>;
244 template class Exposure<int>;
245 template class Exposure<float>;
246 template class Exposure<double>;
247 template class Exposure<std::uint64_t>;
249 } // namespace image
250 } // namespace afw
251 } // namespace lsst
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst::log.log.logContinued.info
def info(fmt, *args)
Definition: logContinued.py:198
std::string
STL class.
std::shared_ptr
STL class.
Psf.h
lsst::afw::image::Exposure::setXY0
void setXY0(lsst::geom::Point2I const &origin)
Set the Exposure's origin (including correcting the Wcs)
Definition: Exposure.cc:122
lsst::afw::image::Exposure
A class to contain the data, WCS, and other information needed to describe an image of the sky.
Definition: Exposure.h:72
lsst::afw::fits::Fits
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
lsst::geom::Box2I::makeCenteredBox
static Box2I makeCenteredBox(Point2D const &center, Extent const &size)
Create a box centered as closely as possible on a particular point.
Definition: Box.cc:97
fits.h
lsst::afw
Definition: imageAlgorithm.dox:1
std::stringstream
STL class.
SkyWcs.h
lsst::afw::fits::MemFileManager
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
lsst::afw::image::detail::image_traits::image_category
ImageT::image_category image_category
Definition: ImageBase.h:67
lsst::geom::Box2I::isEmpty
bool isEmpty() const noexcept
Return true if the box contains no points.
Definition: Box.h:213
lsst::afw::fits::ImageWriteOptions
Options for writing an image to FITS.
Definition: fits.h:219
lsst::afw::image::Exposure::writeFits
void writeFits(std::string const &fileName) const
Write an Exposure to a regular multi-extension FITS file.
Definition: Exposure.cc:140
src
std::shared_ptr< RecordT > src
Definition: Match.cc:48
PropertyList.h
data
char * data
Definition: BaseRecord.cc:62
lsst::afw::image::MaskedImage
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
lsst::afw::image::Exposure::~Exposure
virtual ~Exposure()
Destructor.
ast::detail::source
const char * source()
Source function that allows astChannel to source from a Stream.
Definition: Stream.h:224
lsst::afw::image::ExposureInfo
A collection of all the things that make an Exposure different from a MaskedImage.
Definition: ExposureInfo.h:86
lsst::afw::fits::Fits::AUTO_CHECK
@ AUTO_CHECK
Definition: fits.h:308
lsst::afw::fits::Fits::AUTO_CLOSE
@ AUTO_CLOSE
Definition: fits.h:307
astshim.fitsChanContinued.contains
def contains(self, name)
Definition: fitsChanContinued.py:127
PropertySet.h
lsstDebug.getInfo
getInfo
Definition: lsstDebug.py:87
lsst::pex::exceptions::LogicError
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
dimensions
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
lsst::afw::image::Exposure::operator=
Exposure & operator=(Exposure const &)
ExposureFitsReader.h
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::afw::image::Exposure::getCutout
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:210
lsst::afw::image::ExposureFitsReader
A FITS reader class for Exposures and their components.
Definition: ExposureFitsReader.h:42
lsst::pex::exceptions::InvalidParameterError
Reports invalid arguments.
Definition: Runtime.h:66
lsst::geom::Box2I::clip
void clip(Box2I const &other) noexcept
Shrink this to ensure that other.contains(*this).
Definition: Box.cc:189
std
STL namespace.
lsst::geom::Point< int, 2 >
lsst::geom::Box2I
An integer coordinate rectangle.
Definition: Box.h:55
lsst::geom::Extent2D
Extent< double, 2 > Extent2D
Definition: Extent.h:400
std::stringstream::str
T str(T... args)
wcs
Key< int > wcs
Definition: Exposure.cc:64
lsst::afw::image::Exposure::setMaskedImage
void setMaskedImage(MaskedImageT &maskedImage)
Set the MaskedImage of the Exposure.
Definition: Exposure.cc:117
lsst::afw::image::ImageOrigin
ImageOrigin
Definition: ImageBase.h:94
lsst::geom::SpherePoint
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:57
lsst::geom::Box2D
A floating-point coordinate rectangle geometry.
Definition: Box.h:413
lsst::afw::image::ExposureFitsReader::read
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.
Definition: ExposureFitsReader.cc:473
lsst::afw::image::Exposure::Exposure
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
Exposure.h
ConvolveImage.h
lsst::geom::Extent< int, 2 >
Detector.h
exceptions.h
bbox
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117