86 |
|
#include <string.h> |
87 |
|
#include <ctype.h> |
88 |
|
#include "rtmath.h" |
89 |
+ |
#include "rtio.h" |
90 |
+ |
#include "resolu.h" |
91 |
+ |
#include "platform.h" |
92 |
|
#include "color.h" |
93 |
+ |
#include "resolu.h" |
94 |
|
|
95 |
|
char *progname; /* Program name */ |
96 |
|
char errmsg[128]; /* Error message buffer */ |
112 |
|
double solar_rad; /* Solar radiance */ |
113 |
|
double sun_zenith; /* Sun zenith angle (radians) */ |
114 |
|
int input = 0; /* Input type */ |
115 |
+ |
int output = 0; /* Output type */ |
116 |
|
|
117 |
|
extern double dmax( double, double ); |
118 |
|
extern double CalcAirMass(); |
213 |
|
{ 1.950, 2.800 }, |
214 |
|
{ 2.800, 4.500 }, |
215 |
|
{ 4.500, 6.200 }, |
216 |
< |
{ 6.200, 12.00 } /* Clear */ |
216 |
> |
{ 6.200, 12.01 } /* Clear */ |
217 |
|
}; |
218 |
|
|
219 |
|
/* Luminous efficacy model coefficients */ |
252 |
|
}; |
253 |
|
|
254 |
|
#ifndef NSUNPATCH |
255 |
< |
#define NSUNPATCH 4 /* # patches to spread sun into */ |
255 |
> |
#define NSUNPATCH 4 /* max. # patches to spread sun into */ |
256 |
|
#endif |
257 |
|
|
258 |
|
extern int jdate(int month, int day); |
265 |
|
extern double s_longitude; |
266 |
|
extern double s_meridian; |
267 |
|
|
268 |
+ |
int nsuns = NSUNPATCH; /* number of sun patches to use */ |
269 |
+ |
double fixed_sun_sa = -1; /* fixed solid angle per sun? */ |
270 |
+ |
|
271 |
|
int verbose = 0; /* progress reports to stderr? */ |
272 |
|
|
273 |
|
int outfmt = 'a'; /* output format */ |
296 |
|
extern float * resize_dmatrix(float *mtx_data, int nsteps, int npatch); |
297 |
|
extern void AddDirect(float *parr); |
298 |
|
|
299 |
+ |
|
300 |
+ |
static const char * |
301 |
+ |
getfmtname(int fmt) |
302 |
+ |
{ |
303 |
+ |
switch (fmt) { |
304 |
+ |
case 'a': |
305 |
+ |
return("ascii"); |
306 |
+ |
case 'f': |
307 |
+ |
return("float"); |
308 |
+ |
case 'd': |
309 |
+ |
return("double"); |
310 |
+ |
} |
311 |
+ |
return("unknown"); |
312 |
+ |
} |
313 |
+ |
|
314 |
+ |
|
315 |
|
int |
316 |
|
main(int argc, char *argv[]) |
317 |
|
{ |
318 |
|
char buf[256]; |
319 |
+ |
int doheader = 1; /* output header? */ |
320 |
|
double rotation = 0; /* site rotation (degrees) */ |
321 |
|
double elevation; /* site elevation (meters) */ |
322 |
|
int dir_is_horiz; /* direct is meas. on horizontal? */ |
323 |
|
float *mtx_data = NULL; /* our matrix data */ |
324 |
|
int ntsteps = 0; /* number of rows in matrix */ |
325 |
+ |
int step_alloc = 0; |
326 |
|
int last_monthly = 0; /* month of last report */ |
327 |
+ |
int inconsistent = 0; /* inconsistent options set? */ |
328 |
|
int mo, da; /* month (1-12) and day (1-31) */ |
329 |
|
double hr; /* hour (local standard time) */ |
330 |
|
double dir, dif; /* direct and diffuse values */ |
343 |
|
case 'v': /* verbose progress reports */ |
344 |
|
verbose++; |
345 |
|
break; |
346 |
+ |
case 'h': /* turn off header */ |
347 |
+ |
doheader = 0; |
348 |
+ |
break; |
349 |
|
case 'o': /* output format */ |
350 |
|
switch (argv[i][2]) { |
351 |
|
case 'f': |
357 |
|
goto userr; |
358 |
|
} |
359 |
|
break; |
360 |
+ |
case 'O': /* output type */ |
361 |
+ |
switch (argv[i][2]) { |
362 |
+ |
case '0': |
363 |
+ |
output = 0; |
364 |
+ |
break; |
365 |
+ |
case '1': |
366 |
+ |
output = 1; |
367 |
+ |
break; |
368 |
+ |
default: |
369 |
+ |
goto userr; |
370 |
+ |
} |
371 |
+ |
if (argv[i][3]) |
372 |
+ |
goto userr; |
373 |
+ |
break; |
374 |
|
case 'm': /* Reinhart subdivisions */ |
375 |
|
rhsubdiv = atoi(argv[++i]); |
376 |
|
break; |
377 |
|
case 'c': /* sky color */ |
378 |
+ |
inconsistent |= (skycolor[1] <= 1e-4); |
379 |
|
skycolor[0] = atof(argv[++i]); |
380 |
|
skycolor[1] = atof(argv[++i]); |
381 |
|
skycolor[2] = atof(argv[++i]); |
382 |
|
break; |
383 |
|
case 'd': /* solar (direct) only */ |
384 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 0; |
385 |
< |
if (suncolor[1] <= 1e-4) |
385 |
> |
if (suncolor[1] <= 1e-4) { |
386 |
> |
inconsistent = 1; |
387 |
|
suncolor[0] = suncolor[1] = suncolor[2] = 1; |
388 |
+ |
} |
389 |
|
break; |
390 |
|
case 's': /* sky only (no direct) */ |
391 |
|
suncolor[0] = suncolor[1] = suncolor[2] = 0; |
392 |
< |
if (skycolor[1] <= 1e-4) |
392 |
> |
if (skycolor[1] <= 1e-4) { |
393 |
> |
inconsistent = 1; |
394 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 1; |
395 |
+ |
} |
396 |
|
break; |
397 |
|
case 'r': /* rotate distribution */ |
398 |
|
if (argv[i][2] && argv[i][2] != 'z') |
399 |
|
goto userr; |
400 |
|
rotation = atof(argv[++i]); |
401 |
|
break; |
402 |
+ |
case '5': /* 5-phase calculation */ |
403 |
+ |
nsuns = 1; |
404 |
+ |
fixed_sun_sa = PI/360.*atof(argv[++i]); |
405 |
+ |
if (fixed_sun_sa <= 0) { |
406 |
+ |
fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n", |
407 |
+ |
argv[0]); |
408 |
+ |
exit(1); |
409 |
+ |
} |
410 |
+ |
fixed_sun_sa *= fixed_sun_sa*PI; |
411 |
+ |
break; |
412 |
|
default: |
413 |
|
goto userr; |
414 |
|
} |
415 |
|
if (i < argc-1) |
416 |
|
goto userr; |
417 |
+ |
if (inconsistent) |
418 |
+ |
fprintf(stderr, "%s: WARNING: inconsistent -s, -d, -c options!\n", |
419 |
+ |
progname); |
420 |
|
if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
421 |
|
fprintf(stderr, "%s: cannot open '%s' for input\n", |
422 |
|
progname, argv[i]); |
479 |
|
double sda, sta; |
480 |
|
/* make space for next time step */ |
481 |
|
mtx_offset = 3*nskypatch*ntsteps++; |
482 |
< |
mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch); |
482 |
> |
if (ntsteps > step_alloc) { |
483 |
> |
step_alloc += (step_alloc>>1) + ntsteps + 7; |
484 |
> |
mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch); |
485 |
> |
} |
486 |
|
if (dif <= 1e-4) { |
487 |
|
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
488 |
|
continue; |
521 |
|
break; |
522 |
|
} |
523 |
|
/* write out matrix */ |
524 |
+ |
if (outfmt != 'a') |
525 |
+ |
SET_FILE_BINARY(stdout); |
526 |
|
#ifdef getc_unlocked |
527 |
|
flockfile(stdout); |
528 |
|
#endif |
529 |
|
if (verbose) |
530 |
|
fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", |
531 |
|
progname, outfmt=='a' ? "" : "binary ", ntsteps); |
532 |
+ |
if (doheader) { |
533 |
+ |
newheader("RADIANCE", stdout); |
534 |
+ |
printargs(argc, argv, stdout); |
535 |
+ |
printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude), |
536 |
+ |
-RadToDeg(s_longitude)); |
537 |
+ |
printf("NROWS=%d\n", nskypatch); |
538 |
+ |
printf("NCOLS=%d\n", ntsteps); |
539 |
+ |
printf("NCOMP=3\n"); |
540 |
+ |
fputformat((char *)getfmtname(outfmt), stdout); |
541 |
+ |
putchar('\n'); |
542 |
+ |
} |
543 |
|
/* patches are rows (outer sort) */ |
544 |
|
for (i = 0; i < nskypatch; i++) { |
545 |
|
mtx_offset = 3*i; |
556 |
|
break; |
557 |
|
case 'f': |
558 |
|
for (j = 0; j < ntsteps; j++) { |
559 |
< |
fwrite(mtx_data+mtx_offset, sizeof(float), 3, |
559 |
> |
putbinary(mtx_data+mtx_offset, sizeof(float), 3, |
560 |
|
stdout); |
561 |
|
mtx_offset += 3*nskypatch; |
562 |
|
} |
567 |
|
ment[0] = mtx_data[mtx_offset]; |
568 |
|
ment[1] = mtx_data[mtx_offset+1]; |
569 |
|
ment[2] = mtx_data[mtx_offset+2]; |
570 |
< |
fwrite(ment, sizeof(double), 3, stdout); |
570 |
> |
putbinary(ment, sizeof(double), 3, stdout); |
571 |
|
mtx_offset += 3*nskypatch; |
572 |
|
} |
573 |
|
break; |
581 |
|
fprintf(stderr, "%s: done.\n", progname); |
582 |
|
exit(0); |
583 |
|
userr: |
584 |
< |
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", |
584 |
> |
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", |
585 |
|
progname); |
586 |
|
exit(1); |
587 |
|
fmterr: |
634 |
|
sky_brightness = CalcSkyBrightness(); |
635 |
|
sky_clearness = CalcSkyClearness(); |
636 |
|
|
637 |
+ |
/* Limit sky clearness */ |
638 |
+ |
if (sky_clearness > 11.9) |
639 |
+ |
sky_clearness = 11.9; |
640 |
+ |
|
641 |
+ |
/* Limit sky brightness */ |
642 |
+ |
if (sky_brightness < 0.01) |
643 |
+ |
sky_brightness = 0.01; |
644 |
+ |
|
645 |
|
/* Calculate illuminance */ |
646 |
|
index = GetCategoryIndex(); |
647 |
|
diff_illum = diff_irrad * CalcDiffuseIllumRatio(index); |
653 |
|
index = CalcSkyParamFromIllum(); |
654 |
|
} |
655 |
|
|
656 |
+ |
if (output == 1) { /* hack for solar radiance */ |
657 |
+ |
diff_illum = diff_irrad * WHTEFFICACY; |
658 |
+ |
dir_illum = dir_irrad * WHTEFFICACY; |
659 |
+ |
} |
660 |
+ |
|
661 |
|
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
662 |
|
memset(parr, 0, sizeof(float)*3*nskypatch); |
663 |
|
return; |
678 |
|
/* Calculate relative horizontal illuminance */ |
679 |
|
norm_diff_illum = CalcRelHorzIllum(parr); |
680 |
|
|
681 |
+ |
/* Check for zero sky -- make uniform in that case */ |
682 |
+ |
if (norm_diff_illum <= FTINY) { |
683 |
+ |
for (i = 1; i < nskypatch; i++) |
684 |
+ |
setcolor(parr+3*i, 1., 1., 1.); |
685 |
+ |
norm_diff_illum = PI; |
686 |
+ |
} |
687 |
|
/* Normalization coefficient */ |
688 |
|
norm_diff_illum = diff_illum / norm_diff_illum; |
689 |
|
|
706 |
|
|
707 |
|
if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4) |
708 |
|
return; |
709 |
< |
/* identify NSUNPATCH closest patches */ |
710 |
< |
for (i = NSUNPATCH; i--; ) |
709 |
> |
/* identify nsuns closest patches */ |
710 |
> |
if (nsuns > NSUNPATCH) |
711 |
> |
nsuns = NSUNPATCH; |
712 |
> |
else if (nsuns <= 0) |
713 |
> |
nsuns = 1; |
714 |
> |
for (i = nsuns; i--; ) |
715 |
|
near_dprod[i] = -1.; |
716 |
|
vector(svec, altitude, azimuth); |
717 |
|
for (p = 1; p < nskypatch; p++) { |
719 |
|
double dprod; |
720 |
|
rh_vector(pvec, p); |
721 |
|
dprod = DOT(pvec, svec); |
722 |
< |
for (i = 0; i < NSUNPATCH; i++) |
722 |
> |
for (i = 0; i < nsuns; i++) |
723 |
|
if (dprod > near_dprod[i]) { |
724 |
< |
for (j = NSUNPATCH; --j > i; ) { |
724 |
> |
for (j = nsuns; --j > i; ) { |
725 |
|
near_dprod[j] = near_dprod[j-1]; |
726 |
|
near_patch[j] = near_patch[j-1]; |
727 |
|
} |
731 |
|
} |
732 |
|
} |
733 |
|
wtot = 0; /* weight by proximity */ |
734 |
< |
for (i = NSUNPATCH; i--; ) |
734 |
> |
for (i = nsuns; i--; ) |
735 |
|
wtot += wta[i] = 1./(1.002 - near_dprod[i]); |
736 |
|
/* add to nearest patch radiances */ |
737 |
< |
for (i = NSUNPATCH; i--; ) { |
737 |
> |
for (i = nsuns; i--; ) { |
738 |
|
float *pdest = parr + 3*near_patch[i]; |
739 |
< |
float val_add = wta[i] * dir_illum / |
740 |
< |
(WHTEFFICACY * wtot * rh_dom[near_patch[i]]); |
739 |
> |
float val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot); |
740 |
> |
|
741 |
> |
val_add /= (fixed_sun_sa > 0) ? fixed_sun_sa |
742 |
> |
: rh_dom[near_patch[i]] ; |
743 |
|
*pdest++ += val_add*suncolor[0]; |
744 |
|
*pdest++ += val_add*suncolor[1]; |
745 |
|
*pdest++ += val_add*suncolor[2]; |
883 |
|
double sz_cubed; /* Sun zenith angle cubed */ |
884 |
|
|
885 |
|
/* Calculate sun zenith angle cubed */ |
886 |
< |
sz_cubed = pow(sun_zenith, 3.0); |
886 |
> |
sz_cubed = sun_zenith*sun_zenith*sun_zenith; |
887 |
|
|
888 |
|
return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 * |
889 |
|
sz_cubed) / (1.0 + 1.041 * sz_cubed); |
914 |
|
double CalcDirectIrradiance() |
915 |
|
{ |
916 |
|
return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041 |
917 |
< |
* pow(sun_zenith, 3.0))); |
917 |
> |
* sun_zenith*sun_zenith*sun_zenith)); |
918 |
|
} |
919 |
|
|
920 |
|
/* Calculate sky brightness and clearness from illuminance values */ |
940 |
|
sky_clearness = 12.0; |
941 |
|
|
942 |
|
/* Limit sky brightness */ |
943 |
< |
if (sky_brightness < 0.05) |
943 |
> |
if (sky_brightness < 0.01) |
944 |
|
sky_brightness = 0.01; |
945 |
|
|
946 |
|
while (((fabs(diff_irrad - test1) > 10.0) || |
964 |
|
sky_clearness = 12.0; |
965 |
|
|
966 |
|
/* Limit sky brightness */ |
967 |
< |
if (sky_brightness < 0.05) |
967 |
> |
if (sky_brightness < 0.01) |
968 |
|
sky_brightness = 0.01; |
969 |
|
} |
970 |
|
|
1129 |
|
double sspa; /* Sun-sky point angle */ |
1130 |
|
double zsa; /* Zenithal sun angle */ |
1131 |
|
|
1132 |
+ |
fprintf(stderr, "Alt, azi: %f %f\n", altitude, azimuth); |
1133 |
+ |
fprintf(stderr, "Perez parameters: %f %f %f %f %f\n", |
1134 |
+ |
perez_param[0], perez_param[1], perez_param[2], perez_param[3], perez_param[4]); |
1135 |
+ |
|
1136 |
|
for (i = 1; i < nskypatch; i++) |
1137 |
|
{ |
1138 |
|
/* Calculate sun-sky point azimuthal angle */ |
1147 |
|
|
1148 |
|
/* Calculate patch luminance */ |
1149 |
|
parr[3*i] = CalcRelLuminance(sspa, zsa); |
1150 |
+ |
fprintf(stderr, "CalcRelLuminance(%f, %f) = %f\n", sspa, zsa, parr[3*i]); |
1151 |
|
if (parr[3*i] < 0) parr[3*i] = 0; |
1152 |
|
parr[3*i+2] = parr[3*i+1] = parr[3*i]; |
1153 |
|
} |