13#ifndef RTCTK_COMPONENTFRAMEWORK_FITSIOFUNCTIONS_HPP
14#define RTCTK_COMPONENTFRAMEWORK_FITSIOFUNCTIONS_HPP
26#include <fmt/format.h>
42void IdentifyCfitsioTypes(
int& bitpix,
int& datatype) {
43 if constexpr (std::is_integral_v<T>) {
44 if constexpr (std::is_same_v<T, bool>) {
47 }
else if constexpr (
sizeof(T) == 1) {
48 if constexpr (std::is_signed_v<T>) {
55 }
else if constexpr (
sizeof(T) == 2) {
56 if constexpr (std::is_signed_v<T>) {
63 }
else if constexpr (
sizeof(T) == 4) {
64 if constexpr (std::is_signed_v<T>) {
66 if constexpr (
sizeof(int) == 4) {
68 }
else if constexpr (
sizeof(long) == 4) {
71 static_assert(
sizeof(int) == 4 or
sizeof(
long) == 4,
72 "Require either int or long type to be 32 bits wide.");
76 if constexpr (
sizeof(
unsigned int) == 4) {
78 }
else if constexpr (
sizeof(
unsigned long) == 4) {
82 sizeof(
unsigned int) == 4 or
sizeof(
unsigned long) == 4,
83 "Require either unsigned int or unsigned long type to be 32 bits wide.");
86 }
else if constexpr (
sizeof(T) == 8) {
87 if constexpr (std::is_signed_v<T>) {
88 bitpix = LONGLONG_IMG;
92 bitpix = ULONGLONG_IMG;
93 datatype = TULONGLONG;
97 bitpix = LONGLONG_IMG;
103 sizeof(T) == 1 or
sizeof(T) == 2 or
sizeof(T) == 4 or
sizeof(T) == 8,
104 "The integer type used as the template parameter must be one of 8, 16, 32, or 64"
108 if constexpr (std::is_floating_point_v<T>) {
109 if constexpr (
sizeof(T) == 4) {
112 }
else if constexpr (
sizeof(T) == 8) {
117 sizeof(T) == 4 or
sizeof(T) == 8,
118 "The floating-point type used as the template parameter must be float or double.");
121 static_assert(std::is_arithmetic_v<T>,
122 "The template parameter used must be an integer or floating-point type.");
167 "The matrix argument must be of type MatrixBuffer or MatrixSpan.");
171 using U =
typename T::value_type;
184 std::string
msg = fmt::format(
196 std::string
msg = fmt::format(
"Failed to create the image type from FITS file '{}'. {}",
203 char value[80] =
"true";
205 std::string
msg = fmt::format(
"Failed to write keyword BOOLITEM to '{}'. {}",
217 void*
array =
const_cast<void*
>(
reinterpret_cast<const void*
>(
matrix.data()));
220 std::string
msg = fmt::format(
"Failed to write the matrix to FITS file '{}'. {}",
232 fmt::format(
"Failed to close file '{}' while handling an exception. {}",
241 std::string
msg = fmt::format(
251 }
catch (
const std::exception& error) {
269 "The matrix argument must be of type MatrixBuffer or MatrixSpan.");
271 using U =
typename T::value_type;
292 std::string
msg = fmt::format(
"Failed to read the image type from FITS file '{}'. {}",
304 std::string
msg = fmt::format(
"Failed to read keyword BZERO from '{}'. {}",
309 if (std::string(value) ==
310 std::to_string(std::numeric_limits<uint64_t>::max() / 2 + 1)) {
321 std::string
msg = fmt::format(
322 "The FITS file '{}' has the wrong image format of {}. Expected a FITS image of "
335 fmt::format(
"Failed to read the number of image axes from FITS file '{}'. {}",
341 std::string
msg = fmt::format(
342 "The FITS file '{}' has image dimensions that we cannot handle. Expect a 2D image "
343 "when loading as a matrix.",
348 long size[2] = {-1, -1};
351 std::string
msg = fmt::format(
"Failed to read the image size from FITS file '{}'. {}",
376 std::string
msg = fmt::format(
"Failed to read the image from FITS file '{}'. {}",
388 fmt::format(
"Failed to close file '{}' while handling an exception. {}",
397 std::string
msg = fmt::format(
414 "The vector argument must be of type std::vector or gsl::span.");
417 using U =
typename T::value_type;
430 std::string
msg = fmt::format(
437 long size =
static_cast<long>(vector.size());
440 std::string
msg = fmt::format(
"Failed to create the image type from FITS file '{}'. {}",
447 char value[80] =
"true";
449 std::string
msg = fmt::format(
"Failed to write keyword BOOLITEM to '{}'. {}",
461 void*
array =
const_cast<void*
>(
reinterpret_cast<const void*
>(vector.data()));
464 std::string
msg = fmt::format(
"Failed to write the vector to FITS file '{}'. {}",
476 fmt::format(
"Failed to close file '{}' while handling an exception. {}",
485 std::string
msg = fmt::format(
495 }
catch (
const std::exception& error) {
513 "The vector argument must be of type std::vector or gsl::span.");
515 using U =
typename T::value_type;
536 std::string
msg = fmt::format(
"Failed to read the image type from FITS file '{}'. {}",
548 std::string
msg = fmt::format(
"Failed to read keyword BZERO from '{}'. {}",
553 if (std::string(value) ==
554 std::to_string(std::numeric_limits<uint64_t>::max() / 2 + 1)) {
565 std::string
msg = fmt::format(
566 "The FITS file '{}' has the wrong image format of {}. Expected a FITS image of "
579 fmt::format(
"Failed to read the number of image axes from FITS file '{}'. {}",
585 std::string
msg = fmt::format(
586 "The FITS file '{}' has image dimensions that we cannot handle. Expect a 1D image "
587 "when loading as a vector.",
596 fmt::format(
"Failed to read the 1D image size from FITS file '{}'. {}",
612 void*
array = vector.data();
617 std::string
msg = fmt::format(
"Failed to read the image from FITS file '{}'. {}",
629 fmt::format(
"Failed to close file '{}' while handling an exception. {}",
638 std::string
msg = fmt::format(
686 for (std::vector<int8_t>::size_type
n = 0;
n <
int_vector.size(); ++
n) {
701 for (std::vector<int8_t>::size_type
n = 0;
n <
int_vector.size(); ++
n) {
709#pragma GCC diagnostic push
710#pragma GCC diagnostic ignored "-Wdangling-reference"
728#pragma GCC diagnostic pop
The BufferTooSmall is thrown when an API call fails because the provided buffer is not big enough to ...
Definition exceptions.hpp:264
A buffer class representing 2D matrix data.
Definition matrixBuffer.hpp:28
size_type GetNcols() const
Definition matrixBuffer.hpp:101
size_type GetNrows() const
Definition matrixBuffer.hpp:97
constexpr void resize(size_type n, size_type m)
Definition matrixBuffer.hpp:72
The RtctkException class is the base class for all Rtctk exceptions.
Definition exceptions.hpp:211
Provides macros and utilities for exception handling.
Logging Support Library based on log4cplus.
Declaration of the MatrixBuffer template class used in APIs.
Definition commandReplier.cpp:22
std::string GetCfitsioErrorMsg(int status)
Helper function to convert a Cfitsio status code to a human readable message.
Definition fitsIoFunctions.cpp:20
void ReadVectorFromFits(const std::string &filename, T &vector)
Reads a FITS file containing a 1D image into a buffer object representing a vector.
Definition fitsIoFunctions.hpp:511
void WriteMatrixToFits(const std::string &filename, const T &matrix, bool boolean_items=false)
Writes data representing a matrix as an image to a FITS file.
Definition fitsIoFunctions.hpp:165
void ReadMatrixFromFits(const std::string &filename, T &matrix)
Reads a FITS file image into a buffer object representing a matrix.
Definition fitsIoFunctions.hpp:267
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition logger.cpp:193
std::string CfitsioDataTypeToString(int datatype)
Returns a string representation of a Cfitsio data type code.
Definition fitsIoFunctions.cpp:58
std::vector< uint64_t > GetFitsImageShape(const std::string &filename)
Get the shape of a FITS image.
Definition fitsIoFunctions.cpp:271
std::string CfitsioImageTypeToString(int bitpix)
Returns a string representation of a Cfitsio image type code.
Definition fitsIoFunctions.cpp:29
void WriteVectorToFits(const std::string &filename, const T &vector, bool boolean_items=false)
Writes data as a 1D image to a FITS file.
Definition fitsIoFunctions.hpp:412
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23
const std::type_info & GetFitsImageType(const std::string &filename)
Get the C++ type corresponding to a FITS image.
Definition fitsIoFunctions.cpp:101
std::size_t GetFitsImageSize(const std::string &filename)
Get the number of pixels in a FITS image.
Definition fitsIoFunctions.cpp:333
Provides useful mechanisms to test various type traits.