\section{/home/peifer/diffit/modules/read\-Data.cc File Reference}
\label{readData_8cc}\index{/home/peifer/diffit/modules/readData.cc@{/home/peifer/diffit/modules/readData.cc}}
{\bf Module} for reading the data file of the measurements 

{\tt \#include $<$stdlib.h$>$}\par
{\tt \#include $<$stdio.h$>$}\par
{\tt \#include $<$iostream$>$}\par
{\tt \#include $<$fstream$>$}\par
{\tt \#include $<$ctype.h$>$}\par
{\tt \#include $<$math.h$>$}\par
{\tt \#include $<$string.h$>$}\par
{\tt \#include \char`\"{}../nr.h\char`\"{}}\par
{\tt \#include \char`\"{}../def.h\char`\"{}}\par
\subsection*{Functions}
\begin{CompactItemize}
\item 
void {\bf read\-Data} ({\bf Glob\-Exp} $\ast$ex, {\bf Glob} $\ast$globs, long exp\-Nr)
\end{CompactItemize}


\subsection{Detailed Description}
{\bf Module} for reading the data file of the measurements 



Definition in file {\bf read\-Data.cc}.

\subsection{Function Documentation}
\index{readData.cc@{read\-Data.cc}!readData@{readData}}
\index{readData@{readData}!readData.cc@{read\-Data.cc}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void read\-Data ({\bf Glob\-Exp} $\ast$ {\em ex}, {\bf Glob} $\ast$ {\em globs}, long {\em exp\-Nr})}\label{readData_8cc_2bf00ef122e7627b0af66c27e8adf570}




Definition at line 17 of file read\-Data.cc.

References Glob\-Exp::file\-Name, and Glob\-Exp::nobs.

\footnotesize\begin{verbatim}18 {
19   int lineNr=0;
20   long k,l,index;
21   long nLines=0,maxLeng=0;
22   long nobsmax=ex[expNr].nobs;
23   char c,*line,*arg;
24   double *x;
25   double **y;
26   double **sig;
27 
28   ifstream in;
29 
30   FILE *fp;
31 
32   fp=fopen(ex[expNr].fileName,"r");
33   if (fp==NULL) {
34     cerr << "Error opening data file " << ex[expNr].fileName << "\n";
35     exit(1);
36   }
37   
38   //read total number of lines
39   while((c=fgetc(fp))!=EOF)
40         {
41           for(k=1;c!='\n';k++)
42             {
43               if(k>maxLeng)
44                 maxLeng=k;
45               c=fgetc(fp);
46             }
47           nLines++;
48         }
49 #ifdef DEBUGREADDATA
50   *dbg << "Total number of lines in " <<  ex[expNr].fileName << " : " << nLines << endl;
51   *dbg << "Maximal length of line in " << ex[expNr].fileName << " : " << maxLeng << endl;
52 #endif
53   fclose(fp);
54 
55   in.open(ex[expNr].fileName);
56 
57   //allocate memory
58   line=new char[maxLeng+2];
59   arg=new char[maxLeng+2];
60   x=dvector(1,nLines);
61   y=dmatrix(1,nLines,1,ex[expNr].nobs);
62   sig=dmatrix(1,nLines,1,ex[expNr].nobs);
63 
64   //extracting the data
65   while(!in.eof())
66         {
67           in.getline(line,maxLeng+2,'\n');
68           if(isspace(line[0])==FALSE && line[0]!= '#' && line[0] != '\0' )
69             {
70               lineNr++;
71 #ifdef DEBUGREADDATA
72               *dbg << "Extracting data line no. : " << lineNr << endl;
73 #endif
74               l=0;
75               index=0;
76               for(k=0;k<=strlen(line);k++)
77                 {
78                   if(isspace(line[k])!=FALSE)
79                     {
80                       arg[l]='\0';
81                       l=0;
82                       if(index==0)
83                         x[lineNr]=atof(arg);
84                       else if(index <=2*ex[expNr].nobs && (index % 2)==1)
85                         y[lineNr][(index+1)/2]=atof(arg);
86                       else if(index <=2*ex[expNr].nobs && (index % 2)==0)
87                         sig[lineNr][index/2]=atof(arg);
88                       index++;
89                     }
90                   else
91                     {
92                       arg[l]=line[k];
93                       l++;
94                     }
95                 }
96               arg[l]='\0';      
97               if(index==0)
98                 x[lineNr]=atof(arg);
99               else if(index <=2*ex[expNr].nobs && (index % 2)==1)
100                 y[lineNr][(index+1)/2]=atof(arg);
101               else if(index <=2*ex[expNr].nobs && (index % 2)==0)
102                 sig[lineNr][index/2]=atof(arg);
103               
104               //determ. max. complete obs.
105               if((index % 2)==1)
106                 index--;
107               if(index==0)
108                 {
109                   cerr << "Data line " << lineNr << " contains no data." << endl;
110                   exit(1);
111                 }
112               if(index/2 <=  nobsmax)
113                 nobsmax=index/2;
114             }
115                     
116         }
117   in.close();
118 
119   ex[expNr].nobs=nobsmax;
120   ex[expNr].nMeasure=lineNr;
121   if(ex[expNr].nMeasure==0)
122     {
123       cerr << "File " << ex[expNr].fileName << " contains no data." << endl;
124       exit(1);
125     }
126 
127   //check if the time is in ascending order
128     for(k=2;k<=ex[expNr].nMeasure;k++)
129       {
130         if(x[k-1] >= x[k])
131           {
132             cerr << "Data not in temporal ascending order at dataline " << k << "\n";
133             exit(1);
134           }
135       }
136     //fit range stuff
137     if(ex[expNr].fitstart == -1e99)
138       ex[expNr].fitstart=x[1];
139     if(ex[expNr].fitend ==  1e99)
140       ex[expNr].fitend=x[ex[expNr].nMeasure];
141     
142     //for memory allocation
143     index=1;
144     for(k=1;k<=ex[expNr].nMeasure;k++)
145       {
146         if((x[k] >= ex[expNr].fitstart) && (x[k] <= ex[expNr].fitend))
147           { 
148             index++;
149           }
150       }
151     index--; 
152   
153     //the actual amount of data
154     ex[expNr].nMeasure=index;
155 
156     //memory allocation
157     ex[expNr].xMeasure=dvector(1,ex[expNr].nMeasure);
158     ex[expNr].yMeasure=dmatrix(1,ex[expNr].nMeasure,1,ex[expNr].nobs);
159     ex[expNr].sigma=dmatrix(1,ex[expNr].nMeasure,1,ex[expNr].nobs);
160 
161     // copy data to data structure
162     index=1;
163     for(k=1;k<=lineNr;k++)
164       {
165         if((x[k] >= ex[expNr].fitstart) && (x[k] <= ex[expNr].fitend))
166           {
167             ex[expNr].xMeasure[index]=x[k];
168             for(l=1;l<=ex[expNr].nobs;l++)
169               {
170                 ex[expNr].yMeasure[index][l]=y[k][l];
171                 ex[expNr].sigma[index][l]=sig[k][l];
172               }
173             index++;
174           }
175       }
176     index--;
177 
178     //setting some variables
179     ex[expNr].firstMeasure=1;
180     ex[expNr].lastMeasure=ex[expNr].nMeasure;
181     
182     //free memory
183     free_dvector(x,1,nLines);
184     free_dmatrix(y,1,nLines,1,ex[expNr].nobs);
185     free_dmatrix(sig,1,nLines,1,ex[expNr].nobs);
186 }
\end{verbatim}\normalsize 


