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.1 by greg, Fri Jan 18 01:12:59 2013 UTC vs.
Revision 2.20 by greg, Sat Aug 1 23:27:04 2015 UTC

# Line 86 | Line 86 | static const char RCSid[] = "$Id$";
86   #include <string.h>
87   #include <ctype.h>
88   #include "rtmath.h"
89 + #include "resolu.h"
90 + #include "platform.h"
91   #include "color.h"
92 + #include "resolu.h"
93  
94   char *progname;                                                         /* Program name */
95   char errmsg[128];                                                       /* Error message buffer */
# Line 108 | Line 111 | double sky_clearness;                  /* Sky clearness */
111   double solar_rad;                       /* Solar radiance */
112   double sun_zenith;                      /* Sun zenith angle (radians) */
113   int     input = 0;                              /* Input type */
114 + int     output = 0;                             /* Output type */
115  
116   extern double dmax( double, double );
117   extern double CalcAirMass();
# Line 208 | Line 212 | static const CategoryBounds SkyClearCat[8] =
212          { 1.950, 2.800 },
213          { 2.800, 4.500 },
214          { 4.500, 6.200 },
215 <        { 6.200, 12.00 }        /* Clear */
215 >        { 6.200, 12.01 }        /* Clear */
216   };
217  
218   /* Luminous efficacy model coefficients */
# Line 246 | Line 250 | static const ModelCoeff DirectLumEff[8] =
250          { 101.18,  1.58, -1.10,  -8.29 }
251   };
252  
253 + #ifndef NSUNPATCH
254 + #define NSUNPATCH       4               /* max. # patches to spread sun into */
255 + #endif
256 +
257   extern int jdate(int month, int day);
258   extern double stadj(int  jd);
259   extern double sdec(int  jd);
# Line 256 | Line 264 | extern double  s_latitude;
264   extern double  s_longitude;
265   extern double  s_meridian;
266  
267 < double          grefl = 0.2;            /* diffuse ground reflectance */
267 > int             nsuns = NSUNPATCH;      /* number of sun patches to use */
268 > double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
269  
270   int             verbose = 0;            /* progress reports to stderr? */
271  
# Line 264 | Line 273 | int            outfmt = 'a';           /* output format */
273  
274   int             rhsubdiv = 1;           /* Reinhart sky subdivisions */
275  
276 < float           skycolor[3] = {.96, 1.004, 1.118};      /* sky coloration */
276 > COLOR           skycolor = {.96, 1.004, 1.118}; /* sky coloration */
277 > COLOR           suncolor = {1., 1., 1.};        /* sun color */
278 > COLOR           grefl = {.2, .2, .2};           /* ground reflectance */
279  
269 int             do_sun = 1;             /* output direct solar contribution? */
270
280   int             nskypatch;              /* number of Reinhart patches */
281   float           *rh_palt;               /* sky patch altitudes (radians) */
282   float           *rh_pazi;               /* sky patch azimuths (radians) */
# Line 286 | Line 295 | extern int     rh_init(void);
295   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
296   extern void     AddDirect(float *parr);
297  
298 +
299 + static const char *
300 + getfmtname(int fmt)
301 + {
302 +        switch (fmt) {
303 +        case 'a':
304 +                return("ascii");
305 +        case 'f':
306 +                return("float");
307 +        case 'd':
308 +                return("double");
309 +        }
310 +        return("unknown");
311 + }
312 +
313 +
314   int
315   main(int argc, char *argv[])
316   {
317          char    buf[256];
318 +        int     doheader = 1;           /* output header? */
319 +        double  rotation = 0;           /* site rotation (degrees) */
320          double  elevation;              /* site elevation (meters) */
321          int     dir_is_horiz;           /* direct is meas. on horizontal? */
322          float   *mtx_data = NULL;       /* our matrix data */
323          int     ntsteps = 0;            /* number of rows in matrix */
324 +        int     step_alloc = 0;
325          int     last_monthly = 0;       /* month of last report */
326          int     mo, da;                 /* month (1-12) and day (1-31) */
327          double  hr;                     /* hour (local standard time) */
# Line 305 | Line 333 | main(int argc, char *argv[])
333                                          /* get options */
334          for (i = 1; i < argc && argv[i][0] == '-'; i++)
335                  switch (argv[i][1]) {
336 <                case 'g':
337 <                        grefl = atof(argv[++i]);
336 >                case 'g':                       /* ground reflectance */
337 >                        grefl[0] = atof(argv[++i]);
338 >                        grefl[1] = atof(argv[++i]);
339 >                        grefl[2] = atof(argv[++i]);
340                          break;
341 <                case 'v':
341 >                case 'v':                       /* verbose progress reports */
342                          verbose++;
343                          break;
344 <                case 'o':
344 >                case 'h':                       /* turn off header */
345 >                        doheader = 0;
346 >                        break;
347 >                case 'o':                       /* output format */
348                          switch (argv[i][2]) {
349                          case 'f':
350                          case 'd':
# Line 322 | Line 355 | main(int argc, char *argv[])
355                                  goto userr;
356                          }
357                          break;
358 <                case 'm':
358 >                case 'O':                       /* output type */
359 >                        switch (argv[i][2]) {
360 >                        case '0':
361 >                                output = 0;
362 >                                break;
363 >                        case '1':
364 >                                output = 1;
365 >                                break;
366 >                        default:
367 >                                goto userr;
368 >                        }
369 >                        if (argv[i][3])
370 >                                goto userr;
371 >                        break;
372 >                case 'm':                       /* Reinhart subdivisions */
373                          rhsubdiv = atoi(argv[++i]);
374                          break;
375 <                case 'c':
375 >                case 'c':                       /* sky color */
376                          skycolor[0] = atof(argv[++i]);
377                          skycolor[1] = atof(argv[++i]);
378                          skycolor[2] = atof(argv[++i]);
379                          break;
380 <                case 'd':
334 <                        do_sun = 1;
380 >                case 'd':                       /* solar (direct) only */
381                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
382 +                        if (suncolor[1] <= 1e-4)
383 +                                suncolor[0] = suncolor[1] = suncolor[2] = 1;
384                          break;
385 <                case 's':
386 <                        do_sun = 0;
385 >                case 's':                       /* sky only (no direct) */
386 >                        suncolor[0] = suncolor[1] = suncolor[2] = 0;
387                          if (skycolor[1] <= 1e-4)
388                                  skycolor[0] = skycolor[1] = skycolor[2] = 1;
389                          break;
390 +                case 'r':                       /* rotate distribution */
391 +                        if (argv[i][2] && argv[i][2] != 'z')
392 +                                goto userr;
393 +                        rotation = atof(argv[++i]);
394 +                        break;
395 +                case '5':                       /* 5-phase calculation */
396 +                        nsuns = 1;
397 +                        fixed_sun_sa = PI/360.*atof(argv[++i]);
398 +                        fixed_sun_sa *= fixed_sun_sa*PI;
399 +                        break;
400                  default:
401                          goto userr;
402                  }
# Line 358 | Line 416 | main(int argc, char *argv[])
416                                          progname);
417          }
418                                          /* read weather tape header */
419 <        if (scanf("place %[^\n]\n", buf) != 1)
419 >        if (scanf("place %[^\r\n] ", buf) != 1)
420                  goto fmterr;
421          if (scanf("latitude %lf\n", &s_latitude) != 1)
422                  goto fmterr;
# Line 393 | Line 451 | main(int argc, char *argv[])
451                                  progname, s_latitude, s_longitude);
452                  fprintf(stderr, "%s: %d sky patches per time step\n",
453                                  progname, nskypatch);
454 +                if (rotation != 0)
455 +                        fprintf(stderr, "%s: rotating output %.0f degrees\n",
456 +                                        progname, rotation);
457          }
458 +                                        /* convert quantities to radians */
459 +        s_latitude = DegToRad(s_latitude);
460 +        s_longitude = DegToRad(s_longitude);
461 +        s_meridian = DegToRad(s_meridian);
462                                          /* process each time step in tape */
463          while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) {
464                  double          sda, sta;
465                                          /* make space for next time step */
466                  mtx_offset = 3*nskypatch*ntsteps++;
467 <                mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch);
467 >                if (ntsteps > step_alloc) {
468 >                        step_alloc += (step_alloc>>1) + ntsteps + 7;
469 >                        mtx_data = resize_dmatrix(mtx_data, step_alloc, nskypatch);
470 >                }
471                  if (dif <= 1e-4) {
472                          memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
473                          continue;
# Line 412 | Line 480 | main(int argc, char *argv[])
480                  sda = sdec(julian_date);
481                  sta = stadj(julian_date);
482                  altitude = salt(sda, hr+sta);
483 <                azimuth = sazi(sda, hr+sta);
483 >                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
484                                          /* convert measured values */
485                  if (dir_is_horiz && altitude > 0.)
486                          dir /= sin(altitude);
# Line 425 | Line 493 | main(int argc, char *argv[])
493                  }
494                                          /* compute sky patch values */
495                  ComputeSky(mtx_data+mtx_offset);
496 <                if (do_sun)
429 <                        AddDirect(mtx_data+mtx_offset);
496 >                AddDirect(mtx_data+mtx_offset);
497          }
498                                          /* check for junk at end */
499          while ((i = fgetc(stdin)) != EOF)
# Line 439 | Line 506 | main(int argc, char *argv[])
506                          break;
507                  }
508                                          /* write out matrix */
509 +        if (outfmt != 'a')
510 +                SET_FILE_BINARY(stdout);
511   #ifdef getc_unlocked
512          flockfile(stdout);
513   #endif
514          if (verbose)
515                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
516                                  progname, outfmt=='a' ? "" : "binary ", ntsteps);
517 +        if (doheader) {
518 +                newheader("RADIANCE", stdout);
519 +                printargs(argc, argv, stdout);
520 +                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
521 +                                        -RadToDeg(s_longitude));
522 +                printf("NROWS=%d\n", nskypatch);
523 +                printf("NCOLS=%d\n", ntsteps);
524 +                printf("NCOMP=3\n");
525 +                fputformat((char *)getfmtname(outfmt), stdout);
526 +                putchar('\n');
527 +        }
528                                          /* patches are rows (outer sort) */
529          for (i = 0; i < nskypatch; i++) {
530                  mtx_offset = 3*i;
531                  switch (outfmt) {
532                  case 'a':
533                          for (j = 0; j < ntsteps; j++) {
534 <                                printf("%.3e %.3e %.3e\n", mtx_data[mtx_offset],
534 >                                printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset],
535                                                  mtx_data[mtx_offset+1],
536                                                  mtx_data[mtx_offset+2]);
537                                  mtx_offset += 3*nskypatch;
538                          }
539 <                        fputc('\n', stdout);
539 >                        if (ntsteps > 1)
540 >                                fputc('\n', stdout);
541                          break;
542                  case 'f':
543                          for (j = 0; j < ntsteps; j++) {
# Line 485 | Line 566 | main(int argc, char *argv[])
566                  fprintf(stderr, "%s: done.\n", progname);
567          exit(0);
568   userr:
569 <        fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g refl][-c r g b][-o{f|d}] [tape.wea]\n",
569 >        fprintf(stderr, "Usage: %s [-v][-h][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n",
570                          progname);
571          exit(1);
572   fmterr:
# Line 506 | Line 587 | ComputeSky(float *parr)
587   {
588          int index;                      /* Category index */
589          double norm_diff_illum;         /* Normalized diffuse illuimnance */
509        double zlumin;                  /* Zenith luminance */
590          int i;
511
512        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
513                memset(parr, 0, sizeof(float)*3*nskypatch);
514                return;
515        }
591          
592          /* Calculate atmospheric precipitable water content */
593          apwc = CalcPrecipWater(dew_point);
594  
595 <        /* Limit solar altitude to keep circumsolar off zenith */
596 <        if (altitude > DegToRad(87.0))
597 <                altitude = DegToRad(87.0);
595 >        /* Calculate sun zenith angle (don't let it dip below horizon) */
596 >        /* Also limit minimum angle to keep circumsolar off zenith */
597 >        if (altitude <= 0.0)
598 >                sun_zenith = DegToRad(90.0);
599 >        else if (altitude >= DegToRad(87.0))
600 >                sun_zenith = DegToRad(3.0);
601 >        else
602 >                sun_zenith = DegToRad(90.0) - altitude;
603  
524        /* Calculate sun zenith angle */
525        sun_zenith = DegToRad(90.0) - altitude;
526
604          /* Compute the inputs for the calculation of the sky distribution */
605          
606          if (input == 0)                                 /* XXX never used */
# Line 542 | Line 619 | ComputeSky(float *parr)
619                  sky_brightness = CalcSkyBrightness();
620                  sky_clearness =  CalcSkyClearness();
621  
622 +                /* Limit sky clearness */
623 +                if (sky_clearness > 11.9)
624 +                        sky_clearness = 11.9;
625 +
626 +                /* Limit sky brightness */
627 +                if (sky_brightness < 0.01)
628 +                        sky_brightness = 0.01;
629 +
630                  /* Calculate illuminance */
631                  index = GetCategoryIndex();
632                  diff_illum = diff_irrad * CalcDiffuseIllumRatio(index);
# Line 553 | Line 638 | ComputeSky(float *parr)
638                  index = CalcSkyParamFromIllum();
639          }
640  
641 +        if (output == 1) {                      /* hack for solar radiance */
642 +                diff_illum = diff_irrad * WHTEFFICACY;
643 +                dir_illum = dir_irrad * WHTEFFICACY;
644 +        }
645 +
646 +        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
647 +                memset(parr, 0, sizeof(float)*3*nskypatch);
648 +                return;
649 +        }
650          /* Compute ground radiance (include solar contribution if any) */
651 <        parr[0] = diff_illum * (1./PI/WHTEFFICACY);
651 >        parr[0] = diff_illum;
652          if (altitude > 0)
653 <                parr[0] += dir_illum * sin(altitude) * (1./PI/WHTEFFICACY);
654 <        parr[2] = parr[1] = parr[0];
653 >                parr[0] += dir_illum * sin(altitude);
654 >        parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
655 >        multcolor(parr, grefl);
656  
657          /* Calculate Perez sky model parameters */
658          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
# Line 568 | Line 663 | ComputeSky(float *parr)
663          /* Calculate relative horizontal illuminance */
664          norm_diff_illum = CalcRelHorzIllum(parr);
665  
666 +        /* Check for zero sky -- make uniform in that case */
667 +        if (norm_diff_illum <= FTINY) {
668 +                for (i = 1; i < nskypatch; i++)
669 +                        setcolor(parr+3*i, 1., 1., 1.);
670 +                norm_diff_illum = PI;
671 +        }
672          /* Normalization coefficient */
673          norm_diff_illum = diff_illum / norm_diff_illum;
674  
574        /* Calculate relative zenith luminance */
575        zlumin = CalcRelLuminance(sun_zenith, 0.0);
576
577        /* Calculate absolute zenith illuminance */
578        zlumin *= norm_diff_illum;
579
675          /* Apply to sky patches to get absolute radiance values */
676          for (i = 1; i < nskypatch; i++) {
677 <                scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY));
677 >                scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY));
678                  multcolor(parr+3*i, skycolor);
679          }
680   }
# Line 589 | Line 684 | void
684   AddDirect(float *parr)
685   {
686          FVECT   svec;
687 <        double  near_dprod[4];
688 <        int     near_patch[4];
689 <        double  wta[4], wtot;
687 >        double  near_dprod[NSUNPATCH];
688 >        int     near_patch[NSUNPATCH];
689 >        double  wta[NSUNPATCH], wtot;
690          int     i, j, p;
691  
692 <        if (!do_sun || dir_illum < 1e-4)
692 >        if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4)
693                  return;
694 <                                        /* identify 4 closest patches */
695 <        for (i = 4; i--; )
694 >                                        /* identify nsuns closest patches */
695 >        if (nsuns > NSUNPATCH)
696 >                nsuns = NSUNPATCH;
697 >        else if (nsuns <= 0)
698 >                nsuns = 1;
699 >        for (i = nsuns; i--; )
700                  near_dprod[i] = -1.;
701          vector(svec, altitude, azimuth);
702          for (p = 1; p < nskypatch; p++) {
# Line 605 | Line 704 | AddDirect(float *parr)
704                  double  dprod;
705                  rh_vector(pvec, p);
706                  dprod = DOT(pvec, svec);
707 <                for (i = 0; i < 4; i++)
707 >                for (i = 0; i < nsuns; i++)
708                          if (dprod > near_dprod[i]) {
709 <                                for (j = 4; --j > i; ) {
709 >                                for (j = nsuns; --j > i; ) {
710                                          near_dprod[j] = near_dprod[j-1];
711                                          near_patch[j] = near_patch[j-1];
712                                  }
# Line 617 | Line 716 | AddDirect(float *parr)
716                          }
717          }
718          wtot = 0;                       /* weight by proximity */
719 <        for (i = 4; i--; )
719 >        for (i = nsuns; i--; )
720                  wtot += wta[i] = 1./(1.002 - near_dprod[i]);
721                                          /* add to nearest patch radiances */
722 <        for (i = 4; i--; )
723 <                parr[near_patch[i]] += wta[i] * dir_illum /
724 <                                        (wtot * rh_dom[near_patch[i]]);
722 >        for (i = nsuns; i--; ) {
723 >                float   *pdest = parr + 3*near_patch[i];
724 >                float   val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot);
725 >
726 >                val_add /= (fixed_sun_sa > 0)   ? fixed_sun_sa
727 >                                                : rh_dom[near_patch[i]] ;
728 >                *pdest++ += val_add*suncolor[0];
729 >                *pdest++ += val_add*suncolor[1];
730 >                *pdest++ += val_add*suncolor[2];
731 >        }
732   }
733  
734   /* Initialize Reinhart sky patch positions (GW) */
# Line 656 | Line 762 | rh_init(void)
762          for (i = 0; i < NROW*rhsubdiv; i++) {
763                  const float     ralt = alpha*(i + .5);
764                  const int       ninrow = tnaz[i/rhsubdiv]*rhsubdiv;
765 <                const float     dom = (sin(alpha*(i+1)) - sin(alpha*i))/ninrow;
765 >                const float     dom = 2.*PI*(sin(alpha*(i+1)) - sin(alpha*i)) /
766 >                                                (double)ninrow;
767                  for (j = 0; j < ninrow; j++) {
768                          rh_palt[p] = ralt;
769                          rh_pazi[p] = 2.*PI * j / (double)ninrow;
# Line 761 | Line 868 | double CalcSkyClearness()
868          double sz_cubed;        /* Sun zenith angle cubed */
869  
870          /* Calculate sun zenith angle cubed */
871 <        sz_cubed = pow(sun_zenith, 3.0);
871 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
872  
873          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
874                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 792 | Line 899 | double CalcDiffuseIrradiance()
899   double CalcDirectIrradiance()
900   {
901          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
902 <                        * pow(sun_zenith, 3.0)));
902 >                        * sun_zenith*sun_zenith*sun_zenith));
903   }
904  
905   /* Calculate sky brightness and clearness from illuminance values */
# Line 818 | Line 925 | int CalcSkyParamFromIllum()
925                  sky_clearness = 12.0;
926  
927          /* Limit sky brightness */
928 <        if (sky_brightness < 0.05)
928 >        if (sky_brightness < 0.01)
929                          sky_brightness = 0.01;
930  
931          while (((fabs(diff_irrad - test1) > 10.0) ||
# Line 842 | Line 949 | int CalcSkyParamFromIllum()
949                          sky_clearness = 12.0;
950          
951                  /* Limit sky brightness */
952 <                if (sky_brightness < 0.05)
952 >                if (sky_brightness < 0.01)
953                          sky_brightness = 0.01;
954          }
955  
# Line 928 | Line 1035 | double CalcRelHorzIllum( float *parr )
1035          double rh_illum = 0.0;  /* Relative horizontal illuminance */
1036  
1037          for (i = 1; i < nskypatch; i++)
1038 <                rh_illum += parr[3*i+1] * rh_cos(i);
1038 >                rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i];
1039  
1040 <        return rh_illum * (2.0 * PI / (nskypatch-1));
1040 >        return rh_illum;
1041   }
1042  
1043   /* Calculate earth orbit eccentricity correction factor */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines