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
RevLine 
[262]1
[92]2#include "loggers.h"
[93]3#include "../osutils.h"
[92]4
5#include <fcntl.h>
[95]6#ifdef WIN32
7#include <io.h>
8#endif
[92]9
[254]10namespace bdm{
[162]11void memlog::itsave ( const char* fname ) {
12        it_file itf ( fname );
[102]13        int i;
[347]14        string vec_name="";
15        int istart, iend;
[162]16        for ( i=0; i<entries.length();i++ ) {
[347]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;
[162]24                }
[347]25               
[102]26        }
27}
28
[92]29void dirfilelog::init() {
30        int i,j,k;
31        int nsc=0;
[270]32        for ( i=0;i<entries.length();i++ ) {nsc+=entries ( i )._dsize();}
[92]33        ; //all entries!!
34
35        char filename[200];
36        char num[3];
37
38// Initialize directory
[162]39        makedir ( dirname ); //
[92]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 );
[230]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
[92]59                                for ( k=0;k<rvsize;k++ ) { //for all scalars in given RV
60                                        sprintf ( num,"%d",k );
[230]61                                        scalarnames ( ii ) += (std::string)"_" + num;
[92]62                                        ii++;
63                                }
[230]64                        } else {
65                        ii++;}
[92]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
[162]81        //Delete old data
[92]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();
[162]101                DtRows = vectors ( i ).rows();
102
[92]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
[162]113                                        write ( fp,&Dt[jj* ( DtRows ) ], ( Len ) *sizeof ( double ) );
[92]114                                        close ( fp );
115
116                                        //next row in Dt
117                                        jj++;
118                                        //next scalarname
119                                        ii++;
120                                }
121                        }
122                }
123        }
124}
125
[162]126void dirfilelog::step ( ) {
[92]127
[162]128        if ( ind== ( maxlen -1 ) ) {
129                write_buffers ( ind+1 );
130                ind = 0;
131        }
132        else
133                ind++;
[92]134
135}
[162]136
137void dirfilelog::finalize ( ) {
138        if ( ind>0 )
139                write_buffers ( ind ); //assuming here that i+1 was not filled
140}
[254]141
[262]142}
Note: See TracBrowser for help on using the browser.