LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
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"
32 #include "lsst/afw/geom/SkyWcs.h"
34 #include "lsst/afw/detection/Psf.h"
36 #include "lsst/afw/fits.h"
38 
39 namespace lsst {
40 namespace afw {
41 namespace image {
42 
43 // CLASS CONSTRUCTORS and DESTRUCTOR
44 
45 template <typename ImageT, typename MaskT, typename VarianceT>
46 Exposure<ImageT, MaskT, VarianceT>::Exposure(unsigned int width, unsigned int height,
48  : _maskedImage(width, height), _info(new ExposureInfo(wcs)) {}
49 
50 template <typename ImageT, typename MaskT, typename VarianceT>
53  : _maskedImage(dimensions), _info(new ExposureInfo(wcs)) {}
54 
55 template <typename ImageT, typename MaskT, typename VarianceT>
58  : _maskedImage(bbox), _info(new ExposureInfo(wcs)) {}
59 
60 template <typename ImageT, typename MaskT, typename VarianceT>
63  : _maskedImage(maskedImage), _info(new ExposureInfo(wcs)) {}
64 
65 template <typename ImageT, typename MaskT, typename VarianceT>
67  : _maskedImage(maskedImage), _info(info ? info : std::make_shared<ExposureInfo>()) {}
68 
69 template <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
73 template <typename ImageT, typename MaskT, typename VarianceT>
75 
76 template <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 
82 template <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 
90 template <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 
98 template <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 
105 template <typename ImageT, typename MaskT, typename VarianceT>
107 
108 // SET METHODS
109 
110 template <typename ImageT, typename MaskT, typename VarianceT>
112  _maskedImage = maskedImage;
113 }
114 
115 template <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 
126 template <typename ImageT, typename MaskT, typename VarianceT>
127 Exposure<ImageT, MaskT, VarianceT> &Exposure<ImageT, MaskT, VarianceT>::operator=(Exposure const &) = default;
128 template <typename ImageT, typename MaskT, typename VarianceT>
129 Exposure<ImageT, MaskT, VarianceT> &Exposure<ImageT, MaskT, VarianceT>::operator=(Exposure &&) = default;
130 
131 // Write FITS
132 
133 template <typename ImageT, typename MaskT, typename VarianceT>
135  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
136  writeFits(fitsfile);
137 }
138 
139 template <typename ImageT, typename MaskT, typename VarianceT>
141  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
142  writeFits(fitsfile);
143 }
144 
145 template <typename ImageT, typename MaskT, typename VarianceT>
147  writeFits(fitsfile, fits::ImageWriteOptions(*_maskedImage.getImage()),
148  fits::ImageWriteOptions(*_maskedImage.getMask()),
149  fits::ImageWriteOptions(*_maskedImage.getVariance()));
150 }
151 
152 template <typename ImageT, typename MaskT, typename VarianceT>
154  fits::ImageWriteOptions const &imageOptions,
155  fits::ImageWriteOptions const &maskOptions,
156  fits::ImageWriteOptions const &varianceOptions) const {
157  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
158  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
159 }
160 
161 template <typename ImageT, typename MaskT, typename VarianceT>
163  fits::ImageWriteOptions const &imageOptions,
164  fits::ImageWriteOptions const &maskOptions,
165  fits::ImageWriteOptions const &varianceOptions) const {
166  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
167  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
168 }
169 
170 template <typename ImageT, typename MaskT, typename VarianceT>
172  fits::ImageWriteOptions const &imageOptions,
173  fits::ImageWriteOptions const &maskOptions,
174  fits::ImageWriteOptions const &varianceOptions) const {
175  ExposureInfo::FitsWriteData data = _info->_startWriteFits(getXY0());
176  _maskedImage.writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, data.metadata,
177  data.imageMetadata, data.maskMetadata, data.varianceMetadata);
178  _info->_finishWriteFits(fitsfile, data);
179 }
180 
181 namespace {
190 template <class ExposureT>
191 void _copyCommonPixels(ExposureT &destination, ExposureT const &source) {
192  lsst::geom::Box2I overlapBox = destination.getBBox();
193  overlapBox.clip(source.getBBox());
194 
195  // MaskedImage::assign interprets empty bounding box as "whole image"
196  if (!overlapBox.isEmpty()) {
197  typename ExposureT::MaskedImageT overlapPixels(source.getMaskedImage(), overlapBox);
198  destination.getMaskedImage().assign(overlapPixels, overlapBox);
199  }
200 }
201 } // namespace
202 
203 template <typename ImageT, typename MaskT, typename VarianceT>
205  lsst::geom::SpherePoint const &center, lsst::geom::Extent2I const &size) const {
206  if (!hasWcs()) {
207  throw LSST_EXCEPT(pex::exceptions::LogicError, "Cannot look up source position without WCS.");
208  }
209  lsst::geom::Point2D pixelCenter = getWcs()->skyToPixel(center);
210 
211  if (!lsst::geom::Box2D(getBBox()).contains(pixelCenter)) {
212  std::stringstream buffer;
213  buffer << "Point " << center << " lies at pixel " << pixelCenter << ", which lies outside Exposure "
214  << getBBox();
216  }
217  if (size[0] <= 0 || size[1] <= 0) {
218  std::stringstream buffer;
219  buffer << "Cannot create bounding box with dimensions " << size;
221  }
223 
224  // cutout must have independent ExposureInfo
225  auto copyInfo = std::make_shared<ExposureInfo>(*getInfo());
226  MaskedImageT blank(bbox); // Can't initialize Exposure with a temporary
227  blank = math::edgePixel<MaskedImageT>(
229  Exposure cutout(blank, copyInfo);
230 
231  _copyCommonPixels(cutout, *this);
232  return cutout;
233 }
234 
235 // Explicit instantiations
237 template class Exposure<std::uint16_t>;
238 template class Exposure<int>;
239 template class Exposure<float>;
240 template class Exposure<double>;
241 template class Exposure<std::uint64_t>;
243 } // namespace image
244 } // namespace afw
245 } // namespace lsst
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
char * data
Definition: BaseRecord.cc:61
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:48
std::shared_ptr< RecordT > src
Definition: Match.cc:48
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
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
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
A collection of all the things that make an Exposure different from a MaskedImage.
Definition: ExposureInfo.h:86
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
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
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
const char * source()
Source function that allows astChannel to source from a Stream.
Definition: Stream.h:224
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
FilterProperty & operator=(FilterProperty const &)=default
Extent< double, 2 > Extent2D
Definition: Extent.h:400
def writeFits(filename, stamps, metadata, type_name, write_mask, write_variance, write_archive=False)
Definition: stamps.py:42
A base class for image defects.
STL namespace.
T str(T... args)
Options for writing an image to FITS.
Definition: fits.h:219
typename ImageT::image_category image_category
Definition: ImageBase.h:67
Key< int > wcs
Definition: Exposure.cc:64