Index: /trunk/SophyaExt/FitsIOServer/fabtcolread.cc
===================================================================
--- /trunk/SophyaExt/FitsIOServer/fabtcolread.cc	(revision 1658)
+++ /trunk/SophyaExt/FitsIOServer/fabtcolread.cc	(revision 1659)
@@ -12,12 +12,20 @@
   Class for reading a column in a FITS ASCII or BINARY table
   \verbatim
-  Exemple:
+  -- Exemple:
   FitsABTColRead fbt("myfits.fits","BoloMuv_28",0,1000,1,3);
   fbt.SetDebug(3);
   fbt.Print(3);
+  // Read element by element
   for(long i=0;i<fbt.GetNbLine();i++) {
     double x = fbt.Read(i);
     if(i%lpmod==0) cout<<i<<": "<<x<<endl;
   }
+  // Read into a vector
+  TVector<double> data;
+  long n = fbt.Read(32,50,data);
+    cout<<"Number of values read: "<<n<<endl;
+  data.ReSize(100);
+  n = fbt.Read(10,-1,data);
+    cout<<"Number of values read: "<<n<<endl;
   \endverbatim
 */
@@ -26,15 +34,17 @@
 /*!
   Constructor.
+  \param fname : FITS file name to be read
+  \param collabel : label of the column to be read
+  \param ihdu : number of the HDU where the column is.
+  \param blen : read buffer length
+  \param bsens : buffer reading direction
+  \param lp : debug level
   \verbatim
-  fname : FITS file name to be read
-  collabel : label of the column to be read
-  ihdu : number of the HDU where the column is.
-     if ihdu=0 or ihdu>nhdu first binary or ASCII table is taken
-     Warning: ihdu = [1,nhdu]
-  blen : read buffer length
-  bsens : read buffer reading direction
-          (bsens>0 forward, bsens<0 backward, bsens==0 centered)
-  lp : debug level
+  - if ihdu=0 or ihdu>nhdu first binary or ASCII table is taken
+  - bsens>0    read forward
+    bsens<0    read backward
+    bsens==0   read centered
   \endverbatim
+  \warning ihdu = [1,nhdu]
 */
 FitsABTColRead::FitsABTColRead(string fname,string collabel
@@ -46,9 +56,7 @@
 /*!
   Constructor.
-  \verbatim
-  Same as before but the column is identified by its number
-  colnum : number of the column to be read
-  WARNING: col = [0,ncol[
-  \endverbatim
+  Same as before but the column is identified by its column number
+  \param colnum : number of the column to be read
+  \warning col = [0,ncol[
 */
 FitsABTColRead::FitsABTColRead(string fname,int colnum
@@ -237,5 +245,5 @@
 
 //////////////////////////////////////////////////////////////
-/*! Change the buffer caracteristiques (see creator)  */
+/*! Change the buffer caracteristiques (see creator) */
 void FitsABTColRead::ChangeBuffer(long blen,long bsens)
 {
@@ -249,6 +257,10 @@
 
  // De-allocate if necessary
- if(Buffer!=NULL && oldnbuffer!=NBuffer)
-   {delete [] Buffer; Buffer=NULL;}
+ if(Buffer!=NULL) {
+   // On des-alloue si pas assez de place
+   // ou si l'ancienne place est beaucoup trop grande (>25%)
+   if(oldnbuffer<NBuffer || (oldnbuffer>NBuffer+long(0.25*NBuffer)) )
+     {delete [] Buffer; Buffer=NULL;}
+ }
 
  // Re-allocate
@@ -271,33 +283,44 @@
 /////////////////////////////////////////////////
 /*!
-  Read row "n" and return the value cats into double
+  Read row "n" and return the value into a double
+  \warning be carefull for the range: row = [0,NRows[
+  \return value in double
+  \param n : number of the row to be read.
   \verbatim
-  WARNING: row = [0,NRows[
+  usebuffer == true  : use read optimisation with bufferisation
+            == false : no optimisation with bufferisation
+                       just read one value
   \endverbatim
 */
-double FitsABTColRead::Read(long n)
+double FitsABTColRead::Read(long n,bool usebuffer)
 // Attention: n=nline [0,NBline[, cfistio veut [1,NBline]
 // Attention: colnum  [0,NBcol[ , cfistio veut [1,NBcol]
 {
- int sta=0,anynul;
+ int sta=0;
  if(n<0 || n>=NBline)
    throw RangeCheckError("FitsABTColRead::Read try to read outside line range\n");
 
  // Pas de bufferisation, on lit betement
- if(NBuffer==1) {
+ if(NBuffer==1 || !usebuffer) {
    NFitsRead++;
-   fits_read_col_dbl(FitsPtr,ColNum+1,n+1,1,1,NulVal,Buffer,&anynul,&sta);
+   double val;
+   fits_read_col(FitsPtr,TDOUBLE,ColNum+1,n+1,1,1,NULL,&val,NULL,&sta);
    if(sta) {
      printerror(sta);
      throw NotAvailableOperation("FitsABTColRead::Read: Error Reading Fits file\n");
    }
-   return Buffer[0];
+   // On ne remplit Buffer[0] que si on a choisit
+   // un mode de lecture non bufferise (n==1) DES LE DEBUT.
+   // Si on a initialement choisit un mode bufferise (avec n>1),
+   // Buffer contient les valeurs chargees auparavent.
+   // Il ne faut pas faire {Buffer[0]=val; LineDeb=LineFin=n;}
+   // car on perd l'info de ces valeurs.
+   if(NBuffer==1) {Buffer[0]=val; LineDeb=LineFin=n;}
+   return val;
  }
 
  // Gestion avec bufferisation
- if(!Buffer) {
-   cout<<"FitsABTNtuIntf::Read Buffer not allocated"<<endl;
-   return 0;
- }
+ if(!Buffer)
+   throw RangeCheckError("FitsABTColRead::Read: Buffer not allocated\n");
  if(n<LineDeb || n>LineFin) {
    NFitsRead++;
@@ -317,5 +340,5 @@
    //cout<<"DBG-FitsRead: row1="<<row1<<" row2="<<row2<<" nrow="<<nrow
    //    <<" LineDeb,Fin="<<LineDeb<<","<<LineFin<<endl;
-   fits_read_col_dbl(FitsPtr,ColNum+1,row1,1,nrow,NulVal,Buffer,&anynul,&sta);
+   fits_read_col(FitsPtr,TDOUBLE,ColNum+1,row1,1,nrow,NULL,Buffer,NULL,&sta);
    if(sta) {
      printerror(sta);
@@ -330,19 +353,163 @@
 
 /*!
-  Read rows from "n1" to "n2" and return the values cats in a TVector of double
+  Read rows from "n1" to "n2" and return the values into TVector of double
+  \return NREAD the number of values read (n2-n1+1).
+  \warning row = [0,NRows[, the routine read [n1,n2]
   \verbatim
-  There are n2-n1+1 values returned
-  WARNING: row = [0,NRows[
+  - if n2<0 then read [n1,n2] where "n2=min(n1+vector_size-1,nrows-1)"
+  - Last row read is ALWAYS: "n2 = n1 + NREAD -1"
+  - The TVector is never resized if not necessary
+  -------------------------------------------------------------------------
+  - ex: suppose the column table contains 10 elements: nrows=10, rows=[0,9]
+
+    TVector<double> V(5);
+      bt.Read(3,5,V)  -> read rows=3,4,5     -> V.Size()==5 -> return 3
+      bt.Read(3,-1,V) -> read rows=3,4,5,6,7 -> V.Size()==5 -> return 5
+      bt.Read(7,-1,V) -> read rows=7,8,9     -> V.Size()==5 -> return 3
+      bt.Read(2,-1,V) -> read rows=2,3,4,5,6 -> V.Size()==5 -> return 5
+      bt.Read(-1,5,V) -> throw exception
+
+    TVector<double> V(5);
+      bt.Read(3,99,V) -> read rows=3,4,5,6,7,8,9 -> V.Size()==7 -> return 7
+
+    TVector<double> V(5);
+      bt.Read(2,8,V)  -> read rows=2,3,4,5,6,7,8 -> V.Size()==7 -> return 7
+
+    TVector<double> V;
+      bt.Read(3,5,V)  -> read rows=3,4,5 -> V.Size()==3 -> return 3
+
+    TVector<double> V;
+      bt.Read(3,-1,V) -> throw exception
+  -------------------------------------------------------------------------
   \endverbatim
 */
-void FitsABTColRead::Read(long n1,long n2,TVector<double>& data)
-{
- if(n1<0 || n1>=NBline || n2<0 || n2>=NBline || n1>n2)
-   throw RangeCheckError("FitsABTColRead::Read TVector bad requested line range\n");
-
- sa_size_t n = n2-n1+1;
- if(data.Size()<n) data.SetSize(n);
- // Il faut faire mieux mais comment ????
- for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
+long FitsABTColRead::Read(long n1,long n2,TVector<double>& data)
+{
+ if(n1<0 || n1>=NBline)
+   throw RangeCheckError("FitsABTColRead::Read TVector bad requested 1srt line \n");
+ if(data.Size()<=0 && n2<n1)
+   throw RangeCheckError("FitsABTColRead::Read TVector bad requested 2sd line \n");
+ if(n2<0) n2 = n1 + data.Size()-1;
+ if(n2>=NBline) n2 = NBline-1;
+
+ sa_size_t nread = n2-n1+1;
+ if(data.Size()<nread) data.SetSize(nread);
+
+ //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);
+ if(sta) {
+   printerror(sta);
+   throw NotAvailableOperation("FitsABTColRead::Read_TVector<double>: Error Reading Fits file\n");
+ }
+
+ return nread;
+}
+
+/*! idem before but for TVector of float */
+long FitsABTColRead::Read(long n1,long n2,TVector<float>& data)
+{
+ if(n1<0 || n1>=NBline)
+   throw RangeCheckError("FitsABTColRead::Read TVector bad requested 1srt line \n");
+ if(data.Size()<=0 && n2<n1)
+   throw RangeCheckError("FitsABTColRead::Read TVector bad requested 2sd line \n");
+ if(n2<0) n2 = n1 + data.Size()-1;
+ if(n2>=NBline) n2 = NBline-1;
+
+ sa_size_t nread = n2-n1+1;
+ if(data.Size()<nread) data.SetSize(nread);
+
+ //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);
+ if(sta) {
+   printerror(sta);
+   throw NotAvailableOperation("FitsABTColRead::Read_TVector<float>: Error Reading Fits file\n");
+ }
+
+ return nread;
+}
+
+/*! idem before but for TVector of int_4 */
+long FitsABTColRead::Read(long n1,long n2,TVector<int_4>& data)
+{
+ if(n1<0 || n1>=NBline)
+   throw RangeCheckError("FitsABTColRead::Read TVector bad requested 1srt line \n");
+ if(data.Size()<=0 && n2<n1)
+   throw RangeCheckError("FitsABTColRead::Read TVector bad requested 2sd line \n");
+ if(n2<0) n2 = n1 + data.Size()-1;
+ if(n2>=NBline) n2 = NBline-1;
+
+ sa_size_t nread = n2-n1+1;
+ if(data.Size()<nread) data.SetSize(nread);
+
+ //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
+ 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);
+ if(sta) {
+   printerror(sta);
+   throw NotAvailableOperation("FitsABTColRead::Read_TVector<float>: Error Reading Fits file\n");
+ }
+
+ return nread;
+}
+
+/////////////////////////////////////////////////
+/*!
+  Return the number of the first row where "val1"<=val<="val2" starting at row "rowstart"
+  \verbatim
+  - The search is performed from "rowstart" to the end
+      in ascending order (from "rowstart" to nrows).
+  - Warning: "rowstart<0" means "rowstart==0" (search all the table column)
+             That is the default
+  \endverbatim
+  \return <0 means not found
+*/
+long FitsABTColRead::FirstRow(double val1,double val2,long rowstart)
+{
+ long row = -1;
+ if(NBline==0) return row;
+ // Change buffer for efficiency
+ long bsens=BuffSens; bool bchange=false;
+ if(bsens<=0) {ChangeBuffer(BuffLen,1); bchange=true;}
+ if(rowstart<0) rowstart = 0;
+ if(rowstart>=NBline) rowstart = NBline-1;
+ for(long i=rowstart;i<NBline;i++) {
+   double val = Read(i);
+   if(val<val1 || val>val2) continue;
+   row = i;
+   break;
+ }
+ if(bchange) ChangeBuffer(BuffLen,bsens);
+ return row;
+}
+
+/*!
+  Return the number of the first row where val1<=val<=val2 starting at row rowstart
+  \return <0 means not found
+  \verbatim
+  - The search is performed from "rowstart" to the beginning
+      in descending order (from "rowstart" to 0).
+  - Warning: "rowstart<0" means "rowstart==nrows-1" (search all the table column)
+             That is the default
+  \endverbatim
+*/
+long FitsABTColRead::LastRow(double val1,double val2,long rowstart)
+{
+ long row = -1;
+ if(NBline==0) return row;
+ // Change buffer for efficiency
+ long bsens=BuffSens; bool bchange=false;
+ if(bsens>=0) {ChangeBuffer(BuffLen,-1); bchange=true;}
+ if(rowstart<0 || rowstart>=NBline) rowstart = NBline-1;
+ for(long i=rowstart;i>=0;i--) {
+   double val = Read(i);
+   if(val<val1 || val>val2) continue;
+   row = i;
+   break;
+ }
+ if(bchange) ChangeBuffer(BuffLen,bsens);
+ return row;
 }
 
Index: /trunk/SophyaExt/FitsIOServer/fabtcolread.h
===================================================================
--- /trunk/SophyaExt/FitsIOServer/fabtcolread.h	(revision 1658)
+++ /trunk/SophyaExt/FitsIOServer/fabtcolread.h	(revision 1659)
@@ -18,11 +18,11 @@
 public:
   FitsABTColRead(string fname,string collabel,int ihdu=0
-               ,long blen=100,long bsens=1,int lp=0);
+                ,long blen=100,long bsens=1,int lp=0);
   FitsABTColRead(string fname,int colnum,int ihdu=0
-               ,long blen=100,long bsens=1,int lp=0);
+                ,long blen=100,long bsens=1,int lp=0);
   FitsABTColRead(const char *cfname,const char *collabel,int ihdu=0
-               ,long blen=100,long bsens=1,int lp=0);
+                ,long blen=100,long bsens=1,int lp=0);
   FitsABTColRead(const char *cfname,int colnum,int ihdu=0
-               ,long blen=100,long bsens=1,int lp=0);
+                ,long blen=100,long bsens=1,int lp=0);
   FitsABTColRead(FitsABTColRead& fbt);
   virtual ~FitsABTColRead();
@@ -30,6 +30,18 @@
   void ChangeBuffer(long blen=100,long bsens=1);
 
-  double Read(long n);
-  void Read(long n1,long n2,TVector<double>& data);
+  double Read(long n,bool usebuffer=true);
+  long   Read(long n1,long n2,TVector<double>& data);
+  long   Read(long n1,long n2,TVector<float>& data);
+  long   Read(long n1,long n2,TVector<int_4>& data);
+
+  //! return the value of the first row
+  double ReadFirstRow(bool usebuffer=false)
+                     {return Read(0,usebuffer);}
+  //! return the value of the last row
+  double ReadLastRow(bool usebuffer=false)
+                    {return Read(NBline-1,usebuffer);}
+
+  long FirstRow(double val1,double val2,long rowstart=-1);
+  long LastRow(double val1,double val2,long rowstart=-1);
 
   //! Set debug level
Index: /trunk/SophyaExt/FitsIOServer/fabtwriter.cc
===================================================================
--- /trunk/SophyaExt/FitsIOServer/fabtwriter.cc	(revision 1658)
+++ /trunk/SophyaExt/FitsIOServer/fabtwriter.cc	(revision 1659)
@@ -10,7 +10,8 @@
   \ingroup FitsIOServer
   Class for writing a FITS ASCII or BINARY table
-  \verbatim\
-  Exemple:
-  FitsABTWriter fbtw("myfits.fits",BINARY_TBL,3);
+  \verbatim
+  //-----------------------------------------------------------
+  -- Exemple 1: Writing element by element
+  FitsABTWriter fbtw(fitswrit,BINARY_TBL);
   fbtw.SetExtName("MY_OWN_EXTENSION");
   fbtw.AddCol("vars",TSHORT,"","km");       // col=0
@@ -33,4 +34,20 @@
   cout<<"Number of Overflows when writing: "
       <<fbtw.GetNOverFlow()<<endl;
+
+  //-----------------------------------------------------------
+  -- Exemple 2: Writing into TVector
+  ...
+  TVector<double> datad(100);
+  TVector<float>  dataf(100);
+  TVector<int_4>  datal(100);
+  for(long i=0;i<9990;i+=100) {
+    long i2=i+100-1; if(i2>=9990) i2=9990-1;
+    for(long j=0;j<100;j++) datad(i) =  ...;
+    for(long j=0;j<100;j++) dataf(i) =  ...;
+    for(long j=0;j<100;j++) datal(i) =  ...;
+    fbtw.Write(1,i,datal);
+    fbtw.Write(2,i,dataf);
+    fbtw.Write(3,i,datad);
+  }
   \endverbatim
 */
@@ -39,9 +56,7 @@
 /*!
   Constructor.
-  \verbatim
-  fname : FITS file name to be written
-  hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
-  lp : debug level
-  \endverbatim
+  \param fname : FITS file name to be written
+  \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL)
+  \param lp : debug level
 */
 FitsABTWriter::FitsABTWriter(string fname,int hdutype,int lp)
@@ -103,11 +118,11 @@
 /*!
   Add a new column to the FITS table
-  \verbatim
-  label : column label
-  datatype : TSHORT TINT32BIT TFLOAT or TDOUBLE
-  tform : fits tform definition
-          (can be automatically set if BINARY_TBL and tform="")
-  tunit : fits tunit definition (optional)
-  \endverbatim
+  \param label : column label
+  \param datatype : TSHORT TINT32BIT TFLOAT or TDOUBLE
+  \param tform : fits tform definition
+         (can be automatically set if BINARY_TBL and tform="")
+  \param tunit : fits tunit definition (optional)
+  \return The number of the new added column in the table.
+  \warning col = [0,ncol-1]
 */
 int FitsABTWriter::addcol(const char* label,int datatype
@@ -197,11 +212,11 @@
 /*!
   Write a short data to FITS file.
-  \verbatim
-  col : column number [0,ncol[
-  row : row number    [0,nrow[
-  val : value to be written
-  WARNING: that routine write a SHORT value into column "col"
+  \param col : column number [0,ncol[
+  \param row : row number    [0,nrow[
+  \param val : value to be written
+  \warning that routine write a SHORT value into column "col"
            which could have been defined with an other type.
            Cast is performed by the cfitsio package.
+  \verbatim
   WARNING: suppose that the column has be defined to be TSHORT
            and suppose that you wanted to write a double value
@@ -254,11 +269,9 @@
 /*!
   Write a vector of long data to FITS file.
-  \verbatim
-  col : column number [0,ncol[
-  row : starting row number    [0,nrow[
-  val : vector to be written
-  Return: "N" = number of the next row to be written
-          that is "N-1" is the number of the last row written.
-  \endverbatim
+  \param col : column number [0,ncol[
+  \param row : starting row number    [0,nrow[
+  \param val : vector to be written
+  \return "N" = number of the next row to be written,
+      that is "N-1" is the number of the last row written.
 */
 /*! Write a vector of long data to FITS file (see below) */
