FACT++  1.0
bool Fits::Open ( const string &  fileName,
const string &  tableName,
uint32_t *  fitsCounter,
MessageImp out,
int  runNumber,
CCfits::FITS *  file = 0 
)

Opens a FITS file.

This opens the FITS file (after the columns have been passed)

Parameters
fileNamethe filename with complete or relative path of the file to open
tableNamethe name of the table that will receive the logged data.
filea pointer to an existing FITS file. If NULL, file will be opened and managed internally
fitsCountera pointer to the integer keeping track of the opened FITS files
outa pointer to the MessageImp that should be used to log errors
runNumberthe runNumber for which this file is opened. 0 means nightly file.

Definition at line 156 of file Fits.cc.

References counter, MessageImp::Error(), fRunNumber, i, Time::Mjd(), and str.

157 {
158  fRunNumber = runNumber;
159  fMess = out;
160  fFileName = fileName;
161 
162  if (fFile)
163  {
164  fMess->Error("File already open...");
165  return false;
166  }
167 
168  fFile = new FitsFile(*fMess);
169 
170  if (file == NULL)
171  {
172  if (!fFile->OpenFile(fileName, true))
173  return false;
174 
175  fNumOpenFitsFiles = fitsCounter;
176  (*fNumOpenFitsFiles)++;
177  }
178  else
179  {
180  if (!fFile->SetFile(file))
181  return false;
182  }
183 
184  //concatenate the standard and data columns
185  //do it the inneficient way first: its easier and faster to code.
186  for (unsigned int i=0;i<fStandardColDesc.size();i++)
187  {
189  fStandardColDesc[i].unit);
190  }
191 
192  for (unsigned int i=0; i<fDataColDesc.size(); i++)
193  {
194  string name = fDataColDesc[i].name;
195  if (name.empty())
196  {
197  ostringstream stt;
198  stt << "Data" << i;
199  name = stt.str();
200  }
201 //cout << endl << "#####adding column: " << name << " " << fDataFormats[i] << " " << fDataColDesc[i].unit << endl << endl;
202  fFile->AddColumn(name, fDataFormats[i], fDataColDesc[i].unit);
203  }
204 
205  try
206  {
207  if (!fFile->OpenNewTable(tableName, 100))
208  {
209  Close();
210  //if the file already exist, then the column names must have changed
211  //let's move the file and try to open it again.
212  string fileNameWithoutFits = fFileName.substr(0, fileName.size()-4);
213  int counter = 0;
214  while (counter < 100)
215  {
216  ostringstream newFileName;
217  newFileName << fileNameWithoutFits << counter << ".fits";
218  ifstream testStream(newFileName.str().c_str());
219  if (!testStream)
220  {
221  if (rename(fFileName.c_str(), newFileName.str().c_str()))
222  return false;
223  break;
224  }
225  counter++;
226  }
227  if (counter == 100)
228  return false;
229  //now we open it again.
230  fFile = new FitsFile(*fMess);
231  if (file == NULL)
232  {
233  if (!fFile->OpenFile(fileName, true))
234  return false;
235  fNumOpenFitsFiles = fitsCounter;
236  (*fNumOpenFitsFiles)++;
237  }
238  else
239  {
240  if (!fFile->SetFile(file))
241  return false;
242  }
243  //YES, we must also redo that thing here...
244  //concatenate the standard and data columns
245  //do it the inneficient way first: its easier and faster to code.
246  for (unsigned int i=0;i<fStandardColDesc.size();i++)
247  {
249  fStandardColDesc[i].unit);
250  }
251 
252  for (unsigned int i=0; i<fDataColDesc.size(); i++)
253  {
254  string name = fDataColDesc[i].name;
255  if (name.empty())
256  {
257  ostringstream stt;
258  stt << "Data" << i;
259  name = stt.str();
260  }
261  //cout << endl << "#####adding column: " << name << " " << fDataFormats[i] << " " << fDataColDesc[i].unit << endl << endl;
262  fFile->AddColumn(name, fDataFormats[i], fDataColDesc[i].unit);
263  }
264  if (!fFile->OpenNewTable(tableName, 100))
265  {
266  Close();
267  return false;
268  }
269  }
270 
271  fCopyBuffer.resize(fFile->GetDataSize());
272 //write header comments
273 
274  ostringstream str;
275  for (unsigned int i=0;i<fStandardColDesc.size();i++)
276  {
277  str.str("");
278  str << "TTYPE" << i+1;
279  fFile->WriteKeyNT(str.str(), fStandardColDesc[i].name, fStandardColDesc[i].comment);
280  str.str("");
281  str << "TCOMM" << i+1;
282  fFile->WriteKeyNT(str.str(), fStandardColDesc[i].comment, "");
283  }
284 
285  for (unsigned int i=0; i<fDataColDesc.size(); i++)
286  {
287  string name = fDataColDesc[i].name;
288  if (name.empty())
289  {
290  ostringstream stt;
291  stt << "Data" << i;
292  name = stt.str();
293  }
294  str.str("");
295  str << "TTYPE" << i+fStandardColDesc.size()+1;
296  fFile->WriteKeyNT(str.str(), name, fDataColDesc[i].comment);
297  str.str("");
298  str << "TCOMM" << i+fStandardColDesc.size()+1;
299  fFile->WriteKeyNT(str.str(), fDataColDesc[i].comment, "");
300  }
301 
302  fFile->WriteKeyNT("COMMENT", fTableDesc, "");
303 
304  if (fFile->GetNumRows() == 0)
305  {//if new file, then write header keys -> reset fEndMjD used as flag
306  fEndMjD = 0;
307  }
308  else
309  {//file is beingn updated. Prevent from overriding header keys
310  fEndMjD = Time().Mjd();
311  }
312 
313  return fFile->GetNumRows()==0 ? WriteHeaderKeys() : true;
314  }
315  catch (const CCfits::FitsException &e)
316  {
317  cout << "Exception !" << endl;
318  fMess->Error("Opening or creating table '"+tableName+"' in '"+fileName+"': "+e.message());
319 
320  fFile->fTable = NULL;
321  Close();
322  return false;
323  }
324 }
vector< string > fDataFormats
the data format of the data columns
Definition: Fits.h:33
string fFileName
Definition: Fits.h:15
vector< char > fCopyBuffer
the copy buffer. Required to put the standard and data variable in contguous memory ...
Definition: Fits.h:36
size_t GetDataSize() const
Definition: FitsFile.cc:463
CCfits::Table * fTable
The pointer to the CCfits FITS file.
Definition: FitsFile.h:19
int i
Definition: db_dim_client.c:21
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
char str[80]
Definition: test_client.c:7
bool WriteHeaderKeys()
Write the FITS header keys.
Definition: Fits.cc:330
bool SetFile(CCfits::FITS *file=0)
Definition: FitsFile.cc:139
size_t GetNumRows() const
Definition: FitsFile.h:105
vector< Description > fStandardColDesc
Definition: Fits.h:20
string fTableDesc
Definition: Fits.h:31
MessageImp * fMess
were to log the errors
Definition: Fits.h:42
void Mjd(double mjd)
Definition: Time.cc:145
int Error(const std::string &str)
Definition: MessageImp.h:49
bool OpenFile(const string &filename, bool allow_open=false)
Definition: FitsFile.cc:94
bool WriteKeyNT(const string &name, const T &value, const string &comment)
Definition: FitsFile.h:62
FITS writter for the FACT project.
Definition: FitsFile.h:9
FitsFile * fFile
Definition: Fits.h:14
uint32_t * fNumOpenFitsFiles
Keep track of number of opened fits.
Definition: Fits.h:40
int32_t fRunNumber
current run number being logged
Definition: Fits.h:53
int counter
Definition: db_dim_client.c:19
vector< string > fStandardFormats
Format of the standard columns.
Definition: Fits.h:22
void AddColumn(char type, const string &name, int numElems=1, const string &unit="")
bool OpenNewTable(const string &tableName, int maxtry=1)
Definition: FitsFile.cc:206
vector< Description > fDataColDesc
the vector of data column names
Definition: Fits.h:29
double fEndMjD
to keep track of the time of the latest written entry (to update the header when closing the file) ...
Definition: Fits.h:38
void Close()
Close the currently opened file.
Definition: Fits.cc:441

+ Here is the call graph for this function: