FACT++  1.0
bool FitsFile::OpenNewTable ( const string &  tableName,
int  maxtry = 1 
)

This looks for a suitable table in the fits file, i.e. that corresponds to the name and column names. (no format check yet)

Parameters
tableName.the base table name to be obtained. If not suitable, numbers are appened to the name
allNames.the name of all columns
allDataTypes.the data types of all columns
allUnits.the units of the columns
Returns
a pointer to the newly retrieved/created table

Definition at line 206 of file FitsFile.cc.

References Error(), i, and str.

Referenced by AddColumn().

207 {
208  if (!fFile)
209  {
210  Error("FitsFile::OpenNewTable - No file open.");
211  return false;
212  }
213 
214  if (fTable)
215  {
216  Error("FitsFile::OpenNewTable - Table already open.");
217  return false;
218  }
219 
220  //first, let's check if the table already exist in the file
221  fFile->read(vector<string>(1, tableName));
222 
223  // FIXME: Check for fFile and fTable
224  const multimap<string, CCfits::ExtHDU *> &extMap = fFile->extension();
225 
226  for (int i=0; i<maxtry; i++)
227  {
228  //if (i==10)
229  // fMess->Warn("Already 10 different tables with different formats exist in this file. Please consider re-creating the file entirely (i.e. delete it please)");
230 
231  ostringstream str;
232  str << tableName;
233  if (i != 0)
234  str << "-" << i;
235 
236  const string tname = str.str();
237 
238  const multimap<string,CCfits::ExtHDU*>::const_iterator it = extMap.find(tname);
239 
240  //current table name does not exist yet. return its associated fits table newly created
241  if (it == extMap.end())
242  {
243  // What is this for?
244  //for (multimap<string, CCfits::ExtHDU*>::const_iterator it=extMap.begin();
245  // it!= extMap.end(); it++)
246  // fMess->Debug(it->first);
247 
248  return OpenTable(tname);
249  }
250 
251  CCfits::Table *table = dynamic_cast<CCfits::Table*>(it->second);
252 
253  // something wrong happened while getting the table pointer
254  if (!table)
255  {
256  Error("HDU '"+tname+"' found in file, but it is not a proper CCfits::Table.");
257  return false;
258  }
259 
260  //now check that the table columns are the same
261  //as the service columns
262  table->makeThisCurrent();
263 
264  // FIXME: To be checked...
265  /*
266  const map<string, Column*> cMap = table->column();
267  for (vector<string>::const_iterator ii=fFile->fColNames;
268  ii!=fFile->fColNames.end(); ii++)
269  if (cMap.find(*ii) == cMap.end())
270  continue;
271  */
272 
273  fNumRows = table->rows();
274 
275  // ----------- This is just a simple sanity check ----------
276 
277  // This is not necessary this is done already in
278  // findSuitableTable (either directly or indirectly through OpenTable)
279  // fFile->fTable->makeThisCurrent();
280 
281  //If the file already existed, then we must load its data to memory before writing to it.
282  if (fNumRows>0)
283  {
284  CCfits::BinTable* bTable = dynamic_cast<CCfits::BinTable*>(table);
285  if (!bTable)
286  {
287  Error("Table '"+tableName+"' found in '"+fFile->name()+"' is not a binary table.");
288  return false;
289  }
290 
291  //read the table binary data.
292  vector<string> colName;
293  bTable->readData(true, colName);
294 
295  // double check that the data was indeed read from the disk.
296  // Go through the fTable instead as colName is empty (yes, it is !)
297  const map<string,CCfits::Column*> &cMap = table->column();
298 
299  //check that the existing columns are the same as the ones we want to write
300  for (map<string, CCfits::Column*>::const_iterator mapIt = cMap.begin(); mapIt != cMap.end(); mapIt++)
301  {
302  bool found = false;
303  for (unsigned int ii=0;ii<fColNames.size();ii++)
304  {
305  if (mapIt->first == fColNames[ii])
306  {
307  found = true;
308  if (mapIt->second->format() != fColTypes[ii])
309  {
310  Error("Column "+fColNames[ii]+" has wrong format ("+fColTypes[ii]+" vs "+mapIt->second->format()+" in file)");
311  return false;
312  }
313  }
314  }
315  if (!found)
316  {
317  Error("Column "+mapIt->first+" only exist in written file");
318  return false;
319  }
320  }
321  //now we know that all the file's columns are requested. Let's do it the other way around
322  for (unsigned int ii=0;ii<fColNames.size();ii++)
323  {
324  bool found = false;
325  for (map<string, CCfits::Column*>::const_iterator mapIt = cMap.begin(); mapIt != cMap.end(); mapIt++)
326  {
327  if (fColNames[ii] == mapIt->first)
328  {
329  found = true;
330  if (fColTypes[ii] != mapIt->second->format())
331  {
332  Error("Column "+fColNames[ii]+" has wrong format ("+fColTypes[ii]+" vs "+mapIt->second->format()+" in file)");
333  return false;
334  }
335  }
336  }
337  if (!found)
338  {
339  Error("Column "+fColNames[ii]+" only exist in requested description");
340  return false;
341  }
342  }
343 
344  for (map<string,CCfits::Column*>::const_iterator cMapIt = cMap.begin();
345  cMapIt != cMap.end(); cMapIt++)
346  {
347  if (!cMapIt->second->isRead())
348  {
349  Error("Reading column '"+cMapIt->first+"' back from '"+fFile->name()+"' failed.");
350  return false;
351  }
352  }
353  }
354 
355  // Set this as last - we use it for IsOpen()
356  fTable = table;
357 
358  return true;
359  }
360 
361  ostringstream str;
362  str << "FitsFile::OpenNewTable failed - more than " << maxtry << " tables tried." << endl;
363  Error(str);
364 
365  return false;
366 }
size_t fNumRows
The pointer to the CCfits binary table.
Definition: FitsFile.h:21
CCfits::Table * fTable
The pointer to the CCfits FITS file.
Definition: FitsFile.h:19
int i
Definition: db_dim_client.c:21
char str[80]
Definition: test_client.c:7
CCfits::FITS * fFile
Definition: FitsFile.h:18
int Error(const std::string &str)
Definition: MessageImp.h:49
std::vector< std::string > fColTypes
Definition: FitsFile.h:15
bool OpenTable(const string &tablename)
Definition: FitsFile.cc:159
std::vector< std::string > fColNames
Definition: FitsFile.h:14

+ Here is the call graph for this function:

+ Here is the caller graph for this function: