ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/wrapBSDF.c
(Generate patch)

Comparing ray/src/util/wrapBSDF.c (file contents):
Revision 2.1 by greg, Wed Feb 11 18:08:10 2015 UTC vs.
Revision 2.17 by greg, Tue Feb 2 18:02:32 2016 UTC

# Line 2 | Line 2
2   static const char RCSid[] = "$Id$";
3   #endif
4   /*
5 < * Wrap BSDF data in valid WINDOW XML wrapper
5 > * Wrap BSDF data in valid WINDOW XML file
6   *
7 < *      G. Ward         January 2015
7 > *      G. Ward         February 2015
8   */
9  
10   #include <ctype.h>
11   #include "rtio.h"
12 < #include "rtprocess.h"
12 > #include "paths.h"
13   #include "ezxml.h"
14   #include "bsdf.h"
15   #include "bsdf_m.h"
16                                          /* XML template file names */
17   const char      def_template[] = "minimalBSDFt.xml";
18 < const char      win6_template[] = "window6BSDFt.xml";
18 > const char      win6_template[] = "WINDOW6BSDFt.xml";
19  
20   const char      stdin_name[] = "<stdin>";
21
21                                          /* input files (can be stdin_name) */
22   const char      *xml_input = NULL;
23                                          /* unit for materials & geometry */
24 < const char      *attr_unit = "millimeter";
24 > const char      *attr_unit = "meter";
25   const char      legal_units[] = "meter|foot|inch|centimeter|millimeter";
26                                          /* system materials & geometry */
27   const char      *mgf_geometry = NULL;
28 <
29 <                                        /* angle basis */
28 >                                        /* comment list */
29 > #define MAXCOMM         30
30 > const char      *commlist[MAXCOMM];
31 > int             ncomm = 0;
32 >                                        /* angle bases */
33   enum { ABdefault=-1, ABklemsFull=0, ABklemsHalf, ABklemsQuarter,
34                  ABtensorTree3, ABtensorTree4, ABend };
35  
36   int             angle_basis = ABdefault;
37  
38 + int             correct_solid_angle = 0;
39 +
40 + const char      *klems_basis_name[] = {
41 +        "LBNL/Klems Full",
42 +        "LBNL/Klems Half",
43 +        "LBNL/Klems Quarter",
44 + };
45                                          /* field IDs and nicknames */
46   struct s_fieldID {
47          char            nickName[4];
48 <        int             has_unit;
48 >        short           has_unit;
49 >        short           win_need;
50          const char      *fullName;
51   } XMLfieldID[] = {
52 <        {"m", 0, "Manufacturer"},
53 <        {"n", 0, "Name"},
54 <        {"c", 0, "ThermalConductivity"},
55 <        {"ef", 0, "EmissivityFront"},
56 <        {"eb", 0, "EmissivityBack"},
57 <        {"tir", 0, "TIR"},
58 <        {"eo", 0, "EffectiveOpennessFraction"},
59 <        {"t", 1, "Thickness"},
60 <        {"h", 1, "Height"},
61 <        {"w", 1, "Width"},
62 <        {"\0", -1, NULL}        /* terminator */
52 >        {"m", 0, 1, "Manufacturer"},
53 >        {"n", 0, 1, "Name"},
54 >        {"c", 0, 0, "ThermalConductivity"},
55 >        {"ef", 0, 0, "EmissivityFront"},
56 >        {"eb", 0, 0, "EmissivityBack"},
57 >        {"tir", 0, 0, "TIR"},
58 >        {"eo", 0, 0, "EffectiveOpennessFraction"},
59 >        {"t", 1, 1, "Thickness"},
60 >        {"h", 1, 0, "Height"},
61 >        {"w", 1, 0, "Width"},
62 >        {"\0", 0, 0, NULL}      /* terminator */
63   };
64                                          /* field assignments */
65   #define MAXASSIGN       12
66   const char      *field_assignment[MAXASSIGN];
67   int             nfield_assign = 0;
68   #define FASEP   ';'
59
69                                          /* data file(s) & spectra */
70   enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward };
71  
72 < enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 };
72 > enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5,
73 >        DSuprime=-6, DSvprime=-7 };
74  
75   #define MAXFILES        20
76  
# Line 76 | Line 86 | const char     *spectr_file[MAXFILES]; /* custom spectral
86  
87   const char      top_level_name[] = "WindowElement";
88  
89 < static char     *basis_definition[] = {
89 > static char     basis_definition[][256] = {
90  
91 +        "\t<DataDefinition>\n"
92          "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
93          "\t\t<AngleBasis>\n"
94          "\t\t\t<AngleBasisName>LBNL/Klems Full</AngleBasisName>\n"
95 <        "\t\t</AngleBasis>\n",
95 >        "\t\t\t</AngleBasis>\n"
96 >        "\t</DataDefinition>\n",
97  
98 +        "\t<DataDefinition>\n"
99          "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
100          "\t\t<AngleBasis>\n"
101          "\t\t\t<AngleBasisName>LBNL/Klems Half</AngleBasisName>\n"
102 <        "\t\t</AngleBasis>\n",
102 >        "\t\t\t</AngleBasis>\n"
103 >        "\t</DataDefinition>\n",
104  
105 +        "\t<DataDefinition>\n"
106          "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
107          "\t\t<AngleBasis>\n"
108          "\t\t\t<AngleBasisName>LBNL/Klems Quarter</AngleBasisName>\n"
109 <        "\t\t</AngleBasis>\n",
109 >        "\t\t\t</AngleBasis>\n"
110 >        "\t</DataDefinition>\n",
111  
112 <        "\t\t<IncidentDataStructure>TensorTree3</IncidentDataStructure>\n",
112 >        "\t<DataDefinition>\n"
113 >        "\t\t<IncidentDataStructure>TensorTree3</IncidentDataStructure>\n"
114 >        "\t</DataDefinition>\n",
115  
116 <        "\t\t<IncidentDataStructure>TensorTree4</IncidentDataStructure>\n",
116 >        "\t<DataDefinition>\n"
117 >        "\t\t<IncidentDataStructure>TensorTree4</IncidentDataStructure>\n"
118 >        "\t</DataDefinition>\n",
119   };
120  
121   /* Copy data from file descriptor to stdout and close */
# Line 130 | Line 150 | input2str(const char *inpspec)
150                  return "";
151          if (inpspec == stdin_name) {            /* read from stdin */
152                  fp = stdin;
153 <        } else if (*inpspec == '!') {           /* read from command */
153 >        } else if (inpspec[0] == '!') {         /* read from command */
154                  fp = popen(inpspec+1, "r");
155                  if (fp == NULL) {
156                          fprintf(stderr, "Cannot start process '%s'\n",
157 <                                        inpspec+1);
157 >                                        inpspec);
158                          return "";
159                  }
160          } else {                                /* else load file */
# Line 143 | Line 163 | input2str(const char *inpspec)
163                          fprintf(stderr, "%s: cannot open\n", inpspec);
164                          return "";
165                  }
166 + #ifndef _WIN32                          /* XXX somehow broken on Windows */
167                  len = lseek(fd, 0L, SEEK_END);
168 <                if (len < 0) {
169 <                        fprintf(stderr, "%s: seek error\n", inpspec);
168 >                if (len > 0) {
169 >                        lseek(fd, 0L, SEEK_SET);
170 >                        str = (char *)malloc(len+1);
171 >                        if (str == NULL) {
172 >                                close(fd);
173 >                                goto memerr;
174 >                        }
175 >                        if (read(fd, str, len) != len) {
176 >                                fprintf(stderr, "%s: read error\n", inpspec);
177 >                                free(str);
178 >                                close(fd);
179 >                                return "";
180 >                        }
181 >                        str[len] = '\0';
182                          close(fd);
183 <                        return "";
183 >                        return str;
184                  }
185 <                lseek(fd, 0L, SEEK_SET);
186 <                str = (char *)malloc(len+1);
154 <                if (str == NULL) {
155 <                        close(fd);
156 <                        goto memerr;
157 <                }
158 <                if (read(fd, str, len) != len) {
159 <                        fprintf(stderr, "%s: read error\n", inpspec);
160 <                        free(str);
161 <                        close(fd);
162 <                        return "";
163 <                }
164 <                str[len] = '\0';
165 <                close(fd);
166 <                return str;
185 > #endif
186 >                fp = fdopen(fd, "r");           /* not a regular file */
187          }
188                                                  /* reading from stream */
189          str = (char *)malloc((len=8192)+1);
# Line 186 | Line 206 | input2str(const char *inpspec)
206                  if (str == NULL)
207                          goto memerr;
208          }
209 <        if (*inpspec != '!')
209 >        if (inpspec[0] != '!')
210                  fclose(fp);
211          else if (pclose(fp))
212 <                fprintf(stderr, "Error running command '%s'\n", inpspec+1);
212 >                fprintf(stderr, "Error running command '%s'\n", inpspec);
213          return str;
214   memerr:
215          fprintf(stderr, "%s: error allocating memory\n", inpspec);
216          if (fp != NULL)
217 <                (*inpspec == '!') ? pclose(fp) : fclose(fp);
217 >                (inpspec[0] == '!') ? pclose(fp) : fclose(fp);
218          return "";
219   }
220  
# Line 204 | Line 224 | mat_assignments(const char *caller, const char *fn, ez
224   {
225          int     i;
226  
207        if (!nfield_assign)
208                return 1;
227          wtl = ezxml_child(wtl, "Material");
228          if (wtl == NULL) {
229                  fprintf(stderr, "%s: missing <Material> tag\n", fn);
# Line 214 | Line 232 | mat_assignments(const char *caller, const char *fn, ez
232          for (i = 0; i < nfield_assign; i++) {
233                  const char      *fnext = field_assignment[i];
234                  for ( ; ; ) {
235 +                        int     added = 0;
236                          ezxml_t fld;
237                          char    sbuf[512];
238                          int     j;
# Line 222 | Line 241 | mat_assignments(const char *caller, const char *fn, ez
241                                  ++fnext;
242                          if (!*fnext)
243                                  break;
244 <                        for (j = 0; (*fnext != '=') & !isspace(*fnext); ) {
244 >                        for (j = 0; *fnext != '=' && !isspace(*fnext); ) {
245                                  if (!*fnext | (*fnext == FASEP) |
246                                                  (j >= sizeof(sbuf)-1)) {
247                                          fprintf(stderr,
# Line 232 | Line 251 | mat_assignments(const char *caller, const char *fn, ez
251                                  }
252                                  sbuf[j++] = *fnext++;
253                          }
254 <                        sbuf[j] = '\0';         /* check nick-names */
254 >                        sbuf[j] = '\0';         /* check known field */
255                          for (j = 0; XMLfieldID[j].nickName[0]; j++)
256 <                                if (!strcasecmp(sbuf, XMLfieldID[j].nickName)) {
256 >                                if (!strcasecmp(sbuf, XMLfieldID[j].nickName) ||
257 >                                        !strcasecmp(sbuf, XMLfieldID[j].fullName)) {
258                                          strcpy(sbuf, XMLfieldID[j].fullName);
259                                          break;
260                                  }
261                                                  /* check if tag exists */
262                          fld = ezxml_child(wtl, sbuf);
263                          if (fld == NULL) {      /* otherwise, create one */
264 <                                fprintf(stderr, "%s: warning - adding tag <%s>\n",
265 <                                        fn, sbuf);
264 >                                if (!XMLfieldID[j].nickName[0])
265 >                                        fprintf(stderr,
266 >                                                "%s: warning - adding tag <%s>\n",
267 >                                                        fn, sbuf);
268 >                                ezxml_add_txt(wtl, "\t");
269                                  fld = ezxml_add_child_d(wtl, sbuf, strlen(wtl->txt));
270 +                                ++added;
271                          }
272                          if (XMLfieldID[j].has_unit)
273                                  ezxml_set_attr(fld, "unit", attr_unit);
274 +                        XMLfieldID[j].win_need = 0;
275                          while (isspace(*fnext))
276                                  ++fnext;
277                          if (*fnext++ != '=') {
# Line 255 | Line 280 | mat_assignments(const char *caller, const char *fn, ez
280                                                  caller, field_assignment[i]);
281                                  return 0;
282                          }
283 <                        for (j = 0; *fnext & (*fnext != FASEP); ) {
283 >                        for (j = 0; *fnext && *fnext != FASEP; ) {
284                                  if (j >= sizeof(sbuf)-1) {
285                                          fprintf(stderr,
286                                          "%s: field too long in '%s'\n",
# Line 266 | Line 291 | mat_assignments(const char *caller, const char *fn, ez
291                          }
292                          sbuf[j] = '\0';
293                          ezxml_set_txt_d(fld, sbuf);
294 +                        if (added)
295 +                                ezxml_add_txt(wtl, "\n\t");
296                          fnext += (*fnext == FASEP);
297                  }
298          }
299 <        return 1;               /* no errors */
299 >                                        /* check required WINDOW settings */
300 >        if (xml_input == win6_template)
301 >            for (i = 0; XMLfieldID[i].nickName[0]; i++)
302 >                if (XMLfieldID[i].win_need &&
303 >                        !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0])
304 >                        fprintf(stderr,
305 >                        "%s: warning - missing '%s' assignment for WINDOW <%s>\n",
306 >                                        caller, XMLfieldID[i].nickName,
307 >                                        XMLfieldID[i].fullName);
308 >        return 1;
309   }
310  
311   /* Complete angle basis specification */
# Line 297 | Line 333 | finish_angle_basis(ezxml_t ab)
333                  sprintf(buf, "%g", i ?
334                  .5*(abase_list[n].lat[i].tmin + abase_list[n].lat[i+1].tmin) :
335                                  .0);
336 +                ezxml_add_txt(abb, "\n\t\t\t\t");
337                  ezxml_set_txt_d(ezxml_add_child(abb,"Theta",strlen(abb->txt)), buf);
338                  sprintf(buf, "%d", abase_list[n].lat[i].nphis);
339 +                ezxml_add_txt(abb, "\n\t\t\t\t");
340                  ezxml_set_txt_d(ezxml_add_child(abb,"nPhis",strlen(abb->txt)), buf);
341 +                ezxml_add_txt(abb, "\n\t\t\t\t");
342                  tb = ezxml_add_child(abb, "ThetaBounds", strlen(abb->txt));
343 +                ezxml_add_txt(tb, "\n\t\t\t\t\t");
344                  sprintf(buf, "%g", abase_list[n].lat[i].tmin);
345 <                ezxml_set_txt(ezxml_add_child(tb,"LowerTheta",strlen(tb->txt)), buf);
345 >                ezxml_set_txt_d(ezxml_add_child(tb,"LowerTheta",strlen(tb->txt)), buf);
346 >                ezxml_add_txt(tb, "\n\t\t\t\t\t");
347                  sprintf(buf, "%g", abase_list[n].lat[i+1].tmin);
348 <                ezxml_set_txt(ezxml_add_child(tb,"UpperTheta",strlen(tb->txt)), buf);
348 >                ezxml_set_txt_d(ezxml_add_child(tb,"UpperTheta",strlen(tb->txt)), buf);
349 >                ezxml_add_txt(tb, "\n\t\t\t\t");
350 >                ezxml_add_txt(abb, "\n\t\t\t");
351 >                ezxml_add_txt(ab, "\n\t\t\t");
352          }
353          return 1;
354   }
# Line 320 | Line 364 | determine_angle_basis(const char *fn, ezxml_t wtl)
364          if (wtl == NULL)
365                  return -1;
366          ids = ezxml_txt(ezxml_child(wtl, "IncidentDataStructure"));
367 <        if (ids == NULL)
367 >        if (!ids[0])
368                  return -1;
369          for (i = 0; i < ABend; i++) {
370                  ezxml_t parsed = ezxml_parse_str(basis_definition[i],
# Line 343 | Line 387 | determine_angle_basis(const char *fn, ezxml_t wtl)
387          return -1;
388   }
389  
390 + /* Filter Klems angle basis, factoring out incident projected solid angle */
391 + static int
392 + filter_klems_matrix(FILE *fp)
393 + {
394 + #define MAX_COLUMNS     145
395 +        const char      *bn = klems_basis_name[angle_basis];
396 +        float           col_corr[MAX_COLUMNS];
397 +        int             i, j, n = nabases;
398 +                                        /* get angle basis */
399 +        while (n-- > 0)
400 +                if (!strcasecmp(bn, abase_list[n].name))
401 +                        break;
402 +        if (n < 0)
403 +                return 0;
404 +        if (abase_list[n].nangles > MAX_COLUMNS) {
405 +                fputs("Internal error - too many Klems columns!\n", stderr);
406 +                return 0;
407 +        }
408 +                                        /* get correction factors */
409 +        for (j = abase_list[n].nangles; j--; )
410 +                col_corr[j] = 1.f / io_getohm(j, &abase_list[n]);
411 +                                        /* read/correct/write matrix */
412 +        for (i = 0; i < abase_list[n].nangles; i++) {
413 +            for (j = 0; j < abase_list[n].nangles; j++) {
414 +                double  d;
415 +                if (fscanf(fp, "%lf", &d) != 1)
416 +                        return 0;
417 +                if (d < -1e-3) {
418 +                        fputs("Negative BSDF data!\n", stderr);
419 +                        return 0;
420 +                }
421 +                printf(" %.3e", d*col_corr[j]*(d > 0));
422 +            }
423 +            fputc('\n', stdout);
424 +        }
425 +        while ((i = getc(fp)) != EOF)
426 +                if (!isspace(i)) {
427 +                        fputs("Unexpected data past EOF\n", stderr);
428 +                        return 0;
429 +                }
430 +        return 1;                       /* all is good */
431 + #undef MAX_COLUMNS
432 + }
433 +
434   /* Write out BSDF data block with surrounding tags */
435   static int
436   writeBSDFblock(const char *caller, struct s_dfile *df)
437   {
438 +        int     correct_klems = correct_solid_angle;
439          char    *cp;
440  
441          puts("\t<WavelengthData>");
# Line 354 | Line 443 | writeBSDFblock(const char *caller, struct s_dfile *df)
443          switch (df->spectrum) {
444          case DSvisible:
445                  puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
446 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
446 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
447                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
448                  break;
449          case DSxbar31:
450                  puts("\t\t<Wavelength unit=\"Integral\">CIE-X</Wavelength>");
451 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
451 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
452                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 X.dsp</DetectorSpectrum>");
453                  break;
454          case DSzbar31:
455                  puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</Wavelength>");
456 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
456 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
457                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 Z.dsp</DetectorSpectrum>");
458                  break;
459 +        case DSuprime:
460 +                puts("\t\t<Wavelength unit=\"Integral\">CIE-u</Wavelength>");
461 +                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
462 +                break;
463 +        case DSvprime:
464 +                puts("\t\t<Wavelength unit=\"Integral\">CIE-v</Wavelength>");
465 +                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
466 +                break;
467          case DSsolar:
468                  puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>");
469 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
469 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
470                  puts("\t\t<DetectorSpectrum>None</DetectorSpectrum>");
471                  break;
472          case DSnir:
473                  puts("\t\t<Wavelength unit=\"Integral\">NIR</Wavelength>");
474 <                puts("\t\tSourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
474 >                puts("\t\t<SourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
475                  puts("\t\t<DetectorSpectrum>PLACE_HOLDER</DetectorSpectrum>");
476                  break;
477          default:
# Line 385 | Line 482 | writeBSDFblock(const char *caller, struct s_dfile *df)
482                                  spectr_file[df->spectrum]);
483                  if (cp != NULL)
484                          *cp = '.';
485 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
485 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
486                  printf("\t\t<DetectorSpectrum>%s</DetectorSpectrum>\n",
487                                  spectr_file[df->spectrum]);
488                  break;
# Line 412 | Line 509 | writeBSDFblock(const char *caller, struct s_dfile *df)
509          puts("</WavelengthDataDirection>");
510          switch (angle_basis) {
511          case ABklemsFull:
415                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>");
416                break;
512          case ABklemsHalf:
418                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Half</ColumnAngleBasis>");
419                break;
513          case ABklemsQuarter:
514 <                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Quarter</ColumnAngleBasis>");
514 >                fputs("\t\t\t<ColumnAngleBasis>", stdout);
515 >                fputs(klems_basis_name[angle_basis], stdout);
516 >                puts("</ColumnAngleBasis>");
517 >                fputs("\t\t\t<RowAngleBasis>", stdout);
518 >                fputs(klems_basis_name[angle_basis], stdout);
519 >                puts("</RowAngleBasis>");
520                  break;
521          case ABtensorTree3:
522          case ABtensorTree4:
523                  puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
524 +                correct_klems = 0;
525                  break;
526          default:
527                  fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis);
# Line 431 | Line 530 | writeBSDFblock(const char *caller, struct s_dfile *df)
530          puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
531          puts("\t\t\t<ScatteringData>");
532          fflush(stdout);
533 <        if (df->fname == stdin_name) {
533 >        if (correct_klems) {                    /* correct Klems matrix data */
534 >                FILE    *fp = stdin;
535 >                if (df->fname[0] == '!')
536 >                        fp = popen(df->fname+1, "r");
537 >                else if (df->fname != stdin_name)
538 >                        fp = fopen(df->fname, "r");
539 >                if (fp == NULL) {
540 >                        fprintf(stderr, "%s: cannot open '%s'\n",
541 >                                        caller, df->fname);
542 >                        return 0;
543 >                }
544 >                if (!filter_klems_matrix(fp)) {
545 >                        fprintf(stderr, "%s: Klems data error from '%s'\n",
546 >                                        caller, df->fname);
547 >                        return 0;
548 >                }
549 >                if (df->fname[0] != '!') {
550 >                        fclose(fp);
551 >                } else if (pclose(fp)) {
552 >                        fprintf(stderr, "%s: error running '%s'\n",
553 >                                        caller, df->fname);
554 >                        return 0;
555 >                }
556 >        } else if (df->fname == stdin_name) {
557                  copy_and_close(fileno(stdin));
558 <        } else if (*df->fname != '!') {
558 >        } else if (df->fname[0] != '!') {
559                  if (!copy_and_close(open(df->fname, O_RDONLY))) {
560                          fprintf(stderr, "%s: error reading from '%s'\n",
561                                          caller, df->fname);
562                          return 0;
563                  }
564          } else if (system(df->fname+1)) {
565 <                fprintf(stderr, "%s: error running '%s'\n", caller, df->fname+1);
565 >                fprintf(stderr, "%s: error running '%s'\n", caller, df->fname);
566                  return 0;
567          }
568          puts("\t\t\t</ScatteringData>");
# Line 453 | Line 575 | writeBSDFblock(const char *caller, struct s_dfile *df)
575   static int
576   writeBSDF(const char *caller, ezxml_t fl)
577   {
578 <        char    *xml = ezxml_toxml(fl);
578 >        char    *xml = ezxml_toxml(fl);         /* store XML in string */
579          int     ei, i;
580                                                  /* locate trailer */
581          for (ei = strlen(xml)-strlen("</Layer></Optical></WindowElement>");
# Line 466 | Line 588 | writeBSDF(const char *caller, ezxml_t fl)
588                  free(xml);
589                  return 0;
590          }
591 +        puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
592 +        for (i = 0; i < ncomm; i++)
593 +                printf("<!-- %s -->\n", commlist[i]);
594          fflush(stdout);                         /* write previous XML info. */
595          if (write(fileno(stdout), xml, ei) != ei) {
596                  free(xml);
# Line 477 | Line 602 | writeBSDF(const char *caller, ezxml_t fl)
602                          return 0;
603                  }
604          fputs(xml+ei, stdout);                  /* write trailer */
605 <        free(xml);
605 >        free(xml);                              /* free string */
606 >        fputc('\n', stdout);
607          return (fflush(stdout) == 0);
608   }
609  
# Line 485 | Line 611 | writeBSDF(const char *caller, ezxml_t fl)
611   static int
612   wrapBSDF(const char *caller)
613   {
614 <        const char      *xml_path = getpath((char *)xml_input, getrlibpath(), R_OK);
614 >        const char      *xml_path = xml_input;
615          ezxml_t         fl, wtl;
616                                          /* load previous XML/template */
617 <        if (xml_path == NULL) {
618 <                fprintf(stderr, "%s: cannot find XML file named '%s'\n",
619 <                                caller, xml_input==NULL ? "NULL" : xml_input);
620 <                return 0;
617 >        if (xml_input == stdin_name) {
618 >                fl = ezxml_parse_fp(stdin);
619 >        } else if (xml_input[0] == '!') {
620 >                FILE    *pfp = popen(xml_input+1, "r");
621 >                if (pfp == NULL) {
622 >                        fprintf(stderr, "%s: cannot start process '%s'\n",
623 >                                        caller, xml_input);
624 >                        return 0;
625 >                }
626 >                fl = ezxml_parse_fp(pfp);
627 >                if (pclose(pfp)) {
628 >                        fprintf(stderr, "%s: error running '%s'\n",
629 >                                        caller, xml_input);
630 >                        return 0;
631 >                }
632 >        } else {
633 >                xml_path = getpath((char *)xml_input, getrlibpath(), R_OK);
634 >                if (xml_path == NULL) {
635 >                        fprintf(stderr, "%s: cannot find XML file named '%s'\n",
636 >                                        caller, xml_input==NULL ? "NULL" : xml_input);
637 >                        return 0;
638 >                }
639 >                fl = ezxml_parse_file(xml_path);
640          }
496        fl = ezxml_parse_file(xml_path);
641          if (fl == NULL) {
642 <                fprintf(stderr, "%s: cannot open XML path '%s'\n",
499 <                                caller, xml_path);
642 >                fprintf(stderr, "%s: cannot load XML '%s'\n", caller, xml_path);
643                  return 0;
644          }
645          if (ezxml_error(fl)[0]) {
646 <                fprintf(stderr, "%s: error in XML %s: %s\n", caller, xml_path,
646 >                fprintf(stderr, "%s: error in XML '%s': %s\n", caller, xml_path,
647                                  ezxml_error(fl));
648                  goto failure;
649          }
# Line 522 | Line 665 | wrapBSDF(const char *caller)
665                  goto failure;
666          }
667                                          /* make material assignments */
668 <        if (!mat_assignments(caller, xml_path, wtl)) {
668 >        if (!mat_assignments(caller, xml_path, wtl))
669                  goto failure;
527        }
670          if (mgf_geometry != NULL) {     /* add geometry if specified */
671                  ezxml_t geom = ezxml_child(wtl, "Geometry");
672                  if (geom == NULL)
673 <                        geom = ezxml_add_child(wtl, "Geometry", 0);
673 >                        geom = ezxml_add_child(wtl, "Geometry", strlen(wtl->txt));
674                  ezxml_set_attr(geom, "format", "MGF");
675                  geom = ezxml_child(geom, "MGFblock");
676                  if (geom == NULL) {
677                          geom = ezxml_child(wtl, "Geometry");
678 <                        geom = ezxml_add_child(geom, "MGFblock", strlen(geom->txt));
678 >                        geom = ezxml_add_child(geom, "MGFblock", 0);
679                  }
680                  ezxml_set_attr(geom, "unit", attr_unit);
681                  ezxml_set_txt(geom, input2str(mgf_geometry));
# Line 542 | Line 684 | wrapBSDF(const char *caller)
684          }
685                                          /* check basis */
686          if (angle_basis != ABdefault) {
687 +                size_t  offset = 0;
688                  ezxml_t ab, dd = ezxml_child(wtl, "DataDefinition");
689 <                if (dd == NULL) {
690 <                        dd = ezxml_add_child(wtl, "DataDefinition", 0);
691 <                } else {
549 <                        if (ezxml_child(dd, "DataDefinition") != NULL)
689 >                if (dd != NULL) {
690 >                        offset = dd->off;
691 >                        if (dd->child != NULL)
692                                  fprintf(stderr,
693                  "%s: warning - replacing existing <DataDefinition> in '%s'\n",
694                                                  caller, xml_path);
695 <                        while (dd->child != NULL)
696 <                                ezxml_remove(dd->child);
697 <                }
698 <                ezxml_insert(ezxml_parse_str(basis_definition[angle_basis],
695 >                        ezxml_remove(dd);
696 >                } else
697 >                        offset = strlen(wtl->txt);
698 >                dd = ezxml_insert(ezxml_parse_str(basis_definition[angle_basis],
699                                          strlen(basis_definition[angle_basis])),
700 <                                dd, 0);
700 >                                wtl, offset);
701                  if ((ab = ezxml_child(dd, "AngleBasis")) != NULL &&
702                                  !finish_angle_basis(ab))
703                          goto failure;
# Line 580 | Line 722 | UsageExit(const char *pname)
722   {
723          fputs("Usage: ", stderr);
724          fputs(pname, stderr);
725 <        fputs(" [options] [input.xml]\n", stderr);
725 >        fputs(" [-W][-c][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr);
726 >        fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp][-C comm]", stderr);
727 >        fputs(" [input.xml]\n", stderr);
728          exit(1);
729   }
730  
# Line 617 | Line 761 | main(int argc, char *argv[])
761                                                  argv[0]);
762                                  return 1;
763                          }
764 <                        if (strstr(argv[i], legal_units) == NULL) {
765 <                                fprintf(stderr, "%s: unit must be one of (%s)\n",
764 >                        if (strstr(legal_units, argv[i]) == NULL) {
765 >                                fprintf(stderr, "%s: -u unit must be one of (%s)\n",
766                                                  argv[0], legal_units);
767                                  return 1;
768                          }
# Line 645 | Line 789 | main(int argc, char *argv[])
789                          else
790                                  UsageExit(argv[0]);
791                          continue;
792 +                case 'c':               /* correct solid angle */
793 +                        correct_solid_angle = 1;
794 +                        continue;
795 +                case 'C':               /* comment */
796 +                        if (ncomm >= MAXCOMM) {
797 +                                fprintf(stderr, "%s: too many comments\n",
798 +                                                argv[0]);
799 +                                return 1;
800 +                        }
801 +                        if (strchr(argv[++i], '>') != NULL) {
802 +                                fprintf(stderr, "%s: illegal character in comment\n",
803 +                                                argv[0]);
804 +                                return 1;
805 +                        }
806 +                        commlist[ncomm++] = argv[i];
807 +                        continue;
808                  case 't':               /* transmission */
809                          if (i >= argc-1)
810                                  UsageExit(argv[0]);
# Line 686 | Line 846 | main(int argc, char *argv[])
846                                  argv[i] = (char *)stdin_name;
847                          }
848                          data_file[ndataf].fname = argv[i];
849 <                        data_file[ndataf++].spectrum = cur_spectrum;
849 >                        data_file[ndataf].spectrum = cur_spectrum;
850 >                        ndataf++;
851                          continue;
852                  case 's':               /* spectrum name or input file */
853                          if (++i >= argc)
# Line 700 | Line 861 | main(int argc, char *argv[])
861                                  cur_spectrum = DSxbar31;
862                          else if (!strcasecmp(argv[i], "CIE-Z"))
863                                  cur_spectrum = DSzbar31;
864 +                        else if (!strcasecmp(argv[i], "CIE-u"))
865 +                                cur_spectrum = DSuprime;
866 +                        else if (!strcasecmp(argv[i], "CIE-v"))
867 +                                cur_spectrum = DSvprime;
868                          else if (!strcasecmp(argv[i], "NIR"))
869                                  cur_spectrum = DSnir;
870                          else {
# Line 748 | Line 913 | doneOptions:                                   /* get XML input */
913          } else {
914                  xml_input = argv[i];
915          }
751        if ((xml_input == win6_template) & (angle_basis == ABdefault))
752                angle_basis = ABklemsFull;
916                                                  /* wrap it! */
917          return !wrapBSDF(argv[0]);
918   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines