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.34 by greg, Tue Jan 7 01:42:30 2020 UTC vs.
Revision 2.44 by greg, Sat Jun 7 05:09:45 2025 UTC

# Line 88 | Line 88 | static const char RCSid[] = "$Id$";
88   #include "rtio.h"
89   #include "color.h"
90   #include "sun.h"
91 + #include "loadEPW.h"
92  
92 char *progname;                                                         /* Program name */
93 char errmsg[128];                                                       /* Error message buffer */
93   const double DC_SolarConstantE = 1367.0;        /* Solar constant W/m^2 */
94   const double DC_SolarConstantL = 127.5;         /* Solar constant klux */
95  
# Line 129 | Line 128 | extern void CalcPerezParam( double, double, double, in
128   extern void CalcSkyPatchLumin( float *parr );
129   extern void ComputeSky( float *parr );
130  
131 +
132 + extern double  solar_sunset(int month, int day);
133 + extern double  solar_sunrise(int month, int day);
134 +
135   /* Degrees into radians */
136   #define DegToRad(deg)   ((deg)*(PI/180.))
137  
138   /* Radiuans into degrees */
139   #define RadToDeg(rad)   ((rad)*(180./PI))
140  
138
141   /* Perez sky model coefficients */
142  
143   /* Reference:   Perez, R., R. Seals, and J. Michalsky, 1993. "All- */
# Line 252 | Line 254 | static const ModelCoeff DirectLumEff[8] =
254   #define NSUNPATCH       4               /* max. # patches to spread sun into */
255   #endif
256  
257 + #define SUN_ANG_DEG     0.533           /* sun full-angle in degrees */
258 +
259   int             nsuns = NSUNPATCH;      /* number of sun patches to use */
260   double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
261  
# Line 270 | Line 274 | float          *rh_palt;               /* sky patch altitudes (radians) */
274   float           *rh_pazi;               /* sky patch azimuths (radians) */
275   float           *rh_dom;                /* sky patch solid angle (sr) */
276  
277 < #define         vector(v,alt,azi)       (       (v)[1] = tcos(alt), \
278 <                                                (v)[0] = (v)[1]*tsin(azi), \
279 <                                                (v)[1] *= tcos(azi), \
280 <                                                (v)[2] = tsin(alt) )
277 > #define         vector(v,alt,azi)       (       (v)[1] = cos(alt), \
278 >                                                (v)[0] = (v)[1]*sin(azi), \
279 >                                                (v)[1] *= cos(azi), \
280 >                                                (v)[2] = sin(alt) )
281  
282   #define         rh_vector(v,i)          vector(v,rh_palt[i],rh_pazi[i])
283  
284   #define         rh_cos(i)               tsin(rh_palt[i])
285  
286 + #define         solar_minute(jd,hr)     ((24*60)*((jd)-1)+(int)((hr)*60.+.5))
287 +
288   extern int      rh_init(void);
289   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
290 + extern void     OutputSun(int id, int goodsun, FILE *fp, FILE *mfp);
291   extern void     AddDirect(float *parr);
292  
293  
# Line 302 | Line 309 | getfmtname(int fmt)
309   int
310   main(int argc, char *argv[])
311   {
312 <        char    buf[256];
312 >        EPWheader       *epw = NULL;    /* EPW/WEA input file */
313 >        EPWrecord       erec;           /* current EPW/WEA input record */
314 >        float   dpthist[2];             /* previous dew point temps */
315 >        double  dir, dif;
316          int     doheader = 1;           /* output header? */
317          double  rotation = 0;           /* site rotation (degrees) */
318          double  elevation;              /* site elevation (meters) */
319 +        int     leap_day = 0;           /* add leap day? */
320 +        int     sun_hours_only = 0;     /* only output sun hours? */
321          int     dir_is_horiz;           /* direct is meas. on horizontal? */
322          FILE    *sunsfp = NULL;         /* output file for individual suns */
323 +        FILE    *modsfp = NULL;         /* modifier output file */
324          float   *mtx_data = NULL;       /* our matrix data */
325          int     avgSky = 0;             /* compute average sky r.t. matrix? */
326          int     ntsteps = 0;            /* number of time steps */
327          int     tstorage = 0;           /* number of allocated time steps */
328          int     nstored = 0;            /* number of time steps in matrix */
329          int     last_monthly = 0;       /* month of last report */
317        int     mo, da;                 /* month (1-12) and day (1-31) */
318        double  hr;                     /* hour (local standard time) */
319        double  dir, dif;               /* direct and diffuse values */
330          int     mtx_offset;
331          int     i, j;
332 +        double  timeinterval = 0;
333  
334 <        progname = argv[0];
334 >        fixargv0(argv[0]);
335                                          /* get options */
336          for (i = 1; i < argc && argv[i][0] == '-'; i++)
337                  switch (argv[i][1]) {
# Line 368 | Line 379 | main(int argc, char *argv[])
379                          skycolor[1] = atof(argv[++i]);
380                          skycolor[2] = atof(argv[++i]);
381                          break;
382 +                case 'D':                       /* output suns to file */
383 +                        if (strcmp(argv[++i], "-")) {
384 +                                sunsfp = fopen(argv[i], "w");
385 +                                if (sunsfp == NULL) {
386 +                                        fprintf(stderr,
387 +                                        "%s: cannot open '%s' for output\n",
388 +                                                        progname, argv[i]);
389 +                                        exit(1);
390 +                                }
391 +                                break;          /* still may output matrix */
392 +                        }
393 +                        sunsfp = stdout;        /* sending to stdout, so... */
394 +                        /* fall through */
395                  case 'n':                       /* no matrix output */
396                          avgSky = -1;
397                          rhsubdiv = 1;
# Line 376 | Line 400 | main(int argc, char *argv[])
400                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
401                          grefl[0] = grefl[1] = grefl[2] = 0;
402                          break;
403 <                case 'D':                       /* output suns to file */
404 <                        sunsfp = fopen(argv[++i], "w");
381 <                        if (!sunsfp) {
403 >                case 'M':                       /* send sun modifiers to file */
404 >                        if ((modsfp = fopen(argv[++i], "w")) == NULL) {
405                                  fprintf(stderr, "%s: cannot open '%s' for output\n",
406 <                                                argv[0], argv[i]);
406 >                                                progname, argv[i]);
407                                  exit(1);
408                          }
386                        fixed_sun_sa = PI/360.*0.533;
387                        fixed_sun_sa *= PI*fixed_sun_sa;
409                          break;
410                  case 's':                       /* sky only (no direct) */
411                          suncolor[0] = suncolor[1] = suncolor[2] = 0;
412                          break;
413 +                case 'u':                       /* solar hours only */
414 +                        sun_hours_only = 1;
415 +                        break;
416                  case 'r':                       /* rotate distribution */
417                          if (argv[i][2] && argv[i][2] != 'z')
418                                  goto userr;
# Line 399 | Line 423 | main(int argc, char *argv[])
423                          fixed_sun_sa = PI/360.*atof(argv[++i]);
424                          if (fixed_sun_sa <= 0) {
425                                  fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
426 <                                                argv[0]);
426 >                                                progname);
427                                  exit(1);
428                          }
429                          fixed_sun_sa *= fixed_sun_sa*PI;
# Line 407 | Line 431 | main(int argc, char *argv[])
431                  case 'A':                       /* compute average sky */
432                          avgSky = 1;
433                          break;
434 +                case 'i':
435 +                        timeinterval = atof(argv[++i]);
436 +                        break;
437                  default:
438                          goto userr;
439                  }
440 <        if (i < argc-1)
440 >        if ((i < argc-1) | (i > argc))
441                  goto userr;
442 <        if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
443 <                fprintf(stderr, "%s: cannot open '%s' for input\n",
417 <                                progname, argv[i]);
442 >        epw = EPWopen(argv[i]);
443 >        if (epw == NULL)
444                  exit(1);
445 <        }
445 >        if ((modsfp != NULL) & (sunsfp == NULL))
446 >                fprintf(stderr, "%s: warning -M output will be empty without -D\n",
447 >                                progname);
448          if (verbose) {
449                  if (i == argc-1)
450                          fprintf(stderr, "%s: reading weather tape '%s'\n",
# Line 426 | Line 454 | main(int argc, char *argv[])
454                                          progname);
455          }
456                                          /* read weather tape header */
457 <        if (scanf("place %[^\r\n] ", buf) != 1)
458 <                goto fmterr;
459 <        if (scanf("latitude %lf\n", &s_latitude) != 1)
460 <                goto fmterr;
461 <        if (scanf("longitude %lf\n", &s_longitude) != 1)
462 <                goto fmterr;
463 <        if (scanf("time_zone %lf\n", &s_meridian) != 1)
436 <                goto fmterr;
437 <        if (scanf("site_elevation %lf\n", &elevation) != 1)
438 <                goto fmterr;
439 <        if (scanf("weather_data_file_units %d\n", &input) != 1)
440 <                goto fmterr;
441 <        switch (input) {                /* translate units */
442 <        case 1:
457 >        s_latitude = epw->loc.latitude;
458 >        s_longitude = -epw->loc.longitude;
459 >        s_meridian = -15.*epw->loc.timezone;
460 >        elevation = epw->loc.elevation;
461 >        switch (epw->isWEA) {           /* translate units */
462 >        case WEAnot:
463 >        case WEAradnorm:
464                  input = 1;              /* radiometric quantities */
465                  dir_is_horiz = 0;       /* direct is perpendicular meas. */
466                  break;
467 <        case 2:
467 >        case WEAradhoriz:
468                  input = 1;              /* radiometric quantities */
469                  dir_is_horiz = 1;       /* solar measured horizontally */
470                  break;
471 <        case 3:
471 >        case WEAphotnorm:
472                  input = 2;              /* photometric quantities */
473                  dir_is_horiz = 0;       /* direct is perpendicular meas. */
474                  break;
# Line 456 | Line 477 | main(int argc, char *argv[])
477          }
478          rh_init();                      /* initialize sky patches */
479          if (verbose) {
480 <                fprintf(stderr, "%s: location '%s'\n", progname, buf);
480 >                fprintf(stderr, "%s: location '%s, %s'\n", progname,
481 >                                epw->loc.city, epw->loc.country);
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);
# Line 471 | Line 497 | main(int argc, char *argv[])
497          s_meridian = DegToRad(s_meridian);
498                                          /* initial allocation */
499          mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch);
500 +        dpthist[0] = -100;
501                                          /* process each time step in tape */
502 <        while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) {
503 <                double          sda, sta;
502 >        while ((j = EPWread(epw, &erec)) > 0) {
503 >                const int       mo = erec.date.month+1;
504 >                const int       da = erec.date.day;
505 >                const double    hr = erec.date.hour;
506 >                double          sda, sta, st;
507 >                int             sun_in_sky;
508 >                                        /* 3-hour dew point temp */
509 >                if (EPWisset(&erec,dptemp)) {
510 >                        if (dpthist[0] < -99)
511 >                                dpthist[0] = dpthist[1] = erec.dptemp;
512 >                        dew_point = (1./3.)*(dpthist[0] + dpthist[1] + erec.dptemp);
513 >                        dpthist[0] = dpthist[1]; dpthist[1] = erec.dptemp;
514 >                } else
515 >                        dpthist[0] = -100;
516 >                                        /* compute solar position */
517 >                if ((mo == 2) & (da == 29)) {
518 >                        julian_date = 60;
519 >                        leap_day = 1;
520 >                } else
521 >                        julian_date = jdate(mo, da) + leap_day;
522 >                sda = sdec(julian_date);
523 >                sta = stadj(julian_date);
524 >                st = hr + sta;
525 >                
526 >                if (timeinterval > 0) {
527 >                        if (fabs(solar_sunrise(mo, da) - st) <= timeinterval/120)
528 >                                st = (st + timeinterval/120 + solar_sunrise(mo, da))/2;
529 >                        else if (fabs(solar_sunset(mo, da) - st) < timeinterval/120)
530 >                                st = (st - timeinterval/120 + solar_sunset(mo, da))/2;
531 >                }
532 >                altitude = salt(sda, st);
533 >                sun_in_sky = (altitude > -DegToRad(SUN_ANG_DEG/2.));
534 >                if (sun_hours_only & !sun_in_sky)
535 >                        continue;       /* skipping nighttime points */
536 >                azimuth = sazi(sda, st) + PI - DegToRad(rotation);
537  
538 +                switch (epw->isWEA) {           /* translate units */
539 +                case WEAnot:
540 +                case WEAradnorm:
541 +                        if (!EPWisset(&erec,dirirrad) |
542 +                                        !EPWisset(&erec,horizdiffirrad)) {
543 +                                fprintf(stderr, "%s: missing required irradiances at line %d\n",
544 +                                                progname, epw->lino);
545 +                                exit(1);
546 +                        }
547 +                        dir = erec.dirirrad;
548 +                        dif = erec.horizdiffirrad;
549 +                        break;
550 +                case WEAradhoriz:
551 +                        dir = erec.globhorizirrad - erec.horizdiffirrad;
552 +                        dif = erec.horizdiffirrad;
553 +                        break;
554 +                case WEAphotnorm:
555 +                        dir = erec.dirillum;
556 +                        dif = erec.diffillum;
557 +                        break;
558 +                }
559                  mtx_offset = 3*nskypatch*nstored;
560                  nstored += !avgSky | !nstored;
561                                          /* make space for next row */
# Line 483 | Line 564 | main(int argc, char *argv[])
564                          mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch);
565                  }
566                  ntsteps++;              /* keep count of time steps */
567 <                if (dif <= 1e-4) {
567 >
568 >                if (dir+dif <= 1e-4) {  /* effectively nighttime? */
569                          if (!avgSky | !mtx_offset)
570 <                                memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
570 >                                memset(mtx_data+mtx_offset, 0,
571 >                                                sizeof(float)*3*nskypatch);
572 >                                        /* output black sun? */
573 >                        if (sunsfp && sun_in_sky)
574 >                                OutputSun(solar_minute(julian_date,hr), 0,
575 >                                                        sunsfp, modsfp);
576                          continue;
577                  }
578 <                                        /* compute solar position */
579 <                julian_date = jdate(mo, da);
580 <                sda = sdec(julian_date);
581 <                sta = stadj(julian_date);
495 <                altitude = salt(sda, hr+sta);
496 <                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
578 >                if (!sun_in_sky && dir > (input==1 ? 20. : 20.*WHTEFFICACY))
579 >                        fprintf(stderr,
580 >                                "%s: warning - unusually bright at %.1f on %d-%d\n",
581 >                                        progname, hr, mo, da);
582                                          /* convert measured values */
583 <                if (dir_is_horiz && altitude > 0.)
583 >                if (dir_is_horiz && altitude > FTINY)
584                          dir /= sin(altitude);
585                  if (input == 1) {
586                          dir_irrad = dir;
# Line 506 | Line 591 | main(int argc, char *argv[])
591                  }
592                                          /* compute sky patch values */
593                  ComputeSky(mtx_data+mtx_offset);
594 <                                        /* output sun if indicated */
595 <                if (sunsfp && (altitude > 0) & (dir_illum > 1e-4)) {
596 <                        double  srad = dir_illum/(WHTEFFICACY * fixed_sun_sa);
597 <                        FVECT   sv;
598 <                        vector(sv, altitude, azimuth);
514 <                        fprintf(sunsfp, "\nvoid light solar%d\n0\n0\n", ntsteps);
515 <                        fprintf(sunsfp, "3 %.3e %.3e %.3e\n", srad*suncolor[0],
516 <                                        srad*suncolor[1], srad*suncolor[2]);
517 <                        fprintf(sunsfp, "\nsolar%d source sun%d\n0\n0\n", ntsteps, ntsteps);
518 <                        fprintf(sunsfp, "4 %.6f %.6f %.6f 0.533\n", sv[0], sv[1], sv[2]);
519 <                }
594 >                                        /* output sun if requested */
595 >                if (sunsfp && sun_in_sky)
596 >                        OutputSun(solar_minute(julian_date,hr), 1,
597 >                                                sunsfp, modsfp);
598 >
599                  if (avgSky < 0)         /* no matrix? */
600                          continue;
601  
# Line 528 | Line 607 | main(int argc, char *argv[])
607                  if (verbose && mo != last_monthly)
608                          fprintf(stderr, "%s: stepping through month %d...\n",
609                                                  progname, last_monthly=mo);
610 +                                        /* note whether leap-day was given */
611          }
612 +        if (j != EOF) {
613 +                fprintf(stderr, "%s: error on input\n", progname);
614 +                exit(1);
615 +        }
616 +        EPWclose(epw); epw = NULL;
617          if (!ntsteps) {
618                  fprintf(stderr, "%s: no valid time steps on input\n", progname);
619                  exit(1);
620          }
536                                        /* check for junk at end */
537        while ((i = fgetc(stdin)) != EOF)
538                if (!isspace(i)) {
539                        fprintf(stderr, "%s: warning - unexpected data past EOT: ",
540                                        progname);
541                        buf[0] = i; buf[1] = '\0';
542                        fgets(buf+1, sizeof(buf)-1, stdin);
543                        fputs(buf, stderr); fputc('\n', stderr);
544                        break;
545                }
546
621          if (avgSky < 0)                 /* no matrix output? */
622                  goto alldone;
623  
# Line 614 | Line 688 | alldone:
688                  fprintf(stderr, "%s: done.\n", progname);
689          exit(0);
690   userr:
691 <        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",
691 >        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",
692                          progname);
693          exit(1);
694   fmterr:
# Line 625 | Line 699 | writerr:
699          exit(1);
700   }
701  
702 +
703   /* Return maximum of two doubles */
704   double dmax( double a, double b )
705   { return (a > b) ? a : b; }
# Line 636 | Line 711 | ComputeSky(float *parr)
711          int index;                      /* Category index */
712          double norm_diff_illum;         /* Normalized diffuse illuimnance */
713          int i;
714 <        
714 >
715          /* Calculate atmospheric precipitable water content */
716          apwc = CalcPrecipWater(dew_point);
717  
# Line 670 | Line 745 | ComputeSky(float *parr)
745                  /* Limit sky clearness */
746                  if (sky_clearness > 11.9)
747                          sky_clearness = 11.9;
748 +                else if (sky_clearness < 1.0)
749 +                        sky_clearness = 1.0;
750  
751                  /* Limit sky brightness */
752                  if (sky_brightness < 0.01)
753                          sky_brightness = 0.01;
754 +                else if (sky_brightness > 0.6)
755 +                        sky_brightness = 0.6;
756  
757                  /* Calculate illuminance */
758                  index = GetCategoryIndex();
# Line 726 | Line 805 | ComputeSky(float *parr)
805          }
806   }
807  
808 +
809 + double
810 + solar_sunset(int month, int day)
811 + {
812 +        float W;
813 +        W = -1 * (tan(s_latitude) * tan(sdec(jdate(month, day))));
814 +        return(12 + (M_PI / 2 - atan2(W, sqrt(1 - W * W))) * 180 / (M_PI * 15));
815 + }
816 +
817 +
818 + double
819 + solar_sunrise(int month, int day)
820 + {
821 +        float W;
822 +        W = -1 * (tan(s_latitude) * tan(sdec(jdate(month, day))));
823 +        return(12 - (M_PI / 2 - atan2(W, sqrt(1 - W * W))) * 180 / (M_PI * 15));
824 + }
825 +
826 +
827   /* Add in solar direct to nearest sky patches (GW) */
828   void
829   AddDirect(float *parr)
# Line 778 | Line 876 | AddDirect(float *parr)
876          }
877   }
878  
879 + /* Output a sun to indicated file if appropriate for this time step */
880 + void
881 + OutputSun(int id, int goodsun, FILE *fp, FILE *mfp)
882 + {
883 +        double  srad;
884 +        FVECT   sv;
885 +
886 +        srad = DegToRad(SUN_ANG_DEG/2.);
887 +        srad = goodsun ? dir_illum/(WHTEFFICACY * PI*srad*srad) : 0;
888 +        vector(sv, altitude, azimuth);
889 +        fprintf(fp, "\nvoid light solar%d\n0\n0\n", id);
890 +        fprintf(fp, "3 %.3e %.3e %.3e\n", srad*suncolor[0],
891 +                        srad*suncolor[1], srad*suncolor[2]);
892 +        fprintf(fp, "\nsolar%d source sun%d\n0\n0\n", id, id);
893 +        fprintf(fp, "4 %.6f %.6f %.6f %.4f\n", sv[0], sv[1], sv[2], SUN_ANG_DEG);
894 +        
895 +        if (mfp != NULL)                /* saving modifier IDs? */
896 +                fprintf(mfp, "solar%d\n", id);
897 + }
898 +
899   /* Initialize Reinhart sky patch positions (GW) */
900   int
901   rh_init(void)
# Line 975 | Line 1093 | int CalcSkyParamFromIllum()
1093          if (sky_brightness < 0.01)
1094                          sky_brightness = 0.01;
1095  
1096 +        if (sky_clearness < 1.0000)
1097 +        {
1098 +                sky_clearness = 1.0000;
1099 +        }
1100 +
1101 +        if (sky_brightness > 0.6)
1102 +        {
1103 +                sky_brightness = 0.6;
1104 +        }
1105 +
1106          while (((fabs(diff_irrad - test1) > 10.0) ||
1107                          (fabs(dir_irrad - test2) > 10.0)) && !(counter == 5))
1108          {
# Line 1000 | Line 1128 | int CalcSkyParamFromIllum()
1128                  /* Limit sky brightness */
1129                  if (sky_brightness < 0.01)
1130                          sky_brightness = 0.01;
1131 +
1132 +                if (sky_clearness < 1.0000)
1133 +                {
1134 +                        sky_clearness = 1.0000;
1135 +                }
1136 +
1137 +                if (sky_brightness > 0.6)
1138 +                {
1139 +                        sky_brightness = 0.6;
1140 +                }
1141          }
1142  
1143          return GetCategoryIndex();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines