Index: /trunk/SophyaExt/FitsIOServer/fitsfile.cc
===================================================================
--- /trunk/SophyaExt/FitsIOServer/fitsfile.cc	(revision 970)
+++ /trunk/SophyaExt/FitsIOServer/fitsfile.cc	(revision 971)
@@ -74,6 +74,7 @@
 	vector<char> types;   
 	vector<int>  taille_des_chaines;
-	GetBinTabParameters(fileptr, naxis, nrows, naxisn, noms, types, taille_des_chaines);  
-	for (int k=0; k< naxisn.size(); k++) naxisn[k] *= nrows;
+	GetBinTabParameters(fileptr, naxis, nrows, naxisn, noms, types, taille_des_chaines); 
+	int k;
+	for (k=0; k< naxisn.size(); k++) naxisn[k] *= nrows;
 	if(hdutype == ASCII_TBL)
 	  {
@@ -104,7 +105,5 @@
       }
   
-  fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
-  if( status ) printerror( status,"getBlockType: erreur movabs, pour dvlist");
-  KeywordsIntoDVList(fileptr, dvl);
+  KeywordsIntoDVList(fileptr, dvl, hdunum);
   fits_close_file(fileptr,&status);
 }
@@ -114,42 +113,89 @@
   int status = 0;
   hdutype_= 0;
-  hdunum_ = 0; 
+  
   fits_open_file(&fptr_,flnm,READONLY,&status);
   if( status ) printerror( status );
-
-  // move to the specified HDU number
+  //
+  if (hdunum <= 1) 
+    {
+      hdunum_ = 1; 
+      // presence of image ?
+      int naxis= 0;
+      fits_read_key(fptr_,TINT,"NAXIS",&naxis,NULL,&status);
+      if( status ) printerror( status );
+      if (naxis > 0 )       // there is an image
+	{
+	  hdutype_ == IMAGE_HDU;
+	  GetImageParameters (fptr_, bitpix_, naxis_, naxisn_);
+	  nbData_ =  1;
+	  int k;
+	  for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
+	  KeywordsIntoDVList(fptr_, dvl_,hdunum_);
+	}
+      else
+	{
+	  throw PException(" first header : no image, probably error in hdunum");
+	}
+  //
+    }
+  else 
+    {
+      hdunum_ = hdunum-1; 
+      int hdutype;
+      fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
+      if( status ) printerror( status,":FitsFile::ReadF : erreur movabs");
+      moveToFollowingHeader();
+    }
+  ReadFromFits(*this);
+}
+
+
+void FitsFile::moveToFollowingHeader()
+{
+  int status = 0;
   int hdutype;
-  fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
-  if( status ) printerror( status );
-  
+  fits_movrel_hdu(fptr_, 1,&hdutype,&status); 
+  if( status ) printerror( status," lecture du header suivant" );
+  hdunum_++;
   hdutype_= hdutype;
-  hdunum_ = hdunum;
   if(hdutype_ == IMAGE_HDU) 
     {
       GetImageParameters (fptr_, bitpix_, naxis_, naxisn_);
       nbData_ =  1;
-      for (int k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
-      fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
-      if( status ) printerror( status);
-      KeywordsIntoDVList(fptr_, dvl_);
+      int k;
+      for (k=0; k<naxis_; k++) if (naxisn_[k] > 0) nbData_ *= naxisn_[k];
+      KeywordsIntoDVList(fptr_, dvl_,hdunum_);
     }
   if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
     {
       GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
-  fits_movabs_hdu(fptr_,hdunum,&hdutype,&status);
-  if( status ) printerror( status);
-  KeywordsIntoDVList(fptr_, dvl_);
-    }
-  ReadFromFits(*this);
-
-} 
-void FitsFile::WriteF(char flnm[],int hdunum)
+      KeywordsIntoDVList(fptr_, dvl_, hdunum_);
+    }
+}
+
+ 
+void FitsFile::WriteF(char flnm[], bool OldFile)
 {
   int status = 0; 
   hdutype_= 0;
-  hdunum_ = hdunum; 
+  hdunum_ = 0;
+
+ 
   // create new FITS file
-  fits_create_file(&fptr_,flnm,&status);
-  if( status ) printerror(status,"file already exists");
+  if (!OldFile)
+    {
+      fits_create_file(&fptr_,flnm,&status);
+      if( status ) printerror(status,"file already exists");
+    }
+  else
+    {
+      fits_open_file(&fptr_,flnm,READWRITE,&status);
+      if( status ) printerror(status,"file does not exist");
+      fits_get_num_hdus(fptr_, &hdunum_, &status);
+      int hdutype;
+      fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
+      if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
+
+    }
   WriteToFits(*this);
 }
