Initial snark14m import
[snark14.git] / src / snark / read_eval_recon1.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_recon1.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_recon1.h"
19
20 /* ----------------------------- read_eval_recon1.c ----------------------
21
22  This function reads from the eval file the abnormality index for
23  all the structures in a reconstruction. The abnormality index for a
24  structure is defined to be the average pixel value for those pixels
25  whose centers are within that structure.
26
27  INPUTS:
28  iter - iteration number
29  keywrd - keyword which defines the algorithm whose reconstruction is
30  to be read in.
31  numstr - number of structures in reconstruction.
32
33  OUTPUT:
34  recon - array of length numstr containing the abnormality index for each
35  structure in the reconstruction.
36  */
37
38 void read_eval_recon1(int* iter, char* keywrd, int numstr, double* recon)
39 {
40         int reg, area, i, tmp_iter;
41         double average;
42         char string[MAXLINESIZE], iter_text[MAXLINESIZE], test[MAXLINESIZE];
43         char * tmp_iter_ptr;
44         FILE *evalfile;
45
46         if (*iter == 0)
47         {
48                 /* this means we want the LAST iteration of the given algorithm;
49                  * first we need to figure out the number of this iteration though;
50                  *
51                  * 1) scan the eval file until the like containing the keywrd for the
52                  * algorithm is found
53                  * 2) keep scanning until the last iteration is found
54                  */
55
56                 if ((evalfile = fopen("eval", "r")) == NULL)
57                 {
58                         errorc("in read_eval_recon1.c: error in opening file ", "eval");
59                 }
60
61                 while (1)
62                 {
63                         /* Read eval file until a line which contains the string
64                          'execution name' is reached. If this line contains the
65                          keyword break out of the while loop. If end of file is
66                          reached then keyword was not found so terminate program. */
67
68                         fgets(string, sizeof(string), evalfile);
69                         if (strstr(string, "execution name") != NULL)
70                         {
71                                 sscanf(string, "%*s%*s %s", test);
72                                 if (strncasecmp(test, keywrd, 4) == 0)
73                                 {
74                                         break;
75                                 }
76                         }
77                         if (feof(evalfile))
78                         {
79                                 sprintf(string, "Error in read_eval_recon1.c: keyword %s not "
80                                                 "found in file", keywrd);
81                                 errorc(string, " eval");
82                         }
83                 }
84
85                 while (1)
86                 {
87                         /* Continue reading eval file until a line which contains
88                          the string 'Metrics for algorithm' is reached. If this
89                          line contains the string 'iteration' and the required
90                          iteration number break out of the while loop. If end
91                          of file is reached then the appropriate string was
92                          not found so print error message and exit */
93
94                         fgets(string, sizeof(string), evalfile);
95                         if ((strstr(string, "metrics for algorithm") != NULL) && // changed "m" in "metrics for algorithm" to lowercase. Lajos, Dec 16, 2004
96                                         ((tmp_iter_ptr = strstr(string, "iteration")) != NULL))
97                         {
98                                 //grab the iteration number from this line
99                                 tmp_iter_ptr = tmp_iter_ptr + 9;
100                                 if (0 == sscanf(tmp_iter_ptr, "%d", &tmp_iter))
101                                         sprintf(string,
102                                                         "Error in read_eval_recon1.c: iteration number missing");
103                         }
104
105                         if ((strstr(string, "execution name") != NULL) || (strstr(string, "execution name") != NULL))
106                         {
107                                 //we got to the next algorithm section, so better save the
108                                 //number of last iteration and break
109                                 *iter = tmp_iter;
110                                 break;
111                         }
112
113                 }
114                 fclose(evalfile); /* close eval file */
115         }
116
117         /* convert iteration number to a string */
118
119         sprintf(iter_text, "%d", *iter);
120
121         if ((evalfile = fopen("eval", "r")) == NULL)
122         {
123                 errorc("in read_eval_recon1.c: error in opening file ", "eval");
124         }
125
126         while (1)
127         {
128                 /* Read eval file until a line which contains the string
129                  'execution name' is reached. If this line contains the
130                  keyword break out of the while loop. If end of file is
131                  reached then keyword was not found so terminate program.  */
132
133                 fgets(string, sizeof(string), evalfile);
134                 if (strstr(string, "execution name") != NULL)
135                 {
136                         sscanf(string, "%*s%*s %s", test);
137                         if (strncasecmp(test, keywrd, 4) == 0)
138                         {
139                                 break;
140                         }
141                 }
142                 if (feof(evalfile))
143                 {
144                         sprintf(string, "Error in read_eval_recon1.c: keyword %s not found in file", keywrd);
145                         errorc(string, " eval");
146                 }
147         }
148
149         while (1)
150         {
151                 /* Continue reading eval file until a line which contains
152                  the string 'Metrics for algorithm' is reached. If this
153                  line contains the string 'iteration' and the required
154                  iteration number break out of the while loop. If end
155                  of file is reached then the appropriate string was
156                  not found so print error message and exit */
157
158                 fgets(string, sizeof(string), evalfile);
159                 if ((strstr(string, "metrics for algorithm") != NULL)
160                                 && // changed "m" in "metrics for algorithm" to lowercase. Lajos, Dec 16, 2004
161                                 (strstr(string, iter_text) != NULL)
162                                 && (strstr(string, "iteration") != NULL))
163                 {
164                         break;
165                 }
166
167                 if (feof(evalfile))
168                 {
169                         sprintf(string, "Error in read_eval_recon1.c: %s ---> iteration %d not found in", keywrd, *iter);
170                         errorc(string, " eval");
171                 }
172         }
173
174         while (1)
175         {
176                 /* Continue reading eval file until a line of numbers is
177                  reached then break out of while loop. Terminate program
178                  if end of file is reached */
179
180                 fgets(string, sizeof(string), evalfile);
181                 if (sscanf(string, "%d %d %lf", &reg, &area, &average) == 3)
182                 {
183                         break;
184                 }
185         }
186
187         if (feof(evalfile))
188         {
189                 errorc("Error in read_eval_recon1.c: end of file reached in file ",
190                                 "eval");
191         }
192         /* this is the abnormality index of the first structure
193          in the reconstruction */
194
195         recon[0] = average;
196
197         /* now read in the abnormality index for the remaining
198          structures */
199
200         for (i = 1; i < numstr; i++)
201         {
202                 fgets(string, sizeof(string), evalfile);
203                 sscanf(string, "%d %d %lf", &reg, &area, &average);
204                 recon[i] = average;
205         }
206
207         fclose(evalfile); /* close eval file */
208 }