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.30 by greg, Thu Nov 7 23:15:07 2019 UTC vs.
Revision 2.37 by greg, Sat Aug 15 03:28:56 2020 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"
# Line 137 | Line 135 | extern void ComputeSky( float *parr );
135   /* Radiuans into degrees */
136   #define RadToDeg(rad)   ((rad)*(180./PI))
137  
140
138   /* Perez sky model coefficients */
139  
140   /* Reference:   Perez, R., R. Seals, and J. Michalsky, 1993. "All- */
# Line 254 | Line 251 | static const ModelCoeff DirectLumEff[8] =
251   #define NSUNPATCH       4               /* max. # patches to spread sun into */
252   #endif
253  
254 + #define SUN_ANG_DEG     0.533           /* sun full-angle in degrees */
255 +
256   int             nsuns = NSUNPATCH;      /* number of sun patches to use */
257   double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
258  
# Line 272 | Line 271 | float          *rh_palt;               /* sky patch altitudes (radians) */
271   float           *rh_pazi;               /* sky patch azimuths (radians) */
272   float           *rh_dom;                /* sky patch solid angle (sr) */
273  
274 < #define         vector(v,alt,azi)       (       (v)[1] = tcos(alt), \
275 <                                                (v)[0] = (v)[1]*tsin(azi), \
276 <                                                (v)[1] *= tcos(azi), \
277 <                                                (v)[2] = tsin(alt) )
274 > #define         vector(v,alt,azi)       (       (v)[1] = cos(alt), \
275 >                                                (v)[0] = (v)[1]*sin(azi), \
276 >                                                (v)[1] *= cos(azi), \
277 >                                                (v)[2] = sin(alt) )
278  
279   #define         rh_vector(v,i)          vector(v,rh_palt[i],rh_pazi[i])
280  
281   #define         rh_cos(i)               tsin(rh_palt[i])
282  
283 + #define         solar_minute(jd,hr)     ((24*60)*((jd)-1)+(int)((hr)*60.+.5))
284 +
285   extern int      rh_init(void);
286   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
287 + extern void     OutputSun(int id, int goodsun, FILE *fp, FILE *mfp);
288   extern void     AddDirect(float *parr);
289  
290  
# Line 308 | Line 310 | main(int argc, char *argv[])
310          int     doheader = 1;           /* output header? */
311          double  rotation = 0;           /* site rotation (degrees) */
312          double  elevation;              /* site elevation (meters) */
313 +        int     leap_day = 0;           /* add leap day? */
314 +        int     sun_hours_only = 0;     /* only output sun hours? */
315          int     dir_is_horiz;           /* direct is meas. on horizontal? */
316 +        FILE    *sunsfp = NULL;         /* output file for individual suns */
317 +        FILE    *modsfp = NULL;         /* modifier output file */
318          float   *mtx_data = NULL;       /* our matrix data */
319          int     avgSky = 0;             /* compute average sky r.t. matrix? */
320          int     ntsteps = 0;            /* number of time steps */
321          int     tstorage = 0;           /* number of allocated time steps */
322          int     nstored = 0;            /* number of time steps in matrix */
323          int     last_monthly = 0;       /* month of last report */
318        int     inconsistent = 0;       /* inconsistent options set? */
324          int     mo, da;                 /* month (1-12) and day (1-31) */
325          double  hr;                     /* hour (local standard time) */
326          double  dir, dif;               /* direct and diffuse values */
# Line 366 | Line 371 | main(int argc, char *argv[])
371                          rhsubdiv = atoi(argv[++i]);
372                          break;
373                  case 'c':                       /* sky color */
369                        inconsistent |= (skycolor[1] <= 1e-4);
374                          skycolor[0] = atof(argv[++i]);
375                          skycolor[1] = atof(argv[++i]);
376                          skycolor[2] = atof(argv[++i]);
377                          break;
378 +                case 'D':                       /* output suns to file */
379 +                        if (strcmp(argv[++i], "-")) {
380 +                                sunsfp = fopen(argv[i], "w");
381 +                                if (sunsfp == NULL) {
382 +                                        fprintf(stderr,
383 +                                        "%s: cannot open '%s' for output\n",
384 +                                                        progname, argv[i]);
385 +                                        exit(1);
386 +                                }
387 +                                break;          /* still may output matrix */
388 +                        }
389 +                        sunsfp = stdout;        /* sending to stdout, so... */
390 +                        /* fall through */
391 +                case 'n':                       /* no matrix output */
392 +                        avgSky = -1;
393 +                        rhsubdiv = 1;
394 +                        /* fall through */
395                  case 'd':                       /* solar (direct) only */
396                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
397 <                        if (suncolor[1] <= 1e-4) {
398 <                                inconsistent = 1;
399 <                                suncolor[0] = suncolor[1] = suncolor[2] = 1;
397 >                        grefl[0] = grefl[1] = grefl[2] = 0;
398 >                        break;
399 >                case 'M':                       /* send sun modifiers to file */
400 >                        if ((modsfp = fopen(argv[++i], "w")) == NULL) {
401 >                                fprintf(stderr, "%s: cannot open '%s' for output\n",
402 >                                                progname, argv[i]);
403 >                                exit(1);
404                          }
405                          break;
406                  case 's':                       /* sky only (no direct) */
407                          suncolor[0] = suncolor[1] = suncolor[2] = 0;
383                        if (skycolor[1] <= 1e-4) {
384                                inconsistent = 1;
385                                skycolor[0] = skycolor[1] = skycolor[2] = 1;
386                        }
408                          break;
409 +                case 'u':                       /* solar hours only */
410 +                        sun_hours_only = 1;
411 +                        break;
412                  case 'r':                       /* rotate distribution */
413                          if (argv[i][2] && argv[i][2] != 'z')
414                                  goto userr;
# Line 395 | Line 419 | main(int argc, char *argv[])
419                          fixed_sun_sa = PI/360.*atof(argv[++i]);
420                          if (fixed_sun_sa <= 0) {
421                                  fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
422 <                                                argv[0]);
422 >                                                progname);
423                                  exit(1);
424                          }
425                          fixed_sun_sa *= fixed_sun_sa*PI;
# Line 408 | Line 432 | main(int argc, char *argv[])
432                  }
433          if (i < argc-1)
434                  goto userr;
411        if (inconsistent)
412                fprintf(stderr, "%s: WARNING: inconsistent -s, -d, -c options!\n",
413                                progname);
435          if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
436                  fprintf(stderr, "%s: cannot open '%s' for input\n",
437                                  progname, argv[i]);
438                  exit(1);
439          }
440 +        if ((modsfp != NULL) & (sunsfp == NULL))
441 +                fprintf(stderr, "%s: warning -M output will be empty without -D\n",
442 +                                progname);
443          if (verbose) {
444                  if (i == argc-1)
445                          fprintf(stderr, "%s: reading weather tape '%s'\n",
# Line 458 | Line 482 | main(int argc, char *argv[])
482                  fprintf(stderr, "%s: location '%s'\n", progname, buf);
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 file\n",
490 >                                        progname);
491                  if (rotation != 0)
492                          fprintf(stderr, "%s: rotating output %.0f degrees\n",
493                                          progname, rotation);
# Line 473 | Line 501 | main(int argc, char *argv[])
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;
504 +                int             sun_in_sky;
505 +                                        /* compute solar position */
506 +                if ((mo == 2) & (da == 29)) {
507 +                        julian_date = 60;
508 +                        leap_day = 1;
509 +                } else
510 +                        julian_date = jdate(mo, da) + leap_day;
511 +                sda = sdec(julian_date);
512 +                sta = stadj(julian_date);
513 +                altitude = salt(sda, hr+sta);
514 +                sun_in_sky = (altitude > -DegToRad(SUN_ANG_DEG/2.));
515 +                if (sun_hours_only && !sun_in_sky)
516 +                        continue;       /* skipping nighttime points */
517 +                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
518  
519                  mtx_offset = 3*nskypatch*nstored;
520                  nstored += !avgSky | !nstored;
# Line 482 | Line 524 | main(int argc, char *argv[])
524                          mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch);
525                  }
526                  ntsteps++;              /* keep count of time steps */
527 <                if (dif <= 1e-4) {
527 >
528 >                if (dir+dif <= 1e-4) {  /* effectively nighttime? */
529                          if (!avgSky | !mtx_offset)
530 <                                memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
530 >                                memset(mtx_data+mtx_offset, 0,
531 >                                                sizeof(float)*3*nskypatch);
532 >                                        /* output black sun? */
533 >                        if (sunsfp && sun_in_sky)
534 >                                OutputSun(solar_minute(julian_date,hr), 0,
535 >                                                        sunsfp, modsfp);
536                          continue;
537                  }
490                if (verbose && mo != last_monthly)
491                        fprintf(stderr, "%s: stepping through month %d...\n",
492                                                progname, last_monthly=mo);
493                                        /* compute solar position */
494                julian_date = jdate(mo, da);
495                sda = sdec(julian_date);
496                sta = stadj(julian_date);
497                altitude = salt(sda, hr+sta);
498                azimuth = sazi(sda, hr+sta) + PI - DegToRad(rotation);
538                                          /* convert measured values */
539                  if (dir_is_horiz && altitude > 0.)
540                          dir /= sin(altitude);
# Line 508 | Line 547 | main(int argc, char *argv[])
547                  }
548                                          /* compute sky patch values */
549                  ComputeSky(mtx_data+mtx_offset);
550 +                                        /* output sun if requested */
551 +                if (sunsfp && sun_in_sky)
552 +                        OutputSun(solar_minute(julian_date,hr), 1,
553 +                                                sunsfp, modsfp);
554 +
555 +                if (avgSky < 0)         /* no matrix? */
556 +                        continue;
557 +
558                  AddDirect(mtx_data+mtx_offset);
559                                          /* update cumulative sky? */
560                  for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
561                          mtx_data[i] += mtx_data[mtx_offset+i];
562 +                                        /* monthly reporting */
563 +                if (verbose && mo != last_monthly)
564 +                        fprintf(stderr, "%s: stepping through month %d...\n",
565 +                                                progname, last_monthly=mo);
566 +                                        /* note whether leap-day was given */
567          }
568 +        if (!ntsteps) {
569 +                fprintf(stderr, "%s: no valid time steps on input\n", progname);
570 +                exit(1);
571 +        }
572                                          /* check for junk at end */
573          while ((i = fgetc(stdin)) != EOF)
574                  if (!isspace(i)) {
# Line 523 | Line 579 | main(int argc, char *argv[])
579                          fputs(buf, stderr); fputc('\n', stderr);
580                          break;
581                  }
582 <        if (!ntsteps) {
583 <                fprintf(stderr, "%s: no valid time steps on input\n", progname);
584 <                exit(1);
585 <        }
582 >
583 >        if (avgSky < 0)                 /* no matrix output? */
584 >                goto alldone;
585 >
586          dif = 1./(double)ntsteps;       /* average sky? */
587          for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
588                  mtx_data[i] *= dif;
# Line 587 | Line 643 | main(int argc, char *argv[])
643                  if (ferror(stdout))
644                          goto writerr;
645          }
646 <        if (fflush(stdout) == EOF)
646 > alldone:
647 >        if (fflush(NULL) == EOF)
648                  goto writerr;
649          if (verbose)
650                  fprintf(stderr, "%s: done.\n", progname);
651          exit(0);
652   userr:
653 <        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",
653 >        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",
654                          progname);
655          exit(1);
656   fmterr:
657 <        fprintf(stderr, "%s: input weather tape format error\n", progname);
657 >        fprintf(stderr, "%s: weather tape format error in header\n", progname);
658          exit(1);
659   writerr:
660          fprintf(stderr, "%s: write error on output\n", progname);
# Line 669 | Line 726 | ComputeSky(float *parr)
726                  diff_illum = diff_irrad * WHTEFFICACY;
727                  dir_illum = dir_irrad * WHTEFFICACY;
728          }
672
673        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
674                memset(parr, 0, sizeof(float)*3*nskypatch);
675                return;
676        }
729          /* Compute ground radiance (include solar contribution if any) */
730          parr[0] = diff_illum;
731          if (altitude > 0)
# Line 681 | Line 733 | ComputeSky(float *parr)
733          parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
734          multcolor(parr, grefl);
735  
736 +        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
737 +                memset(parr+3, 0, sizeof(float)*3*(nskypatch-1));
738 +                return;
739 +        }
740          /* Calculate Perez sky model parameters */
741          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
742  
# Line 756 | Line 812 | AddDirect(float *parr)
812                  *pdest++ += val_add*suncolor[1];
813                  *pdest++ += val_add*suncolor[2];
814          }
815 + }
816 +
817 + /* Output a sun to indicated file if appropriate for this time step */
818 + void
819 + OutputSun(int id, int goodsun, FILE *fp, FILE *mfp)
820 + {
821 +        double  srad;
822 +        FVECT   sv;
823 +
824 +        srad = DegToRad(SUN_ANG_DEG/2.);
825 +        srad = goodsun ? dir_illum/(WHTEFFICACY * PI*srad*srad) : 0;
826 +        vector(sv, altitude, azimuth);
827 +        fprintf(fp, "\nvoid light solar%d\n0\n0\n", id);
828 +        fprintf(fp, "3 %.3e %.3e %.3e\n", srad*suncolor[0],
829 +                        srad*suncolor[1], srad*suncolor[2]);
830 +        fprintf(fp, "\nsolar%d source sun%d\n0\n0\n", id, id);
831 +        fprintf(fp, "4 %.6f %.6f %.6f %.4f\n", sv[0], sv[1], sv[2], SUN_ANG_DEG);
832 +        
833 +        if (mfp != NULL)                /* saving modifier IDs? */
834 +                fprintf(mfp, "solar%d\n", id);
835   }
836  
837   /* Initialize Reinhart sky patch positions (GW) */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines