LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
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
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
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, type_name, write_mask, write_variance)
Definition: stamps.py:40
A base class for image defects.
STL namespace.
T str(T... args)
Options for writing an image to FITS.
Definition: fits.h:219
ImageT::image_category image_category
Definition: ImageBase.h:67
Key< int > wcs
Definition: Exposure.cc:64