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.12 by greg, Thu Jun 6 20:21:10 2013 UTC vs.
Revision 2.34 by greg, Tue Jan 7 01:42:30 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 251 | Line 252 | static const ModelCoeff DirectLumEff[8] =
252   #define NSUNPATCH       4               /* max. # patches to spread sun into */
253   #endif
254  
254 extern int jdate(int month, int day);
255 extern double stadj(int  jd);
256 extern double sdec(int  jd);
257 extern double salt(double sd, double st);
258 extern double sazi(double sd, double st);
259                                        /* sun calculation constants */
260 extern double  s_latitude;
261 extern double  s_longitude;
262 extern double  s_meridian;
263
255   int             nsuns = NSUNPATCH;      /* number of sun patches to use */
256   double          fixed_sun_sa = -1;      /* fixed solid angle per sun? */
257  
# Line 292 | Line 283 | extern int     rh_init(void);
283   extern float *  resize_dmatrix(float *mtx_data, int nsteps, int npatch);
284   extern void     AddDirect(float *parr);
285  
286 +
287 + static const char *
288 + getfmtname(int fmt)
289 + {
290 +        switch (fmt) {
291 +        case 'a':
292 +                return("ascii");
293 +        case 'f':
294 +                return("float");
295 +        case 'd':
296 +                return("double");
297 +        }
298 +        return("unknown");
299 + }
300 +
301 +
302   int
303   main(int argc, char *argv[])
304   {
305          char    buf[256];
306 +        int     doheader = 1;           /* output header? */
307          double  rotation = 0;           /* site rotation (degrees) */
308          double  elevation;              /* site elevation (meters) */
309          int     dir_is_horiz;           /* direct is meas. on horizontal? */
310 +        FILE    *sunsfp = NULL;         /* output file for individual suns */
311          float   *mtx_data = NULL;       /* our matrix data */
312 <        int     ntsteps = 0;            /* number of rows in matrix */
312 >        int     avgSky = 0;             /* compute average sky r.t. matrix? */
313 >        int     ntsteps = 0;            /* number of time steps */
314 >        int     tstorage = 0;           /* number of allocated time steps */
315 >        int     nstored = 0;            /* number of time steps in matrix */
316          int     last_monthly = 0;       /* month of last report */
317          int     mo, da;                 /* month (1-12) and day (1-31) */
318          double  hr;                     /* hour (local standard time) */
# Line 320 | Line 332 | main(int argc, char *argv[])
332                  case 'v':                       /* verbose progress reports */
333                          verbose++;
334                          break;
335 +                case 'h':                       /* turn off header */
336 +                        doheader = 0;
337 +                        break;
338                  case 'o':                       /* output format */
339                          switch (argv[i][2]) {
340                          case 'f':
# Line 353 | Line 368 | main(int argc, char *argv[])
368                          skycolor[1] = atof(argv[++i]);
369                          skycolor[2] = atof(argv[++i]);
370                          break;
371 +                case 'n':                       /* no matrix output */
372 +                        avgSky = -1;
373 +                        rhsubdiv = 1;
374 +                        /* fall through */
375                  case 'd':                       /* solar (direct) only */
376                          skycolor[0] = skycolor[1] = skycolor[2] = 0;
377 <                        if (suncolor[1] <= 1e-4)
359 <                                suncolor[0] = suncolor[1] = suncolor[2] = 1;
377 >                        grefl[0] = grefl[1] = grefl[2] = 0;
378                          break;
379 +                case 'D':                       /* output suns to file */
380 +                        sunsfp = fopen(argv[++i], "w");
381 +                        if (!sunsfp) {
382 +                                fprintf(stderr, "%s: cannot open '%s' for output\n",
383 +                                                argv[0], argv[i]);
384 +                                exit(1);
385 +                        }
386 +                        fixed_sun_sa = PI/360.*0.533;
387 +                        fixed_sun_sa *= PI*fixed_sun_sa;
388 +                        break;
389                  case 's':                       /* sky only (no direct) */
390                          suncolor[0] = suncolor[1] = suncolor[2] = 0;
363                        if (skycolor[1] <= 1e-4)
364                                skycolor[0] = skycolor[1] = skycolor[2] = 1;
391                          break;
392                  case 'r':                       /* rotate distribution */
393                          if (argv[i][2] && argv[i][2] != 'z')
# Line 370 | Line 396 | main(int argc, char *argv[])
396                          break;
397                  case '5':                       /* 5-phase calculation */
398                          nsuns = 1;
399 <                        fixed_sun_sa = 6.797e-05;
399 >                        fixed_sun_sa = PI/360.*atof(argv[++i]);
400 >                        if (fixed_sun_sa <= 0) {
401 >                                fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n",
402 >                                                argv[0]);
403 >                                exit(1);
404 >                        }
405 >                        fixed_sun_sa *= fixed_sun_sa*PI;
406                          break;
407 +                case 'A':                       /* compute average sky */
408 +                        avgSky = 1;
409 +                        break;
410                  default:
411                          goto userr;
412                  }
# Line 434 | Line 469 | main(int argc, char *argv[])
469          s_latitude = DegToRad(s_latitude);
470          s_longitude = DegToRad(s_longitude);
471          s_meridian = DegToRad(s_meridian);
472 +                                        /* initial allocation */
473 +        mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch);
474                                          /* process each time step in tape */
475          while (scanf("%d %d %lf %lf %lf\n", &mo, &da, &hr, &dir, &dif) == 5) {
476                  double          sda, sta;
477 <                                        /* make space for next time step */
478 <                mtx_offset = 3*nskypatch*ntsteps++;
479 <                mtx_data = resize_dmatrix(mtx_data, ntsteps, nskypatch);
477 >
478 >                mtx_offset = 3*nskypatch*nstored;
479 >                nstored += !avgSky | !nstored;
480 >                                        /* make space for next row */
481 >                if (nstored > tstorage) {
482 >                        tstorage += (tstorage>>1) + nstored + 7;
483 >                        mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch);
484 >                }
485 >                ntsteps++;              /* keep count of time steps */
486                  if (dif <= 1e-4) {
487 <                        memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
487 >                        if (!avgSky | !mtx_offset)
488 >                                memset(mtx_data+mtx_offset, 0, sizeof(float)*3*nskypatch);
489                          continue;
490                  }
447                if (verbose && mo != last_monthly)
448                        fprintf(stderr, "%s: stepping through month %d...\n",
449                                                progname, last_monthly=mo);
491                                          /* compute solar position */
492                  julian_date = jdate(mo, da);
493                  sda = sdec(julian_date);
# Line 465 | Line 506 | main(int argc, char *argv[])
506                  }
507                                          /* compute sky patch values */
508                  ComputeSky(mtx_data+mtx_offset);
509 +                                        /* output sun if indicated */
510 +                if (sunsfp && (altitude > 0) & (dir_illum > 1e-4)) {
511 +                        double  srad = dir_illum/(WHTEFFICACY * fixed_sun_sa);
512 +                        FVECT   sv;
513 +                        vector(sv, altitude, azimuth);
514 +                        fprintf(sunsfp, "\nvoid light solar%d\n0\n0\n", ntsteps);
515 +                        fprintf(sunsfp, "3 %.3e %.3e %.3e\n", srad*suncolor[0],
516 +                                        srad*suncolor[1], srad*suncolor[2]);
517 +                        fprintf(sunsfp, "\nsolar%d source sun%d\n0\n0\n", ntsteps, ntsteps);
518 +                        fprintf(sunsfp, "4 %.6f %.6f %.6f 0.533\n", sv[0], sv[1], sv[2]);
519 +                }
520 +                if (avgSky < 0)         /* no matrix? */
521 +                        continue;
522 +
523                  AddDirect(mtx_data+mtx_offset);
524 +                                        /* update cumulative sky? */
525 +                for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
526 +                        mtx_data[i] += mtx_data[mtx_offset+i];
527 +                                        /* monthly reporting */
528 +                if (verbose && mo != last_monthly)
529 +                        fprintf(stderr, "%s: stepping through month %d...\n",
530 +                                                progname, last_monthly=mo);
531          }
532 +        if (!ntsteps) {
533 +                fprintf(stderr, "%s: no valid time steps on input\n", progname);
534 +                exit(1);
535 +        }
536                                          /* check for junk at end */
537          while ((i = fgetc(stdin)) != EOF)
538                  if (!isspace(i)) {
# Line 477 | Line 543 | main(int argc, char *argv[])
543                          fputs(buf, stderr); fputc('\n', stderr);
544                          break;
545                  }
546 +
547 +        if (avgSky < 0)                 /* no matrix output? */
548 +                goto alldone;
549 +
550 +        dif = 1./(double)ntsteps;       /* average sky? */
551 +        for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; )
552 +                mtx_data[i] *= dif;
553                                          /* write out matrix */
554 +        if (outfmt != 'a')
555 +                SET_FILE_BINARY(stdout);
556   #ifdef getc_unlocked
557          flockfile(stdout);
558   #endif
559          if (verbose)
560                  fprintf(stderr, "%s: writing %smatrix with %d time steps...\n",
561 <                                progname, outfmt=='a' ? "" : "binary ", ntsteps);
561 >                                progname, outfmt=='a' ? "" : "binary ", nstored);
562 >        if (doheader) {
563 >                newheader("RADIANCE", stdout);
564 >                printargs(argc, argv, stdout);
565 >                printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude),
566 >                                        -RadToDeg(s_longitude));
567 >                printf("NROWS=%d\n", nskypatch);
568 >                printf("NCOLS=%d\n", nstored);
569 >                printf("NCOMP=3\n");
570 >                if ((outfmt == 'f') | (outfmt == 'd'))
571 >                        fputendian(stdout);
572 >                fputformat((char *)getfmtname(outfmt), stdout);
573 >                putchar('\n');
574 >        }
575                                          /* patches are rows (outer sort) */
576          for (i = 0; i < nskypatch; i++) {
577                  mtx_offset = 3*i;
578                  switch (outfmt) {
579                  case 'a':
580 <                        for (j = 0; j < ntsteps; j++) {
580 >                        for (j = 0; j < nstored; j++) {
581                                  printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset],
582                                                  mtx_data[mtx_offset+1],
583                                                  mtx_data[mtx_offset+2]);
584                                  mtx_offset += 3*nskypatch;
585                          }
586 <                        if (ntsteps > 1)
586 >                        if (nstored > 1)
587                                  fputc('\n', stdout);
588                          break;
589                  case 'f':
590 <                        for (j = 0; j < ntsteps; j++) {
591 <                                fwrite(mtx_data+mtx_offset, sizeof(float), 3,
590 >                        for (j = 0; j < nstored; j++) {
591 >                                putbinary(mtx_data+mtx_offset, sizeof(float), 3,
592                                                  stdout);
593                                  mtx_offset += 3*nskypatch;
594                          }
595                          break;
596                  case 'd':
597 <                        for (j = 0; j < ntsteps; j++) {
597 >                        for (j = 0; j < nstored; j++) {
598                                  double  ment[3];
599                                  ment[0] = mtx_data[mtx_offset];
600                                  ment[1] = mtx_data[mtx_offset+1];
601                                  ment[2] = mtx_data[mtx_offset+2];
602 <                                fwrite(ment, sizeof(double), 3, stdout);
602 >                                putbinary(ment, sizeof(double), 3, stdout);
603                                  mtx_offset += 3*nskypatch;
604                          }
605                          break;
# Line 519 | Line 607 | main(int argc, char *argv[])
607                  if (ferror(stdout))
608                          goto writerr;
609          }
610 <        if (fflush(stdout) == EOF)
610 > alldone:
611 >        if (fflush(NULL) == EOF)
612                  goto writerr;
613          if (verbose)
614                  fprintf(stderr, "%s: done.\n", progname);
615          exit(0);
616   userr:
617 <        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",
617 >        fprintf(stderr, "Usage: %s [-v][-h][-A][-d|-s|-n][-D file][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n",
618                          progname);
619          exit(1);
620   fmterr:
621 <        fprintf(stderr, "%s: input weather tape format error\n", progname);
621 >        fprintf(stderr, "%s: weather tape format error in header\n", progname);
622          exit(1);
623   writerr:
624          fprintf(stderr, "%s: write error on output\n", progname);
# Line 601 | Line 690 | ComputeSky(float *parr)
690                  diff_illum = diff_irrad * WHTEFFICACY;
691                  dir_illum = dir_irrad * WHTEFFICACY;
692          }
604
605        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
606                memset(parr, 0, sizeof(float)*3*nskypatch);
607                return;
608        }
693          /* Compute ground radiance (include solar contribution if any) */
694          parr[0] = diff_illum;
695          if (altitude > 0)
# Line 613 | Line 697 | ComputeSky(float *parr)
697          parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY);
698          multcolor(parr, grefl);
699  
700 +        if (bright(skycolor) <= 1e-4) {                 /* 0 sky component? */
701 +                memset(parr+3, 0, sizeof(float)*3*(nskypatch-1));
702 +                return;
703 +        }
704          /* Calculate Perez sky model parameters */
705          CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index);
706  
# Line 622 | Line 710 | ComputeSky(float *parr)
710          /* Calculate relative horizontal illuminance */
711          norm_diff_illum = CalcRelHorzIllum(parr);
712  
713 +        /* Check for zero sky -- make uniform in that case */
714 +        if (norm_diff_illum <= FTINY) {
715 +                for (i = 1; i < nskypatch; i++)
716 +                        setcolor(parr+3*i, 1., 1., 1.);
717 +                norm_diff_illum = PI;
718 +        }
719          /* Normalization coefficient */
720          norm_diff_illum = diff_illum / norm_diff_illum;
721  
# Line 891 | Line 985 | int CalcSkyParamFromIllum()
985                  /* Convert illuminance to irradiance */
986                  index = GetCategoryIndex();
987                  diff_irrad = diff_illum / CalcDiffuseIllumRatio(index);
988 <                dir_irrad = dir_illum / CalcDirectIllumRatio(index);
988 >                dir_irrad = CalcDirectIllumRatio(index);
989 >                if (dir_irrad > 0.1)
990 >                        dir_irrad = dir_illum / dir_irrad;
991          
992                  /* Calculate sky brightness and clearness */
993                  sky_brightness = CalcSkyBrightness();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines