SourceXtractorPlusPlus  0.8
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FitsFileManager.cpp
Go to the documentation of this file.
1 /*
2  * FitsFileManager.cpp
3  *
4  * Created on: Sep 19, 2019
5  * Author: mschefer
6  */
7 
8 #include <iostream>
9 #include <assert.h>
10 
12 
14 
15 namespace SourceXtractor {
16 
18 
19 FitsFileManager::FitsFileManager() : m_max_open_files(500) {
20 }
21 
23  closeAllFiles();
24 }
25 
27  for (auto& file : m_fits_files) {
28  if (file.second.m_is_file_opened) {
29  closeFitsFile(file.second.m_file_pointer);
30  file.second.m_is_file_opened = false;
31  file.second.m_file_pointer = nullptr;
32  }
33  }
34 }
35 
36 
37 fitsfile* FitsFileManager::getFitsFile(const std::string& filename, bool writeable) {
38  if (m_fits_files.find(filename) == m_fits_files.end()) {
39  FitsInfo info;
40  info.m_is_file_opened = false;
41  info.m_file_pointer = nullptr;
42  info.m_is_writeable = writeable;
43  m_fits_files[filename] = info;
44  }
45 
47  if (!info.m_is_file_opened) {
48  info.m_is_writeable = info.m_is_writeable || writeable;
49  info.m_file_pointer = openFitsFile(filename, info.m_is_writeable);
50  info.m_is_file_opened = true;
51  m_open_files.push_front(filename);
52 
54  }
55 
56  if (writeable && !info.m_is_writeable) {
58  info.m_file_pointer = openFitsFile(filename, true);
59  info.m_is_writeable = true;
60  }
61 
62  return info.m_file_pointer;
63 }
64 
66  while (m_open_files.size() > m_max_open_files) {
67  auto& file_to_close = m_fits_files[m_open_files.back()];
68  closeFitsFile(file_to_close.m_file_pointer);
69  file_to_close.m_is_file_opened = false;
70  file_to_close.m_file_pointer = nullptr;
71  m_open_files.pop_back();
72  }
73 }
74 
75 
76 fitsfile* FitsFileManager::openFitsFile(const std::string& filename, bool writeable) const {
77  int status = 0;
78  fitsfile* fptr = nullptr;
79 
80  fits_open_file(&fptr, filename.c_str(), writeable ? READWRITE : READONLY, &status);
81  if (status != 0) {
82  throw Elements::Exception() << "Can't open FITS file: " << filename;
83  }
84  assert(fptr != nullptr);
85 
86  return fptr;
87 }
88 
89 void FitsFileManager::closeFitsFile(fitsfile* fptr) const {
90  int status = 0;
91  fits_close_file(fptr, &status);
92 }
93 
94 }
std::unordered_map< std::string, FitsInfo > m_fits_files
void closeFitsFile(fitsfile *fptr) const
static std::shared_ptr< FitsFileManager > s_instance
STL class.
string filename
Definition: conf.py:63
std::list< std::string > m_open_files
fitsfile * getFitsFile(const std::string &filename, bool writeable=false)
T c_str(T...args)
fitsfile * openFitsFile(const std::string &filename, bool writeable) const