LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
LSSTDataManagementBasePackage
Functions
fits.cc File Reference
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "ndarray/pybind11.h"
#include "lsst/pex/exceptions/Exception.h"
#include "lsst/pex/exceptions/Runtime.h"
#include "lsst/pex/exceptions/python/Exception.h"
#include "lsst/daf/base.h"
#include "lsst/afw/image/Image.h"
#include "lsst/afw/fits.h"

Go to the source code of this file.

Functions

void declareImageCompression (py::module &mod)
 
template<typename T >
void declareImageScalingOptionsTemplates (py::class_< ImageScalingOptions > &cls)
 
void declareImageScalingOptions (py::module &mod)
 
template<typename T >
void declareImageScaleTemplates (py::class_< ImageScale > &cls, std::string const &suffix)
 
void declareImageScale (py::module &mod)
 
void declareImageWriteOptions (py::module &mod)
 
void declareFits (py::module &mod)
 
 PYBIND11_MODULE (fits, mod)
 

Function Documentation

◆ declareFits()

void declareFits ( py::module &  mod)

Definition at line 161 of file fits.cc.

161  {
162  py::class_<Fits> cls(mod, "Fits");
163 
164  cls.def(py::init<std::string const&, std::string const&, int>(), "filename"_a, "mode"_a,
165  "behavior"_a=Fits::AUTO_CLOSE | Fits::AUTO_CHECK);
166  cls.def(py::init<MemFileManager &, std::string const&, int>(), "manager"_a, "mode"_a,
167  "behavior"_a=Fits::AUTO_CLOSE | Fits::AUTO_CHECK);
168 
169  cls.def("closeFile", &Fits::closeFile);
170  cls.def("getFileName", &Fits::getFileName);
171  cls.def("getHdu", &Fits::getHdu);
172  cls.def("setHdu", &Fits::setHdu, "hdu"_a, "relative"_a=false);
173  cls.def("countHdus", &Fits::countHdus);
174 
175  cls.def("writeMetadata", &Fits::writeMetadata);
176  cls.def("readMetadata", [](Fits & self, bool strip=false) { return readMetadata(self, strip); },
177  "strip"_a=false);
178  cls.def("createEmpty", &Fits::createEmpty);
179 
180  cls.def("gotoFirstHdu", [](Fits & self) { self.setHdu(DEFAULT_HDU); });
181 
182  cls.def("setImageCompression", &Fits::setImageCompression);
183  cls.def("getImageCompression", &Fits::getImageCompression);
184  cls.def("checkCompressedImagePhu", &Fits::checkCompressedImagePhu);
185 
186  cls.def_readonly("status", &Fits::status);
187 }
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
bool strip
Definition: fits.cc:883
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=DEFAULT_HDU, bool strip=false)
Read FITS header.
Definition: fits.cc:1616

◆ declareImageCompression()

void declareImageCompression ( py::module &  mod)

Definition at line 43 of file fits.cc.

43  {
44  py::class_<ImageCompressionOptions> cls(mod, "ImageCompressionOptions");
45 
46  py::enum_<ImageCompressionOptions::CompressionAlgorithm>(cls, "CompressionAlgorithm").
47  value("NONE", ImageCompressionOptions::CompressionAlgorithm::NONE).
48  value("GZIP", ImageCompressionOptions::CompressionAlgorithm::GZIP).
49  value("GZIP_SHUFFLE", ImageCompressionOptions::CompressionAlgorithm::GZIP_SHUFFLE).
50  value("RICE", ImageCompressionOptions::CompressionAlgorithm::RICE).
51  value("PLIO", ImageCompressionOptions::CompressionAlgorithm::PLIO).
52  export_values();
53 
54  cls.def(py::init<ImageCompressionOptions::CompressionAlgorithm, ImageCompressionOptions::Tiles, float>(),
55  "algorithm"_a, "tiles"_a, "quantizeLevel"_a=0.0);
56  cls.def(py::init<ImageCompressionOptions::CompressionAlgorithm, int, float>(), "algorithm"_a, "rows"_a=1,
57  "quantizeLevel"_a=0.0);
58 
61  cls.def(py::init<lsst::afw::image::Image<short> const&>());
62  cls.def(py::init<lsst::afw::image::Image<int> const&>());
64  cls.def(py::init<lsst::afw::image::Image<float> const&>());
65  cls.def(py::init<lsst::afw::image::Image<double> const&>());
67 
70  cls.def(py::init<lsst::afw::image::Mask<short> const&>());
72 
73  cls.def_readonly("algorithm", &ImageCompressionOptions::algorithm);
74  cls.def_readonly("tiles", &ImageCompressionOptions::tiles);
75  cls.def_readonly("quantizeLevel", &ImageCompressionOptions::quantizeLevel);
76 }
def init()
Definition: tests.py:75
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:78
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:59

