LSST Applications g00d0e8bbd7+edbf708997,g03191d30f7+9ce8016dbd,g1955dfad08+0bd186d245,g199a45376c+5137f08352,g1fd858c14a+a888a50aa2,g262e1987ae+45f9aba685,g29ae962dfc+1c7d47a24f,g2cef7863aa+73c82f25e4,g35bb328faa+edbf708997,g3fd5ace14f+eed17d2c67,g47891489e3+6dc8069a4c,g53246c7159+edbf708997,g64539dfbff+c4107e45b5,g67b6fd64d1+6dc8069a4c,g74acd417e5+f452e9c21a,g786e29fd12+af89c03590,g7ae74a0b1c+a25e60b391,g7aefaa3e3d+2025e9ce17,g7cc15d900a+2d158402f9,g87389fa792+a4172ec7da,g89139ef638+6dc8069a4c,g8d4809ba88+c4107e45b5,g8d7436a09f+e96c132b44,g8ea07a8fe4+db21c37724,g98df359435+aae6d409c1,ga2180abaac+edbf708997,gac66b60396+966efe6077,gb632fb1845+88945a90f8,gbaa8f7a6c5+38b34f4976,gbf99507273+edbf708997,gca7fc764a6+6dc8069a4c,gd7ef33dd92+6dc8069a4c,gda68eeecaf+7d1e613a8d,gdab6d2f7ff+f452e9c21a,gdbb4c4dda9+c4107e45b5,ge410e46f29+6dc8069a4c,ge41e95a9f2+c4107e45b5,geaed405ab2+e194be0d2b,w.2025.47
LSST Data Management Base Package
Loading...
Searching...
No Matches
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 "lsst/pex/exceptions.h"
36#include "lsst/afw/fits.h"
38
39namespace lsst {
40namespace afw {
41namespace image {
42
43// CLASS CONSTRUCTORS and DESTRUCTOR
44
45template <typename ImageT, typename MaskT, typename VarianceT>
46Exposure<ImageT, MaskT, VarianceT>::Exposure(unsigned int width, unsigned int height,
48 : _maskedImage(width, height), _info(new ExposureInfo(wcs)) {}
49
50template <typename ImageT, typename MaskT, typename VarianceT>
53 : _maskedImage(dimensions), _info(new ExposureInfo(wcs)) {}
54
55template <typename ImageT, typename MaskT, typename VarianceT>
59
60template <typename ImageT, typename MaskT, typename VarianceT>
63 : _maskedImage(maskedImage), _info(new ExposureInfo(wcs)) {}
64
65template <typename ImageT, typename MaskT, typename VarianceT>
67 : _maskedImage(maskedImage), _info(info ? info : std::make_shared<ExposureInfo>()) {}
68
69template <typename ImageT, typename MaskT, typename VarianceT>
71 : _maskedImage(src.getMaskedImage(), deep), _info(new ExposureInfo(*src.getInfo(), deep)) {}
72// Delegate to copy-constructor for backwards compatibility
73template <typename ImageT, typename MaskT, typename VarianceT>
75
76template <typename ImageT, typename MaskT, typename VarianceT>
78 ImageOrigin const origin, bool const deep)
79 : _maskedImage(src.getMaskedImage(), bbox, origin, deep),
80 _info(new ExposureInfo(*src.getInfo(), deep)) {}
81
82template <typename ImageT, typename MaskT, typename VarianceT>
84 ImageOrigin origin, bool conformMasks, bool allowUnsafe)
85 : _maskedImage(), _info(new ExposureInfo()) {
86 ExposureFitsReader reader(fileName);
87 *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
88}
89
90template <typename ImageT, typename MaskT, typename VarianceT>
92 ImageOrigin origin, bool conformMasks, bool allowUnsafe)
93 : _maskedImage(), _info(new ExposureInfo()) {
94 ExposureFitsReader reader(manager);
95 *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
96}
97
98template <typename ImageT, typename MaskT, typename VarianceT>
100 ImageOrigin origin, bool conformMasks, bool allowUnsafe) {
101 ExposureFitsReader reader(&fitsFile);
102 *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
103}
104
105template <typename ImageT, typename MaskT, typename VarianceT>
107
108// SET METHODS
110template <typename ImageT, typename MaskT, typename VarianceT>
112 _maskedImage = maskedImage;
113}
114
115template <typename ImageT, typename MaskT, typename VarianceT>
117 lsst::geom::Point2I old(_maskedImage.getXY0());
118 if (_info->hasWcs()) {
119 auto shift = lsst::geom::Extent2D(origin - old);
120 auto newWcs = _info->getWcs()->copyAtShiftedPixelOrigin(shift);
121 _info->setWcs(newWcs);
122 }
123 _maskedImage.setXY0(origin);
124}
125
126template <typename ImageT, typename MaskT, typename VarianceT>
127Exposure<ImageT, MaskT, VarianceT> &Exposure<ImageT, MaskT, VarianceT>::operator=(Exposure const &) = default;
128template <typename ImageT, typename MaskT, typename VarianceT>
129Exposure<ImageT, MaskT, VarianceT> &Exposure<ImageT, MaskT, VarianceT>::operator=(Exposure &&) = default;
130
131// Write FITS
133template <typename ImageT, typename MaskT, typename VarianceT>
135 fits::CompressionOptions const * imageOptions,
136 fits::CompressionOptions const * maskOptions,
137 fits::CompressionOptions const * varianceOptions) const {
139 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
140}
141
142template <typename ImageT, typename MaskT, typename VarianceT>
144 fits::CompressionOptions const * imageOptions,
145 fits::CompressionOptions const * maskOptions,
146 fits::CompressionOptions const * varianceOptions) const {
148 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
149}
150
151template <typename ImageT, typename MaskT, typename VarianceT>
153 fits::CompressionOptions const * imageOptions,
154 fits::CompressionOptions const * maskOptions,
155 fits::CompressionOptions const * varianceOptions) const {
156 ExposureInfo::FitsWriteData data = _info->_startWriteFits(getXY0());
157 _maskedImage.writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, data.metadata,
158 data.imageMetadata, data.maskMetadata, data.varianceMetadata);
159 _info->_finishWriteFits(fitsfile, data);
161
162namespace {
168 * @param destination The Exposure to copy pixels to.
169 * @param source The Exposure whose pixels will be copied.
170 */
171template <class ExposureT>
172void _copyCommonPixels(ExposureT &destination, ExposureT const &source) {
173 lsst::geom::Box2I overlapBox = destination.getBBox();
174 overlapBox.clip(source.getBBox());
175
176 // MaskedImage::assign interprets empty bounding box as "whole image"
177 if (!overlapBox.isEmpty()) {
178 typename ExposureT::MaskedImageT overlapPixels(source.getMaskedImage(), overlapBox);
179 destination.getMaskedImage().assign(overlapPixels, overlapBox);
180 }
182} // namespace
183
184template <typename ImageT, typename MaskT, typename VarianceT>
186 lsst::geom::SpherePoint const &center, lsst::geom::Extent2I const &size) const {
187 if (!hasWcs()) {
188 throw LSST_EXCEPT(pex::exceptions::LogicError, "Cannot look up source position without WCS.");
189 }
190 lsst::geom::Point2D pixelCenter = getWcs()->skyToPixel(center);
191
192 return getCutout(pixelCenter, size);
193}
194
195template <typename ImageT, typename MaskT, typename VarianceT>
197 lsst::geom::Point2D const &center, lsst::geom::Extent2I const &size) const {
198 if (!lsst::geom::Box2D(getBBox()).contains(center)) {
199 std::stringstream buffer;
200 buffer << "Point " << center << " lies at pixel " << center << ", which lies outside Exposure "
201 << getBBox();
203 }
204 if (size[0] <= 0 || size[1] <= 0) {
205 std::stringstream buffer;
206 buffer << "Cannot create bounding box with dimensions " << size;
208 }
210
211 // cutout must have independent ExposureInfo
212 auto copyInfo = std::make_shared<ExposureInfo>(*getInfo());
213 MaskedImageT blank(bbox); // Can't initialize Exposure with a temporary
216 Exposure cutout(blank, copyInfo);
217
218 _copyCommonPixels(cutout, *this);
219 return cutout;
220}
221
222template <typename ImageT, typename MaskT, typename VarianceT>
227
228// Explicit instantiations
230template class Exposure<std::uint16_t>;
231template class Exposure<int>;
232template class Exposure<float>;
233template class Exposure<double>;
234template class Exposure<std::uint64_t>;
236} // namespace image
237} // namespace afw
238} // namespace lsst
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition fits.h:242
Lifetime-management for memory that goes into FITS memory files.
Definition fits.h:126
A FITS reader class for Exposures and their components.
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.
A class to contain the data, WCS, and other information needed to describe an image of the sky.
Definition Exposure.h:72
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition Exposure.h:228
bool hasWcs() const
Does this Exposure have a Wcs?
Definition Exposure.h:316
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:185
std::shared_ptr< ExposureInfo > getInfo()
Get the ExposureInfo that aggregates all the non-image components. Never null.
Definition Exposure.h:322
void writeFits(std::string const &fileName, fits::CompressionOptions const *imageOptions=nullptr, fits::CompressionOptions const *maskOptions=nullptr, fits::CompressionOptions const *varianceOptions=nullptr) const
Write an Exposure to a regular multi-extension FITS file.
Definition Exposure.cc:134
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:46
std::shared_ptr< geom::SkyWcs const > getWcs() const
Definition Exposure.h:232
Exposure & operator=(Exposure const &)
lsst::geom::Point2I getXY0() const
Return the Exposure's origin.
Definition Exposure.h:273
void setXY0(lsst::geom::Point2I const &origin)
Set the Exposure's origin (including correcting the Wcs)
Definition Exposure.cc:116
void setMaskedImage(MaskedImageT &maskedImage)
Set the MaskedImage of the Exposure.
Definition Exposure.cc:111
MaskedImage< ImageT, MaskT, VarianceT > MaskedImageT
Definition Exposure.h:74
lsst::geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
Definition Exposure.h:275
A collection of all the things that make an Exposure different from a MaskedImage.
A floating-point coordinate rectangle geometry.
Definition Box.h:413
An integer coordinate rectangle.
Definition Box.h:55
void clip(Box2I const &other) noexcept
Shrink this to ensure that other.contains(*this).
Definition Box.cc:189
bool isEmpty() const noexcept
Return true if the box contains no points.
Definition Box.h:213
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
Extent2I const getDimensions() const noexcept
Definition Box.h:186
Point2D const getCenter() const noexcept
1-d interval accessors
Definition Box.cc:93
Point in an unspecified spherical coordinate system.
Definition SpherePoint.h:57
Reports invalid arguments.
Definition Runtime.h:66
Reports errors in the logical structure of the program.
Definition Runtime.h:46
T make_shared(T... args)
ImageT::SinglePixel edgePixel(lsst::afw::image::detail::Image_tag)
Return an off-the-edge pixel appropriate for a given Image type.
Extent< double, 2 > Extent2D
Definition Extent.h:400
Point< double, 2 > Point2D
Definition Point.h:324
Extent< int, 2 > Extent2I
Definition Extent.h:397
Point< int, 2 > Point2I
Definition Point.h:321
STL namespace.
T str(T... args)
Options controlling image compression with FITS.
typename ImageT::image_category image_category
Definition ImageBase.h:67