86 |
|
#include <string.h> |
87 |
|
#include <ctype.h> |
88 |
|
#include "rtmath.h" |
89 |
+ |
#include "platform.h" |
90 |
|
#include "color.h" |
91 |
+ |
#include "resolu.h" |
92 |
|
|
93 |
|
char *progname; /* Program name */ |
94 |
|
char errmsg[128]; /* Error message buffer */ |
110 |
|
double solar_rad; /* Solar radiance */ |
111 |
|
double sun_zenith; /* Sun zenith angle (radians) */ |
112 |
|
int input = 0; /* Input type */ |
113 |
+ |
int output = 0; /* Output type */ |
114 |
|
|
115 |
|
extern double dmax( double, double ); |
116 |
|
extern double CalcAirMass(); |
211 |
|
{ 1.950, 2.800 }, |
212 |
|
{ 2.800, 4.500 }, |
213 |
|
{ 4.500, 6.200 }, |
214 |
< |
{ 6.200, 12.00 } /* Clear */ |
214 |
> |
{ 6.200, 12.01 } /* Clear */ |
215 |
|
}; |
216 |
|
|
217 |
|
/* Luminous efficacy model coefficients */ |
250 |
|
}; |
251 |
|
|
252 |
|
#ifndef NSUNPATCH |
253 |
< |
#define NSUNPATCH 4 /* # patches to spread sun into */ |
253 |
> |
#define NSUNPATCH 4 /* max. # patches to spread sun into */ |
254 |
|
#endif |
255 |
|
|
256 |
|
extern int jdate(int month, int day); |
263 |
|
extern double s_longitude; |
264 |
|
extern double s_meridian; |
265 |
|
|
266 |
+ |
int nsuns = NSUNPATCH; /* number of sun patches to use */ |
267 |
+ |
double fixed_sun_sa = -1; /* fixed solid angle per sun? */ |
268 |
+ |
|
269 |
|
int verbose = 0; /* progress reports to stderr? */ |
270 |
|
|
271 |
|
int outfmt = 'a'; /* output format */ |
294 |
|
extern float * resize_dmatrix(float *mtx_data, int nsteps, int npatch); |
295 |
|
extern void AddDirect(float *parr); |
296 |
|
|
297 |
+ |
|
298 |
+ |
static const char * |
299 |
+ |
getfmtname(int fmt) |
300 |
+ |
{ |
301 |
+ |
switch (fmt) { |
302 |
+ |
case 'a': |
303 |
+ |
return("ascii"); |
304 |
+ |
case 'f': |
305 |
+ |
return("float"); |
306 |
+ |
case 'd': |
307 |
+ |
return("double"); |
308 |
+ |
} |
309 |
+ |
return("unknown"); |
310 |
+ |
} |
311 |
+ |
|
312 |
+ |
|
313 |
|
int |
314 |
|
main(int argc, char *argv[]) |
315 |
|
{ |
316 |
|
char buf[256]; |
317 |
+ |
int doheader = 1; /* output header? */ |
318 |
+ |
double rotation = 0; /* site rotation (degrees) */ |
319 |
|
double elevation; /* site elevation (meters) */ |
320 |
|
int dir_is_horiz; /* direct is meas. on horizontal? */ |
321 |
|
float *mtx_data = NULL; /* our matrix data */ |
322 |
|
int ntsteps = 0; /* number of rows in matrix */ |
323 |
+ |
int step_alloc = 0; |
324 |
|
int last_monthly = 0; /* month of last report */ |
325 |
|
int mo, da; /* month (1-12) and day (1-31) */ |
326 |
|
double hr; /* hour (local standard time) */ |
340 |
|
case 'v': /* verbose progress reports */ |
341 |
|
verbose++; |
342 |
|
break; |
343 |
+ |
case 'h': /* turn off header */ |
344 |
+ |
doheader = 0; |
345 |
+ |
break; |
346 |
|
case 'o': /* output format */ |
347 |
|
switch (argv[i][2]) { |
348 |
|
case 'f': |
354 |
|
goto userr; |
355 |
|
} |
356 |
|
break; |
357 |
+ |
case 'O': /* output type */ |
358 |
+ |
switch (argv[i][2]) { |
359 |
+ |
case '0': |
360 |
+ |
output = 0; |
361 |
+ |
break; |
362 |
+ |
case '1': |
363 |
+ |
output = 1; |
364 |
+ |
break; |
365 |
+ |
default: |
366 |
+ |
goto userr; |
367 |
+ |
} |
368 |
+ |
if (argv[i][3]) |
369 |
+ |
goto userr; |
370 |
+ |
break; |
371 |
|
case 'm': /* Reinhart subdivisions */ |
372 |
|
rhsubdiv = atoi(argv[++i]); |
373 |
|
break; |
386 |
|
if (skycolor[1] <= 1e-4) |
387 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 1; |
388 |
|
break; |
389 |
+ |
case 'r': /* rotate distribution */ |
390 |
+ |
if (argv[i][2] && argv[i][2] != 'z') |
391 |
+ |
goto userr; |
392 |
+ |
rotation = atof(argv[++i]); |
393 |
+ |
break; |
394 |
+ |
case '5': /* 5-phase calculation */ |
395 |
+ |
nsuns = 1; |
396 |
+ |
fixed_sun_sa = PI/360.*atof(argv[++i]); |
397 |
+ |
fixed_sun_sa *= fixed_sun_sa*PI; |
398 |
+ |
break; |
399 |
|
default: |
400 |
|
goto userr; |
401 |
|
} |
450 |
|
progname, s_latitude, s_longitude); |
451 |
|
fprintf(stderr, "%s: %d sky patches per time step\n", |
452 |
|
progname, nskypatch); |
453 |
+ |
if (rotation != 0) |
454 |
+ |
fprintf(stderr, "%s: rotating output %.0f degrees\n", |
455 |
+ |
progname, rotation); |
456 |
|
} |
457 |
|
/* convert quantities to radians */ |
458 |
|
s_latitude = DegToRad(s_latitude); |
463 |
|
double sda, sta; |
464 |
|
/* make space for next time step */ |
465 |
|
mtx_offset = 3*nskypatch*ntsteps++; |
466 |
< |
mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch); |
466 |
> |
if (ntsteps > step_alloc) { |
467 |
> |
step_alloc += (step_alloc>>1) + ntsteps + 7; |
468 |
> |
mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch); |
469 |
> |
} |
470 |
|
if (dif <= 1e-4) { |
471 |
|
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
472 |
|
continue; |
479 |
|
sda = sdec(julian_date); |
480 |
|
sta = stadj(julian_date); |
481 |
|
altitude = salt(sda, hr+sta); |
482 |
< |
azimuth = sazi(sda, hr+sta) + PI; |
482 |
> |
azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation); |
483 |
|
/* convert measured values */ |
484 |
|
if (dir_is_horiz && altitude > 0.) |
485 |
|
dir /= sin(altitude); |
505 |
|
break; |
506 |
|
} |
507 |
|
/* write out matrix */ |
508 |
+ |
if (outfmt != 'a') |
509 |
+ |
SET_FILE_BINARY(stdout); |
510 |
|
#ifdef getc_unlocked |
511 |
|
flockfile(stdout); |
512 |
|
#endif |
513 |
|
if (verbose) |
514 |
|
fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", |
515 |
|
progname, outfmt=='a' ? "" : "binary ", ntsteps); |
516 |
+ |
if (doheader) { |
517 |
+ |
newheader("RADIANCE", stdout); |
518 |
+ |
printargs(argc, argv, stdout); |
519 |
+ |
printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude), |
520 |
+ |
-RadToDeg(s_longitude)); |
521 |
+ |
printf("NROWS=%d\n", nskypatch); |
522 |
+ |
printf("NCOLS=%d\n", ntsteps); |
523 |
+ |
printf("NCOMP=3\n"); |
524 |
+ |
fputformat((char *)getfmtname(outfmt), stdout); |
525 |
+ |
putchar('\n'); |
526 |
+ |
} |
527 |
|
/* patches are rows (outer sort) */ |
528 |
|
for (i = 0; i < nskypatch; i++) { |
529 |
|
mtx_offset = 3*i; |
565 |
|
fprintf(stderr, "%s: done.\n", progname); |
566 |
|
exit(0); |
567 |
|
userr: |
568 |
< |
fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g r g b][-c r g b][-o{f|d}] [tape.wea]\n", |
568 |
> |
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", |
569 |
|
progname); |
570 |
|
exit(1); |
571 |
|
fmterr: |
618 |
|
sky_brightness = CalcSkyBrightness(); |
619 |
|
sky_clearness = CalcSkyClearness(); |
620 |
|
|
621 |
+ |
/* Limit sky clearness */ |
622 |
+ |
if (sky_clearness > 11.9) |
623 |
+ |
sky_clearness = 11.9; |
624 |
+ |
|
625 |
+ |
/* Limit sky brightness */ |
626 |
+ |
if (sky_brightness < 0.01) |
627 |
+ |
sky_brightness = 0.01; |
628 |
+ |
|
629 |
|
/* Calculate illuminance */ |
630 |
|
index = GetCategoryIndex(); |
631 |
|
diff_illum = diff_irrad * CalcDiffuseIllumRatio(index); |
637 |
|
index = CalcSkyParamFromIllum(); |
638 |
|
} |
639 |
|
|
640 |
+ |
if (output == 1) { /* hack for solar radiance */ |
641 |
+ |
diff_illum = diff_irrad * WHTEFFICACY; |
642 |
+ |
dir_illum = dir_irrad * WHTEFFICACY; |
643 |
+ |
} |
644 |
+ |
|
645 |
|
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
646 |
|
memset(parr, 0, sizeof(float)*3*nskypatch); |
647 |
|
return; |
662 |
|
/* Calculate relative horizontal illuminance */ |
663 |
|
norm_diff_illum = CalcRelHorzIllum(parr); |
664 |
|
|
665 |
+ |
/* Check for zero sky -- make uniform in that case */ |
666 |
+ |
if (norm_diff_illum <= FTINY) { |
667 |
+ |
for (i = 1; i < nskypatch; i++) |
668 |
+ |
setcolor(parr+3*i, 1., 1., 1.); |
669 |
+ |
norm_diff_illum = PI; |
670 |
+ |
} |
671 |
|
/* Normalization coefficient */ |
672 |
|
norm_diff_illum = diff_illum / norm_diff_illum; |
673 |
|
|
690 |
|
|
691 |
|
if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4) |
692 |
|
return; |
693 |
< |
/* identify NSUNPATCH closest patches */ |
694 |
< |
for (i = NSUNPATCH; i--; ) |
693 |
> |
/* identify nsuns closest patches */ |
694 |
> |
if (nsuns > NSUNPATCH) |
695 |
> |
nsuns = NSUNPATCH; |
696 |
> |
else if (nsuns <= 0) |
697 |
> |
nsuns = 1; |
698 |
> |
for (i = nsuns; i--; ) |
699 |
|
near_dprod[i] = -1.; |
700 |
|
vector(svec, altitude, azimuth); |
701 |
|
for (p = 1; p < nskypatch; p++) { |
703 |
|
double dprod; |
704 |
|
rh_vector(pvec, p); |
705 |
|
dprod = DOT(pvec, svec); |
706 |
< |
for (i = 0; i < NSUNPATCH; i++) |
706 |
> |
for (i = 0; i < nsuns; i++) |
707 |
|
if (dprod > near_dprod[i]) { |
708 |
< |
for (j = NSUNPATCH; --j > i; ) { |
708 |
> |
for (j = nsuns; --j > i; ) { |
709 |
|
near_dprod[j] = near_dprod[j-1]; |
710 |
|
near_patch[j] = near_patch[j-1]; |
711 |
|
} |
715 |
|
} |
716 |
|
} |
717 |
|
wtot = 0; /* weight by proximity */ |
718 |
< |
for (i = NSUNPATCH; i--; ) |
718 |
> |
for (i = nsuns; i--; ) |
719 |
|
wtot += wta[i] = 1./(1.002 - near_dprod[i]); |
720 |
|
/* add to nearest patch radiances */ |
721 |
< |
for (i = NSUNPATCH; i--; ) { |
721 |
> |
for (i = nsuns; i--; ) { |
722 |
|
float *pdest = parr + 3*near_patch[i]; |
723 |
< |
float val_add = wta[i] * dir_illum / |
724 |
< |
(WHTEFFICACY * wtot * rh_dom[near_patch[i]]); |
723 |
> |
float val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot); |
724 |
> |
|
725 |
> |
val_add /= (fixed_sun_sa > 0) ? fixed_sun_sa |
726 |
> |
: rh_dom[near_patch[i]] ; |
727 |
|
*pdest++ += val_add*suncolor[0]; |
728 |
|
*pdest++ += val_add*suncolor[1]; |
729 |
|
*pdest++ += val_add*suncolor[2]; |
867 |
|
double sz_cubed; /* Sun zenith angle cubed */ |
868 |
|
|
869 |
|
/* Calculate sun zenith angle cubed */ |
870 |
< |
sz_cubed = pow(sun_zenith, 3.0); |
870 |
> |
sz_cubed = sun_zenith*sun_zenith*sun_zenith; |
871 |
|
|
872 |
|
return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 * |
873 |
|
sz_cubed) / (1.0 + 1.041 * sz_cubed); |
898 |
|
double CalcDirectIrradiance() |
899 |
|
{ |
900 |
|
return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041 |
901 |
< |
* pow(sun_zenith, 3.0))); |
901 |
> |
* sun_zenith*sun_zenith*sun_zenith)); |
902 |
|
} |
903 |
|
|
904 |
|
/* Calculate sky brightness and clearness from illuminance values */ |
924 |
|
sky_clearness = 12.0; |
925 |
|
|
926 |
|
/* Limit sky brightness */ |
927 |
< |
if (sky_brightness < 0.05) |
927 |
> |
if (sky_brightness < 0.01) |
928 |
|
sky_brightness = 0.01; |
929 |
|
|
930 |
|
while (((fabs(diff_irrad - test1) > 10.0) || |
948 |
|
sky_clearness = 12.0; |
949 |
|
|
950 |
|
/* Limit sky brightness */ |
951 |
< |
if (sky_brightness < 0.05) |
951 |
> |
if (sky_brightness < 0.01) |
952 |
|
sky_brightness = 0.01; |
953 |
|
} |
954 |
|
|