Ignore:
Timestamp:
Sep 28, 2001, 7:22:39 PM (24 years ago)
Author:
cmv
Message:
  • documentation updatee
  • des lecteurs de TVector double,float,int_4 dans FitsABTColRead
  • des methodes nouvelles dans le FitsABTColRead

cmv 28/9/01

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaExt/FitsIOServer/fabtcolread.cc

    r1657 r1659  
    1212  Class for reading a column in a FITS ASCII or BINARY table
    1313  \verbatim
    14   Exemple:
     14  -- Exemple:
    1515  FitsABTColRead fbt("myfits.fits","BoloMuv_28",0,1000,1,3);
    1616  fbt.SetDebug(3);
    1717  fbt.Print(3);
     18  // Read element by element
    1819  for(long i=0;i<fbt.GetNbLine();i++) {
    1920    double x = fbt.Read(i);
    2021    if(i%lpmod==0) cout<<i<<": "<<x<<endl;
    2122  }
     23  // Read into a vector
     24  TVector<double> data;
     25  long n = fbt.Read(32,50,data);
     26    cout<<"Number of values read: "<<n<<endl;
     27  data.ReSize(100);
     28  n = fbt.Read(10,-1,data);
     29    cout<<"Number of values read: "<<n<<endl;
    2230  \endverbatim
    2331*/
     
    2634/*!
    2735  Constructor.
     36  \param fname : FITS file name to be read
     37  \param collabel : label of the column to be read
     38  \param ihdu : number of the HDU where the column is.
     39  \param blen : read buffer length
     40  \param bsens : buffer reading direction
     41  \param lp : debug level
    2842  \verbatim
    29   fname : FITS file name to be read
    30   collabel : label of the column to be read
    31   ihdu : number of the HDU where the column is.
    32      if ihdu=0 or ihdu>nhdu first binary or ASCII table is taken
    33      Warning: ihdu = [1,nhdu]
    34   blen : read buffer length
    35   bsens : read buffer reading direction
    36           (bsens>0 forward, bsens<0 backward, bsens==0 centered)
    37   lp : debug level
     43  - if ihdu=0 or ihdu>nhdu first binary or ASCII table is taken
     44  - bsens>0    read forward
     45    bsens<0    read backward
     46    bsens==0   read centered
    3847  \endverbatim
     48  \warning ihdu = [1,nhdu]
    3949*/
    4050FitsABTColRead::FitsABTColRead(string fname,string collabel
     
    4656/*!
    4757  Constructor.
    48   \verbatim
    49   Same as before but the column is identified by its number
    50   colnum : number of the column to be read
    51   WARNING: col = [0,ncol[
    52   \endverbatim
     58  Same as before but the column is identified by its column number
     59  \param colnum : number of the column to be read
     60  \warning col = [0,ncol[
    5361*/
    5462FitsABTColRead::FitsABTColRead(string fname,int colnum
     
    237245
    238246//////////////////////////////////////////////////////////////
    239 /*! Change the buffer caracteristiques (see creator)  */
     247/*! Change the buffer caracteristiques (see creator) */
    240248void FitsABTColRead::ChangeBuffer(long blen,long bsens)
    241249{
     
    249257
    250258 // De-allocate if necessary
    251  if(Buffer!=NULL && oldnbuffer!=NBuffer)
    252    {delete [] Buffer; Buffer=NULL;}
     259 if(Buffer!=NULL) {
     260   // On des-alloue si pas assez de place
     261   // ou si l'ancienne place est beaucoup trop grande (>25%)
     262   if(oldnbuffer<NBuffer || (oldnbuffer>NBuffer+long(0.25*NBuffer)) )
     263     {delete [] Buffer; Buffer=NULL;}
     264 }
    253265
    254266 // Re-allocate
     
    271283/////////////////////////////////////////////////
    272284/*!
    273   Read row "n" and return the value cats into double
     285  Read row "n" and return the value into a double
     286  \warning be carefull for the range: row = [0,NRows[
     287  \return value in double
     288  \param n : number of the row to be read.
    274289  \verbatim
    275   WARNING: row = [0,NRows[
     290  usebuffer == true  : use read optimisation with bufferisation
     291            == false : no optimisation with bufferisation
     292                       just read one value
    276293  \endverbatim
    277294*/
    278 double FitsABTColRead::Read(long n)
     295double FitsABTColRead::Read(long n,bool usebuffer)
    279296// Attention: n=nline [0,NBline[, cfistio veut [1,NBline]
    280297// Attention: colnum  [0,NBcol[ , cfistio veut [1,NBcol]
    281298{
    282  int sta=0,anynul;
     299 int sta=0;
    283300 if(n<0 || n>=NBline)
    284301   throw RangeCheckError("FitsABTColRead::Read try to read outside line range\n");
    285302
    286303 // Pas de bufferisation, on lit betement
    287  if(NBuffer==1) {
     304 if(NBuffer==1 || !usebuffer) {
    288305   NFitsRead++;
    289    fits_read_col_dbl(FitsPtr,ColNum+1,n+1,1,1,NulVal,Buffer,&anynul,&sta);
     306   double val;
     307   fits_read_col(FitsPtr,TDOUBLE,ColNum+1,n+1,1,1,NULL,&val,NULL,&sta);
    290308   if(sta) {
    291309     printerror(sta);
    292310     throw NotAvailableOperation("FitsABTColRead::Read: Error Reading Fits file\n");
    293311   }
    294    return Buffer[0];
     312   // On ne remplit Buffer[0] que si on a choisit
     313   // un mode de lecture non bufferise (n==1) DES LE DEBUT.
     314   // Si on a initialement choisit un mode bufferise (avec n>1),
     315   // Buffer contient les valeurs chargees auparavent.
     316   // Il ne faut pas faire {Buffer[0]=val; LineDeb=LineFin=n;}
     317   // car on perd l'info de ces valeurs.
     318   if(NBuffer==1) {Buffer[0]=val; LineDeb=LineFin=n;}
     319   return val;
    295320 }
    296321
    297322 // Gestion avec bufferisation
    298  if(!Buffer) {
    299    cout<<"FitsABTNtuIntf::Read Buffer not allocated"<<endl;
    300    return 0;
    301  }
     323 if(!Buffer)
     324   throw RangeCheckError("FitsABTColRead::Read: Buffer not allocated\n");
    302325 if(n<LineDeb || n>LineFin) {
    303326   NFitsRead++;
     
    317340   //cout<<"DBG-FitsRead: row1="<<row1<<" row2="<<row2<<" nrow="<<nrow
    318341   //    <<" LineDeb,Fin="<<LineDeb<<","<<LineFin<<endl;
    319    fits_read_col_dbl(FitsPtr,ColNum+1,row1,1,nrow,NulVal,Buffer,&anynul,&sta);
     342   fits_read_col(FitsPtr,TDOUBLE,ColNum+1,row1,1,nrow,NULL,Buffer,NULL,&sta);
    320343   if(sta) {
    321344     printerror(sta);
     
    330353
    331354/*!
    332   Read rows from "n1" to "n2" and return the values cats in a TVector of double
     355  Read rows from "n1" to "n2" and return the values into TVector of double
     356  \return NREAD the number of values read (n2-n1+1).
     357  \warning row = [0,NRows[, the routine read [n1,n2]
    333358  \verbatim
    334   There are n2-n1+1 values returned
    335   WARNING: row = [0,NRows[
     359  - if n2<0 then read [n1,n2] where "n2=min(n1+vector_size-1,nrows-1)"
     360  - Last row read is ALWAYS: "n2 = n1 + NREAD -1"
     361  - The TVector is never resized if not necessary
     362  -------------------------------------------------------------------------
     363  - ex: suppose the column table contains 10 elements: nrows=10, rows=[0,9]
     364
     365    TVector<double> V(5);
     366      bt.Read(3,5,V)  -> read rows=3,4,5     -> V.Size()==5 -> return 3
     367      bt.Read(3,-1,V) -> read rows=3,4,5,6,7 -> V.Size()==5 -> return 5
     368      bt.Read(7,-1,V) -> read rows=7,8,9     -> V.Size()==5 -> return 3
     369      bt.Read(2,-1,V) -> read rows=2,3,4,5,6 -> V.Size()==5 -> return 5
     370      bt.Read(-1,5,V) -> throw exception
     371
     372    TVector<double> V(5);
     373      bt.Read(3,99,V) -> read rows=3,4,5,6,7,8,9 -> V.Size()==7 -> return 7
     374
     375    TVector<double> V(5);
     376      bt.Read(2,8,V)  -> read rows=2,3,4,5,6,7,8 -> V.Size()==7 -> return 7
     377
     378    TVector<double> V;
     379      bt.Read(3,5,V)  -> read rows=3,4,5 -> V.Size()==3 -> return 3
     380
     381    TVector<double> V;
     382      bt.Read(3,-1,V) -> throw exception
     383  -------------------------------------------------------------------------
    336384  \endverbatim
    337385*/
    338 void FitsABTColRead::Read(long n1,long n2,TVector<double>& data)
    339 {
    340  if(n1<0 || n1>=NBline || n2<0 || n2>=NBline || n1>n2)
    341    throw RangeCheckError("FitsABTColRead::Read TVector bad requested line range\n");
    342 
    343  sa_size_t n = n2-n1+1;
    344  if(data.Size()<n) data.SetSize(n);
    345  // Il faut faire mieux mais comment ????
    346  for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
     386long FitsABTColRead::Read(long n1,long n2,TVector<double>& data)
     387{
     388 if(n1<0 || n1>=NBline)
     389   throw RangeCheckError("FitsABTColRead::Read TVector bad requested 1srt line \n");
     390 if(data.Size()<=0 && n2<n1)
     391   throw RangeCheckError("FitsABTColRead::Read TVector bad requested 2sd line \n");
     392 if(n2<0) n2 = n1 + data.Size()-1;
     393 if(n2>=NBline) n2 = NBline-1;
     394
     395 sa_size_t nread = n2-n1+1;
     396 if(data.Size()<nread) data.SetSize(nread);
     397
     398 //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
     399 int sta=0;
     400 fits_read_col(FitsPtr,TDOUBLE,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
     401 if(sta) {
     402   printerror(sta);
     403   throw NotAvailableOperation("FitsABTColRead::Read_TVector<double>: Error Reading Fits file\n");
     404 }
     405
     406 return nread;
     407}
     408
     409/*! idem before but for TVector of float */
     410long FitsABTColRead::Read(long n1,long n2,TVector<float>& data)
     411{
     412 if(n1<0 || n1>=NBline)
     413   throw RangeCheckError("FitsABTColRead::Read TVector bad requested 1srt line \n");
     414 if(data.Size()<=0 && n2<n1)
     415   throw RangeCheckError("FitsABTColRead::Read TVector bad requested 2sd line \n");
     416 if(n2<0) n2 = n1 + data.Size()-1;
     417 if(n2>=NBline) n2 = NBline-1;
     418
     419 sa_size_t nread = n2-n1+1;
     420 if(data.Size()<nread) data.SetSize(nread);
     421
     422 //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
     423 int sta=0;
     424 fits_read_col(FitsPtr,TFLOAT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
     425 if(sta) {
     426   printerror(sta);
     427   throw NotAvailableOperation("FitsABTColRead::Read_TVector<float>: Error Reading Fits file\n");
     428 }
     429
     430 return nread;
     431}
     432
     433/*! idem before but for TVector of int_4 */
     434long FitsABTColRead::Read(long n1,long n2,TVector<int_4>& data)
     435{
     436 if(n1<0 || n1>=NBline)
     437   throw RangeCheckError("FitsABTColRead::Read TVector bad requested 1srt line \n");
     438 if(data.Size()<=0 && n2<n1)
     439   throw RangeCheckError("FitsABTColRead::Read TVector bad requested 2sd line \n");
     440 if(n2<0) n2 = n1 + data.Size()-1;
     441 if(n2>=NBline) n2 = NBline-1;
     442
     443 sa_size_t nread = n2-n1+1;
     444 if(data.Size()<nread) data.SetSize(nread);
     445
     446 //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
     447 int sta=0;
     448 int T = (sizeof(long)==4) ? TLONG: TINT;
     449 fits_read_col(FitsPtr,T,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
     450 if(sta) {
     451   printerror(sta);
     452   throw NotAvailableOperation("FitsABTColRead::Read_TVector<float>: Error Reading Fits file\n");
     453 }
     454
     455 return nread;
     456}
     457
     458/////////////////////////////////////////////////
     459/*!
     460  Return the number of the first row where "val1"<=val<="val2" starting at row "rowstart"
     461  \verbatim
     462  - The search is performed from "rowstart" to the end
     463      in ascending order (from "rowstart" to nrows).
     464  - Warning: "rowstart<0" means "rowstart==0" (search all the table column)
     465             That is the default
     466  \endverbatim
     467  \return <0 means not found
     468*/
     469long FitsABTColRead::FirstRow(double val1,double val2,long rowstart)
     470{
     471 long row = -1;
     472 if(NBline==0) return row;
     473 // Change buffer for efficiency
     474 long bsens=BuffSens; bool bchange=false;
     475 if(bsens<=0) {ChangeBuffer(BuffLen,1); bchange=true;}
     476 if(rowstart<0) rowstart = 0;
     477 if(rowstart>=NBline) rowstart = NBline-1;
     478 for(long i=rowstart;i<NBline;i++) {
     479   double val = Read(i);
     480   if(val<val1 || val>val2) continue;
     481   row = i;
     482   break;
     483 }
     484 if(bchange) ChangeBuffer(BuffLen,bsens);
     485 return row;
     486}
     487
     488/*!
     489  Return the number of the first row where val1<=val<=val2 starting at row rowstart
     490  \return <0 means not found
     491  \verbatim
     492  - The search is performed from "rowstart" to the beginning
     493      in descending order (from "rowstart" to 0).
     494  - Warning: "rowstart<0" means "rowstart==nrows-1" (search all the table column)
     495             That is the default
     496  \endverbatim
     497*/
     498long FitsABTColRead::LastRow(double val1,double val2,long rowstart)
     499{
     500 long row = -1;
     501 if(NBline==0) return row;
     502 // Change buffer for efficiency
     503 long bsens=BuffSens; bool bchange=false;
     504 if(bsens>=0) {ChangeBuffer(BuffLen,-1); bchange=true;}
     505 if(rowstart<0 || rowstart>=NBline) rowstart = NBline-1;
     506 for(long i=rowstart;i>=0;i--) {
     507   double val = Read(i);
     508   if(val<val1 || val>val2) continue;
     509   row = i;
     510   break;
     511 }
     512 if(bchange) ChangeBuffer(BuffLen,bsens);
     513 return row;
    347514}
    348515
Note: See TracChangeset for help on using the changeset viewer.