LSSTApplications  16.0-11-g09ed895+2,16.0-11-g12e47bd,16.0-11-g9bb73b2+6,16.0-12-g5c924a4+6,16.0-14-g9a974b3+1,16.0-15-g1417920+1,16.0-15-gdd5ca33+1,16.0-16-gf0259e2,16.0-17-g31abd91+7,16.0-17-g7d7456e+7,16.0-17-ga3d2e9f+13,16.0-18-ga4d4bcb+1,16.0-18-gd06566c+1,16.0-2-g0febb12+21,16.0-2-g9d5294e+69,16.0-2-ga8830df+6,16.0-20-g21842373+7,16.0-24-g3eae5ec,16.0-28-gfc9ea6c+4,16.0-29-ge8801f9,16.0-3-ge00e371+34,16.0-4-g18f3627+13,16.0-4-g5f3a788+20,16.0-4-ga3eb747+10,16.0-4-gabf74b7+29,16.0-4-gb13d127+6,16.0-49-g42e581f7+6,16.0-5-g27fb78a+7,16.0-5-g6a53317+34,16.0-5-gb3f8a4b+87,16.0-6-g9321be7+4,16.0-6-gcbc7b31+42,16.0-6-gf49912c+29,16.0-7-gd2eeba5+51,16.0-71-ge89f8615e,16.0-8-g21fd5fe+29,16.0-8-g3a9f023+20,16.0-8-g4734f7a+1,16.0-8-g5858431+3,16.0-9-gf5c1f43+8,master-gd73dc1d098+1,w.2019.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"
40 #include "lsst/afw/image/Calib.h"
43 #include "lsst/afw/fits.h"
45 
46 namespace lsst {
47 namespace afw {
48 namespace image {
49 
50 // CLASS CONSTRUCTORS and DESTRUCTOR
51 
52 template <typename ImageT, typename MaskT, typename VarianceT>
53 Exposure<ImageT, MaskT, VarianceT>::Exposure(unsigned int width, unsigned int height,
55  : daf::base::Citizen(typeid(this)), _maskedImage(width, height), _info(new ExposureInfo(wcs)) {}
56 
57 template <typename ImageT, typename MaskT, typename VarianceT>
60  : daf::base::Citizen(typeid(this)), _maskedImage(dimensions), _info(new ExposureInfo(wcs)) {}
61 
62 template <typename ImageT, typename MaskT, typename VarianceT>
65  : daf::base::Citizen(typeid(this)), _maskedImage(bbox), _info(new ExposureInfo(wcs)) {}
66 
67 template <typename ImageT, typename MaskT, typename VarianceT>
70  : daf::base::Citizen(typeid(this)), _maskedImage(maskedImage), _info(new ExposureInfo(wcs)) {}
71 
72 template <typename ImageT, typename MaskT, typename VarianceT>
74  : daf::base::Citizen(typeid(this)),
75  _maskedImage(maskedImage),
76  _info(info ? info : std::make_shared<ExposureInfo>()) {}
77 
78 template <typename ImageT, typename MaskT, typename VarianceT>
80  : daf::base::Citizen(typeid(this)),
81  _maskedImage(src.getMaskedImage(), deep),
82  _info(new ExposureInfo(*src.getInfo(), deep)) {}
83 // Delegate to copy-constructor for backwards compatibility
84 template <typename ImageT, typename MaskT, typename VarianceT>
86 
87 template <typename ImageT, typename MaskT, typename VarianceT>
89  ImageOrigin const origin, bool const deep)
90  : daf::base::Citizen(typeid(this)),
91  _maskedImage(src.getMaskedImage(), bbox, origin, deep),
92  _info(new ExposureInfo(*src.getInfo(), deep)) {}
93 
94 template <typename ImageT, typename MaskT, typename VarianceT>
96  ImageOrigin origin, bool conformMasks, bool allowUnsafe)
97  : daf::base::Citizen(typeid(this)), _maskedImage(), _info(new ExposureInfo()) {
98  ExposureFitsReader reader(fileName);
99  *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
100 }
101 
102 template <typename ImageT, typename MaskT, typename VarianceT>
104  ImageOrigin origin, bool conformMasks, bool allowUnsafe)
105  : daf::base::Citizen(typeid(this)), _maskedImage(), _info(new ExposureInfo()) {
106  ExposureFitsReader reader(manager);
107  *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
108 }
109 
110 template <typename ImageT, typename MaskT, typename VarianceT>
112  ImageOrigin origin, bool conformMasks, bool allowUnsafe)
113  : daf::base::Citizen(typeid(this)) {
114  ExposureFitsReader reader(&fitsFile);
115  *this = reader.read<ImageT, MaskT, VarianceT>(bbox, origin, conformMasks, allowUnsafe);
116 }
117 
118 template <typename ImageT, typename MaskT, typename VarianceT>
120 
121 // SET METHODS
122 
123 template <typename ImageT, typename MaskT, typename VarianceT>
125  _maskedImage = maskedImage;
126 }
127 
128 template <typename ImageT, typename MaskT, typename VarianceT>
130  lsst::geom::Point2I old(_maskedImage.getXY0());
131  if (_info->hasWcs()) {
132  auto shift = lsst::geom::Extent2D(origin - old);
133  auto newWcs = _info->getWcs()->copyAtShiftedPixelOrigin(shift);
134  _info->setWcs(newWcs);
135  }
136  _maskedImage.setXY0(origin);
137 }
138 
139 template <typename ImageT, typename MaskT, typename VarianceT>
141 template <typename ImageT, typename MaskT, typename VarianceT>
143 
144 // Write FITS
145 
146 template <typename ImageT, typename MaskT, typename VarianceT>
148  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
149  writeFits(fitsfile);
150 }
151 
152 template <typename ImageT, typename MaskT, typename VarianceT>
154  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
155  writeFits(fitsfile);
156 }
157 
158 template <typename ImageT, typename MaskT, typename VarianceT>
160  writeFits(fitsfile, fits::ImageWriteOptions(*_maskedImage.getImage()),
161  fits::ImageWriteOptions(*_maskedImage.getMask()),
162  fits::ImageWriteOptions(*_maskedImage.getVariance()));
163 }
164 
165 template <typename ImageT, typename MaskT, typename VarianceT>
167  fits::ImageWriteOptions const &imageOptions,
168  fits::ImageWriteOptions const &maskOptions,
169  fits::ImageWriteOptions const &varianceOptions) const {
170  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
171  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
172 }
173 
174 template <typename ImageT, typename MaskT, typename VarianceT>
176  fits::ImageWriteOptions const &imageOptions,
177  fits::ImageWriteOptions const &maskOptions,
178  fits::ImageWriteOptions const &varianceOptions) const {
179  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
180  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
181 }
182 
183 template <typename ImageT, typename MaskT, typename VarianceT>
185  fits::ImageWriteOptions const &imageOptions,
186  fits::ImageWriteOptions const &maskOptions,
187  fits::ImageWriteOptions const &varianceOptions) const {
188  ExposureInfo::FitsWriteData data = _info->_startWriteFits(getXY0());
189  _maskedImage.writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, data.metadata,
190  data.imageMetadata, data.maskMetadata, data.varianceMetadata);
191  _info->_finishWriteFits(fitsfile, data);
192 }
193 
194 namespace {
203 template <class ExposureT>
204 void _copyCommonPixels(ExposureT &destination, ExposureT const &source) {
205  lsst::geom::Box2I overlapBox = destination.getBBox();
206  overlapBox.clip(source.getBBox());
207 
208  // MaskedImage::assign interprets empty bounding box as "whole image"
209  if (!overlapBox.isEmpty()) {
210  typename ExposureT::MaskedImageT overlapPixels(source.getMaskedImage(), overlapBox);
211  destination.getMaskedImage().assign(overlapPixels, overlapBox);
212  }
213 }
214 } // namespace
215 
216 template <typename ImageT, typename MaskT, typename VarianceT>
218  lsst::geom::SpherePoint const &center, lsst::geom::Extent2I const &size) const {
219  if (!hasWcs()) {
220  throw LSST_EXCEPT(pex::exceptions::LogicError, "Cannot look up source position without WCS.");
221  }
222  lsst::geom::Point2D pixelCenter = getWcs()->skyToPixel(center);
223 
224  if (!lsst::geom::Box2D(getBBox()).contains(pixelCenter)) {
225  std::stringstream buffer;
226  buffer << "Point " << center << " lies at pixel " << pixelCenter << ", which lies outside Exposure "
227  << getBBox();
229  }
230  if (size[0] <= 0 || size[1] <= 0) {
231  std::stringstream buffer;
232  buffer << "Cannot create bounding box with dimensions " << size;
234  }
236 
237  // cutout must have independent ExposureInfo
238  auto copyInfo = std::make_shared<ExposureInfo>(*getInfo());
239  MaskedImageT blank(bbox); // Can't initialize Exposure with a temporary
240  blank = math::edgePixel<MaskedImageT>(
242  Exposure cutout(blank, copyInfo);
243 
244  _copyCommonPixels(cutout, *this);
245  return cutout;
246 }
247 
248 // Explicit instantiations
250 template class Exposure<std::uint16_t>;
251 template class Exposure<int>;
252 template class Exposure<float>;
253 template class Exposure<double>;
254 template class Exposure<std::uint64_t>;
256 } // namespace image
257 } // namespace afw
258 } // namespace lsst
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
char * data
Definition: BaseTable.cc:205
bool contains(VertexIterator const begin, VertexIterator const end, UnitVector3d const &v)
A floating-point coordinate rectangle geometry.
Definition: Box.h:294
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:183
Options for writing an image to FITS.
Definition: fits.h:218
STL namespace.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:296
STL class.
A base class for image defects.
Definition: cameraGeom.dox:3
Key< int > wcs
Definition: Exposure.cc:63
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:120
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:166
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:53
void clip(Box2I const &other) noexcept
Shrink this to ensure that other.contains(*this).
Definition: Box.cc:190
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:96
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
A collection of all the things that make an Exposure different from a MaskedImage.
Definition: ExposureInfo.h:87
An integer coordinate rectangle.
Definition: Box.h:54