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