FACT++  1.0
virtual bool factofits::WriteDrsOffsetsTable ( )
inlinevirtual

Uncompressed version of the DrsCalibration table.

Actually write the drs calibration table

Reimplemented from zofits.

Definition at line 193 of file factofits.h.

References Checksum::add(), ofits::AlignTo2880Bytes(), zofits::compressHUFFMAN16(), ofits::End(), fOffsetCalibration, IsOffsetCalibration(), FITS::kFactHuffman16, FITS::kOrderByRow, FITS::Compression::Memcpy(), FITS::TileHeader::numRows, FITS::Compression::SetBlockSize(), ofits::SetBool(), ofits::SetFloat(), ofits::SetInt(), ofits::SetStr(), FITS::TileHeader::size, Checksum::str(), TileHeader(), Checksum::val(), and ofits::WriteHeader().

194  {
195  if (!IsOffsetCalibration())
196  return false;
197 
198  const uint32_t catalog_size = sizeof(int64_t)*2;
199 
200  ofits c;
201  c.SetStr("XTENSION", "BINTABLE" , "binary table extension");
202  c.SetInt("BITPIX" , 8 , "8-bit bytes");
203  c.SetInt("NAXIS" , 2 , "2-dimensional binary table");
204  c.SetInt("NAXIS1" , catalog_size , "width of table in bytes");
205  c.SetInt("NAXIS2" , 1 , "number of rows in table");
206  c.SetInt("PCOUNT" , 0 , "size of special data area");
207  c.SetInt("GCOUNT" , 1 , "one data group (required keyword)");
208  c.SetInt("TFIELDS" , 1 , "number of fields in each row");
209  c.SetStr("CHECKSUM", "0000000000000000" , "Checksum for the whole HDU");
210  c.SetStr("DATASUM" , " 0" , "Checksum for the data block");
211  c.SetStr("EXTNAME" , "ZDrsCellOffsets" , "name of this binary table extension");
212  c.SetStr("TTYPE1" , "OffsetCalibration" , "label for field 1");
213  c.SetStr("ZFORM1" , "1474560I" , "data format of field: 2-byte INTEGER");
214  c.SetStr("TFORM1" , "1QB" , "data format of variable length bytes");
215  c.SetStr("ZCTYP1" , "FACT" , "Compression type FACT");
216 
217  c.SetBool( "ZTABLE", true, "Table is compressed");
218  c.SetInt( "ZNAXIS1", 1024*1440*2, "Width of uncompressed rows");
219  c.SetInt( "ZNAXIS2", 1, "Number of uncompressed rows");
220  c.SetInt( "ZPCOUNT", 0, "");
221  c.SetInt( "ZHEAPPTR", catalog_size, "");
222  c.SetInt( "ZTILELEN", 1, "Number of rows per tile");
223  c.SetInt( "THEAP", catalog_size, "");
224  c.SetStr( "RAWSUM", " 0", "Checksum of raw little endian data");
225  c.SetFloat("ZRATIO", 0, "Compression ratio");
226 
227  c.SetInt( "ZSHRINK", 1, "Catalog shrink factor");
228  c.End();
229 
230  c.WriteHeader(*this);
231 
232  const off_t here_I_am = tellp();
233 
234  //go after the catalog to compress and write the table data
235  seekp(here_I_am + catalog_size);
236 
237  //calculate RAWSUM
238  Checksum rawsum;
239  rawsum.add((char*)(fOffsetCalibration.data()), 1024*1440*sizeof(int16_t));
240 #if GCC_VERSION < 40603
241  c.SetStr("RAWSUM", std::to_string((long long unsigned int)(rawsum.val())));
242 #else
243  c.SetStr("RAWSUM", std::to_string(rawsum.val()));
244 #endif
245 
246  //compress data and calculate final, compressed size
247  const uint32_t compressed_header_size = sizeof(FITS::TileHeader) + sizeof(FITS::BlockHeader) + 1*sizeof(uint16_t);
248  std::vector<char> compressed_calib(1024*1440*2 + compressed_header_size + 8); //+8 for checksum;
249  char* data_start = compressed_calib.data() + compressed_header_size;
250  uint32_t compressed_size = compressHUFFMAN16(data_start, (char*)(fOffsetCalibration.data()), 1024*1440, 2, 1);;
251  compressed_size += compressed_header_size;
252 
253  //Write tile header
254  FITS::TileHeader th(0, 0);
255  std::vector<uint16_t> seq(1, FITS::kFactHuffman16);
257  th.numRows = 1;
258  th.size = compressed_size;
259  bh.SetBlockSize(compressed_size-sizeof(FITS::TileHeader));
260  memcpy(compressed_calib.data(), &(th), sizeof(FITS::TileHeader));
261  bh.Memcpy(compressed_calib.data()+sizeof(FITS::TileHeader));
262 
263  //calculate resulting compressed datasum
264  Checksum datasum;
265  memset(compressed_calib.data()+compressed_size, 0, 8-compressed_size%8);
266  datasum.add(compressed_calib.data(), compressed_size + 8-compressed_size%8);
267 
268  //write the catalog !
269  seekp(here_I_am);
270 
271  std::vector<uint64_t> catalog(2,0);
272  catalog[0] = compressed_size-sizeof(FITS::TileHeader);
273  catalog[1] = sizeof(FITS::TileHeader);
274 
275  std::vector<char> swappedCatalog(catalog_size);
276  revcpy<sizeof(int64_t)>(swappedCatalog.data(), (char*)(catalog.data()), 2);//catalog_size);
277  datasum.add(swappedCatalog.data(), catalog_size);
278 
279  write(swappedCatalog.data(), catalog_size);
280 
281  //update relevant keywords
282  c.SetFloat("ZRATIO", (float)(1024*1440*2)/(float)(compressed_size));
283  c.SetInt("PCOUNT", compressed_size);// + catalog_size);
284 
285 
286 //cout << "DEBUG: compressed_size=" << compressed_size << " " << compressed_size%2880 << " " << catalog_size << endl;
287 
288 
289 #if GCC_VERSION < 40603
290  c.SetStr("DATASUM", std::to_string((long long unsigned int)(datasum.val())));
291 #else
292  c.SetStr("DATASUM", std::to_string(datasum.val()));
293 #endif
294 
295  datasum += c.WriteHeader(*this);
296 
297  c.SetStr("CHECKSUM", datasum.str());
298 
299  c.WriteHeader(*this);
300 
301  //write the compressed data
302  seekp(here_I_am + catalog_size);
303  write(compressed_calib.data(), compressed_size);
304 
306 
307  return good();
308  }
bool SetInt(const std::string &key, int64_t i, const std::string &comment="")
Definition: ofits.h:535
void AlignTo2880Bytes()
Definition: ofits.h:961
uint32_t compressHUFFMAN16(char *dest, const char *src, uint32_t numRows, uint32_t sizeOfElems, uint32_t numRowElems)
Definition: zofits.h:900
bool SetBool(const std::string &key, bool b, const std::string &comment="")
Definition: ofits.h:516
uint32_t val() const
Definition: checksum.h:20
Definition: ofits.h:29
bool SetStr(const std::string &key, std::string s, const std::string &comment="")
Definition: ofits.h:526
std::string str(bool complm=true) const
Definition: checksum.h:148
std::vector< int16_t > fOffsetCalibration
The calibration itself.
Definition: factofits.h:363
bool SetFloat(const std::string &key, double f, int p, const std::string &comment="")
Definition: ofits.h:543
void End()
Definition: ofits.h:588
virtual bool IsOffsetCalibration()
whether or not a calibration was given to the file writer
Definition: factofits.h:42
bool add(const char *buf, size_t len, bool big_endian=true)
Definition: checksum.h:49
Checksum WriteHeader(std::ostream &fout)
Definition: ofits.h:782
TileHeader()
Definition: FITS.h:75

+ Here is the call graph for this function: