LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
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
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
A floating-point coordinate rectangle geometry.
Definition: Box.h:413
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
Return true if the box contains no points.
Definition: Box.h:213
Options for writing an image to FITS.
Definition: fits.h:219
STL namespace.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
STL class.
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
A base class for image defects.
Key< int > wcs
Definition: Exposure.cc:64
char * data
Definition: BaseRecord.cc:62
std::shared_ptr< RecordT > src
Definition: Match.cc:48
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
T str(T... args)
const char * source()
Source function that allows astChannel to source from a Stream.
Definition: Stream.h:224
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:57
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
void clip(Box2I const &other) noexcept
Shrink this to ensure that other.contains(*this).
Definition: Box.cc:189
Reports invalid arguments.
Definition: Runtime.h:66
ImageT::image_category image_category
Definition: ImageBase.h:67
A FITS reader class for Exposures and their components.
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
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.
Extent< double, 2 > Extent2D
Definition: Extent.h:400
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
A collection of all the things that make an Exposure different from a MaskedImage.
Definition: ExposureInfo.h:86
An integer coordinate rectangle.
Definition: Box.h:55