Index: trunk/SophyaExt/FitsIOServer/swfitsdtable.cc
===================================================================
--- trunk/SophyaExt/FitsIOServer/swfitsdtable.cc	(revision 3391)
+++ trunk/SophyaExt/FitsIOServer/swfitsdtable.cc	(revision 3395)
@@ -24,4 +24,5 @@
    In this case, the Show() method (or ostream& << operator) will compute min/max 
    values. This operation will take some time for large tables.
+   - The SwFitsDataTable operation (AddRow()/GetRow() ) is NOT thread safe
 
    \sa SOPHYA::MuTyV
@@ -37,5 +38,5 @@
    // Create the swap stream 
    FitsInOutFile so("myswtable.fits", FitsInOutFile::Fits_Create);
-   SwFitsDataTable dt(so, 16);
+   SwFitsDataTable dt(so, 16, true);
    // define table columns
    dt.AddFloatColumn("X0_f");
@@ -54,10 +55,8 @@
    {
    // ---- Accessing information from a previously created table
-   SwFitsDataTable dt;
    FitsInOutFile si("myswtable.fits", FitsInOutFile::Fits_ReadOnly);
    // Position the fits file on the first extension (BinTable)
    si.MoveAbsToHDU(2);  
-   // Reading in the table (initialisation) 
-   si >> dt;
+   SwFitsDataTable dt(si, 512, false);
    // Printing table info 
    cout << dt ;   
@@ -67,7 +66,6 @@
 //! Default constructor with optional specification of block (or segment) size 
 SwFitsDataTable::SwFitsDataTable(sa_size_t segsz)
-  : BaseDataTable(segsz)
-{
-  mFgCreate = false;
+  : BaseDataTable(segsz) , mFgCreate(false) , mFgDefDone(false)
+{
 }
 /*!
@@ -79,20 +77,20 @@
 SwFitsDataTable::SwFitsDataTable(FitsInOutFile & os, sa_size_t segsz, bool fgcreate)
   : BaseDataTable(segsz) ,
-    mSwF(os) , mFgCreate(fgcreate)
+    mSwF(os) , mFgCreate(fgcreate) , mFgDefDone(false)
 {
   if (!fgcreate) {   // Lecture de la table 
     FitsHandler<BaseDataTable> fio(*this);
     fio.Read(os);
+    mFgDefDone = true;
   }
 }
 
 /*!
-  \brief Construcor with specification of the FITS file name.
-  if fgcreate == false , the FITS file is opened and the table is 
-  initialized (read in) from HDU \b hdunum
-*/
-SwFitsDataTable::SwFitsDataTable(string fitsname, int hdunum, sa_size_t segsz)
+  \brief Construcor with specification of the FITS file name, as a Read-only file.
+  The FITS file is opened and the table is initialized (read in) from HDU \b hdunum
+*/
+  SwFitsDataTable::SwFitsDataTable(string fitsname, int hdunum, sa_size_t segsz)
   : BaseDataTable(segsz) ,
-    mSwF(fitsname, FitsInOutFile::Fits_RO) 
+    mSwF(fitsname, FitsInOutFile::Fits_RO) , mFgCreate(false) , mFgDefDone(true)
 {
   // Lecture de la table 
@@ -105,6 +103,7 @@
 SwFitsDataTable::SwFitsDataTable(SwFitsDataTable const & a)
   : BaseDataTable(a.SegmentSize()),
-    mSwF(a.mSwF) , mFgCreate(a.mFgCreate)
-{
+    mSwF(a.mSwF) , mFgCreate(a.mFgCreate) , mFgDefDone(a.mFgDefDone)
+{
+  // Attention(Reza Nov07): le mFgDefDone doit etre en principe partage entre les objets 
   Share(a);
 }
@@ -228,4 +227,5 @@
   if (mThS) delete mThS;
   mThS = NULL;
+  mFgCreate = mFgDefDone = false;
 
   mMin.clear();
@@ -401,16 +401,38 @@
 }
 
+
+/*! 
+  \brief Writes table definition (column name/type) to the FITS file
+
+  It is not possible to add columns once the table definition is 
+  written to the fits file. Returns number of table columns.
+  Although AddRow() checks and perform this operation automatically,
+  it is advised to do it explicitely, before using an SwFitsDataTable
+  for filling in multi-thread programs.
+*/
+sa_size_t SwFitsDataTable::WriteTableDefinitionToFits()
+{
+  if (!mFgDefDone && (NRows() == 0)) {  
+    FitsHandler<BaseDataTable> fio(*this);
+    fio.Write(mSwF);
+    mFgDefDone = true;
+  }
+  return NVar();
+}
+
 //! Adds a line (or row to the table) with r_8* input data.
 /*! 
   The min/max values for each column is updated, in addition 
   to the actions performed by the base class AddRow().
-  The table is also created on the FITS file at the first call to AddRow() 
+  The table is also created on the FITS file at the first call to AddRow(), 
+  by calling WriteTableDefinitionToFits() , if not already done.
 */
 sa_size_t SwFitsDataTable::AddRow(const r_8* data)
 {
   if (mThS) mThS->lock();   // tread-safety
-  if (NRows() == 0) {  // On cree la definition de la table FITS au premier appel
+  if (!mFgDefDone && (NRows() == 0)) { // On cree la definition de la table FITS au premier appel  
     FitsHandler<BaseDataTable> fio(*this);
     fio.Write(mSwF);
+    mFgDefDone = true;
   }
   // On est oblige de calculer les min-max lors du remplissage
@@ -426,4 +448,5 @@
 }
 
+
 //! Adds a row (or line to the table) with input data as an array of MuTyV
 /*! 
@@ -435,7 +458,8 @@
 {
   if (mThS) mThS->lock();   // tread-safety
-  if (NRows() == 0) {  // On cree la definition de la table FITS au premier appel
+  if (!mFgDefDone && (NRows() == 0)) { // On cree la definition de la table FITS au premier appel  
     FitsHandler<BaseDataTable> fio(*this);
     fio.Write(mSwF);
+    mFgDefDone = true;
   }
   // On est oblige de calculer les min-max lors du remplissage
