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

Last change on this file since 4086 was 4029, checked in by cmv, 14 years ago

suppression lecture Bufferisee de FitsABTColRd (inutile: cfitsio gere bufferisation en interne et complication code), cmv 22/10/2011

File size: 60.5 KB
RevLine 
[4029]1/* Lecteur de colonne de table Fits (binaire ou ASCII) */
[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*/
[3572]191double FitsOpenFile::ReadKey(fitsfile *fitsptr,const char *keyname)
[2453]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*/
[3572]206long FitsOpenFile::ReadKeyL(fitsfile *fitsptr,const char *keyname)
[2453]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/*!
[3128]216 Read a fitsheader key into long
217 \param fitsptr : cfitio pointer to Fits file
218 \param keyname : name of the key
219 \return value into long long
220*/
[3572]221LONGLONG FitsOpenFile::ReadKeyLL(fitsfile *fitsptr,const char *keyname)
[3128]222{
223 if(keyname==NULL || fitsptr==NULL) return 0;
224 int sta=0; LONGLONG val=0;
225 if(fits_read_key(fitsptr,TLONGLONG,keyname,&val,NULL,&sta))
226 printerror(sta);
227 return val;
228}
229
230/*!
[2453]231 Read a fitsheader key into string
232 \param fitsptr : cfitio pointer to Fits file
233 \param keyname : name of the key
234 \return value into string
235*/
[3572]236string FitsOpenFile::ReadKeyS(fitsfile *fitsptr,const char *keyname)
[2453]237{
[2456]238 if(keyname==NULL || fitsptr==NULL) return (string)"";
[2453]239 int sta=0; char val[FLEN_VALUE];
240 if(fits_read_key(fitsptr,TSTRING,keyname,val,NULL,&sta))
241 printerror(sta);
242 string sval = val;
243 return sval;
244}
245
246/*!
247 CFitsIO error printing routine
248 \param sta : cfitio error return code
249*/
250 void FitsOpenFile::printerror(int sta)
[2449]251 {
252 int stat = sta;
253 fits_report_error(stdout,stat);
254 fflush(stdout);
255 return;
256 }
257
258///////////////////////////////////////////////////////////////////
259///////////////////////////////////////////////////////////////////
260///////////////////////////////////////////////////////////////////
261///////////////////////////////////////////////////////////////////
262
263///////////////////////////////////////////////////////////////////
[1654]264//! Class for reading a column in a FITS ASCII or BINARY table
265
266/*!
[2449]267 \class SOPHYA::FitsABTColRd
[1654]268 \ingroup FitsIOServer
[2789]269 Class for reading a column in a FITS ASCII or BINARY table.
270 You can read many columns of the same FITS table by instanciating
271 many FitsABTColRd on the same FitsOpenFile. So, the FITS file is
272 opened only once. Of course the various FitsABTColRd must read
273 the same FITS file HDU.
[1654]274 \verbatim
[1659]275 -- Exemple:
[2449]276 // Open the fits file with FitsOpenFile
277 FitsOpenFile fof = new FitsOpenFile("myfits.fits");
278 // Select the column to be read
279 FitsABTColRd fbt(fof,"BoloMuv_28",0,1000,1,3);
[2456]280 FitsABTColRd fbt2(fof,"BoloMuv_29",0,1000,1,3);
[1654]281 fbt.SetDebug(3);
282 fbt.Print(3);
[1659]283 // Read element by element
[3128]284 for(LONGLONG i=0;i<fbt.GetNbLine();i++) {
[1654]285 double x = fbt.Read(i);
[2456]286 double y = fbt2.Read(i);
287 if(i%lpmod==0) cout<<i<<": "<<x<<", "<<y<<endl;
[1654]288 }
[1659]289 // Read into a vector
290 TVector<double> data;
[3128]291 LONGLONG n = fbt.Read(32,50,data);
[1659]292 cout<<"Number of values read: "<<n<<endl;
293 data.ReSize(100);
294 n = fbt.Read(10,-1,data);
295 cout<<"Number of values read: "<<n<<endl;
[2456]296 TVector<double> data2;
297 fbt2.Read(32,50,data);
[2449]298 // Close the fits file
299 delete fof;
[1654]300 \endverbatim
301*/
302
303//////////////////////////////////////////////////////////////
304/*!
305 Constructor.
[2449]306 \param fof : Pointer to the Class for opening the FITS file
[1659]307 \param collabel : label of the column to be read
308 \param ihdu : number of the HDU where the column is.
309 \param lp : debug level
[1654]310 \verbatim
[2456]311 - if ihdu<=0 first BINARY or ASCII table is taken
312 - if ihdu>nhdu ihdu is set to nhdu
[1654]313 \endverbatim
[1659]314 \warning ihdu = [1,nhdu]
[1654]315*/
[4029]316FitsABTColRd::FitsABTColRd(FitsOpenFile* fof,string collabel,int ihdu,int lp)
[1654]317{
[4029]318 Init(fof,collabel.c_str(),-1,ihdu,lp);
[1654]319}
320
321/*!
322 Constructor.
[1659]323 Same as before but the column is identified by its column number
324 \param colnum : number of the column to be read
325 \warning col = [0,ncol[
[1654]326*/
[4029]327FitsABTColRd::FitsABTColRd(FitsOpenFile* fof,int colnum,int ihdu,int lp)
[1654]328{
[4029]329 Init(fof,"",colnum,ihdu,lp);
[1654]330}
331
[2449]332/*! Constructor by copy */
333FitsABTColRd::FitsABTColRd(FitsABTColRd& fbt)
[1654]334{
[4029]335 Init(fbt.GetFitsOpenFile(),fbt.GetColLabel().c_str(),fbt.GetColNum(),fbt.HDU(),fbt.DbgLevel);
[1654]336}
337
[2449]338/*! Constructor by default */
339FitsABTColRd::FitsABTColRd()
[1654]340{
[2449]341 ColLabel = ""; ColTUnit = ""; ColTForm = "";
[4025]342 ColNum = -1; ColTypeCode = 0; ColRepeat=0; ColDispWidth=0;
343 StrBuff = NULL;
[2449]344 NBcol = 0; NBline = 0;
345 SetNulVal(); SetDebug(0);
346 NFitsRead = 0;
[3114]347 FitsOF = NULL;
[1654]348}
349
350/*! Init routine called by the constructor */
[4029]351void FitsABTColRd::Init(FitsOpenFile* fof,const char* collabel,int colnum,int ihdu,int lp)
[1654]352{
[2449]353 // Initialisation des Parametres Generaux
[4025]354 ColLabel=collabel; ColTUnit=""; ColTForm="";
355 ColNum=colnum; ColTypeCode=0; ColRepeat=0; ColDispWidth=0;
356 StrBuff = NULL;
[2456]357 NBcol = 0; NBline = 0;
358 SetNulVal(); SetDebug(lp);
[1654]359 NFitsRead = 0;
[3114]360 FitsOF = NULL;
[1654]361
[2449]362 // Caracteristiques du FitsOpenFile
363 FitsOF = fof;
[2456]364 if(FitsOF==NULL)
[2449]365 throw NullPtrError("FitsABTColRd::Init: FitsOpenFile pointer is NULL\n");
[2456]366
[3114]367 if(GetFitsPtr()==NULL)
[2449]368 throw NullPtrError("FitsABTColRd::Init: FitsPtr pointer is NULL\n");
[1654]369
[2449]370 int sta = 0;
[2456]371 if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU();
[2449]372
[1654]373 // Get HDU for bin/ascii table
[2456]374 // ATTENTION: le fichier est ouvert mais non positionne sur un HDU,
375 // une classe utilisant ce fichier doit le positionner sur un HDU.
376 // Par contre, si une autre classe utilise ce meme FitsOpenFile,
377 // elle ne peut le positionner que sur ce meme HDU !
378 if(FitsOF->GetPosStatus()==false) {
379 if(ihdu==0) { // find the first BINARY then the first ASCII
380 int rc = FitsOF->MoveToFirst(BINARY_TBL);
381 if(rc!=BINARY_TBL) FitsOF->MoveToFirst(ASCII_TBL);
382 } else {
383 int rc = FitsOF->MoveToHDU(ihdu);
384 if(rc!=ihdu)
385 throw RangeCheckError("FitsABTColRd::Init: Error moving to requested HDU\n");
[1654]386 }
[2456]387 } else { // Fits file has already been positionned
388 if(ihdu>0 && ihdu!=HDU())
389 throw RangeCheckError("FitsABTColRd::Init: file already posit. at another HDU\n");
[1654]390 }
[2456]391
392 // Check HDUType and set position status to TRUE
393 if(HDUType()!=BINARY_TBL && HDUType()!=ASCII_TBL)
[2449]394 throw TypeMismatchExc("FitsABTColRd::Init: HDU not ASCII/BINARY table\n");
[2456]395 if(DbgLevel>1) cout<<"...Init ihdu="<<ihdu<<" HduType="<<HDUType()<<endl;
396 FitsOF->SetPosStatus(true);
[1654]397
398 // Get number of columns
[3114]399 if(fits_get_num_cols(GetFitsPtr(),&NBcol,&sta)) {
[2456]400 FitsOpenFile::printerror(sta);
[2449]401 throw NotAvailableOperation("FitsABTColRd::Init: Error getting number of columns\n");
[1654]402 }
403 if(DbgLevel>1) cout<<"...Init NBcol="<<NBcol<<endl;
[2456]404 if(NBcol<1)
[2449]405 throw RangeCheckError("FitsABTColRd::Init: Bad number of colums\n");
[1654]406
407 // Get number of rows
[3128]408 if(fits_get_num_rowsll(GetFitsPtr(),&NBline,&sta)) {
[2456]409 FitsOpenFile::printerror(sta);
[2449]410 throw NotAvailableOperation("FitsABTColRd::Init: Error getting number of rows\n");
[1654]411 }
412 if(DbgLevel>1) cout<<"...Init NBline="<<NBline<<endl;
[2456]413 if(NBline<1)
[2449]414 throw RangeCheckError("FitsABTColRd::Init: Bad number of rows\n");
[1654]415
416 // Get column number
[1660]417 char labelcol[128];
[1654]418 if(ColLabel.size() > 0) {
419 strcpy(labelcol,ColLabel.c_str());
[3114]420 if(fits_get_colnum(GetFitsPtr(),CASESEN,labelcol,&ColNum,&sta)) {
[2456]421 FitsOpenFile::printerror(sta);
[3660]422 throw NotAvailableOperation("FitsABTColRd::Init: Error getting column number\n");
[1654]423 }
424 ColNum--; // Convention [0,ncol[
425 }
426 if(DbgLevel>1) cout<<"...Init ColNum="<<ColNum<<endl;
[2456]427 if(ColNum<0 || ColNum>=NBcol)
[2449]428 throw RangeCheckError("FitsABTColRd::Init: Bad column number\n");
[1654]429
430 // Get column type
[3128]431 if(fits_get_coltypell(GetFitsPtr(),ColNum+1,&ColTypeCode,NULL,NULL,&sta)) {
[2456]432 FitsOpenFile::printerror(sta);
[2449]433 throw ParmError("FitsABTColRd::Init: Error getting column type\n");
[1654]434 }
435 if(DbgLevel>1) cout<<"...Init ColTypeCode="<<ColTypeCode<<endl;
[4025]436 if(ColTypeCode<0 )
437 throw ParmError("FitsABTColRd::Init: Selected column type decoding not implemented\n");
[1654]438
439 // Get column name back, tunit, tform
[2174]440 char tunit[64], tform[64], tdisp[64];
[3128]441 LONGLONG repeat=0; double tscale=1., tzero=0.;
[1654]442 int rc=0;
[2456]443 if(HDUType()==BINARY_TBL) {
[4025]444 rc = fits_get_bcolparmsll(GetFitsPtr(),ColNum+1,labelcol,tunit,tform
445 ,&repeat,&tscale,&tzero,NULL,tdisp,&sta);
[1654]446 } else {
[3128]447 long repeatlng;
[4025]448 rc = fits_get_acolparms(GetFitsPtr(),ColNum+1,labelcol,&repeatlng,tunit,tform
449 ,&tscale,&tzero,NULL,tdisp,&sta);
[3128]450 repeat = repeatlng;
[1654]451 }
452 if(rc) {
[2456]453 FitsOpenFile::printerror(sta);
[2449]454 throw RangeCheckError("FitsABTColRd::Init: Error getting the column caracteristics\n");
[1654]455 }
456 ColLabel = labelcol;
457 ColTUnit = tunit;
458 ColTForm = tform;
[4023]459 ColRepeat = repeat;
[1654]460
[4025]461 fits_get_col_display_width(GetFitsPtr(),ColNum+1,&ColDispWidth,&sta);
462
[1654]463 if(DbgLevel)
[2449]464 cout<<"FitsABTColRd::Init Num="<<ColNum<<" Label="<<ColLabel
[4025]465 <<" TypeCode="<<ColTypeCode<<" TUnit="<<ColTUnit<<" TForm="<<ColTForm
466 <<" Repeat="<<ColRepeat<<" DispWidth="<<ColDispWidth<<endl;
[2173]467 if(DbgLevel>1)
[4023]468 cout<<" (tscale="<<tscale<<",tzero="<<tzero<<",tdisp="<<tdisp<<")"<<endl;
[1654]469
470}
471
472/*! Destructor. */
[2449]473FitsABTColRd::~FitsABTColRd()
[1654]474{
475 Delete();
476}
477
[2449]478/*! Delete called by the destructor */
479void FitsABTColRd::Delete(void)
[1814]480{
[4025]481 if(StrBuff!=NULL) {delete [] StrBuff; StrBuff=NULL;}
[2449]482 //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
[1814]483}
[1654]484
485//////////////////////////////////////////////////////////////
[2451]486/*!
487 Read a fitsheader key into double
488 \param keyname : name of the key
489 \return value into double
490*/
[3572]491double FitsABTColRd::ReadKey(const char *keyname)
[1654]492{
[3114]493 return FitsOpenFile::ReadKey(GetFitsPtr(),keyname);
[1654]494}
495
[2451]496/*!
497 Read a fitsheader key into long
498 \param keyname : name of the key
499 \return value into long
500*/
[3572]501long FitsABTColRd::ReadKeyL(const char *keyname)
[2451]502{
[3114]503 return FitsOpenFile::ReadKeyL(GetFitsPtr(),keyname);
[2451]504}
505
506/*!
[3128]507 Read a fitsheader key into long long
508 \param keyname : name of the key
509 \return value into long long
510*/
[3572]511LONGLONG FitsABTColRd::ReadKeyLL(const char *keyname)
[3128]512{
513 return FitsOpenFile::ReadKeyLL(GetFitsPtr(),keyname);
514}
515
516/*!
[2451]517 Read a fitsheader key into string
518 \param keyname : name of the key
519 \return value into string
520*/
[3572]521string FitsABTColRd::ReadKeyS(const char *keyname)
[2451]522{
[3114]523 return FitsOpenFile::ReadKeyS(GetFitsPtr(),keyname);
[2451]524}
525
[1654]526/////////////////////////////////////////////////
527/*!
[4023]528 Read row "n" element "nfirstel" and return the value into a double
[1659]529 \warning be carefull for the range: row = [0,NRows[
530 \return value in double
531 \param n : number of the row to be read.
[1654]532*/
[4029]533double FitsABTColRd::Read(LONGLONG n,long nfirstel)
[1654]534// Attention: n=nline [0,NBline[, cfistio veut [1,NBline]
535// Attention: colnum [0,NBcol[ , cfistio veut [1,NBcol]
[4023]536// Attention: nfirstel [0,ColRepeat[ , cfistio veut [1,ColRepeat]
[1654]537{
[1659]538 int sta=0;
[1654]539 if(n<0 || n>=NBline)
[2449]540 throw RangeCheckError("FitsABTColRd::Read try to read outside line range\n");
[4023]541 if(nfirstel<0 || nfirstel>=ColRepeat)
542 throw RangeCheckError("FitsABTColRd::Read try to read outside element range: nfirstel>repeat\n");
[1654]543
[4029]544 double val;
545 fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,n+1,nfirstel+1,1,NULL,&val,NULL,&sta);
546 if(sta) {
547 FitsOpenFile::printerror(sta);
548 throw NotAvailableOperation("FitsABTColRd::Read: Error Reading Fits file\n");
[1654]549 }
[4029]550 NFitsRead++;
551 return val;
552}
[1654]553
[4029]554/*!
555 Read row "n" element "nfirstel" and return a long long int
556*/
557int_8 FitsABTColRd::ReadLL(LONGLONG n,long nfirstel)
558{
559 int sta=0;
560 if(n<0 || n>=NBline)
561 throw RangeCheckError("FitsABTColRd::ReadLL try to read outside line range\n");
562 if(nfirstel<0 || nfirstel>=ColRepeat)
563 throw RangeCheckError("FitsABTColRd::ReadLL try to read outside element range: nfirstel>repeat\n");
564
565 int_8 val;
566 fits_read_col(GetFitsPtr(),TLONGLONG,ColNum+1,n+1,nfirstel+1,1,NULL,&val,NULL,&sta);
567 if(sta) {
568 FitsOpenFile::printerror(sta);
569 throw NotAvailableOperation("FitsABTColRd::ReadLL: Error Reading Fits file\n");
[1654]570 }
[4029]571 NFitsRead++;
572 return val;
[1654]573}
574
575/*!
[4025]576 Read row "n" element "nfirstel" and return a complex value
577*/
578complex<r_8> FitsABTColRd::ReadComplex(LONGLONG n,long nfirstel)
579{
580 int sta=0;
581 if(n<0 || n>=NBline)
582 throw RangeCheckError("FitsABTColRd::ReadComplex try to read outside line range\n");
583 if(nfirstel<0 || nfirstel>=ColRepeat)
584 throw RangeCheckError("FitsABTColRd::ReadComplex try to read outside element range: nfirstel>repeat\n");
585
586 r_8 val[2];
587 fits_read_col(GetFitsPtr(),TDBLCOMPLEX,ColNum+1,n+1,nfirstel+1,1,NULL,val,NULL,&sta);
588 if(sta) {
589 FitsOpenFile::printerror(sta);
590 throw NotAvailableOperation("FitsABTColRd::ReadComplex: Error Reading Fits file\n");
591 }
[4029]592 NFitsRead++;
[4025]593 return complex<r_8>(val[0],val[1]);
594}
595
596/*!
597 Read row "n" element "nfirstel" and return in string (internal pointer)
598*/
599char* FitsABTColRd::ReadInStr(LONGLONG n,long nfirstel)
600{
601 int sta=0;
602 if(n<0 || n>=NBline)
603 throw RangeCheckError("FitsABTColRd::ReadInStr try to read outside line range\n");
604 if(nfirstel<0 || nfirstel>=ColRepeat)
605 throw RangeCheckError("FitsABTColRd::ReadInStr try to read outside element range: nfirstel>repeat\n");
606
607 if(StrBuff==NULL) StrBuff= new char[ColDispWidth+5];
608 char nullstr[]="";
609
610 fits_read_col(GetFitsPtr(),TSTRING,ColNum+1,n+1,nfirstel+1,1,nullstr,&StrBuff,NULL,&sta);
611 if(sta) {
612 FitsOpenFile::printerror(sta);
613 throw NotAvailableOperation("FitsABTColRd::ReadInStr: Error Reading Fits file\n");
614 }
[4029]615 NFitsRead++;
[4025]616
617 return StrBuff;
618}
619
620/*!
[1659]621 Read rows from "n1" to "n2" and return the values into TVector of double
622 \return NREAD the number of values read (n2-n1+1).
623 \warning row = [0,NRows[, the routine read [n1,n2]
[1654]624 \verbatim
[1659]625 - if n2<0 then read [n1,n2] where "n2=min(n1+vector_size-1,nrows-1)"
626 - Last row read is ALWAYS: "n2 = n1 + NREAD -1"
627 - The TVector is never resized if not necessary
628 -------------------------------------------------------------------------
629 - ex: suppose the column table contains 10 elements: nrows=10, rows=[0,9]
630
631 TVector<double> V(5);
632 bt.Read(3,5,V) -> read rows=3,4,5 -> V.Size()==5 -> return 3
633 bt.Read(3,-1,V) -> read rows=3,4,5,6,7 -> V.Size()==5 -> return 5
634 bt.Read(7,-1,V) -> read rows=7,8,9 -> V.Size()==5 -> return 3
635 bt.Read(2,-1,V) -> read rows=2,3,4,5,6 -> V.Size()==5 -> return 5
636 bt.Read(-1,5,V) -> throw exception
637
638 TVector<double> V(5);
639 bt.Read(3,99,V) -> read rows=3,4,5,6,7,8,9 -> V.Size()==7 -> return 7
640
641 TVector<double> V(5);
642 bt.Read(2,8,V) -> read rows=2,3,4,5,6,7,8 -> V.Size()==7 -> return 7
643
644 TVector<double> V;
645 bt.Read(3,5,V) -> read rows=3,4,5 -> V.Size()==3 -> return 3
646
647 TVector<double> V;
648 bt.Read(3,-1,V) -> throw exception
649 -------------------------------------------------------------------------
[1654]650 \endverbatim
651*/
[3128]652LONGLONG FitsABTColRd::Read(LONGLONG n1,LONGLONG n2,TVector<double>& data)
[1654]653{
[4023]654 if(ColRepeat>1)
655 throw RangeCheckError("FitsABTColRd::Read TVector not-implemented for repeat>1 \n");
[1659]656 if(n1<0 || n1>=NBline)
[2449]657 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[1659]658 if(data.Size()<=0 && n2<n1)
[2449]659 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[1659]660 if(n2<0) n2 = n1 + data.Size()-1;
661 if(n2>=NBline) n2 = NBline-1;
[1654]662
[3128]663 LONGLONG nread = n2-n1+1;
[1659]664 if(data.Size()<nread) data.SetSize(nread);
665
[3128]666 //for(LONGLONG i=n1;i<=n2;i++) data(i-n1) = Read(i);
[1659]667 int sta=0;
[3114]668 fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
[1659]669 if(sta) {
[2453]670 FitsOpenFile::printerror(sta);
[2449]671 throw NotAvailableOperation("FitsABTColRd::Read_TVector<double>: Error Reading Fits file\n");
[1659]672 }
[4029]673 NFitsRead += nread;
[1659]674
675 return nread;
[1654]676}
677
[1659]678/*! idem before but for TVector of float */
[3128]679LONGLONG FitsABTColRd::Read(LONGLONG n1,LONGLONG n2,TVector<float>& data)
[1659]680{
[4023]681 if(ColRepeat>1)
682 throw RangeCheckError("FitsABTColRd::Read TVector not-implemented for repeat>1 \n");
[1659]683 if(n1<0 || n1>=NBline)
[2449]684 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[1659]685 if(data.Size()<=0 && n2<n1)
[2449]686 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[1659]687 if(n2<0) n2 = n1 + data.Size()-1;
688 if(n2>=NBline) n2 = NBline-1;
689
[3128]690 LONGLONG nread = n2-n1+1;
[1659]691 if(data.Size()<nread) data.SetSize(nread);
692
[3128]693 //for(LONGLONG i=n1;i<=n2;i++) data(i-n1) = Read(i);
[1659]694 int sta=0;
[3114]695 fits_read_col(GetFitsPtr(),TFLOAT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
[1659]696 if(sta) {
[2453]697 FitsOpenFile::printerror(sta);
[2449]698 throw NotAvailableOperation("FitsABTColRd::Read_TVector<float>: Error Reading Fits file\n");
[1659]699 }
[4029]700 NFitsRead += nread;
[1659]701
702 return nread;
703}
704
[2170]705/*! idem before but for TVector of unsigned short */
[3128]706LONGLONG FitsABTColRd::Read(LONGLONG n1,LONGLONG n2,TVector<uint_2>& data)
[2170]707{
[4023]708 if(ColRepeat>1)
709 throw RangeCheckError("FitsABTColRd::Read TVector not-implemented for repeat>1 \n");
[2170]710 if(n1<0 || n1>=NBline)
[2449]711 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[2170]712 if(data.Size()<=0 && n2<n1)
[2449]713 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[2170]714 if(n2<0) n2 = n1 + data.Size()-1;
715 if(n2>=NBline) n2 = NBline-1;
716
[3128]717 LONGLONG nread = n2-n1+1;
[2170]718 if(data.Size()<nread) data.SetSize(nread);
719
720 int sta=0;
[3114]721 fits_read_col(GetFitsPtr(),TUSHORT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
[2170]722 if(sta) {
[2453]723 FitsOpenFile::printerror(sta);
[2449]724 throw NotAvailableOperation("FitsABTColRd::Read_TVector<uint_2>: Error Reading Fits file\n");
[2170]725 }
[4029]726 NFitsRead += nread;
[2170]727
728 return nread;
729}
730
[1659]731/*! idem before but for TVector of int_4 */
[3128]732LONGLONG FitsABTColRd::Read(LONGLONG n1,LONGLONG n2,TVector<int_4>& data)
[1659]733{
[4023]734 if(ColRepeat>1)
735 throw RangeCheckError("FitsABTColRd::Read TVector not-implemented for repeat>1 \n");
[1659]736 if(n1<0 || n1>=NBline)
[2449]737 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[1659]738 if(data.Size()<=0 && n2<n1)
[2449]739 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[1659]740 if(n2<0) n2 = n1 + data.Size()-1;
741 if(n2>=NBline) n2 = NBline-1;
742
[3128]743 LONGLONG nread = n2-n1+1;
[1659]744 if(data.Size()<nread) data.SetSize(nread);
745
[3128]746 //for(LONGLONG i=n1;i<=n2;i++) data(i-n1) = Read(i);
[1659]747 int sta=0;
748 int T = (sizeof(long)==4) ? TLONG: TINT;
[3114]749 fits_read_col(GetFitsPtr(),T,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
[1659]750 if(sta) {
[2453]751 FitsOpenFile::printerror(sta);
[2449]752 throw NotAvailableOperation("FitsABTColRd::Read_TVector<int_4>: Error Reading Fits file\n");
[1659]753 }
[4029]754 NFitsRead += nread;
[1659]755
756 return nread;
757}
758
[2169]759/*! idem before but for TVector of int_8 */
[3128]760LONGLONG FitsABTColRd::Read(LONGLONG n1,LONGLONG n2,TVector<int_8>& data)
[2169]761{
762#ifdef TLONGLONG
[4023]763 if(ColRepeat>1)
764 throw RangeCheckError("FitsABTColRd::Read TVector not-implemented for repeat>1 \n");
[2169]765 if(n1<0 || n1>=NBline)
[2449]766 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
[2169]767 if(data.Size()<=0 && n2<n1)
[2449]768 throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
[2169]769 if(n2<0) n2 = n1 + data.Size()-1;
770 if(n2>=NBline) n2 = NBline-1;
771
[3128]772 LONGLONG nread = n2-n1+1;
[2169]773 if(data.Size()<nread) data.SetSize(nread);
774
775 int sta=0;
[3114]776 fits_read_col(GetFitsPtr(),TLONGLONG,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
[2169]777 if(sta) {
[2453]778 FitsOpenFile::printerror(sta);
[2449]779 throw NotAvailableOperation("FitsABTColRd::Read_TVector<int_8>: Error Reading Fits file\n");
[2169]780 }
[4029]781 NFitsRead += nread;
[2169]782
783 return nread;
784#else
[2449]785 throw PException("FitsABTColRd::Read(..,TVector<int_8>&) Not in that cfitsio version");
[2169]786#endif
787}
788
[1654]789/////////////////////////////////////////////////
[1659]790/*!
791 Return the number of the first row where "val1"<=val<="val2" starting at row "rowstart"
[4023]792 for element "nfirstel"
[1659]793 \verbatim
794 - The search is performed from "rowstart" to the end
795 in ascending order (from "rowstart" to nrows).
796 - Warning: "rowstart<0" means "rowstart==0" (search all the table column)
797 That is the default
798 \endverbatim
799 \return <0 means not found
800*/
[4023]801LONGLONG FitsABTColRd::FirstRow(long nfirstel,double val1,double val2,LONGLONG rowstart)
[1659]802{
[4023]803 if(nfirstel<0 || nfirstel>=ColRepeat)
804 throw RangeCheckError("FitsABTColRd::FirstRow try to read outside element range: nfirstel>repeat\n");
[3128]805 LONGLONG row = -1;
[1659]806 if(NBline==0) return row;
807 if(rowstart<0) rowstart = 0;
808 if(rowstart>=NBline) rowstart = NBline-1;
[3128]809 for(LONGLONG i=rowstart;i<NBline;i++) {
[4023]810 double val = Read(i,nfirstel);
[1659]811 if(val<val1 || val>val2) continue;
812 row = i;
813 break;
814 }
815 return row;
816}
817
818/*!
[4023]819 Return the number of the last row where val1<=val<=val2 starting at row rowstart
820 for element "nfirstel"
[1659]821 \return <0 means not found
822 \verbatim
823 - The search is performed from "rowstart" to the beginning
824 in descending order (from "rowstart" to 0).
825 - Warning: "rowstart<0" means "rowstart==nrows-1" (search all the table column)
826 That is the default
827 \endverbatim
828*/
[4023]829LONGLONG FitsABTColRd::LastRow(long nfirstel,double val1,double val2,LONGLONG rowstart)
[1659]830{
[4023]831 if(nfirstel<0 || nfirstel>=ColRepeat)
832 throw RangeCheckError("FitsABTColRd::FirstRow try to read outside element range: nfirstel>repeat\n");
[3128]833 LONGLONG row = -1;
[1659]834 if(NBline==0) return row;
835 if(rowstart<0 || rowstart>=NBline) rowstart = NBline-1;
[3128]836 for(LONGLONG i=rowstart;i>=0;i--) {
[4023]837 double val = Read(i,nfirstel);
[1659]838 if(val<val1 || val>val2) continue;
839 row = i;
840 break;
841 }
842 return row;
843}
844
[1654]845/*! Print on stream os */
[2449]846void FitsABTColRd::Print(ostream& os,int lp) const
[1654]847{
[4029]848 os<<"FitsABTColRd:Print ("<<NulVal<<")"
[1654]849 <<" ncols="<<NBcol<<" nrows="<<NBline;
850 if(lp>0) os<<" NRead="<<NFitsRead;
[2456]851 os<<"\n... "<<FileName()<<"["<<HDU()<<"/"<<NHDU()<<" type="<<HDUType()<<"]"
852 <<"\n... Label["<<ColNum<<"]="<<ColLabel<<" TypeCode="<<ColTypeCode
[4023]853 <<" TUnit="<<ColTUnit<<" TForm="<<ColTForm<<" Repeat="<<ColRepeat
[4025]854 <<" DispWidth="<<ColDispWidth
[1654]855 <<endl;
856}
[2449]857
858///////////////////////////////////////////////////////////////////
859///////////////////////////////////////////////////////////////////
860///////////////////////////////////////////////////////////////////
861///////////////////////////////////////////////////////////////////
862
863//! Class for reading a column in a FITS ASCII or BINARY table with fits file opening
864
865/*!
866 \class SOPHYA::FitsABTColRead
867 \ingroup FitsIOServer
[2789]868 Class for reading a column in a FITS ASCII or BINARY table with fits file opening.
869 The FITS file is opened each time you instanciate a FitsABTColRead.
870 So reading "n" columns of the same FITS table by instanciating "n"
871 FitsABTColRead, will open "n" times te FITS file.
872 Use FitsABTColRd if you want to open the FITS file only once.
[2449]873 \verbatim
874 -- Exemple:
875 FitsABTColRead fbt("myfits.fits","BoloMuv_28",0,1000,1,3);
876 fbt.SetDebug(3);
877 fbt.Print(3);
878 // Read element by element
[3128]879 for(LONGLONG i=0;i<fbt.GetNbLine();i++) {
[2449]880 double x = fbt.Read(i);
881 if(i%lpmod==0) cout<<i<<": "<<x<<endl;
882 }
883 // Read into a vector
884 TVector<double> data;
[3128]885 LONGLONG n = fbt.Read(32,50,data);
[2449]886 cout<<"Number of values read: "<<n<<endl;
887 data.ReSize(100);
888 n = fbt.Read(10,-1,data);
889 cout<<"Number of values read: "<<n<<endl;
890 \endverbatim
891*/
892
893
894//////////////////////////////////////////////////////////////
895/*!
896 Constructor.
897 \param fname : FITS file name to be read
898 \param collabel : label of the column to be read
899 \param ihdu : number of the HDU where the column is.
900 \param lp : debug level
901 \verbatim
[2456]902 - if ihdu<=0 first BINARY or ASCII table is taken
903 - if ihdu>nhdu ihdu is set to nhdu
[2449]904 \endverbatim
905 \warning ihdu = [1,nhdu]
906*/
[4029]907FitsABTColRead::FitsABTColRead(string fname,string collabel,int ihdu,int lp)
908: FitsABTColRd(new FitsOpenFile(fname),collabel,ihdu,lp)
[2449]909{
910}
911
912/*!
913 Constructor.
914 Same as before but the column is identified by its column number
915 \param colnum : number of the column to be read
916 \warning col = [0,ncol[
917*/
[4029]918FitsABTColRead::FitsABTColRead(string fname,int colnum,int ihdu,int lp)
919: FitsABTColRd(new FitsOpenFile(fname),colnum,ihdu,lp)
[2449]920{
921}
922
923/*! Constructor. see below */
[4029]924FitsABTColRead::FitsABTColRead(const char * cfname,const char* collabel,int ihdu,int lp)
925: FitsABTColRd(new FitsOpenFile(cfname),collabel,ihdu,lp)
[2449]926{
927}
928
929/*! Constructor. see below */
[4029]930FitsABTColRead::FitsABTColRead(const char * cfname,int colnum,int ihdu,int lp)
931: FitsABTColRd(new FitsOpenFile(cfname),colnum,ihdu,lp)
[2449]932{
933}
934/*! Constructor by default */
935FitsABTColRead::FitsABTColRead()
[2791]936: FitsABTColRd()
[2449]937{
938}
939
940/*! Constructor by copy */
941FitsABTColRead::FitsABTColRead(FitsABTColRead& fbt)
942{
943 // --- ATTENTION ---
944 // FitsABTColRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile
945 FitsOpenFile* fof = new FitsOpenFile(*fbt.GetFitsOpenFile());
[4029]946 Init(fof,fbt.GetColLabel().c_str(),fbt.GetColNum(),fbt.HDU(),fbt.DbgLevel);
[2449]947}
948
949/*! Destructor. */
950FitsABTColRead::~FitsABTColRead()
951{
[2789]952 Delete(); // ?? inutile ??
953 // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS
[2449]954 if(FitsOF!=NULL) delete FitsOF;
955}
[2453]956
957///////////////////////////////////////////////////////////////////
[2791]958///////////////////////////////////////////////////////////////////
959///////////////////////////////////////////////////////////////////
960///////////////////////////////////////////////////////////////////
961
[4025]962FitsABTColRd1F::FitsABTColRd1F(FitsOpenFile* fof,int ihdu,int lp)
[3660]963{
[4025]964 Init(fof,ihdu,lp);
[3660]965}
966
967/*! Init routine called by the constructor */
[4025]968void FitsABTColRd1F::Init(FitsOpenFile* fof,int ihdu,int lp)
[3660]969{
970 // Initialisation des Parametres Generaux
[4025]971 ColLabel.resize(0); ColTUnit.resize(0); ColTForm.resize(0);
972 ColTypeCode.resize(0); ColRepeat.resize(0); ColDispWidth.resize(0);
973 StrBuff.resize(0);
[3660]974 NBcol = 0; NBline = 0;
975 SetNulVal(); SetDebug(lp);
976 FitsOF = NULL;
977
978 // Caracteristiques du FitsOpenFile
979 FitsOF = fof;
980 if(FitsOF==NULL)
981 throw NullPtrError("FitsABTColRd1F::Init: FitsOpenFile pointer is NULL\n");
982
983 if(GetFitsPtr()==NULL)
984 throw NullPtrError("FitsABTColRd1F::Init: FitsPtr pointer is NULL\n");
985
986 int sta = 0;
987 if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU();
988
989 // Get HDU for bin/ascii table
990 // ATTENTION: le fichier est ouvert mais non positionne sur un HDU,
991 // une classe utilisant ce fichier doit le positionner sur un HDU.
992 // Par contre, si une autre classe utilise ce meme FitsOpenFile,
993 // elle ne peut le positionner que sur ce meme HDU !
994 if(FitsOF->GetPosStatus()==false) {
995 if(ihdu==0) { // find the first BINARY then the first ASCII
996 int rc = FitsOF->MoveToFirst(BINARY_TBL);
997 if(rc!=BINARY_TBL) FitsOF->MoveToFirst(ASCII_TBL);
998 } else {
999 int rc = FitsOF->MoveToHDU(ihdu);
1000 if(rc!=ihdu)
1001 throw RangeCheckError("FitsABTColRd1F::Init: Error moving to requested HDU\n");
1002 }
1003 } else { // Fits file has already been positionned
1004 if(ihdu>0 && ihdu!=HDU())
1005 throw RangeCheckError("FitsABTColRd1F::Init: file already posit. at another HDU\n");
1006 }
1007
1008 // Check HDUType and set position status to TRUE
1009 if(HDUType()!=BINARY_TBL && HDUType()!=ASCII_TBL)
1010 throw TypeMismatchExc("FitsABTColRd1F::Init: HDU not ASCII/BINARY table\n");
1011 if(DbgLevel>1) cout<<"...Init ihdu="<<ihdu<<" HduType="<<HDUType()<<endl;
1012 FitsOF->SetPosStatus(true);
1013
1014 // Get number of columns
1015 if(fits_get_num_cols(GetFitsPtr(),&NBcol,&sta)) {
1016 FitsOpenFile::printerror(sta);
1017 throw NotAvailableOperation("FitsABTColRd1F::Init: Error getting number of columns\n");
1018 }
1019 if(DbgLevel>1) cout<<"...Init NBcol="<<NBcol<<endl;
1020 if(NBcol<1)
1021 throw RangeCheckError("FitsABTColRd1F::Init: Bad number of colums\n");
1022
1023 // Get number of rows
1024 if(fits_get_num_rowsll(GetFitsPtr(),&NBline,&sta)) {
1025 FitsOpenFile::printerror(sta);
1026 throw NotAvailableOperation("FitsABTColRd1F::Init: Error getting number of rows\n");
1027 }
1028 if(DbgLevel>1) cout<<"...Init NBline="<<NBline<<endl;
1029 if(NBline<1)
1030 throw RangeCheckError("FitsABTColRd1F::Init: Bad number of rows\n");
1031
1032 // --- Boucle sur les colonnes
1033 for(int ColNum=0;ColNum<NBcol;ColNum++) { // ***** ColNum
1034 // Get column type
1035 ColTypeCode.push_back(-999);
1036 if(fits_get_coltypell(GetFitsPtr(),ColNum+1,&ColTypeCode[ColNum],NULL,NULL,&sta)) {
1037 FitsOpenFile::printerror(sta);
1038 throw ParmError("FitsABTColRd1F::Init: Error getting column type\n");
1039 }
1040 if(DbgLevel>1) cout<<"...Init ColTypeCode="<<ColTypeCode[ColNum]<<endl;
[4025]1041 if(ColTypeCode[ColNum]<0 )
1042 throw ParmError("FitsABTColRd1F::Init: Selected column type decoding not implemented\n");
[3660]1043 // Get column name, tunit, tform
1044 char labelcol[128];
1045 char tunit[64], tform[64], tdisp[64];
1046 LONGLONG repeat=0; double tscale=1., tzero=0.;
1047 int rc=0;
1048 if(HDUType()==BINARY_TBL) {
[4025]1049 rc = fits_get_bcolparmsll(GetFitsPtr(),ColNum+1,labelcol,tunit,tform
[3660]1050 ,&repeat,&tscale,&tzero,NULL,tdisp,&sta);
1051 } else {
1052 long repeatlng;
[4025]1053 rc = fits_get_acolparms(GetFitsPtr(),ColNum+1,labelcol,&repeatlng,tunit,tform
[3660]1054 ,&tscale,&tzero,NULL,tdisp,&sta);
1055 repeat = repeatlng;
1056 }
1057 if(rc) {
1058 FitsOpenFile::printerror(sta);
1059 throw RangeCheckError("FitsABTColRd1F::Init: Error getting the column caracteristics\n");
1060 }
1061 ColLabel.push_back(labelcol);
1062 ColTUnit.push_back(tunit);
1063 ColTForm.push_back(tform);
[4023]1064 ColRepeat.push_back(repeat);
[4025]1065
1066 int cdispw = 0;
1067 fits_get_col_display_width(GetFitsPtr(),ColNum+1,&cdispw,&sta);
1068 ColDispWidth.push_back(cdispw);
1069 StrBuff.push_back(NULL);
1070
[3660]1071 // some debug print if requested
1072 if(DbgLevel)
1073 cout<<"FitsABTColRd1F::Init Num="<<ColNum<<" Label="<<ColLabel[ColNum]
[4025]1074 <<" TypeCode="<<ColTypeCode[ColNum]<<" TUnit="<<ColTUnit[ColNum]<<" TForm="<<ColTForm[ColNum]
1075 <<" Repeat="<<ColRepeat[ColNum]<<" ColDispWidth="<<ColDispWidth[ColNum]<<endl;
[3660]1076 if(DbgLevel>1)
[4023]1077 cout<<" (tscale="<<tscale<<",tzero="<<tzero<<",tdisp="<<tdisp<<")"<<endl;
[3660]1078 } // ***** ColNum
1079
1080}
1081
1082/*! Destructor. */
1083FitsABTColRd1F::~FitsABTColRd1F()
1084{
1085 Delete();
1086}
1087
1088/*! Delete called by the destructor */
1089void FitsABTColRd1F::Delete(void)
1090{
[4025]1091 if(NBcol>0) for(int ColNum=0; ColNum<NBcol; ColNum++) {
1092 if(StrBuff[ColNum]==NULL) continue;
1093 delete [] StrBuff[ColNum];
1094 StrBuff[ColNum] = NULL;
1095 }
[3660]1096 //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
1097}
1098
1099//////////////////////////////////////////////////////////////
1100/*!
1101 Read a fitsheader key into double
1102 \param keyname : name of the key
1103 \return value into double
1104*/
1105double FitsABTColRd1F::ReadKey(const char *keyname)
1106{
1107 return FitsOpenFile::ReadKey(GetFitsPtr(),keyname);
1108}
1109
1110/*!
1111 Read a fitsheader key into long
1112 \param keyname : name of the key
1113 \return value into long
1114*/
1115long FitsABTColRd1F::ReadKeyL(const char *keyname)
1116{
1117 return FitsOpenFile::ReadKeyL(GetFitsPtr(),keyname);
1118}
1119
1120/*!
1121 Read a fitsheader key into long long
1122 \param keyname : name of the key
1123 \return value into long long
1124*/
1125LONGLONG FitsABTColRd1F::ReadKeyLL(const char *keyname)
1126{
1127 return FitsOpenFile::ReadKeyLL(GetFitsPtr(),keyname);
1128}
1129
1130/*!
1131 Read a fitsheader key into string
1132 \param keyname : name of the key
1133 \return value into string
1134*/
1135string FitsABTColRd1F::ReadKeyS(const char *keyname)
1136{
1137 return FitsOpenFile::ReadKeyS(GetFitsPtr(),keyname);
1138}
1139
1140/////////////////////////////////////////////////
1141int FitsABTColRd1F::GetColNum(const char *colname)
1142 // Get column number from column name
1143{
1144 if(strlen(colname)<1 || NBcol<1) return -1;
1145 string slab(colname);
1146 for(int ColNum=0;ColNum<NBcol;ColNum++)
1147 if(slab == ColLabel[ColNum]) return ColNum;
1148 return -1;
1149}
1150
1151/////////////////////////////////////////////////
1152/*!
[4023]1153 Read row "n" element "nfirstel" of column "ColNum" and return the value into a double
[3660]1154 \warning be carefull for the range: row = [0,NRows[
1155 \return value in double
1156 \param n : number of the row to be read.
1157*/
[4025]1158double FitsABTColRd1F::Read(int ColNum,LONGLONG n,long nfirstel)
[3660]1159// Attention: n=nline [0,NBline[, cfistio veut [1,NBline]
1160// Attention: colnum [0,NBcol[ , cfistio veut [1,NBcol]
[4023]1161// Attention: nfirstel [0,ColRepeat[ , cfistio veut [1,ColRepeat]
[3660]1162{
1163 int sta=0;
1164 if(ColNum<0 || ColNum>=NBcol)
1165 throw RangeCheckError("FitsABTColRd1F::Read try to read outside column range\n");
1166 if(n<0 || n>=NBline)
1167 throw RangeCheckError("FitsABTColRd1F::Read try to read outside line range\n");
[4023]1168 if(nfirstel<0 || nfirstel>=ColRepeat[ColNum])
1169 throw RangeCheckError("FitsABTColRd1F::Read try to read outside element range: nfirstel>repeat\n");
[3660]1170
[4025]1171 double val;
1172 fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,n+1,nfirstel+1,1,NULL,&val,NULL,&sta);
1173 if(sta) {
1174 FitsOpenFile::printerror(sta);
1175 throw NotAvailableOperation("FitsABTColRd1F::Read: Error Reading Fits file\n");
[3660]1176 }
[4025]1177 return val;
1178}
[3660]1179
[4025]1180
1181/*!
[4029]1182 Read row "n" element "nfirstel" and return a long long int
1183*/
1184int_8 FitsABTColRd1F::ReadLL(int ColNum,LONGLONG n,long nfirstel)
1185{
1186 int sta=0;
1187 if(ColNum<0 || ColNum>=NBcol)
1188 throw RangeCheckError("FitsABTColRd1F::ReadLL try to read outside column range\n");
1189 if(n<0 || n>=NBline)
1190 throw RangeCheckError("FitsABTColRd1F::ReadLL try to read outside line range\n");
1191 if(nfirstel<0 || nfirstel>=ColRepeat[ColNum])
1192 throw RangeCheckError("FitsABTColRd1F::ReadLL try to read outside element range: nfirstel>repeat\n");
1193
1194 int_8 val;
1195 fits_read_col(GetFitsPtr(),TLONGLONG,ColNum+1,n+1,nfirstel+1,1,NULL,&val,NULL,&sta);
1196 if(sta) {
1197 FitsOpenFile::printerror(sta);
1198 throw NotAvailableOperation("FitsABTColRd1F::ReadLL: Error Reading Fits file\n");
1199 }
1200 return val;
1201}
1202
1203
1204/*!
[4025]1205 Read row "n" element "nfirstel" and return a complex value
1206*/
1207complex<r_8> FitsABTColRd1F::ReadComplex(int ColNum,LONGLONG n,long nfirstel)
1208{
1209 int sta=0;
1210 if(ColNum<0 || ColNum>=NBcol)
1211 throw RangeCheckError("FitsABTColRd1F::ReadComplex try to read outside column range\n");
1212 if(n<0 || n>=NBline)
1213 throw RangeCheckError("FitsABTColRd1F::ReadComplex try to read outside line range\n");
1214 if(nfirstel<0 || nfirstel>=ColRepeat[ColNum])
1215 throw RangeCheckError("FitsABTColRd1F::ReadComplex try to read outside element range: nfirstel>repeat\n");
1216
1217 r_8 val[2];
1218 fits_read_col(GetFitsPtr(),TDBLCOMPLEX,ColNum+1,n+1,nfirstel+1,1,NULL,val,NULL,&sta);
1219 if(sta) {
1220 FitsOpenFile::printerror(sta);
1221 throw NotAvailableOperation("FitsABTColRd1F::ReadComplex: Error Reading Fits file\n");
[3660]1222 }
[4025]1223 return complex<r_8>(val[0],val[1]);
1224}
[3660]1225
[4025]1226/*!
1227 Read row "n" element "nfirstel" and return in string (internal pointer)
1228*/
1229char* FitsABTColRd1F::ReadInStr(int ColNum,LONGLONG n,long nfirstel)
1230{
1231 int sta=0;
1232 if(ColNum<0 || ColNum>=NBcol)
1233 throw RangeCheckError("FitsABTColRd1F::ReadInStr try to read outside column range\n");
1234 if(n<0 || n>=NBline)
1235 throw RangeCheckError("FitsABTColRd1F::ReadInStr try to read outside line range\n");
1236 if(nfirstel<0 || nfirstel>=ColRepeat[ColNum])
1237 throw RangeCheckError("FitsABTColRd1F::ReadInStr try to read outside element range: nfirstel>repeat\n");
1238
1239 if(StrBuff[ColNum]==NULL) StrBuff[ColNum] = new char[ColDispWidth[ColNum]+5];
1240 char nullstr[]="";
1241
1242 fits_read_col(GetFitsPtr(),TSTRING,ColNum+1,n+1,nfirstel+1,1,nullstr,&(StrBuff[ColNum]),NULL,&sta);
1243 if(sta) {
1244 FitsOpenFile::printerror(sta);
1245 throw NotAvailableOperation("FitsABTColRd::ReadInStr: Error Reading Fits file\n");
1246 }
1247 return StrBuff[ColNum];
[3660]1248}
1249
1250/*! Print on stream os */
1251void FitsABTColRd1F::Print(ostream& os,int lp) const
1252{
[4025]1253 os<<"FitsABTColRd1F:Print ("<<NulVal<<")"
[3660]1254 <<" ncols="<<NBcol<<" nrows="<<NBline;
1255 os<<"\n... "<<FileName()<<"["<<HDU()<<"/"<<NHDU()<<" type="<<HDUType()<<"]"<<endl;
1256 if(lp>0 && NBcol>0) {
1257 for(int ColNum=0;ColNum<NBcol;ColNum++) {
1258 os<<"..Col="<<ColNum<<" Label="<<ColLabel[ColNum]<<" TypeCode="<<ColTypeCode[ColNum]
[4025]1259 <<" TUnit="<<ColTUnit[ColNum]<<" TForm="<<ColTForm[ColNum]<<" Repeat="<<ColRepeat[ColNum]
1260 <<" DispWidth="<<ColDispWidth[ColNum]
[3660]1261 <<endl;
1262 }
1263 }
1264}
1265
1266///////////////////////////////////////////////////////////////////
1267///////////////////////////////////////////////////////////////////
1268///////////////////////////////////////////////////////////////////
1269///////////////////////////////////////////////////////////////////
1270
1271/*!
1272 Constructor.
1273 Same as before but the column is identified by its column number
1274 \param colnum : number of the column to be read
1275 \warning col = [0,ncol[
1276*/
[4025]1277FitsABTColRead1F::FitsABTColRead1F(string fname,int ihdu,int lp)
1278: FitsABTColRd1F(new FitsOpenFile(fname),ihdu,lp)
[3660]1279{
1280}
1281
1282/*! Constructor. see below */
[4025]1283FitsABTColRead1F::FitsABTColRead1F(const char * cfname,int ihdu,int lp)
1284: FitsABTColRd1F(new FitsOpenFile(cfname),ihdu,lp)
[3660]1285{
1286}
1287
1288/*! Destructor. */
1289FitsABTColRead1F::~FitsABTColRead1F()
1290{
1291 Delete(); // ?? inutile ??
1292 // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS
1293 if(FitsOF!=NULL) delete FitsOF;
1294}
1295
1296///////////////////////////////////////////////////////////////////
1297///////////////////////////////////////////////////////////////////
1298///////////////////////////////////////////////////////////////////
1299///////////////////////////////////////////////////////////////////
1300
[2453]1301//! Class for reading a 2D image from a FITS file
1302
1303/*!
1304 \class SOPHYA::FitsImg2DRd
1305 \ingroup FitsIOServer
1306 Class for reading a 2D image from a FITS file
1307*/
1308
1309//////////////////////////////////////////////////////////////
1310/*!
1311 Constructor.
1312 \param fof : Pointer to the Class for opening the FITS file
[2456]1313 \param ihdu : number of the HDU where the image is.
[2453]1314 \param lp : debug level
1315 \verbatim
[2456]1316 - if ihdu<=0 first IMAGE hdu is taken
1317 - if ihdu>nhdu ihdu is set to nhdu
[2453]1318 \endverbatim
1319 \warning ihdu = [1,nhdu]
1320*/
1321FitsImg2DRd::FitsImg2DRd(FitsOpenFile* fof,int ihdu,int lp)
1322{
1323 Init(fof,ihdu,lp);
1324}
1325
1326/*! Constructor by copy */
1327FitsImg2DRd::FitsImg2DRd(FitsImg2DRd& fbt)
1328{
[2456]1329 Init(fbt.GetFitsOpenFile(),fbt.HDU(),fbt.DbgLevel);
[2453]1330}
1331
1332/*! Constructor by default */
1333FitsImg2DRd::FitsImg2DRd()
1334{
1335 Naxis[0] = Naxis[1] = 0;
1336 SetNulVal(); SetDebug(0);
[3114]1337 FitsOF = NULL;
[2453]1338}
1339
1340/*! Init routine called by the constructor */
1341void FitsImg2DRd::Init(FitsOpenFile* fof,int ihdu,int lp)
1342{
1343 // Initialisation des Parametres Generaux
1344 Naxis[0] = Naxis[1] = 0;
1345 SetNulVal(); SetDebug(lp);
[3114]1346 FitsOF = NULL;
[2453]1347
1348 // Caracteristiques du FitsOpenFile
1349 FitsOF = fof;
1350 if(FitsOF==NULL)
1351 throw NullPtrError("FitsImg2DRd::Init: FitsOpenFile pointer is NULL\n");
[2456]1352
[3114]1353 if(GetFitsPtr()==NULL)
[2453]1354 throw NullPtrError("FitsImg2DRd::Init: FitsPtr pointer is NULL\n");
1355
1356 int sta = 0;
[2456]1357 if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU();
[2453]1358
1359 // Get HDU 2D image
[2456]1360 // ATTENTION: ... cf blabla equivalent dans FitsABTColRd::Init()
1361 if(FitsOF->GetPosStatus()==false) {
1362 if(ihdu==0) { // find the first IMAGE_HDU
1363 FitsOF->MoveToFirst(IMAGE_HDU);
1364 } else {
1365 int rc = FitsOF->MoveToHDU(ihdu);
1366 if(rc!=ihdu)
[3114]1367 throw RangeCheckError("FitsImg2DRd::Init: Error moving to requested HDU\n");
[2453]1368 }
[2456]1369 } else { // Fits file has already been positionned
1370 if(ihdu>0 && ihdu!=HDU())
[3114]1371 throw RangeCheckError("FitsImg2DRd::Init: file already posit. at another HDU\n");
[2453]1372 }
[2456]1373
1374 // Check HDUType and set position status to TRUE
1375 if(HDUType()!=IMAGE_HDU)
[2453]1376 throw TypeMismatchExc("FitsImg2DRd::Init: HDU not IMAGE_HDU\n");
[2456]1377 FitsOF->SetPosStatus(true);
[2453]1378
1379 // Get NAXIS 1 et 2
1380 int nfound=0;
[3128]1381 // car fits_read_keys_lnglng n'est pas prototype dans longnam.h
1382 if(ffgknjj(GetFitsPtr(),"NAXIS",1,2,Naxis,&nfound,&sta)) {
[2453]1383 FitsOpenFile::printerror(sta);
1384 throw RangeCheckError("FitsImg2DRd::Init: Error reading NAXIS cards\n");
1385 }
1386 if(DbgLevel>1)
[2456]1387 cout<<"...Init(hdu="<<HDU()<<") NAXIS1="<<Naxis[0]<<" NAXIS2="
1388 <<Naxis[1]<<" (nfound="<<nfound<<")"<<endl;
[2453]1389 if(nfound!=2 || Naxis[0]<=0 || Naxis[1]<=0)
1390 throw NotAvailableOperation("FitsImg2DRd::Init: bad Naxis[0-1] value\n");
1391
1392}
1393
1394/*! Destructor. */
1395FitsImg2DRd::~FitsImg2DRd()
1396{
1397 //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
1398 Naxis[0] = Naxis[1] = 0;
1399}
1400
1401//////////////////////////////////////////////////////////////
1402/*!
1403 Read a fitsheader key into double
1404 \param keyname : name of the key
1405 \return value into double
1406*/
[3572]1407double FitsImg2DRd::ReadKey(const char *keyname)
[2453]1408{
[3114]1409 return FitsOpenFile::ReadKey(GetFitsPtr(),keyname);
[2453]1410}
1411
1412/*!
1413 Read a fitsheader key into long
1414 \param keyname : name of the key
1415 \return value into long
1416*/
[3572]1417long FitsImg2DRd::ReadKeyL(const char *keyname)
[2453]1418{
[3114]1419 return FitsOpenFile::ReadKeyL(GetFitsPtr(),keyname);
[2453]1420}
1421
1422/*!
[3128]1423 Read a fitsheader key into long long
1424 \param keyname : name of the key
1425 \return value into long long
1426*/
[3572]1427LONGLONG FitsImg2DRd::ReadKeyLL(const char *keyname)
[3128]1428{
1429 return FitsOpenFile::ReadKeyLL(GetFitsPtr(),keyname);
1430}
1431
1432/*!
[2453]1433 Read a fitsheader key into string
1434 \param keyname : name of the key
1435 \return value into string
1436*/
[3572]1437string FitsImg2DRd::ReadKeyS(const char *keyname)
[2453]1438{
[3114]1439 return FitsOpenFile::ReadKeyS(GetFitsPtr(),keyname);
[2453]1440}
1441
1442//////////////////////////////////////////////////////////////
1443/* REMARQUE:
1444 * Si une image FITS a NAXIS1=100 et NAXIS2=50
1445 * alors un tableau 2D juste assez grand pour contenir l'image
1446 * doit etre declare array[50][100] (et non pas array[100][50])
1447 * array[NAXIS2][NAXIS1]
1448 */
1449/*!
1450Read image into a TMatrix<uint_2>
1451\warning TMatrix data(Naxis2,Naxis1)
1452*/
[3128]1453LONGLONG FitsImg2DRd::Read(TMatrix<uint_2>& data)
[2453]1454{
1455 int sta=0;
1456 uint_2* arr = new uint_2[Naxis[0]];
1457 data.ReSize(Naxis[1],Naxis[0]);
1458
[3128]1459 for(LONGLONG j=0;j<Naxis[1];j++) {
1460 LONGLONG deb = j*Naxis[0]+1, nel = Naxis[0];
[3114]1461 fits_read_img(GetFitsPtr(),TUSHORT,deb,nel,&NulVal,arr,NULL,&sta);
[2453]1462 if(sta) {
1463 FitsOpenFile::printerror(sta); delete [] arr;
1464 throw
1465 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<uint_2>): Error Reading Fits file\n");
1466 }
[3128]1467 for(LONGLONG i=0;i<Naxis[0];i++) data(j,i) = arr[i];
[2453]1468 }
1469
1470 delete [] arr;
1471 return Naxis[0]*Naxis[1];
[3188]1472}
[2453]1473
1474/*! Read image into a TMatrix<int_4> */
[3128]1475LONGLONG FitsImg2DRd::Read(TMatrix<int_4>& data)
[2453]1476{
1477 int sta=0;
1478 int_4* arr = new int_4[Naxis[0]];
1479 data.ReSize(Naxis[1],Naxis[0]);
1480 int T = (sizeof(long)==4) ? TLONG: TINT;
1481
[3128]1482 for(LONGLONG j=0;j<Naxis[1];j++) {
1483 LONGLONG deb = j*Naxis[0]+1, nel = Naxis[0];
[3114]1484 fits_read_img(GetFitsPtr(),T,deb,nel,&NulVal,arr,NULL,&sta);
[2453]1485 if(sta) {
1486 FitsOpenFile::printerror(sta); delete [] arr;
1487 throw
1488 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<int_4>): Error Reading Fits file\n");
1489 }
[3128]1490 for(LONGLONG i=0;i<Naxis[0];i++) data(j,i) = arr[i];
[2453]1491 }
1492
1493 delete [] arr;
1494 return Naxis[0]*Naxis[1];
[3188]1495}
[2453]1496
1497/*! Read image into a TMatrix<int_8> */
[3128]1498LONGLONG FitsImg2DRd::Read(TMatrix<int_8>& data)
[2453]1499{
1500 int sta=0;
1501 int_8* arr = new int_8[Naxis[0]];
1502 data.ReSize(Naxis[1],Naxis[0]);
1503
[3128]1504 for(LONGLONG j=0;j<Naxis[1];j++) {
1505 LONGLONG deb = j*Naxis[0]+1, nel = Naxis[0];
[3114]1506 fits_read_img(GetFitsPtr(),TLONGLONG,deb,nel,&NulVal,arr,NULL,&sta);
[2453]1507 if(sta) {
1508 FitsOpenFile::printerror(sta); delete [] arr;
1509 throw
1510 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<int_8>): Error Reading Fits file\n");
1511 }
[3128]1512 for(LONGLONG i=0;i<Naxis[0];i++) data(j,i) = arr[i];
[2453]1513 }
1514
1515 delete [] arr;
1516 return Naxis[0]*Naxis[1];
[3188]1517}
[2453]1518
1519/*! Read image into a TMatrix<float> */
[3128]1520LONGLONG FitsImg2DRd::Read(TMatrix<float>& data)
[2453]1521{
1522 int sta=0;
1523 float* arr = new float[Naxis[0]];
1524 data.ReSize(Naxis[1],Naxis[0]);
1525
[3128]1526 for(LONGLONG j=0;j<Naxis[1];j++) {
1527 LONGLONG deb = j*Naxis[0]+1, nel = Naxis[0];
[3114]1528 fits_read_img(GetFitsPtr(),TFLOAT,deb,nel,&NulVal,arr,NULL,&sta);
[2453]1529 if(sta) {
1530 FitsOpenFile::printerror(sta); delete [] arr;
1531 throw
1532 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<float>): Error Reading Fits file\n");
1533 }
[3128]1534 for(LONGLONG i=0;i<Naxis[0];i++) data(j,i) = arr[i];
[2453]1535 }
1536
1537 delete [] arr;
1538 return Naxis[0]*Naxis[1];
[3188]1539}
[2453]1540
1541/*! Read image into a TMatrix<double> */
[3128]1542LONGLONG FitsImg2DRd::Read(TMatrix<double>& data)
[2453]1543{
1544 int sta=0;
1545 double* arr = new double[Naxis[0]];
1546 data.ReSize(Naxis[1],Naxis[0]);
1547
[3128]1548 for(LONGLONG j=0;j<Naxis[1];j++) {
1549 LONGLONG deb = j*Naxis[0]+1, nel = Naxis[0];
[3114]1550 fits_read_img(GetFitsPtr(),TDOUBLE,deb,nel,&NulVal,arr,NULL,&sta);
[2453]1551 if(sta) {
1552 FitsOpenFile::printerror(sta); delete [] arr;
1553 throw
1554 NotAvailableOperation("FitsImg2DRd::Read(TMatrix<double>): Error Reading Fits file\n");
1555 }
[3128]1556 for(LONGLONG i=0;i<Naxis[0];i++) data(j,i) = arr[i];
[2453]1557 }
1558
1559 delete [] arr;
1560 return Naxis[0]*Naxis[1];
[3188]1561}
[2791]1562
[3188]1563/*! Read image pixel numcol,numrow with numcol=[0,Naxis1[ and numrow=[0,Naxis2[ */
1564double FitsImg2DRd::Read(LONGLONG numcol, LONGLONG numrow)
1565{
1566 int sta=0;
1567 if(numcol<0 || numrow<0 || numcol>=Naxis[0] || numrow>=Naxis[1])
1568 throw
1569 NotAvailableOperation("FitsImg2DRd::Read(col,row): bad col/row number\n");
1570
1571 LONGLONG deb = numrow*Naxis[0] + numcol + 1;
1572 double val = 0.;
1573 fits_read_img(GetFitsPtr(),TDOUBLE,deb,1,&NulVal,&val,NULL,&sta);
1574
1575 if(sta) {
1576 FitsOpenFile::printerror(sta);
1577 throw
1578 NotAvailableOperation("FitsImg2DRd::Read(col,num): Error Reading Fits file\n");
1579 }
1580
1581 return val;
1582}
1583
[2791]1584///////////////////////////////////////////////////////////////////
1585///////////////////////////////////////////////////////////////////
1586///////////////////////////////////////////////////////////////////
1587///////////////////////////////////////////////////////////////////
1588
1589//! Class for reading a 2D image from a FITS file
1590
1591/*!
1592 \class SOPHYA::FitsImg2DRead
1593 \ingroup FitsIOServer
1594 Class for reading a 2D image from a FITS file
1595*/
1596
1597//////////////////////////////////////////////////////////////
1598/*!
1599 Constructor.
1600 \param fname : name of the FITS file
1601 \param ihdu : number of the HDU where the image is.
1602 \param lp : debug level
1603 \verbatim
1604 - if ihdu<=0 first IMAGE hdu is taken
1605 - if ihdu>nhdu ihdu is set to nhdu
1606 \endverbatim
1607 \warning ihdu = [1,nhdu]
1608*/
1609FitsImg2DRead::FitsImg2DRead(string fname,int ihdu,int lp)
1610: FitsImg2DRd(new FitsOpenFile(fname),ihdu,lp)
1611{
1612}
1613
1614/*! Constructor. see below */
1615FitsImg2DRead::FitsImg2DRead(const char * cfname,int ihdu,int lp)
1616: FitsImg2DRd(new FitsOpenFile(cfname),ihdu,lp)
1617{
1618}
1619
1620/*! Constructor by default */
1621FitsImg2DRead::FitsImg2DRead()
1622: FitsImg2DRd()
1623{
1624}
1625
1626/*! Constructor by copy */
1627FitsImg2DRead::FitsImg2DRead(FitsImg2DRead& fimg)
1628{
1629 // --- ATTENTION ---
1630 // FitsImg2DRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile
1631 FitsOpenFile* fof = new FitsOpenFile(*fimg.GetFitsOpenFile());
1632 Init(fof,fimg.HDU(),fimg.DbgLevel);
1633}
1634
1635/*! Destructor. */
1636FitsImg2DRead::~FitsImg2DRead()
1637{
1638 // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS
1639 if(FitsOF!=NULL) delete FitsOF;
1640}
[3114]1641
1642
1643
1644///////////////////////////////////////////////////////////////////
1645///////////////////////////////////////////////////////////////////
1646///////////////////////////////////////////////////////////////////
1647///////////////////////////////////////////////////////////////////
1648
1649//! Class for reading a 3D image from a FITS file
1650
1651/*!
1652 \class SOPHYA::FitsImg3DRd
1653 \ingroup FitsIOServer
1654 Class for reading a 3D image from a FITS file
1655*/
1656
1657//////////////////////////////////////////////////////////////
1658/*!
1659 Constructor.
1660 \param fof : Pointer to the Class for opening the FITS file
1661 \param ihdu : number of the HDU where the 3D image is.
1662 \param lp : debug level
1663 \verbatim
1664 - if ihdu<=0 first IMAGE hdu is taken
1665 - if ihdu>nhdu ihdu is set to nhdu
1666 \endverbatim
1667 \warning ihdu = [1,nhdu]
1668*/
1669FitsImg3DRd::FitsImg3DRd(FitsOpenFile* fof,int ihdu,int lp)
1670{
1671 Init(fof,ihdu,lp);
1672}
1673
1674/*! Constructor by copy */
1675FitsImg3DRd::FitsImg3DRd(FitsImg3DRd& fbt)
1676{
1677 Init(fbt.GetFitsOpenFile(),fbt.HDU(),fbt.DbgLevel);
1678}
1679
1680/*! Constructor by default */
1681FitsImg3DRd::FitsImg3DRd()
1682{
1683 Naxis[0] = Naxis[1] = Naxis[2] = 0;
1684 SetNulVal(); SetDebug(0);
1685 FitsOF = NULL;
1686}
1687
1688/*! Init routine called by the constructor */
1689void FitsImg3DRd::Init(FitsOpenFile* fof,int ihdu,int lp)
1690{
1691 // Initialisation des Parametres Generaux
1692 Naxis[0] = Naxis[1] = Naxis[2] = 0;
1693 SetNulVal(); SetDebug(lp);
1694 FitsOF = NULL;
1695
1696 // Caracteristiques du FitsOpenFile
1697 FitsOF = fof;
1698 if(FitsOF==NULL)
1699 throw NullPtrError("FitsImg3DRd::Init: FitsOpenFile pointer is NULL\n");
1700
1701 if(GetFitsPtr()==NULL)
1702 throw NullPtrError("FitsImg3DRd::Init: FitsPtr pointer is NULL\n");
1703
1704 int sta = 0;
1705 if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU();
1706
1707 // Get HDU 3D image
1708 // ATTENTION: ... cf blabla equivalent dans FitsABTColRd::Init()
1709 if(FitsOF->GetPosStatus()==false) {
1710 if(ihdu==0) { // find the first IMAGE_HDU
1711 FitsOF->MoveToFirst(IMAGE_HDU);
1712 } else {
1713 int rc = FitsOF->MoveToHDU(ihdu);
1714 if(rc!=ihdu)
1715 throw RangeCheckError("FitsImg3DRd::Init: Error moving to requested HDU\n");
1716 }
1717 } else { // Fits file has already been positionned
1718 if(ihdu>0 && ihdu!=HDU())
1719 throw RangeCheckError("FitsImg3DRd::Init: file already posit. at another HDU\n");
1720 }
1721
1722 // Check HDUType and set position status to TRUE
1723 if(HDUType()!=IMAGE_HDU)
1724 throw TypeMismatchExc("FitsImg3DRd::Init: HDU not IMAGE_HDU\n");
1725 FitsOF->SetPosStatus(true);
1726
1727 // Get NAXIS 1, 2 et 3
1728 int nfound=0;
[3128]1729 // car fits_read_keys_lnglng n'est pas prototype dans longnam.h
1730 if(ffgknjj(GetFitsPtr(),"NAXIS",1,3,Naxis,&nfound,&sta)) {
[3114]1731 FitsOpenFile::printerror(sta);
1732 throw RangeCheckError("FitsImg3DRd::Init: Error reading NAXIS cards\n");
1733 }
1734 if(DbgLevel>1)
1735 cout<<"...Init(hdu="<<HDU()<<") NAXIS1="<<Naxis[0]<<" NAXIS2="
1736 <<Naxis[1]<<" NAXIS3="<<Naxis[2]<<" (nfound="<<nfound<<")"<<endl;
1737 if(nfound!=3 || Naxis[0]<=0 || Naxis[1]<=0 || Naxis[2]<=0)
1738 throw NotAvailableOperation("FitsImg3DRd::Init: bad Naxis[0-2] value\n");
1739
1740}
1741
1742/*! Destructor. */
1743FitsImg3DRd::~FitsImg3DRd()
1744{
1745 //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
1746 Naxis[0] = Naxis[1] = Naxis[2] = 0;
1747}
1748
1749//////////////////////////////////////////////////////////////
1750/*!
1751 Read a fitsheader key into double
1752 \param keyname : name of the key
1753 \return value into double
1754*/
[3572]1755double FitsImg3DRd::ReadKey(const char *keyname)
[3114]1756{
1757 return FitsOpenFile::ReadKey(GetFitsPtr(),keyname);
1758}
1759
1760/*!
1761 Read a fitsheader key into long
1762 \param keyname : name of the key
1763 \return value into long
1764*/
[3572]1765long FitsImg3DRd::ReadKeyL(const char *keyname)
[3114]1766{
1767 return FitsOpenFile::ReadKeyL(GetFitsPtr(),keyname);
1768}
1769
1770/*!
[3128]1771 Read a fitsheader key into long long
1772 \param keyname : name of the key
1773 \return value into long long
1774*/
[3572]1775LONGLONG FitsImg3DRd::ReadKeyLL(const char *keyname)
[3128]1776{
1777 return FitsOpenFile::ReadKeyLL(GetFitsPtr(),keyname);
1778}
1779
1780/*!
[3114]1781 Read a fitsheader key into string
1782 \param keyname : name of the key
1783 \return value into string
1784*/
[3572]1785string FitsImg3DRd::ReadKeyS(const char *keyname)
[3114]1786{
1787 return FitsOpenFile::ReadKeyS(GetFitsPtr(),keyname);
1788}
1789
1790//////////////////////////////////////////////////////////////
1791/* REMARQUE:
1792 * Dans TArray A(naxis1,naxis2,naxis3);
1793 * A(i,j,k) -> i varie le plus vite et k le moins vite
1794 */
1795/*!
1796Read 3D image into a TArray<uint_2>
1797*/
[3128]1798LONGLONG FitsImg3DRd::Read(TArray<uint_2>& data)
[3114]1799{
1800 int sta=0;
1801 uint_2* arr = new uint_2[Naxis[0]];
1802 sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
1803
[3128]1804 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1805 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
[3114]1806 fits_read_img(GetFitsPtr(),TUSHORT,deb,nel,&NulVal,arr,NULL,&sta);
1807 if(sta) {
1808 FitsOpenFile::printerror(sta); delete [] arr;
1809 throw
1810 NotAvailableOperation("FitsImg3DRd::Read(TArray<uint_2>): Error Reading Fits file\n");
1811 }
[3128]1812 for(LONGLONG i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
[3114]1813 }
1814
1815 delete [] arr;
1816 return Naxis[0]*Naxis[1]*Naxis[2];
[3188]1817}
[3114]1818
1819/*! Read 3D image into a TArray<int_4> */
[3128]1820LONGLONG FitsImg3DRd::Read(TArray<int_4>& data)
[3114]1821{
1822 int sta=0;
1823 int_4* arr = new int_4[Naxis[0]];
1824 sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
1825 int T = (sizeof(long)==4) ? TLONG: TINT;
1826
[3128]1827 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1828 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
[3114]1829 fits_read_img(GetFitsPtr(),T,deb,nel,&NulVal,arr,NULL,&sta);
1830 if(sta) {
1831 FitsOpenFile::printerror(sta); delete [] arr;
1832 throw
1833 NotAvailableOperation("FitsImg3DRd::Read(TArray<int_4>): Error Reading Fits file\n");
1834 }
[3128]1835 for(LONGLONG i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
[3114]1836 }
1837
1838 delete [] arr;
1839 return Naxis[0]*Naxis[1]*Naxis[2];
[3188]1840}
[3114]1841
1842/*! Read 3D image into a TArray<int_8> */
[3128]1843LONGLONG FitsImg3DRd::Read(TArray<int_8>& data)
[3114]1844{
1845 int sta=0;
1846 int_8* arr = new int_8[Naxis[0]];
1847 sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
1848
[3128]1849 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1850 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
[3114]1851 fits_read_img(GetFitsPtr(),TLONGLONG,deb,nel,&NulVal,arr,NULL,&sta);
1852 if(sta) {
1853 FitsOpenFile::printerror(sta); delete [] arr;
1854 throw
1855 NotAvailableOperation("FitsImg3DRd::Read(TArray<int_8>): Error Reading Fits file\n");
1856 }
[3128]1857 for(LONGLONG i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
[3114]1858 }
1859
1860 delete [] arr;
1861 return Naxis[0]*Naxis[1]*Naxis[2];
[3188]1862}
[3114]1863
1864/*! Read 3D image into a TArray<float> */
[3128]1865LONGLONG FitsImg3DRd::Read(TArray<float>& data)
[3114]1866{
1867 int sta=0;
1868 float* arr = new float[Naxis[0]];
1869 sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
1870
[3128]1871 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1872 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
[3114]1873 fits_read_img(GetFitsPtr(),TFLOAT,deb,nel,&NulVal,arr,NULL,&sta);
1874 if(sta) {
1875 FitsOpenFile::printerror(sta); delete [] arr;
1876 throw
1877 NotAvailableOperation("FitsImg3DRd::Read(TArray<float>): Error Reading Fits file\n");
1878 }
[3128]1879 for(LONGLONG i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
[3114]1880 }
1881
1882 delete [] arr;
1883 return Naxis[0]*Naxis[1]*Naxis[2];
[3188]1884}
[3114]1885
1886/*! Read 3D image into a TArray<double> */
[3128]1887LONGLONG FitsImg3DRd::Read(TArray<double>& data)
[3114]1888{
1889 int sta=0;
1890 double* arr = new double[Naxis[0]];
1891 sa_size_t ndim[3] = {Naxis[0],Naxis[1],Naxis[2]}; data.ReSize(3,ndim);
1892
[3128]1893 for(LONGLONG k=0;k<Naxis[2];k++) for(LONGLONG j=0;j<Naxis[1];j++) {
1894 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
[3114]1895 fits_read_img(GetFitsPtr(),TDOUBLE,deb,nel,&NulVal,arr,NULL,&sta);
1896 if(sta) {
1897 FitsOpenFile::printerror(sta); delete [] arr;
1898 throw
1899 NotAvailableOperation("FitsImg3DRd::Read(TArray<double>): Error Reading Fits file\n");
1900 }
[3128]1901 for(LONGLONG i=0;i<Naxis[0];i++) data(i,j,k) = arr[i];
[3114]1902 }
1903
1904 delete [] arr;
1905 return Naxis[0]*Naxis[1]*Naxis[2];
[3188]1906}
[3114]1907
[3772]1908/*! Read 3D image into a TVector<int_4> */
1909LONGLONG FitsImg3DRd::Read(LONGLONG j, LONGLONG k, TVector<int_4>& data)
1910{
1911 if(j<0 || k<0 || j>=Naxis[1] || k>=Naxis[2])
1912 throw NotAvailableOperation("FitsImg3DRd::Read(TVector<int_4>): bad j/k number\n");
1913 int sta=0;
1914 if(data.Size() != Naxis[0]) data.ReSize(Naxis[0]);
1915 int T = (sizeof(long)==4) ? TLONG: TINT;
1916 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
1917 fits_read_img(GetFitsPtr(),T,deb,nel,&NulVal,data.Data(),NULL,&sta);
1918 if(sta) {
1919 FitsOpenFile::printerror(sta);
1920 throw
1921 NotAvailableOperation("FitsImg3DRd::Read(TVector<int_4>): Error Reading Fits file\n");
1922 }
1923 return Naxis[0];
1924}
1925
1926/*! Read 3D image into a TVector<float> */
1927LONGLONG FitsImg3DRd::Read(LONGLONG j, LONGLONG k, TVector<float>& data)
1928{
1929 if(j<0 || k<0 || j>=Naxis[1] || k>=Naxis[2])
1930 throw NotAvailableOperation("FitsImg3DRd::Read(TVector<float>): bad j/k number\n");
1931 int sta=0;
1932 if(data.Size() != Naxis[0]) data.ReSize(Naxis[0]);
1933 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
1934 fits_read_img(GetFitsPtr(),TFLOAT,deb,nel,&NulVal,data.Data(),NULL,&sta);
1935 if(sta) {
1936 FitsOpenFile::printerror(sta);
1937 throw
1938 NotAvailableOperation("FitsImg3DRd::Read(TVector<float>): Error Reading Fits file\n");
1939 }
1940 return Naxis[0];
1941}
1942
1943/*! Read 3D image into a TVector<double> */
1944LONGLONG FitsImg3DRd::Read(LONGLONG j, LONGLONG k, TVector<double>& data)
1945{
1946 if(j<0 || k<0 || j>=Naxis[1] || k>=Naxis[2])
1947 throw NotAvailableOperation("FitsImg3DRd::Read(TVector<double>): bad j/k number\n");
1948 int sta=0;
1949 if(data.Size() != Naxis[0]) data.ReSize(Naxis[0]);
1950 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+1, nel = Naxis[0];
1951 fits_read_img(GetFitsPtr(),TDOUBLE,deb,nel,&NulVal,data.Data(),NULL,&sta);
1952 if(sta) {
1953 FitsOpenFile::printerror(sta);
1954 throw
1955 NotAvailableOperation("FitsImg3DRd::Read(TVector<double>): Error Reading Fits file\n");
1956 }
1957 return Naxis[0];
1958}
1959
[3188]1960/*! Read 3D image pixel i,j,k with i=[0,Naxis1[ , j=[0,Naxis2[ , k=[0,Naxis3[ */
1961double FitsImg3DRd::Read(LONGLONG i, LONGLONG j, LONGLONG k)
1962{
1963 int sta=0;
1964 if(i<0 || j<0 || k<0 || i>=Naxis[0] || j>=Naxis[1] || k>=Naxis[2])
1965 throw
1966 NotAvailableOperation("FitsImg3DRd::Read(i,j,k): bad i/j/k number\n");
1967
1968 LONGLONG deb = Naxis[0]*(j+Naxis[1]*k)+i+1;
1969 double val = 0.;
1970 fits_read_img(GetFitsPtr(),TDOUBLE,deb,1,&NulVal,&val,NULL,&sta);
1971
1972 if(sta) {
1973 FitsOpenFile::printerror(sta);
1974 throw
1975 NotAvailableOperation("FitsImg3DRd::Read(i,j,k): Error Reading Fits file\n");
1976 }
1977
1978 return val;
1979}
1980
[3114]1981///////////////////////////////////////////////////////////////////
1982///////////////////////////////////////////////////////////////////
1983///////////////////////////////////////////////////////////////////
1984///////////////////////////////////////////////////////////////////
1985
1986//! Class for reading a 3D image from a FITS file
1987
1988/*!
1989 \class SOPHYA::FitsImg3DRead
1990 \ingroup FitsIOServer
1991 Class for reading a 3D image from a FITS file
1992*/
1993
1994//////////////////////////////////////////////////////////////
1995/*!
1996 Constructor.
1997 \param fname : name of the FITS file
1998 \param ihdu : number of the HDU where the 3D image is.
1999 \param lp : debug level
2000 \verbatim
2001 - if ihdu<=0 first IMAGE hdu is taken
2002 - if ihdu>nhdu ihdu is set to nhdu
2003 \endverbatim
2004 \warning ihdu = [1,nhdu]
2005*/
2006FitsImg3DRead::FitsImg3DRead(string fname,int ihdu,int lp)
2007: FitsImg3DRd(new FitsOpenFile(fname),ihdu,lp)
2008{
2009}
2010
2011/*! Constructor. see below */
2012FitsImg3DRead::FitsImg3DRead(const char * cfname,int ihdu,int lp)
2013: FitsImg3DRd(new FitsOpenFile(cfname),ihdu,lp)
2014{
2015}
2016
2017/*! Constructor by default */
2018FitsImg3DRead::FitsImg3DRead()
2019: FitsImg3DRd()
2020{
2021}
2022
2023/*! Constructor by copy */
2024FitsImg3DRead::FitsImg3DRead(FitsImg3DRead& fimg)
2025{
2026 // --- ATTENTION ---
2027 // FitsImg3DRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile
2028 FitsOpenFile* fof = new FitsOpenFile(*fimg.GetFitsOpenFile());
2029 Init(fof,fimg.HDU(),fimg.DbgLevel);
2030}
2031
2032/*! Destructor. */
2033FitsImg3DRead::~FitsImg3DRead()
2034{
2035 // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS
2036 if(FitsOF!=NULL) delete FitsOF;
2037}
Note: See TracBrowser for help on using the repository browser.