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> |
88 |
– |
#include "rtmath.h" |
86 |
|
#include "platform.h" |
87 |
+ |
#include "rtmath.h" |
88 |
+ |
#include "rtio.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 |
|
|
256 |
– |
extern int jdate(int month, int day); |
257 |
– |
extern double stadj(int jd); |
258 |
– |
extern double sdec(int jd); |
259 |
– |
extern double salt(double sd, double st); |
260 |
– |
extern double sazi(double sd, double st); |
261 |
– |
/* sun calculation constants */ |
262 |
– |
extern double s_latitude; |
263 |
– |
extern double s_longitude; |
264 |
– |
extern double s_meridian; |
265 |
– |
|
255 |
|
int nsuns = NSUNPATCH; /* number of sun patches to use */ |
256 |
|
double fixed_sun_sa = -1; /* fixed solid angle per sun? */ |
257 |
|
|
308 |
|
double elevation; /* site elevation (meters) */ |
309 |
|
int dir_is_horiz; /* direct is meas. on horizontal? */ |
310 |
|
float *mtx_data = NULL; /* our matrix data */ |
311 |
< |
int ntsteps = 0; /* number of rows in matrix */ |
312 |
< |
int step_alloc = 0; |
311 |
> |
int avgSky = 0; /* compute average sky r.t. matrix? */ |
312 |
> |
int ntsteps = 0; /* number of time steps */ |
313 |
> |
int tstorage = 0; /* number of allocated time steps */ |
314 |
> |
int nstored = 0; /* number of time steps in matrix */ |
315 |
|
int last_monthly = 0; /* month of last report */ |
316 |
+ |
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 */ |
367 |
+ |
inconsistent |= (skycolor[1] <= 1e-4); |
368 |
|
skycolor[0] = atof(argv[++i]); |
369 |
|
skycolor[1] = atof(argv[++i]); |
370 |
|
skycolor[2] = atof(argv[++i]); |
371 |
|
break; |
372 |
|
case 'd': /* solar (direct) only */ |
373 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 0; |
374 |
< |
if (suncolor[1] <= 1e-4) |
374 |
> |
if (suncolor[1] <= 1e-4) { |
375 |
> |
inconsistent = 1; |
376 |
|
suncolor[0] = suncolor[1] = suncolor[2] = 1; |
377 |
+ |
} |
378 |
|
break; |
379 |
|
case 's': /* sky only (no direct) */ |
380 |
|
suncolor[0] = suncolor[1] = suncolor[2] = 0; |
381 |
< |
if (skycolor[1] <= 1e-4) |
381 |
> |
if (skycolor[1] <= 1e-4) { |
382 |
> |
inconsistent = 1; |
383 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 1; |
384 |
+ |
} |
385 |
|
break; |
386 |
|
case 'r': /* rotate distribution */ |
387 |
|
if (argv[i][2] && argv[i][2] != 'z') |
391 |
|
case '5': /* 5-phase calculation */ |
392 |
|
nsuns = 1; |
393 |
|
fixed_sun_sa = PI/360.*atof(argv[++i]); |
394 |
+ |
if (fixed_sun_sa <= 0) { |
395 |
+ |
fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n", |
396 |
+ |
argv[0]); |
397 |
+ |
exit(1); |
398 |
+ |
} |
399 |
|
fixed_sun_sa *= fixed_sun_sa*PI; |
400 |
|
break; |
401 |
+ |
case 'A': /* compute average sky */ |
402 |
+ |
avgSky = 1; |
403 |
+ |
break; |
404 |
|
default: |
405 |
|
goto userr; |
406 |
|
} |
407 |
|
if (i < argc-1) |
408 |
|
goto userr; |
409 |
+ |
if (inconsistent) |
410 |
+ |
fprintf(stderr, "%s: WARNING: inconsistent -s, -d, -c options!\n", |
411 |
+ |
progname); |
412 |
|
if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
413 |
|
fprintf(stderr, "%s: cannot open '%s' for input\n", |
414 |
|
progname, argv[i]); |
466 |
|
s_latitude = DegToRad(s_latitude); |
467 |
|
s_longitude = DegToRad(s_longitude); |
468 |
|
s_meridian = DegToRad(s_meridian); |
469 |
+ |
/* initial allocation */ |
470 |
+ |
mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch); |
471 |
|
/* process each time step in tape */ |
472 |
|
while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) { |
473 |
|
double sda, sta; |
474 |
< |
/* make space for next time step */ |
475 |
< |
mtx_offset = 3*nskypatch*ntsteps++; |
476 |
< |
if (ntsteps > step_alloc) { |
477 |
< |
step_alloc += (step_alloc>>1) + ntsteps + 7; |
478 |
< |
mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch); |
474 |
> |
|
475 |
> |
mtx_offset = 3*nskypatch*nstored; |
476 |
> |
nstored += !avgSky | !nstored; |
477 |
> |
/* make space for next row */ |
478 |
> |
if (nstored > tstorage) { |
479 |
> |
tstorage += (tstorage>>1) + nstored + 7; |
480 |
> |
mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch); |
481 |
|
} |
482 |
+ |
ntsteps++; /* keep count of time steps */ |
483 |
|
if (dif <= 1e-4) { |
484 |
< |
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
484 |
> |
if (!avgSky | !mtx_offset) |
485 |
> |
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
486 |
|
continue; |
487 |
|
} |
488 |
|
if (verbose && mo != last_monthly) |
507 |
|
/* compute sky patch values */ |
508 |
|
ComputeSky(mtx_data+mtx_offset); |
509 |
|
AddDirect(mtx_data+mtx_offset); |
510 |
+ |
/* update cumulative sky? */ |
511 |
+ |
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
512 |
+ |
mtx_data[i] += mtx_data[mtx_offset+i]; |
513 |
|
} |
514 |
|
/* check for junk at end */ |
515 |
|
while ((i = fgetc(stdin)) != EOF) |
521 |
|
fputs(buf, stderr); fputc('\n', stderr); |
522 |
|
break; |
523 |
|
} |
524 |
+ |
if (!ntsteps) { |
525 |
+ |
fprintf(stderr, "%s: no valid time steps on input\n", progname); |
526 |
+ |
exit(1); |
527 |
+ |
} |
528 |
+ |
dif = 1./(double)ntsteps; /* average sky? */ |
529 |
+ |
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
530 |
+ |
mtx_data[i] *= dif; |
531 |
|
/* write out matrix */ |
532 |
|
if (outfmt != 'a') |
533 |
|
SET_FILE_BINARY(stdout); |
536 |
|
#endif |
537 |
|
if (verbose) |
538 |
|
fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", |
539 |
< |
progname, outfmt=='a' ? "" : "binary ", ntsteps); |
539 |
> |
progname, outfmt=='a' ? "" : "binary ", nstored); |
540 |
|
if (doheader) { |
541 |
|
newheader("RADIANCE", stdout); |
542 |
|
printargs(argc, argv, stdout); |
543 |
|
printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude), |
544 |
|
-RadToDeg(s_longitude)); |
545 |
|
printf("NROWS=%d\n", nskypatch); |
546 |
< |
printf("NCOLS=%d\n", ntsteps); |
546 |
> |
printf("NCOLS=%d\n", nstored); |
547 |
|
printf("NCOMP=3\n"); |
548 |
+ |
if ((outfmt == 'f') | (outfmt == 'd')) |
549 |
+ |
fputendian(stdout); |
550 |
|
fputformat((char *)getfmtname(outfmt), stdout); |
551 |
|
putchar('\n'); |
552 |
|
} |
555 |
|
mtx_offset = 3*i; |
556 |
|
switch (outfmt) { |
557 |
|
case 'a': |
558 |
< |
for (j = 0; j < ntsteps; j++) { |
558 |
> |
for (j = 0; j < nstored; j++) { |
559 |
|
printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset], |
560 |
|
mtx_data[mtx_offset+1], |
561 |
|
mtx_data[mtx_offset+2]); |
562 |
|
mtx_offset += 3*nskypatch; |
563 |
|
} |
564 |
< |
if (ntsteps > 1) |
564 |
> |
if (nstored > 1) |
565 |
|
fputc('\n', stdout); |
566 |
|
break; |
567 |
|
case 'f': |
568 |
< |
for (j = 0; j < ntsteps; j++) { |
569 |
< |
fwrite(mtx_data+mtx_offset, sizeof(float), 3, |
568 |
> |
for (j = 0; j < nstored; j++) { |
569 |
> |
putbinary(mtx_data+mtx_offset, sizeof(float), 3, |
570 |
|
stdout); |
571 |
|
mtx_offset += 3*nskypatch; |
572 |
|
} |
573 |
|
break; |
574 |
|
case 'd': |
575 |
< |
for (j = 0; j < ntsteps; j++) { |
575 |
> |
for (j = 0; j < nstored; j++) { |
576 |
|
double ment[3]; |
577 |
|
ment[0] = mtx_data[mtx_offset]; |
578 |
|
ment[1] = mtx_data[mtx_offset+1]; |
579 |
|
ment[2] = mtx_data[mtx_offset+2]; |
580 |
< |
fwrite(ment, sizeof(double), 3, stdout); |
580 |
> |
putbinary(ment, sizeof(double), 3, stdout); |
581 |
|
mtx_offset += 3*nskypatch; |
582 |
|
} |
583 |
|
break; |
591 |
|
fprintf(stderr, "%s: done.\n", progname); |
592 |
|
exit(0); |
593 |
|
userr: |
594 |
< |
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", |
594 |
> |
fprintf(stderr, "Usage: %s [-v][-h][-A][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n", |
595 |
|
progname); |
596 |
|
exit(1); |
597 |
|
fmterr: |
667 |
|
diff_illum = diff_irrad * WHTEFFICACY; |
668 |
|
dir_illum = dir_irrad * WHTEFFICACY; |
669 |
|
} |
644 |
– |
|
645 |
– |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
646 |
– |
memset(parr, 0, sizeof(float)*3*nskypatch); |
647 |
– |
return; |
648 |
– |
} |
670 |
|
/* Compute ground radiance (include solar contribution if any) */ |
671 |
|
parr[0] = diff_illum; |
672 |
|
if (altitude > 0) |
674 |
|
parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY); |
675 |
|
multcolor(parr, grefl); |
676 |
|
|
677 |
+ |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
678 |
+ |
memset(parr+3, 0, sizeof(float)*3*(nskypatch-1)); |
679 |
+ |
return; |
680 |
+ |
} |
681 |
|
/* Calculate Perez sky model parameters */ |
682 |
|
CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index); |
683 |
|
|
962 |
|
/* Convert illuminance to irradiance */ |
963 |
|
index = GetCategoryIndex(); |
964 |
|
diff_irrad = diff_illum / CalcDiffuseIllumRatio(index); |
965 |
< |
dir_irrad = dir_illum / CalcDirectIllumRatio(index); |
965 |
> |
dir_irrad = CalcDirectIllumRatio(index); |
966 |
> |
if (dir_irrad > 0.1) |
967 |
> |
dir_irrad = dir_illum / dir_irrad; |
968 |
|
|
969 |
|
/* Calculate sky brightness and clearness */ |
970 |
|
sky_brightness = CalcSkyBrightness(); |