source: Sophya/trunk/SophyaExt/FitsIOServer/fitsxntuple.cc@ 2233

Last change on this file since 2233 was 2209, checked in by lemeur, 23 years ago

mise aux normes de for(int k= ...)

File size: 9.3 KB
Line 
1#include "pexceptions.h"
2#include "fitsxntuple.h"
3///////////////////////////////////////////////////////////
4// Les objets delegues pour la gestion de persistance sur fichiers fits
5// pout XNTuple
6///////////////////////////////////////////////////////////
7
8/*!
9 \class SOPHYA::FITS_XNTuple
10 \ingroup FitsIOServer
11 FITS format I/O handler for SOPHYA::XNTuple objects.
12*/
13
14
15#define LONNOM 31
16
17
18FITS_XNTuple::FITS_XNTuple()
19{
20 dobj_ = new XNTuple;
21 InitNull();
22 ownobj_ = true;
23}
24
25FITS_XNTuple::FITS_XNTuple(char inputfile[],int hdunum)
26{
27 dobj_ = new XNTuple;
28 InitNull();
29 ownobj_ = true;
30
31 Read(inputfile,hdunum);
32}
33
34
35FITS_XNTuple::FITS_XNTuple(const XNTuple & obj)
36{
37 dobj_ = new XNTuple(obj);
38 InitNull();
39 ownobj_ = true;
40}
41FITS_XNTuple::FITS_XNTuple(XNTuple* obj)
42{
43 dobj_ = obj;
44 InitNull();
45 ownobj_ = false;
46}
47FITS_XNTuple::~FITS_XNTuple()
48{
49 if (ownobj_ && dobj_ != NULL) delete dobj_;
50}
51
52
53void FITS_XNTuple::ReadLines(char inputfile[],int firstLine, int numberOfLines,int hdunum)
54{
55 fistLineToBeRead_ = firstLine;
56 numberOfLinesToBeRead_ = numberOfLines;
57 Read(inputfile,hdunum);
58}
59
60
61
62void FITS_XNTuple::ReadFromFits(FitsInFile& is)
63{
64 if (!is.IsFitsTable())
65 {
66 throw PException("ReadFromFits: the fits file seems not to be a bintable nor ASCII table");
67 }
68 int nbcols, nbentries;
69 nbcols = is.NbColsFromFits();
70 nbentries = 0;
71 int k;
72 for (k=0; k<nbcols; k++) nbentries=max( nbentries, is.NentriesFromFits(k) );
73
74 //
75 // pour mettre les colonnes dans l'ordre double, float, int, char :
76 // tableau de correspondance
77 // DfitsCol(j)= numero dans le fichier fits de la jeme variable double du
78 // xntuple;
79 // FfitsCol(j)= numero dans le fichier fits de la jeme variable float du
80 // xntuple;
81 // etc.
82 vector<int> DfitsCol;
83 vector<int> FfitsCol;
84 vector<int> IfitsCol;
85 vector<int> SfitsCol;
86 for (k=0; k<nbcols;k++)
87 {
88
89 FitsFile::FitsDataType ss= is.ColTypeFromFits(k);
90 switch (ss)
91 {
92 case FitsFile::FitsDataType_double :
93 {
94 DfitsCol.push_back(k);
95 break;
96 }
97 case FitsFile::FitsDataType_float :
98 {
99 FfitsCol.push_back(k);
100 break;
101 }
102 case FitsFile::FitsDataType_int :
103 {
104 IfitsCol.push_back(k);
105 break;
106 }
107 case FitsFile::FitsDataType_long :
108 {
109 IfitsCol.push_back(k);
110 break;
111 }
112 case FitsFile::FitsDataType_byte :
113 {
114 IfitsCol.push_back(k);
115 break;
116 }
117 case FitsFile::FitsDataType_char :
118 {
119 SfitsCol.push_back(k);
120 break;
121 }
122 default :
123 {
124 cout << " FITS_XNTuple: colonne fits " << k << " type= " << (int) ss << endl;
125 throw IOExc("type de champ inconnu");
126 }
127 }
128 }
129 char ** ColName = new char*[nbcols];
130 int compt=0;
131 for (k=0; k<DfitsCol.size(); k++)
132 {
133 ColName[compt] = new char[LONNOM+1];
134 strncpy(ColName[compt], is.ColNameFromFits(DfitsCol[k]).c_str(), LONNOM);
135 ColName[compt++][ LONNOM] = '\0';
136 }
137 for (k=0; k<FfitsCol.size(); k++)
138 {
139 ColName[compt] = new char[LONNOM+1];
140 strncpy(ColName[compt], is.ColNameFromFits(FfitsCol[k]).c_str(), LONNOM);
141 ColName[compt++][ LONNOM] = '\0';
142 }
143 for (k=0; k<IfitsCol.size(); k++)
144 {
145 ColName[compt] = new char[LONNOM+1];
146 strncpy(ColName[compt], is.ColNameFromFits(IfitsCol[k]).c_str(), LONNOM);
147 ColName[compt++][ LONNOM] = '\0';
148 }
149 for (k=0; k<SfitsCol.size(); k++)
150 {
151 ColName[compt] = new char[LONNOM+1];
152 strncpy(ColName[compt], is.ColNameFromFits(SfitsCol[k]).c_str(), LONNOM);
153 ColName[compt++][LONNOM] = '\0';
154 }
155 int ND = DfitsCol.size();
156 int NF = FfitsCol.size();
157 int NI = IfitsCol.size();
158 int NS = SfitsCol.size();
159 if(dobj_ == NULL)
160 {
161 dobj_= new XNTuple(ND, NF, NI, NS,ColName);
162 ownobj_ = true;
163 }
164 else
165 {
166 if (ownobj_)
167 {
168 (*dobj_)= XNTuple(ND, NF, NI, NS,ColName);
169 }
170 else
171 {
172 if (ND != dobj_->NDVar() || NF != dobj_->NFVar() || NI != dobj_->NIVar() || NS != dobj_->NSVar())
173 {
174 // cout << " WARNING : FITS_XNTuple : XNTuple reconfigured " << endl;
175 (*dobj_)= XNTuple(ND, NF, NI, NS,ColName);
176 }
177 }
178 }
179
180 for (k=0; k<nbcols;k++)
181 {
182 delete [] ColName[k];
183 }
184 delete [] ColName;
185
186 r_8* dligne;
187 r_4* fligne;
188 int_4* iligne;
189 char** cligne;
190
191 if (ND>0) dligne = new double[ND];
192 else dligne=NULL;
193 if (NF>0) fligne = new float[NF];
194 else fligne=NULL;
195 if (NI) iligne = new int[NI];
196 else iligne=NULL;
197 if (NS)
198 {
199 cligne = new char*[NS];
200 int taille_des_chaines=0;
201 for (k=0; k< NS; k++) taille_des_chaines = max( taille_des_chaines, is.ColStringLengthFromFits(SfitsCol[k]) );
202 for (k=0; k<NS; k++) cligne[k]=new char[taille_des_chaines+1];
203 }
204 else cligne=NULL;
205 int firstln, lastln;
206 if (numberOfLinesToBeRead_ > 0)
207 {
208 firstln = fistLineToBeRead_;
209 lastln = firstln + numberOfLinesToBeRead_;
210 }
211 else
212 {
213 firstln = 0;
214 lastln = nbentries;
215 }
216 int numLigne;
217 for (numLigne=firstln; numLigne < lastln; numLigne++)
218 {
219 const FitsFile::BufferLine& bfligne = is.GetBufferLine(numLigne);
220 int k;
221 int rang;
222 int dcount =0;
223 int fcount =0;
224 int icount =0;
225 int ccount = 0;
226 for (k=0; k<nbcols;k++)
227 {
228 rang = bfligne.identificateur()[k].second;
229 switch (bfligne.identificateur()[k].first)
230 {
231 case FitsFile::FitsDataType_double :
232 {
233 dligne[dcount++] = bfligne.r_8Array(rang);
234 break;
235 }
236 case FitsFile::FitsDataType_float :
237 {
238 fligne[fcount++] = bfligne.r_4Array(rang);
239 break;
240 }
241 case FitsFile::FitsDataType_short :
242 {
243 iligne[icount++] = bfligne.int_2Array(rang);
244 break;
245 }
246 case FitsFile::FitsDataType_int :
247 {
248 iligne[icount++] = bfligne.int_4Array(rang);
249 break;
250 }
251 case FitsFile::FitsDataType_long :
252 {
253 iligne[icount++] = (int_4)bfligne.int_8Array(rang);
254 break;
255 }
256 case FitsFile::FitsDataType_byte :
257 {
258 iligne[icount++] = (int_4)bfligne.u_charArray(rang);
259 break;
260 }
261 case FitsFile::FitsDataType_char :
262 {
263 strncpy( cligne[ccount++], bfligne.stringArray(rang).c_str(),bfligne.stringArray(rang).length());
264 break;
265 }
266
267 default:
268 {
269 throw PException(" FITS_XNTuple::ReadFromFits : unsupported FITS data type");
270 }
271 }
272
273 }
274 // is.GetBinTabLine(numLigne, dligne, fligne, iligne, cligne );
275 // dobj_->Fill((r_8*)dligne, (r_4*)fligne, (int_4*)iligne, cligne);
276 dobj_->Fill(dligne, fligne, iligne, cligne);
277 }
278 delete [] dligne;
279 delete [] fligne;
280 delete [] iligne;
281 for (k=0; k< SfitsCol.size(); k++) delete [] cligne[k];
282 delete [] cligne;
283 dobj_->Info()=is.DVListFromFits();
284
285}
286void FITS_XNTuple::WriteToFits(FitsOutFile& os)
287{
288
289 if(dobj_ == NULL)
290 {
291 cout << " WriteToFits:: dobj_= null " << endl;
292 return;
293 }
294
295 // table will have 'ncols' columns
296 int ncols = dobj_->NVar();
297 // table will have 'nrows' rows
298 int nrows = dobj_->NEntry();
299 // cout << " FITS_XNTuple::WriteToFits : nombre de lignes a ecrire " << nrows << endl;
300 // get names and values from the join DVList object
301 DVList dvl= dobj_->Info();
302 // extension name
303 string extname("XNTuple_Binary_tbl");
304 vector<string> Noms(ncols);
305 int k;
306 for (k=0; k< ncols; k++)
307 {
308 Noms[k] = dobj_->NomIndex(k) ;
309 }
310 string types;
311 for (k=0; k<dobj_->NDVar();k++)
312 {
313 types+='D';
314 }
315 for (k=0; k<dobj_->NFVar();k++)
316 {
317 types+='E';
318 }
319 for (k=0; k<dobj_->NIVar();k++)
320 {
321 types+='J';
322 }
323 for (k=0; k<dobj_->NSVar();k++)
324 {
325 types+='A';
326 }
327 vector<int> StringSizes(dobj_->NSVar());
328 for (k=0; k< StringSizes.size(); k++) StringSizes[k]=dobj_->mStrSz;
329 dvl["Content"]= "XNTuple";
330 dvl.SetComment("Content", "name of SOPHYA object");
331 os.makeHeaderBntblOnFits(types, Noms, nrows, ncols, &dvl, extname,StringSizes);
332
333 int compt=0;
334 if (dobj_->NDVar() > 0)
335 {
336 double* dcolumn = new double[nrows];
337 for (k=0; k<dobj_->NDVar();k++)
338 {
339 int j;
340 for(j = 0; j < nrows; j++) dcolumn[j]= dobj_->GetDVal(j,compt);
341 os.PutColToFits(compt, nrows, dcolumn);
342 compt++;
343 }
344 delete [] dcolumn;
345 }
346
347 if (dobj_->NFVar() > 0)
348 {
349 float* fcolumn = new float[nrows];
350 for (k=0; k<dobj_->NFVar();k++)
351 {
352 int j;
353 for(j = 0; j < nrows; j++) fcolumn[j]= dobj_->GetFVal(j,compt);
354 os.PutColToFits(compt, nrows, fcolumn);
355 compt++;
356 }
357 delete [] fcolumn;
358 }
359
360 if (dobj_->NIVar() > 0)
361 {
362 int_4* icolumn = new int_4[nrows];
363 for (k=0; k<dobj_->NIVar();k++)
364 {
365 int j;
366 for(j = 0; j < nrows; j++) icolumn[j]= dobj_->GetIVal(j,compt);
367 // for(j = 0; j < nrows; j++) cout << icolumn[j] << endl;;
368
369 os.PutColToFits(compt, nrows, icolumn);
370 compt++;
371 }
372 delete [] icolumn;
373 }
374
375 if (dobj_->NSVar() > 0)
376 {
377 char** ccolumn = new char*[nrows];
378 for (k=0; k<dobj_->NSVar();k++)
379 {
380 int j;
381 for(j = 0; j < nrows; j++)
382 {
383 string s= dobj_->GetSVal(j,compt);
384 ccolumn[j] = new char[dobj_->mStrSz+1];
385 strcpy(ccolumn[j],s.c_str());
386 }
387 os.PutColToFits(compt, nrows, ccolumn);
388 compt++;
389 for(j = 0; j < nrows; j++)
390 {
391 delete [] ccolumn[j];
392 ccolumn[j] = NULL;
393 }
394
395 }
396 delete [] ccolumn;
397 }
398}
Note: See TracBrowser for help on using the repository browser.