Index: /trunk/SophyaExt/FitsIOServer/fitshdtable.cc
===================================================================
--- /trunk/SophyaExt/FitsIOServer/fitshdtable.cc	(revision 3068)
+++ /trunk/SophyaExt/FitsIOServer/fitshdtable.cc	(revision 3069)
@@ -188,10 +188,11 @@
 
   // Determination de la taille de segment 
-  sa_size_t segsz = is.GetNbRows() / 16; // Taille de segment par defaut 
+  sa_size_t segsz = is.GetNbRows() / 64; // Taille de segment par defaut 
   if (segsz > 2048) segsz = 2048;
   string key = "SEGMSIZE";
   bool knex = false;
   string ssegsz = is.KeyValue(key, knex);
-  if (!knex && (ssegsz.length() > 0))  segsz = atoi(ssegsz.c_str());
+  bool fgsss = false; 
+  if (!knex && (ssegsz.length() > 0))  { segsz = atoi(ssegsz.c_str());  fgsss = true; }
   if (segsz < 16) segsz = 16;
 
@@ -205,13 +206,26 @@
 
   if (dobj == NULL) { // On cree la table si besoin 
+    /*
+      Reza, Sep06 : On ne cree pas automatiquement un SwFitsDataTable - 
+      En effet, on ne peut garantir que l'objet FitsInOutFile is reste 
+      valide pendant toute la duree de vie de l'objet SwFitsDataTable
+
     string key = "SOPCLSNM"; 
     string clsnm = is.KeyValue(key);
-    if ( (clsnm == "SOPHYA::SwFitsDataTable") || (nbrows*colnames.size() < 10000000) )
+        if ( (clsnm == "SOPHYA::SwFitsDataTable") || (nbrows*colnames.size() < 10000000) )
       dobj = new DataTable(segsz);
-    else dobj = new SwFitsDataTable(is, segsz, false);
+    else { 
+      dobj = new SwFitsDataTable(segsz);
+      // dobj = new SwFitsDataTable(is, segsz, false);
+      // Reza, Sep2006: Ce constructeur de SwFitsDataTable lit l'entete 
+      // et initialise donc l'objet SwFitsDataTable  -> return
+      // return;
+    }
+    */
+    dobj = new DataTable(segsz);
   }
   else { 
     dobj->Clear();  // On efface la table sinon 
-    dobj->mSegSz = segsz;
+    if (fgsss)  dobj->mSegSz = segsz;
   }
 
@@ -221,12 +235,16 @@
 
   vector<int_8> swpos;
+  segsz = dobj->mSegSz;
+  int_8 swp = 1;
+  while (swp < nbrows) {
+    swpos.push_back(swp); swp += segsz;
+  }
   // Initialize the fits swap stream for SwFitsDataTable if necessary
   if (swfdt)  {
-    if ( swfdt->FitsSwapFile().FitsPtr() != is.FitsPtr() )
+    if ( swfdt->FitsSwapFile().FitsPtr() != is.FitsPtr() ) {
+      //      swfdt->FitsSwapFile().Close(); 
+      //      swfdt->FitsSwapFile().Open(is.FileName().c_str(), FitsInOutFile::Fits_RO);
+      //      swfdt->FitsSwapFile().MoveAbsToHDU(is.CurrentHDU());
       swfdt->FitsSwapFile().ShareFitsPtr(is); 
-    int_8 swp = 1;
-    
-    while (swp < nbrows) {
-      swpos.push_back(swp); swp += segsz;
     }
   }
@@ -303,5 +321,8 @@
 
   // On ne doit pas lire les donnees de la table pour un SwFitsDataTable
-  if (swfdt)  return;
+  if (swfdt) {
+    swfdt->mNSeg = swpos.size();
+    return;
+  }
 
   while ((dobj->SegmentSize()*dobj->NbSegments()) < dobj->NEntry())
Index: /trunk/SophyaExt/FitsIOServer/fitsinoutfile.cc
===================================================================
--- /trunk/SophyaExt/FitsIOServer/fitsinoutfile.cc	(revision 3068)
+++ /trunk/SophyaExt/FitsIOServer/fitsinoutfile.cc	(revision 3069)
@@ -203,6 +203,8 @@
 void FitsInOutFile::Close()
 {
-  if (ownfptr == false) return;
   if (fptr_ == NULL) return;
+  if (ownfptr == false) {
+    fptr_ = NULL; return;
+  }
   int status = 0;
   if (mode_ == Fits_Create) {
Index: /trunk/SophyaExt/FitsIOServer/fitsswapper.h
===================================================================
--- /trunk/SophyaExt/FitsIOServer/fitsswapper.h	(revision 3068)
+++ /trunk/SophyaExt/FitsIOServer/fitsswapper.h	(revision 3069)
@@ -11,4 +11,6 @@
 #include "fitsinoutfile.h"
 #include "fitsblkrw.h"
+
+#include "basedtable.h"
 
 /*!
@@ -25,13 +27,14 @@
 public:
   FITSDataSwapper() 
-    : fcol(0) , rowos(1)
+    : fcol(0) , rowos(1) , dtp(NULL)
   {
   }
-  FITSDataSwapper(FitsInOutFile & ios, int col) 
-    : fios(ios) , fcol(col), rowos(1)
+  FITSDataSwapper(FitsInOutFile & ios, int col, BaseDataTable* dt=NULL) 
+    : fios(ios) , fcol(col), rowos(1) , dtp(dt)
   {
   }
 
   inline FitsInOutFile & InOutStream() { return fios; }
+
   void SetInOutStream(FitsInOutFile & ios, int col) 
   {
@@ -53,4 +56,5 @@
     fcol = a.fcol;
     rowos = a.rowos;
+    dtp = a.dtp;
   }
 
@@ -60,4 +64,6 @@
     long row = rowos;
     if (osw)  row = oswp;
+    if (dtp != NULL)
+       if ((row+sz-1) > dtp->NRows() )  sz = dtp->NRows()-row+1;
     FitsBlockRW<T>::WriteColumnData(fios, fcol, row, 1, d, sz);
     if (!osw) rowos += sz;
@@ -67,10 +73,19 @@
   virtual void  ReadFromSwap(int_8 idx, int_8 swp, T* d, size_t sz)
   {
+    long nrows = fios.GetNbRows();
+    size_t szi = sz;
+    if ((swp+sz-1) > nrows) sz = nrows-swp+1;  
     FitsBlockRW<T>::ReadColumnData(fios, fcol, swp, 1, d, sz);
+    if (sz < szi) {
+      T zz = d[sz-1];
+      for(size_t k=sz; k<szi; k++)  d[k] = zz;
+    }
   }
 
   virtual DataSwapperInterface<T>* Clone()
   {
-    FITSDataSwapper<T> * rsw = new FITSDataSwapper<T>(fios, fcol) ;
+    FITSDataSwapper<T> * rsw = new FITSDataSwapper<T>(fios, fcol, dtp) ;
+    rsw->rowos = rowos;
+    rsw->dtp = dtp;    
     return rsw;
   }
@@ -80,4 +95,5 @@
   int fcol; 
   long rowos;
+  BaseDataTable* dtp;
 };
 
Index: /trunk/SophyaExt/FitsIOServer/swfitsdtable.cc
===================================================================
--- /trunk/SophyaExt/FitsIOServer/swfitsdtable.cc	(revision 3068)
+++ /trunk/SophyaExt/FitsIOServer/swfitsdtable.cc	(revision 3069)
@@ -70,6 +70,9 @@
   mFgCreate = false;
 }
-/*! Constructor with the specification of the output swap stream -
-    and optional specification of block (or segment) size
+/*!
+  \brief Construcor with specification of the \b FitsInOutFile swap stream.
+  if fgcreate == true, creates an empty table (the fits file should then be 
+  opened for writing). 
+  if fgcreate == false , the table is initialized (read in) from the current HDU.
 */
 SwFitsDataTable::SwFitsDataTable(FitsInOutFile & os, sa_size_t segsz, bool fgcreate)
@@ -82,4 +85,20 @@
   }
 }
+
+/*!
+  \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)
+  : BaseDataTable(segsz) ,
+    mSwF(fitsname, FitsInOutFile::Fits_RO) 
+{
+  // Lecture de la table 
+  FitsHandler<BaseDataTable> fio(*this);
+  mSwF.MoveAbsToHDU(hdunum);
+  fio.Read(mSwF);
+}
+
 //! copy constructor - shares the data
 SwFitsDataTable::SwFitsDataTable(SwFitsDataTable const & a)
@@ -158,4 +177,5 @@
 void SwFitsDataTable::SwapOutAll() const
 {
+  if (NRows() < 1) return;
   // Et on vide les buffers de swap 
   for (size_t kk=0; kk<mNames.size(); kk++) {
@@ -185,5 +205,5 @@
       break;
     default:
-      throw ForbiddenError("SwFitsDataTable::Share() : unknown column type ");
+      throw ForbiddenError("SwFitsDataTable::SwapOutAll() : unknown column type ");
     break;
     }
@@ -261,5 +281,5 @@
   {
     ser = mICols.size();
-    FITSDataSwapper<int_4> ISwapper(mSwF, colidx);
+    FITSDataSwapper<int_4> ISwapper(mSwF, colidx, this);
     if (swpos)
       mICols.push_back(SwSegDataBlock<int_4>(ISwapper, *swpos, mSegSz));
@@ -275,5 +295,5 @@
   {
     ser = mLCols.size();
-    FITSDataSwapper<int_8> LSwapper(mSwF, colidx);
+    FITSDataSwapper<int_8> LSwapper(mSwF, colidx, this);
     if (swpos)    
       mLCols.push_back(SwSegDataBlock<int_8>(LSwapper, *swpos, mSegSz));
@@ -289,5 +309,5 @@
   {
     ser = mFCols.size();
-    FITSDataSwapper<r_4> FSwapper(mSwF, colidx);
+    FITSDataSwapper<r_4> FSwapper(mSwF, colidx, this);
     if (swpos)    
       mFCols.push_back(SwSegDataBlock<r_4>(FSwapper, *swpos, mSegSz));
@@ -304,5 +324,5 @@
   {
     ser = mDCols.size();
-    FITSDataSwapper<r_8> DSwapper(mSwF, colidx);
+    FITSDataSwapper<r_8> DSwapper(mSwF, colidx, this);
     if (swpos)    
       mDCols.push_back(SwSegDataBlock<r_8>(DSwapper, *swpos, mSegSz));
@@ -318,5 +338,5 @@
   {
     ser = mYCols.size();
-    FITSDataSwapper< complex<r_4> > YSwapper(mSwF, colidx);
+    FITSDataSwapper< complex<r_4> > YSwapper(mSwF, colidx, this);
     if (swpos)    
       mYCols.push_back(SwSegDataBlock< complex<r_4> >(YSwapper, *swpos, mSegSz));
@@ -332,5 +352,5 @@
   {
     ser = mZCols.size();
-    FITSDataSwapper< complex<r_8> > ZSwapper(mSwF, colidx);
+    FITSDataSwapper< complex<r_8> > ZSwapper(mSwF, colidx, this);
     if (swpos)    
       mZCols.push_back(SwSegDataBlock< complex<r_8> >(ZSwapper, *swpos, mSegSz));
@@ -346,5 +366,5 @@
   {
     ser = mSCols.size();
-    FITSDataSwapper< string > SSwapper(mSwF, colidx);
+    FITSDataSwapper< string > SSwapper(mSwF, colidx, this);
     if (swpos)    
       mSCols.push_back(SwSegDataBlock< string >(SSwapper, *swpos, mSegSz));
Index: /trunk/SophyaExt/FitsIOServer/swfitsdtable.h
===================================================================
--- /trunk/SophyaExt/FitsIOServer/swfitsdtable.h	(revision 3068)
+++ /trunk/SophyaExt/FitsIOServer/swfitsdtable.h	(revision 3069)
@@ -11,4 +11,5 @@
 #include "fitsswapper.h"
 
+#include "fitshandler.h"
 
 namespace SOPHYA {
@@ -19,4 +20,5 @@
   SwFitsDataTable(sa_size_t segsz=512);
   SwFitsDataTable(FitsInOutFile & os, sa_size_t segsz=512, bool fgcreate=true); 
+  SwFitsDataTable(string fitsname, int hdunum=2, sa_size_t segsz=512); 
   SwFitsDataTable(SwFitsDataTable const& a); 
 
@@ -71,4 +73,5 @@
   bool mFgCreate; // true -> creation de table, false -> lecture
 }; 
+
   
 } // namespace SOPHYA
