Index: trunk/SophyaExt/FitsIOServer/fitsfile.cc
===================================================================
--- trunk/SophyaExt/FitsIOServer/fitsfile.cc	(revision 1136)
+++ trunk/SophyaExt/FitsIOServer/fitsfile.cc	(revision 1143)
@@ -40,5 +40,4 @@
 FitsFile::~FitsFile()
 {
-  cout << " destructeur FitsFile " << endl;
   int status = 0;
   if( fptr_ != NULL)
@@ -106,5 +105,4 @@
 void FitsInFile::InitNull()
 {
-  cout << " init FitsInFile " << endl;
   bitpix_ = 0;
   naxis_ = 0;
@@ -215,5 +213,4 @@
   fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
   if( status ) printerror( status );
-  //  cout << " dimension lue " << naxis << endl;
   // read the NAXISn keywords to get image size
   long* naxes = new long[naxis] ;
@@ -232,8 +229,7 @@
 
 
-//void FitsInFile::ReadFInit(char flnm[],int hdunum) 
 void FitsInFile::ReadFInit(int hdunum) 
 {
-  // InitNull();
+   InitNull();
   // int status = 0;
   
@@ -275,4 +271,13 @@
 }
 
+DVList  FitsInFile::DVListFromPrimaryHeader() const
+   {
+     int status;
+     DVList dvl;
+     KeywordsIntoDVList(fptr_, dvl, 1);
+     int hdutype = 0;
+     if (hdunum_ > 0) fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
+     return dvl;
+   }
 
 
@@ -281,4 +286,5 @@
   int status = 0;
   int hdutype;
+  InitNull();
   fits_movrel_hdu(fptr_, 1,&hdutype,&status); 
   if( status ) printerror( status," lecture du header suivant" );
@@ -484,5 +490,4 @@
 void FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
     {
-      cout <<" entree GetBinTabFCol " << endl;
       int status= 0;
       int DTYPE;
@@ -735,8 +740,36 @@
   char card[FLEN_CARD];  
   char *comkey = "COMMENT";
+  char comment[FLEN_COMMENT];
 
   // shift with the number of mandatory keywords
-  int num= 8;
-
+  //  int num= 8;
+    int num= 0;
+    // primary header
+    if (hdunum == 1) 
+      {
+	// read NAXIS
+	int naxis=0;
+	fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
+	// number of mandatory keywords
+	num = naxis+3;
+      }
+    // extensions
+    else
+      {
+	if (hdutype == IMAGE_HDU)
+	  {
+	    // read NAXIS
+	    int naxis=0;
+	    fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
+	    // number of mandatory keywords
+	    num = naxis+5;
+	  }
+	else
+	  if(hdutype == ASCII_TBL || hdutype == BINARY_TBL)
+	    {
+	      // number of mandatory keywords
+	      num = 8;
+	    }
+      }
   int j;
   for(j = num+1; j <= nkeys; j++)
@@ -760,21 +793,25 @@
 	    { 
 	    case 'C':
-	      dvl[keyname]= strval;	  
+	      fits_read_key(fileptr,TSTRING,keyname,strval,comment,&status);
+	      dvl[keyname]= strval;
+	      dvl.SetComment(keyname, comment);
 	      break;
 	    case 'I':
 	      int ival;
-	      fits_read_key(fileptr,TINT,keyname,&ival,NULL,&status);
+	      fits_read_key(fileptr,TINT,keyname,&ival,comment,&status);
 	        dvl[keyname]= (int_4) ival;		// Portage mac DY
+		dvl.SetComment(keyname, comment);
 	      break;
 	    case 'L':
 	      int ilog;
-	      if(strncmp(strval,"T",1) == 0) ilog= 1;
-	      else ilog= 0;
+	      fits_read_key(fileptr,TLOGICAL,keyname,&ilog,comment,&status);
 	      dvl[keyname]= (int_4) ilog;
+	      dvl.SetComment(keyname, comment);
 	      break;
 	    case 'F':
 	      double dval;
-	      fits_read_key(fileptr,TDOUBLE,keyname,&dval,NULL,&status);
+	      fits_read_key(fileptr,TDOUBLE,keyname,&dval,comment,&status);
 	      dvl[keyname]= dval;
+	      dvl.SetComment(keyname, comment);
 	      break;
 	    }
@@ -817,5 +854,5 @@
 
 
-void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn) 
+void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn,  DVList &dvl) 
 {
   int status = 0;
@@ -824,5 +861,8 @@
   if (hdunum_ == 1)
     {
-      if (imageOnPrimary_ == false) fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
+      if (imageOnPrimary_ == false)
+	{ 
+	  fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
+	  writeSignatureOnFits();	}
       else  hdunum_--;
     }
@@ -844,4 +884,9 @@
 
   hdunum_++;
+
+  // write supplementary keywords
+  //  dvl.Print();
+  addKeywordsOfDVList(dvl);
+
   delete [] naxes;
   if( status ) printerror( status, "erreur creation HDU IMAGE"  );
@@ -854,5 +899,5 @@
   fits_write_img(fptr_,TDOUBLE,1,npix,map,&status); 
   if( status ) printerror( status, "erreur ecriture putImageToFits" );
-  writeSignatureOnFits();
+  //  writeSignatureOnFits();
 }
 
@@ -863,5 +908,5 @@
   fits_write_img(fptr_,TFLOAT,1,npix, map,&status); 
   if( status ) printerror( status, "erreur ecriture putImageToFits" );
-  writeSignatureOnFits();
+  //  writeSignatureOnFits();
 
 }
