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

Last change on this file since 16430 was 16426, checked in by tbretz, 11 years ago
Fixed some typos and a compiler warning.
File size: 29.6 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 typedef enum
74 {
75 UNCOMPRESSED,
76 SMOOTHMAN
77 } FitsCompression;
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 FitsCompression 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 if (val[0]=='\'')
202 {
203 // First skip all '' in the string
204 size_t p = 1;
205 while (1)
206 {
207 const size_t pp = val.find_first_of('\'', p);
208 if (pp==string::npos)
209 break;
210
211 p = val[pp+1]=='\'' ? pp+2 : pp+1;
212 }
213
214 // Now find the comment
215 const size_t ppp = val.find_first_of('/', p);
216
217 // Set value, comment and type
218 if (ppp==string::npos)
219 com = "";
220 else
221 {// comments could be just spaces. take care of this.
222 if (val.size() == ppp+1)
223 com = "";
224 else
225 com = Trim(val.substr(ppp+1));
226 }
227// com = ppp==string::npos ? "" : Trim(val.substr(ppp+1));
228 val = Trim(val.substr(1, p-2));
229 type = 'T';
230 }
231 else
232 {
233 const size_t p = val.find_first_of('/');
234
235 if (val.size() == p+1)
236 com = "";
237 else
238 com = Trim(val.substr(p+2));
239 val = Trim(val.substr(0, p));
240
241 if (val.empty() || val.find_first_of('T')!=string::npos || val.find_first_of('F')!=string::npos)
242 type = 'B';
243 else
244 type = val.find_last_of('.')==string::npos ? 'I' : 'F';
245 }
246
247 const Entry e = { type, val, com, vec[i] };
248 rc[key] = e;
249 }
250
251 return rc;
252 }
253
254 Table() : offset(0) { }
255 Table(const vector<string> &vec, off_t off) :
256 offset(off), isCompressed(false), keys(ParseBlock(vec))
257 {
258 if (HasKey("ZTABLE") && Check("ZTABLE", 'B', "T"))
259 isCompressed = true;
260
261 if (!Check("XTENSION", 'T', "BINTABLE") ||
262 !Check("NAXIS", 'I', "2") ||
263 !Check("BITPIX", 'I', "8") ||
264 !Check("GCOUNT", 'I', "1") ||
265 !Check("EXTNAME", 'T') ||
266 !Check("NAXIS1", 'I') ||
267 !Check("NAXIS2", 'I') ||
268 !Check("TFIELDS", 'I'))
269 return;
270
271 if (isCompressed)
272 {
273 if (!Check("ZNAXIS1", 'I') ||
274 !Check("ZNAXIS2", 'I') ||
275 !Check("ZPCOUNT", 'I', "0"))
276 return;
277 }
278 else
279 {
280 if (!Check("PCOUNT", 'I', "0"))
281 return;
282 }
283
284 total_bytes = Get<size_t>("NAXIS1")*Get<size_t>("NAXIS2");
285 bytes_per_row = isCompressed ? Get<size_t>("ZNAXIS1") : Get<size_t>("NAXIS1");
286 num_rows = isCompressed ? Get<size_t>("ZNAXIS2") : Get<size_t>("NAXIS2");
287 num_cols = Get<size_t>("TFIELDS");
288 datasum = isCompressed ? Get<int64_t>("ZDATASUM", -1) : Get<int64_t>("DATASUM", -1);
289
290 size_t bytes = 0;
291 string tFormName = isCompressed ? "ZFORM" : "TFORM";
292 for (size_t i=1; i<=num_cols; i++)
293 {
294 ostringstream num;
295 num << i;
296
297 if (!Check("TTYPE"+num.str(), 'T') ||
298 !Check(tFormName+num.str(), 'T'))
299 return;
300
301 const string id = Get<string>("TTYPE"+num.str());
302 const string fmt = Get<string>(tFormName+num.str());
303 const string unit = Get<string>("TUNIT"+num.str(), "");
304 const string comp = Get<string>("ZCTYP"+num.str(), "");
305
306 FitsCompression compress = UNCOMPRESSED;
307 if (comp == "SMOO")
308 compress = SMOOTHMAN;
309
310 istringstream sin(fmt);
311 int n = 0;
312 sin >> n;
313 if (!sin)
314 n = 1;
315
316 const char type = fmt[fmt.length()-1];
317
318 size_t size = 0;
319 switch (type)
320 {
321 // We could use negative values to mark floats
322 // otheriwse we could just cast them to int64_t?
323 case 'L': // logical
324 case 'A': // char
325 case 'B': size = 1; break; // byte
326 case 'I': size = 2; break; // short
327 case 'J': size = 4; break; // int
328 case 'K': size = 8; break; // long long
329 case 'E': size = 4; break; // float
330 case 'D': size = 8; break; // double
331 // case 'X': size = n; break; // bits (n=number of bytes needed to contain all bits)
332 // case 'C': size = 8; break; // complex float
333 // case 'M': size = 16; break; // complex double
334 // case 'P': size = 8; break; // array descriptor (32bit)
335 // case 'Q': size = 16; break; // array descriptor (64bit)
336 default:
337 {
338 ostringstream str;
339 str << "FITS format TFORM='" << fmt << "' not yet supported.";
340#ifdef __EXCEPTIONS
341 throw runtime_error(str.str());
342#else
343 gLog << ___err___ << "ERROR - " << str.str() << endl;
344 return;
345#endif
346 }
347 }
348
349 const Table::Column col = { bytes, n, size, n*size, type, unit, compress};
350
351 cols[id] = col;
352 sortedCols.push_back(col);
353 bytes += n*size;
354 }
355
356 if (bytes!=bytes_per_row)
357 {
358#ifdef __EXCEPTIONS
359 throw runtime_error("Column size mismatch");
360#else
361 gLog << ___err___ << "ERROR - Column size mismatch" << endl;
362 return;
363#endif
364 }
365
366 name = Get<string>("EXTNAME");
367 }
368
369 void PrintKeys(bool display_all=false) const
370 {
371 for (Keys::const_iterator it=keys.begin(); it!=keys.end(); it++)
372 {
373 if (!display_all &&
374 (it->first.substr(0, 6)=="TTYPE" ||
375 it->first.substr(0, 6)=="TFORM" ||
376 it->first.substr(0, 6)=="TUNIT" ||
377 it->first=="TFIELDS" ||
378 it->first=="XTENSION" ||
379 it->first=="NAXIS" ||
380 it->first=="BITPIX" ||
381 it->first=="PCOUNT" ||
382 it->first=="GCOUNT")
383 )
384 continue;
385
386 gLog << ___all___ << setw(2) << it->second.type << '|' << it->first << '=' << it->second.value << '/' << it->second.comment << '|' << endl;
387 }}
388
389 void PrintColumns() const
390 {
391 typedef map<pair<size_t, string>, Column> Sorted;
392
393 Sorted sorted;
394
395 for (Columns::const_iterator it=cols.begin(); it!=cols.end(); it++)
396 sorted[make_pair(it->second.offset, it->first)] = it->second;
397
398 for (Sorted::const_iterator it=sorted.begin(); it!=sorted.end(); it++)
399 {
400 gLog << ___all___ << setw(6) << it->second.offset << "| ";
401 gLog << it->second.num << 'x';
402 switch (it->second.type)
403 {
404 case 'A': gLog << "char(8)"; break;
405 case 'L': gLog << "bool(8)"; break;
406 case 'B': gLog << "byte(8)"; break;
407 case 'I': gLog << "short(16)"; break;
408 case 'J': gLog << "int(32)"; break;
409 case 'K': gLog << "int(64)"; break;
410 case 'E': gLog << "float(32)"; break;
411 case 'D': gLog << "double(64)"; break;
412 }
413 gLog << ": " << it->first.second << " [" << it->second.unit << "]" << endl;
414 }
415 }
416
417 operator bool() const { return !name.empty(); }
418
419 bool HasKey(const string &key) const
420 {
421 return keys.find(key)!=keys.end();
422 }
423
424 bool HasColumn(const string& col) const
425 {
426 return cols.find(col)!=cols.end();
427 }
428
429 const Columns &GetColumns() const
430 {
431 return cols;
432 }
433
434 const Keys &GetKeys() const
435 {
436 return keys;
437 }
438
439 // Values of keys are always signed
440 template<typename T>
441 T Get(const string &key) const
442 {
443 const map<string,Entry>::const_iterator it = keys.find(key);
444 if (it==keys.end())
445 {
446 ostringstream str;
447 str << "Key '" << key << "' not found." << endl;
448#ifdef __EXCEPTIONS
449 throw runtime_error(str.str());
450#else
451 gLog << ___err___ << "ERROR - " << str.str() << endl;
452 return T();
453#endif
454 }
455 return it->second.Get<T>();
456 }
457
458 // Values of keys are always signed
459 template<typename T>
460 T Get(const string &key, const T &deflt) const
461 {
462 const map<string,Entry>::const_iterator it = keys.find(key);
463 return it==keys.end() ? deflt :it->second.Get<T>();
464 }
465
466 size_t GetN(const string &key) const
467 {
468 const Columns::const_iterator it = cols.find(key);
469 return it==cols.end() ? 0 : it->second.num;
470 }
471
472 // There may be a gap between the main table and the start of the heap:
473 // this computes the offset
474 streamoff GetHeapShift() const
475 {
476 if (!HasKey("THEAP"))
477 return 0;
478
479 const size_t shift = Get<size_t>("THEAP");
480 return shift <= total_bytes ? 0 : shift - total_bytes;
481 }
482
483 // return total number of bytes 'all inclusive'
484 streamoff GetTotalBytes() const
485 {
486 //get offset of special data area from start of main table
487 const streamoff shift = GetHeapShift();
488
489 //and special data area size
490 const streamoff size = HasKey("PCOUNT") ? Get<streamoff>("PCOUNT") : 0;
491
492 // Get the total size
493 const streamoff total = total_bytes + size + shift;
494
495 // check for padding
496 if (total%2880==0)
497 return total;
498
499 // padding necessary
500 return total + (2880 - (total%2880));
501 }
502 };
503
504protected:
505 ofstream fCopy;
506
507 Table fTable;
508
509 typedef pair<void*, Table::Column> Address;
510 typedef vector<Address> Addresses;
511 //map<void*, Table::Column> fAddresses;
512 Addresses fAddresses;
513
514#if defined(__MARS__) || defined(__CINT__)
515 typedef map<string, void*> Pointers;
516#else
517 typedef unordered_map<string, void*> Pointers;
518#endif
519 Pointers fPointers;
520
521 vector<vector<char>> fGarbage;
522
523 vector<char> fBufferRow;
524 vector<char> fBufferDat;
525
526 size_t fRow;
527
528 Checksum fChkHeader;
529 Checksum fChkData;
530
531 bool ReadBlock(vector<string> &vec)
532 {
533 int endtag = 0;
534 for (int i=0; i<36; i++)
535 {
536 char c[81];
537 c[80] = 0;
538 read(c, 80);
539 if (!good())
540 break;
541
542 fChkHeader.add(c, 80);
543
544// if (c[0]==0)
545// return vector<string>();
546
547 string str(c);
548
549// if (!str.empty())
550// cout << setw(2) << i << "|" << str << "|" << (endtag?'-':'+') << endl;
551
552 if (endtag==2 || str=="END ")
553 {
554 endtag = 2; // valid END tag found
555 continue;
556 }
557
558 if (endtag==1 || str==" ")
559 {
560 endtag = 1; // end tag not found, but expected to be there
561 continue;
562 }
563
564 vec.push_back(str);
565 }
566
567 // Make sure that no empty vector is returned
568 if (endtag && vec.size()%36==0)
569 vec.emplace_back("END = '' / ");
570
571 return endtag==2;
572 }
573
574 string Compile(const string &key, int16_t i=-1) const
575 {
576 if (i<0)
577 return key;
578
579 ostringstream str;
580 str << key << i;
581 return str.str();
582 }
583
584 void Constructor(const string &fname, string fout, const string& tableName, bool force)
585 {
586 char simple[10];
587 read(simple, 10);
588 if (!good())
589 return;
590
591 if (memcmp(simple, "SIMPLE = ", 10))
592 {
593 clear(rdstate()|ios::badbit);
594#ifdef __EXCEPTIONS
595 throw runtime_error("File is not a FITS file.");
596#else
597 gLog << ___err___ << "ERROR - File is not a FITS file." << endl;
598 return;
599#endif
600 }
601
602 seekg(0);
603
604 while (good())
605 {
606 vector<string> block;
607 while (1)
608 {
609 // If we search for a table, we implicitly assume that
610 // not finding the table is not an error. The user
611 // can easily check that by eof() && !bad()
612 peek();
613 if (eof() && !bad() && !tableName.empty())
614 break;
615
616 // FIXME: Set limit on memory consumption
617 const int rc = ReadBlock(block);
618 if (!good())
619 {
620 clear(rdstate()|ios::badbit);
621#ifdef __EXCEPTIONS
622 throw runtime_error("FITS file corrupted.");
623#else
624 gLog << ___err___ << "ERROR - FITS file corrupted." << endl;
625 return;
626#endif
627 }
628
629 if (block.size()%36)
630 {
631 if (!rc && !force)
632 {
633 clear(rdstate()|ios::badbit);
634#ifdef __EXCEPTIONS
635 throw runtime_error("END keyword missing in FITS header.");
636#else
637 gLog << ___err___ << "ERROR - END keyword missing in FITS file... file might be corrupted." << endl;
638 return;
639#endif
640 }
641 break;
642 }
643 }
644
645 if (block.empty())
646 break;
647
648 if (block[0].substr(0, 9)=="SIMPLE =")
649 {
650 fChkHeader.reset();
651 continue;
652 }
653
654 if (block[0].substr(0, 9)=="XTENSION=")
655 {
656 fTable = Table(block, tellg());
657 fRow = (size_t)-1;
658
659 if (!fTable)
660 {
661 clear(rdstate()|ios::badbit);
662 return;
663 }
664
665 //Check for table name. Skip until eof or requested table are found.
666 // skip the current table?
667 if (!tableName.empty() && tableName!=fTable.Get<string>("EXTNAME"))
668 {
669 const streamoff skip = fTable.GetTotalBytes();
670 seekg(skip, ios_base::cur);
671
672 fChkHeader.reset();
673
674 continue;
675 }
676
677 //fTable.PrintKeys();
678
679 fBufferRow.resize(fTable.bytes_per_row + 8-fTable.bytes_per_row%4);
680 fBufferDat.resize(fTable.bytes_per_row);
681
682 break;
683 }
684 }
685
686 if (fout.empty())
687 return;
688
689 if (*fout.rbegin()=='/')
690 {
691 const size_t p = fname.find_last_of('/');
692 fout.append(fname.substr(p+1));
693 }
694
695 fCopy.open(fout);
696 if (!fCopy)
697 {
698 clear(rdstate()|ios::badbit);
699#ifdef __EXCEPTIONS
700 throw runtime_error("Could not open output file.");
701#else
702 gLog << ___err___ << "ERROR - Failed to open output file." << endl;
703#endif
704 }
705
706 const streampos p = tellg();
707 seekg(0);
708
709 vector<char> buf(p);
710 read(buf.data(), p);
711
712 fCopy.write(buf.data(), p);
713 if (!fCopy)
714 clear(rdstate()|ios::badbit);
715 }
716
717public:
718 fits(const string &fname, const string& tableName="", bool force=false) : izstream(fname.c_str())
719 {
720 Constructor(fname, "", tableName, force);
721 }
722
723 fits(const string &fname, const string &fout, const string& tableName, bool force=false) : izstream(fname.c_str())
724 {
725 Constructor(fname, fout, tableName, force);
726 }
727
728 ~fits()
729 {
730 copy(istreambuf_iterator<char>(*this),
731 istreambuf_iterator<char>(),
732 ostreambuf_iterator<char>(fCopy));
733 }
734
735 virtual void StageRow(size_t row, char* dest)
736 {
737 // if (row!=fRow+1) // Fast seeking is ensured by izstream
738 seekg(fTable.offset+row*fTable.bytes_per_row);
739 read(dest, fTable.bytes_per_row);
740 //fin.clear(fin.rdstate()&~ios::eofbit);
741 }
742
743 uint8_t ReadRow(size_t row)
744 {
745 // For the checksum we need everything to be correctly aligned
746 const uint8_t offset = (row*fTable.bytes_per_row)%4;
747
748 auto ib = fBufferRow.begin();
749 auto ie = fBufferRow.end();
750 *ib++ = 0;
751 *ib++ = 0;
752 *ib++ = 0;
753 *ib = 0;
754
755 *--ie = 0;
756 *--ie = 0;
757 *--ie = 0;
758 *--ie = 0;
759 *--ie = 0;
760 *--ie = 0;
761 *--ie = 0;
762 *--ie = 0;
763
764 StageRow(row, fBufferRow.data()+offset);
765
766 if (row==fRow+1)
767 {
768 fChkData.add(fBufferRow);
769 if (fCopy.is_open() && fCopy.good())
770 fCopy.write(fBufferRow.data()+offset, fTable.bytes_per_row);
771 if (!fCopy)
772 clear(rdstate()|ios::badbit);
773 }
774 else
775 if (fCopy.is_open())
776 clear(rdstate()|ios::badbit);
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.sortedCols;}
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 bool IsFileOk() const { return (fChkHeader+fChkData).valid(); }
1011
1012 bool IsCompressedFITS() const { return fTable.isCompressed;}
1013};
1014
1015#ifndef __MARS__
1016};
1017#endif
1018#endif
Note: See TracBrowser for help on using the repository browser.