ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gensky.c
Revision: 2.29
Committed: Thu Mar 30 20:19:05 2023 UTC (12 months, 4 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, HEAD
Changes since 2.28: +4 -4 lines
Log Message:
perf(gensky): increased output precision to 0.1% (from 1%)

File Contents

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