Ignore:
Timestamp:
10/18/13 16:55:40 (11 years ago)
Author:
tbretz
Message:
Removed 'using namespace' from header; some minor simplifications
File:
1 edited

Legend:

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

    r17253 r17261  
    1414#include "FITS.h"
    1515
    16 using namespace FITS;
    17 
    1816#ifndef __MARS__
    1917namespace std
     
    2624
    2725    // 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)
    3428    {
    3529        open(fname.c_str());
     
    3933
    4034    // 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)
    4737    {
    4838        open(fname.c_str());
     
    6757        if (HasKey("RAWSUM"))
    6858        {
    69                 ostringstream str;
     59                std::ostringstream str;
    7060                str << fRawsum.val();
    7161                rawsum = (GetStr("RAWSUM") == str.str());
     
    117107
    118108        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++)
    120110            {
    121111                if (it->comp == kCompFACT)
     
    131121            }
    132122
    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
    136125        //Get compressed specific keywords
    137126        fNumTiles       = fTable.is_compressed ? GetInt("NAXIS2") : 0;
     
    160149    }
    161150
    162     vector<char> fBuffer;           ///<store the uncompressed rows
    163     vector<char> fTransposedBuffer; ///<intermediate buffer to transpose the rows
    164     vector<char> fCompressedBuffer; ///<compressed rows
    165     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.
    166155
    167156    size_t fNumTiles;       ///< Total number of tiles
     
    169158    int64_t fCurrentRow;    ///< current row in memory signed because we need -1
    170159
    171     streamoff fHeapOff;     ///< offset from the beginning of the file of the binary data
     160    streamoff fHeapOff;           ///< offset from the beginning of the file of the binary data
    172161    streamoff fHeapFromDataStart; ///< offset from the beginning of the data table
    173162
    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 tile
    176     vector<vector<size_t>>                 fTileOffsets; ///< offset from start of tile of a given compressed column
     163    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
    177166
    178167    Checksum fRawsum;   ///< Checksum of the uncompressed, raw data
     
    183172        uint32_t buffer_size = fTable.bytes_per_row*fNumRowsPerTile;
    184173        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);
    190186
    191187        fBuffer.resize(buffer_size);
     
    198194    void ReadCatalog()
    199195    {
    200         vector<char> readBuf(16);
     196        std::vector<char> readBuf(16);
    201197        fCatalog.resize(fNumTiles);
    202198
     
    265261        const size_t catSize = fTable.GetHeapShift() + fTable.total_bytes;
    266262
    267         vector<char> buf(catSize);
     263        std::vector<char> buf(catSize);
    268264        read(buf.data(), catSize);
    269265
     
    297293        {
    298294            //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);
    300296
    301297            //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);
    303299
    304300            seekg(fHeapOff+tileStart);
     
    334330
    335331            //uncompress it
    336             UncompressBuffer(requestedTile, thisRoundNumRows, offset+sizeof(TileHeader));
     332            UncompressBuffer(requestedTile, thisRoundNumRows, offset+sizeof(FITS::TileHeader));
    337333
    338334            // pointer to column (source buffer)
     
    340336
    341337            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++)
    343339            {
    344340                char *buffer = fBuffer.data() + it->offset; // pointer to column (destination buffer)
     
    346342                switch (fColumnOrdering[i])
    347343                {
    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
    351358                        {
    352                             memcpy(dest, src, it->bytes);
    353                             src += it->bytes;  // next column
    354                         }
    355                     break;
    356 
    357                     case kOrderByCol:
    358                         // transposed copy
    359                         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-row
    362                             {
    363359                                memcpy(dest, src, it->size);
    364360                                src += it->size; // next element
    365                             }
    366361                        }
     362                    }
    367363                    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
    376373                    break;
    377374                };
     
    399396                                 uint32_t    numChunks)
    400397    {
    401         vector<uint16_t> uncompressed;
     398        std::vector<uint16_t> uncompressed;
    402399
    403400        //read compressed sizes (one per row)
     
    448445            const int64_t compressedOffset = fTileOffsets[catalogCurrentRow][i]+offset;
    449446
    450             const BlockHeader* head = reinterpret_cast<BlockHeader*>(&fCompressedBuffer[compressedOffset]);
     447            const FITS::BlockHeader* head = reinterpret_cast<FITS::BlockHeader*>(&fCompressedBuffer[compressedOffset]);
    451448
    452449            fColumnOrdering[i] = head->ordering;
    453450
    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;
    458455
    459456            for (int32_t j=head->numProcs-1;j >= 0; j--)
     
    463460                switch (head->processings[j])
    464461                {
    465                 case kFactRaw:
     462                case FITS::kFactRaw:
    466463                    sizeWritten = UncompressUNCOMPRESSED(dest, src, numRows*numCols, col.size);
    467464                    break;
    468465
    469                 case kFactSmoothing:
     466                case FITS::kFactSmoothing:
    470467                    sizeWritten = UnApplySMOOTHING(reinterpret_cast<int16_t*>(dest), numRows*numCols);
    471468                    break;
    472469
    473                 case kFactHuffman16:
     470                case FITS::kFactHuffman16:
    474471                    sizeWritten = UncompressHUFFMAN16(dest, src, numRows);
    475472                    break;
     
    477474                default:
    478475                    clear(rdstate()|ios::badbit);
    479                     ostringstream str;
     476
     477                    std::ostringstream str;
    480478                    str << "Unkown processing applied to data. Col " << i << " proc " << j << " out of " << (int)head->numProcs;
    481479#ifdef __EXCEPTIONS
     
    505503        size_t numCols = fTable.num_cols;
    506504
    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
    511510        streamoff offsetInHeap = 0;
    512511        //skip through the heap
    513512        while (true)
    514513        {
    515             read((char*)(&tileHead), sizeof(TileHeader));
     514            read((char*)(&tileHead), sizeof(FITS::TileHeader));
    516515            //end of file
    517516            if (!good())
     
    526525
    527526            //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);
    530529
    531530            //skip through the columns
     
    535534                if (fTable.sorted_cols[i].num == 0)
    536535                {
    537                     catalog.back().push_back(make_pair(0,0));
     536                    catalog.back().emplace_back(0,0);
    538537                    continue;
    539538                }
     539
    540540                //read column header
    541                 read((char*)(&columnHead), sizeof(BlockHeader));
     541                read((char*)(&columnHead), sizeof(FITS::BlockHeader));
     542
    542543                //corrupted tile
    543544                if (!good())
    544545                    break;
     546
    545547                catalog.back().emplace_back((int64_t)(columnHead.size),offsetInHeap);
    546548                offsetInHeap += columnHead.size;
     
    561563        {
    562564            clear(rdstate()|ios::badbit);
    563             ostringstream str;
     565            std::ostringstream str;
    564566            str << "Heap data does not agree with header: " << numRows << " calculated vs " << fTable.num_rows << " from header.";
    565567#ifdef __EXCEPTIONS
Note: See TracChangeset for help on using the changeset viewer.