@@ -245,10 +291,11 @@
       }
 }
-void FitsFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn) const
+void FitsFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn) 
 {
   int status = 0;
   long naxis = nbdim;
   long* naxes = new long[nbdim];
-  for (int k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
+  int k;
+  for (k=0; k< nbdim; k++) naxes[k] = (long)naxisn[k];
   if (type == 'D')
     fits_create_img(fptr_,DOUBLE_IMG,naxis,naxes,&status);
@@ -264,4 +311,6 @@
 	  throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
 	}
+
+  hdunum_++;
   delete [] naxes;
   if( status ) printerror( status );
@@ -320,5 +369,6 @@
   if (nfound != naxis ) 
     cout << " WARNING : " << nfound << " axes found, expected naxis= " << naxis << endl;
-  for (int k=0; k<naxis; k++) 
+  int k;
+  for (k=0; k<naxis; k++) 
     {
       naxisn.push_back( (int)naxes[k] );
@@ -327,9 +377,12 @@
 }
 
-void FitsFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl)
-{
+void FitsFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
+{
+  int status = 0;
+  int hdutype;
+  fits_movabs_hdu(fileptr,hdunum,&hdutype,&status);
+  if( status ) printerror( status,":KeywordsIntoDVList : erreur movabs");
   // get number of keywords
   int nkeys,keypos;
-  int status = 0;
   fits_get_hdrpos(fileptr,&nkeys,&keypos,&status);
   if( status ) printerror( status );
@@ -345,5 +398,6 @@
   int num= 8;
 
-  for(int j = num+1; j <= nkeys; j++)
+  int j;
+  for(j = num+1; j <= nkeys; j++)
     {
       fits_read_keyn(fileptr,j,card,strval,NULL,&status);
@@ -403,10 +457,22 @@
 	}
       long nels=nentries;
+      int    anull;
       // no checking for undefined pixels
-      int    anull;
-      float dnull= 0.;
+      double dnull= 0.;
+      //     fits_read_key(fptr_,TDOUBLE,"BAD_DATA",&dnull,NULL,&status);
+      //  if (status != 0) 
+      //	{
+      //	  dnull = -1.6375e30;  // default value
+      //	  status = 0;
+      //	}
+      if (nentries != nrows_*repeat)
+	{
+	  cout << " found " << nentries << " pixels, expected:  " << nrows_*repeat << endl;
+	  throw PException(" FitsFile:::GetBinTabFCol ");
+	}
       fits_read_col(fptr_,TDOUBLE,NoCol+1,1,1,nels,&dnull,valeurs,
 			    &anull,&status);
       if( status ) printerror( status,"erreur lecture de colonne" );
+      
     }
 
@@ -422,7 +488,18 @@
 	}
       long nels=nentries;
+      int    anull;
       // no checking for undefined pixels
-      int    anull;
       float fnull= 0.;
+      //     fits_read_key(fptr_,TFLOAT,"BAD_DATA",&fnull,NULL,&status);
+      //    if (status != 0) 
+      //	{
+      //	  fnull = -1.6375e30;  // default value
+      //	  status = 0;
+      //	}
+      if (nentries != nrows_*repeat)
+	{
+	  cout << " found " << nentries << " pixels, expected:  " << nrows_*repeat << endl;
+	  throw PException(" FitsFile:::GetBinTabFCol ");
+	}
       fits_read_col(fptr_,TFLOAT,NoCol+1,1,1,nels,&fnull,valeurs,
 			    &anull,&status);
