source: Sophya/trunk/SophyaExt/FitsIOServer/fabtcolread.cc@ 2451

Last change on this file since 2451 was 2451, checked in by cmv, 22 years ago

lecture d'une clef fits string cmv 29/10/2003

File size: 24.4 KB
RevLine 
[1654]1/* Lecteur de colonne de table Fits (binaire ou ASCII) avec buffer */
2#include "machdefs.h"
3#include <stdlib.h>
4#include <stdio.h>
5#include "pexceptions.h"
6#include "fabtcolread.h"
[2449]7
8///////////////////////////////////////////////////////////////////
9///////////////////////////////////////////////////////////////////
10///////////////////////////////////////////////////////////////////
11///////////////////////////////////////////////////////////////////
12
13//! Class for opening a FITS file for reading
14
15/*!
16 \class SOPHYA::FitsOpenFile
17 \ingroup FitsIOServer
18 Class for opening a FITS file for reading
19*/
20
21/*!
22 Constructor.
23 \param fname : FITS file name to be opened for reading
24*/
25FitsOpenFile::FitsOpenFile(string fname)
26{
27 Init(fname.c_str());
28}
29
30/*!
31 Constructor.
32 \param cfname : FITS file name to be opened for reading
33*/
34FitsOpenFile::FitsOpenFile(const char *cfname)
35{
36 Init(cfname);
37}
38
39/*!
40 Constructor by default.
41*/
42FitsOpenFile::FitsOpenFile()
43{
44 FitsFN = "";
45 NHdu = -1;
46 FitsPtr = NULL;
47}
48
49/*!
50 Constructor by copy.
51*/
52FitsOpenFile::FitsOpenFile(FitsOpenFile& fof)
53{
54 Init(fof.GetFileName().c_str());
55}
56
57/*!
58 Destructor.
59*/
60FitsOpenFile::~FitsOpenFile()
61{
62 Delete();
63 FitsFN = "";
64 NHdu = 0;
65}
66
67/*!
68 Delete routine called by the destructor.
69*/
70void FitsOpenFile::Delete(void)
71{
72 if(FitsPtr != NULL) {
73 int sta = 0;
74 if(fits_close_file(FitsPtr,&sta)) printerror(sta);
75 FitsPtr = NULL;
76 }
77}
78
79/*! Init routine called by the constructor */
80void FitsOpenFile::Init(const char* fname)
81{
82 // Parametres Generaux
83 FitsFN = fname;
84 NHdu = 0;
85 FitsPtr = NULL;
86
87 // Ouverture du fichier
88 if(FitsFN.size() <= 0 )
89 throw ParmError("FitsOpenFile::Init: Fits file name error\n");
90
91 int sta = 0;
92 if(fits_open_file(&FitsPtr,FitsFN.c_str(),READONLY,&sta)) {
93 printerror(sta);
94 FitsPtr = NULL;
95 throw NullPtrError("FitsOpenFile::Init: Error opening Fits file\n");
96 }
97
98 // Get number of hdu
99 if(fits_get_num_hdus(FitsPtr,&NHdu,&sta)) {
100 printerror(sta);
101 NHdu = 0;
102 Delete();
103 throw NotAvailableOperation("FitsOpenFile::Init: Error getting NHdu\n");
104 }
105 if(NHdu<=0) {
106 Delete();
107 throw SzMismatchError("FitsOpenFile::Init: Bad NHdu\n");
108 }
109
110 }
111
112 void FitsOpenFile::printerror(int sta) const
113 {
114 int stat = sta;
115 fits_report_error(stdout,stat);
116 fflush(stdout);
117 return;
118 }
119
120///////////////////////////////////////////////////////////////////
121///////////////////////////////////////////////////////////////////
122///////////////////////////////////////////////////////////////////
123///////////////////////////////////////////////////////////////////
124
125///////////////////////////////////////////////////////////////////
[1654]126//! Class for reading a column in a FITS ASCII or BINARY table
127
128/*!
[2449]129 \class SOPHYA::FitsABTColRd
[1654]130 \ingroup FitsIOServer
131 Class for reading a column in a FITS ASCII or BINARY table
132 \verbatim
[1659]133 -- Exemple:
[2449]134 // Open the fits file with FitsOpenFile
135 FitsOpenFile fof = new FitsOpenFile("myfits.fits");
136 // Select the column to be read
137 FitsABTColRd fbt(fof,"BoloMuv_28",0,1000,1,3);
[1654]138 fbt.SetDebug(3);
139 fbt.Print(3);
[1659]140 // Read element by element
[1654]141 for(long i=0;i<fbt.GetNbLine();i++) {
142 double x = fbt.Read(i);
143 if(i%lpmod==0) cout<<i<<": "<<x<<endl;
144 }
[1659]145 // Read into a vector
146 TVector<double> data;
147 long n = fbt.Read(32,50,data);
148 cout<<"Number of values read: "<<n<<endl;
149 data.ReSize(100);
150 n = fbt.Read(10,-1,data);
151 cout<<"Number of values read: "<<n<<endl;
[2449]152 // Close the fits file
153 delete fof;
[1654]154 \endverbatim
155*/
156
157//////////////////////////////////////////////////////////////
158/*!
159 Constructor.
[2449]160 \param fof : Pointer to the Class for opening the FITS file
[1659]161 \param collabel : label of the column to be read
162 \param ihdu : number of the HDU where the column is.
163 \param blen : read buffer length
164 \param bsens : buffer reading direction
165 \param lp : debug level
[1654]166 \verbatim
[1659]167 - if ihdu=0 or ihdu>nhdu first binary or ASCII table is taken
168 - bsens>0 read forward
169 bsens<0 read backward
170 bsens==0 read centered
[1654]171 \endverbatim
[1659]172 \warning ihdu = [1,nhdu]
[1654]173*/
[2449]174FitsABTColRd::FitsABTColRd(FitsOpenFile* fof,string collabel
175 ,int ihdu,long blen,long bsens,int lp)
[1654]176{
[2449]177 Init(fof,collabel.c_str(),-1,ihdu,blen,bsens,lp);
[1654]178}
179
180/*!
181 Constructor.
[1659]182 Same as before but the column is identified by its column number
183 \param colnum : number of the column to be read
184 \warning col = [0,ncol[
[1654]185*/
[2449]186FitsABTColRd::FitsABTColRd(FitsOpenFile* fof,int colnum
187 ,int ihdu,long blen,long bsens,int lp)
[1654]188{
[2449]189 Init(fof,"",colnum,ihdu,blen,bsens,lp);
[1654]190}
191
[2449]192/*! Constructor by copy */
193FitsABTColRd::FitsABTColRd(FitsABTColRd& fbt)
[1654]194{
[2449]195 Init(fbt.GetFitsOpenFile(),fbt.GetColLabel().c_str()
196 ,fbt.GetColNum(),fbt.GetHDU()
197 ,fbt.GetBLen(),fbt.GetBSens(),fbt.DbgLevel);
[1654]198}
199
[2449]200/*! Constructor by default */
201FitsABTColRd::FitsABTColRd()
[1654]202{
[2449]203 FitsFN = "";
204 ColLabel = ""; ColTUnit = ""; ColTForm = "";
205 ColNum = -1; ColTypeCode = 0;
206 IHdu = 0; NHdu = 0; HduType = 0;
207 NBcol = 0; NBline = 0;
208 SetNulVal(); SetDebug(0);
209 NFitsRead = 0;
210 FitsOF = NULL; FitsPtr = NULL;
211 LineDeb = LineFin = -1;
212 Buffer = NULL;
[1654]213}
214
215/*! Init routine called by the constructor */
[2449]216void FitsABTColRd::Init(FitsOpenFile* fof,const char* collabel,int colnum
[1654]217 ,int ihdu,long blen,long bsens,int lp)
218{
[2449]219 // Initialisation des Parametres Generaux
220 FitsFN = "";
[1654]221 ColLabel = collabel;
222 ColTUnit = "";
223 ColTForm = "";
224 ColNum = colnum;
225 ColTypeCode = 0;
226 IHdu = ihdu;
227 NHdu = 0;
228 HduType = 0;
229 NBcol = 0;
230 NBline = 0;
231 SetNulVal();
232 SetDebug(lp);
233 NFitsRead = 0;
[2449]234 FitsOF = NULL;
[1654]235 FitsPtr = NULL;
236 LineDeb = LineFin = -1;
237 Buffer = NULL;
[1657]238 ChangeBuffer(blen,bsens);
[1654]239
[2449]240 // Caracteristiques du FitsOpenFile
241 FitsOF = fof;
242 if(FitsOF==NULL) {
243 Delete();
244 throw NullPtrError("FitsABTColRd::Init: FitsOpenFile pointer is NULL\n");
[1654]245 }
[2449]246 FitsPtr = FitsOF->GetFitsPtr();
247 if(FitsPtr==NULL) {
248 Delete();
249 throw NullPtrError("FitsABTColRd::Init: FitsPtr pointer is NULL\n");
[1654]250 }
[2449]251 NHdu = FitsOF->GetNHdu();
252 if(DbgLevel>1) cout<<"FitsABTColRd::Init NHdu="<<NHdu<<endl;
[1654]253 if(NHdu<=0) {
254 Delete();
[2449]255 throw SzMismatchError("FitsABTColRd::Init: Bad NHdu\n");
[1654]256 }
[2449]257 FitsFN = FitsOF->GetFileName();
258 if(FitsFN.size() <= 0 ) {
259 Delete();
260 throw ParmError("FitsABTColRd::Init: Fits file name error\n");
261 }
[1654]262
[2449]263 int sta = 0;
264
[1654]265 // Get HDU for bin/ascii table
266 // si IHdu <=0 || >NHdu on cherche la 1ere bin/ascii table
267 // sinon on se positionne sur IHdu
268 if(IHdu<=0 || IHdu>NHdu)
269 for(int ihdu=1;ihdu<=NHdu;ihdu++) {
270 if(fits_movabs_hdu(FitsPtr,ihdu,&HduType,&sta)) printerror(sta);
271 if(DbgLevel>1) cout<<"...Init ihdu="
272 <<ihdu<<" HduType="<<HduType<<endl;
273 if(HduType==BINARY_TBL || HduType==ASCII_TBL) {IHdu = ihdu; break;}
274 }
275 if(IHdu<=0 || IHdu>NHdu) {
276 cout<<"NO BINARY or ASCII hdu found"<<endl;
[1657]277 IHdu = 0; Delete();
[2449]278 throw TypeMismatchExc("FitsABTColRd::Init: NO BINARY or ASCII hdu found\n");
[1654]279 }
280 if(fits_movabs_hdu(FitsPtr,IHdu,&HduType,&sta)) {
281 printerror(sta); Delete();
[2449]282 throw RangeCheckError("FitsABTColRd::Init: Error moving to requested HDU\n");
[1654]283 }
284 if(HduType!=BINARY_TBL && HduType!=ASCII_TBL) {
285 Delete();
[2449]286 throw TypeMismatchExc("FitsABTColRd::Init: HDU not ASCII/BINARY table\n");
[1654]287 }
288
289 // Get number of columns
290 if(fits_get_num_cols(FitsPtr,&NBcol,&sta)) {
291 printerror(sta); Delete();
[2449]292 throw NotAvailableOperation("FitsABTColRd::Init: Error getting number of columns\n");
[1654]293 }
294 if(DbgLevel>1) cout<<"...Init NBcol="<<NBcol<<endl;
295 if(NBcol<1) {
296 Delete();
[2449]297 throw RangeCheckError("FitsABTColRd::Init: Bad number of colums\n");
[1654]298 }
299
300 // Get number of rows
301 if(fits_get_num_rows(FitsPtr,&NBline,&sta)) {
302 printerror(sta); Delete();
[2449]303 throw NotAvailableOperation("FitsABTColRd::Init: Error getting number of rows\n");
[1654]304 }
305 if(DbgLevel>1) cout<<"...Init NBline="<<NBline<<endl;
306 if(NBline<1) {
307 Delete();
[2449]308 throw RangeCheckError("FitsABTColRd::Init: Bad number of rows\n");
[1654]309 }
310
311 // Get column number
[1660]312 char labelcol[128];
[1654]313 if(ColLabel.size() > 0) {
314 strcpy(labelcol,ColLabel.c_str());
315 if(fits_get_colnum(FitsPtr,CASESEN,labelcol,&ColNum,&sta)) {
316 printerror(sta); Delete();
[2449]317 throw NotAvailableOperation("FitsABTColRd::Init: Error getting column name\n");
[1654]318 }
319 ColNum--; // Convention [0,ncol[
320 }
321 if(DbgLevel>1) cout<<"...Init ColNum="<<ColNum<<endl;
322 if(ColNum<0 || ColNum>=NBcol) {
323 Delete();
[2449]324 throw RangeCheckError("FitsABTColRd::Init: Bad column number\n");
[1654]325 }
326
327 // Get column type
328 if(fits_get_coltype(FitsPtr,ColNum+1,&ColTypeCode,NULL,NULL,&sta)) {
329 printerror(sta); Delete();
[2449]330 throw ParmError("FitsABTColRd::Init: Error getting column type\n");
[1654]331 }
332 if(DbgLevel>1) cout<<"...Init ColTypeCode="<<ColTypeCode<<endl;
[1660]333 if(ColTypeCode==TSTRING || ColTypeCode==TCOMPLEX || ColTypeCode==TDBLCOMPLEX
334 || ColTypeCode<0 ) {
[1654]335 Delete();
[2449]336 throw ParmError("FitsABTColRd::Init: Selected column is not Numerical\n");
[1654]337 }
338
339 // Get column name back, tunit, tform
[2174]340 char tunit[64], tform[64], tdisp[64];
[2173]341 long repeat=0; double tscale=1., tzero=0.;
[1654]342 int rc=0;
343 if(HduType==BINARY_TBL) {
[2173]344 fits_get_bcolparms(FitsPtr,ColNum+1,labelcol,tunit,tform
[2174]345 ,&repeat,&tscale,&tzero,NULL,tdisp,&sta);
[1654]346 } else {
[2173]347 fits_get_acolparms(FitsPtr,ColNum+1,labelcol,&repeat,tunit,tform
[2174]348 ,&tscale,&tzero,NULL,tdisp,&sta);
[1654]349 }
350 if(rc) {
351 printerror(sta); Delete();
[2449]352 throw RangeCheckError("FitsABTColRd::Init: Error getting the column caracteristics\n");
[1654]353 }
354 ColLabel = labelcol;
355 ColTUnit = tunit;
356 ColTForm = tform;
357
358 if(DbgLevel)
[2449]359 cout<<"FitsABTColRd::Init Num="<<ColNum<<" Label="<<ColLabel
[2173]360 <<" TypeCode="<<ColTypeCode<<" TUnit="<<ColTUnit<<" TForm="<<ColTForm<<endl;
361 if(DbgLevel>1)
[2174]362 cout<<" (repeat="<<repeat<<",tscale="<<tscale<<",tzero="<<tzero
363 <<",tdisp="<<tdisp<<")"<<endl;
[1654]364
365}
366
367/*! Destructor. */
[2449]368FitsABTColRd::~FitsABTColRd()
[1654]369{
370 Delete();
371}
372
[2449]373/*! Delete called by the destructor */
374void FitsABTColRd::Delete(void)
[1814]375{
[2449]376 if(Buffer!=NULL) {delete [] Buffer; Buffer=NULL;}
377 LineDeb = LineFin = -1;
378 //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
[1814]379}
[1654]380
381//////////////////////////////////////////////////////////////
[1659]382/*! Change the buffer caracteristiques (see creator) */
[2449]383void FitsABTColRd::ChangeBuffer(long blen,long bsens)
[1654]384{
[1657]385 long oldnbuffer = NBuffer;
386
387 // Compute buffer caracteristics
[1654]388 BuffLen = (blen<=0)? 1: blen;
389 BuffSens = bsens;
[1657]390 NBuffer = BuffLen;
391 if(bsens==0 && NBuffer%2==0) NBuffer++;
[1654]392
[1657]393 // De-allocate if necessary
[1659]394 if(Buffer!=NULL) {
395 // On des-alloue si pas assez de place
396 // ou si l'ancienne place est beaucoup trop grande (>25%)
397 if(oldnbuffer<NBuffer || (oldnbuffer>NBuffer+long(0.25*NBuffer)) )
398 {delete [] Buffer; Buffer=NULL;}
399 }
[1654]400
[1657]401 // Re-allocate
402 if(Buffer==NULL) Buffer = new double[NBuffer];
403
[1654]404 // Tell program that nothing is into buffer
405 LineDeb = LineFin = -1;
406}
407
[2449]408//////////////////////////////////////////////////////////////
[2451]409/*!
410 Read a fitsheader key into double
411 \param keyname : name of the key
412 \return value into double
413*/
[2449]414double FitsABTColRd::ReadKey(char *keyname)
[1654]415{
[2449]416 if(keyname==NULL) return 0.;
417 int sta=0; double val=0.;
418 if(fits_read_key(FitsPtr,TDOUBLE,keyname,&val,NULL,&sta))
419 printerror(sta);
420 return val;
[1654]421}
422
[2451]423/*!
424 Read a fitsheader key into long
425 \param keyname : name of the key
426 \return value into long
427*/
428long FitsABTColRd::ReadKeyL(char *keyname)
429{
430 if(keyname==NULL) return 0;
431 int sta=0; long val=0;
432 if(fits_read_key(FitsPtr,TLONG,keyname,&val,NULL,&sta))
433 printerror(sta);
434 return val;
435}
436
437/*!
438 Read a fitsheader key into string
439 \param keyname : name of the key
440 \return value into string
441*/
442string FitsABTColRd::ReadKeyS(char *keyname)
443{
444 if(keyname==NULL) return 0;
445 int sta=0; char val[FLEN_VALUE];
446 if(fits_read_key(FitsPtr,TSTRING,keyname,val,NULL,&sta))
447 printerror(sta);
448 string sval = val;
449 return sval;
450}
451
[1654]452/////////////////////////////////////////////////
453/*!
[1659]454 Read row "n" and return the value into a double
455 \warning be carefull for the range: row = [0,NRows[
456 \return value in double
457 \param n : number of the row to be read.
[1654]458 \verbatim
[1659]459 usebuffer == true : use read optimisation with bufferisation
460 == false : no optimisation with bufferisation
461 just read one value
[1654]462 \endverbatim
463*/
[2449]464double FitsABTColRd::Read(long n,bool usebuffer)
[1654]465// Attention: n=nline [0,NBline[, cfistio veut [1,NBline]
466// Attention: colnum [0,NBcol[ , cfistio veut [1,NBcol]
467{
[1659]468 int sta=0;
[1654]469 if(n<0 || n>=NBline)
[2449]470 throw RangeCheckError("FitsABTColRd::Read try to read outside line range\n");
[1654]471
472 // Pas de bufferisation, on lit betement
[1659]473 if(NBuffer==1 || !usebuffer) {
[1654]474 NFitsRead++;
[1659]475 double val;
476 fits_read_col(FitsPtr,TDOUBLE,ColNum+1,n+1,1,1,NULL,&val,NULL,&sta);
[1654]477 if(sta) {
478 printerror(sta);
[2449]479 throw NotAvailableOperation("FitsABTColRd::Read: Error Reading Fits file\n");
[1654]480 }
[1659]481 // On ne remplit Buffer[0] que si on a choisit
482 // un mode de lecture non bufferise (n==1) DES LE DEBUT.
483 // Si on a initialement choisit un mode bufferise (avec n>1),
484 // Buffer contient les valeurs chargees auparavent.
485 // Il ne faut pas faire {Buffer[0]=val; LineDeb=LineFin=n;}
486 // car on perd l'info de ces valeurs.
487 if(NBuffer==1) {Buffer[0]=val; LineDeb=LineFin=n;}
488 return val;
[1654]489 }
490
491 // Gestion avec bufferisation
[1659]492 if(!Buffer)
[2449]493 throw RangeCheckError("FitsABTColRd::Read: Buffer not allocated\n");
[1654]494 if(n<LineDeb || n>LineFin) {
495 NFitsRead++;
496 long row1,row2,nrow;
497 if(BuffSens>0) { // Cas remplissage forward
498 row1 = n+1;
[1657]499 row2 = row1+NBuffer-1; if(row2>NBline) row2 = NBline;
[1654]500 } else if(BuffSens<0) { // Cas remplissage backward
501 row2 = n+1;
[1657]502 row1 = row2-NBuffer+1; if(row1<1) row1 = 1;
[1654]503 } else { // Cas remplissage centre
[1657]504 row1 = n+1 - NBuffer/2; if(row1<1) row1 = 1;
505 row2 = n+1 + NBuffer/2; if(row2>NBline) row2 = NBline;
[1654]506 }
507 nrow = row2 - row1 + 1;
508 LineDeb = row1-1; LineFin = row2-1;
509 //cout<<"DBG-FitsRead: row1="<<row1<<" row2="<<row2<<" nrow="<<nrow
510 // <<" LineDeb,Fin="<<LineDeb<<","<<LineFin<<endl;
[1659]511 fits_read_col(FitsPtr,TDOUBLE,ColNum+1,row1,1,nrow,NULL,Buffer,NULL,&sta);
[1654]512 if(sta) {
513 printerror(sta);
514 LineDeb = LineFin = -1;
[2449]515 throw NotAvailableOperation("FitsABTColRd::Read: Error Reading Fits file\n");
[1654]516 }
517 }
518
519 long ibuf = n-LineDeb;
520 return Buffer[ibuf];
521}
522
523/*!
[1659]524 Read rows from "n1" to "n2" and return the values into TVector of double
525 \return NREAD the number of values read (n2-n1+1).
526 \warning row = [0,NRows[, the routine read [n1,n2]
[1654]527 \verbatim
[1659]528 - if n2<0 then read [n1,n2] where "n2=min(n1+vector_size-1,nrows-1)"
529 - Last row read is ALWAYS: "n2 = n1 + NREAD -1"
530 - The TVector is never resized if not necessary
531 -------------------------------------------------------------------------
532 - ex: suppose the column table contains 10 elements: nrows=10, rows=[0,9]
533
534 TVector<double> V(5);
535 bt.Read(3,5,V) -> read rows=3,4,5 -> V.Size()==5 -> return 3
536 bt.Read(3,-1,V) -> read rows=3,4,5,6,7 -> V.Size()==5 -> return 5
537 bt.Read(7,-1,V) -> read rows=7,8,9 -> V.Size()==5 -> return 3
538 bt.Read(2,-1,V) -> read rows=2,3,4,5,6 -> V.Size()==5 -> return 5
539 bt.Read(-1,5,V) -> throw exception
540
541 TVector<double> V(5);
542 bt.Read(3,99,V) -> read rows=3,4,5,6,7,8,9 -> V.Size()==7 -> return 7
543
544 TVector<double> V(5);
545 bt.Read(2,8,V) -> read rows=2,3,4,5,6,7,8 -> V.Size()==7 -> return 7
546
547 TVector<double> V;
548 bt.Read(3,5,V) -> read rows=3,4,5 -> V.Size()==3 -> return 3
549
550 TVector<double> V;
551 bt.Read(3,-1,V) -> throw exception
552 -------------------------------------------------------------------------
[1654]553 \endverbatim
554*/
[2449]555long FitsABTColRd::Read(long n1,long n2,TVector<double>& data)
[1654]556{
[1659]557 if(n1<0 || n1>=NBline)
[2449]558 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[1659]559 if(data.Size()<=0 && n2<n1)
[2449]560 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[1659]561 if(n2<0) n2 = n1 + data.Size()-1;
562 if(n2>=NBline) n2 = NBline-1;
[1654]563
[1659]564 sa_size_t nread = n2-n1+1;
565 if(data.Size()<nread) data.SetSize(nread);
566
567 //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
568 int sta=0;
569 fits_read_col(FitsPtr,TDOUBLE,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
570 if(sta) {
571 printerror(sta);
[2449]572 throw NotAvailableOperation("FitsABTColRd::Read_TVector<double>: Error Reading Fits file\n");
[1659]573 }
574
575 return nread;
[1654]576}
577
[1659]578/*! idem before but for TVector of float */
[2449]579long FitsABTColRd::Read(long n1,long n2,TVector<float>& data)
[1659]580{
581 if(n1<0 || n1>=NBline)
[2449]582 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[1659]583 if(data.Size()<=0 && n2<n1)
[2449]584 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[1659]585 if(n2<0) n2 = n1 + data.Size()-1;
586 if(n2>=NBline) n2 = NBline-1;
587
588 sa_size_t nread = n2-n1+1;
589 if(data.Size()<nread) data.SetSize(nread);
590
591 //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
592 int sta=0;
593 fits_read_col(FitsPtr,TFLOAT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
594 if(sta) {
595 printerror(sta);
[2449]596 throw NotAvailableOperation("FitsABTColRd::Read_TVector<float>: Error Reading Fits file\n");
[1659]597 }
598
599 return nread;
600}
601
[2170]602/*! idem before but for TVector of unsigned short */
[2449]603long FitsABTColRd::Read(long n1,long n2,TVector<uint_2>& data)
[2170]604{
605 if(n1<0 || n1>=NBline)
[2449]606 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[2170]607 if(data.Size()<=0 && n2<n1)
[2449]608 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[2170]609 if(n2<0) n2 = n1 + data.Size()-1;
610 if(n2>=NBline) n2 = NBline-1;
611
612 sa_size_t nread = n2-n1+1;
613 if(data.Size()<nread) data.SetSize(nread);
614
615 int sta=0;
616 fits_read_col(FitsPtr,TUSHORT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
617 if(sta) {
618 printerror(sta);
[2449]619 throw NotAvailableOperation("FitsABTColRd::Read_TVector<uint_2>: Error Reading Fits file\n");
[2170]620 }
621
622 return nread;
623}
624
[1659]625/*! idem before but for TVector of int_4 */
[2449]626long FitsABTColRd::Read(long n1,long n2,TVector<int_4>& data)
[1659]627{
628 if(n1<0 || n1>=NBline)
[2449]629 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[1659]630 if(data.Size()<=0 && n2<n1)
[2449]631 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[1659]632 if(n2<0) n2 = n1 + data.Size()-1;
633 if(n2>=NBline) n2 = NBline-1;
634
635 sa_size_t nread = n2-n1+1;
636 if(data.Size()<nread) data.SetSize(nread);
637
638 //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
639 int sta=0;
640 int T = (sizeof(long)==4) ? TLONG: TINT;
641 fits_read_col(FitsPtr,T,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
642 if(sta) {
643 printerror(sta);
[2449]644 throw NotAvailableOperation("FitsABTColRd::Read_TVector<int_4>: Error Reading Fits file\n");
[1659]645 }
646
647 return nread;
648}
649
[2169]650/*! idem before but for TVector of int_8 */
[2449]651long FitsABTColRd::Read(long n1,long n2,TVector<int_8>& data)
[2169]652{
653#ifdef TLONGLONG
654 if(n1<0 || n1>=NBline)
[2449]655 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[2169]656 if(data.Size()<=0 && n2<n1)
[2449]657 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[2169]658 if(n2<0) n2 = n1 + data.Size()-1;
659 if(n2>=NBline) n2 = NBline-1;
660
661 sa_size_t nread = n2-n1+1;
662 if(data.Size()<nread) data.SetSize(nread);
663
664 int sta=0;
665 fits_read_col(FitsPtr,TLONGLONG,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
666 if(sta) {
667 printerror(sta);
[2449]668 throw NotAvailableOperation("FitsABTColRd::Read_TVector<int_8>: Error Reading Fits file\n");
[2169]669 }
670
671 return nread;
672#else
[2449]673 throw PException("FitsABTColRd::Read(..,TVector<int_8>&) Not in that cfitsio version");
[2169]674#endif
675}
676
[1654]677/////////////////////////////////////////////////
[1659]678/*!
679 Return the number of the first row where "val1"<=val<="val2" starting at row "rowstart"
680 \verbatim
681 - The search is performed from "rowstart" to the end
682 in ascending order (from "rowstart" to nrows).
683 - Warning: "rowstart<0" means "rowstart==0" (search all the table column)
684 That is the default
685 \endverbatim
686 \return <0 means not found
687*/
[2449]688long FitsABTColRd::FirstRow(double val1,double val2,long rowstart)
[1659]689{
690 long row = -1;
691 if(NBline==0) return row;
692 // Change buffer for efficiency
693 long bsens=BuffSens; bool bchange=false;
694 if(bsens<=0) {ChangeBuffer(BuffLen,1); bchange=true;}
695 if(rowstart<0) rowstart = 0;
696 if(rowstart>=NBline) rowstart = NBline-1;
697 for(long i=rowstart;i<NBline;i++) {
698 double val = Read(i);
699 if(val<val1 || val>val2) continue;
700 row = i;
701 break;
702 }
703 if(bchange) ChangeBuffer(BuffLen,bsens);
704 return row;
705}
706
707/*!
708 Return the number of the first row where val1<=val<=val2 starting at row rowstart
709 \return <0 means not found
710 \verbatim
711 - The search is performed from "rowstart" to the beginning
712 in descending order (from "rowstart" to 0).
713 - Warning: "rowstart<0" means "rowstart==nrows-1" (search all the table column)
714 That is the default
715 \endverbatim
716*/
[2449]717long FitsABTColRd::LastRow(double val1,double val2,long rowstart)
[1659]718{
719 long row = -1;
720 if(NBline==0) return row;
721 // Change buffer for efficiency
722 long bsens=BuffSens; bool bchange=false;
723 if(bsens>=0) {ChangeBuffer(BuffLen,-1); bchange=true;}
724 if(rowstart<0 || rowstart>=NBline) rowstart = NBline-1;
725 for(long i=rowstart;i>=0;i--) {
726 double val = Read(i);
727 if(val<val1 || val>val2) continue;
728 row = i;
729 break;
730 }
731 if(bchange) ChangeBuffer(BuffLen,bsens);
732 return row;
733}
734
735/////////////////////////////////////////////////
[2449]736void FitsABTColRd::printerror(int sta) const
[1654]737{
738 int stat = sta;
739 fits_report_error(stdout,stat);
740 fflush(stdout);
741 return;
742}
743
744/*! Print on stream os */
[2449]745void FitsABTColRd::Print(ostream& os,int lp) const
[1654]746{
[2449]747 os<<"FitsABTColRd:Print ("<<BuffLen<<","<<BuffSens<<","<<NulVal<<")"
[1654]748 <<" ncols="<<NBcol<<" nrows="<<NBline;
749 if(lp>0) os<<" NRead="<<NFitsRead;
750 os<<"\n... "<<FitsFN<<"["<<IHdu<<"/"<<NHdu<<"]"
751 <<"\n... Label["<<ColNum<<"]="<<ColLabel
752 <<" TypeCode="<<ColTypeCode
753 <<" TUnit="<<ColTUnit<<" TForm="<<ColTForm
754 <<endl;
755}
[2449]756
757///////////////////////////////////////////////////////////////////
758///////////////////////////////////////////////////////////////////
759///////////////////////////////////////////////////////////////////
760///////////////////////////////////////////////////////////////////
761
762//! Class for reading a column in a FITS ASCII or BINARY table with fits file opening
763
764/*!
765 \class SOPHYA::FitsABTColRead
766 \ingroup FitsIOServer
767 Class for reading a column in a FITS ASCII or BINARY table with fits file opening
768 \verbatim
769 -- Exemple:
770 FitsABTColRead fbt("myfits.fits","BoloMuv_28",0,1000,1,3);
771 fbt.SetDebug(3);
772 fbt.Print(3);
773 // Read element by element
774 for(long i=0;i<fbt.GetNbLine();i++) {
775 double x = fbt.Read(i);
776 if(i%lpmod==0) cout<<i<<": "<<x<<endl;
777 }
778 // Read into a vector
779 TVector<double> data;
780 long n = fbt.Read(32,50,data);
781 cout<<"Number of values read: "<<n<<endl;
782 data.ReSize(100);
783 n = fbt.Read(10,-1,data);
784 cout<<"Number of values read: "<<n<<endl;
785 \endverbatim
786*/
787
788
789//////////////////////////////////////////////////////////////
790/*!
791 Constructor.
792 \param fname : FITS file name to be read
793 \param collabel : label of the column to be read
794 \param ihdu : number of the HDU where the column is.
795 \param blen : read buffer length
796 \param bsens : buffer reading direction
797 \param lp : debug level
798 \verbatim
799 - if ihdu=0 or ihdu>nhdu first binary or ASCII table is taken
800 - bsens>0 read forward
801 bsens<0 read backward
802 bsens==0 read centered
803 \endverbatim
804 \warning ihdu = [1,nhdu]
805*/
806FitsABTColRead::FitsABTColRead(string fname,string collabel
807 ,int ihdu,long blen,long bsens,int lp)
808: FitsABTColRd(new FitsOpenFile(fname),collabel,ihdu,blen,bsens,lp)
809{
810}
811
812/*!
813 Constructor.
814 Same as before but the column is identified by its column number
815 \param colnum : number of the column to be read
816 \warning col = [0,ncol[
817*/
818FitsABTColRead::FitsABTColRead(string fname,int colnum
819 ,int ihdu,long blen,long bsens,int lp)
820: FitsABTColRd(new FitsOpenFile(fname),colnum,ihdu,blen,bsens,lp)
821{
822}
823
824/*! Constructor. see below */
825FitsABTColRead::FitsABTColRead(const char * cfname,const char* collabel
826 ,int ihdu,long blen,long bsens,int lp)
827: FitsABTColRd(new FitsOpenFile(cfname),collabel,ihdu,blen,bsens,lp)
828{
829}
830
831/*! Constructor. see below */
832FitsABTColRead::FitsABTColRead(const char * cfname,int colnum
833 ,int ihdu,long blen,long bsens,int lp)
834: FitsABTColRd(new FitsOpenFile(cfname),colnum,ihdu,blen,bsens,lp)
835{
836}
837/*! Constructor by default */
838FitsABTColRead::FitsABTColRead()
839{
840}
841
842/*! Constructor by copy */
843FitsABTColRead::FitsABTColRead(FitsABTColRead& fbt)
844{
845 // --- ATTENTION ---
846 // FitsABTColRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile
847 FitsOpenFile* fof = new FitsOpenFile(*fbt.GetFitsOpenFile());
848 Init(fof,fbt.GetColLabel().c_str()
849 ,fbt.GetColNum(),fbt.GetHDU()
850 ,fbt.GetBLen(),fbt.GetBSens(),fbt.DbgLevel);
851}
852
853/*! Destructor. */
854FitsABTColRead::~FitsABTColRead()
855{
856 Delete();
857 if(FitsOF!=NULL) delete FitsOF;
858}
Note: See TracBrowser for help on using the repository browser.