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

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

lecture xntuple par blocs et complement de types

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