source: branches/Mars_MC/mcore/fits.h@ 18004

Last change on this file since 18004 was 16935, checked in by lyard, 11 years ago
god rid of ambiguous cast error
File size: 29.4 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 is_compressed;
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 sorted_cols;
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 keys(ParseBlock(vec))
252 {
253 is_compressed = HasKey("ZTABLE") && Check("ZTABLE", 'B', "T");
254
255 if (!Check("XTENSION", 'T', "BINTABLE") ||
256 !Check("NAXIS", 'I', "2") ||
257 !Check("BITPIX", 'I', "8") ||
258 !Check("GCOUNT", 'I', "1") ||
259 !Check("EXTNAME", 'T') ||
260 !Check("NAXIS1", 'I') ||
261 !Check("NAXIS2", 'I') ||
262 !Check("TFIELDS", 'I'))
263 return;
264
265 if (is_compressed)
266 {
267 if (!Check("ZNAXIS1", 'I') ||
268 !Check("ZNAXIS2", 'I') ||
269 !Check("ZPCOUNT", 'I', "0"))
270 return;
271 }
272 else
273 {
274 if (!Check("PCOUNT", 'I', "0"))
275 return;
276 }
277
278 total_bytes = Get<size_t>("NAXIS1")*Get<size_t>("NAXIS2");
279 bytes_per_row = is_compressed ? Get<size_t>("ZNAXIS1") : Get<size_t>("NAXIS1");
280 num_rows = is_compressed ? Get<size_t>("ZNAXIS2") : Get<size_t>("NAXIS2");
281 num_cols = Get<size_t>("TFIELDS");
282 datasum = is_compressed ? Get<int64_t>("ZDATASUM", -1) : Get<int64_t>("DATASUM", -1);
283
284 size_t bytes = 0;
285
286 const string tFormName = is_compressed ? "ZFORM" : "TFORM";
287 for (long long unsigned int i=1; i<=num_cols; i++)
288 {
289 const string num(to_string(i));
290
291 if (!Check("TTYPE"+num, 'T') ||
292 !Check(tFormName+num, 'T'))
293 return;
294
295 const string id = Get<string>("TTYPE"+num);
296 const string fmt = Get<string>(tFormName+num);
297 const string unit = Get<string>("TUNIT"+num, "");
298 const string comp = Get<string>("ZCTYP"+num, "");
299
300 Compression_t compress = kCompUnknown;
301 if (comp == "FACT")
302 compress = kCompFACT;
303
304 istringstream sin(fmt);
305 int n = 0;
306 sin >> n;
307 if (!sin)
308 n = 1;
309
310 const char type = fmt[fmt.length()-1];
311
312 size_t size = 0;
313 switch (type)
314 {
315 // We could use negative values to mark floats
316 // otheriwse we could just cast them to int64_t?
317 case 'L': // logical
318 case 'A': // char
319 case 'B': size = 1; break; // byte
320 case 'I': size = 2; break; // short
321 case 'J': size = 4; break; // int
322 case 'K': size = 8; break; // long long
323 case 'E': size = 4; break; // float
324 case 'D': size = 8; break; // double
325 // case 'X': size = n; break; // bits (n=number of bytes needed to contain all bits)
326 // case 'C': size = 8; break; // complex float
327 // case 'M': size = 16; break; // complex double
328 // case 'P': size = 8; break; // array descriptor (32bit)
329 // case 'Q': size = 16; break; // array descriptor (64bit)
330 default:
331 {
332 ostringstream str;
333 str << "FITS format TFORM='" << fmt << "' not yet supported.";
334#ifdef __EXCEPTIONS
335 throw runtime_error(str.str());
336#else
337 gLog << ___err___ << "ERROR - " << str.str() << endl;
338 return;
339#endif
340 }
341 }
342
343 const Table::Column col = { bytes, n, size, n*size, type, unit, compress};
344
345 cols[id] = col;
346 sorted_cols.push_back(col);
347 bytes += n*size;
348 }
349
350 if (bytes!=bytes_per_row)
351 {
352#ifdef __EXCEPTIONS
353 throw runtime_error("Column size mismatch");
354#else
355 gLog << ___err___ << "ERROR - Column size mismatch" << endl;
356 return;
357#endif
358 }
359
360 name = Get<string>("EXTNAME");
361 }
362
363 void PrintKeys(bool display_all=false) const
364 {
365 for (Keys::const_iterator it=keys.begin(); it!=keys.end(); it++)
366 {
367 if (!display_all &&
368 (it->first.substr(0, 6)=="TTYPE" ||
369 it->first.substr(0, 6)=="TFORM" ||
370 it->first.substr(0, 6)=="TUNIT" ||
371 it->first=="TFIELDS" ||
372 it->first=="XTENSION" ||
373 it->first=="NAXIS" ||
374 it->first=="BITPIX" ||
375 it->first=="PCOUNT" ||
376 it->first=="GCOUNT")
377 )
378 continue;
379
380 gLog << ___all___ << setw(2) << it->second.type << '|' << it->first << '=' << it->second.value << '/' << it->second.comment << '|' << endl;
381 }}
382
383 void PrintColumns() const
384 {
385 typedef map<pair<size_t, string>, Column> Sorted;
386
387 Sorted sorted;
388
389 for (Columns::const_iterator it=cols.begin(); it!=cols.end(); it++)
390 sorted[make_pair(it->second.offset, it->first)] = it->second;
391
392 for (Sorted::const_iterator it=sorted.begin(); it!=sorted.end(); it++)
393 {
394 gLog << ___all___ << setw(6) << it->second.offset << "| ";
395 gLog << it->second.num << 'x';
396 switch (it->second.type)
397 {
398 case 'A': gLog << "char(8)"; break;
399 case 'L': gLog << "bool(8)"; break;
400 case 'B': gLog << "byte(8)"; break;
401 case 'I': gLog << "short(16)"; break;
402 case 'J': gLog << "int(32)"; break;
403 case 'K': gLog << "int(64)"; break;
404 case 'E': gLog << "float(32)"; break;
405 case 'D': gLog << "double(64)"; break;
406 }
407 gLog << ": " << it->first.second << " [" << it->second.unit << "]" << endl;
408 }
409 }
410
411 operator bool() const { return !name.empty(); }
412
413 bool HasKey(const string &key) const
414 {
415 return keys.find(key)!=keys.end();
416 }
417
418 bool HasColumn(const string& col) const
419 {
420 return cols.find(col)!=cols.end();
421 }
422
423 const Columns &GetColumns() const
424 {
425 return cols;
426 }
427
428 const Keys &GetKeys() const
429 {
430 return keys;
431 }
432
433 // Values of keys are always signed
434 template<typename T>
435 T Get(const string &key) const
436 {
437 const map<string,Entry>::const_iterator it = keys.find(key);
438 if (it==keys.end())
439 {
440 ostringstream str;
441 str << "Key '" << key << "' not found." << endl;
442#ifdef __EXCEPTIONS
443 throw runtime_error(str.str());
444#else
445 gLog << ___err___ << "ERROR - " << str.str() << endl;
446 return T();
447#endif
448 }
449 return it->second.Get<T>();
450 }
451
452 // Values of keys are always signed
453 template<typename T>
454 T Get(const string &key, const T &deflt) const
455 {
456 const map<string,Entry>::const_iterator it = keys.find(key);
457 return it==keys.end() ? deflt :it->second.Get<T>();
458 }
459
460 size_t GetN(const string &key) const
461 {
462 const Columns::const_iterator it = cols.find(key);
463 return it==cols.end() ? 0 : it->second.num;
464 }
465
466 // There may be a gap between the main table and the start of the heap:
467 // this computes the offset
468 streamoff GetHeapShift() const
469 {
470 if (!HasKey("THEAP"))
471 return 0;
472
473 const size_t shift = Get<size_t>("THEAP");
474 return shift <= total_bytes ? 0 : shift - total_bytes;
475 }
476
477 // return total number of bytes 'all inclusive'
478 streamoff GetTotalBytes() const
479 {
480 //get offset of special data area from start of main table
481 const streamoff shift = GetHeapShift();
482
483 //and special data area size
484 const streamoff size = HasKey("PCOUNT") ? Get<streamoff>("PCOUNT") : 0;
485
486 // Get the total size
487 const streamoff total = total_bytes + size + shift;
488
489 // check for padding
490 if (total%2880==0)
491 return total;
492
493 // padding necessary
494 return total + (2880 - (total%2880));
495 }
496 };
497
498protected:
499 ofstream fCopy;
500
501 Table fTable;
502
503 typedef pair<void*, Table::Column> Address;
504 typedef vector<Address> Addresses;
505 //map<void*, Table::Column> fAddresses;
506 Addresses fAddresses;
507
508#if defined(__MARS__) || defined(__CINT__)
509 typedef map<string, void*> Pointers;
510#else
511 typedef unordered_map<string, void*> Pointers;
512#endif
513 Pointers fPointers;
514
515 vector<vector<char>> fGarbage;
516
517 vector<char> fBufferRow;
518 vector<char> fBufferDat;
519
520 size_t fRow;
521
522 Checksum fChkHeader;
523 Checksum fChkData;
524
525 bool ReadBlock(vector<string> &vec)
526 {
527 int endtag = 0;
528 for (int i=0; i<36; i++)
529 {
530 char c[81];
531 c[80] = 0;
532 read(c, 80);
533 if (!good())
534 break;
535
536 fChkHeader.add(c, 80);
537
538// if (c[0]==0)
539// return vector<string>();
540
541 string str(c);
542
543// if (!str.empty())
544// cout << setw(2) << i << "|" << str << "|" << (endtag?'-':'+') << endl;
545
546 if (endtag==2 || str=="END ")
547 {
548 endtag = 2; // valid END tag found
549 continue;
550 }
551
552 if (endtag==1 || str==" ")
553 {
554 endtag = 1; // end tag not found, but expected to be there
555 continue;
556 }
557
558 vec.push_back(str);
559 }
560
561 // Make sure that no empty vector is returned
562 if (endtag && vec.size()%36==0)
563 vec.emplace_back("END = '' / ");
564
565 return endtag==2;
566 }
567
568 string Compile(const string &key, int16_t i=-1) const
569 {
570 if (i<0)
571 return key;
572
573 ostringstream str;
574 str << key << i;
575 return str.str();
576 }
577
578 void Constructor(const string &fname, string fout, const string& tableName, bool force)
579 {
580 char simple[10];
581 read(simple, 10);
582 if (!good())
583 return;
584
585 if (memcmp(simple, "SIMPLE = ", 10))
586 {
587 clear(rdstate()|ios::badbit);
588#ifdef __EXCEPTIONS
589 throw runtime_error("File is not a FITS file.");
590#else
591 gLog << ___err___ << "ERROR - File is not a FITS file." << endl;
592 return;
593#endif
594 }
595
596 seekg(0);
597
598 while (good())
599 {
600 vector<string> block;
601 while (1)
602 {
603 // If we search for a table, we implicitly assume that
604 // not finding the table is not an error. The user
605 // can easily check that by eof() && !bad()
606 peek();
607 if (eof() && !bad() && !tableName.empty())
608 {
609 break;
610 }
611 // FIXME: Set limit on memory consumption
612 const int rc = ReadBlock(block);
613 if (!good())
614 {
615 clear(rdstate()|ios::badbit);
616#ifdef __EXCEPTIONS
617 throw runtime_error("FITS file corrupted.");
618#else
619 gLog << ___err___ << "ERROR - FITS file corrupted." << endl;
620 return;
621#endif
622 }
623
624 if (block.size()%36)
625 {
626 if (!rc && !force)
627 {
628 clear(rdstate()|ios::badbit);
629#ifdef __EXCEPTIONS
630 throw runtime_error("END keyword missing in FITS header.");
631#else
632 gLog << ___err___ << "ERROR - END keyword missing in FITS file... file might be corrupted." << endl;
633 return;
634#endif
635 }
636 break;
637 }
638 }
639
640 if (block.empty())
641 break;
642
643 if (block[0].substr(0, 9)=="SIMPLE =")
644 {
645 fChkHeader.reset();
646 continue;
647 }
648
649 if (block[0].substr(0, 9)=="XTENSION=")
650 {
651 fTable = Table(block, tellg());
652 fRow = (size_t)-1;
653
654 if (!fTable)
655 {
656 clear(rdstate()|ios::badbit);
657 return;
658 }
659
660 // Check for table name. Skip until eof or requested table are found.
661 // skip the current table?
662 if (!tableName.empty() && tableName!=fTable.Get<string>("EXTNAME"))
663 {
664 const streamoff skip = fTable.GetTotalBytes();
665 seekg(skip, ios_base::cur);
666
667 fChkHeader.reset();
668
669 continue;
670 }
671
672 fBufferRow.resize(fTable.bytes_per_row + 8-fTable.bytes_per_row%4);
673 fBufferDat.resize(fTable.bytes_per_row);
674
675 break;
676 }
677 }
678
679 if (fout.empty())
680 return;
681
682 if (*fout.rbegin()=='/')
683 {
684 const size_t p = fname.find_last_of('/');
685 fout.append(fname.substr(p+1));
686 }
687
688 fCopy.open(fout);
689 if (!fCopy)
690 {
691 clear(rdstate()|ios::badbit);
692#ifdef __EXCEPTIONS
693 throw runtime_error("Could not open output file.");
694#else
695 gLog << ___err___ << "ERROR - Failed to open output file." << endl;
696#endif
697 }
698
699 const streampos p = tellg();
700 seekg(0);
701
702 vector<char> buf(p);
703 read(buf.data(), p);
704
705 fCopy.write(buf.data(), p);
706 if (!fCopy)
707 clear(rdstate()|ios::badbit);
708 }
709
710public:
711 fits(const string &fname, const string& tableName="", bool force=false) : izstream(fname.c_str())
712 {
713 Constructor(fname, "", tableName, force);
714 }
715
716 fits(const string &fname, const string &fout, const string& tableName, bool force=false) : izstream(fname.c_str())
717 {
718 Constructor(fname, fout, tableName, force);
719 }
720
721 ~fits()
722 {
723 copy(istreambuf_iterator<char>(*this),
724 istreambuf_iterator<char>(),
725 ostreambuf_iterator<char>(fCopy));
726 }
727
728 virtual void StageRow(size_t row, char* dest)
729 {
730 // if (row!=fRow+1) // Fast seeking is ensured by izstream
731 seekg(fTable.offset+row*fTable.bytes_per_row);
732 read(dest, fTable.bytes_per_row);
733 //fin.clear(fin.rdstate()&~ios::eofbit);
734 }
735
736 virtual void WriteRowToCopyFile(size_t row)
737 {
738 if (row==fRow+1)
739 {
740 const uint8_t offset = (row*fTable.bytes_per_row)%4;
741
742 fChkData.add(fBufferRow);
743 if (fCopy.is_open() && fCopy.good())
744 fCopy.write(fBufferRow.data()+offset, fTable.bytes_per_row);
745 if (!fCopy)
746 clear(rdstate()|ios::badbit);
747 }
748 else
749 if (fCopy.is_open())
750 clear(rdstate()|ios::badbit);
751 }
752
753 void ZeroBufferForChecksum(vector<char>& vec, const uint64_t extraZeros=0)
754 {
755 auto ib = vec.begin();
756 auto ie = vec.end();
757
758 *ib++ = 0;
759 *ib++ = 0;
760 *ib++ = 0;
761 *ib = 0;
762
763 for (uint64_t i=0;i<extraZeros+8;i++)
764 *--ie = 0;
765 }
766
767 uint8_t ReadRow(size_t row)
768 {
769 // For the checksum we need everything to be correctly aligned
770 const uint8_t offset = (row*fTable.bytes_per_row)%4;
771
772 ZeroBufferForChecksum(fBufferRow);
773
774 StageRow(row, fBufferRow.data()+offset);
775
776 WriteRowToCopyFile(row);
777
778 fRow = row;
779
780 return offset;
781 }
782
783 template<size_t N>
784 void revcpy(char *dest, const char *src, const int &num)
785 {
786 const char *pend = src + num*N;
787 for (const char *ptr = src; ptr<pend; ptr+=N, dest+=N)
788 reverse_copy(ptr, ptr+N, dest);
789 }
790
791 virtual void MoveColumnDataToUserSpace(char *dest, const char *src, const Table::Column& c)
792 {
793 // Let the compiler do some optimization by
794 // knowing that we only have 1, 2, 4 and 8
795 switch (c.size)
796 {
797 case 1: memcpy (dest, src, c.bytes); break;
798 case 2: revcpy<2>(dest, src, c.num); break;
799 case 4: revcpy<4>(dest, src, c.num); break;
800 case 8: revcpy<8>(dest, src, c.num); break;
801 }
802 }
803
804#if !defined(__MARS__) && !defined(__CINT__)
805 virtual bool GetRow(size_t row, bool check=true)
806#else
807 virtual bool GetRowNum(size_t row, bool check=true)
808#endif
809 {
810 if (check && row>=fTable.num_rows)
811 return false;
812
813 const uint8_t offset = ReadRow(row);
814 if (!good())
815 return good();
816
817 const char *ptr = fBufferRow.data() + offset;
818
819 for (Addresses::const_iterator it=fAddresses.begin(); it!=fAddresses.end(); it++)
820 {
821 const Table::Column &c = it->second;
822
823 const char *src = ptr + c.offset;
824 char *dest = reinterpret_cast<char*>(it->first);
825
826 MoveColumnDataToUserSpace(dest, src, c);
827 }
828
829 return good();
830 }
831
832 bool GetNextRow(bool check=true)
833 {
834#if !defined(__MARS__) && !defined(__CINT__)
835 return GetRow(fRow+1, check);
836#else
837 return GetRowNum(fRow+1, check);
838#endif
839 }
840
841 virtual bool SkipNextRow()
842 {
843 seekg(fTable.offset+(++fRow)*fTable.bytes_per_row);
844 return good();
845 }
846
847 static bool Compare(const Address &p1, const Address &p2)
848 {
849 return p1.first>p2.first;
850 }
851
852 template<class T, class S>
853 const T &GetAs(const string &name)
854 {
855 return *reinterpret_cast<S*>(fPointers[name]);
856 }
857
858 void *SetPtrAddress(const string &name)
859 {
860 if (fTable.cols.count(name)==0)
861 {
862 ostringstream str;
863 str << "SetPtrAddress('" << name << "') - Column not found." << endl;
864#ifdef __EXCEPTIONS
865 throw runtime_error(str.str());
866#else
867 gLog << ___err___ << "ERROR - " << str.str() << endl;
868 return NULL;
869#endif
870 }
871
872 Pointers::const_iterator it = fPointers.find(name);
873 if (it!=fPointers.end())
874 return it->second;
875
876 fGarbage.emplace_back(fTable.cols[name].bytes);
877
878 void *ptr = fGarbage.back().data();
879
880 fPointers[name] = ptr;
881 fAddresses.emplace_back(ptr, fTable.cols[name]);
882 sort(fAddresses.begin(), fAddresses.end(), Compare);
883 return ptr;
884 }
885
886 template<typename T>
887 bool SetPtrAddress(const string &name, T *ptr, size_t cnt)
888 {
889 if (fTable.cols.count(name)==0)
890 {
891 ostringstream str;
892 str << "SetPtrAddress('" << name << "') - Column not found." << endl;
893#ifdef __EXCEPTIONS
894 throw runtime_error(str.str());
895#else
896 gLog << ___err___ << "ERROR - " << str.str() << endl;
897 return false;
898#endif
899 }
900
901 if (sizeof(T)!=fTable.cols[name].size)
902 {
903 ostringstream str;
904 str << "SetPtrAddress('" << name << "') - Element size mismatch: expected "
905 << fTable.cols[name].size << " from header, got " << sizeof(T) << endl;
906#ifdef __EXCEPTIONS
907 throw runtime_error(str.str());
908#else
909 gLog << ___err___ << "ERROR - " << str.str() << endl;
910 return false;
911#endif
912 }
913
914 if (cnt!=fTable.cols[name].num)
915 {
916 ostringstream str;
917 str << "SetPtrAddress('" << name << "') - Element count mismatch: expected "
918 << fTable.cols[name].num << " from header, got " << cnt << endl;
919#ifdef __EXCEPTIONS
920 throw runtime_error(str.str());
921#else
922 gLog << ___err___ << "ERROR - " << str.str() << endl;
923 return false;
924#endif
925 }
926
927 // if (fAddresses.count(ptr)>0)
928 // gLog << warn << "SetPtrAddress('" << name << "') - Pointer " << ptr << " already assigned." << endl;
929
930 //fAddresses[ptr] = fTable.cols[name];
931 fPointers[name] = ptr;
932 fAddresses.emplace_back(ptr, fTable.cols[name]);
933 sort(fAddresses.begin(), fAddresses.end(), Compare);
934 return true;
935 }
936
937 template<class T>
938 bool SetRefAddress(const string &name, T &ptr)
939 {
940 return SetPtrAddress(name, &ptr, sizeof(ptr)/sizeof(T));
941 }
942
943 template<typename T>
944 bool SetVecAddress(const string &name, vector<T> &vec)
945 {
946 return SetPtrAddress(name, vec.data(), vec.size());
947 }
948
949 template<typename T>
950 T Get(const string &key) const
951 {
952 return fTable.Get<T>(key);
953 }
954
955 template<typename T>
956 T Get(const string &key, const string &deflt) const
957 {
958 return fTable.Get<T>(key, deflt);
959 }
960
961 bool SetPtrAddress(const string &name, void *ptr)
962 {
963 if (fTable.cols.count(name)==0)
964 {
965 ostringstream str;
966 str <<"SetPtrAddress('" << name << "') - Column not found." << endl;
967#ifdef __EXCEPTIONS
968 throw runtime_error(str.str());
969#else
970 gLog << ___err___ << "ERROR - " << str.str() << endl;
971 return false;
972#endif
973 }
974
975 // if (fAddresses.count(ptr)>0)
976 // gLog << warn << "SetPtrAddress('" << name << "') - Pointer " << ptr << " already assigned." << endl;
977
978 //fAddresses[ptr] = fTable.cols[name];
979 fPointers[name] = ptr;
980 fAddresses.emplace_back(ptr, fTable.cols[name]);
981 sort(fAddresses.begin(), fAddresses.end(), Compare);
982 return true;
983 }
984
985 bool HasKey(const string &key) const { return fTable.HasKey(key); }
986 bool HasColumn(const string& col) const { return fTable.HasColumn(col);}
987 const Table::Columns &GetColumns() const { return fTable.GetColumns();}
988 const Table::SortedColumns& GetSortedColumns() const { return fTable.sorted_cols;}
989 const Table::Keys &GetKeys() const { return fTable.GetKeys();}
990
991 int64_t GetInt(const string &key) const { return fTable.Get<int64_t>(key); }
992 uint64_t GetUInt(const string &key) const { return fTable.Get<uint64_t>(key); }
993 double GetFloat(const string &key) const { return fTable.Get<double>(key); }
994 string GetStr(const string &key) const { return fTable.Get<string>(key); }
995
996 size_t GetN(const string &key) const
997 {
998 return fTable.GetN(key);
999 }
1000
1001 size_t GetNumRows() const { return fTable.num_rows; }
1002 size_t GetRow() const { return fRow==(size_t)-1 ? 0 : fRow; }
1003
1004 operator bool() const { return fTable && fTable.offset!=0; }
1005
1006 void PrintKeys() const { fTable.PrintKeys(); }
1007 void PrintColumns() const { fTable.PrintColumns(); }
1008
1009 bool IsHeaderOk() const { return fTable.datasum<0?false:(fChkHeader+Checksum(fTable.datasum)).valid(); }
1010 virtual bool IsFileOk() const { return (fChkHeader+fChkData).valid(); }
1011
1012 bool IsCompressedFITS() const { return fTable.is_compressed;}
1013};
1014
1015#ifndef __MARS__
1016};
1017#endif
1018#endif
Note: See TracBrowser for help on using the repository browser.