Changeset 17778 for trunk/Mars/mcore/ofits.h
- Timestamp:
- 05/05/14 09:48:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Mars/mcore/ofits.h
r17761 r17778 2 2 #define MARS_ofits 3 3 4 #include <string> 5 #include <string.h> 6 #include <algorithm> 7 #include <sstream> 8 #include <iostream> 9 #include <fstream> 10 #include <iomanip> 11 #include <vector> 12 #include <algorithm> 13 #include <stdexcept> 14 15 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) 4 #include "FITS.h" 5 #include "fits.h" 16 6 17 7 #ifndef PACKAGE_NAME … … 31 21 #endif 32 22 33 #ifndef __MARS__34 #ifndef gLog35 #define gLog std::cerr36 #define ___err___ ""37 #define ___warn___ ""38 #define ___all___ ""39 #endif40 #else41 #include "MLog.h"42 #include "MLogManip.h"43 #define ___err___ err44 #define ___warn___ warn45 #define ___all___ all46 #endif47 48 #include "FITS.h"49 #include "checksum.h"50 51 23 // Sloppy: allow / <--- left 52 24 // allow all characters (see specs for what is possible) 53 25 54 26 // units: m kg s rad sr K A mol cd Hz J W V N Pa C Ohm S F Wb T Hlm lx 27 55 28 56 29 class ofits : public std::ostream … … 72 45 bool changed; // For closing the file 73 46 74 Key(const std::string &k="") : key(k), delim(false), fitsString(""), offset(0), changed(true) { } 47 Key() : delim(false), offset(0), changed(true) { } 48 Key(const std::string &s) : delim(false), fitsString(s), offset(0), changed(true) { } 75 49 76 50 std::string Trim(const std::string &str) … … 215 189 std::string Compile() 216 190 { 217 218 if (fitsString != "") 191 if (!fitsString.empty()) 219 192 return fitsString; 220 193 … … 318 291 entry.value = value; 319 292 entry.comment = comment; 320 entry.offset = 0;321 entry.changed = true;322 293 323 294 if (!entry.check(fCommentTrimming)) 324 295 return false; 325 296 326 fKeys. push_back(entry);297 fKeys.emplace_back(entry); 327 298 return true; 328 299 } … … 455 426 fCommentTrimming = allow; 456 427 } 428 457 429 //Etienne: required to enable 1 to 1 reconstruction of files 458 430 bool SetKeyComment(const std::string& key, const std::string& comment) … … 461 433 if (it==fKeys.end()) 462 434 return false; 435 463 436 it->comment = comment; 464 437 it->changed = true; 465 438 return true; 466 439 } 440 /* tbretz, I removed that, because it does not comply 441 with the FITS standard, it omits all checks... it 442 neither checks if a row already exists, not 443 checks the chcracter set 467 444 bool SetKeyFromFitsString(const std::string& fitsString) 468 445 { … … 484 461 fKeys.push_back(entry); 485 462 return true; 486 } 463 }*/ 464 465 466 bool CopyKeys(const fits &fin, bool update=false) 467 { 468 if (fTable.num_rows>0) 469 { 470 #ifdef __EXCEPTIONS 471 throw std::runtime_error("No header keys can be copied, rows were already written to the file... ignoring CopyKeys()."); 472 #else 473 gLog << ___err___ << "ERROR - No header key can be copyied, rows were already written to the file... ignoring CopyKeys()." << std::endl; 474 return false; 475 #endif 476 } 477 478 const auto &keys = fin.GetKeys(); 479 480 // We can assume that the keys are all valid 481 for (auto it=keys.cbegin(); it!=keys.cend(); it++) 482 { 483 const std::string &key = it->first; 484 485 if (FITS::IsReservedKeyWord(key)) 486 continue; 487 488 const auto &entry = it->second; 489 490 // If no delimit add the row no matter if it alread exists 491 if (entry.fitsString[9]=='=') 492 { 493 // if the row already exists: remove it 494 const auto it2 = findkey(key); 495 if (it2!=fKeys.end()) 496 { 497 if (!update) 498 continue; 499 500 fKeys.erase(it2); 501 } 502 } 503 504 fKeys.emplace_back(entry.fitsString); 505 } 506 507 return true; 508 } 509 510 487 511 bool SetRaw(const std::string &key, const std::string &val, const std::string &comment) 488 512 { … … 569 593 } 570 594 571 std::string CommentFromType(char type)572 {573 std::string comment;574 575 switch (type)576 {577 case 'L': comment = "[1-byte BOOL]"; break;578 case 'A': comment = "[1-byte CHAR]"; break;579 case 'B': comment = "[1-byte BOOL]"; break;580 case 'I': comment = "[2-byte INT]"; break;581 case 'J': comment = "[4-byte INT]"; break;582 case 'K': comment = "[8-byte INT]"; break;583 case 'E': comment = "[4-byte FLOAT]"; break;584 case 'D': comment = "[8-byte FLOAT]"; break;585 case 'Q': comment = "[var. Length]"; break;586 }587 588 return comment;589 }590 591 uint32_t SizeFromType(char type)592 {593 size_t size = 0;594 595 switch (type)596 {597 case 'L': size = 1; break;598 case 'A': size = 1; break;599 case 'B': size = 1; break;600 case 'I': size = 2; break;601 case 'J': size = 4; break;602 case 'K': size = 8; break;603 case 'E': size = 4; break;604 case 'D': size = 8; break;605 case 'Q': size = 16; break;606 }607 608 return size;609 }610 595 //ETIENNE to be able to restore the file 1 to 1, I must restore the header keys myself 611 596 virtual bool AddColumn(uint32_t cnt, char typechar, const std::string &name, const std::string &unit, const std::string &comment="", bool addHeaderKeys=true) … … 682 667 const std::string nc = std::to_string(fTable.num_cols); 683 668 #endif 684 SetStr("TFORM"+nc, type.str(), "format of "+name+" "+ CommentFromType(typechar));669 SetStr("TFORM"+nc, type.str(), "format of "+name+" "+FITS::CommentFromType(typechar)); 685 670 SetStr("TTYPE"+nc, name, comment); 686 671 if (!unit.empty()) 687 672 SetStr("TUNIT"+nc, unit, "unit of "+name); 688 673 } 689 size_t size = SizeFromType(typechar); 674 675 const size_t size = FITS::SizeFromType(typechar); 690 676 691 677 Table::Column col; … … 697 683 col.offset = fTable.bytes_per_row; 698 684 699 fTable.cols. push_back(col);685 fTable.cols.emplace_back(col); 700 686 701 687 fTable.bytes_per_row += size*cnt;
Note:
See TracChangeset
for help on using the changeset viewer.