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

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

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

File size: 9.3 KB
RevLine 
[860]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
[1371]8/*!
9 \class SOPHYA::FITS_XNTuple
10 \ingroup FitsIOServer
11 FITS format I/O handler for SOPHYA::XNTuple objects.
12*/
[860]13
[1371]14
[860]15#define LONNOM 31
16
17
18FITS_XNTuple::FITS_XNTuple()
19{
20 dobj_ = new XNTuple;
[1047]21 InitNull();
22 ownobj_ = true;
[860]23}
24
25FITS_XNTuple::FITS_XNTuple(char inputfile[],int hdunum)
26{
27 dobj_ = new XNTuple;
[1047]28 InitNull();
29 ownobj_ = true;
[860]30
[1136]31 Read(inputfile,hdunum);
[860]32}
33
34
35FITS_XNTuple::FITS_XNTuple(const XNTuple & obj)
36{
37 dobj_ = new XNTuple(obj);
[1047]38 InitNull();
39 ownobj_ = true;
[860]40}
[1047]41FITS_XNTuple::FITS_XNTuple(XNTuple* obj)
42{
43 dobj_ = obj;
44 InitNull();
45 ownobj_ = false;
46}
[860]47FITS_XNTuple::~FITS_XNTuple()
48{
[1047]49 if (ownobj_ && dobj_ != NULL) delete dobj_;
[860]50}
[1047]51
52
53void FITS_XNTuple::ReadLines(char inputfile[],int firstLine, int numberOfLines,int hdunum)
54{
55 fistLineToBeRead_ = firstLine;
56 numberOfLinesToBeRead_ = numberOfLines;
[1136]57 Read(inputfile,hdunum);
[1047]58}
59
60
[860]61
[1136]62void FITS_XNTuple::ReadFromFits(FitsInFile& is)
[860]63{
[1136]64 if (!is.IsFitsTable())
[860]65 {
66 throw PException("ReadFromFits: the fits file seems not to be a bintable nor ASCII table");
67 }
68 int nbcols, nbentries;
[1136]69 nbcols = is.NbColsFromFits();
[860]70 nbentries = 0;
[923]71 int k;
[1136]72 for (k=0; k<nbcols; k++) nbentries=max( nbentries, is.NentriesFromFits(k) );
[860]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;
[923]86 for (k=0; k<nbcols;k++)
[860]87 {
[2197]88
[1300]89 FitsFile::FitsDataType ss= is.ColTypeFromFits(k);
[2197]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 }
[860]128 }
129 char ** ColName = new char*[nbcols];
130 int compt=0;
[923]131 for (k=0; k<DfitsCol.size(); k++)
[860]132 {
133 ColName[compt] = new char[LONNOM+1];
[1136]134 strncpy(ColName[compt], is.ColNameFromFits(DfitsCol[k]).c_str(), LONNOM);
[860]135 ColName[compt++][ LONNOM] = '\0';
136 }
[923]137 for (k=0; k<FfitsCol.size(); k++)
[860]138 {
139 ColName[compt] = new char[LONNOM+1];
[1136]140 strncpy(ColName[compt], is.ColNameFromFits(FfitsCol[k]).c_str(), LONNOM);
[860]141 ColName[compt++][ LONNOM] = '\0';
142 }
[923]143 for (k=0; k<IfitsCol.size(); k++)
[860]144 {
145 ColName[compt] = new char[LONNOM+1];
[1136]146 strncpy(ColName[compt], is.ColNameFromFits(IfitsCol[k]).c_str(), LONNOM);
[860]147 ColName[compt++][ LONNOM] = '\0';
148 }
[923]149 for (k=0; k<SfitsCol.size(); k++)
[860]150 {
151 ColName[compt] = new char[LONNOM+1];
[1136]152 strncpy(ColName[compt], is.ColNameFromFits(SfitsCol[k]).c_str(), LONNOM);
[860]153 ColName[compt++][LONNOM] = '\0';
154 }
[1499]155 int ND = DfitsCol.size();
156 int NF = FfitsCol.size();
157 int NI = IfitsCol.size();
158 int NS = SfitsCol.size();
[860]159 if(dobj_ == NULL)
160 {
[1499]161 dobj_= new XNTuple(ND, NF, NI, NS,ColName);
[1047]162 ownobj_ = true;
[860]163 }
164 else
165 {
[1047]166 if (ownobj_)
167 {
[1499]168 (*dobj_)= XNTuple(ND, NF, NI, NS,ColName);
[1047]169 }
170 else
171 {
[1499]172 if (ND != dobj_->NDVar() || NF != dobj_->NFVar() || NI != dobj_->NIVar() || NS != dobj_->NSVar())
[1300]173 {
[2197]174 // cout << " WARNING : FITS_XNTuple : XNTuple reconfigured " << endl;
[1499]175 (*dobj_)= XNTuple(ND, NF, NI, NS,ColName);
[1300]176 }
[1047]177 }
[860]178 }
[1047]179
[923]180 for (k=0; k<nbcols;k++)
[860]181 {
182 delete [] ColName[k];
183 }
184 delete [] ColName;
185
[2197]186 r_8* dligne;
187 r_4* fligne;
188 int_4* iligne;
[1047]189 char** cligne;
190
[1499]191 if (ND>0) dligne = new double[ND];
[860]192 else dligne=NULL;
[1499]193 if (NF>0) fligne = new float[NF];
[860]194 else fligne=NULL;
[1499]195 if (NI) iligne = new int[NI];
[860]196 else iligne=NULL;
[1499]197 if (NS)
[860]198 {
[1499]199 cligne = new char*[NS];
[860]200 int taille_des_chaines=0;
[1499]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];
[860]203 }
204 else cligne=NULL;
[1047]205 int firstln, lastln;
206 if (numberOfLinesToBeRead_ > 0)
[860]207 {
[1047]208 firstln = fistLineToBeRead_;
209 lastln = firstln + numberOfLinesToBeRead_;
[860]210 }
[1047]211 else
[860]212 {
[1047]213 firstln = 0;
214 lastln = nbentries;
[860]215 }
[1047]216 int numLigne;
217 for (numLigne=firstln; numLigne < lastln; numLigne++)
[860]218 {
[2197]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);
[860]277 }
[1047]278 delete [] dligne;
279 delete [] fligne;
280 delete [] iligne;
281 for (k=0; k< SfitsCol.size(); k++) delete [] cligne[k];
282 delete [] cligne;
[1136]283 dobj_->Info()=is.DVListFromFits();
[860]284
285}
[1136]286void FITS_XNTuple::WriteToFits(FitsOutFile& os)
[860]287{
[1499]288
[860]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();
[2209]299 // cout << " FITS_XNTuple::WriteToFits : nombre de lignes a ecrire " << nrows << endl;
[860]300 // get names and values from the join DVList object
301 DVList dvl= dobj_->Info();
302 // extension name
[1194]303 string extname("XNTuple_Binary_tbl");
304 vector<string> Noms(ncols);
[923]305 int k;
306 for (k=0; k< ncols; k++)
[860]307 {
[1194]308 Noms[k] = dobj_->NomIndex(k) ;
[860]309 }
[1194]310 string types;
[923]311 for (k=0; k<dobj_->NDVar();k++)
[860]312 {
[1194]313 types+='D';
[860]314 }
[923]315 for (k=0; k<dobj_->NFVar();k++)
[860]316 {
[1194]317 types+='E';
[860]318 }
[923]319 for (k=0; k<dobj_->NIVar();k++)
[860]320 {
[2197]321 types+='J';
[860]322 }
[923]323 for (k=0; k<dobj_->NSVar();k++)
[860]324 {
[1194]325 types+='A';
[860]326 }
327 vector<int> StringSizes(dobj_->NSVar());
[923]328 for (k=0; k< StringSizes.size(); k++) StringSizes[k]=dobj_->mStrSz;
[1300]329 dvl["Content"]= "XNTuple";
330 dvl.SetComment("Content", "name of SOPHYA object");
[1221]331 os.makeHeaderBntblOnFits(types, Noms, nrows, ncols, &dvl, extname,StringSizes);
[1136]332
[1194]333 int compt=0;
[1136]334 if (dobj_->NDVar() > 0)
335 {
336 double* dcolumn = new double[nrows];
337 for (k=0; k<dobj_->NDVar();k++)
338 {
[2209]339 int j;
340 for(j = 0; j < nrows; j++) dcolumn[j]= dobj_->GetDVal(j,compt);
[1210]341 os.PutColToFits(compt, nrows, dcolumn);
[1136]342 compt++;
343 }
344 delete [] dcolumn;
345 }
[860]346
[1136]347 if (dobj_->NFVar() > 0)
348 {
349 float* fcolumn = new float[nrows];
350 for (k=0; k<dobj_->NFVar();k++)
351 {
[2209]352 int j;
353 for(j = 0; j < nrows; j++) fcolumn[j]= dobj_->GetFVal(j,compt);
[1210]354 os.PutColToFits(compt, nrows, fcolumn);
[1136]355 compt++;
356 }
357 delete [] fcolumn;
358 }
359
360 if (dobj_->NIVar() > 0)
361 {
[1352]362 int_4* icolumn = new int_4[nrows];
[1136]363 for (k=0; k<dobj_->NIVar();k++)
364 {
[2209]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;;
[2197]368
[1210]369 os.PutColToFits(compt, nrows, icolumn);
[1136]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 }
[1210]387 os.PutColToFits(compt, nrows, ccolumn);
[1136]388 compt++;
389 for(j = 0; j < nrows; j++)
390 {
391 delete [] ccolumn[j];
392 ccolumn[j] = NULL;
393 }
[1194]394
[1136]395 }
396 delete [] ccolumn;
397 }
[860]398}
Note: See TracBrowser for help on using the repository browser.