-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHeader.cpp
More file actions
238 lines (211 loc) · 7.13 KB
/
Header.cpp
File metadata and controls
238 lines (211 loc) · 7.13 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
// Class that mirrors the FITS header data structure.
#include "Header.h"
#include "StringStuff.h"
#include <cstring>
#include <cstdio>
#include <cctype>
#include <ios>
#include <limits>
using namespace img;
using namespace std;
// Case-raising & blank-stripping function for keywords.
string img::KeyFormat(const string input) {
string output;
//strip leading blanks
int i=0;
while (i<input.size() && isspace(input[i])) i++;
//Convert to upper case, stop at whitespace
while (i<input.size() && !isspace(input[i]))
output += toupper(input[i++]);
return output;
}
string
HdrRecordBase::writeCard() const {
string vv=getValueString();
string card= (keyword.size() <=8) ? keyword : "HIERARCH " + keyword;
for (int i=card.size(); i<8; i++) card += " ";
if (!vv.empty()) {
card += "= ";
for (int i=vv.size(); i<20; i++) card += " ";
card += vv;
if (!comment.empty() || !units.empty())
card += " /";
}
card += " ";
if (!units.empty()) card+= "[" + units + "] ";
card += comment;
return card;
}
namespace img {
//specializations for bool
template<>
string
HdrRecord<bool>::getValueString() const {
if (val) return "T";
else return "F";
}
template<>
bool
HdrRecord<bool>::setValueString(const string _v) {
istringstream iss(_v.c_str());
string s;
string leftover;
// Should be only one word, T or F:
if (!(iss >> s) || (iss >> leftover)) return true;
if (s=="T" || s=="t" || s=="true") val=true;
else if (s=="F" || s=="f" || s=="false") val=false;
else return true;
return false;
};
//and for string - enclose in quotes
template<>
string
HdrRecord<string>::getValueString() const {
return "'" + val + "'";
}
//and for double - use capital E, many digits...
template<>
string
HdrRecord<double>::getValueString() const {
ostringstream oss;
oss << right << setw(20) << uppercase
<< showpoint << setprecision(12) << val;
// Above puts lots of trailing zeroes... get rid of them:
string work=oss.str();
size_t eSpot = work.find('E');
int nPad=0; // Number of zeros removed
bool hasExp = (eSpot != string::npos);
// where is last non-zero part of mantissa?
int last = (hasExp ? eSpot : work.size() ) -1;
while (last>0 && work[last]=='0') {
last--;
nPad++;
}
string out;
for (int i=0; i<nPad; i++) out += " ";
out += work.substr(0,last+1);
if (hasExp)
out += work.substr(eSpot);
return out;
}
// ??? Force decimal point on float, double?
///////////////////////////////////////////////
// Headers to/from ASCII streams
///////////////////////////////////////////////
HdrRecordBase*
ReadASCIIHeader(string in) {
const char quote='\''; //will bound a quoted string keyword or value
const string comments="/"; //starts comment (unless quoted)
const char eq='='; //can be present after keyword
string keyword;
string vstring;
string comment;
string::size_type i=0;
string::size_type l=in.length();
// skip opening white space:
while (i<l && std::isspace(in[i])) ++i;
if (i==l) return 0;
//Keyword ends at next white, =, comment, or end:
while (i<l
&& in[i]!=quote
&& in[i]!=eq
&& !std::isspace(in[i])
&& comments.find(in[i])==string::npos ) keyword+=in[i++];
// HIERARCH means an extension whereby keyword keeps going until = sign:
if (keyword=="HIERARCH") {
keyword.clear();
while (i<l && std::isspace(in[i])) ++i;
if (i==l) return 0;
while (i<l && in[i]!=eq) keyword+=in[i++];
}
if (keyword.length()==0) return 0; //no useful info.
// Skip whitespace or equals; done for end or comment
while (i<l && (in[i]==eq || std::isspace(in[i])) ) ++i;
// Null record if we have nothing or comment left:
if (i==l) return new HdrRecordNull(keyword);
if (comments.find(in[i])!=string::npos) return new HdrRecordNull(keyword, in.substr(i+1));
// A keyword named "HISTORY" or "COMMENT" is all comment, really
if (keyword=="COMMENT" || keyword=="HISTORY")
return new HdrRecordNull(keyword, in.substr(i));
// A quoted value is string:
if (in[i]==quote) {
//If value is quoted, get everything until next quote
++i; //skip the quote
while (i<l && in[i]!=quote) vstring+=in[i++];
if (i==l) return 0; // unbounded quote, failure!!!
++i; //skip the closing quote
while (i<l && std::isspace(in[i])) i++; // skip whitespace
if (i==l) return new HdrRecord<string>(keyword, vstring);
else if (comments.find(in[i])!=string::npos) // Comment left?
return new HdrRecord<string>(keyword, vstring, in.substr(i+1));
else return 0; // ??? failure - something other than comment after string
}
if (in[i]=='T' || in[i]=='F') {
// Boolean valued:
bool value= (in[i]=='T');
i++;
while (i<l && isspace(in[i])) i++; // skip whitespace
if (i==l) return new HdrRecord<bool>(keyword, value);
else if (comments.find(in[i])!=string::npos) // Comment left?
return new HdrRecord<bool>(keyword, value, in.substr(i+1));
else return 0; // ??? failure - something other than comment after T/F
}
// Otherwise we are getting either an integer or a float (ignore complex)
while (i<l && comments.find(in[i])==string::npos ) vstring+=in[i++];
// Strip trailing whitespace if was not a quoted response:
string::size_type pos = vstring.size();
while (pos > 0 && isspace(vstring[pos - 1])) pos--;
vstring.erase(pos);
// Collect comment
if (comments.find(in[i])!=string::npos) // Comment left?
comment = in.substr(i+1);
HdrRecord<int>* hi = new HdrRecord<int>(keyword, 0, comment);
if (!hi->setValueString(vstring)) return hi;
// If that failed, try a double
delete hi;
HdrRecord<double>* hd = new HdrRecord<double>(keyword, 0., comment);
if (!hd->setValueString(vstring)) return hd;
delete hd;
// Last try: some DECam headers coming back with NAN.0 listed.
if (stringstuff::nocaseEqual(vstring.substr(0,3),"NAN")) {
return new HdrRecord<double>(keyword,
std::numeric_limits<double>::quiet_NaN(),
comment);
}
/**/cerr << "Got ASCII header with bad value <" << vstring << "> keyword " << keyword << endl;
return 0; // Formatting error
}
istream&
operator>>(istream& is, Header& h) {
string buffer;
while (stringstuff::getlineNoComment(is, buffer)) {
HdrRecordBase* hrb = ReadASCIIHeader(buffer);
if (!hrb) {
is.setstate(ios::failbit); // ??? do we want to throw here?
continue;
}
if (hrb->getKeyword()=="END") {
delete hrb;
return is;
} else if (hrb->getKeyword()=="COMMENT") {
h.addComment(hrb->getComment());
delete hrb;
} else if (hrb->getKeyword()=="HISTORY") {
h.addHistory(hrb->getComment());
delete hrb;
} else {
h.append(hrb);
}
}
// Get here is we exhaust the istream before END. eof should already be set.
return is;
}
ostream&
operator<<(ostream& os,
const img::Header& h) {
for (h.rewind(); !h.atEnd(); h.incr())
os << h.current()->writeCard() << endl;
os << "END " << endl;
return os;
}
}