GFitsHeaderCard.cpp
39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
/***************************************************************************
* GFitsHeaderCard.cpp - FITS header card class *
* ----------------------------------------------------------------------- *
* copyright (C) 2008-2018 by Juergen Knoedlseder *
* ----------------------------------------------------------------------- *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
/**
* @file GFitsHeaderCard.cpp
* @brief FITS header card class implementation
* @author Juergen Knoedlseder
*/
/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cfloat>
#include <climits>
#include "GException.hpp"
#include "GTools.hpp"
#include "GFitsCfitsio.hpp"
#include "GFits.hpp"
#include "GFitsHeaderCard.hpp"
/* __ Method name definitions ____________________________________________ */
#define G_COPY_DTYPE "GFitsHeaderCard::copy_dtype(GFitsHeaderCard&)"
#define G_FREE_DTYPE "GFitsHeaderCard::free_dtype()"
#define G_SET_DTYPE "GFitsHeaderCard::set_dtype(std::string&)"
#define G_READ_NUM "GFitsHeaderCard::read(void*, int&)"
#define G_READ_STR "GFitsHeaderCard::read(void*, std::string&)"
#define G_WRITE "GFitsHeaderCard::write(void*)"
/* __ Definitions ________________________________________________________ */
/* __ Macros _____________________________________________________________ */
/* __ Coding definitions _________________________________________________ */
/* __ Enumerations _______________________________________________________ */
/* __ Debug definitions __________________________________________________ */
/*==========================================================================
= =
= Constructors/destructors =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Void constructor
***************************************************************************/
GFitsHeaderCard::GFitsHeaderCard(void)
{
// Initialise class members
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Card constructor
*
* @param[in] keyname Card name.
* @param[in] value Card value.
* @param[in] unit Card unit.
* @param[in] comment Card comment.
*
* Constructs a header card from @p keyname, @p value, @p unit and
* @p comment string. The card type is determined from the card value format.
***************************************************************************/
GFitsHeaderCard::GFitsHeaderCard(const std::string& keyname,
const std::string& value,
const std::string& unit,
const std::string& comment)
{
// Initialise class members
init_members();
// Set members
set_members(keyname, value, unit, comment);
// Return
return;
}
/***********************************************************************//**
* @brief Constructor for string cards
*
* @param[in] keyname Card name.
* @param[in] value Card string value.
* @param[in] comment Card comment.
*
* This constructor builds a header card from the keyname, value and comment.
***************************************************************************/
GFitsHeaderCard::GFitsHeaderCard(const std::string& keyname,
const std::string& value,
const std::string& comment)
{
// Initialise class members
init_members();
// Set members
this->keyname(keyname);
this->value("'"+value+"'");
this->comment(comment);
// Return
return;
}
/***********************************************************************//**
* @brief Constructor for floating point cards
*
* @param[in] keyname Card name.
* @param[in] value Card floating point value.
* @param[in] comment Card comment.
*
* This constructor builds a header card from the keyname, value and comment.
***************************************************************************/
GFitsHeaderCard::GFitsHeaderCard(const std::string& keyname,
const double& value,
const std::string& comment)
{
// Initialise class members
init_members();
// Set members
this->keyname(keyname);
this->value(value);
this->comment(comment);
// Return
return;
}
/***********************************************************************//**
* @brief Constructor for boolean cards
*
* @param[in] keyname Card name.
* @param[in] value Card integer value.
* @param[in] comment Card comment.
*
* This constructor builds a header card from the keyname, value and comment.
***************************************************************************/
GFitsHeaderCard::GFitsHeaderCard(const std::string& keyname,
const int& value,
const std::string& comment)
{
// Initialise class members
init_members();
// Set members
this->keyname(keyname);
this->value(value);
this->comment(comment);
// Return
return;
}
/***********************************************************************//**
* @brief Constructor for integer cards
*
* @param[in] keyname Card name.
* @param[in] value Card boolean value.
* @param[in] comment Card comment.
*
* This constructor builds a header card from the keyname, value and comment.
***************************************************************************/
GFitsHeaderCard::GFitsHeaderCard(const std::string& keyname,
const bool& value,
const std::string& comment)
{
// Initialise class members
init_members();
// Set members
this->keyname(keyname);
this->value(value);
this->comment(comment);
// Return
return;
}
/***********************************************************************//**
* @brief Constructor for character string cards
*
* @param[in] keyname Card name.
* @param[in] value Card character string value.
* @param[in] comment Card comment.
*
* This constructor builds a header card from the keyname, value and comment.
***************************************************************************/
GFitsHeaderCard::GFitsHeaderCard(const std::string& keyname,
const char* value,
const std::string& comment)
{
// Initialise class members
init_members();
// Set members
this->keyname(keyname);
this->value(std::string(value));
this->comment(comment);
// Return
return;
}
/***********************************************************************//**
* @brief Copy constructor
*
* @param[in] card Header card.
***************************************************************************/
GFitsHeaderCard::GFitsHeaderCard(const GFitsHeaderCard& card)
{
// Initialise class members for clean destruction
init_members();
// Copy members
copy_members(card);
// Return
return;
}
/***********************************************************************//**
* @brief Destructor
***************************************************************************/
GFitsHeaderCard::~GFitsHeaderCard(void)
{
// Free members
free_members();
// Return
return;
}
/*==========================================================================
= =
= Operators =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Assignment operator
*
* @param[in] card Header card.
* @return Header card.
***************************************************************************/
GFitsHeaderCard& GFitsHeaderCard::operator=(const GFitsHeaderCard& card)
{
// Execute only if object is not identical
if (this != &card) {
// Free members
free_members();
// Initialise private members for clean destruction
init_members();
// Copy members
copy_members(card);
} // endif: object was not identical
// Return this object
return *this;
}
/*==========================================================================
= =
= Public methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Clear header card
***************************************************************************/
void GFitsHeaderCard::clear(void)
{
// Free members
free_members();
// Initialise members
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Clone header card
*
* @return Pointer to deep copy of header card.
***************************************************************************/
GFitsHeaderCard* GFitsHeaderCard::clone(void) const
{
return new GFitsHeaderCard(*this);
}
/***********************************************************************//**
* @brief Set name of header card
*
* @param[in] keyname Card name.
***************************************************************************/
void GFitsHeaderCard::keyname(const std::string& keyname)
{
// Set name of card
m_keyname = keyname;
// Return
return;
}
/***********************************************************************//**
* @brief Set string value of header card
*
* @param[in] value Card string value.
*
* This method sets the value of a string header card. The internal string
* representation contains hyphens, yet for keyword writing the hyphens are
* stripped.
***************************************************************************/
void GFitsHeaderCard::value(const std::string& value)
{
// Free data type
free_dtype();
// Set value
m_value = value;
// Attach hyphens to internal string representation if required
if (m_value[0] != '\x27') {
m_value = "'" + m_value;
}
if (m_value[m_value.length()-1] != '\x27') {
m_value = m_value + "'";
}
// Strip hyphens and whitespace from datatype value that is used for
// keyword writing
std::string value_dtype =
gammalib::strip_whitespace(m_value.substr(1, m_value.length() - 2));
// Set data type
m_dtype = __TSTRING;
m_value_dtype = new std::string(value_dtype);
// Return
return;
}
/***********************************************************************//**
* @brief Set boolean value of header card
*
* @param[in] value Card boolean value.
***************************************************************************/
void GFitsHeaderCard::value(const bool& value)
{
// Free data type
free_dtype();
// Set value and data type
m_value = (value) ? "T" : "F";
m_dtype = __TLOGICAL;
m_value_dtype = new bool(value);
// Return
return;
}
/***********************************************************************//**
* @brief Set single precision value of header card
*
* @param[in] value Card single precision value.
***************************************************************************/
void GFitsHeaderCard::value(const float& value)
{
// Free data type
free_dtype();
// Set value and data type
m_value = gammalib::str(value);
m_dtype = __TFLOAT;
m_value_dtype = new float(value);
// Return
return;
}
/***********************************************************************//**
* @brief Set double precision value of header card
*
* @param[in] value Card double precision value.
***************************************************************************/
void GFitsHeaderCard::value(const double& value)
{
// Free data type
free_dtype();
// Set value and data type
m_value = gammalib::str(value);
m_dtype = __TDOUBLE;
m_value_dtype = new double(value);
// Return
return;
}
/***********************************************************************//**
* @brief Set unsigned short integer value of header card
*
* @param[in] value Card unsigned short integer value.
***************************************************************************/
void GFitsHeaderCard::value(const unsigned short& value)
{
// Free data type
free_dtype();
// Set value and data type
m_value = gammalib::str(value);
m_dtype = __TUSHORT;
m_value_dtype = new unsigned short(value);
// Return
return;
}
/***********************************************************************//**
* @brief Set short integer value of header card
*
* @param[in] value Card short integer value.
***************************************************************************/
void GFitsHeaderCard::value(const short& value)
{
// Free data type
free_dtype();
// Set value and data type
m_value = gammalib::str(value);
m_dtype = __TSHORT;
m_value_dtype = new short(value);
// Return
return;
}
/***********************************************************************//**
* @brief Set unsigned integer value of header card
*
* @param[in] value Card unsigned integer value.
***************************************************************************/
void GFitsHeaderCard::value(const unsigned int& value)
{
// Free data type
free_dtype();
// Set value and data type
m_value = gammalib::str(value);
m_dtype = __TUINT;
m_value_dtype = new unsigned int(value);
// Return
return;
}
/***********************************************************************//**
* @brief Set integer value of header card
*
* @param[in] value Card integer value.
***************************************************************************/
void GFitsHeaderCard::value(const int& value)
{
// Free data type
free_dtype();
// Set value and data type
m_value = gammalib::str(value);
m_dtype = __TINT;
m_value_dtype = new int(value);
// Return
return;
}
/***********************************************************************//**
* @brief Set long integer value of header card
*
* @param[in] value Card long integer value.
***************************************************************************/
void GFitsHeaderCard::value(const long& value)
{
// Free data type
free_dtype();
// Set value and data type
m_value = gammalib::str(value);
m_dtype = __TLONG;
m_value_dtype = new long(value);
// Return
return;
}
/***********************************************************************//**
* @brief Set unsigned integer long value of header card
*
* @param[in] value Card unsigned long integer value.
***************************************************************************/
void GFitsHeaderCard::value(const unsigned long& value)
{
// Free data type
free_dtype();
// Set value and data type
m_value = gammalib::str(value);
m_dtype = __TULONG;
m_value_dtype = new unsigned long(value);
// Return
return;
}
/***********************************************************************//**
* @brief Set long long integer value of header card
*
* @param[in] value Card long long integer value.
***************************************************************************/
void GFitsHeaderCard::value(const long long& value)
{
// Free data type
free_dtype();
// Set value and data type
m_value = gammalib::str(value);
m_dtype = __TLONGLONG;
m_value_dtype = new long long(value);
// Return
return;
}
/***********************************************************************//**
* @brief Return header card value as string
*
* @return Header card value as string
*
* Returns header card value as string. Any hyphens that may occur in the
* FITS card will be stripped.
***************************************************************************/
std::string GFitsHeaderCard::string(void) const
{
// Initialize return value to actual value string
std::string result = m_value;
// Type dependent conversion
if (m_value_dtype != NULL) {
switch (m_dtype) {
case __TSTRING:
{
int n = m_value.length();
if (n > 2) {
result = gammalib::strip_whitespace(m_value.substr(1, n-2));
}
else {
result = "";
}
}
break;
default:
break;
}
}
// Return string
return result;
}
/***********************************************************************//**
* @brief Return header card value as double precision
*
* Convert header card value into a double precision value. In case that the
* card did not contain a numerical value, 0 will be returned by the
* method.
***************************************************************************/
double GFitsHeaderCard::real(void) const
{
// Initialize return value to 0.0
double result = 0.0;
// Type dependent conversion
if (m_value_dtype != NULL) {
switch (m_dtype) {
case __TSTRING:
if (m_value.length() > 2) {
result = gammalib::todouble(m_value.substr(1, m_value.length() - 2));
}
break;
case __TLOGICAL:
result = (m_value == "T") ? 1.0 : 0.0;
break;
default:
result = gammalib::todouble(m_value);
break;
}
}
// Return string
return result;
}
/***********************************************************************//**
* @brief Return header card value as integer
*
* Convert header card value into a integer value. In case that the
* card did not contain a numerical value, 0 will be returned by the
* method.
***************************************************************************/
int GFitsHeaderCard::integer(void) const
{
// Initialize return value to 0
int result = 0;
// Type dependent conversion
if (m_value_dtype != NULL) {
switch (m_dtype) {
case __TSTRING:
if (m_value.length() > 2) {
result = gammalib::toint(m_value.substr(1, m_value.length() - 2));
}
break;
case __TLOGICAL:
result = (m_value == "T") ? 1 : 0;
break;
default:
result = gammalib::toint(m_value);
break;
}
}
// Return string
return result;
}
/***********************************************************************//**
* @brief Print header card information
*
* @param[in] chatter Chattiness (defaults to NORMAL).
* @return String containing header card information.
***************************************************************************/
std::string GFitsHeaderCard::print(const GChatter& chatter) const
{
// Initialise result string
std::string result;
// Continue only if chatter is not silent
if (chatter != SILENT) {
// Append keyname
result.append(gammalib::left(m_keyname,8));
// Format values
if (m_keyname != "COMMENT" && m_keyname != "HISTORY" && m_keyname != "") {
if (m_unit.length() > 0) {
result.append(" ="+gammalib::right(m_value,21)+" / ["+m_unit+"] "+m_comment);
}
else {
result.append(" ="+gammalib::right(m_value,21)+" / "+m_comment);
}
}
else {
result.append(" "+m_comment);
}
// Attach card type
if (m_value_dtype != NULL) {
switch (m_dtype) {
case __TNULL:
result.append(" <null>");
break;
case __TBIT:
result.append(" <bit>");
break;
case __TBYTE:
result.append(" <byte>");
break;
case __TSBYTE:
result.append(" <signed byte>");
break;
case __TLOGICAL:
result.append(" <bool>");
break;
case __TSTRING:
result.append(" <string>");
break;
case __TUSHORT:
result.append(" <unsigned short>");
break;
case __TSHORT:
result.append(" <short>");
break;
case __TUINT:
result.append(" <unsigned int>");
break;
case __TINT:
result.append(" <int>");
break;
case __TULONG:
result.append(" <unsigned long>");
break;
case __TLONG:
result.append(" <long>");
break;
case __TLONGLONG:
result.append(" <long long>");
break;
case __TFLOAT:
result.append(" <float>");
break;
case __TDOUBLE:
result.append(" <double>");
break;
case __TCOMPLEX:
result.append(" <complex>");
break;
case __TDBLCOMPLEX:
result.append(" <double complex>");
break;
default:
result.append(" <unsupported value "+gammalib::str(m_dtype)+">");
break;
}
}
/*
else {
switch (m_dtype) {
case __TNULL:
result.append(" <null>");
break;
default:
result.append(" <non native>");
break;
}
}
*/
} // endif: chatter was not silent
// Return result
return result;
}
/*==========================================================================
= =
= Private methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Initialise class members
***************************************************************************/
void GFitsHeaderCard::init_members(void)
{
// Initialise members
m_keyname.clear();
m_value.clear();
m_unit.clear();
m_comment.clear();
m_value_dtype = NULL;
m_dtype = __TNULL;
m_value_decimals = 10;
m_comment_write = true; // Was false before, not sure why ...
// Return
return;
}
/***********************************************************************//**
* @brief Copy class members
*
* @param[in] card Header card to be copied
***************************************************************************/
void GFitsHeaderCard::copy_members(const GFitsHeaderCard& card)
{
// Copy members
m_keyname = card.m_keyname;
m_value = card.m_value;
m_value_decimals = card.m_value_decimals;
m_unit = card.m_unit;
m_comment = card.m_comment;
m_comment_write = card.m_comment_write;
// Copy native data types
copy_dtype(card);
// Return
return;
}
/***********************************************************************//**
* @brief Delete class members
***************************************************************************/
void GFitsHeaderCard::free_members(void)
{
// Free members
free_dtype();
// Return
return;
}
/***********************************************************************//**
* @brief Set card members
*
* @param[in] keyname Card name.
* @param[in] value Card value.
* @param[in] unit Card unit.
* @param[in] comment Card comment.
*
* Sets the card members from @p keyname, @p value, @p unit, and @p comment
* strings. The card type is determined from the format of the card value.
***************************************************************************/
void GFitsHeaderCard::set_members(const std::string& keyname,
const std::string& value,
const std::string& unit,
const std::string& comment) {
// Store attributes
m_keyname = keyname;
m_value = value;
m_comment = comment;
// Determine card type
set_dtype(m_value);
// Return
return;
}
/***********************************************************************//**
* @brief Copy dtype
*
* @param[in] card Header card.
*
* Copies the data type of a header card.
***************************************************************************/
void GFitsHeaderCard::copy_dtype(const GFitsHeaderCard& card)
{
// Copy data type
if (card.m_value_dtype != NULL) {
m_dtype = card.m_dtype;
switch (m_dtype) {
case __TLOGICAL:
m_value_dtype = new bool(*((bool*)card.m_value_dtype));
break;
case __TSTRING:
m_value_dtype = new std::string(*((std::string*)card.m_value_dtype));
break;
case __TUSHORT:
m_value_dtype = new unsigned short(*((unsigned short*)card.m_value_dtype));
break;
case __TSHORT:
m_value_dtype = new short(*((short*)card.m_value_dtype));
break;
case __TUINT:
m_value_dtype = new unsigned int(*((unsigned int*)card.m_value_dtype));
break;
case __TINT:
m_value_dtype = new int(*((int*)card.m_value_dtype));
break;
case __TULONG:
m_value_dtype = new unsigned long(*((unsigned long*)card.m_value_dtype));
break;
case __TLONG:
m_value_dtype = new long(*((long*)card.m_value_dtype));
break;
case __TLONGLONG:
m_value_dtype = new long long(*((long long*)card.m_value_dtype));
break;
case __TFLOAT:
m_value_dtype = new float(*((float*)card.m_value_dtype));
break;
case __TDOUBLE:
m_value_dtype = new double(*((double*)card.m_value_dtype));
break;
default:
std::string msg = "Invalid data type code "+
gammalib::str(m_dtype)+" encountered.";
gammalib::warning(G_COPY_DTYPE, msg);
break;
}
}
// Return
return;
}
/***********************************************************************//**
* @brief Free dtype
***************************************************************************/
void GFitsHeaderCard::free_dtype(void)
{
// Free data type
if (m_value_dtype != NULL) {
switch (m_dtype) {
case __TLOGICAL:
delete (bool*)m_value_dtype;
break;
case __TSTRING:
delete (std::string*)m_value_dtype;
break;
case __TUSHORT:
delete (unsigned short*)m_value_dtype;
break;
case __TSHORT:
delete (short*)m_value_dtype;
break;
case __TUINT:
delete (unsigned int*)m_value_dtype;
break;
case __TINT:
delete (int*)m_value_dtype;
break;
case __TULONG:
delete (unsigned long*)m_value_dtype;
break;
case __TLONG:
delete (long*)m_value_dtype;
break;
case __TLONGLONG:
delete (long long*)m_value_dtype;
break;
case __TFLOAT:
delete (float*)m_value_dtype;
break;
case __TDOUBLE:
delete (double*)m_value_dtype;
break;
default:
std::string msg = "Invalid data type code "+
gammalib::str(m_dtype)+" encountered.";
gammalib::warning(G_FREE_DTYPE, msg);
break;
}
}
// Return
return;
}
/***********************************************************************//**
* @brief Set native data type from card string
*
* @param[in] value Card string.
*
* The native data type is determined from the card string. This method sets
* the members m_dtype and m_value_type. The card type is determined by
* analyzing the card value.
*
* @todo Implement syntax checking for integer or float values.
***************************************************************************/
void GFitsHeaderCard::set_dtype(const std::string& value)
{
// Free data type
free_dtype();
// Main loop to allow fall through
do {
// Get index of last string element
int last = m_value.length() - 1;
// If value is empty and there is a COMMENT, HISTORY or BLANKFIELD
// (empty) keyword then we have a commentary card. We can just
// skip this card here. Otherwise we have an undefined value, also
// called NULL value.
if (last < 0) {
if (m_keyname == "COMMENT" ||
m_keyname == "HISTORY" ||
m_keyname == "") {
continue;
}
else {
m_dtype = __TNULL;
continue;
}
}
// If value is enclosed in parentheses then we have a string
if (m_value[0] == '\x27' && m_value[last] == '\x27') {
this->value(value);
continue;
}
// If value has only one digit and is either 'F' or 'T' we have a
// Boolean
if (last == 0 && (m_value[0] == 'F' || m_value[0] == 'T')) {
this->value((m_value[0] == 'T'));
continue;
}
// Conversion
double value_dbl = gammalib::todouble(m_value);
long long value_ll = gammalib::tolonglong(m_value);
unsigned long long value_ull = gammalib::toulonglong(m_value);
// Check if we have an integer
if ((value_dbl >= 0 && value_dbl == value_ull) ||
(value_dbl < 0 && value_dbl == value_ll)) {
// Consider positive integers as unsigned
if (value_dbl >= 0) {
if (value_ull > ULONG_MAX) {
m_dtype = __TLONGLONG;
m_value_dtype = new long long(value_ll);
}
else if (value_ull > USHRT_MAX) {
m_dtype = __TULONG;
m_value_dtype = new unsigned long(value_ull);
}
else {
m_dtype = __TUSHORT;
m_value_dtype = new unsigned short(value_ull);
}
}
// ... otherwise consider signed
else {
if (value_ll > LONG_MAX || value_ll < LONG_MIN) {
m_dtype = __TLONGLONG;
m_value_dtype = new long long(value_ll);
}
else if (value_ll > SHRT_MAX || value_ll < SHRT_MIN) {
m_dtype = __TLONG;
m_value_dtype = new long(value_ll);
}
else {
m_dtype = __TSHORT;
m_value_dtype = new short(value_ll);
}
}
} // endif: handled integers
// ... otherwise handle floats
else {
if (value_dbl > FLT_MAX || value_dbl < FLT_MIN) {
m_dtype = __TDOUBLE;
m_value_dtype = new double(value_dbl);
}
else {
m_dtype = __TFLOAT;
m_value_dtype = new float(value_dbl);
}
}
} while(0);
// Return
return;
}
/***********************************************************************//**
* @brief Read header card from FITS file
*
* @param[in] vptr FITS file void pointer.
* @param[in] keynum Number of the header card.
***************************************************************************/
void GFitsHeaderCard::read(void* vptr, const int& keynum)
{
// Initialise status
int status = 0;
// Move to HDU
gammalib::fits_move_to_hdu(G_READ_NUM, vptr);
// Read keyword
char keyname[80];
char value[80];
char comment[80];
status = __ffgkyn(FPTR(vptr), keynum, keyname, value, comment, &status);
if (status != 0) {
throw GException::fits_error(G_READ_NUM, status);
}
// Store result
m_keyname.assign(keyname);
m_value.assign(value);
m_comment.assign(comment);
// Determine card type
set_dtype(m_value);
// Return
return;
}
/***********************************************************************//**
* @brief Read header card from FITS file
*
* @param[in] vptr FITS file void pointer.
* @param[in] keyname Name of the header card.
*
* @exception GException::invalid_value
* Specified @p keyname not found in FITS header.
* @exception GException::fits_error
* cfitsio error occured.
***************************************************************************/
void GFitsHeaderCard::read(void* vptr, const std::string& keyname)
{
// Initialise FITS status
int status = 0;
// Move to HDU
gammalib::fits_move_to_hdu(G_READ_STR, vptr);
// Read keyword
char value[80];
char comment[80];
status = __ffgkey(FPTR(vptr), (char*)keyname.c_str(), value, comment,
&status);
// Catch error
if (status == 202) { // Keyword not found
std::string msg = "Header card with keyword \""+keyname+"\" not"
" found in FITS header (status=202).";
throw GException::invalid_value(G_READ_STR, msg);
}
else if (status != 0) { // Any other error
std::string msg = "Unable to read keyword \""+keyname+"\" from"
" FITS extension "+
gammalib::str((FPTR(vptr)->HDUposition))+
" header.";
throw GException::fits_error(G_READ_STR, status, msg);
}
// Store result
m_keyname = keyname;
m_value.assign(value);
m_comment.assign(comment);
// Determine card type
set_dtype(m_value);
// Return
return;
}
/***********************************************************************//**
* @brief Write header card
*
* @param[in] vptr FITS file void pointer.
*
* Writes any kind of header card to a FITS file.
***************************************************************************/
void GFitsHeaderCard::write(void* vptr) const
{
// Initialise status
int status = 0;
// Move to HDU
gammalib::fits_move_to_hdu(G_WRITE, vptr);
// If card is comment then write comment
if (m_keyname == "COMMENT") {
if (m_comment_write) {
status = __ffpcom(FPTR(vptr), (char*)m_comment.c_str(), &status);
}
}
// If card is history then write history
else if (m_keyname == "HISTORY") {
if (m_comment_write) {
status = __ffphis(FPTR(vptr), (char*)m_comment.c_str(), &status);
}
}
// If card is BLANKFIELD (empty keyword) then write empty line
else if (m_keyname == "") {
if (m_comment_write) {
status = __ffprec(FPTR(vptr), "", &status);
}
}
// If card holds a native data type then write it
else if (m_value_dtype != NULL) {
switch (m_dtype) {
case __TLOGICAL:
{
int value = (*((bool*)m_value_dtype)) ? 1 : 0;
status = __ffukyl(FPTR(vptr), (char*)m_keyname.c_str(), value,
(char*)m_comment.c_str(), &status);
}
break;
case __TSTRING:
status = __ffukys(FPTR(vptr), (char*)m_keyname.c_str(),
(char*)((std::string*)m_value_dtype)->c_str(),
(char*)m_comment.c_str(), &status);
break;
case __TFLOAT:
status = __ffukye(FPTR(vptr), (char*)m_keyname.c_str(),
*((float*)m_value_dtype), decimals(),
(char*)m_comment.c_str(), &status);
break;
case __TDOUBLE:
status = __ffukyd(FPTR(vptr), (char*)m_keyname.c_str(),
*((double*)m_value_dtype), decimals(),
(char*)m_comment.c_str(), &status);
break;
default:
status = __ffuky(FPTR(vptr), m_dtype, (char*)m_keyname.c_str(),
m_value_dtype, (char*)m_comment.c_str(), &status);
break;
}
} // endif: had native data type
// ... otherwise if card holds a NULL value then write it
else if (m_dtype == __TNULL) {
status = __ffukyu(FPTR(vptr), (char*)m_keyname.c_str(),
(char*)m_comment.c_str(), &status);
}
// ... capture all other stuff
else {
std::string msg = "The code should never arrive at this point!\n"
"The keyname \""+m_keyname+"\" is neither"
" \"COMMENT\" nor \"HISTRORY\" but it has a"
" NULL data type value and a data type of"
" "+gammalib::str(m_dtype)+".\n"
"Maybe an invalid FITS header card has been"
" encountered in reading a file?";
gammalib::warning(G_WRITE, msg);
}
// Throw exception in case of a FITS error
if (status != 0) {
throw GException::fits_error(G_WRITE, status);
}
// Return
return;
}