Index: trunk/SophyaExt/FitsIOServer/fabtcolread.cc
===================================================================
--- trunk/SophyaExt/FitsIOServer/fabtcolread.cc	(revision 3111)
+++ trunk/SophyaExt/FitsIOServer/fabtcolread.cc	(revision 3114)
@@ -338,5 +338,5 @@
  SetNulVal(); SetDebug(0);
  NFitsRead = 0;
- FitsOF = NULL; FitsPtr = NULL;
+ FitsOF = NULL;
  LineDeb = LineFin = -1;
  Buffer = NULL;
@@ -352,5 +352,5 @@
  SetNulVal(); SetDebug(lp);
  NFitsRead = 0;
- FitsOF = NULL; FitsPtr = NULL;
+ FitsOF = NULL;
  LineDeb = LineFin = -1;
  Buffer = NULL;
@@ -361,6 +361,5 @@
    throw NullPtrError("FitsABTColRd::Init: FitsOpenFile pointer is NULL\n");
 
- FitsPtr = FitsOF->GetFitsPtr();
- if(FitsPtr==NULL)
+ if(GetFitsPtr()==NULL)
    throw NullPtrError("FitsABTColRd::Init: FitsPtr pointer is NULL\n");
 
@@ -394,5 +393,5 @@
 
  // Get number of columns
- if(fits_get_num_cols(FitsPtr,&NBcol,&sta)) {
+ if(fits_get_num_cols(GetFitsPtr(),&NBcol,&sta)) {
    FitsOpenFile::printerror(sta);
    throw NotAvailableOperation("FitsABTColRd::Init: Error getting number of columns\n");
@@ -403,5 +402,5 @@
 
  // Get number of rows
- if(fits_get_num_rows(FitsPtr,&NBline,&sta)) {
+ if(fits_get_num_rows(GetFitsPtr(),&NBline,&sta)) {
    FitsOpenFile::printerror(sta);
    throw NotAvailableOperation("FitsABTColRd::Init: Error getting number of rows\n");
@@ -415,5 +414,5 @@
  if(ColLabel.size() > 0) {
    strcpy(labelcol,ColLabel.c_str());
-   if(fits_get_colnum(FitsPtr,CASESEN,labelcol,&ColNum,&sta)) {
+   if(fits_get_colnum(GetFitsPtr(),CASESEN,labelcol,&ColNum,&sta)) {
      FitsOpenFile::printerror(sta);
      throw NotAvailableOperation("FitsABTColRd::Init: Error getting column name\n");
@@ -426,5 +425,5 @@
 
  // Get column type
- if(fits_get_coltype(FitsPtr,ColNum+1,&ColTypeCode,NULL,NULL,&sta)) {
+ if(fits_get_coltype(GetFitsPtr(),ColNum+1,&ColTypeCode,NULL,NULL,&sta)) {
    FitsOpenFile::printerror(sta);
    throw ParmError("FitsABTColRd::Init: Error getting column type\n");
@@ -440,8 +439,8 @@
  int rc=0;
  if(HDUType()==BINARY_TBL) {
-   fits_get_bcolparms(FitsPtr,ColNum+1,labelcol,tunit,tform
+   fits_get_bcolparms(GetFitsPtr(),ColNum+1,labelcol,tunit,tform
                      ,&repeat,&tscale,&tzero,NULL,tdisp,&sta);
  } else {
-   fits_get_acolparms(FitsPtr,ColNum+1,labelcol,&repeat,tunit,tform
+   fits_get_acolparms(GetFitsPtr(),ColNum+1,labelcol,&repeat,tunit,tform
                      ,&tscale,&tzero,NULL,tdisp,&sta);
  }
@@ -515,5 +514,5 @@
 double FitsABTColRd::ReadKey(char *keyname)
 {
- return FitsOpenFile::ReadKey(FitsPtr,keyname);
+ return FitsOpenFile::ReadKey(GetFitsPtr(),keyname);
 }
 
@@ -525,5 +524,5 @@
 long FitsABTColRd::ReadKeyL(char *keyname)
 {
- return FitsOpenFile::ReadKeyL(FitsPtr,keyname);
+ return FitsOpenFile::ReadKeyL(GetFitsPtr(),keyname);
 }
 
@@ -535,5 +534,5 @@
 string FitsABTColRd::ReadKeyS(char *keyname)
 {
- return FitsOpenFile::ReadKeyS(FitsPtr,keyname);
+ return FitsOpenFile::ReadKeyS(GetFitsPtr(),keyname);
 }
 
@@ -562,5 +561,5 @@
    NFitsRead++;
    double val;
-   fits_read_col(FitsPtr,TDOUBLE,ColNum+1,n+1,1,1,NULL,&val,NULL,&sta);
+   fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,n+1,1,1,NULL,&val,NULL,&sta);
    if(sta) {
      FitsOpenFile::printerror(sta);
@@ -597,5 +596,5 @@
    //cout<<"DBG-FitsRead: row1="<<row1<<" row2="<<row2<<" nrow="<<nrow
    //    <<" LineDeb,Fin="<<LineDeb<<","<<LineFin<<endl;
-   fits_read_col(FitsPtr,TDOUBLE,ColNum+1,row1,1,nrow,NULL,Buffer,NULL,&sta);
+   fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,row1,1,nrow,NULL,Buffer,NULL,&sta);
    if(sta) {
      FitsOpenFile::printerror(sta);
@@ -655,5 +654,5 @@
  //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
  int sta=0;
- fits_read_col(FitsPtr,TDOUBLE,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
+ fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
  if(sta) {
    FitsOpenFile::printerror(sta);
@@ -679,5 +678,5 @@
  //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
  int sta=0;
- fits_read_col(FitsPtr,TFLOAT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
+ fits_read_col(GetFitsPtr(),TFLOAT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
  if(sta) {
    FitsOpenFile::printerror(sta);
@@ -702,5 +701,5 @@
 
  int sta=0;
- fits_read_col(FitsPtr,TUSHORT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
+ fits_read_col(GetFitsPtr(),TUSHORT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
  if(sta) {
    FitsOpenFile::printerror(sta);
@@ -727,5 +726,5 @@
  int sta=0;
  int T = (sizeof(long)==4) ? TLONG: TINT;
- fits_read_col(FitsPtr,T,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
+ fits_read_col(GetFitsPtr(),T,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
  if(sta) {
    FitsOpenFile::printerror(sta);
@@ -751,5 +750,5 @@
 
  int sta=0;
- fits_read_col(FitsPtr,TLONGLONG,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
+ fits_read_col(GetFitsPtr(),TLONGLONG,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
  if(sta) {
    FitsOpenFile::printerror(sta);
@@ -984,5 +983,5 @@
  Naxis[0] = Naxis[1] = 0;
  SetNulVal(); SetDebug(0);
- FitsOF = NULL; FitsPtr = NULL;
+ FitsOF = NULL;
 }
 
@@ -993,5 +992,5 @@
  Naxis[0] = Naxis[1] = 0;
  SetNulVal(); SetDebug(lp);
- FitsOF = NULL; FitsPtr = NULL;
+ FitsOF = NULL;
 
  // Caracteristiques du FitsOpenFile
@@ -1000,6 +999,5 @@
    throw NullPtrError("FitsImg2DRd::Init: FitsOpenFile pointer is NULL\n");
 
- FitsPtr = FitsOF->GetFitsPtr();
- if(FitsPtr==NULL)
+ if(GetFitsPtr()==NULL)
    throw NullPtrError("FitsImg2DRd::Init: FitsPtr pointer is NULL\n");
 
@@ -1015,9 +1013,9 @@
      int rc = FitsOF->MoveToHDU(ihdu);
      if(rc!=ihdu)
-       throw RangeCheckError("FitsABTColRd::Init: Error moving to requested HDU\n");
+       throw RangeCheckError("FitsImg2DRd::Init: Error moving to requested HDU\n");
    }
  } else { // Fits file has already been positionned
    if(ihdu>0 && ihdu!=HDU())
-     throw RangeCheckError("FitsABTColRd::Init: file already posit. at another HDU\n");
+     throw RangeCheckError("FitsImg2DRd::Init: file already posit. at another HDU\n");
  }
 
@@ -1029,5 +1027,5 @@
  // Get NAXIS 1 et 2
  int nfound=0;
- if(fits_read_keys_lng(FitsPtr,"NAXIS",1,2,Naxis,&nfound,&sta)) {
+ if(fits_read_keys_lng(GetFitsPtr(),"NAXIS",1,2,Naxis,&nfound,&sta)) {
    FitsOpenFile::printerror(sta);
    throw RangeCheckError("FitsImg2DRd::Init: Error reading NAXIS cards\n");
@@ -1056,5 +1054,5 @@
 double FitsImg2DRd::ReadKey(char *keyname)
 {
- return FitsOpenFile::ReadKey(FitsPtr,keyname);
+ return FitsOpenFile::ReadKey(GetFitsPtr(),keyname);
 }
 
@@ -1066,5 +1064,5 @@
 long FitsImg2DRd::ReadKeyL(char *keyname)
 {
- return FitsOpenFile::ReadKeyL(FitsPtr,keyname);
+ return FitsOpenFile::ReadKeyL(GetFitsPtr(),keyname);
 }
 
@@ -1076,5 +1074,5 @@
 string FitsImg2DRd::ReadKeyS(char *keyname)
 {
- return FitsOpenFile::ReadKeyS(FitsPtr,keyname);
+ return FitsOpenFile::ReadKeyS(GetFitsPtr(),keyname);
 }
 
@@ -1090,5 +1088,5 @@
 \warning TMatrix data(Naxis2,Naxis1)
 */
-long FitsImg2DRd::Read(TMatrix<uint_2>& data)
+size_t FitsImg2DRd::Read(TMatrix<uint_2>& data)
 {
  int sta=0;
@@ -1098,5 +1096,5 @@
  for(int j=0;j<Naxis[1];j++) {
    long deb = j*Naxis[0]+1, nel = Naxis[0];
-   fits_read_img(FitsPtr,TUSHORT,deb,nel,&NulVal,arr,NULL,&sta);
+   fits_read_img(GetFitsPtr(),TUSHORT,deb,nel,&NulVal,arr,NULL,&sta);
    if(sta) {
      FitsOpenFile::printerror(sta); delete [] arr;
@@ -1112,5 +1110,5 @@
 
 /*! Read image into a TMatrix<int_4> */
-long FitsImg2DRd::Read(TMatrix<int_4>& data)
+size_t FitsImg2DRd::Read(TMatrix<int_4>& data)
 {
  int sta=0;
@@ -1121,5 +1119,5 @@
  for(int j=0;j<Naxis[1];j++) {
    long deb = j*Naxis[0]+1, nel = Naxis[0];
-   fits_read_img(FitsPtr,T,deb,nel,&NulVal,arr,NULL,&sta);
+   fits_read_img(GetFitsPtr(),T,deb,nel,&NulVal,arr,NULL,&sta);
    if(sta) {
      FitsOpenFile::printerror(sta); delete [] arr;
@@ -1135,5 +1133,5 @@
 
 /*! Read image into a TMatrix<int_8> */
-long FitsImg2DRd::Read(TMatrix<int_8>& data)
+size_t FitsImg2DRd::Read(TMatrix<int_8>& data)
 {
  int sta=0;
@@ -1143,5 +1141,5 @@
  for(int j=0;j<Naxis[1];j++) {
    long deb = j*Naxis[0]+1, nel = Naxis[0];
-   fits_read_img(FitsPtr,TLONGLONG,deb,nel,&NulVal,arr,NULL,&sta);
+   fits_read_img(GetFitsPtr(),TLONGLONG,deb,nel,&NulVal,arr,NULL,&sta);
    if(sta) {
      FitsOpenFile::printerror(sta); delete [] arr;
@@ -1157,5 +1155,5 @@
 
 /*! Read image into a TMatrix<float> */
-long FitsImg2DRd::Read(TMatrix<float>& data)
+size_t FitsImg2DRd::Read(TMatrix<float>& data)
 {
  int sta=0;
@@ -1165,5 +1163,5 @@
  for(int j=0;j<Naxis[1];j++) {
    long deb = j*Naxis[0]+1, nel = Naxis[0];
-   fits_read_img(FitsPtr,TFLOAT,deb,nel,&NulVal,arr,NULL,&sta);
+   fits_read_img(GetFitsPtr(),TFLOAT,deb,nel,&NulVal,arr,NULL,&sta);
    if(sta) {
      FitsOpenFile::printerror(sta); delete [] arr;
@@ -1179,5 +1177,5 @@
 
 /*! Read image into a TMatrix<double> */
-long FitsImg2DRd::Read(TMatrix<double>& data)
+size_t FitsImg2DRd::Read(TMatrix<double>& data)
 {
  int sta=0;
@@ -1187,5 +1185,5 @@
  for(int j=0;j<Naxis[1];j++) {
    long deb = j*Naxis[0]+1, nel = Naxis[0];
-   fits_read_img(FitsPtr,TDOUBLE,deb,nel,&NulVal,arr,NULL,&sta);
+   fits_read_img(GetFitsPtr(),TDOUBLE,deb,nel,&NulVal,arr,NULL,&sta);
    if(sta) {
      FitsOpenFile::printerror(sta); delete [] arr;
@@ -1257,2 +1255,315 @@
  if(FitsOF!=NULL) delete FitsOF;
 }
+
+
+
+///////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////
+
+//! Class for reading a 3D image from a FITS file
+
+/*!
+  \class SOPHYA::FitsImg3DRd
+  \ingroup FitsIOServer
+  Class for reading a 3D image from a FITS file
+*/
+
+//////////////////////////////////////////////////////////////
+/*!
+  Constructor.
+  \param fof : Pointer to the Class for opening the FITS file
+  \param ihdu : number of the HDU where the 3D image is.
+  \param lp : debug level
+  \verbatim
+  - if ihdu<=0 first IMAGE hdu is taken
+  - if ihdu>nhdu ihdu is set to nhdu
+  \endverbatim
+  \warning ihdu = [1,nhdu]
+*/
+FitsImg3DRd::FitsImg3DRd(FitsOpenFile* fof,int ihdu,int lp)
+{
+ Init(fof,ihdu,lp);
+}
+
+/*! Constructor by copy */
+FitsImg3DRd::FitsImg3DRd(FitsImg3DRd& fbt)
+{
+ Init(fbt.GetFitsOpenFile(),fbt.HDU(),fbt.DbgLevel);
+}
+
+/*! Constructor by default */
+FitsImg3DRd::FitsImg3DRd()
+{
+ Naxis[0] = Naxis[1] = Naxis[2] = 0;
+ SetNulVal(); SetDebug(0);
+ FitsOF = NULL;
+}
+
+/*! Init routine called by the constructor */
+void FitsImg3DRd::Init(FitsOpenFile* fof,int ihdu,int lp)
+{
+ // Initialisation des Parametres Generaux
+ Naxis[0] = Naxis[1] = Naxis[2] = 0;
+ SetNulVal(); SetDebug(lp);
+ FitsOF = NULL;
+
+ // Caracteristiques du FitsOpenFile
+ FitsOF = fof;
+ if(FitsOF==NULL)
+   throw NullPtrError("FitsImg3DRd::Init: FitsOpenFile pointer is NULL\n");
+
+ if(GetFitsPtr()==NULL)
+   throw NullPtrError("FitsImg3DRd::Init: FitsPtr pointer is NULL\n");
+
+ int sta = 0;
+ if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU(); 
+
+ // Get HDU 3D image
+ // ATTENTION: ... cf blabla equivalent dans FitsABTColRd::Init()
+ if(FitsOF->GetPosStatus()==false) {
+   if(ihdu==0) { // find the first IMAGE_HDU
+     FitsOF->MoveToFirst(IMAGE_HDU);
+   } else {
+     int rc = FitsOF->MoveToHDU(ihdu);
+     if(rc!=ihdu)
+       throw RangeCheckError("FitsImg3DRd::Init: Error moving to requested HDU\n");
+   }
+ } else { // Fits file has already been positionned
+   if(ihdu>0 && ihdu!=HDU())
+     throw RangeCheckError("FitsImg3DRd::Init: file already posit. at another HDU\n");
+ }
+
+ // Check HDUType and set position status to TRUE
+ if(HDUType()!=IMAGE_HDU)
+   throw TypeMismatchExc("FitsImg3DRd::Init: HDU not IMAGE_HDU\n");
+ FitsOF->SetPosStatus(true);
+
+ // Get NAXIS 1, 2 et 3
+ int nfound=0;
+ if(fits_read_keys_lng(GetFitsPtr(),"NAXIS",1,3,Naxis,&nfound,&sta)) {
+   FitsOpenFile::printerror(sta);
+   throw RangeCheckError("FitsImg3DRd::Init: Error reading NAXIS cards\n");
+ }
+ if(DbgLevel>1)
+   cout<<"...Init(hdu="<<HDU()<<")  NAXIS1="<<Naxis[0]<<" NAXIS2="
+       <<Naxis[1]<<" NAXIS3="<<Naxis[2]<<" (nfound="<<nfound<<")"<<endl;
+ if(nfound!=3 || Naxis[0]<=0 || Naxis[1]<=0 || Naxis[2]<=0)
+   throw NotAvailableOperation("FitsImg3DRd::Init: bad Naxis[0-2] value\n");
+
+}
+
+/*! Destructor. */
+FitsImg3DRd::~FitsImg3DRd()
+{
+ //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
+ Naxis[0] = Naxis[1] = Naxis[2] = 0;
+}
+
+//////////////////////////////////////////////////////////////
+/*!
+  Read a fitsheader key into double
+  \param keyname : name of the key
+  \return value into double
+*/
+double FitsImg3DRd::ReadKey(char *keyname)
+{
+ return FitsOpenFile::ReadKey(GetFitsPtr(),keyname);
+}
+
+/*!
+  Read a fitsheader key into long
+  \param keyname : name of the key
+  \return value into long
+*/
+long FitsImg3DRd::ReadKeyL(char *keyname)
+{
+ return FitsOpenFile::ReadKeyL(GetFitsPtr(),keyname);
+}
+
+/*!
+  Read a fitsheader key into string
+  \param keyname : name of the key
+  \return value into string
+*/
+string FitsImg3DRd::ReadKeyS(char *keyname)
+{
+ return FitsOpenFile::ReadKeyS(GetFitsPtr(),keyname);
+}
+
+//////////////////////////////////////////////////////////////
+/* REMARQUE:
+ * Dans TArray A(naxis1,naxis2,naxis3);
+ *      A(i,j,k) -> i varie le plus vite et k le moins vite
+ */
+/*!
+Read 3D image into a TArray<uint_2>
+*/
+size_t FitsImg3DRd::Read(TArray<uint_2>& data)
+{
+ int sta=0;
+ uint_2* arr = new uint_2[Naxis[0]];
+ sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
+
+ for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
+   long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
+   fits_read_img(GetFitsPtr(),TUSHORT,deb,nel,&NulVal,arr,NULL,&sta);
+   if(sta) {
+     FitsOpenFile::printerror(sta); delete [] arr;
+     throw
+       NotAvailableOperation("FitsImg3DRd::Read(TArray<uint_2>): Error Reading Fits file\n");
+   }
+   for(int i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
+ }
+
+ delete [] arr;
+ return Naxis[0]*Naxis[1]*Naxis[2];
+ }
+
+/*! Read 3D image into a TArray<int_4> */
+size_t FitsImg3DRd::Read(TArray<int_4>& data)
+{
+ int sta=0;
+ int_4* arr = new int_4[Naxis[0]];
+ sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
+ int T = (sizeof(long)==4) ? TLONG: TINT;
+
+ for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
+   long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
+   fits_read_img(GetFitsPtr(),T,deb,nel,&NulVal,arr,NULL,&sta);
+   if(sta) {
+     FitsOpenFile::printerror(sta); delete [] arr;
+     throw
+       NotAvailableOperation("FitsImg3DRd::Read(TArray<int_4>): Error Reading Fits file\n");
+   }
+   for(int i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
+ }
+
+ delete [] arr;
+ return Naxis[0]*Naxis[1]*Naxis[2];
+ }
+
+/*! Read 3D image into a TArray<int_8> */
+size_t FitsImg3DRd::Read(TArray<int_8>& data)
+{
+ int sta=0;
+ int_8* arr = new int_8[Naxis[0]];
+ sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
+
+ for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
+   long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
+   fits_read_img(GetFitsPtr(),TLONGLONG,deb,nel,&NulVal,arr,NULL,&sta);
+   if(sta) {
+     FitsOpenFile::printerror(sta); delete [] arr;
+     throw
+       NotAvailableOperation("FitsImg3DRd::Read(TArray<int_8>): Error Reading Fits file\n");
+   }
+   for(int i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
+ }
+
+ delete [] arr;
+ return Naxis[0]*Naxis[1]*Naxis[2];
+ }
+
+/*! Read 3D image into a TArray<float> */
+size_t FitsImg3DRd::Read(TArray<float>& data)
+{
+ int sta=0;
+ float* arr = new float[Naxis[0]];
+ sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
+
+ for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
+   long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
+   fits_read_img(GetFitsPtr(),TFLOAT,deb,nel,&NulVal,arr,NULL,&sta);
+   if(sta) {
+     FitsOpenFile::printerror(sta); delete [] arr;
+     throw
+       NotAvailableOperation("FitsImg3DRd::Read(TArray<float>): Error Reading Fits file\n");
+   }
+   for(int i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
+ }
+
+ delete [] arr;
+ return Naxis[0]*Naxis[1]*Naxis[2];
+ }
+
+/*! Read 3D image into a TArray<double> */
+size_t FitsImg3DRd::Read(TArray<double>& data)
+{
+ int sta=0;
+ double* arr = new double[Naxis[0]];
+ sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
+
+ for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
+   long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
+   fits_read_img(GetFitsPtr(),TDOUBLE,deb,nel,&NulVal,arr,NULL,&sta);
+   if(sta) {
+     FitsOpenFile::printerror(sta); delete [] arr;
+     throw
+       NotAvailableOperation("FitsImg3DRd::Read(TArray<double>): Error Reading Fits file\n");
+   }
+   for(int i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
+ }
+
+ delete [] arr;
+ return Naxis[0]*Naxis[1]*Naxis[2];
+ }
+
+///////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////
+
+//! Class for reading a 3D image from a FITS file
+
+/*!
+  \class SOPHYA::FitsImg3DRead
+  \ingroup FitsIOServer
+  Class for reading a 3D image from a FITS file
+*/
+
+//////////////////////////////////////////////////////////////
+/*!
+  Constructor.
+  \param fname : name of the FITS file
+  \param ihdu : number of the HDU where the 3D image is.
+  \param lp : debug level
+  \verbatim
+  - if ihdu<=0 first IMAGE hdu is taken
+  - if ihdu>nhdu ihdu is set to nhdu
+  \endverbatim
+  \warning ihdu = [1,nhdu]
+*/
+FitsImg3DRead::FitsImg3DRead(string fname,int ihdu,int lp)
+: FitsImg3DRd(new FitsOpenFile(fname),ihdu,lp)
+{
+}
+
+/*! Constructor. see below */
+FitsImg3DRead::FitsImg3DRead(const char * cfname,int ihdu,int lp)
+: FitsImg3DRd(new FitsOpenFile(cfname),ihdu,lp)
+{
+}
+
+/*! Constructor by default */
+FitsImg3DRead::FitsImg3DRead()
+: FitsImg3DRd()
+{
+}
+
+/*! Constructor by copy */
+FitsImg3DRead::FitsImg3DRead(FitsImg3DRead& fimg)
+{
+ // --- ATTENTION ---
+ // FitsImg3DRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile
+ FitsOpenFile* fof = new FitsOpenFile(*fimg.GetFitsOpenFile());
+ Init(fof,fimg.HDU(),fimg.DbgLevel);
+}
+
+/*! Destructor. */
+FitsImg3DRead::~FitsImg3DRead()
+{
+ // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS
+ if(FitsOF!=NULL) delete FitsOF;
+}
Index: trunk/SophyaExt/FitsIOServer/fabtcolread.h
===================================================================
--- trunk/SophyaExt/FitsIOServer/fabtcolread.h	(revision 3111)
+++ trunk/SophyaExt/FitsIOServer/fabtcolread.h	(revision 3114)
@@ -104,5 +104,5 @@
   inline FitsOpenFile* GetFitsOpenFile(void) const {return FitsOF;}
   //! Get the FITS file pointer (cfistio pointer)
-  inline fitsfile* GetFitsPtr(void) const {return FitsPtr;}
+  inline fitsfile* GetFitsPtr(void) const {return FitsOF->GetFitsPtr();}
   //! Get the number of HDU in the FITS file
   inline int NHDU(void) const
@@ -156,5 +156,4 @@
   unsigned long NFitsRead;
   FitsOpenFile* FitsOF;
-  fitsfile* FitsPtr; // Redite avec FitsOF->FitsPtr, mais utilise partout!
   long LineDeb, LineFin;
   double *Buffer;
@@ -193,9 +192,9 @@
   string ReadKeyS(char *keyname);
 
-  long Read(TMatrix<uint_2>& data);
-  long Read(TMatrix<int_4>& data);
-  long Read(TMatrix<int_8>& data);
-  long Read(TMatrix<float>& data);
-  long Read(TMatrix<double>& data);
+  size_t Read(TMatrix<uint_2>& data);
+  size_t Read(TMatrix<int_4>& data);
+  size_t Read(TMatrix<int_8>& data);
+  size_t Read(TMatrix<float>& data);
+  size_t Read(TMatrix<double>& data);
 
   //! Set debug level
@@ -206,5 +205,5 @@
   inline FitsOpenFile* GetFitsOpenFile(void) const {return FitsOF;}
   //! Get the FITS file pointer (cfistio pointer)
-  inline fitsfile* GetFitsPtr(void) const {return FitsPtr;}
+  inline fitsfile* GetFitsPtr(void) const {return FitsOF->GetFitsPtr();}
   //! Get the number of HDU in the FITS file
   inline int NHDU(void) const
@@ -229,5 +228,4 @@
 
   FitsOpenFile* FitsOF;
-  fitsfile* FitsPtr; // Redite avec FitsOF->FitsPtr, mais utilise partout!
 };
 
@@ -243,4 +241,69 @@
 };
 
+
+///////////////////////////////////////////////////////////////////
+//! Class for reading a 3D image from a FITS file
+class FitsImg3DRd : public AnyDataObj {
+public:
+  FitsImg3DRd(FitsOpenFile* fof,int ihdu=0,int lp=0);
+  FitsImg3DRd(FitsImg3DRd& fbt);
+  FitsImg3DRd();
+  virtual ~FitsImg3DRd();
+
+  double ReadKey(char *keyname);
+  long   ReadKeyL(char *keyname);
+  string ReadKeyS(char *keyname);
+
+  size_t Read(TArray<uint_2>& data);
+  size_t Read(TArray<int_4>& data);
+  size_t Read(TArray<int_8>& data);
+  size_t Read(TArray<float>& data);
+  size_t Read(TArray<double>& data);
+
+  //! Set debug level
+  inline void    SetDebug(int lp=0) {DbgLevel = (unsigned short) lp;}
+  //! Set null value to be return when reading null data (0=return the data)
+  inline void    SetNulVal(double nulval=0.) {NulVal = nulval;}
+  //! Get the pointer to FitsOpenFile
+  inline FitsOpenFile* GetFitsOpenFile(void) const {return FitsOF;}
+  //! Get the FITS file pointer (cfistio pointer)
+  inline fitsfile* GetFitsPtr(void) const {return FitsOF->GetFitsPtr();}
+  //! Get the number of HDU in the FITS file
+  inline int NHDU(void) const
+         {if(FitsOF) return FitsOF->NHDU(); else return 0;}
+  //! Get the number of the HDU read
+  inline int HDU(void) const
+         {if(FitsOF) return FitsOF->HDU(); else return 0;}
+  //! Get the HDU type
+  inline int HDUType(void) const
+         {if(FitsOF) return FitsOF->HDUType(); else return 0;}
+  //! Get NAXIS1
+  inline long Naxis1(void) const {return Naxis[0];}
+  //! Get NAXIS2
+  inline long Naxis2(void) const {return Naxis[1];}
+  //! Get NAXIS3
+  inline long Naxis3(void) const {return Naxis[2];}
+
+protected:
+  void Init(FitsOpenFile* fof,int ihdu,int lp);
+
+  long Naxis[3];
+  double NulVal;
+  unsigned short DbgLevel;
+
+  FitsOpenFile* FitsOF;
+};
+
+///////////////////////////////////////////////////////////////////
+//! Class for reading a 3D image from a FITS file
+class FitsImg3DRead : public FitsImg3DRd {
+public:
+  FitsImg3DRead(string fname,int ihdu=0,int lp=0);
+  FitsImg3DRead(const char *cfname,int ihdu=0,int lp=0);
+  FitsImg3DRead(FitsImg3DRead& fbt);
+  FitsImg3DRead();
+  virtual ~FitsImg3DRead();
+};
+
 } // namespace SOPHYA
 #endif    /* FABTCOLREAD_H_SEEN */
Index: trunk/SophyaExt/FitsIOServer/fabtwriter.cc
===================================================================
--- trunk/SophyaExt/FitsIOServer/fabtwriter.cc	(revision 3111)
+++ trunk/SophyaExt/FitsIOServer/fabtwriter.cc	(revision 3114)
@@ -884,2 +884,200 @@
   <<"  Naxis1 "<<Naxis[0]<<"  Naxis2 "<<Naxis[1]<<endl;
 }
+
+
+//////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////
+/*!
+  \class SOPHYA::FitsImg3DWriter
+  \ingroup FitsIOServer
+  Class for writing an 3D image into FITS file
+*/
+
+/*!
+  Constructor.
+  \param fname : FITS file name to be written (a new fits file is created)
+  \param bitpix : 3D image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
+  \param lp : debug level
+*/
+FitsImg3DWriter::FitsImg3DWriter(string fname,int bitpix,int lp)
+: FitsWriter(fname,lp)
+{
+ BitPix = bitpix;
+ HduType = IMAGE_HDU;
+ FirstTime = true;
+ Naxis[0] = Naxis[1] = Naxis[2] = 0;
+}
+
+/*!
+  Constructor.
+  \param cfname : FITS file name to be written (a new fits file is created)
+  \param bitpix : 3D image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
+  \param lp : debug level
+*/
+FitsImg3DWriter::FitsImg3DWriter(const char* cfname,int bitpix,int lp)
+: FitsWriter(cfname,lp)
+{
+ BitPix = bitpix;
+ HduType = IMAGE_HDU;
+ FirstTime = true;
+ Naxis[0] = Naxis[1] = Naxis[2] = 0;
+}
+
+/*!
+  Constructor.
+  \param fname : FITS file name to be written (created or updated)
+  \param update : file is created if FALSE, open for update if TRUE
+  \param bitpix : 3D image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
+  \param lp : debug level
+*/
+FitsImg3DWriter::FitsImg3DWriter(string fname,bool update,int bitpix,int lp)
+: FitsWriter(fname,update,lp)
+{
+ BitPix = bitpix;
+ HduType = IMAGE_HDU;
+ FirstTime = true;
+ Naxis[0] = Naxis[1] = Naxis[2] = 0;
+}
+
+/*!
+  Constructor.
+  \param cfname : FITS file name to be written (created or updated)
+  \param update : file is created if FALSE, open for update if TRUE
+  \param bitpix : 3D image type (BYTE_IMG,USHORT_IMG,SHORT_IMG,LONG_IMG,FLOAT_IMG,DOUBLE_IMG)
+  \param lp : debug level
+*/
+FitsImg3DWriter::FitsImg3DWriter(const char* cfname,bool update,int bitpix,int lp)
+: FitsWriter(cfname,update,lp)
+{
+ BitPix = bitpix;
+ HduType = IMAGE_HDU;
+ FirstTime = true;
+ Naxis[0] = Naxis[1] = Naxis[2] = 0;
+}
+
+/*! Destructor */
+FitsImg3DWriter::~FitsImg3DWriter()
+{
+}
+
+/*! Create the 3D image. Done at the first write request. */
+void FitsImg3DWriter::createimg(void)
+{
+ if(!FirstTime)
+   throw NotAvailableOperation("FitsImg3DWriter::createimg: already created 3D image\n");
+ if(Naxis[0]<=0 || Naxis[1]<=0 || Naxis[2]<=0)
+   throw ParmError("FitsImg3DWriter::createimg: bad Naxis 1 or 2 or 3 value\n");
+
+ int sta=0;
+ if(fits_create_img(FitsPtr,BitPix,3,Naxis,&sta)) {
+   printerror(sta);
+   throw NullPtrError("FitsImg3DWriter::createimg: Error creating 3D image extension\n");
+ }
+
+ FirstTime = false;
+}
+
+/*!
+  Write an unsigned short 3D image to FITS file.
+*/
+void FitsImg3DWriter::Write(TArray<uint_2>& data)
+{
+ if(data.Rank()!=3)
+   throw ParmError("FitsImg3DRd::Write(TArray<uint_2>): not a 3D array\n");
+
+ Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
+ uint_2* arr = new uint_2[Naxis[0]];
+
+ for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
+   for(int i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
+   long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
+   fits_write_img(FitsPtr,TUSHORT,deb,nel,arr,&sta);
+   if(sta) {
+     printerrorwrite("uint_2",j,k,sta); delete [] arr;
+     throw
+       NotAvailableOperation("FitsImg3DRd::Write(TArray<uint_2>): Error Writing Fits file\n");
+   }
+ }
+
+ delete [] arr;
+}
+
+/*! Write a long 3D image to FITS file. */
+void FitsImg3DWriter::Write(TArray<int_4>& data)
+{
+ if(data.Rank()!=3)
+   throw ParmError("FitsImg3DRd::Write(TArray<int_4>): not a 3D array\n");
+
+ int T = (sizeof(long)==4) ? TLONG: TINT;
+ Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
+ int_4* arr = new int_4[Naxis[0]];
+
+ for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
+   for(int i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
+   long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
+   fits_write_img(FitsPtr,T,deb,nel,arr,&sta);
+   if(sta) {
+     printerrorwrite("int_4",j,k,sta); delete [] arr;
+     throw
+       NotAvailableOperation("FitsImg3DRd::Write(TArray<int_4>): Error Writing Fits file\n");
+   }
+ }
+
+ delete [] arr;
+}
+
+/*! Write a float 3D image to FITS file. */
+void FitsImg3DWriter::Write(TArray<float>& data)
+{
+ if(data.Rank()!=3)
+   throw ParmError("FitsImg3DRd::Write(TArray<float>): not a 3D array\n");
+
+ Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
+ float* arr = new float[Naxis[0]];
+
+ for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
+   for(int i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
+   long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
+   fits_write_img(FitsPtr,TFLOAT,deb,nel,arr,&sta);
+   if(sta) {
+     printerrorwrite("float",j,k,sta); delete [] arr;
+     throw
+       NotAvailableOperation("FitsImg3DRd::Write(TArray<float>): Error Writing Fits file\n");
+   }
+ }
+
+ delete [] arr;
+}
+
+/*! Write a double 3D image to FITS file. */
+void FitsImg3DWriter::Write(TArray<double>& data)
+{
+ if(data.Rank()!=3)
+   throw ParmError("FitsImg3DRd::Write(TArray<double>): not a 3D array\n");
+
+ Naxis[0]=data.SizeX(); Naxis[1]=data.SizeY(); Naxis[2]=data.SizeZ(); createimg();
+ double* arr = new double[Naxis[0]];
+
+ for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
+   for(int i=0;i<Naxis[0];i++) arr[i] = data(i,j,k);
+   long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0]; int sta=0;
+   fits_write_img(FitsPtr,TDOUBLE,deb,nel,arr,&sta);
+   if(sta) {
+     printerrorwrite("double",j,k,sta); delete [] arr;
+     throw
+       NotAvailableOperation("FitsImg3DRd::Write(TArray<double>): Error Writing Fits file\n");
+   }
+ }
+
+ delete [] arr;
+}
+
+/*! Print infos. */
+void FitsImg3DWriter::Print(ostream& os) const
+{
+os<<"FitsImg3DWriter::Print: FitsFN "<<FitsFN<<endl
+  <<"  HduType "<<HduType<<"  NOverFlow "<<NOverFlow<<"  BitPix "<<BitPix
+  <<"  Naxis1 "<<Naxis[0]<<"  Naxis2 "<<Naxis[1]<<"  Naxis3 "<<Naxis[2]<<endl;
+}
Index: trunk/SophyaExt/FitsIOServer/fabtwriter.h
===================================================================
--- trunk/SophyaExt/FitsIOServer/fabtwriter.h	(revision 3111)
+++ trunk/SophyaExt/FitsIOServer/fabtwriter.h	(revision 3114)
@@ -157,4 +157,33 @@
 };
 
+///////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////
+//! Class for writing a FITS Image
+class FitsImg3DWriter : public FitsWriter {
+public:
+  FitsImg3DWriter(string fname,int bitpix=FLOAT_IMG,int lp=0);
+  FitsImg3DWriter(const char* cfname,int bitpix=FLOAT_IMG,int lp=0);
+  FitsImg3DWriter(string fname,bool update,int bitpix=FLOAT_IMG,int lp=0);
+  FitsImg3DWriter(const char* cfname,bool update,int bitpix=FLOAT_IMG,int lp=0);
+  virtual ~FitsImg3DWriter();
+
+  void Write(TArray<uint_2>& data);
+  void Write(TArray<int_4>& data);
+  void Write(TArray<float>& data);
+  void Write(TArray<double>& data);
+
+  //! Print to os
+  virtual void   Print(ostream& os) const;
+  //! Print to stdout
+  inline  void   Print(void) const {Print(cout);}
+
+protected:
+  void createimg(void);
+
+  int BitPix;
+  long Naxis[3];
+  bool FirstTime;
+};
+
 } // namespace SOPHYA
 #endif    /* FABTWRITER_H_SEEN */
