1 | #include <stdio.h>
|
---|
2 | #include <string.h>
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <math.h>
|
---|
5 | #include "diag.h"
|
---|
6 | #include "geometry.h"
|
---|
7 | #include "init.h"
|
---|
8 |
|
---|
9 | extern char line[]; /* parsing buf. (init) */
|
---|
10 | extern char axisdev_filename[256], reflectivity_filename[256];
|
---|
11 |
|
---|
12 | float mean_refl; /* Mirror mean reflectivity 270-610 nm.
|
---|
13 | * AM June 2002.
|
---|
14 | */
|
---|
15 |
|
---|
16 | float ct_Focal_mean; /* focal dist. (mean) (cm) */
|
---|
17 | float ct_PSpread_mean; /* pt. spread fn. (mean) (cm) */
|
---|
18 | float ct_BlackSpot_rad; /* black spot radius (cm) */
|
---|
19 | float ct_RMirror; /* rad. of single mirror (cm) */
|
---|
20 | int ct_NMirrors=0; /* number of mirrors */
|
---|
21 | float ct_CameraWidth; /* camera width (cm) */
|
---|
22 | int ct_NPixels; /* number of pixels */
|
---|
23 | float ct_PixelWidth; /* pixel width (cm) */
|
---|
24 |
|
---|
25 | mirror *ct_data=NULL; /* ptr to mirror data */
|
---|
26 | FILE *ct_BinaryData=NULL; /* binary data for mirrors */
|
---|
27 | char ct_BinaryName[128]; /* binary data filename */
|
---|
28 |
|
---|
29 | int nReflectivity=0; /* elements in refl. table */
|
---|
30 | float *Reflectivity[2]; /* reflectivity table */
|
---|
31 | float *AxisDeviation[2]; /* axis deviation table */
|
---|
32 | float *ct_Focal; /* focal length table */
|
---|
33 |
|
---|
34 | /* Prototypes */
|
---|
35 | static void ReadMirrorTable(FILE *geofile);
|
---|
36 | static void ReadReflectivity(char *datname);
|
---|
37 | static void ReadAxisDev(char *datname);
|
---|
38 | static void ReadFocals(void);
|
---|
39 |
|
---|
40 | static void ReadMirrorTable(FILE *geofile)
|
---|
41 | { int i; /* Mirror index */
|
---|
42 |
|
---|
43 | if ((ct_data=(mirror *)malloc(sizeof(mirror)*ct_NMirrors)) == NULL)
|
---|
44 | FatalError(MIRR_ALLOC_FTL, ct_NMirrors);
|
---|
45 | Log(MIRR_ALLOC_LOG, ct_NMirrors);
|
---|
46 | Log(MIRR_TABLE_LOG);
|
---|
47 |
|
---|
48 | Log(READ_ASCII_LOG);
|
---|
49 | for (i=0; i<ct_NMirrors; i++)
|
---|
50 | {
|
---|
51 | if (12 != fscanf(geofile, "%d %f %f %f %f %f %f %f %f %f %f %f",
|
---|
52 | &ct_data[i].i, &ct_data[i].f,
|
---|
53 | &ct_data[i].sx, &ct_data[i].sy,
|
---|
54 | &ct_data[i].x, &ct_data[i].y, &ct_data[i].z,
|
---|
55 | &ct_data[i].theta, &ct_data[i].phi,
|
---|
56 | &ct_data[i].xn, &ct_data[i].yn, &ct_data[i].zn))
|
---|
57 | break;
|
---|
58 | }
|
---|
59 | if (i < ct_NMirrors)
|
---|
60 | FatalError(MIRR_FEW___FTL, i);
|
---|
61 |
|
---|
62 | } /* end of ReadMirrorTable */
|
---|
63 |
|
---|
64 | static void ReadReflectivity(char *datname)
|
---|
65 | {
|
---|
66 | FILE *datfile = fopen(datname, "r");
|
---|
67 | int current = 0;
|
---|
68 |
|
---|
69 | mean_refl = 0.;
|
---|
70 |
|
---|
71 | if (datfile == NULL)
|
---|
72 | FatalError(RFLF_ERROR_FTL, datname);
|
---|
73 | else
|
---|
74 | printf("Reading file %s\n", datname);
|
---|
75 |
|
---|
76 | while (fgets(line, LINE_MAX_LENGTH, datfile))
|
---|
77 | {
|
---|
78 | if (line[0] == '#') continue;
|
---|
79 |
|
---|
80 | if (nReflectivity == 0)
|
---|
81 | {
|
---|
82 | nReflectivity = atoi(line);
|
---|
83 | if (nReflectivity)
|
---|
84 | {
|
---|
85 | if ((Reflectivity[0] =
|
---|
86 | (float *) malloc(sizeof(float) * nReflectivity)) == NULL ||
|
---|
87 | (Reflectivity[1] =
|
---|
88 | (float *) malloc(sizeof(float) * nReflectivity)) == NULL)
|
---|
89 | FatalError(REFL_ALLOC_FTL, nReflectivity);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | else if (2 == sscanf(line, "%f %f", &Reflectivity[0][current],
|
---|
93 | &Reflectivity[1][current]))
|
---|
94 | {
|
---|
95 | // Added June 2002, AM:
|
---|
96 | mean_refl += Reflectivity[1][current];
|
---|
97 | current++;
|
---|
98 | if (current >= nReflectivity) break;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | fclose(datfile);
|
---|
102 |
|
---|
103 | nReflectivity = current;
|
---|
104 | if (current > 0)
|
---|
105 | mean_refl /= (float) current;
|
---|
106 |
|
---|
107 | } /* end of ReadReflectivity */
|
---|
108 |
|
---|
109 | static void ReadAxisDev(char *datname)
|
---|
110 | { FILE *datfile = fopen(datname, "r");
|
---|
111 | int current = 0;
|
---|
112 |
|
---|
113 | if (datfile == NULL)
|
---|
114 | FatalError(AXIS_ERROR_FTL, datname);
|
---|
115 | else
|
---|
116 | printf("Reading file %s\n", axisdev_filename);
|
---|
117 |
|
---|
118 | if ((AxisDeviation[0]=
|
---|
119 | (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL
|
---|
120 | || (AxisDeviation[1]=
|
---|
121 | (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL)
|
---|
122 | FatalError(AXIS_ALLOC_FTL, ct_NMirrors);
|
---|
123 | while (fgets(line, LINE_MAX_LENGTH, datfile))
|
---|
124 | { if (line[0] == '#') continue;
|
---|
125 | if (2==sscanf(line, "%f %f",
|
---|
126 | &AxisDeviation[0][current], &AxisDeviation[1][current]));
|
---|
127 | { current++;
|
---|
128 | if (current >= ct_NMirrors) break; }}
|
---|
129 | fclose(datfile);
|
---|
130 |
|
---|
131 | if (current != ct_NMirrors)
|
---|
132 | FatalError(AXIS_FEW___FTL, current, ct_NMirrors);
|
---|
133 | } /* end of ReadAxisDev */
|
---|
134 |
|
---|
135 | static void ReadFocals(void)
|
---|
136 | { int loop;
|
---|
137 |
|
---|
138 | if ((ct_Focal = (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL)
|
---|
139 | FatalError(FOCL_FEW___FTL, ct_NMirrors);
|
---|
140 |
|
---|
141 | for (loop=0; loop<ct_NMirrors; loop++)
|
---|
142 | ct_Focal[loop] = ct_data[loop].f;
|
---|
143 | } /* end of ReadFocals */
|
---|
144 |
|
---|
145 | void GeometrySwitch(FILE *geofile)
|
---|
146 | { char *value_ptr = NULL; /* ptr at parm value */
|
---|
147 | int switch_end = FALSE; /* bool to exit loop */
|
---|
148 | extern char whites[]; /* white chars (init) */
|
---|
149 | extern int ParseLine(FILE *parfile, /* FILE with parms */
|
---|
150 | const char *token_list[], /* array w/tokens */
|
---|
151 | int tokens, /* nr of tokens */
|
---|
152 | char **value_ptr); /* ptr->parm val. */
|
---|
153 |
|
---|
154 | /* Initialise arrays */
|
---|
155 | Reflectivity[0] = AxisDeviation[0] = NULL;
|
---|
156 |
|
---|
157 | do
|
---|
158 | { switch(ParseLine(geofile, ctparms, ARRAY_SZ(ctparms), &value_ptr))
|
---|
159 | { case type:
|
---|
160 | if (1 != atoi(value_ptr))
|
---|
161 | FatalError(TYPE_ERROR_FTL);
|
---|
162 | break;
|
---|
163 | case focal_distance:
|
---|
164 | Log(LOG__FLOAT_LOG, "focal distance (average, cm)",
|
---|
165 | ct_Focal_mean = (float) atof(value_ptr));
|
---|
166 | break;
|
---|
167 | case focal_std: /* not implemented. */
|
---|
168 | break;
|
---|
169 | case point_spread:
|
---|
170 | Log(LOG__FLOAT_LOG, "point spread fn. sigma (average, cm)",
|
---|
171 | ct_PSpread_mean = (float) atof(value_ptr));
|
---|
172 | break;
|
---|
173 | case point_std: /* not implemented */
|
---|
174 | break;
|
---|
175 | case adjustment_dev: /* not implemented */
|
---|
176 | break;
|
---|
177 | case black_spot:
|
---|
178 | Log(LOG__FLOAT_LOG, "radius of black spot (cm)",
|
---|
179 | ct_BlackSpot_rad = (float) atof(value_ptr));
|
---|
180 | break;
|
---|
181 | case n_mirrors:
|
---|
182 | Log(LOG__INT___LOG, "number of mirrors",
|
---|
183 | ct_NMirrors = atoi(value_ptr));
|
---|
184 | break;
|
---|
185 | case r_mirror:
|
---|
186 | Log(LOG__FLOAT_LOG, "single mirror radius (cm)",
|
---|
187 | ct_RMirror = (float) atof(value_ptr));
|
---|
188 | break;
|
---|
189 | case camera_width:
|
---|
190 | Log(LOG__FLOAT_LOG, "camera radius (cm)",
|
---|
191 | ct_CameraWidth = (float) atof(value_ptr));
|
---|
192 | break;
|
---|
193 | case n_pixels:
|
---|
194 | Log(LOG__INT___LOG, "number of pixels",
|
---|
195 | ct_NPixels = atoi(value_ptr));
|
---|
196 | break;
|
---|
197 | case pixel_width:
|
---|
198 | Log(LOG__FLOAT_LOG, "pixel width (cm)",
|
---|
199 | ct_PixelWidth = (float) atof(value_ptr));
|
---|
200 | break;
|
---|
201 | case n_centralpixels:
|
---|
202 | /* this parameter is for camera, not for reflector */
|
---|
203 | break;
|
---|
204 | case n_gappixels:
|
---|
205 | /* this parameter is for camera, not for reflector */
|
---|
206 | break;
|
---|
207 | case refl_file:
|
---|
208 | ReadReflectivity(value_ptr);
|
---|
209 | break;
|
---|
210 | case axisdev_file:
|
---|
211 | ReadAxisDev(value_ptr);
|
---|
212 | break;
|
---|
213 | case define_mirrors:
|
---|
214 | if (ct_NMirrors) ReadMirrorTable(geofile);
|
---|
215 | else FatalError(MIRR_NSPEC_FTL);
|
---|
216 | switch_end = TRUE;
|
---|
217 | break;
|
---|
218 | default: switch_end = TRUE;
|
---|
219 | break; }}
|
---|
220 | while (!switch_end);
|
---|
221 | fclose(geofile);
|
---|
222 |
|
---|
223 | if (strlen(reflectivity_filename) == 0)
|
---|
224 | strcpy(reflectivity_filename, REFLECTIVITY_FILE);
|
---|
225 | if (Reflectivity[0] == NULL) ReadReflectivity(reflectivity_filename);
|
---|
226 |
|
---|
227 | if (strlen(axisdev_filename) == 0)
|
---|
228 | strcpy(axisdev_filename, AXISDEVIATION_FILE);
|
---|
229 | if (AxisDeviation[0]== NULL) ReadAxisDev(axisdev_filename);
|
---|
230 |
|
---|
231 |
|
---|
232 | ReadFocals();
|
---|
233 | } /* end of GeometrySwitch */
|
---|
234 |
|
---|