Index: /trunk/SophyaExt/FitsIOServer/fitsfile.cc
===================================================================
--- /trunk/SophyaExt/FitsIOServer/fitsfile.cc	(revision 2859)
+++ /trunk/SophyaExt/FitsIOServer/fitsfile.cc	(revision 2860)
@@ -6,5 +6,4 @@
 #include "strutil.h"
 #include "anydataobj.h" 
-#include "fitsspherehealpix.h"
 
 /*!
@@ -281,17 +280,27 @@
 
 
-
- 
+FitsFile::FitsFile()
+  : FitsInOutFile()
+{
+  InitNull();
+}
+
+FitsFile::FitsFile(FitsInOutFile const& fios)
+  : FitsInOutFile(fios)
+{
+  InitNull();
+}
+
+
+/*RzDel 
 FitsFile::~FitsFile()
 {
-  int status = 0;
-    if( fptr_ != NULL)
-    {
-      fits_close_file(fptr_,&status);
-      // je ne fais pas      delete fptr_, c'est la lib. fitsio qui a fait 
-      // new...
-    }
-   if( status )  printerror( status );
-}
+  //  Close fait par le destructeur de FitsInOutFile - Reza , Dec 2005
+  //  int status = 0;
+  //  if( fptr_ != NULL)
+  //    fits_close_file(fptr_,&status);
+  // if( status )  printerror( status );
+}
+*/
 
 
@@ -353,4 +362,6 @@
 
 FitsInFile::FitsInFile()
+  : FitsFile()
+
 {
   InitNull();
@@ -358,17 +369,34 @@
 
 FitsInFile::FitsInFile(string const & flnm)
-{
-    InitNull();
+  : FitsFile()
+{
+  InitNull();
+  Open(flnm.c_str(), Fits_RO);
+  /*RZDEL
    int status = 0;
    fits_open_file(&fptr_,flnm.c_str(),READONLY,&status);
    if( status ) printerror( status );
+  */
 }
 
 FitsInFile::FitsInFile(const char * flnm)
-{
-    InitNull();
-   int status = 0;
+  : FitsFile()
+{
+  InitNull();
+  Open(flnm, Fits_RO);
+  /*RZDEL
+  int status = 0;
    fits_open_file(&fptr_,flnm,READONLY,&status);
    if( status ) printerror( status );
+  */
+}
+
+FitsInFile::FitsInFile(FitsInOutFile const& fios)
+  : FitsFile(fios)
+
+{
+  InitNull();
+  if (mode_ == Fits_Create)
+    throw FitsIOException("FitsInFile::FitsInFile(FitsInOutFile const& fios) newly created fits file");
 }
 
@@ -1569,4 +1597,5 @@
 
 FitsOutFile::FitsOutFile()
+  : FitsFile()
 {
   InitNull(); 
@@ -1580,4 +1609,5 @@
 
 FitsOutFile::FitsOutFile(string const & flnm, WriteMode wrm)
+  : FitsFile()
 {
   InitNull();
@@ -1586,4 +1616,5 @@
 
 FitsOutFile::FitsOutFile(const char * flnm, WriteMode wrm)
+  : FitsFile()
 {
   InitNull();
@@ -1591,4 +1622,17 @@
 }
 
+FitsOutFile::FitsOutFile(FitsInOutFile const& fios)
+  : FitsFile(fios)
+{
+  InitNull(); 
+  if (mode_ == Fits_RO)
+    throw FitsIOException("FitsOutFile::FitsOutFile(FitsInOutFile const& ) ReadOnly Fits file");
+}
+
+FitsOutFile::~FitsOutFile()
+{
+  if (dvlToPrimary_ != NULL) delete dvlToPrimary_;
+}
+
 void FitsOutFile::openoutputfitsfile(const char * flnm, WriteMode wrm)
 {
@@ -1597,51 +1641,54 @@
   // create new FITS file
   fits_create_file(&fptr_,flnm,&status);
-  if( status )
-    {
-
-      switch (wrm)
-	{
+  if( status )    {
+    switch (wrm) {
       // si on veut ecrire a la fin de ce fichier
-	case append :
+    case append :
+      status = 0;
+      fits_clear_errmsg();
+      fits_open_file(&fptr_,flnm,READWRITE,&status);
+      if( status ) {
+	cout << " error opening file: " << flnm << endl;
+	printerror(status, "failure opening a file supposed to exist");
+      }
+      else cout << " file " << flnm << " opened, new objects will be appended " << endl;
+      fits_get_num_hdus(fptr_, &hdunum_, &status);
+      int hdutype;
+      fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
+      if( status ) { 
+	printerror( status,":FitsFile::WriteF : erreur movabs");
+	throw FitsIOException("FitsOutFile::openoutputfitsfile()/fits movabs error");
+	break;
+	
+      case clear :
+	{
 	  status = 0;
 	  fits_clear_errmsg();
-	  fits_open_file(&fptr_,flnm,READWRITE,&status);
-	  if( status )
-	    {
-	      cout << " error opening file: " << flnm << endl;
-	      printerror(status, "failure opening a file supposed to exist");
-	    }
-	  else cout << " file " << flnm << " opened, new objects will be appended " << endl;
-	  fits_get_num_hdus(fptr_, &hdunum_, &status);
-	  int hdutype;
-	  fits_movabs_hdu(fptr_,hdunum_,&hdutype,&status);
-	  if( status ) printerror( status,":FitsFile::WriteF : erreur movabs");
-	  break;
-       
-	case clear :
-	  {
-	    status = 0;
-	    fits_clear_errmsg();
-	    char* newname = new char[strlen(flnm)+2];
-	    //
-	    newname[0] = '!';
-	    newname[1] = '\0';
-	    strcat(newname, flnm);
-	    fits_create_file(&fptr_,newname,&status);
-	    delete [] newname;
-	    if (status)
-	      {
-		cout << " error opening file: " << flnm << endl;
-		printerror(status, "unable to open file, supposed to exist");
-	      }
-	    else  cout << "  WARNING : file " << flnm << " is overwritten " << endl;
-	    break;
-	  }
-	case unknown : 
-	  printerror(status, " file seems already to exist");
-	  break;
-      
-	}
-    }
+	  char* newname = new char[strlen(flnm)+2];
+	  //
+	  newname[0] = '!';
+	  newname[1] = '\0';
+	  strcat(newname, flnm);
+	  fits_create_file(&fptr_,newname,&status);
+	  delete [] newname;
+	  if (status) {
+	    cout << " error opening file: " << flnm << endl;
+	    printerror(status, "unable to open file, supposed to exist");
+	    throw FitsIOException("FitsOutFile::openoutputfitsfile()/fits open Error ");
+	  }
+	  else  cout << "  WARNING : file " << flnm << " is overwritten " << endl;
+	  break;
+	}
+      case unknown : 
+	printerror(status, " file seems already to exist");
+	throw FitsIOException("FitsOutFile::openoutputfitsfile()/fits open Error - existing file");
+	break;
+      }
+    }
+  }
+  fname_ = flnm;
+  if ( wrm == append ) mode_ = Fits_RW;
+  else mode_ = Fits_Create;
+  ownfptr = true;
 }
 
Index: /trunk/SophyaExt/FitsIOServer/fitsfile.h
===================================================================
--- /trunk/SophyaExt/FitsIOServer/fitsfile.h	(revision 2859)
+++ /trunk/SophyaExt/FitsIOServer/fitsfile.h	(revision 2860)
@@ -4,5 +4,5 @@
 #include "ndatablock.h"
 #include "dvlist.h"
-#include "FitsIO/fitsio.h" 
+#include "fitsinoutfile.h" 
 
 #define OPENFILE    0
@@ -75,5 +75,5 @@
 
 //! Class (virtual) for managing FITS format files
- class FitsFile {
+ class FitsFile : public FitsInOutFile {
 
  public:
@@ -148,6 +148,7 @@
 
 
-   FitsFile() { InitNull(); };
-   virtual ~FitsFile();
+   FitsFile();
+   FitsFile(FitsInOutFile const& fios);
+   // RzDel virtual ~FitsFile();
    static string GetErrStatus(int status);
    inline  int   statusF() const { return fits_status_;}
@@ -169,5 +170,4 @@
 inline void  InitNull() 
   {
-    fptr_ = NULL; 
     hdutype_= FitsExtensionType_NULL; 
     hdunum_ = 0; 
@@ -177,6 +177,4 @@
 
 
-
-   fitsfile *fptr_;     /**<  pointer to the FITS file, defined in fitsio.h */
    FitsExtensionType hdutype_;        /**<  image or bintable ? */
    int hdunum_;         /**<   index of header to be read/written */
@@ -197,5 +195,6 @@
    FitsInFile(string const & flnm);
    FitsInFile(const char * flnm);
-   ~FitsInFile() { ; };
+   FitsInFile(FitsInOutFile const& fios);
+   //   virtual ~FitsInFile();
 
    static int  NbBlocks(char flnm[]);
@@ -342,5 +341,6 @@
    FitsOutFile(string const & flnm, WriteMode wrm = unknown );
    FitsOutFile(const char * flnm, WriteMode wrm = unknown );
-   ~FitsOutFile() { if (dvlToPrimary_ != NULL) delete dvlToPrimary_;};
+   FitsOutFile(FitsInOutFile const& fios);
+   virtual ~FitsOutFile();
    inline void InitNull() {dvlToPrimary_ = NULL;}
 
Index: /trunk/SophyaExt/FitsIOServer/fitsinoutfile.cc
===================================================================
--- /trunk/SophyaExt/FitsIOServer/fitsinoutfile.cc	(revision 2859)
+++ /trunk/SophyaExt/FitsIOServer/fitsinoutfile.cc	(revision 2860)
@@ -111,4 +111,5 @@
   //  cout << " DBG - FitsInOutFile(string name= " << name << ")" << endl;
   fptr_ = NULL;
+  ownfptr = true;
   SetDef_BinTable();
   SetDef_StrColWidth();
@@ -122,7 +123,22 @@
   //  cout << " DBG - FitsInOutFile(char* name= " << name << ")" << endl;
   fptr_ = NULL;
+  ownfptr = true;
   SetDef_BinTable();
   SetDef_StrColWidth();
   Open(name, mode);
+}
+
+/*! \brief Copy constructor 
+  The fits file pointer is owned by the original object and should not be closed
+  as long as the new object is being used.
+*/
+FitsInOutFile::FitsInOutFile(FitsInOutFile const& fios)
+{
+  fptr_ = fios.fptr_;
+  fname_ = fios.fname_;
+  mode_ = fios.mode_;
+  ownfptr = false;
+  SetDef_BinTable();
+  SetDef_StrColWidth();
 }
 
@@ -177,4 +193,5 @@
   fname_ = name;
   mode_ = mode;
+  ownfptr = true;
   return;
 }
@@ -184,4 +201,5 @@
 void FitsInOutFile::Close()
 {
+  if (ownfptr == false) return;
   if (fptr_ == NULL) return;
   int status = 0;
Index: /trunk/SophyaExt/FitsIOServer/fitsinoutfile.h
===================================================================
--- /trunk/SophyaExt/FitsIOServer/fitsinoutfile.h	(revision 2859)
+++ /trunk/SophyaExt/FitsIOServer/fitsinoutfile.h	(revision 2860)
@@ -82,4 +82,5 @@
                    FitsInOutFile(string const & name, FitsIOMode mode);
                    FitsInOutFile(const char* name, FitsIOMode mode);
+                   FitsInOutFile(FitsInOutFile const& fios); 
  virtual           ~FitsInOutFile();
 
@@ -191,4 +192,5 @@
  string fname_;       //  File name as passed to creator
  FitsIOMode mode_;    
+ bool ownfptr;        //  If true, owns the FitsPointer, which will be closed by the destructor
 
  // Default extension name 
