ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gendaymtx.c
(Generate patch)

Comparing ray/src/gen/gendaymtx.c (file contents):
Revision 2.27 by greg, Tue Jun 25 00:09:45 2019 UTC vs.
Revision 2.41 by greg, Wed Feb 26 20:39:28 2025 UTC

# Line 81 | Line 81 | static const char RCSid[] = "$Id$";
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"
90 #include "resolu.h"
91 #include "platform.h"
89   #include "color.h"
90 < #include "resolu.h"
90 > #include "sun.h"
91 > #include "loadEPW.h"
92  
93 < char *progname;                                                         /* Program name */
96 < char errmsg[128];                                                       /* Error message buffer */
93 > char *progname;                                 /* Program name */
94   const double DC_SolarConstantE = 1367.0;        /* Solar constant W/m^2 */
95   const double DC_SolarConstantL = 127.5;         /* Solar constant klux */
96  
# Line 132 | Line 129 | extern void CalcPerezParam( double, double, double, in
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  
139   /* Radiuans into degrees */
140   #define RadToDeg(rad)   ((rad)*(180./PI))
141  
141
142   /* Perez sky model coefficients */
143  
144   /* Reference:   Perez, R., R. Seals, and J. Michalsky, 1993. "All- */
# Line 255 | Line 255 | static const ModelCoeff DirectLumEff[8] =
255   #define NSUNPATCH       4               /* max. # patches to spread sun into */
256   #endif
257  
258 < extern int jdate(int month, int day);
259 < extern double stadj(int  jd);
260 < extern double sdec(int  jd);
261 < extern double salt(double sd, double st);
262 < extern double sazi(double sd, double st);
263 <                                        /* sun calculation constants */
264 < extern double  s_latitude;
265 < extern double  s_longitude;
266 < extern double  s_meridian;
258 > #define SUN_ANG_DEG     0.533           /* sun full-angle in degrees */
259  
260   int             nsuns = NSUNPATCH;      /* number of sun patches to use */
261   double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
# Line 283 | Line 275 | float          *rh_palt;               /* sky patch altitudes (radians) */
275   float           *rh_pazi;               /* sky patch azimuths (radians) */
276   float           *rh_dom;                /* sky patch solid angle (sr) */
277  
278 < #define         vector(v,alt,azi)       (       (v)[1] = tcos(alt), \
279 <                                                (v)[0] = (v)[1]*tsin(azi), \
280 <                                                (v)[1] *= tcos(azi), \
281 <                                                (v)[2] = tsin(alt) )
278 > #define         vector(v,alt,azi)       (       (v)[1] = cos(alt), \
279 >                                                (v)[0] = (v)[1]*sin(azi), \
280 >                                                (v)[1] *= cos(azi), \
281 >                                                (v)[2] = sin(alt) )
282  
283   #define         rh_vector(v,i)          vector(v,rh_palt[i],rh_pazi[i])
284  
285   #define         rh_cos(i)               tsin(rh_palt[i])
286  
287 + #define         solar_minute(jd,hr)     ((24*60)*((jd)-1)+(int)((hr)*60.+.5))
288 +
289   extern int      rh_init(void);
290   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
291 + extern void     OutputSun(int id, int goodsun, FILE *fp, FILE *mfp);
292   extern void     AddDirect(float *parr);
293  
294  
# Line 315 | Line 310 | getfmtname(int fmt)
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) */
320 +        int     leap_day = 0;           /* add leap day? */
321 +        int     sun_hours_only = 0;     /* only output sun hours? */
322          int     dir_is_horiz;           /* direct is meas. on horizontal? */
323 +        FILE    *sunsfp = NULL;         /* output file for individual suns */
324 +        FILE    *modsfp = NULL;         /* modifier output file */
325          float   *mtx_data = NULL;       /* our matrix data */
324        int     ntsteps = 0;            /* number of time steps */
326          int     avgSky = 0;             /* compute average sky r.t. matrix? */
327 <        int     nrows = 0;              /* number of rows in matrix */
328 <        int     rows_alloc = 0;         /* number of allocated rows */
327 >        int     ntsteps = 0;            /* number of time steps */
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 */
329        int     inconsistent = 0;       /* inconsistent options set? */
330        int     mo, da;                 /* month (1-12) and day (1-31) */
331        double  hr;                     /* hour (local standard time) */
332        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 */
# Line 377 | Line 376 | main(int argc, char *argv[])
376                          rhsubdiv = atoi(argv[++i]);
377                          break;
378                  case 'c':                       /* sky color */
380                        inconsistent |= (skycolor[1] <= 1e-4);
379                          skycolor[0] = atof(argv[++i]);
380                          skycolor[1] = atof(argv[++i]);
381                          skycolor[2] = atof(argv[++i]);
382                          break;
383 +                case 'D':                       /* output suns to file */
384 +                        if (strcmp(argv[++i], "-")) {
385 +                                sunsfp = fopen(argv[i], "w");
386 +                                if (sunsfp == NULL) {
387 +                                        fprintf(stderr,
388 +                                        "%s: cannot open '%s' for output\n",
389 +                                                        progname, argv[i]);
390 +                                        exit(1);
391 +                                }
392 +                                break;          /* still may output matrix */
393 +                        }
394 +                        sunsfp = stdout;        /* sending to stdout, so... */
395 +                        /* fall through */
396 +                case 'n':                       /* no matrix output */
397 +                        avgSky = -1;
398 +                        rhsubdiv = 1;
399 +                        /* fall through */
400                  case 'd':                       /* solar (direct) only */
401                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
402 <                        if (suncolor[1] <= 1e-4) {
403 <                                inconsistent = 1;
404 <                                suncolor[0] = suncolor[1] = suncolor[2] = 1;
402 >                        grefl[0] = grefl[1] = grefl[2] = 0;
403 >                        break;
404 >                case 'M':                       /* send sun modifiers to file */
405 >                        if ((modsfp = fopen(argv[++i], "w")) == NULL) {
406 >                                fprintf(stderr, "%s: cannot open '%s' for output\n",
407 >                                                progname, argv[i]);
408 >                                exit(1);
409                          }
410                          break;
411                  case 's':                       /* sky only (no direct) */
412                          suncolor[0] = suncolor[1] = suncolor[2] = 0;
394                        if (skycolor[1] <= 1e-4) {
395                                inconsistent = 1;
396                                skycolor[0] = skycolor[1] = skycolor[2] = 1;
397                        }
413                          break;
414 +                case 'u':                       /* solar hours only */
415 +                        sun_hours_only = 1;
416 +                        break;
417                  case 'r':                       /* rotate distribution */
418                          if (argv[i][2] && argv[i][2] != 'z')
419                                  goto userr;
# Line 406 | Line 424 | main(int argc, char *argv[])
424                          fixed_sun_sa = PI/360.*atof(argv[++i]);
425                          if (fixed_sun_sa <= 0) {
426                                  fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
427 <                                                argv[0]);
427 >                                                progname);
428                                  exit(1);
429                          }
430                          fixed_sun_sa *= fixed_sun_sa*PI;
# Line 414 | Line 432 | main(int argc, char *argv[])
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 (inconsistent)
444 <                fprintf(stderr, "%s: WARNING: inconsistent -s, -d, -c options!\n",
424 <                                progname);
425 <        if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
426 <                fprintf(stderr, "%s: cannot open '%s' for input\n",
427 <                                progname, argv[i]);
443 >        epw = EPWopen(argv[i]);
444 >        if (epw == NULL)
445                  exit(1);
446 <        }
446 >        if ((modsfp != NULL) & (sunsfp == NULL))
447 >                fprintf(stderr, "%s: warning -M output will be empty without -D\n",
448 >                                progname);
449          if (verbose) {
450                  if (i == argc-1)
451                          fprintf(stderr, "%s: reading weather tape '%s'\n",
# Line 436 | Line 455 | main(int argc, char *argv[])
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)
446 <                goto fmterr;
447 <        if (scanf("site_elevation %lf\n", &elevation) != 1)
448 <                goto fmterr;
449 <        if (scanf("weather_data_file_units %d\n", &input) != 1)
450 <                goto fmterr;
451 <        switch (input) {                /* translate units */
452 <        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;
# Line 466 | Line 478 | main(int argc, char *argv[])
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 <                fprintf(stderr, "%s: %d sky patches per time step\n",
486 <                                progname, nskypatch);
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 %s\n",
490 >                                        progname, sunsfp==stdout ? "stdout" : "file");
491                  if (rotation != 0)
492                          fprintf(stderr, "%s: rotating output %.0f degrees\n",
493                                          progname, rotation);
# Line 480 | Line 497 | main(int argc, char *argv[])
497          s_longitude = DegToRad(s_longitude);
498          s_meridian = DegToRad(s_meridian);
499                                          /* initial allocation */
500 <        mtx_data = resize_dmatrix(mtx_data, rows_alloc=2, nskypatch);
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 >                int             mo = erec.date.month+1;
505 >                int             da = erec.date.day;
506 >                double          hr = erec.date.hour;
507 >                double          sda, sta, st;
508 >                int             sun_in_sky;
509 >                                        /* compute solar position */
510 >                if ((mo == 2) & (da == 29)) {
511 >                        julian_date = 60;
512 >                        leap_day = 1;
513 >                } else
514 >                        julian_date = jdate(mo, da) + leap_day;
515 >                sda = sdec(julian_date);
516 >                sta = stadj(julian_date);
517 >                st = hr + sta;
518 >                
519 >                if (timeinterval > 0) {
520 >                        if (fabs(solar_sunrise(mo, da) - st) <= timeinterval/120)
521 >                                st = (st + timeinterval/120 + solar_sunrise(mo, da))/2;
522 >                        else if (fabs(solar_sunset(mo, da) - st) < timeinterval/120)
523 >                                st = (st - timeinterval/120 + solar_sunset(mo, da))/2;
524 >                }
525 >                altitude = salt(sda, st);
526 >                sun_in_sky = (altitude > -DegToRad(SUN_ANG_DEG/2.));
527 >                if (sun_hours_only && !sun_in_sky)
528 >                        continue;       /* skipping nighttime points */
529 >                azimuth = sazi(sda, st) + PI - DegToRad(rotation);
530  
531 <                mtx_offset = 3*nskypatch*nrows;
532 <                nrows += !avgSky | !nrows;
531 >                switch (epw->isWEA) {           /* translate units */
532 >                case WEAnot:
533 >                case WEAradnorm:
534 >                        if (!EPWisset(&erec,dirirrad) |
535 >                                        !EPWisset(&erec,horizdiffirrad)) {
536 >                                fprintf(stderr, "%s: missing required irradiances at line %d\n",
537 >                                                progname, epw->lino);
538 >                                exit(1);
539 >                        }
540 >                        dir = erec.dirirrad;
541 >                        dif = erec.horizdiffirrad;
542 >                        break;
543 >                case WEAradhoriz:
544 >                        dir = erec.globhorizirrad - erec.horizdiffirrad;
545 >                        dif = erec.horizdiffirrad;
546 >                        break;
547 >                case WEAphotnorm:
548 >                        dir = erec.dirillum;
549 >                        dif = erec.diffillum;
550 >                        break;
551 >                }
552 >                if (EPWisset(&erec,dptemp)) {   /* 3-hour dew point temp */
553 >                        if (dpthist[0] < -99)
554 >                                dpthist[0] = dpthist[1] = erec.dptemp;
555 >                        dew_point = (1./3.)*(dpthist[0] + dpthist[1] + erec.dptemp);
556 >                        dpthist[0] = dpthist[1]; dpthist[1] = erec.dptemp;
557 >                }
558 >                mtx_offset = 3*nskypatch*nstored;
559 >                nstored += !avgSky | !nstored;
560                                          /* make space for next row */
561 <                if (nrows > rows_alloc) {
562 <                        rows_alloc += (rows_alloc>>1) + nrows + 7;
563 <                        mtx_data = resize_dmatrix(mtx_data, rows_alloc, nskypatch);
561 >                if (nstored > tstorage) {
562 >                        tstorage += (tstorage>>1) + nstored + 7;
563 >                        mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch);
564                  }
565                  ntsteps++;              /* keep count of time steps */
566 <                if (dif <= 1e-4) {
567 <                        if (!avgSky | !nrows)
568 <                                memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
566 >
567 >                if (dir+dif <= 1e-4) {  /* effectively nighttime? */
568 >                        if (!avgSky | !mtx_offset)
569 >                                memset(mtx_data+mtx_offset, 0,
570 >                                                sizeof(float)*3*nskypatch);
571 >                                        /* output black sun? */
572 >                        if (sunsfp && sun_in_sky)
573 >                                OutputSun(solar_minute(julian_date,hr), 0,
574 >                                                        sunsfp, modsfp);
575                          continue;
576                  }
577 <                if (verbose && mo != last_monthly)
578 <                        fprintf(stderr, "%s: stepping through month %d...\n",
579 <                                                progname, last_monthly=mo);
580 <                                        /* compute solar position */
505 <                julian_date = jdate(mo, da);
506 <                sda = sdec(julian_date);
507 <                sta = stadj(julian_date);
508 <                altitude = salt(sda, hr+sta);
509 <                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
577 >                if (!sun_in_sky && dir > (input==1 ? 20. : 20.*WHTEFFICACY))
578 >                        fprintf(stderr,
579 >                                "%s: warning - unusually bright at %.1f on %d-%d\n",
580 >                                        progname, hr, mo, da);
581                                          /* convert measured values */
582 <                if (dir_is_horiz && altitude > 0.)
582 >                if (dir_is_horiz && altitude > FTINY)
583                          dir /= sin(altitude);
584                  if (input == 1) {
585                          dir_irrad = dir;
# Line 519 | Line 590 | main(int argc, char *argv[])
590                  }
591                                          /* compute sky patch values */
592                  ComputeSky(mtx_data+mtx_offset);
593 +                                        /* output sun if requested */
594 +                if (sunsfp && sun_in_sky)
595 +                        OutputSun(solar_minute(julian_date,hr), 1,
596 +                                                sunsfp, modsfp);
597 +
598 +                if (avgSky < 0)         /* no matrix? */
599 +                        continue;
600 +
601                  AddDirect(mtx_data+mtx_offset);
602                                          /* update cumulative sky? */
603                  for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
604                          mtx_data[i] += mtx_data[mtx_offset+i];
605 +                                        /* monthly reporting */
606 +                if (verbose && mo != last_monthly)
607 +                        fprintf(stderr, "%s: stepping through month %d...\n",
608 +                                                progname, last_monthly=mo);
609 +                                        /* note whether leap-day was given */
610          }
611 <                                        /* check for junk at end */
612 <        while ((i = fgetc(stdin)) != EOF)
613 <                if (!isspace(i)) {
614 <                        fprintf(stderr, "%s: warning - unexpected data past EOT: ",
615 <                                        progname);
532 <                        buf[0] = i; buf[1] = '\0';
533 <                        fgets(buf+1, sizeof(buf)-1, stdin);
534 <                        fputs(buf, stderr); fputc('\n', stderr);
535 <                        break;
536 <                }
611 >        if (j != EOF) {
612 >                fprintf(stderr, "%s: error on input\n", progname);
613 >                exit(1);
614 >        }
615 >        EPWclose(epw); epw = NULL;
616          if (!ntsteps) {
617                  fprintf(stderr, "%s: no valid time steps on input\n", progname);
618                  exit(1);
619          }
620 +        if (avgSky < 0)                 /* no matrix output? */
621 +                goto alldone;
622 +
623          dif = 1./(double)ntsteps;       /* average sky? */
624          for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
625                  mtx_data[i] *= dif;
# Line 549 | Line 631 | main(int argc, char *argv[])
631   #endif
632          if (verbose)
633                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
634 <                                progname, outfmt=='a' ? "" : "binary ", nrows);
634 >                                progname, outfmt=='a' ? "" : "binary ", nstored);
635          if (doheader) {
636                  newheader("RADIANCE", stdout);
637                  printargs(argc, argv, stdout);
638                  printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
639                                          -RadToDeg(s_longitude));
640                  printf("NROWS=%d\n", nskypatch);
641 <                printf("NCOLS=%d\n", nrows);
641 >                printf("NCOLS=%d\n", nstored);
642                  printf("NCOMP=3\n");
643 +                if ((outfmt == 'f') | (outfmt == 'd'))
644 +                        fputendian(stdout);
645                  fputformat((char *)getfmtname(outfmt), stdout);
646                  putchar('\n');
647          }
# Line 566 | Line 650 | main(int argc, char *argv[])
650                  mtx_offset = 3*i;
651                  switch (outfmt) {
652                  case 'a':
653 <                        for (j = 0; j < nrows; j++) {
653 >                        for (j = 0; j < nstored; j++) {
654                                  printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset],
655                                                  mtx_data[mtx_offset+1],
656                                                  mtx_data[mtx_offset+2]);
657                                  mtx_offset += 3*nskypatch;
658                          }
659 <                        if (nrows > 1)
659 >                        if (nstored > 1)
660                                  fputc('\n', stdout);
661                          break;
662                  case 'f':
663 <                        for (j = 0; j < nrows; j++) {
663 >                        for (j = 0; j < nstored; j++) {
664                                  putbinary(mtx_data+mtx_offset, sizeof(float), 3,
665                                                  stdout);
666                                  mtx_offset += 3*nskypatch;
667                          }
668                          break;
669                  case 'd':
670 <                        for (j = 0; j < nrows; j++) {
670 >                        for (j = 0; j < nstored; j++) {
671                                  double  ment[3];
672                                  ment[0] = mtx_data[mtx_offset];
673                                  ment[1] = mtx_data[mtx_offset+1];
# Line 596 | Line 680 | main(int argc, char *argv[])
680                  if (ferror(stdout))
681                          goto writerr;
682          }
683 <        if (fflush(stdout) == EOF)
683 > alldone:
684 >        if (fflush(NULL) == EOF)
685                  goto writerr;
686          if (verbose)
687                  fprintf(stderr, "%s: done.\n", progname);
688          exit(0);
689   userr:
690 <        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",
690 >        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",
691                          progname);
692          exit(1);
693   fmterr:
694 <        fprintf(stderr, "%s: input weather tape format error\n", progname);
694 >        fprintf(stderr, "%s: weather tape format error in header\n", progname);
695          exit(1);
696   writerr:
697          fprintf(stderr, "%s: write error on output\n", progname);
698          exit(1);
699   }
700  
701 +
702   /* Return maximum of two doubles */
703   double dmax( double a, double b )
704   { return (a > b) ? a : b; }
# Line 658 | Line 744 | ComputeSky(float *parr)
744                  /* Limit sky clearness */
745                  if (sky_clearness > 11.9)
746                          sky_clearness = 11.9;
747 +                else if (sky_clearness < 1.0)
748 +                        sky_clearness = 1.0;
749  
750                  /* Limit sky brightness */
751                  if (sky_brightness < 0.01)
752                          sky_brightness = 0.01;
753 +                else if (sky_brightness > 0.6)
754 +                        sky_brightness = 0.6;
755  
756                  /* Calculate illuminance */
757                  index = GetCategoryIndex();
# Line 678 | Line 768 | ComputeSky(float *parr)
768                  diff_illum = diff_irrad * WHTEFFICACY;
769                  dir_illum = dir_irrad * WHTEFFICACY;
770          }
681
682        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
683                memset(parr, 0, sizeof(float)*3*nskypatch);
684                return;
685        }
771          /* Compute ground radiance (include solar contribution if any) */
772          parr[0] = diff_illum;
773          if (altitude > 0)
# Line 690 | Line 775 | ComputeSky(float *parr)
775          parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
776          multcolor(parr, grefl);
777  
778 +        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
779 +                memset(parr+3, 0, sizeof(float)*3*(nskypatch-1));
780 +                return;
781 +        }
782          /* Calculate Perez sky model parameters */
783          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
784  
# Line 715 | Line 804 | ComputeSky(float *parr)
804          }
805   }
806  
807 +
808 + double
809 + solar_sunset(int month, int day)
810 + {
811 +        float W;
812 +        W = -1 * (tan(s_latitude) * tan(sdec(jdate(month, day))));
813 +        return(12 + (M_PI / 2 - atan2(W, sqrt(1 - W * W))) * 180 / (M_PI * 15));
814 + }
815 +
816 +
817 + double
818 + solar_sunrise(int month, int day)
819 + {
820 +        float W;
821 +        W = -1 * (tan(s_latitude) * tan(sdec(jdate(month, day))));
822 +        return(12 - (M_PI / 2 - atan2(W, sqrt(1 - W * W))) * 180 / (M_PI * 15));
823 + }
824 +
825 +
826   /* Add in solar direct to nearest sky patches (GW) */
827   void
828   AddDirect(float *parr)
# Line 767 | Line 875 | AddDirect(float *parr)
875          }
876   }
877  
878 + /* Output a sun to indicated file if appropriate for this time step */
879 + void
880 + OutputSun(int id, int goodsun, FILE *fp, FILE *mfp)
881 + {
882 +        double  srad;
883 +        FVECT   sv;
884 +
885 +        srad = DegToRad(SUN_ANG_DEG/2.);
886 +        srad = goodsun ? dir_illum/(WHTEFFICACY * PI*srad*srad) : 0;
887 +        vector(sv, altitude, azimuth);
888 +        fprintf(fp, "\nvoid light solar%d\n0\n0\n", id);
889 +        fprintf(fp, "3 %.3e %.3e %.3e\n", srad*suncolor[0],
890 +                        srad*suncolor[1], srad*suncolor[2]);
891 +        fprintf(fp, "\nsolar%d source sun%d\n0\n0\n", id, id);
892 +        fprintf(fp, "4 %.6f %.6f %.6f %.4f\n", sv[0], sv[1], sv[2], SUN_ANG_DEG);
893 +        
894 +        if (mfp != NULL)                /* saving modifier IDs? */
895 +                fprintf(mfp, "solar%d\n", id);
896 + }
897 +
898   /* Initialize Reinhart sky patch positions (GW) */
899   int
900   rh_init(void)
# Line 964 | Line 1092 | int CalcSkyParamFromIllum()
1092          if (sky_brightness < 0.01)
1093                          sky_brightness = 0.01;
1094  
1095 +        if (sky_clearness < 1.0000)
1096 +        {
1097 +                sky_clearness = 1.0000;
1098 +        }
1099 +
1100 +        if (sky_brightness > 0.6)
1101 +        {
1102 +                sky_brightness = 0.6;
1103 +        }
1104 +
1105          while (((fabs(diff_irrad - test1) > 10.0) ||
1106                          (fabs(dir_irrad - test2) > 10.0)) && !(counter == 5))
1107          {
# Line 989 | Line 1127 | int CalcSkyParamFromIllum()
1127                  /* Limit sky brightness */
1128                  if (sky_brightness < 0.01)
1129                          sky_brightness = 0.01;
1130 +
1131 +                if (sky_clearness < 1.0000)
1132 +                {
1133 +                        sky_clearness = 1.0000;
1134 +                }
1135 +
1136 +                if (sky_brightness > 0.6)
1137 +                {
1138 +                        sky_brightness = 0.6;
1139 +                }
1140          }
1141  
1142          return GetCategoryIndex();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines