Initial snark14m import
[snark14.git] / src / snark / read_eval_recon2.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_recon2.c $
5  $LastChangedRevision: 92 $
6  $Date: 2014-07-02 17:46:48 -0400 (Wed, 02 Jul 2014) $
7  $Author: agulati $
8  ***********************************************************
9
10  Previously part of SuperSNARK
11  */
12
13 #include <string.h>
14 #include <strings.h>
15
16 #include "experimenter.h"
17 #include "errorc.h"
18 #include "read_eval_recon2.h"
19
20 /* ----------------------------- read_eval_recon2.c ----------------------
21
22  This function reads from the eval file the normalized root mean
23  square distance between a reconstruction and the phantom
24
25  INPUTS:
26  mode - number of MODEs read in so far by analyze.c.
27  iteration - iteration number
28  keywrd - keyword which defines the algorithm whose reconstruction is
29  to be read in.
30  
31
32  OUTPUT:
33  recon - double precision variable which stores the normalized root mean
34  square distance between the (clipped) reconstruction and the
35  (clipped) phantom.
36  */
37
38 void read_eval_recon2(int mode, int iteration, char* keywrd, double* recon)
39 {
40         int area, global = 0, iter;
41         double average, distance;
42         char string[MAXLINESIZE], test[MAXLINESIZE];
43         FILE *evalfile;
44
45         if ((evalfile = fopen("eval", "r")) == NULL)
46                 errorc("in read_eval_recon2.c: error in opening file ", "eval");
47
48         /* read eval until the (mode+1)th occurrence of 'global' is reached. */
49         while (global != (mode + 1))
50         {
51
52                 fgets(string, sizeof(string), evalfile);
53                 if (strstr(string, "global") != NULL)
54                 {
55                         global++;
56                 }
57                 if (feof(evalfile))
58                 {
59                         errorc( "Error in read_eval_recon2.c: number of MODEs in comparison file exceeds number of globals in file", " eval");
60                 }
61         }
62
63         while (1)
64         {
65                 /* Read eval file until a line which contains the string
66                  'execution name' is reached. If this line contains the
67                  keyword, break out of the while loop. If end of file is
68                  reached then keyword was not found so terminate program. */
69
70                 fgets(string, sizeof(string), evalfile);
71
72                 if (strstr(string, "execution name") != NULL)
73                 {
74
75                         sscanf(string, "%*s%*s %s", test);
76                         if (strncasecmp(test, keywrd, 4) == 0)
77                         {
78                                 break;
79                         }
80                 }
81
82                 if (feof(evalfile))
83                 {
84                         sprintf(string,
85                                         "Error in read_eval_recon2.c: keyword %s not found in file",
86                                         keywrd);
87                         errorc(string, " eval");
88                 }
89         }
90
91         while (1)
92         {
93                 /* Continue reading eval file until a line of numbers is
94                  reached then break out of while loop. If end of file
95                  is encountered then terminate program. */
96
97                 fgets(string, sizeof(string), evalfile);
98                 if (sscanf(string, "%d %d %lf %lf", &iter, &area, &average, &distance) == 4)
99                 {
100                         break;
101                 }
102                 if (feof(evalfile))
103                 {
104                         errorc("Error in read_eval_recon2.c: end of file reached in file ", "eval");
105                 }
106         }
107
108         /* If necessary search the remaining lines of numbers until
109          the appropriate iteration number is found. Then save the
110          distance and break out of while loop. If approriate
111          iteration number is not found then terminate program. */
112
113         if (iteration != 0)
114         {
115
116                 /* if this first line of numbers has correct iteration
117                  number save the distance and return*/
118
119                 if (iter == iteration)
120                 {
121                         *recon = distance;
122                         fclose(evalfile);
123                         return;
124                 }
125
126                 while (1)
127                 {
128
129                         fgets(string, sizeof(string), evalfile);
130                         sscanf(string, "%d %d %lf %lf", &iter, &area, &average, &distance);
131                         if (iter == iteration)
132                         {
133                                 *recon = distance;
134                                 break;
135                         }
136                         if (feof(evalfile))
137                         {
138                                 sprintf(string, "Error in read_eval_recon2.c: %s --> iteration %d not found in file", keywrd, iteration);
139                                 errorc(string, " eval");
140                         }
141                 }
142         }
143
144         // special case iteration == 0
145         // the last iteration should be processed instead of a literal interpretation of 0
146         // olangthaler, 03/14/14
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_recon2.c: %s --> iteration %d not found in file", keywrd, iteration);
162                                 errorc(string, " eval");
163                         }
164                 }
165
166                 sscanf(prev, "%d %d %lf %lf", &iter, &area, &average, &distance);
167                 *recon = distance;
168         }
169
170         fclose(evalfile); /* close eval file */
171 }