252 |
|
#define NSUNPATCH 4 /* max. # patches to spread sun into */ |
253 |
|
#endif |
254 |
|
|
255 |
+ |
#define SUN_ANG_DEG 0.533 /* sun full-angle in degrees */ |
256 |
+ |
|
257 |
|
int nsuns = NSUNPATCH; /* number of sun patches to use */ |
258 |
|
double fixed_sun_sa = -1; /* fixed solid angle per sun? */ |
259 |
|
|
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, FILE *fp); |
287 |
|
extern void AddDirect(float *parr); |
288 |
|
|
289 |
|
|
310 |
|
double rotation = 0; /* site rotation (degrees) */ |
311 |
|
double elevation; /* site elevation (meters) */ |
312 |
|
int dir_is_horiz; /* direct is meas. on horizontal? */ |
313 |
+ |
FILE *sunsfp = NULL; /* output file for individual suns */ |
314 |
|
float *mtx_data = NULL; /* our matrix data */ |
315 |
|
int avgSky = 0; /* compute average sky r.t. matrix? */ |
316 |
|
int ntsteps = 0; /* number of time steps */ |
317 |
|
int tstorage = 0; /* number of allocated time steps */ |
318 |
|
int nstored = 0; /* number of time steps in matrix */ |
319 |
|
int last_monthly = 0; /* month of last report */ |
316 |
– |
int inconsistent = 0; /* inconsistent options set? */ |
320 |
|
int mo, da; /* month (1-12) and day (1-31) */ |
321 |
|
double hr; /* hour (local standard time) */ |
322 |
|
double dir, dif; /* direct and diffuse values */ |
367 |
|
rhsubdiv = atoi(argv[++i]); |
368 |
|
break; |
369 |
|
case 'c': /* sky color */ |
367 |
– |
inconsistent |= (skycolor[1] <= 1e-4); |
370 |
|
skycolor[0] = atof(argv[++i]); |
371 |
|
skycolor[1] = atof(argv[++i]); |
372 |
|
skycolor[2] = atof(argv[++i]); |
373 |
|
break; |
374 |
+ |
case 'n': /* no matrix output */ |
375 |
+ |
avgSky = -1; |
376 |
+ |
rhsubdiv = 1; |
377 |
+ |
/* fall through */ |
378 |
|
case 'd': /* solar (direct) only */ |
379 |
|
skycolor[0] = skycolor[1] = skycolor[2] = 0; |
380 |
< |
if (suncolor[1] <= 1e-4) { |
381 |
< |
inconsistent = 1; |
382 |
< |
suncolor[0] = suncolor[1] = suncolor[2] = 1; |
380 |
> |
grefl[0] = grefl[1] = grefl[2] = 0; |
381 |
> |
break; |
382 |
> |
case 'D': /* output suns to file */ |
383 |
> |
sunsfp = fopen(argv[++i], "w"); |
384 |
> |
if (!sunsfp) { |
385 |
> |
fprintf(stderr, "%s: cannot open '%s' for output\n", |
386 |
> |
progname, argv[i]); |
387 |
> |
exit(1); |
388 |
|
} |
389 |
|
break; |
390 |
|
case 's': /* sky only (no direct) */ |
391 |
|
suncolor[0] = suncolor[1] = suncolor[2] = 0; |
381 |
– |
if (skycolor[1] <= 1e-4) { |
382 |
– |
inconsistent = 1; |
383 |
– |
skycolor[0] = skycolor[1] = skycolor[2] = 1; |
384 |
– |
} |
392 |
|
break; |
393 |
|
case 'r': /* rotate distribution */ |
394 |
|
if (argv[i][2] && argv[i][2] != 'z') |
400 |
|
fixed_sun_sa = PI/360.*atof(argv[++i]); |
401 |
|
if (fixed_sun_sa <= 0) { |
402 |
|
fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n", |
403 |
< |
argv[0]); |
403 |
> |
progname); |
404 |
|
exit(1); |
405 |
|
} |
406 |
|
fixed_sun_sa *= fixed_sun_sa*PI; |
413 |
|
} |
414 |
|
if (i < argc-1) |
415 |
|
goto userr; |
409 |
– |
if (inconsistent) |
410 |
– |
fprintf(stderr, "%s: WARNING: inconsistent -s, -d, -c options!\n", |
411 |
– |
progname); |
416 |
|
if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
417 |
|
fprintf(stderr, "%s: cannot open '%s' for input\n", |
418 |
|
progname, argv[i]); |
460 |
|
fprintf(stderr, "%s: location '%s'\n", progname, buf); |
461 |
|
fprintf(stderr, "%s: (lat,long)=(%.1f,%.1f) degrees north, west\n", |
462 |
|
progname, s_latitude, s_longitude); |
463 |
< |
fprintf(stderr, "%s: %d sky patches per time step\n", |
464 |
< |
progname, nskypatch); |
463 |
> |
if (avgSky >= 0) |
464 |
> |
fprintf(stderr, "%s: %d sky patches\n", |
465 |
> |
progname, nskypatch); |
466 |
> |
if (sunsfp) |
467 |
> |
fprintf(stderr, "%s: outputting suns to file\n", |
468 |
> |
progname); |
469 |
|
if (rotation != 0) |
470 |
|
fprintf(stderr, "%s: rotating output %.0f degrees\n", |
471 |
|
progname, rotation); |
493 |
|
memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); |
494 |
|
continue; |
495 |
|
} |
488 |
– |
if (verbose && mo != last_monthly) |
489 |
– |
fprintf(stderr, "%s: stepping through month %d...\n", |
490 |
– |
progname, last_monthly=mo); |
496 |
|
/* compute solar position */ |
497 |
|
julian_date = jdate(mo, da); |
498 |
|
sda = sdec(julian_date); |
511 |
|
} |
512 |
|
/* compute sky patch values */ |
513 |
|
ComputeSky(mtx_data+mtx_offset); |
514 |
+ |
|
515 |
+ |
if (sunsfp) /* output sun if indicated */ |
516 |
+ |
OutputSun(ntsteps, sunsfp); |
517 |
+ |
|
518 |
+ |
if (avgSky < 0) /* no matrix? */ |
519 |
+ |
continue; |
520 |
+ |
|
521 |
|
AddDirect(mtx_data+mtx_offset); |
522 |
|
/* update cumulative sky? */ |
523 |
|
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
524 |
|
mtx_data[i] += mtx_data[mtx_offset+i]; |
525 |
+ |
/* monthly reporting */ |
526 |
+ |
if (verbose && mo != last_monthly) |
527 |
+ |
fprintf(stderr, "%s: stepping through month %d...\n", |
528 |
+ |
progname, last_monthly=mo); |
529 |
|
} |
530 |
+ |
if (!ntsteps) { |
531 |
+ |
fprintf(stderr, "%s: no valid time steps on input\n", progname); |
532 |
+ |
exit(1); |
533 |
+ |
} |
534 |
|
/* check for junk at end */ |
535 |
|
while ((i = fgetc(stdin)) != EOF) |
536 |
|
if (!isspace(i)) { |
541 |
|
fputs(buf, stderr); fputc('\n', stderr); |
542 |
|
break; |
543 |
|
} |
544 |
< |
if (!ntsteps) { |
545 |
< |
fprintf(stderr, "%s: no valid time steps on input\n", progname); |
546 |
< |
exit(1); |
547 |
< |
} |
544 |
> |
|
545 |
> |
if (avgSky < 0) /* no matrix output? */ |
546 |
> |
goto alldone; |
547 |
> |
|
548 |
|
dif = 1./(double)ntsteps; /* average sky? */ |
549 |
|
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
550 |
|
mtx_data[i] *= dif; |
605 |
|
if (ferror(stdout)) |
606 |
|
goto writerr; |
607 |
|
} |
608 |
< |
if (fflush(stdout) == EOF) |
608 |
> |
alldone: |
609 |
> |
if (fflush(NULL) == EOF) |
610 |
|
goto writerr; |
611 |
|
if (verbose) |
612 |
|
fprintf(stderr, "%s: done.\n", progname); |
613 |
|
exit(0); |
614 |
|
userr: |
615 |
< |
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", |
615 |
> |
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", |
616 |
|
progname); |
617 |
|
exit(1); |
618 |
|
fmterr: |
619 |
< |
fprintf(stderr, "%s: input weather tape format error\n", progname); |
619 |
> |
fprintf(stderr, "%s: weather tape format error in header\n", progname); |
620 |
|
exit(1); |
621 |
|
writerr: |
622 |
|
fprintf(stderr, "%s: write error on output\n", progname); |
774 |
|
*pdest++ += val_add*suncolor[1]; |
775 |
|
*pdest++ += val_add*suncolor[2]; |
776 |
|
} |
777 |
+ |
} |
778 |
+ |
|
779 |
+ |
/* Output a sun to indicated file if appropriate for this time step */ |
780 |
+ |
void |
781 |
+ |
OutputSun(int id, FILE *fp) |
782 |
+ |
{ |
783 |
+ |
double srad; |
784 |
+ |
FVECT sv; |
785 |
+ |
|
786 |
+ |
if ((altitude <= 0) | (dir_illum <= 1e-4)) |
787 |
+ |
return; |
788 |
+ |
|
789 |
+ |
srad = DegToRad(SUN_ANG_DEG/2.); |
790 |
+ |
srad = dir_illum/(WHTEFFICACY * PI*srad*srad); |
791 |
+ |
vector(sv, altitude, azimuth); |
792 |
+ |
fprintf(fp, "\nvoid light solar%d\n0\n0\n", id); |
793 |
+ |
fprintf(fp, "3 %.3e %.3e %.3e\n", srad*suncolor[0], |
794 |
+ |
srad*suncolor[1], srad*suncolor[2]); |
795 |
+ |
fprintf(fp, "\nsolar%d source sun%d\n0\n0\n", id, id); |
796 |
+ |
fprintf(fp, "4 %.6f %.6f %.6f %.4f\n", sv[0], sv[1], sv[2], SUN_ANG_DEG); |
797 |
|
} |
798 |
|
|
799 |
|
/* Initialize Reinhart sky patch positions (GW) */ |