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