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.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"
88 + #include "rtio.h"
89   #include "color.h"
90 + #include "sun.h"
91  
92   char *progname;                                                         /* Program name */
93   char errmsg[128];                                                       /* Error message buffer */
# Line 108 | Line 109 | double sky_clearness;                  /* Sky clearness */
109   double solar_rad;                       /* Solar radiance */
110   double sun_zenith;                      /* Sun zenith angle (radians) */
111   int     input = 0;                              /* Input type */
112 + int     output = 0;                             /* Output type */
113  
114   extern double dmax( double, double );
115   extern double CalcAirMass();
# Line 133 | Line 135 | extern void ComputeSky( float *parr );
135   /* Radiuans into degrees */
136   #define RadToDeg(rad)   ((rad)*(180./PI))
137  
136
138   /* Perez sky model coefficients */
139  
140   /* Reference:   Perez, R., R. Seals, and J. Michalsky, 1993. "All- */
# Line 208 | Line 209 | static const CategoryBounds SkyClearCat[8] =
209          { 1.950, 2.800 },
210          { 2.800, 4.500 },
211          { 4.500, 6.200 },
212 <        { 6.200, 12.00 }        /* Clear */
212 >        { 6.200, 12.01 }        /* Clear */
213   };
214  
215   /* Luminous efficacy model coefficients */
# Line 250 | Line 251 | static const ModelCoeff DirectLumEff[8] =
251   #define NSUNPATCH       4               /* max. # patches to spread sun into */
252   #endif
253  
254 < 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;
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? */
# Line 278 | 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 +
291 + static const char *
292 + getfmtname(int fmt)
293 + {
294 +        switch (fmt) {
295 +        case 'a':
296 +                return("ascii");
297 +        case 'f':
298 +                return("float");
299 +        case 'd':
300 +                return("double");
301 +        }
302 +        return("unknown");
303 + }
304 +
305 +
306   int
307   main(int argc, char *argv[])
308   {
309          char    buf[256];
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     ntsteps = 0;            /* number of rows in matrix */
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 */
324          int     mo, da;                 /* month (1-12) and day (1-31) */
325          double  hr;                     /* hour (local standard time) */
# Line 319 | Line 339 | main(int argc, char *argv[])
339                  case 'v':                       /* verbose progress reports */
340                          verbose++;
341                          break;
342 +                case 'h':                       /* turn off header */
343 +                        doheader = 0;
344 +                        break;
345                  case 'o':                       /* output format */
346                          switch (argv[i][2]) {
347                          case 'f':
# Line 330 | Line 353 | main(int argc, char *argv[])
353                                  goto userr;
354                          }
355                          break;
356 +                case 'O':                       /* output type */
357 +                        switch (argv[i][2]) {
358 +                        case '0':
359 +                                output = 0;
360 +                                break;
361 +                        case '1':
362 +                                output = 1;
363 +                                break;
364 +                        default:
365 +                                goto userr;
366 +                        }
367 +                        if (argv[i][3])
368 +                                goto userr;
369 +                        break;
370                  case 'm':                       /* Reinhart subdivisions */
371                          rhsubdiv = atoi(argv[++i]);
372                          break;
# Line 338 | Line 375 | main(int argc, char *argv[])
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)
344 <                                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;
348                        if (skycolor[1] <= 1e-4)
349                                skycolor[0] = skycolor[1] = skycolor[2] = 1;
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 355 | Line 416 | main(int argc, char *argv[])
416                          break;
417                  case '5':                       /* 5-phase calculation */
418                          nsuns = 1;
419 <                        fixed_sun_sa = 6.797e-05;
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 >                                                progname);
423 >                                exit(1);
424 >                        }
425 >                        fixed_sun_sa *= fixed_sun_sa*PI;
426                          break;
427 +                case 'A':                       /* compute average sky */
428 +                        avgSky = 1;
429 +                        break;
430                  default:
431                          goto userr;
432                  }
# Line 367 | Line 437 | main(int argc, char *argv[])
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 409 | 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 419 | Line 496 | main(int argc, char *argv[])
496          s_latitude = DegToRad(s_latitude);
497          s_longitude = DegToRad(s_longitude);
498          s_meridian = DegToRad(s_meridian);
499 +                                        /* initial allocation */
500 +        mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch);
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 <                                        /* make space for next time step */
426 <                mtx_offset = 3*nskypatch*ntsteps++;
427 <                mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch);
428 <                if (dif <= 1e-4) {
429 <                        memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
430 <                        continue;
431 <                }
432 <                if (verbose && mo != last_monthly)
433 <                        fprintf(stderr, "%s: stepping through month %d...\n",
434 <                                                progname, last_monthly=mo);
504 >                int             sun_in_sky;
505                                          /* compute solar position */
506 <                julian_date = jdate(mo, da);
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;
521 +                                        /* make space for next row */
522 +                if (nstored > tstorage) {
523 +                        tstorage += (tstorage>>1) + nstored + 7;
524 +                        mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch);
525 +                }
526 +                ntsteps++;              /* keep count of time steps */
527 +
528 +                if (dir+dif <= 1e-4) {  /* effectively nighttime? */
529 +                        if (!avgSky | !mtx_offset)
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 +                }
538                                          /* convert measured values */
539                  if (dir_is_horiz && altitude > 0.)
540                          dir /= sin(altitude);
# Line 450 | 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 462 | Line 579 | main(int argc, char *argv[])
579                          fputs(buf, stderr); fputc('\n', stderr);
580                          break;
581                  }
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;
589                                          /* write out matrix */
590 +        if (outfmt != 'a')
591 +                SET_FILE_BINARY(stdout);
592   #ifdef getc_unlocked
593          flockfile(stdout);
594   #endif
595          if (verbose)
596                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
597 <                                progname, outfmt=='a' ? "" : "binary ", ntsteps);
597 >                                progname, outfmt=='a' ? "" : "binary ", nstored);
598 >        if (doheader) {
599 >                newheader("RADIANCE", stdout);
600 >                printargs(argc, argv, stdout);
601 >                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
602 >                                        -RadToDeg(s_longitude));
603 >                printf("NROWS=%d\n", nskypatch);
604 >                printf("NCOLS=%d\n", nstored);
605 >                printf("NCOMP=3\n");
606 >                if ((outfmt == 'f') | (outfmt == 'd'))
607 >                        fputendian(stdout);
608 >                fputformat((char *)getfmtname(outfmt), stdout);
609 >                putchar('\n');
610 >        }
611                                          /* patches are rows (outer sort) */
612          for (i = 0; i < nskypatch; i++) {
613                  mtx_offset = 3*i;
614                  switch (outfmt) {
615                  case 'a':
616 <                        for (j = 0; j < ntsteps; j++) {
616 >                        for (j = 0; j < nstored; j++) {
617                                  printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset],
618                                                  mtx_data[mtx_offset+1],
619                                                  mtx_data[mtx_offset+2]);
620                                  mtx_offset += 3*nskypatch;
621                          }
622 <                        if (ntsteps > 1)
622 >                        if (nstored > 1)
623                                  fputc('\n', stdout);
624                          break;
625                  case 'f':
626 <                        for (j = 0; j < ntsteps; j++) {
627 <                                fwrite(mtx_data+mtx_offset, sizeof(float), 3,
626 >                        for (j = 0; j < nstored; j++) {
627 >                                putbinary(mtx_data+mtx_offset, sizeof(float), 3,
628                                                  stdout);
629                                  mtx_offset += 3*nskypatch;
630                          }
631                          break;
632                  case 'd':
633 <                        for (j = 0; j < ntsteps; j++) {
633 >                        for (j = 0; j < nstored; j++) {
634                                  double  ment[3];
635                                  ment[0] = mtx_data[mtx_offset];
636                                  ment[1] = mtx_data[mtx_offset+1];
637                                  ment[2] = mtx_data[mtx_offset+2];
638 <                                fwrite(ment, sizeof(double), 3, stdout);
638 >                                putbinary(ment, sizeof(double), 3, stdout);
639                                  mtx_offset += 3*nskypatch;
640                          }
641                          break;
# Line 504 | 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][-d|-s][-r deg][-m N][-g r g b][-c r g b][-o{f|d}] [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 569 | Line 709 | ComputeSky(float *parr)
709  
710                  /* Limit sky brightness */
711                  if (sky_brightness < 0.01)
712 <                        sky_brightness = 0.01;
712 >                        sky_brightness = 0.01;
713  
714                  /* Calculate illuminance */
715                  index = GetCategoryIndex();
# Line 582 | Line 722 | ComputeSky(float *parr)
722                  index = CalcSkyParamFromIllum();
723          }
724  
725 <        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
726 <                memset(parr, 0, sizeof(float)*3*nskypatch);
727 <                return;
725 >        if (output == 1) {                      /* hack for solar radiance */
726 >                diff_illum = diff_irrad * WHTEFFICACY;
727 >                dir_illum = dir_irrad * WHTEFFICACY;
728          }
729          /* Compute ground radiance (include solar contribution if any) */
730          parr[0] = diff_illum;
# Line 593 | 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 602 | Line 746 | ComputeSky(float *parr)
746          /* Calculate relative horizontal illuminance */
747          norm_diff_illum = CalcRelHorzIllum(parr);
748  
749 +        /* Check for zero sky -- make uniform in that case */
750 +        if (norm_diff_illum <= FTINY) {
751 +                for (i = 1; i < nskypatch; i++)
752 +                        setcolor(parr+3*i, 1., 1., 1.);
753 +                norm_diff_illum = PI;
754 +        }
755          /* Normalization coefficient */
756          norm_diff_illum = diff_illum / norm_diff_illum;
757  
# Line 664 | Line 814 | AddDirect(float *parr)
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) */
838   int
839   rh_init(void)
# Line 801 | Line 971 | double CalcSkyClearness()
971          double sz_cubed;        /* Sun zenith angle cubed */
972  
973          /* Calculate sun zenith angle cubed */
974 <        sz_cubed = pow(sun_zenith, 3.0);
974 >        sz_cubed = sun_zenith*sun_zenith*sun_zenith;
975  
976          return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 *
977                          sz_cubed) / (1.0 + 1.041 * sz_cubed);
# Line 832 | Line 1002 | double CalcDiffuseIrradiance()
1002   double CalcDirectIrradiance()
1003   {
1004          return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041
1005 <                        * pow(sun_zenith, 3.0)));
1005 >                        * sun_zenith*sun_zenith*sun_zenith));
1006   }
1007  
1008   /* Calculate sky brightness and clearness from illuminance values */
# Line 871 | Line 1041 | int CalcSkyParamFromIllum()
1041                  /* Convert illuminance to irradiance */
1042                  index = GetCategoryIndex();
1043                  diff_irrad = diff_illum / CalcDiffuseIllumRatio(index);
1044 <                dir_irrad = dir_illum / CalcDirectIllumRatio(index);
1044 >                dir_irrad = CalcDirectIllumRatio(index);
1045 >                if (dir_irrad > 0.1)
1046 >                        dir_irrad = dir_illum / dir_irrad;
1047          
1048                  /* Calculate sky brightness and clearness */
1049                  sky_brightness = CalcSkyBrightness();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines