ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gensky.c
Revision: 2.27
Committed: Thu Nov 7 23:15:07 2019 UTC (4 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.26: +22 -9 lines
Log Message:
Added more accurate Michalsky solar position calculation and made correction to old IES handbook formula (thanks to Axel Jacobs)

File Contents

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