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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines