Initial snark14m import
[snark14.git] / src / snark / experimenter.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/experimenter.c $
5  $LastChangedRevision: 177 $
6  $Date: 2015-08-03 16:44:07 -0400 (Mon, 03 Aug 2015) $
7  $Author: zye $
8  ***********************************************************
9
10  Previously part of SuperSNARK
11  */
12
13 /* --------------------------- experimenter.c -----------------------------
14
15  This is the main program. It reads input directly from the keyboard
16  or from a redirected file.
17
18  --------------------------------------------------------------------------- */
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <strings.h>
26 #include <stdlib.h>
27
28 #include "experimenter.h"
29 #include "errorc.h"
30 #include "supersnk.h"
31
32 #ifdef WIN32
33
34 #include <string.h>
35
36 int strncasecmp(char* str1, char* str2, int len)
37 {
38         return strncmp(_strupr(str1), _strupr(str2), len);
39 }
40
41 #endif
42
43 void errorchk1(int nexp, int expflg, int nphan1, int nrun1);
44
45 int experimenter(int argc, char* argv[])
46 {
47         char string[MAXLINESIZE], ensflname[MAXLINESIZE], projflname[MAXLINESIZE],
48                         recflname[MAXLINESIZE], anaflname[MAXLINESIZE], tmp[MAXLINESIZE];
49         int nexp, expflg, nelem, navel, nphan[MAXEXP], nrun[MAXEXP];
50         float pixsz;
51         long seed;
52
53         char progName[256];
54         strcpy(progName, argv[0]);
55
56         fprintf(stdout, "snark14Experimenter.s170426\n");
57
58         if (argc > 1)
59         {
60                 close(fileno(stdin));
61                 if (open(argv[2], O_RDONLY) < 0)
62                 {
63                         perror(argv[2]);
64                         exit(1);
65                 }
66         }
67
68         while (1)
69         {
70                 // optional SEED line  and  ENSEMBLE line
71                 if (fgets(string, sizeof(string), stdin) == NULL)
72                 {
73                         fprintf(stdout, "input error: end of file reached\n");
74                         break;
75                 }
76                 while (isSkip(string))     // skip comment lines and blank lines
77                 {
78                         if (fgets(string, sizeof(string), stdin) == NULL)
79                         {
80                                 fprintf(stdout, "input error: end of file reached\n");
81                                 break;
82                         }
83                 }
84                 if (strncasecmp(string, "see", 3) == 0)
85                 {
86                         if (1 == sscanf(string, "%*s%ld", &seed))
87                         {
88                                 if (seed < 0)
89                                         time(&seed);
90                                 srand48(seed);
91                         }
92                         else
93                         {
94                                 errorc("Error reading supersnark input file at ", string);
95                         }
96                         // now read the ens line
97                         if (fgets(string, sizeof(string), stdin) == NULL)
98                         {
99                                 fprintf(stdout, "input error: end of file reached\n");
100                                 break;
101                         }
102                         while (isSkip(string))     // skip comment lines and blank lines
103                         {
104                                 if (fgets(string, sizeof(string), stdin) == NULL)
105                                 {
106                                         fprintf(stdout, "input error: end of file reached\n");
107                                         break;
108                                 }
109                         }
110                         if (strncasecmp(string, "ens", 3) == 0)
111                         {
112                                 sscanf(string, "%*s%s", ensflname);
113                         }
114                         else if (strncasecmp(string, "end", 3) == 0)
115                         {
116                                 break;
117                         }
118                         else
119                         {
120                                 errorc("Error reading supersnark input file at ", string);
121                                 break;
122                         }
123
124                 }
125                 else if (strncasecmp(string, "ens", 3) == 0)
126                 {
127                         sscanf(string, "%*s%s", ensflname);
128                         // there is not seed line, so set seed to current time
129                         time(&seed);
130                         srand48(seed);
131                 }
132                 else if (strncasecmp(string, "end", 3) == 0)
133                 {
134                         break;
135                 }
136                 else
137                 {
138                         errorc("Error reading supersnark input file at ", string);
139                         break;
140                 }
141
142                 if (fgets(string, sizeof(string), stdin) == NULL)
143                 {
144                         fprintf(stdout, "input error: end of file reached\n");
145                         break;
146                 }
147                 while (isSkip(string))     // skip comment lines and blank lines
148                 {
149                         if (fgets(string, sizeof(string), stdin) == NULL)
150                         {
151                                 fprintf(stdout, "input error: end of file reached\n");
152                                 break;
153                         }
154                 }
155                 if (strncasecmp(string, "exp", 3) == 0)
156                 {
157
158                         sscanf(string, "%*s %d %d %d %f %d %d %d", &nexp, &expflg, &nelem,
159                                         &pixsz, &navel, &nphan[0], &nrun[0]);
160                         errorchk1(nexp, expflg, nphan[0], nrun[0]);
161
162                         if (nexp > 1)
163                         {
164                                 sscanf(string, "%*s%*d%*d%*d%*f%*d%*d%*d%d%d", &nphan[1],
165                                                 &nrun[1]);
166                                 if (nphan[1] <= 0 || nrun[1] <= 0)
167                                 {
168                                         errorc("Invalid number of phantoms and/or runs ",
169                                                         "for experiment 2");
170                                 }
171                         }
172                 }
173                 else
174                 {
175                         errorc("Error reading supersnark input file at ", string);
176                         break;
177                 }
178
179                 if (fgets(string, sizeof(string), stdin) == NULL)
180                 {
181                         fprintf(stdout, "input error: end of file reached\n");
182                         break;
183                 }
184                 while (isSkip(string))     // skip comment lines and blank lines
185                 {
186                         if (fgets(string, sizeof(string), stdin) == NULL)
187                         {
188                                 fprintf(stdout, "input error: end of file reached\n");
189                                 break;
190                         }
191                 }
192                 if (strncasecmp(string, "dat", 3) == 0)
193                 {
194                         sscanf(string, "%*s %s", projflname);
195                 }
196                 else
197                 {
198                         errorc("Error reading supersnark input file at ", string);
199                         break;
200                 }
201
202                 if (fgets(string, sizeof(string), stdin) == NULL)
203                 {
204                         fprintf(stdout, "input error: end of file reached\n");
205                         break;
206                 }
207                 while (isSkip(string))     // skip comment lines and blank lines
208                 {
209                         if (fgets(string, sizeof(string), stdin) == NULL)
210                         {
211                                 fprintf(stdout, "input error: end of file reached\n");
212                                 break;
213                         }
214                 }
215                 if (strncasecmp(string, "rec", 3) == 0)
216                 {
217                         sscanf(string, "%*s %s", recflname);
218                 }
219                 else
220                 {
221                         errorc("Error reading supersnark input file at ", string);
222                         break;
223                 }
224
225                 if (fgets(string, sizeof(string), stdin) == NULL)
226                 {
227                         fprintf(stdout, "input error: end of file reached\n");
228                         break;
229                 }
230                 while (isSkip(string))     // skip comment lines and blank lines
231                 {
232                         if (fgets(string, sizeof(string), stdin) == NULL)
233                         {
234                                 fprintf(stdout, "input error: end of file reached\n");
235                                 break;
236                         }
237                 }
238                 if (strncasecmp(string, "ana", 3) == 0)
239                 {
240                         sscanf(string, "%*s %s", anaflname);
241                 }
242                 else
243                 {
244                         errorc("Error reading supersnark input file at ", string);
245                         break;
246                 }
247
248                 supersnk(nexp, expflg, nelem, pixsz, navel, nphan, nrun, ensflname,
249                                 projflname, recflname, anaflname, progName, seed);
250         }
251         return 0;
252 }
253
254 // this function checks for invalid inputs
255 void errorchk1(int nexp, int expflg, int nphan1, int nrun1)
256 {
257         if (nexp != 1 && nexp != 2)
258         {
259                 errorc("Invalid number of experiments. ", "See EXPERIMENT command");
260         }
261
262         if (expflg != 0 && expflg != 1)
263         {
264                 errorc("Invalid entry for exp-flg. ", "See EXPERIMENT command");
265         }
266
267         if (nphan1 <= 0 || nrun1 <= 0)
268         {
269                 errorc("Invalid number of phantoms and/or runs ", "for experiment 1");
270         }
271 }