ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gensky.c
Revision: 2.18
Committed: Thu Jul 18 18:00:51 1996 UTC (27 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.17: +1 -1 lines
Log Message:
changed panic at standard time difference from 2 to 3 hours

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 <ctype.h>
21
22 #include "color.h"
23
24 extern char *strcpy(), *strcat(), *malloc();
25 extern double stadj(), sdec(), sazi(), salt(), tz2mer();
26
27 #ifndef PI
28 #define PI 3.14159265358979323846
29 #endif
30
31 #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
32
33 #define S_CLEAR 1
34 #define S_OVER 2
35 #define S_UNIF 3
36 #define S_INTER 4
37
38 #define overcast (skytype==S_OVER|skytype==S_UNIF)
39
40 double normsc();
41 /* sun calculation constants */
42 extern double s_latitude;
43 extern double s_longitude;
44 extern double s_meridian;
45
46 #undef toupper
47 #define toupper(c) ((c) & ~0x20) /* ASCII trick to convert case */
48
49 /* European and North American zones */
50 struct {
51 char zname[8]; /* time zone name (all caps) */
52 float zmer; /* standard meridian */
53 } tzone[] = {
54 "YST", 135, "YDT", 120,
55 "PST", 120, "PDT", 105,
56 "MST", 105, "MDT", 90,
57 "CST", 90, "CDT", 75,
58 "EST", 75, "EDT", 60,
59 "AST", 60, "ADT", 45,
60 "NST", 52.5, "NDT", 37.5,
61 "GMT", 0, "BST", -15,
62 "WET", -15, "WETDST", -30,
63 "MET", -30, "METDST", -45,
64 "MEZ", -30, "MESZ", -45,
65 "", 0
66 };
67 /* required values */
68 int month, day; /* date */
69 double hour; /* time */
70 int tsolar; /* 0=standard, 1=solar */
71 double altitude, azimuth; /* or solar angles */
72 /* default values */
73 int skytype = S_CLEAR; /* sky type */
74 int dosun = 1;
75 double zenithbr = 0.0;
76 int u_zenith = 0; /* -1=irradiance, 1=radiance */
77 double turbidity = 2.75;
78 double gprefl = 0.2;
79 /* computed values */
80 double sundir[3];
81 double groundbr;
82 double F2;
83 double solarbr = 0.0;
84 int u_solar = 0; /* -1=irradiance, 1=radiance */
85
86 char *progname;
87 char errmsg[128];
88
89
90 main(argc, argv)
91 int argc;
92 char *argv[];
93 {
94 int i;
95
96 progname = argv[0];
97 if (argc == 2 && !strcmp(argv[1], "-defaults")) {
98 printdefaults();
99 exit(0);
100 }
101 if (argc < 4)
102 userror("arg count");
103 if (!strcmp(argv[1], "-ang")) {
104 altitude = atof(argv[2]) * (PI/180);
105 azimuth = atof(argv[3]) * (PI/180);
106 month = 0;
107 } else {
108 month = atoi(argv[1]);
109 if (month < 1 || month > 12)
110 userror("bad month");
111 day = atoi(argv[2]);
112 if (day < 1 || day > 31)
113 userror("bad day");
114 cvthour(argv[3]);
115 }
116 for (i = 4; i < argc; i++)
117 if (argv[i][0] == '-' || argv[i][0] == '+')
118 switch (argv[i][1]) {
119 case 's':
120 skytype = S_CLEAR;
121 dosun = argv[i][0] == '+';
122 break;
123 case 'r':
124 case 'R':
125 u_solar = argv[i][1]=='R' ? -1 : 1;
126 solarbr = atof(argv[++i]);
127 break;
128 case 'c':
129 skytype = S_OVER;
130 break;
131 case 'u':
132 skytype = S_UNIF;
133 break;
134 case 'i':
135 skytype = S_INTER;
136 dosun = argv[i][0] == '+';
137 break;
138 case 't':
139 turbidity = atof(argv[++i]);
140 break;
141 case 'b':
142 case 'B':
143 u_zenith = argv[i][1]=='B' ? -1 : 1;
144 zenithbr = atof(argv[++i]);
145 break;
146 case 'g':
147 gprefl = atof(argv[++i]);
148 break;
149 case 'a':
150 s_latitude = atof(argv[++i]) * (PI/180);
151 break;
152 case 'o':
153 s_longitude = atof(argv[++i]) * (PI/180);
154 break;
155 case 'm':
156 s_meridian = atof(argv[++i]) * (PI/180);
157 break;
158 default:
159 sprintf(errmsg, "unknown option: %s", argv[i]);
160 userror(errmsg);
161 }
162 else
163 userror("bad option");
164
165 if (fabs(s_meridian-s_longitude) > 45*PI/180)
166 fprintf(stderr,
167 "%s: warning: %.1f hours btwn. standard meridian and longitude\n",
168 progname, (s_longitude-s_meridian)*12/PI);
169
170 printhead(argc, argv);
171
172 computesky();
173 printsky();
174
175 exit(0);
176 }
177
178
179 computesky() /* compute sky parameters */
180 {
181 double normfactor;
182 /* compute solar direction */
183 if (month) { /* from date and time */
184 int jd;
185 double sd, st;
186
187 jd = jdate(month, day); /* Julian date */
188 sd = sdec(jd); /* solar declination */
189 if (tsolar) /* solar time */
190 st = hour;
191 else
192 st = hour + stadj(jd);
193 altitude = salt(sd, st);
194 azimuth = sazi(sd, st);
195 printf("# Local solar time: %.2f\n", st);
196 printf("# Solar altitude and azimuth: %.1f %.1f\n",
197 180./PI*altitude, 180./PI*azimuth);
198 }
199 if (!overcast && altitude > 87.*PI/180.) {
200 fprintf(stderr,
201 "%s: warning - sun too close to zenith, reducing altitude to 87 degrees\n",
202 progname);
203 printf(
204 "# warning - sun too close to zenith, reducing altitude to 87 degrees\n");
205 altitude = 87.*PI/180.;
206 }
207 sundir[0] = -sin(azimuth)*cos(altitude);
208 sundir[1] = -cos(azimuth)*cos(altitude);
209 sundir[2] = sin(altitude);
210
211 /* Compute normalization factor */
212 switch (skytype) {
213 case S_UNIF:
214 normfactor = 1.0;
215 break;
216 case S_OVER:
217 normfactor = 0.777778;
218 break;
219 case S_CLEAR:
220 F2 = 0.274*(0.91 + 10.0*exp(-3.0*(PI/2.0-altitude)) +
221 0.45*sundir[2]*sundir[2]);
222 normfactor = normsc()/F2/PI;
223 break;
224 case S_INTER:
225 F2 = (2.739 + .9891*sin(.3119+2.6*altitude)) *
226 exp(-(PI/2.0-altitude)*(.4441+1.48*altitude));
227 normfactor = normsc()/F2/PI;
228 break;
229 }
230 /* Compute zenith brightness */
231 if (u_zenith == -1)
232 zenithbr /= normfactor*PI;
233 else if (u_zenith == 0) {
234 if (overcast)
235 zenithbr = 8.6*sundir[2] + .123;
236 else
237 zenithbr = (1.376*turbidity-1.81)*tan(altitude)+0.38;
238 if (skytype == S_INTER)
239 zenithbr = (zenithbr + 8.6*sundir[2] + .123)/2.0;
240 if (zenithbr < 0.0)
241 zenithbr = 0.0;
242 else
243 zenithbr *= 1000.0/SKYEFFICACY;
244 }
245 /* Compute horizontal radiance */
246 groundbr = zenithbr*normfactor;
247 printf("# Ground ambient level: %.1f\n", groundbr);
248 if (!overcast && sundir[2] > 0.0 && (!u_solar || solarbr > 0.0)) {
249 if (u_solar == -1)
250 solarbr /= 6e-5*sundir[2];
251 else if (u_solar == 0) {
252 solarbr = 1.5e9/SUNEFFICACY *
253 (1.147 - .147/(sundir[2]>.16?sundir[2]:.16));
254 if (skytype == S_INTER)
255 solarbr *= 0.15; /* fudge factor! */
256 }
257 groundbr += 6e-5/PI*solarbr*sundir[2];
258 } else
259 dosun = 0;
260 groundbr *= gprefl;
261 }
262
263
264 printsky() /* print out sky */
265 {
266 if (dosun) {
267 printf("\nvoid light solar\n");
268 printf("0\n0\n");
269 printf("3 %.2e %.2e %.2e\n", solarbr, solarbr, solarbr);
270 printf("\nsolar source sun\n");
271 printf("0\n0\n");
272 printf("4 %f %f %f 0.5\n", sundir[0], sundir[1], sundir[2]);
273 }
274
275 printf("\nvoid brightfunc skyfunc\n");
276 printf("2 skybr skybright.cal\n");
277 printf("0\n");
278 if (overcast)
279 printf("3 %d %.2e %.2e\n", skytype, zenithbr, groundbr);
280 else
281 printf("7 %d %.2e %.2e %.2e %f %f %f\n",
282 skytype, zenithbr, groundbr, F2,
283 sundir[0], sundir[1], sundir[2]);
284 }
285
286
287 printdefaults() /* print default values */
288 {
289 switch (skytype) {
290 case S_OVER:
291 printf("-c\t\t\t\t# Cloudy sky\n");
292 break;
293 case S_UNIF:
294 printf("-u\t\t\t\t# Uniform cloudy sky\n");
295 break;
296 case S_INTER:
297 if (dosun)
298 printf("+i\t\t\t\t# Intermediate sky with sun\n");
299 else
300 printf("-i\t\t\t\t# Intermediate sky without sun\n");
301 break;
302 case S_CLEAR:
303 if (dosun)
304 printf("+s\t\t\t\t# Sunny sky with sun\n");
305 else
306 printf("-s\t\t\t\t# Sunny sky without sun\n");
307 break;
308 }
309 printf("-g %f\t\t\t# Ground plane reflectance\n", gprefl);
310 if (zenithbr > 0.0)
311 printf("-b %f\t\t\t# Zenith radiance (watts/ster/m2\n", zenithbr);
312 else
313 printf("-t %f\t\t\t# Atmospheric turbidity\n", turbidity);
314 printf("-a %f\t\t\t# Site latitude (degrees)\n", s_latitude*(180/PI));
315 printf("-o %f\t\t\t# Site longitude (degrees)\n", s_longitude*(180/PI));
316 printf("-m %f\t\t\t# Standard meridian (degrees)\n", s_meridian*(180/PI));
317 }
318
319
320 userror(msg) /* print usage error and quit */
321 char *msg;
322 {
323 if (msg != NULL)
324 fprintf(stderr, "%s: Use error - %s\n", progname, msg);
325 fprintf(stderr, "Usage: %s month day hour [options]\n", progname);
326 fprintf(stderr, " Or: %s -ang altitude azimuth [options]\n", progname);
327 fprintf(stderr, " Or: %s -defaults\n", progname);
328 exit(1);
329 }
330
331
332 double
333 normsc() /* compute normalization factor (E0*F2/L0) */
334 {
335 static double nfc[2][5] = {
336 /* clear sky approx. */
337 {2.766521, 0.547665, -0.369832, 0.009237, 0.059229},
338 /* intermediate sky approx. */
339 {3.5556, -2.7152, -1.3081, 1.0660, 0.60227},
340 };
341 register double *nf;
342 double x, nsc;
343 register int i;
344 /* polynomial approximation */
345 nf = nfc[skytype==S_INTER];
346 x = (altitude - PI/4.0)/(PI/4.0);
347 nsc = nf[i=4];
348 while (i--)
349 nsc = nsc*x + nf[i];
350
351 return(nsc);
352 }
353
354
355 cvthour(hs) /* convert hour string */
356 char *hs;
357 {
358 register char *cp = hs;
359 register int i, j;
360
361 if (tsolar = *cp == '+') cp++; /* solar time? */
362 while (isdigit(*cp)) cp++;
363 if (*cp == ':')
364 hour = atoi(hs) + atoi(++cp)/60.0;
365 else {
366 hour = atof(hs);
367 if (*cp == '.') cp++;
368 }
369 while (isdigit(*cp)) cp++;
370 if (!*cp)
371 return;
372 if (tsolar || !isalpha(*cp)) {
373 fprintf(stderr, "%s: bad time format: %s\n", progname, hs);
374 exit(1);
375 }
376 i = 0;
377 do {
378 for (j = 0; cp[j]; j++)
379 if (toupper(cp[j]) != tzone[i].zname[j])
380 break;
381 if (!cp[j] && !tzone[i].zname[j]) {
382 s_meridian = tzone[i].zmer * (PI/180);
383 return;
384 }
385 } while (tzone[i++].zname[0]);
386
387 fprintf(stderr, "%s: unknown time zone: %s\n", progname, cp);
388 fprintf(stderr, "Known time zones:\n\t%s", tzone[0].zname);
389 for (i = 1; tzone[i].zname[0]; i++)
390 fprintf(stderr, " %s", tzone[i].zname);
391 putc('\n', stderr);
392 exit(1);
393 }
394
395
396 printhead(ac, av) /* print command header */
397 register int ac;
398 register char **av;
399 {
400 putchar('#');
401 while (ac--) {
402 putchar(' ');
403 fputs(*av++, stdout);
404 }
405 putchar('\n');
406 }