source: Sophya/trunk/SophyaProg/Tests/tfitsdt.cc@ 4064

Last change on this file since 4064 was 3906, checked in by ansari, 15 years ago

Codage test extensive de DataTable (mem/swPPF/swFITS) ds tfitsdt.cc, Reza 2/11/2010

File size: 10.0 KB
RevLine 
[2821]1#include <stdio.h>
2#include <stdlib.h>
3#include <math.h>
4#include <iostream>
5
6#include "sopnamsp.h"
7#include "histinit.h"
8#include "datatable.h"
9#include "fitshdtable.h"
[2890]10#include "swfitsdtable.h"
[3906]11#include "swppfdtable.h"
12#include "ctimer.h"
[2821]13
[3906]14/* ----------------------------------------------------------
[2890]15 Programme de test lecture-ecriture FITS de DataTable
16 + test classe SwFitsDataTable
17 Oct 2005 - Jan 2006
18
[3906]19 --- This code is part of the SOPHYA library ---
20 (C) Univ. Paris-Sud (C) LAL-IN2P3/CNRS (C) IRFU-CEA
21 (C) R. Ansari, C.Magneville 2005 - 2010
[2821]22
[3906]23 ---------------------------------------------------------- */
24
25//-----------------------------------------------------------
26int test_fdtable() ;
27void tdfill_dtable(BaseDataTable& dtr, uint_8 sz);
28void tdcheck_dtable(BaseDataTable& dtr, uint_8& errcntI, uint_8& errcntF);
29int test_memdtable(uint_8 sz, bool fgppf=true) ;
30int test_swppfdtable(uint_8 sz) ;
31int test_swfitsdtable(uint_8 sz) ;
32#define _TDTSEGSIZ_ 8192
33//------------------------------------------------------------
34
35//-------------- MAIN PROGRAM ------------
[2821]36int main(int narg, char *arg[])
37{
38 SophyaInit();
[3906]39// 0 -> test_fdtable , 1 -> test_memdtable/PPF , 2 -> test_memdtable/FITS
40// 3 -> test_swppfdtable , 4 -> test_swfitsdtable
41 int opt = 0;
42 if (narg<2) {
43 cout << " Usage: tfitsdt OPT=0/1/2/3/4 [ Size=NRows in units of 1000] \n"
44 << " 0 -> test_fdtable \n 1 -> test_memdtable/PPF , 2 -> test_memdtable/FITS \n"
45 << " 3 -> test_swppfdtable , 4 -> test_swfitsdtable \n"
46 << " Rc=0 OK for 1/2/3/4 , Def.Size=2048 (*1000) " << endl;
47 return 1;
48 }
49 opt = atoi(arg[1]);
50 uint_8 sz = 2048*1000;
51 if (narg>2) sz = (uint_8)(atof(arg[2])*1000.);
52 int rc=0;
[2821]53 try {
[3906]54 if (opt==1) rc=test_memdtable(sz,true);
55 else if (opt==2) rc=test_memdtable(sz,false);
56 else if (opt==3) rc=test_swppfdtable(sz);
57 else if (opt==4) rc=test_swfitsdtable(sz);
58 else test_fdtable();
[2821]59 }
60 catch(PThrowable exc ) {
[2890]61 cerr << "tfitsdt-main() , Catched exception: \n" << exc.Msg() << endl;
[2821]62 }
63 catch(std::exception ex) {
[2890]64 cerr << "tfitsdt-main() , Catched exception ! " << (string)(ex.what()) << endl;
[2821]65 }
66 catch(...) {
[2890]67 cerr << "tfitsdt-main() , Catched ... ! " << endl;
[2821]68 }
[3906]69 cout << " ========== End of tfitsdt.cc , Rc= " << rc << " ============= " << endl;
70 return rc;
[2821]71}
72
73
[3906]74/* --Nouvelle-Fonction-- */
75int test_fdtable()
[2821]76{
77 int NL = 600;
[2890]78 DataTable refdt(100); // Reference DataTable - for tests
79
80 cout << "======= test_fdtable: Simple DataTable+FITS test ======= " << endl;
[2821]81 {
82 cout << "1/ Creating DataTable / Writing to FITS " << endl;
83 DataTable dt(64);
84 dt.AddIntegerColumn("line");
85 dt.AddDoubleColumn("x");
86 dt.AddFloatColumn("f_sin");
87 dt.AddDoubleColumn("f_x2");
[2845]88 dt.AddStringColumn("str_line");
89 dt.AddComplexColumn("cmplx_cos_sin");
[2821]90 MuTyV rec[10];
[2845]91 char sbuff[32];
[2821]92 cout << " Filling ... (NLines=" << NL << ")" << endl;
93 for(int k = 0; k<NL; k++) {
94 rec[0] = k;
95 double x = M_PI*k/100.;
96 double fx = sin(x)*cos(x);
97 rec[1] = x+1;
98 rec[2] = sin(x);
99 rec[3] = x*x;
[2845]100 sprintf(sbuff, "SL-%d", k);
101 rec[4] = sbuff;
102 double sx = sin(x);
103 double cx = cos(x);
104 rec[5] = complex<r_4>(cx, sx);
[2821]105 dt.AddLine(rec);
106 }
107 cout << dt;
[2890]108 cout << " Copying dt to refdt (refdt = dt) " << endl;
109 refdt = dt;
[2821]110 cout << " Writing dt to fits file dtable.fits ... " << endl;
111 FitsInOutFile fios("!dtable.fits", FitsInOutFile::Fits_Create);
112 fios << dt;
113 cout << " Writing dt to fits as ASCII table with extname= ASC_DTable ... " << endl;
114 fios.SetDef_AscTable();
115 fios.SetNextExtensionName("ASC_DTable");
116 fios << dt;
[2890]117 cout << "1.b/ Creating SwFitsDataTable (file swdtable.fits) " << endl;
118 FitsInOutFile swf("!swdtable.fits", FitsInOutFile::Fits_Create);
119 SwFitsDataTable swdt(swf, 64);
120 cout << " Copying from DataTable dt ..." << endl;
121 swdt = dt;
122 cout << swdt;
[2821]123 }
124 {
125 cout << "2/ Reading DataTable from FITS " << endl;
126 FitsInOutFile fios("dtable.fits", FitsInOutFile::Fits_RO);
127 cout << fios ;
128 fios.MoveToNextHDU();
129 DataTable dtr;
130 fios >> dtr;
131 cout << dtr;
132 cout << "2.b/ dtr.LineHeaderToString() dtr.LineToString(k) : " << endl;
[2845]133 cout << dtr.LineHeaderToString() << endl;
[2821]134 for(int k = 0; k<NL; k+=NL/12)
135 cout << "Line[" << k << "] " << dtr.LineToString(k) << endl ;
[2890]136 cout << "2.b/ Reading from ASCII table " << endl;
[2821]137 DataTable dtra;
138 fios >> dtra;
139 cout << dtra;
[2845]140 cout << dtra.LineHeaderToString() << endl;
141 for(int k = 0; k<NL; k+=NL/12)
142 cout << "Line[" << k << "] " << dtra.LineToString(k) << endl ;
[2890]143
144 cout << "2.c/ Reading SwFitsDataTable from swdtable.fits " << endl;
145 SwFitsDataTable swdtr;
146 FitsInOutFile swf("swdtable.fits", FitsInOutFile::Fits_RO);
147 swf.MoveAbsToHDU(2);
148 swf >> swdtr;
149 cout << swdtr;
150 cout << swdtr.LineHeaderToString() << endl;
151 for(int k = 0; k<NL; k+=NL/12)
152 cout << "Line[" << k << "] " << swdtr.LineToString(k) << endl ;
[2821]153 }
[2890]154 cout << "============ FIN test_fdtable =============== " << endl;
[3906]155 return 0;
[2821]156}
157
158
159
[3906]160/* --Nouvelle-Fonction-- */
161void tdfill_dtable(BaseDataTable& dt, uint_8 sz)
162{
163 dt.AddIntegerColumn("ic1");
164 dt.AddFloatColumn("fc2");
165 int ic1,ric1;
166 float fc2,rfc2;
167 MuTyV rec[5];
168 uint_8 modprt=sz/10;
169 for(uint_8 k=0; k<sz; k++) {
170 ic1 = (int)(k%1000000000LL);
171 fc2 = (float)ic1+0.5; fc2 *= (fc2+0.2);
172 rec[0]=ic1; rec[1]=fc2;
173 dt.AddLine(rec);
174 if (k%modprt==0) cout << " tdfill_dtable/ done k=" << k << " : max=" << sz << endl;
175 }
176 return;
177}
[2821]178
[3906]179/* --Nouvelle-Fonction-- */
180void tdcheck_dtable(BaseDataTable& dtr, uint_8& errcntI, uint_8& errcntF)
181{
182 errcntI=0;
183 errcntF=0;
184 int ic1,ric1;
185 float fc2,rfc2;
186 MuTyV rec[5];
187 uint_8 modprt=dtr.NRows()/10;
188
189 for(sa_size_t k=0; k<dtr.NRows(); k++) {
190 ic1 = (int)(k%1000000000LL);
191 fc2 = (float)ic1+0.5; fc2 *= (fc2+0.2);
192 dtr.GetRow(k,rec);
193 ric1=rec[0]; rfc2=rec[1];
194 if (ric1!=ic1) errcntI++;
195 if (fabs(fc2-rfc2)>1.e-9) errcntF++;
196 if (k%modprt==0)
197 cout << " tdcheck_dtable/ done k=" << k << " : max=" << dtr.NRows()
198 << " ErrCntI=" << errcntI << " ErrCntF=" << errcntF << endl;
199 }
200 return;
201}
202
203/* --Nouvelle-Fonction-- */
204int test_memdtable(uint_8 sz, bool fgppf)
205{
206 Timer tm("Tst_MEM_DataTable");
207 cout << " ------------------- Tst_MEM_DataTable -------------------------- " << endl;
208 cout << " --- test_memdtable[1]: creating DataTable with " << sz << " rows (=" << sz/1000 << " kilo) ... " << endl;
209 string dtfilename="tstmemdtable.ppf";
210 {
211 DataTable dt(_TDTSEGSIZ_);
212 tdfill_dtable(dt, sz);
213 cout << dt;
214 tm.Split();
215 if (fgppf) {
216 POutPersist po(dtfilename);
217 po << dt;
218 cout << " --- test_memdtable[2]: datatable written to PPF file " << dtfilename << endl;
219 }
220 else {
221 dtfilename="!tstmemdtable.fits";
222 FitsInOutFile fios(dtfilename, FitsInOutFile::Fits_Create);
223 fios << dt;
224 cout << " --- test_memdtable[2]: datatable written to FITS file " << dtfilename << endl;
225 }
226 }
227
228 uint_8 errcntI=0;
229 uint_8 errcntF=0;
230 {
231 DataTable dtr;
232 if (fgppf) {
233 cout << " --- test_memdtable[3]: reading DataTable from PPF file " << dtfilename << endl;
234 PInPersist pin(dtfilename);
235 pin >> dtr;
236 }
237 else {
238 dtfilename="tstmemdtable.fits";
239 FitsInOutFile fios(dtfilename, FitsInOutFile::Fits_RO);
240 fios.MoveToNextHDU();
241 cout << " --- test_memdtable[3]: reading DataTable from FITS file " << dtfilename << endl;
242 fios >> dtr;
243 }
244 tm.Split();
245 tdcheck_dtable(dtr, errcntI, errcntF);
246 }
247 cout << " --- test_memdtable[4]: End Read/Check ErrCntI=" << errcntI << " ErrCntF=" << errcntF << endl;
248 if ((errcntI==0)&&(errcntF==0)) return 0;
249 else if (errcntI==0) return 5;
250 else return 9;
251}
252
253/* --Nouvelle-Fonction-- */
254int test_swppfdtable(uint_8 sz)
255{
256 Timer tm("Tst_SwPPF_DataTable");
257 cout << " ------------------- Tst_SwPPF_DataTable ------------------------- " << endl;
258 cout << " --- test_swppfdtable[1]: creating DataTable with " << sz << " rows (=" << sz/1000 << " kilo) ... " << endl;
259 string dtswfilename="tstswppfdtable.ppf";
260 {
261 POutPersist po(dtswfilename);
262 SwPPFDataTable dt(po, _TDTSEGSIZ_);
263 tdfill_dtable(dt, sz);
264 po << dt;
265 cout << " --- test_swppfdtable[2]: SwPPFDataTable written to PPF file " << dtswfilename << endl;
266 tm.Split();
267 }
268
269 uint_8 errcntI=0;
270 uint_8 errcntF=0;
271 {
272 PInPersist pin(dtswfilename);
273 SwPPFDataTable dtr;
274 cout << " --- test_swppfdtable[3]: reading SwPPFDataTable from PPF file " << dtswfilename << endl;
275 pin >> dtr;
276 tdcheck_dtable(dtr, errcntI, errcntF);
277 }
278 cout << " --- test_swppfdtable[4]: End Read/Check ErrCntI=" << errcntI << " ErrCntF=" << errcntF << endl;
279 if ((errcntI==0)&&(errcntF==0)) return 0;
280 else if (errcntI==0) return 5;
281 else return 9;
282}
283
284/* --Nouvelle-Fonction-- */
285int test_swfitsdtable(uint_8 sz)
286{
287 Timer tm("Tst_SwFITS_DataTable");
288 cout << " ------------------- Tst_SwFITS_DataTable ----------------------- " << endl;
289 cout << " --- test_swfitstable[1]: creating SwFitsDataTable with " << sz << " rows (=" << sz/1000 << " kilo) ... " << endl;
290 string dtswfilename="!tstswfitsdtable.fits";
291 {
292 FitsInOutFile swf(dtswfilename, FitsInOutFile::Fits_Create);
293 SwFitsDataTable swdt(swf, _TDTSEGSIZ_);
294 tdfill_dtable(swdt, sz);
295 swf << swdt;
296 cout << " --- test_swfitstable[2]: SwFitsDataTable written to FITS file " << dtswfilename << endl;
297 tm.Split();
298 }
299
300 uint_8 errcntI=0;
301 uint_8 errcntF=0;
302 {
303 dtswfilename="tstswfitsdtable.fits";
304 SwFitsDataTable swdtr;
305 FitsInOutFile swif(dtswfilename, FitsInOutFile::Fits_RO);
306 swif.MoveAbsToHDU(2);
307 cout << " --- test_swfitstable[3]: reading SwFitsDataTable from FITS file " << dtswfilename << endl;
308 swif >> swdtr;
309 tdcheck_dtable(swdtr, errcntI, errcntF);
310 }
311 cout << " --- test_swfitstable[4]: End Read/Check ErrCntI=" << errcntI << " ErrCntF=" << errcntF << endl;
312 if ((errcntI==0)&&(errcntF==0)) return 0;
313 else if (errcntI==0) return 5;
314 else return 9;
315}
316
317
318
Note: See TracBrowser for help on using the repository browser.