Initial snark14m import
[snark14.git] / src / snark / read_eval_recon4.c
1 /*
2 ***********************************************************
3 $SNARK_Header: S N A R K  1 4 - A PICTURE RECONSTRUCTION PROGRAM $
4 $HeadURL: svn://dig.cs.gc.cuny.edu/snark/trunk/src/snark/read_eval_recon4.c $
5 $LastChangedRevision: 137 $
6 $Date: 2014-07-15 16:38:33 -0400 (Tue, 15 Jul 2014) $
7 $Author: bprommegger $
8 ***********************************************************
9 */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <strings.h>
14 #include "experimenter.h"
15 #include "errorc.h"
16 #include "read_eval_recon4.h"
17
18 /*
19  this function reads from the eval file the kullback-leibler distance
20
21  INPUTS:
22  iteration - iteration number
23  keywrd - keyword which defines the algorithm whose reconstruction is to be read in.
24
25  OUTPUT:
26  recon - double precision variable which stores the normalized root mean
27  square distance between the (clipped) reconstruction and the
28  (clipped) phantom.
29 */
30
31 void read_eval_recon4(int iteration, char* keywrd, double* klds, double* wsqd)
32 {
33         int area = 0;
34         int global = 0;
35         int iter = 0;
36         double average = 0;
37         double distance = 0;
38         double relerr = 0;
39         double variance = 0;
40         double stddev = 0;
41         double residual = 0;
42         double kld = 0;
43         double local_wsqd = 0;
44         char string[MAXLINESIZE];
45         char test[MAXLINESIZE];
46         FILE *evalfile;
47
48         if ((evalfile = fopen("eval", "r")) == NULL) errorc("in read_eval_recon4.c: error in opening file ", "eval");
49
50         while (1)
51         {
52                 /* Read eval file until a line which contains the string
53                  'experimenter evaluation 2' is reached. If end of file is
54                  reached then keyword was not found so terminate program. */
55
56                 fgets(string, sizeof(string), evalfile);
57
58                 if (strstr(string, "experimenter evaluation 2") != NULL)
59                 {
60                         break;
61                 }
62
63                 if (feof(evalfile))
64                 {
65                         fflush(stdout);
66                         sprintf(string,"Error in read_eval_recon4.c: keyword %s not found in file ","experimenter evaluation 2");
67                         errorc(string, "eval");
68                 }
69         }
70
71         while (1)
72         {
73                 /* Read eval file until a line which contains the string
74                  'execution name' is reached. If this line contains the
75                  keyword, break out of the while loop. If end of file is
76                  reached then keyword was not found so terminate program. */
77
78                 fgets(string, sizeof(string), evalfile);
79
80                 if (strstr(string, "execution name") != NULL)
81                 {
82                         sscanf(string, "%*s%*s %s", test);
83                         if (strncasecmp(test, keywrd, 4) == 0)
84                         {
85                                 break;
86                         }
87                 }
88
89                 if (feof(evalfile))
90                 {
91                         fflush(stdout);
92                         sprintf(string,"Error in read_eval_recon4.c: keyword %s not found in file ",keywrd);
93                         errorc(string, "eval");
94                 }
95         }
96
97         while (1)
98         {
99                 /* Continue reading eval file until a line of numbers is
100                  reached, then break out of while loop. If end of file
101                  is encountered then terminate program. */
102                 fgets(string, sizeof(string), evalfile);
103                 if (sscanf(string, "%d %d %lf %lf %lf %lf %lf %lf %lf %lf", &iter, &area, &average, &distance, &relerr, &variance, &stddev, &residual, &kld, &local_wsqd) == 10)
104                 {
105                         break;
106                 }
107                 if (feof(evalfile))
108                 {
109                         errorc("Error in read_eval_recon4.c: end of file reached in file eval. Hint: Have the iteration flags been set to 3?","");
110                 }
111         }
112
113         /* If necessary, search the remaining lines of numbers until
114          the appropriate iteration number is found. Then save the
115          distance and break out of while loop. If approriate
116          iteration number is not found then terminate program. */
117         if (iteration != 0)
118         {
119                 /* if this first line of numbers has correct iteration number, return*/
120                 if (iter == iteration)
121                 {
122                         *klds = kld;
123                         *wsqd = local_wsqd;
124                         fclose(evalfile);
125                         return;
126                 }
127
128                 while (1)
129                 {
130                         fgets(string, sizeof(string), evalfile);
131                         sscanf(string, "%d %d %lf %lf %lf %lf %lf %lf %lf %lf", &iter, &area, &average, &distance, &relerr, &variance, &stddev, &residual, &kld, &local_wsqd);
132                         if (iter == iteration)
133                         {
134                                 *klds = kld;
135                                 *wsqd = local_wsqd;
136                                 break;
137                         }
138                         if (feof(evalfile))
139                         {
140                                 sprintf(string,"Error in read_eval_recon4.c: %s --> iteration %d not found in file", keywrd, iteration);
141                                 errorc(string, " eval");
142                         }
143                 }
144         }
145
146         // iteration == 0: the last iteration should be processed instead of a literal interpretation of 0
147         else
148         {
149                 char prev[MAXLINESIZE];
150
151                 while (1)
152                 {
153                         strcpy(prev, string);
154                         fgets(string, sizeof(string), evalfile);
155                         if (strlen(string) < 3)
156                         {
157                                 break;
158                         }
159                         if (feof(evalfile))
160                         {
161                                 sprintf(string, "Error in read_eval_recon4.c: %s --> iteration %d not found in file",keywrd, iteration);
162                                 errorc(string, " eval");
163                         }
164                 }
165
166                 sscanf(prev, "%d %d %lf %lf %lf %lf %lf %lf %lf %lf", &iter, &area, &average, &distance, &relerr, &variance, &stddev, &residual, &kld, &local_wsqd);
167                 *klds = kld;
168                 *wsqd = local_wsqd;
169         }
170
171         fclose(evalfile);
172 }