Changeset 10837 for trunk/FACT++
- Timestamp:
- 05/26/11 16:17:49 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/fitsdump.cc
r10797 r10837 17 17 class FitsDumper 18 18 { 19 20 19 public: 21 20 FitsDumper(); … … 38 37 39 38 template<class T> 40 void Write(ostream &out, const unsigned char* &ptr) const; 39 T PtrToValue(const unsigned char* &ptr) const; 40 // template<class T> 41 // double PtrToDouble(const unsigned char *ptr) const; 42 // double PtrToDouble(const unsigned char *ptr, CCfits::ValueType type) const; 41 43 42 44 /// Write a single row of the selected data 43 int WriteRow(ostream &, const vector< string> &, const vector<int> &, unsigned char *) const;45 int WriteRow(ostream &, const vector<CCfits::Column*> &, const vector<int> &, unsigned char *) const; 44 46 45 47 bool OpenFile(const string &); /// Open a file … … 54 56 bool Dump(const string &, const vector<string> &list, int); 55 57 58 // bool Plot(const vector<string> &list); 56 59 57 60 public: … … 122 125 123 126 template<class T> 124 void FitsDumper::Write(ostream &out,const unsigned char* &ptr) const127 T FitsDumper::PtrToValue(const unsigned char* &ptr) const 125 128 { 126 129 T t; 127 130 reverse_copy(ptr, ptr+sizeof(T), reinterpret_cast<unsigned char*>(&t)); 128 out << t;129 130 131 ptr += sizeof(T); 131 } 132 132 133 return t; 134 } 135 /* 136 template<class T> 137 double FitsDumper::PtrToDouble(const unsigned char *ptr) const 138 { 139 T t; 140 reverse_copy(ptr, ptr+sizeof(T), reinterpret_cast<unsigned char*>(&t)); 141 return t; 142 } 143 144 double FitsDumper::PtrToDouble(const unsigned char *ptr, CCfits::ValueType type) const 145 { 146 switch (type) 147 { 148 case CCfits::Tbyte: return PtrToDouble<uint8_t> (ptr); 149 case CCfits::Tushort: return PtrToDouble<uint16_t>(ptr); 150 case CCfits::Tuint: 151 case CCfits::Tulong: return PtrToDouble<uint32_t>(ptr); 152 case CCfits::Tshort: return PtrToDouble<int16_t> (ptr); 153 case CCfits::Tint: 154 case CCfits::Tlong: return PtrToDouble<int32_t> (ptr); 155 case CCfits::Tlonglong: return PtrToDouble<int64_t> (ptr); 156 case CCfits::Tfloat: return PtrToDouble<float> (ptr); 157 case CCfits::Tdouble: return PtrToDouble<double> (ptr); 158 159 default: 160 throw runtime_error("Data type not implemented yet."); 161 } 162 } 163 */ 133 164 134 165 // -------------------------------------------------------------------------- … … 151 182 //! the memory were the row has been loaded by cfitsio 152 183 // 153 int FitsDumper::WriteRow(ostream &out, const vector< string> &list, const vector<int> &offsets, unsigned char* fitsBuffer) const184 int FitsDumper::WriteRow(ostream &out, const vector<CCfits::Column*> &list, const vector<int> &offsets, unsigned char* fitsBuffer) const 154 185 { 155 186 int cnt = 0; 156 187 157 for (vector<string>::const_iterator it=list.begin(); it!=list.end(); it++) 158 { 159 // Loop over all columns in our list of requested columns 160 const CCfits::Column *col = fColMap.find(*it)->second; 188 for (vector<CCfits::Column*>::const_iterator it=list.begin(); it!=list.end(); it++) 189 { 190 const CCfits::Column *col = *it; 161 191 162 192 // CCfits starts counting at 1 not 0 … … 171 201 switch (col->type()) 172 202 { 173 case CCfits::Tbyte: Write<uint8_t> (out,ptr); break;174 case CCfits::Tushort: Write<uint16_t>(out,ptr); break;203 case CCfits::Tbyte: out << PtrToValue<uint8_t> (ptr); break; 204 case CCfits::Tushort: out << PtrToValue<uint16_t>(ptr); break; 175 205 case CCfits::Tuint: 176 case CCfits::Tulong: Write<uint32_t>(out,ptr); break;177 case CCfits::Tshort: Write<int16_t> (out,ptr); break;206 case CCfits::Tulong: out << PtrToValue<uint32_t>(ptr); break; 207 case CCfits::Tshort: out << PtrToValue<int16_t> (ptr); break; 178 208 case CCfits::Tint: 179 case CCfits::Tlong: Write<int32_t> (out,ptr); break;180 case CCfits::Tlonglong: Write<int64_t> (out,ptr); break;181 case CCfits::Tfloat: Write<float> (out,ptr); break;182 case CCfits::Tdouble: Write<double> (out,ptr); break;209 case CCfits::Tlong: out << PtrToValue<int32_t> (ptr); break; 210 case CCfits::Tlonglong: out << PtrToValue<int64_t> (ptr); break; 211 case CCfits::Tfloat: out << PtrToValue<float> (ptr); break; 212 case CCfits::Tdouble: out << PtrToValue<double> (ptr); break; 183 213 184 214 default: … … 189 219 out << " "; 190 220 cnt++; 191 192 if (out.fail())193 {194 cerr << "ERROR - writing output: " << strerror(errno) << endl;195 return 0;196 }197 221 } 198 222 } … … 200 224 if (cnt>0) 201 225 out << endl; 226 227 if (out.fail()) 228 { 229 cerr << "ERROR - writing output: " << strerror(errno) << endl; 230 return -1; 231 } 202 232 203 233 return cnt; … … 420 450 } 421 451 422 452 // FIXME: Maybe do this when opening a table? 423 453 const vector<int> offsets = CalculateOffsets(); 424 454 if (offsets.size()==0) 425 455 return false; 426 427 const int size = offsets[offsets.size()-1];428 456 429 457 ofstream out(filename=="-"?"/dev/stdout":filename); … … 459 487 out << "#" << endl; 460 488 489 490 // Loop over all columns in our list of requested columns 491 vector<CCfits::Column*> columns; 492 for (vector<string>::const_iterator it=list.begin(); it!=list.end(); it++) 493 columns.push_back(fColMap.find(*it)->second); 494 495 496 const int size = offsets[offsets.size()-1]; 461 497 unsigned char* fitsBuffer = new unsigned char[size]; 462 498 … … 470 506 break; 471 507 } 472 WriteRow(out, list, offsets, fitsBuffer); 508 if (WriteRow(out, columns, offsets, fitsBuffer)<0) 509 { 510 status=1; 511 break; 512 } 473 513 } 474 514 delete[] fitsBuffer; 475 515 476 return status; 477 } 516 return status==0; 517 } 518 519 // -------------------------------------------------------------------------- 520 // 521 //! Perform the actual dump, based on the current parameters 522 // 523 /* 524 bool FitsDumper::Plot(const vector<string> &list) 525 { 526 for (vector<string>::const_iterator it=list.begin(); it!=list.end(); it++) 527 if (fColMap.find(*it) == fColMap.end()) 528 { 529 cerr << "WARNING - Column '" << *it << "' not found in table." << endl; 530 return false; 531 } 532 533 // FIXME: Maybe do this when opening a table? 534 const vector<int> offsets = CalculateOffsets(); 535 if (offsets.size()==0) 536 return false; 537 538 // Loop over all columns in our list of requested columns 539 const CCfits::Column *col[3] = 540 { 541 list.size()>0 ? fColMap.find(list[0])->second : 0, 542 list.size()>1 ? fColMap.find(list[1])->second : 0, 543 list.size()>2 ? fColMap.find(list[2])->second : 0 544 }; 545 546 const int size = offsets[offsets.size()-1]; 547 unsigned char* fitsBuffer = new unsigned char[size]; 548 549 const int idx = 0; 550 551 // CCfits starts counting at 1 not 0 552 const size_t pos[3] = 553 { 554 col[0] ? offsets[col[0]->index()-1] + idx*col[0]->width()*ValueTypeToSize(col[0]->type()) : 0, 555 col[1] ? offsets[col[1]->index()-1] + idx*col[1]->width()*ValueTypeToSize(col[1]->type()) : 0, 556 col[2] ? offsets[col[2]->index()-1] + idx*col[2]->width()*ValueTypeToSize(col[2]->type()) : 0 557 }; 558 559 int status = 0; 560 for (int i=1; i<=fTable->rows(); i++) 561 { 562 fits_read_tblbytes(fFile->fitsPointer(), i, 1, size, fitsBuffer, &status); 563 if (status) 564 { 565 cerr << "An error occurred while reading fits row #" << i << " error code: " << status << endl; 566 break; 567 } 568 569 try 570 { 571 const double x[3] = 572 { 573 col[0] ? PtrToDouble(fitsBuffer+pos[0], col[0]->type()) : 0, 574 col[1] ? PtrToDouble(fitsBuffer+pos[1], col[1]->type()) : 0, 575 col[2] ? PtrToDouble(fitsBuffer+pos[2], col[2]->type()) : 0 576 }; 577 } 578 catch (const runtime_error &e) 579 { 580 cerr << e.what() << endl; 581 status=1; 582 break; 583 } 584 } 585 delete[] fitsBuffer; 586 587 return status==0; 588 } 589 */ 478 590 479 591 // --------------------------------------------------------------------------
Note:
See TracChangeset
for help on using the changeset viewer.