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" |
89 |
|
#include "color.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 |
|
|
254 |
– |
extern int jdate(int month, int day); |
255 |
– |
extern double stadj(int jd); |
256 |
– |
extern double sdec(int jd); |
257 |
– |
extern double salt(double sd, double st); |
258 |
– |
extern double sazi(double sd, double st); |
259 |
– |
/* sun calculation constants */ |
260 |
– |
extern double s_latitude; |
261 |
– |
extern double s_longitude; |
262 |
– |
extern double s_meridian; |
263 |
– |
|
255 |
|
int nsuns = NSUNPATCH; /* number of sun patches to use */ |
256 |
|
double fixed_sun_sa = -1; /* fixed solid angle per sun? */ |
257 |
|
|
283 |
|
extern float * resize_dmatrix(float *mtx_data, int nsteps, int npatch); |
284 |
|
extern void AddDirect(float *parr); |
285 |
|
|
286 |
+ |
|
287 |
+ |
static const char * |
288 |
+ |
getfmtname(int fmt) |
289 |
+ |
{ |
290 |
+ |
switch (fmt) { |
291 |
+ |
case 'a': |
292 |
+ |
return("ascii"); |
293 |
+ |
case 'f': |
294 |
+ |
return("float"); |
295 |
+ |
case 'd': |
296 |
+ |
return("double"); |
297 |
+ |
} |
298 |
+ |
return("unknown"); |
299 |
+ |
} |
300 |
+ |
|
301 |
+ |
|
302 |
|
int |
303 |
|
main(int argc, char *argv[]) |
304 |
|
{ |
305 |
|
char buf[256]; |
306 |
+ |
int doheader = 1; /* output header? */ |
307 |
|
double rotation = 0; /* site rotation (degrees) */ |
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 */ |
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 mo, da; /* month (1-12) and day (1-31) */ |
317 |
|
double hr; /* hour (local standard time) */ |
331 |
|
case 'v': /* verbose progress reports */ |
332 |
|
verbose++; |
333 |
|
break; |
334 |
+ |
case 'h': /* turn off header */ |
335 |
+ |
doheader = 0; |
336 |
+ |
break; |
337 |
|
case 'o': /* output format */ |
338 |
|
switch (argv[i][2]) { |
339 |
|
case 'f': |
369 |
|
break; |
370 |
|
case 'd': /* solar (direct) only */ |
371 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 0; |
372 |
< |
if (suncolor[1] <= 1e-4) |
359 |
< |
suncolor[0] = suncolor[1] = suncolor[2] = 1; |
372 |
> |
grefl[0] = grefl[1] = grefl[2] = 0; |
373 |
|
break; |
374 |
|
case 's': /* sky only (no direct) */ |
375 |
|
suncolor[0] = suncolor[1] = suncolor[2] = 0; |
363 |
– |
if (skycolor[1] <= 1e-4) |
364 |
– |
skycolor[0] = skycolor[1] = skycolor[2] = 1; |
376 |
|
break; |
377 |
|
case 'r': /* rotate distribution */ |
378 |
|
if (argv[i][2] && argv[i][2] != 'z') |
381 |
|
break; |
382 |
|
case '5': /* 5-phase calculation */ |
383 |
|
nsuns = 1; |
384 |
< |
fixed_sun_sa = 6.797e-05; |
384 |
> |
fixed_sun_sa = PI/360.*atof(argv[++i]); |
385 |
> |
if (fixed_sun_sa <= 0) { |
386 |
> |
fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n", |
387 |
> |
argv[0]); |
388 |
> |
exit(1); |
389 |
> |
} |
390 |
> |
fixed_sun_sa *= fixed_sun_sa*PI; |
391 |
|
break; |
392 |
+ |
case 'A': /* compute average sky */ |
393 |
+ |
avgSky = 1; |
394 |
+ |
break; |
395 |
|
default: |
396 |
|
goto userr; |
397 |
|
} |
454 |
|
s_latitude = DegToRad(s_latitude); |
455 |
|
s_longitude = DegToRad(s_longitude); |
456 |
|
s_meridian = DegToRad(s_meridian); |
457 |
+ |
/* initial allocation */ |
458 |
+ |
mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch); |
459 |
|
/* process each time step in tape */ |
460 |
|
while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) { |
461 |
|
double sda, sta; |
462 |
< |
/* make space for next time step */ |
463 |
< |
mtx_offset = 3*nskypatch*ntsteps++; |
464 |
< |
mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch); |
462 |
> |
|
463 |
> |
mtx_offset = 3*nskypatch*nstored; |
464 |
> |
nstored += !avgSky | !nstored; |
465 |
> |
/* make space for next row */ |
466 |
> |
if (nstored > tstorage) { |
467 |
> |
tstorage += (tstorage>>1) + nstored + 7; |
468 |
> |
mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch); |
469 |
> |
} |
470 |
> |
ntsteps++; /* keep count of time steps */ |
471 |
|
if (dif <= 1e-4) { |
472 |
< |
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
472 |
> |
if (!avgSky | !mtx_offset) |
473 |
> |
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
474 |
|
continue; |
475 |
|
} |
476 |
|
if (verbose && mo != last_monthly) |
495 |
|
/* compute sky patch values */ |
496 |
|
ComputeSky(mtx_data+mtx_offset); |
497 |
|
AddDirect(mtx_data+mtx_offset); |
498 |
+ |
/* update cumulative sky? */ |
499 |
+ |
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
500 |
+ |
mtx_data[i] += mtx_data[mtx_offset+i]; |
501 |
|
} |
502 |
|
/* check for junk at end */ |
503 |
|
while ((i = fgetc(stdin)) != EOF) |
509 |
|
fputs(buf, stderr); fputc('\n', stderr); |
510 |
|
break; |
511 |
|
} |
512 |
+ |
if (!ntsteps) { |
513 |
+ |
fprintf(stderr, "%s: no valid time steps on input\n", progname); |
514 |
+ |
exit(1); |
515 |
+ |
} |
516 |
+ |
dif = 1./(double)ntsteps; /* average sky? */ |
517 |
+ |
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
518 |
+ |
mtx_data[i] *= dif; |
519 |
|
/* write out matrix */ |
520 |
+ |
if (outfmt != 'a') |
521 |
+ |
SET_FILE_BINARY(stdout); |
522 |
|
#ifdef getc_unlocked |
523 |
|
flockfile(stdout); |
524 |
|
#endif |
525 |
|
if (verbose) |
526 |
|
fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", |
527 |
< |
progname, outfmt=='a' ? "" : "binary ", ntsteps); |
527 |
> |
progname, outfmt=='a' ? "" : "binary ", nstored); |
528 |
> |
if (doheader) { |
529 |
> |
newheader("RADIANCE", stdout); |
530 |
> |
printargs(argc, argv, stdout); |
531 |
> |
printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude), |
532 |
> |
-RadToDeg(s_longitude)); |
533 |
> |
printf("NROWS=%d\n", nskypatch); |
534 |
> |
printf("NCOLS=%d\n", nstored); |
535 |
> |
printf("NCOMP=3\n"); |
536 |
> |
if ((outfmt == 'f') | (outfmt == 'd')) |
537 |
> |
fputendian(stdout); |
538 |
> |
fputformat((char *)getfmtname(outfmt), stdout); |
539 |
> |
putchar('\n'); |
540 |
> |
} |
541 |
|
/* patches are rows (outer sort) */ |
542 |
|
for (i = 0; i < nskypatch; i++) { |
543 |
|
mtx_offset = 3*i; |
544 |
|
switch (outfmt) { |
545 |
|
case 'a': |
546 |
< |
for (j = 0; j < ntsteps; j++) { |
546 |
> |
for (j = 0; j < nstored; j++) { |
547 |
|
printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset], |
548 |
|
mtx_data[mtx_offset+1], |
549 |
|
mtx_data[mtx_offset+2]); |
550 |
|
mtx_offset += 3*nskypatch; |
551 |
|
} |
552 |
< |
if (ntsteps > 1) |
552 |
> |
if (nstored > 1) |
553 |
|
fputc('\n', stdout); |
554 |
|
break; |
555 |
|
case 'f': |
556 |
< |
for (j = 0; j < ntsteps; j++) { |
557 |
< |
fwrite(mtx_data+mtx_offset, sizeof(float), 3, |
556 |
> |
for (j = 0; j < nstored; j++) { |
557 |
> |
putbinary(mtx_data+mtx_offset, sizeof(float), 3, |
558 |
|
stdout); |
559 |
|
mtx_offset += 3*nskypatch; |
560 |
|
} |
561 |
|
break; |
562 |
|
case 'd': |
563 |
< |
for (j = 0; j < ntsteps; j++) { |
563 |
> |
for (j = 0; j < nstored; j++) { |
564 |
|
double ment[3]; |
565 |
|
ment[0] = mtx_data[mtx_offset]; |
566 |
|
ment[1] = mtx_data[mtx_offset+1]; |
567 |
|
ment[2] = mtx_data[mtx_offset+2]; |
568 |
< |
fwrite(ment, sizeof(double), 3, stdout); |
568 |
> |
putbinary(ment, sizeof(double), 3, stdout); |
569 |
|
mtx_offset += 3*nskypatch; |
570 |
|
} |
571 |
|
break; |
579 |
|
fprintf(stderr, "%s: done.\n", progname); |
580 |
|
exit(0); |
581 |
|
userr: |
582 |
< |
fprintf(stderr, "Usage: %s [-v][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n", |
582 |
> |
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", |
583 |
|
progname); |
584 |
|
exit(1); |
585 |
|
fmterr: |
655 |
|
diff_illum = diff_irrad * WHTEFFICACY; |
656 |
|
dir_illum = dir_irrad * WHTEFFICACY; |
657 |
|
} |
604 |
– |
|
605 |
– |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
606 |
– |
memset(parr, 0, sizeof(float)*3*nskypatch); |
607 |
– |
return; |
608 |
– |
} |
658 |
|
/* Compute ground radiance (include solar contribution if any) */ |
659 |
|
parr[0] = diff_illum; |
660 |
|
if (altitude > 0) |
662 |
|
parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY); |
663 |
|
multcolor(parr, grefl); |
664 |
|
|
665 |
+ |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
666 |
+ |
memset(parr+3, 0, sizeof(float)*3*(nskypatch-1)); |
667 |
+ |
return; |
668 |
+ |
} |
669 |
|
/* Calculate Perez sky model parameters */ |
670 |
|
CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index); |
671 |
|
|
675 |
|
/* Calculate relative horizontal illuminance */ |
676 |
|
norm_diff_illum = CalcRelHorzIllum(parr); |
677 |
|
|
678 |
+ |
/* Check for zero sky -- make uniform in that case */ |
679 |
+ |
if (norm_diff_illum <= FTINY) { |
680 |
+ |
for (i = 1; i < nskypatch; i++) |
681 |
+ |
setcolor(parr+3*i, 1., 1., 1.); |
682 |
+ |
norm_diff_illum = PI; |
683 |
+ |
} |
684 |
|
/* Normalization coefficient */ |
685 |
|
norm_diff_illum = diff_illum / norm_diff_illum; |
686 |
|
|
950 |
|
/* Convert illuminance to irradiance */ |
951 |
|
index = GetCategoryIndex(); |
952 |
|
diff_irrad = diff_illum / CalcDiffuseIllumRatio(index); |
953 |
< |
dir_irrad = dir_illum / CalcDirectIllumRatio(index); |
953 |
> |
dir_irrad = CalcDirectIllumRatio(index); |
954 |
> |
if (dir_irrad > 0.1) |
955 |
> |
dir_irrad = dir_illum / dir_irrad; |
956 |
|
|
957 |
|
/* Calculate sky brightness and clearness */ |
958 |
|
sky_brightness = CalcSkyBrightness(); |