Index: trunk/SophyaExt/FitsIOServer/fitsfile.cc
===================================================================
--- trunk/SophyaExt/FitsIOServer/fitsfile.cc	(revision 1235)
+++ trunk/SophyaExt/FitsIOServer/fitsfile.cc	(revision 1246)
@@ -104,6 +104,4 @@
 void FitsIOHandler::Read(FitsInFile& is, int hdunum) 
 {
-  //  if (hdunum == 0 ) is.moveToFollowingHeader();
-  //  else is.ReadFInit(hdunum);
   is.ReadFInit(hdunum);
   ReadFromFits(is);
@@ -199,5 +197,5 @@
   \class SOPHYA::FitsInFile
 
-class for saving  SOPHYA objects on FITS Format Files (uses cfitsio lib)
+class for reading  SOPHYA objects from FITS Format Files (uses cfitsio lib)
 */
 
@@ -341,5 +339,5 @@
 	{
 	  // on calcule le numero de header a lire
-	  if (imageOnPrimary_ == true) hdunum_ = 1;
+	  if (imageOnPrimary_ == true ) hdunum_ = 1;
 	  else hdunum_ = 2;
 	}
@@ -420,5 +418,10 @@
       else
 	{
-	  throw PException(" first header : no image, probably error in hdunum");
+	  // the object to be read is probably a bin or ascii table
+	  // (on an extension)
+	  hdunum_++;
+	  getHeader();
+
+	  //	  throw PException(" first header : no image, probably error in hdunum");
 	}
     }
@@ -1193,6 +1196,6 @@
   long naxis = nbdim;
   long* naxes = new long[nbdim];
-  //  if (hdunum_ == 1)
-  if (hdunum_ == 0)
+  bool hdunfirst= (hdunum_ == 0);
+  if (hdunfirst)
     {
       if (imageOnPrimary_ == false)
@@ -1200,6 +1203,5 @@
 	  hdunum_ = 1;
 	  fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
-	  writeSignatureOnFits();	}
-      //      else  hdunum_--;
+	}
     }
   int k;
@@ -1218,6 +1220,11 @@
 	  throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
 	}
-
+  // on ajoute eventuellement un dvlist prepare et la doc SOPHYA
   hdunum_++;
+  if (hdunfirst) 
+    {
+      addDVListOnPrimary();
+      writeSignatureOnFits(1);
+    }
 
   // write supplementary keywords
@@ -1242,5 +1249,4 @@
   fits_write_img(fptr_,TDOUBLE,1,npix,map,&status); 
   if( status ) printerror( status, "erreur ecriture PutImageToFits" );
-  //  writeSignatureOnFits();
 }
 
@@ -1255,5 +1261,4 @@
   fits_write_img(fptr_,TFLOAT,1,npix, map,&status); 
   if( status ) printerror( status, "erreur ecriture PutImageToFits" );
-  //  writeSignatureOnFits();
 
 }
@@ -1269,5 +1274,4 @@
   fits_write_img(fptr_,TINT,1,npix,map,&status); 
   if( status ) printerror( status, "erreur ecriture PutImageToFits" );
-  //  writeSignatureOnFits();
 }
 
@@ -1366,5 +1370,11 @@
 		  NULL,extn,&status);
   if( status ) printerror( status );
-  if ( hdunum_ == 0 ) hdunum_ = 2;
+  // on ajoute eventuellement un dvlist prepare
+  if ( hdunum_ == 0 )
+    {
+      hdunum_ = 2;
+      addDVListOnPrimary();
+      writeSignatureOnFits(1);
+    }
   else  hdunum_++;
   int ii;
@@ -1543,20 +1553,36 @@
 Put keywords from a DVList into the primary header of the fits-file 
 */
-void  FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
+void  FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) 
 {
   int status = 0;
   int hdutype;
-  fits_movabs_hdu(fptr_,1,&hdutype,&status);
-  addKeywordsOfDVList(dvl);
-  fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
-}
-
-
-void FitsOutFile::writeSignatureOnFits() const
-{
-  int status = 0;
+  if (hdunum_ == 0) 
+    {
+      if (dvlToPrimary_ == NULL) dvlToPrimary_ = new DVList(dvl);
+      else dvlToPrimary_->Merge(dvl);
+    }
+  else
+    {
+      fits_movabs_hdu(fptr_,1,&hdutype,&status);
+      addKeywordsOfDVList(dvl);
+      fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
+    }
+}
+
+
+void FitsOutFile::writeSignatureOnFits(int hdunum) const
+{
+  int status = 0;
+  int hdutype;
   char keyname[LEN_KEYWORD];
   char strval[FLEN_VALUE];
   char comment[FLEN_COMMENT];
+  if (hdunum_ == 0) 
+    {
+      cerr << " WARNING : can't write keywords on non existing primary header" << endl;
+      return;
+    }
+  fits_movabs_hdu(fptr_,1,&hdutype,&status);
+  //
   strncpy(keyname, "CREATOR", LEN_KEYWORD); 
   keyname[LEN_KEYWORD-1] = '\0';
@@ -1572,12 +1598,13 @@
   fits_write_comment(fptr_,"..............................................", &status);
   if( status ) printerror( status, "erreur writeSignatureOnFits"  );
-}
-
-
-void FitsOutFile::addKeywordsOfDVList(DVList& dvl) const
+  //
+  fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
+}
+
+
+void FitsOutFile::addKeywordsOfDVList( DVList& dvl) const
 {
   int status = 0;
   fits_write_comment(fptr_,"---------- keywords from SOPHYA ---------", &status);
-  if (hdunum_ == 1) writeSignatureOnFits();
   DVList::ValList::const_iterator it;
   for(it = dvl.Begin(); it != dvl.End(); it++)
@@ -1631,3 +1658,21 @@
 
 
-
+void FitsOutFile::addDVListOnPrimary()
+    {
+      int status = 0;
+      int hdutype;
+      if (hdunum_ == 0) 
+	{
+	  cerr << " WARNING : can't write keywords on non existing primary header" << endl;
+	  return;
+	}
+      if (dvlToPrimary_ != NULL)
+	{
+	  fits_movabs_hdu(fptr_,1,&hdutype,&status);
+	  addKeywordsOfDVList(*dvlToPrimary_);
+	  delete dvlToPrimary_;
+	  dvlToPrimary_ = NULL;
+	  fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
+	}
+    }
+
