86 |
|
#include <string.h> |
87 |
|
#include <ctype.h> |
88 |
|
#include "rtmath.h" |
89 |
+ |
#include "platform.h" |
90 |
|
#include "color.h" |
91 |
|
|
92 |
|
char *progname; /* Program name */ |
109 |
|
double solar_rad; /* Solar radiance */ |
110 |
|
double sun_zenith; /* Sun zenith angle (radians) */ |
111 |
|
int input = 0; /* Input type */ |
112 |
+ |
int output = 0; /* Output type */ |
113 |
|
|
114 |
|
extern double dmax( double, double ); |
115 |
|
extern double CalcAirMass(); |
210 |
|
{ 1.950, 2.800 }, |
211 |
|
{ 2.800, 4.500 }, |
212 |
|
{ 4.500, 6.200 }, |
213 |
< |
{ 6.200, 12.00 } /* Clear */ |
213 |
> |
{ 6.200, 12.01 } /* Clear */ |
214 |
|
}; |
215 |
|
|
216 |
|
/* Luminous efficacy model coefficients */ |
249 |
|
}; |
250 |
|
|
251 |
|
#ifndef NSUNPATCH |
252 |
< |
#define NSUNPATCH 4 /* # patches to spread sun into */ |
252 |
> |
#define NSUNPATCH 4 /* max. # patches to spread sun into */ |
253 |
|
#endif |
254 |
|
|
255 |
|
extern int jdate(int month, int day); |
262 |
|
extern double s_longitude; |
263 |
|
extern double s_meridian; |
264 |
|
|
265 |
+ |
int nsuns = NSUNPATCH; /* number of sun patches to use */ |
266 |
+ |
double fixed_sun_sa = -1; /* fixed solid angle per sun? */ |
267 |
+ |
|
268 |
|
int verbose = 0; /* progress reports to stderr? */ |
269 |
|
|
270 |
|
int outfmt = 'a'; /* output format */ |
293 |
|
extern float * resize_dmatrix(float *mtx_data, int nsteps, int npatch); |
294 |
|
extern void AddDirect(float *parr); |
295 |
|
|
296 |
+ |
|
297 |
+ |
static const char * |
298 |
+ |
getfmtname(int fmt) |
299 |
+ |
{ |
300 |
+ |
switch (fmt) { |
301 |
+ |
case 'a': |
302 |
+ |
return("ascii"); |
303 |
+ |
case 'f': |
304 |
+ |
return("float"); |
305 |
+ |
case 'd': |
306 |
+ |
return("double"); |
307 |
+ |
} |
308 |
+ |
return("unknown"); |
309 |
+ |
} |
310 |
+ |
|
311 |
+ |
|
312 |
|
int |
313 |
|
main(int argc, char *argv[]) |
314 |
|
{ |
315 |
|
char buf[256]; |
316 |
+ |
int doheader = 1; /* output header? */ |
317 |
|
double rotation = 0; /* site rotation (degrees) */ |
318 |
|
double elevation; /* site elevation (meters) */ |
319 |
|
int dir_is_horiz; /* direct is meas. on horizontal? */ |
338 |
|
case 'v': /* verbose progress reports */ |
339 |
|
verbose++; |
340 |
|
break; |
341 |
+ |
case 'h': /* turn off header */ |
342 |
+ |
doheader = 0; |
343 |
+ |
break; |
344 |
|
case 'o': /* output format */ |
345 |
|
switch (argv[i][2]) { |
346 |
|
case 'f': |
352 |
|
goto userr; |
353 |
|
} |
354 |
|
break; |
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; |
389 |
|
goto userr; |
390 |
|
rotation = atof(argv[++i]); |
391 |
|
break; |
392 |
+ |
case '5': /* 5-phase calculation */ |
393 |
+ |
nsuns = 1; |
394 |
+ |
fixed_sun_sa = 6.797e-05; |
395 |
+ |
break; |
396 |
|
default: |
397 |
|
goto userr; |
398 |
|
} |
457 |
|
s_meridian = DegToRad(s_meridian); |
458 |
|
/* process each time step in tape */ |
459 |
|
while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) { |
460 |
+ |
static int step_alloc = 0; |
461 |
|
double sda, sta; |
462 |
|
/* make space for next time step */ |
463 |
|
mtx_offset = 3*nskypatch*ntsteps++; |
464 |
< |
mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch); |
464 |
> |
if (ntsteps > step_alloc) { |
465 |
> |
step_alloc += step_alloc>>1 + 8; |
466 |
> |
mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch); |
467 |
> |
} |
468 |
|
if (dif <= 1e-4) { |
469 |
|
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
470 |
|
continue; |
503 |
|
break; |
504 |
|
} |
505 |
|
/* write out matrix */ |
506 |
+ |
if (outfmt != 'a') |
507 |
+ |
SET_FILE_BINARY(stdout); |
508 |
|
#ifdef getc_unlocked |
509 |
|
flockfile(stdout); |
510 |
|
#endif |
511 |
|
if (verbose) |
512 |
|
fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", |
513 |
|
progname, outfmt=='a' ? "" : "binary ", ntsteps); |
514 |
+ |
if (doheader) { |
515 |
+ |
newheader("RADIANCE", stdout); |
516 |
+ |
printargs(argc, argv, stdout); |
517 |
+ |
printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude), |
518 |
+ |
-RadToDeg(s_longitude)); |
519 |
+ |
printf("NROWS=%d\n", nskypatch); |
520 |
+ |
printf("NCOLS=%d\n", ntsteps); |
521 |
+ |
printf("NCOMP=3\n"); |
522 |
+ |
fputformat(getfmtname(outfmt), stdout); |
523 |
+ |
putchar('\n'); |
524 |
+ |
} |
525 |
|
/* patches are rows (outer sort) */ |
526 |
|
for (i = 0; i < nskypatch; i++) { |
527 |
|
mtx_offset = 3*i; |
563 |
|
fprintf(stderr, "%s: done.\n", progname); |
564 |
|
exit(0); |
565 |
|
userr: |
566 |
< |
fprintf(stderr, "Usage: %s [-v][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}] [tape.wea]\n", |
566 |
> |
fprintf(stderr, "Usage: %s [-v][-h][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n", |
567 |
|
progname); |
568 |
|
exit(1); |
569 |
|
fmterr: |
616 |
|
sky_brightness = CalcSkyBrightness(); |
617 |
|
sky_clearness = CalcSkyClearness(); |
618 |
|
|
619 |
+ |
/* Limit sky clearness */ |
620 |
+ |
if (sky_clearness > 11.9) |
621 |
+ |
sky_clearness = 11.9; |
622 |
+ |
|
623 |
+ |
/* Limit sky brightness */ |
624 |
+ |
if (sky_brightness < 0.01) |
625 |
+ |
sky_brightness = 0.01; |
626 |
+ |
|
627 |
|
/* Calculate illuminance */ |
628 |
|
index = GetCategoryIndex(); |
629 |
|
diff_illum = diff_irrad * CalcDiffuseIllumRatio(index); |
635 |
|
index = CalcSkyParamFromIllum(); |
636 |
|
} |
637 |
|
|
638 |
+ |
if (output == 1) { /* hack for solar radiance */ |
639 |
+ |
diff_illum = diff_irrad * WHTEFFICACY; |
640 |
+ |
dir_illum = dir_irrad * WHTEFFICACY; |
641 |
+ |
} |
642 |
+ |
|
643 |
|
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
644 |
|
memset(parr, 0, sizeof(float)*3*nskypatch); |
645 |
|
return; |
660 |
|
/* Calculate relative horizontal illuminance */ |
661 |
|
norm_diff_illum = CalcRelHorzIllum(parr); |
662 |
|
|
663 |
+ |
/* Check for zero sky -- make uniform in that case */ |
664 |
+ |
if (norm_diff_illum <= FTINY) { |
665 |
+ |
for (i = 1; i < nskypatch; i++) |
666 |
+ |
setcolor(parr+3*i, 1., 1., 1.); |
667 |
+ |
norm_diff_illum = PI; |
668 |
+ |
} |
669 |
|
/* Normalization coefficient */ |
670 |
|
norm_diff_illum = diff_illum / norm_diff_illum; |
671 |
|
|
688 |
|
|
689 |
|
if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4) |
690 |
|
return; |
691 |
< |
/* identify NSUNPATCH closest patches */ |
692 |
< |
for (i = NSUNPATCH; i--; ) |
691 |
> |
/* identify nsuns closest patches */ |
692 |
> |
if (nsuns > NSUNPATCH) |
693 |
> |
nsuns = NSUNPATCH; |
694 |
> |
else if (nsuns <= 0) |
695 |
> |
nsuns = 1; |
696 |
> |
for (i = nsuns; i--; ) |
697 |
|
near_dprod[i] = -1.; |
698 |
|
vector(svec, altitude, azimuth); |
699 |
|
for (p = 1; p < nskypatch; p++) { |
701 |
|
double dprod; |
702 |
|
rh_vector(pvec, p); |
703 |
|
dprod = DOT(pvec, svec); |
704 |
< |
for (i = 0; i < NSUNPATCH; i++) |
704 |
> |
for (i = 0; i < nsuns; i++) |
705 |
|
if (dprod > near_dprod[i]) { |
706 |
< |
for (j = NSUNPATCH; --j > i; ) { |
706 |
> |
for (j = nsuns; --j > i; ) { |
707 |
|
near_dprod[j] = near_dprod[j-1]; |
708 |
|
near_patch[j] = near_patch[j-1]; |
709 |
|
} |
713 |
|
} |
714 |
|
} |
715 |
|
wtot = 0; /* weight by proximity */ |
716 |
< |
for (i = NSUNPATCH; i--; ) |
716 |
> |
for (i = nsuns; i--; ) |
717 |
|
wtot += wta[i] = 1./(1.002 - near_dprod[i]); |
718 |
|
/* add to nearest patch radiances */ |
719 |
< |
for (i = NSUNPATCH; i--; ) { |
719 |
> |
for (i = nsuns; i--; ) { |
720 |
|
float *pdest = parr + 3*near_patch[i]; |
721 |
< |
float val_add = wta[i] * dir_illum / |
722 |
< |
(WHTEFFICACY * wtot * rh_dom[near_patch[i]]); |
721 |
> |
float val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot); |
722 |
> |
|
723 |
> |
val_add /= (fixed_sun_sa > 0) ? fixed_sun_sa |
724 |
> |
: rh_dom[near_patch[i]] ; |
725 |
|
*pdest++ += val_add*suncolor[0]; |
726 |
|
*pdest++ += val_add*suncolor[1]; |
727 |
|
*pdest++ += val_add*suncolor[2]; |
865 |
|
double sz_cubed; /* Sun zenith angle cubed */ |
866 |
|
|
867 |
|
/* Calculate sun zenith angle cubed */ |
868 |
< |
sz_cubed = pow(sun_zenith, 3.0); |
868 |
> |
sz_cubed = sun_zenith*sun_zenith*sun_zenith; |
869 |
|
|
870 |
|
return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 * |
871 |
|
sz_cubed) / (1.0 + 1.041 * sz_cubed); |
896 |
|
double CalcDirectIrradiance() |
897 |
|
{ |
898 |
|
return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041 |
899 |
< |
* pow(sun_zenith, 3.0))); |
899 |
> |
* sun_zenith*sun_zenith*sun_zenith)); |
900 |
|
} |
901 |
|
|
902 |
|
/* Calculate sky brightness and clearness from illuminance values */ |
922 |
|
sky_clearness = 12.0; |
923 |
|
|
924 |
|
/* Limit sky brightness */ |
925 |
< |
if (sky_brightness < 0.05) |
925 |
> |
if (sky_brightness < 0.01) |
926 |
|
sky_brightness = 0.01; |
927 |
|
|
928 |
|
while (((fabs(diff_irrad - test1) > 10.0) || |
946 |
|
sky_clearness = 12.0; |
947 |
|
|
948 |
|
/* Limit sky brightness */ |
949 |
< |
if (sky_brightness < 0.05) |
949 |
> |
if (sky_brightness < 0.01) |
950 |
|
sky_brightness = 0.01; |
951 |
|
} |
952 |
|
|