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

Last change on this file since 1418 was 1371, checked in by ansari, 25 years ago

MAJ documentation, Makefile, ... - Reza 5/1/2001

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