--- ray/src/gen/gendaymtx.c 2013/01/18 19:56:03 2.2 +++ ray/src/gen/gendaymtx.c 2024/04/26 23:10:59 2.40 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: gendaymtx.c,v 2.2 2013/01/18 19:56:03 greg Exp $"; +static const char RCSid[] = "$Id: gendaymtx.c,v 2.40 2024/04/26 23:10:59 greg Exp $"; #endif /* * gendaymtx.c @@ -81,15 +81,15 @@ static const char RCSid[] = "$Id: gendaymtx.c,v 2.2 20 /* Include files */ #define _USE_MATH_DEFINES -#include #include -#include #include +#include "platform.h" #include "rtmath.h" +#include "rtio.h" #include "color.h" +#include "sun.h" -char *progname; /* Program name */ -char errmsg[128]; /* Error message buffer */ +char *progname; /* Program name */ const double DC_SolarConstantE = 1367.0; /* Solar constant W/m^2 */ const double DC_SolarConstantL = 127.5; /* Solar constant klux */ @@ -108,6 +108,7 @@ double sky_clearness; /* Sky clearness */ double solar_rad; /* Solar radiance */ double sun_zenith; /* Sun zenith angle (radians) */ int input = 0; /* Input type */ +int output = 0; /* Output type */ extern double dmax( double, double ); extern double CalcAirMass(); @@ -127,13 +128,16 @@ extern void CalcPerezParam( double, double, double, in extern void CalcSkyPatchLumin( float *parr ); extern void ComputeSky( float *parr ); + +extern double solar_sunset(int month, int day); +extern double solar_sunrise(int month, int day); + /* Degrees into radians */ #define DegToRad(deg) ((deg)*(PI/180.)) /* Radiuans into degrees */ #define RadToDeg(rad) ((rad)*(180./PI)) - /* Perez sky model coefficients */ /* Reference: Perez, R., R. Seals, and J. Michalsky, 1993. "All- */ @@ -208,7 +212,7 @@ static const CategoryBounds SkyClearCat[8] = { 1.950, 2.800 }, { 2.800, 4.500 }, { 4.500, 6.200 }, - { 6.200, 12.00 } /* Clear */ + { 6.200, 12.01 } /* Clear */ }; /* Luminous efficacy model coefficients */ @@ -246,72 +250,103 @@ static const ModelCoeff DirectLumEff[8] = { 101.18, 1.58, -1.10, -8.29 } }; -extern int jdate(int month, int day); -extern double stadj(int jd); -extern double sdec(int jd); -extern double salt(double sd, double st); -extern double sazi(double sd, double st); - /* sun calculation constants */ -extern double s_latitude; -extern double s_longitude; -extern double s_meridian; +#ifndef NSUNPATCH +#define NSUNPATCH 4 /* max. # patches to spread sun into */ +#endif -double grefl = 0.2; /* diffuse ground reflectance */ +#define SUN_ANG_DEG 0.533 /* sun full-angle in degrees */ +int nsuns = NSUNPATCH; /* number of sun patches to use */ +double fixed_sun_sa = -1; /* fixed solid angle per sun? */ + int verbose = 0; /* progress reports to stderr? */ int outfmt = 'a'; /* output format */ int rhsubdiv = 1; /* Reinhart sky subdivisions */ -float skycolor[3] = {.96, 1.004, 1.118}; /* sky coloration */ +COLOR skycolor = {.96, 1.004, 1.118}; /* sky coloration */ +COLOR suncolor = {1., 1., 1.}; /* sun color */ +COLOR grefl = {.2, .2, .2}; /* ground reflectance */ -int do_sun = 1; /* output direct solar contribution? */ - int nskypatch; /* number of Reinhart patches */ float *rh_palt; /* sky patch altitudes (radians) */ float *rh_pazi; /* sky patch azimuths (radians) */ float *rh_dom; /* sky patch solid angle (sr) */ -#define vector(v,alt,azi) ( (v)[1] = tcos(alt), \ - (v)[0] = (v)[1]*tsin(azi), \ - (v)[1] *= tcos(azi), \ - (v)[2] = tsin(alt) ) +#define vector(v,alt,azi) ( (v)[1] = cos(alt), \ + (v)[0] = (v)[1]*sin(azi), \ + (v)[1] *= cos(azi), \ + (v)[2] = sin(alt) ) #define rh_vector(v,i) vector(v,rh_palt[i],rh_pazi[i]) #define rh_cos(i) tsin(rh_palt[i]) +#define solar_minute(jd,hr) ((24*60)*((jd)-1)+(int)((hr)*60.+.5)) + extern int rh_init(void); extern float * resize_dmatrix(float *mtx_data, int nsteps, int npatch); +extern void OutputSun(int id, int goodsun, FILE *fp, FILE *mfp); extern void AddDirect(float *parr); + +static const char * +getfmtname(int fmt) +{ + switch (fmt) { + case 'a': + return("ascii"); + case 'f': + return("float"); + case 'd': + return("double"); + } + return("unknown"); +} + + int main(int argc, char *argv[]) { char buf[256]; + int doheader = 1; /* output header? */ + double rotation = 0; /* site rotation (degrees) */ double elevation; /* site elevation (meters) */ + int leap_day = 0; /* add leap day? */ + int sun_hours_only = 0; /* only output sun hours? */ int dir_is_horiz; /* direct is meas. on horizontal? */ + FILE *sunsfp = NULL; /* output file for individual suns */ + FILE *modsfp = NULL; /* modifier output file */ float *mtx_data = NULL; /* our matrix data */ - int ntsteps = 0; /* number of rows in matrix */ + int avgSky = 0; /* compute average sky r.t. matrix? */ + int ntsteps = 0; /* number of time steps */ + int tstorage = 0; /* number of allocated time steps */ + int nstored = 0; /* number of time steps in matrix */ int last_monthly = 0; /* month of last report */ int mo, da; /* month (1-12) and day (1-31) */ double hr; /* hour (local standard time) */ double dir, dif; /* direct and diffuse values */ int mtx_offset; int i, j; + double timeinterval = 0; progname = argv[0]; /* get options */ for (i = 1; i < argc && argv[i][0] == '-'; i++) switch (argv[i][1]) { - case 'g': - grefl = atof(argv[++i]); + case 'g': /* ground reflectance */ + grefl[0] = atof(argv[++i]); + grefl[1] = atof(argv[++i]); + grefl[2] = atof(argv[++i]); break; - case 'v': + case 'v': /* verbose progress reports */ verbose++; break; - case 'o': + case 'h': /* turn off header */ + doheader = 0; + break; + case 'o': /* output format */ switch (argv[i][2]) { case 'f': case 'd': @@ -322,23 +357,83 @@ main(int argc, char *argv[]) goto userr; } break; - case 'm': + case 'O': /* output type */ + switch (argv[i][2]) { + case '0': + output = 0; + break; + case '1': + output = 1; + break; + default: + goto userr; + } + if (argv[i][3]) + goto userr; + break; + case 'm': /* Reinhart subdivisions */ rhsubdiv = atoi(argv[++i]); break; - case 'c': + case 'c': /* sky color */ skycolor[0] = atof(argv[++i]); skycolor[1] = atof(argv[++i]); skycolor[2] = atof(argv[++i]); break; - case 'd': - do_sun = 1; + case 'D': /* output suns to file */ + if (strcmp(argv[++i], "-")) { + sunsfp = fopen(argv[i], "w"); + if (sunsfp == NULL) { + fprintf(stderr, + "%s: cannot open '%s' for output\n", + progname, argv[i]); + exit(1); + } + break; /* still may output matrix */ + } + sunsfp = stdout; /* sending to stdout, so... */ + /* fall through */ + case 'n': /* no matrix output */ + avgSky = -1; + rhsubdiv = 1; + /* fall through */ + case 'd': /* solar (direct) only */ skycolor[0] = skycolor[1] = skycolor[2] = 0; + grefl[0] = grefl[1] = grefl[2] = 0; break; - case 's': - do_sun = 0; - if (skycolor[1] <= 1e-4) - skycolor[0] = skycolor[1] = skycolor[2] = 1; + case 'M': /* send sun modifiers to file */ + if ((modsfp = fopen(argv[++i], "w")) == NULL) { + fprintf(stderr, "%s: cannot open '%s' for output\n", + progname, argv[i]); + exit(1); + } break; + case 's': /* sky only (no direct) */ + suncolor[0] = suncolor[1] = suncolor[2] = 0; + break; + case 'u': /* solar hours only */ + sun_hours_only = 1; + break; + case 'r': /* rotate distribution */ + if (argv[i][2] && argv[i][2] != 'z') + goto userr; + rotation = atof(argv[++i]); + break; + case '5': /* 5-phase calculation */ + nsuns = 1; + fixed_sun_sa = PI/360.*atof(argv[++i]); + if (fixed_sun_sa <= 0) { + fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n", + progname); + exit(1); + } + fixed_sun_sa *= fixed_sun_sa*PI; + break; + case 'A': /* compute average sky */ + avgSky = 1; + break; + case 'i': + timeinterval = atof(argv[++i]); + break; default: goto userr; } @@ -349,6 +444,9 @@ main(int argc, char *argv[]) progname, argv[i]); exit(1); } + if ((modsfp != NULL) & (sunsfp == NULL)) + fprintf(stderr, "%s: warning -M output will be empty without -D\n", + progname); if (verbose) { if (i == argc-1) fprintf(stderr, "%s: reading weather tape '%s'\n", @@ -391,34 +489,73 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: location '%s'\n", progname, buf); fprintf(stderr, "%s: (lat,long)=(%.1f,%.1f) degrees north, west\n", progname, s_latitude, s_longitude); - fprintf(stderr, "%s: %d sky patches per time step\n", - progname, nskypatch); + if (avgSky >= 0) + fprintf(stderr, "%s: %d sky patches\n", + progname, nskypatch); + if (sunsfp) + fprintf(stderr, "%s: outputting suns to %s\n", + progname, sunsfp==stdout ? "stdout" : "file"); + if (rotation != 0) + fprintf(stderr, "%s: rotating output %.0f degrees\n", + progname, rotation); } /* convert quantities to radians */ s_latitude = DegToRad(s_latitude); s_longitude = DegToRad(s_longitude); s_meridian = DegToRad(s_meridian); + /* initial allocation */ + mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch); /* process each time step in tape */ while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) { - double sda, sta; - /* make space for next time step */ - mtx_offset = 3*nskypatch*ntsteps++; - mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch); - if (dif <= 1e-4) { - memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch); - continue; - } - if (verbose && mo != last_monthly) - fprintf(stderr, "%s: stepping through month %d...\n", - progname, last_monthly=mo); + double sda, sta, st; + int sun_in_sky; /* compute solar position */ - julian_date = jdate(mo, da); + if ((mo == 2) & (da == 29)) { + julian_date = 60; + leap_day = 1; + } else + julian_date = jdate(mo, da) + leap_day; sda = sdec(julian_date); sta = stadj(julian_date); - altitude = salt(sda, hr+sta); - azimuth = sazi(sda, hr+sta) + PI; + st = hr + sta; + + if (timeinterval > 0) { + if (fabs(solar_sunrise(mo, da) - st) <= timeinterval/120) + st = (st + timeinterval/120 + solar_sunrise(mo, da))/2; + else if (fabs(solar_sunset(mo, da) - st) < timeinterval/120) + st = (st - timeinterval/120 + solar_sunset(mo, da))/2; + } + altitude = salt(sda, st); + sun_in_sky = (altitude > -DegToRad(SUN_ANG_DEG/2.)); + if (sun_hours_only && !sun_in_sky) + continue; /* skipping nighttime points */ + azimuth = sazi(sda, st) + PI - DegToRad(rotation); + + mtx_offset = 3*nskypatch*nstored; + nstored += !avgSky | !nstored; + /* make space for next row */ + if (nstored > tstorage) { + tstorage += (tstorage>>1) + nstored + 7; + mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch); + } + ntsteps++; /* keep count of time steps */ + + if (dir+dif <= 1e-4) { /* effectively nighttime? */ + if (!avgSky | !mtx_offset) + memset(mtx_data+mtx_offset, 0, + sizeof(float)*3*nskypatch); + /* output black sun? */ + if (sunsfp && sun_in_sky) + OutputSun(solar_minute(julian_date,hr), 0, + sunsfp, modsfp); + continue; + } + if (!sun_in_sky && dir > (input==1 ? 20. : 20.*WHTEFFICACY)) + fprintf(stderr, + "%s: warning - unusually bright at %.1f on %d-%d\n", + progname, hr, mo, da); /* convert measured values */ - if (dir_is_horiz && altitude > 0.) + if (dir_is_horiz && altitude > FTINY) dir /= sin(altitude); if (input == 1) { dir_irrad = dir; @@ -429,9 +566,28 @@ main(int argc, char *argv[]) } /* compute sky patch values */ ComputeSky(mtx_data+mtx_offset); - if (do_sun) - AddDirect(mtx_data+mtx_offset); + /* output sun if requested */ + if (sunsfp && sun_in_sky) + OutputSun(solar_minute(julian_date,hr), 1, + sunsfp, modsfp); + + if (avgSky < 0) /* no matrix? */ + continue; + + AddDirect(mtx_data+mtx_offset); + /* update cumulative sky? */ + for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) + mtx_data[i] += mtx_data[mtx_offset+i]; + /* monthly reporting */ + if (verbose && mo != last_monthly) + fprintf(stderr, "%s: stepping through month %d...\n", + progname, last_monthly=mo); + /* note whether leap-day was given */ } + if (!ntsteps) { + fprintf(stderr, "%s: no valid time steps on input\n", progname); + exit(1); + } /* check for junk at end */ while ((i = fgetc(stdin)) != EOF) if (!isspace(i)) { @@ -442,41 +598,63 @@ main(int argc, char *argv[]) fputs(buf, stderr); fputc('\n', stderr); break; } + + if (avgSky < 0) /* no matrix output? */ + goto alldone; + + dif = 1./(double)ntsteps; /* average sky? */ + for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) + mtx_data[i] *= dif; /* write out matrix */ + if (outfmt != 'a') + SET_FILE_BINARY(stdout); #ifdef getc_unlocked flockfile(stdout); #endif if (verbose) fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", - progname, outfmt=='a' ? "" : "binary ", ntsteps); + progname, outfmt=='a' ? "" : "binary ", nstored); + if (doheader) { + newheader("RADIANCE", stdout); + printargs(argc, argv, stdout); + printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude), + -RadToDeg(s_longitude)); + printf("NROWS=%d\n", nskypatch); + printf("NCOLS=%d\n", nstored); + printf("NCOMP=3\n"); + if ((outfmt == 'f') | (outfmt == 'd')) + fputendian(stdout); + fputformat((char *)getfmtname(outfmt), stdout); + putchar('\n'); + } /* patches are rows (outer sort) */ for (i = 0; i < nskypatch; i++) { mtx_offset = 3*i; switch (outfmt) { case 'a': - for (j = 0; j < ntsteps; j++) { - printf("%.3e %.3e %.3e\n", mtx_data[mtx_offset], + for (j = 0; j < nstored; j++) { + printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset], mtx_data[mtx_offset+1], mtx_data[mtx_offset+2]); mtx_offset += 3*nskypatch; } - if (ntsteps > 1) + if (nstored > 1) fputc('\n', stdout); break; case 'f': - for (j = 0; j < ntsteps; j++) { - fwrite(mtx_data+mtx_offset, sizeof(float), 3, + for (j = 0; j < nstored; j++) { + putbinary(mtx_data+mtx_offset, sizeof(float), 3, stdout); mtx_offset += 3*nskypatch; } break; case 'd': - for (j = 0; j < ntsteps; j++) { + for (j = 0; j < nstored; j++) { double ment[3]; ment[0] = mtx_data[mtx_offset]; ment[1] = mtx_data[mtx_offset+1]; ment[2] = mtx_data[mtx_offset+2]; - fwrite(ment, sizeof(double), 3, stdout); + putbinary(ment, sizeof(double), 3, stdout); mtx_offset += 3*nskypatch; } break; @@ -484,23 +662,25 @@ main(int argc, char *argv[]) if (ferror(stdout)) goto writerr; } - if (fflush(stdout) == EOF) +alldone: + if (fflush(NULL) == EOF) goto writerr; if (verbose) fprintf(stderr, "%s: done.\n", progname); exit(0); userr: - fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g refl][-c r g b][-o{f|d}] [tape.wea]\n", + 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", progname); exit(1); fmterr: - fprintf(stderr, "%s: input weather tape format error\n", progname); + fprintf(stderr, "%s: weather tape format error in header\n", progname); exit(1); writerr: fprintf(stderr, "%s: write error on output\n", progname); exit(1); } + /* Return maximum of two doubles */ double dmax( double a, double b ) { return (a > b) ? a : b; } @@ -511,19 +691,20 @@ ComputeSky(float *parr) { int index; /* Category index */ double norm_diff_illum; /* Normalized diffuse illuimnance */ - double zlumin; /* Zenith luminance */ int i; /* Calculate atmospheric precipitable water content */ apwc = CalcPrecipWater(dew_point); - /* Limit solar altitude to keep circumsolar off zenith */ - if (altitude > DegToRad(87.0)) - altitude = DegToRad(87.0); + /* Calculate sun zenith angle (don't let it dip below horizon) */ + /* Also limit minimum angle to keep circumsolar off zenith */ + if (altitude <= 0.0) + sun_zenith = DegToRad(90.0); + else if (altitude >= DegToRad(87.0)) + sun_zenith = DegToRad(3.0); + else + sun_zenith = DegToRad(90.0) - altitude; - /* Calculate sun zenith angle */ - sun_zenith = DegToRad(90.0) - altitude; - /* Compute the inputs for the calculation of the sky distribution */ if (input == 0) /* XXX never used */ @@ -542,6 +723,18 @@ ComputeSky(float *parr) sky_brightness = CalcSkyBrightness(); sky_clearness = CalcSkyClearness(); + /* Limit sky clearness */ + if (sky_clearness > 11.9) + sky_clearness = 11.9; + else if (sky_clearness < 1.0) + sky_clearness = 1.0; + + /* Limit sky brightness */ + if (sky_brightness < 0.01) + sky_brightness = 0.01; + else if (sky_brightness > 0.6) + sky_brightness = 0.6; + /* Calculate illuminance */ index = GetCategoryIndex(); diff_illum = diff_irrad * CalcDiffuseIllumRatio(index); @@ -553,16 +746,21 @@ ComputeSky(float *parr) index = CalcSkyParamFromIllum(); } - if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ - memset(parr, 0, sizeof(float)*3*nskypatch); - return; + if (output == 1) { /* hack for solar radiance */ + diff_illum = diff_irrad * WHTEFFICACY; + dir_illum = dir_irrad * WHTEFFICACY; } /* Compute ground radiance (include solar contribution if any) */ - parr[0] = diff_illum * (1./PI/WHTEFFICACY); + parr[0] = diff_illum; if (altitude > 0) - parr[0] += dir_illum * sin(altitude) * (1./PI/WHTEFFICACY); - parr[2] = parr[1] = parr[0]; + parr[0] += dir_illum * sin(altitude); + parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY); + multcolor(parr, grefl); + if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ + memset(parr+3, 0, sizeof(float)*3*(nskypatch-1)); + return; + } /* Calculate Perez sky model parameters */ CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index); @@ -572,36 +770,59 @@ ComputeSky(float *parr) /* Calculate relative horizontal illuminance */ norm_diff_illum = CalcRelHorzIllum(parr); + /* Check for zero sky -- make uniform in that case */ + if (norm_diff_illum <= FTINY) { + for (i = 1; i < nskypatch; i++) + setcolor(parr+3*i, 1., 1., 1.); + norm_diff_illum = PI; + } /* Normalization coefficient */ norm_diff_illum = diff_illum / norm_diff_illum; - /* Calculate relative zenith luminance */ - zlumin = CalcRelLuminance(sun_zenith, 0.0); - - /* Calculate absolute zenith illuminance */ - zlumin *= norm_diff_illum; - /* Apply to sky patches to get absolute radiance values */ for (i = 1; i < nskypatch; i++) { - scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY)); + scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY)); multcolor(parr+3*i, skycolor); } } + +double +solar_sunset(int month, int day) +{ + float W; + W = -1 * (tan(s_latitude) * tan(sdec(jdate(month, day)))); + return(12 + (M_PI / 2 - atan2(W, sqrt(1 - W * W))) * 180 / (M_PI * 15)); +} + + +double +solar_sunrise(int month, int day) +{ + float W; + W = -1 * (tan(s_latitude) * tan(sdec(jdate(month, day)))); + return(12 - (M_PI / 2 - atan2(W, sqrt(1 - W * W))) * 180 / (M_PI * 15)); +} + + /* Add in solar direct to nearest sky patches (GW) */ void AddDirect(float *parr) { FVECT svec; - double near_dprod[4]; - int near_patch[4]; - double wta[4], wtot; + double near_dprod[NSUNPATCH]; + int near_patch[NSUNPATCH]; + double wta[NSUNPATCH], wtot; int i, j, p; - if (!do_sun || dir_illum < 1e-4) + if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4) return; - /* identify 4 closest patches */ - for (i = 4; i--; ) + /* identify nsuns closest patches */ + if (nsuns > NSUNPATCH) + nsuns = NSUNPATCH; + else if (nsuns <= 0) + nsuns = 1; + for (i = nsuns; i--; ) near_dprod[i] = -1.; vector(svec, altitude, azimuth); for (p = 1; p < nskypatch; p++) { @@ -609,9 +830,9 @@ AddDirect(float *parr) double dprod; rh_vector(pvec, p); dprod = DOT(pvec, svec); - for (i = 0; i < 4; i++) + for (i = 0; i < nsuns; i++) if (dprod > near_dprod[i]) { - for (j = 4; --j > i; ) { + for (j = nsuns; --j > i; ) { near_dprod[j] = near_dprod[j-1]; near_patch[j] = near_patch[j-1]; } @@ -621,19 +842,41 @@ AddDirect(float *parr) } } wtot = 0; /* weight by proximity */ - for (i = 4; i--; ) + for (i = nsuns; i--; ) wtot += wta[i] = 1./(1.002 - near_dprod[i]); /* add to nearest patch radiances */ - for (i = 4; i--; ) { + for (i = nsuns; i--; ) { float *pdest = parr + 3*near_patch[i]; - float val_add = wta[i] * dir_illum / - (WHTEFFICACY * wtot * rh_dom[near_patch[i]]); - *pdest++ += val_add; - *pdest++ += val_add; - *pdest++ += val_add; + float val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot); + + val_add /= (fixed_sun_sa > 0) ? fixed_sun_sa + : rh_dom[near_patch[i]] ; + *pdest++ += val_add*suncolor[0]; + *pdest++ += val_add*suncolor[1]; + *pdest++ += val_add*suncolor[2]; } } +/* Output a sun to indicated file if appropriate for this time step */ +void +OutputSun(int id, int goodsun, FILE *fp, FILE *mfp) +{ + double srad; + FVECT sv; + + srad = DegToRad(SUN_ANG_DEG/2.); + srad = goodsun ? dir_illum/(WHTEFFICACY * PI*srad*srad) : 0; + vector(sv, altitude, azimuth); + fprintf(fp, "\nvoid light solar%d\n0\n0\n", id); + fprintf(fp, "3 %.3e %.3e %.3e\n", srad*suncolor[0], + srad*suncolor[1], srad*suncolor[2]); + fprintf(fp, "\nsolar%d source sun%d\n0\n0\n", id, id); + fprintf(fp, "4 %.6f %.6f %.6f %.4f\n", sv[0], sv[1], sv[2], SUN_ANG_DEG); + + if (mfp != NULL) /* saving modifier IDs? */ + fprintf(mfp, "solar%d\n", id); +} + /* Initialize Reinhart sky patch positions (GW) */ int rh_init(void) @@ -665,7 +908,8 @@ rh_init(void) for (i = 0; i < NROW*rhsubdiv; i++) { const float ralt = alpha*(i + .5); const int ninrow = tnaz[i/rhsubdiv]*rhsubdiv; - const float dom = (sin(alpha*(i+1)) - sin(alpha*i))/ninrow; + const float dom = 2.*PI*(sin(alpha*(i+1)) - sin(alpha*i)) / + (double)ninrow; for (j = 0; j < ninrow; j++) { rh_palt[p] = ralt; rh_pazi[p] = 2.*PI * j / (double)ninrow; @@ -770,7 +1014,7 @@ double CalcSkyClearness() double sz_cubed; /* Sun zenith angle cubed */ /* Calculate sun zenith angle cubed */ - sz_cubed = pow(sun_zenith, 3.0); + sz_cubed = sun_zenith*sun_zenith*sun_zenith; return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 * sz_cubed) / (1.0 + 1.041 * sz_cubed); @@ -801,7 +1045,7 @@ double CalcDiffuseIrradiance() double CalcDirectIrradiance() { return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041 - * pow(sun_zenith, 3.0))); + * sun_zenith*sun_zenith*sun_zenith)); } /* Calculate sky brightness and clearness from illuminance values */ @@ -827,9 +1071,19 @@ int CalcSkyParamFromIllum() sky_clearness = 12.0; /* Limit sky brightness */ - if (sky_brightness < 0.05) + if (sky_brightness < 0.01) sky_brightness = 0.01; + if (sky_clearness < 1.0000) + { + sky_clearness = 1.0000; + } + + if (sky_brightness > 0.6) + { + sky_brightness = 0.6; + } + while (((fabs(diff_irrad - test1) > 10.0) || (fabs(dir_irrad - test2) > 10.0)) && !(counter == 5)) { @@ -840,7 +1094,9 @@ int CalcSkyParamFromIllum() /* Convert illuminance to irradiance */ index = GetCategoryIndex(); diff_irrad = diff_illum / CalcDiffuseIllumRatio(index); - dir_irrad = dir_illum / CalcDirectIllumRatio(index); + dir_irrad = CalcDirectIllumRatio(index); + if (dir_irrad > 0.1) + dir_irrad = dir_illum / dir_irrad; /* Calculate sky brightness and clearness */ sky_brightness = CalcSkyBrightness(); @@ -851,8 +1107,18 @@ int CalcSkyParamFromIllum() sky_clearness = 12.0; /* Limit sky brightness */ - if (sky_brightness < 0.05) + if (sky_brightness < 0.01) sky_brightness = 0.01; + + if (sky_clearness < 1.0000) + { + sky_clearness = 1.0000; + } + + if (sky_brightness > 0.6) + { + sky_brightness = 0.6; + } } return GetCategoryIndex(); @@ -937,9 +1203,9 @@ double CalcRelHorzIllum( float *parr ) double rh_illum = 0.0; /* Relative horizontal illuminance */ for (i = 1; i < nskypatch; i++) - rh_illum += parr[3*i+1] * rh_cos(i); + rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i]; - return rh_illum * (2.0 * PI / (nskypatch-1)); + return rh_illum; } /* Calculate earth orbit eccentricity correction factor */