88 |
|
#include "rtio.h" |
89 |
|
#include "color.h" |
90 |
|
#include "sun.h" |
91 |
+ |
#include "loadEPW.h" |
92 |
|
|
93 |
|
char *progname; /* Program name */ |
94 |
|
const double DC_SolarConstantE = 1367.0; /* Solar constant W/m^2 */ |
129 |
|
extern void CalcSkyPatchLumin( float *parr ); |
130 |
|
extern void ComputeSky( float *parr ); |
131 |
|
|
132 |
+ |
|
133 |
+ |
extern double solar_sunset(int month, int day); |
134 |
+ |
extern double solar_sunrise(int month, int day); |
135 |
+ |
|
136 |
|
/* Degrees into radians */ |
137 |
|
#define DegToRad(deg) ((deg)*(PI/180.)) |
138 |
|
|
310 |
|
int |
311 |
|
main(int argc, char *argv[]) |
312 |
|
{ |
313 |
< |
char buf[256]; |
313 |
> |
EPWheader *epw = NULL; /* EPW/WEA input file */ |
314 |
> |
EPWrecord erec; /* current EPW/WEA input record */ |
315 |
> |
float dpthist[2]; /* previous dew point temps */ |
316 |
> |
double dir, dif; |
317 |
|
int doheader = 1; /* output header? */ |
318 |
|
double rotation = 0; /* site rotation (degrees) */ |
319 |
|
double elevation; /* site elevation (meters) */ |
328 |
|
int tstorage = 0; /* number of allocated time steps */ |
329 |
|
int nstored = 0; /* number of time steps in matrix */ |
330 |
|
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) */ |
325 |
– |
double dir, dif; /* direct and diffuse values */ |
331 |
|
int mtx_offset; |
332 |
|
int i, j; |
333 |
+ |
double timeinterval = 0; |
334 |
|
|
335 |
|
progname = argv[0]; |
336 |
|
/* get options */ |
432 |
|
case 'A': /* compute average sky */ |
433 |
|
avgSky = 1; |
434 |
|
break; |
435 |
+ |
case 'i': |
436 |
+ |
timeinterval = atof(argv[++i]); |
437 |
+ |
break; |
438 |
|
default: |
439 |
|
goto userr; |
440 |
|
} |
441 |
< |
if (i < argc-1) |
441 |
> |
if ((i < argc-1) | (i > argc)) |
442 |
|
goto userr; |
443 |
< |
if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
444 |
< |
fprintf(stderr, "%s: cannot open '%s' for input\n", |
436 |
< |
progname, argv[i]); |
443 |
> |
epw = EPWopen(argv[i]); |
444 |
> |
if (epw == NULL) |
445 |
|
exit(1); |
438 |
– |
} |
446 |
|
if ((modsfp != NULL) & (sunsfp == NULL)) |
447 |
|
fprintf(stderr, "%s: warning -M output will be empty without -D\n", |
448 |
|
progname); |
455 |
|
progname); |
456 |
|
} |
457 |
|
/* read weather tape header */ |
458 |
< |
if (scanf("place %[^\r\n] ", buf) != 1) |
459 |
< |
goto fmterr; |
460 |
< |
if (scanf("latitude %lf\n", &s_latitude) != 1) |
461 |
< |
goto fmterr; |
462 |
< |
if (scanf("longitude %lf\n", &s_longitude) != 1) |
463 |
< |
goto fmterr; |
464 |
< |
if (scanf("time_zone %lf\n", &s_meridian) != 1) |
458 |
< |
goto fmterr; |
459 |
< |
if (scanf("site_elevation %lf\n", &elevation) != 1) |
460 |
< |
goto fmterr; |
461 |
< |
if (scanf("weather_data_file_units %d\n", &input) != 1) |
462 |
< |
goto fmterr; |
463 |
< |
switch (input) { /* translate units */ |
464 |
< |
case 1: |
458 |
> |
s_latitude = epw->loc.latitude; |
459 |
> |
s_longitude = -epw->loc.longitude; |
460 |
> |
s_meridian = -15.*epw->loc.timezone; |
461 |
> |
elevation = epw->loc.elevation; |
462 |
> |
switch (epw->isWEA) { /* translate units */ |
463 |
> |
case WEAnot: |
464 |
> |
case WEAradnorm: |
465 |
|
input = 1; /* radiometric quantities */ |
466 |
|
dir_is_horiz = 0; /* direct is perpendicular meas. */ |
467 |
|
break; |
468 |
< |
case 2: |
468 |
> |
case WEAradhoriz: |
469 |
|
input = 1; /* radiometric quantities */ |
470 |
|
dir_is_horiz = 1; /* solar measured horizontally */ |
471 |
|
break; |
472 |
< |
case 3: |
472 |
> |
case WEAphotnorm: |
473 |
|
input = 2; /* photometric quantities */ |
474 |
|
dir_is_horiz = 0; /* direct is perpendicular meas. */ |
475 |
|
break; |
478 |
|
} |
479 |
|
rh_init(); /* initialize sky patches */ |
480 |
|
if (verbose) { |
481 |
< |
fprintf(stderr, "%s: location '%s'\n", progname, buf); |
481 |
> |
fprintf(stderr, "%s: location '%s, %s'\n", progname, |
482 |
> |
epw->loc.city, epw->loc.country); |
483 |
|
fprintf(stderr, "%s: (lat,long)=(%.1f,%.1f) degrees north, west\n", |
484 |
|
progname, s_latitude, s_longitude); |
485 |
|
if (avgSky >= 0) |
486 |
|
fprintf(stderr, "%s: %d sky patches\n", |
487 |
|
progname, nskypatch); |
488 |
|
if (sunsfp) |
489 |
< |
fprintf(stderr, "%s: outputting suns to file\n", |
490 |
< |
progname); |
489 |
> |
fprintf(stderr, "%s: outputting suns to %s\n", |
490 |
> |
progname, sunsfp==stdout ? "stdout" : "file"); |
491 |
|
if (rotation != 0) |
492 |
|
fprintf(stderr, "%s: rotating output %.0f degrees\n", |
493 |
|
progname, rotation); |
498 |
|
s_meridian = DegToRad(s_meridian); |
499 |
|
/* initial allocation */ |
500 |
|
mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch); |
501 |
+ |
dpthist[0] = -100; |
502 |
|
/* process each time step in tape */ |
503 |
< |
while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) { |
504 |
< |
double sda, sta; |
503 |
> |
while ((j = EPWread(epw, &erec)) > 0) { |
504 |
> |
const int mo = erec.date.month+1; |
505 |
> |
const int da = erec.date.day; |
506 |
> |
const double hr = erec.date.hour; |
507 |
> |
double sda, sta, st; |
508 |
|
int sun_in_sky; |
509 |
+ |
/* 3-hour dew point temp */ |
510 |
+ |
if (EPWisset(&erec,dptemp)) { |
511 |
+ |
if (dpthist[0] < -99) |
512 |
+ |
dpthist[0] = dpthist[1] = erec.dptemp; |
513 |
+ |
dew_point = (1./3.)*(dpthist[0] + dpthist[1] + erec.dptemp); |
514 |
+ |
dpthist[0] = dpthist[1]; dpthist[1] = erec.dptemp; |
515 |
+ |
} else |
516 |
+ |
dpthist[0] = -100; |
517 |
|
/* compute solar position */ |
518 |
|
if ((mo == 2) & (da == 29)) { |
519 |
|
julian_date = 60; |
522 |
|
julian_date = jdate(mo, da) + leap_day; |
523 |
|
sda = sdec(julian_date); |
524 |
|
sta = stadj(julian_date); |
525 |
< |
altitude = salt(sda, hr+sta); |
525 |
> |
st = hr + sta; |
526 |
> |
|
527 |
> |
if (timeinterval > 0) { |
528 |
> |
if (fabs(solar_sunrise(mo, da) - st) <= timeinterval/120) |
529 |
> |
st = (st + timeinterval/120 + solar_sunrise(mo, da))/2; |
530 |
> |
else if (fabs(solar_sunset(mo, da) - st) < timeinterval/120) |
531 |
> |
st = (st - timeinterval/120 + solar_sunset(mo, da))/2; |
532 |
> |
} |
533 |
> |
altitude = salt(sda, st); |
534 |
|
sun_in_sky = (altitude > -DegToRad(SUN_ANG_DEG/2.)); |
535 |
< |
if (sun_hours_only && !sun_in_sky) |
535 |
> |
if (sun_hours_only & !sun_in_sky) |
536 |
|
continue; /* skipping nighttime points */ |
537 |
< |
azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation); |
537 |
> |
azimuth = sazi(sda, st) + PI - DegToRad(rotation); |
538 |
|
|
539 |
+ |
switch (epw->isWEA) { /* translate units */ |
540 |
+ |
case WEAnot: |
541 |
+ |
case WEAradnorm: |
542 |
+ |
if (!EPWisset(&erec,dirirrad) | |
543 |
+ |
!EPWisset(&erec,horizdiffirrad)) { |
544 |
+ |
fprintf(stderr, "%s: missing required irradiances at line %d\n", |
545 |
+ |
progname, epw->lino); |
546 |
+ |
exit(1); |
547 |
+ |
} |
548 |
+ |
dir = erec.dirirrad; |
549 |
+ |
dif = erec.horizdiffirrad; |
550 |
+ |
break; |
551 |
+ |
case WEAradhoriz: |
552 |
+ |
dir = erec.globhorizirrad - erec.horizdiffirrad; |
553 |
+ |
dif = erec.horizdiffirrad; |
554 |
+ |
break; |
555 |
+ |
case WEAphotnorm: |
556 |
+ |
dir = erec.dirillum; |
557 |
+ |
dif = erec.diffillum; |
558 |
+ |
break; |
559 |
+ |
} |
560 |
|
mtx_offset = 3*nskypatch*nstored; |
561 |
|
nstored += !avgSky | !nstored; |
562 |
|
/* make space for next row */ |
610 |
|
progname, last_monthly=mo); |
611 |
|
/* note whether leap-day was given */ |
612 |
|
} |
613 |
+ |
if (j != EOF) { |
614 |
+ |
fprintf(stderr, "%s: error on input\n", progname); |
615 |
+ |
exit(1); |
616 |
+ |
} |
617 |
+ |
EPWclose(epw); epw = NULL; |
618 |
|
if (!ntsteps) { |
619 |
|
fprintf(stderr, "%s: no valid time steps on input\n", progname); |
620 |
|
exit(1); |
621 |
|
} |
575 |
– |
/* check for junk at end */ |
576 |
– |
while ((i = fgetc(stdin)) != EOF) |
577 |
– |
if (!isspace(i)) { |
578 |
– |
fprintf(stderr, "%s: warning - unexpected data past EOT: ", |
579 |
– |
progname); |
580 |
– |
buf[0] = i; buf[1] = '\0'; |
581 |
– |
fgets(buf+1, sizeof(buf)-1, stdin); |
582 |
– |
fputs(buf, stderr); fputc('\n', stderr); |
583 |
– |
break; |
584 |
– |
} |
585 |
– |
|
622 |
|
if (avgSky < 0) /* no matrix output? */ |
623 |
|
goto alldone; |
624 |
|
|
700 |
|
exit(1); |
701 |
|
} |
702 |
|
|
703 |
+ |
|
704 |
|
/* Return maximum of two doubles */ |
705 |
|
double dmax( double a, double b ) |
706 |
|
{ return (a > b) ? a : b; } |
712 |
|
int index; /* Category index */ |
713 |
|
double norm_diff_illum; /* Normalized diffuse illuimnance */ |
714 |
|
int i; |
715 |
< |
|
715 |
> |
|
716 |
|
/* Calculate atmospheric precipitable water content */ |
717 |
|
apwc = CalcPrecipWater(dew_point); |
718 |
|
|
746 |
|
/* Limit sky clearness */ |
747 |
|
if (sky_clearness > 11.9) |
748 |
|
sky_clearness = 11.9; |
749 |
+ |
else if (sky_clearness < 1.0) |
750 |
+ |
sky_clearness = 1.0; |
751 |
|
|
752 |
|
/* Limit sky brightness */ |
753 |
|
if (sky_brightness < 0.01) |
754 |
|
sky_brightness = 0.01; |
755 |
+ |
else if (sky_brightness > 0.6) |
756 |
+ |
sky_brightness = 0.6; |
757 |
|
|
758 |
|
/* Calculate illuminance */ |
759 |
|
index = GetCategoryIndex(); |
806 |
|
} |
807 |
|
} |
808 |
|
|
809 |
+ |
|
810 |
+ |
double |
811 |
+ |
solar_sunset(int month, int day) |
812 |
+ |
{ |
813 |
+ |
float W; |
814 |
+ |
W = -1 * (tan(s_latitude) * tan(sdec(jdate(month, day)))); |
815 |
+ |
return(12 + (M_PI / 2 - atan2(W, sqrt(1 - W * W))) * 180 / (M_PI * 15)); |
816 |
+ |
} |
817 |
+ |
|
818 |
+ |
|
819 |
+ |
double |
820 |
+ |
solar_sunrise(int month, int day) |
821 |
+ |
{ |
822 |
+ |
float W; |
823 |
+ |
W = -1 * (tan(s_latitude) * tan(sdec(jdate(month, day)))); |
824 |
+ |
return(12 - (M_PI / 2 - atan2(W, sqrt(1 - W * W))) * 180 / (M_PI * 15)); |
825 |
+ |
} |
826 |
+ |
|
827 |
+ |
|
828 |
|
/* Add in solar direct to nearest sky patches (GW) */ |
829 |
|
void |
830 |
|
AddDirect(float *parr) |
1094 |
|
if (sky_brightness < 0.01) |
1095 |
|
sky_brightness = 0.01; |
1096 |
|
|
1097 |
+ |
if (sky_clearness < 1.0000) |
1098 |
+ |
{ |
1099 |
+ |
sky_clearness = 1.0000; |
1100 |
+ |
} |
1101 |
+ |
|
1102 |
+ |
if (sky_brightness > 0.6) |
1103 |
+ |
{ |
1104 |
+ |
sky_brightness = 0.6; |
1105 |
+ |
} |
1106 |
+ |
|
1107 |
|
while (((fabs(diff_irrad - test1) > 10.0) || |
1108 |
|
(fabs(dir_irrad - test2) > 10.0)) && !(counter == 5)) |
1109 |
|
{ |
1129 |
|
/* Limit sky brightness */ |
1130 |
|
if (sky_brightness < 0.01) |
1131 |
|
sky_brightness = 0.01; |
1132 |
+ |
|
1133 |
+ |
if (sky_clearness < 1.0000) |
1134 |
+ |
{ |
1135 |
+ |
sky_clearness = 1.0000; |
1136 |
+ |
} |
1137 |
+ |
|
1138 |
+ |
if (sky_brightness > 0.6) |
1139 |
+ |
{ |
1140 |
+ |
sky_brightness = 0.6; |
1141 |
+ |
} |
1142 |
|
} |
1143 |
|
|
1144 |
|
return GetCategoryIndex(); |