@@ -431,4 +508,5 @@
   void FitsFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
     {
+      cout <<" entree GetBinTabFCol " << endl;
       int status= 0;
       int DTYPE;
@@ -443,4 +521,15 @@
       int    anull;
       int inull= 0;
+      //     fits_read_key(fptr_,TINT,"BAD_DATA",&inull,NULL,&status);
+      //     if (status != 0) 
+      //	{
+      //	  inull = -999999;  // default value
+      //	  status = 0;
+      //	}
+      if (nentries != nrows_*repeat)
+	{
+	  cout << " found " << nentries << " pixels, expected:  " << nrows_*repeat << endl;
+	  throw PException(" FitsFile:::GetBinTabFCol ");
+	}
       fits_read_col(fptr_,TINT,NoCol+1,1,1,nels,&inull,valeurs,
 			    &anull,&status);
@@ -460,5 +549,10 @@
       // no checking for undefined pixels
       int    anull;
-      char* cnull= " ";
+      char* cnull= "";
+      if (nentries != nrows_*repeat/width)
+	{
+	  cout << " found " << nentries << " pixels, expected:  " << nrows_*repeat/width << endl;
+	  throw PException(" FitsFile:::GetBinTabFCol ");
+	}
       long frow=1;
       long felem=1;
@@ -521,5 +615,6 @@
     }
   int index=-1;
