root/bdm/stat/loggers.cpp @ 102

Revision 102, 2.5 kB (checked in by smidl, 16 years ago)

corrections of sampling

  • Property svn:eol-style set to native
Line 
1#include <itpp/itbase.h>
2#include "loggers.h"
3#include "../osutils.h"
4
5#include <fcntl.h>
6#ifdef WIN32
7#include <io.h>
8#endif
9
10void memlog::itsave(const char* fname){
11        it_file itf(fname);
12        int i;
13        for (i=0; i<entries.length();i++){
14         itf << Name(names(i)) << vectors(i);
15        }
16}
17
18void dirfilelog::init() {
19        int i,j,k;
20        int nsc=0;
21        for ( i=0;i<entries.length();i++ ) {nsc+=entries ( i ).count();}
22        ; //all entries!!
23
24        char filename[200];
25        char num[3];
26
27// Initialize directory
28        makedir(dirname); //
29
30// directory OK, or it can be overwritten
31
32//Create filenames
33        scalarnames.set_length ( nsc );
34        // For all entries
35        int ii=0;
36        for ( i=0;i<entries.length();i++ ) { //for entries
37                for ( j=0;j<entries ( i ).length();j++ ) { //for RVs in entries
38                        int rvsize = entries ( i ).size ( j );
39                        if ( rvsize ==1 ) {
40                                scalarnames ( ii ) = names ( i ) + "_" + entries ( i ).name ( j );
41                                ii++;
42                        } else {
43                                for ( k=0;k<rvsize;k++ ) { //for all scalars in given RV
44                                        sprintf ( num,"%d",k );
45                                        scalarnames ( ii ) = names ( i ) + "_" + entries ( i ).name ( j ) + "_" + num;
46                                        ii++;
47                                }
48                        }
49                }
50        }
51        //Create format
52        string frm="format";
53        get_fname ( filename, dirname, frm );
54
55        FILE* fpf;
56        fpf = fopen ( filename, "w" );
57        for ( i=0;i<ii;i++ ) {
58                fprintf ( fpf, "%s RAW d 1\n", scalarnames ( i ).c_str() );
59        }
60        fclose ( fpf );
61
62        memlog::init();
63
64        write_buffers ( 0 );
65}
66
67
68void dirfilelog::write_buffers ( int Len ) {
69        int fp;
70        int nen=entries.length(); //all entries!!
71        double *Dt;
72        int DtRows;
73        int i,j,k;
74        int ii; //index in scalarnames;
75        int jj; //index in vectors()
76        char filename[200];
77
78        it_assert_debug ( Len<=maxlen,"diffilelog" );
79
80        ii = 0;
81        for ( i=0;i<nen;i++ ) { //for entries
82                Dt = vectors ( i )._data();
83                DtRows = vectors(i).rows();
84               
85                jj=0;
86                for ( j=0;j<entries ( i ).length();j++ ) { //for RVs in entries
87                        int rvsize = entries ( i ).size ( j );
88                        for ( k=0;k<rvsize;k++ ) { { //for all scalars in given RV
89                                        get_fname ( filename,dirname,scalarnames ( ii ) );
90                                        if ( Len==0 ) //initialization
91                                                fp = open ( filename, O_CREAT | O_WRONLY | O_TRUNC, 00644 );
92                                        else
93                                                fp = open ( filename, O_CREAT | O_WRONLY | O_APPEND, 00644 );
94
95                                        write ( fp,&Dt[jj*(DtRows)],(Len)*sizeof ( double ) );
96                                        close ( fp );
97
98                                        //next row in Dt
99                                        jj++;
100                                        //next scalarname
101                                        ii++;
102                                }
103                        }
104                }
105        }
106}
107
108void dirfilelog::step ( bool final ) {
109
110        if ( final ) {
111                if ( ind>0 )
112                        write_buffers ( ind ); //assuming here that i+1 was not filled
113        } else
114                if ( ind== ( maxlen -1 ) ) {
115                        write_buffers ( ind+1 );
116                        ind = 0;
117                } else
118                        ind++;
119
120}
Note: See TracBrowser for help on using the browser.