source: trunk/MagicSoft/Simulation/Detector/ReflectorII/geometry.c@ 1000

Last change on this file since 1000 was 923, checked in by blanch, 23 years ago
The kibrary stdlib has been included to use 'atof'
File size: 7.1 KB
Line 
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
9extern char line[]; /* parsing buf. (init) */
10
11float ct_Focal_mean; /* focal dist. (mean) (cm) */
12float ct_Focal_std; /* focal dist. (std) (cm) */
13float ct_PSpread_mean; /* pt. spread fn. (mean) (cm) */
14float ct_PSpread_std; /* pt. spread fn. (std) (cm) */
15float ct_Adjustment_std; /* adjustment dev. (std) (cm) */
16float ct_BlackSpot_rad; /* black spot radius (cm) */
17float ct_RMirror; /* rad. of single mirror (cm) */
18int ct_NMirrors=0; /* number of mirrors */
19float ct_CameraWidth; /* camera width (cm) */
20int ct_NPixels; /* number of pixels */
21float ct_PixelWidth; /* pixel width (cm) */
22
23mirror *ct_data=NULL; /* ptr to mirror data */
24FILE *ct_BinaryData=NULL; /* binary data for mirrors */
25char ct_BinaryName[128]; /* binary data filename */
26
27int nReflectivity=0; /* elements in refl. table */
28float *Reflectivity[2]; /* reflectivity table */
29float *AxisDeviation[2]; /* axis deviation table */
30float *ct_Focal; /* focal length table */
31
32/* Prototypes */
33static void ReadMirrorTable(FILE *geofile);
34static void ReadReflectivity(char *datname);
35static void ReadAxisDev(char *datname);
36static void ReadFocals(void);
37
38static void ReadMirrorTable(FILE *geofile)
39{ int i; /* Mirror index */
40
41 if ((ct_data=(mirror *)malloc(sizeof(mirror)*ct_NMirrors)) == NULL)
42 FatalError(MIRR_ALLOC_FTL, ct_NMirrors);
43 Log(MIRR_ALLOC_LOG, ct_NMirrors);
44 Log(MIRR_TABLE_LOG);
45 if (ct_BinaryData)
46 { Log(BINF_OPEN__LOG, ct_BinaryName);
47 fread(ct_data, sizeof(mirror), ct_NMirrors, ct_BinaryData);
48 fclose(ct_BinaryData); }
49 else
50 { /* read ASCII data */
51 Log(READ_ASCII_LOG);
52 for (i=0; i<ct_NMirrors; i++)
53 { if (12 != fscanf(geofile, "%d %f %f %f %f %f %f %f %f %f %f %f",
54 &ct_data[i].i, &ct_data[i].f,
55 &ct_data[i].sx, &ct_data[i].sy,
56 &ct_data[i].x, &ct_data[i].y, &ct_data[i].z,
57 &ct_data[i].theta, &ct_data[i].phi,
58 &ct_data[i].xn, &ct_data[i].yn, &ct_data[i].zn))
59 break;
60 Log("[%d]", ct_data[i].i); }
61 Log("%c", '\n');
62 if (i < ct_NMirrors)
63 FatalError(MIRR_FEW___FTL, i);
64 /* Data Ok: save binary data for next time */
65 if ((ct_BinaryData=fopen(ct_BinaryName, "w"))==NULL)
66 Log(BINF_ERROR_LOG, ct_BinaryName);
67 else
68 { Log(BINF_WRITE_LOG, ct_BinaryName);
69 fwrite(ct_data, sizeof(mirror), ct_NMirrors, ct_BinaryData);
70 fclose(ct_BinaryData); }
71 } /* end of if: reading ASCII data */
72} /* end of ReadMirrorTable */
73
74static void ReadReflectivity(char *datname)
75{ FILE *datfile = fopen(datname, "r");
76 int current = 0;
77
78 if (datfile == NULL)
79 FatalError(RFLF_ERROR_FTL, datname);
80 while (fgets(line, LINE_MAX_LENGTH, datfile))
81 { if (line[0] == '#') continue;
82 if (nReflectivity == 0)
83 { nReflectivity = atoi(line);
84 if (nReflectivity)
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 else if (2 == sscanf(line, "%f %f",
91 &Reflectivity[0][current], &Reflectivity[1][current]));
92 { current++;
93 if (current >= nReflectivity) break; }}
94 fclose(datfile);
95
96 nReflectivity = current;
97} /* end of ReadReflectivity */
98
99static void ReadAxisDev(char *datname)
100{ FILE *datfile = fopen(datname, "r");
101 int current = 0;
102
103 if (datfile == NULL)
104 FatalError(AXIS_ERROR_FTL, datname);
105 if ((AxisDeviation[0]=
106 (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL
107 || (AxisDeviation[1]=
108 (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL)
109 FatalError(AXIS_ALLOC_FTL, ct_NMirrors);
110 while (fgets(line, LINE_MAX_LENGTH, datfile))
111 { if (line[0] == '#') continue;
112 if (2==sscanf(line, "%f %f",
113 &AxisDeviation[0][current], &AxisDeviation[1][current]));
114 { current++;
115 if (current >= ct_NMirrors) break; }}
116 fclose(datfile);
117
118 if (current != ct_NMirrors)
119 FatalError(AXIS_FEW___FTL, current, ct_NMirrors);
120} /* end of ReadAxisDev */
121
122static void ReadFocals(void)
123{ int loop;
124
125 if ((ct_Focal = (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL)
126 FatalError(FOCL_FEW___FTL, ct_NMirrors);
127
128 for (loop=0; loop<ct_NMirrors; loop++)
129 ct_Focal[loop] = ct_data[loop].f;
130} /* end of ReadFocals */
131
132void GeometrySwitch(FILE *geofile)
133{ char *value_ptr = NULL; /* ptr at parm value */
134 int switch_end = FALSE; /* bool to exit loop */
135 extern char whites[]; /* white chars (init) */
136 extern int ParseLine(FILE *parfile, /* FILE with parms */
137 const char *token_list[], /* array w/tokens */
138 int tokens, /* nr of tokens */
139 char **value_ptr); /* ptr->parm val. */
140
141 /* Initialise arrays */
142 Reflectivity[0] = AxisDeviation[0] = NULL;
143
144 do
145 { switch(ParseLine(geofile, ctparms, ARRAY_SZ(ctparms), &value_ptr))
146 { case type:
147 if (1 != atoi(value_ptr))
148 FatalError(TYPE_ERROR_FTL);
149 break;
150 case focal_distance:
151 Log(LOG__FLOAT_LOG, "focal distance (average)",
152 ct_Focal_mean = (float) atof(value_ptr));
153 break;
154 case focal_std:
155 Log(LOG__FLOAT_LOG, "focal distance (std. dev.)",
156 ct_Focal_std = (float) atof(value_ptr));
157 break;
158 case point_spread:
159 Log(LOG__FLOAT_LOG, "point spread fn. (average)",
160 ct_PSpread_mean = (float) atof(value_ptr));
161 break;
162 case point_std:
163 Log(LOG__FLOAT_LOG, "point spread fn. (std. dev.)",
164 ct_PSpread_std = (float) atof(value_ptr));
165 break;
166 case adjustment_dev:
167 Log(LOG__FLOAT_LOG, "adjustment dev. (std. dev.)",
168 ct_Adjustment_std = (float) atof(value_ptr));
169 break;
170 case black_spot:
171 Log(LOG__FLOAT_LOG, "radius of black spot (cm)",
172 ct_BlackSpot_rad = (float) atof(value_ptr));
173 break;
174 case r_mirror:
175 Log(LOG__FLOAT_LOG, "single mirror radius (cm)",
176 ct_RMirror = (float) atof(value_ptr));
177 break;
178 case n_mirrors:
179 Log(LOG__INT___LOG, "number of mirrors",
180 ct_NMirrors = atoi(value_ptr));
181 break;
182 case camera_width:
183 Log(LOG__FLOAT_LOG, "camera width (cm)",
184 ct_CameraWidth = (float) atof(value_ptr));
185 break;
186 case n_pixels:
187 Log(LOG__INT___LOG, "number of pixels",
188 ct_NPixels = atoi(value_ptr));
189 break;
190 case pixel_width:
191 Log(LOG__FLOAT_LOG, "pixel width (cm)",
192 ct_PixelWidth = (float) atof(value_ptr));
193 break;
194 case refl_file:
195 ReadReflectivity(value_ptr);
196 break;
197 case axisdev_file:
198 ReadAxisDev(value_ptr);
199 break;
200 case define_mirrors:
201 if (ct_NMirrors) ReadMirrorTable(geofile);
202 else FatalError(MIRR_NSPEC_FTL);
203 switch_end = TRUE;
204 break;
205 default: switch_end = TRUE;
206 break; }}
207 while (!switch_end);
208 fclose(geofile);
209
210 if (Reflectivity[0] == NULL) ReadReflectivity(REFLECTIVITY_FILE);
211 if (AxisDeviation[0]== NULL) ReadAxisDev(AXISDEVIATION_FILE);
212 ReadFocals();
213} /* end of GeometrySwitch */
Note: See TracBrowser for help on using the repository browser.