source: Sophya/trunk/SophyaExt/FitsIOServer/fitshistos.cc@ 3049

Last change on this file since 3049 was 3049, checked in by cmv, 19 years ago

Fits IO Histo,HProf,HistErr,Histo2D cmv 11/8/2006

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