24 #include <pybind11/pybind11.h>
25 #include <pybind11/stl.h>
29 #include "ndarray/pybind11.h"
41 using namespace pybind11::literals;
48 py::class_<ImageCompressionOptions>(wrappers.
module,
"ImageCompressionOptions"),
49 [](
auto &mod,
auto &cls) {
50 cls.def(py::init<ImageCompressionOptions::CompressionAlgorithm,
51 ImageCompressionOptions::Tiles, float>(),
52 "algorithm"_a,
"tiles"_a,
"quantizeLevel"_a = 0.0);
53 cls.def(py::init<ImageCompressionOptions::CompressionAlgorithm, int, float>(),
"algorithm"_a,
54 "rows"_a = 1,
"quantizeLevel"_a = 0.0);
56 cls.def(py::init<lsst::afw::image::Image<unsigned char> const &>());
57 cls.def(py::init<lsst::afw::image::Image<unsigned short> const &>());
58 cls.def(py::init<lsst::afw::image::Image<short> const &>());
59 cls.def(py::init<lsst::afw::image::Image<int> const &>());
60 cls.def(py::init<lsst::afw::image::Image<unsigned int> const &>());
61 cls.def(py::init<lsst::afw::image::Image<float> const &>());
62 cls.def(py::init<lsst::afw::image::Image<double> const &>());
63 cls.def(py::init<lsst::afw::image::Image<std::uint64_t> const &>());
65 cls.def(py::init<lsst::afw::image::Mask<unsigned char> const &>());
66 cls.def(py::init<lsst::afw::image::Mask<unsigned short> const &>());
67 cls.def(py::init<lsst::afw::image::Mask<short> const &>());
68 cls.def(py::init<lsst::afw::image::Mask<std::int32_t> const &>());
70 cls.def_readonly(
"algorithm", &ImageCompressionOptions::algorithm);
71 cls.def_readonly(
"tiles", &ImageCompressionOptions::tiles);
72 cls.def_readonly(
"quantizeLevel", &ImageCompressionOptions::quantizeLevel);
75 py::enum_<ImageCompressionOptions::CompressionAlgorithm>(options,
"CompressionAlgorithm"),
76 [](
auto &mod,
auto &enm) {
77 enm.value(
"NONE", ImageCompressionOptions::CompressionAlgorithm::NONE);
78 enm.value(
"GZIP", ImageCompressionOptions::CompressionAlgorithm::GZIP);
79 enm.value(
"GZIP_SHUFFLE", ImageCompressionOptions::CompressionAlgorithm::GZIP_SHUFFLE);
80 enm.value(
"RICE", ImageCompressionOptions::CompressionAlgorithm::RICE);
81 enm.value(
"PLIO", ImageCompressionOptions::CompressionAlgorithm::PLIO);
87 void declareImageScalingOptionsTemplates(py::class_<ImageScalingOptions> &cls) {
88 cls.def(
"determine", &ImageScalingOptions::determine<T>);
93 py::class_<ImageScalingOptions>(wrappers.
module,
"ImageScalingOptions"),
94 [](
auto &mod,
auto &cls) {
95 cls.def(py::init<>());
96 cls.def(py::init<ImageScalingOptions::ScalingAlgorithm, int, std::vector<std::string> const &,
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,
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);
112 declareImageScalingOptionsTemplates<float>(cls);
113 declareImageScalingOptionsTemplates<double>(cls);
115 wrappers.
wrapType(py::enum_<ImageScalingOptions::ScalingAlgorithm>(options,
"ScalingAlgorithm"),
116 [](
auto &mod,
auto &enm) {
117 enm.value(
"NONE", ImageScalingOptions::ScalingAlgorithm::NONE);
118 enm.value(
"RANGE", ImageScalingOptions::ScalingAlgorithm::RANGE);
119 enm.value(
"STDEV_POSITIVE", ImageScalingOptions::ScalingAlgorithm::STDEV_POSITIVE);
120 enm.value(
"STDEV_NEGATIVE", ImageScalingOptions::ScalingAlgorithm::STDEV_NEGATIVE);
121 enm.value(
"STDEV_BOTH", ImageScalingOptions::ScalingAlgorithm::STDEV_BOTH);
122 enm.value(
"MANUAL", ImageScalingOptions::ScalingAlgorithm::MANUAL);
127 template <
typename T>
128 void declareImageScaleTemplates(py::class_<ImageScale> &cls,
std::string const &suffix) {
129 cls.def(
"toFits", &ImageScale::toFits<T>,
"image"_a,
"forceNonfiniteRemoval"_a =
false,
"fuzz"_a =
true,
130 "tiles"_a = ndarray::Array<long, 1, 1>(),
"seed"_a = 1);
131 cls.def(
"fromFits", &ImageScale::fromFits<T>);
135 wrappers.
wrapType(py::class_<ImageScale>(wrappers.
module,
"ImageScale"), [](
auto &mod,
auto &cls) {
136 cls.def(py::init<int, double, double>(),
"bitpix"_a,
"bscale"_a,
"bzero"_a);
137 cls.def_readonly(
"bitpix", &ImageScale::bitpix);
138 cls.def_readonly(
"bscale", &ImageScale::bscale);
139 cls.def_readonly(
"bzero", &ImageScale::bzero);
140 cls.def_readonly(
"blank", &ImageScale::blank);
142 declareImageScaleTemplates<float>(cls,
"F");
143 declareImageScaleTemplates<double>(cls,
"D");
148 wrappers.
wrapType(py::class_<ImageWriteOptions>(wrappers.
module,
"ImageWriteOptions"),
149 [](
auto &mod,
auto &cls) {
150 cls.def(py::init<lsst::afw::image::Image<std::uint16_t>>());
151 cls.def(py::init<lsst::afw::image::Image<std::int32_t>>());
152 cls.def(py::init<lsst::afw::image::Image<std::uint64_t>>());
153 cls.def(py::init<lsst::afw::image::Image<float>>());
154 cls.def(py::init<lsst::afw::image::Image<double>>());
156 cls.def(py::init<lsst::afw::image::Mask<lsst::afw::image::MaskPixel>>());
158 cls.def(py::init<ImageCompressionOptions const &, ImageScalingOptions const &>(),
159 "compression"_a,
"scaling"_a = ImageScalingOptions());
160 cls.def(py::init<ImageScalingOptions const &>());
162 cls.def(py::init<lsst::daf::base::PropertySet const &>());
164 cls.def_readonly(
"compression", &ImageWriteOptions::compression);
165 cls.def_readonly(
"scaling", &ImageWriteOptions::scaling);
167 cls.def_static(
"validate", &ImageWriteOptions::validate);
176 wrappers.
wrapType(py::class_<Fits>(wrappers.
module,
"Fits"), [](
auto &mod,
auto &cls) {
177 cls.def(py::init<std::string const &, std::string const &, int>(),
"filename"_a,
"mode"_a,
178 "behavior"_a = Fits::AUTO_CLOSE | Fits::AUTO_CHECK);
179 cls.def(py::init<MemFileManager &, std::string const &, int>(),
"manager"_a,
"mode"_a,
180 "behavior"_a = Fits::AUTO_CLOSE | Fits::AUTO_CHECK);
182 cls.def(
"closeFile", &Fits::closeFile);
183 cls.def(
"getFileName", &Fits::getFileName);
184 cls.def(
"getHdu", &Fits::getHdu);
185 cls.def(
"setHdu", &Fits::setHdu,
"hdu"_a,
"relative"_a = false);
186 cls.def(
"countHdus", &Fits::countHdus);
188 cls.def(
"writeMetadata", &Fits::writeMetadata);
190 "readMetadata", [](Fits &self, bool strip = false) { return readMetadata(self, strip); },
192 cls.def(
"createEmpty", &Fits::createEmpty);
194 cls.def(
"readImageI", [](Fits &
self) {
195 ndarray::Vector<int, 2>
const offset;
196 ndarray::Vector<ndarray::Size, 2> shape =
self.getImageShape<2>();
197 ndarray::Array<int, 2, 2>
result = ndarray::allocate(shape[0], shape[1]);
198 self.readImage(
result, offset);
202 cls.def(
"gotoFirstHdu", [](Fits &
self) {
self.setHdu(
DEFAULT_HDU); });
204 cls.def(
"setImageCompression", &Fits::setImageCompression);
205 cls.def(
"getImageCompression", &Fits::getImageCompression);
206 cls.def(
"checkCompressedImagePhu", &Fits::checkCompressedImagePhu);
208 cls.def_readonly(
"status", &Fits::status);
213 wrappers.
wrap([](
auto &mod) {
214 py::class_<MemFileManager> clsMemFileManager(mod,
"MemFileManager");
216 clsMemFileManager.def(py::init<>());
217 clsMemFileManager.def(py::init<size_t>());
222 clsMemFileManager.def(
"getLength", &MemFileManager::getLength);
223 clsMemFileManager.def(
"getData", [](MemFileManager &
m) {
224 return py::bytes(
static_cast<char *
>(
m.getData()),
m.getLength());
226 clsMemFileManager.def(
"setData", [](MemFileManager &
m,
py::bytes const &d,
size_t size) {
227 memcpy(
m.getData(), PyBytes_AsString(d.ptr()), size);
229 clsMemFileManager.def(
244 "fileName"_a,
"hdu"_a =
DEFAULT_HDU,
"strip"_a =
false);
262 declareImageCompression(wrappers);
263 declareImageScalingOptions(wrappers);
264 declareImageScale(wrappers);
265 declareImageWriteOptions(wrappers);
266 declareFits(wrappers);
267 declareFitsModule(wrappers);
table::Key< table::Array< std::uint8_t > > bytes
An exception thrown when problems are found when reading or writing FITS files.
Reports errors in external input/output operations.
A helper class for subdividing pybind11 module across multiple translation units (i....
auto wrapException(std::string const &pyName, std::string const &pyBase, bool setModuleName=true)
Wrap a C++ exception as a Python exception.
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
void finish()
Invoke all deferred wrapper-declaring callables.
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() i...
void wrap(WrapperCallback function)
Add a set of wrappers without defining a class.
void addSignatureDependency(std::string const &name)
Indicate an external module that provides a type used in function/method signatures.
const int DEFAULT_HDU
Specify that the default HDU should be read.
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.
std::string compressionAlgorithmToString(ImageCompressionOptions::CompressionAlgorithm algorithm)
Provide string version of compression algorithm.
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=DEFAULT_HDU, bool strip=false)
Read FITS header.
ImageScalingOptions::ScalingAlgorithm scalingAlgorithmFromString(std::string const &name)
Interpret scaling algorithm expressed in string.
PYBIND11_MODULE(_fits, mod)
void setAllowImageCompression(bool allow)
bool getAllowImageCompression()
std::string scalingAlgorithmToString(ImageScalingOptions::ScalingAlgorithm algorithm)
Provide string version of compression algorithm.
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.
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromString(std::string const &name)
Interpret compression algorithm expressed in string.
A base class for image defects.