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(); |
209 |
|
{ 1.950, 2.800 }, |
210 |
|
{ 2.800, 4.500 }, |
211 |
|
{ 4.500, 6.200 }, |
212 |
< |
{ 6.200, 12.00 } /* Clear */ |
212 |
> |
{ 6.200, 12.01 } /* Clear */ |
213 |
|
}; |
214 |
|
|
215 |
|
/* Luminous efficacy model coefficients */ |
248 |
|
}; |
249 |
|
|
250 |
|
#ifndef NSUNPATCH |
251 |
< |
#define NSUNPATCH 4 /* # patches to spread sun into */ |
251 |
> |
#define NSUNPATCH 4 /* max. # patches to spread sun into */ |
252 |
|
#endif |
253 |
|
|
254 |
|
extern int jdate(int month, int day); |
261 |
|
extern double s_longitude; |
262 |
|
extern double s_meridian; |
263 |
|
|
264 |
+ |
int nsuns = NSUNPATCH; /* number of sun patches to use */ |
265 |
+ |
double fixed_sun_sa = -1; /* fixed solid angle per sun? */ |
266 |
+ |
|
267 |
|
int verbose = 0; /* progress reports to stderr? */ |
268 |
|
|
269 |
|
int outfmt = 'a'; /* output format */ |
296 |
|
main(int argc, char *argv[]) |
297 |
|
{ |
298 |
|
char buf[256]; |
299 |
+ |
double rotation = 0; /* site rotation (degrees) */ |
300 |
|
double elevation; /* site elevation (meters) */ |
301 |
|
int dir_is_horiz; /* direct is meas. on horizontal? */ |
302 |
|
float *mtx_data = NULL; /* our matrix data */ |
331 |
|
goto userr; |
332 |
|
} |
333 |
|
break; |
334 |
+ |
case 'O': /* output type */ |
335 |
+ |
switch (argv[i][2]) { |
336 |
+ |
case '0': |
337 |
+ |
output = 0; |
338 |
+ |
break; |
339 |
+ |
case '1': |
340 |
+ |
output = 1; |
341 |
+ |
break; |
342 |
+ |
default: |
343 |
+ |
goto userr; |
344 |
+ |
} |
345 |
+ |
if (argv[i][3]) |
346 |
+ |
goto userr; |
347 |
+ |
break; |
348 |
|
case 'm': /* Reinhart subdivisions */ |
349 |
|
rhsubdiv = atoi(argv[++i]); |
350 |
|
break; |
363 |
|
if (skycolor[1] <= 1e-4) |
364 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 1; |
365 |
|
break; |
366 |
+ |
case 'r': /* rotate distribution */ |
367 |
+ |
if (argv[i][2] && argv[i][2] != 'z') |
368 |
+ |
goto userr; |
369 |
+ |
rotation = atof(argv[++i]); |
370 |
+ |
break; |
371 |
+ |
case '5': /* 5-phase calculation */ |
372 |
+ |
nsuns = 1; |
373 |
+ |
fixed_sun_sa = 6.797e-05; |
374 |
+ |
break; |
375 |
|
default: |
376 |
|
goto userr; |
377 |
|
} |
426 |
|
progname, s_latitude, s_longitude); |
427 |
|
fprintf(stderr, "%s: %d sky patches per time step\n", |
428 |
|
progname, nskypatch); |
429 |
+ |
if (rotation != 0) |
430 |
+ |
fprintf(stderr, "%s: rotating output %.0f degrees\n", |
431 |
+ |
progname, rotation); |
432 |
|
} |
433 |
|
/* convert quantities to radians */ |
434 |
|
s_latitude = DegToRad(s_latitude); |
452 |
|
sda = sdec(julian_date); |
453 |
|
sta = stadj(julian_date); |
454 |
|
altitude = salt(sda, hr+sta); |
455 |
< |
azimuth = sazi(sda, hr+sta) + PI; |
455 |
> |
azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation); |
456 |
|
/* convert measured values */ |
457 |
|
if (dir_is_horiz && altitude > 0.) |
458 |
|
dir /= sin(altitude); |
525 |
|
fprintf(stderr, "%s: done.\n", progname); |
526 |
|
exit(0); |
527 |
|
userr: |
528 |
< |
fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g r g b][-c r g b][-o{f|d}] [tape.wea]\n", |
528 |
> |
fprintf(stderr, "Usage: %s [-v][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n", |
529 |
|
progname); |
530 |
|
exit(1); |
531 |
|
fmterr: |
546 |
|
{ |
547 |
|
int index; /* Category index */ |
548 |
|
double norm_diff_illum; /* Normalized diffuse illuimnance */ |
518 |
– |
double zlumin; /* Zenith luminance */ |
549 |
|
int i; |
550 |
|
|
551 |
|
/* Calculate atmospheric precipitable water content */ |
552 |
|
apwc = CalcPrecipWater(dew_point); |
553 |
|
|
554 |
< |
/* Limit solar altitude to keep circumsolar off zenith */ |
555 |
< |
if (altitude > DegToRad(87.0)) |
556 |
< |
altitude = DegToRad(87.0); |
554 |
> |
/* Calculate sun zenith angle (don't let it dip below horizon) */ |
555 |
> |
/* Also limit minimum angle to keep circumsolar off zenith */ |
556 |
> |
if (altitude <= 0.0) |
557 |
> |
sun_zenith = DegToRad(90.0); |
558 |
> |
else if (altitude >= DegToRad(87.0)) |
559 |
> |
sun_zenith = DegToRad(3.0); |
560 |
> |
else |
561 |
> |
sun_zenith = DegToRad(90.0) - altitude; |
562 |
|
|
528 |
– |
/* Calculate sun zenith angle */ |
529 |
– |
sun_zenith = DegToRad(90.0) - altitude; |
530 |
– |
|
563 |
|
/* Compute the inputs for the calculation of the sky distribution */ |
564 |
|
|
565 |
|
if (input == 0) /* XXX never used */ |
578 |
|
sky_brightness = CalcSkyBrightness(); |
579 |
|
sky_clearness = CalcSkyClearness(); |
580 |
|
|
581 |
+ |
/* Limit sky clearness */ |
582 |
+ |
if (sky_clearness > 11.9) |
583 |
+ |
sky_clearness = 11.9; |
584 |
+ |
|
585 |
+ |
/* Limit sky brightness */ |
586 |
+ |
if (sky_brightness < 0.01) |
587 |
+ |
sky_brightness = 0.01; |
588 |
+ |
|
589 |
|
/* Calculate illuminance */ |
590 |
|
index = GetCategoryIndex(); |
591 |
|
diff_illum = diff_irrad * CalcDiffuseIllumRatio(index); |
597 |
|
index = CalcSkyParamFromIllum(); |
598 |
|
} |
599 |
|
|
600 |
+ |
if (output == 1) { /* hack for solar radiance */ |
601 |
+ |
diff_illum = diff_irrad * WHTEFFICACY; |
602 |
+ |
dir_illum = dir_irrad * WHTEFFICACY; |
603 |
+ |
} |
604 |
+ |
|
605 |
|
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
606 |
|
memset(parr, 0, sizeof(float)*3*nskypatch); |
607 |
|
return; |
622 |
|
/* Calculate relative horizontal illuminance */ |
623 |
|
norm_diff_illum = CalcRelHorzIllum(parr); |
624 |
|
|
625 |
+ |
/* Check for zero sky -- make uniform in that case */ |
626 |
+ |
if (norm_diff_illum <= FTINY) { |
627 |
+ |
for (i = 1; i < nskypatch; i++) |
628 |
+ |
setcolor(parr+3*i, 1., 1., 1.); |
629 |
+ |
norm_diff_illum = PI; |
630 |
+ |
} |
631 |
|
/* Normalization coefficient */ |
632 |
|
norm_diff_illum = diff_illum / norm_diff_illum; |
633 |
|
|
583 |
– |
/* Calculate relative zenith luminance */ |
584 |
– |
zlumin = CalcRelLuminance(sun_zenith, 0.0); |
585 |
– |
|
586 |
– |
/* Calculate absolute zenith illuminance */ |
587 |
– |
zlumin *= norm_diff_illum; |
588 |
– |
|
634 |
|
/* Apply to sky patches to get absolute radiance values */ |
635 |
|
for (i = 1; i < nskypatch; i++) { |
636 |
< |
scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY)); |
636 |
> |
scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY)); |
637 |
|
multcolor(parr+3*i, skycolor); |
638 |
|
} |
639 |
|
} |
650 |
|
|
651 |
|
if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4) |
652 |
|
return; |
653 |
< |
/* identify NSUNPATCH closest patches */ |
654 |
< |
for (i = NSUNPATCH; i--; ) |
653 |
> |
/* identify nsuns closest patches */ |
654 |
> |
if (nsuns > NSUNPATCH) |
655 |
> |
nsuns = NSUNPATCH; |
656 |
> |
else if (nsuns <= 0) |
657 |
> |
nsuns = 1; |
658 |
> |
for (i = nsuns; i--; ) |
659 |
|
near_dprod[i] = -1.; |
660 |
|
vector(svec, altitude, azimuth); |
661 |
|
for (p = 1; p < nskypatch; p++) { |
663 |
|
double dprod; |
664 |
|
rh_vector(pvec, p); |
665 |
|
dprod = DOT(pvec, svec); |
666 |
< |
for (i = 0; i < NSUNPATCH; i++) |
666 |
> |
for (i = 0; i < nsuns; i++) |
667 |
|
if (dprod > near_dprod[i]) { |
668 |
< |
for (j = NSUNPATCH; --j > i; ) { |
668 |
> |
for (j = nsuns; --j > i; ) { |
669 |
|
near_dprod[j] = near_dprod[j-1]; |
670 |
|
near_patch[j] = near_patch[j-1]; |
671 |
|
} |
675 |
|
} |
676 |
|
} |
677 |
|
wtot = 0; /* weight by proximity */ |
678 |
< |
for (i = NSUNPATCH; i--; ) |
678 |
> |
for (i = nsuns; i--; ) |
679 |
|
wtot += wta[i] = 1./(1.002 - near_dprod[i]); |
680 |
|
/* add to nearest patch radiances */ |
681 |
< |
for (i = NSUNPATCH; i--; ) { |
681 |
> |
for (i = nsuns; i--; ) { |
682 |
|
float *pdest = parr + 3*near_patch[i]; |
683 |
< |
float val_add = wta[i] * dir_illum / |
684 |
< |
(WHTEFFICACY * wtot * rh_dom[near_patch[i]]); |
683 |
> |
float val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot); |
684 |
> |
|
685 |
> |
val_add /= (fixed_sun_sa > 0) ? fixed_sun_sa |
686 |
> |
: rh_dom[near_patch[i]] ; |
687 |
|
*pdest++ += val_add*suncolor[0]; |
688 |
|
*pdest++ += val_add*suncolor[1]; |
689 |
|
*pdest++ += val_add*suncolor[2]; |
827 |
|
double sz_cubed; /* Sun zenith angle cubed */ |
828 |
|
|
829 |
|
/* Calculate sun zenith angle cubed */ |
830 |
< |
sz_cubed = pow(sun_zenith, 3.0); |
830 |
> |
sz_cubed = sun_zenith*sun_zenith*sun_zenith; |
831 |
|
|
832 |
|
return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 * |
833 |
|
sz_cubed) / (1.0 + 1.041 * sz_cubed); |
858 |
|
double CalcDirectIrradiance() |
859 |
|
{ |
860 |
|
return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041 |
861 |
< |
* pow(sun_zenith, 3.0))); |
861 |
> |
* sun_zenith*sun_zenith*sun_zenith)); |
862 |
|
} |
863 |
|
|
864 |
|
/* Calculate sky brightness and clearness from illuminance values */ |
884 |
|
sky_clearness = 12.0; |
885 |
|
|
886 |
|
/* Limit sky brightness */ |
887 |
< |
if (sky_brightness < 0.05) |
887 |
> |
if (sky_brightness < 0.01) |
888 |
|
sky_brightness = 0.01; |
889 |
|
|
890 |
|
while (((fabs(diff_irrad - test1) > 10.0) || |
908 |
|
sky_clearness = 12.0; |
909 |
|
|
910 |
|
/* Limit sky brightness */ |
911 |
< |
if (sky_brightness < 0.05) |
911 |
> |
if (sky_brightness < 0.01) |
912 |
|
sky_brightness = 0.01; |
913 |
|
} |
914 |
|
|
994 |
|
double rh_illum = 0.0; /* Relative horizontal illuminance */ |
995 |
|
|
996 |
|
for (i = 1; i < nskypatch; i++) |
997 |
< |
rh_illum += parr[3*i+1] * rh_cos(i); |
997 |
> |
rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i]; |
998 |
|
|
999 |
< |
return rh_illum * (2.0 * PI / (nskypatch-1)); |
999 |
> |
return rh_illum; |
1000 |
|
} |
1001 |
|
|
1002 |
|
/* Calculate earth orbit eccentricity correction factor */ |