source: trunk/Mars/mcore/ofits.h@ 17581

Last change on this file since 17581 was 17559, checked in by aparavac, 11 years ago
Added compatibilty for gcc 4.7 and c11 std
File size: 33.6 KB
Line 
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#include <stdexcept>
14
15#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
16
17#ifdef __CINT__
18#define off_t size_t
19#endif
20
21#ifndef __MARS__
22#ifndef gLog
23#define gLog std::cerr
24#define ___err___ ""
25#define ___warn___ ""
26#define ___all___ ""
27#endif
28#else
29#include "MLog.h"
30#include "MLogManip.h"
31#define ___err___ err
32#define ___warn___ warn
33#define ___all___ all
34#endif
35
36#include "FITS.h"
37#include "checksum.h"
38
39// Sloppy: allow / <--- left
40// allow all characters (see specs for what is possible)
41
42// units: m kg s rad sr K A mol cd Hz J W V N Pa C Ohm S F Wb T Hlm lx
43
44class ofits : public std::ofstream
45{
46public:
47 struct Key
48 {
49 std::string key;
50 bool delim;
51 std::string value;
52 std::string comment;
53 std::string fitsString;
54
55 off_t offset; // offset in file
56
57 bool changed; // For closing the file
58
59 Key(const std::string &k="") : key(k), delim(false), fitsString(""), offset(0), changed(true) { }
60
61 std::string Trim(const std::string &str)
62 {
63 // Trim Both leading and trailing spaces
64 const size_t first = str.find_first_not_of(' '); // Find the first character position after excluding leading blank spaces
65 const size_t last = str.find_last_not_of(' '); // Find the first character position from reverse af
66
67 // if all spaces or empty return an empty string
68 if (std::string::npos==first || std::string::npos==last)
69 return std::string();
70
71 return str.substr(first, last-first+1);
72 }
73
74 bool FormatKey()
75 {
76 key = Trim(key);
77 if (key.empty())
78 {
79#ifdef __EXCEPTIONS
80 throw std::runtime_error("Key name empty.");
81#else
82 gLog << ___err___ << "ERROR - Key name empty." << std::endl;
83 return false;
84#endif
85 }
86 if (key.size()>8)
87 {
88 std::ostringstream sout;
89 sout << "Key '" << key << "' exceeds 8 bytes.";
90#ifdef __EXCEPTIONS
91 throw std::runtime_error(sout.str());
92#else
93 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
94 return false;
95#endif
96 }
97
98 //transform(key.begin(), key.end(), key.begin(), toupper);
99#if GCC_VERSION < 40603
100 for (std::string::const_iterator c=key.begin(); c<key.end(); c++)
101#else
102 for (std::string::const_iterator c=key.cbegin(); c<key.cend(); c++)
103#endif
104 if ((*c<'A' || *c>'Z') && (*c<'0' || *c>'9') && *c!='-' && *c!='_')
105 {
106 std::ostringstream sout;
107 sout << "Invalid character '" << *c << "' found in key '" << key << "'";
108#ifdef __EXCEPTIONS
109 throw std::runtime_error(sout.str());
110#else
111 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
112 return false;
113#endif
114 }
115
116 return true;
117 }
118
119 bool FormatComment()
120 {
121 comment = Trim(comment);
122
123#if GCC_VERSION < 40603
124 for (std::string::const_iterator c=key.begin(); c<key.end(); c++)
125#else
126 for (std::string::const_iterator c=key.cbegin(); c<key.cend(); c++)
127#endif
128 if (*c<32 || *c>126)
129 {
130 std::ostringstream sout;
131 sout << "Invalid character '" << *c << "' [" << int(*c) << "] found in comment '" << comment << "'";
132#ifdef __EXCEPTIONS
133 throw std::runtime_error(sout.str());
134#else
135 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
136 return false;
137#endif
138 }
139
140 return true;
141 }
142
143 bool check(bool trim=false)
144 {
145 if (!FormatKey())
146 return false;
147
148 if (!FormatComment())
149 return false;
150
151 size_t sz = CalcSize();
152 if (sz<=80)
153 return true;
154
155 if (!trim)
156 {
157 std::ostringstream sout;
158 sout << "Size " << sz << " of entry for key '" << key << "' exceeds 80 characters.";
159#ifdef __EXCEPTIONS
160 throw std::runtime_error(sout.str());
161#else
162 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
163#endif
164 return false;
165 }
166
167 //looks like something went wrong. Maybe entry is too long ?
168 //try to remove the comment
169 comment = "";
170
171 sz = CalcSize();
172 if (sz<=80)
173 {
174#ifndef __EXCEPTIONS
175 std::ostringstream sout;
176 sout << "Size " << sz << " of entry for key '" << key << "' exceeds 80 characters... removed comment.";
177 gLog << ___warn___ << "WARNING - " << sout.str() << std::endl;
178#endif
179 return true;
180 }
181
182 std::ostringstream sout;
183 sout << "Size " << sz << " of entry for key '" << key << "' exceeds 80 characters even without comment.";
184#ifdef __EXCEPTIONS
185 throw std::runtime_error(sout.str());
186#else
187 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
188 return false;
189#endif
190 }
191
192 size_t CalcSize() const
193 {
194 if (!delim)
195 return 10+comment.size();
196
197 return 10 + (value.size()<20?20:value.size()) + 3 + comment.size();
198 }
199
200 std::string Compile()
201 {
202
203 if (fitsString != "")
204 return fitsString;
205
206 std::ostringstream sout;
207 sout << std::left << std::setw(8) << key;
208
209 if (!delim)
210 {
211 sout << " " << comment;
212 return sout.str();
213 }
214
215 sout << "= ";
216 sout << (!value.empty() && value[0]=='\''?std::left:std::right);
217 sout << std::setw(20) << value << std::left;
218
219 if (!comment.empty())
220 sout << " / " << comment;
221
222 return sout.str();
223 }
224
225 Checksum checksum;
226
227 void Out(std::ofstream &fout)
228 {
229 if (!changed)
230 return;
231
232 std::string str = Compile();
233 str.insert(str.end(), 80-str.size(), ' ');
234
235 if (offset==0)
236 offset = fout.tellp();
237
238 //cout << "Write[" << offset << "]: " << key << "/" << value << endl;
239
240 fout.seekp(offset);
241 fout << str;
242
243 checksum.reset();
244 checksum.add(str.c_str(), 80);
245
246 changed = false;
247 }
248 /*
249 void Out(ostream &out)
250 {
251 std::string str = Compile();
252
253 str.insert(str.end(), 80-str.size(), ' ');
254
255 out << str;
256 changed = false;
257 }*/
258 };
259
260private:
261 std::vector<Key> fKeys;
262
263 std::vector<Key>::iterator findkey(const std::string &key)
264 {
265 for (auto it=fKeys.begin(); it!=fKeys.end(); it++)
266 if (key==it->key)
267 return it;
268
269 return fKeys.end();
270 }
271
272 bool Set(const std::string &key="", bool delim=false, const std::string &value="", const std::string &comment="")
273 {
274 // If no delimit add the row no matter if it alread exists
275 if (delim)
276 {
277 // if the row already exists: update it
278 auto it = findkey(key);
279 if (it!=fKeys.end())
280 {
281 it->value = value;
282 it->changed = true;
283 return true;
284 }
285 }
286
287 if (fTable.num_rows>0)
288 {
289 std::ostringstream sout;
290 sout << "No new header key can be defined, rows were already written to the file... ignoring new key '" << key << "'";
291#ifdef __EXCEPTIONS
292 throw std::runtime_error(sout.str());
293#else
294 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
295 return false;
296#endif
297 }
298
299 Key entry;
300
301 entry.key = key;
302 entry.delim = delim;
303 entry.value = value;
304 entry.comment = comment;
305 entry.offset = 0;
306 entry.changed = true;
307
308 if (!entry.check(fCommentTrimming))
309 return false;
310
311 fKeys.push_back(entry);
312 return true;
313 }
314
315protected:
316 struct Table
317 {
318 off_t offset;
319
320 size_t bytes_per_row;
321 size_t num_rows;
322 size_t num_cols;
323
324 struct Column
325 {
326 std::string name;
327 size_t offset;
328 size_t num;
329 size_t size;
330 char type;
331 };
332
333 std::vector<Column> cols;
334
335 Table() : offset(0), bytes_per_row(0), num_rows(0), num_cols(0)
336 {
337 }
338 };
339
340
341 Table fTable;
342
343 std::vector<char> fOutputBuffer;
344
345 std::vector<Table::Column>::const_iterator findcol(const std::string &name)
346 {
347 for (auto it=fTable.cols.cbegin(); it!=fTable.cols.cend(); it++)
348 if (name==it->name)
349 return it;
350
351 return fTable.cols.cend();
352 }
353
354 Checksum fDataSum;
355 Checksum fHeaderSum;
356
357 bool fCommentTrimming;
358 bool fManualExtName;
359
360public:
361 ofits() : fCommentTrimming(false),
362 fManualExtName(false)
363 {
364 }
365 ofits(const char *fname) : std::ofstream(),
366 fCommentTrimming(false),
367 fManualExtName(false)
368 {
369 this->open(fname);
370 }
371 virtual ~ofits() { if (is_open()) close(); }
372
373 virtual void open(const char * filename, bool addEXTNAMEKey=true)
374 {
375 fDataSum = 0;
376 fHeaderSum = 0;
377
378 fTable = Table();
379 fKeys.clear();
380
381 SetStr("XTENSION", "BINTABLE", "binary table extension");
382 SetInt("BITPIX", 8, "8-bit bytes");
383 SetInt("NAXIS", 2, "2-dimensional binary table");
384 SetInt("NAXIS1", 0, "width of table in bytes");
385 SetInt("NAXIS2", 0, "number of rows in table");
386 SetInt("PCOUNT", 0, "size of special data area");
387 SetInt("GCOUNT", 1, "one data group (required keyword)");
388 SetInt("TFIELDS", 0, "number of fields in each row");
389 if (addEXTNAMEKey)
390 SetStr("EXTNAME", "", "name of extension table");
391 else
392 fManualExtName = true;
393 SetStr("CHECKSUM", "0000000000000000", "Checksum for the whole HDU");
394 SetStr("DATASUM", " 0", "Checksum for the data block");
395
396 std::ofstream::open(filename);
397 }
398
399 void AllowCommentsTrimming(bool allow)
400 {
401 fCommentTrimming = allow;
402 }
403 //Etienne: required to enable 1 to 1 reconstruction of files
404 bool SetKeyComment(const std::string& key, const std::string& comment)
405 {
406 auto it = findkey(key);
407 if (it==fKeys.end())
408 return false;
409 it->comment = comment;
410 it->changed = true;
411 return true;
412 }
413 bool SetKeyFromFitsString(const std::string& fitsString)
414 {
415 if (fTable.num_rows>0)
416 {
417 std::ostringstream sout;
418 sout << "No new header key can be defined, rows were already written to the file... ignoring new key '" << fitsString << "'";
419#ifdef __EXCEPTIONS
420 throw std::runtime_error(sout.str());
421#else
422 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
423 return false;
424#endif
425 }
426
427 Key entry;
428 entry.fitsString = fitsString;
429 entry.changed = true;
430 fKeys.push_back(entry);
431 return true;
432 }
433 bool SetRaw(const std::string &key, const std::string &val, const std::string &comment)
434 {
435 return Set(key, true, val, comment);
436 }
437
438 bool SetBool(const std::string &key, bool b, const std::string &comment="")
439 {
440 return Set(key, true, b?"T":"F", comment);
441 }
442
443 bool AddEmpty(const std::string &key, const std::string &comment="")
444 {
445 return Set(key, true, "", comment);
446 }
447
448 bool SetStr(const std::string &key, std::string s, const std::string &comment="")
449 {
450 for (uint i=0; i<s.length(); i++)
451 if (s[i]=='\'')
452 s.insert(i++, "\'");
453
454 return Set(key, true, "'"+s+"'", comment);
455 }
456
457 bool SetInt(const std::string &key, int64_t i, const std::string &comment="")
458 {
459 std::ostringstream sout;
460 sout << i;
461
462 return Set(key, true, sout.str(), comment);
463 }
464
465 bool SetFloat(const std::string &key, double f, int p, const std::string &comment="")
466 {
467 std::ostringstream sout;
468
469 if (p<0)
470 sout << std::setprecision(-p) << fixed;
471 if (p>0)
472 sout << std::setprecision(p);
473 if (p==0)
474 sout << std::setprecision(f>1e-100 && f<1e100 ? 15 : 14);
475
476 sout << f;
477
478 std::string str = sout.str();
479
480 replace(str.begin(), str.end(), 'e', 'E');
481
482 if (str.find_first_of('E')==std::string::npos && str.find_first_of('.')==std::string::npos)
483 str += ".";
484
485 return Set(key, true, str, comment);
486 }
487
488 bool SetFloat(const std::string &key, double f, const std::string &comment="")
489 {
490 return SetFloat(key, f, 0, comment);
491 }
492
493 bool SetHex(const std::string &key, uint64_t i, const std::string &comment="")
494 {
495 std::ostringstream sout;
496 sout << std::hex << "0x" << i;
497 return SetStr(key, sout.str(), comment);
498 }
499
500 bool AddComment(const std::string &comment)
501 {
502 return Set("COMMENT", false, "", comment);
503 }
504
505 bool AddHistory(const std::string &comment)
506 {
507 return Set("HISTORY", false, "", comment);
508 }
509
510 void End()
511 {
512 Set("END");
513 while (fKeys.size()%36!=0)
514 fKeys.emplace_back();
515 }
516
517 std::string CommentFromType(char type)
518 {
519 std::string comment;
520
521 switch (type)
522 {
523 case 'L': comment = "[1-byte BOOL]"; break;
524 case 'A': comment = "[1-byte CHAR]"; break;
525 case 'B': comment = "[1-byte BOOL]"; break;
526 case 'I': comment = "[2-byte INT]"; break;
527 case 'J': comment = "[4-byte INT]"; break;
528 case 'K': comment = "[8-byte INT]"; break;
529 case 'E': comment = "[4-byte FLOAT]"; break;
530 case 'D': comment = "[8-byte FLOAT]"; break;
531 case 'Q': comment = "[var. Length]"; break;
532 }
533
534 return comment;
535 }
536
537 uint32_t SizeFromType(char type)
538 {
539 size_t size = 0;
540
541 switch (type)
542 {
543 case 'L': size = 1; break;
544 case 'A': size = 1; break;
545 case 'B': size = 1; break;
546 case 'I': size = 2; break;
547 case 'J': size = 4; break;
548 case 'K': size = 8; break;
549 case 'E': size = 4; break;
550 case 'D': size = 8; break;
551 case 'Q': size = 16; break;
552 }
553
554 return size;
555 }
556 //ETIENNE to be able to restore the file 1 to 1, I must restore the header keys myself
557 virtual bool AddColumn(uint32_t cnt, char typechar, const std::string &name, const std::string &unit, const std::string &comment="", bool addHeaderKeys=true)
558 {
559 if (tellp()<0)
560 {
561 std::ostringstream sout;
562 sout << "File not open... ignoring column '" << name << "'";
563#ifdef __EXCEPTIONS
564 throw std::runtime_error(sout.str());
565#else
566 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
567 return false;
568#endif
569 }
570
571 if (tellp()>0)
572 {
573 std::ostringstream sout;
574 sout << "Header already written, no new column can be defined... ignoring column '" << name << "'";
575#ifdef __EXCEPTIONS
576 throw std::runtime_error(sout.str());
577#else
578 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
579 return false;
580#endif
581 }
582
583 if (findcol(name)!=fTable.cols.cend())
584 {
585 std::ostringstream sout;
586 sout << "A column with the name '" << name << "' already exists.";
587#ifdef __EXCEPTIONS
588 throw std::runtime_error(sout.str());
589#else
590 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
591 return false;
592#endif
593 }
594
595 typechar = toupper(typechar);
596
597 static const std::string allow("LABIJKEDQ");
598#if GCC_VERSION < 40603
599 if (std::find(allow.begin(), allow.end(), typechar)==allow.end())
600#else
601 if (std::find(allow.cbegin(), allow.cend(), typechar)==allow.end())
602#endif
603 {
604 std::ostringstream sout;
605 sout << "Column type '" << typechar << "' not supported.";
606#ifdef __EXCEPTIONS
607 throw std::runtime_error(sout.str());
608#else
609 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
610 return false;
611#endif
612 }
613
614 std::ostringstream type;
615 type << cnt;
616 if (typechar=='Q')
617 type << "QB";
618 else
619 type << typechar;
620
621 fTable.num_cols++;
622
623 if (addHeaderKeys)
624 {
625#if GCC_VERSION < 40603
626 const std::string nc = std::to_string((long long int)(fTable.num_cols));
627#else
628 const std::string nc = std::to_string(fTable.num_cols);
629#endif
630 SetStr("TFORM"+nc, type.str(), "format of "+name+" "+CommentFromType(typechar));
631 SetStr("TTYPE"+nc, name, comment);
632 if (!unit.empty())
633 SetStr("TUNIT"+nc, unit, "unit of "+name);
634 }
635 size_t size = SizeFromType(typechar);
636
637 Table::Column col;
638
639 col.name = name;
640 col.type = typechar;
641 col.num = cnt;
642 col.size = size;
643 col.offset = fTable.bytes_per_row;
644
645 fTable.cols.push_back(col);
646
647 fTable.bytes_per_row += size*cnt;
648
649 // Align to four bytes
650 fOutputBuffer.resize(fTable.bytes_per_row + 4-fTable.bytes_per_row%4);
651
652 return true;
653 }
654
655 virtual bool AddColumn(const FITS::Compression&, uint32_t cnt, char typechar, const std::string& name, const std::string& unit, const std::string& comment="", bool addHeaderKeys=true)
656 {
657 return AddColumn(cnt, typechar, name, unit, comment, addHeaderKeys);
658 }
659
660 bool AddColumnShort(uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
661 { return AddColumn(cnt, 'I', name, unit, comment); }
662 bool AddColumnInt(uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
663 { return AddColumn(cnt, 'J', name, unit, comment); }
664 bool AddColumnLong(uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
665 { return AddColumn(cnt, 'K', name, unit, comment); }
666 bool AddColumnFloat(uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
667 { return AddColumn(cnt, 'E', name, unit, comment); }
668 bool AddColumnDouble(uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
669 { return AddColumn(cnt, 'D', name, unit, comment); }
670 bool AddColumnChar(uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
671 { return AddColumn(cnt, 'A', name, unit, comment); }
672 bool AddColumnByte(uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
673 { return AddColumn(cnt, 'B', name, unit, comment); }
674 bool AddColumnBool(uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
675 { return AddColumn(cnt, 'L', name, unit, comment); }
676
677 bool AddColumnShort(const std::string &name, const std::string &unit="", const std::string &comment="")
678 { return AddColumn(1, 'I', name, unit, comment); }
679 bool AddColumnInt(const std::string &name, const std::string &unit="", const std::string &comment="")
680 { return AddColumn(1, 'J', name, unit, comment); }
681 bool AddColumnLong(const std::string &name, const std::string &unit="", const std::string &comment="")
682 { return AddColumn(1, 'K', name, unit, comment); }
683 bool AddColumnFloat(const std::string &name, const std::string &unit="", const std::string &comment="")
684 { return AddColumn(1, 'E', name, unit, comment); }
685 bool AddColumnDouble(const std::string &name, const std::string &unit="", const std::string &comment="")
686 { return AddColumn(1, 'D', name, unit, comment); }
687 bool AddColumnChar(const std::string &name, const std::string &unit="", const std::string &comment="")
688 { return AddColumn(1, 'A', name, unit, comment); }
689 bool AddColumnByte(const std::string &name, const std::string &unit="", const std::string &comment="")
690 { return AddColumn(1, 'B', name, unit, comment); }
691 bool AddColumnBool(const std::string &name, const std::string &unit="", const std::string &comment="")
692 { return AddColumn(1, 'L', name, unit, comment); }
693
694 bool AddColumnShort(const FITS::Compression &comp, uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
695 { return AddColumn(comp, cnt, 'I', name, unit, comment); }
696 bool AddColumnInt(const FITS::Compression &comp, uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
697 { return AddColumn(comp, cnt, 'J', name, unit, comment); }
698 bool AddColumnLong(const FITS::Compression &comp, uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
699 { return AddColumn(comp, cnt, 'K', name, unit, comment); }
700 bool AddColumnFloat(const FITS::Compression &comp, uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
701 { return AddColumn(comp, cnt, 'E', name, unit, comment); }
702 bool AddColumnDouble(const FITS::Compression &comp, uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
703 { return AddColumn(comp, cnt, 'D', name, unit, comment); }
704 bool AddColumnChar(const FITS::Compression &comp, uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
705 { return AddColumn(comp, cnt, 'A', name, unit, comment); }
706 bool AddColumnByte(const FITS::Compression &comp, uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
707 { return AddColumn(comp, cnt, 'B', name, unit, comment); }
708 bool AddColumnBool(const FITS::Compression &comp, uint32_t cnt, const std::string &name, const std::string &unit="", const std::string &comment="")
709 { return AddColumn(comp, cnt, 'L', name, unit, comment); }
710
711 bool AddColumnShort(const FITS::Compression &comp, const std::string &name, const std::string &unit="", const std::string &comment="")
712 { return AddColumn(comp, 1, 'I', name, unit, comment); }
713 bool AddColumnInt(const FITS::Compression &comp, const std::string &name, const std::string &unit="", const std::string &comment="")
714 { return AddColumn(comp, 1, 'J', name, unit, comment); }
715 bool AddColumnLong(const FITS::Compression &comp, const std::string &name, const std::string &unit="", const std::string &comment="")
716 { return AddColumn(comp, 1, 'K', name, unit, comment); }
717 bool AddColumnFloat(const FITS::Compression &comp, const std::string &name, const std::string &unit="", const std::string &comment="")
718 { return AddColumn(comp, 1, 'E', name, unit, comment); }
719 bool AddColumnDouble(const FITS::Compression &comp, const std::string &name, const std::string &unit="", const std::string &comment="")
720 { return AddColumn(comp, 1, 'D', name, unit, comment); }
721 bool AddColumnChar(const FITS::Compression &comp, const std::string &name, const std::string &unit="", const std::string &comment="")
722 { return AddColumn(comp, 1, 'A', name, unit, comment); }
723 bool AddColumnByte(const FITS::Compression &comp, const std::string &name, const std::string &unit="", const std::string &comment="")
724 { return AddColumn(comp, 1, 'B', name, unit, comment); }
725 bool AddColumnBool(const FITS::Compression &comp, const std::string &name, const std::string &unit="", const std::string &comment="")
726 { return AddColumn(comp, 1, 'L', name, unit, comment); }
727
728 /*
729 bool AddKey(string key, double d, const std::string &comment)
730 {
731 ostringstream out;
732 out << d;
733
734 std::string s = out.str();
735
736 replace(s.begin(), s.end(), "e", "E");
737
738 return AddKey(key, s, comment);
739 }*/
740
741
742 Checksum WriteHeader(std::ofstream &fout)
743 {
744 Checksum sum;
745 uint32_t count=0;
746 for (auto it=fKeys.begin(); it!=fKeys.end(); it++)
747 {
748 it->Out(fout);
749 sum += it->checksum;
750 count++;
751 }
752 fout.flush();
753
754 return sum;
755 }
756
757 Checksum WriteHeader()
758 {
759 return WriteHeader(*this);
760 }
761
762 void FlushHeader()
763 {
764 const off_t pos = tellp();
765 WriteHeader();
766 seekp(pos);
767 }
768
769 Checksum WriteFitsHeader()
770 {
771 ofits h;
772
773 h.SetBool("SIMPLE", true, "file does conform to FITS standard");
774 h.SetInt ("BITPIX", 8, "number of bits per data pixel");
775 h.SetInt ("NAXIS", 0, "number of data axes");
776 h.SetBool("EXTEND", true, "FITS dataset may contain extensions");
777 h.SetStr ("CHECKSUM","0000000000000000", "Checksum for the whole HDU");
778 h.SetStr ("DATASUM", " 0", "Checksum for the data block");
779 h.AddComment("FITS (Flexible Image Transport System) format is defined in 'Astronomy");
780 h.AddComment("and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H");
781 h.End();
782
783 const Checksum sum = h.WriteHeader(*this);
784
785 h.SetStr("CHECKSUM", sum.str());
786
787 const size_t offset = tellp();
788 h.WriteHeader(*this);
789 seekp(offset);
790
791 return sum;
792 }
793
794 virtual bool WriteDrsOffsetsTable ()
795 {
796 return true;
797 }
798
799 virtual bool WriteCatalog()
800 {
801 return true;
802 }
803
804 virtual bool WriteTableHeader(const char *name="DATA")
805 {
806 if (tellp()>0)
807 {
808#ifdef __EXCEPTIONS
809 throw std::runtime_error("File not empty anymore.");
810#else
811 gLog << ___err___ << "ERROR - File not empty anymore." << std::endl;
812 return false;
813#endif
814 }
815
816 fHeaderSum = WriteFitsHeader();
817
818 WriteDrsOffsetsTable();
819
820 if (!fManualExtName)
821 SetStr("EXTNAME", name);
822 SetInt("NAXIS1", fTable.bytes_per_row);
823 SetInt("TFIELDS", fTable.cols.size());
824
825 End();
826
827 WriteHeader();
828
829 WriteCatalog();
830
831 return good();
832 }
833
834 template<size_t N>
835 void revcpy(char *dest, const char *src, int num)
836 {
837 const char *pend = src + num*N;
838 for (const char *ptr = src; ptr<pend; ptr+=N, dest+=N)
839 std::reverse_copy(ptr, ptr+N, dest);
840 }
841
842 virtual uint32_t GetBytesPerRow() const { return fTable.bytes_per_row; }
843
844 virtual bool WriteRow(const void *ptr, size_t cnt, bool byte_swap=true)
845 {
846 // FIXME: Make sure that header was already written
847 // or write header now!
848 if (cnt!=fTable.bytes_per_row)
849 {
850 std::ostringstream sout;
851 sout << "WriteRow - Size " << cnt << " does not match expected size " << fTable.bytes_per_row;
852#ifdef __EXCEPTIONS
853 throw std::runtime_error(sout.str());
854#else
855 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
856 return false;
857#endif
858 }
859
860 // For the checksum we need everything to be correctly aligned
861 const uint8_t offset = fTable.offset%4;
862
863 char *buffer = fOutputBuffer.data() + offset;
864
865 auto ib = fOutputBuffer.begin();
866 auto ie = fOutputBuffer.rbegin();
867 *ib++ = 0;
868 *ib++ = 0;
869 *ib++ = 0;
870 *ib = 0;
871
872 *ie++ = 0;
873 *ie++ = 0;
874 *ie++ = 0;
875 *ie = 0;
876
877 if (!byte_swap)
878 memcpy(buffer, ptr, cnt);
879 else
880 {
881 for (auto it=fTable.cols.cbegin(); it!=fTable.cols.cend(); it++)
882 {
883 const char *src = reinterpret_cast<const char*>(ptr) + it->offset;
884 char *dest = buffer + it->offset;
885
886 // Let the compiler do some optimization by
887 // knowing the we only have 1, 2, 4 and 8
888 switch (it->size)
889 {
890 case 1: memcpy (dest, src, it->num*it->size); break;
891 case 2: revcpy<2>(dest, src, it->num); break;
892 case 4: revcpy<4>(dest, src, it->num); break;
893 case 8: revcpy<8>(dest, src, it->num); break;
894 }
895 }
896 }
897
898 write(buffer, cnt);
899 fDataSum.add(fOutputBuffer);
900
901 fTable.num_rows++;
902 fTable.offset += cnt;
903 return good();
904 }
905
906 template<typename N>
907 bool WriteRow(const std::vector<N> &vec)
908 {
909 return WriteRow(vec.data(), vec.size()*sizeof(N));
910 }
911
912 // Flushes the number of rows to the header on disk
913 virtual void FlushNumRows()
914 {
915 SetInt("NAXIS2", fTable.num_rows);
916 FlushHeader();
917 }
918
919 size_t GetNumRows() const { return fTable.num_rows; }
920
921 void AlignTo2880Bytes()
922 {
923 if (tellp()%(80*36)>0)
924 {
925 std::vector<char> filler(80*36-tellp()%(80*36));
926 write(filler.data(), filler.size());
927 }
928 }
929
930 Checksum UpdateHeaderChecksum()
931 {
932
933 std::ostringstream dataSumStr;
934 dataSumStr << fDataSum.val();
935 SetStr("DATASUM", dataSumStr.str());
936
937 const Checksum sum = WriteHeader();
938
939 //sum += headersum;
940
941 SetStr("CHECKSUM", (sum+fDataSum).str());
942
943 return WriteHeader();
944 }
945 virtual bool close()
946 {
947 if (tellp()<0)
948 return false;
949
950 AlignTo2880Bytes();
951
952 // We don't have to jump back to the end of the file
953 SetInt("NAXIS2", fTable.num_rows);
954
955
956 const Checksum chk = UpdateHeaderChecksum();
957
958 std::ofstream::close();
959
960 if ((chk+fDataSum).valid())
961 return true;
962
963 std::ostringstream sout;
964 sout << "Checksum (" << std::hex << chk.val() << ") invalid.";
965#ifdef __EXCEPTIONS
966 throw std::runtime_error(sout.str());
967#else
968 gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
969 return false;
970#endif
971 }
972
973 std::pair<std::string, int> GetChecksumData()
974 {
975 std::string datasum;
976 std::string checksum;
977 //cannot directly use the Get methods, because they are only in fits.h
978 for (std::vector<Key>::const_iterator it=fKeys.cbegin(); it!= fKeys.cend(); it++)
979 {
980 if (it->key == "CHECKSUM") checksum = it->value;
981 if (it->key == "DATASUM") datasum = it->value;
982 }
983 if (checksum[0] == '\'')
984 checksum = checksum.substr(1,checksum.size()-2);
985 if (datasum[0] == '\'')
986 datasum = datasum.substr(1, datasum.size()-2);
987 return std::make_pair(checksum, atoi(datasum.c_str()));
988 }
989
990 void SetDefaultKeys()
991 {
992 SetStr("TELESCOP", "FACT", "Telescope that acquired this data");
993 SetStr("CREATOR", typeid(*this).name(), "Class that wrote this file");
994 SetFloat("EXTREL", 1.0, "Release Number");
995 SetStr("COMPILED", __DATE__" " __TIME__, "Compile time");
996 SetStr("ORIGIN", "FACT", "Institution that wrote the file");
997 SetStr("TIMESYS", "UTC", "Time system");
998 SetStr("TIMEUNIT", "d", "Time given in days w.r.t. to MJDREF");
999 SetInt("MJDREF", 40587, "MJD to UNIX time (seconds since 1970/1/1)");
1000 SetStr("PACKAGE", PACKAGE_NAME, "Package name");
1001 SetStr("VERSION", PACKAGE_VERSION, "Package description");
1002 SetStr("REVISION", REVISION, "SVN revision");
1003
1004 const time_t t0 = time(NULL);
1005 const struct tm *tmp1 = gmtime(&t0);
1006
1007 std::string str(19, '\0');
1008 if (tmp1 && strftime(const_cast<char*>(str.data()), 20, "%Y-%m-%dT%H:%M:%S", tmp1))
1009 SetStr("DATE", str, "File creation date");
1010 }
1011};
1012
1013#if 0
1014#include "fits.h"
1015
1016int main()
1017{
1018 using namespace std;
1019
1020 ofits h2("delme.fits");
1021
1022 h2.SetInt("KEY1", 1, "comment 1");
1023 h2.AddColumnInt(2, "testcol1", "counts", "My comment");
1024 h2.AddColumnInt("testcol2", "counts", "My comment");
1025 //h2.AddColumnInt("testcol2", "counts", "My comment");
1026 h2.SetInt("KEY2", 2, "comment 2");
1027
1028 /*
1029 AddFloat("X0", 0.000123456, "number of fields in each row");
1030 AddFloat("X1", 0, "number of fields in each row");
1031 AddFloat("X2", 12345, "number of fields in each row");
1032 AddFloat("X3", 123456.67890, "number of fields in each row");
1033 AddFloat("X4", 1234567890123456789.12345678901234567890, "number of fields in each row");
1034 AddFloat("X5", 1234567890.1234567890e20, "number of fields in each row");
1035 AddFloat("X6", 1234567890.1234567890e-20, "number of fields in each row");
1036 AddFloat("XB", 1234567890.1234567890e-111, "number of fields in each row");
1037 AddFloat("X7", 1e-5, "number of fields in each row");
1038 AddFloat("X8", 1e-6, "number of fields in each row");
1039 //AddStr("12345678", "123456789012345678", "12345678901234567890123456789012345678901234567");
1040 */
1041 // -
1042
1043 h2.WriteTableHeader("TABLE_NAME");
1044
1045 for (int i=0; i<10; i++)
1046 {
1047 int j[3] = { i+10, i*10, i*100 };
1048 h2.WriteRow(j, 3*sizeof(i));
1049 }
1050
1051 //h2.AddColumnInt("testcol2", "counts", "My comment");
1052 //h2.SetInt("KEY3", 2, "comment 2");
1053 h2.SetInt("KEY2", 2, "comment 2xxx");
1054 h2.SetInt("KEY1", 11);
1055
1056 h2.close();
1057
1058 cout << "---" << endl;
1059
1060 fits f("delme.fits");
1061 if (!f)
1062 throw std::runtime_error("xxx");
1063
1064 cout << "Header is valid: " << f.IsHeaderOk() << endl;
1065
1066 while (f.GetNextRow());
1067
1068 cout << "File is valid: " << f.IsFileOk() << endl;
1069
1070 cout << "---" << endl;
1071
1072 return 0;
1073}
1074#endif
1075
1076#endif
Note: See TracBrowser for help on using the repository browser.