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.10 by greg, Sat Apr 6 00:44:59 2013 UTC vs.
Revision 2.30 by greg, Thu Nov 7 23:15:07 2019 UTC

# Line 85 | Line 85 | static const char RCSid[] = "$Id$";
85   #include <stdlib.h>
86   #include <string.h>
87   #include <ctype.h>
88 + #include "platform.h"
89   #include "rtmath.h"
90 + #include "rtio.h"
91   #include "color.h"
92 + #include "sun.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 250 | Line 254 | static const ModelCoeff DirectLumEff[8] =
254   #define NSUNPATCH       4               /* max. # patches to spread sun into */
255   #endif
256  
253 extern int jdate(int month, int day);
254 extern double stadj(int  jd);
255 extern double sdec(int  jd);
256 extern double salt(double sd, double st);
257 extern double sazi(double sd, double st);
258                                        /* sun calculation constants */
259 extern double  s_latitude;
260 extern double  s_longitude;
261 extern double  s_meridian;
262
257   int             nsuns = NSUNPATCH;      /* number of sun patches to use */
258   double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
259  
# Line 291 | Line 285 | extern int     rh_init(void);
285   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
286   extern void     AddDirect(float *parr);
287  
288 +
289 + static const char *
290 + getfmtname(int fmt)
291 + {
292 +        switch (fmt) {
293 +        case 'a':
294 +                return("ascii");
295 +        case 'f':
296 +                return("float");
297 +        case 'd':
298 +                return("double");
299 +        }
300 +        return("unknown");
301 + }
302 +
303 +
304   int
305   main(int argc, char *argv[])
306   {
307          char    buf[256];
308 +        int     doheader = 1;           /* output header? */
309          double  rotation = 0;           /* site rotation (degrees) */
310          double  elevation;              /* site elevation (meters) */
311          int     dir_is_horiz;           /* direct is meas. on horizontal? */
312          float   *mtx_data = NULL;       /* our matrix data */
313 <        int     ntsteps = 0;            /* number of rows in matrix */
313 >        int     avgSky = 0;             /* compute average sky r.t. matrix? */
314 >        int     ntsteps = 0;            /* number of time steps */
315 >        int     tstorage = 0;           /* number of allocated time steps */
316 >        int     nstored = 0;            /* number of time steps in matrix */
317          int     last_monthly = 0;       /* month of last report */
318 +        int     inconsistent = 0;       /* inconsistent options set? */
319          int     mo, da;                 /* month (1-12) and day (1-31) */
320          double  hr;                     /* hour (local standard time) */
321          double  dir, dif;               /* direct and diffuse values */
# Line 319 | Line 334 | main(int argc, char *argv[])
334                  case 'v':                       /* verbose progress reports */
335                          verbose++;
336                          break;
337 +                case 'h':                       /* turn off header */
338 +                        doheader = 0;
339 +                        break;
340                  case 'o':                       /* output format */
341                          switch (argv[i][2]) {
342                          case 'f':
# Line 330 | Line 348 | main(int argc, char *argv[])
348                                  goto userr;
349                          }
350                          break;
351 +                case 'O':                       /* output type */
352 +                        switch (argv[i][2]) {
353 +                        case '0':
354 +                                output = 0;
355 +                                break;
356 +                        case '1':
357 +                                output = 1;
358 +                                break;
359 +                        default:
360 +                                goto userr;
361 +                        }
362 +                        if (argv[i][3])
363 +                                goto userr;
364 +                        break;
365                  case 'm':                       /* Reinhart subdivisions */
366                          rhsubdiv = atoi(argv[++i]);
367                          break;
368                  case 'c':                       /* sky color */
369 +                        inconsistent |= (skycolor[1] <= 1e-4);
370                          skycolor[0] = atof(argv[++i]);
371                          skycolor[1] = atof(argv[++i]);
372                          skycolor[2] = atof(argv[++i]);
373                          break;
374                  case 'd':                       /* solar (direct) only */
375                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
376 <                        if (suncolor[1] <= 1e-4)
376 >                        if (suncolor[1] <= 1e-4) {
377 >                                inconsistent = 1;
378                                  suncolor[0] = suncolor[1] = suncolor[2] = 1;
379 +                        }
380                          break;
381                  case 's':                       /* sky only (no direct) */
382                          suncolor[0] = suncolor[1] = suncolor[2] = 0;
383 <                        if (skycolor[1] <= 1e-4)
383 >                        if (skycolor[1] <= 1e-4) {
384 >                                inconsistent = 1;
385                                  skycolor[0] = skycolor[1] = skycolor[2] = 1;
386 +                        }
387                          break;
388                  case 'r':                       /* rotate distribution */
389                          if (argv[i][2] && argv[i][2] != 'z')
# Line 355 | Line 392 | main(int argc, char *argv[])
392                          break;
393                  case '5':                       /* 5-phase calculation */
394                          nsuns = 1;
395 <                        fixed_sun_sa = 6.797e-05;
395 >                        fixed_sun_sa = PI/360.*atof(argv[++i]);
396 >                        if (fixed_sun_sa <= 0) {
397 >                                fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
398 >                                                argv[0]);
399 >                                exit(1);
400 >                        }
401 >                        fixed_sun_sa *= fixed_sun_sa*PI;
402                          break;
403 +                case 'A':                       /* compute average sky */
404 +                        avgSky = 1;
405 +                        break;
406                  default:
407                          goto userr;
408                  }
409          if (i < argc-1)
410                  goto userr;
411 +        if (inconsistent)
412 +                fprintf(stderr, "%s: WARNING: inconsistent -s, -d, -c options!\n",
413 +                                progname);
414          if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
415                  fprintf(stderr, "%s: cannot open '%s' for input\n",
416                                  progname, argv[i]);
# Line 419 | Line 468 | main(int argc, char *argv[])
468          s_latitude = DegToRad(s_latitude);
469          s_longitude = DegToRad(s_longitude);
470          s_meridian = DegToRad(s_meridian);
471 +                                        /* initial allocation */
472 +        mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch);
473                                          /* process each time step in tape */
474          while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) {
475                  double          sda, sta;
476 <                                        /* make space for next time step */
477 <                mtx_offset = 3*nskypatch*ntsteps++;
478 <                mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch);
476 >
477 >                mtx_offset = 3*nskypatch*nstored;
478 >                nstored += !avgSky | !nstored;
479 >                                        /* make space for next row */
480 >                if (nstored > tstorage) {
481 >                        tstorage += (tstorage>>1) + nstored + 7;
482 >                        mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch);
483 >                }
484 >                ntsteps++;              /* keep count of time steps */
485                  if (dif <= 1e-4) {
486 <                        memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
486 >                        if (!avgSky | !mtx_offset)
487 >                                memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
488                          continue;
489                  }
490                  if (verbose && mo != last_monthly)
# Line 451 | Line 509 | main(int argc, char *argv[])
509                                          /* compute sky patch values */
510                  ComputeSky(mtx_data+mtx_offset);
511                  AddDirect(mtx_data+mtx_offset);
512 +                                        /* update cumulative sky? */
513 +                for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
514 +                        mtx_data[i] += mtx_data[mtx_offset+i];
515          }
516                                          /* check for junk at end */
517          while ((i = fgetc(stdin)) != EOF)
# Line 462 | Line 523 | main(int argc, char *argv[])
523                          fputs(buf, stderr); fputc('\n', stderr);
524                          break;
525                  }
526 +        if (!ntsteps) {
527 +                fprintf(stderr, "%s: no valid time steps on input\n", progname);
528 +                exit(1);
529 +        }
530 +        dif = 1./(double)ntsteps;       /* average sky? */
531 +        for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
532 +                mtx_data[i] *= dif;
533                                          /* write out matrix */
534 +        if (outfmt != 'a')
535 +                SET_FILE_BINARY(stdout);
536   #ifdef getc_unlocked
537          flockfile(stdout);
538   #endif
539          if (verbose)
540                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
541 <                                progname, outfmt=='a' ? "" : "binary ", ntsteps);
541 >                                progname, outfmt=='a' ? "" : "binary ", nstored);
542 >        if (doheader) {
543 >                newheader("RADIANCE", stdout);
544 >                printargs(argc, argv, stdout);
545 >                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
546 >                                        -RadToDeg(s_longitude));
547 >                printf("NROWS=%d\n", nskypatch);
548 >                printf("NCOLS=%d\n", nstored);
549 >                printf("NCOMP=3\n");
550 >                if ((outfmt == 'f') | (outfmt == 'd'))
551 >                        fputendian(stdout);
552 >                fputformat((char *)getfmtname(outfmt), stdout);
553 >                putchar('\n');
554 >        }
555                                          /* patches are rows (outer sort) */
556          for (i = 0; i < nskypatch; i++) {
557                  mtx_offset = 3*i;
558                  switch (outfmt) {
559                  case 'a':
560 <                        for (j = 0; j < ntsteps; j++) {
560 >                        for (j = 0; j < nstored; j++) {
561                                  printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset],
562                                                  mtx_data[mtx_offset+1],
563                                                  mtx_data[mtx_offset+2]);
564                                  mtx_offset += 3*nskypatch;
565                          }
566 <                        if (ntsteps > 1)
566 >                        if (nstored > 1)
567                                  fputc('\n', stdout);
568                          break;
569                  case 'f':
570 <                        for (j = 0; j < ntsteps; j++) {
571 <                                fwrite(mtx_data+mtx_offset, sizeof(float), 3,
570 >                        for (j = 0; j < nstored; j++) {
571 >                                putbinary(mtx_data+mtx_offset, sizeof(float), 3,
572                                                  stdout);
573                                  mtx_offset += 3*nskypatch;
574                          }
575                          break;
576                  case 'd':
577 <                        for (j = 0; j < ntsteps; j++) {
577 >                        for (j = 0; j < nstored; j++) {
578                                  double  ment[3];
579                                  ment[0] = mtx_data[mtx_offset];
580                                  ment[1] = mtx_data[mtx_offset+1];
581                                  ment[2] = mtx_data[mtx_offset+2];
582 <                                fwrite(ment, sizeof(double), 3, stdout);
582 >                                putbinary(ment, sizeof(double), 3, stdout);
583                                  mtx_offset += 3*nskypatch;
584                          }
585                          break;
# Line 510 | Line 593 | main(int argc, char *argv[])
593                  fprintf(stderr, "%s: done.\n", progname);
594          exit(0);
595   userr:
596 <        fprintf(stderr, "Usage: %s [-v][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}] [tape.wea]\n",
596 >        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",
597                          progname);
598          exit(1);
599   fmterr:
# Line 569 | Line 652 | ComputeSky(float *parr)
652  
653                  /* Limit sky brightness */
654                  if (sky_brightness < 0.01)
655 <                        sky_brightness = 0.01;
655 >                        sky_brightness = 0.01;
656  
657                  /* Calculate illuminance */
658                  index = GetCategoryIndex();
# Line 582 | Line 665 | ComputeSky(float *parr)
665                  index = CalcSkyParamFromIllum();
666          }
667  
668 +        if (output == 1) {                      /* hack for solar radiance */
669 +                diff_illum = diff_irrad * WHTEFFICACY;
670 +                dir_illum = dir_irrad * WHTEFFICACY;
671 +        }
672 +
673          if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
674                  memset(parr, 0, sizeof(float)*3*nskypatch);
675                  return;
# Line 602 | Line 690 | ComputeSky(float *parr)
690          /* Calculate relative horizontal illuminance */
691          norm_diff_illum = CalcRelHorzIllum(parr);
692  
693 +        /* Check for zero sky -- make uniform in that case */
694 +        if (norm_diff_illum <= FTINY) {
695 +                for (i = 1; i < nskypatch; i++)
696 +                        setcolor(parr+3*i, 1., 1., 1.);
697 +                norm_diff_illum = PI;
698 +        }
699          /* Normalization coefficient */
700          norm_diff_illum = diff_illum / norm_diff_illum;
701  
# Line 801 | Line 895 | double CalcSkyClearness()
895          double sz_cubed;        /* Sun zenith angle cubed */
896  
897          /* Calculate sun zenith angle cubed */
898 <        sz_cubed = pow(sun_zenith, 3.0);
898 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
899  
900          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
901                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 832 | Line 926 | double CalcDiffuseIrradiance()
926   double CalcDirectIrradiance()
927   {
928          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
929 <                        * pow(sun_zenith, 3.0)));
929 >                        * sun_zenith*sun_zenith*sun_zenith));
930   }
931  
932   /* Calculate sky brightness and clearness from illuminance values */
# Line 871 | Line 965 | int CalcSkyParamFromIllum()
965                  /* Convert illuminance to irradiance */
966                  index = GetCategoryIndex();
967                  diff_irrad = diff_illum / CalcDiffuseIllumRatio(index);
968 <                dir_irrad = dir_illum / CalcDirectIllumRatio(index);
968 >                dir_irrad = CalcDirectIllumRatio(index);
969 >                if (dir_irrad > 0.1)
970 >                        dir_irrad = dir_illum / dir_irrad;
971          
972                  /* Calculate sky brightness and clearness */
973                  sky_brightness = CalcSkyBrightness();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines