8#include <unordered_set>
9#include <unordered_map>
18#include "boost/algorithm/string.hpp"
19#include "boost/preprocessor/seq/for_each.hpp"
20#include "boost/format.hpp"
49 daf::base::PropertySet
const &metadata) {
51 for (
auto const &fullName : paramNames) {
53 auto name = (lastPeriod == std::string::npos) ? fullName : fullName.substr(lastPeriod + 1);
58 if (
name.size() > 8) {
63 if (
type ==
typeid(
bool)) {
64 out += metadata.get<
bool>(
name) ?
"T" :
"F";
67 }
else if (
type ==
typeid(
int)) {
69 }
else if (
type ==
typeid(
double)) {
70 double value = metadata.get<
double>(
name);
77 BOOST_CURRENT_FUNCTION %
name);
81 }
else if (
type ==
typeid(
float)) {
82 float value = metadata.get<
float>(
name);
88 BOOST_CURRENT_FUNCTION %
name);
96 if (out.
size() > 80) {
101 int const len = out.
size();
104 }
else if (len > 80) {
107 "Formatted data too long: " +
std::to_string(len) +
" > 80: \"" + out +
"\"");
124class StringStartSet {
128 for (
auto const &word : input) {
130 if (size < _minSize) {
134 for (
auto const &word : input) {
136 assert(_words.
count(start) == 0);
137 _words[start] = word;
143 auto const iter = _words.
find(startString(key));
169 "SIMPLE",
"BITPIX",
"NAXIS",
"EXTEND",
"GCOUNT",
"PCOUNT",
"XTENSION",
"TFIELDS",
"BSCALE",
"BZERO",
171 "ZBITPIX",
"ZIMAGE",
"ZCMPTYPE",
"ZSIMPLE",
"ZEXTEND",
"ZBLANK",
"ZDATASUM",
"ZHECKSUM",
"ZQUANTIZ",
173 "DATASUM",
"CHECKSUM"};
180StringStartSet
const ignoreKeyStarts{
181 "NAXIS",
"TZERO",
"TSCAL",
183 "ZNAXIS",
"ZTILE",
"ZNAME",
"ZVAL"};
190StringStartSet
const ignoreKeyStartsWrite{
"TFORM",
"TTYPE"};
194 if (s.
empty())
return s;
197 return s.
substr(i1, (i1 == std::string::npos) ? 0 : 1 + i2 - i1);
202char getFormatCode(
bool *) {
return 'X'; }
211char getFormatCode(
float *) {
return 'E'; }
212char getFormatCode(
double *) {
return 'D'; }
222 return (
boost::format(
"%d%c") % size % getFormatCode((
T *)
nullptr)).str();
223 }
else if (size < 0) {
225 return (
boost::format(
"1Q%c(%d)") % getFormatCode((
T *)
nullptr) % (-size)).str();
228 return (
boost::format(
"1Q%c") % getFormatCode((
T *)
nullptr)).str();
238struct FitsType<bool> {
239 static int const CONSTANT = TLOGICAL;
242struct FitsType<
char> {
243 static int const CONSTANT = TSTRING;
246struct FitsType<signed
char> {
247 static int const CONSTANT = TSBYTE;
250struct FitsType<unsigned
char> {
251 static int const CONSTANT = TBYTE;
254struct FitsType<
short> {
255 static int const CONSTANT = TSHORT;
258struct FitsType<unsigned
short> {
259 static int const CONSTANT = TUSHORT;
262struct FitsType<
int> {
263 static int const CONSTANT = TINT;
266struct FitsType<unsigned
int> {
267 static int const CONSTANT = TUINT;
270struct FitsType<
long> {
271 static int const CONSTANT = TLONG;
274struct FitsType<unsigned
long> {
275 static int const CONSTANT = TULONG;
279 static int const CONSTANT = TLONGLONG;
282struct FitsType<unsigned
long long> {
283 static int const CONSTANT = TLONGLONG;
286struct FitsType<
float> {
287 static int const CONSTANT = TFLOAT;
291 static int const CONSTANT = TDOUBLE;
295 static int const CONSTANT = TDOUBLE;
298struct FitsType<
std::complex<float> > {
299 static int const CONSTANT = TCOMPLEX;
302struct FitsType<
std::complex<double> > {
303 static int const CONSTANT = TDBLCOMPLEX;
308struct FitsTableType :
public FitsType<T> {};
310struct FitsTableType<bool> {
311 static int const CONSTANT = TBIT;
318struct FitsBitPix<unsigned
char> {
319 static int const CONSTANT = BYTE_IMG;
322struct FitsBitPix<
short> {
323 static int const CONSTANT = SHORT_IMG;
326struct FitsBitPix<unsigned
short> {
327 static int const CONSTANT = USHORT_IMG;
330struct FitsBitPix<
int> {
331 static int const CONSTANT = LONG_IMG;
334struct FitsBitPix<unsigned
int> {
335 static int const CONSTANT = ULONG_IMG;
338struct FitsBitPix<
std::int64_t> {
339 static int const CONSTANT = LONGLONG_IMG;
342struct FitsBitPix<
std::uint64_t> {
343 static int const CONSTANT = LONGLONG_IMG;
346struct FitsBitPix<
float> {
347 static int const CONSTANT = FLOAT_IMG;
350struct FitsBitPix<
double> {
351 static int const CONSTANT = DOUBLE_IMG;
354bool isFitsImageTypeSigned(
int constant) {
356 case BYTE_IMG:
return false;
357 case SHORT_IMG:
return true;
358 case USHORT_IMG:
return false;
359 case LONG_IMG:
return true;
360 case ULONG_IMG:
return false;
361 case LONGLONG_IMG:
return true;
362 case FLOAT_IMG:
return true;
363 case DOUBLE_IMG:
return true;
365 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"Invalid constant.");
368static bool allowImageCompression =
true;
370int fitsTypeForBitpix(
int bitpix) {
386 os <<
"Invalid bitpix value: " << bitpix;
387 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
os.str());
409ItemInfo isCommentIsValid(daf::base::PropertyList
const &pl,
std::string const &
name) {
410 if (!pl.exists(
name)) {
411 return ItemInfo(
false,
false);
414 if ((
name ==
"COMMENT") || (
name ==
"HISTORY")) {
417 return ItemInfo(
false,
true);
428 os <<
"cfitsio error";
429 if (fileName !=
"") {
430 os <<
" (" << fileName <<
")";
433 char fitsErrMsg[FLEN_ERRMSG];
434 fits_get_errstatus(status, fitsErrMsg);
435 os <<
": " << fitsErrMsg <<
" (" << status <<
")";
440 os <<
"\ncfitsio error stack:\n";
441 char cfitsioMsg[FLEN_ERRMSG];
442 while (fits_read_errmsg(cfitsioMsg) != 0) {
443 os <<
" " << cfitsioMsg <<
"\n";
450 fitsfile *fd =
reinterpret_cast<fitsfile *
>(fptr);
451 if (fd !=
nullptr && fd->Fptr !=
nullptr && fd->Fptr->filename !=
nullptr) {
452 fileName = fd->Fptr->filename;
467 for (
auto const &
name : allParamNames) {
472 return makeLimitedFitsHeaderImpl(desiredParamNames, metadata);
491 return FitsBitPix<T>::CONSTANT;
500 fitsfile *fd =
reinterpret_cast<fitsfile *
>(
fptr);
501 if (fd !=
nullptr && fd->Fptr !=
nullptr && fd->Fptr->filename !=
nullptr) {
502 fileName = fd->Fptr->filename;
509 fits_get_hdu_num(
reinterpret_cast<fitsfile *
>(
fptr), &n);
515 fits_movrel_hdu(
reinterpret_cast<fitsfile *
>(
fptr), hdu,
nullptr, &
status);
521 fits_movabs_hdu(
reinterpret_cast<fitsfile *
>(
fptr), hdu + 1,
nullptr, &
status);
526 fits_movrel_hdu(
reinterpret_cast<fitsfile *
>(
fptr), 1,
nullptr, &tmpStatus);
536 fits_get_num_hdus(
reinterpret_cast<fitsfile *
>(
fptr), &n, &
status);
573double stringToNonFiniteDouble(
std::string const &value) {
574 if (value ==
"NAN") {
577 if (value ==
"+INFINITY") {
580 if (value ==
"-INFINITY") {
587void updateKeyImpl(Fits &
fits,
char const *key,
T const &value,
char const *comment) {
588 fits_update_key(
reinterpret_cast<fitsfile *
>(
fits.
fptr), FitsType<T>::CONSTANT,
const_cast<char *
>(key),
589 const_cast<T *
>(&value),
const_cast<char *
>(comment), &
fits.
status);
592void updateKeyImpl(Fits &
fits,
char const *key,
std::string const &value,
char const *comment) {
593 fits_update_key_longstr(
reinterpret_cast<fitsfile *
>(
fits.
fptr),
const_cast<char *
>(key),
594 const_cast<char *
>(value.c_str()),
const_cast<char *
>(comment), &
fits.
status);
597void updateKeyImpl(Fits &
fits,
char const *key,
bool const &value,
char const *comment) {
599 fits_update_key(
reinterpret_cast<fitsfile *
>(
fits.
fptr), TLOGICAL,
const_cast<char *
>(key), &v,
603void updateKeyImpl(Fits &
fits,
char const *key,
double const &value,
char const *comment) {
604 std::string strValue = nonFiniteDoubleToString(value);
605 if (!strValue.
empty()) {
606 updateKeyImpl(
fits, key, strValue, comment);
608 fits_update_key(
reinterpret_cast<fitsfile *
>(
fits.
fptr), FitsType<double>::CONSTANT,
609 const_cast<char *
>(key),
const_cast<double *
>(&value),
const_cast<char *
>(comment),
615void writeKeyImpl(Fits &
fits,
char const *key,
T const &value,
char const *comment) {
616 fits_write_key(
reinterpret_cast<fitsfile *
>(
fits.
fptr), FitsType<T>::CONSTANT,
const_cast<char *
>(key),
617 const_cast<T *
>(&value),
const_cast<char *
>(comment), &
fits.
status);
620void writeKeyImpl(Fits &
fits,
char const *key,
char const *comment) {
622 fits_write_key_null(
reinterpret_cast<fitsfile *
>(
fits.
fptr),
const_cast<char *
>(key),
626void writeKeyImpl(Fits &
fits,
char const *key,
std::string const &value,
char const *comment) {
627 if (
strncmp(key,
"COMMENT", 7) == 0) {
628 fits_write_comment(
reinterpret_cast<fitsfile *
>(
fits.
fptr),
const_cast<char *
>(value.c_str()),
630 }
else if (
strncmp(key,
"HISTORY", 7) == 0) {
631 fits_write_history(
reinterpret_cast<fitsfile *
>(
fits.
fptr),
const_cast<char *
>(value.c_str()),
634 fits_write_key_longstr(
reinterpret_cast<fitsfile *
>(
fits.
fptr),
const_cast<char *
>(key),
635 const_cast<char *
>(value.c_str()),
const_cast<char *
>(comment), &
fits.
status);
639void writeKeyImpl(Fits &
fits,
char const *key,
bool const &value,
char const *comment) {
641 fits_write_key(
reinterpret_cast<fitsfile *
>(
fits.
fptr), TLOGICAL,
const_cast<char *
>(key), &v,
645void writeKeyImpl(Fits &
fits,
char const *key,
double const &value,
char const *comment) {
646 std::string strValue = nonFiniteDoubleToString(value);
647 if (!strValue.
empty()) {
648 writeKeyImpl(
fits, key, strValue, comment);
650 fits_write_key(
reinterpret_cast<fitsfile *
>(
fits.
fptr), FitsType<double>::CONSTANT,
651 const_cast<char *
>(key),
const_cast<double *
>(&value),
const_cast<char *
>(comment),
660 updateKeyImpl(*
this, key.
c_str(), value, comment.
c_str());
668 writeKeyImpl(*
this, key.
c_str(), value, comment.
c_str());
676 updateKeyImpl(*
this, key.
c_str(), value,
nullptr);
684 writeKeyImpl(*
this, key.
c_str(), value,
nullptr);
727void readKeyImpl(
Fits &
fits,
char const *key,
T &value) {
728 fits_read_key(
reinterpret_cast<fitsfile *
>(
fits.
fptr), FitsType<T>::CONSTANT,
const_cast<char *
>(key),
734 fits_read_key_longstr(
reinterpret_cast<fitsfile *
>(
fits.
fptr),
const_cast<char *
>(key), &buf,
nullptr,
742void readKeyImpl(Fits &
fits,
char const *key,
double &value) {
746 char buf[FLEN_VALUE];
747 fits_read_keyword(
reinterpret_cast<fitsfile *
>(
fits.
fptr),
const_cast<char *
>(key), buf,
nullptr, &
fits.
status);
753 readKeyImpl(
fits, key, unquoted);
757 value = stringToNonFiniteDouble(unquoted);
760 afw::fits::FitsError,
761 (
boost::format(
"Unrecognised string value for keyword '%s' when parsing as double: %s") %
766 fits_read_key(
reinterpret_cast<fitsfile *
>(
fits.
fptr), FitsType<double>::CONSTANT,
767 const_cast<char *
>(key), &value,
nullptr, &
fits.
status);
775 readKeyImpl(*
this, key.
c_str(), value);
786 fits_get_hdrspace(
reinterpret_cast<fitsfile *
>(
fptr), &nKeys,
nullptr, &
status);
792 fits_read_keyn(
reinterpret_cast<fitsfile *
>(
fptr), i, key, value, comment, &
status);
796 boost::to_upper(upperKey);
797 if (upperKey.
compare(key) != 0){
799 boost::format(
"In %s, standardizing key '%s' to uppercase '%s' on read.") %
800 BOOST_CURRENT_FUNCTION % key % upperKey);
804 commentStr = comment;
806 while (valueStr.
size() > 2 && valueStr[valueStr.
size() - 2] ==
'&' && i <= nKeys) {
808 fits_read_record(
reinterpret_cast<fitsfile *
>(
fptr), i, key, &
status);
809 if (strncmp(key,
"CONTINUE", 8) != 0) {
816 if (firstQuote == std::string::npos) {
821 boost::format(
"Invalid CONTINUE at header key %d: \"%s\".") % i % card));
824 if (lastQuote == std::string::npos) {
829 boost::format(
"Invalid CONTINUE at header key %d: \"%s\".") % i % card));
831 valueStr += card.
substr(firstQuote + 1, lastQuote - firstQuote);
833 if (slash != std::string::npos) {
841 functor(keyStr, valueStr, commentStr);
850 return ((ignoreKeys.
find(key) != ignoreKeys.
end()) || ignoreKeyStarts.matches(key) ||
851 (
write && ignoreKeyStartsWrite.matches(key)));
854class MetadataIterationFunctor :
public HeaderIterationFunctor {
858 template <
typename T>
864 if (
list->exists(key) &&
list->isUndefined(key)) {
866 boost::format(
"In %s, replacing undefined value for key '%s'.") %
867 BOOST_CURRENT_FUNCTION % key);
868 list->set(key, value, comment);
870 list->add(key, value, comment);
873 if (
set->exists(key) &&
set->isUndefined(key)) {
875 boost::format(
"In %s, replacing undefined value for key '%s'.") %
876 BOOST_CURRENT_FUNCTION % key);
877 set->set(key, value);
879 set->add(key, value);
889 if (
list->exists(key) && !
list->isUndefined(key)) {
893 boost::format(
"In %s, dropping undefined value for key '%s'.") %
894 BOOST_CURRENT_FUNCTION % key);
896 list->add(key,
nullptr, comment);
899 if (
set->exists(key) && !
set->isUndefined(key)) {
903 boost::format(
"In %s, dropping undefined value for key '%s'.") %
904 BOOST_CURRENT_FUNCTION % key);
906 set->add(key,
nullptr);
912 daf::base::PropertySet *
set;
919 static std::regex const intRegex(
"[+-]?[0-9]+");
920 static std::regex const doubleRegex(
"[+-]?([0-9]*\\.?[0-9]+|[0-9]+\\.?[0-9]*)([eE][+-]?[0-9]+)?");
921 static std::regex const fitsStringRegex(
"'(.*?) *'");
923 static std::regex const fitsDefinitionCommentRegex(
924 " *(FITS \\(Flexible Image Transport System\\)|and Astrophysics', volume 376, page 359).*");
927 if (
strip && isKeyIgnored(key)) {
934 add(key,
bool(value ==
"T" || value ==
"t"), comment);
939 if (
val < (1LL << 31) &&
val > -(1LL << 31)) {
940 add(key,
static_cast<int>(
val), comment);
942 add(key,
val, comment);
948 add(key,
val, comment);
951 double val = stringToNonFiniteDouble(
str);
953 add(key,
val, comment);
955 add(key,
str, comment);
957 }
else if (key ==
"HISTORY") {
958 add(key, comment,
"");
960 add(key, comment,
"");
961 }
else if (key.
empty() && value.empty()) {
966 add(
"COMMENT", comment,
"");
967 }
else if (value.empty()) {
970 if (key !=
"COMMENT") {
975 afw::fits::FitsError,
976 (
boost::format(
"Could not parse header value for key '%s': '%s'") % key % value).
str());
980void writeKeyFromProperty(Fits &
fits, daf::base::PropertySet
const &metadata,
std::string const &key,
981 char const *comment =
nullptr) {
983 boost::to_upper(upperKey);
984 if (upperKey.compare(key) != 0){
986 boost::format(
"In %s, key '%s' may be standardized to uppercase '%s' on write.") %
987 BOOST_CURRENT_FUNCTION % key % upperKey);
990 if (valueType ==
typeid(
bool)) {
991 if (metadata.isArray(key)) {
995 writeKeyImpl(
fits, key.
c_str(),
static_cast<bool>(tmp[i]), comment);
998 writeKeyImpl(
fits, key.
c_str(), metadata.get<
bool>(key), comment);
1001 if (metadata.isArray(key)) {
1004 writeKeyImpl(
fits, key.
c_str(), tmp[i], comment);
1009 }
else if (valueType ==
typeid(
int)) {
1010 if (metadata.isArray(key)) {
1013 writeKeyImpl(
fits, key.
c_str(), tmp[i], comment);
1016 writeKeyImpl(
fits, key.
c_str(), metadata.get<
int>(key), comment);
1018 }
else if (valueType ==
typeid(
long)) {
1019 if (metadata.isArray(key)) {
1022 writeKeyImpl(
fits, key.
c_str(), tmp[i], comment);
1025 writeKeyImpl(
fits, key.
c_str(), metadata.get<
long>(key), comment);
1027 }
else if (valueType ==
typeid(
long long)) {
1028 if (metadata.isArray(key)) {
1031 writeKeyImpl(
fits, key.
c_str(), tmp[i], comment);
1034 writeKeyImpl(
fits, key.
c_str(), metadata.get<
long long>(key), comment);
1037 if (metadata.isArray(key)) {
1040 writeKeyImpl(
fits, key.
c_str(), tmp[i], comment);
1045 }
else if (valueType ==
typeid(
double)) {
1046 if (metadata.isArray(key)) {
1049 writeKeyImpl(
fits, key.
c_str(), tmp[i], comment);
1052 writeKeyImpl(
fits, key.
c_str(), metadata.get<
double>(key), comment);
1055 if (metadata.isArray(key)) {
1058 writeKeyImpl(
fits, key.
c_str(), tmp[i], comment);
1064 if (metadata.isArray(key)) {
1068 writeKeyImpl(
fits, key.
c_str(), comment);
1071 writeKeyImpl(
fits, key.
c_str(), comment);
1075 LOGLS_WARN(
"lsst.afw.fits.writeKeyFromProperty",
1078 BOOST_CURRENT_FUNCTION % valueType.
name() % key));
1088 MetadataIterationFunctor f;
1098 NameList paramNames;
1104 for (
auto const ¶mName : paramNames) {
1105 if (!isKeyIgnored(paramName,
true)) {
1107 writeKeyFromProperty(*
this, metadata, paramName, pl->
getComment(paramName).
c_str());
1109 writeKeyFromProperty(*
this, metadata, paramName);
1118 char *ttype =
nullptr;
1119 char *tform =
nullptr;
1120 fits_create_tbl(
reinterpret_cast<fitsfile *
>(
fptr), BINARY_TBL, 0, 0, &ttype, &tform,
nullptr,
nullptr, &
status);
1126template <
typename T>
1129 fits_get_num_cols(
reinterpret_cast<fitsfile *
>(
fptr), &nCols, &
status);
1131 fits_insert_col(
reinterpret_cast<fitsfile *
>(
fptr), nCols + 1,
const_cast<char *
>(ttype.
c_str()),
1139template <
typename T>
1141 int nCols = addColumn<T>(ttype, size);
1151 fits_get_num_rows(
reinterpret_cast<fitsfile *
>(
fptr), &
first, &
status);
1152 fits_insert_rows(
reinterpret_cast<fitsfile *
>(
fptr),
first, nRows, &
status);
1161 fits_get_num_rows(
reinterpret_cast<fitsfile *
>(
fptr), &r, &
status);
1168template <
typename T>
1170 fits_write_col(
reinterpret_cast<fitsfile *
>(
fptr), FitsTableType<T>::CONSTANT,
col + 1,
row + 1, 1,
1171 nElements,
const_cast<T *
>(value), &
status);
1183 char const *tmp = value.c_str();
1184 fits_write_col(
reinterpret_cast<fitsfile *
>(
fptr), TSTRING,
col + 1,
row + 1, 1, 1,
1185 const_cast<char const **
>(&tmp), &
status);
1191template <
typename T>
1194 fits_read_col(
reinterpret_cast<fitsfile *
>(
fptr), FitsTableType<T>::CONSTANT,
col + 1,
row + 1, 1,
1195 nElements,
nullptr, value, &anynul, &
status);
1208 char *tmp = &buf.
front();
1209 fits_read_col(
reinterpret_cast<fitsfile *
>(
fptr), TSTRING,
col + 1,
row + 1, 1, 1,
nullptr, &tmp, &anynul,
1221 fits_get_coltype(
reinterpret_cast<fitsfile *
>(
fptr),
col + 1, &typecode, &
result, &width, &
status);
1242 fits_create_img(
reinterpret_cast<fitsfile *
>(
fptr), 8, 0, &naxes, &
status);
1248void Fits::createImageImpl(
int bitpix,
int naxis,
long const *naxes) {
1249 fits_create_img(
reinterpret_cast<fitsfile *
>(
fptr), bitpix, naxis,
const_cast<long *
>(naxes), &
status);
1255template <
typename T>
1256void Fits::writeImageImpl(
T const *
data,
int nElements) {
1257 fits_write_img(
reinterpret_cast<fitsfile *
>(
fptr), FitsType<T>::CONSTANT, 1, nElements,
1270struct ImageCompressionContext {
1272 ImageCompressionContext(Fits &fits_, ImageCompressionOptions
const &useThis)
1273 :
fits(fits_), old(
fits.getImageCompression()) {
1274 fits.setImageCompression(useThis);
1276 ~ImageCompressionContext() {
1280 fits.setImageCompression(old);
1290 ImageCompressionOptions old;
1295template <
typename T>
1299 auto fits =
reinterpret_cast<fitsfile *
>(
fptr);
1301 image.getBBox().getArea() > 0
1305 ImageCompressionContext comp(*
this, compression);
1313 ndarray::Vector<long, 2> dims(
image.getArray().getShape().reverse());
1322 fullMetadata->combine(*wcsMetadata);
1324 fullMetadata = wcsMetadata;
1347 int const fitsType =
scale.bitpix == 0 ? FitsType<T>::CONSTANT : fitsTypeForBitpix(
scale.bitpix);
1348 fits_write_img(
fits, fitsType, 1, pixels->getNumElements(),
const_cast<void *
>(pixels->getData()),
1360 if (
scale.bzero != 0.0) {
1361 fits_write_key_lng(
fits,
"BZERO",
static_cast<long>(
scale.bzero),
1362 "Scaling: MEMORY = BZERO + BSCALE * DISK", &
status);
1364 if (
scale.bscale != 1.0) {
1365 fits_write_key_lng(
fits,
"BSCALE",
static_cast<long>(
scale.bscale),
1366 "Scaling: MEMORY = BZERO + BSCALE * DISK", &
status);
1369 fits_write_key_dbl(
fits,
"BZERO",
scale.bzero, 12,
"Scaling: MEMORY = BZERO + BSCALE * DISK",
1371 fits_write_key_dbl(
fits,
"BSCALE",
scale.bscale, 12,
"Scaling: MEMORY = BZERO + BSCALE * DISK",
1380 fits_write_key_lng(
fits,
"BLANK",
scale.blank,
"Value for undefined pixels", &
status);
1382 fits_write_key_str(
fits,
"ZQUANTIZ",
"SUBTRACTIVE_DITHER_1",
"Dithering algorithm", &
status);
1397template <
typename T>
1407template <
typename T,
class Enable =
void>
1409 static T constexpr value = 0;
1413template <
typename T>
1414struct NullValue<
T, typename
std::enable_if<std::numeric_limits<T>::has_quiet_NaN>
::type> {
1420template <
typename T>
1421void Fits::readImageImpl(
int nAxis,
T *
data,
long *begin,
long *
end,
long *increment) {
1422 T null = NullValue<T>::value;
1424 fits_read_subset(
reinterpret_cast<fitsfile *
>(
fptr), FitsType<T>::CONSTANT, begin,
end, increment,
1425 reinterpret_cast<void *
>(&
null),
data, &anyNulls, &
status);
1431 fits_get_img_dim(
reinterpret_cast<fitsfile *
>(
fptr), &nAxis, &
status);
1436void Fits::getImageShapeImpl(
int maxDim,
long *nAxes) {
1437 fits_get_img_size(
reinterpret_cast<fitsfile *
>(
fptr), maxDim, nAxes, &
status);
1441template <
typename T>
1444 fits_get_img_equivtype(
reinterpret_cast<fitsfile *
>(
fptr), &imageType, &
status);
1447 if (imageType < 0) {
1451 if (isFitsImageTypeSigned(imageType)) {
1452 return FitsBitPix<T>::CONSTANT >= imageType;
1455 return FitsBitPix<T>::CONSTANT > imageType;
1458 if (!isFitsImageTypeSigned(imageType)) {
1459 return FitsBitPix<T>::CONSTANT >= imageType;
1460 }
else if (imageType == LONGLONG_IMG) {
1463 return FitsBitPix<T>::CONSTANT >= imageType;
1475 fits_get_img_equivtype(
reinterpret_cast<fitsfile *
>(
fptr), &bitpix, &
status);
1488 case BYTE_IMG:
return "uint8";
1489 case SBYTE_IMG:
return "int8";
1490 case SHORT_IMG:
return "int16";
1491 case USHORT_IMG:
return "uint16";
1492 case LONG_IMG:
return "int32";
1493 case ULONG_IMG:
return "uint32";
1494 case LONGLONG_IMG:
return "int64";
1503 auto fits =
reinterpret_cast<fitsfile *
>(
fptr);
1505 fits_get_compression_type(
fits, &compType, &
status);
1511 fits_get_tile_dim(
fits, tiles.getNumElements(), tiles.getData(), &
status);
1516 float quantizeLevel;
1517 fits_get_quantize_level(
fits, &quantizeLevel, &
status);
1526 auto fits =
reinterpret_cast<fitsfile *
>(
fptr);
1527 fits_unset_compression_request(
fits, &
status);
1560 : fptr(nullptr), status(0), behavior(behavior_) {
1561 if (mode ==
"r" || mode ==
"rb") {
1562 fits_open_file(
reinterpret_cast<fitsfile **
>(&
fptr),
const_cast<char *
>(filename.
c_str()), READONLY,
1564 }
else if (mode ==
"w" || mode ==
"wb") {
1565 std::filesystem::remove(filename);
1566 fits_create_file(
reinterpret_cast<fitsfile **
>(&
fptr),
const_cast<char *
>(filename.
c_str()), &
status);
1567 }
else if (mode ==
"a" || mode ==
"ab") {
1568 fits_open_file(
reinterpret_cast<fitsfile **
>(&
fptr),
const_cast<char *
>(filename.
c_str()), READWRITE,
1571 fits_get_num_hdus(
reinterpret_cast<fitsfile *
>(
fptr), &nHdu, &
status);
1572 fits_movabs_hdu(
reinterpret_cast<fitsfile *
>(
fptr), nHdu,
nullptr, &
status);
1577 fits_close_file(
reinterpret_cast<fitsfile *
>(
fptr), &tmpStatus);
1582 (
boost::format(
"Invalid mode '%s' given when opening file '%s'") % mode % filename).
str());
1590 : fptr(nullptr), status(0), behavior(behavior_) {
1591 using Reallocator =
void *(*)(
void *,
std::size_t);
1594 if (mode ==
"r" || mode ==
"rb") {
1595 fits_open_memfile(
reinterpret_cast<fitsfile **
>(&
fptr),
"unused", READONLY, &manager._ptr,
1596 &manager._len, 0,
nullptr,
1598 }
else if (mode ==
"w" || mode ==
"wb") {
1599 Reallocator reallocator =
nullptr;
1601 fits_create_memfile(
reinterpret_cast<fitsfile **
>(&
fptr), &manager._ptr, &manager._len, 0,
1604 }
else if (mode ==
"a" || mode ==
"ab") {
1605 Reallocator reallocator =
nullptr;
1607 fits_open_memfile(
reinterpret_cast<fitsfile **
>(&
fptr),
"unused", READWRITE, &manager._ptr,
1608 &manager._len, 0, reallocator, &
status);
1610 fits_get_num_hdus(
reinterpret_cast<fitsfile *
>(
fptr), &nHdu, &
status);
1611 fits_movabs_hdu(
reinterpret_cast<fitsfile *
>(
fptr), nHdu,
nullptr, &
status);
1616 fits_close_file(
reinterpret_cast<fitsfile *
>(
fptr), &tmpStatus);
1620 (
boost::format(
"Invalid mode '%s' given when opening memory file at '%s'") % mode %
1626 *
this,
boost::format(
"Opening memory file at '%s' with mode '%s'") % manager._ptr % mode);
1631 fits_close_file(
reinterpret_cast<fitsfile *
>(
fptr), &
status);
1638 auto combined = std::make_shared<daf::base::PropertyList>();
1639 bool const asScalar =
true;
1640 for (
auto const &
name :
first.getOrderedNames()) {
1641 auto const iscv = isCommentIsValid(
first,
name);
1642 if (iscv.isComment) {
1650 for (
auto const &
name :
second.getOrderedNames()) {
1651 auto const iscv = isCommentIsValid(
second,
name);
1652 if (iscv.isComment) {
1689 auto metadata = std::make_shared<lsst::daf::base::PropertyList>();
1692 int oldHdu = fitsfile.
getHdu();
1693 if (oldHdu != 0 && metadata->exists(
"INHERIT")) {
1694 bool inherit =
false;
1695 if (metadata->typeOf(
"INHERIT") ==
typeid(
std::string)) {
1696 inherit = (metadata->get<
std::string>(
"INHERIT") ==
"T");
1698 inherit = metadata->get<
bool>(
"INHERIT");
1700 if (
strip) metadata->remove(
"INHERIT");
1706 auto primaryHduMetadata = std::make_shared<daf::base::PropertyList>();
1711 auto const emptyMetadata = std::make_shared<lsst::daf::base::PropertyList>();
1721 _oldHdu(_fits.getHdu()),
1724 _fits.
setHdu(hdu, relative);
1746 auto fits =
reinterpret_cast<fitsfile *
>(
fptr);
1761 bool isCompressed = fits_is_compressed_image(
fits, &
status);
1768 return isCompressed;
1775 config.getAsDouble(
"compression.quantizeLevel")),
1777 config.getAsInt(
"scaling.bitpix"),
1778 config.exists(
"scaling.maskPlanes") ? config.getArray<
std::string>(
"scaling.maskPlanes")
1780 config.getAsInt(
"scaling.seed"), config.getAsDouble(
"scaling.quantizeLevel"),
1781 config.getAsDouble(
"scaling.quantizePad"), config.get<
bool>(
"scaling.fuzz"),
1782 config.getAsDouble(
"scaling.bscale"), config.getAsDouble(
"scaling.bzero")) {}
1786template <
typename T>
1792template <
typename T>
1793void validateEntry(daf::base::PropertySet &output, daf::base::PropertySet
const &input,
1795 output.add(
name, input.exists(
name) ? input.getArray<
T>(
name) : defaultValue);
1801 auto validated = std::make_shared<daf::base::PropertySet>();
1803 validateEntry(*validated, config,
"compression.algorithm",
std::string(
"NONE"));
1804 validateEntry(*validated, config,
"compression.columns", 0);
1805 validateEntry(*validated, config,
"compression.rows", 1);
1806 validateEntry(*validated, config,
"compression.quantizeLevel", 0.0);
1808 validateEntry(*validated, config,
"scaling.algorithm",
std::string(
"NONE"));
1809 validateEntry(*validated, config,
"scaling.bitpix", 0);
1811 validateEntry(*validated, config,
"scaling.seed", 1);
1812 validateEntry(*validated, config,
"scaling.quantizeLevel", 5.0);
1813 validateEntry(*validated, config,
"scaling.quantizePad", 10.0);
1814 validateEntry(*validated, config,
"scaling.fuzz",
true);
1815 validateEntry(*validated, config,
"scaling.bscale", 1.0);
1816 validateEntry(*validated, config,
"scaling.bzero", 0.0);
1819 for (
auto const &
name : config.
names(
false)) {
1820 if (!validated->exists(
name)) {
1822 os <<
"Invalid image write option: " <<
name;
1830#define INSTANTIATE_KEY_OPS(r, data, T) \
1831 template void Fits::updateKey(std::string const &, T const &, std::string const &); \
1832 template void Fits::writeKey(std::string const &, T const &, std::string const &); \
1833 template void Fits::updateKey(std::string const &, T const &); \
1834 template void Fits::writeKey(std::string const &, T const &); \
1835 template void Fits::updateColumnKey(std::string const &, int, T const &, std::string const &); \
1836 template void Fits::writeColumnKey(std::string const &, int, T const &, std::string const &); \
1837 template void Fits::updateColumnKey(std::string const &, int, T const &); \
1838 template void Fits::writeColumnKey(std::string const &, int, T const &); \
1839 template void Fits::readKey(std::string const &, T &);
1841#define INSTANTIATE_IMAGE_OPS(r, data, T) \
1842 template void Fits::writeImageImpl(T const *, int); \
1843 template void Fits::writeImage(image::ImageBase<T> const &, ImageWriteOptions const &, \
1844 daf::base::PropertySet const *, \
1845 image::Mask<image::MaskPixel> const *); \
1846 template void Fits::writeImage(image::ImageBase<T> const &, ImageWriteOptions const &, \
1847 std::shared_ptr<daf::base::PropertySet const>, \
1848 std::shared_ptr<image::Mask<image::MaskPixel> const>); \
1849 template void Fits::readImageImpl(int, T *, long *, long *, long *); \
1850 template bool Fits::checkImageType<T>(); \
1851 template int getBitPix<T>();
1853#define INSTANTIATE_TABLE_OPS(r, data, T) \
1854 template int Fits::addColumn<T>(std::string const &ttype, int size); \
1855 template int Fits::addColumn<T>(std::string const &ttype, int size, std::string const &comment);
1856#define INSTANTIATE_TABLE_ARRAY_OPS(r, data, T) \
1857 template void Fits::writeTableArray(std::size_t row, int col, int nElements, T const *value); \
1858 template void Fits::readTableArray(std::size_t row, int col, int nElements, T *value);
1865 (bool)(unsigned char)(short)(unsigned short)(int)(unsigned int)(long)(unsigned long)(LONGLONG)( \
1866 float)(double)(std::complex<float>)(std::complex<double>)(std::string)
1868#define COLUMN_TYPES \
1869 (bool)(std::string)(std::int8_t)(std::uint8_t)(std::int16_t)(std::uint16_t)(std::int32_t)(std::uint32_t) \
1870 (std::int64_t)(float)(double)(lsst::geom::Angle)(std::complex<float>)(std::complex<double>)
1872#define COLUMN_ARRAY_TYPES \
1873 (bool)(char)(std::uint8_t)(std::int16_t)(std::uint16_t)(std::int32_t)(std::uint32_t)(std::int64_t)( \
1874 float)(double)(lsst::geom::Angle)(std::complex<float>)(std::complex<double>)
1876#define IMAGE_TYPES \
1877 (unsigned char)(short)(unsigned short)(int)(unsigned int)(std::int64_t)(std::uint64_t)(float)(double)
table::Key< std::string > name
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
LSST DM logging module built on log4cxx.
#define LOGLS_WARN(logger, message)
Log a warn-level message using an iostream-based interface.
#define LOGL_WARN(logger, message...)
Log a warn-level message using a varargs/printf style interface.
#define LOGLS_DEBUG(logger, message)
Log a debug-level message using an iostream-based interface.
table::Key< double > scaling
An exception thrown when problems are found when reading or writing FITS files.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
void writeKey(std::string const &key, T const &value, std::string const &comment)
Add a FITS header key to the bottom of the header.
void writeColumnKey(std::string const &prefix, int n, T const &value, std::string const &comment)
Write a key of the form XXXXXnnn, where XXXXX is the prefix and nnn is a column number.
void closeFile()
Close a FITS file.
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
int getImageDim()
Return the number of dimensions in the current HDU.
std::size_t countRows()
Return the number of row in a table.
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 readTableArray(std::size_t row, int col, int nElements, T *value)
Read an array value from a binary table.
void updateColumnKey(std::string const &prefix, int n, T const &value, std::string const &comment)
Update a key of the form XXXXXnnn, where XXXXX is the prefix and nnn is a column number.
void setHdu(int hdu, bool relative=false)
Set the current HDU.
void createTable()
Create a new binary table extension.
Fits()
Default constructor; set all data members to 0.
void readTableScalar(std::size_t row, int col, T &value)
Read an array scalar from a binary table.
bool checkCompressedImagePhu()
Go to the first image header in the FITS file.
int countHdus()
Return the number of HDUs in the file.
void writeTableScalar(std::size_t row, int col, T value)
Write a scalar value to a binary table.
void forEachKey(HeaderIterationFunctor &functor)
Call a polymorphic functor for every key in the header.
std::string getFileName() const
Return the file name associated with the FITS object or "<unknown>" if there is none.
int addColumn(std::string const &ttype, int size, std::string const &comment)
Add a column to a table.
void updateKey(std::string const &key, T const &value, std::string const &comment)
Set a FITS header key, editing if it already exists and appending it if not.
void setImageCompression(ImageCompressionOptions const &options)
Set compression options for writing FITS images.
void readMetadata(daf::base::PropertySet &metadata, bool strip=false)
Read a FITS header into a PropertySet or PropertyList.
void writeTableArray(std::size_t row, int col, int nElements, T const *value)
Write an array value to a binary table.
void readKey(std::string const &key, T &value)
Read a FITS header key into the given reference.
std::string getImageDType()
Return the numpy dtype equivalent of the image pixel type (e.g.
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.
long getTableArraySize(int col)
Return the size of an array column.
std::size_t addRows(std::size_t nRows)
Append rows to a table, and return the index of the first new row.
bool checkImageType()
Return true if the current HDU is compatible with the given pixel type.
RAII scoped guard for moving the HDU in a Fits object.
bool fuzz
Fuzz the values when quantising floating-point values?
ImageScale determine(image::ImageBase< T > const &image, image::Mask< image::MaskPixel > const *mask=nullptr) const
Determine the scaling for a particular image.
int seed
Seed for random number generator when fuzzing.
Lifetime-management for memory that goes into FITS memory files.
void reset()
Return the manager to the same state it would be if default-constructed.
The base class for all image classed (Image, Mask, MaskedImage, ...)
Represent a 2-dimensional array of bitmask pixels.
Class for storing ordered metadata with comments.
std::string const & getComment(std::string const &name) const
Get the comment for a string property name (possibly hierarchical).
std::vector< std::string > getOrderedNames() const
Get the list of property names, in the order they were added.
Class for storing generic metadata.
int64_t getAsInt64(std::string const &name) const
Get the last value for a bool/char/short/int/int64_t property name (possibly hierarchical).
std::vector< std::string > names(bool topLevelOnly=true) const
Get the names in the PropertySet, optionally including those in subproperties.
T get(std::string const &name) const
Get the last value for a property name (possibly hierarchical).
void add(std::string const &name, T const &value)
Append a single value to the vector of values for a property name (possibly hierarchical).
virtual std::shared_ptr< PropertySet > deepCopy() const
Make a deep copy of the PropertySet and all of its contents.
std::vector< std::string > paramNames(bool topLevelOnly=true) const
A variant of names that excludes the names of subproperties.
A class representing an angle.
Reports invalid arguments.
Reports errors that are due to events beyond the control of the program.
T find_first_not_of(T... args)
T find_last_not_of(T... args)
#define INSTANTIATE_TABLE_OPS(r, data, T)
daf::base::PropertyList * list
#define INSTANTIATE_IMAGE_OPS(r, data, T)
#define COLUMN_ARRAY_TYPES
daf::base::PropertySet * set
#define INSTANTIATE_KEY_OPS(r, data, T)
#define INSTANTIATE_TABLE_ARRAY_OPS(r, data, T)
#define LSST_FITS_CHECK_STATUS(fitsObj,...)
Throw a FitsError exception if the status of the given Fits object is nonzero.
def scale(algorithm, min, max=None, frame=None)
std::shared_ptr< daf::base::PropertyList > combineMetadata(daf::base::PropertyList const &first, daf::base::PropertyList const &second)
Combine two sets of metadata in a FITS-appropriate fashion.
const int DEFAULT_HDU
Specify that the default HDU should be read.
int getBitPix()
Return the cfitsio integer BITPIX code for the given data type.
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)
std::string makeErrorMessage(std::string const &fileName="", int status=0, std::string const &msg="")
Return an error message reflecting FITS I/O errors.
bool getAllowImageCompression()
int compressionAlgorithmToCfitsio(ImageCompressionOptions::CompressionAlgorithm algorithm)
Convert ImageCompressionOptions::CompressionAlgorithm to cfitsio.
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromCfitsio(int cfitsio)
Convert compression algorithm from cfitsio to ImageCompressionOptions::CompressionAlgorithm.
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.
ndarray::Array< T const, N, N > const makeContiguousArray(ndarray::Array< T, N, C > const &array)
Construct a contiguous ndarray.
std::shared_ptr< daf::base::PropertyList > createTrivialWcsMetadata(std::string const &wcsName, lsst::geom::Point2I const &xy0)
std::string const wcsNameForXY0
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_SCALAR, _, BOOST_PP_TUPLE_TO_SEQ(AFW_TABLE_SCALAR_FIELD_TYPE_N, AFW_TABLE_SCALAR_FIELD_TYPE_TUPLE)) BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_ARRAY
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
def write(self, patchRef, catalog)
Write the output.
A base class for image defects.
Options for tile compression of image pixels.
float quantizeLevel
quantization level: 0.0 = none requires use of GZIP or GZIP_SHUFFLE
CompressionAlgorithm algorithm
Compresion algorithm to use.
ndarray::Array< long, 1, 1 > Tiles
Tiles tiles
Tile size; a dimension with 0 means infinite (e.g., to specify one row: 0,1)
CompressionAlgorithm
Compression algorithms.
Options for writing an image to 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.
ImageWriteOptions(image::Image< T > const &image)
Construct with default options for images.
FITS BITPIX header value by C++ type.