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

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