source: Sophya/trunk/SophyaExt/FitsIOServer/fitsntuple.cc@ 3167

Last change on this file since 3167 was 3067, checked in by ansari, 19 years ago

correction bug (ancien) ds ecriture Ntuple en FITS - Reza 02/09/2006

File size: 5.7 KB
RevLine 
[2615]1#include "sopnamsp.h"
[860]2#include "pexceptions.h"
3#include "fitsntuple.h"
4///////////////////////////////////////////////////////////
5// Les objets delegues pour la gestion de persistance sur fichiers fits
6// pout NTuple
7///////////////////////////////////////////////////////////
8
9
10#define LONNOM 8
11#define LONNOM1 (LONNOM+1)
12
13
[1371]14/*!
15 \class SOPHYA::FITS_NTuple
16 \ingroup FitsIOServer
17 FITS format I/O handler for SOPHYA::NTuple objects.
18*/
[860]19
20FITS_NTuple::FITS_NTuple()
21{
22 dobj_ = new NTuple;
[1047]23 InitNull();
24 ownobj_ = true;
[860]25}
26
27FITS_NTuple::FITS_NTuple(char inputfile[],int hdunum)
28{
29 dobj_ = new NTuple;
[1047]30 InitNull();
31 ownobj_ = true;
[860]32
[1136]33 Read(inputfile,hdunum);
[860]34}
35
36
37FITS_NTuple::FITS_NTuple(const NTuple & obj)
38{
39 dobj_ = new NTuple(obj);
[1047]40 InitNull();
41 ownobj_ = true;
[860]42}
[1047]43FITS_NTuple::FITS_NTuple(NTuple* obj)
44{
45 dobj_ = obj;
46 InitNull();
47 ownobj_ = false;
48}
[860]49
50
51FITS_NTuple::~FITS_NTuple()
52{
[1047]53 if (ownobj_ && dobj_ != NULL) delete dobj_;
[1136]54 // if (column_ != NULL) delete [] column_;
[860]55}
56
[2897]57int FITS_NTuple::CheckReadability(FitsInOutFile& is)
58{
59 if (is.CurrentHDUType() == IMAGE_HDU ) return 0;
60 string key = "SOPCLSNM";
61 string clsnm = is.KeyValue(key);
62 if (clsnm == "SOPHYA::NTuple") return 2;
[2898]63 key = "Content";
64 if (is.KeyValue(key) == "NTuple") return 2;
[2897]65 else return 1;
66}
67
[1136]68//void FITS_NTuple::Read(char inputfile[],int hdunum)
69//{
70// ReadF(inputfile,hdunum);
71//}
[1047]72void FITS_NTuple::ReadLines(char inputfile[],int firstLine, int numberOfLines,int hdunum)
73{
74 fistLineToBeRead_ = firstLine;
75 numberOfLinesToBeRead_ = numberOfLines;
[1136]76 Read(inputfile,hdunum);
[1047]77}
78
79
80
[1136]81//void FITS_NTuple::Write(char outputfile[], bool OldFile)
82//{
83// WriteF(outputfile, OldFile);
84//}
[860]85
[1136]86void FITS_NTuple::ReadFromFits(FitsInFile& is)
[860]87{
[1136]88 if (!is.IsFitsTable())
[860]89 {
90 throw PException("ReadFromFits: the fits file seems not to be a bintable nor ASCII table");
91 }
92 int nbcols, nbentries;
[1136]93 nbcols = is.NbColsFromFits();
[860]94 nbentries = 0;
[923]95 int k;
[1136]96 for (k=0; k<nbcols; k++) nbentries=max( nbentries, is.NentriesFromFits(k) );
[860]97
98 char ** ColName = new char*[nbcols];
99
[923]100 for (k=0; k<nbcols;k++)
[860]101 {
102 ColName[k] = new char[LONNOM1];
[1136]103 strncpy(ColName[k], is.ColNameFromFits(k).c_str(),LONNOM);
[860]104 ColName[k][LONNOM] = '\0';
105 }
[2907]106 // Reza Dec 2006 : Colonnes du NTuple de type double par defaut
[923]107 for (k=0; k<nbcols;k++)
[860]108 {
[1300]109 FitsFile::FitsDataType ss= is.ColTypeFromFits(k);
[860]110 string type;
[2907]111 if ( (ss != FitsFile::FitsDataType_double) && (ss != FitsFile::FitsDataType_float) )
[860]112 {
[2907]113 if (ss == FitsFile::FitsDataType_int) type= string("integer");
[860]114 else
[2907]115 if (ss == FitsFile::FitsDataType_char) type = string("char*");
[860]116 else
[2907]117 type = string("unknown");
[860]118 cout << " WARNING: the column " << k << " on fits file is not float but : " << type << endl;
119 }
120 }
[2907]121 bool fgdouble = true;
122 if ( is.ColTypeFromFits(0) == FitsFile::FitsDataType_float ) fgdouble = false;
[860]123 if(dobj_ == NULL)
124 {
[2907]125 dobj_= new NTuple(nbcols,ColName,512,fgdouble);
[1047]126 ownobj_ = true;
[860]127 }
128 else
129 {
130 dobj_->Clean();
[2907]131 (*dobj_) = NTuple(nbcols,ColName,512,fgdouble);
[860]132 }
[923]133 for (k=0; k<nbcols;k++)
[860]134 {
135 delete [] ColName[k];
136 }
137 delete [] ColName;
138
[2907]139 // Reza Dec 2006 : Colonnes du NTuple de type double par defaut
140 double* dligne = NULL;
141 float* fligne = NULL;
142 if (fgdouble) dligne = new double[nbcols];
143 else fligne = new float[nbcols];
[1047]144
[1136]145 int firstln, lastln;
146 if (numberOfLinesToBeRead_ > 0)
147 {
148 firstln = fistLineToBeRead_;
149 lastln = firstln + numberOfLinesToBeRead_;
150 }
151 else
152 {
153 firstln = 0;
154 lastln = nbentries;
155 }
156 int numLigne;
[2907]157 if (fgdouble)
158 for (numLigne=firstln; numLigne < lastln; numLigne++) {
159 is.GetBinTabLine(numLigne, dligne);
160 dobj_->Fill((r_8*)dligne);
[1136]161 }
[2907]162 else
163 for (numLigne=firstln; numLigne < lastln; numLigne++) {
164 is.GetBinTabLine(numLigne, fligne);
165 dobj_->Fill((r_4*)fligne);
166 }
167
168 if (dligne) delete [] dligne;
169 if (fligne) delete [] fligne;
[1047]170
[1136]171 dobj_->Info()=is.DVListFromFits();
172}
[1047]173
[1136]174void FITS_NTuple::WriteToFits(FitsOutFile& os)
[860]175{
176 if(dobj_ == NULL)
177 {
178 cout << " WriteToFits:: dobj_= null " << endl;
179 return;
180 }
181
182 // table will have 'ncols' columns
183 int ncols = dobj_->NVar();
184
185 // table will have 'nrows' rows
186 int nentries = dobj_->NEntry();
187
188 // get names and values from the join DVList object
189 DVList dvl= dobj_->Info();
[1300]190 dvl["Content"]= "NTuple";
191 dvl.SetComment("Content", "name of SOPHYA object");
[2897]192 dvl["SOPCLSNM"]= "SOPHYA::NTuple";
193 dvl.SetComment("SOPCLSNM", "SOPHYA class name");
[860]194 // extension name
[2897]195 // string extname("NTuple_Binary_tbl");
196 string extname = os.NextExtensionName();
[860]197
[1194]198 vector<string> Noms(ncols);
[923]199 int k;
200 for (k=0; k< ncols; k++)
[860]201 {
[1194]202 Noms[k]= dobj_->NomIndex(k);
[860]203 }
[1136]204
[2907]205 // Gestion NTuple en double ou float / Reza Jan 2006
206 if (dobj_->mFgDouble) { // NTuple avec colonnes double
207 string type(ncols, 'D');
208 vector<int> dummy;
[3067]209 // Reza 09/2006 : nentries -> 1 pour eviter les colonnes de type vecteur
210 os.makeHeaderBntblOnFits(type,Noms, 1, ncols, &dvl, extname, dummy);
[2907]211 double* column = new double[nentries];
212 for (k=0; k<ncols;k++)
213 {
214 int j;
215 for(j = 0; j < nentries; j++) column[j]= dobj_->GetCell(j,k);
216 os.PutColToFits(k, nentries, column);
217 }
218 delete [] column;
219 }
220 else { // NTuple avec colonnes float
221 string type(ncols, 'E');
222 vector<int> dummy;
[3067]223 // Reza 09/2006 : nentries -> 1 pour eviter les colonnes de type vecteur
224 os.makeHeaderBntblOnFits(type,Noms, 1, ncols, &dvl, extname, dummy);
[2907]225 float* column = new float[nentries];
226 for (k=0; k<ncols;k++)
227 {
228 int j;
229 for(j = 0; j < nentries; j++) column[j]= dobj_->GetVal(j,k);
230 os.PutColToFits(k, nentries, column);
231 }
232 delete [] column;
233 }
[860]234}
235
Note: See TracBrowser for help on using the repository browser.