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

Last change on this file since 3123 was 3114, checked in by cmv, 19 years ago

1-/ intro des reader et writer pour tableaux 3D
2-/ j enleve la variable redondante FitsPtr :

si travail OK c est une modif mineure
mais ca peut conduire a de gros PB si qquechose a ete oublie
ATTENTION ATTENTION MODIF .... cmv 13/12/2006

File size: 44.9 KB
RevLine 
[1654]1/* Lecteur de colonne de table Fits (binaire ou ASCII) avec buffer */
[2615]2#include "sopnamsp.h"
[1654]3#include "machdefs.h"
4#include <stdlib.h>
5#include <stdio.h>
6#include "pexceptions.h"
7#include "fabtcolread.h"
[2449]8
9///////////////////////////////////////////////////////////////////
10///////////////////////////////////////////////////////////////////
11///////////////////////////////////////////////////////////////////
12///////////////////////////////////////////////////////////////////
13
14//! Class for opening a FITS file for reading
15
16/*!
17 \class SOPHYA::FitsOpenFile
18 \ingroup FitsIOServer
19 Class for opening a FITS file for reading
20*/
21
22/*!
23 Constructor.
24 \param fname : FITS file name to be opened for reading
25*/
26FitsOpenFile::FitsOpenFile(string fname)
27{
28 Init(fname.c_str());
29}
30
31/*!
32 Constructor.
33 \param cfname : FITS file name to be opened for reading
34*/
35FitsOpenFile::FitsOpenFile(const char *cfname)
36{
37 Init(cfname);
38}
39
40/*!
41 Constructor by default.
42*/
43FitsOpenFile::FitsOpenFile()
44{
45 FitsFN = "";
[2456]46 NHdu = IHdu = HduType = 0;
47 HasBeenPos = false;
[2449]48 FitsPtr = NULL;
49}
50
51/*!
52 Constructor by copy.
53*/
54FitsOpenFile::FitsOpenFile(FitsOpenFile& fof)
55{
[2456]56 Init(fof.FileName().c_str());
[2449]57}
58
59/*!
60 Destructor.
61*/
62FitsOpenFile::~FitsOpenFile()
63{
64 Delete();
65 FitsFN = "";
[2456]66 NHdu = IHdu = HduType = 0;
67 HasBeenPos = false;
[2449]68}
69
70/*!
71 Delete routine called by the destructor.
72*/
73void FitsOpenFile::Delete(void)
74{
75 if(FitsPtr != NULL) {
76 int sta = 0;
77 if(fits_close_file(FitsPtr,&sta)) printerror(sta);
78 FitsPtr = NULL;
79 }
80}
81
82/*! Init routine called by the constructor */
83void FitsOpenFile::Init(const char* fname)
84{
[2456]85 // Parametres Generaux
86 FitsFN = fname;
87 NHdu = IHdu = HduType = 0;
88 HasBeenPos = false;
89 FitsPtr = NULL;
[2449]90
[2456]91 // Ouverture du fichier
92 if(FitsFN.size() <= 0 )
93 throw ParmError("FitsOpenFile::Init: Fits file name error\n");
[2449]94
[2456]95 int sta = 0;
96 if(fits_open_file(&FitsPtr,FitsFN.c_str(),READONLY,&sta)) {
97 printerror(sta);
98 FitsPtr = NULL;
99 throw NullPtrError("FitsOpenFile::Init: Error opening Fits file\n");
100 }
[2449]101
[2456]102 // Get number of hdu
103 if(fits_get_num_hdus(FitsPtr,&NHdu,&sta)) {
104 printerror(sta);
105 NHdu = 0;
106 Delete();
107 throw NotAvailableOperation("FitsOpenFile::Init: Error getting NHdu\n");
108 }
109 if(NHdu<=0) {
110 Delete();
111 throw SzMismatchError("FitsOpenFile::Init: Bad NHdu\n");
112 }
[2449]113
[2456]114 MoveToHDU(1);
115}
116
117/*! Move to an HDU
118\param ihdu: hdu number to move
119\warning ihdu = [1,nhdu]
120\return 0 if positionning failed, ihdu if success
121*/
122int FitsOpenFile::MoveToHDU(int ihdu)
123{
124 if(FitsPtr==NULL)
125 throw NullPtrError("FitsOpenFile::MoveToHDU: no fits file open FitsPtr==NULL\n");
126 int ihdusave = IHdu;
127 if(ihdu<=0) ihdu=1; if(ihdu>NHdu) ihdu=NHdu;
128 int sta=0;
129 if(fits_movabs_hdu(FitsPtr,ihdu,&HduType,&sta)) {
130 printerror(sta);
131 // On se repositionne ou on etait
132 fits_movabs_hdu(FitsPtr,ihdusave,&HduType,&sta);
133 IHdu = ihdusave;
134 } else IHdu = ihdu;
135 return IHdu;
136}
137
138/*! Move to the first HDU of a certain type
139\param hdutype: type of the hdu
140\param hdudeb: start at that hdu
141\return the type of HDU the file is positionned
142*/
143int FitsOpenFile::MoveToFirst(int hdutype,int ihdudeb)
144{
145 if(ihdudeb<=0) ihdudeb=1; if(ihdudeb>NHdu) ihdudeb=NHdu;
146 int ihdusave = IHdu;
147 for(int ihdu=ihdudeb;ihdu<=NHdu;ihdu++) {
148 MoveToHDU(ihdu);
149 if(HduType==hdutype) break;
[2449]150 }
[2456]151 // Si echec, on se repositionne ou on etait
152 if(HduType!=hdutype) MoveToHDU(ihdusave);
153 return HduType;
154}
[2449]155
[2456]156/*! Move to the last HDU of a certain type
157\param hdutype: type of the hdu
158\param hdudeb: stop at that hdu
159\return the type of HDU the file is positionned
160*/
161int FitsOpenFile::MoveToLast(int hdutype,int ihdudeb)
162{
163 if(ihdudeb<=0) ihdudeb=1; if(ihdudeb>NHdu) ihdudeb=NHdu;
164 int ihdusave = IHdu;
165 for(int ihdu=NHdu;ihdu>=ihdudeb;ihdu--) {
166 MoveToHDU(ihdu);
167 if(HduType==hdutype) break;
168 }
169 // Si echec, on se repositionne ou on etait
170 if(HduType!=hdutype) MoveToHDU(ihdusave);
171 return HduType;
172}
173
174/*! Print */
175void FitsOpenFile::Print(void)
176{
177 cout<<"FitsOpenFile::Print: "<<FitsFN
178 <<" hdu="<<IHdu<<"/"<<NHdu<<" type="<<HduType
179 <<" hasbeenpos="<<HasBeenPos<<endl;
180}
181
[2453]182//////////////////////////////////////////////////////////////
183//// Methodes statiques
184//////////////////////////////////////////////////////////////
185/*!
186 Read a fitsheader key into double
187 \param fitsptr : cfitio pointer to Fits file
188 \param keyname : name of the key
189 \return value into double
190*/
191double FitsOpenFile::ReadKey(fitsfile *fitsptr,char *keyname)
192{
[2456]193 if(keyname==NULL || fitsptr==NULL) return 0.;
[2453]194 int sta=0; double val=0.;
195 if(fits_read_key(fitsptr,TDOUBLE,keyname,&val,NULL,&sta))
196 printerror(sta);
197 return val;
198}
199
200/*!
201 Read a fitsheader key into long
202 \param fitsptr : cfitio pointer to Fits file
203 \param keyname : name of the key
204 \return value into long
205*/
206long FitsOpenFile::ReadKeyL(fitsfile *fitsptr,char *keyname)
207{
[2456]208 if(keyname==NULL || fitsptr==NULL) return 0;
[2453]209 int sta=0; long val=0;
210 if(fits_read_key(fitsptr,TLONG,keyname,&val,NULL,&sta))
211 printerror(sta);
212 return val;
213}
214
215/*!
216 Read a fitsheader key into string
217 \param fitsptr : cfitio pointer to Fits file
218 \param keyname : name of the key
219 \return value into string
220*/
221string FitsOpenFile::ReadKeyS(fitsfile *fitsptr,char *keyname)
222{
[2456]223 if(keyname==NULL || fitsptr==NULL) return (string)"";
[2453]224 int sta=0; char val[FLEN_VALUE];
225 if(fits_read_key(fitsptr,TSTRING,keyname,val,NULL,&sta))
226 printerror(sta);
227 string sval = val;
228 return sval;
229}
230
231/*!
232 CFitsIO error printing routine
233 \param sta : cfitio error return code
234*/
235 void FitsOpenFile::printerror(int sta)
[2449]236 {
237 int stat = sta;
238 fits_report_error(stdout,stat);
239 fflush(stdout);
240 return;
241 }
242
243///////////////////////////////////////////////////////////////////
244///////////////////////////////////////////////////////////////////
245///////////////////////////////////////////////////////////////////
246///////////////////////////////////////////////////////////////////
247
248///////////////////////////////////////////////////////////////////
[1654]249//! Class for reading a column in a FITS ASCII or BINARY table
250
251/*!
[2449]252 \class SOPHYA::FitsABTColRd
[1654]253 \ingroup FitsIOServer
[2789]254 Class for reading a column in a FITS ASCII or BINARY table.
255 You can read many columns of the same FITS table by instanciating
256 many FitsABTColRd on the same FitsOpenFile. So, the FITS file is
257 opened only once. Of course the various FitsABTColRd must read
258 the same FITS file HDU.
[1654]259 \verbatim
[1659]260 -- Exemple:
[2449]261 // Open the fits file with FitsOpenFile
262 FitsOpenFile fof = new FitsOpenFile("myfits.fits");
263 // Select the column to be read
264 FitsABTColRd fbt(fof,"BoloMuv_28",0,1000,1,3);
[2456]265 FitsABTColRd fbt2(fof,"BoloMuv_29",0,1000,1,3);
[1654]266 fbt.SetDebug(3);
267 fbt.Print(3);
[1659]268 // Read element by element
[1654]269 for(long i=0;i<fbt.GetNbLine();i++) {
270 double x = fbt.Read(i);
[2456]271 double y = fbt2.Read(i);
272 if(i%lpmod==0) cout<<i<<": "<<x<<", "<<y<<endl;
[1654]273 }
[1659]274 // Read into a vector
275 TVector<double> data;
276 long n = fbt.Read(32,50,data);
277 cout<<"Number of values read: "<<n<<endl;
278 data.ReSize(100);
279 n = fbt.Read(10,-1,data);
280 cout<<"Number of values read: "<<n<<endl;
[2456]281 TVector<double> data2;
282 fbt2.Read(32,50,data);
[2449]283 // Close the fits file
284 delete fof;
[1654]285 \endverbatim
286*/
287
288//////////////////////////////////////////////////////////////
289/*!
290 Constructor.
[2449]291 \param fof : Pointer to the Class for opening the FITS file
[1659]292 \param collabel : label of the column to be read
293 \param ihdu : number of the HDU where the column is.
294 \param blen : read buffer length
295 \param bsens : buffer reading direction
296 \param lp : debug level
[1654]297 \verbatim
[2456]298 - if ihdu<=0 first BINARY or ASCII table is taken
299 - if ihdu>nhdu ihdu is set to nhdu
[1659]300 - bsens>0 read forward
301 bsens<0 read backward
302 bsens==0 read centered
[1654]303 \endverbatim
[1659]304 \warning ihdu = [1,nhdu]
[1654]305*/
[2449]306FitsABTColRd::FitsABTColRd(FitsOpenFile* fof,string collabel
307 ,int ihdu,long blen,long bsens,int lp)
[1654]308{
[2449]309 Init(fof,collabel.c_str(),-1,ihdu,blen,bsens,lp);
[1654]310}
311
312/*!
313 Constructor.
[1659]314 Same as before but the column is identified by its column number
315 \param colnum : number of the column to be read
316 \warning col = [0,ncol[
[1654]317*/
[2449]318FitsABTColRd::FitsABTColRd(FitsOpenFile* fof,int colnum
319 ,int ihdu,long blen,long bsens,int lp)
[1654]320{
[2449]321 Init(fof,"",colnum,ihdu,blen,bsens,lp);
[1654]322}
323
[2449]324/*! Constructor by copy */
325FitsABTColRd::FitsABTColRd(FitsABTColRd& fbt)
[1654]326{
[2449]327 Init(fbt.GetFitsOpenFile(),fbt.GetColLabel().c_str()
[2456]328 ,fbt.GetColNum(),fbt.HDU()
[2449]329 ,fbt.GetBLen(),fbt.GetBSens(),fbt.DbgLevel);
[1654]330}
331
[2449]332/*! Constructor by default */
333FitsABTColRd::FitsABTColRd()
[1654]334{
[2449]335 ColLabel = ""; ColTUnit = ""; ColTForm = "";
336 ColNum = -1; ColTypeCode = 0;
337 NBcol = 0; NBline = 0;
338 SetNulVal(); SetDebug(0);
339 NFitsRead = 0;
[3114]340 FitsOF = NULL;
[2449]341 LineDeb = LineFin = -1;
342 Buffer = NULL;
[1654]343}
344
345/*! Init routine called by the constructor */
[2449]346void FitsABTColRd::Init(FitsOpenFile* fof,const char* collabel,int colnum
[1654]347 ,int ihdu,long blen,long bsens,int lp)
348{
[2449]349 // Initialisation des Parametres Generaux
[2456]350 ColLabel=collabel; ColTUnit=""; ColTForm=""; ColNum=colnum; ColTypeCode=0;
351 NBcol = 0; NBline = 0;
352 SetNulVal(); SetDebug(lp);
[1654]353 NFitsRead = 0;
[3114]354 FitsOF = NULL;
[1654]355 LineDeb = LineFin = -1;
356 Buffer = NULL;
357
[2449]358 // Caracteristiques du FitsOpenFile
359 FitsOF = fof;
[2456]360 if(FitsOF==NULL)
[2449]361 throw NullPtrError("FitsABTColRd::Init: FitsOpenFile pointer is NULL\n");
[2456]362
[3114]363 if(GetFitsPtr()==NULL)
[2449]364 throw NullPtrError("FitsABTColRd::Init: FitsPtr pointer is NULL\n");
[1654]365
[2449]366 int sta = 0;
[2456]367 if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU();
[2449]368
[1654]369 // Get HDU for bin/ascii table
[2456]370 // ATTENTION: le fichier est ouvert mais non positionne sur un HDU,
371 // une classe utilisant ce fichier doit le positionner sur un HDU.
372 // Par contre, si une autre classe utilise ce meme FitsOpenFile,
373 // elle ne peut le positionner que sur ce meme HDU !
374 if(FitsOF->GetPosStatus()==false) {
375 if(ihdu==0) { // find the first BINARY then the first ASCII
376 int rc = FitsOF->MoveToFirst(BINARY_TBL);
377 if(rc!=BINARY_TBL) FitsOF->MoveToFirst(ASCII_TBL);
378 } else {
379 int rc = FitsOF->MoveToHDU(ihdu);
380 if(rc!=ihdu)
381 throw RangeCheckError("FitsABTColRd::Init: Error moving to requested HDU\n");
[1654]382 }
[2456]383 } else { // Fits file has already been positionned
384 if(ihdu>0 && ihdu!=HDU())
385 throw RangeCheckError("FitsABTColRd::Init: file already posit. at another HDU\n");
[1654]386 }
[2456]387
388 // Check HDUType and set position status to TRUE
389 if(HDUType()!=BINARY_TBL && HDUType()!=ASCII_TBL)
[2449]390 throw TypeMismatchExc("FitsABTColRd::Init: HDU not ASCII/BINARY table\n");
[2456]391 if(DbgLevel>1) cout<<"...Init ihdu="<<ihdu<<" HduType="<<HDUType()<<endl;
392 FitsOF->SetPosStatus(true);
[1654]393
394 // Get number of columns
[3114]395 if(fits_get_num_cols(GetFitsPtr(),&NBcol,&sta)) {
[2456]396 FitsOpenFile::printerror(sta);
[2449]397 throw NotAvailableOperation("FitsABTColRd::Init: Error getting number of columns\n");
[1654]398 }
399 if(DbgLevel>1) cout<<"...Init NBcol="<<NBcol<<endl;
[2456]400 if(NBcol<1)
[2449]401 throw RangeCheckError("FitsABTColRd::Init: Bad number of colums\n");
[1654]402
403 // Get number of rows
[3114]404 if(fits_get_num_rows(GetFitsPtr(),&NBline,&sta)) {
[2456]405 FitsOpenFile::printerror(sta);
[2449]406 throw NotAvailableOperation("FitsABTColRd::Init: Error getting number of rows\n");
[1654]407 }
408 if(DbgLevel>1) cout<<"...Init NBline="<<NBline<<endl;
[2456]409 if(NBline<1)
[2449]410 throw RangeCheckError("FitsABTColRd::Init: Bad number of rows\n");
[1654]411
412 // Get column number
[1660]413 char labelcol[128];
[1654]414 if(ColLabel.size() > 0) {
415 strcpy(labelcol,ColLabel.c_str());
[3114]416 if(fits_get_colnum(GetFitsPtr(),CASESEN,labelcol,&ColNum,&sta)) {
[2456]417 FitsOpenFile::printerror(sta);
[2449]418 throw NotAvailableOperation("FitsABTColRd::Init: Error getting column name\n");
[1654]419 }
420 ColNum--; // Convention [0,ncol[
421 }
422 if(DbgLevel>1) cout<<"...Init ColNum="<<ColNum<<endl;
[2456]423 if(ColNum<0 || ColNum>=NBcol)
[2449]424 throw RangeCheckError("FitsABTColRd::Init: Bad column number\n");
[1654]425
426 // Get column type
[3114]427 if(fits_get_coltype(GetFitsPtr(),ColNum+1,&ColTypeCode,NULL,NULL,&sta)) {
[2456]428 FitsOpenFile::printerror(sta);
[2449]429 throw ParmError("FitsABTColRd::Init: Error getting column type\n");
[1654]430 }
431 if(DbgLevel>1) cout<<"...Init ColTypeCode="<<ColTypeCode<<endl;
[1660]432 if(ColTypeCode==TSTRING || ColTypeCode==TCOMPLEX || ColTypeCode==TDBLCOMPLEX
[2456]433 || ColTypeCode<0 )
[2449]434 throw ParmError("FitsABTColRd::Init: Selected column is not Numerical\n");
[1654]435
436 // Get column name back, tunit, tform
[2174]437 char tunit[64], tform[64], tdisp[64];
[2173]438 long repeat=0; double tscale=1., tzero=0.;
[1654]439 int rc=0;
[2456]440 if(HDUType()==BINARY_TBL) {
[3114]441 fits_get_bcolparms(GetFitsPtr(),ColNum+1,labelcol,tunit,tform
[2174]442 ,&repeat,&tscale,&tzero,NULL,tdisp,&sta);
[1654]443 } else {
[3114]444 fits_get_acolparms(GetFitsPtr(),ColNum+1,labelcol,&repeat,tunit,tform
[2174]445 ,&tscale,&tzero,NULL,tdisp,&sta);
[1654]446 }
447 if(rc) {
[2456]448 FitsOpenFile::printerror(sta);
[2449]449 throw RangeCheckError("FitsABTColRd::Init: Error getting the column caracteristics\n");
[1654]450 }
451 ColLabel = labelcol;
452 ColTUnit = tunit;
453 ColTForm = tform;
454
[2456]455 // Set the buffer for reading
456 ChangeBuffer(blen,bsens);
457
[1654]458 if(DbgLevel)
[2449]459 cout<<"FitsABTColRd::Init Num="<<ColNum<<" Label="<<ColLabel
[2173]460 <<" TypeCode="<<ColTypeCode<<" TUnit="<<ColTUnit<<" TForm="<<ColTForm<<endl;
461 if(DbgLevel>1)
[2174]462 cout<<" (repeat="<<repeat<<",tscale="<<tscale<<",tzero="<<tzero
463 <<",tdisp="<<tdisp<<")"<<endl;
[1654]464
465}
466
467/*! Destructor. */
[2449]468FitsABTColRd::~FitsABTColRd()
[1654]469{
470 Delete();
471}
472
[2449]473/*! Delete called by the destructor */
474void FitsABTColRd::Delete(void)
[1814]475{
[2449]476 if(Buffer!=NULL) {delete [] Buffer; Buffer=NULL;}
477 LineDeb = LineFin = -1;
478 //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
[1814]479}
[1654]480
481//////////////////////////////////////////////////////////////
[1659]482/*! Change the buffer caracteristiques (see creator) */
[2449]483void FitsABTColRd::ChangeBuffer(long blen,long bsens)
[1654]484{
[1657]485 long oldnbuffer = NBuffer;
486
487 // Compute buffer caracteristics
[1654]488 BuffLen = (blen<=0)? 1: blen;
489 BuffSens = bsens;
[1657]490 NBuffer = BuffLen;
491 if(bsens==0 && NBuffer%2==0) NBuffer++;
[1654]492
[1657]493 // De-allocate if necessary
[1659]494 if(Buffer!=NULL) {
495 // On des-alloue si pas assez de place
496 // ou si l'ancienne place est beaucoup trop grande (>25%)
497 if(oldnbuffer<NBuffer || (oldnbuffer>NBuffer+long(0.25*NBuffer)) )
498 {delete [] Buffer; Buffer=NULL;}
499 }
[1654]500
[1657]501 // Re-allocate
502 if(Buffer==NULL) Buffer = new double[NBuffer];
503
[1654]504 // Tell program that nothing is into buffer
505 LineDeb = LineFin = -1;
506}
507
[2449]508//////////////////////////////////////////////////////////////
[2451]509/*!
510 Read a fitsheader key into double
511 \param keyname : name of the key
512 \return value into double
513*/
[2449]514double FitsABTColRd::ReadKey(char *keyname)
[1654]515{
[3114]516 return FitsOpenFile::ReadKey(GetFitsPtr(),keyname);
[1654]517}
518
[2451]519/*!
520 Read a fitsheader key into long
521 \param keyname : name of the key
522 \return value into long
523*/
524long FitsABTColRd::ReadKeyL(char *keyname)
525{
[3114]526 return FitsOpenFile::ReadKeyL(GetFitsPtr(),keyname);
[2451]527}
528
529/*!
530 Read a fitsheader key into string
531 \param keyname : name of the key
532 \return value into string
533*/
534string FitsABTColRd::ReadKeyS(char *keyname)
535{
[3114]536 return FitsOpenFile::ReadKeyS(GetFitsPtr(),keyname);
[2451]537}
538
[1654]539/////////////////////////////////////////////////
540/*!
[1659]541 Read row "n" and return the value into a double
542 \warning be carefull for the range: row = [0,NRows[
543 \return value in double
544 \param n : number of the row to be read.
[1654]545 \verbatim
[1659]546 usebuffer == true : use read optimisation with bufferisation
547 == false : no optimisation with bufferisation
548 just read one value
[1654]549 \endverbatim
550*/
[2449]551double FitsABTColRd::Read(long n,bool usebuffer)
[1654]552// Attention: n=nline [0,NBline[, cfistio veut [1,NBline]
553// Attention: colnum [0,NBcol[ , cfistio veut [1,NBcol]
554{
[1659]555 int sta=0;
[1654]556 if(n<0 || n>=NBline)
[2449]557 throw RangeCheckError("FitsABTColRd::Read try to read outside line range\n");
[1654]558
559 // Pas de bufferisation, on lit betement
[1659]560 if(NBuffer==1 || !usebuffer) {
[1654]561 NFitsRead++;
[1659]562 double val;
[3114]563 fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,n+1,1,1,NULL,&val,NULL,&sta);
[1654]564 if(sta) {
[2453]565 FitsOpenFile::printerror(sta);
[2449]566 throw NotAvailableOperation("FitsABTColRd::Read: Error Reading Fits file\n");
[1654]567 }
[1659]568 // On ne remplit Buffer[0] que si on a choisit
569 // un mode de lecture non bufferise (n==1) DES LE DEBUT.
570 // Si on a initialement choisit un mode bufferise (avec n>1),
571 // Buffer contient les valeurs chargees auparavent.
572 // Il ne faut pas faire {Buffer[0]=val; LineDeb=LineFin=n;}
573 // car on perd l'info de ces valeurs.
574 if(NBuffer==1) {Buffer[0]=val; LineDeb=LineFin=n;}
575 return val;
[1654]576 }
577
578 // Gestion avec bufferisation
[1659]579 if(!Buffer)
[2449]580 throw RangeCheckError("FitsABTColRd::Read: Buffer not allocated\n");
[1654]581 if(n<LineDeb || n>LineFin) {
582 NFitsRead++;
583 long row1,row2,nrow;
584 if(BuffSens>0) { // Cas remplissage forward
585 row1 = n+1;
[1657]586 row2 = row1+NBuffer-1; if(row2>NBline) row2 = NBline;
[1654]587 } else if(BuffSens<0) { // Cas remplissage backward
588 row2 = n+1;
[1657]589 row1 = row2-NBuffer+1; if(row1<1) row1 = 1;
[1654]590 } else { // Cas remplissage centre
[1657]591 row1 = n+1 - NBuffer/2; if(row1<1) row1 = 1;
592 row2 = n+1 + NBuffer/2; if(row2>NBline) row2 = NBline;
[1654]593 }
594 nrow = row2 - row1 + 1;
595 LineDeb = row1-1; LineFin = row2-1;
596 //cout<<"DBG-FitsRead: row1="<<row1<<" row2="<<row2<<" nrow="<<nrow
597 // <<" LineDeb,Fin="<<LineDeb<<","<<LineFin<<endl;
[3114]598 fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,row1,1,nrow,NULL,Buffer,NULL,&sta);
[1654]599 if(sta) {
[2453]600 FitsOpenFile::printerror(sta);
[1654]601 LineDeb = LineFin = -1;
[2449]602 throw NotAvailableOperation("FitsABTColRd::Read: Error Reading Fits file\n");
[1654]603 }
604 }
605
606 long ibuf = n-LineDeb;
607 return Buffer[ibuf];
608}
609
610/*!
[1659]611 Read rows from "n1" to "n2" and return the values into TVector of double
612 \return NREAD the number of values read (n2-n1+1).
613 \warning row = [0,NRows[, the routine read [n1,n2]
[1654]614 \verbatim
[1659]615 - if n2<0 then read [n1,n2] where "n2=min(n1+vector_size-1,nrows-1)"
616 - Last row read is ALWAYS: "n2 = n1 + NREAD -1"
617 - The TVector is never resized if not necessary
618 -------------------------------------------------------------------------
619 - ex: suppose the column table contains 10 elements: nrows=10, rows=[0,9]
620
621 TVector<double> V(5);
622 bt.Read(3,5,V) -> read rows=3,4,5 -> V.Size()==5 -> return 3
623 bt.Read(3,-1,V) -> read rows=3,4,5,6,7 -> V.Size()==5 -> return 5
624 bt.Read(7,-1,V) -> read rows=7,8,9 -> V.Size()==5 -> return 3
625 bt.Read(2,-1,V) -> read rows=2,3,4,5,6 -> V.Size()==5 -> return 5
626 bt.Read(-1,5,V) -> throw exception
627
628 TVector<double> V(5);
629 bt.Read(3,99,V) -> read rows=3,4,5,6,7,8,9 -> V.Size()==7 -> return 7
630
631 TVector<double> V(5);
632 bt.Read(2,8,V) -> read rows=2,3,4,5,6,7,8 -> V.Size()==7 -> return 7
633
634 TVector<double> V;
635 bt.Read(3,5,V) -> read rows=3,4,5 -> V.Size()==3 -> return 3
636
637 TVector<double> V;
638 bt.Read(3,-1,V) -> throw exception
639 -------------------------------------------------------------------------
[1654]640 \endverbatim
641*/
[2449]642long FitsABTColRd::Read(long n1,long n2,TVector<double>& data)
[1654]643{
[1659]644 if(n1<0 || n1>=NBline)
[2449]645 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[1659]646 if(data.Size()<=0 && n2<n1)
[2449]647 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[1659]648 if(n2<0) n2 = n1 + data.Size()-1;
649 if(n2>=NBline) n2 = NBline-1;
[1654]650
[1659]651 sa_size_t nread = n2-n1+1;
652 if(data.Size()<nread) data.SetSize(nread);
653
654 //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
655 int sta=0;
[3114]656 fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
[1659]657 if(sta) {
[2453]658 FitsOpenFile::printerror(sta);
[2449]659 throw NotAvailableOperation("FitsABTColRd::Read_TVector<double>: Error Reading Fits file\n");
[1659]660 }
661
662 return nread;
[1654]663}
664
[1659]665/*! idem before but for TVector of float */
[2449]666long FitsABTColRd::Read(long n1,long n2,TVector<float>& 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;
[3114]680 fits_read_col(GetFitsPtr(),TFLOAT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
[1659]681 if(sta) {
[2453]682 FitsOpenFile::printerror(sta);
[2449]683 throw NotAvailableOperation("FitsABTColRd::Read_TVector<float>: Error Reading Fits file\n");
[1659]684 }
685
686 return nread;
687}
688
[2170]689/*! idem before but for TVector of unsigned short */
[2449]690long FitsABTColRd::Read(long n1,long n2,TVector<uint_2>& data)
[2170]691{
692 if(n1<0 || n1>=NBline)
[2449]693 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[2170]694 if(data.Size()<=0 && n2<n1)
[2449]695 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[2170]696 if(n2<0) n2 = n1 + data.Size()-1;
697 if(n2>=NBline) n2 = NBline-1;
698
699 sa_size_t nread = n2-n1+1;
700 if(data.Size()<nread) data.SetSize(nread);
701
702 int sta=0;
[3114]703 fits_read_col(GetFitsPtr(),TUSHORT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
[2170]704 if(sta) {
[2453]705 FitsOpenFile::printerror(sta);
[2449]706 throw NotAvailableOperation("FitsABTColRd::Read_TVector<uint_2>: Error Reading Fits file\n");
[2170]707 }
708
709 return nread;
710}
711
[1659]712/*! idem before but for TVector of int_4 */
[2449]713long FitsABTColRd::Read(long n1,long n2,TVector<int_4>& data)
[1659]714{
715 if(n1<0 || n1>=NBline)
[2449]716 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[1659]717 if(data.Size()<=0 && n2<n1)
[2449]718 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[1659]719 if(n2<0) n2 = n1 + data.Size()-1;
720 if(n2>=NBline) n2 = NBline-1;
721
722 sa_size_t nread = n2-n1+1;
723 if(data.Size()<nread) data.SetSize(nread);
724
725 //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
726 int sta=0;
727 int T = (sizeof(long)==4) ? TLONG: TINT;
[3114]728 fits_read_col(GetFitsPtr(),T,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
[1659]729 if(sta) {
[2453]730 FitsOpenFile::printerror(sta);
[2449]731 throw NotAvailableOperation("FitsABTColRd::Read_TVector<int_4>: Error Reading Fits file\n");
[1659]732 }
733
734 return nread;
735}
736
[2169]737/*! idem before but for TVector of int_8 */
[2449]738long FitsABTColRd::Read(long n1,long n2,TVector<int_8>& data)
[2169]739{
740#ifdef TLONGLONG
741 if(n1<0 || n1>=NBline)
[2449]742 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[2169]743 if(data.Size()<=0 && n2<n1)
[2449]744 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[2169]745 if(n2<0) n2 = n1 + data.Size()-1;
746 if(n2>=NBline) n2 = NBline-1;
747
748 sa_size_t nread = n2-n1+1;
749 if(data.Size()<nread) data.SetSize(nread);
750
751 int sta=0;
[3114]752 fits_read_col(GetFitsPtr(),TLONGLONG,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
[2169]753 if(sta) {
[2453]754 FitsOpenFile::printerror(sta);
[2449]755 throw NotAvailableOperation("FitsABTColRd::Read_TVector<int_8>: Error Reading Fits file\n");
[2169]756 }
757
758 return nread;
759#else
[2449]760 throw PException("FitsABTColRd::Read(..,TVector<int_8>&) Not in that cfitsio version");
[2169]761#endif
762}
763
[1654]764/////////////////////////////////////////////////
[1659]765/*!
766 Return the number of the first row where "val1"<=val<="val2" starting at row "rowstart"
767 \verbatim
768 - The search is performed from "rowstart" to the end
769 in ascending order (from "rowstart" to nrows).
770 - Warning: "rowstart<0" means "rowstart==0" (search all the table column)
771 That is the default
772 \endverbatim
773 \return <0 means not found
774*/
[2449]775long FitsABTColRd::FirstRow(double val1,double val2,long rowstart)
[1659]776{
777 long row = -1;
778 if(NBline==0) return row;
779 // Change buffer for efficiency
780 long bsens=BuffSens; bool bchange=false;
781 if(bsens<=0) {ChangeBuffer(BuffLen,1); bchange=true;}
782 if(rowstart<0) rowstart = 0;
783 if(rowstart>=NBline) rowstart = NBline-1;
784 for(long i=rowstart;i<NBline;i++) {
785 double val = Read(i);
786 if(val<val1 || val>val2) continue;
787 row = i;
788 break;
789 }
790 if(bchange) ChangeBuffer(BuffLen,bsens);
791 return row;
792}
793
794/*!
795 Return the number of the first row where val1<=val<=val2 starting at row rowstart
796 \return <0 means not found
797 \verbatim
798 - The search is performed from "rowstart" to the beginning
799 in descending order (from "rowstart" to 0).
800 - Warning: "rowstart<0" means "rowstart==nrows-1" (search all the table column)
801 That is the default
802 \endverbatim
803*/
[2449]804long FitsABTColRd::LastRow(double val1,double val2,long rowstart)
[1659]805{
806 long row = -1;
807 if(NBline==0) return row;
808 // Change buffer for efficiency
809 long bsens=BuffSens; bool bchange=false;
810 if(bsens>=0) {ChangeBuffer(BuffLen,-1); bchange=true;}
811 if(rowstart<0 || rowstart>=NBline) rowstart = NBline-1;
812 for(long i=rowstart;i>=0;i--) {
813 double val = Read(i);
814 if(val<val1 || val>val2) continue;
815 row = i;
816 break;
817 }
818 if(bchange) ChangeBuffer(BuffLen,bsens);
819 return row;
820}
821
[1654]822/*! Print on stream os */
[2449]823void FitsABTColRd::Print(ostream& os,int lp) const
[1654]824{
[2449]825 os<<"FitsABTColRd:Print ("<<BuffLen<<","<<BuffSens<<","<<NulVal<<")"
[1654]826 <<" ncols="<<NBcol<<" nrows="<<NBline;
827 if(lp>0) os<<" NRead="<<NFitsRead;
[2456]828 os<<"\n... "<<FileName()<<"["<<HDU()<<"/"<<NHDU()<<" type="<<HDUType()<<"]"
829 <<"\n... Label["<<ColNum<<"]="<<ColLabel<<" TypeCode="<<ColTypeCode
[1654]830 <<" TUnit="<<ColTUnit<<" TForm="<<ColTForm
831 <<endl;
832}
[2449]833
834///////////////////////////////////////////////////////////////////
835///////////////////////////////////////////////////////////////////
836///////////////////////////////////////////////////////////////////
837///////////////////////////////////////////////////////////////////
838
839//! Class for reading a column in a FITS ASCII or BINARY table with fits file opening
840
841/*!
842 \class SOPHYA::FitsABTColRead
843 \ingroup FitsIOServer
[2789]844 Class for reading a column in a FITS ASCII or BINARY table with fits file opening.
845 The FITS file is opened each time you instanciate a FitsABTColRead.
846 So reading "n" columns of the same FITS table by instanciating "n"
847 FitsABTColRead, will open "n" times te FITS file.
848 Use FitsABTColRd if you want to open the FITS file only once.
[2449]849 \verbatim
850 -- Exemple:
851 FitsABTColRead fbt("myfits.fits","BoloMuv_28",0,1000,1,3);
852 fbt.SetDebug(3);
853 fbt.Print(3);
854 // Read element by element
855 for(long i=0;i<fbt.GetNbLine();i++) {
856 double x = fbt.Read(i);
857 if(i%lpmod==0) cout<<i<<": "<<x<<endl;
858 }
859 // Read into a vector
860 TVector<double> data;
861 long n = fbt.Read(32,50,data);
862 cout<<"Number of values read: "<<n<<endl;
863 data.ReSize(100);
864 n = fbt.Read(10,-1,data);
865 cout<<"Number of values read: "<<n<<endl;
866 \endverbatim
867*/
868
869
870//////////////////////////////////////////////////////////////
871/*!
872 Constructor.
873 \param fname : FITS file name to be read
874 \param collabel : label of the column to be read
875 \param ihdu : number of the HDU where the column is.
876 \param blen : read buffer length
877 \param bsens : buffer reading direction
878 \param lp : debug level
879 \verbatim
[2456]880 - if ihdu<=0 first BINARY or ASCII table is taken
881 - if ihdu>nhdu ihdu is set to nhdu
[2449]882 - bsens>0 read forward
883 bsens<0 read backward
884 bsens==0 read centered
885 \endverbatim
886 \warning ihdu = [1,nhdu]
887*/
888FitsABTColRead::FitsABTColRead(string fname,string collabel
889 ,int ihdu,long blen,long bsens,int lp)
890: FitsABTColRd(new FitsOpenFile(fname),collabel,ihdu,blen,bsens,lp)
891{
892}
893
894/*!
895 Constructor.
896 Same as before but the column is identified by its column number
897 \param colnum : number of the column to be read
898 \warning col = [0,ncol[
899*/
900FitsABTColRead::FitsABTColRead(string fname,int colnum
901 ,int ihdu,long blen,long bsens,int lp)
902: FitsABTColRd(new FitsOpenFile(fname),colnum,ihdu,blen,bsens,lp)
903{
904}
905
906/*! Constructor. see below */
907FitsABTColRead::FitsABTColRead(const char * cfname,const char* collabel
908 ,int ihdu,long blen,long bsens,int lp)
909: FitsABTColRd(new FitsOpenFile(cfname),collabel,ihdu,blen,bsens,lp)
910{
911}
912
913/*! Constructor. see below */
914FitsABTColRead::FitsABTColRead(const char * cfname,int colnum
915 ,int ihdu,long blen,long bsens,int lp)
916: FitsABTColRd(new FitsOpenFile(cfname),colnum,ihdu,blen,bsens,lp)
917{
918}
919/*! Constructor by default */
920FitsABTColRead::FitsABTColRead()
[2791]921: FitsABTColRd()
[2449]922{
923}
924
925/*! Constructor by copy */
926FitsABTColRead::FitsABTColRead(FitsABTColRead& fbt)
927{
928 // --- ATTENTION ---
929 // FitsABTColRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile
930 FitsOpenFile* fof = new FitsOpenFile(*fbt.GetFitsOpenFile());
931 Init(fof,fbt.GetColLabel().c_str()
[2456]932 ,fbt.GetColNum(),fbt.HDU()
[2449]933 ,fbt.GetBLen(),fbt.GetBSens(),fbt.DbgLevel);
934}
935
936/*! Destructor. */
937FitsABTColRead::~FitsABTColRead()
938{
[2789]939 Delete(); // ?? inutile ??
940 // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS
[2449]941 if(FitsOF!=NULL) delete FitsOF;
942}
[2453]943
944///////////////////////////////////////////////////////////////////
[2791]945///////////////////////////////////////////////////////////////////
946///////////////////////////////////////////////////////////////////
947///////////////////////////////////////////////////////////////////
948
[2453]949//! Class for reading a 2D image from a FITS file
950
951/*!
952 \class SOPHYA::FitsImg2DRd
953 \ingroup FitsIOServer
954 Class for reading a 2D image from a FITS file
955*/
956
957//////////////////////////////////////////////////////////////
958/*!
959 Constructor.
960 \param fof : Pointer to the Class for opening the FITS file
[2456]961 \param ihdu : number of the HDU where the image is.
[2453]962 \param lp : debug level
963 \verbatim
[2456]964 - if ihdu<=0 first IMAGE hdu is taken
965 - if ihdu>nhdu ihdu is set to nhdu
[2453]966 \endverbatim
967 \warning ihdu = [1,nhdu]
968*/
969FitsImg2DRd::FitsImg2DRd(FitsOpenFile* fof,int ihdu,int lp)
970{
971 Init(fof,ihdu,lp);
972}
973
974/*! Constructor by copy */
975FitsImg2DRd::FitsImg2DRd(FitsImg2DRd& fbt)
976{
[2456]977 Init(fbt.GetFitsOpenFile(),fbt.HDU(),fbt.DbgLevel);
[2453]978}
979
980/*! Constructor by default */
981FitsImg2DRd::FitsImg2DRd()
982{
983 Naxis[0] = Naxis[1] = 0;
984 SetNulVal(); SetDebug(0);
[3114]985 FitsOF = NULL;
[2453]986}
987
988/*! Init routine called by the constructor */
989void FitsImg2DRd::Init(FitsOpenFile* fof,int ihdu,int lp)
990{
991 // Initialisation des Parametres Generaux
992 Naxis[0] = Naxis[1] = 0;
993 SetNulVal(); SetDebug(lp);
[3114]994 FitsOF = NULL;
[2453]995
996 // Caracteristiques du FitsOpenFile
997 FitsOF = fof;
998 if(FitsOF==NULL)
999 throw NullPtrError("FitsImg2DRd::Init: FitsOpenFile pointer is NULL\n");
[2456]1000
[3114]1001 if(GetFitsPtr()==NULL)
[2453]1002 throw NullPtrError("FitsImg2DRd::Init: FitsPtr pointer is NULL\n");
1003
1004 int sta = 0;
[2456]1005 if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU();
[2453]1006
1007 // Get HDU 2D image
[2456]1008 // ATTENTION: ... cf blabla equivalent dans FitsABTColRd::Init()
1009 if(FitsOF->GetPosStatus()==false) {
1010 if(ihdu==0) { // find the first IMAGE_HDU
1011 FitsOF->MoveToFirst(IMAGE_HDU);
1012 } else {
1013 int rc = FitsOF->MoveToHDU(ihdu);
1014 if(rc!=ihdu)
[3114]1015 throw RangeCheckError("FitsImg2DRd::Init: Error moving to requested HDU\n");
[2453]1016 }
[2456]1017 } else { // Fits file has already been positionned
1018 if(ihdu>0 && ihdu!=HDU())
[3114]1019 throw RangeCheckError("FitsImg2DRd::Init: file already posit. at another HDU\n");
[2453]1020 }
[2456]1021
1022 // Check HDUType and set position status to TRUE
1023 if(HDUType()!=IMAGE_HDU)
[2453]1024 throw TypeMismatchExc("FitsImg2DRd::Init: HDU not IMAGE_HDU\n");
[2456]1025 FitsOF->SetPosStatus(true);
[2453]1026
1027 // Get NAXIS 1 et 2
1028 int nfound=0;
[3114]1029 if(fits_read_keys_lng(GetFitsPtr(),"NAXIS",1,2,Naxis,&nfound,&sta)) {
[2453]1030 FitsOpenFile::printerror(sta);
1031 throw RangeCheckError("FitsImg2DRd::Init: Error reading NAXIS cards\n");
1032 }
1033 if(DbgLevel>1)
[2456]1034 cout<<"...Init(hdu="<<HDU()<<") NAXIS1="<<Naxis[0]<<" NAXIS2="
1035 <<Naxis[1]<<" (nfound="<<nfound<<")"<<endl;
[2453]1036 if(nfound!=2 || Naxis[0]<=0 || Naxis[1]<=0)
1037 throw NotAvailableOperation("FitsImg2DRd::Init: bad Naxis[0-1] value\n");
1038
1039}
1040
1041/*! Destructor. */
1042FitsImg2DRd::~FitsImg2DRd()
1043{
1044 //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
1045 Naxis[0] = Naxis[1] = 0;
1046}
1047
1048//////////////////////////////////////////////////////////////
1049/*!
1050 Read a fitsheader key into double
1051 \param keyname : name of the key
1052 \return value into double
1053*/
1054double FitsImg2DRd::ReadKey(char *keyname)
1055{
[3114]1056 return FitsOpenFile::ReadKey(GetFitsPtr(),keyname);
[2453]1057}
1058
1059/*!
1060 Read a fitsheader key into long
1061 \param keyname : name of the key
1062 \return value into long
1063*/
1064long FitsImg2DRd::ReadKeyL(char *keyname)
1065{
[3114]1066 return FitsOpenFile::ReadKeyL(GetFitsPtr(),keyname);
[2453]1067}
1068
1069/*!
1070 Read a fitsheader key into string
1071 \param keyname : name of the key
1072 \return value into string
1073*/
1074string FitsImg2DRd::ReadKeyS(char *keyname)
1075{
[3114]1076 return FitsOpenFile::ReadKeyS(GetFitsPtr(),keyname);
[2453]1077}
1078
1079//////////////////////////////////////////////////////////////
1080/* REMARQUE:
1081 * Si une image FITS a NAXIS1=100 et NAXIS2=50
1082 * alors un tableau 2D juste assez grand pour contenir l'image
1083 * doit etre declare array[50][100] (et non pas array[100][50])
1084 * array[NAXIS2][NAXIS1]
1085 */
1086/*!
1087Read image into a TMatrix<uint_2>
1088\warning TMatrix data(Naxis2,Naxis1)
1089*/
[3114]1090size_t FitsImg2DRd::Read(TMatrix<uint_2>& data)
[2453]1091{
1092 int sta=0;
1093 uint_2* arr = new uint_2[Naxis[0]];
1094 data.ReSize(Naxis[1],Naxis[0]);
1095
1096 for(int j=0;j<Naxis[1];j++) {
1097 long deb = j*Naxis[0]+1, nel = Naxis[0];
[3114]1098 fits_read_img(GetFitsPtr(),TUSHORT,deb,nel,&NulVal,arr,NULL,&sta);
[2453]1099 if(sta) {
1100 FitsOpenFile::printerror(sta); delete [] arr;
1101 throw
1102 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<uint_2>): Error Reading Fits file\n");
1103 }
1104 for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
1105 }
1106
1107 delete [] arr;
1108 return Naxis[0]*Naxis[1];
1109 }
1110
1111/*! Read image into a TMatrix<int_4> */
[3114]1112size_t FitsImg2DRd::Read(TMatrix<int_4>& data)
[2453]1113{
1114 int sta=0;
1115 int_4* arr = new int_4[Naxis[0]];
1116 data.ReSize(Naxis[1],Naxis[0]);
1117 int T = (sizeof(long)==4) ? TLONG: TINT;
1118
1119 for(int j=0;j<Naxis[1];j++) {
1120 long deb = j*Naxis[0]+1, nel = Naxis[0];
[3114]1121 fits_read_img(GetFitsPtr(),T,deb,nel,&NulVal,arr,NULL,&sta);
[2453]1122 if(sta) {
1123 FitsOpenFile::printerror(sta); delete [] arr;
1124 throw
1125 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<int_4>): Error Reading Fits file\n");
1126 }
1127 for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
1128 }
1129
1130 delete [] arr;
1131 return Naxis[0]*Naxis[1];
1132 }
1133
1134/*! Read image into a TMatrix<int_8> */
[3114]1135size_t FitsImg2DRd::Read(TMatrix<int_8>& data)
[2453]1136{
1137 int sta=0;
1138 int_8* arr = new int_8[Naxis[0]];
1139 data.ReSize(Naxis[1],Naxis[0]);
1140
1141 for(int j=0;j<Naxis[1];j++) {
1142 long deb = j*Naxis[0]+1, nel = Naxis[0];
[3114]1143 fits_read_img(GetFitsPtr(),TLONGLONG,deb,nel,&NulVal,arr,NULL,&sta);
[2453]1144 if(sta) {
1145 FitsOpenFile::printerror(sta); delete [] arr;
1146 throw
1147 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<int_8>): Error Reading Fits file\n");
1148 }
1149 for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
1150 }
1151
1152 delete [] arr;
1153 return Naxis[0]*Naxis[1];
1154 }
1155
1156/*! Read image into a TMatrix<float> */
[3114]1157size_t FitsImg2DRd::Read(TMatrix<float>& data)
[2453]1158{
1159 int sta=0;
1160 float* arr = new float[Naxis[0]];
1161 data.ReSize(Naxis[1],Naxis[0]);
1162
1163 for(int j=0;j<Naxis[1];j++) {
1164 long deb = j*Naxis[0]+1, nel = Naxis[0];
[3114]1165 fits_read_img(GetFitsPtr(),TFLOAT,deb,nel,&NulVal,arr,NULL,&sta);
[2453]1166 if(sta) {
1167 FitsOpenFile::printerror(sta); delete [] arr;
1168 throw
1169 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<float>): Error Reading Fits file\n");
1170 }
1171 for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
1172 }
1173
1174 delete [] arr;
1175 return Naxis[0]*Naxis[1];
1176 }
1177
1178/*! Read image into a TMatrix<double> */
[3114]1179size_t FitsImg2DRd::Read(TMatrix<double>& data)
[2453]1180{
1181 int sta=0;
1182 double* arr = new double[Naxis[0]];
1183 data.ReSize(Naxis[1],Naxis[0]);
1184
1185 for(int j=0;j<Naxis[1];j++) {
1186 long deb = j*Naxis[0]+1, nel = Naxis[0];
[3114]1187 fits_read_img(GetFitsPtr(),TDOUBLE,deb,nel,&NulVal,arr,NULL,&sta);
[2453]1188 if(sta) {
1189 FitsOpenFile::printerror(sta); delete [] arr;
1190 throw
1191 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<double>): Error Reading Fits file\n");
1192 }
1193 for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
1194 }
1195
1196 delete [] arr;
1197 return Naxis[0]*Naxis[1];
1198 }
[2791]1199
1200///////////////////////////////////////////////////////////////////
1201///////////////////////////////////////////////////////////////////
1202///////////////////////////////////////////////////////////////////
1203///////////////////////////////////////////////////////////////////
1204
1205//! Class for reading a 2D image from a FITS file
1206
1207/*!
1208 \class SOPHYA::FitsImg2DRead
1209 \ingroup FitsIOServer
1210 Class for reading a 2D image from a FITS file
1211*/
1212
1213//////////////////////////////////////////////////////////////
1214/*!
1215 Constructor.
1216 \param fname : name of the FITS file
1217 \param ihdu : number of the HDU where the image is.
1218 \param lp : debug level
1219 \verbatim
1220 - if ihdu<=0 first IMAGE hdu is taken
1221 - if ihdu>nhdu ihdu is set to nhdu
1222 \endverbatim
1223 \warning ihdu = [1,nhdu]
1224*/
1225FitsImg2DRead::FitsImg2DRead(string fname,int ihdu,int lp)
1226: FitsImg2DRd(new FitsOpenFile(fname),ihdu,lp)
1227{
1228}
1229
1230/*! Constructor. see below */
1231FitsImg2DRead::FitsImg2DRead(const char * cfname,int ihdu,int lp)
1232: FitsImg2DRd(new FitsOpenFile(cfname),ihdu,lp)
1233{
1234}
1235
1236/*! Constructor by default */
1237FitsImg2DRead::FitsImg2DRead()
1238: FitsImg2DRd()
1239{
1240}
1241
1242/*! Constructor by copy */
1243FitsImg2DRead::FitsImg2DRead(FitsImg2DRead& fimg)
1244{
1245 // --- ATTENTION ---
1246 // FitsImg2DRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile
1247 FitsOpenFile* fof = new FitsOpenFile(*fimg.GetFitsOpenFile());
1248 Init(fof,fimg.HDU(),fimg.DbgLevel);
1249}
1250
1251/*! Destructor. */
1252FitsImg2DRead::~FitsImg2DRead()
1253{
1254 // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS
1255 if(FitsOF!=NULL) delete FitsOF;
1256}
[3114]1257
1258
1259
1260///////////////////////////////////////////////////////////////////
1261///////////////////////////////////////////////////////////////////
1262///////////////////////////////////////////////////////////////////
1263///////////////////////////////////////////////////////////////////
1264
1265//! Class for reading a 3D image from a FITS file
1266
1267/*!
1268 \class SOPHYA::FitsImg3DRd
1269 \ingroup FitsIOServer
1270 Class for reading a 3D image from a FITS file
1271*/
1272
1273//////////////////////////////////////////////////////////////
1274/*!
1275 Constructor.
1276 \param fof : Pointer to the Class for opening the FITS file
1277 \param ihdu : number of the HDU where the 3D image is.
1278 \param lp : debug level
1279 \verbatim
1280 - if ihdu<=0 first IMAGE hdu is taken
1281 - if ihdu>nhdu ihdu is set to nhdu
1282 \endverbatim
1283 \warning ihdu = [1,nhdu]
1284*/
1285FitsImg3DRd::FitsImg3DRd(FitsOpenFile* fof,int ihdu,int lp)
1286{
1287 Init(fof,ihdu,lp);
1288}
1289
1290/*! Constructor by copy */
1291FitsImg3DRd::FitsImg3DRd(FitsImg3DRd& fbt)
1292{
1293 Init(fbt.GetFitsOpenFile(),fbt.HDU(),fbt.DbgLevel);
1294}
1295
1296/*! Constructor by default */
1297FitsImg3DRd::FitsImg3DRd()
1298{
1299 Naxis[0] = Naxis[1] = Naxis[2] = 0;
1300 SetNulVal(); SetDebug(0);
1301 FitsOF = NULL;
1302}
1303
1304/*! Init routine called by the constructor */
1305void FitsImg3DRd::Init(FitsOpenFile* fof,int ihdu,int lp)
1306{
1307 // Initialisation des Parametres Generaux
1308 Naxis[0] = Naxis[1] = Naxis[2] = 0;
1309 SetNulVal(); SetDebug(lp);
1310 FitsOF = NULL;
1311
1312 // Caracteristiques du FitsOpenFile
1313 FitsOF = fof;
1314 if(FitsOF==NULL)
1315 throw NullPtrError("FitsImg3DRd::Init: FitsOpenFile pointer is NULL\n");
1316
1317 if(GetFitsPtr()==NULL)
1318 throw NullPtrError("FitsImg3DRd::Init: FitsPtr pointer is NULL\n");
1319
1320 int sta = 0;
1321 if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU();
1322
1323 // Get HDU 3D image
1324 // ATTENTION: ... cf blabla equivalent dans FitsABTColRd::Init()
1325 if(FitsOF->GetPosStatus()==false) {
1326 if(ihdu==0) { // find the first IMAGE_HDU
1327 FitsOF->MoveToFirst(IMAGE_HDU);
1328 } else {
1329 int rc = FitsOF->MoveToHDU(ihdu);
1330 if(rc!=ihdu)
1331 throw RangeCheckError("FitsImg3DRd::Init: Error moving to requested HDU\n");
1332 }
1333 } else { // Fits file has already been positionned
1334 if(ihdu>0 && ihdu!=HDU())
1335 throw RangeCheckError("FitsImg3DRd::Init: file already posit. at another HDU\n");
1336 }
1337
1338 // Check HDUType and set position status to TRUE
1339 if(HDUType()!=IMAGE_HDU)
1340 throw TypeMismatchExc("FitsImg3DRd::Init: HDU not IMAGE_HDU\n");
1341 FitsOF->SetPosStatus(true);
1342
1343 // Get NAXIS 1, 2 et 3
1344 int nfound=0;
1345 if(fits_read_keys_lng(GetFitsPtr(),"NAXIS",1,3,Naxis,&nfound,&sta)) {
1346 FitsOpenFile::printerror(sta);
1347 throw RangeCheckError("FitsImg3DRd::Init: Error reading NAXIS cards\n");
1348 }
1349 if(DbgLevel>1)
1350 cout<<"...Init(hdu="<<HDU()<<") NAXIS1="<<Naxis[0]<<" NAXIS2="
1351 <<Naxis[1]<<" NAXIS3="<<Naxis[2]<<" (nfound="<<nfound<<")"<<endl;
1352 if(nfound!=3 || Naxis[0]<=0 || Naxis[1]<=0 || Naxis[2]<=0)
1353 throw NotAvailableOperation("FitsImg3DRd::Init: bad Naxis[0-2] value\n");
1354
1355}
1356
1357/*! Destructor. */
1358FitsImg3DRd::~FitsImg3DRd()
1359{
1360 //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
1361 Naxis[0] = Naxis[1] = Naxis[2] = 0;
1362}
1363
1364//////////////////////////////////////////////////////////////
1365/*!
1366 Read a fitsheader key into double
1367 \param keyname : name of the key
1368 \return value into double
1369*/
1370double FitsImg3DRd::ReadKey(char *keyname)
1371{
1372 return FitsOpenFile::ReadKey(GetFitsPtr(),keyname);
1373}
1374
1375/*!
1376 Read a fitsheader key into long
1377 \param keyname : name of the key
1378 \return value into long
1379*/
1380long FitsImg3DRd::ReadKeyL(char *keyname)
1381{
1382 return FitsOpenFile::ReadKeyL(GetFitsPtr(),keyname);
1383}
1384
1385/*!
1386 Read a fitsheader key into string
1387 \param keyname : name of the key
1388 \return value into string
1389*/
1390string FitsImg3DRd::ReadKeyS(char *keyname)
1391{
1392 return FitsOpenFile::ReadKeyS(GetFitsPtr(),keyname);
1393}
1394
1395//////////////////////////////////////////////////////////////
1396/* REMARQUE:
1397 * Dans TArray A(naxis1,naxis2,naxis3);
1398 * A(i,j,k) -> i varie le plus vite et k le moins vite
1399 */
1400/*!
1401Read 3D image into a TArray<uint_2>
1402*/
1403size_t FitsImg3DRd::Read(TArray<uint_2>& data)
1404{
1405 int sta=0;
1406 uint_2* arr = new uint_2[Naxis[0]];
1407 sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
1408
1409 for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
1410 long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
1411 fits_read_img(GetFitsPtr(),TUSHORT,deb,nel,&NulVal,arr,NULL,&sta);
1412 if(sta) {
1413 FitsOpenFile::printerror(sta); delete [] arr;
1414 throw
1415 NotAvailableOperation("FitsImg3DRd::Read(TArray<uint_2>): Error Reading Fits file\n");
1416 }
1417 for(int i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
1418 }
1419
1420 delete [] arr;
1421 return Naxis[0]*Naxis[1]*Naxis[2];
1422 }
1423
1424/*! Read 3D image into a TArray<int_4> */
1425size_t FitsImg3DRd::Read(TArray<int_4>& data)
1426{
1427 int sta=0;
1428 int_4* arr = new int_4[Naxis[0]];
1429 sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
1430 int T = (sizeof(long)==4) ? TLONG: TINT;
1431
1432 for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
1433 long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
1434 fits_read_img(GetFitsPtr(),T,deb,nel,&NulVal,arr,NULL,&sta);
1435 if(sta) {
1436 FitsOpenFile::printerror(sta); delete [] arr;
1437 throw
1438 NotAvailableOperation("FitsImg3DRd::Read(TArray<int_4>): Error Reading Fits file\n");
1439 }
1440 for(int i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
1441 }
1442
1443 delete [] arr;
1444 return Naxis[0]*Naxis[1]*Naxis[2];
1445 }
1446
1447/*! Read 3D image into a TArray<int_8> */
1448size_t FitsImg3DRd::Read(TArray<int_8>& data)
1449{
1450 int sta=0;
1451 int_8* arr = new int_8[Naxis[0]];
1452 sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
1453
1454 for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
1455 long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
1456 fits_read_img(GetFitsPtr(),TLONGLONG,deb,nel,&NulVal,arr,NULL,&sta);
1457 if(sta) {
1458 FitsOpenFile::printerror(sta); delete [] arr;
1459 throw
1460 NotAvailableOperation("FitsImg3DRd::Read(TArray<int_8>): Error Reading Fits file\n");
1461 }
1462 for(int i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
1463 }
1464
1465 delete [] arr;
1466 return Naxis[0]*Naxis[1]*Naxis[2];
1467 }
1468
1469/*! Read 3D image into a TArray<float> */
1470size_t FitsImg3DRd::Read(TArray<float>& data)
1471{
1472 int sta=0;
1473 float* arr = new float[Naxis[0]];
1474 sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
1475
1476 for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
1477 long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
1478 fits_read_img(GetFitsPtr(),TFLOAT,deb,nel,&NulVal,arr,NULL,&sta);
1479 if(sta) {
1480 FitsOpenFile::printerror(sta); delete [] arr;
1481 throw
1482 NotAvailableOperation("FitsImg3DRd::Read(TArray<float>): Error Reading Fits file\n");
1483 }
1484 for(int i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
1485 }
1486
1487 delete [] arr;
1488 return Naxis[0]*Naxis[1]*Naxis[2];
1489 }
1490
1491/*! Read 3D image into a TArray<double> */
1492size_t FitsImg3DRd::Read(TArray<double>& data)
1493{
1494 int sta=0;
1495 double* arr = new double[Naxis[0]];
1496 sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
1497
1498 for(int k=0;k<Naxis[2];k++) for(int j=0;j<Naxis[1];j++) {
1499 long deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
1500 fits_read_img(GetFitsPtr(),TDOUBLE,deb,nel,&NulVal,arr,NULL,&sta);
1501 if(sta) {
1502 FitsOpenFile::printerror(sta); delete [] arr;
1503 throw
1504 NotAvailableOperation("FitsImg3DRd::Read(TArray<double>): Error Reading Fits file\n");
1505 }
1506 for(int i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
1507 }
1508
1509 delete [] arr;
1510 return Naxis[0]*Naxis[1]*Naxis[2];
1511 }
1512
1513///////////////////////////////////////////////////////////////////
1514///////////////////////////////////////////////////////////////////
1515///////////////////////////////////////////////////////////////////
1516///////////////////////////////////////////////////////////////////
1517
1518//! Class for reading a 3D image from a FITS file
1519
1520/*!
1521 \class SOPHYA::FitsImg3DRead
1522 \ingroup FitsIOServer
1523 Class for reading a 3D image from a FITS file
1524*/
1525
1526//////////////////////////////////////////////////////////////
1527/*!
1528 Constructor.
1529 \param fname : name of the FITS file
1530 \param ihdu : number of the HDU where the 3D image is.
1531 \param lp : debug level
1532 \verbatim
1533 - if ihdu<=0 first IMAGE hdu is taken
1534 - if ihdu>nhdu ihdu is set to nhdu
1535 \endverbatim
1536 \warning ihdu = [1,nhdu]
1537*/
1538FitsImg3DRead::FitsImg3DRead(string fname,int ihdu,int lp)
1539: FitsImg3DRd(new FitsOpenFile(fname),ihdu,lp)
1540{
1541}
1542
1543/*! Constructor. see below */
1544FitsImg3DRead::FitsImg3DRead(const char * cfname,int ihdu,int lp)
1545: FitsImg3DRd(new FitsOpenFile(cfname),ihdu,lp)
1546{
1547}
1548
1549/*! Constructor by default */
1550FitsImg3DRead::FitsImg3DRead()
1551: FitsImg3DRd()
1552{
1553}
1554
1555/*! Constructor by copy */
1556FitsImg3DRead::FitsImg3DRead(FitsImg3DRead& fimg)
1557{
1558 // --- ATTENTION ---
1559 // FitsImg3DRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile
1560 FitsOpenFile* fof = new FitsOpenFile(*fimg.GetFitsOpenFile());
1561 Init(fof,fimg.HDU(),fimg.DbgLevel);
1562}
1563
1564/*! Destructor. */
1565FitsImg3DRead::~FitsImg3DRead()
1566{
1567 // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS
1568 if(FitsOF!=NULL) delete FitsOF;
1569}
Note: See TracBrowser for help on using the repository browser.