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.21 by greg, Fri Oct 14 00:54:22 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 "platform.h"
11   #include <ctype.h>
12   #include "rtio.h"
13 < #include "rtprocess.h"
13 > #include "paths.h"
14   #include "ezxml.h"
15   #include "bsdf.h"
16   #include "bsdf_m.h"
17                                          /* XML template file names */
18   const char      def_template[] = "minimalBSDFt.xml";
19 < const char      win6_template[] = "window6BSDFt.xml";
19 > const char      win6_template[] = "WINDOW6BSDFt.xml";
20  
21   const char      stdin_name[] = "<stdin>";
21
22                                          /* input files (can be stdin_name) */
23   const char      *xml_input = NULL;
24                                          /* unit for materials & geometry */
25 < const char      *attr_unit = "millimeter";
25 > const char      *attr_unit = "meter";
26   const char      legal_units[] = "meter|foot|inch|centimeter|millimeter";
27                                          /* system materials & geometry */
28   const char      *mgf_geometry = NULL;
29 <
30 <                                        /* angle basis */
29 >                                        /* comment list */
30 > #define MAXCOMM         30
31 > const char      *commlist[MAXCOMM];
32 > int             ncomm = 0;
33 >                                        /* angle bases */
34   enum { ABdefault=-1, ABklemsFull=0, ABklemsHalf, ABklemsQuarter,
35                  ABtensorTree3, ABtensorTree4, ABend };
36  
37   int             angle_basis = ABdefault;
38  
39 + int             correct_solid_angle = 0;
40 +
41 + const char      *klems_basis_name[] = {
42 +        "LBNL/Klems Full",
43 +        "LBNL/Klems Half",
44 +        "LBNL/Klems Quarter",
45 + };
46                                          /* field IDs and nicknames */
47   struct s_fieldID {
48          char            nickName[4];
49 <        int             has_unit;
49 >        short           has_unit;
50 >        short           win_need;
51          const char      *fullName;
52   } XMLfieldID[] = {
53 <        {"m", 0, "Manufacturer"},
54 <        {"n", 0, "Name"},
55 <        {"c", 0, "ThermalConductivity"},
56 <        {"ef", 0, "EmissivityFront"},
57 <        {"eb", 0, "EmissivityBack"},
58 <        {"tir", 0, "TIR"},
59 <        {"eo", 0, "EffectiveOpennessFraction"},
60 <        {"t", 1, "Thickness"},
61 <        {"h", 1, "Height"},
62 <        {"w", 1, "Width"},
63 <        {"\0", -1, NULL}        /* terminator */
53 >        {"m", 0, 1, "Manufacturer"},
54 >        {"n", 0, 1, "Name"},
55 >        {"c", 0, 0, "ThermalConductivity"},
56 >        {"ef", 0, 0, "EmissivityFront"},
57 >        {"eb", 0, 0, "EmissivityBack"},
58 >        {"tir", 0, 0, "TIR"},
59 >        {"eo", 0, 0, "EffectiveOpennessFraction"},
60 >        {"t", 1, 1, "Thickness"},
61 >        {"h", 1, 0, "Height"},
62 >        {"w", 1, 0, "Width"},
63 >        {"\0", 0, 0, NULL}      /* terminator */
64   };
65                                          /* field assignments */
66   #define MAXASSIGN       12
67   const char      *field_assignment[MAXASSIGN];
68   int             nfield_assign = 0;
69   #define FASEP   ';'
59
70                                          /* data file(s) & spectra */
71   enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward };
72  
73 < enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 };
73 > enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5,
74 >        DSuprime=-6, DSvprime=-7 };
75  
76   #define MAXFILES        20
77  
# Line 72 | Line 83 | struct s_dfile {
83  
84   int             ndataf = 0;             /* number of data files */
85  
86 + int             unlink_datafiles = 0;   /* unlink data files when done */
87 +
88   const char      *spectr_file[MAXFILES]; /* custom spectral curve input */
89  
90   const char      top_level_name[] = "WindowElement";
91  
92 < static char     *basis_definition[] = {
92 > static char     basis_definition[][256] = {
93  
94 +        "\t<DataDefinition>\n"
95          "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
96          "\t\t<AngleBasis>\n"
97          "\t\t\t<AngleBasisName>LBNL/Klems Full</AngleBasisName>\n"
98 <        "\t\t</AngleBasis>\n",
98 >        "\t\t\t</AngleBasis>\n"
99 >        "\t</DataDefinition>\n",
100  
101 +        "\t<DataDefinition>\n"
102          "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
103          "\t\t<AngleBasis>\n"
104          "\t\t\t<AngleBasisName>LBNL/Klems Half</AngleBasisName>\n"
105 <        "\t\t</AngleBasis>\n",
105 >        "\t\t\t</AngleBasis>\n"
106 >        "\t</DataDefinition>\n",
107  
108 +        "\t<DataDefinition>\n"
109          "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
110          "\t\t<AngleBasis>\n"
111          "\t\t\t<AngleBasisName>LBNL/Klems Quarter</AngleBasisName>\n"
112 <        "\t\t</AngleBasis>\n",
112 >        "\t\t\t</AngleBasis>\n"
113 >        "\t</DataDefinition>\n",
114  
115 <        "\t\t<IncidentDataStructure>TensorTree3</IncidentDataStructure>\n",
115 >        "\t<DataDefinition>\n"
116 >        "\t\t<IncidentDataStructure>TensorTree3</IncidentDataStructure>\n"
117 >        "\t</DataDefinition>\n",
118  
119 <        "\t\t<IncidentDataStructure>TensorTree4</IncidentDataStructure>\n",
119 >        "\t<DataDefinition>\n"
120 >        "\t\t<IncidentDataStructure>TensorTree4</IncidentDataStructure>\n"
121 >        "\t</DataDefinition>\n",
122   };
123  
124   /* Copy data from file descriptor to stdout and close */
# Line 130 | Line 153 | input2str(const char *inpspec)
153                  return "";
154          if (inpspec == stdin_name) {            /* read from stdin */
155                  fp = stdin;
156 <        } else if (*inpspec == '!') {           /* read from command */
156 >        } else if (inpspec[0] == '!') {         /* read from command */
157                  fp = popen(inpspec+1, "r");
158                  if (fp == NULL) {
159                          fprintf(stderr, "Cannot start process '%s'\n",
160 <                                        inpspec+1);
160 >                                        inpspec);
161                          return "";
162                  }
163          } else {                                /* else load file */
# Line 143 | Line 166 | input2str(const char *inpspec)
166                          fprintf(stderr, "%s: cannot open\n", inpspec);
167                          return "";
168                  }
169 + #if !defined(_WIN32) && !defined(_WIN64)
170 +                                /* XXX somehow broken on Windows */
171                  len = lseek(fd, 0L, SEEK_END);
172 <                if (len < 0) {
173 <                        fprintf(stderr, "%s: seek error\n", inpspec);
172 >                if (len > 0) {
173 >                        lseek(fd, 0L, SEEK_SET);
174 >                        str = (char *)malloc(len+1);
175 >                        if (str == NULL) {
176 >                                close(fd);
177 >                                goto memerr;
178 >                        }
179 >                        if (read(fd, str, len) != len) {
180 >                                fprintf(stderr, "%s: read error\n", inpspec);
181 >                                free(str);
182 >                                close(fd);
183 >                                return "";
184 >                        }
185 >                        str[len] = '\0';
186                          close(fd);
187 <                        return "";
187 >                        return str;
188                  }
189 <                lseek(fd, 0L, SEEK_SET);
190 <                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;
189 > #endif
190 >                fp = fdopen(fd, "r");           /* not a regular file */
191          }
192                                                  /* reading from stream */
193          str = (char *)malloc((len=8192)+1);
# Line 186 | Line 210 | input2str(const char *inpspec)
210                  if (str == NULL)
211                          goto memerr;
212          }
213 <        if (*inpspec != '!')
213 >        if (inpspec[0] != '!')
214                  fclose(fp);
215          else if (pclose(fp))
216 <                fprintf(stderr, "Error running command '%s'\n", inpspec+1);
216 >                fprintf(stderr, "Error running command '%s'\n", inpspec);
217          return str;
218   memerr:
219          fprintf(stderr, "%s: error allocating memory\n", inpspec);
220          if (fp != NULL)
221 <                (*inpspec == '!') ? pclose(fp) : fclose(fp);
221 >                (inpspec[0] == '!') ? pclose(fp) : fclose(fp);
222          return "";
223   }
224  
# Line 204 | Line 228 | mat_assignments(const char *caller, const char *fn, ez
228   {
229          int     i;
230  
207        if (!nfield_assign)
208                return 1;
231          wtl = ezxml_child(wtl, "Material");
232          if (wtl == NULL) {
233                  fprintf(stderr, "%s: missing <Material> tag\n", fn);
# Line 214 | Line 236 | mat_assignments(const char *caller, const char *fn, ez
236          for (i = 0; i < nfield_assign; i++) {
237                  const char      *fnext = field_assignment[i];
238                  for ( ; ; ) {
239 +                        int     added = 0;
240                          ezxml_t fld;
241                          char    sbuf[512];
242                          int     j;
# Line 222 | Line 245 | mat_assignments(const char *caller, const char *fn, ez
245                                  ++fnext;
246                          if (!*fnext)
247                                  break;
248 <                        for (j = 0; (*fnext != '=') & !isspace(*fnext); ) {
248 >                        for (j = 0; *fnext != '=' && !isspace(*fnext); ) {
249                                  if (!*fnext | (*fnext == FASEP) |
250                                                  (j >= sizeof(sbuf)-1)) {
251                                          fprintf(stderr,
# Line 232 | Line 255 | mat_assignments(const char *caller, const char *fn, ez
255                                  }
256                                  sbuf[j++] = *fnext++;
257                          }
258 <                        sbuf[j] = '\0';         /* check nick-names */
258 >                        sbuf[j] = '\0';         /* check known field */
259                          for (j = 0; XMLfieldID[j].nickName[0]; j++)
260 <                                if (!strcasecmp(sbuf, XMLfieldID[j].nickName)) {
260 >                                if (!strcasecmp(sbuf, XMLfieldID[j].nickName) ||
261 >                                        !strcasecmp(sbuf, XMLfieldID[j].fullName)) {
262                                          strcpy(sbuf, XMLfieldID[j].fullName);
263                                          break;
264                                  }
265                                                  /* check if tag exists */
266                          fld = ezxml_child(wtl, sbuf);
267                          if (fld == NULL) {      /* otherwise, create one */
268 <                                fprintf(stderr, "%s: warning - adding tag <%s>\n",
269 <                                        fn, sbuf);
268 >                                if (!XMLfieldID[j].nickName[0])
269 >                                        fprintf(stderr,
270 >                                                "%s: warning - adding tag <%s>\n",
271 >                                                        fn, sbuf);
272 >                                ezxml_add_txt(wtl, "\t");
273                                  fld = ezxml_add_child_d(wtl, sbuf, strlen(wtl->txt));
274 +                                ++added;
275                          }
276                          if (XMLfieldID[j].has_unit)
277                                  ezxml_set_attr(fld, "unit", attr_unit);
278 +                        XMLfieldID[j].win_need = 0;
279                          while (isspace(*fnext))
280                                  ++fnext;
281                          if (*fnext++ != '=') {
# Line 255 | Line 284 | mat_assignments(const char *caller, const char *fn, ez
284                                                  caller, field_assignment[i]);
285                                  return 0;
286                          }
287 <                        for (j = 0; *fnext & (*fnext != FASEP); ) {
287 >                        for (j = 0; *fnext && *fnext != FASEP; ) {
288                                  if (j >= sizeof(sbuf)-1) {
289                                          fprintf(stderr,
290                                          "%s: field too long in '%s'\n",
# Line 266 | Line 295 | mat_assignments(const char *caller, const char *fn, ez
295                          }
296                          sbuf[j] = '\0';
297                          ezxml_set_txt_d(fld, sbuf);
298 +                        if (added)
299 +                                ezxml_add_txt(wtl, "\n\t");
300                          fnext += (*fnext == FASEP);
301                  }
302          }
303 <        return 1;               /* no errors */
303 >                                        /* check required WINDOW settings */
304 >        if (xml_input == win6_template)
305 >            for (i = 0; XMLfieldID[i].nickName[0]; i++)
306 >                if (XMLfieldID[i].win_need &&
307 >                        !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0])
308 >                        fprintf(stderr,
309 >                        "%s: warning - missing '%s' assignment for WINDOW <%s>\n",
310 >                                        caller, XMLfieldID[i].nickName,
311 >                                        XMLfieldID[i].fullName);
312 >        return 1;
313   }
314  
315   /* Complete angle basis specification */
# Line 297 | Line 337 | finish_angle_basis(ezxml_t ab)
337                  sprintf(buf, "%g", i ?
338                  .5*(abase_list[n].lat[i].tmin + abase_list[n].lat[i+1].tmin) :
339                                  .0);
340 +                ezxml_add_txt(abb, "\n\t\t\t\t");
341                  ezxml_set_txt_d(ezxml_add_child(abb,"Theta",strlen(abb->txt)), buf);
342                  sprintf(buf, "%d", abase_list[n].lat[i].nphis);
343 +                ezxml_add_txt(abb, "\n\t\t\t\t");
344                  ezxml_set_txt_d(ezxml_add_child(abb,"nPhis",strlen(abb->txt)), buf);
345 +                ezxml_add_txt(abb, "\n\t\t\t\t");
346                  tb = ezxml_add_child(abb, "ThetaBounds", strlen(abb->txt));
347 +                ezxml_add_txt(tb, "\n\t\t\t\t\t");
348                  sprintf(buf, "%g", abase_list[n].lat[i].tmin);
349 <                ezxml_set_txt(ezxml_add_child(tb,"LowerTheta",strlen(tb->txt)), buf);
349 >                ezxml_set_txt_d(ezxml_add_child(tb,"LowerTheta",strlen(tb->txt)), buf);
350 >                ezxml_add_txt(tb, "\n\t\t\t\t\t");
351                  sprintf(buf, "%g", abase_list[n].lat[i+1].tmin);
352 <                ezxml_set_txt(ezxml_add_child(tb,"UpperTheta",strlen(tb->txt)), buf);
352 >                ezxml_set_txt_d(ezxml_add_child(tb,"UpperTheta",strlen(tb->txt)), buf);
353 >                ezxml_add_txt(tb, "\n\t\t\t\t");
354 >                ezxml_add_txt(abb, "\n\t\t\t");
355 >                ezxml_add_txt(ab, "\n\t\t\t");
356          }
357          return 1;
358   }
# Line 320 | Line 368 | determine_angle_basis(const char *fn, ezxml_t wtl)
368          if (wtl == NULL)
369                  return -1;
370          ids = ezxml_txt(ezxml_child(wtl, "IncidentDataStructure"));
371 <        if (ids == NULL)
371 >        if (!ids[0])
372                  return -1;
373          for (i = 0; i < ABend; i++) {
374                  ezxml_t parsed = ezxml_parse_str(basis_definition[i],
# Line 343 | Line 391 | determine_angle_basis(const char *fn, ezxml_t wtl)
391          return -1;
392   }
393  
394 + /* Filter Klems angle basis, factoring out incident projected solid angle */
395 + static int
396 + filter_klems_matrix(FILE *fp)
397 + {
398 +        const char      *bn = klems_basis_name[angle_basis];
399 +        int             i, j, n = nabases;
400 +                                        /* get angle basis */
401 +        while (n-- > 0)
402 +                if (!strcasecmp(bn, abase_list[n].name))
403 +                        break;
404 +        if (n < 0)
405 +                return 0;
406 +                                        /* read/correct/write matrix */
407 +        for (i = 0; i < abase_list[n].nangles; i++) {
408 +            const double        corr = 1./io_getohm(i, &abase_list[n]);
409 +            for (j = 0; j < abase_list[n].nangles; j++) {
410 +                double  d;
411 +                if (fscanf(fp, "%lf", &d) != 1)
412 +                        return 0;
413 +                if (d < -1e-3) {
414 +                        fputs("Negative BSDF data!\n", stderr);
415 +                        return 0;
416 +                }
417 +                printf(" %.3e", d*corr*(d > 0));
418 +            }
419 +            fputc('\n', stdout);
420 +        }
421 +        while ((i = getc(fp)) != EOF)
422 +                if (!isspace(i)) {
423 +                        fputs("Unexpected data past EOF\n", stderr);
424 +                        return 0;
425 +                }
426 +        return 1;                       /* all is good */
427 + }
428 +
429   /* Write out BSDF data block with surrounding tags */
430   static int
431   writeBSDFblock(const char *caller, struct s_dfile *df)
432   {
433 +        int     correct_klems = correct_solid_angle;
434          char    *cp;
435  
436          puts("\t<WavelengthData>");
# Line 354 | Line 438 | writeBSDFblock(const char *caller, struct s_dfile *df)
438          switch (df->spectrum) {
439          case DSvisible:
440                  puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
441 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
441 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
442                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
443                  break;
444          case DSxbar31:
445                  puts("\t\t<Wavelength unit=\"Integral\">CIE-X</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 X.dsp</DetectorSpectrum>");
448                  break;
449          case DSzbar31:
450                  puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</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 Z.dsp</DetectorSpectrum>");
453                  break;
454 +        case DSuprime:
455 +                puts("\t\t<Wavelength unit=\"Integral\">CIE-u</Wavelength>");
456 +                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
457 +                break;
458 +        case DSvprime:
459 +                puts("\t\t<Wavelength unit=\"Integral\">CIE-v</Wavelength>");
460 +                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
461 +                break;
462          case DSsolar:
463                  puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>");
464 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
464 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
465                  puts("\t\t<DetectorSpectrum>None</DetectorSpectrum>");
466                  break;
467          case DSnir:
468                  puts("\t\t<Wavelength unit=\"Integral\">NIR</Wavelength>");
469 <                puts("\t\tSourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
469 >                puts("\t\t<SourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
470                  puts("\t\t<DetectorSpectrum>PLACE_HOLDER</DetectorSpectrum>");
471                  break;
472          default:
# Line 385 | Line 477 | writeBSDFblock(const char *caller, struct s_dfile *df)
477                                  spectr_file[df->spectrum]);
478                  if (cp != NULL)
479                          *cp = '.';
480 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
480 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
481                  printf("\t\t<DetectorSpectrum>%s</DetectorSpectrum>\n",
482                                  spectr_file[df->spectrum]);
483                  break;
# Line 412 | Line 504 | writeBSDFblock(const char *caller, struct s_dfile *df)
504          puts("</WavelengthDataDirection>");
505          switch (angle_basis) {
506          case ABklemsFull:
415                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>");
416                break;
507          case ABklemsHalf:
418                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Half</ColumnAngleBasis>");
419                break;
508          case ABklemsQuarter:
509 <                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Quarter</ColumnAngleBasis>");
509 >                fputs("\t\t\t<ColumnAngleBasis>", stdout);
510 >                fputs(klems_basis_name[angle_basis], stdout);
511 >                puts("</ColumnAngleBasis>");
512 >                fputs("\t\t\t<RowAngleBasis>", stdout);
513 >                fputs(klems_basis_name[angle_basis], stdout);
514 >                puts("</RowAngleBasis>");
515                  break;
516          case ABtensorTree3:
517          case ABtensorTree4:
518                  puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
519 +                correct_klems = 0;
520                  break;
521          default:
522                  fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis);
# Line 431 | Line 525 | writeBSDFblock(const char *caller, struct s_dfile *df)
525          puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
526          puts("\t\t\t<ScatteringData>");
527          fflush(stdout);
528 <        if (df->fname == stdin_name) {
528 >        if (correct_klems) {                    /* correct Klems matrix data */
529 >                FILE    *fp = stdin;
530 >                if (df->fname[0] == '!')
531 >                        fp = popen(df->fname+1, "r");
532 >                else if (df->fname != stdin_name)
533 >                        fp = fopen(df->fname, "r");
534 >                if (fp == NULL) {
535 >                        fprintf(stderr, "%s: cannot open '%s'\n",
536 >                                        caller, df->fname);
537 >                        return 0;
538 >                }
539 >                if (!filter_klems_matrix(fp)) {
540 >                        fprintf(stderr, "%s: Klems data error from '%s'\n",
541 >                                        caller, df->fname);
542 >                        return 0;
543 >                }
544 >                if (df->fname[0] != '!') {
545 >                        fclose(fp);
546 >                } else if (pclose(fp)) {
547 >                        fprintf(stderr, "%s: error running '%s'\n",
548 >                                        caller, df->fname);
549 >                        return 0;
550 >                }
551 >        } else if (df->fname == stdin_name) {
552                  copy_and_close(fileno(stdin));
553 <        } else if (*df->fname != '!') {
553 >        } else if (df->fname[0] != '!') {
554                  if (!copy_and_close(open(df->fname, O_RDONLY))) {
555                          fprintf(stderr, "%s: error reading from '%s'\n",
556                                          caller, df->fname);
557                          return 0;
558                  }
559          } else if (system(df->fname+1)) {
560 <                fprintf(stderr, "%s: error running '%s'\n", caller, df->fname+1);
560 >                fprintf(stderr, "%s: error running '%s'\n", caller, df->fname);
561                  return 0;
562          }
563          puts("\t\t\t</ScatteringData>");
# Line 453 | Line 570 | writeBSDFblock(const char *caller, struct s_dfile *df)
570   static int
571   writeBSDF(const char *caller, ezxml_t fl)
572   {
573 <        char    *xml = ezxml_toxml(fl);
573 >        char    *xml = ezxml_toxml(fl);         /* store XML in string */
574          int     ei, i;
575                                                  /* locate trailer */
576          for (ei = strlen(xml)-strlen("</Layer></Optical></WindowElement>");
# Line 466 | Line 583 | writeBSDF(const char *caller, ezxml_t fl)
583                  free(xml);
584                  return 0;
585          }
586 +        puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
587 +        for (i = 0; i < ncomm; i++)
588 +                printf("<!-- %s -->\n", commlist[i]);
589          fflush(stdout);                         /* write previous XML info. */
590          if (write(fileno(stdout), xml, ei) != ei) {
591                  free(xml);
# Line 477 | Line 597 | writeBSDF(const char *caller, ezxml_t fl)
597                          return 0;
598                  }
599          fputs(xml+ei, stdout);                  /* write trailer */
600 <        free(xml);
601 <        return (fflush(stdout) == 0);
600 >        free(xml);                              /* free string */
601 >        fputc('\n', stdout);
602 >        if (fflush(stdout) != 0)
603 >                return 0;
604 >                                                /* unlink data files if req. */
605 >        for (i = ndataf*(unlink_datafiles != 0); i--; )
606 >                if (data_file[i].fname != stdin_name &&
607 >                                data_file[i].fname[0] != '!')
608 >                        unlink(data_file[i].fname);
609 >        if (unlink_datafiles > 1 && mgf_geometry != NULL &&
610 >                        mgf_geometry != stdin_name &&
611 >                        mgf_geometry[0] != '!')
612 >                unlink(mgf_geometry);
613 >        return 1;
614   }
615  
616   /* Insert BSDF data into XML wrapper */
617   static int
618   wrapBSDF(const char *caller)
619   {
620 <        const char      *xml_path = getpath((char *)xml_input, getrlibpath(), R_OK);
620 >        const char      *xml_path = xml_input;
621          ezxml_t         fl, wtl;
622                                          /* load previous XML/template */
623 <        if (xml_path == NULL) {
624 <                fprintf(stderr, "%s: cannot find XML file named '%s'\n",
625 <                                caller, xml_input==NULL ? "NULL" : xml_input);
626 <                return 0;
623 >        if (xml_input == stdin_name) {
624 >                fl = ezxml_parse_fp(stdin);
625 >        } else if (xml_input[0] == '!') {
626 >                FILE    *pfp = popen(xml_input+1, "r");
627 >                if (pfp == NULL) {
628 >                        fprintf(stderr, "%s: cannot start process '%s'\n",
629 >                                        caller, xml_input);
630 >                        return 0;
631 >                }
632 >                fl = ezxml_parse_fp(pfp);
633 >                if (pclose(pfp)) {
634 >                        fprintf(stderr, "%s: error running '%s'\n",
635 >                                        caller, xml_input);
636 >                        return 0;
637 >                }
638 >        } else {
639 >                xml_path = getpath((char *)xml_input, getrlibpath(), R_OK);
640 >                if (xml_path == NULL) {
641 >                        fprintf(stderr, "%s: cannot find XML file named '%s'\n",
642 >                                        caller, xml_input==NULL ? "NULL" : xml_input);
643 >                        return 0;
644 >                }
645 >                fl = ezxml_parse_file(xml_path);
646          }
496        fl = ezxml_parse_file(xml_path);
647          if (fl == NULL) {
648 <                fprintf(stderr, "%s: cannot open XML path '%s'\n",
499 <                                caller, xml_path);
648 >                fprintf(stderr, "%s: cannot load XML '%s'\n", caller, xml_path);
649                  return 0;
650          }
651          if (ezxml_error(fl)[0]) {
652 <                fprintf(stderr, "%s: error in XML %s: %s\n", caller, xml_path,
652 >                fprintf(stderr, "%s: error in XML '%s': %s\n", caller, xml_path,
653                                  ezxml_error(fl));
654                  goto failure;
655          }
# Line 522 | Line 671 | wrapBSDF(const char *caller)
671                  goto failure;
672          }
673                                          /* make material assignments */
674 <        if (!mat_assignments(caller, xml_path, wtl)) {
674 >        if (!mat_assignments(caller, xml_path, wtl))
675                  goto failure;
527        }
676          if (mgf_geometry != NULL) {     /* add geometry if specified */
677                  ezxml_t geom = ezxml_child(wtl, "Geometry");
678                  if (geom == NULL)
679 <                        geom = ezxml_add_child(wtl, "Geometry", 0);
679 >                        geom = ezxml_add_child(wtl, "Geometry", strlen(wtl->txt));
680                  ezxml_set_attr(geom, "format", "MGF");
681                  geom = ezxml_child(geom, "MGFblock");
682                  if (geom == NULL) {
683                          geom = ezxml_child(wtl, "Geometry");
684 <                        geom = ezxml_add_child(geom, "MGFblock", strlen(geom->txt));
684 >                        geom = ezxml_add_child(geom, "MGFblock", 0);
685                  }
686                  ezxml_set_attr(geom, "unit", attr_unit);
687                  ezxml_set_txt(geom, input2str(mgf_geometry));
# Line 542 | Line 690 | wrapBSDF(const char *caller)
690          }
691                                          /* check basis */
692          if (angle_basis != ABdefault) {
693 +                size_t  offset = 0;
694                  ezxml_t ab, dd = ezxml_child(wtl, "DataDefinition");
695 <                if (dd == NULL) {
696 <                        dd = ezxml_add_child(wtl, "DataDefinition", 0);
697 <                } else {
549 <                        if (ezxml_child(dd, "DataDefinition") != NULL)
695 >                if (dd != NULL) {
696 >                        offset = dd->off;
697 >                        if (dd->child != NULL)
698                                  fprintf(stderr,
699                  "%s: warning - replacing existing <DataDefinition> in '%s'\n",
700                                                  caller, xml_path);
701 <                        while (dd->child != NULL)
702 <                                ezxml_remove(dd->child);
703 <                }
704 <                ezxml_insert(ezxml_parse_str(basis_definition[angle_basis],
701 >                        ezxml_remove(dd);
702 >                } else
703 >                        offset = strlen(wtl->txt);
704 >                dd = ezxml_insert(ezxml_parse_str(basis_definition[angle_basis],
705                                          strlen(basis_definition[angle_basis])),
706 <                                dd, 0);
706 >                                wtl, offset);
707                  if ((ab = ezxml_child(dd, "AngleBasis")) != NULL &&
708                                  !finish_angle_basis(ab))
709                          goto failure;
# Line 580 | Line 728 | UsageExit(const char *pname)
728   {
729          fputs("Usage: ", stderr);
730          fputs(pname, stderr);
731 <        fputs(" [options] [input.xml]\n", stderr);
731 >        fputs(" [-W][-c][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr);
732 >        fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp][-C comm]", stderr);
733 >        fputs(" [input.xml]\n", stderr);
734          exit(1);
735   }
736  
# Line 617 | Line 767 | main(int argc, char *argv[])
767                                                  argv[0]);
768                                  return 1;
769                          }
770 <                        if (strstr(argv[i], legal_units) == NULL) {
771 <                                fprintf(stderr, "%s: unit must be one of (%s)\n",
770 >                        if (strstr(legal_units, argv[i]) == NULL) {
771 >                                fprintf(stderr, "%s: -u unit must be one of (%s)\n",
772                                                  argv[0], legal_units);
773                                  return 1;
774                          }
775                          attr_unit = argv[i];
776                          continue;
777 +                case 'U':               /* unlink data files when done */
778 +                        unlink_datafiles = 1 + (argv[i][2] == 'U');
779 +                        continue;
780                  case 'a':               /* angle basis */
781                          if (++i >= argc)
782                                  UsageExit(argv[0]);
# Line 645 | Line 798 | main(int argc, char *argv[])
798                          else
799                                  UsageExit(argv[0]);
800                          continue;
801 +                case 'c':               /* correct solid angle */
802 +                        correct_solid_angle = 1;
803 +                        continue;
804 +                case 'C':               /* comment */
805 +                        if (ncomm >= MAXCOMM) {
806 +                                fprintf(stderr, "%s: too many comments\n",
807 +                                                argv[0]);
808 +                                return 1;
809 +                        }
810 +                        if (strchr(argv[++i], '>') != NULL) {
811 +                                fprintf(stderr, "%s: illegal character in comment\n",
812 +                                                argv[0]);
813 +                                return 1;
814 +                        }
815 +                        commlist[ncomm++] = argv[i];
816 +                        continue;
817                  case 't':               /* transmission */
818                          if (i >= argc-1)
819                                  UsageExit(argv[0]);
# Line 686 | Line 855 | main(int argc, char *argv[])
855                                  argv[i] = (char *)stdin_name;
856                          }
857                          data_file[ndataf].fname = argv[i];
858 <                        data_file[ndataf++].spectrum = cur_spectrum;
858 >                        data_file[ndataf].spectrum = cur_spectrum;
859 >                        ndataf++;
860                          continue;
861                  case 's':               /* spectrum name or input file */
862                          if (++i >= argc)
# Line 700 | Line 870 | main(int argc, char *argv[])
870                                  cur_spectrum = DSxbar31;
871                          else if (!strcasecmp(argv[i], "CIE-Z"))
872                                  cur_spectrum = DSzbar31;
873 +                        else if (!strcasecmp(argv[i], "CIE-u"))
874 +                                cur_spectrum = DSuprime;
875 +                        else if (!strcasecmp(argv[i], "CIE-v"))
876 +                                cur_spectrum = DSvprime;
877                          else if (!strcasecmp(argv[i], "NIR"))
878                                  cur_spectrum = DSnir;
879                          else {
# Line 748 | Line 922 | doneOptions:                                   /* get XML input */
922          } else {
923                  xml_input = argv[i];
924          }
751        if ((xml_input == win6_template) & (angle_basis == ABdefault))
752                angle_basis = ABklemsFull;
925                                                  /* wrap it! */
926          return !wrapBSDF(argv[0]);
927   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines