23 #include <pybind11/pybind11.h>
24 #include <pybind11/stl.h>
26 #include "ndarray/pybind11.h"
40 using namespace pybind11::literals;
44 py::class_<ImageCompressionOptions>
cls(mod,
"ImageCompressionOptions");
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).
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);
81 cls.def(
"determine", &ImageScalingOptions::determine<T>);
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).
95 cls.def(py::init<>());
97 unsigned long,
float,
float,
bool,
double,
double>(),
99 "quantizeLevel"_a=4.0,
"quantizePad"_a=5.0,
"fuzz"_a=
true,
"bscale"_a=1.0,
"bzero"_a=0.0);
111 declareImageScalingOptionsTemplates<float>(
cls);
112 declareImageScalingOptionsTemplates<double>(
cls);
115 template <
typename T>
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>);
123 py::class_<ImageScale>
cls(mod,
"ImageScale");
124 cls.def(py::init<int, double, double>(),
"bitpix"_a,
"bscale"_a,
"bzero"_a);
130 declareImageScaleTemplates<float>(
cls,
"F");
131 declareImageScaleTemplates<double>(
cls,
"D");
135 py::class_<ImageWriteOptions>
cls(mod,
"ImageWriteOptions");
145 cls.def(py::init<ImageCompressionOptions const&, ImageScalingOptions const&>(),
147 cls.def(py::init<ImageScalingOptions const&>());
149 cls.def(py::init<lsst::daf::base::PropertySet const&>());
162 py::class_<Fits>
cls(mod,
"Fits");
164 cls.def(py::init<std::string const&, std::string const&, int>(),
"filename"_a,
"mode"_a,
166 cls.def(py::init<MemFileManager &, std::string const&, int>(),
"manager"_a,
"mode"_a,
180 cls.def(
"readImageI", [](
Fits &
self) {
181 ndarray::Vector<int, 2>
const offset;
182 ndarray::Vector<ndarray::Size, 2> shape =
self.getImageShape<2>();
183 ndarray::Array<int, 2, 2>
result = ndarray::allocate(shape[0], shape[1]);
184 self.readImage(
result, offset);
199 py::class_<MemFileManager> clsMemFileManager(mod,
"MemFileManager");
201 py::module::import(
"lsst.pex.exceptions");
203 lsst::pex::exceptions::python::declareException<FitsError, lsst::pex::exceptions::IoError>(
204 mod,
"FitsError",
"IoError");
208 clsMemFileManager.def(py::init<>());
209 clsMemFileManager.def(py::init<size_t>());
216 return py::bytes(
static_cast<char *
>(
m.getData()),
m.getLength());
219 memcpy(
m.getData(), PyBytes_AsString(d.ptr()), size);
221 clsMemFileManager.def(
"readMetadata",
236 mod.def(
"readMetadata",
239 },
"fileName"_a,
"hdu"_a=
DEFAULT_HDU,
"strip"_a=
false);
table::Key< table::Array< std::uint8_t > > bytes
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
void closeFile()
Close a FITS file.
ImageCompressionOptions getImageCompression()
Return the current image compression settings.
void createEmpty()
Create an empty image HDU with NAXIS=0 at the end of the file.
void setHdu(int hdu, bool relative=false)
Set the current HDU.
bool checkCompressedImagePhu()
Go to the first image header in the FITS file.
int countHdus()
Return the number of HDUs in the file.
std::string getFileName() const
Return the file name associated with the FITS object or "<unknown>" if there is none.
void setImageCompression(ImageCompressionOptions const &options)
Set compression options for writing FITS images.
int getHdu()
Return the current HDU (0-indexed; 0 is the Primary HDU).
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
Options for scaling image pixels.
ScalingAlgorithm algorithm
Scaling algorithm to use.
float quantizeLevel
Divisor of the standard deviation for STDEV_* scaling.
bool fuzz
Fuzz the values when quantising floating-point values?
float quantizePad
Number of stdev to allow on the low/high side (for STDEV_POSITIVE/NEGATIVE)
int bitpix
Bits per pixel (0, 8,16,32,64,-32,-64)
double bscale
Manually specified BSCALE (for MANUAL scaling)
std::vector< std::string > maskPlanes
Mask planes to ignore when doing statistics.
int seed
Seed for random number generator when fuzzing.
double bzero
Manually specified BZERO (for MANUAL scaling)
Lifetime-management for memory that goes into FITS memory files.
std::size_t getLength() const
Return the buffer length.
A class to represent a 2-dimensional array of pixels.
Represent a 2-dimensional array of bitmask pixels.
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.
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.
void declareImageScaleTemplates(py::class_< ImageScale > &cls, std::string const &suffix)
void declareImageScalingOptions(py::module &mod)
void declareImageCompression(py::module &mod)
void declareImageWriteOptions(py::module &mod)
void declareFits(py::module &mod)
void declareImageScalingOptionsTemplates(py::class_< ImageScalingOptions > &cls)
PYBIND11_MODULE(fits, mod)
void declareImageScale(py::module &mod)
float quantizeLevel
quantization level: 0.0 = none requires use of GZIP or GZIP_SHUFFLE
CompressionAlgorithm algorithm
Compresion algorithm to use.
Tiles tiles
Tile size; a dimension with 0 means infinite (e.g., to specify one row: 0,1)
double bscale
Scale to apply when reading from FITS.
long blank
Value for integer images indicating non-finite values.
int bitpix
Bits per pixel; negative means floating-point: 8,16,32,64,-32,-64.
double bzero
Zero-point to apply when reading from FITS.
ImageCompressionOptions compression
Options controlling compression.
ImageScalingOptions scaling
Options controlling scaling.
static std::shared_ptr< daf::base::PropertySet > validate(daf::base::PropertySet const &config)
Validate a PropertySet.