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