LSSTApplications  11.0-24-g0a022a1,14.0+77,15.0,15.0+1
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 <stdexcept>
26 
27 #include "boost/format.hpp"
28 #include <memory>
29 #include <cstdint>
30 #include "boost/algorithm/string/trim.hpp"
31 
34 #include "lsst/pex/exceptions.h"
36 #include "lsst/afw/geom/SkyWcs.h"
38 #include "lsst/afw/detection/Psf.h"
39 #include "lsst/afw/image/Calib.h"
41 #include "lsst/afw/fits.h"
42 
43 namespace lsst {
44 namespace afw {
45 namespace image {
46 
47 // CLASS CONSTRUCTORS and DESTRUCTOR
48 
49 template <typename ImageT, typename MaskT, typename VarianceT>
50 Exposure<ImageT, MaskT, VarianceT>::Exposure(unsigned int width, unsigned int height,
52  : daf::base::Citizen(typeid(this)), _maskedImage(width, height), _info(new ExposureInfo(wcs)) {}
53 
54 template <typename ImageT, typename MaskT, typename VarianceT>
57  : daf::base::Citizen(typeid(this)), _maskedImage(dimensions), _info(new ExposureInfo(wcs)) {}
58 
59 template <typename ImageT, typename MaskT, typename VarianceT>
61  : daf::base::Citizen(typeid(this)), _maskedImage(bbox), _info(new ExposureInfo(wcs)) {}
62 
63 template <typename ImageT, typename MaskT, typename VarianceT>
66  : daf::base::Citizen(typeid(this)), _maskedImage(maskedImage), _info(new ExposureInfo(wcs)) {}
67 
68 template <typename ImageT, typename MaskT, typename VarianceT>
70  : daf::base::Citizen(typeid(this)),
71  _maskedImage(maskedImage),
72  _info(info ? info : std::make_shared<ExposureInfo>()) {}
73 
74 template <typename ImageT, typename MaskT, typename VarianceT>
76  : daf::base::Citizen(typeid(this)),
77  _maskedImage(src.getMaskedImage(), deep),
78  _info(new ExposureInfo(*src.getInfo(), deep)) {}
79 // Delegate to copy-constructor for backwards compatibility
80 template <typename ImageT, typename MaskT, typename VarianceT>
82 
83 template <typename ImageT, typename MaskT, typename VarianceT>
85  ImageOrigin const origin, bool const deep)
86  : daf::base::Citizen(typeid(this)),
87  _maskedImage(src.getMaskedImage(), bbox, origin, deep),
88  _info(new ExposureInfo(*src.getInfo(), deep)) {}
89 
90 template <typename ImageT, typename MaskT, typename VarianceT>
92  ImageOrigin origin, bool conformMasks)
93  : daf::base::Citizen(typeid(this)), _maskedImage(), _info(new ExposureInfo()) {
94  fits::Fits fitsfile(fileName, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
95  _readFits(fitsfile, bbox, origin, conformMasks);
96 }
97 
98 template <typename ImageT, typename MaskT, typename VarianceT>
100  ImageOrigin origin, bool conformMasks)
101  : daf::base::Citizen(typeid(this)), _maskedImage(), _info(new ExposureInfo()) {
102  fits::Fits fitsfile(manager, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
103  _readFits(fitsfile, bbox, origin, conformMasks);
104 }
105 
106 template <typename ImageT, typename MaskT, typename VarianceT>
108  ImageOrigin origin, bool conformMasks)
109  : daf::base::Citizen(typeid(this)) {
110  _readFits(fitsfile, bbox, origin, conformMasks);
111 }
112 
113 template <typename ImageT, typename MaskT, typename VarianceT>
115  ImageOrigin origin, bool conformMasks) {
118  _maskedImage = MaskedImageT(fitsfile, metadata, bbox, origin, conformMasks, false, imageMetadata);
119  _info->_readFits(fitsfile, metadata, imageMetadata);
120 }
121 
122 template <typename ImageT, typename MaskT, typename VarianceT>
124 
125 // SET METHODS
126 
127 template <typename ImageT, typename MaskT, typename VarianceT>
129  _maskedImage = maskedImage;
130 }
131 
132 template <typename ImageT, typename MaskT, typename VarianceT>
134  geom::Point2I old(_maskedImage.getXY0());
135  if (_info->hasWcs()) {
136  auto shift = geom::Extent2D(origin - old);
137  auto newWcs = _info->getWcs()->copyAtShiftedPixelOrigin(shift);
138  _info->setWcs(newWcs);
139  }
140  _maskedImage.setXY0(origin);
141 }
142 
143 template <typename ImageT, typename MaskT, typename VarianceT>
145 template <typename ImageT, typename MaskT, typename VarianceT>
147 
148 // Write FITS
149 
150 template <typename ImageT, typename MaskT, typename VarianceT>
152  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
153  writeFits(fitsfile);
154 }
155 
156 template <typename ImageT, typename MaskT, typename VarianceT>
158  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
159  writeFits(fitsfile);
160 }
161 
162 template <typename ImageT, typename MaskT, typename VarianceT>
164  writeFits(fitsfile, fits::ImageWriteOptions(*_maskedImage.getImage()),
165  fits::ImageWriteOptions(*_maskedImage.getMask()),
166  fits::ImageWriteOptions(*_maskedImage.getVariance()));
167 }
168 
169 template <typename ImageT, typename MaskT, typename VarianceT>
171  std::string const& fileName,
172  fits::ImageWriteOptions const& imageOptions,
173  fits::ImageWriteOptions const& maskOptions,
174  fits::ImageWriteOptions const& varianceOptions
175 ) const {
176  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
177  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
178 }
179 
180 template <typename ImageT, typename MaskT, typename VarianceT>
182  fits::MemFileManager& manager,
183  fits::ImageWriteOptions const& imageOptions,
184  fits::ImageWriteOptions const& maskOptions,
185  fits::ImageWriteOptions const& varianceOptions
186 ) const {
187  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
188  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions);
189 }
190 
191 template <typename ImageT, typename MaskT, typename VarianceT>
193  fits::Fits &fitsfile,
194  fits::ImageWriteOptions const& imageOptions,
195  fits::ImageWriteOptions const& maskOptions,
196  fits::ImageWriteOptions const& varianceOptions
197 ) const {
198  ExposureInfo::FitsWriteData data = _info->_startWriteFits(getXY0());
199  _maskedImage.writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, data.metadata,
200  data.imageMetadata, data.maskMetadata, data.varianceMetadata);
201  _info->_finishWriteFits(fitsfile, data);
202 }
203 
204 // Explicit instantiations
206 template class Exposure<std::uint16_t>;
207 template class Exposure<int>;
208 template class Exposure<float>;
209 template class Exposure<double>;
210 template class Exposure<std::uint64_t>;
212 }
213 }
214 } // end lsst::afw::image
std::shared_ptr< daf::base::PropertyList > maskMetadata
Definition: ExposureInfo.h:260
Extent< double, 2 > Extent2D
Definition: Extent.h:383
Class for storing ordered metadata with comments.
Definition: PropertyList.h:73
A class to contain the data, WCS, and other information needed to describe an image of the sky...
Definition: Exposure.h:76
Options for writing an image to FITS.
Definition: fits.h:220
STL namespace.
std::shared_ptr< daf::base::PropertyList > imageMetadata
Definition: ExposureInfo.h:259
tbl::Key< int > wcs
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:298
An integer coordinate rectangle.
Definition: Box.h:55
std::shared_ptr< daf::base::PropertyList > varianceMetadata
Definition: ExposureInfo.h:261
STL class.
Include files required for standard LSST Exception handling.
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
A struct passed back and forth between Exposure and ExposureInfo when writing FITS files...
Definition: ExposureInfo.h:257
A base class for image defects.
Definition: cameraGeom.dox:3
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:120
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:50
table::Box2IKey bbox
A collection of all the things that make an Exposure different from a MaskedImage.
Definition: ExposureInfo.h:87
std::shared_ptr< daf::base::PropertyList > metadata
Definition: ExposureInfo.h:258