root/bdm/stat/loggers.cpp @ 347

Revision 347, 3.0 kB (checked in by smidl, 15 years ago)

change in loggers! old experiments may stop working

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