◆ declareImageScale()

void declareImageScale ( py::module &  mod)

Definition at line 122 of file fits.cc.

122  {
123  py::class_<ImageScale> cls(mod, "ImageScale");
124  cls.def(py::init<int, double, double>(), "bitpix"_a, "bscale"_a, "bzero"_a);
125  cls.def_readonly("bitpix", &ImageScale::bitpix);
126  cls.def_readonly("bscale", &ImageScale::bscale);
127  cls.def_readonly("bzero", &ImageScale::bzero);
128  cls.def_readonly("blank", &ImageScale::blank);
129 
130  declareImageScaleTemplates<float>(cls, "F");
131  declareImageScaleTemplates<double>(cls, "D");
132 }

◆ declareImageScaleTemplates()

template<typename T >
void declareImageScaleTemplates ( py::class_< ImageScale > &  cls,
std::string const &  suffix 
)

Definition at line 116 of file fits.cc.

116  {
117  cls.def("toFits", &ImageScale::toFits<T>, "image"_a, "forceNonfiniteRemoval"_a=false, "fuzz"_a=true,
118  "tiles"_a=ndarray::Array<long, 1, 1>(), "seed"_a=1);
119  cls.def("fromFits", &ImageScale::fromFits<T>);
120 }

◆ declareImageScalingOptions()

void declareImageScalingOptions ( py::module &  mod)

Definition at line 84 of file fits.cc.

84  {
85  py::class_<ImageScalingOptions> cls(mod, "ImageScalingOptions");
86  py::enum_<ImageScalingOptions::ScalingAlgorithm>(cls, "ScalingAlgorithm").
87  value("NONE", ImageScalingOptions::ScalingAlgorithm::NONE).
88  value("RANGE", ImageScalingOptions::ScalingAlgorithm::RANGE).
89  value("STDEV_POSITIVE", ImageScalingOptions::ScalingAlgorithm::STDEV_POSITIVE).
90  value("STDEV_NEGATIVE", ImageScalingOptions::ScalingAlgorithm::STDEV_NEGATIVE).
91  value("STDEV_BOTH", ImageScalingOptions::ScalingAlgorithm::STDEV_BOTH).
92  value("MANUAL", ImageScalingOptions::ScalingAlgorithm::MANUAL).
93  export_values();
94 
95  cls.def(py::init<>());
97  unsigned long, float, float, bool, double, double>(),
98  "algorithm"_a, "bitpix"_a, "maskPlanes"_a=std::vector<std::string>(), "seed"_a=1,
99  "quantizeLevel"_a=4.0, "quantizePad"_a=5.0, "fuzz"_a=true, "bscale"_a=1.0, "bzero"_a=0.0);
100 
101  cls.def_readonly("algorithm", &ImageScalingOptions::algorithm);
102  cls.def_readonly("bitpix", &ImageScalingOptions::bitpix);
103  cls.def_readonly("maskPlanes", &ImageScalingOptions::maskPlanes);
104  cls.def_readonly("seed", &ImageScalingOptions::seed);
105  cls.def_readonly("quantizeLevel", &ImageScalingOptions::quantizeLevel);
106  cls.def_readonly("quantizePad", &ImageScalingOptions::quantizePad);
107  cls.def_readonly("fuzz", &ImageScalingOptions::fuzz);
108  cls.def_readonly("bscale", &ImageScalingOptions::bscale);
109  cls.def_readonly("bzero", &ImageScalingOptions::bzero);
110 
111  declareImageScalingOptionsTemplates<float>(cls);
112  declareImageScalingOptionsTemplates<double>(cls);
113 }
def init()
Definition: tests.py:75

◆ declareImageScalingOptionsTemplates()

template<typename T >
void declareImageScalingOptionsTemplates ( py::class_< ImageScalingOptions > &  cls)

Definition at line 80 of file fits.cc.

80  {
81  cls.def("determine", &ImageScalingOptions::determine<T>);
82 }

◆ declareImageWriteOptions()

void declareImageWriteOptions ( py::module &  mod)

Definition at line 134 of file fits.cc.

134  {
135  py::class_<ImageWriteOptions> cls(mod, "ImageWriteOptions");
136 
142 
144 
145  cls.def(py::init<ImageCompressionOptions const&, ImageScalingOptions const&>(),
146  "compression"_a, "scaling"_a=ImageScalingOptions());
147  cls.def(py::init<ImageScalingOptions const&>());
148 
149  cls.def(py::init<lsst::daf::base::PropertySet const&>());
150 
151  cls.def_readonly("compression", &ImageWriteOptions::compression);
152  cls.def_readonly("scaling", &ImageWriteOptions::scaling);
153 
154  cls.def_static("validate", &ImageWriteOptions::validate);
155 }
def init()
Definition: tests.py:75
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:78
Options for scaling image pixels.
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:59

