FACT++  1.0
bool zofits::close ( )
inlinevirtual

close an open file.

Returns
the state of the file

Reimplemented from ofits.

Definition at line 468 of file zofits.h.

References ___err___, gLog, TileHeader(), and Checksum::val().

469  {
470  // stop compression and write threads
471  for (auto it=fCompressionQueues.begin(); it != fCompressionQueues.end(); it++)
472  it->wait();
473 
474  fWriteToDiskQueue.wait();
475 
476  if (tellp() < 0)
477  return false;
478 
479 #ifdef __EXCEPTIONS
480  //check if something hapenned while the compression threads were working
481  //if so, re-throw the exception that was generated
483  std::rethrow_exception(fThreadsException);
484 #endif
485 
486  //write the last tile of data (if any)
487  if (fErrno==0 && fTable.num_rows%fNumRowsPerTile!=0)
488  {
489  fWriteToDiskQueue.enablePromptExecution();
490  fCompressionQueues.front().enablePromptExecution();
491  fCompressionQueues.front().emplace(InitNextCompression());
492  }
493 
495 
496  int64_t heap_size = 0;
497  int64_t compressed_offset = 0;
498  for (auto it=fCatalog.begin(); it!= fCatalog.end(); it++)
499  {
500  compressed_offset += sizeof(FITS::TileHeader);
501  heap_size += sizeof(FITS::TileHeader);
502  for (uint32_t j=0; j<it->size(); j++)
503  {
504  heap_size += (*it)[j].first;
505  (*it)[j].second = compressed_offset;
506  compressed_offset += (*it)[j].first;
507  if ((*it)[j].first == 0)
508  (*it)[j].second = 0;
509  }
510  }
511 
512  const uint32_t shrink_factor = ShrinkCatalog();
513 
514  //update header keywords
515  SetInt("ZNAXIS1", fRealRowWidth);
516  SetInt("ZNAXIS2", fTable.num_rows);
517 
518  SetInt("ZHEAPPTR", fCatalogSize*fTable.num_cols*sizeof(uint64_t)*2);
519 
520  const uint32_t total_num_tiles_written = (fTable.num_rows + fNumRowsPerTile-1)/fNumRowsPerTile;
521  const uint32_t total_catalog_width = 2*sizeof(int64_t)*fTable.num_cols;
522 
523  SetInt("THEAP", total_num_tiles_written*total_catalog_width);
524  SetInt("NAXIS1", total_catalog_width);
525  SetInt("NAXIS2", total_num_tiles_written);
526  SetStr("RAWSUM", std::to_string((long long int)(fRawSum.val())));
527 
528  const float compression_ratio = (float)(fRealRowWidth*fTable.num_rows)/(float)heap_size;
529  SetFloat("ZRATIO", compression_ratio);
530 
531  //add to the heap size the size of the gap between the catalog and the actual heap
532  heap_size += (fCatalogSize - total_num_tiles_written)*fTable.num_cols*sizeof(uint64_t)*2;
533 
534  SetInt("PCOUNT", heap_size, "size of special data area");
535 
536  //Just for updating the fCatalogSum value
537  WriteCatalog();
538 
540 
541  const Checksum checksm = UpdateHeaderChecksum();
542 
543  if (!fFilebuf.close())
544  setstate(ios_base::failbit);
545 
546  fSmartBuffer = std::shared_ptr<char>();
547 
548  //restore the number of rows per tile in case the catalog has been shrinked
549  if (shrink_factor != 1)
550  fNumRowsPerTile /= shrink_factor;
551 
552  if ((checksm+fDataSum).valid())
553  return true;
554 
555  std::ostringstream sout;
556  sout << "Checksum (" << std::hex << checksm.val() << ") invalid.";
557 #ifdef __EXCEPTIONS
558  throw std::runtime_error(sout.str());
559 #else
560  gLog << ___err___ << "ERROR - " << sout.str() << std::endl;
561  return false;
562 #endif
563  }
bool SetInt(const std::string &key, int64_t i, const std::string &comment="")
Definition: ofits.h:535
void AlignTo2880Bytes()
Definition: ofits.h:961
Checksum fDataSum
Definition: ofits.h:340
Checksum UpdateHeaderChecksum()
Definition: ofits.h:970
bool WriteCatalog()
Definition: zofits.h:247
uint32_t fNumRowsPerTile
Number of rows per tile.
Definition: zofits.h:980
uint32_t val() const
Definition: checksum.h:20
#define gLog
Definition: fits.h:36
uint32_t fCatalogSize
Actual catalog size (.size() is slow on large lists)
Definition: zofits.h:978
Checksum fRawSum
Raw sum (specific to FACT)
Definition: zofits.h:985
Checksum fCatalogSum
Checksum of the catalog.
Definition: zofits.h:984
#define ___err___
Definition: fits.h:37
Table fTable
Definition: ofits.h:327
std::vector< Queue< CompressionTarget > > fCompressionQueues
Processing queues (=threads)
Definition: zofits.h:973
bool SetStr(const std::string &key, std::string s, const std::string &comment="")
Definition: ofits.h:526
std::filebuf fFilebuf
Definition: ofits.h:32
uint32_t ShrinkCatalog()
Shrinks a catalog that is too long to fit into the reserved space at the beginning of the file...
Definition: zofits.h:415
void * exception_ptr
Definition: zofits.h:24
Queue< WriteTarget, QueueMin< WriteTarget > > fWriteToDiskQueue
Writing queue (=thread)
Definition: zofits.h:974
int fErrno
propagate errno to main thread
Definition: zofits.h:1011
size_t num_cols
Definition: ofits.h:308
size_t num_rows
Definition: ofits.h:307
bool SetFloat(const std::string &key, double f, int p, const std::string &comment="")
Definition: ofits.h:543
uint32_t fRealRowWidth
Width in bytes of one uncompressed row.
Definition: zofits.h:1006
std::exception_ptr fThreadsException
exception pointer to store exceptions coming from the threads
Definition: zofits.h:1010
CatalogType fCatalog
Catalog for this file.
Definition: zofits.h:977
CompressionTarget InitNextCompression()
Definition: zofits.h:393
std::shared_ptr< char > fSmartBuffer
Smart pointer to the buffer where the incoming rows are written.
Definition: zofits.h:1007
TileHeader()
Definition: FITS.h:75

+ Here is the call graph for this function: