lsst.afw  18.1.0-16-g27f8efa26+4
fits.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 #include <cstdint>
4 #include <cstdio>
5 #include <complex>
6 #include <cmath>
7 #include <sstream>
8 #include <unordered_set>
9 #include <unordered_map>
10 
11 #include "fitsio.h"
12 extern "C" {
13 #include "fitsio2.h"
14 }
15 
16 #include "boost/regex.hpp"
17 #include "boost/filesystem.hpp"
18 #include "boost/preprocessor/seq/for_each.hpp"
19 #include "boost/format.hpp"
20 
21 #include "lsst/pex/exceptions.h"
22 #include "lsst/log/Log.h"
23 #include "lsst/afw/fits.h"
24 #include "lsst/geom/Angle.h"
25 #include "lsst/afw/geom/wcsUtils.h"
27 
28 namespace lsst {
29 namespace afw {
30 namespace fits {
31 
32 // ----------------------------------------------------------------------------------------------------------
33 // ---- Miscellaneous utilities -----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------------------------------------
35 
36 namespace {
37 
38 /*
39  * Format a PropertySet into a FITS header string using simplifying assumptions.
40  *
41  * See @ref makeLimitedFitsHeader for details.
42  *
43  * @param[in] paramNames Names of properties to format
44  * @param[in] metadata Metadata to format
45  * @return a FITS header string (exactly 80 characters per entry, no line terminators)
46  */
47 std::string makeLimitedFitsHeaderImpl(std::vector<std::string> const &paramNames,
48  daf::base::PropertySet const &metadata) {
50  for (auto const &fullName : paramNames) {
51  std::size_t lastPeriod = fullName.rfind(char('.'));
52  auto name = (lastPeriod == std::string::npos) ? fullName : fullName.substr(lastPeriod + 1);
53  std::type_info const &type = metadata.typeOf(name);
54 
55  std::string out = "";
56  out.reserve(80);
57  if (name.size() > 8) {
58  continue; // The name is too long for a FITS keyword; skip this item
59  }
60  out = (boost::format("%-8s= ") % name).str();
61 
62  if (type == typeid(bool)) {
63  out += metadata.get<bool>(name) ? "T" : "F";
64  } else if (type == typeid(std::uint8_t)) {
65  out += (boost::format("%20d") % static_cast<int>(metadata.get<std::uint8_t>(name))).str();
66  } else if (type == typeid(int)) {
67  out += (boost::format("%20d") % metadata.get<int>(name)).str();
68  } else if (type == typeid(double)) {
69  // use G because FITS wants uppercase E for exponents
70  out += (boost::format("%#20.17G") % metadata.get<double>(name)).str();
71  } else if (type == typeid(float)) {
72  out += (boost::format("%#20.15G") % metadata.get<float>(name)).str();
73  } else if (type == typeid(std::nullptr_t)) {
74  out += " ";
75  } else if (type == typeid(std::string)) {
76  out += "'" + metadata.get<std::string>(name) + "'";
77  if (out.size() > 80) {
78  continue; // Formatted data is too long; skip this item
79  }
80  }
81 
82  int const len = out.size();
83  if (len < 80) {
84  out += std::string(80 - len, ' ');
85  } else if (len > 80) {
86  // non-string item has a formatted value that is too long; this should never happen
87  throw LSST_EXCEPT(pex::exceptions::LogicError,
88  "Formatted data too long: " + std::to_string(len) + " > 80: \"" + out + "\"");
89  }
90 
91  result << out;
92  }
93 
94  return result.str();
95 }
96 
105 class StringStartSet {
106 public:
108  StringStartSet(std::initializer_list<std::string> const &input) : _minSize(-1) {
109  for (auto const &word : input) {
110  std::size_t const size = word.size();
111  if (size < _minSize) {
112  _minSize = size;
113  }
114  }
115  for (auto const &word : input) {
116  std::string const start = startString(word);
117  assert(_words.count(start) == 0); // This should be the only word that starts this way
118  _words[start] = word;
119  }
120  }
121 
123  bool matches(std::string const &key) const {
124  auto const iter = _words.find(startString(key));
125  if (iter == _words.end()) {
126  return false;
127  }
128  // Check that the full word matches too
129  std::string const &word = iter->second;
130  return key.compare(0, word.size(), word) == 0;
131  }
132 
133 private:
135 
137  std::string startString(std::string const &word) const { return word.substr(0, _minSize); }
138 
139  std::size_t _minSize; // Minimum length of provided words
140  Map _words; // Start of words --> full word
141 };
142 
148 static std::unordered_set<std::string> const ignoreKeys = {
149  // FITS core keywords
150  "SIMPLE", "BITPIX", "NAXIS", "EXTEND", "GCOUNT", "PCOUNT", "XTENSION", "TFIELDS", "BSCALE", "BZERO",
151  // FITS compression keywords
152  "ZBITPIX", "ZIMAGE", "ZCMPTYPE", "ZSIMPLE", "ZEXTEND", "ZBLANK", "ZDATASUM", "ZHECKSUM", "ZQUANTIZ",
153  // Not essential, but will prevent fitsverify warnings
154  "DATASUM", "CHECKSUM"};
155 
161 StringStartSet const ignoreKeyStarts{// FITS core keywords
162  "NAXIS", "TZERO", "TSCAL",
163  // FITS compression keywords
164  "ZNAXIS", "ZTILE", "ZNAME", "ZVAL"};
165 
171 StringStartSet const ignoreKeyStartsWrite{"TFORM", "TTYPE"};
172 
173 // Strip leading and trailing single quotes and whitespace from a string.
174 std::string strip(std::string const &s) {
175  if (s.empty()) return s;
176  std::size_t i1 = s.find_first_not_of(" '");
177  std::size_t i2 = s.find_last_not_of(" '");
178  return s.substr(i1, (i1 == std::string::npos) ? 0 : 1 + i2 - i1);
179 }
180 
181 // ---- FITS binary table format codes for various C++ types. -----------------------------------------------
182 
183 char getFormatCode(bool *) { return 'X'; }
184 char getFormatCode(std::string *) { return 'A'; }
185 char getFormatCode(std::int8_t *) { return 'S'; }
186 char getFormatCode(std::uint8_t *) { return 'B'; }
187 char getFormatCode(std::int16_t *) { return 'I'; }
188 char getFormatCode(std::uint16_t *) { return 'U'; }
189 char getFormatCode(std::int32_t *) { return 'J'; }
190 char getFormatCode(std::uint32_t *) { return 'V'; }
191 char getFormatCode(std::int64_t *) { return 'K'; }
192 char getFormatCode(float *) { return 'E'; }
193 char getFormatCode(double *) { return 'D'; }
194 char getFormatCode(std::complex<float> *) { return 'C'; }
195 char getFormatCode(std::complex<double> *) { return 'M'; }
196 char getFormatCode(lsst::geom::Angle *) { return 'D'; }
197 
198 // ---- Create a TFORM value for the given type and size ----------------------------------------------------
199 
200 template <typename T>
201 std::string makeColumnFormat(int size = 1) {
202  if (size > 0) {
203  return (boost::format("%d%c") % size % getFormatCode((T *)0)).str();
204  } else if (size < 0) {
205  // variable length, max size given as -size
206  return (boost::format("1Q%c(%d)") % getFormatCode((T *)0) % (-size)).str();
207  } else {
208  // variable length, max size unknown
209  return (boost::format("1Q%c") % getFormatCode((T *)0)).str();
210  }
211 }
212 
213 // ---- Traits class to get cfitsio type constants from templates -------------------------------------------
214 
215 template <typename T>
216 struct FitsType;
217 
218 template <>
219 struct FitsType<bool> {
220  static int const CONSTANT = TLOGICAL;
221 };
222 template <>
223 struct FitsType<char> {
224  static int const CONSTANT = TSTRING;
225 };
226 template <>
227 struct FitsType<signed char> {
228  static int const CONSTANT = TSBYTE;
229 };
230 template <>
231 struct FitsType<unsigned char> {
232  static int const CONSTANT = TBYTE;
233 };
234 template <>
235 struct FitsType<short> {
236  static int const CONSTANT = TSHORT;
237 };
238 template <>
239 struct FitsType<unsigned short> {
240  static int const CONSTANT = TUSHORT;
241 };
242 template <>
243 struct FitsType<int> {
244  static int const CONSTANT = TINT;
245 };
246 template <>
247 struct FitsType<unsigned int> {
248  static int const CONSTANT = TUINT;
249 };
250 template <>
251 struct FitsType<long> {
252  static int const CONSTANT = TLONG;
253 };
254 template <>
255 struct FitsType<unsigned long> {
256  static int const CONSTANT = TULONG;
257 };
258 template <>
259 struct FitsType<long long> {
260  static int const CONSTANT = TLONGLONG;
261 };
262 template <>
263 struct FitsType<unsigned long long> {
264  static int const CONSTANT = TLONGLONG;
265 };
266 template <>
267 struct FitsType<float> {
268  static int const CONSTANT = TFLOAT;
269 };
270 template <>
271 struct FitsType<double> {
272  static int const CONSTANT = TDOUBLE;
273 };
274 template <>
275 struct FitsType<lsst::geom::Angle> {
276  static int const CONSTANT = TDOUBLE;
277 };
278 template <>
279 struct FitsType<std::complex<float> > {
280  static int const CONSTANT = TCOMPLEX;
281 };
282 template <>
283 struct FitsType<std::complex<double> > {
284  static int const CONSTANT = TDBLCOMPLEX;
285 };
286 
287 // We use TBIT when writing booleans to table cells, but TLOGICAL in headers.
288 template <typename T>
289 struct FitsTableType : public FitsType<T> {};
290 template <>
291 struct FitsTableType<bool> {
292  static int const CONSTANT = TBIT;
293 };
294 
295 template <typename T>
296 struct FitsBitPix;
297 
298 template <>
299 struct FitsBitPix<unsigned char> {
300  static int const CONSTANT = BYTE_IMG;
301 };
302 template <>
303 struct FitsBitPix<short> {
304  static int const CONSTANT = SHORT_IMG;
305 };
306 template <>
307 struct FitsBitPix<unsigned short> {
308  static int const CONSTANT = USHORT_IMG;
309 };
310 template <>
311 struct FitsBitPix<int> {
312  static int const CONSTANT = LONG_IMG;
313 }; // not a typo!
314 template <>
315 struct FitsBitPix<unsigned int> {
316  static int const CONSTANT = ULONG_IMG;
317 };
318 template <>
319 struct FitsBitPix<std::int64_t> {
320  static int const CONSTANT = LONGLONG_IMG;
321 };
322 template <>
323 struct FitsBitPix<std::uint64_t> {
324  static int const CONSTANT = LONGLONG_IMG;
325 };
326 template <>
327 struct FitsBitPix<float> {
328  static int const CONSTANT = FLOAT_IMG;
329 };
330 template <>
331 struct FitsBitPix<double> {
332  static int const CONSTANT = DOUBLE_IMG;
333 };
334 
335 bool isFitsImageTypeSigned(int constant) {
336  switch (constant) {
337  case BYTE_IMG: return false;
338  case SHORT_IMG: return true;
339  case USHORT_IMG: return false;
340  case LONG_IMG: return true;
341  case ULONG_IMG: return false;
342  case LONGLONG_IMG: return true;
343  case FLOAT_IMG: return true;
344  case DOUBLE_IMG: return true;
345  }
346  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, "Invalid constant.");
347 }
348 
349 static bool allowImageCompression = true;
350 
351 int fitsTypeForBitpix(int bitpix) {
352  switch (bitpix) {
353  case 8:
354  return TBYTE;
355  case 16:
356  return TSHORT;
357  case 32:
358  return TINT;
359  case 64:
360  return TLONGLONG;
361  case -32:
362  return TFLOAT;
363  case -64:
364  return TDOUBLE;
365  default:
367  os << "Invalid bitpix value: " << bitpix;
368  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, os.str());
369  }
370 }
371 
372 /*
373  * Information about one item of metadata: is a comment? is valid?
374  *
375  * See isCommentIsValid for more information.
376  */
377 struct ItemInfo {
378  ItemInfo(bool isComment, bool isValid) : isComment(isComment), isValid(isValid) {}
379  bool isComment;
380  bool isValid;
381 };
382 
383 /*
384  * Is an item a commnt (or history) and is it usable in a FITS header?
385  *
386  * For an item to be valid:
387  * - If name is COMMENT or HISTORY then the item must be of type std::string
388  * - All other items are always valid
389  */
390 ItemInfo isCommentIsValid(daf::base::PropertyList const &pl, std::string const &name) {
391  if (!pl.exists(name)) {
392  return ItemInfo(false, false);
393  }
394  std::type_info const &type = pl.typeOf(name);
395  if ((name == "COMMENT") || (name == "HISTORY")) {
396  return ItemInfo(true, type == typeid(std::string));
397  }
398  return ItemInfo(false, true);
399 }
400 
401 } // namespace
402 
403 // ----------------------------------------------------------------------------------------------------------
404 // ---- Implementations for stuff in fits.h -----------------------------------------------------------------
405 // ----------------------------------------------------------------------------------------------------------
406 
407 std::string makeErrorMessage(std::string const &fileName, int status, std::string const &msg) {
409  os << "cfitsio error";
410  if (fileName != "") {
411  os << " (" << fileName << ")";
412  }
413  if (status != 0) {
414  char fitsErrMsg[FLEN_ERRMSG];
415  fits_get_errstatus(status, fitsErrMsg);
416  os << ": " << fitsErrMsg << " (" << status << ")";
417  }
418  if (msg != "") {
419  os << " : " << msg;
420  }
421  os << "\ncfitsio error stack:\n";
422  char cfitsioMsg[FLEN_ERRMSG];
423  while (fits_read_errmsg(cfitsioMsg) != 0) {
424  os << " " << cfitsioMsg << "\n";
425  }
426  return os.str();
427 }
428 
429 std::string makeErrorMessage(void *fptr, int status, std::string const &msg) {
430  std::string fileName = "";
431  fitsfile *fd = reinterpret_cast<fitsfile *>(fptr);
432  if (fd != 0 && fd->Fptr != 0 && fd->Fptr->filename != 0) {
433  fileName = fd->Fptr->filename;
434  }
435  return makeErrorMessage(fileName, status, msg);
436 }
437 
439  std::set<std::string> const &excludeNames) {
440  daf::base::PropertyList const *pl = dynamic_cast<daf::base::PropertyList const *>(&metadata);
441  std::vector<std::string> allParamNames;
442  if (pl) {
443  allParamNames = pl->getOrderedNames();
444  } else {
445  allParamNames = metadata.paramNames(false);
446  }
447  std::vector<std::string> desiredParamNames;
448  for (auto const &name : allParamNames) {
449  if (excludeNames.count(name) == 0) {
450  desiredParamNames.push_back(name);
451  }
452  }
453  return makeLimitedFitsHeaderImpl(desiredParamNames, metadata);
454 }
455 
457  if (_managed) std::free(_ptr);
458  _ptr = 0;
459  _len = 0;
460  _managed = true;
461 }
462 
464  reset();
465  _ptr = std::malloc(len);
466  _len = len;
467  _managed = true;
468 }
469 
470 template <typename T>
471 int getBitPix() {
472  return FitsBitPix<T>::CONSTANT;
473 }
474 
475 // ----------------------------------------------------------------------------------------------------------
476 // ---- Implementations for Fits class ----------------------------------------------------------------------
477 // ----------------------------------------------------------------------------------------------------------
478 
480  std::string fileName = "<unknown>";
481  fitsfile *fd = reinterpret_cast<fitsfile *>(fptr);
482  if (fd != 0 && fd->Fptr != 0 && fd->Fptr->filename != 0) {
483  fileName = fd->Fptr->filename;
484  }
485  return fileName;
486 }
487 
489  int n = 1;
490  fits_get_hdu_num(reinterpret_cast<fitsfile *>(fptr), &n);
491  return n - 1;
492 }
493 
494 void Fits::setHdu(int hdu, bool relative) {
495  if (relative) {
496  fits_movrel_hdu(reinterpret_cast<fitsfile *>(fptr), hdu, 0, &status);
497  if (behavior & AUTO_CHECK) {
498  LSST_FITS_CHECK_STATUS(*this, boost::format("Incrementing HDU by %d") % hdu);
499  }
500  } else {
501  if (hdu != DEFAULT_HDU) {
502  fits_movabs_hdu(reinterpret_cast<fitsfile *>(fptr), hdu + 1, 0, &status);
503  }
504  if (hdu == DEFAULT_HDU && getHdu() == 0 && getImageDim() == 0) {
505  // want a silent failure here
506  int tmpStatus = status;
507  fits_movrel_hdu(reinterpret_cast<fitsfile *>(fptr), 1, 0, &tmpStatus);
508  }
509  if (behavior & AUTO_CHECK) {
510  LSST_FITS_CHECK_STATUS(*this, boost::format("Moving to HDU %d") % hdu);
511  }
512  }
513 }
514 
516  int n = 0;
517  fits_get_num_hdus(reinterpret_cast<fitsfile *>(fptr), &n, &status);
518  if (behavior & AUTO_CHECK) {
519  LSST_FITS_CHECK_STATUS(*this, "Getting number of HDUs in file.");
520  }
521  return n;
522 }
523 
524 // ---- Writing and updating header keys --------------------------------------------------------------------
525 
526 namespace {
527 
528 // Impl functions in the anonymous namespace do special handling for strings, bools, and IEEE fp values.
529 
536 std::string nonFiniteDoubleToString(double value) {
537  if (std::isfinite(value)) {
538  return "";
539  }
540  if (std::isnan(value)) {
541  return "NAN";
542  }
543  if (value < 0) {
544  return "-INFINITY";
545  }
546  return "+INFINITY";
547 }
548 
554 double stringToNonFiniteDouble(std::string const &value) {
555  if (value == "NAN") {
557  }
558  if (value == "+INFINITY") {
560  }
561  if (value == "-INFINITY") {
563  }
564  return 0;
565 }
566 
567 template <typename T>
568 void updateKeyImpl(Fits &fits, char const *key, T const &value, char const *comment) {
569  fits_update_key(reinterpret_cast<fitsfile *>(fits.fptr), FitsType<T>::CONSTANT, const_cast<char *>(key),
570  const_cast<T *>(&value), const_cast<char *>(comment), &fits.status);
571 }
572 
573 void updateKeyImpl(Fits &fits, char const *key, std::string const &value, char const *comment) {
574  fits_update_key_longstr(reinterpret_cast<fitsfile *>(fits.fptr), const_cast<char *>(key),
575  const_cast<char *>(value.c_str()), const_cast<char *>(comment), &fits.status);
576 }
577 
578 void updateKeyImpl(Fits &fits, char const *key, bool const &value, char const *comment) {
579  int v = value;
580  fits_update_key(reinterpret_cast<fitsfile *>(fits.fptr), TLOGICAL, const_cast<char *>(key), &v,
581  const_cast<char *>(comment), &fits.status);
582 }
583 
584 void updateKeyImpl(Fits &fits, char const *key, double const &value, char const *comment) {
585  std::string strValue = nonFiniteDoubleToString(value);
586  if (!strValue.empty()) {
587  updateKeyImpl(fits, key, strValue, comment);
588  } else {
589  fits_update_key(reinterpret_cast<fitsfile *>(fits.fptr), FitsType<double>::CONSTANT,
590  const_cast<char *>(key), const_cast<double *>(&value), const_cast<char *>(comment),
591  &fits.status);
592  }
593 }
594 
595 template <typename T>
596 void writeKeyImpl(Fits &fits, char const *key, T const &value, char const *comment) {
597  fits_write_key(reinterpret_cast<fitsfile *>(fits.fptr), FitsType<T>::CONSTANT, const_cast<char *>(key),
598  const_cast<T *>(&value), const_cast<char *>(comment), &fits.status);
599 }
600 
601 void writeKeyImpl(Fits &fits, char const *key, char const *comment) {
602  // Write a key with an undefined value
603  fits_write_key_null(reinterpret_cast<fitsfile *>(fits.fptr), const_cast<char *>(key),
604  const_cast<char *>(comment), &fits.status);
605 }
606 
607 void writeKeyImpl(Fits &fits, char const *key, std::string const &value, char const *comment) {
608  if (strncmp(key, "COMMENT", 7) == 0) {
609  fits_write_comment(reinterpret_cast<fitsfile *>(fits.fptr), const_cast<char *>(value.c_str()),
610  &fits.status);
611  } else if (strncmp(key, "HISTORY", 7) == 0) {
612  fits_write_history(reinterpret_cast<fitsfile *>(fits.fptr), const_cast<char *>(value.c_str()),
613  &fits.status);
614  } else {
615  fits_write_key_longstr(reinterpret_cast<fitsfile *>(fits.fptr), const_cast<char *>(key),
616  const_cast<char *>(value.c_str()), const_cast<char *>(comment), &fits.status);
617  }
618 }
619 
620 void writeKeyImpl(Fits &fits, char const *key, bool const &value, char const *comment) {
621  int v = value;
622  fits_write_key(reinterpret_cast<fitsfile *>(fits.fptr), TLOGICAL, const_cast<char *>(key), &v,
623  const_cast<char *>(comment), &fits.status);
624 }
625 
626 void writeKeyImpl(Fits &fits, char const *key, double const &value, char const *comment) {
627  std::string strValue = nonFiniteDoubleToString(value);
628  if (!strValue.empty()) {
629  writeKeyImpl(fits, key, strValue, comment);
630  } else {
631  fits_write_key(reinterpret_cast<fitsfile *>(fits.fptr), FitsType<double>::CONSTANT,
632  const_cast<char *>(key), const_cast<double *>(&value), const_cast<char *>(comment),
633  &fits.status);
634  }
635 }
636 
637 } // namespace
638 
639 template <typename T>
640 void Fits::updateKey(std::string const &key, T const &value, std::string const &comment) {
641  updateKeyImpl(*this, key.c_str(), value, comment.c_str());
642  if (behavior & AUTO_CHECK) {
643  LSST_FITS_CHECK_STATUS(*this, boost::format("Updating key '%s': '%s'") % key % value);
644  }
645 }
646 
647 template <typename T>
648 void Fits::writeKey(std::string const &key, T const &value, std::string const &comment) {
649  writeKeyImpl(*this, key.c_str(), value, comment.c_str());
650  if (behavior & AUTO_CHECK) {
651  LSST_FITS_CHECK_STATUS(*this, boost::format("Writing key '%s': '%s'") % key % value);
652  }
653 }
654 
655 template <typename T>
656 void Fits::updateKey(std::string const &key, T const &value) {
657  updateKeyImpl(*this, key.c_str(), value, 0);
658  if (behavior & AUTO_CHECK) {
659  LSST_FITS_CHECK_STATUS(*this, boost::format("Updating key '%s': '%s'") % key % value);
660  }
661 }
662 
663 template <typename T>
664 void Fits::writeKey(std::string const &key, T const &value) {
665  writeKeyImpl(*this, key.c_str(), value, 0);
666  if (behavior & AUTO_CHECK) {
667  LSST_FITS_CHECK_STATUS(*this, boost::format("Writing key '%s': '%s'") % key % value);
668  }
669 }
670 
671 template <typename T>
672 void Fits::updateColumnKey(std::string const &prefix, int n, T const &value, std::string const &comment) {
673  updateKey((boost::format("%s%d") % prefix % (n + 1)).str(), value, comment);
674  if (behavior & AUTO_CHECK) {
675  LSST_FITS_CHECK_STATUS(*this, boost::format("Updating key '%s%d': '%s'") % prefix % (n + 1) % value);
676  }
677 }
678 
679 template <typename T>
680 void Fits::writeColumnKey(std::string const &prefix, int n, T const &value, std::string const &comment) {
681  writeKey((boost::format("%s%d") % prefix % (n + 1)).str(), value, comment);
682  if (behavior & AUTO_CHECK) {
683  LSST_FITS_CHECK_STATUS(*this, boost::format("Writing key '%s%d': '%s'") % prefix % (n + 1) % value);
684  }
685 }
686 
687 template <typename T>
688 void Fits::updateColumnKey(std::string const &prefix, int n, T const &value) {
689  updateKey((boost::format("%s%d") % prefix % (n + 1)).str(), value);
690  if (behavior & AUTO_CHECK) {
691  LSST_FITS_CHECK_STATUS(*this, boost::format("Updating key '%s%d': '%s'") % prefix % (n + 1) % value);
692  }
693 }
694 
695 template <typename T>
696 void Fits::writeColumnKey(std::string const &prefix, int n, T const &value) {
697  writeKey((boost::format("%s%d") % prefix % (n + 1)).str(), value);
698  if (behavior & AUTO_CHECK) {
699  LSST_FITS_CHECK_STATUS(*this, boost::format("Writing key '%s%d': '%s'") % prefix % (n + 1) % value);
700  }
701 }
702 
703 // ---- Reading header keys ---------------------------------------------------------------------------------
704 
705 namespace {
706 
707 template <typename T>
708 void readKeyImpl(Fits &fits, char const *key, T &value) {
709  fits_read_key(reinterpret_cast<fitsfile *>(fits.fptr), FitsType<T>::CONSTANT, const_cast<char *>(key),
710  &value, 0, &fits.status);
711 }
712 
713 void readKeyImpl(Fits &fits, char const *key, std::string &value) {
714  char *buf = 0;
715  fits_read_key_longstr(reinterpret_cast<fitsfile *>(fits.fptr), const_cast<char *>(key), &buf, 0,
716  &fits.status);
717  if (buf) {
718  value = strip(buf);
719  free(buf);
720  }
721 }
722 
723 void readKeyImpl(Fits &fits, char const *key, double &value) {
724  // We need to check for the possibility that the value is a special string (for NAN, +/-Inf).
725  // If a quote mark (') is present then it's a string.
726 
727  char buf[FLEN_VALUE];
728  fits_read_keyword(reinterpret_cast<fitsfile *>(fits.fptr), const_cast<char *>(key), buf, 0, &fits.status);
729  if (fits.status != 0) {
730  return;
731  }
732  if (std::string(buf).find('\'') != std::string::npos) {
733  std::string unquoted;
734  readKeyImpl(fits, key, unquoted); // Let someone else remove quotes and whitespace
735  if (fits.status != 0) {
736  return;
737  }
738  value = stringToNonFiniteDouble(unquoted);
739  if (value == 0) {
740  throw LSST_EXCEPT(
742  (boost::format("Unrecognised string value for keyword '%s' when parsing as double: %s") %
743  key % unquoted)
744  .str());
745  }
746  } else {
747  fits_read_key(reinterpret_cast<fitsfile *>(fits.fptr), FitsType<double>::CONSTANT,
748  const_cast<char *>(key), &value, 0, &fits.status);
749  }
750 }
751 
752 } // namespace
753 
754 template <typename T>
755 void Fits::readKey(std::string const &key, T &value) {
756  readKeyImpl(*this, key.c_str(), value);
757  if (behavior & AUTO_CHECK) {
758  LSST_FITS_CHECK_STATUS(*this, boost::format("Reading key '%s'") % key);
759  }
760 }
761 
763  char key[81]; // allow for terminating NUL
764  char value[81];
765  char comment[81];
766  int nKeys = 0;
767  fits_get_hdrspace(reinterpret_cast<fitsfile *>(fptr), &nKeys, 0, &status);
768  std::string keyStr;
769  std::string valueStr;
770  std::string commentStr;
771  int i = 1;
772  while (i <= nKeys) {
773  fits_read_keyn(reinterpret_cast<fitsfile *>(fptr), i, key, value, comment, &status);
774  keyStr = key;
775  valueStr = value;
776  commentStr = comment;
777  ++i;
778  while (valueStr.size() > 2 && valueStr[valueStr.size() - 2] == '&' && i <= nKeys) {
779  // we're using key to hold the entire record here; the actual key is safe in keyStr
780  fits_read_record(reinterpret_cast<fitsfile *>(fptr), i, key, &status);
781  if (strncmp(key, "CONTINUE", 8) != 0) {
782  // require both trailing '&' and CONTINUE to invoke long-string handling
783  break;
784  }
785  std::string card = key;
786  valueStr.erase(valueStr.size() - 2);
787  std::size_t firstQuote = card.find('\'');
788  if (firstQuote == std::string::npos) {
789  throw LSST_EXCEPT(
790  FitsError,
792  fptr, status,
793  boost::format("Invalid CONTINUE at header key %d: \"%s\".") % i % card));
794  }
795  std::size_t lastQuote = card.find('\'', firstQuote + 1);
796  if (lastQuote == std::string::npos) {
797  throw LSST_EXCEPT(
798  FitsError,
800  fptr, status,
801  boost::format("Invalid CONTINUE at header key %d: \"%s\".") % i % card));
802  }
803  valueStr += card.substr(firstQuote + 1, lastQuote - firstQuote);
804  std::size_t slash = card.find('/', lastQuote + 1);
805  if (slash != std::string::npos) {
806  commentStr += strip(card.substr(slash + 1));
807  }
808  ++i;
809  }
810  if (behavior & AUTO_CHECK) {
811  LSST_FITS_CHECK_STATUS(*this, boost::format("Reading key '%s'") % keyStr);
812  }
813  functor(keyStr, valueStr, commentStr);
814  }
815 }
816 
817 // ---- Reading and writing PropertySet/PropertyList --------------------------------------------------------
818 
819 namespace {
820 
821 bool isKeyIgnored(std::string const &key, bool write = false) {
822  return ((ignoreKeys.find(key) != ignoreKeys.end()) || ignoreKeyStarts.matches(key) ||
823  (write && ignoreKeyStartsWrite.matches(key)));
824 }
825 
826 class MetadataIterationFunctor : public HeaderIterationFunctor {
827 public:
828  void operator()(std::string const &key, std::string const &value, std::string const &comment) override;
829 
830  template <typename T>
831  void add(std::string const &key, T value, std::string const &comment) {
832  // PropertyList/Set can not support array items where some elements are
833  // defined and some undefined. If we are adding defined value where
834  // previously we have an undefined value we must use set instead.
835  if (list) {
836  if (list->exists(key) && list->isUndefined(key)) {
837  LOGLS_WARN("afw.fits",
838  boost::format("In %s, replacing undefined value for key '%s'.") %
839  BOOST_CURRENT_FUNCTION % key);
840  list->set(key, value, comment);
841  } else {
842  list->add(key, value, comment);
843  }
844  } else {
845  if (set->exists(key) && set->isUndefined(key)) {
846  LOGLS_WARN("afw.fits",
847  boost::format("In %s, replacing undefined value for key '%s'.") %
848  BOOST_CURRENT_FUNCTION % key);
849  set->set(key, value);
850  } else {
851  set->add(key, value);
852  }
853  }
854  }
855 
856  void add(std::string const &key, std::string const &comment) {
857  // If this undefined value is adding to a pre-existing key that has
858  // a defined value we must skip the add so as not to break
859  // PropertyList/Set.
860  if (list) {
861  if (list->exists(key) && !list->isUndefined(key)) {
862  // Do nothing. Assume the previously defined value takes
863  // precedence.
864  LOGLS_WARN("afw.fits",
865  boost::format("In %s, dropping undefined value for key '%s'.") %
866  BOOST_CURRENT_FUNCTION % key);
867  } else {
868  list->add(key, nullptr, comment);
869  }
870  } else {
871  if (set->exists(key) && !set->isUndefined(key)) {
872  // Do nothing. Assume the previously defined value takes
873  // precedence.
874  LOGLS_WARN("afw.fits",
875  boost::format("In %s, dropping undefined value for key '%s'.") %
876  BOOST_CURRENT_FUNCTION % key);
877  } else {
878  set->add(key, nullptr);
879  }
880  }
881  }
882 
883  bool strip;
884  daf::base::PropertySet *set;
885  daf::base::PropertyList *list;
886 };
887 
888 void MetadataIterationFunctor::operator()(std::string const &key, std::string const &value,
889  std::string const &comment) {
890  static boost::regex const boolRegex("[tTfF]");
891  static boost::regex const intRegex("[+-]?[0-9]+");
892  static boost::regex const doubleRegex("[+-]?([0-9]*\\.?[0-9]+|[0-9]+\\.?[0-9]*)([eE][+-]?[0-9]+)?");
893  static boost::regex const fitsStringRegex("'(.*?) *'");
894  // regex for two-line comment added to all FITS headers by CFITSIO
895  static boost::regex const fitsDefinitionCommentRegex(
896  " *(FITS \\(Flexible Image Transport System\\)|and Astrophysics', volume 376, page 359).*");
897  boost::smatch matchStrings;
898 
899  if (strip && isKeyIgnored(key)) {
900  return;
901  }
902 
903  std::istringstream converter(value);
904  if (boost::regex_match(value, boolRegex)) {
905  // convert the string to an bool
906  add(key, bool(value == "T" || value == "t"), comment);
907  } else if (boost::regex_match(value, intRegex)) {
908  // convert the string to an int
909  std::int64_t val;
910  converter >> val;
911  if (val < (1LL << 31) && val > -(1LL << 31)) {
912  add(key, static_cast<int>(val), comment);
913  } else {
914  add(key, val, comment);
915  }
916  } else if (boost::regex_match(value, doubleRegex)) {
917  // convert the string to a double
918  double val;
919  converter >> val;
920  add(key, val, comment);
921  } else if (boost::regex_match(value, matchStrings, fitsStringRegex)) {
922  std::string const str = matchStrings[1].str(); // strip off the enclosing single quotes
923  double val = stringToNonFiniteDouble(str);
924  if (val != 0.0) {
925  add(key, val, comment);
926  } else {
927  add(key, str, comment);
928  }
929  } else if (key == "HISTORY") {
930  add(key, comment, "");
931  } else if (key == "COMMENT" && !(strip && boost::regex_match(comment, fitsDefinitionCommentRegex))) {
932  add(key, comment, "");
933  } else if (key.empty() && value.empty()) {
934  // This is a blank keyword comment. Since comments do not retain
935  // their position on read there is nothing to be gained by storing
936  // this in the PropertyList as a blank keyword. Therefore store
937  // them with the other comments.
938  add("COMMENT", comment, "");
939  } else if (value.empty()) {
940  // do nothing for empty values that are comments
941  // Otherwise write null value to PropertySet
942  if (key != "COMMENT") {
943  add(key, comment);
944  }
945  } else {
946  throw LSST_EXCEPT(
947  afw::fits::FitsError,
948  (boost::format("Could not parse header value for key '%s': '%s'") % key % value).str());
949  }
950 }
951 
952 void writeKeyFromProperty(Fits &fits, daf::base::PropertySet const &metadata, std::string const &key,
953  char const *comment = 0) {
954  std::type_info const &valueType = metadata.typeOf(key);
955  if (valueType == typeid(bool)) {
956  if (metadata.isArray(key)) {
957  std::vector<bool> tmp = metadata.getArray<bool>(key);
958  // work around unfortunate specialness of std::vector<bool>
959  for (std::size_t i = 0; i != tmp.size(); ++i) {
960  writeKeyImpl(fits, key.c_str(), static_cast<bool>(tmp[i]), comment);
961  }
962  } else {
963  writeKeyImpl(fits, key.c_str(), metadata.get<bool>(key), comment);
964  }
965  } else if (valueType == typeid(std::uint8_t)) {
966  if (metadata.isArray(key)) {
967  std::vector<std::uint8_t> tmp = metadata.getArray<std::uint8_t>(key);
968  for (std::size_t i = 0; i != tmp.size(); ++i) {
969  writeKeyImpl(fits, key.c_str(), tmp[i], comment);
970  }
971  } else {
972  writeKeyImpl(fits, key.c_str(), metadata.get<std::uint8_t>(key), comment);
973  }
974  } else if (valueType == typeid(int)) {
975  if (metadata.isArray(key)) {
976  std::vector<int> tmp = metadata.getArray<int>(key);
977  for (std::size_t i = 0; i != tmp.size(); ++i) {
978  writeKeyImpl(fits, key.c_str(), tmp[i], comment);
979  }
980  } else {
981  writeKeyImpl(fits, key.c_str(), metadata.get<int>(key), comment);
982  }
983  } else if (valueType == typeid(long)) {
984  if (metadata.isArray(key)) {
985  std::vector<long> tmp = metadata.getArray<long>(key);
986  for (std::size_t i = 0; i != tmp.size(); ++i) {
987  writeKeyImpl(fits, key.c_str(), tmp[i], comment);
988  }
989  } else {
990  writeKeyImpl(fits, key.c_str(), metadata.get<long>(key), comment);
991  }
992  } else if (valueType == typeid(long long)) {
993  if (metadata.isArray(key)) {
994  std::vector<long long> tmp = metadata.getArray<long long>(key);
995  for (std::size_t i = 0; i != tmp.size(); ++i) {
996  writeKeyImpl(fits, key.c_str(), tmp[i], comment);
997  }
998  } else {
999  writeKeyImpl(fits, key.c_str(), metadata.get<long long>(key), comment);
1000  }
1001  } else if (valueType == typeid(std::int64_t)) {
1002  if (metadata.isArray(key)) {
1003  std::vector<std::int64_t> tmp = metadata.getArray<std::int64_t>(key);
1004  for (std::size_t i = 0; i != tmp.size(); ++i) {
1005  writeKeyImpl(fits, key.c_str(), tmp[i], comment);
1006  }
1007  } else {
1008  writeKeyImpl(fits, key.c_str(), metadata.get<std::int64_t>(key), comment);
1009  }
1010  } else if (valueType == typeid(double)) {
1011  if (metadata.isArray(key)) {
1012  std::vector<double> tmp = metadata.getArray<double>(key);
1013  for (std::size_t i = 0; i != tmp.size(); ++i) {
1014  writeKeyImpl(fits, key.c_str(), tmp[i], comment);
1015  }
1016  } else {
1017  writeKeyImpl(fits, key.c_str(), metadata.get<double>(key), comment);
1018  }
1019  } else if (valueType == typeid(std::string)) {
1020  if (metadata.isArray(key)) {
1021  std::vector<std::string> tmp = metadata.getArray<std::string>(key);
1022  for (std::size_t i = 0; i != tmp.size(); ++i) {
1023  writeKeyImpl(fits, key.c_str(), tmp[i], comment);
1024  }
1025  } else {
1026  writeKeyImpl(fits, key.c_str(), metadata.get<std::string>(key), comment);
1027  }
1028  } else if (valueType == typeid(nullptr_t)) {
1029  if (metadata.isArray(key)) {
1030  // Write multiple undefined values for the same key
1031  std::vector<std::string> tmp = metadata.getArray<std::string>(key);
1032  for (std::size_t i = 0; i != tmp.size(); ++i) {
1033  writeKeyImpl(fits, key.c_str(), comment);
1034  }
1035  } else {
1036  writeKeyImpl(fits, key.c_str(), comment);
1037  }
1038  } else {
1039  // FIXME: inherited this error handling from fitsIo.cc; need a better option.
1040  LOGLS_WARN("afw.writeKeyFromProperty",
1041  makeErrorMessage(fits.fptr, fits.status,
1042  boost::format("In %s, unknown type '%s' for key '%s'.") %
1043  BOOST_CURRENT_FUNCTION % valueType.name() % key));
1044  }
1045  if (fits.behavior & Fits::AUTO_CHECK) {
1046  LSST_FITS_CHECK_STATUS(fits, boost::format("Writing key '%s'") % key);
1047  }
1048 }
1049 
1050 } // namespace
1051 
1053  MetadataIterationFunctor f;
1054  f.strip = strip;
1055  f.set = &metadata;
1056  f.list = dynamic_cast<daf::base::PropertyList *>(&metadata);
1057  forEachKey(f);
1058 }
1059 
1061  typedef std::vector<std::string> NameList;
1062  daf::base::PropertyList const *pl = dynamic_cast<daf::base::PropertyList const *>(&metadata);
1063  NameList paramNames;
1064  if (pl) {
1065  paramNames = pl->getOrderedNames();
1066  } else {
1067  paramNames = metadata.paramNames(false);
1068  }
1069  for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
1070  if (!isKeyIgnored(*i, true)) {
1071  if (pl) {
1072  writeKeyFromProperty(*this, metadata, *i, pl->getComment(*i).c_str());
1073  } else {
1074  writeKeyFromProperty(*this, metadata, *i);
1075  }
1076  }
1077  }
1078 }
1079 
1080 // ---- Manipulating tables ---------------------------------------------------------------------------------
1081 
1083  char *ttype = 0;
1084  char *tform = 0;
1085  fits_create_tbl(reinterpret_cast<fitsfile *>(fptr), BINARY_TBL, 0, 0, &ttype, &tform, 0, 0, &status);
1086  if (behavior & AUTO_CHECK) {
1087  LSST_FITS_CHECK_STATUS(*this, "Creating binary table");
1088  }
1089 }
1090 
1091 template <typename T>
1092 int Fits::addColumn(std::string const &ttype, int size) {
1093  int nCols = 0;
1094  fits_get_num_cols(reinterpret_cast<fitsfile *>(fptr), &nCols, &status);
1095  std::string tform = makeColumnFormat<T>(size);
1096  fits_insert_col(reinterpret_cast<fitsfile *>(fptr), nCols + 1, const_cast<char *>(ttype.c_str()),
1097  const_cast<char *>(tform.c_str()), &status);
1098  if (behavior & AUTO_CHECK) {
1099  LSST_FITS_CHECK_STATUS(*this, boost::format("Adding column '%s' with size %d") % ttype % size);
1100  }
1101  return nCols;
1102 }
1103 
1104 template <typename T>
1105 int Fits::addColumn(std::string const &ttype, int size, std::string const &comment) {
1106  int nCols = addColumn<T>(ttype, size);
1107  updateColumnKey("TTYPE", nCols, ttype, comment);
1108  if (behavior & AUTO_CHECK) {
1109  LSST_FITS_CHECK_STATUS(*this, boost::format("Adding column '%s' with size %d") % ttype % size);
1110  }
1111  return nCols;
1112 }
1113 
1115  long first = 0;
1116  fits_get_num_rows(reinterpret_cast<fitsfile *>(fptr), &first, &status);
1117  fits_insert_rows(reinterpret_cast<fitsfile *>(fptr), first, nRows, &status);
1118  if (behavior & AUTO_CHECK) {
1119  LSST_FITS_CHECK_STATUS(*this, boost::format("Adding %d rows to binary table") % nRows);
1120  }
1121  return first;
1122 }
1123 
1125  long r = 0;
1126  fits_get_num_rows(reinterpret_cast<fitsfile *>(fptr), &r, &status);
1127  if (behavior & AUTO_CHECK) {
1128  LSST_FITS_CHECK_STATUS(*this, "Checking how many rows are in table");
1129  }
1130  return r;
1131 }
1132 
1133 template <typename T>
1134 void Fits::writeTableArray(std::size_t row, int col, int nElements, T const *value) {
1135  fits_write_col(reinterpret_cast<fitsfile *>(fptr), FitsTableType<T>::CONSTANT, col + 1, row + 1, 1,
1136  nElements, const_cast<T *>(value), &status);
1137  if (behavior & AUTO_CHECK) {
1138  LSST_FITS_CHECK_STATUS(*this, boost::format("Writing %d-element array at table cell (%d, %d)") %
1139  nElements % row % col);
1140  }
1141 }
1142 
1143 void Fits::writeTableScalar(std::size_t row, int col, std::string const &value) {
1144  // cfitsio doesn't let us specify the size of a string, it just looks for null terminator.
1145  // Using std::string::c_str() guarantees that we have one. But we can't store arbitrary
1146  // data in a string field because cfitsio will also chop off anything after the first null
1147  // terminator.
1148  char const *tmp = value.c_str();
1149  fits_write_col(reinterpret_cast<fitsfile *>(fptr), TSTRING, col + 1, row + 1, 1, 1,
1150  const_cast<char const **>(&tmp), &status);
1151  if (behavior & AUTO_CHECK) {
1152  LSST_FITS_CHECK_STATUS(*this, boost::format("Writing value at table cell (%d, %d)") % row % col);
1153  }
1154 }
1155 
1156 template <typename T>
1157 void Fits::readTableArray(std::size_t row, int col, int nElements, T *value) {
1158  int anynul = false;
1159  fits_read_col(reinterpret_cast<fitsfile *>(fptr), FitsTableType<T>::CONSTANT, col + 1, row + 1, 1,
1160  nElements, 0, value, &anynul, &status);
1161  if (behavior & AUTO_CHECK) {
1162  LSST_FITS_CHECK_STATUS(*this, boost::format("Reading value at table cell (%d, %d)") % row % col);
1163  }
1164 }
1165 
1166 void Fits::readTableScalar(std::size_t row, int col, std::string &value, bool isVariableLength) {
1167  int anynul = false;
1168  long size = isVariableLength ? getTableArraySize(row, col) : getTableArraySize(col);
1169  // We can't directly write into a std::string until C++17.
1170  std::vector<char> buf(size + 1, 0);
1171  // cfitsio wants a char** because they imagine we might want an array of strings,
1172  // but we only want one element.
1173  char *tmp = &buf.front();
1174  fits_read_col(reinterpret_cast<fitsfile *>(fptr), TSTRING, col + 1, row + 1, 1, 1, 0, &tmp, &anynul,
1175  &status);
1176  if (behavior & AUTO_CHECK) {
1177  LSST_FITS_CHECK_STATUS(*this, boost::format("Reading value at table cell (%d, %d)") % row % col);
1178  }
1179  value = std::string(tmp);
1180 }
1181 
1183  int typecode = 0;
1184  long result = 0;
1185  long width = 0;
1186  fits_get_coltype(reinterpret_cast<fitsfile *>(fptr), col + 1, &typecode, &result, &width, &status);
1187  if (behavior & AUTO_CHECK) {
1188  LSST_FITS_CHECK_STATUS(*this, boost::format("Looking up array size for column %d") % col);
1189  }
1190  return result;
1191 }
1192 
1194  long result = 0;
1195  long offset = 0;
1196  fits_read_descript(reinterpret_cast<fitsfile *>(fptr), col + 1, row + 1, &result, &offset, &status);
1197  if (behavior & AUTO_CHECK) {
1198  LSST_FITS_CHECK_STATUS(*this, boost::format("Looking up array size for cell (%d, %d)") % row % col);
1199  }
1200  return result;
1201 }
1202 
1203 // ---- Manipulating images ---------------------------------------------------------------------------------
1204 
1206  long naxes = 0;
1207  fits_create_img(reinterpret_cast<fitsfile *>(fptr), 8, 0, &naxes, &status);
1208  if (behavior & AUTO_CHECK) {
1209  LSST_FITS_CHECK_STATUS(*this, "Creating empty image HDU");
1210  }
1211 }
1212 
1213 void Fits::createImageImpl(int bitpix, int naxis, long const *naxes) {
1214  fits_create_img(reinterpret_cast<fitsfile *>(fptr), bitpix, naxis, const_cast<long *>(naxes), &status);
1215  if (behavior & AUTO_CHECK) {
1216  LSST_FITS_CHECK_STATUS(*this, "Creating new image HDU");
1217  }
1218 }
1219 
1220 template <typename T>
1221 void Fits::writeImageImpl(T const *data, int nElements) {
1222  fits_write_img(reinterpret_cast<fitsfile *>(fptr), FitsType<T>::CONSTANT, 1, nElements,
1223  const_cast<T *>(data), &status);
1224  if (behavior & AUTO_CHECK) {
1225  LSST_FITS_CHECK_STATUS(*this, "Writing image");
1226  }
1227 }
1228 
1229 namespace {
1230 
1235 struct ImageCompressionContext {
1236 public:
1237  ImageCompressionContext(Fits &fits_, ImageCompressionOptions const &useThis)
1238  : fits(fits_), old(fits.getImageCompression()) {
1239  fits.setImageCompression(useThis);
1240  }
1241  ~ImageCompressionContext() {
1242  int status = 0;
1243  std::swap(status, fits.status);
1244  try {
1245  fits.setImageCompression(old);
1246  } catch (...) {
1247  LOGLS_WARN("afw.fits",
1248  makeErrorMessage(fits.fptr, fits.status, "Failed to restore compression settings"));
1249  }
1250  std::swap(status, fits.status);
1251  }
1252 
1253 private:
1254  Fits &fits; // FITS file we're working on
1255  ImageCompressionOptions old; // Former compression options, to be restored
1256 };
1257 
1258 } // anonymous namespace
1259 
1260 template <typename T>
1264  auto fits = reinterpret_cast<fitsfile *>(fptr);
1265  ImageCompressionOptions const &compression =
1266  image.getBBox().getArea() > 0
1267  ? options.compression
1269  ImageCompressionOptions::NONE); // cfitsio can't compress empty images
1270  ImageCompressionContext comp(*this, compression); // RAII
1271  if (behavior & AUTO_CHECK) {
1272  LSST_FITS_CHECK_STATUS(*this, "Activating compression for write image");
1273  }
1274 
1275  ImageScale scale = options.scaling.determine(image, mask);
1276 
1277  // We need a place to put the image+header, and CFITSIO needs to know the dimenions.
1278  ndarray::Vector<long, 2> dims(image.getArray().getShape().reverse());
1279  createImageImpl(scale.bitpix == 0 ? detail::Bitpix<T>::value : scale.bitpix, 2, dims.elems);
1280 
1281  // Write the header
1284  if (header) {
1285  std::shared_ptr<daf::base::PropertySet> copy = header->deepCopy();
1286  copy->combine(wcsMetadata);
1287  header = copy;
1288  } else {
1289  header = wcsMetadata;
1290  }
1291  writeMetadata(*header);
1292 
1293  // Scale the image how we want it on disk
1294  ndarray::Array<T const, 2, 2> array = makeContiguousArray(image.getArray());
1295  auto pixels = scale.toFits(array, compression.quantizeLevel != 0, options.scaling.fuzz,
1296  options.compression.tiles, options.scaling.seed);
1297 
1298  // We only want cfitsio to do the scale and zero for unsigned 64-bit integer types. For those,
1299  // "double bzero" has sufficient precision to represent the appropriate value. We'll let
1300  // cfitsio handle it itself.
1301  // In all other cases, we will convert the image to use the appropriate scale and zero
1302  // (because we want to fuzz the numbers in the quantisation), so we don't want cfitsio
1303  // rescaling.
1305  fits_set_bscale(fits, 1.0, 0.0, &status);
1306  if (behavior & AUTO_CHECK) {
1307  LSST_FITS_CHECK_STATUS(*this, "Setting bscale,bzero");
1308  }
1309  }
1310 
1311  // Write the pixels
1312  int const fitsType = scale.bitpix == 0 ? FitsType<T>::CONSTANT : fitsTypeForBitpix(scale.bitpix);
1313  fits_write_img(fits, fitsType, 1, pixels->getNumElements(), const_cast<void *>(pixels->getData()),
1314  &status);
1315  if (behavior & AUTO_CHECK) {
1316  LSST_FITS_CHECK_STATUS(*this, "Writing image");
1317  }
1318 
1319  // Now write the headers we didn't want cfitsio to know about when we were writing the pixels
1320  // (because we don't want it using them to modify the pixels, and we don't want it overwriting
1321  // these values).
1323  std::isfinite(scale.bzero) && std::isfinite(scale.bscale) && (scale.bscale != 0.0)) {
1325  if (scale.bzero != 0.0) {
1326  fits_write_key_lng(fits, "BZERO", static_cast<long>(scale.bzero),
1327  "Scaling: MEMORY = BZERO + BSCALE * DISK", &status);
1328  }
1329  if (scale.bscale != 1.0) {
1330  fits_write_key_lng(fits, "BSCALE", static_cast<long>(scale.bscale),
1331  "Scaling: MEMORY = BZERO + BSCALE * DISK", &status);
1332  }
1333  } else {
1334  fits_write_key_dbl(fits, "BZERO", scale.bzero, 12, "Scaling: MEMORY = BZERO + BSCALE * DISK",
1335  &status);
1336  fits_write_key_dbl(fits, "BSCALE", scale.bscale, 12, "Scaling: MEMORY = BZERO + BSCALE * DISK",
1337  &status);
1338  }
1339  if (behavior & AUTO_CHECK) {
1340  LSST_FITS_CHECK_STATUS(*this, "Writing BSCALE,BZERO");
1341  }
1342  }
1343 
1344  if (scale.bitpix > 0 && !std::numeric_limits<T>::is_integer) {
1345  fits_write_key_lng(fits, "BLANK", scale.blank, "Value for undefined pixels", &status);
1346  fits_write_key_lng(fits, "ZDITHER0", options.scaling.seed, "Dithering seed", &status);
1347  fits_write_key_str(fits, "ZQUANTIZ", "SUBTRACTIVE_DITHER_1", "Dithering algorithm", &status);
1348  if (behavior & AUTO_CHECK) {
1349  LSST_FITS_CHECK_STATUS(*this, "Writing [Z]BLANK");
1350  }
1351  }
1352 
1353  // cfitsio says this is deprecated, but Pan-STARRS found that it was sometimes necessary, writing:
1354  // "This forces a re-scan of the header to ensure everything's kosher.
1355  // Without this, compressed HDUs have been written out with PCOUNT=0 and TFORM1 not correctly set."
1356  fits_set_hdustruc(fits, &status);
1357  if (behavior & AUTO_CHECK) {
1358  LSST_FITS_CHECK_STATUS(*this, "Finalizing header");
1359  }
1360 }
1361 
1362 namespace {
1363 
1365 template <typename T, class Enable = void>
1366 struct NullValue {
1367  static T constexpr value = 0;
1368 };
1369 
1371 template <typename T>
1372 struct NullValue<T, typename std::enable_if<std::numeric_limits<T>::has_quiet_NaN>::type> {
1373  static T constexpr value = std::numeric_limits<T>::quiet_NaN();
1374 };
1375 
1376 } // namespace
1377 
1378 template <typename T>
1379 void Fits::readImageImpl(int nAxis, T *data, long *begin, long *end, long *increment) {
1380  T null = NullValue<T>::value;
1381  int anyNulls = 0;
1382  fits_read_subset(reinterpret_cast<fitsfile *>(fptr), FitsType<T>::CONSTANT, begin, end, increment,
1383  reinterpret_cast<void *>(&null), data, &anyNulls, &status);
1384  if (behavior & AUTO_CHECK) LSST_FITS_CHECK_STATUS(*this, "Reading image");
1385 }
1386 
1388  int nAxis = 0;
1389  fits_get_img_dim(reinterpret_cast<fitsfile *>(fptr), &nAxis, &status);
1390  if (behavior & AUTO_CHECK) LSST_FITS_CHECK_STATUS(*this, "Getting NAXIS");
1391  return nAxis;
1392 }
1393 
1394 void Fits::getImageShapeImpl(int maxDim, long *nAxes) {
1395  fits_get_img_size(reinterpret_cast<fitsfile *>(fptr), maxDim, nAxes, &status);
1396  if (behavior & AUTO_CHECK) LSST_FITS_CHECK_STATUS(*this, "Getting NAXES");
1397 }
1398 
1399 template <typename T>
1401  int imageType = 0;
1402  fits_get_img_equivtype(reinterpret_cast<fitsfile *>(fptr), &imageType, &status);
1403  if (behavior & AUTO_CHECK) LSST_FITS_CHECK_STATUS(*this, "Getting image type");
1405  if (imageType < 0) {
1406  return false; // can't represent floating-point with integer
1407  }
1409  if (isFitsImageTypeSigned(imageType)) {
1410  return FitsBitPix<T>::CONSTANT >= imageType;
1411  } else {
1412  // need extra bits to safely convert unsigned to signed
1413  return FitsBitPix<T>::CONSTANT > imageType;
1414  }
1415  } else {
1416  if (!isFitsImageTypeSigned(imageType)) {
1417  return FitsBitPix<T>::CONSTANT >= imageType;
1418  } else if (imageType == LONGLONG_IMG) {
1419  // workaround for CFITSIO not recognizing uint64 as
1420  // unsigned
1421  return FitsBitPix<T>::CONSTANT >= imageType;
1422  } else {
1423  return false;
1424  }
1425  }
1426  }
1427  // we allow all conversions to float and double, even if they lose precision
1428  return true;
1429 }
1430 
1432  int bitpix = 0;
1433  fits_get_img_equivtype(reinterpret_cast<fitsfile *>(fptr), &bitpix, &status);
1434  if (behavior & AUTO_CHECK) LSST_FITS_CHECK_STATUS(*this, "Getting image type");
1435  // FITS' 'BITPIX' key is the number of bits in a pixel, but negative for
1436  // floats. But the above CFITSIO routine adds support for unsigned
1437  // integers by checking BZERO for an offset as well. So the 'bitpix' value
1438  // we get back from that should be the raw value for signed integers and
1439  // floats, but may be something else (still positive) for unsigned, and
1440  // hence we'll compare to some FITSIO constants to be safe looking at
1441  // integers.
1442  if (bitpix < 0) {
1443  return "float" + std::to_string(-bitpix);
1444  }
1445  switch (bitpix) {
1446  case BYTE_IMG: return "uint8";
1447  case SBYTE_IMG: return "int8";
1448  case SHORT_IMG: return "int16";
1449  case USHORT_IMG: return "uint16";
1450  case LONG_IMG: return "int32";
1451  case ULONG_IMG: return "uint32";
1452  case LONGLONG_IMG: return "int64";
1453  }
1454  throw LSST_EXCEPT(
1455  FitsError,
1456  (boost::format("Unrecognized BITPIX value: %d") % bitpix).str()
1457  );
1458 }
1459 
1461  auto fits = reinterpret_cast<fitsfile *>(fptr);
1462  int compType = 0; // cfitsio compression type
1463  fits_get_compression_type(fits, &compType, &status);
1464  if (behavior & AUTO_CHECK) {
1465  LSST_FITS_CHECK_STATUS(*this, "Getting compression type");
1466  }
1467 
1468  ImageCompressionOptions::Tiles tiles = ndarray::allocate(MAX_COMPRESS_DIM);
1469  fits_get_tile_dim(fits, tiles.getNumElements(), tiles.getData(), &status);
1470  if (behavior & AUTO_CHECK) {
1471  LSST_FITS_CHECK_STATUS(*this, "Getting tile dimensions");
1472  }
1473 
1474  float quantizeLevel;
1475  fits_get_quantize_level(fits, &quantizeLevel, &status);
1476  if (behavior & AUTO_CHECK) {
1477  LSST_FITS_CHECK_STATUS(*this, "Getting quantizeLevel");
1478  }
1479 
1480  return ImageCompressionOptions(compressionAlgorithmFromCfitsio(compType), tiles, quantizeLevel);
1481 }
1482 
1484  auto fits = reinterpret_cast<fitsfile *>(fptr);
1485  fits_unset_compression_request(fits, &status); // wipe the slate and start over
1487  allowImageCompression ? comp.algorithm : ImageCompressionOptions::NONE;
1488  fits_set_compression_type(fits, compressionAlgorithmToCfitsio(algorithm), &status);
1489  if (behavior & AUTO_CHECK) {
1490  LSST_FITS_CHECK_STATUS(*this, "Setting compression type");
1491  }
1492 
1493  if (algorithm == ImageCompressionOptions::NONE) {
1494  // Nothing else worth doing
1495  return;
1496  }
1497 
1498  fits_set_tile_dim(fits, comp.tiles.getNumElements(), comp.tiles.getData(), &status);
1499  if (behavior & AUTO_CHECK) {
1500  LSST_FITS_CHECK_STATUS(*this, "Setting tile dimensions");
1501  }
1502 
1504  fits_set_quantize_level(fits, comp.quantizeLevel, &status);
1505  if (behavior & AUTO_CHECK) {
1506  LSST_FITS_CHECK_STATUS(*this, "Setting quantization level");
1507  }
1508  }
1509 }
1510 
1511 void setAllowImageCompression(bool allow) { allowImageCompression = allow; }
1512 
1513 bool getAllowImageCompression() { return allowImageCompression; }
1514 
1515 // ---- Manipulating files ----------------------------------------------------------------------------------
1516 
1517 Fits::Fits(std::string const &filename, std::string const &mode, int behavior_)
1518  : fptr(0), status(0), behavior(behavior_) {
1519  if (mode == "r" || mode == "rb") {
1520  fits_open_file(reinterpret_cast<fitsfile **>(&fptr), const_cast<char *>(filename.c_str()), READONLY,
1521  &status);
1522  } else if (mode == "w" || mode == "wb") {
1523  boost::filesystem::remove(filename); // cfitsio doesn't like over-writing files
1524  fits_create_file(reinterpret_cast<fitsfile **>(&fptr), const_cast<char *>(filename.c_str()), &status);
1525  } else if (mode == "a" || mode == "ab") {
1526  fits_open_file(reinterpret_cast<fitsfile **>(&fptr), const_cast<char *>(filename.c_str()), READWRITE,
1527  &status);
1528  int nHdu = 0;
1529  fits_get_num_hdus(reinterpret_cast<fitsfile *>(fptr), &nHdu, &status);
1530  fits_movabs_hdu(reinterpret_cast<fitsfile *>(fptr), nHdu, NULL, &status);
1531  if ((behavior & AUTO_CHECK) && (behavior & AUTO_CLOSE) && (status) && (fptr)) {
1532  // We're about to throw an exception, and the destructor won't get called
1533  // because we're in the constructor, so cleanup here first.
1534  int tmpStatus = 0;
1535  fits_close_file(reinterpret_cast<fitsfile *>(fptr), &tmpStatus);
1536  }
1537  } else {
1538  throw LSST_EXCEPT(
1539  FitsError,
1540  (boost::format("Invalid mode '%s' given when opening file '%s'") % mode % filename).str());
1541  }
1542  if (behavior & AUTO_CHECK) {
1543  LSST_FITS_CHECK_STATUS(*this, boost::format("Opening file '%s' with mode '%s'") % filename % mode);
1544  }
1545 }
1546 
1547 Fits::Fits(MemFileManager &manager, std::string const &mode, int behavior_)
1548  : fptr(0), status(0), behavior(behavior_) {
1549  typedef void *(*Reallocator)(void *, std::size_t);
1550  // It's a shame this logic is essentially a duplicate of above, but the innards are different enough
1551  // we can't really reuse it.
1552  if (mode == "r" || mode == "rb") {
1553  fits_open_memfile(reinterpret_cast<fitsfile **>(&fptr), "unused", READONLY, &manager._ptr,
1554  &manager._len, 0, 0, // no reallocator or deltasize necessary for READONLY
1555  &status);
1556  } else if (mode == "w" || mode == "wb") {
1557  Reallocator reallocator = 0;
1558  if (manager._managed) reallocator = &std::realloc;
1559  fits_create_memfile(reinterpret_cast<fitsfile **>(&fptr), &manager._ptr, &manager._len, 0,
1560  reallocator, // use default deltasize
1561  &status);
1562  } else if (mode == "a" || mode == "ab") {
1563  Reallocator reallocator = 0;
1564  if (manager._managed) reallocator = &std::realloc;
1565  fits_open_memfile(reinterpret_cast<fitsfile **>(&fptr), "unused", READWRITE, &manager._ptr,
1566  &manager._len, 0, reallocator, &status);
1567  int nHdu = 0;
1568  fits_get_num_hdus(reinterpret_cast<fitsfile *>(fptr), &nHdu, &status);
1569  fits_movabs_hdu(reinterpret_cast<fitsfile *>(fptr), nHdu, NULL, &status);
1570  if ((behavior & AUTO_CHECK) && (behavior & AUTO_CLOSE) && (status) && (fptr)) {
1571  // We're about to throw an exception, and the destructor won't get called
1572  // because we're in the constructor, so cleanup here first.
1573  int tmpStatus = 0;
1574  fits_close_file(reinterpret_cast<fitsfile *>(fptr), &tmpStatus);
1575  }
1576  } else {
1577  throw LSST_EXCEPT(FitsError,
1578  (boost::format("Invalid mode '%s' given when opening memory file at '%s'") % mode %
1579  manager._ptr)
1580  .str());
1581  }
1582  if (behavior & AUTO_CHECK) {
1584  *this, boost::format("Opening memory file at '%s' with mode '%s'") % manager._ptr % mode);
1585  }
1586 }
1587 
1589  fits_close_file(reinterpret_cast<fitsfile *>(fptr), &status);
1590  fptr = nullptr;
1591 }
1592 
1596  auto combined = std::make_shared<daf::base::PropertyList>();
1597  bool const asScalar = true;
1598  for (auto const &name : first->getOrderedNames()) {
1599  auto const iscv = isCommentIsValid(*first, name);
1600  if (iscv.isComment) {
1601  if (iscv.isValid) {
1602  combined->add<std::string>(name, first->getArray<std::string>(name));
1603  }
1604  } else {
1605  combined->copy(name, first, name, asScalar);
1606  }
1607  }
1608  for (auto const &name : second->getOrderedNames()) {
1609  auto const iscv = isCommentIsValid(*second, name);
1610  if (iscv.isComment) {
1611  if (iscv.isValid) {
1612  combined->add<std::string>(name, second->getArray<std::string>(name));
1613  }
1614  } else {
1615  // `copy` will replace an item, even if has a different type, so no need to call `remove`
1616  combined->copy(name, second, name, asScalar);
1617  }
1618  }
1619  return combined;
1620 }
1621 
1624  fp.setHdu(hdu);
1625  return readMetadata(fp, strip);
1626 }
1627 
1630  fp.setHdu(hdu);
1631  return readMetadata(fp, strip);
1632 }
1633 
1635  auto metadata = std::make_shared<lsst::daf::base::PropertyList>();
1636  fitsfile.readMetadata(*metadata, strip);
1637  // if INHERIT=T, we want to also include header entries from the primary HDU
1638  int oldHdu = fitsfile.getHdu();
1639  if (oldHdu != 0 && metadata->exists("INHERIT")) {
1640  bool inherit = false;
1641  if (metadata->typeOf("INHERIT") == typeid(std::string)) {
1642  inherit = (metadata->get<std::string>("INHERIT") == "T");
1643  } else {
1644  inherit = metadata->get<bool>("INHERIT");
1645  }
1646  if (strip) metadata->remove("INHERIT");
1647  if (inherit) {
1648  HduMoveGuard guard(fitsfile, 0);
1649  // Combine the metadata from the primary HDU with the metadata from the specified HDU,
1650  // with non-comment values from the specified HDU superseding those in the primary HDU
1651  // and comments from the specified HDU appended to comments from the primary HDU
1652  auto primaryHduMetadata = std::make_shared<daf::base::PropertyList>();
1653  fitsfile.readMetadata(*primaryHduMetadata, strip);
1654  metadata = combineMetadata(primaryHduMetadata, metadata);
1655  } else {
1656  // Purge invalid values
1657  auto const emptyMetadata = std::make_shared<lsst::daf::base::PropertyList>();
1658  metadata = combineMetadata(metadata, emptyMetadata);
1659  }
1660  }
1661  return metadata;
1662 }
1663 
1664 
1665 HduMoveGuard::HduMoveGuard(Fits & fits, int hdu, bool relative) :
1666  _fits(fits),
1667  _oldHdu(_fits.getHdu()),
1668  _enabled(true)
1669 {
1670  _fits.setHdu(hdu, relative);
1671 }
1672 
1674  if (!_enabled) {
1675  return;
1676  }
1677  int status = 0;
1678  std::swap(status, _fits.status); // unset error indicator, but remember the old status
1679  try {
1680  _fits.setHdu(_oldHdu);
1681  } catch (...) {
1682  LOGL_WARN(
1683  "afw.fits",
1684  makeErrorMessage(_fits.fptr, _fits.status, "Failed to move back to HDU %d").c_str(),
1685  _oldHdu
1686  );
1687  }
1688  std::swap(status, _fits.status); // reset the old status
1689 }
1690 
1692  auto fits = reinterpret_cast<fitsfile *>(fptr);
1693  if (getHdu() != 0 || countHdus() == 1) {
1694  return false; // Can't possibly be the PHU leading a compressed image
1695  }
1696  // Check NAXIS = 0
1697  int naxis;
1698  fits_get_img_dim(fits, &naxis, &status);
1699  if (behavior & AUTO_CHECK) {
1700  LSST_FITS_CHECK_STATUS(*this, "Checking NAXIS of PHU");
1701  }
1702  if (naxis != 0) {
1703  return false;
1704  }
1705  // Check first extension (and move back there when we're done if we're not compressed)
1706  HduMoveGuard move(*this, 1);
1707  bool isCompressed = fits_is_compressed_image(fits, &status);
1708  if (behavior & AUTO_CHECK) {
1709  LSST_FITS_CHECK_STATUS(*this, "Checking compression");
1710  }
1711  if (isCompressed) {
1712  move.disable();
1713  }
1714  return isCompressed;
1715 }
1716 
1718  : compression(fits::compressionAlgorithmFromString(config.get<std::string>("compression.algorithm")),
1719  std::vector<long>{config.getAsInt64("compression.columns"),
1720  config.getAsInt64("compression.rows")},
1721  config.getAsDouble("compression.quantizeLevel")),
1722  scaling(fits::scalingAlgorithmFromString(config.get<std::string>("scaling.algorithm")),
1723  config.getAsInt("scaling.bitpix"),
1724  config.exists("scaling.maskPlanes") ? config.getArray<std::string>("scaling.maskPlanes")
1726  config.getAsInt("scaling.seed"), config.getAsDouble("scaling.quantizeLevel"),
1727  config.getAsDouble("scaling.quantizePad"), config.get<bool>("scaling.fuzz"),
1728  config.getAsDouble("scaling.bscale"), config.getAsDouble("scaling.bzero")) {}
1729 
1730 namespace {
1731 
1732 template <typename T>
1733 void validateEntry(daf::base::PropertySet &output, daf::base::PropertySet const &input,
1734  std::string const &name, T defaultValue) {
1735  output.add(name, input.get<T>(name, defaultValue));
1736 }
1737 
1738 template <typename T>
1739 void validateEntry(daf::base::PropertySet &output, daf::base::PropertySet const &input,
1740  std::string const &name, std::vector<T> defaultValue) {
1741  output.add(name, input.exists(name) ? input.getArray<T>(name) : defaultValue);
1742 }
1743 
1744 } // anonymous namespace
1745 
1747  auto validated = std::make_shared<daf::base::PropertySet>();
1748 
1749  validateEntry(*validated, config, "compression.algorithm", std::string("NONE"));
1750  validateEntry(*validated, config, "compression.columns", 0);
1751  validateEntry(*validated, config, "compression.rows", 1);
1752  validateEntry(*validated, config, "compression.quantizeLevel", 0.0);
1753 
1754  validateEntry(*validated, config, "scaling.algorithm", std::string("NONE"));
1755  validateEntry(*validated, config, "scaling.bitpix", 0);
1756  validateEntry(*validated, config, "scaling.maskPlanes", std::vector<std::string>{"NO_DATA"});
1757  validateEntry(*validated, config, "scaling.seed", 1);
1758  validateEntry(*validated, config, "scaling.quantizeLevel", 5.0);
1759  validateEntry(*validated, config, "scaling.quantizePad", 10.0);
1760  validateEntry(*validated, config, "scaling.fuzz", true);
1761  validateEntry(*validated, config, "scaling.bscale", 1.0);
1762  validateEntry(*validated, config, "scaling.bzero", 0.0);
1763 
1764  // Check for additional entries that we don't support (e.g., from typos)
1765  for (auto const &name : config.names(false)) {
1766  if (!validated->exists(name)) {
1768  os << "Invalid image write option: " << name;
1770  }
1771  }
1772 
1773  return validated;
1774 }
1775 
1776 #define INSTANTIATE_KEY_OPS(r, data, T) \
1777  template void Fits::updateKey(std::string const &, T const &, std::string const &); \
1778  template void Fits::writeKey(std::string const &, T const &, std::string const &); \
1779  template void Fits::updateKey(std::string const &, T const &); \
1780  template void Fits::writeKey(std::string const &, T const &); \
1781  template void Fits::updateColumnKey(std::string const &, int, T const &, std::string const &); \
1782  template void Fits::writeColumnKey(std::string const &, int, T const &, std::string const &); \
1783  template void Fits::updateColumnKey(std::string const &, int, T const &); \
1784  template void Fits::writeColumnKey(std::string const &, int, T const &); \
1785  template void Fits::readKey(std::string const &, T &);
1786 
1787 #define INSTANTIATE_IMAGE_OPS(r, data, T) \
1788  template void Fits::writeImageImpl(T const *, int); \
1789  template void Fits::writeImage(image::ImageBase<T> const &, ImageWriteOptions const &, \
1790  std::shared_ptr<daf::base::PropertySet const>, \
1791  std::shared_ptr<image::Mask<image::MaskPixel> const>); \
1792  template void Fits::readImageImpl(int, T *, long *, long *, long *); \
1793  template bool Fits::checkImageType<T>(); \
1794  template int getBitPix<T>();
1795 
1796 #define INSTANTIATE_TABLE_OPS(r, data, T) \
1797  template int Fits::addColumn<T>(std::string const &ttype, int size); \
1798  template int Fits::addColumn<T>(std::string const &ttype, int size, std::string const &comment);
1799 #define INSTANTIATE_TABLE_ARRAY_OPS(r, data, T) \
1800  template void Fits::writeTableArray(std::size_t row, int col, int nElements, T const *value); \
1801  template void Fits::readTableArray(std::size_t row, int col, int nElements, T *value);
1802 
1803 // ----------------------------------------------------------------------------------------------------------
1804 // ---- Explicit instantiation ------------------------------------------------------------------------------
1805 // ----------------------------------------------------------------------------------------------------------
1806 
1807 #define KEY_TYPES \
1808  (bool)(unsigned char)(short)(unsigned short)(int)(unsigned int)(long)(unsigned long)(LONGLONG)( \
1809  float)(double)(std::complex<float>)(std::complex<double>)(std::string)
1810 
1811 #define COLUMN_TYPES \
1812  (bool)(std::string)(std::int8_t)(std::uint8_t)(std::int16_t)(std::uint16_t)(std::int32_t)(std::uint32_t) \
1813  (std::int64_t)(float)(double)(lsst::geom::Angle)(std::complex<float>)(std::complex<double>)
1814 
1815 #define COLUMN_ARRAY_TYPES \
1816  (bool)(char)(std::uint8_t)(std::int16_t)(std::uint16_t)(std::int32_t)(std::uint32_t)(std::int64_t)( \
1817  float)(double)(lsst::geom::Angle)(std::complex<float>)(std::complex<double>)
1818 
1819 #define IMAGE_TYPES \
1820  (unsigned char)(short)(unsigned short)(int)(unsigned int)(std::int64_t)(std::uint64_t)(float)(double)
1821 
1826 } // namespace fits
1827 } // namespace afw
1828 } // namespace lsst
#define LOGLS_WARN(logger, message)
#define INSTANTIATE_TABLE_OPS(r, data, T)
Definition: fits.cc:1796
#define INSTANTIATE_KEY_OPS(r, data, T)
Definition: fits.cc:1776
int getImageDim()
Return the number of dimensions in the current HDU.
Definition: fits.cc:1387
std::size_t countRows()
Return the number of row in a table.
Definition: fits.cc:1124
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.
Definition: fits.cc:1593
std::string getFileName() const
Return the file name associated with the FITS object or "<unknown>" if there is none.
Definition: fits.cc:479
void forEachKey(HeaderIterationFunctor &functor)
Call a polymorphic functor for every key in the header.
Definition: fits.cc:762
T empty(T... args)
bool isComment
Definition: fits.cc:379
ImageCompressionOptions getImageCompression()
Return the current image compression settings.
Definition: fits.cc:1460
ImageCompressionOptions compression
Options controlling compression.
Definition: fits.h:220
T find_first_not_of(T... args)
afw::table::Key< afw::table::Array< MaskPixelT > > mask
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
Definition: fits.cc:1060
#define LSST_FITS_CHECK_STATUS(fitsObj,...)
Throw a FitsError exception if the status of the given Fits object is nonzero.
Definition: fits.h:111
std::vector< T > getArray(std::string const &name) const
int compressionAlgorithmToCfitsio(ImageCompressionOptions::CompressionAlgorithm algorithm)
Convert ImageCompressionOptions::CompressionAlgorithm to cfitsio.
T swap(T... args)
T front(T... args)
T get(std::string const &name) const
long blank
Value for integer images indicating non-finite values.
void readKey(std::string const &key, T &value)
Read a FITS header key into the given reference.
Definition: fits.cc:755
int bitpix
Bits per pixel; negative means floating-point: 8,16,32,64,-32,-64.
#define IMAGE_TYPES
Definition: fits.cc:1819
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.
Definition: fits.cc:672
Options for writing an image to FITS.
Definition: fits.h:219
std::string const & getComment(std::string const &name) const
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:109
T to_string(T... args)
int getBitPix()
Return the cfitsio integer BITPIX code for the given data type.
Definition: fits.cc:471
void readTableScalar(std::size_t row, int col, T &value)
Read an array scalar from a binary table.
Definition: fits.h:595
bool checkImageType()
Return true if the current HDU is compatible with the given pixel type.
Definition: fits.cc:1400
#define INSTANTIATE_TABLE_ARRAY_OPS(r, data, T)
Definition: fits.cc:1799
STL namespace.
bool strip
Definition: fits.cc:883
STL class.
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.
Definition: fits.cc:680
T end(T... args)
std::vector< std::string > paramNames(bool topLevelOnly=true) const
CompressionAlgorithm
Compression algorithms.
T free(T... args)
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:102
long getTableArraySize(int col)
Return the size of an array column.
Definition: fits.cc:1182
FITS BITPIX header value by C++ type.
void closeFile()
Close a FITS file.
Definition: fits.cc:1588
int seed
Seed for random number generator when fuzzing.
Fits * fits
Definition: FitsWriter.cc:90
void createEmpty()
Create an empty image HDU with NAXIS=0 at the end of the file.
Definition: fits.cc:1205
std::string getImageDType()
Return the numpy dtype equivalent of the image pixel type (e.g.
Definition: fits.cc:1431
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.
Definition: fits.cc:640
STL class.
ndarray::Array< T const, N, N > const makeContiguousArray(ndarray::Array< T, N, C > const &array)
Construct a contiguous ndarray.
Definition: fits.h:208
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:36
Tiles tiles
Tile size; a dimension with 0 means infinite (e.g., to specify one row: 0,1)
T push_back(T... args)
Options for tile compression of image pixels.
A base class for image defects.
char * data
Definition: BaseRecord.cc:62
int end
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: ImageBase.h:462
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
int64_t getAsInt64(std::string const &name) const
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
double bzero
Zero-point to apply when reading from FITS.
std::size_t addRows(std::size_t nRows)
Append rows to a table, and return the index of the first new row.
Definition: fits.cc:1114
std::shared_ptr< daf::base::PropertyList > createTrivialWcsMetadata(std::string const &wcsName, lsst::geom::Point2I const &xy0)
Definition: wcsUtils.cc:51
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromString(std::string const &name)
Interpret compression algorithm expressed in string.
int getArea() const
Fits()
Default constructor; set all data members to 0.
Definition: fits.h:609
T erase(T... args)
#define INSTANTIATE_IMAGE_OPS(r, data, T)
Definition: fits.cc:1787
Key< U > key
Definition: Schema.cc:281
T str(T... args)
T isfinite(T... args)
bool exists(std::string const &name) const
ndarray::Array< long, 1, 1 > Tiles
void createTable()
Create a new binary table extension.
Definition: fits.cc:1082
#define LOGL_WARN(logger, message...)
double bscale
Scale to apply when reading from FITS.
table::Key< int > type
Definition: Detector.cc:167
T infinity(T... args)
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:195
void setHdu(int hdu, bool relative=false)
Set the current HDU.
Definition: fits.cc:494
T count(T... args)
T find_last_not_of(T... args)
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
lsst::geom::Point2I getXY0() const
Return the image&#39;s origin.
Definition: ImageBase.h:340
T find(T... args)
T size(T... args)
STL class.
#define LSST_EXCEPT(type,...)
T realloc(T... args)
T name(T... args)
#define COLUMN_TYPES
Definition: fits.cc:1811
T malloc(T... args)
ImageScalingOptions scaling
Options controlling scaling.
Definition: fits.h:221
static std::shared_ptr< daf::base::PropertySet > validate(daf::base::PropertySet const &config)
Validate a PropertySet.
Definition: fits.cc:1746
void disable()
Disable the guard, leaving the HDU at its current state at destruction.
Definition: fits.h:751
bool checkCompressedImagePhu()
Go to the first image header in the FITS file.
Definition: fits.cc:1691
void setAllowImageCompression(bool allow)
Definition: fits.cc:1511
T c_str(T... args)
float quantizeLevel
quantization level: 0.0 = none requires use of GZIP or GZIP_SHUFFLE
void writeTableArray(std::size_t row, int col, int nElements, T const *value)
Write an array value to a binary table.
Definition: fits.cc:1134
void reset()
Return the manager to the same state it would be if default-constructed.
Definition: fits.cc:456
CompressionAlgorithm algorithm
Compresion algorithm to use.
void readTableArray(std::size_t row, int col, int nElements, T *value)
Read an array value from a binary table.
Definition: fits.cc:1157
std::string prefix
Definition: SchemaMapper.cc:79
T isnan(T... args)
T substr(T... args)
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromCfitsio(int cfitsio)
Convert compression algorithm from cfitsio to ImageCompressionOptions::CompressionAlgorithm.
ImageScale determine(image::ImageBase< T > const &image, std::shared_ptr< image::Mask< image::MaskPixel > const > mask=nullptr) const
Determine the scaling for a particular image.
void setImageCompression(ImageCompressionOptions const &options)
Set compression options for writing FITS images.
Definition: fits.cc:1483
daf::base::PropertyList * list
Definition: fits.cc:885
T quiet_NaN(T... args)
void writeKey(std::string const &key, T const &value, std::string const &comment)
Add a FITS header key to the bottom of the header.
Definition: fits.cc:648
bool fuzz
Fuzz the values when quantising floating-point values?
#define COLUMN_ARRAY_TYPES
Definition: fits.cc:1815
std::vector< std::string > names(bool topLevelOnly=true) const
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
bool getAllowImageCompression()
Definition: fits.cc:1513
int getHdu()
Return the current HDU (0-indexed; 0 is the Primary HDU).
Definition: fits.cc:488
void add(std::string const &name, T const &value)
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
Definition: fits.h:477
std::ostream * os
Definition: Schema.cc:746
std::vector< std::string > getOrderedNames() const
bool isValid
Definition: fits.cc:380
int addColumn(std::string const &ttype, int size, std::string const &comment)
Add a column to a table.
Definition: fits.cc:1105
std::string makeErrorMessage(std::string const &fileName="", int status=0, std::string const &msg="")
Return an error message reflecting FITS I/O errors.
Definition: fits.cc:407
#define KEY_TYPES
Definition: fits.cc:1807
RAII scoped guard for moving the HDU in a Fits object.
Definition: fits.h:724
ImageScalingOptions::ScalingAlgorithm scalingAlgorithmFromString(std::string const &name)
Interpret scaling algorithm expressed in string.
std::string const wcsNameForXY0
Definition: ImageBase.h:70
Base class for polymorphic functors used to iterator over FITS key headers.
Definition: fits.h:49
void writeTableScalar(std::size_t row, int col, T value)
Write a scalar value to a binary table.
Definition: fits.h:583
void readMetadata(daf::base::PropertySet &metadata, bool strip=false)
Read a FITS header into a PropertySet or PropertyList.
Definition: fits.cc:1052
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.
Definition: fits.cc:438
T compare(T... args)
T reserve(T... args)
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
std::shared_ptr< detail::PixelArrayBase > toFits(ndarray::Array< T const, 2, 2 > const &image, bool forceNonfiniteRemoval, bool fuzz=true, ndarray::Array< long, 1 > const &tiles=ndarray::Array< long, 1, 1 >(), int seed=1) const
Convert to an array of pixel values to write to FITS.
ImageWriteOptions(image::Image< T > const &image)
Construct with default options for images.
Definition: fits.h:225
int countHdus()
Return the number of HDUs in the file.
Definition: fits.cc:515
Scale to apply to image.
daf::base::PropertySet * set
Definition: fits.cc:884