Changeset 11557
- Timestamp:
- 07/24/11 11:07:12 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Mars/mbase/fits.h
r11550 r11557 1 #ifndef MARS_MFits 2 #define MARS_MFits 1 #ifndef MARS_fits 2 #define MARS_fits 3 4 #include <stdint.h> 3 5 4 6 #include <map> … … 7 9 #include <algorithm> 8 10 9 #include <stdint.h> 11 #ifdef __EXCEPTIONS 12 #include <stdexcept> 13 #endif 10 14 11 15 #ifdef __CINT__ … … 14 18 15 19 #ifndef __MARS__ 16 #include <vector>17 #include <fstream>18 20 #include <iomanip> 19 #include <iostream>20 #define MZlib ifstream21 21 #define gLog cerr 22 22 #define err "" 23 23 #define all "" 24 24 #else 25 #include "MZlib.h"26 27 25 #include "MLog.h" 28 26 #include "MLogManip.h" 29 27 #endif 30 28 31 using namespace std; 32 33 class MFits : public MZlib 29 #include "izstream.h" 30 31 namespace std 32 { 33 34 class fits : public izstream 34 35 { 35 36 public: … … 78 79 { 79 80 // Trim Both leading and trailing spaces 80 const size_t start = str.find_first_not_of(c); // Find the first character position after excluding leading blank spaces81 const size_t end = str.find_last_not_of(c); // Find the first character position from reverse af81 const size_t pstart = str.find_first_not_of(c); // Find the first character position after excluding leading blank spaces 82 const size_t pend = str.find_last_not_of(c); // Find the first character position from reverse af 82 83 83 84 // if all spaces or empty return an empty string 84 if (string::npos== start || string::npos==end)85 if (string::npos==pstart || string::npos==pend) 85 86 return string(); 86 87 87 return str.substr( start, end-start+1);88 return str.substr(pstart, pend-pstart+1); 88 89 } 89 90 … … 93 94 if (it==keys.end()) 94 95 { 95 gLog << err << "ERROR - Key '" << key << "' not found." << endl; 96 ostringstream str; 97 str << "Key '" << key << "' not found."; 98 #ifdef __EXCEPTIONS 99 throw runtime_error(str.str()); 100 #else 101 gLog << err << "ERROR - " << str.str() << endl; 96 102 return false; 103 #endif 97 104 } 98 105 99 106 if (it->second.type!=type) 100 107 { 101 gLog << err << "ERROR - Wrong type for key '" << key << "': expected " << type << ", found " << it->second.type << "." << endl; 108 ostringstream str; 109 str << "Wrong type for key '" << key << "': expected " << type << ", found " << it->second.type << "."; 110 #ifdef __EXCEPTIONS 111 throw runtime_error(str.str()); 112 #else 113 gLog << err << "ERROR - " << str.str() << endl; 102 114 return false; 115 #endif 103 116 } 104 117 105 118 if (!value.empty() && it->second.value!=value) 106 119 { 107 gLog << err << "ERROR - Wrong value for key '" << key << "': expected " << value << ", found " << it->second.value << "." << endl; 120 ostringstream str; 121 str << "Wrong value for key '" << key << "': expected " << value << ", found " << it->second.value << "."; 122 #ifdef __EXCEPTIONS 123 throw runtime_error(str.str()); 124 #else 125 gLog << err << "ERROR - " << str.str() << endl; 108 126 return false; 127 #endif 109 128 } 110 129 … … 197 216 return; 198 217 199 const string name= Get<string>("TTYPE"+num.str());200 const string fmt 201 202 istringstream in(fmt);218 const string id = Get<string>("TTYPE"+num.str()); 219 const string fmt = Get<string>("TFORM"+num.str()); 220 221 istringstream sin(fmt); 203 222 int n = 0; 204 in >> n;205 if (! in)223 sin >> n; 224 if (!sin) 206 225 n = 1; 207 226 … … 226 245 // case 'Q': size = 16; break; // array descriptor (64bit) 227 246 default: 228 gLog << err << "FITS format '" << fmt << "' not yet supported." << endl; 229 continue; 247 { 248 ostringstream str; 249 str << "FITS format TFORM='" << fmt << "' not yet supported."; 250 #ifdef __EXCEPTIONS 251 throw runtime_error(str.str()); 252 #else 253 gLog << err << "ERROR - " << str.str() << endl; 254 return; 255 #endif 256 } 230 257 } 231 258 232 259 const Table::Column col = { bytes, n, size, type }; 233 260 234 cols[ name] = col;261 cols[id] = col; 235 262 bytes += n*size; 236 263 } … … 238 265 if (bytes!=bytes_per_row) 239 266 { 240 gLog << err << "Column size mismatch" << endl; 267 #ifdef __EXCEPTIONS 268 throw runtime_error("Column size mismatch"); 269 #else 270 gLog << err << "ERROR - Column size mismatch" << endl; 241 271 return; 272 #endif 242 273 } 243 274 … … 275 306 T Get(const string &key) const 276 307 { 277 const ::map<string,Entry>::const_iterator it = keys.find(key);308 const map<string,Entry>::const_iterator it = keys.find(key); 278 309 if (it==keys.end()) 279 310 { 280 gLog << err << "Key '" << key << "' not found." << endl; 311 ostringstream str; 312 str << "Key '" << key << "' not found." << endl; 313 #ifdef __EXCEPTIONS 314 throw runtime_error(str.str()); 315 #else 316 gLog << err << "ERROR - " << str.str() << endl; 281 317 return 0; 318 #endif 282 319 } 283 320 return it->second.Get<T>(); … … 306 343 vector<string> ReadBlock(vector<string> &vec) 307 344 { 308 bool end = false;345 bool endtag = false; 309 346 for (int i=0; i<36; i++) 310 347 { … … 324 361 325 362 if (str=="END ") 326 end = true;327 328 if (end )363 endtag = true; 364 365 if (endtag) 329 366 continue; 330 367 … … 346 383 347 384 public: 348 MFits(const string &fname) : MZlib(fname.c_str())385 fits(const string &fname) : izstream(fname.c_str()) 349 386 { 350 387 char simple[6]; … … 355 392 if (memcmp(simple, "SIMPLE", 6)) 356 393 { 357 gLog << err << "File is not a FITS file." << endl;358 394 clear(rdstate()|ios::badbit); 395 #ifdef __EXCEPTIONS 396 throw runtime_error("File is not a FITS file."); 397 #else 398 gLog << err << "ERROR - File is not a FITS file." << endl; 359 399 return; 400 #endif 360 401 } 361 402 … … 371 412 if (!good()) 372 413 { 373 gLog << err << "FITS file corrupted." << endl;374 414 clear(rdstate()|ios::badbit); 415 #ifdef __EXCEPTIONS 416 throw runtime_error("FITS file corrupted."); 417 #else 418 gLog << err << "ERROR - FITS file corrupted." << endl; 375 419 return; 420 #endif 376 421 } 377 422 … … 396 441 397 442 if (!fTable) 398 return;399 400 if (!fTable.Check("TELESCOP", 'T', "FACT"))401 443 { 402 gLog << err << "Not a valid FACT fits file." << endl;444 clear(rdstate()|ios::badbit); 403 445 return; 404 446 } … … 427 469 void ReadRow(size_t row) 428 470 { 429 // if (row!=fRow+1) // Fast seeking is ensured by MZlib471 // if (row!=fRow+1) // Fast seeking is ensured by izstream 430 472 seekg(fTable.offset+row*fTable.bytes_per_row); 431 473 … … 439 481 void revcpy(char *dest, const char *src, int num) 440 482 { 441 const char * end = src + num*N;442 for (const char *ptr = src; ptr< end; ptr+=N, dest+=N)483 const char *pend = src + num*N; 484 for (const char *ptr = src; ptr<pend; ptr+=N, dest+=N) 443 485 reverse_copy(ptr, ptr+N, dest); 444 486 } … … 492 534 if (fTable.cols.count(name)==0) 493 535 { 494 gLog << err <<"SetPtrAddress('" << name << "') - Column not found." << endl; 536 ostringstream str; 537 str <<"SetPtrAddress('" << name << "') - Column not found." << endl; 538 #ifdef __EXCEPTIONS 539 throw runtime_error(str.str()); 540 #else 541 gLog << err << "ERROR - " << str.str() << endl; 495 542 return false; 543 #endif 496 544 } 497 545 498 546 if (sizeof(T)!=fTable.cols[name].size) 499 547 { 500 gLog << err << "SetPtrAddress('" << name << "') - Element size mismatch: expected " 548 ostringstream str; 549 str << "SetPtrAddress('" << name << "') - Element size mismatch: expected " 501 550 << fTable.cols[name].size << " from header, got " << sizeof(T) << endl; 502 551 #ifdef __EXCEPTIONS 552 throw runtime_error(str.str()); 553 #else 554 gLog << err << "ERROR - " << str.str() << endl; 503 555 return false; 556 #endif 504 557 } 505 558 506 559 if (cnt!=fTable.cols[name].num) 507 560 { 508 gLog << err << "SetPtrAddress('" << name << "') - Element count mismatch: expected " 561 ostringstream str; 562 str << "SetPtrAddress('" << name << "') - Element count mismatch: expected " 509 563 << fTable.cols[name].num << " from header, got " << cnt << endl; 510 564 #ifdef __EXCEPTIONS 565 throw runtime_error(str.str()); 566 #else 567 gLog << err << "ERROR - " << str.str() << endl; 511 568 return false; 569 #endif 512 570 } 513 571 … … 543 601 if (fTable.cols.count(name)==0) 544 602 { 545 gLog << err <<"SetPtrAddress('" << name << "') - Column not found." << endl; 603 ostringstream str; 604 str <<"SetPtrAddress('" << name << "') - Column not found." << endl; 605 #ifdef __EXCEPTIONS 606 throw runtime_error(str.str()); 607 #else 608 gLog << err << "ERROR - " << str.str() << endl; 546 609 return false; 610 #endif 547 611 } 548 612 … … 560 624 uint64_t GetUInt(const string &key) const { return fTable.Get<uint64_t>(key); } 561 625 double GetFloat(const string &key) const { return fTable.Get<double>(key); } 626 string GetStr(const string &key) const { return fTable.Get<string>(key); } 562 627 563 628 size_t GetN(const string &key) const … … 574 639 void PrintColumns() const { fTable.PrintColumns(); } 575 640 }; 576 #endif 641 642 }; 643 #endif
Note:
See TracChangeset
for help on using the changeset viewer.