◆ PYBIND11_MODULE()

PYBIND11_MODULE ( fits  ,
mod   
)

Definition at line 190 of file fits.cc.

190  {
191  py::class_<MemFileManager> clsMemFileManager(mod, "MemFileManager");
192 
193  py::module::import("lsst.pex.exceptions");
194 
195  lsst::pex::exceptions::python::declareException<FitsError, lsst::pex::exceptions::IoError>(
196  mod, "FitsError", "IoError");
197  // lsst::pex::exceptions::python::declareException<FitsTypeError, FitsError>(mod, "FitsTypeError",
198  // "FitsError");
199 
200  clsMemFileManager.def(py::init<>());
201  clsMemFileManager.def(py::init<size_t>());
202 
203  /* TODO: We should really revisit persistence and pickling as this is quite ugly.
204  * But it is what Swig did (sort of, it used the cdata.i extension), so I reckon this
205  * is cleaner because it does not expose casting to the Python side. */
206  clsMemFileManager.def("getLength", &MemFileManager::getLength);
207  clsMemFileManager.def("getData", [](MemFileManager &m) {
208  return py::bytes(static_cast<char *>(m.getData()), m.getLength());
209  });
210  clsMemFileManager.def("setData", [](MemFileManager &m, py::bytes const &d, size_t size) {
211  memcpy(m.getData(), PyBytes_AsString(d.ptr()), size);
212  });
213  clsMemFileManager.def("readMetadata",
214  [](MemFileManager & self, int hdu=DEFAULT_HDU, bool strip=false) {
215  return readMetadata(self, hdu, strip);
216  }, "hdu"_a=DEFAULT_HDU, "strip"_a=false);
217 
220  declareImageScale(mod);
222  declareFits(mod);
223 
224  mod.attr("DEFAULT_HDU") = DEFAULT_HDU;
225  mod.def("combineMetadata", combineMetadata, "first"_a, "second"_a);
226  mod.def("makeLimitedFitsHeader", &makeLimitedFitsHeader, "metadata"_a,
227  "excludeNames"_a = std::set<std::string>());
228  mod.def("readMetadata",
229  [](std::string const& filename, int hdu=DEFAULT_HDU, bool strip=false) {
230  return readMetadata(filename, hdu, strip);
231  }, "fileName"_a, "hdu"_a=DEFAULT_HDU, "strip"_a=false);
232  mod.def("setAllowImageCompression", &setAllowImageCompression, "allow"_a);
233  mod.def("getAllowImageCompression", &getAllowImageCompression);
234 
235  mod.def("compressionAlgorithmFromString", &compressionAlgorithmFromString);
236  mod.def("compressionAlgorithmToString", &compressionAlgorithmToString);
237  mod.def("scalingAlgorithmFromString", &scalingAlgorithmFromString);
238  mod.def("scalingAlgorithmToString", &scalingAlgorithmToString);
239 }
std::shared_ptr< daf::base::PropertyList > combineMetadata(std::shared_ptr< const daf::base::PropertyList > first, std::shared_ptr< const daf::base::PropertyList > second)
Combine two sets of metadata in a FITS-appropriate fashion.
Definition: fits.cc:1587
void declareImageWriteOptions(py::module &mod)
Definition: fits.cc:134
void declareImageScale(py::module &mod)
Definition: fits.cc:122
STL class.
void declareImageScalingOptions(py::module &mod)
Definition: fits.cc:84
T memcpy(T... args)
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
void * getData() const
Return the buffer.
Definition: fits.h:191
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromString(std::string const &name)
Interpret compression algorithm expressed in string.
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 declareImageCompression(py::module &mod)
Definition: fits.cc:43
STL class.
table::Key< table::Array< std::uint8_t > > bytes
Definition: Transform.cc:199
std::size_t getLength() const
Return the buffer length.
Definition: fits.h:194
void setAllowImageCompression(bool allow)
Definition: fits.cc:1505
int m
Definition: SpanSet.cc:49
bool getAllowImageCompression()
Definition: fits.cc:1507
ImageScalingOptions::ScalingAlgorithm scalingAlgorithmFromString(std::string const &name)
Interpret scaling algorithm expressed in string.
bool strip
Definition: fits.cc:883
std::string makeLimitedFitsHeader(lsst::daf::base::PropertySet const &metadata, std::set< std::string > const &excludeNames={})
Format a PropertySet into an FITS header string in a simplistic fashion.
Definition: fits.cc:438
void declareFits(py::module &mod)
Definition: fits.cc:161
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=DEFAULT_HDU, bool strip=false)
Read FITS header.
Definition: fits.cc:1616