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

Last change on this file since 3166 was 3128, checked in by cmv, 19 years ago

gestion gros fichiers FITS dans fabtcolread et fabtwriter cmv 11/01/2007

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