-  for (int k=0; k<=nocol; k++) 
+  int k;
+  for (k=0; k<=nocol; k++) 
     {
       if (types_[k] == 'S') index++;
@@ -617,5 +712,5 @@
 
 
-void FitsFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines) const
+void FitsFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines) 
 { 
   int status = 0;
@@ -633,5 +728,6 @@
 
 
-  for (int k=0; k<tfields;k++)
+  int k;
+  for (k=0; k<tfields;k++)
     {
       char  format[FLEN_VALUE];
@@ -671,5 +767,4 @@
       strcpy(tform[k],format);
     }
-  const char* TypeOfContent= dvl.GetS("TypeOfContent").c_str();
   // value of the EXTNAME keyword
   char extn[FLEN_VALUE];
@@ -684,5 +779,7 @@
   if( status ) printerror( status );
 
-  for(int ii = 0; ii < tfields; ii++)
+  hdunum_++;
+  int ii;
+  for(ii = 0; ii < tfields; ii++)
     {
       delete [] ttype[ii];
@@ -696,4 +793,5 @@
   // 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++)
@@ -708,5 +806,5 @@
 	  {
 	    int ival=(*it).second.elval.mtv.iv;
-	    strcpy(comment,"I entier");
+	    strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
 	    fits_write_key(fptr_,TINT,keyname,&ival,comment,&status);
 	    break;
@@ -715,5 +813,5 @@
 	  {
 	    double dval=(*it).second.elval.mtv.dv;
-	    strcpy(comment,"D double");
+	    strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
 	    fits_write_key(fptr_,TDOUBLE,keyname,&dval,comment,&status);
 	    break;
@@ -723,5 +821,5 @@
 	    char strval[128];
 	    strncpy(strval,(*it).second.elval.mtv.strv,127);
-	    strcpy(comment,"S character string");
+	    strncpy(comment,(*it).second.elcomm.c_str(),FLEN_COMMENT );
 	    fits_write_key(fptr_,TSTRING,keyname,&strval,comment,&status);
 	    break;
@@ -730,4 +828,5 @@
   if( status )  printerror( status,"fitsfile: probleme ecriture mot-cle du dvlist" );
     }
+  fits_write_comment(fptr_,"--------------------------------------", &status);
 
 }
@@ -736,5 +835,5 @@
 {
   int status = 0;
-  int hdutype, hdunum;
+  int hdutype;
   fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
   if( status ) printerror(status,"putColToFits: le movabs a foire");
@@ -745,8 +844,4 @@
      throw IOExc("FitsFile::putColToFits, this HDU is not an ASCII table nor a binary table");
           }
-  //  if(hdutype == ASCII_TBL && nocol>0)
-  //  {
-  //   throw IOExc("FitsFile::putColToFits, this HDU is an ASCII table, nocol>0  forbidden");
-  //  }
   int code; 
   long repeat, width;
@@ -806,8 +901,8 @@
   if( code != TLONG &&  code != TINT &&  code != TSHORT ) 
     {
-      cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= FLOAT " << endl;
+      cout << " WARNING : types don't match (putColToFits) : on fits file= " << code << " (FITS code), to be written= INT " << endl;
     } 
   fits_write_col(fptr_,TINT,nocol+1,1,1,nentries, donnees ,&status);
-  if( status )  printerror( status,"erreur ecriture du fichier fits" );
+  if( status )  printerror( status," ecriture du fichier fits" );
 }
 void FitsFile::putColToFits(int nocol, int nentries, char** donnees) const
@@ -857,5 +952,6 @@
 
   cout << " Header listing for HDU : " << hdunum_ << endl;
-  for(int jj = 1; jj <= nkeys; jj++)  
+  int jj;
+  for(jj = 1; jj <= nkeys; jj++)  
     {
       if( fits_read_record(fptr_,jj,card,&status) )
@@ -875,9 +971,17 @@
   char strval[FLEN_VALUE];
   char comment[FLEN_COMMENT];
-  
-  strcpy(keyname,"CREATOR");
-  strcpy(strval,"SOPHYA");
-  strcpy(comment,"SOPHYA Package - FitsFile");
-  fits_write_key(fptr_,TSTRING,keyname,&strval,comment,&status);
+  strncpy(keyname, "CREATOR", LEN_KEYWORD); 
+  keyname[LEN_KEYWORD-1] = '\0';
+  strcpy(strval, "SOPHYA");
+  strcpy(comment," SOPHYA Package - FITSIOServer ");
+  fits_write_key(fptr_, TSTRING, keyname, &strval, comment, &status);
+  if( status ) printerror( status );
+
+  fits_write_comment(fptr_,"..............................................", &status);
+  fits_write_comment(fptr_, " SOPHYA package - FITSIOSever ", &status);
+  fits_write_comment(fptr_, " (C) LAL/IN2P3-CNRS Orsay,  FRANCE 2000", &status);
+  fits_write_comment(fptr_, " (C) DAPNIA/CEA     Saclay, FRANCE 2000", &status);
+  fits_write_comment(fptr_,"..............................................", &status);
+  if( status ) printerror( status );
 }
 
Index: /trunk/SophyaExt/FitsIOServer/fitsfile.h
===================================================================
--- /trunk/SophyaExt/FitsIOServer/fitsfile.h	(revision 970)
+++ /trunk/SophyaExt/FitsIOServer/fitsfile.h	(revision 971)
@@ -76,5 +76,5 @@
 
 */ 
-  void    WriteF(char flnm[],int hdunum= 0); 
+  void    WriteF(char flnm[], bool OldFile=false); 
 
 
@@ -188,5 +188,5 @@
 \param <naxisn>  array containind sizes of the different dimensions
   */
-  void makeHeaderImageOnFits(char type, int nbdim, int* naxisn) const;
+  void makeHeaderImageOnFits(char type, int nbdim, int* naxisn) ;
 
   /*! create an BINTABLE header on FITS file.
@@ -199,12 +199,13 @@
 \param <taille_des_chaines> vector containing the number of characters of  data  for each char* typed column, with order of appearance in 'fieldType'
    */
-  void makeHeaderBntblOnFits ( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname,  vector<int> taille_des_chaines) const;
-
+  void makeHeaderBntblOnFits ( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname,  vector<int> taille_des_chaines) ;
+
+  void moveToFollowingHeader();
   void readheader();
 static  void printerror(int&) ;
 static  void printerror(int&,char* texte) ;
   protected:  
-    virtual void    ReadFromFits(const FitsFile& ff)=0;           
-    virtual void    WriteToFits(const FitsFile& ff)=0;           
+    virtual void    ReadFromFits(FitsFile& ff)=0;           
+    virtual void    WriteToFits(FitsFile& ff)=0;           
 private:
 
@@ -215,5 +216,5 @@
 				  vector<char>& types,   
 				  vector<int>&  taille_des_chaines);
-static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl); 
+static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum); 
   void writeSignatureOnFits() const; 
 
