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

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

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

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);
92 else if (ss == FitsFile::FitsDataType_char) SfitsCol.push_back(k);
[860]93 else {
[1351]94 cout << " FITS_XNTuple: colonne fits " << k << " type= " << (int) ss << endl;
[860]95 throw IOExc("type de champ inconnu");
96 }
97 }
98 char ** ColName = new char*[nbcols];
99 int compt=0;
[923]100 for (k=0; k<DfitsCol.size(); k++)
[860]101 {
102 ColName[compt] = new char[LONNOM+1];
[1136]103 strncpy(ColName[compt], is.ColNameFromFits(DfitsCol[k]).c_str(), LONNOM);
[860]104 ColName[compt++][ LONNOM] = '\0';
105 }
[923]106 for (k=0; k<FfitsCol.size(); k++)
[860]107 {
108 ColName[compt] = new char[LONNOM+1];
[1136]109 strncpy(ColName[compt], is.ColNameFromFits(FfitsCol[k]).c_str(), LONNOM);
[860]110 ColName[compt++][ LONNOM] = '\0';
111 }
[923]112 for (k=0; k<IfitsCol.size(); k++)
[860]113 {
114 ColName[compt] = new char[LONNOM+1];
[1136]115 strncpy(ColName[compt], is.ColNameFromFits(IfitsCol[k]).c_str(), LONNOM);
[860]116 ColName[compt++][ LONNOM] = '\0';
117 }
[923]118 for (k=0; k<SfitsCol.size(); k++)
[860]119 {
120 ColName[compt] = new char[LONNOM+1];
[1136]121 strncpy(ColName[compt], is.ColNameFromFits(SfitsCol[k]).c_str(), LONNOM);
[860]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);
[1047]128 ownobj_ = true;
[860]129 }
130 else
131 {
[1047]132 if (ownobj_)
133 {
[1300]134 (*dobj_)= XNTuple(DfitsCol.size(), FfitsCol.size(), IfitsCol.size(), SfitsCol.size(),ColName);
[1047]135 }
136 else
137 {
[1300]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 }
[1047]143 }
[860]144 }
[1047]145
[923]146 for (k=0; k<nbcols;k++)
[860]147 {
148 delete [] ColName[k];
149 }
150 delete [] ColName;
151
[1143]152 double* dligne;
153 float* fligne;
154 int* iligne;
[1047]155 char** cligne;
156
[1143]157 if (DfitsCol.size()>0) dligne = new double[DfitsCol.size()];
[860]158 else dligne=NULL;
[1143]159 if (FfitsCol.size()>0) fligne = new float[FfitsCol.size()];
[860]160 else fligne=NULL;
[1143]161 if (IfitsCol.size()>0) iligne = new int[IfitsCol.size()];
[860]162 else iligne=NULL;
163 if (SfitsCol.size()>0)
164 {
[1047]165 cligne = new char*[SfitsCol.size()];
[860]166 int taille_des_chaines=0;
[1136]167 for (k=0; k< SfitsCol.size(); k++) taille_des_chaines = max( taille_des_chaines, is.ColStringLengthFromFits(SfitsCol[k]) );
[1047]168 for (k=0; k<SfitsCol.size(); k++) cligne[k]=new char[taille_des_chaines+1];
[860]169 }
170 else cligne=NULL;
[1047]171 int firstln, lastln;
172 if (numberOfLinesToBeRead_ > 0)
[860]173 {
[1047]174 firstln = fistLineToBeRead_;
175 lastln = firstln + numberOfLinesToBeRead_;
[860]176 }
[1047]177 else
[860]178 {
[1047]179 firstln = 0;
180 lastln = nbentries;
[860]181 }
[1047]182 int numLigne;
183 for (numLigne=firstln; numLigne < lastln; numLigne++)
[860]184 {
[1136]185 is.GetBinTabLine(numLigne, dligne, fligne, iligne, cligne );
[1143]186 dobj_->Fill((r_8*)dligne, (r_4*)fligne, (int_4*)iligne, cligne);
[860]187 }
[1047]188 delete [] dligne;
189 delete [] fligne;
190 delete [] iligne;
191 for (k=0; k< SfitsCol.size(); k++) delete [] cligne[k];
192 delete [] cligne;
[1136]193 dobj_->Info()=is.DVListFromFits();
[860]194
195}
[1136]196void FITS_XNTuple::WriteToFits(FitsOutFile& os)
[860]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
[1194]211 string extname("XNTuple_Binary_tbl");
212 vector<string> Noms(ncols);
[923]213 int k;
214 for (k=0; k< ncols; k++)
[860]215 {
[1194]216 Noms[k] = dobj_->NomIndex(k) ;
[860]217 }
[1194]218 string types;
[923]219 for (k=0; k<dobj_->NDVar();k++)
[860]220 {
[1194]221 types+='D';
[860]222 }
[923]223 for (k=0; k<dobj_->NFVar();k++)
[860]224 {
[1194]225 types+='E';
[860]226 }
[923]227 for (k=0; k<dobj_->NIVar();k++)
[860]228 {
[1194]229 types+='I';
[860]230 }
[923]231 for (k=0; k<dobj_->NSVar();k++)
[860]232 {
[1194]233 types+='A';
[860]234 }
235 vector<int> StringSizes(dobj_->NSVar());
[923]236 for (k=0; k< StringSizes.size(); k++) StringSizes[k]=dobj_->mStrSz;
[1300]237 dvl["Content"]= "XNTuple";
238 dvl.SetComment("Content", "name of SOPHYA object");
[1221]239 os.makeHeaderBntblOnFits(types, Noms, nrows, ncols, &dvl, extname,StringSizes);
[1136]240
[1194]241 int compt=0;
[1136]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);
[1210]248 os.PutColToFits(compt, nrows, dcolumn);
[1136]249 compt++;
250 }
251 delete [] dcolumn;
252 }
[860]253
[1136]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);
[1210]260 os.PutColToFits(compt, nrows, fcolumn);
[1136]261 compt++;
262 }
263 delete [] fcolumn;
264 }
265
266 if (dobj_->NIVar() > 0)
267 {
[1352]268 int_4* icolumn = new int_4[nrows];
[1136]269 for (k=0; k<dobj_->NIVar();k++)
270 {
271 for(int j = 0; j < nrows; j++) icolumn[j]= dobj_->GetIVal(j,compt);
[1210]272 os.PutColToFits(compt, nrows, icolumn);
[1136]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 }
[1210]290 os.PutColToFits(compt, nrows, ccolumn);
[1136]291 compt++;
292 for(j = 0; j < nrows; j++)
293 {
294 delete [] ccolumn[j];
295 ccolumn[j] = NULL;
296 }
[1194]297
[1136]298 }
299 delete [] ccolumn;
300 }
[860]301}
Note: See TracBrowser for help on using the repository browser.