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 |
double normsc(); |
30 |
/* sun calculation constants */ |
31 |
extern double s_latitude; |
32 |
extern double s_longitude; |
33 |
extern double s_meridian; |
34 |
/* required values */ |
35 |
int month, day; /* date */ |
36 |
double hour; /* time */ |
37 |
int tsolar; /* 0=standard, 1=solar */ |
38 |
double altitude, azimuth; /* or solar angles */ |
39 |
/* default values */ |
40 |
int cloudy = 0; /* 1=standard, 2=uniform */ |
41 |
int dosun = 1; |
42 |
double zenithbr = -1.0; |
43 |
double turbidity = 2.75; |
44 |
double gprefl = 0.2; |
45 |
/* computed values */ |
46 |
double sundir[3]; |
47 |
double groundbr; |
48 |
double F2; |
49 |
double solarbr = -1.0; |
50 |
|
51 |
char *progname; |
52 |
char errmsg[128]; |
53 |
|
54 |
|
55 |
main(argc, argv) |
56 |
int argc; |
57 |
char *argv[]; |
58 |
{ |
59 |
int i; |
60 |
|
61 |
progname = argv[0]; |
62 |
if (argc == 2 && !strcmp(argv[1], "-defaults")) { |
63 |
printdefaults(); |
64 |
exit(0); |
65 |
} |
66 |
if (argc < 4) |
67 |
userror("arg count"); |
68 |
if (!strcmp(argv[1], "-ang")) { |
69 |
altitude = atof(argv[2]) * (PI/180); |
70 |
azimuth = atof(argv[3]) * (PI/180); |
71 |
month = 0; |
72 |
} else { |
73 |
month = atoi(argv[1]); |
74 |
if (month < 1 || month > 12) |
75 |
userror("bad month"); |
76 |
day = atoi(argv[2]); |
77 |
if (day < 1 || day > 31) |
78 |
userror("bad day"); |
79 |
hour = atof(argv[3]); |
80 |
if (hour < 0 || hour >= 24) |
81 |
userror("bad hour"); |
82 |
tsolar = argv[3][0] == '+'; |
83 |
} |
84 |
for (i = 4; i < argc; i++) |
85 |
if (argv[i][0] == '-' || argv[i][0] == '+') |
86 |
switch (argv[i][1]) { |
87 |
case 's': |
88 |
cloudy = 0; |
89 |
dosun = argv[i][0] == '+'; |
90 |
break; |
91 |
case 'r': |
92 |
solarbr = atof(argv[++i]); |
93 |
break; |
94 |
case 'c': |
95 |
cloudy = argv[i][0] == '+' ? 2 : 1; |
96 |
dosun = 0; |
97 |
break; |
98 |
case 't': |
99 |
turbidity = atof(argv[++i]); |
100 |
break; |
101 |
case 'b': |
102 |
zenithbr = atof(argv[++i]); |
103 |
break; |
104 |
case 'g': |
105 |
gprefl = atof(argv[++i]); |
106 |
break; |
107 |
case 'a': |
108 |
s_latitude = atof(argv[++i]) * (PI/180); |
109 |
break; |
110 |
case 'o': |
111 |
s_longitude = atof(argv[++i]) * (PI/180); |
112 |
break; |
113 |
case 'm': |
114 |
s_meridian = atof(argv[++i]) * (PI/180); |
115 |
break; |
116 |
default: |
117 |
sprintf(errmsg, "unknown option: %s", argv[i]); |
118 |
userror(errmsg); |
119 |
} |
120 |
else |
121 |
userror("bad option"); |
122 |
|
123 |
if (fabs(s_meridian-s_longitude) > 30*PI/180) |
124 |
fprintf(stderr, |
125 |
"%s: warning: %.1f hours btwn. standard meridian and longitude\n", |
126 |
progname, (s_longitude-s_meridian)*12/PI); |
127 |
|
128 |
printhead(argc, argv); |
129 |
|
130 |
computesky(); |
131 |
printsky(); |
132 |
} |
133 |
|
134 |
|
135 |
computesky() /* compute sky parameters */ |
136 |
{ |
137 |
/* compute solar direction */ |
138 |
if (month) { /* from date and time */ |
139 |
int jd; |
140 |
double sd, st; |
141 |
|
142 |
jd = jdate(month, day); /* Julian date */ |
143 |
sd = sdec(jd); /* solar declination */ |
144 |
if (tsolar) /* solar time */ |
145 |
st = hour; |
146 |
else |
147 |
st = hour + stadj(jd); |
148 |
altitude = salt(sd, st); |
149 |
azimuth = sazi(sd, st); |
150 |
printf("# Solar altitude and azimuth: %f %f\n", |
151 |
180./PI*altitude, 180./PI*azimuth); |
152 |
} |
153 |
if (!cloudy && altitude > 87.*PI/180.) { |
154 |
fprintf(stderr, |
155 |
"%s: warning - sun too close to zenith, reducing altitude to 87 degrees\n", |
156 |
progname); |
157 |
printf( |
158 |
"# warning - sun too close to zenith, reducing altitude to 87 degrees\n"); |
159 |
altitude = 87.*PI/180.; |
160 |
} |
161 |
sundir[0] = -sin(azimuth)*cos(altitude); |
162 |
sundir[1] = -cos(azimuth)*cos(altitude); |
163 |
sundir[2] = sin(altitude); |
164 |
|
165 |
/* Compute zenith brightness */ |
166 |
if (zenithbr <= 0.0) |
167 |
if (cloudy) { |
168 |
zenithbr = 8.6*sundir[2] + .123; |
169 |
zenithbr *= 1000.0/WHTEFFICACY; |
170 |
} else { |
171 |
zenithbr = (1.376*turbidity-1.81)*tan(altitude)+0.38; |
172 |
zenithbr *= 1000.0/SKYEFFICACY; |
173 |
} |
174 |
if (zenithbr < 0.0) |
175 |
zenithbr = 0.0; |
176 |
/* Compute horizontal radiance */ |
177 |
if (cloudy) { |
178 |
if (cloudy == 2) |
179 |
groundbr = zenithbr; |
180 |
else |
181 |
groundbr = zenithbr*0.777778; |
182 |
printf("# Ground ambient level: %f\n", groundbr); |
183 |
} else { |
184 |
F2 = 0.274*(0.91 + 10.0*exp(-3.0*(PI/2.0-altitude)) + |
185 |
0.45*sundir[2]*sundir[2]); |
186 |
groundbr = zenithbr*normsc(altitude)/F2/PI; |
187 |
printf("# Ground ambient level: %f\n", groundbr); |
188 |
if (sundir[2] > 0.0 && solarbr != 0.0) { |
189 |
if (solarbr < 0.0) |
190 |
solarbr = 1.5e9/SUNEFFICACY * |
191 |
(1.147 - .147/(sundir[2]>.16?sundir[2]:.16)); |
192 |
groundbr += solarbr*6e-5*sundir[2]/PI; |
193 |
} else |
194 |
dosun = 0; |
195 |
} |
196 |
groundbr *= gprefl; |
197 |
} |
198 |
|
199 |
|
200 |
printsky() /* print out sky */ |
201 |
{ |
202 |
if (dosun) { |
203 |
printf("\nvoid light solar\n"); |
204 |
printf("0\n0\n"); |
205 |
printf("3 %.2e %.2e %.2e\n", solarbr, solarbr, solarbr); |
206 |
printf("\nsolar source sun\n"); |
207 |
printf("0\n0\n"); |
208 |
printf("4 %f %f %f 0.5\n", sundir[0], sundir[1], sundir[2]); |
209 |
} |
210 |
|
211 |
printf("\nvoid brightfunc skyfunc\n"); |
212 |
printf("2 skybright skybright.cal\n"); |
213 |
printf("0\n"); |
214 |
if (cloudy) |
215 |
printf("3 %d %.2e %.2e\n", cloudy, zenithbr, groundbr); |
216 |
else |
217 |
printf("7 -1 %.2e %.2e %.2e %f %f %f\n", zenithbr, groundbr, |
218 |
F2, sundir[0], sundir[1], sundir[2]); |
219 |
} |
220 |
|
221 |
|
222 |
printdefaults() /* print default values */ |
223 |
{ |
224 |
if (cloudy == 1) |
225 |
printf("-c\t\t\t\t# Cloudy sky\n"); |
226 |
else if (cloudy == 2) |
227 |
printf("+c\t\t\t\t# Uniform cloudy sky\n"); |
228 |
else if (dosun) |
229 |
printf("+s\t\t\t\t# Sunny sky with sun\n"); |
230 |
else |
231 |
printf("-s\t\t\t\t# Sunny sky without sun\n"); |
232 |
printf("-g %f\t\t\t# Ground plane reflectance\n", gprefl); |
233 |
if (zenithbr > 0.0) |
234 |
printf("-b %f\t\t\t# Zenith radiance (watts/ster/m2\n", zenithbr); |
235 |
else |
236 |
printf("-t %f\t\t\t# Atmospheric turbidity\n", turbidity); |
237 |
printf("-a %f\t\t\t# Site latitude (degrees)\n", s_latitude*(180/PI)); |
238 |
printf("-o %f\t\t\t# Site longitude (degrees)\n", s_longitude*(180/PI)); |
239 |
printf("-m %f\t\t\t# Standard meridian (degrees)\n", s_meridian*(180/PI)); |
240 |
} |
241 |
|
242 |
|
243 |
userror(msg) /* print usage error and quit */ |
244 |
char *msg; |
245 |
{ |
246 |
if (msg != NULL) |
247 |
fprintf(stderr, "%s: Use error - %s\n", progname, msg); |
248 |
fprintf(stderr, "Usage: %s month day hour [options]\n", progname); |
249 |
fprintf(stderr, " Or: %s -ang altitude azimuth [options]\n", progname); |
250 |
fprintf(stderr, " Or: %s -defaults\n", progname); |
251 |
exit(1); |
252 |
} |
253 |
|
254 |
|
255 |
double |
256 |
normsc(theta) /* compute normalization factor (E0*F2/L0) */ |
257 |
double theta; |
258 |
{ |
259 |
static double nf[5] = {2.766521, 0.547665, |
260 |
-0.369832, 0.009237, 0.059229}; |
261 |
double x, nsc; |
262 |
register int i; |
263 |
/* polynomial approximation */ |
264 |
x = (theta - PI/4.0)/(PI/4.0); |
265 |
nsc = nf[4]; |
266 |
for (i = 3; i >= 0; i--) |
267 |
nsc = nsc*x + nf[i]; |
268 |
|
269 |
return(nsc); |
270 |
} |
271 |
|
272 |
|
273 |
printhead(ac, av) /* print command header */ |
274 |
register int ac; |
275 |
register char **av; |
276 |
{ |
277 |
putchar('#'); |
278 |
while (ac--) { |
279 |
putchar(' '); |
280 |
fputs(*av++, stdout); |
281 |
} |
282 |
putchar('\n'); |
283 |
} |