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++) { |
517 |
|
double norm_diff_illum; /* Normalized diffuse illuimnance */ |
518 |
|
double zlumin; /* Zenith luminance */ |
519 |
|
int i; |
511 |
– |
|
512 |
– |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
513 |
– |
memset(parr, 0, sizeof(float)*3*nskypatch); |
514 |
– |
return; |
515 |
– |
} |
520 |
|
|
521 |
|
/* Calculate atmospheric precipitable water content */ |
522 |
|
apwc = CalcPrecipWater(dew_point); |
557 |
|
index = CalcSkyParamFromIllum(); |
558 |
|
} |
559 |
|
|
560 |
+ |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
561 |
+ |
memset(parr, 0, sizeof(float)*3*nskypatch); |
562 |
+ |
return; |
563 |
+ |
} |
564 |
|
/* Compute ground radiance (include solar contribution if any) */ |
565 |
< |
parr[0] = diff_illum * (1./PI/WHTEFFICACY); |
565 |
> |
parr[0] = diff_illum; |
566 |
|
if (altitude > 0) |
567 |
< |
parr[0] += dir_illum * sin(altitude) * (1./PI/WHTEFFICACY); |
568 |
< |
parr[2] = parr[1] = parr[0]; |
567 |
> |
parr[0] += dir_illum * sin(altitude); |
568 |
> |
parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY); |
569 |
> |
multcolor(parr, grefl); |
570 |
|
|
571 |
|
/* Calculate Perez sky model parameters */ |
572 |
|
CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index); |
598 |
|
AddDirect(float *parr) |
599 |
|
{ |
600 |
|
FVECT svec; |
601 |
< |
double near_dprod[4]; |
602 |
< |
int near_patch[4]; |
603 |
< |
double wta[4], wtot; |
601 |
> |
double near_dprod[NSUNPATCH]; |
602 |
> |
int near_patch[NSUNPATCH]; |
603 |
> |
double wta[NSUNPATCH], wtot; |
604 |
|
int i, j, p; |
605 |
|
|
606 |
< |
if (!do_sun || dir_illum < 1e-4) |
606 |
> |
if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4) |
607 |
|
return; |
608 |
< |
/* identify 4 closest patches */ |
609 |
< |
for (i = 4; i--; ) |
608 |
> |
/* identify NSUNPATCH closest patches */ |
609 |
> |
for (i = NSUNPATCH; i--; ) |
610 |
|
near_dprod[i] = -1.; |
611 |
|
vector(svec, altitude, azimuth); |
612 |
|
for (p = 1; p < nskypatch; p++) { |
614 |
|
double dprod; |
615 |
|
rh_vector(pvec, p); |
616 |
|
dprod = DOT(pvec, svec); |
617 |
< |
for (i = 0; i < 4; i++) |
617 |
> |
for (i = 0; i < NSUNPATCH; i++) |
618 |
|
if (dprod > near_dprod[i]) { |
619 |
< |
for (j = 4; --j > i; ) { |
619 |
> |
for (j = NSUNPATCH; --j > i; ) { |
620 |
|
near_dprod[j] = near_dprod[j-1]; |
621 |
|
near_patch[j] = near_patch[j-1]; |
622 |
|
} |
626 |
|
} |
627 |
|
} |
628 |
|
wtot = 0; /* weight by proximity */ |
629 |
< |
for (i = 4; i--; ) |
629 |
> |
for (i = NSUNPATCH; i--; ) |
630 |
|
wtot += wta[i] = 1./(1.002 - near_dprod[i]); |
631 |
|
/* add to nearest patch radiances */ |
632 |
< |
for (i = 4; i--; ) |
633 |
< |
parr[near_patch[i]] += wta[i] * dir_illum / |
634 |
< |
(wtot * rh_dom[near_patch[i]]); |
632 |
> |
for (i = NSUNPATCH; i--; ) { |
633 |
> |
float *pdest = parr + 3*near_patch[i]; |
634 |
> |
float val_add = wta[i] * dir_illum / |
635 |
> |
(WHTEFFICACY * wtot * rh_dom[near_patch[i]]); |
636 |
> |
*pdest++ += val_add*suncolor[0]; |
637 |
> |
*pdest++ += val_add*suncolor[1]; |
638 |
> |
*pdest++ += val_add*suncolor[2]; |
639 |
> |
} |
640 |
|
} |
641 |
|
|
642 |
|
/* Initialize Reinhart sky patch positions (GW) */ |
670 |
|
for (i = 0; i < NROW*rhsubdiv; i++) { |
671 |
|
const float ralt = alpha*(i + .5); |
672 |
|
const int ninrow = tnaz[i/rhsubdiv]*rhsubdiv; |
673 |
< |
const float dom = (sin(alpha*(i+1)) - sin(alpha*i))/ninrow; |
673 |
> |
const float dom = 2.*PI*(sin(alpha*(i+1)) - sin(alpha*i)) / |
674 |
> |
(double)ninrow; |
675 |
|
for (j = 0; j < ninrow; j++) { |
676 |
|
rh_palt[p] = ralt; |
677 |
|
rh_pazi[p] = 2.*PI * j / (double)ninrow; |