23 #include <pybind11/pybind11.h>
24 #include <pybind11/stl.h>
26 #include "numpy/arrayobject.h"
27 #include "ndarray/pybind11.h"
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"
37 namespace py = pybind11;
39 using namespace lsst::afw::fits;
40 using namespace lsst::afw::fits::detail;
41 using namespace pybind11::literals;
45 py::class_<ImageCompressionOptions>
cls(mod,
"ImageCompressionOptions");
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).
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);
82 cls.def(
"determine", &ImageScalingOptions::determine<T>);
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).
96 cls.def(py::init<>());
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);
112 declareImageScalingOptionsTemplates<float>(
cls);
113 declareImageScalingOptionsTemplates<double>(
cls);
116 template <
typename T>
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>);
124 py::class_<ImageScale>
cls(mod,
"ImageScale");
125 cls.def(py::init<int, double, double>(),
"bitpix"_a,
"bscale"_a,
"bzero"_a);
131 declareImageScaleTemplates<float>(
cls,
"F");
132 declareImageScaleTemplates<double>(
cls,
"D");
136 py::class_<ImageWriteOptions>
cls(mod,
"ImageWriteOptions");
146 cls.def(py::init<ImageCompressionOptions const&, ImageScalingOptions const&>(),
148 cls.def(py::init<ImageScalingOptions const&>());
150 cls.def(py::init<lsst::daf::base::PropertySet const&>());
163 py::class_<Fits>
cls(mod,
"Fits");
165 cls.def(py::init<std::string const&, std::string const&, int>(),
"filename"_a,
"mode"_a,
167 cls.def(py::init<MemFileManager &, std::string const&, int>(),
"manager"_a,
"mode"_a,
177 cls.def(
"readMetadata", [](
Fits &
self,
bool strip=
false) {
return readMetadata(
self, strip); },
181 cls.def(
"gotoFirstHdu", [](
Fits &
self) {
self.setHdu(INT_MIN); });
192 py::module mod(
"_fits",
"Python wrapper for afw _fits library");
195 if (_import_array() < 0) {
196 PyErr_SetString(PyExc_ImportError,
"numpy.core.multiarray failed to import");
200 py::class_<MemFileManager> clsMemFileManager(mod,
"MemFileManager");
202 lsst::pex::exceptions::python::declareException<FitsError, lsst::pex::exceptions::IoError>(
203 mod,
"FitsError",
"IoError");
207 clsMemFileManager.def(py::init<>());
208 clsMemFileManager.def(py::init<size_t>());
217 clsMemFileManager.def(
"setData", [](
MemFileManager &m, py::bytes
const &d,
size_t size) {
218 memcpy(m.
getData(), PyBytes_AsString(d.ptr()), size);
220 clsMemFileManager.def(
"readMetadata",
223 },
"hdu"_a=INT_MIN,
"strip"_a=
false);
231 mod.def(
"readMetadata",
232 [](std::string
const& filename,
int hdu=INT_MIN,
bool strip=
false) {
234 },
"fileName"_a,
"hdu"_a=INT_MIN,
"strip"_a=
false);
void declareFits(py::module &mod)
void declareImageCompression(py::module &mod)
void declareImageScalingOptions(py::module &mod)
int bitpix
Bits per pixel (0, 8,16,32,64,-32,-64)
void declareImageScalingOptionsTemplates(py::class_< ImageScalingOptions > &cls)
ImageCompressionOptions getImageCompression()
Return the current image compression settings.
ImageCompressionOptions compression
Options controlling compression.
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)
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...
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.
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.
PYBIND11_PLUGIN(_cameraPoint)
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.
Lifetime-management for memory that goes into FITS memory files.
double bzero
Zero-point to apply when reading from FITS.
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromString(std::string const &name)
Interpret compression algorithm expressed in string.
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.
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 "<unknown>" 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)
void declareImageScaleTemplates(py::class_< ImageScale > &cls, std::string const &suffix)
void * getData() const
Return the buffer.
A class to represent a 2-dimensional array of pixels.
ImageScalingOptions::ScalingAlgorithm scalingAlgorithmFromString(std::string const &name)
Interpret scaling algorithm expressed in string.
int countHdus()
Return the number of HDUs in the file.