LSSTApplications  18.1.0
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  : daf::base::Citizen(typeid(this)), _maskedImage(width, height), _info(new ExposureInfo(wcs)) {}
55 
56 template <typename ImageT, typename MaskT, typename VarianceT>
59  : daf::base::Citizen(typeid(this)), _maskedImage(dimensions), _info(new ExposureInfo(wcs)) {}
60 
61 template <typename ImageT, typename MaskT, typename VarianceT>
64  : daf::base::Citizen(typeid(this)), _maskedImage(bbox), _info(new ExposureInfo(wcs)) {}
65 
66 template <typename ImageT, typename MaskT, typename VarianceT>
69  : daf::base::Citizen(typeid(this)), _maskedImage(maskedImage), _info(new ExposureInfo(wcs)) {}
70 
71 template <typename ImageT, typename MaskT, typename VarianceT>
73  : daf::base::Citizen(typeid(this)),
74  _maskedImage(maskedImage),
75  _info(info ? info : std::make_shared<ExposureInfo>()) {}
76 
77 template <typename ImageT, typename MaskT, typename VarianceT>
79  : daf::base::Citizen(typeid(this)),
80  _maskedImage(src.getMaskedImage(), deep),
81  _info(new ExposureInfo(*src.getInfo(), deep)) {}
82 // Delegate to copy-constructor for backwards compatibility
83 template <typename ImageT, typename MaskT, typename VarianceT>
85 
86 template <typename ImageT, typename MaskT, typename VarianceT>
88  ImageOrigin const origin, bool const deep)
89  : daf::base::Citizen(typeid(this)),
90  _maskedImage(src.getMaskedImage(), bbox, origin, deep),
91  _info(new ExposureInfo(*src.getInfo(), deep)) {}
92 
93 template <typename ImageT, typename MaskT, typename VarianceT>
95  ImageOrigin origin, bool conformMasks, bool allowUnsafe)
96  : daf::base::Citizen(typeid(this)), _maskedImage(), _info(new ExposureInfo()) {
97  ExposureFitsReader reader(fileName);
98  *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
99 }
100 
101 template <typename ImageT, typename MaskT, typename VarianceT>
103  ImageOrigin origin, bool conformMasks, bool allowUnsafe)
104  : daf::base::Citizen(typeid(this)), _maskedImage(), _info(new ExposureInfo()) {
105  ExposureFitsReader reader(manager);
106  *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
107 }
108 
109 template <typename ImageT, typename MaskT, typename VarianceT>
111  ImageOrigin origin, bool conformMasks, bool allowUnsafe)
112  : daf::base::Citizen(typeid(this)) {
113  ExposureFitsReader reader(&fitsFile);
114  *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
115 }
116 
117 template <typename ImageT, typename MaskT, typename VarianceT>
119 
120 // SET METHODS
121 
122 template <typename ImageT, typename MaskT, typename VarianceT>
124  _maskedImage = maskedImage;
125 }
126 
127 template <typename ImageT, typename MaskT, typename VarianceT>
129  lsst::geom::Point2I old(_maskedImage.getXY0());
130  if (_info->hasWcs()) {
131  auto shift = lsst::geom::Extent2D(origin - old);
132  auto newWcs = _info->getWcs()->copyAtShiftedPixelOrigin(shift);
133  _info->setWcs(newWcs);
134  }
135  _maskedImage.setXY0(origin);
136 }
137 
138 template <typename ImageT, typename MaskT, typename VarianceT>
140 template <typename ImageT, typename MaskT, typename VarianceT>
142 
143 // Write FITS
144 
145 template <typename ImageT, typename MaskT, typename VarianceT>
147  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
148  writeFits(fitsfile);
149 }
150 
151 template <typename ImageT, typename MaskT, typename VarianceT>
153  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
154  writeFits(fitsfile);
155 }
156 
157 template <typename ImageT, typename MaskT, typename VarianceT>
159  writeFits(fitsfile, fits::ImageWriteOptions(*_maskedImage.getImage()),
160  fits::ImageWriteOptions(*_maskedImage.getMask()),
161  fits::ImageWriteOptions(*_maskedImage.getVariance()));
162 }
163 
164 template <typename ImageT, typename MaskT, typename VarianceT>
166  fits::ImageWriteOptions const &imageOptions,
167  fits::ImageWriteOptions const &maskOptions,
168  fits::ImageWriteOptions const &varianceOptions) const {
169  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
170  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
171 }
172 
173 template <typename ImageT, typename MaskT, typename VarianceT>
175  fits::ImageWriteOptions const &imageOptions,
176  fits::ImageWriteOptions const &maskOptions,
177  fits::ImageWriteOptions const &varianceOptions) const {
178  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
179  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
180 }
181 
182 template <typename ImageT, typename MaskT, typename VarianceT>
184  fits::ImageWriteOptions const &imageOptions,
185  fits::ImageWriteOptions const &maskOptions,
186  fits::ImageWriteOptions const &varianceOptions) const {
187  ExposureInfo::FitsWriteData data = _info->_startWriteFits(getXY0());
188  _maskedImage.writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, data.metadata,
189  data.imageMetadata, data.maskMetadata, data.varianceMetadata);
190  _info->_finishWriteFits(fitsfile, data);
191 }
192 
193 namespace {
202 template <class ExposureT>
203 void _copyCommonPixels(ExposureT &destination, ExposureT const &source) {
204  lsst::geom::Box2I overlapBox = destination.getBBox();
205  overlapBox.clip(source.getBBox());
206 
207  // MaskedImage::assign interprets empty bounding box as "whole image"
208  if (!overlapBox.isEmpty()) {
209  typename ExposureT::MaskedImageT overlapPixels(source.getMaskedImage(), overlapBox);
210  destination.getMaskedImage().assign(overlapPixels, overlapBox);
211  }
212 }
213 } // namespace
214 
215 template <typename ImageT, typename MaskT, typename VarianceT>
217  lsst::geom::SpherePoint const &center, lsst::geom::Extent2I const &size) const {
218  if (!hasWcs()) {
219  throw LSST_EXCEPT(pex::exceptions::LogicError, "Cannot look up source position without WCS.");
220  }
221  lsst::geom::Point2D pixelCenter = getWcs()->skyToPixel(center);
222 
223  if (!lsst::geom::Box2D(getBBox()).contains(pixelCenter)) {
224  std::stringstream buffer;
225  buffer << "Point " << center << " lies at pixel " << pixelCenter << ", which lies outside Exposure "
226  << getBBox();
228  }
229  if (size[0] <= 0 || size[1] <= 0) {
230  std::stringstream buffer;
231  buffer << "Cannot create bounding box with dimensions " << size;
233  }
235 
236  // cutout must have independent ExposureInfo
237  auto copyInfo = std::make_shared<ExposureInfo>(*getInfo());
238  MaskedImageT blank(bbox); // Can't initialize Exposure with a temporary
239  blank = math::edgePixel<MaskedImageT>(
241  Exposure cutout(blank, copyInfo);
242 
243  _copyCommonPixels(cutout, *this);
244  return cutout;
245 }
246 
247 // Explicit instantiations
249 template class Exposure<std::uint16_t>;
250 template class Exposure<int>;
251 template class Exposure<float>;
252 template class Exposure<double>;
253 template class Exposure<std::uint64_t>;
255 } // namespace image
256 } // namespace afw
257 } // namespace lsst
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
bool contains(VertexIterator const begin, VertexIterator const end, UnitVector3d const &v)
A floating-point coordinate rectangle geometry.
Definition: Box.h:305
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:194
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.
A base class for image defects.
Key< int > wcs
Definition: Exposure.cc:64
char * data
Definition: BaseRecord.cc:62
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
table::Box2IKey bbox
Definition: Detector.cc:169
std::shared_ptr< RecordT > src
Definition: Match.cc:48
#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:194
Reports invalid arguments.
Definition: Runtime.h:66
ImageT::image_category image_category
Definition: ImageBase.h:68
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:100
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:88
An integer coordinate rectangle.
Definition: Box.h:54