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

Last change on this file since 3066 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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