1 | #include "machdefs.h"
|
---|
2 | #include "sopnamsp.h"
|
---|
3 |
|
---|
4 | #include <stdio.h>
|
---|
5 | #include <string.h>
|
---|
6 | #include <iostream>
|
---|
7 | #include <typeinfo>
|
---|
8 |
|
---|
9 | #include "histos.h"
|
---|
10 | #include "hisprof.h"
|
---|
11 | #include "histerr.h"
|
---|
12 | #include "histos2.h"
|
---|
13 |
|
---|
14 | #include "fitsblkrw.h"
|
---|
15 | #include "fitshandler.h"
|
---|
16 |
|
---|
17 |
|
---|
18 | ///////////////////////////////////////////////////////////////////////////
|
---|
19 | ///////////////////////// Histo , HProf , HistoErr ////////////////////////
|
---|
20 | ///////////////////////////////////////////////////////////////////////////
|
---|
21 |
|
---|
22 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
23 | int FitsHandler<Histo>::CheckReadability(FitsInOutFile& is)
|
---|
24 | {
|
---|
25 | if (is.CurrentHDUType() == IMAGE_HDU ) return 0;
|
---|
26 | string key = "SOPCLSNM";
|
---|
27 | string clsnm = is.KeyValue(key);
|
---|
28 | if ( (clsnm == "SOPHYA::Histo")
|
---|
29 | || (clsnm == "SOPHYA::HProf")
|
---|
30 | || (clsnm == "SOPHYA::HistoErr") ) return 2;
|
---|
31 | return 0;
|
---|
32 | }
|
---|
33 |
|
---|
34 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
35 | int FitsHandler<Histo>::CheckHandling(AnyDataObj & o)
|
---|
36 | {
|
---|
37 | if (typeid(o) == typeid(Histo)) return 2;
|
---|
38 | Histo * po = dynamic_cast< Histo * >(& o);
|
---|
39 | if (po != NULL) return 2;
|
---|
40 | return 0;
|
---|
41 | }
|
---|
42 |
|
---|
43 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
44 | void FitsHandler<Histo>::Write(FitsInOutFile& os)
|
---|
45 | {
|
---|
46 | if(dobj==NULL)
|
---|
47 | throw NullPtrError("FitsHandler<Histo>::Write() NULL dobj pointer ");
|
---|
48 |
|
---|
49 | //--- Le type d'objet et son pointeur
|
---|
50 | Histo* h = dynamic_cast< Histo *> (dobj);
|
---|
51 | HProf* hp = dynamic_cast< HProf *> (dobj);
|
---|
52 | HistoErr* he = dynamic_cast< HistoErr *> (dobj);
|
---|
53 |
|
---|
54 | //--- Les noms de colonnes
|
---|
55 | int tbltyp = os.GetDef_TableType();
|
---|
56 | vector<string> colnames, tform, tunit;
|
---|
57 | // les valeurs du bin
|
---|
58 | if(tbltyp==ASCII_TBL) tform.push_back("D15.8"); else tform.push_back("D");
|
---|
59 | colnames.push_back("val");
|
---|
60 | tunit.push_back("");
|
---|
61 | // Les erreurs
|
---|
62 | if(h->mErr2) {
|
---|
63 | if(tbltyp==ASCII_TBL) tform.push_back("D15.8"); else tform.push_back("D");
|
---|
64 | colnames.push_back("e2");
|
---|
65 | tunit.push_back("");
|
---|
66 | }
|
---|
67 | // Le nombre d'entrees dans le bin
|
---|
68 | if(he!=NULL || hp!=NULL) {
|
---|
69 | if(tbltyp==ASCII_TBL) tform.push_back("D15.8"); else tform.push_back("D");
|
---|
70 | colnames.push_back("nb");
|
---|
71 | tunit.push_back("");
|
---|
72 | }
|
---|
73 |
|
---|
74 | //--- On cree la table
|
---|
75 | string extname = os.NextExtensionName();
|
---|
76 | os.CreateTable(os.GetDef_TableType(),extname,colnames,tform, tunit);
|
---|
77 |
|
---|
78 | // Ecriture des donnees des colonnes
|
---|
79 | int n = h->NBins();
|
---|
80 | if(n>0) {
|
---|
81 | if(hp) hp->UpdateHisto();
|
---|
82 | FitsBlockRW<r_8>::WriteColumnData(os,1,1,1,h->mData,n);
|
---|
83 | if(h->mErr2) FitsBlockRW<r_8>::WriteColumnData(os,2,1,1,h->mErr2,n);
|
---|
84 | if(hp!=NULL) FitsBlockRW<r_8>::WriteColumnData(os,3,1,1,hp->mSumW,n);
|
---|
85 | if(he!=NULL) FitsBlockRW<r_8>::WriteColumnData(os,3,1,1,he->mNData,n);
|
---|
86 | }
|
---|
87 |
|
---|
88 | // Ecriture des clefs fits
|
---|
89 | MuTyV mtv;
|
---|
90 |
|
---|
91 | mtv = "SOPHYA::Histo";
|
---|
92 | if(hp) mtv = "SOPHYA::HProf";
|
---|
93 | else if(he) mtv = "SOPHYA::HistoErr";
|
---|
94 | os.WriteKey("SOPCLSNM",mtv," SOPHYA class name");
|
---|
95 |
|
---|
96 | mtv = "Histo";
|
---|
97 | if(hp) mtv = "HProf";
|
---|
98 | else if(he) mtv = "HistoErr";
|
---|
99 | os.WriteKey("CONTENT",mtv," name of SOPHYA object");
|
---|
100 |
|
---|
101 | mtv = h->mBins;
|
---|
102 | os.WriteKey("NBIN",mtv," number of bins");
|
---|
103 |
|
---|
104 | mtv = h->mMin;
|
---|
105 | os.WriteKey("XMIN",mtv," absc of beginning of 1srt bin");
|
---|
106 |
|
---|
107 | mtv = h->mMax;
|
---|
108 | os.WriteKey("XMAX",mtv," absc of end of last bin");
|
---|
109 |
|
---|
110 | mtv = h->binWidth;
|
---|
111 | os.WriteKey("WBIN",mtv," bin width");
|
---|
112 |
|
---|
113 | mtv = h->mUnder;
|
---|
114 | os.WriteKey("UNDER",mtv," number of bins");
|
---|
115 |
|
---|
116 | mtv = h->mOver;
|
---|
117 | os.WriteKey("OVER",mtv," underflow");
|
---|
118 |
|
---|
119 | mtv = h->nHist;
|
---|
120 | os.WriteKey("NHIST",mtv," entries weighted somme");
|
---|
121 |
|
---|
122 | double x = h->nEntries; mtv = x;
|
---|
123 | os.WriteKey("NENTRIES",mtv," number of entries");
|
---|
124 |
|
---|
125 | int_4 haserr =(h->mErr2) ? 1: 0;
|
---|
126 | mtv = haserr;
|
---|
127 | os.WriteKey("HASERR2",mtv," square errors associated");
|
---|
128 |
|
---|
129 | if(hp) {
|
---|
130 | int_4 ok = (hp->mOk)? 1: 0;
|
---|
131 | mtv = ok;
|
---|
132 | os.WriteKey("UPDOK",mtv," update status flag");
|
---|
133 |
|
---|
134 | mtv = (int_4)hp->mOpt;
|
---|
135 | os.WriteKey("SIGOPT",mtv," sigma statistic flag");
|
---|
136 |
|
---|
137 | mtv = hp->mYMin;
|
---|
138 | os.WriteKey("YMIN",mtv," sum low limit");
|
---|
139 |
|
---|
140 | mtv = hp->mYMax;
|
---|
141 | os.WriteKey("YMAX",mtv," sum high limit");
|
---|
142 | }
|
---|
143 |
|
---|
144 | if(he) {
|
---|
145 | mtv = he->mCorrel;
|
---|
146 | os.WriteKey("NCORREL",mtv," number of Correl calls");
|
---|
147 | }
|
---|
148 |
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
153 | void FitsHandler<Histo>::Read(FitsInOutFile& is)
|
---|
154 | {
|
---|
155 | int hdutyp = is.CurrentHDUType();
|
---|
156 | if( (hdutyp != BINARY_TBL ) && (hdutyp != ASCII_TBL) )
|
---|
157 | throw FitsIOException("FitsHandler<Histo>::Read() Not a binary or ascii table HDU");
|
---|
158 |
|
---|
159 | //--- Nb de lignes et de colonnes
|
---|
160 | vector<string> colnames;
|
---|
161 | vector<int> coltypes;
|
---|
162 | vector<long> repcnt, width;
|
---|
163 | is.GetColInfo(colnames,coltypes,repcnt,width);
|
---|
164 | long ncol = colnames.size();
|
---|
165 | if(ncol<=0)
|
---|
166 | throw FitsIOException("FitsHandler<Histo>::Read() bad number of table columns");
|
---|
167 | long nbrows = is.GetNbRows();
|
---|
168 | if(nbrows<=0)
|
---|
169 | throw FitsIOException("FitsHandler<Histo>::Read() number of rows is zero, no reading");
|
---|
170 |
|
---|
171 | //--- Lecture entete FITS
|
---|
172 | string key = "SOPCLSNM"; string clsnm = is.KeyValue(key);
|
---|
173 | DVList dvl; is.GetHeaderRecords(dvl,true,false);
|
---|
174 |
|
---|
175 | int_4 nbin = dvl.GetI("NBIN",-1);
|
---|
176 | if(nbin<=0 && nbin!=nbrows)
|
---|
177 | throw FitsIOException("FitsHandler<Histo>::Read() number of bins is zero or bad, no reading");
|
---|
178 |
|
---|
179 | r_8 xmin = dvl.GetD("XMIN",-1.);
|
---|
180 | r_8 xmax = dvl.GetD("XMAX",+1.);
|
---|
181 |
|
---|
182 | //--- Creation de l'objet si necessaire
|
---|
183 | if(dobj == NULL) {
|
---|
184 | if(clsnm == "SOPHYA::Histo") dobj = new Histo;
|
---|
185 | else if(clsnm == "SOPHYA::HProf") dobj = new HProf;
|
---|
186 | else if(clsnm == "SOPHYA::HistoErr") dobj = new HistoErr;
|
---|
187 | }
|
---|
188 |
|
---|
189 | //--- Type de l'histo
|
---|
190 | Histo* h = dynamic_cast< Histo *> (dobj);
|
---|
191 | HProf* hp = dynamic_cast< HProf *> (dobj);
|
---|
192 | HistoErr* he = dynamic_cast< HistoErr *> (dobj);
|
---|
193 |
|
---|
194 | //--- Allocation pour histo
|
---|
195 | if(he && (clsnm=="SOPHYA::HistoErr")) {
|
---|
196 | if(ncol<3)
|
---|
197 | throw FitsIOException("FitsHandler<Histo>::Read() wrong number of columns for HistoErr");
|
---|
198 | he->CreateOrResize(xmin,xmax,nbin);
|
---|
199 | } else if(hp&& (clsnm=="SOPHYA::HProf")) {
|
---|
200 | if(ncol<3)
|
---|
201 | throw FitsIOException("FitsHandler<Histo>::Read() wrong number of columns for HProf");
|
---|
202 | r_8 ymin = dvl.GetD("YMIN",1.);
|
---|
203 | r_8 ymax = dvl.GetD("YMAX",-1.);
|
---|
204 | hp->CreateOrResize(xmin,xmax,nbin,ymin,ymax);
|
---|
205 | } else if(h && clsnm == "SOPHYA::Histo" ) {
|
---|
206 | h->CreateOrResize(xmin,xmax,nbin);
|
---|
207 | int_4 haserr2 = dvl.GetI("HASERR2",0);
|
---|
208 | if(ncol>1 && haserr2>0) h->Errors();
|
---|
209 | } else {
|
---|
210 | throw FitsIOException("FitsHandler<Histo>::Read() No assocaition classe/fits_header");
|
---|
211 | }
|
---|
212 |
|
---|
213 | //--- remplissage des variables d'entete
|
---|
214 | h->mUnder = dvl.GetD("UNDER",0.);
|
---|
215 | h->mOver = dvl.GetD("OVER",0.);
|
---|
216 | h->nHist = dvl.GetD("NHIST",0.);
|
---|
217 | double x = dvl.GetD("NENTRIES",0.); h->nEntries = uint_8(x);
|
---|
218 |
|
---|
219 | if(hp) {
|
---|
220 | int_4 ok = dvl.GetI("UPDOK",0); hp->mOk = (ok)? true : false;
|
---|
221 | hp->mOpt = (uint_2)dvl.GetI("SIGOPT",0);
|
---|
222 | hp->mYMin = dvl.GetD("YMIN",1.);
|
---|
223 | hp->mYMax = dvl.GetD("YMAX",-1.);
|
---|
224 | }
|
---|
225 |
|
---|
226 | if(he) he->mCorrel = dvl.GetI("NCORREL",0);
|
---|
227 |
|
---|
228 | //--- remplissage de l'histo
|
---|
229 | FitsBlockRW<r_8>::ReadColumnData(is,1,1,1,h->mData,nbin);
|
---|
230 | if(h->mErr2) FitsBlockRW<r_8>::ReadColumnData(is,2,1,1,h->mErr2,nbin);
|
---|
231 | if(hp) FitsBlockRW<r_8>::ReadColumnData(is,3,1,1,hp->mSumW,nbin);
|
---|
232 | if(he) FitsBlockRW<r_8>::ReadColumnData(is,3,1,1,he->mNData,nbin);
|
---|
233 |
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | ///////////////////////////////////////////////////////////////////////////
|
---|
238 | ///////////////////////////////// Histo2D ////////////////////////////////
|
---|
239 | ///////////////////////////////////////////////////////////////////////////
|
---|
240 |
|
---|
241 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
242 | int FitsHandler<Histo2D>::CheckReadability(FitsInOutFile& is)
|
---|
243 | {
|
---|
244 | if (is.CurrentHDUType() == IMAGE_HDU ) return 0;
|
---|
245 | string key = "SOPCLSNM";
|
---|
246 | string clsnm = is.KeyValue(key);
|
---|
247 | if( clsnm == "SOPHYA::Histo2D") return 2;
|
---|
248 | return 0;
|
---|
249 | }
|
---|
250 |
|
---|
251 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
252 | int FitsHandler<Histo2D>::CheckHandling(AnyDataObj & o)
|
---|
253 | {
|
---|
254 | if (typeid(o) == typeid(Histo2D)) return 2;
|
---|
255 | Histo2D * po = dynamic_cast< Histo2D * >(& o);
|
---|
256 | if (po != NULL) return 2;
|
---|
257 | return 0;
|
---|
258 | }
|
---|
259 |
|
---|
260 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
261 | void FitsHandler<Histo2D>::Write(FitsInOutFile& os)
|
---|
262 | {
|
---|
263 | if(dobj==NULL)
|
---|
264 | throw NullPtrError("FitsHandler<Histo2D>::Write() NULL dobj pointer ");
|
---|
265 |
|
---|
266 | //--- Le type d'objet et son pointeur
|
---|
267 | Histo2D* h = dynamic_cast< Histo2D *> (dobj);
|
---|
268 |
|
---|
269 | //--- Les noms de colonnes
|
---|
270 | int tbltyp = os.GetDef_TableType();
|
---|
271 | vector<string> colnames, tform, tunit;
|
---|
272 | // les valeurs du bin
|
---|
273 | if(tbltyp==ASCII_TBL) tform.push_back("D15.8"); else tform.push_back("D");
|
---|
274 | colnames.push_back("val");
|
---|
275 | tunit.push_back("");
|
---|
276 | // Les erreurs
|
---|
277 | if(h->mErr2) {
|
---|
278 | if(tbltyp==ASCII_TBL) tform.push_back("D15.8"); else tform.push_back("D");
|
---|
279 | colnames.push_back("e2");
|
---|
280 | tunit.push_back("");
|
---|
281 | }
|
---|
282 |
|
---|
283 | //--- On cree la table
|
---|
284 | string extname = os.NextExtensionName();
|
---|
285 | os.CreateTable(os.GetDef_TableType(),extname,colnames,tform, tunit);
|
---|
286 |
|
---|
287 | // Ecriture des donnees des colonnes
|
---|
288 | long n = h->mNxy;
|
---|
289 | if(n>0) {
|
---|
290 | FitsBlockRW<r_8>::WriteColumnData(os,1,1,1,h->mData,n);
|
---|
291 | if(h->mErr2) FitsBlockRW<r_8>::WriteColumnData(os,2,1,1,h->mErr2,n);
|
---|
292 | }
|
---|
293 |
|
---|
294 | // Ecriture des clefs fits
|
---|
295 | MuTyV mtv;
|
---|
296 |
|
---|
297 | mtv = "SOPHYA::Histo2D";
|
---|
298 | os.WriteKey("SOPCLSNM",mtv," SOPHYA class name");
|
---|
299 |
|
---|
300 | mtv = "Histo2D";
|
---|
301 | os.WriteKey("CONTENT",mtv," name of SOPHYA object");
|
---|
302 |
|
---|
303 | mtv = h->mNx;
|
---|
304 | os.WriteKey("NBINX",mtv," number of bins in X");
|
---|
305 | mtv = h->mNy;
|
---|
306 | os.WriteKey("NBINY",mtv," number of bins in Y");
|
---|
307 | mtv = h->mNxy;
|
---|
308 | os.WriteKey("NBINXY",mtv," number of elements");
|
---|
309 |
|
---|
310 | mtv = h->mXmin;
|
---|
311 | os.WriteKey("XMIN",mtv," absc of beginning of 1srt bin in X");
|
---|
312 | mtv = h->mXmax;
|
---|
313 | os.WriteKey("XMAX",mtv," absc of end of last bin in X");
|
---|
314 | mtv = h->mYmin;
|
---|
315 | os.WriteKey("YMIN",mtv," absc of beginning of 1srt bin in Y");
|
---|
316 | mtv = h->mYmax;
|
---|
317 | os.WriteKey("YMAX",mtv," absc of end of last bin in Y");
|
---|
318 |
|
---|
319 | mtv = h->mWBinx;
|
---|
320 | os.WriteKey("WBINX",mtv," bin width in X");
|
---|
321 | mtv = h->mWBiny;
|
---|
322 | os.WriteKey("WBINY",mtv," bin width in Y");
|
---|
323 |
|
---|
324 | for(int i=0;i<3;i++) for(int j=0;j<3;j++) {
|
---|
325 | char str[16]; sprintf(str,"OUT%1d%1d",i,j);
|
---|
326 | mtv = h->mOver[i][j];
|
---|
327 | os.WriteKey(str,mtv," under/over X/Y");
|
---|
328 | }
|
---|
329 |
|
---|
330 | mtv = h->nHist;
|
---|
331 | os.WriteKey("NHIST",mtv," entries weighted somme");
|
---|
332 |
|
---|
333 | mtv = h->nEntries;
|
---|
334 | os.WriteKey("NENTRIES",mtv," number of entries");
|
---|
335 |
|
---|
336 | int_4 haserr =(h->mErr2) ? 1: 0;
|
---|
337 | mtv = haserr;
|
---|
338 | os.WriteKey("HASERR2",mtv," square errors associated");
|
---|
339 |
|
---|
340 | }
|
---|
341 |
|
---|
342 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
343 | void FitsHandler<Histo2D>::Read(FitsInOutFile& is)
|
---|
344 | {
|
---|
345 | int hdutyp = is.CurrentHDUType();
|
---|
346 | if( (hdutyp != BINARY_TBL ) && (hdutyp != ASCII_TBL) )
|
---|
347 | throw FitsIOException("FitsHandler<Histo2D>::Read() Not a binary or ascii table HDU");
|
---|
348 |
|
---|
349 | //--- Nb de lignes et de colonnes
|
---|
350 | vector<string> colnames; vector<int> coltypes; vector<long> repcnt, width;
|
---|
351 | is.GetColInfo(colnames,coltypes,repcnt,width);
|
---|
352 | long ncol = colnames.size();
|
---|
353 | if(ncol<=0)
|
---|
354 | throw FitsIOException("FitsHandler<Histo2D>::Read() bad number of table columns");
|
---|
355 | long nbrows = is.GetNbRows();
|
---|
356 | if(nbrows<=0)
|
---|
357 | throw FitsIOException("FitsHandler<Histo2D>::Read() number of rows is zero, no reading");
|
---|
358 |
|
---|
359 | //--- Lecture entete FITS
|
---|
360 | DVList dvl; is.GetHeaderRecords(dvl,true,false);
|
---|
361 |
|
---|
362 | int_4 nbinx = dvl.GetI("NBINX",-1);
|
---|
363 | int_4 nbiny = dvl.GetI("NBINY",-1);
|
---|
364 | int_8 nbinxy = nbinx*nbiny;
|
---|
365 | if(nbinx<=0 || nbiny<=0 || nbinxy!=nbrows)
|
---|
366 | throw FitsIOException("FitsHandler<Histo2D>::Read() number of bins is zero or bad, no reading");
|
---|
367 |
|
---|
368 | r_8 xmin = dvl.GetD("XMIN",-1.);
|
---|
369 | r_8 xmax = dvl.GetD("XMAX",1.);
|
---|
370 | r_8 ymin = dvl.GetD("YMIN",-1.);
|
---|
371 | r_8 ymax = dvl.GetD("YMAX",1.);
|
---|
372 |
|
---|
373 | //--- Creation de l'objet
|
---|
374 | if(dobj == NULL) dobj = new Histo2D;
|
---|
375 | dobj->CreateOrResize(xmin,xmax,nbinx,ymin,ymax,nbiny);
|
---|
376 | int_4 haserr2 = dvl.GetI("HASERR2",0);
|
---|
377 | if(ncol>1 && haserr2>0) dobj->Errors();
|
---|
378 |
|
---|
379 | //--- remplissage des variables d'entete
|
---|
380 | dobj->nHist = dvl.GetD("NHIST",0.);
|
---|
381 | dobj->nEntries = dvl.GetI("NENTRIES",0);
|
---|
382 |
|
---|
383 | for(int i=0;i<3;i++) for(int j=0;j<3;j++) {
|
---|
384 | char str[16]; sprintf(str,"OUT%1d%1d",i,j);
|
---|
385 | dobj->mOver[i][j] = dvl.GetD(str,0.);
|
---|
386 | }
|
---|
387 |
|
---|
388 | //--- remplissage de l'histo
|
---|
389 | FitsBlockRW<r_8>::ReadColumnData(is,1,1,1,dobj->mData,nbinxy);
|
---|
390 | if(dobj->mErr2) FitsBlockRW<r_8>::ReadColumnData(is,2,1,1,dobj->mErr2,nbinxy);
|
---|
391 |
|
---|
392 | }
|
---|