81 |
|
|
82 |
|
/* Include files */ |
83 |
|
#define _USE_MATH_DEFINES |
84 |
– |
#include <stdio.h> |
84 |
|
#include <stdlib.h> |
86 |
– |
#include <string.h> |
85 |
|
#include <ctype.h> |
86 |
+ |
#include "platform.h" |
87 |
|
#include "rtmath.h" |
88 |
+ |
#include "rtio.h" |
89 |
|
#include "color.h" |
90 |
+ |
#include "sun.h" |
91 |
|
|
92 |
< |
char *progname; /* Program name */ |
92 |
< |
char errmsg[128]; /* Error message buffer */ |
92 |
> |
char *progname; /* Program name */ |
93 |
|
const double DC_SolarConstantE = 1367.0; /* Solar constant W/m^2 */ |
94 |
|
const double DC_SolarConstantL = 127.5; /* Solar constant klux */ |
95 |
|
|
108 |
|
double solar_rad; /* Solar radiance */ |
109 |
|
double sun_zenith; /* Sun zenith angle (radians) */ |
110 |
|
int input = 0; /* Input type */ |
111 |
+ |
int output = 0; /* Output type */ |
112 |
|
|
113 |
|
extern double dmax( double, double ); |
114 |
|
extern double CalcAirMass(); |
134 |
|
/* Radiuans into degrees */ |
135 |
|
#define RadToDeg(rad) ((rad)*(180./PI)) |
136 |
|
|
136 |
– |
|
137 |
|
/* Perez sky model coefficients */ |
138 |
|
|
139 |
|
/* Reference: Perez, R., R. Seals, and J. Michalsky, 1993. "All- */ |
208 |
|
{ 1.950, 2.800 }, |
209 |
|
{ 2.800, 4.500 }, |
210 |
|
{ 4.500, 6.200 }, |
211 |
< |
{ 6.200, 12.00 } /* Clear */ |
211 |
> |
{ 6.200, 12.01 } /* Clear */ |
212 |
|
}; |
213 |
|
|
214 |
|
/* Luminous efficacy model coefficients */ |
246 |
|
{ 101.18, 1.58, -1.10, -8.29 } |
247 |
|
}; |
248 |
|
|
249 |
< |
extern int jdate(int month, int day); |
250 |
< |
extern double stadj(int jd); |
251 |
< |
extern double sdec(int jd); |
252 |
< |
extern double salt(double sd, double st); |
253 |
< |
extern double sazi(double sd, double st); |
254 |
< |
/* sun calculation constants */ |
255 |
< |
extern double s_latitude; |
256 |
< |
extern double s_longitude; |
257 |
< |
extern double s_meridian; |
249 |
> |
#ifndef NSUNPATCH |
250 |
> |
#define NSUNPATCH 4 /* max. # patches to spread sun into */ |
251 |
> |
#endif |
252 |
|
|
253 |
< |
double grefl = 0.2; /* diffuse ground reflectance */ |
253 |
> |
#define SUN_ANG_DEG 0.533 /* sun full-angle in degrees */ |
254 |
|
|
255 |
+ |
int nsuns = NSUNPATCH; /* number of sun patches to use */ |
256 |
+ |
double fixed_sun_sa = -1; /* fixed solid angle per sun? */ |
257 |
+ |
|
258 |
|
int verbose = 0; /* progress reports to stderr? */ |
259 |
|
|
260 |
|
int outfmt = 'a'; /* output format */ |
261 |
|
|
262 |
|
int rhsubdiv = 1; /* Reinhart sky subdivisions */ |
263 |
|
|
264 |
< |
float skycolor[3] = {.96, 1.004, 1.118}; /* sky coloration */ |
264 |
> |
COLOR skycolor = {.96, 1.004, 1.118}; /* sky coloration */ |
265 |
> |
COLOR suncolor = {1., 1., 1.}; /* sun color */ |
266 |
> |
COLOR grefl = {.2, .2, .2}; /* ground reflectance */ |
267 |
|
|
269 |
– |
int do_sun = 1; /* output direct solar contribution? */ |
270 |
– |
|
268 |
|
int nskypatch; /* number of Reinhart patches */ |
269 |
|
float *rh_palt; /* sky patch altitudes (radians) */ |
270 |
|
float *rh_pazi; /* sky patch azimuths (radians) */ |
271 |
|
float *rh_dom; /* sky patch solid angle (sr) */ |
272 |
|
|
273 |
< |
#define vector(v,alt,azi) ( (v)[1] = tcos(alt), \ |
274 |
< |
(v)[0] = (v)[1]*tsin(azi), \ |
275 |
< |
(v)[1] *= tcos(azi), \ |
276 |
< |
(v)[2] = tsin(alt) ) |
273 |
> |
#define vector(v,alt,azi) ( (v)[1] = cos(alt), \ |
274 |
> |
(v)[0] = (v)[1]*sin(azi), \ |
275 |
> |
(v)[1] *= cos(azi), \ |
276 |
> |
(v)[2] = sin(alt) ) |
277 |
|
|
278 |
|
#define rh_vector(v,i) vector(v,rh_palt[i],rh_pazi[i]) |
279 |
|
|
280 |
|
#define rh_cos(i) tsin(rh_palt[i]) |
281 |
|
|
282 |
+ |
#define solar_minute(jd,hr) ((24*60)*((jd)-1)+(int)((hr)*60.+.5)) |
283 |
+ |
|
284 |
|
extern int rh_init(void); |
285 |
|
extern float * resize_dmatrix(float *mtx_data, int nsteps, int npatch); |
286 |
+ |
extern void OutputSun(int id, int goodsun, FILE *fp, FILE *mfp); |
287 |
|
extern void AddDirect(float *parr); |
288 |
|
|
289 |
+ |
|
290 |
+ |
static const char * |
291 |
+ |
getfmtname(int fmt) |
292 |
+ |
{ |
293 |
+ |
switch (fmt) { |
294 |
+ |
case 'a': |
295 |
+ |
return("ascii"); |
296 |
+ |
case 'f': |
297 |
+ |
return("float"); |
298 |
+ |
case 'd': |
299 |
+ |
return("double"); |
300 |
+ |
} |
301 |
+ |
return("unknown"); |
302 |
+ |
} |
303 |
+ |
|
304 |
+ |
|
305 |
|
int |
306 |
|
main(int argc, char *argv[]) |
307 |
|
{ |
308 |
|
char buf[256]; |
309 |
+ |
int doheader = 1; /* output header? */ |
310 |
+ |
double rotation = 0; /* site rotation (degrees) */ |
311 |
|
double elevation; /* site elevation (meters) */ |
312 |
+ |
int leap_day = 0; /* add leap day? */ |
313 |
+ |
int sun_hours_only = 0; /* only output sun hours? */ |
314 |
|
int dir_is_horiz; /* direct is meas. on horizontal? */ |
315 |
+ |
FILE *sunsfp = NULL; /* output file for individual suns */ |
316 |
+ |
FILE *modsfp = NULL; /* modifier output file */ |
317 |
|
float *mtx_data = NULL; /* our matrix data */ |
318 |
< |
int ntsteps = 0; /* number of rows in matrix */ |
318 |
> |
int avgSky = 0; /* compute average sky r.t. matrix? */ |
319 |
> |
int ntsteps = 0; /* number of time steps */ |
320 |
> |
int tstorage = 0; /* number of allocated time steps */ |
321 |
> |
int nstored = 0; /* number of time steps in matrix */ |
322 |
|
int last_monthly = 0; /* month of last report */ |
323 |
|
int mo, da; /* month (1-12) and day (1-31) */ |
324 |
|
double hr; /* hour (local standard time) */ |
330 |
|
/* get options */ |
331 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
332 |
|
switch (argv[i][1]) { |
333 |
< |
case 'g': |
334 |
< |
grefl = atof(argv[++i]); |
333 |
> |
case 'g': /* ground reflectance */ |
334 |
> |
grefl[0] = atof(argv[++i]); |
335 |
> |
grefl[1] = atof(argv[++i]); |
336 |
> |
grefl[2] = atof(argv[++i]); |
337 |
|
break; |
338 |
< |
case 'v': |
338 |
> |
case 'v': /* verbose progress reports */ |
339 |
|
verbose++; |
340 |
|
break; |
341 |
< |
case 'o': |
341 |
> |
case 'h': /* turn off header */ |
342 |
> |
doheader = 0; |
343 |
> |
break; |
344 |
> |
case 'o': /* output format */ |
345 |
|
switch (argv[i][2]) { |
346 |
|
case 'f': |
347 |
|
case 'd': |
352 |
|
goto userr; |
353 |
|
} |
354 |
|
break; |
355 |
< |
case 'm': |
355 |
> |
case 'O': /* output type */ |
356 |
> |
switch (argv[i][2]) { |
357 |
> |
case '0': |
358 |
> |
output = 0; |
359 |
> |
break; |
360 |
> |
case '1': |
361 |
> |
output = 1; |
362 |
> |
break; |
363 |
> |
default: |
364 |
> |
goto userr; |
365 |
> |
} |
366 |
> |
if (argv[i][3]) |
367 |
> |
goto userr; |
368 |
> |
break; |
369 |
> |
case 'm': /* Reinhart subdivisions */ |
370 |
|
rhsubdiv = atoi(argv[++i]); |
371 |
|
break; |
372 |
< |
case 'c': |
372 |
> |
case 'c': /* sky color */ |
373 |
|
skycolor[0] = atof(argv[++i]); |
374 |
|
skycolor[1] = atof(argv[++i]); |
375 |
|
skycolor[2] = atof(argv[++i]); |
376 |
|
break; |
377 |
< |
case 'd': |
378 |
< |
do_sun = 1; |
377 |
> |
case 'D': /* output suns to file */ |
378 |
> |
if (strcmp(argv[++i], "-")) { |
379 |
> |
sunsfp = fopen(argv[i], "w"); |
380 |
> |
if (sunsfp == NULL) { |
381 |
> |
fprintf(stderr, |
382 |
> |
"%s: cannot open '%s' for output\n", |
383 |
> |
progname, argv[i]); |
384 |
> |
exit(1); |
385 |
> |
} |
386 |
> |
break; /* still may output matrix */ |
387 |
> |
} |
388 |
> |
sunsfp = stdout; /* sending to stdout, so... */ |
389 |
> |
/* fall through */ |
390 |
> |
case 'n': /* no matrix output */ |
391 |
> |
avgSky = -1; |
392 |
> |
rhsubdiv = 1; |
393 |
> |
/* fall through */ |
394 |
> |
case 'd': /* solar (direct) only */ |
395 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 0; |
396 |
+ |
grefl[0] = grefl[1] = grefl[2] = 0; |
397 |
|
break; |
398 |
< |
case 's': |
399 |
< |
do_sun = 0; |
400 |
< |
if (skycolor[1] <= 1e-4) |
401 |
< |
skycolor[0] = skycolor[1] = skycolor[2] = 1; |
398 |
> |
case 'M': /* send sun modifiers to file */ |
399 |
> |
if ((modsfp = fopen(argv[++i], "w")) == NULL) { |
400 |
> |
fprintf(stderr, "%s: cannot open '%s' for output\n", |
401 |
> |
progname, argv[i]); |
402 |
> |
exit(1); |
403 |
> |
} |
404 |
|
break; |
405 |
+ |
case 's': /* sky only (no direct) */ |
406 |
+ |
suncolor[0] = suncolor[1] = suncolor[2] = 0; |
407 |
+ |
break; |
408 |
+ |
case 'u': /* solar hours only */ |
409 |
+ |
sun_hours_only = 1; |
410 |
+ |
break; |
411 |
+ |
case 'r': /* rotate distribution */ |
412 |
+ |
if (argv[i][2] && argv[i][2] != 'z') |
413 |
+ |
goto userr; |
414 |
+ |
rotation = atof(argv[++i]); |
415 |
+ |
break; |
416 |
+ |
case '5': /* 5-phase calculation */ |
417 |
+ |
nsuns = 1; |
418 |
+ |
fixed_sun_sa = PI/360.*atof(argv[++i]); |
419 |
+ |
if (fixed_sun_sa <= 0) { |
420 |
+ |
fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n", |
421 |
+ |
progname); |
422 |
+ |
exit(1); |
423 |
+ |
} |
424 |
+ |
fixed_sun_sa *= fixed_sun_sa*PI; |
425 |
+ |
break; |
426 |
+ |
case 'A': /* compute average sky */ |
427 |
+ |
avgSky = 1; |
428 |
+ |
break; |
429 |
|
default: |
430 |
|
goto userr; |
431 |
|
} |
436 |
|
progname, argv[i]); |
437 |
|
exit(1); |
438 |
|
} |
439 |
+ |
if ((modsfp != NULL) & (sunsfp == NULL)) |
440 |
+ |
fprintf(stderr, "%s: warning -M output will be empty without -D\n", |
441 |
+ |
progname); |
442 |
|
if (verbose) { |
443 |
|
if (i == argc-1) |
444 |
|
fprintf(stderr, "%s: reading weather tape '%s'\n", |
481 |
|
fprintf(stderr, "%s: location '%s'\n", progname, buf); |
482 |
|
fprintf(stderr, "%s: (lat,long)=(%.1f,%.1f) degrees north, west\n", |
483 |
|
progname, s_latitude, s_longitude); |
484 |
< |
fprintf(stderr, "%s: %d sky patches per time step\n", |
485 |
< |
progname, nskypatch); |
484 |
> |
if (avgSky >= 0) |
485 |
> |
fprintf(stderr, "%s: %d sky patches\n", |
486 |
> |
progname, nskypatch); |
487 |
> |
if (sunsfp) |
488 |
> |
fprintf(stderr, "%s: outputting suns to file\n", |
489 |
> |
progname); |
490 |
> |
if (rotation != 0) |
491 |
> |
fprintf(stderr, "%s: rotating output %.0f degrees\n", |
492 |
> |
progname, rotation); |
493 |
|
} |
494 |
|
/* convert quantities to radians */ |
495 |
|
s_latitude = DegToRad(s_latitude); |
496 |
|
s_longitude = DegToRad(s_longitude); |
497 |
|
s_meridian = DegToRad(s_meridian); |
498 |
+ |
/* initial allocation */ |
499 |
+ |
mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch); |
500 |
|
/* process each time step in tape */ |
501 |
|
while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) { |
502 |
|
double sda, sta; |
503 |
< |
/* make space for next time step */ |
405 |
< |
mtx_offset = 3*nskypatch*ntsteps++; |
406 |
< |
mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch); |
407 |
< |
if (dif <= 1e-4) { |
408 |
< |
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
409 |
< |
continue; |
410 |
< |
} |
411 |
< |
if (verbose && mo != last_monthly) |
412 |
< |
fprintf(stderr, "%s: stepping through month %d...\n", |
413 |
< |
progname, last_monthly=mo); |
503 |
> |
int sun_in_sky; |
504 |
|
/* compute solar position */ |
505 |
< |
julian_date = jdate(mo, da); |
505 |
> |
if ((mo == 2) & (da == 29)) { |
506 |
> |
julian_date = 60; |
507 |
> |
leap_day = 1; |
508 |
> |
} else |
509 |
> |
julian_date = jdate(mo, da) + leap_day; |
510 |
|
sda = sdec(julian_date); |
511 |
|
sta = stadj(julian_date); |
512 |
|
altitude = salt(sda, hr+sta); |
513 |
< |
azimuth = sazi(sda, hr+sta) + PI; |
513 |
> |
sun_in_sky = (altitude > -DegToRad(SUN_ANG_DEG/2.)); |
514 |
> |
if (sun_hours_only && !sun_in_sky) |
515 |
> |
continue; /* skipping nighttime points */ |
516 |
> |
azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation); |
517 |
> |
|
518 |
> |
mtx_offset = 3*nskypatch*nstored; |
519 |
> |
nstored += !avgSky | !nstored; |
520 |
> |
/* make space for next row */ |
521 |
> |
if (nstored > tstorage) { |
522 |
> |
tstorage += (tstorage>>1) + nstored + 7; |
523 |
> |
mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch); |
524 |
> |
} |
525 |
> |
ntsteps++; /* keep count of time steps */ |
526 |
> |
|
527 |
> |
if (dir+dif <= 1e-4) { /* effectively nighttime? */ |
528 |
> |
if (!avgSky | !mtx_offset) |
529 |
> |
memset(mtx_data+mtx_offset, 0, |
530 |
> |
sizeof(float)*3*nskypatch); |
531 |
> |
/* output black sun? */ |
532 |
> |
if (sunsfp && sun_in_sky) |
533 |
> |
OutputSun(solar_minute(julian_date,hr), 0, |
534 |
> |
sunsfp, modsfp); |
535 |
> |
continue; |
536 |
> |
} |
537 |
> |
if (!sun_in_sky && dir > (input==1 ? 20. : 20.*WHTEFFICACY)) |
538 |
> |
fprintf(stderr, |
539 |
> |
"%s: warning - unusually bright at %.1f on %d-%d\n", |
540 |
> |
progname, hr, mo, da); |
541 |
|
/* convert measured values */ |
542 |
< |
if (dir_is_horiz && altitude > 0.) |
542 |
> |
if (dir_is_horiz && altitude > FTINY) |
543 |
|
dir /= sin(altitude); |
544 |
|
if (input == 1) { |
545 |
|
dir_irrad = dir; |
550 |
|
} |
551 |
|
/* compute sky patch values */ |
552 |
|
ComputeSky(mtx_data+mtx_offset); |
553 |
< |
if (do_sun) |
554 |
< |
AddDirect(mtx_data+mtx_offset); |
553 |
> |
/* output sun if requested */ |
554 |
> |
if (sunsfp && sun_in_sky) |
555 |
> |
OutputSun(solar_minute(julian_date,hr), 1, |
556 |
> |
sunsfp, modsfp); |
557 |
> |
|
558 |
> |
if (avgSky < 0) /* no matrix? */ |
559 |
> |
continue; |
560 |
> |
|
561 |
> |
AddDirect(mtx_data+mtx_offset); |
562 |
> |
/* update cumulative sky? */ |
563 |
> |
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
564 |
> |
mtx_data[i] += mtx_data[mtx_offset+i]; |
565 |
> |
/* monthly reporting */ |
566 |
> |
if (verbose && mo != last_monthly) |
567 |
> |
fprintf(stderr, "%s: stepping through month %d...\n", |
568 |
> |
progname, last_monthly=mo); |
569 |
> |
/* note whether leap-day was given */ |
570 |
|
} |
571 |
+ |
if (!ntsteps) { |
572 |
+ |
fprintf(stderr, "%s: no valid time steps on input\n", progname); |
573 |
+ |
exit(1); |
574 |
+ |
} |
575 |
|
/* check for junk at end */ |
576 |
|
while ((i = fgetc(stdin)) != EOF) |
577 |
|
if (!isspace(i)) { |
582 |
|
fputs(buf, stderr); fputc('\n', stderr); |
583 |
|
break; |
584 |
|
} |
585 |
+ |
|
586 |
+ |
if (avgSky < 0) /* no matrix output? */ |
587 |
+ |
goto alldone; |
588 |
+ |
|
589 |
+ |
dif = 1./(double)ntsteps; /* average sky? */ |
590 |
+ |
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
591 |
+ |
mtx_data[i] *= dif; |
592 |
|
/* write out matrix */ |
593 |
+ |
if (outfmt != 'a') |
594 |
+ |
SET_FILE_BINARY(stdout); |
595 |
|
#ifdef getc_unlocked |
596 |
|
flockfile(stdout); |
597 |
|
#endif |
598 |
|
if (verbose) |
599 |
|
fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", |
600 |
< |
progname, outfmt=='a' ? "" : "binary ", ntsteps); |
600 |
> |
progname, outfmt=='a' ? "" : "binary ", nstored); |
601 |
> |
if (doheader) { |
602 |
> |
newheader("RADIANCE", stdout); |
603 |
> |
printargs(argc, argv, stdout); |
604 |
> |
printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude), |
605 |
> |
-RadToDeg(s_longitude)); |
606 |
> |
printf("NROWS=%d\n", nskypatch); |
607 |
> |
printf("NCOLS=%d\n", nstored); |
608 |
> |
printf("NCOMP=3\n"); |
609 |
> |
if ((outfmt == 'f') | (outfmt == 'd')) |
610 |
> |
fputendian(stdout); |
611 |
> |
fputformat((char *)getfmtname(outfmt), stdout); |
612 |
> |
putchar('\n'); |
613 |
> |
} |
614 |
|
/* patches are rows (outer sort) */ |
615 |
|
for (i = 0; i < nskypatch; i++) { |
616 |
|
mtx_offset = 3*i; |
617 |
|
switch (outfmt) { |
618 |
|
case 'a': |
619 |
< |
for (j = 0; j < ntsteps; j++) { |
620 |
< |
printf("%.3e %.3e %.3e\n", mtx_data[mtx_offset], |
619 |
> |
for (j = 0; j < nstored; j++) { |
620 |
> |
printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset], |
621 |
|
mtx_data[mtx_offset+1], |
622 |
|
mtx_data[mtx_offset+2]); |
623 |
|
mtx_offset += 3*nskypatch; |
624 |
|
} |
625 |
< |
if (ntsteps > 1) |
625 |
> |
if (nstored > 1) |
626 |
|
fputc('\n', stdout); |
627 |
|
break; |
628 |
|
case 'f': |
629 |
< |
for (j = 0; j < ntsteps; j++) { |
630 |
< |
fwrite(mtx_data+mtx_offset, sizeof(float), 3, |
629 |
> |
for (j = 0; j < nstored; j++) { |
630 |
> |
putbinary(mtx_data+mtx_offset, sizeof(float), 3, |
631 |
|
stdout); |
632 |
|
mtx_offset += 3*nskypatch; |
633 |
|
} |
634 |
|
break; |
635 |
|
case 'd': |
636 |
< |
for (j = 0; j < ntsteps; j++) { |
636 |
> |
for (j = 0; j < nstored; j++) { |
637 |
|
double ment[3]; |
638 |
|
ment[0] = mtx_data[mtx_offset]; |
639 |
|
ment[1] = mtx_data[mtx_offset+1]; |
640 |
|
ment[2] = mtx_data[mtx_offset+2]; |
641 |
< |
fwrite(ment, sizeof(double), 3, stdout); |
641 |
> |
putbinary(ment, sizeof(double), 3, stdout); |
642 |
|
mtx_offset += 3*nskypatch; |
643 |
|
} |
644 |
|
break; |
646 |
|
if (ferror(stdout)) |
647 |
|
goto writerr; |
648 |
|
} |
649 |
< |
if (fflush(stdout) == EOF) |
649 |
> |
alldone: |
650 |
> |
if (fflush(NULL) == EOF) |
651 |
|
goto writerr; |
652 |
|
if (verbose) |
653 |
|
fprintf(stderr, "%s: done.\n", progname); |
654 |
|
exit(0); |
655 |
|
userr: |
656 |
< |
fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g refl][-c r g b][-o{f|d}] [tape.wea]\n", |
656 |
> |
fprintf(stderr, "Usage: %s [-v][-h][-A][-d|-s|-n][-u][-D file [-M modfile]][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n", |
657 |
|
progname); |
658 |
|
exit(1); |
659 |
|
fmterr: |
660 |
< |
fprintf(stderr, "%s: input weather tape format error\n", progname); |
660 |
> |
fprintf(stderr, "%s: weather tape format error in header\n", progname); |
661 |
|
exit(1); |
662 |
|
writerr: |
663 |
|
fprintf(stderr, "%s: write error on output\n", progname); |
674 |
|
{ |
675 |
|
int index; /* Category index */ |
676 |
|
double norm_diff_illum; /* Normalized diffuse illuimnance */ |
514 |
– |
double zlumin; /* Zenith luminance */ |
677 |
|
int i; |
678 |
|
|
679 |
|
/* Calculate atmospheric precipitable water content */ |
680 |
|
apwc = CalcPrecipWater(dew_point); |
681 |
|
|
682 |
< |
/* Limit solar altitude to keep circumsolar off zenith */ |
683 |
< |
if (altitude > DegToRad(87.0)) |
684 |
< |
altitude = DegToRad(87.0); |
682 |
> |
/* Calculate sun zenith angle (don't let it dip below horizon) */ |
683 |
> |
/* Also limit minimum angle to keep circumsolar off zenith */ |
684 |
> |
if (altitude <= 0.0) |
685 |
> |
sun_zenith = DegToRad(90.0); |
686 |
> |
else if (altitude >= DegToRad(87.0)) |
687 |
> |
sun_zenith = DegToRad(3.0); |
688 |
> |
else |
689 |
> |
sun_zenith = DegToRad(90.0) - altitude; |
690 |
|
|
524 |
– |
/* Calculate sun zenith angle */ |
525 |
– |
sun_zenith = DegToRad(90.0) - altitude; |
526 |
– |
|
691 |
|
/* Compute the inputs for the calculation of the sky distribution */ |
692 |
|
|
693 |
|
if (input == 0) /* XXX never used */ |
706 |
|
sky_brightness = CalcSkyBrightness(); |
707 |
|
sky_clearness = CalcSkyClearness(); |
708 |
|
|
709 |
+ |
/* Limit sky clearness */ |
710 |
+ |
if (sky_clearness > 11.9) |
711 |
+ |
sky_clearness = 11.9; |
712 |
+ |
|
713 |
+ |
/* Limit sky brightness */ |
714 |
+ |
if (sky_brightness < 0.01) |
715 |
+ |
sky_brightness = 0.01; |
716 |
+ |
|
717 |
|
/* Calculate illuminance */ |
718 |
|
index = GetCategoryIndex(); |
719 |
|
diff_illum = diff_irrad * CalcDiffuseIllumRatio(index); |
725 |
|
index = CalcSkyParamFromIllum(); |
726 |
|
} |
727 |
|
|
728 |
< |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
729 |
< |
memset(parr, 0, sizeof(float)*3*nskypatch); |
730 |
< |
return; |
728 |
> |
if (output == 1) { /* hack for solar radiance */ |
729 |
> |
diff_illum = diff_irrad * WHTEFFICACY; |
730 |
> |
dir_illum = dir_irrad * WHTEFFICACY; |
731 |
|
} |
732 |
|
/* Compute ground radiance (include solar contribution if any) */ |
733 |
< |
parr[0] = diff_illum * (1./PI/WHTEFFICACY); |
733 |
> |
parr[0] = diff_illum; |
734 |
|
if (altitude > 0) |
735 |
< |
parr[0] += dir_illum * sin(altitude) * (1./PI/WHTEFFICACY); |
736 |
< |
parr[2] = parr[1] = parr[0]; |
735 |
> |
parr[0] += dir_illum * sin(altitude); |
736 |
> |
parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY); |
737 |
> |
multcolor(parr, grefl); |
738 |
|
|
739 |
+ |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
740 |
+ |
memset(parr+3, 0, sizeof(float)*3*(nskypatch-1)); |
741 |
+ |
return; |
742 |
+ |
} |
743 |
|
/* Calculate Perez sky model parameters */ |
744 |
|
CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index); |
745 |
|
|
749 |
|
/* Calculate relative horizontal illuminance */ |
750 |
|
norm_diff_illum = CalcRelHorzIllum(parr); |
751 |
|
|
752 |
+ |
/* Check for zero sky -- make uniform in that case */ |
753 |
+ |
if (norm_diff_illum <= FTINY) { |
754 |
+ |
for (i = 1; i < nskypatch; i++) |
755 |
+ |
setcolor(parr+3*i, 1., 1., 1.); |
756 |
+ |
norm_diff_illum = PI; |
757 |
+ |
} |
758 |
|
/* Normalization coefficient */ |
759 |
|
norm_diff_illum = diff_illum / norm_diff_illum; |
760 |
|
|
578 |
– |
/* Calculate relative zenith luminance */ |
579 |
– |
zlumin = CalcRelLuminance(sun_zenith, 0.0); |
580 |
– |
|
581 |
– |
/* Calculate absolute zenith illuminance */ |
582 |
– |
zlumin *= norm_diff_illum; |
583 |
– |
|
761 |
|
/* Apply to sky patches to get absolute radiance values */ |
762 |
|
for (i = 1; i < nskypatch; i++) { |
763 |
< |
scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY)); |
763 |
> |
scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY)); |
764 |
|
multcolor(parr+3*i, skycolor); |
765 |
|
} |
766 |
|
} |
770 |
|
AddDirect(float *parr) |
771 |
|
{ |
772 |
|
FVECT svec; |
773 |
< |
double near_dprod[4]; |
774 |
< |
int near_patch[4]; |
775 |
< |
double wta[4], wtot; |
773 |
> |
double near_dprod[NSUNPATCH]; |
774 |
> |
int near_patch[NSUNPATCH]; |
775 |
> |
double wta[NSUNPATCH], wtot; |
776 |
|
int i, j, p; |
777 |
|
|
778 |
< |
if (!do_sun || dir_illum < 1e-4) |
778 |
> |
if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4) |
779 |
|
return; |
780 |
< |
/* identify 4 closest patches */ |
781 |
< |
for (i = 4; i--; ) |
780 |
> |
/* identify nsuns closest patches */ |
781 |
> |
if (nsuns > NSUNPATCH) |
782 |
> |
nsuns = NSUNPATCH; |
783 |
> |
else if (nsuns <= 0) |
784 |
> |
nsuns = 1; |
785 |
> |
for (i = nsuns; i--; ) |
786 |
|
near_dprod[i] = -1.; |
787 |
|
vector(svec, altitude, azimuth); |
788 |
|
for (p = 1; p < nskypatch; p++) { |
790 |
|
double dprod; |
791 |
|
rh_vector(pvec, p); |
792 |
|
dprod = DOT(pvec, svec); |
793 |
< |
for (i = 0; i < 4; i++) |
793 |
> |
for (i = 0; i < nsuns; i++) |
794 |
|
if (dprod > near_dprod[i]) { |
795 |
< |
for (j = 4; --j > i; ) { |
795 |
> |
for (j = nsuns; --j > i; ) { |
796 |
|
near_dprod[j] = near_dprod[j-1]; |
797 |
|
near_patch[j] = near_patch[j-1]; |
798 |
|
} |
802 |
|
} |
803 |
|
} |
804 |
|
wtot = 0; /* weight by proximity */ |
805 |
< |
for (i = 4; i--; ) |
805 |
> |
for (i = nsuns; i--; ) |
806 |
|
wtot += wta[i] = 1./(1.002 - near_dprod[i]); |
807 |
|
/* add to nearest patch radiances */ |
808 |
< |
for (i = 4; i--; ) { |
808 |
> |
for (i = nsuns; i--; ) { |
809 |
|
float *pdest = parr + 3*near_patch[i]; |
810 |
< |
float val_add = wta[i] * dir_illum / |
811 |
< |
(WHTEFFICACY * wtot * rh_dom[near_patch[i]]); |
812 |
< |
*pdest++ += val_add; |
813 |
< |
*pdest++ += val_add; |
814 |
< |
*pdest++ += val_add; |
810 |
> |
float val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot); |
811 |
> |
|
812 |
> |
val_add /= (fixed_sun_sa > 0) ? fixed_sun_sa |
813 |
> |
: rh_dom[near_patch[i]] ; |
814 |
> |
*pdest++ += val_add*suncolor[0]; |
815 |
> |
*pdest++ += val_add*suncolor[1]; |
816 |
> |
*pdest++ += val_add*suncolor[2]; |
817 |
|
} |
818 |
|
} |
819 |
|
|
820 |
+ |
/* Output a sun to indicated file if appropriate for this time step */ |
821 |
+ |
void |
822 |
+ |
OutputSun(int id, int goodsun, FILE *fp, FILE *mfp) |
823 |
+ |
{ |
824 |
+ |
double srad; |
825 |
+ |
FVECT sv; |
826 |
+ |
|
827 |
+ |
srad = DegToRad(SUN_ANG_DEG/2.); |
828 |
+ |
srad = goodsun ? dir_illum/(WHTEFFICACY * PI*srad*srad) : 0; |
829 |
+ |
vector(sv, altitude, azimuth); |
830 |
+ |
fprintf(fp, "\nvoid light solar%d\n0\n0\n", id); |
831 |
+ |
fprintf(fp, "3 %.3e %.3e %.3e\n", srad*suncolor[0], |
832 |
+ |
srad*suncolor[1], srad*suncolor[2]); |
833 |
+ |
fprintf(fp, "\nsolar%d source sun%d\n0\n0\n", id, id); |
834 |
+ |
fprintf(fp, "4 %.6f %.6f %.6f %.4f\n", sv[0], sv[1], sv[2], SUN_ANG_DEG); |
835 |
+ |
|
836 |
+ |
if (mfp != NULL) /* saving modifier IDs? */ |
837 |
+ |
fprintf(mfp, "solar%d\n", id); |
838 |
+ |
} |
839 |
+ |
|
840 |
|
/* Initialize Reinhart sky patch positions (GW) */ |
841 |
|
int |
842 |
|
rh_init(void) |
868 |
|
for (i = 0; i < NROW*rhsubdiv; i++) { |
869 |
|
const float ralt = alpha*(i + .5); |
870 |
|
const int ninrow = tnaz[i/rhsubdiv]*rhsubdiv; |
871 |
< |
const float dom = (sin(alpha*(i+1)) - sin(alpha*i))/ninrow; |
871 |
> |
const float dom = 2.*PI*(sin(alpha*(i+1)) - sin(alpha*i)) / |
872 |
> |
(double)ninrow; |
873 |
|
for (j = 0; j < ninrow; j++) { |
874 |
|
rh_palt[p] = ralt; |
875 |
|
rh_pazi[p] = 2.*PI * j / (double)ninrow; |
974 |
|
double sz_cubed; /* Sun zenith angle cubed */ |
975 |
|
|
976 |
|
/* Calculate sun zenith angle cubed */ |
977 |
< |
sz_cubed = pow(sun_zenith, 3.0); |
977 |
> |
sz_cubed = sun_zenith*sun_zenith*sun_zenith; |
978 |
|
|
979 |
|
return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 * |
980 |
|
sz_cubed) / (1.0 + 1.041 * sz_cubed); |
1005 |
|
double CalcDirectIrradiance() |
1006 |
|
{ |
1007 |
|
return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041 |
1008 |
< |
* pow(sun_zenith, 3.0))); |
1008 |
> |
* sun_zenith*sun_zenith*sun_zenith)); |
1009 |
|
} |
1010 |
|
|
1011 |
|
/* Calculate sky brightness and clearness from illuminance values */ |
1031 |
|
sky_clearness = 12.0; |
1032 |
|
|
1033 |
|
/* Limit sky brightness */ |
1034 |
< |
if (sky_brightness < 0.05) |
1034 |
> |
if (sky_brightness < 0.01) |
1035 |
|
sky_brightness = 0.01; |
1036 |
|
|
1037 |
|
while (((fabs(diff_irrad - test1) > 10.0) || |
1044 |
|
/* Convert illuminance to irradiance */ |
1045 |
|
index = GetCategoryIndex(); |
1046 |
|
diff_irrad = diff_illum / CalcDiffuseIllumRatio(index); |
1047 |
< |
dir_irrad = dir_illum / CalcDirectIllumRatio(index); |
1047 |
> |
dir_irrad = CalcDirectIllumRatio(index); |
1048 |
> |
if (dir_irrad > 0.1) |
1049 |
> |
dir_irrad = dir_illum / dir_irrad; |
1050 |
|
|
1051 |
|
/* Calculate sky brightness and clearness */ |
1052 |
|
sky_brightness = CalcSkyBrightness(); |
1057 |
|
sky_clearness = 12.0; |
1058 |
|
|
1059 |
|
/* Limit sky brightness */ |
1060 |
< |
if (sky_brightness < 0.05) |
1060 |
> |
if (sky_brightness < 0.01) |
1061 |
|
sky_brightness = 0.01; |
1062 |
|
} |
1063 |
|
|
1143 |
|
double rh_illum = 0.0; /* Relative horizontal illuminance */ |
1144 |
|
|
1145 |
|
for (i = 1; i < nskypatch; i++) |
1146 |
< |
rh_illum += parr[3*i+1] * rh_cos(i); |
1146 |
> |
rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i]; |
1147 |
|
|
1148 |
< |
return rh_illum * (2.0 * PI / (nskypatch-1)); |
1148 |
> |
return rh_illum; |
1149 |
|
} |
1150 |
|
|
1151 |
|
/* Calculate earth orbit eccentricity correction factor */ |