Ignore:
Timestamp:
10/18/13 13:49:17 (11 years ago)
Author:
lyard
Message:
zofits with catalog shrinking and compressed calibration table
File:
1 edited

Legend:

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

    r17242 r17253  
    148148        }
    149149
    150         ///Actually write the drs calibration table
    151         virtual bool WriteDrsOffsetsTable()
     150        ///Uncompressed version of the DrsCalibration table
     151 /*       virtual bool WriteDrsOffsetsTable()
    152152        {
    153153            if (!IsOffsetCalibration())
     
    195195
    196196            return good();
    197         }
    198 
     197        }*/
     198        ///Actually write the drs calibration table
     199        virtual bool WriteDrsOffsetsTable()
     200        {
     201            if (!IsOffsetCalibration())
     202                return false;
     203
     204            const uint32_t catalog_size = sizeof(int64_t)*2;
     205
     206            ofits c;
     207            c.SetStr("XTENSION", "BINTABLE"            , "binary table extension");
     208            c.SetInt("BITPIX"  , 8                     , "8-bit bytes");
     209            c.SetInt("NAXIS"   , 2                     , "2-dimensional binary table");
     210            c.SetInt("NAXIS1"  , catalog_size           , "width of table in bytes");
     211            c.SetInt("NAXIS2"  , 1                     , "number of rows in table");
     212            c.SetInt("PCOUNT"  , 0                     , "size of special data area");
     213            c.SetInt("GCOUNT"  , 1                     , "one data group (required keyword)");
     214            c.SetInt("TFIELDS" , 1                     , "number of fields in each row");
     215            c.SetStr("CHECKSUM", "0000000000000000"    , "Checksum for the whole HDU");
     216            c.SetStr("DATASUM" ,  "         0"         , "Checksum for the data block");
     217            c.SetStr("EXTNAME" , "ZDrsCellOffsets"     , "name of this binary table extension");
     218            c.SetStr("TTYPE1"  , "OffsetCalibration"   , "label for field   1");
     219            c.SetStr("ZFORM1"  , "1474560I"            , "data format of field: 2-byte INTEGER");
     220            c.SetStr("TFORM1"  , "1QB"                 , "data format of variable length bytes");
     221            c.SetStr("ZCTYP1"  , "FACT"                , "Compression type FACT");
     222
     223            c.SetBool( "ZTABLE",   true,            "Table is compressed");
     224            c.SetInt(  "ZNAXIS1",  1024*1440*2,    "Width of uncompressed rows");
     225            c.SetInt(  "ZNAXIS2",  1,               "Number of uncompressed rows");
     226            c.SetInt(  "ZPCOUNT",  0,               "");
     227            c.SetInt(  "ZHEAPPTR", catalog_size,               "");
     228            c.SetInt(  "ZTILELEN", 1, "Number of rows per tile");
     229            c.SetInt(  "THEAP",    catalog_size,               "");
     230            c.SetStr(  "RAWSUM",   "         0",    "Checksum of raw little endian data");
     231            c.SetFloat("ZRATIO",   0,               "Compression ratio");
     232            c.SetInt(  "ZSHRINK",  1,               "Catalog shrink factor");
     233            c.End();
     234
     235            c.WriteHeader(*this);
     236
     237            const off_t here_I_am = tellp();
     238
     239            //go after the catalog to compress and write the table data
     240            seekp(here_I_am + catalog_size);
     241
     242            //calculate RAWSUM
     243            Checksum rawsum;
     244            rawsum.add((char*)(fOffsetCalibration.data()), 1024*1440*sizeof(int16_t));
     245            ostringstream sum_str;
     246            sum_str << rawsum.val();
     247            c.SetStr("RAWSUM", sum_str.str());
     248
     249            //compress data and calculate final, compressed size
     250            const uint32_t compressed_header_size = sizeof(TileHeader) + sizeof(BlockHeader) + 1*sizeof(uint16_t);
     251            vector<char> compressed_calib(1024*1440*2 + compressed_header_size + 8); //+8 for checksum;
     252            char* data_start = compressed_calib.data() + compressed_header_size;
     253            uint32_t compressed_size = compressHUFFMAN16(data_start, (char*)(fOffsetCalibration.data()), 1024*1440, 2, 1);;
     254            compressed_size += compressed_header_size;
     255
     256            //Write tile header
     257            TileHeader th;
     258            vector<uint16_t> seq(1, kFactHuffman16);
     259            Compression bh(seq, kOrderByRow);
     260            th.numRows = 1;
     261            th.size = compressed_size;
     262            bh.SetBlockSize(compressed_size-sizeof(TileHeader));
     263            memcpy(compressed_calib.data(), &(th), sizeof(TileHeader));
     264            bh.Memcpy(compressed_calib.data()+sizeof(TileHeader));
     265
     266            //calculate resulting compressed datasum
     267            Checksum datasum;
     268            memset(compressed_calib.data()+compressed_size, 0, 8-compressed_size%8);
     269            datasum.add(compressed_calib.data(), compressed_size + 8-compressed_size%8);
     270
     271            //write the catalog !
     272            seekp(here_I_am);
     273
     274            vector<uint64_t> catalog(2,0);
     275            catalog[0] = compressed_size-sizeof(TileHeader);
     276            catalog[1] = sizeof(TileHeader);
     277
     278            vector<char> swappedCatalog(catalog_size);
     279            revcpy<sizeof(int64_t)>(swappedCatalog.data(), (char*)(catalog.data()), 2);//catalog_size);
     280            datasum.add(swappedCatalog.data(), catalog_size);
     281
     282            write(swappedCatalog.data(), catalog_size);
     283
     284            //update relevant keywords
     285            c.SetFloat("ZRATIO", (float)(1024*1440*2)/(float)(compressed_size));
     286            c.SetInt("PCOUNT", compressed_size + catalog_size);
     287
     288            sum_str.str("");
     289            sum_str << datasum.val();
     290            c.SetStr("DATASUM", sum_str.str());
     291
     292            datasum += c.WriteHeader(*this);
     293
     294            c.SetStr("CHECKSUM", datasum.str());
     295
     296            c.WriteHeader(*this);
     297
     298            //write the compressed data
     299            seekp(here_I_am + catalog_size);
     300            write(compressed_calib.data(), compressed_size);
     301
     302            AlignTo2880Bytes();
     303
     304            return good();
     305        }
    199306        ///Apply the drs offset calibration (overload of super-method)
    200307        virtual void DrsOffsetCalibrate(char* target_location)
Note: See TracChangeset for help on using the changeset viewer.