1 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* gensky.c - program to generate sky functions. |
9 |
* Our zenith is along the Z-axis, the X-axis |
10 |
* points east, and the Y-axis points north. |
11 |
* Radiance is in watts/steradian/sq. meter. |
12 |
* |
13 |
* 3/26/86 |
14 |
*/ |
15 |
|
16 |
#include <stdio.h> |
17 |
|
18 |
#include <math.h> |
19 |
|
20 |
#include "color.h" |
21 |
|
22 |
extern char *strcpy(), *strcat(), *malloc(); |
23 |
extern double stadj(), sdec(), sazi(), salt(); |
24 |
|
25 |
#define PI 3.141592654 |
26 |
|
27 |
#define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]) |
28 |
|
29 |
#define S_CLEAR 1 |
30 |
#define S_OVER 2 |
31 |
#define S_UNIF 3 |
32 |
#define S_INTER 4 |
33 |
|
34 |
#define overcast (skytype==S_OVER|skytype==S_UNIF) |
35 |
|
36 |
double normsc(); |
37 |
/* sun calculation constants */ |
38 |
extern double s_latitude; |
39 |
extern double s_longitude; |
40 |
extern double s_meridian; |
41 |
/* required values */ |
42 |
int month, day; /* date */ |
43 |
double hour; /* time */ |
44 |
int tsolar; /* 0=standard, 1=solar */ |
45 |
double altitude, azimuth; /* or solar angles */ |
46 |
/* default values */ |
47 |
int skytype = S_CLEAR; /* sky type */ |
48 |
int dosun = 1; |
49 |
double zenithbr = 0.0; |
50 |
int u_zenith = 0; /* -1=irradiance, 1=radiance */ |
51 |
double turbidity = 2.75; |
52 |
double gprefl = 0.2; |
53 |
/* computed values */ |
54 |
double sundir[3]; |
55 |
double groundbr; |
56 |
double F2; |
57 |
double solarbr = 0.0; |
58 |
int u_solar = 0; /* -1=irradiance, 1=radiance */ |
59 |
|
60 |
char *progname; |
61 |
char errmsg[128]; |
62 |
|
63 |
|
64 |
main(argc, argv) |
65 |
int argc; |
66 |
char *argv[]; |
67 |
{ |
68 |
int i; |
69 |
|
70 |
progname = argv[0]; |
71 |
if (argc == 2 && !strcmp(argv[1], "-defaults")) { |
72 |
printdefaults(); |
73 |
exit(0); |
74 |
} |
75 |
if (argc < 4) |
76 |
userror("arg count"); |
77 |
if (!strcmp(argv[1], "-ang")) { |
78 |
altitude = atof(argv[2]) * (PI/180); |
79 |
azimuth = atof(argv[3]) * (PI/180); |
80 |
month = 0; |
81 |
} else { |
82 |
month = atoi(argv[1]); |
83 |
if (month < 1 || month > 12) |
84 |
userror("bad month"); |
85 |
day = atoi(argv[2]); |
86 |
if (day < 1 || day > 31) |
87 |
userror("bad day"); |
88 |
hour = atof(argv[3]); |
89 |
if (hour < 0 || hour >= 24) |
90 |
userror("bad hour"); |
91 |
tsolar = argv[3][0] == '+'; |
92 |
} |
93 |
for (i = 4; i < argc; i++) |
94 |
if (argv[i][0] == '-' || argv[i][0] == '+') |
95 |
switch (argv[i][1]) { |
96 |
case 's': |
97 |
skytype = S_CLEAR; |
98 |
dosun = argv[i][0] == '+'; |
99 |
break; |
100 |
case 'r': |
101 |
case 'R': |
102 |
u_solar = argv[i][1]=='R' ? -1 : 1; |
103 |
solarbr = atof(argv[++i]); |
104 |
break; |
105 |
case 'c': |
106 |
skytype = S_OVER; |
107 |
dosun = 0; |
108 |
break; |
109 |
case 'u': |
110 |
skytype = S_UNIF; |
111 |
dosun = 0; |
112 |
break; |
113 |
case 'i': |
114 |
skytype = S_INTER; |
115 |
dosun = argv[i][0] == '+'; |
116 |
break; |
117 |
case 't': |
118 |
turbidity = atof(argv[++i]); |
119 |
break; |
120 |
case 'b': |
121 |
case 'B': |
122 |
u_zenith = argv[i][1]=='B' ? -1 : 1; |
123 |
zenithbr = atof(argv[++i]); |
124 |
break; |
125 |
case 'g': |
126 |
gprefl = atof(argv[++i]); |
127 |
break; |
128 |
case 'a': |
129 |
s_latitude = atof(argv[++i]) * (PI/180); |
130 |
break; |
131 |
case 'o': |
132 |
s_longitude = atof(argv[++i]) * (PI/180); |
133 |
break; |
134 |
case 'm': |
135 |
s_meridian = atof(argv[++i]) * (PI/180); |
136 |
break; |
137 |
default: |
138 |
sprintf(errmsg, "unknown option: %s", argv[i]); |
139 |
userror(errmsg); |
140 |
} |
141 |
else |
142 |
userror("bad option"); |
143 |
|
144 |
if (fabs(s_meridian-s_longitude) > 30*PI/180) |
145 |
fprintf(stderr, |
146 |
"%s: warning: %.1f hours btwn. standard meridian and longitude\n", |
147 |
progname, (s_longitude-s_meridian)*12/PI); |
148 |
|
149 |
printhead(argc, argv); |
150 |
|
151 |
computesky(); |
152 |
printsky(); |
153 |
} |
154 |
|
155 |
|
156 |
computesky() /* compute sky parameters */ |
157 |
{ |
158 |
double normfactor; |
159 |
/* compute solar direction */ |
160 |
if (month) { /* from date and time */ |
161 |
int jd; |
162 |
double sd, st; |
163 |
|
164 |
jd = jdate(month, day); /* Julian date */ |
165 |
sd = sdec(jd); /* solar declination */ |
166 |
if (tsolar) /* solar time */ |
167 |
st = hour; |
168 |
else |
169 |
st = hour + stadj(jd); |
170 |
altitude = salt(sd, st); |
171 |
azimuth = sazi(sd, st); |
172 |
printf("# Solar altitude and azimuth: %.1f %.1f\n", |
173 |
180./PI*altitude, 180./PI*azimuth); |
174 |
} |
175 |
if (!overcast && altitude > 87.*PI/180.) { |
176 |
fprintf(stderr, |
177 |
"%s: warning - sun too close to zenith, reducing altitude to 87 degrees\n", |
178 |
progname); |
179 |
printf( |
180 |
"# warning - sun too close to zenith, reducing altitude to 87 degrees\n"); |
181 |
altitude = 87.*PI/180.; |
182 |
} |
183 |
sundir[0] = -sin(azimuth)*cos(altitude); |
184 |
sundir[1] = -cos(azimuth)*cos(altitude); |
185 |
sundir[2] = sin(altitude); |
186 |
|
187 |
/* Compute normalization factor */ |
188 |
switch (skytype) { |
189 |
case S_UNIF: |
190 |
normfactor = 1.0; |
191 |
break; |
192 |
case S_OVER: |
193 |
normfactor = 0.777778; |
194 |
break; |
195 |
case S_CLEAR: |
196 |
F2 = 0.274*(0.91 + 10.0*exp(-3.0*(PI/2.0-altitude)) + |
197 |
0.45*sundir[2]*sundir[2]); |
198 |
normfactor = normsc()/F2/PI; |
199 |
break; |
200 |
case S_INTER: |
201 |
F2 = (2.739 + .9891*sin(.3119+2.6*altitude)) * |
202 |
exp(-(PI/2.0-altitude)*(.4441+1.48*altitude)); |
203 |
normfactor = normsc()/F2/PI; |
204 |
break; |
205 |
} |
206 |
/* Compute zenith brightness */ |
207 |
if (u_zenith == -1) |
208 |
zenithbr /= normfactor*PI; |
209 |
else if (u_zenith == 0) { |
210 |
if (overcast) |
211 |
zenithbr = 8.6*sundir[2] + .123; |
212 |
else |
213 |
zenithbr = (1.376*turbidity-1.81)*tan(altitude)+0.38; |
214 |
if (skytype == S_INTER) |
215 |
zenithbr = (zenithbr + 8.6*sundir[2] + .123)/2.0; |
216 |
if (zenithbr < 0.0) |
217 |
zenithbr = 0.0; |
218 |
else |
219 |
zenithbr *= 1000.0/SKYEFFICACY; |
220 |
} |
221 |
/* Compute horizontal radiance */ |
222 |
groundbr = zenithbr*normfactor; |
223 |
printf("# Ground ambient level: %.1f\n", groundbr); |
224 |
if (sundir[2] > 0.0 && (!u_solar || solarbr > 0.0)) { |
225 |
if (u_solar == -1) |
226 |
solarbr /= 6e-5*sundir[2]; |
227 |
else if (u_solar == 0) { |
228 |
solarbr = 1.5e9/SUNEFFICACY * |
229 |
(1.147 - .147/(sundir[2]>.16?sundir[2]:.16)); |
230 |
if (skytype == S_INTER) |
231 |
solarbr *= 0.15; /* fudge factor! */ |
232 |
} |
233 |
groundbr += 6e-5/PI*solarbr*sundir[2]; |
234 |
} else |
235 |
dosun = 0; |
236 |
groundbr *= gprefl; |
237 |
} |
238 |
|
239 |
|
240 |
printsky() /* print out sky */ |
241 |
{ |
242 |
if (dosun) { |
243 |
printf("\nvoid light solar\n"); |
244 |
printf("0\n0\n"); |
245 |
printf("3 %.2e %.2e %.2e\n", solarbr, solarbr, solarbr); |
246 |
printf("\nsolar source sun\n"); |
247 |
printf("0\n0\n"); |
248 |
printf("4 %f %f %f 0.5\n", sundir[0], sundir[1], sundir[2]); |
249 |
} |
250 |
|
251 |
printf("\nvoid brightfunc skyfunc\n"); |
252 |
printf("2 skybr skybright.cal\n"); |
253 |
printf("0\n"); |
254 |
if (overcast) |
255 |
printf("3 %d %.2e %.2e\n", skytype, zenithbr, groundbr); |
256 |
else |
257 |
printf("7 %d %.2e %.2e %.2e %f %f %f\n", |
258 |
skytype, zenithbr, groundbr, F2, |
259 |
sundir[0], sundir[1], sundir[2]); |
260 |
} |
261 |
|
262 |
|
263 |
printdefaults() /* print default values */ |
264 |
{ |
265 |
switch (skytype) { |
266 |
case S_OVER: |
267 |
printf("-c\t\t\t\t# Cloudy sky\n"); |
268 |
break; |
269 |
case S_UNIF: |
270 |
printf("-u\t\t\t\t# Uniform cloudy sky\n"); |
271 |
break; |
272 |
case S_INTER: |
273 |
if (dosun) |
274 |
printf("+i\t\t\t\t# Intermediate sky with sun\n"); |
275 |
else |
276 |
printf("-i\t\t\t\t# Intermediate sky without sun\n"); |
277 |
break; |
278 |
case S_CLEAR: |
279 |
if (dosun) |
280 |
printf("+s\t\t\t\t# Sunny sky with sun\n"); |
281 |
else |
282 |
printf("-s\t\t\t\t# Sunny sky without sun\n"); |
283 |
break; |
284 |
} |
285 |
printf("-g %f\t\t\t# Ground plane reflectance\n", gprefl); |
286 |
if (zenithbr > 0.0) |
287 |
printf("-b %f\t\t\t# Zenith radiance (watts/ster/m2\n", zenithbr); |
288 |
else |
289 |
printf("-t %f\t\t\t# Atmospheric turbidity\n", turbidity); |
290 |
printf("-a %f\t\t\t# Site latitude (degrees)\n", s_latitude*(180/PI)); |
291 |
printf("-o %f\t\t\t# Site longitude (degrees)\n", s_longitude*(180/PI)); |
292 |
printf("-m %f\t\t\t# Standard meridian (degrees)\n", s_meridian*(180/PI)); |
293 |
} |
294 |
|
295 |
|
296 |
userror(msg) /* print usage error and quit */ |
297 |
char *msg; |
298 |
{ |
299 |
if (msg != NULL) |
300 |
fprintf(stderr, "%s: Use error - %s\n", progname, msg); |
301 |
fprintf(stderr, "Usage: %s month day hour [options]\n", progname); |
302 |
fprintf(stderr, " Or: %s -ang altitude azimuth [options]\n", progname); |
303 |
fprintf(stderr, " Or: %s -defaults\n", progname); |
304 |
exit(1); |
305 |
} |
306 |
|
307 |
|
308 |
double |
309 |
normsc() /* compute normalization factor (E0*F2/L0) */ |
310 |
{ |
311 |
static double nfc[2][5] = { |
312 |
/* clear sky approx. */ |
313 |
{2.766521, 0.547665, -0.369832, 0.009237, 0.059229}, |
314 |
/* intermediate sky approx. */ |
315 |
{3.5556, -2.7152, -1.3081, 1.0660, 0.60227}, |
316 |
}; |
317 |
register double *nf; |
318 |
double x, nsc; |
319 |
register int i; |
320 |
/* polynomial approximation */ |
321 |
nf = nfc[skytype==S_INTER]; |
322 |
x = (altitude - PI/4.0)/(PI/4.0); |
323 |
nsc = nf[i=4]; |
324 |
while (i--) |
325 |
nsc = nsc*x + nf[i]; |
326 |
|
327 |
return(nsc); |
328 |
} |
329 |
|
330 |
|
331 |
printhead(ac, av) /* print command header */ |
332 |
register int ac; |
333 |
register char **av; |
334 |
{ |
335 |
putchar('#'); |
336 |
while (ac--) { |
337 |
putchar(' '); |
338 |
fputs(*av++, stdout); |
339 |
} |
340 |
putchar('\n'); |
341 |
} |