86 |
|
#include <string.h> |
87 |
|
#include <ctype.h> |
88 |
|
#include "rtmath.h" |
89 |
+ |
#include "resolu.h" |
90 |
+ |
#include "platform.h" |
91 |
|
#include "color.h" |
92 |
+ |
#include "resolu.h" |
93 |
|
|
94 |
|
char *progname; /* Program name */ |
95 |
|
char errmsg[128]; /* Error message buffer */ |
111 |
|
double solar_rad; /* Solar radiance */ |
112 |
|
double sun_zenith; /* Sun zenith angle (radians) */ |
113 |
|
int input = 0; /* Input type */ |
114 |
+ |
int output = 0; /* Output type */ |
115 |
|
|
116 |
|
extern double dmax( double, double ); |
117 |
|
extern double CalcAirMass(); |
212 |
|
{ 1.950, 2.800 }, |
213 |
|
{ 2.800, 4.500 }, |
214 |
|
{ 4.500, 6.200 }, |
215 |
< |
{ 6.200, 12.00 } /* Clear */ |
215 |
> |
{ 6.200, 12.01 } /* Clear */ |
216 |
|
}; |
217 |
|
|
218 |
|
/* Luminous efficacy model coefficients */ |
251 |
|
}; |
252 |
|
|
253 |
|
#ifndef NSUNPATCH |
254 |
< |
#define NSUNPATCH 4 /* # patches to spread sun into */ |
254 |
> |
#define NSUNPATCH 4 /* max. # patches to spread sun into */ |
255 |
|
#endif |
256 |
|
|
257 |
|
extern int jdate(int month, int day); |
264 |
|
extern double s_longitude; |
265 |
|
extern double s_meridian; |
266 |
|
|
267 |
+ |
int nsuns = NSUNPATCH; /* number of sun patches to use */ |
268 |
+ |
double fixed_sun_sa = -1; /* fixed solid angle per sun? */ |
269 |
+ |
|
270 |
|
int verbose = 0; /* progress reports to stderr? */ |
271 |
|
|
272 |
|
int outfmt = 'a'; /* output format */ |
295 |
|
extern float * resize_dmatrix(float *mtx_data, int nsteps, int npatch); |
296 |
|
extern void AddDirect(float *parr); |
297 |
|
|
298 |
+ |
|
299 |
+ |
static const char * |
300 |
+ |
getfmtname(int fmt) |
301 |
+ |
{ |
302 |
+ |
switch (fmt) { |
303 |
+ |
case 'a': |
304 |
+ |
return("ascii"); |
305 |
+ |
case 'f': |
306 |
+ |
return("float"); |
307 |
+ |
case 'd': |
308 |
+ |
return("double"); |
309 |
+ |
} |
310 |
+ |
return("unknown"); |
311 |
+ |
} |
312 |
+ |
|
313 |
+ |
|
314 |
|
int |
315 |
|
main(int argc, char *argv[]) |
316 |
|
{ |
317 |
|
char buf[256]; |
318 |
+ |
int doheader = 1; /* output header? */ |
319 |
|
double rotation = 0; /* site rotation (degrees) */ |
320 |
|
double elevation; /* site elevation (meters) */ |
321 |
|
int dir_is_horiz; /* direct is meas. on horizontal? */ |
322 |
|
float *mtx_data = NULL; /* our matrix data */ |
323 |
|
int ntsteps = 0; /* number of rows in matrix */ |
324 |
+ |
int step_alloc = 0; |
325 |
|
int last_monthly = 0; /* month of last report */ |
326 |
|
int mo, da; /* month (1-12) and day (1-31) */ |
327 |
|
double hr; /* hour (local standard time) */ |
341 |
|
case 'v': /* verbose progress reports */ |
342 |
|
verbose++; |
343 |
|
break; |
344 |
+ |
case 'h': /* turn off header */ |
345 |
+ |
doheader = 0; |
346 |
+ |
break; |
347 |
|
case 'o': /* output format */ |
348 |
|
switch (argv[i][2]) { |
349 |
|
case 'f': |
355 |
|
goto userr; |
356 |
|
} |
357 |
|
break; |
358 |
+ |
case 'O': /* output type */ |
359 |
+ |
switch (argv[i][2]) { |
360 |
+ |
case '0': |
361 |
+ |
output = 0; |
362 |
+ |
break; |
363 |
+ |
case '1': |
364 |
+ |
output = 1; |
365 |
+ |
break; |
366 |
+ |
default: |
367 |
+ |
goto userr; |
368 |
+ |
} |
369 |
+ |
if (argv[i][3]) |
370 |
+ |
goto userr; |
371 |
+ |
break; |
372 |
|
case 'm': /* Reinhart subdivisions */ |
373 |
|
rhsubdiv = atoi(argv[++i]); |
374 |
|
break; |
392 |
|
goto userr; |
393 |
|
rotation = atof(argv[++i]); |
394 |
|
break; |
395 |
+ |
case '5': /* 5-phase calculation */ |
396 |
+ |
nsuns = 1; |
397 |
+ |
fixed_sun_sa = PI/360.*atof(argv[++i]); |
398 |
+ |
if (fixed_sun_sa <= 0) { |
399 |
+ |
fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n", |
400 |
+ |
argv[0]); |
401 |
+ |
exit(1); |
402 |
+ |
} |
403 |
+ |
fixed_sun_sa *= fixed_sun_sa*PI; |
404 |
+ |
break; |
405 |
|
default: |
406 |
|
goto userr; |
407 |
|
} |
469 |
|
double sda, sta; |
470 |
|
/* make space for next time step */ |
471 |
|
mtx_offset = 3*nskypatch*ntsteps++; |
472 |
< |
mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch); |
472 |
> |
if (ntsteps > step_alloc) { |
473 |
> |
step_alloc += (step_alloc>>1) + ntsteps + 7; |
474 |
> |
mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch); |
475 |
> |
} |
476 |
|
if (dif <= 1e-4) { |
477 |
|
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
478 |
|
continue; |
511 |
|
break; |
512 |
|
} |
513 |
|
/* write out matrix */ |
514 |
+ |
if (outfmt != 'a') |
515 |
+ |
SET_FILE_BINARY(stdout); |
516 |
|
#ifdef getc_unlocked |
517 |
|
flockfile(stdout); |
518 |
|
#endif |
519 |
|
if (verbose) |
520 |
|
fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", |
521 |
|
progname, outfmt=='a' ? "" : "binary ", ntsteps); |
522 |
+ |
if (doheader) { |
523 |
+ |
newheader("RADIANCE", stdout); |
524 |
+ |
printargs(argc, argv, stdout); |
525 |
+ |
printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude), |
526 |
+ |
-RadToDeg(s_longitude)); |
527 |
+ |
printf("NROWS=%d\n", nskypatch); |
528 |
+ |
printf("NCOLS=%d\n", ntsteps); |
529 |
+ |
printf("NCOMP=3\n"); |
530 |
+ |
fputformat((char *)getfmtname(outfmt), stdout); |
531 |
+ |
putchar('\n'); |
532 |
+ |
} |
533 |
|
/* patches are rows (outer sort) */ |
534 |
|
for (i = 0; i < nskypatch; i++) { |
535 |
|
mtx_offset = 3*i; |
571 |
|
fprintf(stderr, "%s: done.\n", progname); |
572 |
|
exit(0); |
573 |
|
userr: |
574 |
< |
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", |
574 |
> |
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", |
575 |
|
progname); |
576 |
|
exit(1); |
577 |
|
fmterr: |
630 |
|
|
631 |
|
/* Limit sky brightness */ |
632 |
|
if (sky_brightness < 0.01) |
633 |
< |
sky_brightness = 0.01; |
633 |
> |
sky_brightness = 0.01; |
634 |
|
|
635 |
|
/* Calculate illuminance */ |
636 |
|
index = GetCategoryIndex(); |
643 |
|
index = CalcSkyParamFromIllum(); |
644 |
|
} |
645 |
|
|
646 |
+ |
if (output == 1) { /* hack for solar radiance */ |
647 |
+ |
diff_illum = diff_irrad * WHTEFFICACY; |
648 |
+ |
dir_illum = dir_irrad * WHTEFFICACY; |
649 |
+ |
} |
650 |
+ |
|
651 |
|
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
652 |
|
memset(parr, 0, sizeof(float)*3*nskypatch); |
653 |
|
return; |
668 |
|
/* Calculate relative horizontal illuminance */ |
669 |
|
norm_diff_illum = CalcRelHorzIllum(parr); |
670 |
|
|
671 |
+ |
/* Check for zero sky -- make uniform in that case */ |
672 |
+ |
if (norm_diff_illum <= FTINY) { |
673 |
+ |
for (i = 1; i < nskypatch; i++) |
674 |
+ |
setcolor(parr+3*i, 1., 1., 1.); |
675 |
+ |
norm_diff_illum = PI; |
676 |
+ |
} |
677 |
|
/* Normalization coefficient */ |
678 |
|
norm_diff_illum = diff_illum / norm_diff_illum; |
679 |
|
|
696 |
|
|
697 |
|
if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4) |
698 |
|
return; |
699 |
< |
/* identify NSUNPATCH closest patches */ |
700 |
< |
for (i = NSUNPATCH; i--; ) |
699 |
> |
/* identify nsuns closest patches */ |
700 |
> |
if (nsuns > NSUNPATCH) |
701 |
> |
nsuns = NSUNPATCH; |
702 |
> |
else if (nsuns <= 0) |
703 |
> |
nsuns = 1; |
704 |
> |
for (i = nsuns; i--; ) |
705 |
|
near_dprod[i] = -1.; |
706 |
|
vector(svec, altitude, azimuth); |
707 |
|
for (p = 1; p < nskypatch; p++) { |
709 |
|
double dprod; |
710 |
|
rh_vector(pvec, p); |
711 |
|
dprod = DOT(pvec, svec); |
712 |
< |
for (i = 0; i < NSUNPATCH; i++) |
712 |
> |
for (i = 0; i < nsuns; i++) |
713 |
|
if (dprod > near_dprod[i]) { |
714 |
< |
for (j = NSUNPATCH; --j > i; ) { |
714 |
> |
for (j = nsuns; --j > i; ) { |
715 |
|
near_dprod[j] = near_dprod[j-1]; |
716 |
|
near_patch[j] = near_patch[j-1]; |
717 |
|
} |
721 |
|
} |
722 |
|
} |
723 |
|
wtot = 0; /* weight by proximity */ |
724 |
< |
for (i = NSUNPATCH; i--; ) |
724 |
> |
for (i = nsuns; i--; ) |
725 |
|
wtot += wta[i] = 1./(1.002 - near_dprod[i]); |
726 |
|
/* add to nearest patch radiances */ |
727 |
< |
for (i = NSUNPATCH; i--; ) { |
727 |
> |
for (i = nsuns; i--; ) { |
728 |
|
float *pdest = parr + 3*near_patch[i]; |
729 |
< |
float val_add = wta[i] * dir_illum / |
730 |
< |
(WHTEFFICACY * wtot * rh_dom[near_patch[i]]); |
729 |
> |
float val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot); |
730 |
> |
|
731 |
> |
val_add /= (fixed_sun_sa > 0) ? fixed_sun_sa |
732 |
> |
: rh_dom[near_patch[i]] ; |
733 |
|
*pdest++ += val_add*suncolor[0]; |
734 |
|
*pdest++ += val_add*suncolor[1]; |
735 |
|
*pdest++ += val_add*suncolor[2]; |
873 |
|
double sz_cubed; /* Sun zenith angle cubed */ |
874 |
|
|
875 |
|
/* Calculate sun zenith angle cubed */ |
876 |
< |
sz_cubed = pow(sun_zenith, 3.0); |
876 |
> |
sz_cubed = sun_zenith*sun_zenith*sun_zenith; |
877 |
|
|
878 |
|
return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 * |
879 |
|
sz_cubed) / (1.0 + 1.041 * sz_cubed); |
904 |
|
double CalcDirectIrradiance() |
905 |
|
{ |
906 |
|
return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041 |
907 |
< |
* pow(sun_zenith, 3.0))); |
907 |
> |
* sun_zenith*sun_zenith*sun_zenith)); |
908 |
|
} |
909 |
|
|
910 |
|
/* Calculate sky brightness and clearness from illuminance values */ |