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

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