ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gensky.c
Revision: 2.16
Committed: Tue Jun 25 20:48:17 1996 UTC (27 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.15: +16 -4 lines
Log Message:
added ability to specify time as HH:MM

File Contents

# Content
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 cvthour(argv[3]);
89 }
90 for (i = 4; i < argc; i++)
91 if (argv[i][0] == '-' || argv[i][0] == '+')
92 switch (argv[i][1]) {
93 case 's':
94 skytype = S_CLEAR;
95 dosun = argv[i][0] == '+';
96 break;
97 case 'r':
98 case 'R':
99 u_solar = argv[i][1]=='R' ? -1 : 1;
100 solarbr = atof(argv[++i]);
101 break;
102 case 'c':
103 skytype = S_OVER;
104 break;
105 case 'u':
106 skytype = S_UNIF;
107 break;
108 case 'i':
109 skytype = S_INTER;
110 dosun = argv[i][0] == '+';
111 break;
112 case 't':
113 turbidity = atof(argv[++i]);
114 break;
115 case 'b':
116 case 'B':
117 u_zenith = argv[i][1]=='B' ? -1 : 1;
118 zenithbr = atof(argv[++i]);
119 break;
120 case 'g':
121 gprefl = atof(argv[++i]);
122 break;
123 case 'a':
124 s_latitude = atof(argv[++i]) * (PI/180);
125 break;
126 case 'o':
127 s_longitude = atof(argv[++i]) * (PI/180);
128 break;
129 case 'm':
130 s_meridian = atof(argv[++i]) * (PI/180);
131 break;
132 default:
133 sprintf(errmsg, "unknown option: %s", argv[i]);
134 userror(errmsg);
135 }
136 else
137 userror("bad option");
138
139 if (fabs(s_meridian-s_longitude) > 30*PI/180)
140 fprintf(stderr,
141 "%s: warning: %.1f hours btwn. standard meridian and longitude\n",
142 progname, (s_longitude-s_meridian)*12/PI);
143
144 printhead(argc, argv);
145
146 computesky();
147 printsky();
148
149 exit(0);
150 }
151
152
153 computesky() /* compute sky parameters */
154 {
155 double normfactor;
156 /* compute solar direction */
157 if (month) { /* from date and time */
158 int jd;
159 double sd, st;
160
161 jd = jdate(month, day); /* Julian date */
162 sd = sdec(jd); /* solar declination */
163 if (tsolar) /* solar time */
164 st = hour;
165 else
166 st = hour + stadj(jd);
167 altitude = salt(sd, st);
168 azimuth = sazi(sd, st);
169 printf("# Solar altitude and azimuth: %.1f %.1f\n",
170 180./PI*altitude, 180./PI*azimuth);
171 }
172 if (!overcast && altitude > 87.*PI/180.) {
173 fprintf(stderr,
174 "%s: warning - sun too close to zenith, reducing altitude to 87 degrees\n",
175 progname);
176 printf(
177 "# warning - sun too close to zenith, reducing altitude to 87 degrees\n");
178 altitude = 87.*PI/180.;
179 }
180 sundir[0] = -sin(azimuth)*cos(altitude);
181 sundir[1] = -cos(azimuth)*cos(altitude);
182 sundir[2] = sin(altitude);
183
184 /* Compute normalization factor */
185 switch (skytype) {
186 case S_UNIF:
187 normfactor = 1.0;
188 break;
189 case S_OVER:
190 normfactor = 0.777778;
191 break;
192 case S_CLEAR:
193 F2 = 0.274*(0.91 + 10.0*exp(-3.0*(PI/2.0-altitude)) +
194 0.45*sundir[2]*sundir[2]);
195 normfactor = normsc()/F2/PI;
196 break;
197 case S_INTER:
198 F2 = (2.739 + .9891*sin(.3119+2.6*altitude)) *
199 exp(-(PI/2.0-altitude)*(.4441+1.48*altitude));
200 normfactor = normsc()/F2/PI;
201 break;
202 }
203 /* Compute zenith brightness */
204 if (u_zenith == -1)
205 zenithbr /= normfactor*PI;
206 else if (u_zenith == 0) {
207 if (overcast)
208 zenithbr = 8.6*sundir[2] + .123;
209 else
210 zenithbr = (1.376*turbidity-1.81)*tan(altitude)+0.38;
211 if (skytype == S_INTER)
212 zenithbr = (zenithbr + 8.6*sundir[2] + .123)/2.0;
213 if (zenithbr < 0.0)
214 zenithbr = 0.0;
215 else
216 zenithbr *= 1000.0/SKYEFFICACY;
217 }
218 /* Compute horizontal radiance */
219 groundbr = zenithbr*normfactor;
220 printf("# Ground ambient level: %.1f\n", groundbr);
221 if (!overcast && sundir[2] > 0.0 && (!u_solar || solarbr > 0.0)) {
222 if (u_solar == -1)
223 solarbr /= 6e-5*sundir[2];
224 else if (u_solar == 0) {
225 solarbr = 1.5e9/SUNEFFICACY *
226 (1.147 - .147/(sundir[2]>.16?sundir[2]:.16));
227 if (skytype == S_INTER)
228 solarbr *= 0.15; /* fudge factor! */
229 }
230 groundbr += 6e-5/PI*solarbr*sundir[2];
231 } else
232 dosun = 0;
233 groundbr *= gprefl;
234 }
235
236
237 printsky() /* print out sky */
238 {
239 if (dosun) {
240 printf("\nvoid light solar\n");
241 printf("0\n0\n");
242 printf("3 %.2e %.2e %.2e\n", solarbr, solarbr, solarbr);
243 printf("\nsolar source sun\n");
244 printf("0\n0\n");
245 printf("4 %f %f %f 0.5\n", sundir[0], sundir[1], sundir[2]);
246 }
247
248 printf("\nvoid brightfunc skyfunc\n");
249 printf("2 skybr skybright.cal\n");
250 printf("0\n");
251 if (overcast)
252 printf("3 %d %.2e %.2e\n", skytype, zenithbr, groundbr);
253 else
254 printf("7 %d %.2e %.2e %.2e %f %f %f\n",
255 skytype, zenithbr, groundbr, F2,
256 sundir[0], sundir[1], sundir[2]);
257 }
258
259
260 printdefaults() /* print default values */
261 {
262 switch (skytype) {
263 case S_OVER:
264 printf("-c\t\t\t\t# Cloudy sky\n");
265 break;
266 case S_UNIF:
267 printf("-u\t\t\t\t# Uniform cloudy sky\n");
268 break;
269 case S_INTER:
270 if (dosun)
271 printf("+i\t\t\t\t# Intermediate sky with sun\n");
272 else
273 printf("-i\t\t\t\t# Intermediate sky without sun\n");
274 break;
275 case S_CLEAR:
276 if (dosun)
277 printf("+s\t\t\t\t# Sunny sky with sun\n");
278 else
279 printf("-s\t\t\t\t# Sunny sky without sun\n");
280 break;
281 }
282 printf("-g %f\t\t\t# Ground plane reflectance\n", gprefl);
283 if (zenithbr > 0.0)
284 printf("-b %f\t\t\t# Zenith radiance (watts/ster/m2\n", zenithbr);
285 else
286 printf("-t %f\t\t\t# Atmospheric turbidity\n", turbidity);
287 printf("-a %f\t\t\t# Site latitude (degrees)\n", s_latitude*(180/PI));
288 printf("-o %f\t\t\t# Site longitude (degrees)\n", s_longitude*(180/PI));
289 printf("-m %f\t\t\t# Standard meridian (degrees)\n", s_meridian*(180/PI));
290 }
291
292
293 userror(msg) /* print usage error and quit */
294 char *msg;
295 {
296 if (msg != NULL)
297 fprintf(stderr, "%s: Use error - %s\n", progname, msg);
298 fprintf(stderr, "Usage: %s month day hour [options]\n", progname);
299 fprintf(stderr, " Or: %s -ang altitude azimuth [options]\n", progname);
300 fprintf(stderr, " Or: %s -defaults\n", progname);
301 exit(1);
302 }
303
304
305 double
306 normsc() /* compute normalization factor (E0*F2/L0) */
307 {
308 static double nfc[2][5] = {
309 /* clear sky approx. */
310 {2.766521, 0.547665, -0.369832, 0.009237, 0.059229},
311 /* intermediate sky approx. */
312 {3.5556, -2.7152, -1.3081, 1.0660, 0.60227},
313 };
314 register double *nf;
315 double x, nsc;
316 register int i;
317 /* polynomial approximation */
318 nf = nfc[skytype==S_INTER];
319 x = (altitude - PI/4.0)/(PI/4.0);
320 nsc = nf[i=4];
321 while (i--)
322 nsc = nsc*x + nf[i];
323
324 return(nsc);
325 }
326
327
328 cvthour(hs) /* convert hour string */
329 char *hs;
330 {
331 register char *cp = hs;
332
333 while (*cp && *cp++ != ':')
334 ;
335 if (*cp)
336 hour = atoi(hs) + atoi(cp)/60.0;
337 else
338 hour = atof(hs);
339 tsolar = *hs == '+';
340 }
341
342
343 printhead(ac, av) /* print command header */
344 register int ac;
345 register char **av;
346 {
347 putchar('#');
348 while (ac--) {
349 putchar(' ');
350 fputs(*av++, stdout);
351 }
352 putchar('\n');
353 }