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

Last change on this file since 859 was 725, checked in by harald, 24 years ago
Ciro and Denis changed the reflector to read in the changed MMCS output (single file version). They made a big change of all the code. That is the reason why I put here a new reflector directory in the repository. This is the future development point for reflector. Until the next drastic change. WARNING: Reflector here is only proved on OSF!!!
File size: 6.9 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <math.h>
4#include "diag.h"
5#include "geometry.h"
6#include "init.h"
7
8extern char line[]; /* parsing buf. (init) */
9
10float ct_Focal_mean; /* focal dist. (mean) (cm) */
11float ct_Focal_std; /* focal dist. (std) (cm) */
12float ct_PSpread_mean; /* pt. spread fn. (mean) (cm) */
13float ct_PSpread_std; /* pt. spread fn. (std) (cm) */
14float ct_Adjustment_std; /* adjustment dev. (std) (cm) */
15float ct_BlackSpot_rad; /* black spot radius (cm) */
16float ct_RMirror; /* rad. of single mirror (cm) */
17int ct_NMirrors=0; /* number of mirrors */
18float ct_CameraWidth; /* camera width (cm) */
19int ct_NPixels; /* number of pixels */
20float ct_PixelWidth; /* pixel width (cm) */
21
22mirror *ct_data=NULL; /* ptr to mirror data */
23FILE *ct_BinaryData=NULL; /* binary data for mirrors */
24char ct_BinaryName[128]; /* binary data filename */
25
26int nReflectivity=0; /* elements in refl. table */
27float *Reflectivity[2]; /* reflectivity table */
28float *AxisDeviation[2]; /* axis deviation table */
29float *ct_Focal; /* focal length table */
30
31/* Prototypes */
32static void ReadMirrorTable(FILE *geofile);
33static void ReadReflectivity(char *datname);
34static void ReadAxisDev(char *datname);
35static void ReadFocals(void);
36
37static void ReadMirrorTable(FILE *geofile)
38{ int i; /* Mirror index */
39
40 if ((ct_data=(mirror *)malloc(sizeof(mirror)*ct_NMirrors)) == NULL)
41 FatalError(MIRR_ALLOC_FTL, ct_NMirrors);
42 Log(MIRR_ALLOC_LOG, ct_NMirrors);
43 Log(MIRR_TABLE_LOG);
44 if (ct_BinaryData)
45 { Log(BINF_OPEN__LOG, ct_BinaryName);
46 fread(ct_data, sizeof(mirror), ct_NMirrors, ct_BinaryData);
47 fclose(ct_BinaryData); }
48 else
49 { /* read ASCII data */
50 Log(READ_ASCII_LOG);
51 for (i=0; i<ct_NMirrors; i++)
52 { if (12 != fscanf(geofile, "%d %f %f %f %f %f %f %f %f %f %f %f",
53 &ct_data[i].i, &ct_data[i].f,
54 &ct_data[i].sx, &ct_data[i].sy,
55 &ct_data[i].x, &ct_data[i].y, &ct_data[i].z,
56 &ct_data[i].theta, &ct_data[i].phi,
57 &ct_data[i].xn, &ct_data[i].yn, &ct_data[i].zn))
58 break;
59 Log("[%d]", ct_data[i].i); }
60 Log("%c", '\n');
61 if (i < ct_NMirrors)
62 FatalError(MIRR_FEW___FTL, i);
63 /* Data Ok: save binary data for next time */
64 if ((ct_BinaryData=fopen(ct_BinaryName, "w"))==NULL)
65 Log(BINF_ERROR_LOG, ct_BinaryName);
66 else
67 { Log(BINF_WRITE_LOG, ct_BinaryName);
68 fwrite(ct_data, sizeof(mirror), ct_NMirrors, ct_BinaryData);
69 fclose(ct_BinaryData); }
70 } /* end of if: reading ASCII data */
71
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.