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 "sun.h" |
91 |
|
|
92 |
< |
char *progname; /* Program name */ |
93 |
< |
char errmsg[128]; /* Error message buffer */ |
92 |
> |
char *progname; /* Program name */ |
93 |
|
const double DC_SolarConstantE = 1367.0; /* Solar constant W/m^2 */ |
94 |
|
const double DC_SolarConstantL = 127.5; /* Solar constant klux */ |
95 |
|
|
134 |
|
/* Radiuans into degrees */ |
135 |
|
#define RadToDeg(rad) ((rad)*(180./PI)) |
136 |
|
|
138 |
– |
|
137 |
|
/* Perez sky model coefficients */ |
138 |
|
|
139 |
|
/* Reference: Perez, R., R. Seals, and J. Michalsky, 1993. "All- */ |
250 |
|
#define NSUNPATCH 4 /* max. # patches to spread sun into */ |
251 |
|
#endif |
252 |
|
|
253 |
< |
extern int jdate(int month, int day); |
256 |
< |
extern double stadj(int jd); |
257 |
< |
extern double sdec(int jd); |
258 |
< |
extern double salt(double sd, double st); |
259 |
< |
extern double sazi(double sd, double st); |
260 |
< |
/* sun calculation constants */ |
261 |
< |
extern double s_latitude; |
262 |
< |
extern double s_longitude; |
263 |
< |
extern double s_meridian; |
253 |
> |
#define SUN_ANG_DEG 0.533 /* sun full-angle in degrees */ |
254 |
|
|
255 |
|
int nsuns = NSUNPATCH; /* number of sun patches to use */ |
256 |
|
double fixed_sun_sa = -1; /* fixed solid angle per sun? */ |
270 |
|
float *rh_pazi; /* sky patch azimuths (radians) */ |
271 |
|
float *rh_dom; /* sky patch solid angle (sr) */ |
272 |
|
|
273 |
< |
#define vector(v,alt,azi) ( (v)[1] = tcos(alt), \ |
274 |
< |
(v)[0] = (v)[1]*tsin(azi), \ |
275 |
< |
(v)[1] *= tcos(azi), \ |
276 |
< |
(v)[2] = tsin(alt) ) |
273 |
> |
#define vector(v,alt,azi) ( (v)[1] = cos(alt), \ |
274 |
> |
(v)[0] = (v)[1]*sin(azi), \ |
275 |
> |
(v)[1] *= cos(azi), \ |
276 |
> |
(v)[2] = sin(alt) ) |
277 |
|
|
278 |
|
#define rh_vector(v,i) vector(v,rh_palt[i],rh_pazi[i]) |
279 |
|
|
280 |
|
#define rh_cos(i) tsin(rh_palt[i]) |
281 |
|
|
282 |
+ |
#define solar_minute(jd,hr) ((24*60)*((jd)-1)+(int)((hr)*60.+.5)) |
283 |
+ |
|
284 |
|
extern int rh_init(void); |
285 |
|
extern float * resize_dmatrix(float *mtx_data, int nsteps, int npatch); |
286 |
+ |
extern void OutputSun(int id, int goodsun, FILE *fp, FILE *mfp); |
287 |
|
extern void AddDirect(float *parr); |
288 |
|
|
289 |
|
|
309 |
|
int doheader = 1; /* output header? */ |
310 |
|
double rotation = 0; /* site rotation (degrees) */ |
311 |
|
double elevation; /* site elevation (meters) */ |
312 |
+ |
int leap_day = 0; /* add leap day? */ |
313 |
+ |
int sun_hours_only = 0; /* only output sun hours? */ |
314 |
|
int dir_is_horiz; /* direct is meas. on horizontal? */ |
315 |
+ |
FILE *sunsfp = NULL; /* output file for individual suns */ |
316 |
+ |
FILE *modsfp = NULL; /* modifier output file */ |
317 |
|
float *mtx_data = NULL; /* our matrix data */ |
318 |
< |
int ntsteps = 0; /* number of rows in matrix */ |
319 |
< |
int step_alloc = 0; |
318 |
> |
int avgSky = 0; /* compute average sky r.t. matrix? */ |
319 |
> |
int ntsteps = 0; /* number of time steps */ |
320 |
> |
int tstorage = 0; /* number of allocated time steps */ |
321 |
> |
int nstored = 0; /* number of time steps in matrix */ |
322 |
|
int last_monthly = 0; /* month of last report */ |
323 |
|
int mo, da; /* month (1-12) and day (1-31) */ |
324 |
|
double hr; /* hour (local standard time) */ |
374 |
|
skycolor[1] = atof(argv[++i]); |
375 |
|
skycolor[2] = atof(argv[++i]); |
376 |
|
break; |
377 |
+ |
case 'D': /* output suns to file */ |
378 |
+ |
if (strcmp(argv[++i], "-")) { |
379 |
+ |
sunsfp = fopen(argv[i], "w"); |
380 |
+ |
if (sunsfp == NULL) { |
381 |
+ |
fprintf(stderr, |
382 |
+ |
"%s: cannot open '%s' for output\n", |
383 |
+ |
progname, argv[i]); |
384 |
+ |
exit(1); |
385 |
+ |
} |
386 |
+ |
break; /* still may output matrix */ |
387 |
+ |
} |
388 |
+ |
sunsfp = stdout; /* sending to stdout, so... */ |
389 |
+ |
/* fall through */ |
390 |
+ |
case 'n': /* no matrix output */ |
391 |
+ |
avgSky = -1; |
392 |
+ |
rhsubdiv = 1; |
393 |
+ |
/* fall through */ |
394 |
|
case 'd': /* solar (direct) only */ |
395 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 0; |
396 |
< |
if (suncolor[1] <= 1e-4) |
381 |
< |
suncolor[0] = suncolor[1] = suncolor[2] = 1; |
396 |
> |
grefl[0] = grefl[1] = grefl[2] = 0; |
397 |
|
break; |
398 |
+ |
case 'M': /* send sun modifiers to file */ |
399 |
+ |
if ((modsfp = fopen(argv[++i], "w")) == NULL) { |
400 |
+ |
fprintf(stderr, "%s: cannot open '%s' for output\n", |
401 |
+ |
progname, argv[i]); |
402 |
+ |
exit(1); |
403 |
+ |
} |
404 |
+ |
break; |
405 |
|
case 's': /* sky only (no direct) */ |
406 |
|
suncolor[0] = suncolor[1] = suncolor[2] = 0; |
385 |
– |
if (skycolor[1] <= 1e-4) |
386 |
– |
skycolor[0] = skycolor[1] = skycolor[2] = 1; |
407 |
|
break; |
408 |
+ |
case 'u': /* solar hours only */ |
409 |
+ |
sun_hours_only = 1; |
410 |
+ |
break; |
411 |
|
case 'r': /* rotate distribution */ |
412 |
|
if (argv[i][2] && argv[i][2] != 'z') |
413 |
|
goto userr; |
415 |
|
break; |
416 |
|
case '5': /* 5-phase calculation */ |
417 |
|
nsuns = 1; |
418 |
< |
fixed_sun_sa = 6.797e-05; |
418 |
> |
fixed_sun_sa = PI/360.*atof(argv[++i]); |
419 |
> |
if (fixed_sun_sa <= 0) { |
420 |
> |
fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n", |
421 |
> |
progname); |
422 |
> |
exit(1); |
423 |
> |
} |
424 |
> |
fixed_sun_sa *= fixed_sun_sa*PI; |
425 |
|
break; |
426 |
+ |
case 'A': /* compute average sky */ |
427 |
+ |
avgSky = 1; |
428 |
+ |
break; |
429 |
|
default: |
430 |
|
goto userr; |
431 |
|
} |
436 |
|
progname, argv[i]); |
437 |
|
exit(1); |
438 |
|
} |
439 |
+ |
if ((modsfp != NULL) & (sunsfp == NULL)) |
440 |
+ |
fprintf(stderr, "%s: warning -M output will be empty without -D\n", |
441 |
+ |
progname); |
442 |
|
if (verbose) { |
443 |
|
if (i == argc-1) |
444 |
|
fprintf(stderr, "%s: reading weather tape '%s'\n", |
481 |
|
fprintf(stderr, "%s: location '%s'\n", progname, buf); |
482 |
|
fprintf(stderr, "%s: (lat,long)=(%.1f,%.1f) degrees north, west\n", |
483 |
|
progname, s_latitude, s_longitude); |
484 |
< |
fprintf(stderr, "%s: %d sky patches per time step\n", |
485 |
< |
progname, nskypatch); |
484 |
> |
if (avgSky >= 0) |
485 |
> |
fprintf(stderr, "%s: %d sky patches\n", |
486 |
> |
progname, nskypatch); |
487 |
> |
if (sunsfp) |
488 |
> |
fprintf(stderr, "%s: outputting suns to %s\n", |
489 |
> |
progname, sunsfp==stdout ? "stdout" : "file"); |
490 |
|
if (rotation != 0) |
491 |
|
fprintf(stderr, "%s: rotating output %.0f degrees\n", |
492 |
|
progname, rotation); |
495 |
|
s_latitude = DegToRad(s_latitude); |
496 |
|
s_longitude = DegToRad(s_longitude); |
497 |
|
s_meridian = DegToRad(s_meridian); |
498 |
+ |
/* initial allocation */ |
499 |
+ |
mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch); |
500 |
|
/* process each time step in tape */ |
501 |
|
while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) { |
502 |
|
double sda, sta; |
503 |
< |
/* make space for next time step */ |
463 |
< |
mtx_offset = 3*nskypatch*ntsteps++; |
464 |
< |
if (ntsteps > step_alloc) { |
465 |
< |
step_alloc += (step_alloc>>1) + ntsteps + 7; |
466 |
< |
mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch); |
467 |
< |
} |
468 |
< |
if (dif <= 1e-4) { |
469 |
< |
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
470 |
< |
continue; |
471 |
< |
} |
472 |
< |
if (verbose && mo != last_monthly) |
473 |
< |
fprintf(stderr, "%s: stepping through month %d...\n", |
474 |
< |
progname, last_monthly=mo); |
503 |
> |
int sun_in_sky; |
504 |
|
/* compute solar position */ |
505 |
< |
julian_date = jdate(mo, da); |
505 |
> |
if ((mo == 2) & (da == 29)) { |
506 |
> |
julian_date = 60; |
507 |
> |
leap_day = 1; |
508 |
> |
} else |
509 |
> |
julian_date = jdate(mo, da) + leap_day; |
510 |
|
sda = sdec(julian_date); |
511 |
|
sta = stadj(julian_date); |
512 |
|
altitude = salt(sda, hr+sta); |
513 |
+ |
sun_in_sky = (altitude > -DegToRad(SUN_ANG_DEG/2.)); |
514 |
+ |
if (sun_hours_only && !sun_in_sky) |
515 |
+ |
continue; /* skipping nighttime points */ |
516 |
|
azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation); |
517 |
+ |
|
518 |
+ |
mtx_offset = 3*nskypatch*nstored; |
519 |
+ |
nstored += !avgSky | !nstored; |
520 |
+ |
/* make space for next row */ |
521 |
+ |
if (nstored > tstorage) { |
522 |
+ |
tstorage += (tstorage>>1) + nstored + 7; |
523 |
+ |
mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch); |
524 |
+ |
} |
525 |
+ |
ntsteps++; /* keep count of time steps */ |
526 |
+ |
|
527 |
+ |
if (dir+dif <= 1e-4) { /* effectively nighttime? */ |
528 |
+ |
if (!avgSky | !mtx_offset) |
529 |
+ |
memset(mtx_data+mtx_offset, 0, |
530 |
+ |
sizeof(float)*3*nskypatch); |
531 |
+ |
/* output black sun? */ |
532 |
+ |
if (sunsfp && sun_in_sky) |
533 |
+ |
OutputSun(solar_minute(julian_date,hr), 0, |
534 |
+ |
sunsfp, modsfp); |
535 |
+ |
continue; |
536 |
+ |
} |
537 |
+ |
if (!sun_in_sky && dir > (input==1 ? 20. : 20.*WHTEFFICACY)) |
538 |
+ |
fprintf(stderr, |
539 |
+ |
"%s: warning - unusually bright at %.1f on %d-%d\n", |
540 |
+ |
progname, hr, mo, da); |
541 |
|
/* convert measured values */ |
542 |
< |
if (dir_is_horiz && altitude > 0.) |
542 |
> |
if (dir_is_horiz && altitude > FTINY) |
543 |
|
dir /= sin(altitude); |
544 |
|
if (input == 1) { |
545 |
|
dir_irrad = dir; |
550 |
|
} |
551 |
|
/* compute sky patch values */ |
552 |
|
ComputeSky(mtx_data+mtx_offset); |
553 |
+ |
/* output sun if requested */ |
554 |
+ |
if (sunsfp && sun_in_sky) |
555 |
+ |
OutputSun(solar_minute(julian_date,hr), 1, |
556 |
+ |
sunsfp, modsfp); |
557 |
+ |
|
558 |
+ |
if (avgSky < 0) /* no matrix? */ |
559 |
+ |
continue; |
560 |
+ |
|
561 |
|
AddDirect(mtx_data+mtx_offset); |
562 |
+ |
/* update cumulative sky? */ |
563 |
+ |
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
564 |
+ |
mtx_data[i] += mtx_data[mtx_offset+i]; |
565 |
+ |
/* monthly reporting */ |
566 |
+ |
if (verbose && mo != last_monthly) |
567 |
+ |
fprintf(stderr, "%s: stepping through month %d...\n", |
568 |
+ |
progname, last_monthly=mo); |
569 |
+ |
/* note whether leap-day was given */ |
570 |
|
} |
571 |
+ |
if (!ntsteps) { |
572 |
+ |
fprintf(stderr, "%s: no valid time steps on input\n", progname); |
573 |
+ |
exit(1); |
574 |
+ |
} |
575 |
|
/* check for junk at end */ |
576 |
|
while ((i = fgetc(stdin)) != EOF) |
577 |
|
if (!isspace(i)) { |
582 |
|
fputs(buf, stderr); fputc('\n', stderr); |
583 |
|
break; |
584 |
|
} |
585 |
+ |
|
586 |
+ |
if (avgSky < 0) /* no matrix output? */ |
587 |
+ |
goto alldone; |
588 |
+ |
|
589 |
+ |
dif = 1./(double)ntsteps; /* average sky? */ |
590 |
+ |
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
591 |
+ |
mtx_data[i] *= dif; |
592 |
|
/* write out matrix */ |
593 |
|
if (outfmt != 'a') |
594 |
|
SET_FILE_BINARY(stdout); |
597 |
|
#endif |
598 |
|
if (verbose) |
599 |
|
fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", |
600 |
< |
progname, outfmt=='a' ? "" : "binary ", ntsteps); |
600 |
> |
progname, outfmt=='a' ? "" : "binary ", nstored); |
601 |
|
if (doheader) { |
602 |
|
newheader("RADIANCE", stdout); |
603 |
|
printargs(argc, argv, stdout); |
604 |
|
printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude), |
605 |
|
-RadToDeg(s_longitude)); |
606 |
|
printf("NROWS=%d\n", nskypatch); |
607 |
< |
printf("NCOLS=%d\n", ntsteps); |
607 |
> |
printf("NCOLS=%d\n", nstored); |
608 |
|
printf("NCOMP=3\n"); |
609 |
< |
fputformat(getfmtname(outfmt), stdout); |
609 |
> |
if ((outfmt == 'f') | (outfmt == 'd')) |
610 |
> |
fputendian(stdout); |
611 |
> |
fputformat((char *)getfmtname(outfmt), stdout); |
612 |
|
putchar('\n'); |
613 |
|
} |
614 |
|
/* patches are rows (outer sort) */ |
616 |
|
mtx_offset = 3*i; |
617 |
|
switch (outfmt) { |
618 |
|
case 'a': |
619 |
< |
for (j = 0; j < ntsteps; j++) { |
619 |
> |
for (j = 0; j < nstored; j++) { |
620 |
|
printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset], |
621 |
|
mtx_data[mtx_offset+1], |
622 |
|
mtx_data[mtx_offset+2]); |
623 |
|
mtx_offset += 3*nskypatch; |
624 |
|
} |
625 |
< |
if (ntsteps > 1) |
625 |
> |
if (nstored > 1) |
626 |
|
fputc('\n', stdout); |
627 |
|
break; |
628 |
|
case 'f': |
629 |
< |
for (j = 0; j < ntsteps; j++) { |
630 |
< |
fwrite(mtx_data+mtx_offset, sizeof(float), 3, |
629 |
> |
for (j = 0; j < nstored; j++) { |
630 |
> |
putbinary(mtx_data+mtx_offset, sizeof(float), 3, |
631 |
|
stdout); |
632 |
|
mtx_offset += 3*nskypatch; |
633 |
|
} |
634 |
|
break; |
635 |
|
case 'd': |
636 |
< |
for (j = 0; j < ntsteps; j++) { |
636 |
> |
for (j = 0; j < nstored; j++) { |
637 |
|
double ment[3]; |
638 |
|
ment[0] = mtx_data[mtx_offset]; |
639 |
|
ment[1] = mtx_data[mtx_offset+1]; |
640 |
|
ment[2] = mtx_data[mtx_offset+2]; |
641 |
< |
fwrite(ment, sizeof(double), 3, stdout); |
641 |
> |
putbinary(ment, sizeof(double), 3, stdout); |
642 |
|
mtx_offset += 3*nskypatch; |
643 |
|
} |
644 |
|
break; |
646 |
|
if (ferror(stdout)) |
647 |
|
goto writerr; |
648 |
|
} |
649 |
< |
if (fflush(stdout) == EOF) |
649 |
> |
alldone: |
650 |
> |
if (fflush(NULL) == EOF) |
651 |
|
goto writerr; |
652 |
|
if (verbose) |
653 |
|
fprintf(stderr, "%s: done.\n", progname); |
654 |
|
exit(0); |
655 |
|
userr: |
656 |
< |
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", |
656 |
> |
fprintf(stderr, "Usage: %s [-v][-h][-A][-d|-s|-n][-u][-D file [-M modfile]][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n", |
657 |
|
progname); |
658 |
|
exit(1); |
659 |
|
fmterr: |
660 |
< |
fprintf(stderr, "%s: input weather tape format error\n", progname); |
660 |
> |
fprintf(stderr, "%s: weather tape format error in header\n", progname); |
661 |
|
exit(1); |
662 |
|
writerr: |
663 |
|
fprintf(stderr, "%s: write error on output\n", progname); |
729 |
|
diff_illum = diff_irrad * WHTEFFICACY; |
730 |
|
dir_illum = dir_irrad * WHTEFFICACY; |
731 |
|
} |
642 |
– |
|
643 |
– |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
644 |
– |
memset(parr, 0, sizeof(float)*3*nskypatch); |
645 |
– |
return; |
646 |
– |
} |
732 |
|
/* Compute ground radiance (include solar contribution if any) */ |
733 |
|
parr[0] = diff_illum; |
734 |
|
if (altitude > 0) |
736 |
|
parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY); |
737 |
|
multcolor(parr, grefl); |
738 |
|
|
739 |
+ |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
740 |
+ |
memset(parr+3, 0, sizeof(float)*3*(nskypatch-1)); |
741 |
+ |
return; |
742 |
+ |
} |
743 |
|
/* Calculate Perez sky model parameters */ |
744 |
|
CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index); |
745 |
|
|
817 |
|
} |
818 |
|
} |
819 |
|
|
820 |
+ |
/* Output a sun to indicated file if appropriate for this time step */ |
821 |
+ |
void |
822 |
+ |
OutputSun(int id, int goodsun, FILE *fp, FILE *mfp) |
823 |
+ |
{ |
824 |
+ |
double srad; |
825 |
+ |
FVECT sv; |
826 |
+ |
|
827 |
+ |
srad = DegToRad(SUN_ANG_DEG/2.); |
828 |
+ |
srad = goodsun ? dir_illum/(WHTEFFICACY * PI*srad*srad) : 0; |
829 |
+ |
vector(sv, altitude, azimuth); |
830 |
+ |
fprintf(fp, "\nvoid light solar%d\n0\n0\n", id); |
831 |
+ |
fprintf(fp, "3 %.3e %.3e %.3e\n", srad*suncolor[0], |
832 |
+ |
srad*suncolor[1], srad*suncolor[2]); |
833 |
+ |
fprintf(fp, "\nsolar%d source sun%d\n0\n0\n", id, id); |
834 |
+ |
fprintf(fp, "4 %.6f %.6f %.6f %.4f\n", sv[0], sv[1], sv[2], SUN_ANG_DEG); |
835 |
+ |
|
836 |
+ |
if (mfp != NULL) /* saving modifier IDs? */ |
837 |
+ |
fprintf(mfp, "solar%d\n", id); |
838 |
+ |
} |
839 |
+ |
|
840 |
|
/* Initialize Reinhart sky patch positions (GW) */ |
841 |
|
int |
842 |
|
rh_init(void) |
1044 |
|
/* Convert illuminance to irradiance */ |
1045 |
|
index = GetCategoryIndex(); |
1046 |
|
diff_irrad = diff_illum / CalcDiffuseIllumRatio(index); |
1047 |
< |
dir_irrad = dir_illum / CalcDirectIllumRatio(index); |
1047 |
> |
dir_irrad = CalcDirectIllumRatio(index); |
1048 |
> |
if (dir_irrad > 0.1) |
1049 |
> |
dir_irrad = dir_illum / dir_irrad; |
1050 |
|
|
1051 |
|
/* Calculate sky brightness and clearness */ |
1052 |
|
sky_brightness = CalcSkyBrightness(); |