source: trunk/Mars/mcore/fits.h@ 16889

Last change on this file since 16889 was 16889, checked in by tbretz, 11 years ago
Some cleaning to easier understand the code.
File size: 29.5 KB
Line 
1#ifndef MARS_fits
2#define MARS_fits
3
4#ifdef __CINT__
5#define int8_t Char_t
6#define int16_t Short_t
7#define int32_t Int_t
8#define int64_t Long64_t
9#define uint8_t UChar_t
10#define uint16_t UShort_t
11#define uint32_t UInt_t
12#define uint64_t ULong64_t
13#else
14#include <stdint.h>
15#endif
16
17#include <map>
18#include <string>
19#include <fstream>
20#include <sstream>
21#include <algorithm>
22
23#ifdef __EXCEPTIONS
24#include <stdexcept>
25#endif
26
27#ifdef __CINT__
28#define off_t size_t
29#endif
30
31#if !defined(__MARS__) && !defined(__CINT__)
32#include <unordered_map>
33#endif
34
35#ifndef __MARS__
36#include <vector>
37#include <iomanip>
38#include <iostream>
39#define gLog cerr
40#define ___err___ ""
41#define ___warn___ ""
42#define ___all___ ""
43#else
44#include "MLog.h"
45#include "MLogManip.h"
46#define ___err___ err
47#define ___warn___ warn
48#define ___all___ all
49#endif
50
51#if defined(HAVE_ZLIB) || defined(__CINT__)
52#include "izstream.h"
53#else
54#include <fstream>
55#define izstream ifstream
56#warning Support for zipped FITS files disabled.
57#endif
58
59#include "checksum.h"
60
61#ifndef __MARS__
62namespace std
63{
64#else
65using namespace std;
66#endif
67
68class fits : public izstream
69{
70public:
71 //I know I know, you're going to yiell that this does not belong here.
72 //It will belong in the global scope eventually, and it makes the coding of zfits much simpler this way.
73 enum Compression_t
74 {
75 kCompUnknown,
76 kCompFACT
77 };
78
79 struct Entry
80 {
81 char type;
82 string value;
83 string comment;
84 string fitsString;
85
86 template<typename T>
87 T Get() const
88 {
89 T t;
90
91 istringstream str(value);
92 str >> t;
93
94 return t;
95 }
96 };
97
98 struct Table
99 {
100 off_t offset;
101
102 bool isCompressed;
103
104 string name;
105 size_t bytes_per_row;
106 size_t num_rows;
107 size_t num_cols;
108 size_t total_bytes; // NAXIS1*NAXIS2
109
110 struct Column
111 {
112 size_t offset;
113 size_t num;
114 size_t size;
115 size_t bytes; // num*size
116 char type;
117 string unit;
118 Compression_t comp;
119 };
120
121 typedef map<string, Entry> Keys;
122 typedef map<string, Column> Columns;
123 typedef vector<Column> SortedColumns;
124
125 Columns cols;
126 SortedColumns sortedCols;
127 Keys keys;
128
129 int64_t datasum;
130
131 string Trim(const string &str, char c=' ') const
132 {
133 // Trim Both leading and trailing spaces
134 const size_t pstart = str.find_first_not_of(c); // Find the first character position after excluding leading blank spaces
135 const size_t pend = str.find_last_not_of(c); // Find the first character position from reverse af
136
137 // if all spaces or empty return an empty string
138 if (string::npos==pstart || string::npos==pend)
139 return string();
140
141 return str.substr(pstart, pend-pstart+1);
142 }
143
144 bool Check(const string &key, char type, const string &value="") const
145 {
146 const Keys::const_iterator it = keys.find(key);
147 if (it==keys.end())
148 {
149 ostringstream str;
150 str << "Key '" << key << "' not found.";
151#ifdef __EXCEPTIONS
152 throw runtime_error(str.str());
153#else
154 gLog << ___err___ << "ERROR - " << str.str() << endl;
155 return false;
156#endif
157 }
158
159 if (it->second.type!=type)
160 {
161 ostringstream str;
162 str << "Wrong type for key '" << key << "': expected " << type << ", found " << it->second.type << ".";
163#ifdef __EXCEPTIONS
164 throw runtime_error(str.str());
165#else
166 gLog << ___err___ << "ERROR - " << str.str() << endl;
167 return false;
168#endif
169 }
170
171 if (!value.empty() && it->second.value!=value)
172 {
173 ostringstream str;
174 str << "Wrong value for key '" << key << "': expected " << value << ", found " << it->second.value << ".";
175#ifdef __EXCEPTIONS
176 throw runtime_error(str.str());
177#else
178 gLog << ___err___ << "ERROR - " << str.str() << endl;
179 return false;
180#endif
181 }
182
183 return true;
184 }
185
186 Keys ParseBlock(const vector<string> &vec) const
187 {
188 map<string,Entry> rc;
189
190 for (unsigned int i=0; i<vec.size(); i++)
191 {
192 const string key = Trim(vec[i].substr(0,8));
193 // Keywords without a value, like COMMENT / HISTORY
194 if (vec[i].substr(8,2)!="= ")
195 continue;
196
197 char type = 0;
198
199 string com;
200 string val = Trim(vec[i].substr(10));
201
202 if (val[0]=='\'')
203 {
204 // First skip all '' in the string
205 size_t p = 1;
206 while (1)
207 {
208 const size_t pp = val.find_first_of('\'', p);
209 if (pp==string::npos)
210 break;
211
212 p = val[pp+1]=='\'' ? pp+2 : pp+1;
213 }
214
215 // Now find the comment
216 const size_t ppp = val.find_first_of('/', p);
217
218 // Set value, comment and type
219 // comments could be just spaces. take care of this.
220 if (ppp!=string::npos && val.size() != ppp+1)
221 com = Trim(val.substr(ppp+1));
222
223 val = Trim(val.substr(1, p-2));
224 type = 'T';
225 }
226 else
227 {
228 const size_t p = val.find_first_of('/');
229
230 if (val.size() != p+1)
231 com = Trim(val.substr(p+2));
232
233 val = Trim(val.substr(0, p));
234
235 if (val.empty() || val.find_first_of('T')!=string::npos || val.find_first_of('F')!=string::npos)
236 type = 'B';
237 else
238 type = val.find_last_of('.')==string::npos ? 'I' : 'F';
239 }
240
241 const Entry e = { type, val, com, vec[i] };
242
243 rc[key] = e;
244 }
245
246 return rc;
247 }
248
249 Table() : offset(0) { }
250 Table(const vector<string> &vec, off_t off) : offset(off),
251 isCompressed(false), keys(ParseBlock(vec))
252 {
253 if (HasKey("ZTABLE") && Check("ZTABLE", 'B', "T"))
254 isCompressed = true;
255
256 if (!Check("XTENSION", 'T', "BINTABLE") ||
257 !Check("NAXIS", 'I', "2") ||
258 !Check("BITPIX", 'I', "8") ||
259 !Check("GCOUNT", 'I', "1") ||
260 !Check("EXTNAME", 'T') ||
261 !Check("NAXIS1", 'I') ||
262 !Check("NAXIS2", 'I') ||
263 !Check("TFIELDS", 'I'))
264 return;
265
266 if (isCompressed)
267 {
268 if (!Check("ZNAXIS1", 'I') ||
269 !Check("ZNAXIS2", 'I') ||
270 !Check("ZPCOUNT", 'I', "0"))
271 return;
272 }
273 else
274 {
275 if (!Check("PCOUNT", 'I', "0"))
276 return;
277 }
278
279 total_bytes = Get<size_t>("NAXIS1")*Get<size_t>("NAXIS2");
280 bytes_per_row = isCompressed ? Get<size_t>("ZNAXIS1") : Get<size_t>("NAXIS1");
281 num_rows = isCompressed ? Get<size_t>("ZNAXIS2") : Get<size_t>("NAXIS2");
282 num_cols = Get<size_t>("TFIELDS");
283 datasum = isCompressed ? Get<int64_t>("ZDATASUM", -1) : Get<int64_t>("DATASUM", -1);
284
285 size_t bytes = 0;
286
287 const string tFormName = isCompressed ? "ZFORM" : "TFORM";
288 for (size_t i=1; i<=num_cols; i++)
289 {
290 ostringstream num;
291 num << i;
292
293 if (!Check("TTYPE"+num.str(), 'T') ||
294 !Check(tFormName+num.str(), 'T'))
295 return;
296
297 const string id = Get<string>("TTYPE"+num.str());
298 const string fmt = Get<string>(tFormName+num.str());
299 const string unit = Get<string>("TUNIT"+num.str(), "");
300 const string comp = Get<string>("ZCTYP"+num.str(), "");
301
302 Compression_t compress = kCompUnknown;
303 if (comp == "FACT")
304 compress = kCompFACT;
305
306 istringstream sin(fmt);
307 int n = 0;
308 sin >> n;
309 if (!sin)
310 n = 1;
311
312 const char type = fmt[fmt.length()-1];
313
314 size_t size = 0;
315 switch (type)
316 {
317 // We could use negative values to mark floats
318 // otheriwse we could just cast them to int64_t?
319 case 'L': // logical
320 case 'A': // char
321 case 'B': size = 1; break; // byte
322 case 'I': size = 2; break; // short
323 case 'J': size = 4; break; // int
324 case 'K': size = 8; break; // long long
325 case 'E': size = 4; break; // float
326 case 'D': size = 8; break; // double
327 // case 'X': size = n; break; // bits (n=number of bytes needed to contain all bits)
328 // case 'C': size = 8; break; // complex float
329 // case 'M': size = 16; break; // complex double
330 // case 'P': size = 8; break; // array descriptor (32bit)
331 // case 'Q': size = 16; break; // array descriptor (64bit)
332 default:
333 {
334 ostringstream str;
335 str << "FITS format TFORM='" << fmt << "' not yet supported.";
336#ifdef __EXCEPTIONS
337 throw runtime_error(str.str());
338#else
339 gLog << ___err___ << "ERROR - " << str.str() << endl;
340 return;
341#endif
342 }
343 }
344
345 const Table::Column col = { bytes, n, size, n*size, type, unit, compress};
346
347 cols[id] = col;
348 sortedCols.push_back(col);
349 bytes += n*size;
350 }
351
352 if (bytes!=bytes_per_row)
353 {
354#ifdef __EXCEPTIONS
355 throw runtime_error("Column size mismatch");
356#else
357 gLog << ___err___ << "ERROR - Column size mismatch" << endl;
358 return;
359#endif
360 }
361
362 name = Get<string>("EXTNAME");
363 }
364
365 void PrintKeys(bool display_all=false) const
366 {
367 for (Keys::const_iterator it=keys.begin(); it!=keys.end(); it++)
368 {
369 if (!display_all &&
370 (it->first.substr(0, 6)=="TTYPE" ||
371 it->first.substr(0, 6)=="TFORM" ||
372 it->first.substr(0, 6)=="TUNIT" ||
373 it->first=="TFIELDS" ||
374 it->first=="XTENSION" ||
375 it->first=="NAXIS" ||
376 it->first=="BITPIX" ||
377 it->first=="PCOUNT" ||
378 it->first=="GCOUNT")
379 )
380 continue;
381
382 gLog << ___all___ << setw(2) << it->second.type << '|' << it->first << '=' << it->second.value << '/' << it->second.comment << '|' << endl;
383 }}
384
385 void PrintColumns() const
386 {
387 typedef map<pair<size_t, string>, Column> Sorted;
388
389 Sorted sorted;
390
391 for (Columns::const_iterator it=cols.begin(); it!=cols.end(); it++)
392 sorted[make_pair(it->second.offset, it->first)] = it->second;
393
394 for (Sorted::const_iterator it=sorted.begin(); it!=sorted.end(); it++)
395 {
396 gLog << ___all___ << setw(6) << it->second.offset << "| ";
397 gLog << it->second.num << 'x';
398 switch (it->second.type)
399 {
400 case 'A': gLog << "char(8)"; break;
401 case 'L': gLog << "bool(8)"; break;
402 case 'B': gLog << "byte(8)"; break;
403 case 'I': gLog << "short(16)"; break;
404 case 'J': gLog << "int(32)"; break;
405 case 'K': gLog << "int(64)"; break;
406 case 'E': gLog << "float(32)"; break;
407 case 'D': gLog << "double(64)"; break;
408 }
409 gLog << ": " << it->first.second << " [" << it->second.unit << "]" << endl;
410 }
411 }
412
413 operator bool() const { return !name.empty(); }
414
415 bool HasKey(const string &key) const
416 {
417 return keys.find(key)!=keys.end();
418 }
419
420 bool HasColumn(const string& col) const
421 {
422 return cols.find(col)!=cols.end();
423 }
424
425 const Columns &GetColumns() const
426 {
427 return cols;
428 }
429
430 const Keys &GetKeys() const
431 {
432 return keys;
433 }
434
435 // Values of keys are always signed
436 template<typename T>
437 T Get(const string &key) const
438 {
439 const map<string,Entry>::const_iterator it = keys.find(key);
440 if (it==keys.end())
441 {
442 ostringstream str;
443 str << "Key '" << key << "' not found." << endl;
444#ifdef __EXCEPTIONS
445 throw runtime_error(str.str());
446#else
447 gLog << ___err___ << "ERROR - " << str.str() << endl;
448 return T();
449#endif
450 }
451 return it->second.Get<T>();
452 }
453
454 // Values of keys are always signed
455 template<typename T>
456 T Get(const string &key, const T &deflt) const
457 {
458 const map<string,Entry>::const_iterator it = keys.find(key);
459 return it==keys.end() ? deflt :it->second.Get<T>();
460 }
461
462 size_t GetN(const string &key) const
463 {
464 const Columns::const_iterator it = cols.find(key);
465 return it==cols.end() ? 0 : it->second.num;
466 }
467
468 // There may be a gap between the main table and the start of the heap:
469 // this computes the offset
470 streamoff GetHeapShift() const
471 {
472 if (!HasKey("THEAP"))
473 return 0;
474
475 const size_t shift = Get<size_t>("THEAP");
476 return shift <= total_bytes ? 0 : shift - total_bytes;
477 }
478
479 // return total number of bytes 'all inclusive'
480 streamoff GetTotalBytes() const
481 {
482 //get offset of special data area from start of main table
483 const streamoff shift = GetHeapShift();
484
485 //and special data area size
486 const streamoff size = HasKey("PCOUNT") ? Get<streamoff>("PCOUNT") : 0;
487
488 // Get the total size
489 const streamoff total = total_bytes + size + shift;
490
491 // check for padding
492 if (total%2880==0)
493 return total;
494
495 // padding necessary
496 return total + (2880 - (total%2880));
497 }
498 };
499
500protected:
501 ofstream fCopy;
502
503 Table fTable;
504
505 typedef pair<void*, Table::Column> Address;
506 typedef vector<Address> Addresses;
507 //map<void*, Table::Column> fAddresses;
508 Addresses fAddresses;
509
510#if defined(__MARS__) || defined(__CINT__)
511 typedef map<string, void*> Pointers;
512#else
513 typedef unordered_map<string, void*> Pointers;
514#endif
515 Pointers fPointers;
516
517 vector<vector<char>> fGarbage;
518
519 vector<char> fBufferRow;
520 vector<char> fBufferDat;
521
522 size_t fRow;
523
524 Checksum fChkHeader;
525 Checksum fChkData;
526
527 bool ReadBlock(vector<string> &vec)
528 {
529 int endtag = 0;
530 for (int i=0; i<36; i++)
531 {
532 char c[81];
533 c[80] = 0;
534 read(c, 80);
535 if (!good())
536 break;
537
538 fChkHeader.add(c, 80);
539
540// if (c[0]==0)
541// return vector<string>();
542
543 string str(c);
544
545// if (!str.empty())
546// cout << setw(2) << i << "|" << str << "|" << (endtag?'-':'+') << endl;
547
548 if (endtag==2 || str=="END ")
549 {
550 endtag = 2; // valid END tag found
551 continue;
552 }
553
554 if (endtag==1 || str==" ")
555 {
556 endtag = 1; // end tag not found, but expected to be there
557 continue;
558 }
559
560 vec.push_back(str);
561 }
562
563 // Make sure that no empty vector is returned
564 if (endtag && vec.size()%36==0)
565 vec.emplace_back("END = '' / ");
566
567 return endtag==2;
568 }
569
570 string Compile(const string &key, int16_t i=-1) const
571 {
572 if (i<0)
573 return key;
574
575 ostringstream str;
576 str << key << i;
577 return str.str();
578 }
579
580 void Constructor(const string &fname, string fout, const string& tableName, bool force)
581 {
582 char simple[10];
583 read(simple, 10);
584 if (!good())
585 return;
586
587 if (memcmp(simple, "SIMPLE = ", 10))
588 {
589 clear(rdstate()|ios::badbit);
590#ifdef __EXCEPTIONS
591 throw runtime_error("File is not a FITS file.");
592#else
593 gLog << ___err___ << "ERROR - File is not a FITS file." << endl;
594 return;
595#endif
596 }
597
598 seekg(0);
599
600 while (good())
601 {
602 vector<string> block;
603 while (1)
604 {
605 // If we search for a table, we implicitly assume that
606 // not finding the table is not an error. The user
607 // can easily check that by eof() && !bad()
608 peek();
609 if (eof() && !bad() && !tableName.empty())
610 {
611 break;
612 }
613 // FIXME: Set limit on memory consumption
614 const int rc = ReadBlock(block);
615 if (!good())
616 {
617 clear(rdstate()|ios::badbit);
618#ifdef __EXCEPTIONS
619 throw runtime_error("FITS file corrupted.");
620#else
621 gLog << ___err___ << "ERROR - FITS file corrupted." << endl;
622 return;
623#endif
624 }
625
626 if (block.size()%36)
627 {
628 if (!rc && !force)
629 {
630 clear(rdstate()|ios::badbit);
631#ifdef __EXCEPTIONS
632 throw runtime_error("END keyword missing in FITS header.");
633#else
634 gLog << ___err___ << "ERROR - END keyword missing in FITS file... file might be corrupted." << endl;
635 return;
636#endif
637 }
638 break;
639 }
640 }
641
642 if (block.empty())
643 break;
644
645 if (block[0].substr(0, 9)=="SIMPLE =")
646 {
647 fChkHeader.reset();
648 continue;
649 }
650
651 if (block[0].substr(0, 9)=="XTENSION=")
652 {
653 fTable = Table(block, tellg());
654 fRow = (size_t)-1;
655
656 if (!fTable)
657 {
658 clear(rdstate()|ios::badbit);
659 return;
660 }
661
662 // Check for table name. Skip until eof or requested table are found.
663 // skip the current table?
664 if (!tableName.empty() && tableName!=fTable.Get<string>("EXTNAME"))
665 {
666 const streamoff skip = fTable.GetTotalBytes();
667 seekg(skip, ios_base::cur);
668
669 fChkHeader.reset();
670
671 continue;
672 }
673
674 fBufferRow.resize(fTable.bytes_per_row + 8-fTable.bytes_per_row%4);
675 fBufferDat.resize(fTable.bytes_per_row);
676
677 break;
678 }
679 }
680
681 if (fout.empty())
682 return;
683
684 if (*fout.rbegin()=='/')
685 {
686 const size_t p = fname.find_last_of('/');
687 fout.append(fname.substr(p+1));
688 }
689
690 fCopy.open(fout);
691 if (!fCopy)
692 {
693 clear(rdstate()|ios::badbit);
694#ifdef __EXCEPTIONS
695 throw runtime_error("Could not open output file.");
696#else
697 gLog << ___err___ << "ERROR - Failed to open output file." << endl;
698#endif
699 }
700
701 const streampos p = tellg();
702 seekg(0);
703
704 vector<char> buf(p);
705 read(buf.data(), p);
706
707 fCopy.write(buf.data(), p);
708 if (!fCopy)
709 clear(rdstate()|ios::badbit);
710 }
711
712public:
713 fits(const string &fname, const string& tableName="", bool force=false) : izstream(fname.c_str())
714 {
715 Constructor(fname, "", tableName, force);
716 }
717
718 fits(const string &fname, const string &fout, const string& tableName, bool force=false) : izstream(fname.c_str())
719 {
720 Constructor(fname, fout, tableName, force);
721 }
722
723 ~fits()
724 {
725 copy(istreambuf_iterator<char>(*this),
726 istreambuf_iterator<char>(),
727 ostreambuf_iterator<char>(fCopy));
728 }
729
730 virtual void StageRow(size_t row, char* dest)
731 {
732 // if (row!=fRow+1) // Fast seeking is ensured by izstream
733 seekg(fTable.offset+row*fTable.bytes_per_row);
734 read(dest, fTable.bytes_per_row);
735 //fin.clear(fin.rdstate()&~ios::eofbit);
736 }
737
738 virtual void WriteRowToCopyFile(size_t row)
739 {
740 if (row==fRow+1)
741 {
742 const uint8_t offset = (row*fTable.bytes_per_row)%4;
743
744 fChkData.add(fBufferRow);
745 if (fCopy.is_open() && fCopy.good())
746 fCopy.write(fBufferRow.data()+offset, fTable.bytes_per_row);
747 if (!fCopy)
748 clear(rdstate()|ios::badbit);
749 }
750 else
751 if (fCopy.is_open())
752 clear(rdstate()|ios::badbit);
753 }
754
755 void ZeroBufferForChecksum(vector<char>& vec, const uint64_t extraZeros=0)
756 {
757 auto ib = vec.begin();
758 auto ie = vec.end();
759
760 *ib++ = 0;
761 *ib++ = 0;
762 *ib++ = 0;
763 *ib = 0;
764
765 for (uint64_t i=0;i<extraZeros+8;i++)
766 *--ie = 0;
767 }
768
769 uint8_t ReadRow(size_t row)
770 {
771 // For the checksum we need everything to be correctly aligned
772 const uint8_t offset = (row*fTable.bytes_per_row)%4;
773
774 ZeroBufferForChecksum(fBufferRow);
775
776 StageRow(row, fBufferRow.data()+offset);
777
778 WriteRowToCopyFile(row);
779
780 fRow = row;
781
782 return offset;
783 }
784
785 template<size_t N>
786 void revcpy(char *dest, const char *src, const int &num)
787 {
788 const char *pend = src + num*N;
789 for (const char *ptr = src; ptr<pend; ptr+=N, dest+=N)
790 reverse_copy(ptr, ptr+N, dest);
791 }
792
793 virtual void MoveColumnDataToUserSpace(char *dest, const char *src, const Table::Column& c)
794 {
795 // Let the compiler do some optimization by
796 // knowing that we only have 1, 2, 4 and 8
797 switch (c.size)
798 {
799 case 1: memcpy (dest, src, c.bytes); break;
800 case 2: revcpy<2>(dest, src, c.num); break;
801 case 4: revcpy<4>(dest, src, c.num); break;
802 case 8: revcpy<8>(dest, src, c.num); break;
803 }
804 }
805
806#if !defined(__MARS__) && !defined(__CINT__)
807 virtual bool GetRow(size_t row, bool check=true)
808#else
809 virtual bool GetRowNum(size_t row, bool check=true)
810#endif
811 {
812 if (check && row>=fTable.num_rows)
813 return false;
814
815 const uint8_t offset = ReadRow(row);
816 if (!good())
817 return good();
818
819 const char *ptr = fBufferRow.data() + offset;
820
821 for (Addresses::const_iterator it=fAddresses.begin(); it!=fAddresses.end(); it++)
822 {
823 const Table::Column &c = it->second;
824
825 const char *src = ptr + c.offset;
826 char *dest = reinterpret_cast<char*>(it->first);
827
828 MoveColumnDataToUserSpace(dest, src, c);
829 }
830
831 return good();
832 }
833
834 bool GetNextRow(bool check=true)
835 {
836#if !defined(__MARS__) && !defined(__CINT__)
837 return GetRow(fRow+1, check);
838#else
839 return GetRowNum(fRow+1, check);
840#endif
841 }
842
843 virtual bool SkipNextRow()
844 {
845 seekg(fTable.offset+(++fRow)*fTable.bytes_per_row);
846 return good();
847 }
848
849 static bool Compare(const Address &p1, const Address &p2)
850 {
851 return p1.first>p2.first;
852 }
853
854 template<class T, class S>
855 const T &GetAs(const string &name)
856 {
857 return *reinterpret_cast<S*>(fPointers[name]);
858 }
859
860 void *SetPtrAddress(const string &name)
861 {
862 if (fTable.cols.count(name)==0)
863 {
864 ostringstream str;
865 str << "SetPtrAddress('" << name << "') - Column not found." << endl;
866#ifdef __EXCEPTIONS
867 throw runtime_error(str.str());
868#else
869 gLog << ___err___ << "ERROR - " << str.str() << endl;
870 return NULL;
871#endif
872 }
873
874 Pointers::const_iterator it = fPointers.find(name);
875 if (it!=fPointers.end())
876 return it->second;
877
878 fGarbage.emplace_back(fTable.cols[name].bytes);
879
880 void *ptr = fGarbage.back().data();
881
882 fPointers[name] = ptr;
883 fAddresses.emplace_back(ptr, fTable.cols[name]);
884 sort(fAddresses.begin(), fAddresses.end(), Compare);
885 return ptr;
886 }
887
888 template<typename T>
889 bool SetPtrAddress(const string &name, T *ptr, size_t cnt)
890 {
891 if (fTable.cols.count(name)==0)
892 {
893 ostringstream str;
894 str << "SetPtrAddress('" << name << "') - Column not found." << endl;
895#ifdef __EXCEPTIONS
896 throw runtime_error(str.str());
897#else
898 gLog << ___err___ << "ERROR - " << str.str() << endl;
899 return false;
900#endif
901 }
902
903 if (sizeof(T)!=fTable.cols[name].size)
904 {
905 ostringstream str;
906 str << "SetPtrAddress('" << name << "') - Element size mismatch: expected "
907 << fTable.cols[name].size << " from header, got " << sizeof(T) << endl;
908#ifdef __EXCEPTIONS
909 throw runtime_error(str.str());
910#else
911 gLog << ___err___ << "ERROR - " << str.str() << endl;
912 return false;
913#endif
914 }
915
916 if (cnt!=fTable.cols[name].num)
917 {
918 ostringstream str;
919 str << "SetPtrAddress('" << name << "') - Element count mismatch: expected "
920 << fTable.cols[name].num << " from header, got " << cnt << endl;
921#ifdef __EXCEPTIONS
922 throw runtime_error(str.str());
923#else
924 gLog << ___err___ << "ERROR - " << str.str() << endl;
925 return false;
926#endif
927 }
928
929 // if (fAddresses.count(ptr)>0)
930 // gLog << warn << "SetPtrAddress('" << name << "') - Pointer " << ptr << " already assigned." << endl;
931
932 //fAddresses[ptr] = fTable.cols[name];
933 fPointers[name] = ptr;
934 fAddresses.emplace_back(ptr, fTable.cols[name]);
935 sort(fAddresses.begin(), fAddresses.end(), Compare);
936 return true;
937 }
938
939 template<class T>
940 bool SetRefAddress(const string &name, T &ptr)
941 {
942 return SetPtrAddress(name, &ptr, sizeof(ptr)/sizeof(T));
943 }
944
945 template<typename T>
946 bool SetVecAddress(const string &name, vector<T> &vec)
947 {
948 return SetPtrAddress(name, vec.data(), vec.size());
949 }
950
951 template<typename T>
952 T Get(const string &key) const
953 {
954 return fTable.Get<T>(key);
955 }
956
957 template<typename T>
958 T Get(const string &key, const string &deflt) const
959 {
960 return fTable.Get<T>(key, deflt);
961 }
962
963 bool SetPtrAddress(const string &name, void *ptr)
964 {
965 if (fTable.cols.count(name)==0)
966 {
967 ostringstream str;
968 str <<"SetPtrAddress('" << name << "') - Column not found." << endl;
969#ifdef __EXCEPTIONS
970 throw runtime_error(str.str());
971#else
972 gLog << ___err___ << "ERROR - " << str.str() << endl;
973 return false;
974#endif
975 }
976
977 // if (fAddresses.count(ptr)>0)
978 // gLog << warn << "SetPtrAddress('" << name << "') - Pointer " << ptr << " already assigned." << endl;
979
980 //fAddresses[ptr] = fTable.cols[name];
981 fPointers[name] = ptr;
982 fAddresses.emplace_back(ptr, fTable.cols[name]);
983 sort(fAddresses.begin(), fAddresses.end(), Compare);
984 return true;
985 }
986
987 bool HasKey(const string &key) const { return fTable.HasKey(key); }
988 bool HasColumn(const string& col) const { return fTable.HasColumn(col);}
989 const Table::Columns &GetColumns() const { return fTable.GetColumns();}
990 const Table::SortedColumns& GetSortedColumns() const { return fTable.sortedCols;}
991 const Table::Keys &GetKeys() const { return fTable.GetKeys();}
992
993 int64_t GetInt(const string &key) const { return fTable.Get<int64_t>(key); }
994 uint64_t GetUInt(const string &key) const { return fTable.Get<uint64_t>(key); }
995 double GetFloat(const string &key) const { return fTable.Get<double>(key); }
996 string GetStr(const string &key) const { return fTable.Get<string>(key); }
997
998 size_t GetN(const string &key) const
999 {
1000 return fTable.GetN(key);
1001 }
1002
1003 size_t GetNumRows() const { return fTable.num_rows; }
1004 size_t GetRow() const { return fRow==(size_t)-1 ? 0 : fRow; }
1005
1006 operator bool() const { return fTable && fTable.offset!=0; }
1007
1008 void PrintKeys() const { fTable.PrintKeys(); }
1009 void PrintColumns() const { fTable.PrintColumns(); }
1010
1011 bool IsHeaderOk() const { return fTable.datasum<0?false:(fChkHeader+Checksum(fTable.datasum)).valid(); }
1012 bool IsFileOk() const { return (fChkHeader+fChkData).valid(); }
1013
1014 bool IsCompressedFITS() const { return fTable.isCompressed;}
1015};
1016
1017#ifndef __MARS__
1018};
1019#endif
1020#endif
Note: See TracBrowser for help on using the repository browser.