LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
FitsChan.h
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2017 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 #ifndef ASTSHIM_FITSCHAN_H
23 #define ASTSHIM_FITSCHAN_H
24 
25 #include <complex>
26 #include <string>
27 #include <vector>
28 
29 #include "astshim/base.h"
30 #include "astshim/Object.h"
31 #include "astshim/Stream.h"
32 #include "astshim/Channel.h"
33 #include "astshim/KeyMap.h"
34 
35 namespace ast {
36 
40 enum class FitsKeyState {
41  ABSENT = 0,
42  NOVALUE,
43  PRESENT
44 };
45 
49 enum class CardType {
50  NOTYPE = AST__NOTYPE,
51  COMMENT = AST__COMMENT,
52  INT = AST__INT,
53  FLOAT = AST__FLOAT,
54  STRING = AST__STRING,
55  COMPLEXF = AST__COMPLEXF,
56  COMPLEXI = AST__COMPLEXI,
57  LOGICAL = AST__LOGICAL,
58  CONTINUE = AST__CONTINUE,
59  UNDEF = AST__UNDEF,
60 };
61 
68 template <typename T>
69 class FoundValue {
70 public:
77  FoundValue(bool found, T const &value) : found(found), value(value) {}
78 
80  FoundValue() : found(false), value() {}
81  bool found;
82  T value;
83 };
84 
202 class FitsChan : public Channel {
203 public:
214  explicit FitsChan(Stream &stream, std::string const &options = "");
215 
216  virtual ~FitsChan();
217 
218  FitsChan(FitsChan const &) = delete;
219  FitsChan(FitsChan &&) = default;
220  FitsChan &operator=(FitsChan const &) = delete;
221  FitsChan &operator=(FitsChan &&) = default;
222 
230  void delFits() {
231  astDelFits(getRawPtr());
232  assertOK();
233  }
234 
244  void emptyFits() {
245  astEmptyFits(getRawPtr());
246  assertOK();
247  }
248 
347 
366  std::complex<double> defval = {0, 0}) const;
367 
387  FoundValue<std::string> getFitsCN(std::string const &name = "", std::string defval = "") const;
388 
406  FoundValue<double> getFitsF(std::string const &name = "", double defval = 0) const;
407 
425  FoundValue<int> getFitsI(std::string const &name = "", int defval = 0) const;
426 
444  FoundValue<bool> getFitsL(std::string const &name = "", bool defval = false) const;
445 
467  FoundValue<std::string> getFitsS(std::string const &name = "", std::string defval = "") const;
468 
476 
481  std::string getAllWarnings() const { return getC("AllWarnings"); }
482 
486  int getCard() const { return getI("Card"); }
487 
491  std::string getCardComm() const { return getC("CardComm"); }
492 
496  std::string getCardName() const { return getC("CardName"); }
497 
501  CardType getCardType() const { return static_cast<CardType>(getI("CardType")); }
502 
506  bool getCarLin() const { return getB("CarLin"); }
507 
512  bool getCDMatrix() const { return getB("CDMatrix"); }
513 
517  bool getClean() const { return getB("Clean"); }
518 
522  bool getDefB1950() const { return getB("DefB1950"); }
523 
528  std::string getEncoding() const { return getC("Encoding"); }
529 
534  std::string getFitsAxisOrder() const { return getC("FitsAxisOrder"); }
535 
540  int getFitsDigits() const { return getI("FitsDigits"); }
541 
545  double getFitsTol() const { return getD("FitsTol"); }
546 
550  bool getIwc() const { return getB("Iwc"); }
551 
555  int getNCard() const { return getI("NCard"); }
556 
560  int getNKey() const { return getI("NKey"); }
561 
565  bool getSipOK() const { return getB("SipOK"); }
566 
571  bool getSipReplace() const { return getB("SipReplace"); }
572 
576  int getTabOK() const { return getI("TabOK"); }
577 
582  int getPolyTan() const { return getI("PolyTan"); }
583 
588  std::string getWarnings() const { return getC("Warnings"); }
589 
595  void purgeWcs() {
596  astPurgeWCS(getRawPtr());
597  assertOK();
598  }
599 
616  void putCards(std::string const &cards) {
617  astPutCards(getRawPtr(), cards.c_str());
618  assertOK();
619  }
620 
644  void putFits(std::string const &card, bool overwrite) {
645  astPutFits(getRawPtr(), card.c_str(), overwrite);
646  assertOK();
647  }
648 
661  void readFits() {
662  astReadFits(getRawPtr());
663  assertOK();
664  }
665 
674  void retainFits() {
675  astRetainFits(getRawPtr());
676  assertOK();
677  }
678 
696  void setFitsCF(std::string const &name, std::complex<double> value, std::string const &comment = "",
697  bool overwrite = false) {
698  // this use of reinterpret_cast is explicitly permitted, for C compatibility
699  astSetFitsCF(getRawPtr(), name.c_str(), reinterpret_cast<double(&)[2]>(value), comment.c_str(),
700  overwrite);
701  assertOK();
702  }
703 
725  void setFitsCM(std::string const &comment, bool overwrite = false) {
726  astSetFitsCM(getRawPtr(), comment.c_str(), overwrite);
727  assertOK();
728  }
729 
756  void setFitsCN(std::string const &name, std::string value, std::string const &comment = "",
757  bool overwrite = false) {
758  astSetFitsCN(getRawPtr(), name.c_str(), value.c_str(), comment.c_str(), overwrite);
759  assertOK();
760  }
761 
786  void setFitsF(std::string const &name, double value, std::string const &comment = "",
787  bool overwrite = false) {
788  astSetFitsF(getRawPtr(), name.c_str(), value, comment.c_str(), overwrite);
789  assertOK();
790  }
791 
816  void setFitsI(std::string const &name, int value, std::string const &comment = "",
817  bool overwrite = false) {
818  astSetFitsI(getRawPtr(), name.c_str(), value, comment.c_str(), overwrite);
819  assertOK();
820  }
821 
846  void setFitsL(std::string const &name, bool value, std::string const &comment = "",
847  bool overwrite = false) {
848  astSetFitsL(getRawPtr(), name.c_str(), value, comment.c_str(), overwrite);
849  assertOK();
850  }
851 
884  void setFitsS(std::string const &name, std::string value, std::string const &comment = "",
885  bool overwrite = false) {
886  astSetFitsS(getRawPtr(), name.c_str(), value.c_str(), comment.c_str(), overwrite);
887  assertOK();
888  }
889 
915  void setFitsU(std::string const &name, std::string const &comment = "", bool overwrite = false) {
916  astSetFitsU(getRawPtr(), name.c_str(), comment.c_str(), overwrite);
917  assertOK();
918  }
919 
923  void setCarLin(bool cdMatrix) { setB("CarLin", cdMatrix); }
924 
929  void setCDMatrix(bool cdMatrix) { setB("CDMatrix", cdMatrix); }
930 
934  void setClean(bool clean) { setB("Clean", clean); }
935 
939  void setDefB1950(bool defB1950) { setB("DefB1950", defB1950); }
940 
945  void setEncoding(std::string const &encoding) { setC("Encoding", encoding); }
946 
951  void setFitsAxisOrder(std::string const &order) { setC("FitsAxisOrder", order); }
952 
957  void setFitsDigits(int digits) { setI("FitsDigits", digits); }
958 
962  void setFitsTol(double tol) { return setD("FitsTol", tol); }
963 
967  void setIwc(bool iwcs) { setB("Iwc", iwcs); }
968 
972  void setSipOK(bool sipOK) { setB("SipOK", sipOK); }
973 
978  void setSipReplace(bool replace) { setB("SipReplace", replace); }
979 
983  void setTabOK(int tabOK) { setI("TabOK", tabOK); }
984 
989  void setPolyTan(int polytan) { setI("PolyTan", polytan); }
990 
995  void setWarnings(std::string const &warnings) { setC("Warnings", warnings); }
996 
1000  void showFits() const {
1001  astShowFits(getRawPtr());
1002  assertOK();
1003  }
1004 
1020  FitsKeyState testFits(std::string const &name = "") const;
1021 
1025  void writeFits() {
1026  astWriteFits(getRawPtr());
1027  assertOK();
1028  }
1029 
1031  void clearCard() { clear("Card"); }
1032 
1036  void setCard(int ind) { setI("Card", ind); }
1037 
1039  auto *rawKeyMap = reinterpret_cast<AstObject *>(astGetTables(getRawPtr()));
1040  assertOK(rawKeyMap);
1041  if (!rawKeyMap) {
1042  throw std::runtime_error("getTables failed (returned a null keymap)");
1043  }
1044  return Object::fromAstObject<KeyMap>(rawKeyMap, true);
1045  }
1046 
1047 
1051  explicit FitsChan(AstFitsChan *rawFitsChan) : Channel(reinterpret_cast<AstChannel *>(rawFitsChan)) {
1052  if (!astIsAFitsChan(getRawPtr())) {
1054  os << "this is a " << getClassName() << ", which is not a FitsChan";
1055  throw std::invalid_argument(os.str());
1056  }
1057  assertOK();
1058  }
1059 };
1060 
1061 } // namespace ast
1062 
1063 #endif
table::Key< std::string > name
Definition: Amplifier.cc:116
std::ostream * os
Definition: Schema.cc:557
T c_str(T... args)
Channel provides input/output of AST objects.
Definition: Channel.h:60
KeyMap warnings() const
Return a KeyMap holding the text of any warnings issued as a result of the previous invocation of rea...
Definition: Channel.cc:67
A specialized form of Channel which reads and writes FITS header cards.
Definition: FitsChan.h:202
bool getDefB1950() const
Get DefB1950: use FK4 B1950 as default equatorial coordinates?
Definition: FitsChan.h:522
int getNKey() const
Get Nkey: the number of unique keywords.
Definition: FitsChan.h:560
bool getCDMatrix() const
Get CDMatrix: use CDi_j keywords to represent pixel scaling, rotation, etc?
Definition: FitsChan.h:512
std::string getCardComm() const
Get CardComm: the comment of the current card.
Definition: FitsChan.h:491
FitsChan(FitsChan const &)=delete
FitsChan & operator=(FitsChan &&)=default
void setFitsF(std::string const &name, double value, std::string const &comment="", bool overwrite=false)
Create a new card of type double, possibly overwriting the current card.
Definition: FitsChan.h:786
void setIwc(bool iwcs)
Set Iwc: add a Frame describing Intermediate World Coords?
Definition: FitsChan.h:967
std::string getWarnings() const
Get Warnings attribute, which controls the issuing of warnings about selected conditions when an Obje...
Definition: FitsChan.h:588
void setFitsDigits(int digits)
Set FitsDigits: digits of precision for floating-point FITS values.
Definition: FitsChan.h:957
FitsChan(FitsChan &&)=default
FitsKeyState testFits(std::string const &name="") const
Determine if a card is present, and if so, whether it has a value.
Definition: FitsChan.cc:132
std::vector< std::string > getAllCardNames()
Get the name of all cards, in order, starting from the first card.
Definition: FitsChan.cc:106
int getPolyTan() const
Get PolyTan: use PVi_m keywords to define distorted TAN projection?
Definition: FitsChan.h:582
FoundValue< std::string > findFits(std::string const &name, bool inc)
Search for a card in a FitsChan by keyword.
Definition: FitsChan.cc:124
FoundValue< std::string > getFitsS(std::string const &name="", std::string defval="") const
Get the value of a string card.
Definition: FitsChan.cc:98
void retainFits()
Keep the current card when an Object is read that uses the card.
Definition: FitsChan.h:674
std::string getEncoding() const
Get Encoding: the encoding system to use when AST Objects are stored as FITS header cards in a FitsCh...
Definition: FitsChan.h:528
void setFitsS(std::string const &name, std::string value, std::string const &comment="", bool overwrite=false)
Create a new card of type string, possibly overwriting the current card.
Definition: FitsChan.h:884
void setFitsI(std::string const &name, int value, std::string const &comment="", bool overwrite=false)
Create a new card of type int, possibly overwriting the current card.
Definition: FitsChan.h:816
virtual ~FitsChan()
Definition: FitsChan.cc:51
void setCard(int ind)
Set Card: the index of the current card, where 1 is the first card.
Definition: FitsChan.h:1036
void putFits(std::string const &card, bool overwrite)
Store a FITS header card in a FitsChan.
Definition: FitsChan.h:644
std::shared_ptr< KeyMap > getTables() const
Definition: FitsChan.h:1038
FoundValue< bool > getFitsL(std::string const &name="", bool defval=false) const
Get the value of a bool card.
Definition: FitsChan.cc:91
void purgeWcs()
Delete all cards in a FitsChan that relate to any of the recognised WCS encodings.
Definition: FitsChan.h:595
std::string getFitsAxisOrder() const
Get FitsAxisOrder: the order for the WCS axes in any new FITS-WCS headers created using Channel::writ...
Definition: FitsChan.h:534
int getCard() const
Get Card: the index of the current card, where 1 is the first card.
Definition: FitsChan.h:486
void showFits() const
Write all the cards in the channel to standard output.
Definition: FitsChan.h:1000
void clearCard()
Rewind the card index to the beginning.
Definition: FitsChan.h:1031
void setCarLin(bool cdMatrix)
Set CarLin: ignore spherical rotations on CAR projections?
Definition: FitsChan.h:923
FitsChan & operator=(FitsChan const &)=delete
void setFitsAxisOrder(std::string const &order)
Set FitsAxisOrder: the order for the WCS axes in any new FITS-WCS headers created using Channel::writ...
Definition: FitsChan.h:951
std::string getAllWarnings() const
Get AllWarnings: a space separated list of all the conditions names recognized by the Warnings attrib...
Definition: FitsChan.h:481
void setClean(bool clean)
Set Clean: remove cards used whilst reading even if an error occurs?
Definition: FitsChan.h:934
bool getSipOK() const
Get SipOK: use Spitzer Space Telescope keywords to define distortion?
Definition: FitsChan.h:565
bool getSipReplace() const
Get SipReplace: ignore inverse SIP coefficients (replacing them with fit coefficients or an iterative...
Definition: FitsChan.h:571
std::string getCardName() const
Get CardName: the keyword name of the current card.
Definition: FitsChan.h:496
void readFits()
Read cards from the source and store them in the FitsChan.
Definition: FitsChan.h:661
void putCards(std::string const &cards)
Replace all FITS header cards.
Definition: FitsChan.h:616
FitsChan(Stream &stream, std::string const &options="")
Construct a channel that uses a provided Stream.
Definition: FitsChan.cc:44
void setFitsCN(std::string const &name, std::string value, std::string const &comment="", bool overwrite=false)
Create a new "CONTINUE" card, possibly overwriting the current card.
Definition: FitsChan.h:756
void setPolyTan(int polytan)
Set PolyTan: use PVi_m keywords to define distorted TAN projection?
Definition: FitsChan.h:989
FoundValue< int > getFitsI(std::string const &name="", int defval=0) const
Get the value of a int card.
Definition: FitsChan.cc:84
void setFitsCM(std::string const &comment, bool overwrite=false)
Create a new comment card, possibly overwriting the current card.
Definition: FitsChan.h:725
CardType getCardType() const
Get CardType: data type of the current FITS card.
Definition: FitsChan.h:501
bool getCarLin() const
Get CarLin: ignore spherical rotations on CAR projections?
Definition: FitsChan.h:506
int getTabOK() const
Get TabOK: should the FITS "-TAB" algorithm be recognised?
Definition: FitsChan.h:576
void setFitsTol(double tol)
Set FitsTol: Tolerance used for writing a FrameSet using a foreign encoding.
Definition: FitsChan.h:962
bool getIwc() const
Get Iwc: add a Frame describing Intermediate World Coords?
Definition: FitsChan.h:550
FitsChan(AstFitsChan *rawFitsChan)
Construct a FitsChan from a raw AstFitsChan.
Definition: FitsChan.h:1051
void setWarnings(std::string const &warnings)
Set Warnings attribute, which controls the issuing of warnings about selected conditions when an Obje...
Definition: FitsChan.h:995
void writeFits()
Write out all cards currently in the channel and clear the channel.
Definition: FitsChan.h:1025
int getNCard() const
Get NCard: the number of cards.
Definition: FitsChan.h:555
int getFitsDigits() const
Get FitsDigits: digits of precision for floating-point FITS values.
Definition: FitsChan.h:540
FoundValue< std::complex< double > > getFitsCF(std::string const &name="", std::complex< double > defval={0, 0}) const
Get the value of a complex double card.
Definition: FitsChan.cc:59
void setTabOK(int tabOK)
Set TabOK: should the FITS "-TAB" algorithm be recognised?
Definition: FitsChan.h:983
FoundValue< double > getFitsF(std::string const &name="", double defval=0) const
Get the value of a double card.
Definition: FitsChan.cc:77
void setSipReplace(bool replace)
Set SipReplace: ignore inverse SIP coefficients (replacing them with fit coefficients or an iterative...
Definition: FitsChan.h:978
void setFitsU(std::string const &name, std::string const &comment="", bool overwrite=false)
Create a new card with an undefined value, possibly overwriting the current card.
Definition: FitsChan.h:915
bool getClean() const
Get Clean: remove cards used whilst reading even if an error occurs?
Definition: FitsChan.h:517
void emptyFits()
Delete all cards and associated information from a FitsChan.
Definition: FitsChan.h:244
void setFitsL(std::string const &name, bool value, std::string const &comment="", bool overwrite=false)
Create a new card of type bool, possibly overwriting the current card.
Definition: FitsChan.h:846
void setSipOK(bool sipOK)
Set SipOK: use Spitzer Space Telescope keywords to define distortion?
Definition: FitsChan.h:972
FoundValue< std::string > getFitsCN(std::string const &name="", std::string defval="") const
Get the value of a CONTINUE card.
Definition: FitsChan.cc:69
void setEncoding(std::string const &encoding)
Set Encoding: the encoding system to use when AST Objects are stored as FITS header cards in a FitsCh...
Definition: FitsChan.h:945
void setCDMatrix(bool cdMatrix)
Get CDMatrix: Use CDi_j keywords to represent pixel scaling, rotation, etc?
Definition: FitsChan.h:929
void delFits()
Delete the current FITS card.
Definition: FitsChan.h:230
double getFitsTol() const
Get FitsTol: Tolerance used for writing a FrameSet using a foreign encoding.
Definition: FitsChan.h:545
void setFitsCF(std::string const &name, std::complex< double > value, std::string const &comment="", bool overwrite=false)
Create a new card of type std::complex<double>, possibly overwriting the current card.
Definition: FitsChan.h:696
void setDefB1950(bool defB1950)
Set DefB1950: use FK4 B1950 as default equatorial coordinates?
Definition: FitsChan.h:939
A value and associated validity flag.
Definition: FitsChan.h:69
FoundValue()
Default constructor: found false, value is default-constructed.
Definition: FitsChan.h:80
T value
The found value; ignore if found is false.
Definition: FitsChan.h:82
FoundValue(bool found, T const &value)
Construct a FoundValue.
Definition: FitsChan.h:77
bool found
Was the value found?
Definition: FitsChan.h:81
void setD(std::string const &attrib, double value)
Set the value of an attribute as a double.
Definition: Object.h:476
double getD(std::string const &attrib) const
Get the value of an attribute as a double.
Definition: Object.h:374
std::string const getC(std::string const &attrib) const
Get the value of an attribute as a string.
Definition: Object.h:361
std::string getClassName() const
Get Class: the name of the class (e.g.
Definition: Object.h:139
void setI(std::string const &attrib, int value)
Set the value of an attribute as an int.
Definition: Object.h:500
bool getB(std::string const &attrib) const
Get the value of an attribute as a bool.
Definition: Object.h:348
AstObject const * getRawPtr() const
Get the raw AST pointer.
Definition: Object.h:292
void clear(std::string const &attrib)
Clear the values of a specified set of attributes for an Object.
Definition: Object.h:119
int getI(std::string const &attrib) const
Get the value of an attribute as an int.
Definition: Object.h:400
void setC(std::string const &attrib, std::string const &value)
Set the value of an attribute as a string.
Definition: Object.h:464
void setB(std::string const &attrib, bool value)
Set the value of an attribute as a bool.
Definition: Object.h:452
A stream for ast::Channel.
Definition: Stream.h:41
AST wrapper classes and functions.
CardType
Enums describing the FITS card type.
Definition: FitsChan.h:49
@ NOTYPE
card does not exist (card number invalid)
@ CONTINUE
CONTINUE card.
@ LOGICAL
boolean
@ COMPLEXI
complex integer
@ INT
integer
@ COMPLEXF
complex floating point
@ STRING
string
@ UNDEF
card has no value
@ COMMENT
card is a comment-style card with no "=" (COMMENT, HISTORY, ...)
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Throw std::runtime_error if AST's state is bad.
Definition: base.cc:49
FitsKeyState
Enums describing the presence or absence of a FITS keyword.
Definition: FitsChan.h:40
@ NOVALUE
keyword is present, but has no value
@ PRESENT
keyword is present and has a value
@ ABSENT
keyword is not present
table::Key< int > order