SourceXtractorPlusPlus  0.10
Please provide a description of the project.
FitsFileManager.cpp
Go to the documentation of this file.
1 
17 /*
18  * FitsFileManager.cpp
19  *
20  * Created on: Sep 19, 2019
21  * Author: mschefer
22  */
23 
24 #include <iostream>
25 #include <assert.h>
26 
28 
30 
31 namespace SourceXtractor {
32 
34 
35 FitsFileManager::FitsFileManager() : m_max_open_files(500) {
36 }
37 
39  closeAllFiles();
40 }
41 
43  for (auto& file : m_fits_files) {
44  if (file.second.m_is_file_opened) {
45  closeFitsFile(file.second.m_file_pointer);
46  file.second.m_is_file_opened = false;
47  file.second.m_file_pointer = nullptr;
48  }
49  }
50 }
51 
52 
53 fitsfile* FitsFileManager::getFitsFile(const std::string& filename, bool writeable) {
54  if (m_fits_files.find(filename) == m_fits_files.end()) {
55  FitsInfo info;
56  info.m_is_file_opened = false;
57  info.m_file_pointer = nullptr;
58  info.m_is_writeable = writeable;
59  m_fits_files[filename] = info;
60  }
61 
63  if (!info.m_is_file_opened) {
64  info.m_is_writeable = info.m_is_writeable || writeable;
66  info.m_is_file_opened = true;
68 
70  }
71 
72  if (writeable && !info.m_is_writeable) {
74  info.m_file_pointer = openFitsFile(filename, true);
75  info.m_is_writeable = true;
76  }
77 
78  return info.m_file_pointer;
79 }
80 
82  while (m_open_files.size() > m_max_open_files) {
83  auto& file_to_close = m_fits_files[m_open_files.back()];
84  closeFitsFile(file_to_close.m_file_pointer);
85  file_to_close.m_is_file_opened = false;
86  file_to_close.m_file_pointer = nullptr;
88  }
89 }
90 
91 
92 fitsfile* FitsFileManager::openFitsFile(const std::string& filename, bool writeable) const {
93  int status = 0;
94  fitsfile* fptr = nullptr;
95 
96  fits_open_image(&fptr, filename.c_str(), writeable ? READWRITE : READONLY, &status);
97  if (status != 0) {
98  throw Elements::Exception() << "Can't open FITS file: " << filename;
99  }
100  assert(fptr != nullptr);
101 
102  return fptr;
103 }
104 
105 void FitsFileManager::closeFitsFile(fitsfile* fptr) const {
106  int status = 0;
107  fits_close_file(fptr, &status);
108 }
109 
110 }
STL class.
std::unordered_map< std::string, FitsInfo > m_fits_files
fitsfile * openFitsFile(const std::string &filename, bool writeable) const
static std::shared_ptr< FitsFileManager > s_instance
STL class.
string filename
Definition: conf.py:63
T pop_back(T... args)
std::list< std::string > m_open_files
fitsfile * getFitsFile(const std::string &filename, bool writeable=false)
T size(T... args)
void closeFitsFile(fitsfile *fptr) const
T back(T... args)
T push_front(T... args)