LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+4b710797af,21.0.0-1-gfc31b0f+3b24369756,21.0.0-10-g2408eff+50e97f2f47,21.0.0-10-g560fb7b+0803ad37c5,21.0.0-10-g5daeb2b+f9b8dc6d5a,21.0.0-10-g8d1d15d+77a6b82ebf,21.0.0-10-gcf60f90+c961be884d,21.0.0-11-g25eff31+7692554667,21.0.0-17-g6590b197+a14a01c114,21.0.0-2-g103fe59+b79afc2051,21.0.0-2-g1367e85+1003a3501c,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+1003a3501c,21.0.0-2-g7f82c8f+c2a1919b98,21.0.0-2-g8f08a60+fd0b970de5,21.0.0-2-ga326454+c2a1919b98,21.0.0-2-gde069b7+ca45a81b40,21.0.0-2-gecfae73+afcaaec585,21.0.0-2-gfc62afb+1003a3501c,21.0.0-21-g5d80ea29e+5e3c9a3766,21.0.0-3-g357aad2+c67f36f878,21.0.0-3-g4be5c26+1003a3501c,21.0.0-3-g65f322c+02b1f88459,21.0.0-3-g7d9da8d+3b24369756,21.0.0-3-ge02ed75+a423c2ae7a,21.0.0-4-g591bb35+a423c2ae7a,21.0.0-4-g65b4814+0803ad37c5,21.0.0-4-g88306b8+199c7497e5,21.0.0-4-gccdca77+a631590478,21.0.0-4-ge8a399c+b923ff878e,21.0.0-5-gd00fb1e+d8b1e95daa,21.0.0-53-ge728e5d5+3cb64fea8e,21.0.0-6-g2d4f3f3+04719a4bac,21.0.0-7-g04766d7+8d320c19d5,21.0.0-7-g98eecf7+205433fbda,21.0.0-9-g39e06b5+a423c2ae7a,master-gac4afde19b+a423c2ae7a,w.2021.11
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 "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
char * data
Definition: BaseRecord.cc:62
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
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 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
void writeFits(std::string const &fileName) const
Write an Exposure to a regular multi-extension FITS file.
Definition: Exposure.cc:140
virtual ~Exposure()
Destructor.
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 & operator=(Exposure const &)
void setXY0(lsst::geom::Point2I const &origin)
Set the Exposure's origin (including correcting the Wcs)
Definition: Exposure.cc:122
void setMaskedImage(MaskedImageT &maskedImage)
Set the MaskedImage of the Exposure.
Definition: Exposure.cc:117
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.
Extent< double, 2 > Extent2D
Definition: Extent.h:400
def writeFits(filename, stamp_ims, metadata, write_mask, write_variance)
Definition: stamps.py:38
A base class for image defects.
STL namespace.
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
Key< int > wcs
Definition: Exposure.cc:64
T str(T... args)
Options for writing an image to FITS.
Definition: fits.h:219
ImageT::image_category image_category
Definition: ImageBase.h:67