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

Last change on this file since 2130 was 1499, checked in by lemeur, 24 years ago

lecture xntuple par blocs et complement de types

File size: 7.7 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 FitsFile::FitsDataType ss= is.ColTypeFromFits(k);
89 if (ss == FitsFile::FitsDataType_double) DfitsCol.push_back(k);
90 else if (ss == FitsFile::FitsDataType_float) FfitsCol.push_back(k);
91 else if (ss == FitsFile::FitsDataType_int) IfitsCol.push_back(k);
92 else if (ss == FitsFile::FitsDataType_long) IfitsCol.push_back(k);
93 else if (ss == FitsFile::FitsDataType_byte) IfitsCol.push_back(k);
94 else if (ss == FitsFile::FitsDataType_char) SfitsCol.push_back(k);
95 else {
96 cout << " FITS_XNTuple: colonne fits " << k << " type= " << (int) ss << endl;
97 throw IOExc("type de champ inconnu");
98 }
99 }
100 char ** ColName = new char*[nbcols];
101 int compt=0;
102 for (k=0; k<DfitsCol.size(); k++)
103 {
104 ColName[compt] = new char[LONNOM+1];
105 strncpy(ColName[compt], is.ColNameFromFits(DfitsCol[k]).c_str(), LONNOM);
106 ColName[compt++][ LONNOM] = '\0';
107 }
108 for (k=0; k<FfitsCol.size(); k++)
109 {
110 ColName[compt] = new char[LONNOM+1];
111 strncpy(ColName[compt], is.ColNameFromFits(FfitsCol[k]).c_str(), LONNOM);
112 ColName[compt++][ LONNOM] = '\0';
113 }
114 for (k=0; k<IfitsCol.size(); k++)
115 {
116 ColName[compt] = new char[LONNOM+1];
117 strncpy(ColName[compt], is.ColNameFromFits(IfitsCol[k]).c_str(), LONNOM);
118 ColName[compt++][ LONNOM] = '\0';
119 }
120 for (k=0; k<SfitsCol.size(); k++)
121 {
122 ColName[compt] = new char[LONNOM+1];
123 strncpy(ColName[compt], is.ColNameFromFits(SfitsCol[k]).c_str(), LONNOM);
124 ColName[compt++][LONNOM] = '\0';
125 }
126 int ND = DfitsCol.size();
127 int NF = FfitsCol.size();
128 int NI = IfitsCol.size();
129 int NS = SfitsCol.size();
130 if(dobj_ == NULL)
131 {
132 dobj_= new XNTuple(ND, NF, NI, NS,ColName);
133 ownobj_ = true;
134 }
135 else
136 {
137 if (ownobj_)
138 {
139 (*dobj_)= XNTuple(ND, NF, NI, NS,ColName);
140 }
141 else
142 {
143 if (ND != dobj_->NDVar() || NF != dobj_->NFVar() || NI != dobj_->NIVar() || NS != dobj_->NSVar())
144 {
145 cout << " WARNING : FITS_XNTuple : XNTuple reconfigured " << endl;
146 (*dobj_)= XNTuple(ND, NF, NI, NS,ColName);
147 }
148 }
149 }
150
151 for (k=0; k<nbcols;k++)
152 {
153 delete [] ColName[k];
154 }
155 delete [] ColName;
156
157 double* dligne;
158 float* fligne;
159 int* iligne;
160 char** cligne;
161
162 if (ND>0) dligne = new double[ND];
163 else dligne=NULL;
164 if (NF>0) fligne = new float[NF];
165 else fligne=NULL;
166 if (NI) iligne = new int[NI];
167 else iligne=NULL;
168 if (NS)
169 {
170 cligne = new char*[NS];
171 int taille_des_chaines=0;
172 for (k=0; k< NS; k++) taille_des_chaines = max( taille_des_chaines, is.ColStringLengthFromFits(SfitsCol[k]) );
173 for (k=0; k<NS; k++) cligne[k]=new char[taille_des_chaines+1];
174 }
175 else cligne=NULL;
176 int firstln, lastln;
177 if (numberOfLinesToBeRead_ > 0)
178 {
179 firstln = fistLineToBeRead_;
180 lastln = firstln + numberOfLinesToBeRead_;
181 }
182 else
183 {
184 firstln = 0;
185 lastln = nbentries;
186 }
187 int numLigne;
188 for (numLigne=firstln; numLigne < lastln; numLigne++)
189 {
190 is.GetBinTabLine(numLigne, dligne, fligne, iligne, cligne );
191 dobj_->Fill((r_8*)dligne, (r_4*)fligne, (int_4*)iligne, cligne);
192 }
193 delete [] dligne;
194 delete [] fligne;
195 delete [] iligne;
196 for (k=0; k< SfitsCol.size(); k++) delete [] cligne[k];
197 delete [] cligne;
198 dobj_->Info()=is.DVListFromFits();
199
200}
201void FITS_XNTuple::WriteToFits(FitsOutFile& os)
202{
203
204 if(dobj_ == NULL)
205 {
206 cout << " WriteToFits:: dobj_= null " << endl;
207 return;
208 }
209
210 // table will have 'ncols' columns
211 int ncols = dobj_->NVar();
212 // table will have 'nrows' rows
213 int nrows = dobj_->NEntry();
214 cout << " FITS_XNTuple::WriteToFits : nombre de lignes a ecrire " << nrows << endl;
215 // get names and values from the join DVList object
216 DVList dvl= dobj_->Info();
217 // extension name
218 string extname("XNTuple_Binary_tbl");
219 vector<string> Noms(ncols);
220 int k;
221 for (k=0; k< ncols; k++)
222 {
223 Noms[k] = dobj_->NomIndex(k) ;
224 }
225 string types;
226 for (k=0; k<dobj_->NDVar();k++)
227 {
228 types+='D';
229 }
230 for (k=0; k<dobj_->NFVar();k++)
231 {
232 types+='E';
233 }
234 for (k=0; k<dobj_->NIVar();k++)
235 {
236 types+='I';
237 }
238 for (k=0; k<dobj_->NSVar();k++)
239 {
240 types+='A';
241 }
242 vector<int> StringSizes(dobj_->NSVar());
243 for (k=0; k< StringSizes.size(); k++) StringSizes[k]=dobj_->mStrSz;
244 dvl["Content"]= "XNTuple";
245 dvl.SetComment("Content", "name of SOPHYA object");
246 os.makeHeaderBntblOnFits(types, Noms, nrows, ncols, &dvl, extname,StringSizes);
247
248 int compt=0;
249 if (dobj_->NDVar() > 0)
250 {
251 double* dcolumn = new double[nrows];
252 for (k=0; k<dobj_->NDVar();k++)
253 {
254 for(int j = 0; j < nrows; j++) dcolumn[j]= dobj_->GetDVal(j,compt);
255 os.PutColToFits(compt, nrows, dcolumn);
256 compt++;
257 }
258 delete [] dcolumn;
259 }
260
261 if (dobj_->NFVar() > 0)
262 {
263 float* fcolumn = new float[nrows];
264 for (k=0; k<dobj_->NFVar();k++)
265 {
266 for(int j = 0; j < nrows; j++) fcolumn[j]= dobj_->GetFVal(j,compt);
267 os.PutColToFits(compt, nrows, fcolumn);
268 compt++;
269 }
270 delete [] fcolumn;
271 }
272
273 if (dobj_->NIVar() > 0)
274 {
275 int_4* icolumn = new int_4[nrows];
276 for (k=0; k<dobj_->NIVar();k++)
277 {
278 for(int j = 0; j < nrows; j++) icolumn[j]= dobj_->GetIVal(j,compt);
279 os.PutColToFits(compt, nrows, icolumn);
280 compt++;
281 }
282 delete [] icolumn;
283 }
284
285 if (dobj_->NSVar() > 0)
286 {
287 char** ccolumn = new char*[nrows];
288 for (k=0; k<dobj_->NSVar();k++)
289 {
290 int j;
291 for(j = 0; j < nrows; j++)
292 {
293 string s= dobj_->GetSVal(j,compt);
294 ccolumn[j] = new char[dobj_->mStrSz+1];
295 strcpy(ccolumn[j],s.c_str());
296 }
297 os.PutColToFits(compt, nrows, ccolumn);
298 compt++;
299 for(j = 0; j < nrows; j++)
300 {
301 delete [] ccolumn[j];
302 ccolumn[j] = NULL;
303 }
304
305 }
306 delete [] ccolumn;
307 }
308}
Note: See TracBrowser for help on using the repository browser.