Changeset 16882 for trunk/Mars/mcore


Ignore:
Timestamp:
06/21/13 17:08:18 (11 years ago)
Author:
lyard
Message:
added correct checksum calculation for compressed fits reader (zfits)
Location:
trunk/Mars/mcore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mcore/fits.h

    r16847 r16882  
    758758                clear(rdstate()|ios::badbit);
    759759    }
    760     uint8_t ReadRow(size_t row)
    761     {
    762         // For the checksum we need everything to be correctly aligned
    763         const uint8_t offset = (row*fTable.bytes_per_row)%4;
    764 
    765         auto ib = fBufferRow.begin();
    766         auto ie = fBufferRow.end();
     760
     761    void ZeroBufferForChecksum(vector<char>& vec, const uint64_t extraZeros=0)
     762    {
     763        auto ib = vec.begin();
     764        auto ie = vec.end();
     765
    767766        *ib++ = 0;
    768767        *ib++ = 0;
     
    770769        *ib   = 0;
    771770
    772         *--ie = 0;
    773         *--ie = 0;
    774         *--ie = 0;
    775         *--ie = 0;
    776         *--ie = 0;
    777         *--ie = 0;
    778         *--ie = 0;
    779         *--ie = 0;
     771        for (uint64_t i=0;i<extraZeros+8;i++)
     772            *--ie = 0;
     773    }
     774
     775    uint8_t ReadRow(size_t row)
     776    {
     777        // For the checksum we need everything to be correctly aligned
     778        const uint8_t offset = (row*fTable.bytes_per_row)%4;
     779
     780        ZeroBufferForChecksum(fBufferRow);
    780781
    781782        StageRow(row, fBufferRow.data()+offset);
  • trunk/Mars/mcore/zfits.h

    r16867 r16882  
    188188    size_t fNumTiles;       ///< Total number of tiles
    189189    size_t fNumRowsPerTile; ///< Number of rows per compressed tile
    190     size_t fCurrentRow;     ///< current row in memory.
     190    int64_t fCurrentRow;    ///< current row in memory signed because we need -1
    191191
    192192    streamoff fHeapOff;     ///< offset from the beginning of the file of the binary data
     193    streamoff fHeapFromDataStart; ///< offset from the beginning of the data table
    193194
    194195    vector<vector<pair<int64_t, int64_t> > > fCatalog;///< Catalog, i.e. the main table that points to the compressed data.
     
    202203
    203204        fTransposedBuffer.resize(fTable.bytes_per_row*fNumRowsPerTile);
    204         fCompressedBuffer.resize(fTable.bytes_per_row*fNumRowsPerTile + fTable.num_cols*(sizeof(BlockHeader)+256)); //use a bit more memory for block headers
     205        fCompressedBuffer.resize(fTable.bytes_per_row*fNumRowsPerTile +
     206                                 fTable.num_cols*(sizeof(BlockHeader)+256) + //use a bit more memory for block headers. 256 char coding the compression sequence max.
     207                                 sizeof(TileHeader), //a bit more for the tile headers
     208                                 8); //and a bit more for checksuming
    205209    }
    206210
     
    208212    void ReadCatalog()
    209213    {
    210         char readBuf[16];
     214        vector<char> readBuf(16);
    211215        fCatalog.resize(fNumTiles);
    212216
    213217        const streampos catalogStart = tellg();
     218
     219fChkData.reset();
    214220
    215221        //do the actual reading
     
    217223            for (uint32_t j=0;j<fTable.num_cols;j++)
    218224            {
    219                 read(readBuf, 2*sizeof(int64_t));
    220 
     225                read(readBuf.data(), 2*sizeof(int64_t));
     226                fChkData.add(readBuf);
    221227                //swap the bytes
    222228                int64_t tempValues[2] = {0,0};
    223                 revcpy<8>(reinterpret_cast<char*>(tempValues), readBuf, 2);
     229                revcpy<8>(reinterpret_cast<char*>(tempValues), readBuf.data(), 2);
    224230                if (tempValues[0] < 0 || tempValues[1] < 0)
    225231                {
     
    250256        //see if there is a gap before heap data
    251257        fHeapOff = tellg()+fTable.GetHeapShift();
     258        fHeapFromDataStart = fNumTiles*fTable.num_cols*2*sizeof(int64_t) + fTable.GetHeapShift();
    252259
    253260        if (!fCopy.is_open())
     
    269276    //overrides fits.h method with empty one
    270277    //work is done in ReadBinaryRow because it requires volatile data from ReadBinaryRow
    271     virtual void WriteRowToCopyFile(size_t row)
    272     {
    273         if (row == fRow+1)
    274             fChkData.add(fBufferRow);
     278    virtual void WriteRowToCopyFile(size_t )
     279    {
     280
    275281    }
    276282
     
    284290        const uint32_t currentTile   = fCurrentRow/fNumRowsPerTile;
    285291
     292        bool addCheckSum = ((requestedTile == currentTile+1) || (fCurrentRow == -1));
     293
    286294        fCurrentRow = rowNum;
    287 
    288295        //should we read yet another chunk of data ?
    289296        if (requestedTile != currentTile)
    290297        {
    291298            //read yet another chunk from the file
    292             const int64_t sizeToRead = fTileSize[requestedTile];
     299            const int64_t sizeToRead = fTileSize[requestedTile] + sizeof(TileHeader);
    293300
    294301            //skip to the beginning of the tile
    295             seekg(fHeapOff+fCatalog[requestedTile][0].second - sizeof(TileHeader));
    296             TileHeader tHead;
    297             read((char*)(&tHead), sizeof(TileHeader));
    298             read(fCompressedBuffer.data(), sizeToRead);
     302            const int64_t tileStart =  fCatalog[requestedTile][0].second - sizeof(TileHeader);
     303
     304            seekg(fHeapOff+tileStart);
     305
     306            //calculate the 32 bits offset of the current tile.
     307            const uint32_t offset = (tileStart + fHeapFromDataStart)%4;
     308
     309            //point the tile header where it should be
     310            //we ain't checking the header now
     311//            TileHeader* tHead = reinterpret_cast<TileHeader*>(fCompressedBuffer.data()+offset);
     312
     313            ZeroBufferForChecksum(fCompressedBuffer, fCompressedBuffer.size()-(sizeToRead+offset+8));
     314
     315            //read one tile from disk
     316            read(fCompressedBuffer.data()+offset, sizeToRead);
     317
     318            if (addCheckSum)
     319                fChkData.add(fCompressedBuffer);
    299320
    300321            if (requestedTile == currentTile+1 &&
     
    302323                fCopy.good())
    303324            {
    304                 fCopy.write((char*)(&tHead), sizeof(TileHeader));
    305                 fCopy.write(fCompressedBuffer.data(), sizeToRead);
     325                fCopy.write(fCompressedBuffer.data()+offset, sizeToRead);
    306326                if (!fCopy)
    307327                    clear(rdstate()|ios::badbit);
     
    314334
    315335            //uncompress it
    316             UncompressBuffer(requestedTile, thisRoundNumRows);
     336            UncompressBuffer(requestedTile, thisRoundNumRows, offset+sizeof(TileHeader));
    317337
    318338            // pointer to column (source buffer)
     
    413433    // Data has been read from disk. Uncompress it !
    414434    void UncompressBuffer(const uint32_t &catalogCurrentRow,
    415                           const uint32_t &thisRoundNumRows)
     435                          const uint32_t &thisRoundNumRows,
     436                          const uint32_t offset)
    416437    {
    417438        char *dest = fTransposedBuffer.data();
     
    425446
    426447            //get the compression flag
    427             const int64_t compressedOffset = fTileOffsets[catalogCurrentRow][i];
     448            const int64_t compressedOffset = fTileOffsets[catalogCurrentRow][i]+offset;
    428449
    429450            const BlockHeader* head = reinterpret_cast<BlockHeader*>(&fCompressedBuffer[compressedOffset]);
Note: See TracChangeset for help on using the changeset viewer.