246 |
|
{ 101.18, 1.58, -1.10, -8.29 } |
247 |
|
}; |
248 |
|
|
249 |
+ |
#ifndef NSUNPATCH |
250 |
+ |
#define NSUNPATCH 4 /* # patches to spread sun into */ |
251 |
+ |
#endif |
252 |
+ |
|
253 |
|
extern int jdate(int month, int day); |
254 |
|
extern double stadj(int jd); |
255 |
|
extern double sdec(int jd); |
260 |
|
extern double s_longitude; |
261 |
|
extern double s_meridian; |
262 |
|
|
259 |
– |
double grefl = 0.2; /* diffuse ground reflectance */ |
260 |
– |
|
263 |
|
int verbose = 0; /* progress reports to stderr? */ |
264 |
|
|
265 |
|
int outfmt = 'a'; /* output format */ |
266 |
|
|
267 |
|
int rhsubdiv = 1; /* Reinhart sky subdivisions */ |
268 |
|
|
269 |
< |
float skycolor[3] = {.96, 1.004, 1.118}; /* sky coloration */ |
269 |
> |
COLOR skycolor = {.96, 1.004, 1.118}; /* sky coloration */ |
270 |
> |
COLOR suncolor = {1., 1., 1.}; /* sun color */ |
271 |
> |
COLOR grefl = {.2, .2, .2}; /* ground reflectance */ |
272 |
|
|
269 |
– |
int do_sun = 1; /* output direct solar contribution? */ |
270 |
– |
|
273 |
|
int nskypatch; /* number of Reinhart patches */ |
274 |
|
float *rh_palt; /* sky patch altitudes (radians) */ |
275 |
|
float *rh_pazi; /* sky patch azimuths (radians) */ |
307 |
|
/* get options */ |
308 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
309 |
|
switch (argv[i][1]) { |
310 |
< |
case 'g': |
311 |
< |
grefl = atof(argv[++i]); |
310 |
> |
case 'g': /* ground reflectance */ |
311 |
> |
grefl[0] = atof(argv[++i]); |
312 |
> |
grefl[1] = atof(argv[++i]); |
313 |
> |
grefl[2] = atof(argv[++i]); |
314 |
|
break; |
315 |
< |
case 'v': |
315 |
> |
case 'v': /* verbose progress reports */ |
316 |
|
verbose++; |
317 |
|
break; |
318 |
< |
case 'o': |
318 |
> |
case 'o': /* output format */ |
319 |
|
switch (argv[i][2]) { |
320 |
|
case 'f': |
321 |
|
case 'd': |
326 |
|
goto userr; |
327 |
|
} |
328 |
|
break; |
329 |
< |
case 'm': |
329 |
> |
case 'm': /* Reinhart subdivisions */ |
330 |
|
rhsubdiv = atoi(argv[++i]); |
331 |
|
break; |
332 |
< |
case 'c': |
332 |
> |
case 'c': /* sky color */ |
333 |
|
skycolor[0] = atof(argv[++i]); |
334 |
|
skycolor[1] = atof(argv[++i]); |
335 |
|
skycolor[2] = atof(argv[++i]); |
336 |
|
break; |
337 |
< |
case 'd': |
334 |
< |
do_sun = 1; |
337 |
> |
case 'd': /* solar (direct) only */ |
338 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 0; |
339 |
+ |
if (suncolor[1] <= 1e-4) |
340 |
+ |
suncolor[0] = suncolor[1] = suncolor[2] = 1; |
341 |
|
break; |
342 |
< |
case 's': |
343 |
< |
do_sun = 0; |
342 |
> |
case 's': /* sky only (no direct) */ |
343 |
> |
suncolor[0] = suncolor[1] = suncolor[2] = 0; |
344 |
|
if (skycolor[1] <= 1e-4) |
345 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 1; |
346 |
|
break; |
363 |
|
progname); |
364 |
|
} |
365 |
|
/* read weather tape header */ |
366 |
< |
if (scanf("place %[^\n]\n", buf) != 1) |
366 |
> |
if (scanf("place %[^\r\n] ", buf) != 1) |
367 |
|
goto fmterr; |
368 |
|
if (scanf("latitude %lf\n", &s_latitude) != 1) |
369 |
|
goto fmterr; |
399 |
|
fprintf(stderr, "%s: %d sky patches per time step\n", |
400 |
|
progname, nskypatch); |
401 |
|
} |
402 |
+ |
/* convert quantities to radians */ |
403 |
+ |
s_latitude = DegToRad(s_latitude); |
404 |
+ |
s_longitude = DegToRad(s_longitude); |
405 |
+ |
s_meridian = DegToRad(s_meridian); |
406 |
|
/* process each time step in tape */ |
407 |
|
while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) { |
408 |
|
double sda, sta; |
421 |
|
sda = sdec(julian_date); |
422 |
|
sta = stadj(julian_date); |
423 |
|
altitude = salt(sda, hr+sta); |
424 |
< |
azimuth = sazi(sda, hr+sta); |
424 |
> |
azimuth = sazi(sda, hr+sta) + PI; |
425 |
|
/* convert measured values */ |
426 |
|
if (dir_is_horiz && altitude > 0.) |
427 |
|
dir /= sin(altitude); |
434 |
|
} |
435 |
|
/* compute sky patch values */ |
436 |
|
ComputeSky(mtx_data+mtx_offset); |
437 |
< |
if (do_sun) |
429 |
< |
AddDirect(mtx_data+mtx_offset); |
437 |
> |
AddDirect(mtx_data+mtx_offset); |
438 |
|
} |
439 |
|
/* check for junk at end */ |
440 |
|
while ((i = fgetc(stdin)) != EOF) |
459 |
|
switch (outfmt) { |
460 |
|
case 'a': |
461 |
|
for (j = 0; j < ntsteps; j++) { |
462 |
< |
printf("%.3e %.3e %.3e\n", mtx_data[mtx_offset], |
462 |
> |
printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset], |
463 |
|
mtx_data[mtx_offset+1], |
464 |
|
mtx_data[mtx_offset+2]); |
465 |
|
mtx_offset += 3*nskypatch; |
466 |
|
} |
467 |
< |
fputc('\n', stdout); |
467 |
> |
if (ntsteps > 1) |
468 |
> |
fputc('\n', stdout); |
469 |
|
break; |
470 |
|
case 'f': |
471 |
|
for (j = 0; j < ntsteps; j++) { |
494 |
|
fprintf(stderr, "%s: done.\n", progname); |
495 |
|
exit(0); |
496 |
|
userr: |
497 |
< |
fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g refl][-c r g b][-o{f|d}] [tape.wea]\n", |
497 |
> |
fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g r g b][-c r g b][-o{f|d}] [tape.wea]\n", |
498 |
|
progname); |
499 |
|
exit(1); |
500 |
|
fmterr: |
515 |
|
{ |
516 |
|
int index; /* Category index */ |
517 |
|
double norm_diff_illum; /* Normalized diffuse illuimnance */ |
509 |
– |
double zlumin; /* Zenith luminance */ |
518 |
|
int i; |
511 |
– |
|
512 |
– |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
513 |
– |
memset(parr, 0, sizeof(float)*3*nskypatch); |
514 |
– |
return; |
515 |
– |
} |
519 |
|
|
520 |
|
/* Calculate atmospheric precipitable water content */ |
521 |
|
apwc = CalcPrecipWater(dew_point); |
522 |
|
|
523 |
< |
/* Limit solar altitude to keep circumsolar off zenith */ |
524 |
< |
if (altitude > DegToRad(87.0)) |
525 |
< |
altitude = DegToRad(87.0); |
523 |
> |
/* Calculate sun zenith angle (don't let it dip below horizon) */ |
524 |
> |
/* Also limit minimum angle to keep circumsolar off zenith */ |
525 |
> |
if (altitude <= 0.0) |
526 |
> |
sun_zenith = DegToRad(90.0); |
527 |
> |
else if (altitude >= DegToRad(87.0)) |
528 |
> |
sun_zenith = DegToRad(3.0); |
529 |
> |
else |
530 |
> |
sun_zenith = DegToRad(90.0) - altitude; |
531 |
|
|
524 |
– |
/* Calculate sun zenith angle */ |
525 |
– |
sun_zenith = DegToRad(90.0) - altitude; |
526 |
– |
|
532 |
|
/* Compute the inputs for the calculation of the sky distribution */ |
533 |
|
|
534 |
|
if (input == 0) /* XXX never used */ |
558 |
|
index = CalcSkyParamFromIllum(); |
559 |
|
} |
560 |
|
|
561 |
+ |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
562 |
+ |
memset(parr, 0, sizeof(float)*3*nskypatch); |
563 |
+ |
return; |
564 |
+ |
} |
565 |
|
/* Compute ground radiance (include solar contribution if any) */ |
566 |
< |
parr[0] = diff_illum * (1./PI/WHTEFFICACY); |
566 |
> |
parr[0] = diff_illum; |
567 |
|
if (altitude > 0) |
568 |
< |
parr[0] += dir_illum * sin(altitude) * (1./PI/WHTEFFICACY); |
569 |
< |
parr[2] = parr[1] = parr[0]; |
568 |
> |
parr[0] += dir_illum * sin(altitude); |
569 |
> |
parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY); |
570 |
> |
multcolor(parr, grefl); |
571 |
|
|
572 |
|
/* Calculate Perez sky model parameters */ |
573 |
|
CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index); |
581 |
|
/* Normalization coefficient */ |
582 |
|
norm_diff_illum = diff_illum / norm_diff_illum; |
583 |
|
|
574 |
– |
/* Calculate relative zenith luminance */ |
575 |
– |
zlumin = CalcRelLuminance(sun_zenith, 0.0); |
576 |
– |
|
577 |
– |
/* Calculate absolute zenith illuminance */ |
578 |
– |
zlumin *= norm_diff_illum; |
579 |
– |
|
584 |
|
/* Apply to sky patches to get absolute radiance values */ |
585 |
|
for (i = 1; i < nskypatch; i++) { |
586 |
< |
scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY)); |
586 |
> |
scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY)); |
587 |
|
multcolor(parr+3*i, skycolor); |
588 |
|
} |
589 |
|
} |
593 |
|
AddDirect(float *parr) |
594 |
|
{ |
595 |
|
FVECT svec; |
596 |
< |
double near_dprod[4]; |
597 |
< |
int near_patch[4]; |
598 |
< |
double wta[4], wtot; |
596 |
> |
double near_dprod[NSUNPATCH]; |
597 |
> |
int near_patch[NSUNPATCH]; |
598 |
> |
double wta[NSUNPATCH], wtot; |
599 |
|
int i, j, p; |
600 |
|
|
601 |
< |
if (!do_sun || dir_illum < 1e-4) |
601 |
> |
if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4) |
602 |
|
return; |
603 |
< |
/* identify 4 closest patches */ |
604 |
< |
for (i = 4; i--; ) |
603 |
> |
/* identify NSUNPATCH closest patches */ |
604 |
> |
for (i = NSUNPATCH; i--; ) |
605 |
|
near_dprod[i] = -1.; |
606 |
|
vector(svec, altitude, azimuth); |
607 |
|
for (p = 1; p < nskypatch; p++) { |
609 |
|
double dprod; |
610 |
|
rh_vector(pvec, p); |
611 |
|
dprod = DOT(pvec, svec); |
612 |
< |
for (i = 0; i < 4; i++) |
612 |
> |
for (i = 0; i < NSUNPATCH; i++) |
613 |
|
if (dprod > near_dprod[i]) { |
614 |
< |
for (j = 4; --j > i; ) { |
614 |
> |
for (j = NSUNPATCH; --j > i; ) { |
615 |
|
near_dprod[j] = near_dprod[j-1]; |
616 |
|
near_patch[j] = near_patch[j-1]; |
617 |
|
} |
621 |
|
} |
622 |
|
} |
623 |
|
wtot = 0; /* weight by proximity */ |
624 |
< |
for (i = 4; i--; ) |
624 |
> |
for (i = NSUNPATCH; i--; ) |
625 |
|
wtot += wta[i] = 1./(1.002 - near_dprod[i]); |
626 |
|
/* add to nearest patch radiances */ |
627 |
< |
for (i = 4; i--; ) |
628 |
< |
parr[near_patch[i]] += wta[i] * dir_illum / |
629 |
< |
(wtot * rh_dom[near_patch[i]]); |
627 |
> |
for (i = NSUNPATCH; i--; ) { |
628 |
> |
float *pdest = parr + 3*near_patch[i]; |
629 |
> |
float val_add = wta[i] * dir_illum / |
630 |
> |
(WHTEFFICACY * wtot * rh_dom[near_patch[i]]); |
631 |
> |
*pdest++ += val_add*suncolor[0]; |
632 |
> |
*pdest++ += val_add*suncolor[1]; |
633 |
> |
*pdest++ += val_add*suncolor[2]; |
634 |
> |
} |
635 |
|
} |
636 |
|
|
637 |
|
/* Initialize Reinhart sky patch positions (GW) */ |
665 |
|
for (i = 0; i < NROW*rhsubdiv; i++) { |
666 |
|
const float ralt = alpha*(i + .5); |
667 |
|
const int ninrow = tnaz[i/rhsubdiv]*rhsubdiv; |
668 |
< |
const float dom = (sin(alpha*(i+1)) - sin(alpha*i))/ninrow; |
668 |
> |
const float dom = 2.*PI*(sin(alpha*(i+1)) - sin(alpha*i)) / |
669 |
> |
(double)ninrow; |
670 |
|
for (j = 0; j < ninrow; j++) { |
671 |
|
rh_palt[p] = ralt; |
672 |
|
rh_pazi[p] = 2.*PI * j / (double)ninrow; |
938 |
|
double rh_illum = 0.0; /* Relative horizontal illuminance */ |
939 |
|
|
940 |
|
for (i = 1; i < nskypatch; i++) |
941 |
< |
rh_illum += parr[3*i+1] * rh_cos(i); |
941 |
> |
rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i]; |
942 |
|
|
943 |
< |
return rh_illum * (2.0 * PI / (nskypatch-1)); |
943 |
> |
return rh_illum; |
944 |
|
} |
945 |
|
|
946 |
|
/* Calculate earth orbit eccentricity correction factor */ |