1 | #include "sopnamsp.h"
|
---|
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 |
|
---|
14 | /*!
|
---|
15 | \class SOPHYA::FITS_NTuple
|
---|
16 | \ingroup FitsIOServer
|
---|
17 | FITS format I/O handler for SOPHYA::NTuple objects.
|
---|
18 | */
|
---|
19 |
|
---|
20 | FITS_NTuple::FITS_NTuple()
|
---|
21 | {
|
---|
22 | dobj_ = new NTuple;
|
---|
23 | InitNull();
|
---|
24 | ownobj_ = true;
|
---|
25 | }
|
---|
26 |
|
---|
27 | FITS_NTuple::FITS_NTuple(char inputfile[],int hdunum)
|
---|
28 | {
|
---|
29 | dobj_ = new NTuple;
|
---|
30 | InitNull();
|
---|
31 | ownobj_ = true;
|
---|
32 |
|
---|
33 | Read(inputfile,hdunum);
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | FITS_NTuple::FITS_NTuple(const NTuple & obj)
|
---|
38 | {
|
---|
39 | dobj_ = new NTuple(obj);
|
---|
40 | InitNull();
|
---|
41 | ownobj_ = true;
|
---|
42 | }
|
---|
43 | FITS_NTuple::FITS_NTuple(NTuple* obj)
|
---|
44 | {
|
---|
45 | dobj_ = obj;
|
---|
46 | InitNull();
|
---|
47 | ownobj_ = false;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | FITS_NTuple::~FITS_NTuple()
|
---|
52 | {
|
---|
53 | if (ownobj_ && dobj_ != NULL) delete dobj_;
|
---|
54 | // if (column_ != NULL) delete [] column_;
|
---|
55 | }
|
---|
56 |
|
---|
57 | int 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;
|
---|
63 | key = "Content";
|
---|
64 | if (is.KeyValue(key) == "NTuple") return 2;
|
---|
65 | else return 1;
|
---|
66 | }
|
---|
67 |
|
---|
68 | //void FITS_NTuple::Read(char inputfile[],int hdunum)
|
---|
69 | //{
|
---|
70 | // ReadF(inputfile,hdunum);
|
---|
71 | //}
|
---|
72 | void FITS_NTuple::ReadLines(char inputfile[],int firstLine, int numberOfLines,int hdunum)
|
---|
73 | {
|
---|
74 | fistLineToBeRead_ = firstLine;
|
---|
75 | numberOfLinesToBeRead_ = numberOfLines;
|
---|
76 | Read(inputfile,hdunum);
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 |
|
---|
81 | //void FITS_NTuple::Write(char outputfile[], bool OldFile)
|
---|
82 | //{
|
---|
83 | // WriteF(outputfile, OldFile);
|
---|
84 | //}
|
---|
85 |
|
---|
86 | void FITS_NTuple::ReadFromFits(FitsInFile& is)
|
---|
87 | {
|
---|
88 | if (!is.IsFitsTable())
|
---|
89 | {
|
---|
90 | throw PException("ReadFromFits: the fits file seems not to be a bintable nor ASCII table");
|
---|
91 | }
|
---|
92 | int nbcols, nbentries;
|
---|
93 | nbcols = is.NbColsFromFits();
|
---|
94 | nbentries = 0;
|
---|
95 | int k;
|
---|
96 | for (k=0; k<nbcols; k++) nbentries=max( nbentries, is.NentriesFromFits(k) );
|
---|
97 |
|
---|
98 | char ** ColName = new char*[nbcols];
|
---|
99 |
|
---|
100 | for (k=0; k<nbcols;k++)
|
---|
101 | {
|
---|
102 | ColName[k] = new char[LONNOM1];
|
---|
103 | strncpy(ColName[k], is.ColNameFromFits(k).c_str(),LONNOM);
|
---|
104 | ColName[k][LONNOM] = '\0';
|
---|
105 | }
|
---|
106 | // Reza Dec 2006 : Colonnes du NTuple de type double par defaut
|
---|
107 | for (k=0; k<nbcols;k++)
|
---|
108 | {
|
---|
109 | FitsFile::FitsDataType ss= is.ColTypeFromFits(k);
|
---|
110 | string type;
|
---|
111 | if ( (ss != FitsFile::FitsDataType_double) && (ss != FitsFile::FitsDataType_float) )
|
---|
112 | {
|
---|
113 | if (ss == FitsFile::FitsDataType_int) type= string("integer");
|
---|
114 | else
|
---|
115 | if (ss == FitsFile::FitsDataType_char) type = string("char*");
|
---|
116 | else
|
---|
117 | type = string("unknown");
|
---|
118 | cout << " WARNING: the column " << k << " on fits file is not float but : " << type << endl;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | bool fgdouble = true;
|
---|
122 | if ( is.ColTypeFromFits(0) == FitsFile::FitsDataType_float ) fgdouble = false;
|
---|
123 | if(dobj_ == NULL)
|
---|
124 | {
|
---|
125 | dobj_= new NTuple(nbcols,ColName,512,fgdouble);
|
---|
126 | ownobj_ = true;
|
---|
127 | }
|
---|
128 | else
|
---|
129 | {
|
---|
130 | dobj_->Clean();
|
---|
131 | (*dobj_) = NTuple(nbcols,ColName,512,fgdouble);
|
---|
132 | }
|
---|
133 | for (k=0; k<nbcols;k++)
|
---|
134 | {
|
---|
135 | delete [] ColName[k];
|
---|
136 | }
|
---|
137 | delete [] ColName;
|
---|
138 |
|
---|
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];
|
---|
144 |
|
---|
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;
|
---|
157 | if (fgdouble)
|
---|
158 | for (numLigne=firstln; numLigne < lastln; numLigne++) {
|
---|
159 | is.GetBinTabLine(numLigne, dligne);
|
---|
160 | dobj_->Fill((r_8*)dligne);
|
---|
161 | }
|
---|
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;
|
---|
170 |
|
---|
171 | dobj_->Info()=is.DVListFromFits();
|
---|
172 | }
|
---|
173 |
|
---|
174 | void FITS_NTuple::WriteToFits(FitsOutFile& os)
|
---|
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();
|
---|
190 | dvl["Content"]= "NTuple";
|
---|
191 | dvl.SetComment("Content", "name of SOPHYA object");
|
---|
192 | dvl["SOPCLSNM"]= "SOPHYA::NTuple";
|
---|
193 | dvl.SetComment("SOPCLSNM", "SOPHYA class name");
|
---|
194 | // extension name
|
---|
195 | // string extname("NTuple_Binary_tbl");
|
---|
196 | string extname = os.NextExtensionName();
|
---|
197 |
|
---|
198 | vector<string> Noms(ncols);
|
---|
199 | int k;
|
---|
200 | for (k=0; k< ncols; k++)
|
---|
201 | {
|
---|
202 | Noms[k]= dobj_->NomIndex(k);
|
---|
203 | }
|
---|
204 |
|
---|
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;
|
---|
209 |
|
---|
210 | os.makeHeaderBntblOnFits(type,Noms, nentries, ncols, &dvl, extname, dummy);
|
---|
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;
|
---|
223 |
|
---|
224 | os.makeHeaderBntblOnFits(type,Noms, nentries, ncols, &dvl, extname, dummy);
|
---|
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 | }
|
---|
234 | }
|
---|
235 |
|
---|