FACT++  1.0
void fits::Constructor ( const std::string &  fname,
std::string  fout = "",
const std::string &  tableName = "",
bool  force = false 
)
inlineprotected

Definition at line 571 of file fits.h.

References ___err___, fits::Table::bytes_per_row, clear(), fits::Table::Get(), fits::Table::GetTotalBytes(), gLog, and Checksum::reset().

Referenced by zfits::zfits().

572  {
573  char simple[10];
574  read(simple, 10);
575  if (!good())
576  return;
577 
579 
580  if (memcmp(simple, "SIMPLE = ", 10))
581  {
582  clear(rdstate()|std::ios::badbit);
583 #ifdef __EXCEPTIONS
584  throw std::runtime_error("File is not a FITS file.");
585 #else
586  gLog << ___err___ << "ERROR - File is not a FITS file." << std::endl;
587  return;
588 #endif
589  }
590 
591  seekg(0);
592 
593  while (good())
594  {
595  std::vector<std::string> block;
596  while (1)
597  {
598  // If we search for a table, we implicitly assume that
599  // not finding the table is not an error. The user
600  // can easily check that by eof() && !bad()
601  peek();
602  if (eof() && !bad() && !tableName.empty())
603  {
604  break;
605  }
606  // FIXME: Set limit on memory consumption
607  const int rc = ReadBlock(block);
608  if (!good())
609  {
610  clear(rdstate()|std::ios::badbit);
611 #ifdef __EXCEPTIONS
612  throw std::runtime_error("FITS file corrupted.");
613 #else
614  gLog << ___err___ << "ERROR - FITS file corrupted." << std::endl;
615  return;
616 #endif
617  }
618 
619  if (block.size()%36)
620  {
621  if (!rc && !force)
622  {
623  clear(rdstate()|std::ios::badbit);
624 #ifdef __EXCEPTIONS
625  throw std::runtime_error("END keyword missing in FITS header.");
626 #else
627  gLog << ___err___ << "ERROR - END keyword missing in FITS file... file might be corrupted." << std::endl;
628  return;
629 #endif
630  }
631  break;
632  }
633  }
634 
635  if (block.empty())
636  break;
637 
638  if (block[0].substr(0, 9)=="SIMPLE =")
639  {
640  fChkHeader.reset();
641  continue;
642  }
643 
644  if (block[0].substr(0, 9)=="XTENSION=")
645  {
646  fTable = Table(block, tellg());
647  fRow = (size_t)-1;
648 
649  if (!fTable)
650  {
651  clear(rdstate()|std::ios::badbit);
652  return;
653  }
654 
655  const std::string &tname = fTable.Get<std::string>("EXTNAME");
656 
657  fListOfTables.emplace_back(tname);
658 
659  // Check for table name. Skip until eof or requested table are found.
660  // skip the current table?
661  if ((!tableName.empty() && tableName!=tname) || (tableName.empty() && "ZDrsCellOffsets"==tname))
662  {
663  const streamoff skip = fTable.GetTotalBytes();
664  seekg(skip, std::ios_base::cur);
665 
666  fChkHeader.reset();
667 
668  continue;
669  }
670 
673 
674  break;
675  }
676  }
677 
678  if (fout.empty())
679  return;
680 
681  if (*fout.rbegin()=='/')
682  {
683  const size_t p = fname.find_last_of('/');
684  fout.append(fname.substr(p+1));
685  }
686 
687  fCopy.open(fout);
688  if (!fCopy)
689  {
690  clear(rdstate()|std::ios::badbit);
691 #ifdef __EXCEPTIONS
692  throw std::runtime_error("Could not open output file.");
693 #else
694  gLog << ___err___ << "ERROR - Failed to open output file." << std::endl;
695  return;
696 #endif
697  }
698 
699  const streampos p = tellg();
700  seekg(0);
701 
702  std::vector<char> buf(p);
703  read(buf.data(), p);
704 
705  fCopy.write(buf.data(), p);
706  if (!fCopy)
707  clear(rdstate()|std::ios::badbit);
708  }
size_t bytes_per_row
Definition: fits.h:96
std::vector< char > fBufferDat
Definition: fits.h:512
std::ofstream fCopy
Definition: fits.h:499
#define gLog
Definition: fits.h:36
Checksum fChkHeader
Definition: fits.h:516
#define ___err___
Definition: fits.h:37
void EnableAddressExceptions(bool b=true)
Definition: fits.h:873
bool ReadBlock(std::vector< std::string > &vec)
Definition: fits.h:519
streamoff GetTotalBytes() const
Definition: fits.h:464
T Get(const std::string &key) const
Definition: fits.h:419
Table fTable
Definition: fits.h:502
size_t fRow
Definition: fits.h:514
void reset()
Definition: checksum.h:15
std::vector< char > fBufferRow
Definition: fits.h:511
std::vector< std::string > fListOfTables
Definition: fits.h:500
void clear()
Definition: HeadersFTM.h:216

+ Here is the call graph for this function:

+ Here is the caller graph for this function: