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