1 | #ifndef MARS_ofits
|
---|
2 | #define MARS_ofits
|
---|
3 |
|
---|
4 | #include <string>
|
---|
5 | #include <string.h>
|
---|
6 | #include <algorithm>
|
---|
7 | #include <sstream>
|
---|
8 | #include <iostream>
|
---|
9 | #include <fstream>
|
---|
10 | #include <iomanip>
|
---|
11 | #include <vector>
|
---|
12 | #include <algorithm>
|
---|
13 |
|
---|
14 | #ifdef __EXCEPTIONS
|
---|
15 | #include <stdexcept>
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | #include "checksum.h"
|
---|
19 |
|
---|
20 | namespace std
|
---|
21 | {
|
---|
22 | // Sloppy: allow / <--- left
|
---|
23 | // allow all characters (see specs for what is possible)
|
---|
24 |
|
---|
25 | // units: m kg s rad sr K A mol cd Hz J W V N Pa C Ohm S F Wb T Hlm lx
|
---|
26 |
|
---|
27 | class ofits : public ofstream
|
---|
28 | {
|
---|
29 | public:
|
---|
30 | struct Key
|
---|
31 | {
|
---|
32 | string key;
|
---|
33 | bool delim;
|
---|
34 | string value;
|
---|
35 | string comment;
|
---|
36 |
|
---|
37 | off_t offset; // offset in file
|
---|
38 |
|
---|
39 | bool changed; // For closing the file
|
---|
40 |
|
---|
41 | Key(const string &k="") : key(k), delim(false), offset(0), changed(true) { }
|
---|
42 |
|
---|
43 | string Trim(const string &str)
|
---|
44 | {
|
---|
45 | // Trim Both leading and trailing spaces
|
---|
46 | const size_t first = str.find_first_not_of(' '); // Find the first character position after excluding leading blank spaces
|
---|
47 | const size_t last = str.find_last_not_of(' '); // Find the first character position from reverse af
|
---|
48 |
|
---|
49 | // if all spaces or empty return an empty string
|
---|
50 | if (string::npos==first || string::npos==last)
|
---|
51 | return string();
|
---|
52 |
|
---|
53 | return str.substr(first, last-first+1);
|
---|
54 | }
|
---|
55 |
|
---|
56 | bool FormatKey()
|
---|
57 | {
|
---|
58 | key = Trim(key);
|
---|
59 | if (key.size()==0)
|
---|
60 | {
|
---|
61 | #ifdef __EXCEPTIONS
|
---|
62 | throw runtime_error("Key name empty.");
|
---|
63 | #else
|
---|
64 | gLog << ___err___ << "ERROR - Key name empty." << endl;
|
---|
65 | return false;
|
---|
66 | #endif
|
---|
67 | }
|
---|
68 | if (key.size()>8)
|
---|
69 | {
|
---|
70 | ostringstream sout;
|
---|
71 | sout << "Key '" << key << "' exceeds 8 bytes.";
|
---|
72 | #ifdef __EXCEPTIONS
|
---|
73 | throw runtime_error(sout.str());
|
---|
74 | #else
|
---|
75 | gLog << ___err___ << "ERROR - " << sout.str() << endl;
|
---|
76 | return false;
|
---|
77 | #endif
|
---|
78 | }
|
---|
79 |
|
---|
80 | //transform(key.begin(), key.end(), key.begin(), toupper);
|
---|
81 |
|
---|
82 | for (string::const_iterator c=key.begin(); c<key.end(); c++)
|
---|
83 | if ((*c<'A' || *c>'Z') && (*c<'0' || *c>'9') && *c!='-' && *c!='_')
|
---|
84 | {
|
---|
85 | ostringstream sout;
|
---|
86 | sout << "Invalid character '" << *c << "' found in key '" << key << "'";
|
---|
87 | #ifdef __EXCEPTIONS
|
---|
88 | throw runtime_error(sout.str());
|
---|
89 | #else
|
---|
90 | gLog << ___err___ << "ERROR - " << sout.str() << endl;
|
---|
91 | return false;
|
---|
92 | #endif
|
---|
93 | }
|
---|
94 |
|
---|
95 | return true;
|
---|
96 | }
|
---|
97 |
|
---|
98 | bool FormatComment()
|
---|
99 | {
|
---|
100 | comment = Trim(comment);
|
---|
101 |
|
---|
102 | for (string::const_iterator c=key.begin(); c<key.end(); c++)
|
---|
103 | if (*c<32 || *c>126)
|
---|
104 | {
|
---|
105 | ostringstream sout;
|
---|
106 | sout << "Invalid character '" << *c << "' [" << int(*c) << "] found in comment '" << comment << "'";
|
---|
107 | #ifdef __EXCEPTIONS
|
---|
108 | throw runtime_error(sout.str());
|
---|
109 | #else
|
---|
110 | gLog << ___err___ << "ERROR - " << sout.str() << endl;
|
---|
111 | return false;
|
---|
112 | #endif
|
---|
113 | }
|
---|
114 |
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | bool check()
|
---|
119 | {
|
---|
120 | if (!FormatKey())
|
---|
121 | return false;
|
---|
122 |
|
---|
123 | if (!FormatComment())
|
---|
124 | return false;
|
---|
125 |
|
---|
126 | const size_t sz = CalcSize();
|
---|
127 | if (sz>80)
|
---|
128 | {
|
---|
129 | ostringstream sout;
|
---|
130 | sout << "Size " << sz << " of entry for key '" << key << "' exceeds 80 characters.";
|
---|
131 | #ifdef __EXCEPTIONS
|
---|
132 | throw runtime_error(sout.str());
|
---|
133 | #else
|
---|
134 | gLog << ___err___ << "ERROR - " << sout.str() << endl;
|
---|
135 | return false;
|
---|
136 | #endif
|
---|
137 | }
|
---|
138 |
|
---|
139 | return true;
|
---|
140 | }
|
---|
141 |
|
---|
142 | size_t CalcSize() const
|
---|
143 | {
|
---|
144 | if (!delim)
|
---|
145 | return 10+comment.size();
|
---|
146 |
|
---|
147 | return 10 + (value.size()<20?20:value.size()) + 3 + comment.size();
|
---|
148 | }
|
---|
149 |
|
---|
150 | string Compile()
|
---|
151 | {
|
---|
152 | ostringstream sout;
|
---|
153 | sout << std::left << setw(8) << key;
|
---|
154 |
|
---|
155 | if (!delim)
|
---|
156 | {
|
---|
157 | sout << " " << comment;
|
---|
158 | return sout.str();
|
---|
159 | }
|
---|
160 |
|
---|
161 | sout << "= ";
|
---|
162 | sout << (value[0]=='\''?std::left:std::right);
|
---|
163 | sout << setw(20) << value << std::left;
|
---|
164 |
|
---|
165 | if (comment.size()>0)
|
---|
166 | sout << " / " << comment;
|
---|
167 |
|
---|
168 | return sout.str();
|
---|
169 | }
|
---|
170 |
|
---|
171 | Checksum checksum;
|
---|
172 |
|
---|
173 | void Out(ofstream &fout)
|
---|
174 | {
|
---|
175 | if (!changed)
|
---|
176 | return;
|
---|
177 |
|
---|
178 | string str = Compile();
|
---|
179 | str.insert(str.end(), 80-str.size(), ' ');
|
---|
180 |
|
---|
181 | if (offset==0)
|
---|
182 | offset = fout.tellp();
|
---|
183 |
|
---|
184 | //cout << "Write[" << offset << "]: " << key << "/" << value << endl;
|
---|
185 |
|
---|
186 | fout.seekp(offset);
|
---|
187 | fout << str;
|
---|
188 |
|
---|
189 | checksum.reset();
|
---|
190 | checksum.add(str.c_str(), 80);
|
---|
191 |
|
---|
192 | changed = false;
|
---|
193 | }
|
---|
194 | /*
|
---|
195 | void Out(ostream &out)
|
---|
196 | {
|
---|
197 | string str = Compile();
|
---|
198 |
|
---|
199 | str.insert(str.end(), 80-str.size(), ' ');
|
---|
200 |
|
---|
201 | out << str;
|
---|
202 | changed = false;
|
---|
203 | }*/
|
---|
204 | };
|
---|
205 |
|
---|
206 | private:
|
---|
207 | vector<Key> fKeys;
|
---|
208 |
|
---|
209 | vector<Key>::iterator findkey(const string &key)
|
---|
210 | {
|
---|
211 | for (auto it=fKeys.begin(); it!=fKeys.end(); it++)
|
---|
212 | if (key==it->key)
|
---|
213 | return it;
|
---|
214 |
|
---|
215 | return fKeys.end();
|
---|
216 | }
|
---|
217 |
|
---|
218 | bool Set(const string &key="", bool delim=false, const string &value="", const string &comment="")
|
---|
219 | {
|
---|
220 | // If no delimit add the row no matter if it alread exists
|
---|
221 | if (delim)
|
---|
222 | {
|
---|
223 | // if the row already exists: update it
|
---|
224 | auto it = findkey(key);
|
---|
225 | if (it!=fKeys.end())
|
---|
226 | {
|
---|
227 | it->value = value;
|
---|
228 | it->changed = true;
|
---|
229 | return true;
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | if (fTable.num_rows>0)
|
---|
234 | {
|
---|
235 | ostringstream sout;
|
---|
236 | sout << "No new header key can be defined, rows were already written to the file... ignoring new key '" << key << "'";
|
---|
237 | #ifdef __EXCEPTIONS
|
---|
238 | throw runtime_error(sout.str());
|
---|
239 | #else
|
---|
240 | gLog << ___err___ << "ERROR - " << sout.str() << endl;
|
---|
241 | return false;
|
---|
242 | #endif
|
---|
243 | }
|
---|
244 |
|
---|
245 | Key entry;
|
---|
246 |
|
---|
247 | entry.key = key;
|
---|
248 | entry.delim = delim;
|
---|
249 | entry.value = value;
|
---|
250 | entry.comment = comment;
|
---|
251 | entry.offset = 0;
|
---|
252 | entry.changed = true;
|
---|
253 |
|
---|
254 | if (!entry.check())
|
---|
255 | return false;
|
---|
256 |
|
---|
257 | fKeys.push_back(entry);
|
---|
258 | return true;
|
---|
259 | }
|
---|
260 |
|
---|
261 | struct Table
|
---|
262 | {
|
---|
263 | off_t offset;
|
---|
264 |
|
---|
265 | size_t bytes_per_row;
|
---|
266 | size_t num_rows;
|
---|
267 | size_t num_cols;
|
---|
268 |
|
---|
269 | struct Column
|
---|
270 | {
|
---|
271 | string name;
|
---|
272 | size_t offset;
|
---|
273 | size_t num;
|
---|
274 | size_t size;
|
---|
275 | char type;
|
---|
276 | };
|
---|
277 |
|
---|
278 | vector<Column> cols;
|
---|
279 |
|
---|
280 | Table() : offset(0), bytes_per_row(0), num_rows(0), num_cols(0)
|
---|
281 | {
|
---|
282 | }
|
---|
283 | };
|
---|
284 |
|
---|
285 |
|
---|
286 | Table fTable;
|
---|
287 |
|
---|
288 | vector<char> fOutputBuffer;
|
---|
289 |
|
---|
290 | vector<Table::Column>::iterator findcol(const string &name)
|
---|
291 | {
|
---|
292 | for (auto it=fTable.cols.begin(); it!=fTable.cols.end(); it++)
|
---|
293 | if (name==it->name)
|
---|
294 | return it;
|
---|
295 |
|
---|
296 | return fTable.cols.end();
|
---|
297 | }
|
---|
298 |
|
---|
299 | Checksum fDataSum;
|
---|
300 | Checksum fHeaderSum;
|
---|
301 |
|
---|
302 | public:
|
---|
303 | ofits()
|
---|
304 | {
|
---|
305 | }
|
---|
306 | ofits(const char *fname) : ofstream()
|
---|
307 | {
|
---|
308 | this->open(fname);
|
---|
309 | }
|
---|
310 | ~ofits() { close(); }
|
---|
311 |
|
---|
312 | void open(const char * filename)
|
---|
313 | {
|
---|
314 | fDataSum = 0;
|
---|
315 | fHeaderSum = 0;
|
---|
316 |
|
---|
317 | fTable = Table();
|
---|
318 | fKeys.clear();
|
---|
319 |
|
---|
320 | SetStr("XTENSION", "BINTABLE", "binary table extension");
|
---|
321 | SetInt("BITPIX", 8, "8-bit bytes");
|
---|
322 | SetInt("NAXIS", 2, "2-dimensional binary table");
|
---|
323 | SetInt("NAXIS1", 0, "width of table in bytes");
|
---|
324 | SetInt("NAXIS2", 0, "number of rows in table");
|
---|
325 | SetInt("PCOUNT", 0, "size of special data area");
|
---|
326 | SetInt("GCOUNT", 1, "one data group (required keyword)");
|
---|
327 | SetInt("TFIELDS", 0, "number of fields in each row");
|
---|
328 | SetStr("EXTNAME", "", "name of extension table");
|
---|
329 | SetStr("CHECKSUM", "0000000000000000", "Checksum for the whole HDU");
|
---|
330 | SetStr("DATASUM", " 0", "Checksum for the data block");
|
---|
331 |
|
---|
332 | ofstream::open(filename);
|
---|
333 | }
|
---|
334 |
|
---|
335 | bool SetRaw(const string &key, const string &val, const string &comment)
|
---|
336 | {
|
---|
337 | return Set(key, true, val, comment);
|
---|
338 | }
|
---|
339 |
|
---|
340 | bool SetBool(const string &key, bool b, const string &comment="")
|
---|
341 | {
|
---|
342 | return Set(key, true, b?"T":"F", comment);
|
---|
343 | }
|
---|
344 |
|
---|
345 | bool AddEmpty(const string &key, const string &comment="")
|
---|
346 | {
|
---|
347 | return Set(key, true, "", comment);
|
---|
348 | }
|
---|
349 |
|
---|
350 | bool SetStr(const string &key, string s, const string &comment="")
|
---|
351 | {
|
---|
352 | for (string::iterator c=s.begin(); c<s.end(); c++)
|
---|
353 | if (*c=='\'')
|
---|
354 | s.insert(c++, '\'');
|
---|
355 |
|
---|
356 | return Set(key, true, "'"+s+"'", comment);
|
---|
357 | }
|
---|
358 |
|
---|
359 | bool SetInt(const string &key, int64_t i, const string &comment="")
|
---|
360 | {
|
---|
361 | ostringstream sout;
|
---|
362 | sout << i;
|
---|
363 |
|
---|
364 | return Set(key, true, sout.str(), comment);
|
---|
365 | }
|
---|
366 |
|
---|
367 | bool SetFloat(const string &key, double f, int p, const string &comment="")
|
---|
368 | {
|
---|
369 | ostringstream sout;
|
---|
370 |
|
---|
371 | if (p<0)
|
---|
372 | sout << setprecision(-p) << fixed;
|
---|
373 | if (p>0)
|
---|
374 | sout << setprecision(p);
|
---|
375 | if (p==0)
|
---|
376 | sout << setprecision(f>1e-100 && f<1e100 ? 15 : 14);
|
---|
377 |
|
---|
378 | sout << f;
|
---|
379 |
|
---|
380 | string str = sout.str();
|
---|
381 |
|
---|
382 | replace(str.begin(), str.end(), 'e', 'E');
|
---|
383 |
|
---|
384 | if (str.find_first_of('E')==string::npos && str.find_first_of('.')==string::npos)
|
---|
385 | str += ".";
|
---|
386 |
|
---|
387 | return Set(key, true, str, comment);
|
---|
388 | }
|
---|
389 |
|
---|
390 | bool SetFloat(const string &key, double f, const string &comment="")
|
---|
391 | {
|
---|
392 | return SetFloat(key, f, 0, comment);
|
---|
393 | }
|
---|
394 |
|
---|
395 | bool SetHex(const string &key, uint64_t i, const string &comment="")
|
---|
396 | {
|
---|
397 | ostringstream sout;
|
---|
398 | sout << std::hex << "0x" << i;
|
---|
399 | return SetStr(key, sout.str(), comment);
|
---|
400 | }
|
---|
401 |
|
---|
402 | bool AddComment(const string &comment)
|
---|
403 | {
|
---|
404 | return Set("COMMENT", false, "", comment);
|
---|
405 | }
|
---|
406 |
|
---|
407 | bool AddHistory(const string &comment)
|
---|
408 | {
|
---|
409 | return Set("HISTORY", false, "", comment);
|
---|
410 | }
|
---|
411 |
|
---|
412 | void End()
|
---|
413 | {
|
---|
414 | Set("END");
|
---|
415 | while (fKeys.size()%36!=0)
|
---|
416 | fKeys.push_back(Key());
|
---|
417 | }
|
---|
418 |
|
---|
419 | bool AddColumn(uint32_t cnt, char typechar, const string &name, const string &unit, const string &comment="")
|
---|
420 | {
|
---|
421 | if (tellp()<0)
|
---|
422 | {
|
---|
423 | ostringstream sout;
|
---|
424 | sout << "File not open... ignoring column '" << name << "'";
|
---|
425 | #ifdef __EXCEPTIONS
|
---|
426 | throw runtime_error(sout.str());
|
---|
427 | #else
|
---|
428 | gLog << ___err___ << "ERROR - " << sout.str() << endl;
|
---|
429 | return false;
|
---|
430 | #endif
|
---|
431 | }
|
---|
432 |
|
---|
433 | if (tellp()>0)
|
---|
434 | {
|
---|
435 | ostringstream sout;
|
---|
436 | sout << "Header already writtenm, no new column can be defined... ignoring column '" << name << "'";
|
---|
437 | #ifdef __EXCEPTIONS
|
---|
438 | throw runtime_error(sout.str());
|
---|
439 | #else
|
---|
440 | gLog << ___err___ << "ERROR - " << sout.str() << endl;
|
---|
441 | return false;
|
---|
442 | #endif
|
---|
443 | }
|
---|
444 |
|
---|
445 | if (findcol(name)!=fTable.cols.end())
|
---|
446 | {
|
---|
447 | ostringstream sout;
|
---|
448 | sout << "A column with the name '" << name << "' already exists.";
|
---|
449 | #ifdef __EXCEPTIONS
|
---|
450 | throw runtime_error(sout.str());
|
---|
451 | #else
|
---|
452 | gLog << ___err___ << "ERROR - " << sout.str() << endl;
|
---|
453 | return false;
|
---|
454 | #endif
|
---|
455 | }
|
---|
456 |
|
---|
457 | typechar = toupper(typechar);
|
---|
458 |
|
---|
459 | static const string allow("LABIJKED");
|
---|
460 | if (std::find(allow.begin(), allow.end(), typechar)==allow.end())
|
---|
461 | {
|
---|
462 | ostringstream sout;
|
---|
463 | sout << "Column type '" << typechar << "' not supported.";
|
---|
464 | #ifdef __EXCEPTIONS
|
---|
465 | throw runtime_error(sout.str());
|
---|
466 | #else
|
---|
467 | gLog << ___err___ << "ERROR - " << sout.str() << endl;
|
---|
468 | return false;
|
---|
469 | #endif
|
---|
470 | }
|
---|
471 |
|
---|
472 | ostringstream type;
|
---|
473 | type << cnt << typechar;
|
---|
474 |
|
---|
475 | fTable.num_cols++;
|
---|
476 |
|
---|
477 | ostringstream typekey, formkey, unitkey, unitcom, typecom;
|
---|
478 | typekey << "TTYPE" << fTable.num_cols;
|
---|
479 | formkey << "TFORM" << fTable.num_cols;
|
---|
480 | unitkey << "TUNIT" << fTable.num_cols;
|
---|
481 | unitcom << "unit of " << name;
|
---|
482 |
|
---|
483 | typecom << "format of " << name << " [";
|
---|
484 |
|
---|
485 | switch (typechar)
|
---|
486 | {
|
---|
487 | case 'L': typecom << "1-byte BOOL]"; break;
|
---|
488 | case 'A': typecom << "1-byte CHAR]"; break;
|
---|
489 | case 'B': typecom << "1-byte BOOL]"; break;
|
---|
490 | case 'I': typecom << "2-byte INT]"; break;
|
---|
491 | case 'J': typecom << "4-byte INT]"; break;
|
---|
492 | case 'K': typecom << "8-byte INT]"; break;
|
---|
493 | case 'E': typecom << "4-byte FLOAT]"; break;
|
---|
494 | case 'D': typecom << "8-byte FLOAT]"; break;
|
---|
495 | }
|
---|
496 |
|
---|
497 | SetStr(formkey.str(), type.str(), typecom.str());
|
---|
498 | SetStr(typekey.str(), name, comment);
|
---|
499 |
|
---|
500 | if (!unit.empty())
|
---|
501 | SetStr(unitkey.str(), unit, unitcom.str());
|
---|
502 |
|
---|
503 | size_t size = 0;
|
---|
504 |
|
---|
505 | switch (typechar)
|
---|
506 | {
|
---|
507 | case 'L': size = 1; break;
|
---|
508 | case 'A': size = 1; break;
|
---|
509 | case 'B': size = 1; break;
|
---|
510 | case 'I': size = 2; break;
|
---|
511 | case 'J': size = 4; break;
|
---|
512 | case 'K': size = 8; break;
|
---|
513 | case 'E': size = 4; break;
|
---|
514 | case 'D': size = 8; break;
|
---|
515 | }
|
---|
516 |
|
---|
517 | Table::Column col;
|
---|
518 | col.name = name;
|
---|
519 | col.type = typechar;
|
---|
520 | col.num = cnt;
|
---|
521 | col.size = size;
|
---|
522 | col.offset = fTable.bytes_per_row;
|
---|
523 |
|
---|
524 | fTable.cols.push_back(col);
|
---|
525 |
|
---|
526 | fTable.bytes_per_row += size*cnt;
|
---|
527 |
|
---|
528 | // Align to four bytes
|
---|
529 | fOutputBuffer.resize(fTable.bytes_per_row + 4-fTable.bytes_per_row%4);
|
---|
530 |
|
---|
531 | return true;
|
---|
532 | }
|
---|
533 |
|
---|
534 | bool AddColumnShort(uint32_t cnt, const string &name, const string &unit="", const string &comment="")
|
---|
535 | { return AddColumn(cnt, 'I', name, unit, comment); }
|
---|
536 | bool AddColumnInt(uint32_t cnt, const string &name, const string &unit="", const string &comment="")
|
---|
537 | { return AddColumn(cnt, 'J', name, unit, comment); }
|
---|
538 | bool AddColumnLong(uint32_t cnt, const string &name, const string &unit="", const string &comment="")
|
---|
539 | { return AddColumn(cnt, 'K', name, unit, comment); }
|
---|
540 | bool AddColumnFloat(uint32_t cnt, const string &name, const string &unit="", const string &comment="")
|
---|
541 | { return AddColumn(cnt, 'E', name, unit, comment); }
|
---|
542 | bool AddColumnDouble(uint32_t cnt, const string &name, const string &unit="", const string &comment="")
|
---|
543 | { return AddColumn(cnt, 'D', name, unit, comment); }
|
---|
544 | bool AddColumnChar(uint32_t cnt, const string &name, const string &unit="", const string &comment="")
|
---|
545 | { return AddColumn(cnt, 'A', name, unit, comment); }
|
---|
546 | bool AddColumnByte(uint32_t cnt, const string &name, const string &unit="", const string &comment="")
|
---|
547 | { return AddColumn(cnt, 'B', name, unit, comment); }
|
---|
548 | bool AddColumnBool(uint32_t cnt, const string &name, const string &unit="", const string &comment="")
|
---|
549 | { return AddColumn(cnt, 'L', name, unit, comment); }
|
---|
550 |
|
---|
551 | bool AddColumnShort(const string &name, const string &unit="", const string &comment="")
|
---|
552 | { return AddColumn(1, 'I', name, unit, comment); }
|
---|
553 | bool AddColumnInt(const string &name, const string &unit="", const string &comment="")
|
---|
554 | { return AddColumn(1, 'J', name, unit, comment); }
|
---|
555 | bool AddColumnLong(const string &name, const string &unit="", const string &comment="")
|
---|
556 | { return AddColumn(1, 'K', name, unit, comment); }
|
---|
557 | bool AddColumnFloat(const string &name, const string &unit="", const string &comment="")
|
---|
558 | { return AddColumn(1, 'E', name, unit, comment); }
|
---|
559 | bool AddColumnDouble(const string &name, const string &unit="", const string &comment="")
|
---|
560 | { return AddColumn(1, 'D', name, unit, comment); }
|
---|
561 | bool AddColumnChar(const string &name, const string &unit="", const string &comment="")
|
---|
562 | { return AddColumn(1, 'A', name, unit, comment); }
|
---|
563 | bool AddColumnByte(const string &name, const string &unit="", const string &comment="")
|
---|
564 | { return AddColumn(1, 'B', name, unit, comment); }
|
---|
565 | bool AddColumnBool(const string &name, const string &unit="", const string &comment="")
|
---|
566 | { return AddColumn(1, 'L', name, unit, comment); }
|
---|
567 |
|
---|
568 | /*
|
---|
569 | bool AddKey(string key, double d, const string &comment)
|
---|
570 | {
|
---|
571 | ostringstream out;
|
---|
572 | out << d;
|
---|
573 |
|
---|
574 | string s = out.str();
|
---|
575 |
|
---|
576 | replace(s.begin(), s.end(), "e", "E");
|
---|
577 |
|
---|
578 | return AddKey(key, s, comment);
|
---|
579 | }*/
|
---|
580 |
|
---|
581 |
|
---|
582 | Checksum WriteHeader(ofstream &fout)
|
---|
583 | {
|
---|
584 | Checksum sum;
|
---|
585 | for (auto it=fKeys.begin(); it!=fKeys.end(); it++)
|
---|
586 | {
|
---|
587 | it->Out(fout);
|
---|
588 | sum += it->checksum;
|
---|
589 | }
|
---|
590 | fout.flush();
|
---|
591 |
|
---|
592 | return sum;
|
---|
593 | }
|
---|
594 |
|
---|
595 | Checksum WriteHeader()
|
---|
596 | {
|
---|
597 | return WriteHeader(*this);
|
---|
598 | }
|
---|
599 |
|
---|
600 | void FlushHeader()
|
---|
601 | {
|
---|
602 | const off_t pos = tellp();
|
---|
603 | WriteHeader();
|
---|
604 | seekp(pos);
|
---|
605 | }
|
---|
606 |
|
---|
607 | Checksum WriteFitsHeader()
|
---|
608 | {
|
---|
609 | ofits h;
|
---|
610 |
|
---|
611 | h.SetBool("SIMPLE", true, "file does conform to FITS standard");
|
---|
612 | h.SetInt ("BITPIX", 8, "number of bits per data pixel");
|
---|
613 | h.SetInt ("NAXIS", 0, "number of data axes");
|
---|
614 | h.SetBool("EXTEND", true, "FITS dataset may contain extensions");
|
---|
615 | h.SetStr ("CHECKSUM","0000000000000000", "Checksum for the whole HDU");
|
---|
616 | h.SetStr ("DATASUM", " 0", "Checksum for the data block");
|
---|
617 | h.AddComment("FITS (Flexible Image Transport System) format is defined in 'Astronomy");
|
---|
618 | h.AddComment("and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H");
|
---|
619 | h.End();
|
---|
620 |
|
---|
621 | const Checksum sum = h.WriteHeader(*this);
|
---|
622 |
|
---|
623 | h.SetStr("CHECKSUM", sum.str());
|
---|
624 |
|
---|
625 | const size_t offset = tellp();
|
---|
626 | h.WriteHeader(*this);
|
---|
627 | seekp(offset);
|
---|
628 |
|
---|
629 | return sum;
|
---|
630 | }
|
---|
631 |
|
---|
632 | bool WriteTableHeader(const char *name="DATA")
|
---|
633 | {
|
---|
634 | if (tellp()>0)
|
---|
635 | {
|
---|
636 | #ifdef __EXCEPTIONS
|
---|
637 | throw runtime_error("File not empty anymore.");
|
---|
638 | #else
|
---|
639 | gLog << ___err___ << "ERROR - File not empty anymore." << endl;
|
---|
640 | return false;
|
---|
641 | #endif
|
---|
642 | }
|
---|
643 |
|
---|
644 | fHeaderSum = WriteFitsHeader();
|
---|
645 |
|
---|
646 | SetStr("EXTNAME", name);
|
---|
647 | SetInt("NAXIS1", fTable.bytes_per_row);
|
---|
648 | SetInt("TFIELDS", fTable.cols.size());
|
---|
649 |
|
---|
650 | End();
|
---|
651 |
|
---|
652 | WriteHeader();
|
---|
653 |
|
---|
654 | return good();
|
---|
655 | }
|
---|
656 |
|
---|
657 | template<size_t N>
|
---|
658 | void revcpy(char *dest, const char *src, int num)
|
---|
659 | {
|
---|
660 | const char *pend = src + num*N;
|
---|
661 | for (const char *ptr = src; ptr<pend; ptr+=N, dest+=N)
|
---|
662 | reverse_copy(ptr, ptr+N, dest);
|
---|
663 | }
|
---|
664 |
|
---|
665 | uint32_t GetBytesPerRow() const { return fTable.bytes_per_row; }
|
---|
666 |
|
---|
667 | bool WriteRow(const void *ptr, size_t cnt, bool byte_swap=true)
|
---|
668 | {
|
---|
669 | // FIXME: Make sure that header was already written
|
---|
670 | // or write header now!
|
---|
671 | if (cnt!=fTable.bytes_per_row)
|
---|
672 | {
|
---|
673 | ostringstream sout;
|
---|
674 | sout << "WriteRow - Size " << cnt << " does not match expected size " << fTable.bytes_per_row;
|
---|
675 | #ifdef __EXCEPTIONS
|
---|
676 | throw runtime_error(sout.str());
|
---|
677 | #else
|
---|
678 | gLog << ___err___ << "ERROR - " << sout.str() << endl;
|
---|
679 | return false;
|
---|
680 | #endif
|
---|
681 | }
|
---|
682 |
|
---|
683 | // For the checksum we need everything to be correctly aligned
|
---|
684 | const uint8_t offset = fTable.offset%4;
|
---|
685 |
|
---|
686 | char *buffer = fOutputBuffer.data() + offset;
|
---|
687 |
|
---|
688 | auto ib = fOutputBuffer.begin();
|
---|
689 | auto ie = fOutputBuffer.end();
|
---|
690 | *ib++ = 0;
|
---|
691 | *ib++ = 0;
|
---|
692 | *ib++ = 0;
|
---|
693 | *ib = 0;
|
---|
694 |
|
---|
695 | *--ie = 0;
|
---|
696 | *--ie = 0;
|
---|
697 | *--ie = 0;
|
---|
698 | *--ie = 0;
|
---|
699 |
|
---|
700 | if (!byte_swap)
|
---|
701 | memcpy(buffer, ptr, cnt);
|
---|
702 | else
|
---|
703 | {
|
---|
704 | for (auto it=fTable.cols.begin(); it!=fTable.cols.end(); it++)
|
---|
705 | {
|
---|
706 | const char *src = reinterpret_cast<const char*>(ptr) + it->offset;
|
---|
707 | char *dest = buffer + it->offset;
|
---|
708 |
|
---|
709 | // Let the compiler do some optimization by
|
---|
710 | // knowing the we only have 1, 2, 4 and 8
|
---|
711 | switch (it->size)
|
---|
712 | {
|
---|
713 | case 1: memcpy (dest, src, it->num*it->size); break;
|
---|
714 | case 2: revcpy<2>(dest, src, it->num); break;
|
---|
715 | case 4: revcpy<4>(dest, src, it->num); break;
|
---|
716 | case 8: revcpy<8>(dest, src, it->num); break;
|
---|
717 | }
|
---|
718 | }
|
---|
719 | }
|
---|
720 |
|
---|
721 | write(buffer, cnt);
|
---|
722 | fDataSum.add(fOutputBuffer);
|
---|
723 |
|
---|
724 | fTable.num_rows++;
|
---|
725 | fTable.offset += cnt;
|
---|
726 | return good();
|
---|
727 | }
|
---|
728 |
|
---|
729 | template<typename N>
|
---|
730 | bool WriteRow(const vector<N> &vec)
|
---|
731 | {
|
---|
732 | return WriteRow(vec.data(), vec.size()*sizeof(N));
|
---|
733 | }
|
---|
734 |
|
---|
735 | // Flushes the number of rows to the header on disk
|
---|
736 | void FlushNumRows()
|
---|
737 | {
|
---|
738 | SetInt("NAXIS2", fTable.num_rows);
|
---|
739 | FlushHeader();
|
---|
740 | }
|
---|
741 |
|
---|
742 | bool close()
|
---|
743 | {
|
---|
744 | if (tellp()<0)
|
---|
745 | return false;
|
---|
746 |
|
---|
747 | if (tellp()%(80*36)>0)
|
---|
748 | {
|
---|
749 | const vector<char> filler(80*36-tellp()%(80*36));
|
---|
750 | write(filler.data(), filler.size());
|
---|
751 | }
|
---|
752 |
|
---|
753 | // We don't have to jump back to the end of the file
|
---|
754 | SetInt("NAXIS2", fTable.num_rows);
|
---|
755 |
|
---|
756 | ostringstream dataSumStr;
|
---|
757 | dataSumStr << fDataSum.val();
|
---|
758 | SetStr("DATASUM", dataSumStr.str());
|
---|
759 |
|
---|
760 | const Checksum sum = WriteHeader();
|
---|
761 |
|
---|
762 | //sum += headersum;
|
---|
763 |
|
---|
764 | SetStr("CHECKSUM", (sum+fDataSum).str());
|
---|
765 |
|
---|
766 | const Checksum chk = WriteHeader();
|
---|
767 |
|
---|
768 | ofstream::close();
|
---|
769 |
|
---|
770 | if ((chk+fDataSum).valid())
|
---|
771 | return true;
|
---|
772 |
|
---|
773 | ostringstream sout;
|
---|
774 | sout << "Checksum (" << std::hex << chk.val() << ") invalid.";
|
---|
775 | #ifdef __EXCEPTIONS
|
---|
776 | throw runtime_error(sout.str());
|
---|
777 | #else
|
---|
778 | gLog << ___err___ << "ERROR - " << sout.str() << endl;
|
---|
779 | return false;
|
---|
780 | #endif
|
---|
781 | }
|
---|
782 | };
|
---|
783 |
|
---|
784 | }
|
---|
785 |
|
---|
786 | #if 0
|
---|
787 | #include "fits.h"
|
---|
788 |
|
---|
789 | int main()
|
---|
790 | {
|
---|
791 | using namespace std;
|
---|
792 |
|
---|
793 | ofits h2("delme.fits");
|
---|
794 |
|
---|
795 | h2.SetInt("KEY1", 1, "comment 1");
|
---|
796 | h2.AddColumnInt(2, "testcol1", "counts", "My comment");
|
---|
797 | h2.AddColumnInt("testcol2", "counts", "My comment");
|
---|
798 | //h2.AddColumnInt("testcol2", "counts", "My comment");
|
---|
799 | h2.SetInt("KEY2", 2, "comment 2");
|
---|
800 |
|
---|
801 | /*
|
---|
802 | AddFloat("X0", 0.000123456, "number of fields in each row");
|
---|
803 | AddFloat("X1", 0, "number of fields in each row");
|
---|
804 | AddFloat("X2", 12345, "number of fields in each row");
|
---|
805 | AddFloat("X3", 123456.67890, "number of fields in each row");
|
---|
806 | AddFloat("X4", 1234567890123456789.12345678901234567890, "number of fields in each row");
|
---|
807 | AddFloat("X5", 1234567890.1234567890e20, "number of fields in each row");
|
---|
808 | AddFloat("X6", 1234567890.1234567890e-20, "number of fields in each row");
|
---|
809 | AddFloat("XB", 1234567890.1234567890e-111, "number of fields in each row");
|
---|
810 | AddFloat("X7", 1e-5, "number of fields in each row");
|
---|
811 | AddFloat("X8", 1e-6, "number of fields in each row");
|
---|
812 | //AddStr("12345678", "123456789012345678", "12345678901234567890123456789012345678901234567");
|
---|
813 | */
|
---|
814 | // -
|
---|
815 |
|
---|
816 | h2.WriteTableHeader("TABLE_NAME");
|
---|
817 |
|
---|
818 | for (int i=0; i<10; i++)
|
---|
819 | {
|
---|
820 | int j[3] = { i+10, i*10, i*100 };
|
---|
821 | h2.WriteRow(j, 3*sizeof(i));
|
---|
822 | }
|
---|
823 |
|
---|
824 | //h2.AddColumnInt("testcol2", "counts", "My comment");
|
---|
825 | //h2.SetInt("KEY3", 2, "comment 2");
|
---|
826 | h2.SetInt("KEY2", 2, "comment 2xxx");
|
---|
827 | h2.SetInt("KEY1", 11);
|
---|
828 |
|
---|
829 | h2.close();
|
---|
830 |
|
---|
831 | cout << "---" << endl;
|
---|
832 |
|
---|
833 | fits f("delme.fits");
|
---|
834 | if (!f)
|
---|
835 | throw runtime_error("xxx");
|
---|
836 |
|
---|
837 | cout << "Header is valid: " << f.IsHeaderOk() << endl;
|
---|
838 |
|
---|
839 | while (f.GetNextRow());
|
---|
840 |
|
---|
841 | cout << "File is valid: " << f.IsFileOk() << endl;
|
---|
842 |
|
---|
843 | cout << "---" << endl;
|
---|
844 |
|
---|
845 | return 0;
|
---|
846 | }
|
---|
847 | #endif
|
---|
848 |
|
---|
849 | #endif
|
---|