| 81 |
|
|
| 82 |
|
/* Include files */ |
| 83 |
|
#define _USE_MATH_DEFINES |
| 84 |
– |
#include <stdio.h> |
| 84 |
|
#include <stdlib.h> |
| 86 |
– |
#include <string.h> |
| 85 |
|
#include <ctype.h> |
| 86 |
+ |
#include "platform.h" |
| 87 |
|
#include "rtmath.h" |
| 88 |
|
#include "rtio.h" |
| 90 |
– |
#include "resolu.h" |
| 91 |
– |
#include "platform.h" |
| 89 |
|
#include "color.h" |
| 90 |
< |
#include "resolu.h" |
| 90 |
> |
#include "sun.h" |
| 91 |
|
|
| 92 |
|
char *progname; /* Program name */ |
| 93 |
|
char errmsg[128]; /* Error message buffer */ |
| 252 |
|
#define NSUNPATCH 4 /* max. # patches to spread sun into */ |
| 253 |
|
#endif |
| 254 |
|
|
| 258 |
– |
extern int jdate(int month, int day); |
| 259 |
– |
extern double stadj(int jd); |
| 260 |
– |
extern double sdec(int jd); |
| 261 |
– |
extern double salt(double sd, double st); |
| 262 |
– |
extern double sazi(double sd, double st); |
| 263 |
– |
/* sun calculation constants */ |
| 264 |
– |
extern double s_latitude; |
| 265 |
– |
extern double s_longitude; |
| 266 |
– |
extern double s_meridian; |
| 267 |
– |
|
| 255 |
|
int nsuns = NSUNPATCH; /* number of sun patches to use */ |
| 256 |
|
double fixed_sun_sa = -1; /* fixed solid angle per sun? */ |
| 257 |
|
|
| 307 |
|
double rotation = 0; /* site rotation (degrees) */ |
| 308 |
|
double elevation; /* site elevation (meters) */ |
| 309 |
|
int dir_is_horiz; /* direct is meas. on horizontal? */ |
| 310 |
+ |
FILE *sunsfp = NULL; /* output file for individual suns */ |
| 311 |
|
float *mtx_data = NULL; /* our matrix data */ |
| 312 |
< |
int ntsteps = 0; /* number of rows in matrix */ |
| 313 |
< |
int step_alloc = 0; |
| 312 |
> |
int avgSky = 0; /* compute average sky r.t. matrix? */ |
| 313 |
> |
int ntsteps = 0; /* number of time steps */ |
| 314 |
> |
int tstorage = 0; /* number of allocated time steps */ |
| 315 |
> |
int nstored = 0; /* number of time steps in matrix */ |
| 316 |
|
int last_monthly = 0; /* month of last report */ |
| 327 |
– |
int inconsistent = 0; /* inconsistent options set? */ |
| 317 |
|
int mo, da; /* month (1-12) and day (1-31) */ |
| 318 |
|
double hr; /* hour (local standard time) */ |
| 319 |
|
double dir, dif; /* direct and diffuse values */ |
| 364 |
|
rhsubdiv = atoi(argv[++i]); |
| 365 |
|
break; |
| 366 |
|
case 'c': /* sky color */ |
| 378 |
– |
inconsistent |= (skycolor[1] <= 1e-4); |
| 367 |
|
skycolor[0] = atof(argv[++i]); |
| 368 |
|
skycolor[1] = atof(argv[++i]); |
| 369 |
|
skycolor[2] = atof(argv[++i]); |
| 370 |
|
break; |
| 371 |
+ |
case 'n': /* no matrix output */ |
| 372 |
+ |
avgSky = -1; |
| 373 |
+ |
rhsubdiv = 1; |
| 374 |
+ |
/* fall through */ |
| 375 |
|
case 'd': /* solar (direct) only */ |
| 376 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 0; |
| 377 |
< |
if (suncolor[1] <= 1e-4) { |
| 378 |
< |
inconsistent = 1; |
| 379 |
< |
suncolor[0] = suncolor[1] = suncolor[2] = 1; |
| 377 |
> |
grefl[0] = grefl[1] = grefl[2] = 0; |
| 378 |
> |
break; |
| 379 |
> |
case 'D': /* output suns to file */ |
| 380 |
> |
sunsfp = fopen(argv[++i], "w"); |
| 381 |
> |
if (!sunsfp) { |
| 382 |
> |
fprintf(stderr, "%s: cannot open '%s' for output\n", |
| 383 |
> |
argv[0], argv[i]); |
| 384 |
> |
exit(1); |
| 385 |
|
} |
| 386 |
+ |
fixed_sun_sa = PI/360.*0.533; |
| 387 |
+ |
fixed_sun_sa *= PI*fixed_sun_sa; |
| 388 |
|
break; |
| 389 |
|
case 's': /* sky only (no direct) */ |
| 390 |
|
suncolor[0] = suncolor[1] = suncolor[2] = 0; |
| 392 |
– |
if (skycolor[1] <= 1e-4) { |
| 393 |
– |
inconsistent = 1; |
| 394 |
– |
skycolor[0] = skycolor[1] = skycolor[2] = 1; |
| 395 |
– |
} |
| 391 |
|
break; |
| 392 |
|
case 'r': /* rotate distribution */ |
| 393 |
|
if (argv[i][2] && argv[i][2] != 'z') |
| 404 |
|
} |
| 405 |
|
fixed_sun_sa *= fixed_sun_sa*PI; |
| 406 |
|
break; |
| 407 |
+ |
case 'A': /* compute average sky */ |
| 408 |
+ |
avgSky = 1; |
| 409 |
+ |
break; |
| 410 |
|
default: |
| 411 |
|
goto userr; |
| 412 |
|
} |
| 413 |
|
if (i < argc-1) |
| 414 |
|
goto userr; |
| 417 |
– |
if (inconsistent) |
| 418 |
– |
fprintf(stderr, "%s: WARNING: inconsistent -s, -d, -c options!\n", |
| 419 |
– |
progname); |
| 415 |
|
if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
| 416 |
|
fprintf(stderr, "%s: cannot open '%s' for input\n", |
| 417 |
|
progname, argv[i]); |
| 469 |
|
s_latitude = DegToRad(s_latitude); |
| 470 |
|
s_longitude = DegToRad(s_longitude); |
| 471 |
|
s_meridian = DegToRad(s_meridian); |
| 472 |
+ |
/* initial allocation */ |
| 473 |
+ |
mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch); |
| 474 |
|
/* process each time step in tape */ |
| 475 |
|
while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) { |
| 476 |
|
double sda, sta; |
| 477 |
< |
/* make space for next time step */ |
| 478 |
< |
mtx_offset = 3*nskypatch*ntsteps++; |
| 479 |
< |
if (ntsteps > step_alloc) { |
| 480 |
< |
step_alloc += (step_alloc>>1) + ntsteps + 7; |
| 481 |
< |
mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch); |
| 477 |
> |
|
| 478 |
> |
mtx_offset = 3*nskypatch*nstored; |
| 479 |
> |
nstored += !avgSky | !nstored; |
| 480 |
> |
/* make space for next row */ |
| 481 |
> |
if (nstored > tstorage) { |
| 482 |
> |
tstorage += (tstorage>>1) + nstored + 7; |
| 483 |
> |
mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch); |
| 484 |
|
} |
| 485 |
+ |
ntsteps++; /* keep count of time steps */ |
| 486 |
|
if (dif <= 1e-4) { |
| 487 |
< |
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
| 487 |
> |
if (!avgSky | !mtx_offset) |
| 488 |
> |
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
| 489 |
|
continue; |
| 490 |
|
} |
| 490 |
– |
if (verbose && mo != last_monthly) |
| 491 |
– |
fprintf(stderr, "%s: stepping through month %d...\n", |
| 492 |
– |
progname, last_monthly=mo); |
| 491 |
|
/* compute solar position */ |
| 492 |
|
julian_date = jdate(mo, da); |
| 493 |
|
sda = sdec(julian_date); |
| 506 |
|
} |
| 507 |
|
/* compute sky patch values */ |
| 508 |
|
ComputeSky(mtx_data+mtx_offset); |
| 509 |
+ |
/* output sun if indicated */ |
| 510 |
+ |
if (sunsfp && (altitude > 0) & (dir_illum > 1e-4)) { |
| 511 |
+ |
double srad = dir_illum/(WHTEFFICACY * fixed_sun_sa); |
| 512 |
+ |
FVECT sv; |
| 513 |
+ |
vector(sv, altitude, azimuth); |
| 514 |
+ |
fprintf(sunsfp, "\nvoid light solar%d\n0\n0\n", ntsteps); |
| 515 |
+ |
fprintf(sunsfp, "3 %.3e %.3e %.3e\n", srad*suncolor[0], |
| 516 |
+ |
srad*suncolor[1], srad*suncolor[2]); |
| 517 |
+ |
fprintf(sunsfp, "\nsolar%d source sun%d\n0\n0\n", ntsteps, ntsteps); |
| 518 |
+ |
fprintf(sunsfp, "4 %.6f %.6f %.6f 0.533\n", sv[0], sv[1], sv[2]); |
| 519 |
+ |
} |
| 520 |
+ |
if (avgSky < 0) /* no matrix? */ |
| 521 |
+ |
continue; |
| 522 |
+ |
|
| 523 |
|
AddDirect(mtx_data+mtx_offset); |
| 524 |
+ |
/* update cumulative sky? */ |
| 525 |
+ |
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
| 526 |
+ |
mtx_data[i] += mtx_data[mtx_offset+i]; |
| 527 |
+ |
/* monthly reporting */ |
| 528 |
+ |
if (verbose && mo != last_monthly) |
| 529 |
+ |
fprintf(stderr, "%s: stepping through month %d...\n", |
| 530 |
+ |
progname, last_monthly=mo); |
| 531 |
|
} |
| 532 |
+ |
if (!ntsteps) { |
| 533 |
+ |
fprintf(stderr, "%s: no valid time steps on input\n", progname); |
| 534 |
+ |
exit(1); |
| 535 |
+ |
} |
| 536 |
|
/* check for junk at end */ |
| 537 |
|
while ((i = fgetc(stdin)) != EOF) |
| 538 |
|
if (!isspace(i)) { |
| 543 |
|
fputs(buf, stderr); fputc('\n', stderr); |
| 544 |
|
break; |
| 545 |
|
} |
| 546 |
+ |
|
| 547 |
+ |
if (avgSky < 0) /* no matrix output? */ |
| 548 |
+ |
goto alldone; |
| 549 |
+ |
|
| 550 |
+ |
dif = 1./(double)ntsteps; /* average sky? */ |
| 551 |
+ |
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
| 552 |
+ |
mtx_data[i] *= dif; |
| 553 |
|
/* write out matrix */ |
| 554 |
|
if (outfmt != 'a') |
| 555 |
|
SET_FILE_BINARY(stdout); |
| 558 |
|
#endif |
| 559 |
|
if (verbose) |
| 560 |
|
fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", |
| 561 |
< |
progname, outfmt=='a' ? "" : "binary ", ntsteps); |
| 561 |
> |
progname, outfmt=='a' ? "" : "binary ", nstored); |
| 562 |
|
if (doheader) { |
| 563 |
|
newheader("RADIANCE", stdout); |
| 564 |
|
printargs(argc, argv, stdout); |
| 565 |
|
printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude), |
| 566 |
|
-RadToDeg(s_longitude)); |
| 567 |
|
printf("NROWS=%d\n", nskypatch); |
| 568 |
< |
printf("NCOLS=%d\n", ntsteps); |
| 568 |
> |
printf("NCOLS=%d\n", nstored); |
| 569 |
|
printf("NCOMP=3\n"); |
| 570 |
+ |
if ((outfmt == 'f') | (outfmt == 'd')) |
| 571 |
+ |
fputendian(stdout); |
| 572 |
|
fputformat((char *)getfmtname(outfmt), stdout); |
| 573 |
|
putchar('\n'); |
| 574 |
|
} |
| 577 |
|
mtx_offset = 3*i; |
| 578 |
|
switch (outfmt) { |
| 579 |
|
case 'a': |
| 580 |
< |
for (j = 0; j < ntsteps; j++) { |
| 580 |
> |
for (j = 0; j < nstored; j++) { |
| 581 |
|
printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset], |
| 582 |
|
mtx_data[mtx_offset+1], |
| 583 |
|
mtx_data[mtx_offset+2]); |
| 584 |
|
mtx_offset += 3*nskypatch; |
| 585 |
|
} |
| 586 |
< |
if (ntsteps > 1) |
| 586 |
> |
if (nstored > 1) |
| 587 |
|
fputc('\n', stdout); |
| 588 |
|
break; |
| 589 |
|
case 'f': |
| 590 |
< |
for (j = 0; j < ntsteps; j++) { |
| 590 |
> |
for (j = 0; j < nstored; j++) { |
| 591 |
|
putbinary(mtx_data+mtx_offset, sizeof(float), 3, |
| 592 |
|
stdout); |
| 593 |
|
mtx_offset += 3*nskypatch; |
| 594 |
|
} |
| 595 |
|
break; |
| 596 |
|
case 'd': |
| 597 |
< |
for (j = 0; j < ntsteps; j++) { |
| 597 |
> |
for (j = 0; j < nstored; j++) { |
| 598 |
|
double ment[3]; |
| 599 |
|
ment[0] = mtx_data[mtx_offset]; |
| 600 |
|
ment[1] = mtx_data[mtx_offset+1]; |
| 607 |
|
if (ferror(stdout)) |
| 608 |
|
goto writerr; |
| 609 |
|
} |
| 610 |
< |
if (fflush(stdout) == EOF) |
| 610 |
> |
alldone: |
| 611 |
> |
if (fflush(NULL) == EOF) |
| 612 |
|
goto writerr; |
| 613 |
|
if (verbose) |
| 614 |
|
fprintf(stderr, "%s: done.\n", progname); |
| 615 |
|
exit(0); |
| 616 |
|
userr: |
| 617 |
< |
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", |
| 617 |
> |
fprintf(stderr, "Usage: %s [-v][-h][-A][-d|-s|-n][-D file][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n", |
| 618 |
|
progname); |
| 619 |
|
exit(1); |
| 620 |
|
fmterr: |
| 621 |
< |
fprintf(stderr, "%s: input weather tape format error\n", progname); |
| 621 |
> |
fprintf(stderr, "%s: weather tape format error in header\n", progname); |
| 622 |
|
exit(1); |
| 623 |
|
writerr: |
| 624 |
|
fprintf(stderr, "%s: write error on output\n", progname); |
| 690 |
|
diff_illum = diff_irrad * WHTEFFICACY; |
| 691 |
|
dir_illum = dir_irrad * WHTEFFICACY; |
| 692 |
|
} |
| 660 |
– |
|
| 661 |
– |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
| 662 |
– |
memset(parr, 0, sizeof(float)*3*nskypatch); |
| 663 |
– |
return; |
| 664 |
– |
} |
| 693 |
|
/* Compute ground radiance (include solar contribution if any) */ |
| 694 |
|
parr[0] = diff_illum; |
| 695 |
|
if (altitude > 0) |
| 697 |
|
parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY); |
| 698 |
|
multcolor(parr, grefl); |
| 699 |
|
|
| 700 |
+ |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
| 701 |
+ |
memset(parr+3, 0, sizeof(float)*3*(nskypatch-1)); |
| 702 |
+ |
return; |
| 703 |
+ |
} |
| 704 |
|
/* Calculate Perez sky model parameters */ |
| 705 |
|
CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index); |
| 706 |
|
|