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

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