lsst.afw  13.0-98-g5a654715e+2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fits.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2016 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #include <pybind11/pybind11.h>
24 #include <pybind11/stl.h>
25 
26 #include "numpy/arrayobject.h"
27 #include "ndarray/pybind11.h"
28 
29 #include "lsst/pex/exceptions/Exception.h"
30 #include "lsst/pex/exceptions/Runtime.h"
31 #include "lsst/pex/exceptions/python/Exception.h"
32 #include "lsst/daf/base.h"
33 #include "lsst/afw/image/Image.h"
34 
35 #include "lsst/afw/fits.h"
36 
37 namespace py = pybind11;
38 
39 using namespace lsst::afw::fits;
40 using namespace lsst::afw::fits::detail;
41 using namespace pybind11::literals;
42 
43 
44 void declareImageCompression(py::module & mod) {
45  py::class_<ImageCompressionOptions> cls(mod, "ImageCompressionOptions");
46 
47  py::enum_<ImageCompressionOptions::CompressionAlgorithm>(cls, "CompressionAlgorithm").
48  value("NONE", ImageCompressionOptions::CompressionAlgorithm::NONE).
49  value("GZIP", ImageCompressionOptions::CompressionAlgorithm::GZIP).
50  value("GZIP_SHUFFLE", ImageCompressionOptions::CompressionAlgorithm::GZIP_SHUFFLE).
51  value("RICE", ImageCompressionOptions::CompressionAlgorithm::RICE).
52  value("PLIO", ImageCompressionOptions::CompressionAlgorithm::PLIO).
53  export_values();
54 
55  cls.def(py::init<ImageCompressionOptions::CompressionAlgorithm, ImageCompressionOptions::Tiles, float>(),
56  "algorithm"_a, "tiles"_a, "quantizeLevel"_a=0.0);
57  cls.def(py::init<ImageCompressionOptions::CompressionAlgorithm, int, float>(), "algorithm"_a, "rows"_a=1,
58  "quantizeLevel"_a=0.0);
59 
60  cls.def(py::init<lsst::afw::image::Image<unsigned char> const&>());
61  cls.def(py::init<lsst::afw::image::Image<unsigned short> const&>());
62  cls.def(py::init<lsst::afw::image::Image<short> const&>());
63  cls.def(py::init<lsst::afw::image::Image<int> const&>());
64  cls.def(py::init<lsst::afw::image::Image<unsigned int> const&>());
65  cls.def(py::init<lsst::afw::image::Image<float> const&>());
66  cls.def(py::init<lsst::afw::image::Image<double> const&>());
67  cls.def(py::init<lsst::afw::image::Image<std::uint64_t> const&>());
68 
69  cls.def(py::init<lsst::afw::image::Mask<unsigned char> const&>());
70  cls.def(py::init<lsst::afw::image::Mask<unsigned short> const&>());
71  cls.def(py::init<lsst::afw::image::Mask<short> const&>());
72  cls.def(py::init<lsst::afw::image::Mask<std::int32_t> const&>());
73 
74  cls.def_readonly("algorithm", &ImageCompressionOptions::algorithm);
75  cls.def_readonly("tiles", &ImageCompressionOptions::tiles);
76  cls.def_readonly("quantizeLevel", &ImageCompressionOptions::quantizeLevel);
77 }
78 
79 
80 template <typename T>
81 void declareImageScalingOptionsTemplates(py::class_<ImageScalingOptions> & cls) {
82  cls.def("determine", &ImageScalingOptions::determine<T>);
83 }
84 
85 void declareImageScalingOptions(py::module & mod) {
86  py::class_<ImageScalingOptions> cls(mod, "ImageScalingOptions");
87  py::enum_<ImageScalingOptions::ScalingAlgorithm>(cls, "ScalingAlgorithm").
88  value("NONE", ImageScalingOptions::ScalingAlgorithm::NONE).
89  value("RANGE", ImageScalingOptions::ScalingAlgorithm::RANGE).
90  value("STDEV_POSITIVE", ImageScalingOptions::ScalingAlgorithm::STDEV_POSITIVE).
91  value("STDEV_NEGATIVE", ImageScalingOptions::ScalingAlgorithm::STDEV_NEGATIVE).
92  value("STDEV_BOTH", ImageScalingOptions::ScalingAlgorithm::STDEV_BOTH).
93  value("MANUAL", ImageScalingOptions::ScalingAlgorithm::MANUAL).
94  export_values();
95 
96  cls.def(py::init<>());
97  cls.def(py::init<ImageScalingOptions::ScalingAlgorithm, int, std::vector<std::string> const&,
98  unsigned long, float, float, bool, double, double>(),
99  "algorithm"_a, "bitpix"_a, "maskPlanes"_a=std::vector<std::string>(), "seed"_a=1,
100  "quantizeLevel"_a=4.0, "quantizePad"_a=5.0, "fuzz"_a=true, "bscale"_a=1.0, "bzero"_a=0.0);
101 
102  cls.def_readonly("algorithm", &ImageScalingOptions::algorithm);
103  cls.def_readonly("bitpix", &ImageScalingOptions::bitpix);
104  cls.def_readonly("maskPlanes", &ImageScalingOptions::maskPlanes);
105  cls.def_readonly("seed", &ImageScalingOptions::seed);
106  cls.def_readonly("quantizeLevel", &ImageScalingOptions::quantizeLevel);
107  cls.def_readonly("quantizePad", &ImageScalingOptions::quantizePad);
108  cls.def_readonly("fuzz", &ImageScalingOptions::fuzz);
109  cls.def_readonly("bscale", &ImageScalingOptions::bscale);
110  cls.def_readonly("bzero", &ImageScalingOptions::bzero);
111 
112  declareImageScalingOptionsTemplates<float>(cls);
113  declareImageScalingOptionsTemplates<double>(cls);
114 }
115 
116 template <typename T>
117 void declareImageScaleTemplates(py::class_<ImageScale> & cls, std::string const& suffix) {
118  cls.def("toFits", &ImageScale::toFits<T>, "image"_a, "forceNonfiniteRemoval"_a=false, "fuzz"_a=true,
119  "tiles"_a=ndarray::Array<long, 1, 1>(), "seed"_a=1);
120  cls.def("fromFits", &ImageScale::fromFits<T>);
121 }
122 
123 void declareImageScale(py::module & mod) {
124  py::class_<ImageScale> cls(mod, "ImageScale");
125  cls.def(py::init<int, double, double>(), "bitpix"_a, "bscale"_a, "bzero"_a);
126  cls.def_readonly("bitpix", &ImageScale::bitpix);
127  cls.def_readonly("bscale", &ImageScale::bscale);
128  cls.def_readonly("bzero", &ImageScale::bzero);
129  cls.def_readonly("blank", &ImageScale::blank);
130 
131  declareImageScaleTemplates<float>(cls, "F");
132  declareImageScaleTemplates<double>(cls, "D");
133 }
134 
135 void declareImageWriteOptions(py::module & mod) {
136  py::class_<ImageWriteOptions> cls(mod, "ImageWriteOptions");
137 
138  cls.def(py::init<lsst::afw::image::Image<std::uint16_t>>());
139  cls.def(py::init<lsst::afw::image::Image<std::int32_t>>());
140  cls.def(py::init<lsst::afw::image::Image<std::uint64_t>>());
141  cls.def(py::init<lsst::afw::image::Image<float>>());
142  cls.def(py::init<lsst::afw::image::Image<double>>());
143 
145 
146  cls.def(py::init<ImageCompressionOptions const&, ImageScalingOptions const&>(),
147  "compression"_a, "scaling"_a=ImageScalingOptions());
148  cls.def(py::init<ImageScalingOptions const&>());
149 
150  cls.def(py::init<lsst::daf::base::PropertySet const&>());
151 
152  cls.def_readonly("compression", &ImageWriteOptions::compression);
153  cls.def_readonly("scaling", &ImageWriteOptions::scaling);
154 
155  cls.def_static("validate", &ImageWriteOptions::validate);
156 }
157 
158 // Wrapping for lsst::afw::fits::Fits
159 //
160 // Not every feature is wrapped, only those that we guess might be useful.
161 // In particular, the header keyword read/write and table read/write are not wrapped.
162 void declareFits(py::module & mod) {
163  py::class_<Fits> cls(mod, "Fits");
164 
165  cls.def(py::init<std::string const&, std::string const&, int>(), "filename"_a, "mode"_a,
166  "behavior"_a=Fits::AUTO_CLOSE | Fits::AUTO_CHECK);
167  cls.def(py::init<MemFileManager &, std::string const&, int>(), "manager"_a, "mode"_a,
168  "behavior"_a=Fits::AUTO_CLOSE | Fits::AUTO_CHECK);
169 
170  cls.def("closeFile", &Fits::closeFile);
171  cls.def("getFileName", &Fits::getFileName);
172  cls.def("getHdu", &Fits::getHdu);
173  cls.def("setHdu", &Fits::setHdu, "hdu"_a, "relative"_a=false);
174  cls.def("countHdus", &Fits::countHdus);
175 
176  cls.def("writeMetadata", &Fits::writeMetadata);
177  cls.def("readMetadata", [](Fits & self, bool strip=false) { return readMetadata(self, strip); },
178  "strip"_a=false);
179  cls.def("createEmpty", &Fits::createEmpty);
180 
181  cls.def("gotoFirstHdu", [](Fits & self) { self.setHdu(INT_MIN); });
182 
183  cls.def("setImageCompression", &Fits::setImageCompression);
184  cls.def("getImageCompression", &Fits::getImageCompression);
185  cls.def("checkCompressedImagePhu", &Fits::checkCompressedImagePhu);
186 
187  cls.def_readonly("status", &Fits::status);
188 }
189 
190 
192  py::module mod("_fits", "Python wrapper for afw _fits library");
193 
194  // Need to import numpy for ndarray and eigen conversions
195  if (_import_array() < 0) {
196  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
197  return nullptr;
198  }
199 
200  py::class_<MemFileManager> clsMemFileManager(mod, "MemFileManager");
201 
202  lsst::pex::exceptions::python::declareException<FitsError, lsst::pex::exceptions::IoError>(
203  mod, "FitsError", "IoError");
204  // lsst::pex::exceptions::python::declareException<FitsTypeError, FitsError>(mod, "FitsTypeError",
205  // "FitsError");
206 
207  clsMemFileManager.def(py::init<>());
208  clsMemFileManager.def(py::init<size_t>());
209 
210  /* TODO: We should really revisit persistence and pickling as this is quite ugly.
211  * But it is what Swig did (sort of, it used the cdata.i extension), so I reckon this
212  * is cleaner because it does not expose casting to the Python side. */
213  clsMemFileManager.def("getLength", &MemFileManager::getLength);
214  clsMemFileManager.def("getData", [](MemFileManager &m) {
215  return py::bytes(static_cast<char *>(m.getData()), m.getLength());
216  });
217  clsMemFileManager.def("setData", [](MemFileManager &m, py::bytes const &d, size_t size) {
218  memcpy(m.getData(), PyBytes_AsString(d.ptr()), size);
219  });
220  clsMemFileManager.def("readMetadata",
221  [](MemFileManager & self, int hdu=INT_MIN, bool strip=false) {
222  return readMetadata(self, hdu, strip);
223  }, "hdu"_a=INT_MIN, "strip"_a=false);
224 
227  declareImageScale(mod);
229  declareFits(mod);
230 
231  mod.def("readMetadata",
232  [](std::string const& filename, int hdu=INT_MIN, bool strip=false) {
233  return readMetadata(filename, hdu, strip);
234  }, "fileName"_a, "hdu"_a=INT_MIN, "strip"_a=false);
235  mod.def("setAllowImageCompression", &setAllowImageCompression, "allow"_a);
236  mod.def("getAllowImageCompression", &getAllowImageCompression);
237 
238  mod.def("compressionAlgorithmFromString", &compressionAlgorithmFromString);
239  mod.def("compressionAlgorithmToString", &compressionAlgorithmToString);
240  mod.def("scalingAlgorithmFromString", &scalingAlgorithmFromString);
241  mod.def("scalingAlgorithmToString", &scalingAlgorithmToString);
242 
243  return mod.ptr();
244 }
void declareFits(py::module &mod)
Definition: fits.cc:162
void declareImageCompression(py::module &mod)
Definition: fits.cc:44
void declareImageScalingOptions(py::module &mod)
Definition: fits.cc:85
int bitpix
Bits per pixel (0, 8,16,32,64,-32,-64)
void declareImageScalingOptionsTemplates(py::class_< ImageScalingOptions > &cls)
Definition: fits.cc:81
ImageCompressionOptions getImageCompression()
Return the current image compression settings.
ImageCompressionOptions compression
Options controlling compression.
Definition: fits.h:202
static std::shared_ptr< daf::base::PropertySet > validate(daf::base::PropertySet const &config)
Validate a PropertySet.
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
long blank
Value for integer images indicating non-finite values.
void declareImageWriteOptions(py::module &mod)
Definition: fits.cc:135
int bitpix
Bits per pixel; negative means floating-point: 8,16,32,64,-32,-64.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:279
void closeFile()
Close a FITS file.
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=INT_MIN, bool strip=false)
Read FITS header.
Definition: Utils.h:64
int seed
Seed for random number generator when fuzzing.
void createEmpty()
Create an empty image HDU with NAXIS=0 at the end of the file.
std::size_t getLength() const
Return the buffer length.
Definition: fits.h:174
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
Tiles tiles
Tile size; a dimension with 0 means infinite (e.g., to specify one row: 0,1)
float quantizeLevel
Divisor of the standard deviation for STDEV_* scaling.
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:83
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:101
double bzero
Zero-point to apply when reading from FITS.
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromString(std::string const &name)
Interpret compression algorithm expressed in string.
double const _a
Definition: RandomImage.cc:77
Options for scaling image pixels.
double bscale
Scale to apply when reading from FITS.
std::string scalingAlgorithmToString(ImageScalingOptions::ScalingAlgorithm algorithm)
Provide string version of compression algorithm.
std::string compressionAlgorithmToString(ImageCompressionOptions::CompressionAlgorithm algorithm)
Provide string version of compression algorithm.
void setHdu(int hdu, bool relative=false)
Set the current HDU.
ImageScalingOptions scaling
Options controlling scaling.
Definition: fits.h:203
bool checkCompressedImagePhu()
Go to the first image header in the FITS file.
std::string getFileName() const
Return the file name associated with the FITS object or &quot;&lt;unknown&gt;&quot; if there is none.
void setAllowImageCompression(bool allow)
float quantizeLevel
quantization level: 0.0 = none requires use of GZIP or GZIP_SHUFFLE
double bzero
Manually specified BSCALE and BZERO (for MANUAL scaling)
CompressionAlgorithm algorithm
Compresion algorithm to use.
void setImageCompression(ImageCompressionOptions const &options)
Set compression options for writing FITS images.
ScalingAlgorithm algorithm
Scaling algorithm to use.
float quantizePad
Number of stdev to allow on the low/high side (for STDEV_POSITIVE/NEGATIVE)
bool fuzz
Fuzz the values when quantising floating-point values?
bool getAllowImageCompression()
int getHdu()
Return the current HDU (0-indexed; 0 is the Primary HDU).
std::vector< std::string > maskPlanes
Mask planes to ignore when doing statistics.
void declareImageScale(py::module &mod)
Definition: fits.cc:123
void declareImageScaleTemplates(py::class_< ImageScale > &cls, std::string const &suffix)
Definition: fits.cc:117
void * getData() const
Return the buffer.
Definition: fits.h:171
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:68
ImageScalingOptions::ScalingAlgorithm scalingAlgorithmFromString(std::string const &name)
Interpret scaling algorithm expressed in string.
int countHdus()
Return the number of HDUs in the file.