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

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

Restructuration des fits ecriteur et lecteur:

  • en lecture: lecture des images 2D
  • en ecriture: ecriture des images 2D d'ou restructuration des classes avec heritage sur une class FitWriter

Pour demande DY...

cmv 13/11/2003

File size: 32.5 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
[2453]112//////////////////////////////////////////////////////////////
113//// Methodes statiques
114//////////////////////////////////////////////////////////////
115/*!
116 Read a fitsheader key into double
117 \param fitsptr : cfitio pointer to Fits file
118 \param keyname : name of the key
119 \return value into double
120*/
121double FitsOpenFile::ReadKey(fitsfile *fitsptr,char *keyname)
122{
123 if(keyname==NULL) return 0.;
124 int sta=0; double val=0.;
125 if(fits_read_key(fitsptr,TDOUBLE,keyname,&val,NULL,&sta))
126 printerror(sta);
127 return val;
128}
129
130/*!
131 Read a fitsheader key into long
132 \param fitsptr : cfitio pointer to Fits file
133 \param keyname : name of the key
134 \return value into long
135*/
136long FitsOpenFile::ReadKeyL(fitsfile *fitsptr,char *keyname)
137{
138 if(keyname==NULL) return 0;
139 int sta=0; long val=0;
140 if(fits_read_key(fitsptr,TLONG,keyname,&val,NULL,&sta))
141 printerror(sta);
142 return val;
143}
144
145/*!
146 Read a fitsheader key into string
147 \param fitsptr : cfitio pointer to Fits file
148 \param keyname : name of the key
149 \return value into string
150*/
151string FitsOpenFile::ReadKeyS(fitsfile *fitsptr,char *keyname)
152{
153 if(keyname==NULL) return (string)"";
154 int sta=0; char val[FLEN_VALUE];
155 if(fits_read_key(fitsptr,TSTRING,keyname,val,NULL,&sta))
156 printerror(sta);
157 string sval = val;
158 return sval;
159}
160
161/*!
162 CFitsIO error printing routine
163 \param sta : cfitio error return code
164*/
165 void FitsOpenFile::printerror(int sta)
[2449]166 {
167 int stat = sta;
168 fits_report_error(stdout,stat);
169 fflush(stdout);
170 return;
171 }
172
173///////////////////////////////////////////////////////////////////
174///////////////////////////////////////////////////////////////////
175///////////////////////////////////////////////////////////////////
176///////////////////////////////////////////////////////////////////
177
178///////////////////////////////////////////////////////////////////
[1654]179//! Class for reading a column in a FITS ASCII or BINARY table
180
181/*!
[2449]182 \class SOPHYA::FitsABTColRd
[1654]183 \ingroup FitsIOServer
184 Class for reading a column in a FITS ASCII or BINARY table
185 \verbatim
[1659]186 -- Exemple:
[2449]187 // Open the fits file with FitsOpenFile
188 FitsOpenFile fof = new FitsOpenFile("myfits.fits");
189 // Select the column to be read
190 FitsABTColRd fbt(fof,"BoloMuv_28",0,1000,1,3);
[1654]191 fbt.SetDebug(3);
192 fbt.Print(3);
[1659]193 // Read element by element
[1654]194 for(long i=0;i<fbt.GetNbLine();i++) {
195 double x = fbt.Read(i);
196 if(i%lpmod==0) cout<<i<<": "<<x<<endl;
197 }
[1659]198 // Read into a vector
199 TVector<double> data;
200 long n = fbt.Read(32,50,data);
201 cout<<"Number of values read: "<<n<<endl;
202 data.ReSize(100);
203 n = fbt.Read(10,-1,data);
204 cout<<"Number of values read: "<<n<<endl;
[2449]205 // Close the fits file
206 delete fof;
[1654]207 \endverbatim
208*/
209
210//////////////////////////////////////////////////////////////
211/*!
212 Constructor.
[2449]213 \param fof : Pointer to the Class for opening the FITS file
[1659]214 \param collabel : label of the column to be read
215 \param ihdu : number of the HDU where the column is.
216 \param blen : read buffer length
217 \param bsens : buffer reading direction
218 \param lp : debug level
[1654]219 \verbatim
[1659]220 - if ihdu=0 or ihdu>nhdu first binary or ASCII table is taken
221 - bsens>0 read forward
222 bsens<0 read backward
223 bsens==0 read centered
[1654]224 \endverbatim
[1659]225 \warning ihdu = [1,nhdu]
[1654]226*/
[2449]227FitsABTColRd::FitsABTColRd(FitsOpenFile* fof,string collabel
228 ,int ihdu,long blen,long bsens,int lp)
[1654]229{
[2449]230 Init(fof,collabel.c_str(),-1,ihdu,blen,bsens,lp);
[1654]231}
232
233/*!
234 Constructor.
[1659]235 Same as before but the column is identified by its column number
236 \param colnum : number of the column to be read
237 \warning col = [0,ncol[
[1654]238*/
[2449]239FitsABTColRd::FitsABTColRd(FitsOpenFile* fof,int colnum
240 ,int ihdu,long blen,long bsens,int lp)
[1654]241{
[2449]242 Init(fof,"",colnum,ihdu,blen,bsens,lp);
[1654]243}
244
[2449]245/*! Constructor by copy */
246FitsABTColRd::FitsABTColRd(FitsABTColRd& fbt)
[1654]247{
[2449]248 Init(fbt.GetFitsOpenFile(),fbt.GetColLabel().c_str()
249 ,fbt.GetColNum(),fbt.GetHDU()
250 ,fbt.GetBLen(),fbt.GetBSens(),fbt.DbgLevel);
[1654]251}
252
[2449]253/*! Constructor by default */
254FitsABTColRd::FitsABTColRd()
[1654]255{
[2449]256 FitsFN = "";
257 ColLabel = ""; ColTUnit = ""; ColTForm = "";
258 ColNum = -1; ColTypeCode = 0;
259 IHdu = 0; NHdu = 0; HduType = 0;
260 NBcol = 0; NBline = 0;
261 SetNulVal(); SetDebug(0);
262 NFitsRead = 0;
263 FitsOF = NULL; FitsPtr = NULL;
264 LineDeb = LineFin = -1;
265 Buffer = NULL;
[1654]266}
267
268/*! Init routine called by the constructor */
[2449]269void FitsABTColRd::Init(FitsOpenFile* fof,const char* collabel,int colnum
[1654]270 ,int ihdu,long blen,long bsens,int lp)
271{
[2449]272 // Initialisation des Parametres Generaux
273 FitsFN = "";
[1654]274 ColLabel = collabel;
275 ColTUnit = "";
276 ColTForm = "";
277 ColNum = colnum;
278 ColTypeCode = 0;
279 IHdu = ihdu;
280 NHdu = 0;
281 HduType = 0;
282 NBcol = 0;
283 NBline = 0;
284 SetNulVal();
285 SetDebug(lp);
286 NFitsRead = 0;
[2449]287 FitsOF = NULL;
[1654]288 FitsPtr = NULL;
289 LineDeb = LineFin = -1;
290 Buffer = NULL;
[1657]291 ChangeBuffer(blen,bsens);
[1654]292
[2449]293 // Caracteristiques du FitsOpenFile
294 FitsOF = fof;
295 if(FitsOF==NULL) {
296 Delete();
297 throw NullPtrError("FitsABTColRd::Init: FitsOpenFile pointer is NULL\n");
[1654]298 }
[2449]299 FitsPtr = FitsOF->GetFitsPtr();
300 if(FitsPtr==NULL) {
301 Delete();
302 throw NullPtrError("FitsABTColRd::Init: FitsPtr pointer is NULL\n");
[1654]303 }
[2449]304 NHdu = FitsOF->GetNHdu();
305 if(DbgLevel>1) cout<<"FitsABTColRd::Init NHdu="<<NHdu<<endl;
[1654]306 if(NHdu<=0) {
307 Delete();
[2449]308 throw SzMismatchError("FitsABTColRd::Init: Bad NHdu\n");
[1654]309 }
[2449]310 FitsFN = FitsOF->GetFileName();
311 if(FitsFN.size() <= 0 ) {
312 Delete();
313 throw ParmError("FitsABTColRd::Init: Fits file name error\n");
314 }
[1654]315
[2449]316 int sta = 0;
317
[1654]318 // Get HDU for bin/ascii table
319 // si IHdu <=0 || >NHdu on cherche la 1ere bin/ascii table
320 // sinon on se positionne sur IHdu
321 if(IHdu<=0 || IHdu>NHdu)
322 for(int ihdu=1;ihdu<=NHdu;ihdu++) {
[2453]323 if(fits_movabs_hdu(FitsPtr,ihdu,&HduType,&sta)) FitsOpenFile::printerror(sta);
[1654]324 if(DbgLevel>1) cout<<"...Init ihdu="
325 <<ihdu<<" HduType="<<HduType<<endl;
326 if(HduType==BINARY_TBL || HduType==ASCII_TBL) {IHdu = ihdu; break;}
327 }
328 if(IHdu<=0 || IHdu>NHdu) {
329 cout<<"NO BINARY or ASCII hdu found"<<endl;
[1657]330 IHdu = 0; Delete();
[2449]331 throw TypeMismatchExc("FitsABTColRd::Init: NO BINARY or ASCII hdu found\n");
[1654]332 }
333 if(fits_movabs_hdu(FitsPtr,IHdu,&HduType,&sta)) {
[2453]334 FitsOpenFile::printerror(sta); Delete();
[2449]335 throw RangeCheckError("FitsABTColRd::Init: Error moving to requested HDU\n");
[1654]336 }
337 if(HduType!=BINARY_TBL && HduType!=ASCII_TBL) {
338 Delete();
[2449]339 throw TypeMismatchExc("FitsABTColRd::Init: HDU not ASCII/BINARY table\n");
[1654]340 }
341
342 // Get number of columns
343 if(fits_get_num_cols(FitsPtr,&NBcol,&sta)) {
[2453]344 FitsOpenFile::printerror(sta); Delete();
[2449]345 throw NotAvailableOperation("FitsABTColRd::Init: Error getting number of columns\n");
[1654]346 }
347 if(DbgLevel>1) cout<<"...Init NBcol="<<NBcol<<endl;
348 if(NBcol<1) {
349 Delete();
[2449]350 throw RangeCheckError("FitsABTColRd::Init: Bad number of colums\n");
[1654]351 }
352
353 // Get number of rows
354 if(fits_get_num_rows(FitsPtr,&NBline,&sta)) {
[2453]355 FitsOpenFile::printerror(sta); Delete();
[2449]356 throw NotAvailableOperation("FitsABTColRd::Init: Error getting number of rows\n");
[1654]357 }
358 if(DbgLevel>1) cout<<"...Init NBline="<<NBline<<endl;
359 if(NBline<1) {
360 Delete();
[2449]361 throw RangeCheckError("FitsABTColRd::Init: Bad number of rows\n");
[1654]362 }
363
364 // Get column number
[1660]365 char labelcol[128];
[1654]366 if(ColLabel.size() > 0) {
367 strcpy(labelcol,ColLabel.c_str());
368 if(fits_get_colnum(FitsPtr,CASESEN,labelcol,&ColNum,&sta)) {
[2453]369 FitsOpenFile::printerror(sta); Delete();
[2449]370 throw NotAvailableOperation("FitsABTColRd::Init: Error getting column name\n");
[1654]371 }
372 ColNum--; // Convention [0,ncol[
373 }
374 if(DbgLevel>1) cout<<"...Init ColNum="<<ColNum<<endl;
375 if(ColNum<0 || ColNum>=NBcol) {
376 Delete();
[2449]377 throw RangeCheckError("FitsABTColRd::Init: Bad column number\n");
[1654]378 }
379
380 // Get column type
381 if(fits_get_coltype(FitsPtr,ColNum+1,&ColTypeCode,NULL,NULL,&sta)) {
[2453]382 FitsOpenFile::printerror(sta); Delete();
[2449]383 throw ParmError("FitsABTColRd::Init: Error getting column type\n");
[1654]384 }
385 if(DbgLevel>1) cout<<"...Init ColTypeCode="<<ColTypeCode<<endl;
[1660]386 if(ColTypeCode==TSTRING || ColTypeCode==TCOMPLEX || ColTypeCode==TDBLCOMPLEX
387 || ColTypeCode<0 ) {
[1654]388 Delete();
[2449]389 throw ParmError("FitsABTColRd::Init: Selected column is not Numerical\n");
[1654]390 }
391
392 // Get column name back, tunit, tform
[2174]393 char tunit[64], tform[64], tdisp[64];
[2173]394 long repeat=0; double tscale=1., tzero=0.;
[1654]395 int rc=0;
396 if(HduType==BINARY_TBL) {
[2173]397 fits_get_bcolparms(FitsPtr,ColNum+1,labelcol,tunit,tform
[2174]398 ,&repeat,&tscale,&tzero,NULL,tdisp,&sta);
[1654]399 } else {
[2173]400 fits_get_acolparms(FitsPtr,ColNum+1,labelcol,&repeat,tunit,tform
[2174]401 ,&tscale,&tzero,NULL,tdisp,&sta);
[1654]402 }
403 if(rc) {
[2453]404 FitsOpenFile::printerror(sta); Delete();
[2449]405 throw RangeCheckError("FitsABTColRd::Init: Error getting the column caracteristics\n");
[1654]406 }
407 ColLabel = labelcol;
408 ColTUnit = tunit;
409 ColTForm = tform;
410
411 if(DbgLevel)
[2449]412 cout<<"FitsABTColRd::Init Num="<<ColNum<<" Label="<<ColLabel
[2173]413 <<" TypeCode="<<ColTypeCode<<" TUnit="<<ColTUnit<<" TForm="<<ColTForm<<endl;
414 if(DbgLevel>1)
[2174]415 cout<<" (repeat="<<repeat<<",tscale="<<tscale<<",tzero="<<tzero
416 <<",tdisp="<<tdisp<<")"<<endl;
[1654]417
418}
419
420/*! Destructor. */
[2449]421FitsABTColRd::~FitsABTColRd()
[1654]422{
423 Delete();
424}
425
[2449]426/*! Delete called by the destructor */
427void FitsABTColRd::Delete(void)
[1814]428{
[2449]429 if(Buffer!=NULL) {delete [] Buffer; Buffer=NULL;}
430 LineDeb = LineFin = -1;
431 //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
[1814]432}
[1654]433
434//////////////////////////////////////////////////////////////
[1659]435/*! Change the buffer caracteristiques (see creator) */
[2449]436void FitsABTColRd::ChangeBuffer(long blen,long bsens)
[1654]437{
[1657]438 long oldnbuffer = NBuffer;
439
440 // Compute buffer caracteristics
[1654]441 BuffLen = (blen<=0)? 1: blen;
442 BuffSens = bsens;
[1657]443 NBuffer = BuffLen;
444 if(bsens==0 && NBuffer%2==0) NBuffer++;
[1654]445
[1657]446 // De-allocate if necessary
[1659]447 if(Buffer!=NULL) {
448 // On des-alloue si pas assez de place
449 // ou si l'ancienne place est beaucoup trop grande (>25%)
450 if(oldnbuffer<NBuffer || (oldnbuffer>NBuffer+long(0.25*NBuffer)) )
451 {delete [] Buffer; Buffer=NULL;}
452 }
[1654]453
[1657]454 // Re-allocate
455 if(Buffer==NULL) Buffer = new double[NBuffer];
456
[1654]457 // Tell program that nothing is into buffer
458 LineDeb = LineFin = -1;
459}
460
[2449]461//////////////////////////////////////////////////////////////
[2451]462/*!
463 Read a fitsheader key into double
464 \param keyname : name of the key
465 \return value into double
466*/
[2449]467double FitsABTColRd::ReadKey(char *keyname)
[1654]468{
[2453]469 return FitsOpenFile::ReadKey(FitsPtr,keyname);
[1654]470}
471
[2451]472/*!
473 Read a fitsheader key into long
474 \param keyname : name of the key
475 \return value into long
476*/
477long FitsABTColRd::ReadKeyL(char *keyname)
478{
[2453]479 return FitsOpenFile::ReadKeyL(FitsPtr,keyname);
[2451]480}
481
482/*!
483 Read a fitsheader key into string
484 \param keyname : name of the key
485 \return value into string
486*/
487string FitsABTColRd::ReadKeyS(char *keyname)
488{
[2453]489 return FitsOpenFile::ReadKeyS(FitsPtr,keyname);
[2451]490}
491
[1654]492/////////////////////////////////////////////////
493/*!
[1659]494 Read row "n" and return the value into a double
495 \warning be carefull for the range: row = [0,NRows[
496 \return value in double
497 \param n : number of the row to be read.
[1654]498 \verbatim
[1659]499 usebuffer == true : use read optimisation with bufferisation
500 == false : no optimisation with bufferisation
501 just read one value
[1654]502 \endverbatim
503*/
[2449]504double FitsABTColRd::Read(long n,bool usebuffer)
[1654]505// Attention: n=nline [0,NBline[, cfistio veut [1,NBline]
506// Attention: colnum [0,NBcol[ , cfistio veut [1,NBcol]
507{
[1659]508 int sta=0;
[1654]509 if(n<0 || n>=NBline)
[2449]510 throw RangeCheckError("FitsABTColRd::Read try to read outside line range\n");
[1654]511
512 // Pas de bufferisation, on lit betement
[1659]513 if(NBuffer==1 || !usebuffer) {
[1654]514 NFitsRead++;
[1659]515 double val;
516 fits_read_col(FitsPtr,TDOUBLE,ColNum+1,n+1,1,1,NULL,&val,NULL,&sta);
[1654]517 if(sta) {
[2453]518 FitsOpenFile::printerror(sta);
[2449]519 throw NotAvailableOperation("FitsABTColRd::Read: Error Reading Fits file\n");
[1654]520 }
[1659]521 // On ne remplit Buffer[0] que si on a choisit
522 // un mode de lecture non bufferise (n==1) DES LE DEBUT.
523 // Si on a initialement choisit un mode bufferise (avec n>1),
524 // Buffer contient les valeurs chargees auparavent.
525 // Il ne faut pas faire {Buffer[0]=val; LineDeb=LineFin=n;}
526 // car on perd l'info de ces valeurs.
527 if(NBuffer==1) {Buffer[0]=val; LineDeb=LineFin=n;}
528 return val;
[1654]529 }
530
531 // Gestion avec bufferisation
[1659]532 if(!Buffer)
[2449]533 throw RangeCheckError("FitsABTColRd::Read: Buffer not allocated\n");
[1654]534 if(n<LineDeb || n>LineFin) {
535 NFitsRead++;
536 long row1,row2,nrow;
537 if(BuffSens>0) { // Cas remplissage forward
538 row1 = n+1;
[1657]539 row2 = row1+NBuffer-1; if(row2>NBline) row2 = NBline;
[1654]540 } else if(BuffSens<0) { // Cas remplissage backward
541 row2 = n+1;
[1657]542 row1 = row2-NBuffer+1; if(row1<1) row1 = 1;
[1654]543 } else { // Cas remplissage centre
[1657]544 row1 = n+1 - NBuffer/2; if(row1<1) row1 = 1;
545 row2 = n+1 + NBuffer/2; if(row2>NBline) row2 = NBline;
[1654]546 }
547 nrow = row2 - row1 + 1;
548 LineDeb = row1-1; LineFin = row2-1;
549 //cout<<"DBG-FitsRead: row1="<<row1<<" row2="<<row2<<" nrow="<<nrow
550 // <<" LineDeb,Fin="<<LineDeb<<","<<LineFin<<endl;
[1659]551 fits_read_col(FitsPtr,TDOUBLE,ColNum+1,row1,1,nrow,NULL,Buffer,NULL,&sta);
[1654]552 if(sta) {
[2453]553 FitsOpenFile::printerror(sta);
[1654]554 LineDeb = LineFin = -1;
[2449]555 throw NotAvailableOperation("FitsABTColRd::Read: Error Reading Fits file\n");
[1654]556 }
557 }
558
559 long ibuf = n-LineDeb;
560 return Buffer[ibuf];
561}
562
563/*!
[1659]564 Read rows from "n1" to "n2" and return the values into TVector of double
565 \return NREAD the number of values read (n2-n1+1).
566 \warning row = [0,NRows[, the routine read [n1,n2]
[1654]567 \verbatim
[1659]568 - if n2<0 then read [n1,n2] where "n2=min(n1+vector_size-1,nrows-1)"
569 - Last row read is ALWAYS: "n2 = n1 + NREAD -1"
570 - The TVector is never resized if not necessary
571 -------------------------------------------------------------------------
572 - ex: suppose the column table contains 10 elements: nrows=10, rows=[0,9]
573
574 TVector<double> V(5);
575 bt.Read(3,5,V) -> read rows=3,4,5 -> V.Size()==5 -> return 3
576 bt.Read(3,-1,V) -> read rows=3,4,5,6,7 -> V.Size()==5 -> return 5
577 bt.Read(7,-1,V) -> read rows=7,8,9 -> V.Size()==5 -> return 3
578 bt.Read(2,-1,V) -> read rows=2,3,4,5,6 -> V.Size()==5 -> return 5
579 bt.Read(-1,5,V) -> throw exception
580
581 TVector<double> V(5);
582 bt.Read(3,99,V) -> read rows=3,4,5,6,7,8,9 -> V.Size()==7 -> return 7
583
584 TVector<double> V(5);
585 bt.Read(2,8,V) -> read rows=2,3,4,5,6,7,8 -> V.Size()==7 -> return 7
586
587 TVector<double> V;
588 bt.Read(3,5,V) -> read rows=3,4,5 -> V.Size()==3 -> return 3
589
590 TVector<double> V;
591 bt.Read(3,-1,V) -> throw exception
592 -------------------------------------------------------------------------
[1654]593 \endverbatim
594*/
[2449]595long FitsABTColRd::Read(long n1,long n2,TVector<double>& data)
[1654]596{
[1659]597 if(n1<0 || n1>=NBline)
[2449]598 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[1659]599 if(data.Size()<=0 && n2<n1)
[2449]600 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[1659]601 if(n2<0) n2 = n1 + data.Size()-1;
602 if(n2>=NBline) n2 = NBline-1;
[1654]603
[1659]604 sa_size_t nread = n2-n1+1;
605 if(data.Size()<nread) data.SetSize(nread);
606
607 //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
608 int sta=0;
609 fits_read_col(FitsPtr,TDOUBLE,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
610 if(sta) {
[2453]611 FitsOpenFile::printerror(sta);
[2449]612 throw NotAvailableOperation("FitsABTColRd::Read_TVector<double>: Error Reading Fits file\n");
[1659]613 }
614
615 return nread;
[1654]616}
617
[1659]618/*! idem before but for TVector of float */
[2449]619long FitsABTColRd::Read(long n1,long n2,TVector<float>& data)
[1659]620{
621 if(n1<0 || n1>=NBline)
[2449]622 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[1659]623 if(data.Size()<=0 && n2<n1)
[2449]624 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[1659]625 if(n2<0) n2 = n1 + data.Size()-1;
626 if(n2>=NBline) n2 = NBline-1;
627
628 sa_size_t nread = n2-n1+1;
629 if(data.Size()<nread) data.SetSize(nread);
630
631 //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
632 int sta=0;
633 fits_read_col(FitsPtr,TFLOAT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
634 if(sta) {
[2453]635 FitsOpenFile::printerror(sta);
[2449]636 throw NotAvailableOperation("FitsABTColRd::Read_TVector<float>: Error Reading Fits file\n");
[1659]637 }
638
639 return nread;
640}
641
[2170]642/*! idem before but for TVector of unsigned short */
[2449]643long FitsABTColRd::Read(long n1,long n2,TVector<uint_2>& data)
[2170]644{
645 if(n1<0 || n1>=NBline)
[2449]646 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[2170]647 if(data.Size()<=0 && n2<n1)
[2449]648 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[2170]649 if(n2<0) n2 = n1 + data.Size()-1;
650 if(n2>=NBline) n2 = NBline-1;
651
652 sa_size_t nread = n2-n1+1;
653 if(data.Size()<nread) data.SetSize(nread);
654
655 int sta=0;
656 fits_read_col(FitsPtr,TUSHORT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
657 if(sta) {
[2453]658 FitsOpenFile::printerror(sta);
[2449]659 throw NotAvailableOperation("FitsABTColRd::Read_TVector<uint_2>: Error Reading Fits file\n");
[2170]660 }
661
662 return nread;
663}
664
[1659]665/*! idem before but for TVector of int_4 */
[2449]666long FitsABTColRd::Read(long n1,long n2,TVector<int_4>& data)
[1659]667{
668 if(n1<0 || n1>=NBline)
[2449]669 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[1659]670 if(data.Size()<=0 && n2<n1)
[2449]671 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[1659]672 if(n2<0) n2 = n1 + data.Size()-1;
673 if(n2>=NBline) n2 = NBline-1;
674
675 sa_size_t nread = n2-n1+1;
676 if(data.Size()<nread) data.SetSize(nread);
677
678 //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
679 int sta=0;
680 int T = (sizeof(long)==4) ? TLONG: TINT;
681 fits_read_col(FitsPtr,T,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
682 if(sta) {
[2453]683 FitsOpenFile::printerror(sta);
[2449]684 throw NotAvailableOperation("FitsABTColRd::Read_TVector<int_4>: Error Reading Fits file\n");
[1659]685 }
686
687 return nread;
688}
689
[2169]690/*! idem before but for TVector of int_8 */
[2449]691long FitsABTColRd::Read(long n1,long n2,TVector<int_8>& data)
[2169]692{
693#ifdef TLONGLONG
694 if(n1<0 || n1>=NBline)
[2449]695 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[2169]696 if(data.Size()<=0 && n2<n1)
[2449]697 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[2169]698 if(n2<0) n2 = n1 + data.Size()-1;
699 if(n2>=NBline) n2 = NBline-1;
700
701 sa_size_t nread = n2-n1+1;
702 if(data.Size()<nread) data.SetSize(nread);
703
704 int sta=0;
705 fits_read_col(FitsPtr,TLONGLONG,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
706 if(sta) {
[2453]707 FitsOpenFile::printerror(sta);
[2449]708 throw NotAvailableOperation("FitsABTColRd::Read_TVector<int_8>: Error Reading Fits file\n");
[2169]709 }
710
711 return nread;
712#else
[2449]713 throw PException("FitsABTColRd::Read(..,TVector<int_8>&) Not in that cfitsio version");
[2169]714#endif
715}
716
[1654]717/////////////////////////////////////////////////
[1659]718/*!
719 Return the number of the first row where "val1"<=val<="val2" starting at row "rowstart"
720 \verbatim
721 - The search is performed from "rowstart" to the end
722 in ascending order (from "rowstart" to nrows).
723 - Warning: "rowstart<0" means "rowstart==0" (search all the table column)
724 That is the default
725 \endverbatim
726 \return <0 means not found
727*/
[2449]728long FitsABTColRd::FirstRow(double val1,double val2,long rowstart)
[1659]729{
730 long row = -1;
731 if(NBline==0) return row;
732 // Change buffer for efficiency
733 long bsens=BuffSens; bool bchange=false;
734 if(bsens<=0) {ChangeBuffer(BuffLen,1); bchange=true;}
735 if(rowstart<0) rowstart = 0;
736 if(rowstart>=NBline) rowstart = NBline-1;
737 for(long i=rowstart;i<NBline;i++) {
738 double val = Read(i);
739 if(val<val1 || val>val2) continue;
740 row = i;
741 break;
742 }
743 if(bchange) ChangeBuffer(BuffLen,bsens);
744 return row;
745}
746
747/*!
748 Return the number of the first row where val1<=val<=val2 starting at row rowstart
749 \return <0 means not found
750 \verbatim
751 - The search is performed from "rowstart" to the beginning
752 in descending order (from "rowstart" to 0).
753 - Warning: "rowstart<0" means "rowstart==nrows-1" (search all the table column)
754 That is the default
755 \endverbatim
756*/
[2449]757long FitsABTColRd::LastRow(double val1,double val2,long rowstart)
[1659]758{
759 long row = -1;
760 if(NBline==0) return row;
761 // Change buffer for efficiency
762 long bsens=BuffSens; bool bchange=false;
763 if(bsens>=0) {ChangeBuffer(BuffLen,-1); bchange=true;}
764 if(rowstart<0 || rowstart>=NBline) rowstart = NBline-1;
765 for(long i=rowstart;i>=0;i--) {
766 double val = Read(i);
767 if(val<val1 || val>val2) continue;
768 row = i;
769 break;
770 }
771 if(bchange) ChangeBuffer(BuffLen,bsens);
772 return row;
773}
774
[1654]775/*! Print on stream os */
[2449]776void FitsABTColRd::Print(ostream& os,int lp) const
[1654]777{
[2449]778 os<<"FitsABTColRd:Print ("<<BuffLen<<","<<BuffSens<<","<<NulVal<<")"
[1654]779 <<" ncols="<<NBcol<<" nrows="<<NBline;
780 if(lp>0) os<<" NRead="<<NFitsRead;
781 os<<"\n... "<<FitsFN<<"["<<IHdu<<"/"<<NHdu<<"]"
782 <<"\n... Label["<<ColNum<<"]="<<ColLabel
783 <<" TypeCode="<<ColTypeCode
784 <<" TUnit="<<ColTUnit<<" TForm="<<ColTForm
785 <<endl;
786}
[2449]787
788///////////////////////////////////////////////////////////////////
789///////////////////////////////////////////////////////////////////
790///////////////////////////////////////////////////////////////////
791///////////////////////////////////////////////////////////////////
792
793//! Class for reading a column in a FITS ASCII or BINARY table with fits file opening
794
795/*!
796 \class SOPHYA::FitsABTColRead
797 \ingroup FitsIOServer
798 Class for reading a column in a FITS ASCII or BINARY table with fits file opening
799 \verbatim
800 -- Exemple:
801 FitsABTColRead fbt("myfits.fits","BoloMuv_28",0,1000,1,3);
802 fbt.SetDebug(3);
803 fbt.Print(3);
804 // Read element by element
805 for(long i=0;i<fbt.GetNbLine();i++) {
806 double x = fbt.Read(i);
807 if(i%lpmod==0) cout<<i<<": "<<x<<endl;
808 }
809 // Read into a vector
810 TVector<double> data;
811 long n = fbt.Read(32,50,data);
812 cout<<"Number of values read: "<<n<<endl;
813 data.ReSize(100);
814 n = fbt.Read(10,-1,data);
815 cout<<"Number of values read: "<<n<<endl;
816 \endverbatim
817*/
818
819
820//////////////////////////////////////////////////////////////
821/*!
822 Constructor.
823 \param fname : FITS file name to be read
824 \param collabel : label of the column to be read
825 \param ihdu : number of the HDU where the column is.
826 \param blen : read buffer length
827 \param bsens : buffer reading direction
828 \param lp : debug level
829 \verbatim
830 - if ihdu=0 or ihdu>nhdu first binary or ASCII table is taken
831 - bsens>0 read forward
832 bsens<0 read backward
833 bsens==0 read centered
834 \endverbatim
835 \warning ihdu = [1,nhdu]
836*/
837FitsABTColRead::FitsABTColRead(string fname,string collabel
838 ,int ihdu,long blen,long bsens,int lp)
839: FitsABTColRd(new FitsOpenFile(fname),collabel,ihdu,blen,bsens,lp)
840{
841}
842
843/*!
844 Constructor.
845 Same as before but the column is identified by its column number
846 \param colnum : number of the column to be read
847 \warning col = [0,ncol[
848*/
849FitsABTColRead::FitsABTColRead(string fname,int colnum
850 ,int ihdu,long blen,long bsens,int lp)
851: FitsABTColRd(new FitsOpenFile(fname),colnum,ihdu,blen,bsens,lp)
852{
853}
854
855/*! Constructor. see below */
856FitsABTColRead::FitsABTColRead(const char * cfname,const char* collabel
857 ,int ihdu,long blen,long bsens,int lp)
858: FitsABTColRd(new FitsOpenFile(cfname),collabel,ihdu,blen,bsens,lp)
859{
860}
861
862/*! Constructor. see below */
863FitsABTColRead::FitsABTColRead(const char * cfname,int colnum
864 ,int ihdu,long blen,long bsens,int lp)
865: FitsABTColRd(new FitsOpenFile(cfname),colnum,ihdu,blen,bsens,lp)
866{
867}
868/*! Constructor by default */
869FitsABTColRead::FitsABTColRead()
870{
871}
872
873/*! Constructor by copy */
874FitsABTColRead::FitsABTColRead(FitsABTColRead& fbt)
875{
876 // --- ATTENTION ---
877 // FitsABTColRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile
878 FitsOpenFile* fof = new FitsOpenFile(*fbt.GetFitsOpenFile());
879 Init(fof,fbt.GetColLabel().c_str()
880 ,fbt.GetColNum(),fbt.GetHDU()
881 ,fbt.GetBLen(),fbt.GetBSens(),fbt.DbgLevel);
882}
883
884/*! Destructor. */
885FitsABTColRead::~FitsABTColRead()
886{
887 Delete();
888 if(FitsOF!=NULL) delete FitsOF;
889}
[2453]890
891///////////////////////////////////////////////////////////////////
892//! Class for reading a 2D image from a FITS file
893
894/*!
895 \class SOPHYA::FitsImg2DRd
896 \ingroup FitsIOServer
897 Class for reading a 2D image from a FITS file
898*/
899
900//////////////////////////////////////////////////////////////
901/*!
902 Constructor.
903 \param fof : Pointer to the Class for opening the FITS file
904 \param ihdu : number of the HDU where the column is.
905 \param lp : debug level
906 \verbatim
907 - if ihdu=0 or ihdu>nhdu first binary or ASCII table is taken
908 \endverbatim
909 \warning ihdu = [1,nhdu]
910*/
911FitsImg2DRd::FitsImg2DRd(FitsOpenFile* fof,int ihdu,int lp)
912{
913 Init(fof,ihdu,lp);
914}
915
916/*! Constructor by copy */
917FitsImg2DRd::FitsImg2DRd(FitsImg2DRd& fbt)
918{
919 Init(fbt.GetFitsOpenFile(),fbt.GetHDU(),fbt.DbgLevel);
920}
921
922/*! Constructor by default */
923FitsImg2DRd::FitsImg2DRd()
924{
925 FitsFN = "";
926 IHdu = 0; NHdu = 0; HduType = 0;
927 Naxis[0] = Naxis[1] = 0;
928 SetNulVal(); SetDebug(0);
929 FitsOF = NULL; FitsPtr = NULL;
930}
931
932/*! Init routine called by the constructor */
933void FitsImg2DRd::Init(FitsOpenFile* fof,int ihdu,int lp)
934{
935 // Initialisation des Parametres Generaux
936 FitsFN = "";
937 IHdu = ihdu; NHdu = 0; HduType = 0;
938 Naxis[0] = Naxis[1] = 0;
939 SetNulVal(); SetDebug(lp);
940 FitsOF = NULL; FitsPtr = NULL;
941
942 // Caracteristiques du FitsOpenFile
943 FitsOF = fof;
944 if(FitsOF==NULL)
945 throw NullPtrError("FitsImg2DRd::Init: FitsOpenFile pointer is NULL\n");
946 FitsPtr = FitsOF->GetFitsPtr();
947 if(FitsPtr==NULL)
948 throw NullPtrError("FitsImg2DRd::Init: FitsPtr pointer is NULL\n");
949 NHdu = FitsOF->GetNHdu();
950 if(DbgLevel>1) cout<<"FitsImg2DRd::Init NHdu="<<NHdu<<endl;
951 if(NHdu<=0)
952 throw SzMismatchError("FitsImg2DRd::Init: Bad NHdu\n");
953 FitsFN = FitsOF->GetFileName();
954 if(FitsFN.size() <= 0 )
955 throw ParmError("FitsImg2DRd::Init: Fits file name error\n");
956
957 int sta = 0;
958
959 // Get HDU 2D image
960 // si IHdu <=0 || >NHdu on cherche la 1ere image
961 // sinon on se positionne sur IHdu
962 if(IHdu<=0 || IHdu>NHdu)
963 for(int ihdu=1;ihdu<=NHdu;ihdu++) {
964 if(fits_movabs_hdu(FitsPtr,ihdu,&HduType,&sta)) FitsOpenFile::printerror(sta);
965 if(DbgLevel>1)
966 cout<<"...Init ihdu="<<ihdu<<" HduType="<<HduType<<endl;
967 if(HduType==IMAGE_HDU) {IHdu = ihdu; break;}
968 }
969 if(IHdu<=0 || IHdu>NHdu) {
970 cout<<"NO IMAGE_HDU hdu found"<<endl;
971 IHdu = 0;
972 throw TypeMismatchExc("FitsImg2DRd::Init: NO IMAGE_HDU hdu found\n");
973 }
974 if(fits_movabs_hdu(FitsPtr,IHdu,&HduType,&sta)) {
975 FitsOpenFile::printerror(sta);
976 throw RangeCheckError("FitsImg2DRd::Init: Error moving to requested HDU\n");
977 }
978 if(HduType!=IMAGE_HDU)
979 throw TypeMismatchExc("FitsImg2DRd::Init: HDU not IMAGE_HDU\n");
980
981 // Get NAXIS 1 et 2
982 int nfound=0;
983 if(fits_read_keys_lng(FitsPtr,"NAXIS",1,2,Naxis,&nfound,&sta)) {
984 FitsOpenFile::printerror(sta);
985 throw RangeCheckError("FitsImg2DRd::Init: Error reading NAXIS cards\n");
986 }
987 if(DbgLevel>1)
988 cout<<"...Init(hdu="<<IHdu<<") NAXIS1="<<Naxis[0]<<" NAXIS2="<<Naxis[1]<<" (nfound="<<nfound<<")"<<endl;
989 if(nfound!=2 || Naxis[0]<=0 || Naxis[1]<=0)
990 throw NotAvailableOperation("FitsImg2DRd::Init: bad Naxis[0-1] value\n");
991
992}
993
994/*! Destructor. */
995FitsImg2DRd::~FitsImg2DRd()
996{
997 //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
998 Naxis[0] = Naxis[1] = 0;
999}
1000
1001//////////////////////////////////////////////////////////////
1002/*!
1003 Read a fitsheader key into double
1004 \param keyname : name of the key
1005 \return value into double
1006*/
1007double FitsImg2DRd::ReadKey(char *keyname)
1008{
1009 return FitsOpenFile::ReadKey(FitsPtr,keyname);
1010}
1011
1012/*!
1013 Read a fitsheader key into long
1014 \param keyname : name of the key
1015 \return value into long
1016*/
1017long FitsImg2DRd::ReadKeyL(char *keyname)
1018{
1019 return FitsOpenFile::ReadKeyL(FitsPtr,keyname);
1020}
1021
1022/*!
1023 Read a fitsheader key into string
1024 \param keyname : name of the key
1025 \return value into string
1026*/
1027string FitsImg2DRd::ReadKeyS(char *keyname)
1028{
1029 return FitsOpenFile::ReadKeyS(FitsPtr,keyname);
1030}
1031
1032//////////////////////////////////////////////////////////////
1033/* REMARQUE:
1034 * Si une image FITS a NAXIS1=100 et NAXIS2=50
1035 * alors un tableau 2D juste assez grand pour contenir l'image
1036 * doit etre declare array[50][100] (et non pas array[100][50])
1037 * array[NAXIS2][NAXIS1]
1038 */
1039/*!
1040Read image into a TMatrix<uint_2>
1041\warning TMatrix data(Naxis2,Naxis1)
1042*/
1043long FitsImg2DRd::Read(TMatrix<uint_2>& data)
1044{
1045 int sta=0;
1046 uint_2* arr = new uint_2[Naxis[0]];
1047 data.ReSize(Naxis[1],Naxis[0]);
1048
1049 for(int j=0;j<Naxis[1];j++) {
1050 long deb = j*Naxis[0]+1, nel = Naxis[0];
1051 fits_read_img(FitsPtr,TUSHORT,deb,nel,&NulVal,arr,NULL,&sta);
1052 if(sta) {
1053 FitsOpenFile::printerror(sta); delete [] arr;
1054 throw
1055 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<uint_2>): Error Reading Fits file\n");
1056 }
1057 for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
1058 }
1059
1060 delete [] arr;
1061 return Naxis[0]*Naxis[1];
1062 }
1063
1064/*! Read image into a TMatrix<int_4> */
1065long FitsImg2DRd::Read(TMatrix<int_4>& data)
1066{
1067 int sta=0;
1068 int_4* arr = new int_4[Naxis[0]];
1069 data.ReSize(Naxis[1],Naxis[0]);
1070 int T = (sizeof(long)==4) ? TLONG: TINT;
1071
1072 for(int j=0;j<Naxis[1];j++) {
1073 long deb = j*Naxis[0]+1, nel = Naxis[0];
1074 fits_read_img(FitsPtr,T,deb,nel,&NulVal,arr,NULL,&sta);
1075 if(sta) {
1076 FitsOpenFile::printerror(sta); delete [] arr;
1077 throw
1078 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<int_4>): Error Reading Fits file\n");
1079 }
1080 for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
1081 }
1082
1083 delete [] arr;
1084 return Naxis[0]*Naxis[1];
1085 }
1086
1087/*! Read image into a TMatrix<int_8> */
1088long FitsImg2DRd::Read(TMatrix<int_8>& data)
1089{
1090 int sta=0;
1091 int_8* arr = new int_8[Naxis[0]];
1092 data.ReSize(Naxis[1],Naxis[0]);
1093
1094 for(int j=0;j<Naxis[1];j++) {
1095 long deb = j*Naxis[0]+1, nel = Naxis[0];
1096 fits_read_img(FitsPtr,TLONGLONG,deb,nel,&NulVal,arr,NULL,&sta);
1097 if(sta) {
1098 FitsOpenFile::printerror(sta); delete [] arr;
1099 throw
1100 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<int_8>): Error Reading Fits file\n");
1101 }
1102 for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
1103 }
1104
1105 delete [] arr;
1106 return Naxis[0]*Naxis[1];
1107 }
1108
1109/*! Read image into a TMatrix<float> */
1110long FitsImg2DRd::Read(TMatrix<float>& data)
1111{
1112 int sta=0;
1113 float* arr = new float[Naxis[0]];
1114 data.ReSize(Naxis[1],Naxis[0]);
1115
1116 for(int j=0;j<Naxis[1];j++) {
1117 long deb = j*Naxis[0]+1, nel = Naxis[0];
1118 fits_read_img(FitsPtr,TFLOAT,deb,nel,&NulVal,arr,NULL,&sta);
1119 if(sta) {
1120 FitsOpenFile::printerror(sta); delete [] arr;
1121 throw
1122 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<float>): Error Reading Fits file\n");
1123 }
1124 for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
1125 }
1126
1127 delete [] arr;
1128 return Naxis[0]*Naxis[1];
1129 }
1130
1131/*! Read image into a TMatrix<double> */
1132long FitsImg2DRd::Read(TMatrix<double>& data)
1133{
1134 int sta=0;
1135 double* arr = new double[Naxis[0]];
1136 data.ReSize(Naxis[1],Naxis[0]);
1137
1138 for(int j=0;j<Naxis[1];j++) {
1139 long deb = j*Naxis[0]+1, nel = Naxis[0];
1140 fits_read_img(FitsPtr,TDOUBLE,deb,nel,&NulVal,arr,NULL,&sta);
1141 if(sta) {
1142 FitsOpenFile::printerror(sta); delete [] arr;
1143 throw
1144 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<double>): Error Reading Fits file\n");
1145 }
1146 for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
1147 }
1148
1149 delete [] arr;
1150 return Naxis[0]*Naxis[1];
1151 }
Note: See TracBrowser for help on using the repository browser.