Index: trunk/SophyaExt/FitsIOServer/fitsfile.h
===================================================================
--- trunk/SophyaExt/FitsIOServer/fitsfile.h	(revision 1047)
+++ trunk/SophyaExt/FitsIOServer/fitsfile.h	(revision 1136)
@@ -12,12 +12,19 @@
 namespace SOPHYA {
 
+ class FitsFile;
+ class FitsInFile;
+ class FitsOutFile;
+
+
+
 //
-//! Virtual Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib)
+//! Class for managing Interface for SOPHYA objects to FITS Format Files (uses cfitsio lib)
 
 /*! 
-
+The class structure is analogous to Sophya-PPersist system :
 Each SOPHYA object XXX is associated with a object of class FITS_XXX 
- (inheriting from FitsFile), to which input/output operations with FITS 
- files are delegated. A typical example of use is the following : 
+ (inheriting from FitsFileHandler), to which input/output operations with FITS 
+ files are delegated (through a class Hierarchy : FitsFile (virtual), 
+ FitsInFile, FitsOutFile) . A typical example of use is the following : 
 
 \verbatim
@@ -39,4 +46,62 @@
 
 */
+
+class FitsIOHandler {
+
+
+  public:
+
+virtual ~FitsIOHandler() {}
+/*!
+this method is called from inherited objects : 
+
+opens a file 'flnm' 
+
+gets parameters in header (hdunum)
+
+calls the method 'ReadFromFits' from the inherited  object
+
+
+*/ 
+  void   Read(char flnm[],int hdunum= 2);
+/*!
+this method is called from inherited objects : 
+
+for writing a new object in a new fits-extension, at the end of 
+
+the existing file (flnm), if OldFile=true. 
+
+If OldFile=false, an exception occurs
+ 
+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 : 
+
+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
+
+*/ 
+  void   Write(char flnm[], bool OldFile=false) ;
+
+  /*!
+Read the data on extension hdunum (or primary header, if hdunum=1) from FitsInFIle. With default value for hdunum, one reads the next extension, with respect to the current position. 
+   */
+  void   Read(FitsInFile& ifts, int hdunum=0);
+  void   Write(FitsOutFile& ofts) ;
+
+
+  protected:  
+  virtual void    ReadFromFits(FitsInFile& is)=0;           
+  virtual void    WriteToFits(FitsOutFile& os) =0;           
+  friend class FitsInFile;
+  friend class FitsOutFile;
+
+  };
+
+
+
 class FitsFile
 {
@@ -44,72 +109,84 @@
 public:
 
-  FitsFile();
+  FitsFile() { InitNull();};
   virtual ~FitsFile();
+
+  static string getErrStatus(int status);
+
+
+
+
+inline  int statusF() const { return fits_status_;}
+ 
+
+protected:
+
+   void ResetStatus(int& status) ; 
+   static  void printerror(int&) ;
+   static  void printerror(int&,char* texte) ;
+   inline void InitNull() { cout << " init fitsfile " << endl; fptr_= NULL; hdutype_= 0; hdunum_ = 1; 
+   fits_status_ = 0;}
+
+  //! pointer to the FITS file, defined in fitsio.h 
+  fitsfile *fptr_; 
+  
+  //!  image or bintable ?
+  int hdutype_;
+
+//! index of header to be read/written
+  int hdunum_;  
+
+  //! last status returned by fitsio library. updated only by several methods
+  int fits_status_;
+
+    
+};
+
+
+ class FitsInFile : public  FitsFile {
+
+ public:
+   FitsInFile();
+   //   FitsInFile(char flnm[], int hdunum=0);
+   FitsInFile(char flnm[]);
+   ~FitsInFile() {  cout << " destructeur FitsInFile " << endl; };
+
+
+//////////////////////////////////////////////////////////
+//     methods with general purpose
+///////////////////////////////////////
+
+
+
   static int NbBlocks(char flnm[]);
   static void getBlockType(char flnm[], int hdunum, string& typeOfExtension, int& naxis, vector<int>& naxisn, string& dataType, DVList& dvl  );
 
-  protected: 
+
+
+  //  void ReadFInit(char flnm[],int hdunum=0);
+  void ReadFInit(int hdunum);
  
-/*!
-this method is called from inherited objects : 
-
-moves to header 'hdunum' on file 'flnm'
-
-gets parameters in header 
-
-calls the method 'ReadFromFits' from the inherited  object
-
-*/ 
-  void   ReadF(char flnm[],int hdunum= 0);
-  //  FitsFile* ReadFInit(char flnm[],int hdunum=0); 
-  void ReadFInit(char flnm[],int hdunum=0); 
- 
-/*!
-this method is called from inherited objects : 
-
-opens a file 'flnm' 
-
-gets parameters in header 
-
-calls the method 'ReadFromFits' from the inherited  object
-
-*/ 
-  void    WriteF(char flnm[], bool OldFile=false);
- //    virtual void    ReadFromFits(FitsFile& ff)=0;           
-
-
-
-  virtual void    ReadFromFits()=0;           
-  virtual void    WriteToFits()=0;           
-
-  static string getErrStatus(int status);
-
-
-  //////////////////////////////////////////////////////////
-  ///////   methods for managing extensions ////////////////
-  //////////////////////////////////////////////////////////
-
-//////////////////////////////////////////////////////////
-    //     methods with general purpose
-    ///////////////////////////////////////
-
   /*! return a reference on a DVList containing the keywords from FITS file
    */
-  inline const DVList& DVListFromFits() const { return dvl_;}
-
-  void moveToFollowingHeader();
-  int statusF() const; 
+inline const DVList& DVListFromFits() const { return dvl_;}
+
+void moveToFollowingHeader();
+
+
+       //////////////////////////////////////////////////////////
+       ///////   methods for managing extensions ////////////////
+       //////////////////////////////////////////////////////////
+
 
 
 /////////////////////////////////////////////////////////////
-  //       methods for managing FITS IMAGE extension
-  ///////////////////////////////////////////////////
-
-
-  //read
-  //----
+//       methods for managing FITS IMAGE extension
+///////////////////////////////////////////////////
+
 
 /*! return true if the current header  corresponds to a FITS image extension */
 inline bool IsFitsImage() const { return (hdutype_ == IMAGE_HDU);}
+
+
 
   /*! number of dimensions of an image extension : NAXIS parameter (in FITS notations)
@@ -125,46 +202,24 @@
 
 
-//write
-//-----
-
-
-  /*! create an IMAGE header on FITS file.
-\param <type> type of data (see method ColTypeFromFits)
-\param <nbdim>  number of dimensions : 1D, 2D, 3D etc. = NAXIS
-\param <naxisn>  array containind sizes of the different dimensions
-  */
-  void makeHeaderImageOnFits(char type, int nbdim, int* naxisn) ;
-
-  /*! write double data from array 'map'on an IMAGE extension 
-\param <nbData>  number of data to be written
-
-   */
-  void putImageToFits( int nbData, double* map) const;
-
-  /*! same as previous method with float data */
-  void putImageToFits(int nbData, float* map ) const;
-
-  /*! same as previous method with int data */
-  void putImageToFits(int nbData, int* map) const;
-
-
 
 //////////////////////////////////////////////////////////////////////////
-  //       methods for managing FITS BINARY TABLE or ASCII TABLE extension
-  ////////////////////////////////////////////////////////////////////////
-
-
-
-// read 
-//-----
+//       methods for managing FITS BINARY TABLE or ASCII TABLE extension
+////////////////////////////////////////////////////////////////////////
+
+
+
 
 /*! return true if the current header  corresponds to a FITS ASCII or BINTABLE extension */
 inline bool IsFitsTable() const {return (hdutype_ == ASCII_TBL || hdutype_ == BINARY_TBL);}
- /*! return number of columns (return 1 if IMAGE) */
-  int     NbColsFromFits() const;
-  /*! number of data in the current IMAGE extension on FITS file, or number
- of data of column number 'nocol' of the current BINTABLE extension
-  */
-  int     NentriesFromFits(int nocol) const;
+
+
+static  void GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
+				  vector<int>& repeat,
+				  vector<string>& noms, 
+				  vector<char>& types,   
+				  vector<int>&  taille_des_chaines);
+
+
+
   /*! return a character denoting data type of column number 'nocol' in a BINTABLE :
 
@@ -200,9 +255,5 @@
   void GetBinTabLine(int NoLine, float* fdata) ;
 
-
-
-
-
-  /*!
+/*!
 fill the array 'valeurs' with double data from the current BINTABLE extension on FITS file, from column number 'NoCol'
 
@@ -219,8 +270,123 @@
   // Write elements into the FITS data array
 
-
-
-  //write
-  //-----
+/////////////////////////////////////////////////////////////
+//       methods for managing any type of FITS extension
+////////////////////////////////////////////////////////
+
+ /*! return number of columns (return 1 if IMAGE) */
+  int     NbColsFromFits() const;
+  /*! number of data in the current IMAGE extension on FITS file, or number
+ of data of column number 'nocol' of the current BINTABLE extension
+  */
+  int     NentriesFromFits(int nocol) const;
+
+
+/*!
+fill the array 'map' with double data from the current extension on FITS file.
+If the extension is BINTABLE, the first column is provided. 
+
+\param <nentries>  number of data to be read
+  */
+  void    GetSingleColumn(double* map, int nentries) const;
+
+  /*! same as above with float data */
+  void    GetSingleColumn(float*  map, int nentries) const;
+
+  /*! same as above with int data */
+  void    GetSingleColumn(int* map, int nentries) const;
+
+  private :
+
+void InitNull();
+static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum); 
+static  void GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn);
+
+
+ 
+
+  //! fits-Image parameter 
+  int bitpix_;
+
+  //! fits-Image parameter 
+  int naxis_;
+
+  //! fits-Image parameters : sizes of dimensions
+  vector<int> naxisn_;
+
+  //! fits-Image parameter: number of data 
+  int nbData_;
+
+  //! Bintable parameter
+  int nrows_;
+
+  //! Bintable parameter
+  vector<int> repeat_;
+
+  //! Bintable parameter
+  int nbcols_;
+
+  //! Bintable parameter: column names
+  vector<string> noms_; 
+
+  //! Bintable parameters: types of columns (D: double, E: float, I: integers,  A: char*)
+  vector<char> types_;   
+
+  //! Bintable parameters:   length of the char* variables                 
+  vector<int>  taille_des_chaines_;
+
+  //! DVList for transferring keywords
+  DVList dvl_;
+
+
+ };
+
+
+ class FitsOutFile : public  FitsFile {
+
+ public:
+   FitsOutFile();
+   FitsOutFile(char flnm[], bool OldFile=false);
+   ~FitsOutFile() {  cout << " destructeur FitsOutFile " << endl;};
+   inline void InitNull() {imageOnPrimary_=false;}
+
+       //////////////////////////////////////////////////////////
+       ///////   methods for managing extensions ////////////////
+       //////////////////////////////////////////////////////////
+
+
+
+/////////////////////////////////////////////////////////////
+//       methods for managing FITS IMAGE extension
+///////////////////////////////////////////////////
+
+
+   inline void firstImageOnPrimaryHeader() {imageOnPrimary_=true;}
+
+  /*! create an IMAGE header on FITS file.
+\param <type> type of data (see method ColTypeFromFits)
+\param <nbdim>  number of dimensions : 1D, 2D, 3D etc. = NAXIS
+\param <naxisn>  array containind sizes of the different dimensions
+  */
+  void makeHeaderImageOnFits(char type, int nbdim, int* naxisn) ;
+
+  /*! write double data from array 'map'on an IMAGE extension 
+\param <nbData>  number of data to be written
+
+   */
+  void putImageToFits( int nbData, double* map) const;
+
+  /*! same as previous method with float data */
+  void putImageToFits(int nbData, float* map ) const;
+
+  /*! same as previous method with int data */
+  void putImageToFits(int nbData, int* map) const;
+
+
+
+//////////////////////////////////////////////////////////////////////////
+//       methods for managing FITS BINARY TABLE or ASCII TABLE extension
+////////////////////////////////////////////////////////////////////////
+
+
 
   /*! create an BINTABLE header on FITS file.
@@ -250,90 +416,13 @@
   void putColToFits(int nocol, int nentries, char** donnees) const;
 
-
-/////////////////////////////////////////////////////////////
-  //       methods for managing any type of FITS extension
-  ////////////////////////////////////////////////////////
-
-
-  /*!
-fill the array 'map' with double data from the current extension on FITS file.
-If the extension is BINTABLE, the first column is provided. 
-
-\param <nentries>  number of data to be read
-  */
-  void    GetSingleColumn(double* map, int nentries) const;
-
-  /*! same as above with float data */
-  void    GetSingleColumn(float*  map, int nentries) const;
-
-  /*! same as above with int data */
-  void    GetSingleColumn(int* map, int nentries) const;
-
-
-
-private:
-
-   void InitNull();
-   void ResetStatus(int& status) ; 
-   static  void printerror(int&) ;
-   static  void printerror(int&,char* texte) ;
-
-static  void GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn);
-static  void GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
-				  vector<int>& repeat,
-				  vector<string>& noms, 
-				  vector<char>& types,   
-				  vector<int>&  taille_des_chaines);
-static void KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum); 
+  private :
+
   void writeSignatureOnFits() const; 
 
-  //! pointer to the FITS file, defined in fitsio.h 
-  fitsfile *fptr_; 
-  
-  //!  image or bintable ?
-  int hdutype_;
-
-//! index of header to be read/written
-  int hdunum_;  
-
-
-  //! fits-Image parameter 
-  int bitpix_;
-
-  //! fits-Image parameter 
-  int naxis_;
-
-  //! fits-Image parameters : sizes of dimensions
-  vector<int> naxisn_;
-
-  //! fits-Image parameter: number of data 
-  int nbData_;
-
-  //! Bintable parameter
-  int nrows_;
-
-  //! Bintable parameter
-  vector<int> repeat_;
-
-  //! Bintable parameter
-  int nbcols_;
-
-  //! Bintable parameter: column names
-  vector<string> noms_; 
- 
-  //! Bintable parameters: types of columns (D: double, E: float, I: integers,  A: char*)
-  vector<char> types_;   
-    
-  //! Bintable parameters:   length of the char* variables                 
-  vector<int>  taille_des_chaines_;
-
-  //! DVList for transferring keywords
-  DVList dvl_;
-
-
-
-  //! last status returned by fitsio library. updated only by several methods
-  int fits_status_;
-};
+
+  bool imageOnPrimary_;
+
+ };
+
 
 
