source: Sophya/trunk/SophyaExt/FitsIOServer/fbtntintf.cc@ 1507

Last change on this file since 1507 was 1507, checked in by cmv, 24 years ago

sens +/0/- et ChangeBuffer pour Fits BT+ASCII reader cmv 22/5/01

File size: 7.3 KB
Line 
1/* Interface Fits BINARY/ASCII Table cmv 21/05/2001 */
2#include "machdefs.h"
3#include <stdlib.h>
4#include <stdio.h>
5#include "pexceptions.h"
6#include "fbtntintf.h"
7
8//////////////////////////////////////////////////////////////
9FitsBTNtuIntf::FitsBTNtuIntf(string fname,int ihdu
10 ,int_4 blen,int_4 bsens,uint_2 lp)
11{
12 DbgLevel = lp;
13 SetNulVal();
14 SetBuffer(blen,bsens);
15 FitsFN = fname;
16 IHdu = ihdu;
17 Init();
18}
19
20FitsBTNtuIntf::FitsBTNtuIntf(char * cfname,int ihdu
21 ,int_4 blen,int_4 bsens,uint_2 lp)
22{
23 DbgLevel = lp;
24 SetNulVal();
25 SetBuffer(blen,bsens);
26 FitsFN = cfname;
27 IHdu = ihdu;
28 Init();
29}
30
31FitsBTNtuIntf::FitsBTNtuIntf(FitsBTNtuIntf& fbtnt)
32{
33 DbgLevel = fbtnt.DbgLevel;
34 SetNulVal(fbtnt.NulVal);
35 SetBuffer(fbtnt.BuffLen,fbtnt.BuffSens);
36 FitsFN = fbtnt.FitsFN;
37 IHdu = fbtnt.IHdu;
38 Init();
39}
40
41FitsBTNtuIntf::FitsBTNtuIntf()
42{
43 DbgLevel = 0;
44 SetNulVal();
45 SetBuffer();
46 FitsFN = "";
47 IHdu = -1;
48 Init();
49}
50
51FitsBTNtuIntf::~FitsBTNtuIntf()
52{
53 Delete();
54}
55
56
57//////////////////////////////////////////////////////////////
58void FitsBTNtuIntf::ChangeBuffer(int_4 blen,int_4 bsens)
59{
60SetBuffer(blen,bsens);
61DeAllocBuff();
62AllocBuff();
63}
64
65void FitsBTNtuIntf::AllocBuff(void)
66{
67if(Buffer==NULL || NBcol<=0) return;
68int_4 n = BuffLen; if(BuffSens==0) n++;
69for(int icol=0;icol<NBcol;icol++)
70 if(ColReadable[icol]) Buffer[icol] = new double[n];
71 else Buffer[icol] = NULL;
72}
73
74void FitsBTNtuIntf::DeAllocBuff(void)
75{
76if(Buffer==NULL || NBcol<=0) return;
77for(int icol=0;icol<NBcol;icol++)
78 if(Buffer[icol]!=NULL)
79 {delete [] Buffer[icol]; Buffer[icol] = NULL;}
80LineDeb = LineFin = -1;
81}
82
83void FitsBTNtuIntf::Delete()
84{
85 DeAllocBuff();
86 delete [] Buffer; Buffer=NULL;
87 if(mRet!=NULL) delete [] mRet;
88 if(ColReadable!=NULL) delete [] ColReadable;
89
90 int sta = 0;
91 if(fits_close_file(FitsPtr,&sta)) printerror(sta);
92 FitsPtr = NULL;
93}
94
95void FitsBTNtuIntf::Init()
96{
97 NBcol = 0; NBline = 0;
98 LineDeb = LineFin = -1;
99 ColName.resize(0);
100 ColReadable = NULL;
101 Buffer = NULL;
102 mRet = NULL;
103 NFitsRead = 0;
104
105 if(FitsFN.size() <= 0 ) {
106 FitsPtr = NULL;
107 IHdu = -1;
108 return;
109 }
110
111 //////////////////////////
112 // Ouverture du fichier //
113 //////////////////////////
114
115 int sta=0,nhdu,hdutype;
116 const char * cfitsfn = FitsFN.c_str();
117
118 // Open fits
119 if(fits_open_file(&FitsPtr,cfitsfn,READONLY,&sta)) printerror(sta);
120
121 // Get number of hdu
122 if(fits_get_num_hdus(FitsPtr,&nhdu,&sta)) printerror(sta);
123 if(DbgLevel) cout<<"FitsBTNtuIntf::Init nhdu="<<nhdu<<endl;
124 if(nhdu<=0) {Delete(); return;}
125
126 // Get HDU for bin table
127 // si IHdu <=0 on cherche la 1ere bin table
128 // sinon on se positionne sur IHdu
129 if(IHdu<=0 || IHdu>nhdu)
130 for(int ihdu=1;ihdu<=nhdu;ihdu++) {
131 if(fits_movabs_hdu(FitsPtr,ihdu,&hdutype,&sta)) printerror(sta);
132 if(DbgLevel) cout<<"...Init ihdu="
133 <<ihdu<<" hdutype="<<hdutype<<endl;
134 if(hdutype==BINARY_TBL || hdutype==ASCII_TBL) {IHdu = ihdu; break;}
135 }
136 if(IHdu<=0 || IHdu>nhdu) {
137 cout<<"NO BINARY or ASCII hdu found"<<endl;
138 Delete();
139 return;
140 }
141 if(fits_movabs_hdu(FitsPtr,IHdu,&hdutype,&sta)) printerror(sta);
142 if(hdutype!=BINARY_TBL && hdutype!=ASCII_TBL) {Delete(); return;}
143
144 // Get number of columns
145 if(fits_get_num_cols(FitsPtr,&NBcol,&sta)) printerror(sta);
146 if(DbgLevel) cout<<"...Init NBcol="<<NBcol<<endl;
147 if(NBcol<1) {Delete(); return;}
148 mRet = new double[NBcol];
149 ColReadable = new bool[NBcol];
150 Buffer = new double*[NBcol];
151 for(int icol=0;icol<NBcol;icol++)
152 {Buffer[icol]=NULL; ColReadable[icol]=false;}
153
154 // Get number of rows
155 if(fits_get_num_rows(FitsPtr,&NBline,&sta)) printerror(sta);
156 if(DbgLevel) cout<<"...Init NBline="<<NBline<<endl;
157 if(NBline<1) {Delete(); return;}
158
159 // Get column infos
160 for(int icol=0;icol<NBcol;icol++) {
161 int colnum,typecode;
162 char templt[16], colname[256];
163 sprintf(templt,"%d",icol+1);
164 if(fits_get_colname(FitsPtr,CASESEN,templt,colname,&colnum,&sta))
165 printerror(sta);
166 ColName.push_back(colname);
167 if(fits_get_coltype(FitsPtr,icol+1,&typecode,NULL,NULL,&sta))
168 printerror(sta);
169 if(typecode!=TSTRING && typecode!=TCOMPLEX && typecode!=TDBLCOMPLEX)
170 ColReadable[icol] = true;
171 if(DbgLevel)
172 cout<<"...Init col="<<icol+1
173 <<" name="<<ColName[icol]
174 <<" typecode="<<typecode<<" (readable="
175 <<ColReadable[icol]<<")"<<endl;
176 }
177 // Prepare buffer
178 AllocBuff();
179
180}
181
182/////////////////////////////////////////////////
183uint_4 FitsBTNtuIntf::NbLines() const
184{
185 return NBline;
186}
187
188uint_4 FitsBTNtuIntf::NbColumns() const
189{
190 return NBcol;
191}
192
193r_8 * FitsBTNtuIntf::GetLineD(int n) const
194// Attention: n [0,NBline[, cfistio veut [1,NBline]
195{
196 int sta=0,anynul;
197 if(n<0 || n>=NBline) return NULL;
198
199 // Pas de bufferisation, on lit betement
200 if(BuffLen==1) {
201 NFitsRead++;
202 for(int icol=0;icol<NBcol;icol++) {
203 mRet[icol] = 0.;
204 if(!ColReadable[icol]) continue;
205 fits_read_col_dbl(FitsPtr,icol+1,n+1,1,1,NulVal,&mRet[icol],&anynul,&sta);
206 if(sta) printerror(sta);
207 }
208 return mRet;
209 }
210
211 // Gestion avec bufferisation
212 if(!Buffer) {
213 cout<<"FitsBTNtuIntf::GetLineD Buffer not allocated"<<endl;
214 return NULL;
215 }
216 if(n<LineDeb || n>LineFin) {
217 NFitsRead++;
218 long row1,row2,nrow;
219 if(BuffSens>0) { // Cas remplissage forward
220 row1 = n+1;
221 row2 = row1+BuffLen-1; if(row2>NBline) row2 = NBline;
222 } else if(BuffSens<0) { // Cas remplissage forward
223 row2 = n+1;
224 row1 = row2-BuffLen+1; if(row1<1) row1 = 1;
225 } else { // Cas remplissage centre
226 row1 = n+1 - BuffLen/2; if(row1<1) row1 = 1;
227 row2 = n+1 + BuffLen/2; if(row2>NBline) row2 = NBline;
228 }
229 nrow = row2 - row1 + 1;
230 LineDeb = row1-1; LineFin = row2-1;
231 //cout<<"DBG-FitsRead: row1="<<row1<<" row2="<<row2<<" nrow="<<nrow
232 // <<" LineDeb,Fin="<<LineDeb<<","<<LineFin<<endl;
233 for(int icol=0;icol<NBcol;icol++) {
234 if(!ColReadable[icol]) continue;
235 fits_read_col_dbl(FitsPtr,icol+1,row1,1,nrow,NulVal,Buffer[icol],&anynul,&sta);
236 if(sta) printerror(sta);
237 }
238 }
239
240 int_4 ibuf = n-LineDeb;
241 for(int icol=0;icol<NBcol;icol++)
242 if(ColReadable[icol]) mRet[icol] = (Buffer[icol])[ibuf];
243 else mRet[icol] = 0.;
244
245 return mRet;
246}
247
248r_8 FitsBTNtuIntf::GetCell(int n, int k) const
249{
250 if(n<0 || n>=NBline || k<0 || k>=NBcol) return 0.;
251 r_8 * r = GetLineD(n);
252 return r[k];
253}
254
255/*!
256 Retourne une chaine de caracteres avec la declaration des noms de
257 variables. si "nomx!=NULL" , des instructions d'affectation
258 a partir d'un tableau "nomx[i]" sont ajoutees (pour interface NTuple).
259*/
260string FitsBTNtuIntf::VarList_C(const char* nomx) const
261{
262 string rets;
263 rets = "\ndouble";
264 for(int icol=0;icol<NBcol;icol++) {
265 rets += " " + ColName[icol];
266 if(icol!=NBcol-1) rets += ","; else rets += ";\n";
267 }
268 if(!nomx) return rets;
269 char buff[256];
270 for(int icol=0;icol<NBcol;icol++) {
271 sprintf(buff," = %s[%d];\n",nomx,icol);
272 rets += ColName[icol] + buff;
273 }
274 return rets;
275}
276
277/////////////////////////////////////////////////
278void FitsBTNtuIntf::printerror(int sta) const
279{
280 int stat = sta;
281 fits_report_error(stdout,stat);
282 return;
283}
284
285void FitsBTNtuIntf::Print(ostream& os,int lp) const
286{
287 os<<"FitsBTNtuIntf:Print ("<<BuffLen<<","<<NulVal<<")"
288 <<" ncols="<<NBcol<<" nrows="<<NBline;
289 if(lp>0) os<<" fitsrd="<<NFitsRead<<"*ncols";
290 os<<"\n"<<FitsFN<<"["<<IHdu<<"]"<<endl;
291 if(NBcol<=0 || lp<1) return;
292 for(int icol=0;icol<NBcol;icol++) {
293 os<<" "<<ColName[icol]<<"("<<ColReadable[icol]<<")";
294 if(icol%4==3 || icol==NBcol-1) os<<endl;
295 }
296}
Note: See TracBrowser for help on using the repository browser.