Changeset 1659 in Sophya for trunk/SophyaExt/FitsIOServer
- Timestamp:
- Sep 28, 2001, 7:22:39 PM (24 years ago)
- Location:
- trunk/SophyaExt/FitsIOServer
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaExt/FitsIOServer/fabtcolread.cc
r1657 r1659 12 12 Class for reading a column in a FITS ASCII or BINARY table 13 13 \verbatim 14 Exemple:14 -- Exemple: 15 15 FitsABTColRead fbt("myfits.fits","BoloMuv_28",0,1000,1,3); 16 16 fbt.SetDebug(3); 17 17 fbt.Print(3); 18 // Read element by element 18 19 for(long i=0;i<fbt.GetNbLine();i++) { 19 20 double x = fbt.Read(i); 20 21 if(i%lpmod==0) cout<<i<<": "<<x<<endl; 21 22 } 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; 22 30 \endverbatim 23 31 */ … … 26 34 /*! 27 35 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 28 42 \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 38 47 \endverbatim 48 \warning ihdu = [1,nhdu] 39 49 */ 40 50 FitsABTColRead::FitsABTColRead(string fname,string collabel … … 46 56 /*! 47 57 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[ 53 61 */ 54 62 FitsABTColRead::FitsABTColRead(string fname,int colnum … … 237 245 238 246 ////////////////////////////////////////////////////////////// 239 /*! Change the buffer caracteristiques (see creator) 247 /*! Change the buffer caracteristiques (see creator) */ 240 248 void FitsABTColRead::ChangeBuffer(long blen,long bsens) 241 249 { … … 249 257 250 258 // 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 } 253 265 254 266 // Re-allocate … … 271 283 ///////////////////////////////////////////////// 272 284 /*! 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. 274 289 \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 276 293 \endverbatim 277 294 */ 278 double FitsABTColRead::Read(long n )295 double FitsABTColRead::Read(long n,bool usebuffer) 279 296 // Attention: n=nline [0,NBline[, cfistio veut [1,NBline] 280 297 // Attention: colnum [0,NBcol[ , cfistio veut [1,NBcol] 281 298 { 282 int sta=0 ,anynul;299 int sta=0; 283 300 if(n<0 || n>=NBline) 284 301 throw RangeCheckError("FitsABTColRead::Read try to read outside line range\n"); 285 302 286 303 // Pas de bufferisation, on lit betement 287 if(NBuffer==1 ) {304 if(NBuffer==1 || !usebuffer) { 288 305 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); 290 308 if(sta) { 291 309 printerror(sta); 292 310 throw NotAvailableOperation("FitsABTColRead::Read: Error Reading Fits file\n"); 293 311 } 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; 295 320 } 296 321 297 322 // 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"); 302 325 if(n<LineDeb || n>LineFin) { 303 326 NFitsRead++; … … 317 340 //cout<<"DBG-FitsRead: row1="<<row1<<" row2="<<row2<<" nrow="<<nrow 318 341 // <<" 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); 320 343 if(sta) { 321 344 printerror(sta); … … 330 353 331 354 /*! 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] 333 358 \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 ------------------------------------------------------------------------- 336 384 \endverbatim 337 385 */ 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); 386 long 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 */ 410 long 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 */ 434 long 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 */ 469 long 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 */ 498 long 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; 347 514 } 348 515 -
trunk/SophyaExt/FitsIOServer/fabtcolread.h
r1657 r1659 18 18 public: 19 19 FitsABTColRead(string fname,string collabel,int ihdu=0 20 ,long blen=100,long bsens=1,int lp=0);20 ,long blen=100,long bsens=1,int lp=0); 21 21 FitsABTColRead(string fname,int colnum,int ihdu=0 22 ,long blen=100,long bsens=1,int lp=0);22 ,long blen=100,long bsens=1,int lp=0); 23 23 FitsABTColRead(const char *cfname,const char *collabel,int ihdu=0 24 ,long blen=100,long bsens=1,int lp=0);24 ,long blen=100,long bsens=1,int lp=0); 25 25 FitsABTColRead(const char *cfname,int colnum,int ihdu=0 26 ,long blen=100,long bsens=1,int lp=0);26 ,long blen=100,long bsens=1,int lp=0); 27 27 FitsABTColRead(FitsABTColRead& fbt); 28 28 virtual ~FitsABTColRead(); … … 30 30 void ChangeBuffer(long blen=100,long bsens=1); 31 31 32 double Read(long n); 33 void Read(long n1,long n2,TVector<double>& data); 32 double Read(long n,bool usebuffer=true); 33 long Read(long n1,long n2,TVector<double>& data); 34 long Read(long n1,long n2,TVector<float>& data); 35 long Read(long n1,long n2,TVector<int_4>& data); 36 37 //! return the value of the first row 38 double ReadFirstRow(bool usebuffer=false) 39 {return Read(0,usebuffer);} 40 //! return the value of the last row 41 double ReadLastRow(bool usebuffer=false) 42 {return Read(NBline-1,usebuffer);} 43 44 long FirstRow(double val1,double val2,long rowstart=-1); 45 long LastRow(double val1,double val2,long rowstart=-1); 34 46 35 47 //! Set debug level -
trunk/SophyaExt/FitsIOServer/fabtwriter.cc
r1658 r1659 10 10 \ingroup FitsIOServer 11 11 Class for writing a FITS ASCII or BINARY table 12 \verbatim\ 13 Exemple: 14 FitsABTWriter fbtw("myfits.fits",BINARY_TBL,3); 12 \verbatim 13 //----------------------------------------------------------- 14 -- Exemple 1: Writing element by element 15 FitsABTWriter fbtw(fitswrit,BINARY_TBL); 15 16 fbtw.SetExtName("MY_OWN_EXTENSION"); 16 17 fbtw.AddCol("vars",TSHORT,"","km"); // col=0 … … 33 34 cout<<"Number of Overflows when writing: " 34 35 <<fbtw.GetNOverFlow()<<endl; 36 37 //----------------------------------------------------------- 38 -- Exemple 2: Writing into TVector 39 ... 40 TVector<double> datad(100); 41 TVector<float> dataf(100); 42 TVector<int_4> datal(100); 43 for(long i=0;i<9990;i+=100) { 44 long i2=i+100-1; if(i2>=9990) i2=9990-1; 45 for(long j=0;j<100;j++) datad(i) = ...; 46 for(long j=0;j<100;j++) dataf(i) = ...; 47 for(long j=0;j<100;j++) datal(i) = ...; 48 fbtw.Write(1,i,datal); 49 fbtw.Write(2,i,dataf); 50 fbtw.Write(3,i,datad); 51 } 35 52 \endverbatim 36 53 */ … … 39 56 /*! 40 57 Constructor. 41 \verbatim 42 fname : FITS file name to be written 43 hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL) 44 lp : debug level 45 \endverbatim 58 \param fname : FITS file name to be written 59 \param hdutype : type of extension to be created (BINARY_TBL or ASCII_TBL) 60 \param lp : debug level 46 61 */ 47 62 FitsABTWriter::FitsABTWriter(string fname,int hdutype,int lp) … … 103 118 /*! 104 119 Add a new column to the FITS table 105 \ verbatim106 label : column label107 datatype : TSHORT TINT32BIT TFLOAT or TDOUBLE108 tform : fits tform definition109 (can be automatically set if BINARY_TBL and tform="")110 tunit : fits tunit definition (optional)111 \ endverbatim120 \param label : column label 121 \param datatype : TSHORT TINT32BIT TFLOAT or TDOUBLE 122 \param tform : fits tform definition 123 (can be automatically set if BINARY_TBL and tform="") 124 \param tunit : fits tunit definition (optional) 125 \return The number of the new added column in the table. 126 \warning col = [0,ncol-1] 112 127 */ 113 128 int FitsABTWriter::addcol(const char* label,int datatype … … 197 212 /*! 198 213 Write a short data to FITS file. 199 \verbatim 200 col : column number [0,ncol[ 201 row : row number [0,nrow[ 202 val : value to be written 203 WARNING: that routine write a SHORT value into column "col" 214 \param col : column number [0,ncol[ 215 \param row : row number [0,nrow[ 216 \param val : value to be written 217 \warning that routine write a SHORT value into column "col" 204 218 which could have been defined with an other type. 205 219 Cast is performed by the cfitsio package. 220 \verbatim 206 221 WARNING: suppose that the column has be defined to be TSHORT 207 222 and suppose that you wanted to write a double value … … 254 269 /*! 255 270 Write a vector of long data to FITS file. 256 \verbatim 257 col : column number [0,ncol[ 258 row : starting row number [0,nrow[ 259 val : vector to be written 260 Return: "N" = number of the next row to be written 261 that is "N-1" is the number of the last row written. 262 \endverbatim 271 \param col : column number [0,ncol[ 272 \param row : starting row number [0,nrow[ 273 \param val : vector to be written 274 \return "N" = number of the next row to be written, 275 that is "N-1" is the number of the last row written. 263 276 */ 264 277 /*! Write a vector of long data to FITS file (see below) */
Note:
See TracChangeset
for help on using the changeset viewer.