Initial snark14m import
[snark14.git] / src / snark / supersnk.c
1 /*
2  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3  *                                                               *
4  *                         S N A R K   1 4                       *
5  *                                                               *
6  *                A PICTURE RECONSTRUCTION PROGRAM               *
7  *                                                               *
8  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
9
10   supersnk.c,v 1.16 2011/02/21 00:45:48 jklukowska Exp
11
12  Previously part of SuperSNARK*/
13
14 //#define SS_DEBUG  //bug 269, Joanna Klukowska, converted to a variable below
15
16 #include <stdio.h>
17 #include <time.h>
18 #include <string.h>
19 #include <stdlib.h>
20
21 #include "experimenter.h"
22
23 #include "get_phantom.h"
24 #include "create_phantom.h"
25 #include "projection.h"
26 #include "recon.h"
27 #include "analysis.h"
28 #include "errorc.h"
29 #include "analyze.h"
30 #include "analyze2.h"
31 #include "get_iter_flag.h"
32 //#include "get_seed.h"
33 #include "check_if_iroi.h"
34 #include "snark.h"
35
36 /* --------------------------- supersnk.c -------------------------------
37  *
38  * This function is the driver program to SNARK. It controls phantom
39  * selection and feeds SNARK the necessary commands to generate the phantoms
40  * plus their projection data, to implement the desired reconstruciton
41  * algorithms on these data, and to evaluate the reconstructed images.
42  * This function also calls the necessary routines to compute the
43  * desired figures of merit and their statistical significance.
44  *
45  * INPUTS:
46  * nexp - number of experiments.
47  * expflg - controls the phantom-file set used in each experiment as
48  * follows: if expflg=0 the same set of phantom files is available
49  * for use in both experiments. If expflg=1 the phantom files used
50  * in the first experiment are excluded in the second experiment.
51  * navel, nelem,pixsz - modifiers described by Set 4 of the SNARK CREATE
52  * input sequence (see the SNARK manual for details).
53  * nphan - two-element array: nphan[i-1]= number of phantom files used for
54  * experiment i (i=1,2).
55  * nrun - two-element array: nrun[i-1]= number of runs for experiment i
56  * (i=1,2).
57  * ensflname - name of ensemble data file.
58  * projflname - name of file containing information about the projection
59  * data.
60  * recflname - name of file containing information about the reconstruction
61  * algorithms to be implemented.
62  * anaflname - name of file containing information about the algorithms
63  * being compared.
64  *
65  * OUTPUTS:
66  * none
67  * -------------------------------------------------------------------- */
68
69
70 void supersnk(
71         int nexp,
72         int expflg,
73         int nelem,
74         float pixsz,
75         int navel,
76         int* nphan,
77         int* nrun,
78         char* ensflname,
79         char* projflname,
80         char* recflname,
81         char* anaflname,
82         char* progName,
83         long seed
84         )
85 //int nexp,expflg,nelem,navel,*nphan,*nrun;
86 //float pixsz;
87 //char *ensflname,*projflname,*recflname,*anaflname;
88 {
89
90     //jk 01/01/08
91     //changed pairsarray[MAXNSTR] to *pairsarray
92     //changed structure[MAXNSTR][MAXLINESIZE] to **structure
93     //to allow unlimited number of structures
94     //the file will need to be scanned firest to
95     //allocate the array of right size
96     int *pairsarray;
97     char **structure;
98     int i, j, run, szens, nstr, npairs, tot_PAIRS;
99     int iroi_used; //set to 0 or 1 by check_if_iroi
100     //it is used by create_phantom() to verify that density of one out
101     //of each paired  structures is zero
102     char namelst[MAXENSEMSIZE][MAXLINESIZE],
103             phanflname[MAXLINESIZE],
104             currentlst[MAXENSEMSIZE][MAXLINESIZE], fomflname[MAXLINESIZE],
105             iter_flag[51], ifname[64], ofname[64], recfilname[64], enums[8], rnums[8], command[256],pipcommand[256];
106
107     FILE *fomfl;
108
109     char * mode;
110     int SS_DEBUG = 0;
111     mode = getenv("SNARK_EXP");
112     if (mode != NULL && strcasecmp(mode, "DEBUG") == 0)
113     {
114         SS_DEBUG = 1;
115         printf("Running in DEBUG mode: extra files will be saved.\n");
116     }
117
118     /* get list of phantom filenames and iteration flag for EVALUATE command */
119     read_ensemble(ensflname, namelst, &szens);
120     strncpy(iter_flag, "10000000000000000000000000000000000000000000000000\0",51);
121     getiterflag(anaflname, iter_flag);
122
123     strcat(pipcommand,"tee snark_e.in | (");
124     strcat(pipcommand,progName);
125     strcat(pipcommand," > snark_e.out)"); 
126     for (i = 0; i < nexp; i++) {
127         fprintf(stdout, "experiment %d\n", i + 1); // bug 187 - progress indicator - swr - 11/11/05
128         /* sprintf(fomflname,"%s.%d",fomflname,i+1); */ /* bug: concats. exp.# onto prev. name ('fomfil.1.2.3....') */
129         sprintf(fomflname, "fomfil.%d", i + 1); /* fix: concats. exp.# onto 'fomfil' only (JD 8/8/05) */
130         tot_PAIRS = 0; /* number of phantoms with PAIRed structures */
131         
132         for (run = 0; run < nrun[i]; run++) {
133             fprintf(stdout, "run %d\n", run + 1); // bug 187 - progress indicator - swr - 11/11/05
134             /* beginning of inner for loop */
135
136
137             if(SS_DEBUG) {
138             //fprintf(stdout, "------------------------------");
139
140                 enums[0] = 0;
141                 sprintf(enums, "%02d", i);
142
143                 rnums[0] = 0;
144                 sprintf(rnums, "%02d", run);
145
146                 ifname[0] = 0;
147                 //strcat(ifname, "snark_e");
148                 //strcat(ifname, ".in");
149                 sprintf(ifname, "snark_e%d.in", run);
150
151                 ofname[0] = 0;
152                 //strcat(ofname, "snark_e");
153                 //strcat(ofname, ".out");
154                 sprintf(ofname, "snark_e%d.out", run);
155
156                 recfilname[0] = '\0';
157                 sprintf(recfilname, "recfil%d", run);
158
159                 if ((pstream = fopen(ifname, "w")) == NULL) {
160                     errorc("Could not open", ifname);
161                 }
162             } else {
163                 // bug 170 - swr - 10/01/05
164                 if (system("test -x ./bin/snark14UserDefined") || // file exists and executable
165                         system("test -s ./bin/snark14UserDefined") || // exists and has non-zero size
166                         system("test -f ./bin/snark14UserDefined")) { // exists and is regular (not a directory
167                 // bug 298 - Ze Ye 08/13/15
168                       if ((pstream = popen("tee snark_e.in | (./bin/snark14 > snark_e.out)", "w")) == NULL) {
169                         errorc("Could not run", "snark");
170                     }
171                 } 
172                         else {
173                    if ((pstream = popen("tee snark_e.in | (./bin/snark14UserDefined> snark_e.out )", "w")) == NULL) {
174                         errorc("Could not run", "snark");
175                     }
176
177                 }
178             }
179
180             get_phantom(i, run, expflg, nphan, &szens, namelst, currentlst,
181                     phanflname);
182
183             check_if_iroi(anaflname, &iroi_used);
184
185             create_phantom(phanflname, seed, navel, nelem, pixsz, &structure,
186                     &nstr, &npairs, &pairsarray, iroi_used);
187             
188             projection(projflname, seed);
189
190             recon(recflname, i + 1, run + 1);
191
192             evaluate(anaflname, &structure, nstr, iter_flag);
193
194             fprintf(pstream, "END \n");
195                 
196             if (SS_DEBUG) {
197                 fclose(pstream);
198
199                 command[0] = 0;
200                 //strcat(command, "snark14 ");
201                 strcat(command, progName);
202                 strcat(command, " ");
203                 strcat(command, ifname);
204                 strcat(command, " > ");
205                 strcat(command, ofname);
206
207                 if (system(command) != 0) {
208                     fprintf(stdout, "\nERROR running: %s\n", command);
209                     exit(1);
210                 }
211                 else
212                     rename ("recfil", recfilname);
213
214                 
215             } else {
216                 pclose(pstream);
217             }
218
219             /*    keep count of the number of phantoms */
220             if (npairs != 0) {
221                 tot_PAIRS += 1; /* with PAIRed structures */
222             }
223
224             if (run == 0) {
225                 if ((fomfl = fopen(fomflname, "w")) == NULL) {
226                     errorc("in supersnk.c: error opening file ", fomflname);
227                 }
228             } else {
229                 if ((fomfl = fopen(fomflname, "a")) == NULL) {
230                     errorc("in supersnk.c: error opening file ", fomflname);
231                 }
232             }
233
234
235             analyze(i + 1, run + 1, npairs, pairsarray, anaflname, fomfl);
236             fclose(fomfl);
237
238             /* remove snark.out after every run except the very last run */
239
240             if( !SS_DEBUG) {
241                 if (nexp == 1) {
242                     if (run != nrun[0] - 1) {
243                         if (remove("snark_e.out") != 0) {
244                             errorc("in supersnk: error deleting file ", "snark.out");
245                         }
246                     }
247                 } else {
248                     if ((i == 0) || ((i == 1) && (run != nrun[1] - 1))) {
249                         if (remove("snark_e.out") != 0) {
250                             errorc("in supersnk: error deleting file ", "snark_e.out");
251                         }
252                     }
253                 }
254             }
255
256         } /*    end of inner for loop */
257
258         //jk 01/01/08 free memory
259         free(pairsarray);
260         for (j = nstr - 1; j >= 0; j--) {
261             free((void*) (structure[j]));
262         }
263
264         analyze2(i + 1, nrun[i], tot_PAIRS, anaflname);
265
266     } /* end of outer for loop */
267 }