Changeset 17261 for trunk/Mars/mcore
- Timestamp:
- 10/18/13 16:55:40 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Mars/mcore/zfits.h
r17253 r17261 14 14 #include "FITS.h" 15 15 16 using namespace FITS;17 18 16 #ifndef __MARS__ 19 17 namespace std … … 26 24 27 25 // Basic constructor 28 zfits(const string& fname, const string& tableName="", bool force=false) : fits(), 29 fNumTiles(0), 30 fNumRowsPerTile(0), 31 fCurrentRow(-1), 32 fHeapOff(0), 33 fTileSize(0) 26 zfits(const string& fname, const string& tableName="", bool force=false) 27 : fNumTiles(0), fNumRowsPerTile(0), fCurrentRow(-1), fHeapOff(0), fTileSize(0) 34 28 { 35 29 open(fname.c_str()); … … 39 33 40 34 // Alternative contstructor 41 zfits(const string& fname, const string& fout, const string& tableName, bool force=false) : fits(), 42 fNumTiles(0), 43 fNumRowsPerTile(0), 44 fCurrentRow(-1), 45 fHeapOff(0), 46 fTileSize(0) 35 zfits(const string& fname, const string& fout, const string& tableName, bool force=false) 36 : fNumTiles(0), fNumRowsPerTile(0), fCurrentRow(-1), fHeapOff(0), fTileSize(0) 47 37 { 48 38 open(fname.c_str()); … … 67 57 if (HasKey("RAWSUM")) 68 58 { 69 ostringstream str;59 std::ostringstream str; 70 60 str << fRawsum.val(); 71 61 rawsum = (GetStr("RAWSUM") == str.str()); … … 117 107 118 108 if (fTable.is_compressed) 119 for (auto it=fTable.sorted_cols.begin(); it!= fTable.sorted_cols.end(); it++)109 for (auto it=fTable.sorted_cols.cbegin(); it!= fTable.sorted_cols.cend(); it++) 120 110 { 121 111 if (it->comp == kCompFACT) … … 131 121 } 132 122 133 fColumnOrdering.resize(fTable.sorted_cols.size()); 134 for (auto it=fColumnOrdering.begin(); it != fColumnOrdering.end(); it++) 135 (*it) = kOrderByRow; 123 fColumnOrdering.resize(fTable.sorted_cols.size(), FITS::kOrderByRow); 124 136 125 //Get compressed specific keywords 137 126 fNumTiles = fTable.is_compressed ? GetInt("NAXIS2") : 0; … … 160 149 } 161 150 162 vector<char> fBuffer; ///<store the uncompressed rows163 vector<char> fTransposedBuffer; ///<intermediate buffer to transpose the rows164 vector<char> fCompressedBuffer; ///<compressed rows165 vector<char> fColumnOrdering; ///< ordering of the column's rows. Can change from tile to tile.151 std::vector<char> fBuffer; ///<store the uncompressed rows 152 std::vector<char> fTransposedBuffer; ///<intermediate buffer to transpose the rows 153 std::vector<char> fCompressedBuffer; ///<compressed rows 154 std::vector<char> fColumnOrdering; ///< ordering of the column's rows. Can change from tile to tile. 166 155 167 156 size_t fNumTiles; ///< Total number of tiles … … 169 158 int64_t fCurrentRow; ///< current row in memory signed because we need -1 170 159 171 streamoff fHeapOff; ///< offset from the beginning of the file of the binary data160 streamoff fHeapOff; ///< offset from the beginning of the file of the binary data 172 161 streamoff fHeapFromDataStart; ///< offset from the beginning of the data table 173 162 174 vector<vector<pair<int64_t, int64_t>>> fCatalog;///< Catalog, i.e. the main table that points to the compressed data.175 vector<size_t> fTileSize;///< size in bytes of each compressed tile176 vector<vector<size_t>> fTileOffsets; ///< offset from start of tile of a given compressed column163 std::vector<std::vector<pair<int64_t, int64_t>>> fCatalog; ///< Catalog, i.e. the main table that points to the compressed data. 164 std::vector<size_t> fTileSize; ///< size in bytes of each compressed tile 165 std::vector<std::vector<size_t>> fTileOffsets; ///< offset from start of tile of a given compressed column 177 166 178 167 Checksum fRawsum; ///< Checksum of the uncompressed, raw data … … 183 172 uint32_t buffer_size = fTable.bytes_per_row*fNumRowsPerTile; 184 173 uint32_t compressed_buffer_size = fTable.bytes_per_row*fNumRowsPerTile + 185 fTable.num_cols*(sizeof(BlockHeader)+256) + //use a bit more memory for block headers. 256 char coding the compression sequence max. 186 sizeof(TileHeader) + //a bit more for the tile headers 187 8;//and a bit more for checksuming 188 if (buffer_size % 4 != 0) buffer_size += 4 - (buffer_size%4); 189 if (compressed_buffer_size % 4 != 0) compressed_buffer_size += 4 - (compressed_buffer_size%4); 174 //use a bit more memory for block headers. 256 char coding the compression sequence max. 175 fTable.num_cols*(sizeof(FITS::BlockHeader)+256) + 176 //a bit more for the tile headers 177 sizeof(FITS::TileHeader) + 178 //and a bit more for checksuming 179 8; 180 181 if (buffer_size % 4 != 0) 182 buffer_size += 4 - (buffer_size%4); 183 184 if (compressed_buffer_size % 4 != 0) 185 compressed_buffer_size += 4 - (compressed_buffer_size%4); 190 186 191 187 fBuffer.resize(buffer_size); … … 198 194 void ReadCatalog() 199 195 { 200 vector<char> readBuf(16);196 std::vector<char> readBuf(16); 201 197 fCatalog.resize(fNumTiles); 202 198 … … 265 261 const size_t catSize = fTable.GetHeapShift() + fTable.total_bytes; 266 262 267 vector<char> buf(catSize);263 std::vector<char> buf(catSize); 268 264 read(buf.data(), catSize); 269 265 … … 297 293 { 298 294 //read yet another chunk from the file 299 const int64_t sizeToRead = fTileSize[requestedTile] + sizeof( TileHeader);295 const int64_t sizeToRead = fTileSize[requestedTile] + sizeof(FITS::TileHeader); 300 296 301 297 //skip to the beginning of the tile 302 const int64_t tileStart = fCatalog[requestedTile][0].second - sizeof( TileHeader);298 const int64_t tileStart = fCatalog[requestedTile][0].second - sizeof(FITS::TileHeader); 303 299 304 300 seekg(fHeapOff+tileStart); … … 334 330 335 331 //uncompress it 336 UncompressBuffer(requestedTile, thisRoundNumRows, offset+sizeof( TileHeader));332 UncompressBuffer(requestedTile, thisRoundNumRows, offset+sizeof(FITS::TileHeader)); 337 333 338 334 // pointer to column (source buffer) … … 340 336 341 337 uint32_t i=0; 342 for (auto it=fTable.sorted_cols. begin(); it!=fTable.sorted_cols.end(); it++, i++)338 for (auto it=fTable.sorted_cols.cbegin(); it!=fTable.sorted_cols.cend(); it++, i++) 343 339 { 344 340 char *buffer = fBuffer.data() + it->offset; // pointer to column (destination buffer) … … 346 342 switch (fColumnOrdering[i]) 347 343 { 348 case kOrderByRow: 349 // regular, "semi-transposed" copy 350 for (char *dest=buffer; dest<buffer+thisRoundNumRows*fTable.bytes_per_row; dest+=fTable.bytes_per_row) // row-by-row 344 case FITS::kOrderByRow: 345 // regular, "semi-transposed" copy 346 for (char *dest=buffer; dest<buffer+thisRoundNumRows*fTable.bytes_per_row; dest+=fTable.bytes_per_row) // row-by-row 347 { 348 memcpy(dest, src, it->bytes); 349 src += it->bytes; // next column 350 } 351 break; 352 353 case FITS::kOrderByCol: 354 // transposed copy 355 for (char *elem=buffer; elem<buffer+it->bytes; elem+=it->size) // element-by-element (arrays) 356 { 357 for (char *dest=elem; dest<elem+thisRoundNumRows*fTable.bytes_per_row; dest+=fTable.bytes_per_row) // row-by-row 351 358 { 352 memcpy(dest, src, it->bytes);353 src += it->bytes; // next column354 }355 break;356 357 case kOrderByCol:358 // transposed copy359 for (char *elem=buffer; elem<buffer+it->bytes; elem+=it->size) // element-by-element (arrays)360 {361 for (char *dest=elem; dest<elem+thisRoundNumRows*fTable.bytes_per_row; dest+=fTable.bytes_per_row) // row-by-row362 {363 359 memcpy(dest, src, it->size); 364 360 src += it->size; // next element 365 }366 361 } 362 } 367 363 break; 368 default: 369 clear(rdstate()|ios::badbit); 370 #ifdef __EXCEPTIONS 371 throw runtime_error("Unkown column ordering scheme found"); 372 #else 373 gLog << ___err___ << "ERROR - unkown column ordering scheme" << endl; 374 return false; 375 #endif 364 365 default: 366 clear(rdstate()|ios::badbit); 367 #ifdef __EXCEPTIONS 368 throw runtime_error("Unkown column ordering scheme found"); 369 #else 370 gLog << ___err___ << "ERROR - unkown column ordering scheme" << endl; 371 return false; 372 #endif 376 373 break; 377 374 }; … … 399 396 uint32_t numChunks) 400 397 { 401 vector<uint16_t> uncompressed;398 std::vector<uint16_t> uncompressed; 402 399 403 400 //read compressed sizes (one per row) … … 448 445 const int64_t compressedOffset = fTileOffsets[catalogCurrentRow][i]+offset; 449 446 450 const BlockHeader* head = reinterpret_cast<BlockHeader*>(&fCompressedBuffer[compressedOffset]);447 const FITS::BlockHeader* head = reinterpret_cast<FITS::BlockHeader*>(&fCompressedBuffer[compressedOffset]); 451 448 452 449 fColumnOrdering[i] = head->ordering; 453 450 454 const uint32_t numRows = (head->ordering== kOrderByRow) ? thisRoundNumRows : col.num;455 const uint32_t numCols = (head->ordering== kOrderByCol) ? thisRoundNumRows : col.num;456 457 const char *src = fCompressedBuffer.data()+compressedOffset+sizeof( BlockHeader)+sizeof(uint16_t)*head->numProcs;451 const uint32_t numRows = (head->ordering==FITS::kOrderByRow) ? thisRoundNumRows : col.num; 452 const uint32_t numCols = (head->ordering==FITS::kOrderByCol) ? thisRoundNumRows : col.num; 453 454 const char *src = fCompressedBuffer.data()+compressedOffset+sizeof(FITS::BlockHeader)+sizeof(uint16_t)*head->numProcs; 458 455 459 456 for (int32_t j=head->numProcs-1;j >= 0; j--) … … 463 460 switch (head->processings[j]) 464 461 { 465 case kFactRaw:462 case FITS::kFactRaw: 466 463 sizeWritten = UncompressUNCOMPRESSED(dest, src, numRows*numCols, col.size); 467 464 break; 468 465 469 case kFactSmoothing:466 case FITS::kFactSmoothing: 470 467 sizeWritten = UnApplySMOOTHING(reinterpret_cast<int16_t*>(dest), numRows*numCols); 471 468 break; 472 469 473 case kFactHuffman16:470 case FITS::kFactHuffman16: 474 471 sizeWritten = UncompressHUFFMAN16(dest, src, numRows); 475 472 break; … … 477 474 default: 478 475 clear(rdstate()|ios::badbit); 479 ostringstream str; 476 477 std::ostringstream str; 480 478 str << "Unkown processing applied to data. Col " << i << " proc " << j << " out of " << (int)head->numProcs; 481 479 #ifdef __EXCEPTIONS … … 505 503 size_t numCols = fTable.num_cols; 506 504 507 vector<vector<pair<int64_t, int64_t> > > catalog; 508 509 TileHeader tileHead; 510 BlockHeader columnHead; 505 std::vector<std::vector<std::pair<int64_t, int64_t> > > catalog; 506 507 FITS::TileHeader tileHead; 508 FITS::BlockHeader columnHead; 509 511 510 streamoff offsetInHeap = 0; 512 511 //skip through the heap 513 512 while (true) 514 513 { 515 read((char*)(&tileHead), sizeof( TileHeader));514 read((char*)(&tileHead), sizeof(FITS::TileHeader)); 516 515 //end of file 517 516 if (!good()) … … 526 525 527 526 //a new tile begins here 528 catalog. push_back(vector<pair<int64_t, int64_t> >(0));529 offsetInHeap += sizeof( TileHeader);527 catalog.emplace_back(std::vector<std::pair<int64_t, int64_t> >(0)); 528 offsetInHeap += sizeof(FITS::TileHeader); 530 529 531 530 //skip through the columns … … 535 534 if (fTable.sorted_cols[i].num == 0) 536 535 { 537 catalog.back(). push_back(make_pair(0,0));536 catalog.back().emplace_back(0,0); 538 537 continue; 539 538 } 539 540 540 //read column header 541 read((char*)(&columnHead), sizeof(BlockHeader)); 541 read((char*)(&columnHead), sizeof(FITS::BlockHeader)); 542 542 543 //corrupted tile 543 544 if (!good()) 544 545 break; 546 545 547 catalog.back().emplace_back((int64_t)(columnHead.size),offsetInHeap); 546 548 offsetInHeap += columnHead.size; … … 561 563 { 562 564 clear(rdstate()|ios::badbit); 563 ostringstream str;565 std::ostringstream str; 564 566 str << "Heap data does not agree with header: " << numRows << " calculated vs " << fTable.num_rows << " from header."; 565 567 #ifdef __EXCEPTIONS
Note:
See TracChangeset
for help on using the changeset viewer.