@@ -873,5 +918,5 @@
   fits_write_img(fptr_,TINT,1,npix,map,&status); 
   if( status ) printerror( status, "erreur ecriture putImageToFits" );
-  writeSignatureOnFits();
+  //  writeSignatureOnFits();
 }
 
@@ -954,43 +999,5 @@
   //
   // write supplementary keywords
-  //
-  // get names and values from the join DVList object
-  //  dvl.Print();
-  fits_write_comment(fptr_,"--------------------------------------", &status);
-  DVList::ValList::const_iterator it;
-  for(it = dvl.Begin(); it != dvl.End(); it++)
-    {
-      char keytype= (*it).second.elval.typ;
-      char keyname[10];
-      strncpy(keyname,(*it).first.substr(0,64).c_str(),10);
-      char comment[FLEN_COMMENT];
-      switch (keytype)
-	{
-	case 'I' :
-	  {
-	    int ival=(*it).second.elval.iv;
-	    strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
-	    fits_write_key(fptr_,TINT,keyname,&ival,comment,&status);
-	    break;
-	  }
-	case 'D' :
-	  {
-	    double dval=(*it).second.elval.dv;
-	    strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
-	    fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
-	    break;
-	  }
-	case 'S' :
-	  {
-	    char strval[128];
-	    strncpy(strval,(*it).second.elval.strv->c_str(),127);
-	    strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
-	    fits_write_key(fptr_,TSTRING,keyname,&strval,comment,&status);
-	    break;
-	  }
-	} 
-  if( status )  printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
-    }
-  fits_write_comment(fptr_,"--------------------------------------", &status);
+  addKeywordsOfDVList(dvl);
 
 }
@@ -1097,4 +1104,13 @@
 }
 
+void  FitsOutFile::DVListIntoPrimaryHeader(DVList& dvl) const
+{
+  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
@@ -1110,5 +1126,5 @@
   fits_write_key(fptr_, TSTRING, keyname, &strval, comment, &status);
   if( status ) printerror( status );
-
+  fits_write_date(fptr_, &status);
   fits_write_comment(fptr_,"..............................................", &status);
   fits_write_comment(fptr_, " SOPHYA package - FITSIOSever ", &status);
@@ -1120,5 +1136,53 @@
 
 
-
-
-
+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++)
+    {
+      char keytype= (*it).second.elval.typ;
+      char keyname[10];
+      strncpy(keyname,(*it).first.substr(0,64).c_str(),10);
+      char comment[FLEN_COMMENT];
+      char strval[FLEN_VALUE]= "";
+      char *comkey = "COMMENT";
+      fits_read_keyword(fptr_, keyname, strval, NULL, &status);
+      if (status != 0 || strncmp(keyname,comkey,LEN_KEYWORD-1) == 0 ) 
+	{
+	  status = 0;
+	  switch (keytype)
+	    {
+	    case 'I' :
+	      {
+		int ival=(*it).second.elval.iv;
+		strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
+		fits_write_key(fptr_,TINT,keyname,&ival,comment,&status);
+		break;
+	      }
+	    case 'D' :
+	      {
+		double dval=(*it).second.elval.dv;
+		strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
+		fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
+		break;
+	      }
+	    case 'S' :
+	      {
+		char strval[128];
+		strncpy(strval,(*it).second.elval.strv->c_str(),127);
+		strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
+		fits_write_key(fptr_,TSTRING,keyname,&strval,comment,&status);
+		break;
+	      }
+	    } 
+	}
+  if( status )  printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
+    }
+  fits_write_comment(fptr_,"--------------------------------------", &status);
+}
+
+
+
