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