source: branches/Corsika7500Compatibility/mcore/fits.h@ 19905

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