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.3 by greg, Sat Jan 19 20:38:36 2013 UTC vs.
Revision 2.11 by greg, Tue Apr 30 17:05:27 2013 UTC

# Line 108 | Line 108 | double sky_clearness;                  /* Sky clearness */
108   double solar_rad;                       /* Solar radiance */
109   double sun_zenith;                      /* Sun zenith angle (radians) */
110   int     input = 0;                              /* Input type */
111 + int     output = 0;                             /* Output type */
112  
113   extern double dmax( double, double );
114   extern double CalcAirMass();
# Line 247 | Line 248 | static const ModelCoeff DirectLumEff[8] =
248   };
249  
250   #ifndef NSUNPATCH
251 < #define NSUNPATCH       4               /* # patches to spread sun into */
251 > #define NSUNPATCH       4               /* max. # patches to spread sun into */
252   #endif
253  
254   extern int jdate(int month, int day);
# Line 260 | Line 261 | extern double  s_latitude;
261   extern double  s_longitude;
262   extern double  s_meridian;
263  
264 < double          grefl = 0.2;            /* diffuse ground reflectance */
264 > int             nsuns = NSUNPATCH;      /* number of sun patches to use */
265 > double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
266  
267   int             verbose = 0;            /* progress reports to stderr? */
268  
# Line 268 | Line 270 | int            outfmt = 'a';           /* output format */
270  
271   int             rhsubdiv = 1;           /* Reinhart sky subdivisions */
272  
273 < float           skycolor[3] = {.96, 1.004, 1.118};      /* sky coloration */
273 > COLOR           skycolor = {.96, 1.004, 1.118}; /* sky coloration */
274 > COLOR           suncolor = {1., 1., 1.};        /* sun color */
275 > COLOR           grefl = {.2, .2, .2};           /* ground reflectance */
276  
273 int             do_sun = 1;             /* output direct solar contribution? */
274
277   int             nskypatch;              /* number of Reinhart patches */
278   float           *rh_palt;               /* sky patch altitudes (radians) */
279   float           *rh_pazi;               /* sky patch azimuths (radians) */
# Line 294 | Line 296 | int
296   main(int argc, char *argv[])
297   {
298          char    buf[256];
299 +        double  rotation = 0;           /* site rotation (degrees) */
300          double  elevation;              /* site elevation (meters) */
301          int     dir_is_horiz;           /* direct is meas. on horizontal? */
302          float   *mtx_data = NULL;       /* our matrix data */
# Line 309 | Line 312 | main(int argc, char *argv[])
312                                          /* get options */
313          for (i = 1; i < argc && argv[i][0] == '-'; i++)
314                  switch (argv[i][1]) {
315 <                case 'g':
316 <                        grefl = atof(argv[++i]);
315 >                case 'g':                       /* ground reflectance */
316 >                        grefl[0] = atof(argv[++i]);
317 >                        grefl[1] = atof(argv[++i]);
318 >                        grefl[2] = atof(argv[++i]);
319                          break;
320 <                case 'v':
320 >                case 'v':                       /* verbose progress reports */
321                          verbose++;
322                          break;
323 <                case 'o':
323 >                case 'o':                       /* output format */
324                          switch (argv[i][2]) {
325                          case 'f':
326                          case 'd':
# Line 326 | Line 331 | main(int argc, char *argv[])
331                                  goto userr;
332                          }
333                          break;
334 <                case 'm':
334 >                case 'O':                       /* output type */
335 >                        switch (argv[i][2]) {
336 >                        case '0':
337 >                                output = 0;
338 >                                break;
339 >                        case '1':
340 >                                output = 1;
341 >                                break;
342 >                        default:
343 >                                goto userr;
344 >                        }
345 >                        if (argv[i][3])
346 >                                goto userr;
347 >                        break;
348 >                case 'm':                       /* Reinhart subdivisions */
349                          rhsubdiv = atoi(argv[++i]);
350                          break;
351 <                case 'c':
351 >                case 'c':                       /* sky color */
352                          skycolor[0] = atof(argv[++i]);
353                          skycolor[1] = atof(argv[++i]);
354                          skycolor[2] = atof(argv[++i]);
355                          break;
356 <                case 'd':
338 <                        do_sun = 1;
356 >                case 'd':                       /* solar (direct) only */
357                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
358 +                        if (suncolor[1] <= 1e-4)
359 +                                suncolor[0] = suncolor[1] = suncolor[2] = 1;
360                          break;
361 <                case 's':
362 <                        do_sun = 0;
361 >                case 's':                       /* sky only (no direct) */
362 >                        suncolor[0] = suncolor[1] = suncolor[2] = 0;
363                          if (skycolor[1] <= 1e-4)
364                                  skycolor[0] = skycolor[1] = skycolor[2] = 1;
365                          break;
366 +                case 'r':                       /* rotate distribution */
367 +                        if (argv[i][2] && argv[i][2] != 'z')
368 +                                goto userr;
369 +                        rotation = atof(argv[++i]);
370 +                        break;
371 +                case '5':                       /* 5-phase calculation */
372 +                        nsuns = 1;
373 +                        fixed_sun_sa = 6.797e-05;
374 +                        break;
375                  default:
376                          goto userr;
377                  }
# Line 397 | Line 426 | main(int argc, char *argv[])
426                                  progname, s_latitude, s_longitude);
427                  fprintf(stderr, "%s: %d sky patches per time step\n",
428                                  progname, nskypatch);
429 +                if (rotation != 0)
430 +                        fprintf(stderr, "%s: rotating output %.0f degrees\n",
431 +                                        progname, rotation);
432          }
433                                          /* convert quantities to radians */
434          s_latitude = DegToRad(s_latitude);
# Line 420 | Line 452 | main(int argc, char *argv[])
452                  sda = sdec(julian_date);
453                  sta = stadj(julian_date);
454                  altitude = salt(sda, hr+sta);
455 <                azimuth = sazi(sda, hr+sta) + PI;
455 >                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
456                                          /* convert measured values */
457                  if (dir_is_horiz && altitude > 0.)
458                          dir /= sin(altitude);
# Line 433 | Line 465 | main(int argc, char *argv[])
465                  }
466                                          /* compute sky patch values */
467                  ComputeSky(mtx_data+mtx_offset);
468 <                if (do_sun)
437 <                        AddDirect(mtx_data+mtx_offset);
468 >                AddDirect(mtx_data+mtx_offset);
469          }
470                                          /* check for junk at end */
471          while ((i = fgetc(stdin)) != EOF)
# Line 494 | Line 525 | main(int argc, char *argv[])
525                  fprintf(stderr, "%s: done.\n", progname);
526          exit(0);
527   userr:
528 <        fprintf(stderr, "Usage: %s [-v][-d|-s][-m N][-g refl][-c r g b][-o{f|d}] [tape.wea]\n",
528 >        fprintf(stderr, "Usage: %s [-v][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n",
529                          progname);
530          exit(1);
531   fmterr:
# Line 515 | Line 546 | ComputeSky(float *parr)
546   {
547          int index;                      /* Category index */
548          double norm_diff_illum;         /* Normalized diffuse illuimnance */
518        double zlumin;                  /* Zenith luminance */
549          int i;
550          
551          /* Calculate atmospheric precipitable water content */
552          apwc = CalcPrecipWater(dew_point);
553  
554 <        /* Limit solar altitude to keep circumsolar off zenith */
555 <        if (altitude > DegToRad(87.0))
556 <                altitude = DegToRad(87.0);
554 >        /* Calculate sun zenith angle (don't let it dip below horizon) */
555 >        /* Also limit minimum angle to keep circumsolar off zenith */
556 >        if (altitude <= 0.0)
557 >                sun_zenith = DegToRad(90.0);
558 >        else if (altitude >= DegToRad(87.0))
559 >                sun_zenith = DegToRad(3.0);
560 >        else
561 >                sun_zenith = DegToRad(90.0) - altitude;
562  
528        /* Calculate sun zenith angle */
529        sun_zenith = DegToRad(90.0) - altitude;
530
563          /* Compute the inputs for the calculation of the sky distribution */
564          
565          if (input == 0)                                 /* XXX never used */
# Line 546 | Line 578 | ComputeSky(float *parr)
578                  sky_brightness = CalcSkyBrightness();
579                  sky_clearness =  CalcSkyClearness();
580  
581 +                /* Limit sky clearness */
582 +                if (sky_clearness > 11.9)
583 +                        sky_clearness = 11.9;
584 +
585 +                /* Limit sky brightness */
586 +                if (sky_brightness < 0.01)
587 +                        sky_brightness = 0.01;
588 +
589                  /* Calculate illuminance */
590                  index = GetCategoryIndex();
591                  diff_illum = diff_irrad * CalcDiffuseIllumRatio(index);
# Line 557 | Line 597 | ComputeSky(float *parr)
597                  index = CalcSkyParamFromIllum();
598          }
599  
600 +        if (output == 1) {                      /* hack for solar radiance */
601 +                diff_illum = diff_irrad * WHTEFFICACY;
602 +                dir_illum = dir_irrad * WHTEFFICACY;
603 +        }
604 +
605          if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
606                  memset(parr, 0, sizeof(float)*3*nskypatch);
607                  return;
# Line 565 | Line 610 | ComputeSky(float *parr)
610          parr[0] = diff_illum;
611          if (altitude > 0)
612                  parr[0] += dir_illum * sin(altitude);
613 <        parr[2] = parr[1] = parr[0] *= grefl*(1./PI/WHTEFFICACY);
613 >        parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
614 >        multcolor(parr, grefl);
615  
616          /* Calculate Perez sky model parameters */
617          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
# Line 579 | Line 625 | ComputeSky(float *parr)
625          /* Normalization coefficient */
626          norm_diff_illum = diff_illum / norm_diff_illum;
627  
582        /* Calculate relative zenith luminance */
583        zlumin = CalcRelLuminance(sun_zenith, 0.0);
584
585        /* Calculate absolute zenith illuminance */
586        zlumin *= norm_diff_illum;
587
628          /* Apply to sky patches to get absolute radiance values */
629          for (i = 1; i < nskypatch; i++) {
630 <                scalecolor(parr+3*i, zlumin*(1./WHTEFFICACY));
630 >                scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY));
631                  multcolor(parr+3*i, skycolor);
632          }
633   }
# Line 602 | Line 642 | AddDirect(float *parr)
642          double  wta[NSUNPATCH], wtot;
643          int     i, j, p;
644  
645 <        if (!do_sun || dir_illum < 1e-4)
645 >        if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4)
646                  return;
647 <                                        /* identify NSUNPATCH closest patches */
648 <        for (i = NSUNPATCH; i--; )
647 >                                        /* identify nsuns closest patches */
648 >        if (nsuns > NSUNPATCH)
649 >                nsuns = NSUNPATCH;
650 >        else if (nsuns <= 0)
651 >                nsuns = 1;
652 >        for (i = nsuns; i--; )
653                  near_dprod[i] = -1.;
654          vector(svec, altitude, azimuth);
655          for (p = 1; p < nskypatch; p++) {
# Line 613 | Line 657 | AddDirect(float *parr)
657                  double  dprod;
658                  rh_vector(pvec, p);
659                  dprod = DOT(pvec, svec);
660 <                for (i = 0; i < NSUNPATCH; i++)
660 >                for (i = 0; i < nsuns; i++)
661                          if (dprod > near_dprod[i]) {
662 <                                for (j = NSUNPATCH; --j > i; ) {
662 >                                for (j = nsuns; --j > i; ) {
663                                          near_dprod[j] = near_dprod[j-1];
664                                          near_patch[j] = near_patch[j-1];
665                                  }
# Line 625 | Line 669 | AddDirect(float *parr)
669                          }
670          }
671          wtot = 0;                       /* weight by proximity */
672 <        for (i = NSUNPATCH; i--; )
672 >        for (i = nsuns; i--; )
673                  wtot += wta[i] = 1./(1.002 - near_dprod[i]);
674                                          /* add to nearest patch radiances */
675 <        for (i = NSUNPATCH; i--; ) {
675 >        for (i = nsuns; i--; ) {
676                  float   *pdest = parr + 3*near_patch[i];
677 <                float   val_add = wta[i] * dir_illum /
678 <                                (WHTEFFICACY * wtot * rh_dom[near_patch[i]]);
679 <                *pdest++ += val_add;
680 <                *pdest++ += val_add;
681 <                *pdest++ += val_add;
677 >                float   val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot);
678 >
679 >                val_add /= (fixed_sun_sa > 0)   ? fixed_sun_sa
680 >                                                : rh_dom[near_patch[i]] ;
681 >                *pdest++ += val_add*suncolor[0];
682 >                *pdest++ += val_add*suncolor[1];
683 >                *pdest++ += val_add*suncolor[2];
684          }
685   }
686  
# Line 775 | Line 821 | double CalcSkyClearness()
821          double sz_cubed;        /* Sun zenith angle cubed */
822  
823          /* Calculate sun zenith angle cubed */
824 <        sz_cubed = pow(sun_zenith, 3.0);
824 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
825  
826          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
827                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 806 | Line 852 | double CalcDiffuseIrradiance()
852   double CalcDirectIrradiance()
853   {
854          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
855 <                        * pow(sun_zenith, 3.0)));
855 >                        * sun_zenith*sun_zenith*sun_zenith));
856   }
857  
858   /* Calculate sky brightness and clearness from illuminance values */
# Line 832 | Line 878 | int CalcSkyParamFromIllum()
878                  sky_clearness = 12.0;
879  
880          /* Limit sky brightness */
881 <        if (sky_brightness < 0.05)
881 >        if (sky_brightness < 0.01)
882                          sky_brightness = 0.01;
883  
884          while (((fabs(diff_irrad - test1) > 10.0) ||
# Line 856 | Line 902 | int CalcSkyParamFromIllum()
902                          sky_clearness = 12.0;
903          
904                  /* Limit sky brightness */
905 <                if (sky_brightness < 0.05)
905 >                if (sky_brightness < 0.01)
906                          sky_brightness = 0.01;
907          }
908  
# Line 942 | Line 988 | double CalcRelHorzIllum( float *parr )
988          double rh_illum = 0.0;  /* Relative horizontal illuminance */
989  
990          for (i = 1; i < nskypatch; i++)
991 <                rh_illum += parr[3*i+1] * rh_cos(i);
991 >                rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i];
992  
993 <        return rh_illum * (2.0 * PI / (nskypatch-1));
993 >        return rh_illum;
994   }
995  
996   /* Calculate earth orbit eccentricity correction factor */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines