Index: trunk/SophyaExt/FitsIOServer/fbtntintf.cc
===================================================================
--- trunk/SophyaExt/FitsIOServer/fbtntintf.cc	(revision 1505)
+++ trunk/SophyaExt/FitsIOServer/fbtntintf.cc	(revision 1507)
@@ -7,9 +7,10 @@
 
 //////////////////////////////////////////////////////////////
-FitsBTNtuIntf::FitsBTNtuIntf(string fname,int ihdu,uint_4 blen,uint_2 lp)
-{
- BuffLen = (blen==0) ? 100 : blen;
+FitsBTNtuIntf::FitsBTNtuIntf(string fname,int ihdu
+               ,int_4 blen,int_4 bsens,uint_2 lp)
+{
  DbgLevel = lp;
  SetNulVal();
+ SetBuffer(blen,bsens);
  FitsFN = fname;
  IHdu = ihdu;
@@ -17,9 +18,10 @@
 }
 
-FitsBTNtuIntf::FitsBTNtuIntf(char * cfname,int ihdu,uint_4 blen,uint_2 lp)
-{
- BuffLen = (blen==0) ? 100 : blen;
+FitsBTNtuIntf::FitsBTNtuIntf(char * cfname,int ihdu
+               ,int_4 blen,int_4 bsens,uint_2 lp)
+{
  DbgLevel = lp;
  SetNulVal();
+ SetBuffer(blen,bsens);
  FitsFN = cfname;
  IHdu = ihdu;
@@ -29,7 +31,7 @@
 FitsBTNtuIntf::FitsBTNtuIntf(FitsBTNtuIntf& fbtnt)
 {
- BuffLen = fbtnt.BuffLen;
  DbgLevel = fbtnt.DbgLevel;
- NulVal = fbtnt.NulVal;
+ SetNulVal(fbtnt.NulVal);
+ SetBuffer(fbtnt.BuffLen,fbtnt.BuffSens);
  FitsFN = fbtnt.FitsFN;
  IHdu = fbtnt.IHdu;
@@ -39,7 +41,7 @@
 FitsBTNtuIntf::FitsBTNtuIntf()
 {
- BuffLen = 100;
  DbgLevel = 0;
  SetNulVal();
+ SetBuffer();
  FitsFN = "";
  IHdu = -1;
@@ -52,5 +54,43 @@
 }
 
+
 //////////////////////////////////////////////////////////////
+void FitsBTNtuIntf::ChangeBuffer(int_4 blen,int_4 bsens)
+{
+SetBuffer(blen,bsens);
+DeAllocBuff();
+AllocBuff();
+}
+
+void FitsBTNtuIntf::AllocBuff(void)
+{
+if(Buffer==NULL || NBcol<=0) return;
+int_4 n = BuffLen; if(BuffSens==0) n++;
+for(int icol=0;icol<NBcol;icol++)
+  if(ColReadable[icol]) Buffer[icol] = new double[n];
+    else Buffer[icol] = NULL;
+}
+
+void FitsBTNtuIntf::DeAllocBuff(void)
+{
+if(Buffer==NULL || NBcol<=0) return;
+for(int icol=0;icol<NBcol;icol++)
+  if(Buffer[icol]!=NULL) 
+    {delete [] Buffer[icol]; Buffer[icol] = NULL;}
+LineDeb = LineFin = -1;
+}
+
+void FitsBTNtuIntf::Delete()
+{
+ DeAllocBuff();
+ delete [] Buffer; Buffer=NULL;
+ if(mRet!=NULL) delete [] mRet;
+ if(ColReadable!=NULL) delete [] ColReadable;
+
+ int sta = 0;
+ if(fits_close_file(FitsPtr,&sta)) printerror(sta);
+ FitsPtr = NULL;
+}
+
 void FitsBTNtuIntf::Init()
 {
@@ -134,23 +174,8 @@
          <<" typecode="<<typecode<<" (readable="
          <<ColReadable[icol]<<")"<<endl;
-   // Prepare buffer
-   if(ColReadable[icol]) Buffer[icol] = new double[BuffLen];
- }
-
-}
-
-void FitsBTNtuIntf::Delete()
-{
-  if(Buffer!=NULL) {
-   for(int i=0;i<NBcol;i++)
-     if(Buffer[i]!=NULL) delete [] Buffer[i];
-   delete [] Buffer;
-  }
- if(mRet!=NULL) delete [] mRet;
- if(ColReadable!=NULL) delete [] ColReadable;
-
- int sta = 0;
- if(fits_close_file(FitsPtr,&sta)) printerror(sta);
- FitsPtr = NULL;
+ }
+ // Prepare buffer
+ AllocBuff();
+
 }
 
@@ -185,10 +210,25 @@
 
  // Gestion avec bufferisation
+ if(!Buffer) {
+   cout<<"FitsBTNtuIntf::GetLineD Buffer not allocated"<<endl;
+   return NULL;
+ }
  if(n<LineDeb || n>LineFin) {
    NFitsRead++;
-   long row1 = n+1;
-   long row2 = row1+BuffLen-1; if(row2>NBline) row2 = NBline;
-   long nrow = row2 - row1 + 1;
-   LineDeb = n; LineFin = row2-1;
+   long row1,row2,nrow;
+   if(BuffSens>0) { // Cas remplissage forward
+     row1 = n+1;
+     row2 = row1+BuffLen-1; if(row2>NBline) row2 = NBline;
+   } else if(BuffSens<0) { // Cas remplissage forward
+     row2 = n+1;
+     row1 = row2-BuffLen+1; if(row1<1) row1 = 1;
+   } else { // Cas remplissage centre
+     row1 = n+1 - BuffLen/2; if(row1<1) row1 = 1;
+     row2 = n+1 + BuffLen/2; if(row2>NBline) row2 = NBline;
+   }
+   nrow = row2 - row1 + 1;
+   LineDeb = row1-1; LineFin = row2-1;
+   //cout<<"DBG-FitsRead: row1="<<row1<<" row2="<<row2<<" nrow="<<nrow
+   //    <<" LineDeb,Fin="<<LineDeb<<","<<LineFin<<endl;
    for(int icol=0;icol<NBcol;icol++) {
      if(!ColReadable[icol]) continue;
@@ -198,6 +238,7 @@
  }
 
+ int_4 ibuf = n-LineDeb;
  for(int icol=0;icol<NBcol;icol++)
-   if(ColReadable[icol]) mRet[icol] = (Buffer[icol])[n-LineDeb];
+   if(ColReadable[icol]) mRet[icol] = (Buffer[icol])[ibuf];
      else mRet[icol] = 0.;
 
