Initial snark14m import
[snark14.git] / src / snark / get_phantom.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/get_phantom.c $
5  $LastChangedRevision: 80 $
6  $Date: 2014-07-01 21:01:54 -0400 (Tue, 01 Jul 2014) $
7  $Author: agulati $
8  ***********************************************************
9
10  Previously part of:
11
12  /*--------------------------------------------------------------------------
13  Super Snark V 1.0
14  Written by Jingsheng Zheng... November 1992
15  Modified by Jolyon Browne... June 1993
16  ----------------------------------------------------------------------------*/
17
18 #include <string.h>
19 #include <stdlib.h>
20
21 #include "experimenter.h"
22 #include "errorc.h"
23 #include "get_phantom.h"
24
25 /*--------------------------------------------------------------------------
26  This function reads the ensemble data file and returns a list of
27  the phantom filenames.
28
29  INPUTS:
30  ensflname - name of ensemble data file.
31
32  OUTPUTS:
33  name - string array of length szens containing the list of phantom filenames.
34  szens - number of phantom filenames in the ensemble data file.
35  ----------------------------------------------------------------------------*/
36
37 void read_ensemble(char* ensflname, char name[][MAXLINESIZE], int* szens)
38 {
39         int i = 0;
40         char data[MAXLINESIZE];
41         FILE *ensfl;
42
43         if ((ensfl = fopen(ensflname, "r")) == NULL)
44         {
45                 errorc("error in opening file", ensflname);
46         }
47
48         while (fgets(data, sizeof(data), ensfl) != NULL)
49         {
50                 /* make sure 'i' is not over ensemble size limit */
51                 if (i >= MAXENSEMSIZE)
52                 {
53                         char limitmsg[25];
54                         sprintf(limitmsg, "(MAXENSEMSIZE = %d)", MAXENSEMSIZE);
55                         errorc("Exceeded maximum number of basic phantoms. ", limitmsg);
56                 }
57                 if (!isSkip(data))  //ignore comment lines and blank lines
58                         strcpy(name[i++], data);
59         }
60
61         *szens = i;
62         fclose(ensfl);
63 }
64
65 /* -----------------------------------------------------------------
66
67  INPUTS:
68  iexp - experiment number.
69  irun - run number.
70  expflg - controls the phantom-file set used in each experiment as
71  follows: if expflg=0 the same set of phantom files is available
72  for use in both experiments. If expflg=1 the phantom files used
73  in the first experiment are excluded in the second experiment.
74  nphan - two-element array: nphan[i-1]= number of phantom files used for
75  experiment i (i=1,2).
76  size_ens - number of phantom filenames in name_lst.
77  name_lst - string array of length szens_ens containing the list of phantom
78  filenames.
79  current_lst - string array of length phan[0] containing the list of
80  phantom filenames to be used for experiment 1.
81  
82
83  OUTPUTS:
84  size_ens - number of phantom filenames in name_lst.
85  name_lst - string array of length szens_ens containing the list of phantom
86  filenames.
87  current_lst - string array of length phan[0] containing the list of
88  phantom filenames to be used for experiment 1.
89  phan_flname - the phantom filename randomly chosen.
90  --------------------------------------------------------------------- */
91
92 void get_phantom(int iexp, int irun, int expflg, int* nphan, int* size_ens, char name_lst[][MAXLINESIZE], char current_lst[][MAXLINESIZE], char* phan_flname)
93 {
94         int i, j, szens = *size_ens;
95
96         if (expflg == 1 && iexp == 0 && irun == 0)
97         {
98                 for (j = 0; j < nphan[iexp]; j++)
99                 {
100
101                         i = (int) (szens * drand48()); /* (a) */
102                         strcpy(current_lst[j], name_lst[i]); /* (b) */
103                         strcpy(name_lst[i], name_lst[szens - 1]); /* (c) */
104                         szens--; /* (d) */
105                 }
106
107                 i = (int) (nphan[iexp] * drand48()); /* randomly select a phantom */
108                 strcpy(phan_flname, current_lst[i]); /* filename from current_lst */
109                 phan_flname[strlen(phan_flname) - 1] = '\0';
110         }
111         else if (expflg == 1 && iexp == 0 && irun > 0)
112         {
113
114                 i = (int) (nphan[iexp] * drand48());
115                 strcpy(phan_flname, current_lst[i]);
116                 phan_flname[strlen(phan_flname) - 1] = '\0';
117         }
118         else
119         {
120
121                 i = (int) (szens * drand48());
122                 strcpy(phan_flname, name_lst[i]);
123                 phan_flname[strlen(phan_flname) - 1] = '\0';
124         }
125         *size_ens = szens; /* number of phantom filenames in name_lst */
126 }