Index: trunk/SophyaExt/FitsIOServer/fitsfile.cc
===================================================================
--- trunk/SophyaExt/FitsIOServer/fitsfile.cc	(revision 1231)
+++ trunk/SophyaExt/FitsIOServer/fitsfile.cc	(revision 1234)
@@ -104,6 +104,7 @@
 void FitsIOHandler::Read(FitsInFile& is, int hdunum) 
 {
-    if (hdunum == 0)  is.moveToFollowingHeader();
-   else is.ReadFInit(hdunum);
+  //  if (hdunum == 0 ) is.moveToFollowingHeader();
+  //  else is.ReadFInit(hdunum);
+  is.ReadFInit(hdunum);
   ReadFromFits(is);
 }
@@ -115,11 +116,8 @@
 for writing a new object in a new fits-extension :
 
-\warning By convention, primary header does not contain fits-image data : i.e. 
-all data are fits-extensions. The first relevant header will have hdunum=2.
-For switching off this convention use the method : 
+\warning By convention, primary header may contain fits-image data.
+For switching off this convention (i.e. to make sure that all data will be on fits-extensions) use the method : 
 
 firstImageOnPrimaryHeader() (see below)
-
-In that case do not forget to precise hdunum=1 when reading data on primary header.
 
 calls the method 'WriteToFits' from the inherited  object
@@ -330,42 +328,24 @@
 void FitsInFile::ReadFInit(int hdunum) 
 {
-   InitNull();
-  // int status = 0;
-  
-  //  fits_open_file(&fptr_,flnm,READONLY,&status);
-  //  if( status ) printerror( status );
-
-  int status = 0;
-
-  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();
-    }
+  InitNull();
+  int status = 0;
+
+  if (hdunum != 0 ) hdunum_ = hdunum;
+
+  // si le numero de header non precise
+  else
+    {
+      // si c'est le premier objet a lire  
+      if (hdunum_ == 0)
+	{
+	  // on calcule le numero de header a lire
+	  if (imageOnPrimary_ == true) hdunum_ = 1;
+	  else hdunum_ = 2;
+	}
+      // sinon objet suivant  
+      else hdunum_++;
+    }
+  getHeader();
+
 }
 
@@ -418,27 +398,56 @@
    }
 
+void FitsInFile::getHeader()
+{
+  int status=0;
+  if (hdunum_ < 1)  throw PException(" attempt to read hdunum < 1");
+  if (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
+    {
+      int hdutype;
+      fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
+      if( status ) printerror( status,":FitsInFile::getHeader : erreur movabs");
+      hdutype_= hdutype;
+      if(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_);
+	}
+      if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
+	{
+	  GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
+	  KeywordsIntoDVList(fptr_, dvl_, hdunum_);
+	}
+    }
+}
+
 
 void FitsInFile::moveToFollowingHeader()
 {
   int status = 0;
-  int hdutype;
-  InitNull();
-  fits_movrel_hdu(fptr_, 1,&hdutype,&status); 
-  if( status ) printerror( status," lecture du header suivant" );
   hdunum_++;
-  hdutype_= hdutype;
-  if(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_);
-    }
-  if(hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL)
-    {
-      GetBinTabParameters(fptr_,nbcols_, nrows_,repeat_, noms_, types_, taille_des_chaines_);
-      KeywordsIntoDVList(fptr_, dvl_, hdunum_);
-    }
+  getHeader();
 }
 
@@ -1182,11 +1191,13 @@
   long naxis = nbdim;
   long* naxes = new long[nbdim];
-  if (hdunum_ == 1)
+  //  if (hdunum_ == 1)
+  if (hdunum_ == 0)
     {
       if (imageOnPrimary_ == false)
 	{ 
+	  hdunum_ = 1;
 	  fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
 	  writeSignatureOnFits();	}
-      else  hdunum_--;
+      //      else  hdunum_--;
     }
   int k;
Index: trunk/SophyaExt/FitsIOServer/fitsfile.h
===================================================================
--- trunk/SophyaExt/FitsIOServer/fitsfile.h	(revision 1231)
+++ trunk/SophyaExt/FitsIOServer/fitsfile.h	(revision 1234)
@@ -71,4 +71,5 @@
    static string GetErrStatus(int status);
    inline  int   statusF() const { return fits_status_;}
+   inline void firstImageOnPrimaryHeader(bool choice) {imageOnPrimary_=choice;}
  
 
@@ -78,6 +79,6 @@
    static  void printerror(int&) ;
    static  void printerror(int&,char* texte) ;
-   inline void  InitNull() {fptr_ = NULL; hdutype_= 0; hdunum_ = 1; 
-   fits_status_ = 0;}
+   inline void  InitNull() {fptr_ = NULL; hdutype_= 0; hdunum_ = 0; 
+   fits_status_ = 0; imageOnPrimary_ = true;}
 
    fitsfile *fptr_;     /**<  pointer to the FITS file, defined in fitsio.h */
@@ -85,4 +86,5 @@
    int hdunum_;         /**<   index of header to be read/written */
    int fits_status_;    /**< last status returned by fitsio library. updated only by several methods */
+   bool imageOnPrimary_;
 
  };
@@ -184,4 +186,5 @@
 
 void InitNull();
+void getHeader();
 static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum); 
 static  void GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn);
@@ -211,5 +214,5 @@
    FitsOutFile(const char * flnm, WriteMode wrm = unknown );
    ~FitsOutFile() { ;};
-   inline void InitNull() {imageOnPrimary_=false;}
+   inline void InitNull() {;}
 
        //////////////////////////////////////////////////////////
@@ -224,5 +227,4 @@
 
 
-   inline void firstImageOnPrimaryHeader() {imageOnPrimary_=true;}
    void makeHeaderImageOnFits(char type, int nbdim, int* naxisn, DVList* dvl) ;
    void PutImageToFits( int nbData, double* map) const;
@@ -261,5 +263,4 @@
   void addKeywordsOfDVList(DVList& dvl) const;
 
-  bool imageOnPrimary_;
 
  };
Index: trunk/SophyaExt/FitsIOServer/fitsspherehealpix.cc
===================================================================
--- trunk/SophyaExt/FitsIOServer/fitsspherehealpix.cc	(revision 1231)
+++ trunk/SophyaExt/FitsIOServer/fitsspherehealpix.cc	(revision 1234)
@@ -231,5 +231,5 @@
      }
    FitsOutFile  fout(filename);
-   fout.firstImageOnPrimaryHeader();
+   fout.firstImageOnPrimaryHeader(true);
    FITS_TArray<r_4> fta(map);
    fta.Write(fout);
@@ -262,5 +262,5 @@
      }
    FitsOutFile  fout(filename);
-   fout.firstImageOnPrimaryHeader();
+   fout.firstImageOnPrimaryHeader(true);
    FITS_TArray<r_4> fta(map);
    fta.Write(fout);
