Index: trunk/SophyaExt/FitsIOServer/fitsfile.cc
===================================================================
--- trunk/SophyaExt/FitsIOServer/fitsfile.cc	(revision 1082)
+++ trunk/SophyaExt/FitsIOServer/fitsfile.cc	(revision 1136)
@@ -6,11 +6,39 @@
 #include "anydataobj.h" 
 #include "fitsspherehealpix.h"
-FitsFile::FitsFile() 
-{
-  InitNull();
-}
+
+
+void   FitsIOHandler::Read(char flnm[],int hdunum)
+{
+  //  FitsInFile ifts(flnm, hdunum);
+  FitsInFile ifts(flnm);
+  Read(ifts, hdunum);
+}
+void FitsIOHandler::Read(FitsInFile& is, int hdunum) 
+{
+  if (hdunum == 0)  is.moveToFollowingHeader();
+  else is.ReadFInit(hdunum);
+  ReadFromFits(is);
+}
+
+
+void FitsIOHandler::Write(char flnm[], bool OldFile) 
+
+{
+
+  FitsOutFile of(flnm, OldFile);
+  Write(of);
+}
+
+void FitsIOHandler::Write(FitsOutFile& os) 
+{
+  WriteToFits(os);
+}
+
+
+
  
 FitsFile::~FitsFile()
 {
+  cout << " destructeur FitsFile " << endl;
   int status = 0;
   if( fptr_ != NULL)
@@ -22,10 +50,61 @@
 }
 
-void FitsFile::InitNull()
-{
-  fptr_= NULL; 
-  hdutype_= 0;
-  hdunum_ = 0; 
-
+
+void FitsFile::printerror(int &status) 
+  //*****************************************************/
+  //* Print out cfitsio error messages and exit program */
+  //*****************************************************/
+{
+  if( status )
+    {
+      fits_report_error(stderr,status);
+      throw IOExc("FitsFile:: error FITSIO status");
+    }
+  return;
+}
+
+void FitsFile::printerror(int& status, char* texte) 
+  //*****************************************************/
+  //* Print out cfitsio error messages and exit program */
+  //*****************************************************/
+{
+  // print out cfitsio error messages and exit program 
+  // print error report  
+  fits_report_error(stderr, status);
+  cout << " erreur:: " << texte << endl;
+  throw IOExc("FitsFile:: error FITSIO status");
+}
+
+void FitsFile::ResetStatus(int& status) 
+{
+  fits_status_ = status;
+  status = 0;
+}
+
+string FitsFile::getErrStatus(int status) 
+{
+  char text[31];
+  fits_get_errstatus(status, text);
+  return string(text);
+}
+
+FitsInFile::FitsInFile()
+{
+  InitNull();
+}
+//FitsInFile::FitsInFile(char flnm[], int hdunum)
+FitsInFile::FitsInFile(char flnm[])
+{
+  InitNull();
+  int status = 0;
+  fits_open_file(&fptr_,flnm,READONLY,&status);
+  if( status ) printerror( status );
+  //  ReadFInit(flnm, hdunum);
+}
+
+
+void FitsInFile::InitNull()
+{
+  cout << " init FitsInFile " << endl;
   bitpix_ = 0;
   naxis_ = 0;
@@ -33,5 +112,4 @@
   nrows_ = 0;
   nbcols_ = 0;
-  fits_status_ = 0;
   naxisn_.clear();
   repeat_.clear();
@@ -42,5 +120,5 @@
 
 
-int FitsFile::NbBlocks(char flnm[])
+int FitsInFile::NbBlocks(char flnm[])
 {
   int status = 0;
@@ -54,5 +132,5 @@
 }
 
-void FitsFile::getBlockType(char flnm[], int hdunum, string& typeOfExtension, int& naxis, vector<int>& naxisn, string& dataType, DVList& dvl )
+void FitsInFile::getBlockType(char flnm[], int hdunum, string& typeOfExtension, int& naxis, vector<int>& naxisn, string& dataType, DVList& dvl )
 {
   int status = 0;
@@ -123,22 +201,46 @@
 }
 
-void FitsFile::ReadF(char flnm[],int hdunum) 
-{
-  ReadFInit(flnm, hdunum);
-  //  ReadFromFits(*this);
-    ReadFromFits();
-}
-
-
-//FitsFile* FitsFile::ReadFInit(char flnm[],int hdunum) 
-void FitsFile::ReadFInit(char flnm[],int hdunum) 
-{
-  InitNull();
-  int status = 0;
-  //  hdutype_= 0;
+void FitsInFile::GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn)
+{
+  int hdunum=0;
+  cout << " Reading a FITS image in HDU : " << fits_get_hdu_num(fileptr,&hdunum)  << endl;
+  int status= 0;
+
+  // bits per pixels
+  fits_read_key(fileptr,TINT,"BITPIX",&bitpix,NULL,&status);
+  if( status )  printerror( status );
+
+  // number of dimensions in the FITS array
+  naxis= 0;
+  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] ;
+  int nfound;
+  fits_read_keys_lng(fileptr,"NAXIS",1,naxis,naxes,&nfound,&status);
+  if( status ) printerror( status );
+  if (nfound != naxis ) 
+    cout << " WARNING : " << nfound << " axes found, expected naxis= " << naxis << endl;
+  int k;
+  for (k=0; k<naxis; k++) 
+    {
+      naxisn.push_back( (int)naxes[k] );
+    }
+  delete [] naxes;
+}
+
+
+//void FitsInFile::ReadFInit(char flnm[],int hdunum) 
+void FitsInFile::ReadFInit(int hdunum) 
+{
+  // InitNull();
+  // int status = 0;
   
-  fits_open_file(&fptr_,flnm,READONLY,&status);
-  if( status ) printerror( status );
-  //
+  //  fits_open_file(&fptr_,flnm,READONLY,&status);
+  //  if( status ) printerror( status );
+
+  int status = 0;
+
   if (hdunum <= 1) 
     {
@@ -171,8 +273,9 @@
       moveToFollowingHeader();
     }
-  //  return this;
-}
-
-void FitsFile::moveToFollowingHeader()
+}
+
+
+
+void FitsInFile::moveToFollowingHeader()
 {
   int status = 0;
@@ -197,32 +300,244 @@
 }
 
- 
-void FitsFile::WriteF(char flnm[], bool OldFile)
-{
-  int status = 0; 
-  hdutype_= 0;
-  hdunum_ = 0;
-
- 
-  // create new FITS file
-  if (!OldFile)
-    {
-      fits_create_file(&fptr_,flnm,&status);
-      if( status ) printerror(status,"file already exists");
-    }
+
+int  FitsInFile::NbColsFromFits() const
+{
+  if(hdutype_ ==  BINARY_TBL) return nbcols_;
   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);
-  WriteToFits();
-}
-void FitsFile::GetSingleColumn(double* map, int nentries) const
+    if(hdutype_ == ASCII_TBL ||  hdutype_ == IMAGE_HDU) return 1;
+    else
+      {
+	cout << " hdutype= " << hdutype_ << endl;
+	throw PException("FitsFile::NbColsFromFits, this HDU is unknown");
+      }
+}
+
+int FitsInFile::NentriesFromFits(int nocol) const
+{
+  if(hdutype_ == BINARY_TBL  )   return nrows_*repeat_[nocol];
+  else
+    if(hdutype_ == ASCII_TBL) return nrows_;
+    else
+      if(hdutype_ == IMAGE_HDU) return nbData_;
+      else
+	{
+	  cout << "hdutype= " << hdutype_ << endl;
+	  throw PException("FitsFile::NentriesFromFits, this HDU is unknown");
+	}
+}
+
+char FitsInFile::ColTypeFromFits(int nocol) const
+{
+  if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
+    {
+      throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
+    }
+  return types_[nocol];
+}
+string FitsInFile::ColNameFromFits(int nocol) const
+{
+  if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
+    {
+      throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
+    }
+  return noms_[nocol];
+}
+
+int FitsInFile::ColStringLengthFromFits(int nocol) const
+{
+  if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
+    {
+      throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
+    }
+  int index=-1;
+  int k;
+  for (k=0; k<=nocol; k++) 
+    {
+      if (types_[k] == 'S') index++;
+    }
+  return  taille_des_chaines_[index];
+}
+void FitsInFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata) 
+{
+  int status= 0;
+  int    anull;
+  double dnull= 0.;
+  float fnull= 0.;
+  int inull= 0;
+  char* cnull= "";
+  int dcount = 0.;
+  int fcount = 0.;
+  int icount = 0;
+  int ccount =0;
+  int ncol;
+  long nels=1;
+  for (ncol=0; ncol<nbcols_; ncol++)
+    {
+      switch (types_[ncol])
+	{ 
+	case 'D' :
+	  fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ddata[dcount++],&anull,&status);
+	    break;
+	case 'E' :
+	  fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[fcount++],&anull,&status);
+	  break;
+	case 'I' :
+	  fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&idata[icount++],
+			&anull,&status);
+	  break;
+	case 'S' :
+	  fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&cdata[ccount++],&anull,&status);
+	  break;
+	}
+      if (status)
+	{
+	  ResetStatus(status);
+	  break;
+	}
+    }
+}
+
+void FitsInFile::GetBinTabLine(int NoLine, float* fdata) 
+{
+  int status= 0;
+  int    anull;
+  float fnull= 0.;
+  long nels=1;
+  int ncol;
+  for (ncol=0; ncol<nbcols_; ncol++)
+    {
+      fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[ncol],&anull,&status);
+      if (status)
+	{
+	  ResetStatus(status);
+	  break;
+	}
+    }
+}
+
+
+void FitsInFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
+    {
+      int status= 0;
+      int DTYPE;
+      long repeat,width;
+            fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
+      if( DTYPE != TDOUBLE) 
+	{
+	  if (DTYPE == TFLOAT)  cout << " WARNING: reading double from float : conversion will be made by fitsio library" << endl;
+	  else
+	    throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
+	}
+      long nels=nentries;
+      int    anull;
+      // no checking for undefined pixels
+      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" );
+      
+    }
+
+void FitsInFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
+    {
+      int status= 0;
+      int DTYPE;
+      long repeat,width;
+      fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
+      if( DTYPE != TFLOAT) 
+	{
+	  if (DTYPE == TDOUBLE)  cout << " WARNING: reading float from double : conversion will be made by fitsio library" << endl;
+	  else
+	    throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
+	}
+      long nels=nentries;
+      int    anull;
+      // no checking for undefined pixels
+      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);
+      if( status ) printerror( status,"erreur lecture de colonne" );
+    }
+
+void FitsInFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
+    {
+      cout <<" entree GetBinTabFCol " << endl;
+      int status= 0;
+      int DTYPE;
+      long repeat,width;
+      fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
+      if( DTYPE != TLONG &&  DTYPE != TINT &&  DTYPE != TSHORT ) 
+	{
+	  throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non entier");
+	}
+      long nels=nentries;
+      // no checking for undefined pixels
+      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);
+      if( status ) printerror( status,"erreur lecture de colonne" );
+    }
+
+void FitsInFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
+    {
+      int status= 0;
+      int DTYPE;
+      long repeat,width;
+      fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
+      if( DTYPE != TSTRING ) 
+	{
+	  throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
+	}
+      long nels=nentries;
+      // no checking for undefined pixels
+      int    anull;
+      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;
+      fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
+		    &anull,&status);
+      if( status ) printerror( status,"erreur lecture de colonne" );
+    }
+
+void FitsInFile::GetSingleColumn(double* map, int nentries) const
 { 
   int status = 0;
@@ -256,5 +571,5 @@
 }
 
-void FitsFile::GetSingleColumn(float* map, int nentries) const
+void FitsInFile::GetSingleColumn(float* map, int nentries) const
 {
   int status = 0;
@@ -286,5 +601,5 @@
 }
 
-void FitsFile::GetSingleColumn( int* map, int nentries) const
+void FitsInFile::GetSingleColumn( int* map, int nentries) const
 {
   int status = 0;
@@ -315,96 +630,93 @@
       }
 }
-void FitsFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn) 
-{
-  int status = 0;
-  long naxis = nbdim;
-  long* naxes = new long[nbdim];
-  if (hdunum_ == 0) 
-    {
-      fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
-      hdunum_ = 1;
-    }
-  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);
-  else
-    if  (type == 'E')
-      fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
-    else
-      if  (type == 'I')
-	fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
-      else
-	{
-	  cout << " type of data: " << type << endl;
-	  throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
-	}
-
-  hdunum_++;
-  delete [] naxes;
-  if( status ) printerror( status, "erreur creation HDU IMAGE"  );
-
-}
-void FitsFile::putImageToFits(int nbData, double* map) const
-{
-  int status = 0;
-  long npix= nbData;
-  fits_write_img(fptr_,TDOUBLE,1,npix,map,&status); 
-  if( status ) printerror( status, "erreur ecriture putImageToFits" );
-  writeSignatureOnFits();
-}
-
-void FitsFile::putImageToFits(int nbData, float* map) const
-{
-  int status = 0;
-  long npix= nbData;
-  fits_write_img(fptr_,TFLOAT,1,npix, map,&status); 
-  if( status ) printerror( status, "erreur ecriture putImageToFits" );
-  writeSignatureOnFits();
-
-}
-void FitsFile::putImageToFits( int nbData, int* map) const
-{
-  int status = 0;
-
-  long npix= nbData;
-  fits_write_img(fptr_,TINT,1,npix,map,&status); 
-  if( status ) printerror( status, "erreur ecriture putImageToFits" );
-  writeSignatureOnFits();
-}
-
-
-
-void FitsFile::GetImageParameters (fitsfile* fileptr,int& bitpix,int& naxis,vector<int>& naxisn)
-{
+
+void FitsInFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
+				  vector<int>& repeat,
+				  vector<string>& noms, 
+				  vector<char>& types,   
+				  vector<int>&  taille_des_chaines)
+{
+  int status= 0;
   int hdunum=0;
-  cout << " Reading a FITS image in HDU : " << fits_get_hdu_num(fileptr,&hdunum)  << endl;
-  int status= 0;
-
-  // bits per pixels
-  fits_read_key(fileptr,TINT,"BITPIX",&bitpix,NULL,&status);
-  if( status )  printerror( status );
-
-  // number of dimensions in the FITS array
-  naxis= 0;
-  fits_read_key(fileptr,TINT,"NAXIS",&naxis,NULL,&status);
+  int hdutype=0;
+  fits_get_hdu_num(fileptr,&hdunum);
+  fits_get_hdu_type(fileptr, &hdutype, &status);
+
+  if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
+    {
+      throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
+    }
+  if(hdutype == ASCII_TBL)
+    cout << " Reading a FITS ascii table in HDU : " << hdunum << endl;  
+  if(hdutype == BINARY_TBL)
+    cout << " Reading a FITS binary table in HDU : " << hdunum << endl;
+
+  // get the number of columns
+  fits_get_num_cols(fileptr, &nbcols,&status);
   if( status ) printerror( status );
 
-  // read the NAXISn keywords to get image size
-  long* naxes = new long[naxis] ;
+  // get the number of rows
+  long naxis2= 0;
+  fits_get_num_rows(fileptr,&naxis2,&status);
+  if( status ) printerror( status );
+  nrows = (int)naxis2;
+
+  // get the datatype,  names and the repeat count
+  noms.clear();
+  noms.reserve(nbcols);
+  types.clear();
+  types.reserve(nbcols);
+  repeat.clear();
+  repeat.reserve(nbcols);
+  taille_des_chaines.clear();
+  char **ttype = new char*[nbcols];
+  int ii;
+  for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
   int nfound;
-  fits_read_keys_lng(fileptr,"NAXIS",1,naxis,naxes,&nfound,&status);
-  if( status ) printerror( status );
-  if (nfound != naxis ) 
-    cout << " WARNING : " << nfound << " axes found, expected naxis= " << naxis << endl;
-  int k;
-  for (k=0; k<naxis; k++) 
-    {
-      naxisn.push_back( (int)naxes[k] );
-    }
-  delete [] naxes;
-}
-
-void FitsFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
+  fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
+  if( status ) printerror( status,"erreur lecture des noms de colonne");
+  int rept=0;
+  for(ii = 0; ii <  nbcols; ii++)
+    {
+      int DTYPE;
+      long width;
+      long repete = 0;
+      fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
+      if( status ) printerror( status,"erreur lecture type de colonne");
+      rept = repete;
+      noms.push_back(string(ttype[ii]));
+      switch (DTYPE)
+	{
+	case TDOUBLE :
+	  types.push_back('D');
+	  break;
+	case TFLOAT :
+	  types.push_back('E');
+	  break;
+	case TLONG :
+	  types.push_back('I');
+	  break;
+	case TINT :
+	  types.push_back('I');
+	  break;
+	case TSHORT :
+	  types.push_back('I');
+	  break;
+	case TSTRING :
+	  types.push_back('S');
+	  taille_des_chaines.push_back(width);
+	  rept/=width;
+	  break;
+	default :
+	  cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
+	  throw IOExc("FitsFile:: unknown type of field");
+	}
+      repeat.push_back(rept);
+    }
+  for (ii=0; ii < nbcols; ii++) delete [] ttype[ii];
+  delete [] ttype;
+}
+
+void FitsInFile::KeywordsIntoDVList(fitsfile* fileptr, DVList& dvl, int hdunum)
 {
   int status = 0;
@@ -473,342 +785,98 @@
 } 
 
-
- 
-  void FitsFile::GetBinTabFCol(double* valeurs,int nentries, int NoCol) const
-    {
-      int status= 0;
-      int DTYPE;
-      long repeat,width;
-            fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
-      if( DTYPE != TDOUBLE) 
-	{
-	  if (DTYPE == TFLOAT)  cout << " WARNING: reading double from float : conversion will be made by fitsio library" << endl;
-	  else
-	    throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non double");
-	}
-      long nels=nentries;
-      int    anull;
-      // no checking for undefined pixels
-      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" );
-      
-    }
-
-  void FitsFile::GetBinTabFCol(float* valeurs,int nentries, int NoCol) const
-    {
-      int status= 0;
-      int DTYPE;
-      long repeat,width;
-      fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
-      if( DTYPE != TFLOAT) 
-	{
-	  if (DTYPE == TDOUBLE)  cout << " WARNING: reading float from double : conversion will be made by fitsio library" << endl;
-	  else
-	    throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
-	}
-      long nels=nentries;
-      int    anull;
-      // no checking for undefined pixels
-      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);
-      if( status ) printerror( status,"erreur lecture de colonne" );
-    }
-  void FitsFile::GetBinTabFCol(int* valeurs,int nentries, int NoCol) const
-    {
-      cout <<" entree GetBinTabFCol " << endl;
-      int status= 0;
-      int DTYPE;
-      long repeat,width;
-      fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
-      if( DTYPE != TLONG &&  DTYPE != TINT &&  DTYPE != TSHORT ) 
-	{
-	  throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non entier");
-	}
-      long nels=nentries;
-      // no checking for undefined pixels
-      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);
-      if( status ) printerror( status,"erreur lecture de colonne" );
-    }
-  void FitsFile::GetBinTabFCol(char** valeurs, int nentries, int NoCol) const
-    {
-      int status= 0;
-      int DTYPE;
-      long repeat,width;
-      fits_get_coltype(fptr_, NoCol+1,&DTYPE,&repeat,&width,&status);
-      if( DTYPE != TSTRING ) 
-	{
-	  throw IOExc("FitsFile::GetBinTabFCol, tentative de lecture non float");
-	}
-      long nels=nentries;
-      // no checking for undefined pixels
-      int    anull;
-      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;
-      fits_read_col(fptr_,TSTRING,NoCol+1,frow,felem,nels,cnull,valeurs,
-		    &anull,&status);
-      if( status ) printerror( status,"erreur lecture de colonne" );
-    }
-
-
-
-void FitsFile::GetBinTabLine(int NoLine, double* ddata, float* fdata, int* idata, char ** cdata) 
-{
-  int status= 0;
-  int    anull;
-  double dnull= 0.;
-  float fnull= 0.;
-  int inull= 0;
-  char* cnull= "";
-  int dcount = 0.;
-  int fcount = 0.;
-  int icount = 0;
-  int ccount =0;
-  int ncol;
-  long nels=1;
-  for (ncol=0; ncol<nbcols_; ncol++)
-    {
-      switch (types_[ncol])
-	{ 
-	case 'D' :
-	  fits_read_col(fptr_,TDOUBLE,ncol+1,NoLine+1,1,1,&dnull,&ddata[dcount++],&anull,&status);
-	    break;
-	case 'E' :
-	  fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[fcount++],&anull,&status);
-	  break;
-	case 'I' :
-	  fits_read_col(fptr_,TINT,ncol+1,NoLine+1,1,1,&inull,&idata[icount++],
-			&anull,&status);
-	  break;
-	case 'S' :
-	  fits_read_col(fptr_,TSTRING,ncol+1,NoLine+1,1,1,cnull,&cdata[ccount++],&anull,&status);
-	  break;
-	}
-      if (status)
-	{
-	  ResetStatus(status);
-	  break;
-	}
-      //	  if( status ) printerror( status,"GetBinTabLine : erreur lecture de colonne char" );
-    }
-}
-
-void FitsFile::GetBinTabLine(int NoLine, float* fdata) 
-{
-  int status= 0;
-  int    anull;
-  float fnull= 0.;
-  long nels=1;
-  int ncol;
-  for (ncol=0; ncol<nbcols_; ncol++)
-    {
-      fits_read_col(fptr_,TFLOAT,ncol+1,NoLine+1,1,1,&fnull,&fdata[ncol],&anull,&status);
-      if (status)
-	{
-	  ResetStatus(status);
-	  break;
-	}
-       //      if( status ) printerror( status,"GetBinTabLine : erreur lecture de colonne float" );
-    }
-}
-
-      
-
-
-
-
-
-
-int  FitsFile::NbColsFromFits() const
-{
-  if(hdutype_ ==  BINARY_TBL) return nbcols_;
+FitsOutFile::FitsOutFile()
+{
+  InitNull();
+}
+
+FitsOutFile::FitsOutFile(char flnm[], bool OldFile)
+{
+
+  InitNull();
+  int status = 0; 
+
+  // create new FITS file
+  if (!OldFile)
+    {
+      fits_create_file(&fptr_,flnm,&status);
+      if( status ) printerror(status,"file already exists");
+    }
   else
-    if(hdutype_ == ASCII_TBL ||  hdutype_ == IMAGE_HDU) return 1;
+    {
+      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");
+
+    }
+
+}
+
+
+
+void FitsOutFile::makeHeaderImageOnFits(char type, int nbdim, int* naxisn) 
+{
+  int status = 0;
+  long naxis = nbdim;
+  long* naxes = new long[nbdim];
+  if (hdunum_ == 1)
+    {
+      if (imageOnPrimary_ == false) fits_create_img(fptr_,FLOAT_IMG,0,naxes,&status);
+      else  hdunum_--;
+    }
+  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);
+  else
+    if  (type == 'E')
+      fits_create_img(fptr_,FLOAT_IMG,naxis,naxes,&status);
     else
-      {
-	cout << " hdutype= " << hdutype_ << endl;
-	throw PException("FitsFile::NbColsFromFits, this HDU is unknown");
-      }
-}
-
-char FitsFile::ColTypeFromFits(int nocol) const
-{
-  if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
-    {
-      throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
-    }
-  return types_[nocol];
-}
-int FitsFile::NentriesFromFits(int nocol) const
-{
-  if(hdutype_ == BINARY_TBL  )   return nrows_*repeat_[nocol];
-  else
-    if(hdutype_ == ASCII_TBL) return nrows_;
-    else
-      if(hdutype_ == IMAGE_HDU) return nbData_;
+      if  (type == 'I')
+	fits_create_img(fptr_,LONG_IMG,naxis,naxes,&status);
       else
 	{
-	  cout << "hdutype= " << hdutype_ << endl;
-	  throw PException("FitsFile::NentriesFromFits, this HDU is unknown");
-	}
-}
-
-string FitsFile::ColNameFromFits(int nocol) const
-{
-  if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
-    {
-      throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
-    }
-  return noms_[nocol];
-}
-int FitsFile::ColStringLengthFromFits(int nocol) const
-{
-  if(hdutype_ != ASCII_TBL && hdutype_ != BINARY_TBL)
-    {
-      throw IOExc("FitsFile::TypeFromFits, this HDU is not an ASCII table nor a binary table");
-    }
-  int index=-1;
-  int k;
-  for (k=0; k<=nocol; k++) 
-    {
-      if (types_[k] == 'S') index++;
-    }
-  return  taille_des_chaines_[index];
-}
-
-
-
-void FitsFile::GetBinTabParameters(fitsfile* fileptr, int& nbcols, int& nrows,
-				  vector<int>& repeat,
-				  vector<string>& noms, 
-				  vector<char>& types,   
-				  vector<int>&  taille_des_chaines)
-{
-  int status= 0;
-  int hdunum=0;
-  int hdutype=0;
-  fits_get_hdu_num(fileptr,&hdunum);
-  fits_get_hdu_type(fileptr, &hdutype, &status);
-
-  if(hdutype != ASCII_TBL && hdutype != BINARY_TBL)
-    {
-      throw IOExc("FitsFile::GetBinTabParameters this HDU is not an ASCII table nor a binary table");
-    }
-  if(hdutype == ASCII_TBL)
-    cout << " Reading a FITS ascii table in HDU : " << hdunum << endl;  
-  if(hdutype == BINARY_TBL)
-    cout << " Reading a FITS binary table in HDU : " << hdunum << endl;
-
-  // get the number of columns
-  fits_get_num_cols(fileptr, &nbcols,&status);
-  if( status ) printerror( status );
-
-  // get the number of rows
-  long naxis2= 0;
-  fits_get_num_rows(fileptr,&naxis2,&status);
-  if( status ) printerror( status );
-  nrows = (int)naxis2;
-
-  // get the datatype,  names and the repeat count
-  noms.clear();
-  noms.reserve(nbcols);
-  types.clear();
-  types.reserve(nbcols);
-  repeat.clear();
-  repeat.reserve(nbcols);
-  taille_des_chaines.clear();
-  char **ttype = new char*[nbcols];
-  int ii;
-  for (ii=0; ii < nbcols; ii++) ttype[ii]=new char[FLEN_VALUE];
-  int nfound;
-  fits_read_keys_str(fileptr, "TTYPE",1,nbcols,ttype,&nfound, &status);
-  if( status ) printerror( status,"erreur lecture des noms de colonne");
-  int rept=0;
-  for(ii = 0; ii <  nbcols; ii++)
-    {
-      int DTYPE;
-      long width;
-      long repete = 0;
-      fits_get_coltype(fileptr,ii+1,&DTYPE,&repete,&width,&status);
-      if( status ) printerror( status,"erreur lecture type de colonne");
-      rept = repete;
-      noms.push_back(string(ttype[ii]));
-      switch (DTYPE)
-	{
-	case TDOUBLE :
-	  types.push_back('D');
-	  break;
-	case TFLOAT :
-	  types.push_back('E');
-	  break;
-	case TLONG :
-	  types.push_back('I');
-	  break;
-	case TINT :
-	  types.push_back('I');
-	  break;
-	case TSHORT :
-	  types.push_back('I');
-	  break;
-	case TSTRING :
-	  types.push_back('S');
-	  taille_des_chaines.push_back(width);
-	  rept/=width;
-	  break;
-	default :
-	  cout << " field " << ii+1 << " DTYPE= " << DTYPE << endl;
-	  throw IOExc("FitsFile:: unknown type of field");
-	}
-      repeat.push_back(rept);
-    } 
-}
-
-
-
-void FitsFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines) 
+	  cout << " type of data: " << type << endl;
+	  throw PException("FitsFile:::makeHeaderImageOnFits:unprogrammed type of data ");
+	}
+
+  hdunum_++;
+  delete [] naxes;
+  if( status ) printerror( status, "erreur creation HDU IMAGE"  );
+
+}
+void FitsOutFile::putImageToFits(int nbData, double* map) const
+{
+  int status = 0;
+  long npix= nbData;
+  fits_write_img(fptr_,TDOUBLE,1,npix,map,&status); 
+  if( status ) printerror( status, "erreur ecriture putImageToFits" );
+  writeSignatureOnFits();
+}
+
+void FitsOutFile::putImageToFits(int nbData, float* map) const
+{
+  int status = 0;
+  long npix= nbData;
+  fits_write_img(fptr_,TFLOAT,1,npix, map,&status); 
+  if( status ) printerror( status, "erreur ecriture putImageToFits" );
+  writeSignatureOnFits();
+
+}
+void FitsOutFile::putImageToFits( int nbData, int* map) const
+{
+  int status = 0;
+
+  long npix= nbData;
+  fits_write_img(fptr_,TINT,1,npix,map,&status); 
+  if( status ) printerror( status, "erreur ecriture putImageToFits" );
+  writeSignatureOnFits();
+}
+
+
+
+void FitsOutFile::makeHeaderBntblOnFits( char* fieldType, char** Noms, int nentries, int tfields, DVList &dvl, char* extname, vector<int> taille_des_chaines) 
 { 
   int status = 0;
@@ -817,5 +885,5 @@
     {
       cout << " nombre de champs :" << tfields << "nombre de types: " << strlen(fieldType) << endl;
-	      throw ParmError("FitsFile:: fields and types don't match");
+      throw ParmError("FitsFile:: fields and types don't match");
 
     }
@@ -824,6 +892,4 @@
   char largeur[FLEN_VALUE];
   int noColString=0;
-
-
   int k;
   for (k=0; k<tfields;k++)
@@ -847,9 +913,9 @@
 	  if (fieldType[k] == 'A') 
 	    {
-	    char largaux[FLEN_VALUE];
-	    sprintf(largeur,"%d",taille_des_chaines[noColString]);
-	    sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
-	    noColString++;
-	    strcpy(format, largaux);    
+	      char largaux[FLEN_VALUE];
+	      sprintf(largeur,"%d",taille_des_chaines[noColString]);
+	      sprintf(largaux,"%d",1024*taille_des_chaines[noColString]);
+	      noColString++;
+	      strcpy(format, largaux);    
 	    }
 	  else strcpy(format,"1024");
@@ -930,5 +996,5 @@
 }
 
-void FitsFile::putColToFits(int nocol, int nentries, double* donnees) const
+void FitsOutFile::putColToFits(int nocol, int nentries, double* donnees) const
 {
   int status = 0;
@@ -952,5 +1018,5 @@
   if( status )  printerror( status,"erreur ecriture du fichier fits" );
 }
-void FitsFile::putColToFits(int nocol, int nentries, float* donnees) const
+void FitsOutFile::putColToFits(int nocol, int nentries, float* donnees) const
 {
   int status = 0;
@@ -978,5 +1044,5 @@
   if( status )  printerror( status,"erreur ecriture du fichier fits" );
 }
-void FitsFile::putColToFits(int nocol, int nentries, int* donnees) const
+void FitsOutFile::putColToFits(int nocol, int nentries, int* donnees) const
 {
   int status = 0;
@@ -1004,5 +1070,5 @@
   if( status )  printerror( status," ecriture du fichier fits" );
 }
-void FitsFile::putColToFits(int nocol, int nentries, char** donnees) const
+void FitsOutFile::putColToFits(int nocol, int nentries, char** donnees) const
 {
   int status = 0;
@@ -1032,9 +1098,5 @@
 
 
-
-
-
-
-void FitsFile::writeSignatureOnFits() const
+void FitsOutFile::writeSignatureOnFits() const
 {
   int status = 0;
@@ -1060,43 +1122,3 @@
 
 
-void FitsFile::printerror(int &status) 
-  //*****************************************************/
-  //* Print out cfitsio error messages and exit program */
-  //*****************************************************/
-{
-  if( status )
-    {
-      fits_report_error(stderr,status);
-      throw IOExc("FitsFile:: error FITSIO status");
-    }
-  return;
-}
-
-void FitsFile::printerror(int& status, char* texte) 
-  //*****************************************************/
-  //* Print out cfitsio error messages and exit program */
-  //*****************************************************/
-{
-  // print out cfitsio error messages and exit program 
-  // print error report  
-  fits_report_error(stderr, status);
-  cout << " erreur:: " << texte << endl;
-  throw IOExc("FitsFile:: error FITSIO status");
-}
-
-int FitsFile::statusF() const
-{
-  return fits_status_;
-}
-void FitsFile::ResetStatus(int& status) 
-{
-  fits_status_ = status;
-  status = 0;
-}
-
-string FitsFile::getErrStatus(int status) 
-{
-  char text[31];
-  fits_get_errstatus(status, text);
-  return string(text);
-}
+
