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.27 by greg, Wed Sep 7 18:55:39 2022 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         80
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 };
72 > enum { DTransFront, DTransBack, DTreflFront, DTreflBack };
73  
74 < enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 };
74 > static const char       component_name[4][20] = {
75 >                "Transmission Front",
76 >                "Transmission Back",
77 >                "Reflection Front",
78 >                "Reflection Back"
79 > };
80  
81 + enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5,
82 +        DSuprime=-6, DSvprime=-7 };
83 +
84   #define MAXFILES        20
85  
86   struct s_dfile {
# Line 72 | Line 91 | struct s_dfile {
91  
92   int             ndataf = 0;             /* number of data files */
93  
94 + int             unlink_datafiles = 0;   /* unlink data files when done */
95 +
96   const char      *spectr_file[MAXFILES]; /* custom spectral curve input */
97  
98   const char      top_level_name[] = "WindowElement";
99  
100 < static char     *basis_definition[] = {
100 > static char     basis_definition[][256] = {
101  
102 +        "\t<DataDefinition>\n"
103          "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
104          "\t\t<AngleBasis>\n"
105          "\t\t\t<AngleBasisName>LBNL/Klems Full</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 Half</AngleBasisName>\n"
113 <        "\t\t</AngleBasis>\n",
113 >        "\t\t\t</AngleBasis>\n"
114 >        "\t</DataDefinition>\n",
115  
116 +        "\t<DataDefinition>\n"
117          "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
118          "\t\t<AngleBasis>\n"
119          "\t\t\t<AngleBasisName>LBNL/Klems Quarter</AngleBasisName>\n"
120 <        "\t\t</AngleBasis>\n",
120 >        "\t\t\t</AngleBasis>\n"
121 >        "\t</DataDefinition>\n",
122  
123 <        "\t\t<IncidentDataStructure>TensorTree3</IncidentDataStructure>\n",
123 >        "\t<DataDefinition>\n"
124 >        "\t\t<IncidentDataStructure>TensorTree3</IncidentDataStructure>\n"
125 >        "\t</DataDefinition>\n",
126  
127 <        "\t\t<IncidentDataStructure>TensorTree4</IncidentDataStructure>\n",
127 >        "\t<DataDefinition>\n"
128 >        "\t\t<IncidentDataStructure>TensorTree4</IncidentDataStructure>\n"
129 >        "\t</DataDefinition>\n",
130   };
131  
132 + /* Check that the last-added data file is unique */
133 + static int
134 + check_new_data_file()
135 + {
136 +        int     i = ndataf;
137 +
138 +        while (i-- > 0)
139 +                if ((data_file[i].spectrum == data_file[ndataf].spectrum) &
140 +                                (data_file[i].type == data_file[ndataf].type)) {
141 +                        fprintf(stderr,
142 +                                "%s: warning - ignoring duplicate component %s\n",
143 +                                        data_file[ndataf].fname,
144 +                                        component_name[data_file[i].type]);
145 +                        return 0;
146 +                }
147 +        return 1;
148 + }
149 +
150   /* Copy data from file descriptor to stdout and close */
151   static int
152   copy_and_close(int fd)
# Line 130 | Line 179 | input2str(const char *inpspec)
179                  return "";
180          if (inpspec == stdin_name) {            /* read from stdin */
181                  fp = stdin;
182 <        } else if (*inpspec == '!') {           /* read from command */
182 >        } else if (inpspec[0] == '!') {         /* read from command */
183                  fp = popen(inpspec+1, "r");
184                  if (fp == NULL) {
185                          fprintf(stderr, "Cannot start process '%s'\n",
186 <                                        inpspec+1);
186 >                                        inpspec);
187                          return "";
188                  }
189          } else {                                /* else load file */
# Line 143 | Line 192 | input2str(const char *inpspec)
192                          fprintf(stderr, "%s: cannot open\n", inpspec);
193                          return "";
194                  }
195 + #if !defined(_WIN32) && !defined(_WIN64)
196 +                                /* XXX somehow broken on Windows */
197                  len = lseek(fd, 0L, SEEK_END);
198 <                if (len < 0) {
199 <                        fprintf(stderr, "%s: seek error\n", inpspec);
198 >                if (len > 0) {
199 >                        lseek(fd, 0L, SEEK_SET);
200 >                        str = (char *)malloc(len+1);
201 >                        if (str == NULL) {
202 >                                close(fd);
203 >                                goto memerr;
204 >                        }
205 >                        if (read(fd, str, len) != len) {
206 >                                fprintf(stderr, "%s: read error\n", inpspec);
207 >                                free(str);
208 >                                close(fd);
209 >                                return "";
210 >                        }
211 >                        str[len] = '\0';
212                          close(fd);
213 <                        return "";
213 >                        return str;
214                  }
215 <                lseek(fd, 0L, SEEK_SET);
216 <                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;
215 > #endif
216 >                fp = fdopen(fd, "r");           /* not a regular file */
217          }
218                                                  /* reading from stream */
219          str = (char *)malloc((len=8192)+1);
# Line 186 | Line 236 | input2str(const char *inpspec)
236                  if (str == NULL)
237                          goto memerr;
238          }
239 <        if (*inpspec != '!')
239 >        if (inpspec[0] != '!')
240                  fclose(fp);
241          else if (pclose(fp))
242 <                fprintf(stderr, "Error running command '%s'\n", inpspec+1);
242 >                fprintf(stderr, "Error running command '%s'\n", inpspec);
243          return str;
244   memerr:
245          fprintf(stderr, "%s: error allocating memory\n", inpspec);
246          if (fp != NULL)
247 <                (*inpspec == '!') ? pclose(fp) : fclose(fp);
247 >                (inpspec[0] == '!') ? pclose(fp) : fclose(fp);
248          return "";
249   }
250  
# Line 204 | Line 254 | mat_assignments(const char *caller, const char *fn, ez
254   {
255          int     i;
256  
207        if (!nfield_assign)
208                return 1;
257          wtl = ezxml_child(wtl, "Material");
258          if (wtl == NULL) {
259                  fprintf(stderr, "%s: missing <Material> tag\n", fn);
# Line 214 | Line 262 | mat_assignments(const char *caller, const char *fn, ez
262          for (i = 0; i < nfield_assign; i++) {
263                  const char      *fnext = field_assignment[i];
264                  for ( ; ; ) {
265 +                        int     added = 0;
266                          ezxml_t fld;
267                          char    sbuf[512];
268                          int     j;
# Line 222 | Line 271 | mat_assignments(const char *caller, const char *fn, ez
271                                  ++fnext;
272                          if (!*fnext)
273                                  break;
274 <                        for (j = 0; (*fnext != '=') & !isspace(*fnext); ) {
274 >                        for (j = 0; *fnext != '=' && !isspace(*fnext); ) {
275                                  if (!*fnext | (*fnext == FASEP) |
276                                                  (j >= sizeof(sbuf)-1)) {
277                                          fprintf(stderr,
# Line 232 | Line 281 | mat_assignments(const char *caller, const char *fn, ez
281                                  }
282                                  sbuf[j++] = *fnext++;
283                          }
284 <                        sbuf[j] = '\0';         /* check nick-names */
284 >                        sbuf[j] = '\0';         /* check known field */
285                          for (j = 0; XMLfieldID[j].nickName[0]; j++)
286 <                                if (!strcasecmp(sbuf, XMLfieldID[j].nickName)) {
286 >                                if (!strcasecmp(sbuf, XMLfieldID[j].nickName) ||
287 >                                        !strcasecmp(sbuf, XMLfieldID[j].fullName)) {
288                                          strcpy(sbuf, XMLfieldID[j].fullName);
289                                          break;
290                                  }
291                                                  /* check if tag exists */
292                          fld = ezxml_child(wtl, sbuf);
293                          if (fld == NULL) {      /* otherwise, create one */
294 <                                fprintf(stderr, "%s: warning - adding tag <%s>\n",
295 <                                        fn, sbuf);
294 >                                if (!XMLfieldID[j].nickName[0])
295 >                                        fprintf(stderr,
296 >                                                "%s: warning - adding tag <%s>\n",
297 >                                                        fn, sbuf);
298 >                                ezxml_add_txt(wtl, "\t");
299                                  fld = ezxml_add_child_d(wtl, sbuf, strlen(wtl->txt));
300 +                                ++added;
301                          }
302                          if (XMLfieldID[j].has_unit)
303                                  ezxml_set_attr(fld, "unit", attr_unit);
304 +                        XMLfieldID[j].win_need = 0;
305                          while (isspace(*fnext))
306                                  ++fnext;
307                          if (*fnext++ != '=') {
# Line 255 | Line 310 | mat_assignments(const char *caller, const char *fn, ez
310                                                  caller, field_assignment[i]);
311                                  return 0;
312                          }
313 <                        for (j = 0; *fnext & (*fnext != FASEP); ) {
313 >                        for (j = 0; *fnext && *fnext != FASEP; ) {
314                                  if (j >= sizeof(sbuf)-1) {
315                                          fprintf(stderr,
316                                          "%s: field too long in '%s'\n",
# Line 266 | Line 321 | mat_assignments(const char *caller, const char *fn, ez
321                          }
322                          sbuf[j] = '\0';
323                          ezxml_set_txt_d(fld, sbuf);
324 +                        if (added)
325 +                                ezxml_add_txt(wtl, "\n\t");
326                          fnext += (*fnext == FASEP);
327                  }
328          }
329 <        return 1;               /* no errors */
329 >                                        /* check required WINDOW settings */
330 >        if (xml_input == win6_template)
331 >            for (i = 0; XMLfieldID[i].nickName[0]; i++)
332 >                if (XMLfieldID[i].win_need &&
333 >                        !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0])
334 >                        fprintf(stderr,
335 >                        "%s: warning - missing '%s' assignment for WINDOW <%s>\n",
336 >                                        caller, XMLfieldID[i].nickName,
337 >                                        XMLfieldID[i].fullName);
338 >        return 1;
339   }
340  
341   /* Complete angle basis specification */
# Line 297 | Line 363 | finish_angle_basis(ezxml_t ab)
363                  sprintf(buf, "%g", i ?
364                  .5*(abase_list[n].lat[i].tmin + abase_list[n].lat[i+1].tmin) :
365                                  .0);
366 +                ezxml_add_txt(abb, "\n\t\t\t\t");
367                  ezxml_set_txt_d(ezxml_add_child(abb,"Theta",strlen(abb->txt)), buf);
368                  sprintf(buf, "%d", abase_list[n].lat[i].nphis);
369 +                ezxml_add_txt(abb, "\n\t\t\t\t");
370                  ezxml_set_txt_d(ezxml_add_child(abb,"nPhis",strlen(abb->txt)), buf);
371 +                ezxml_add_txt(abb, "\n\t\t\t\t");
372                  tb = ezxml_add_child(abb, "ThetaBounds", strlen(abb->txt));
373 +                ezxml_add_txt(tb, "\n\t\t\t\t\t");
374                  sprintf(buf, "%g", abase_list[n].lat[i].tmin);
375 <                ezxml_set_txt(ezxml_add_child(tb,"LowerTheta",strlen(tb->txt)), buf);
375 >                ezxml_set_txt_d(ezxml_add_child(tb,"LowerTheta",strlen(tb->txt)), buf);
376 >                ezxml_add_txt(tb, "\n\t\t\t\t\t");
377                  sprintf(buf, "%g", abase_list[n].lat[i+1].tmin);
378 <                ezxml_set_txt(ezxml_add_child(tb,"UpperTheta",strlen(tb->txt)), buf);
378 >                ezxml_set_txt_d(ezxml_add_child(tb,"UpperTheta",strlen(tb->txt)), buf);
379 >                ezxml_add_txt(tb, "\n\t\t\t\t");
380 >                ezxml_add_txt(abb, "\n\t\t\t");
381 >                ezxml_add_txt(ab, "\n\t\t\t");
382          }
383          return 1;
384   }
# Line 320 | Line 394 | determine_angle_basis(const char *fn, ezxml_t wtl)
394          if (wtl == NULL)
395                  return -1;
396          ids = ezxml_txt(ezxml_child(wtl, "IncidentDataStructure"));
397 <        if (ids == NULL)
397 >        if (!ids[0])
398                  return -1;
399          for (i = 0; i < ABend; i++) {
400                  ezxml_t parsed = ezxml_parse_str(basis_definition[i],
# Line 343 | Line 417 | determine_angle_basis(const char *fn, ezxml_t wtl)
417          return -1;
418   }
419  
420 + /* Filter Klems angle data, factoring out incident projected solid angle */
421 + static int
422 + filter_klems_matrix(FILE *fp)
423 + {
424 +        const char      *bn = klems_basis_name[angle_basis];
425 +        int             i, j, c, n = nabases;
426 +                                        /* get angle basis */
427 +        while (n-- > 0)
428 +                if (!strcasecmp(bn, abase_list[n].name))
429 +                        break;
430 +        if (n < 0)
431 +                return 0;
432 +                                        /* read/correct/write matrix */
433 +        for (i = 0; i < abase_list[n].nangles; i++) {
434 +            const double        corr = 1./io_getohm(i, &abase_list[n]);
435 +            for (j = 0; j < abase_list[n].nangles; j++) {
436 +                double  d;
437 +                if (fscanf(fp, "%lf ", &d) != 1)
438 +                        return 0;
439 +                if ((c = getc(fp)) != ',')
440 +                        ungetc(c, fp);
441 +                if (d < -1e-3) {
442 +                        fputs("Negative BSDF data!\n", stderr);
443 +                        return 0;
444 +                }
445 +                printf(" %.3e", d*corr*(d > 0));
446 +            }
447 +            fputc('\n', stdout);
448 +        }
449 +        while ((c = getc(fp)) != EOF)
450 +                if (!isspace(c)) {
451 +                        fputs("Unexpected data past EOF\n", stderr);
452 +                        return 0;
453 +                }
454 +        return 1;                       /* all is good */
455 + }
456 +
457   /* Write out BSDF data block with surrounding tags */
458   static int
459   writeBSDFblock(const char *caller, struct s_dfile *df)
460   {
461 +        int     correct_klems = correct_solid_angle;
462          char    *cp;
463  
464          puts("\t<WavelengthData>");
# Line 354 | Line 466 | writeBSDFblock(const char *caller, struct s_dfile *df)
466          switch (df->spectrum) {
467          case DSvisible:
468                  puts("\t\t<Wavelength unit=\"Integral\">Visible</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>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
471                  break;
472          case DSxbar31:
473                  puts("\t\t<Wavelength unit=\"Integral\">CIE-X</Wavelength>");
474 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
474 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
475                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 X.dsp</DetectorSpectrum>");
476                  break;
477          case DSzbar31:
478                  puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</Wavelength>");
479 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
479 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
480                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 Z.dsp</DetectorSpectrum>");
481                  break;
482 +        case DSuprime:
483 +                puts("\t\t<Wavelength unit=\"Integral\">CIE-u</Wavelength>");
484 +                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
485 +                break;
486 +        case DSvprime:
487 +                puts("\t\t<Wavelength unit=\"Integral\">CIE-v</Wavelength>");
488 +                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
489 +                break;
490          case DSsolar:
491                  puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>");
492 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
492 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
493                  puts("\t\t<DetectorSpectrum>None</DetectorSpectrum>");
494                  break;
495          case DSnir:
496                  puts("\t\t<Wavelength unit=\"Integral\">NIR</Wavelength>");
497 <                puts("\t\tSourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
497 >                puts("\t\t<SourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
498                  puts("\t\t<DetectorSpectrum>PLACE_HOLDER</DetectorSpectrum>");
499                  break;
500          default:
# Line 385 | Line 505 | writeBSDFblock(const char *caller, struct s_dfile *df)
505                                  spectr_file[df->spectrum]);
506                  if (cp != NULL)
507                          *cp = '.';
508 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
508 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
509                  printf("\t\t<DetectorSpectrum>%s</DetectorSpectrum>\n",
510                                  spectr_file[df->spectrum]);
511                  break;
512          }
513          puts("\t\t<WavelengthDataBlock>");
514          fputs("\t\t\t<WavelengthDataDirection>", stdout);
515 <        switch (df->type) {
396 <        case DTtransForward:
397 <                fputs("Transmission Front", stdout);
398 <                break;
399 <        case DTtransBackward:
400 <                fputs("Transmission Back", stdout);
401 <                break;
402 <        case DTreflForward:
403 <                fputs("Reflection Front", stdout);
404 <                break;
405 <        case DTreflBackward:
406 <                fputs("Reflection Back", stdout);
407 <                break;
408 <        default:
409 <                fprintf(stderr, "%s: internal - bad BSDF type (%d)\n", caller, df->type);
410 <                return 0;
411 <        }
515 >        fputs(component_name[df->type], stdout);
516          puts("</WavelengthDataDirection>");
517          switch (angle_basis) {
518          case ABklemsFull:
415                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>");
416                break;
519          case ABklemsHalf:
418                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Half</ColumnAngleBasis>");
419                break;
520          case ABklemsQuarter:
521 <                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Quarter</ColumnAngleBasis>");
521 >                fputs("\t\t\t<ColumnAngleBasis>", stdout);
522 >                fputs(klems_basis_name[angle_basis], stdout);
523 >                puts("</ColumnAngleBasis>");
524 >                fputs("\t\t\t<RowAngleBasis>", stdout);
525 >                fputs(klems_basis_name[angle_basis], stdout);
526 >                puts("</RowAngleBasis>");
527                  break;
528          case ABtensorTree3:
529          case ABtensorTree4:
530                  puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
531 +                correct_klems = 0;
532                  break;
533          default:
534                  fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis);
# Line 431 | Line 537 | writeBSDFblock(const char *caller, struct s_dfile *df)
537          puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
538          puts("\t\t\t<ScatteringData>");
539          fflush(stdout);
540 <        if (df->fname == stdin_name) {
540 >        if (correct_klems) {                    /* correct Klems matrix data */
541 >                FILE    *fp = stdin;
542 >                if (df->fname[0] == '!')
543 >                        fp = popen(df->fname+1, "r");
544 >                else if (df->fname != stdin_name)
545 >                        fp = fopen(df->fname, "r");
546 >                if (fp == NULL) {
547 >                        fprintf(stderr, "%s: cannot open '%s'\n",
548 >                                        caller, df->fname);
549 >                        return 0;
550 >                }
551 >                if (!filter_klems_matrix(fp)) {
552 >                        fprintf(stderr, "%s: Klems data error from '%s'\n",
553 >                                        caller, df->fname);
554 >                        return 0;
555 >                }
556 >                if (df->fname[0] != '!') {
557 >                        fclose(fp);
558 >                } else if (pclose(fp)) {
559 >                        fprintf(stderr, "%s: error running '%s'\n",
560 >                                        caller, df->fname);
561 >                        return 0;
562 >                }
563 >        } else if (df->fname == stdin_name) {
564                  copy_and_close(fileno(stdin));
565 <        } else if (*df->fname != '!') {
565 >        } else if (df->fname[0] != '!') {
566                  if (!copy_and_close(open(df->fname, O_RDONLY))) {
567                          fprintf(stderr, "%s: error reading from '%s'\n",
568                                          caller, df->fname);
569                          return 0;
570                  }
571          } else if (system(df->fname+1)) {
572 <                fprintf(stderr, "%s: error running '%s'\n", caller, df->fname+1);
572 >                fprintf(stderr, "%s: error running '%s'\n", caller, df->fname);
573                  return 0;
574          }
575          puts("\t\t\t</ScatteringData>");
# Line 453 | Line 582 | writeBSDFblock(const char *caller, struct s_dfile *df)
582   static int
583   writeBSDF(const char *caller, ezxml_t fl)
584   {
585 <        char    *xml = ezxml_toxml(fl);
585 >        char    *xml = ezxml_toxml(fl);         /* store XML in string */
586          int     ei, i;
587                                                  /* locate trailer */
588          for (ei = strlen(xml)-strlen("</Layer></Optical></WindowElement>");
# Line 466 | Line 595 | writeBSDF(const char *caller, ezxml_t fl)
595                  free(xml);
596                  return 0;
597          }
598 +        puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
599 +        for (i = 0; i < ncomm; i++)
600 +                printf("<!-- %s -->\n", commlist[i]);
601          fflush(stdout);                         /* write previous XML info. */
602          if (write(fileno(stdout), xml, ei) != ei) {
603                  free(xml);
# Line 477 | Line 609 | writeBSDF(const char *caller, ezxml_t fl)
609                          return 0;
610                  }
611          fputs(xml+ei, stdout);                  /* write trailer */
612 <        free(xml);
613 <        return (fflush(stdout) == 0);
612 >        free(xml);                              /* free string */
613 >        fputc('\n', stdout);
614 >        if (fflush(stdout) != 0)
615 >                return 0;
616 >                                                /* unlink data files if req. */
617 >        for (i = ndataf*(unlink_datafiles != 0); i--; )
618 >                if (data_file[i].fname != stdin_name &&
619 >                                data_file[i].fname[0] != '!')
620 >                        unlink(data_file[i].fname);
621 >        if (unlink_datafiles > 1 && mgf_geometry != NULL &&
622 >                        mgf_geometry != stdin_name &&
623 >                        mgf_geometry[0] != '!')
624 >                unlink(mgf_geometry);
625 >        return 1;
626   }
627  
628   /* Insert BSDF data into XML wrapper */
629   static int
630   wrapBSDF(const char *caller)
631   {
632 <        const char      *xml_path = getpath((char *)xml_input, getrlibpath(), R_OK);
632 >        const char      *xml_path = xml_input;
633          ezxml_t         fl, wtl;
634                                          /* load previous XML/template */
635 <        if (xml_path == NULL) {
636 <                fprintf(stderr, "%s: cannot find XML file named '%s'\n",
637 <                                caller, xml_input==NULL ? "NULL" : xml_input);
638 <                return 0;
635 >        if (xml_input == stdin_name) {
636 >                fl = ezxml_parse_fp(stdin);
637 >        } else if (xml_input[0] == '!') {
638 >                FILE    *pfp = popen(xml_input+1, "r");
639 >                if (pfp == NULL) {
640 >                        fprintf(stderr, "%s: cannot start process '%s'\n",
641 >                                        caller, xml_input);
642 >                        return 0;
643 >                }
644 >                fl = ezxml_parse_fp(pfp);
645 >                if (pclose(pfp)) {
646 >                        fprintf(stderr, "%s: error running '%s'\n",
647 >                                        caller, xml_input);
648 >                        return 0;
649 >                }
650 >        } else {
651 >                xml_path = getpath((char *)xml_input, getrlibpath(), R_OK);
652 >                if (xml_path == NULL) {
653 >                        fprintf(stderr, "%s: cannot find XML file named '%s'\n",
654 >                                        caller, xml_input==NULL ? "NULL" : xml_input);
655 >                        return 0;
656 >                }
657 >                fl = ezxml_parse_file(xml_path);
658          }
496        fl = ezxml_parse_file(xml_path);
659          if (fl == NULL) {
660 <                fprintf(stderr, "%s: cannot open XML path '%s'\n",
499 <                                caller, xml_path);
660 >                fprintf(stderr, "%s: cannot load XML '%s'\n", caller, xml_path);
661                  return 0;
662          }
663          if (ezxml_error(fl)[0]) {
664 <                fprintf(stderr, "%s: error in XML %s: %s\n", caller, xml_path,
664 >                fprintf(stderr, "%s: error in XML '%s': %s\n", caller, xml_path,
665                                  ezxml_error(fl));
666                  goto failure;
667          }
# Line 522 | Line 683 | wrapBSDF(const char *caller)
683                  goto failure;
684          }
685                                          /* make material assignments */
686 <        if (!mat_assignments(caller, xml_path, wtl)) {
686 >        if (!mat_assignments(caller, xml_path, wtl))
687                  goto failure;
527        }
688          if (mgf_geometry != NULL) {     /* add geometry if specified */
689                  ezxml_t geom = ezxml_child(wtl, "Geometry");
690                  if (geom == NULL)
691 <                        geom = ezxml_add_child(wtl, "Geometry", 0);
691 >                        geom = ezxml_add_child(wtl, "Geometry", strlen(wtl->txt));
692                  ezxml_set_attr(geom, "format", "MGF");
693                  geom = ezxml_child(geom, "MGFblock");
694                  if (geom == NULL) {
695                          geom = ezxml_child(wtl, "Geometry");
696 <                        geom = ezxml_add_child(geom, "MGFblock", strlen(geom->txt));
696 >                        geom = ezxml_add_child(geom, "MGFblock", 0);
697                  }
698                  ezxml_set_attr(geom, "unit", attr_unit);
699                  ezxml_set_txt(geom, input2str(mgf_geometry));
# Line 542 | Line 702 | wrapBSDF(const char *caller)
702          }
703                                          /* check basis */
704          if (angle_basis != ABdefault) {
705 +                size_t  offset = 0;
706                  ezxml_t ab, dd = ezxml_child(wtl, "DataDefinition");
707 <                if (dd == NULL) {
708 <                        dd = ezxml_add_child(wtl, "DataDefinition", 0);
709 <                } else {
549 <                        if (ezxml_child(dd, "DataDefinition") != NULL)
707 >                if (dd != NULL) {
708 >                        offset = dd->off;
709 >                        if (dd->child != NULL)
710                                  fprintf(stderr,
711                  "%s: warning - replacing existing <DataDefinition> in '%s'\n",
712                                                  caller, xml_path);
713 <                        while (dd->child != NULL)
714 <                                ezxml_remove(dd->child);
715 <                }
716 <                ezxml_insert(ezxml_parse_str(basis_definition[angle_basis],
713 >                        ezxml_remove(dd);
714 >                } else
715 >                        offset = strlen(wtl->txt);
716 >                dd = ezxml_insert(ezxml_parse_str(basis_definition[angle_basis],
717                                          strlen(basis_definition[angle_basis])),
718 <                                dd, 0);
718 >                                wtl, offset);
719                  if ((ab = ezxml_child(dd, "AngleBasis")) != NULL &&
720                                  !finish_angle_basis(ab))
721                          goto failure;
# Line 580 | Line 740 | UsageExit(const char *pname)
740   {
741          fputs("Usage: ", stderr);
742          fputs(pname, stderr);
743 <        fputs(" [options] [input.xml]\n", stderr);
743 >        fputs(" [-W][-c][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr);
744 >        fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp][-C comm]", stderr);
745 >        fputs(" [input.xml]\n", stderr);
746          exit(1);
747   }
748  
# Line 617 | Line 779 | main(int argc, char *argv[])
779                                                  argv[0]);
780                                  return 1;
781                          }
782 <                        if (strstr(argv[i], legal_units) == NULL) {
783 <                                fprintf(stderr, "%s: unit must be one of (%s)\n",
782 >                        if (strstr(legal_units, argv[i]) == NULL) {
783 >                                fprintf(stderr, "%s: -u unit must be one of (%s)\n",
784                                                  argv[0], legal_units);
785                                  return 1;
786                          }
787                          attr_unit = argv[i];
788                          continue;
789 +                case 'U':               /* unlink data files when done */
790 +                        unlink_datafiles = 1 + (argv[i][2] == 'U');
791 +                        continue;
792                  case 'a':               /* angle basis */
793                          if (++i >= argc)
794                                  UsageExit(argv[0]);
# Line 645 | Line 810 | main(int argc, char *argv[])
810                          else
811                                  UsageExit(argv[0]);
812                          continue;
813 +                case 'c':               /* correct solid angle */
814 +                        correct_solid_angle = 1;
815 +                        continue;
816 +                case 'C':               /* comment */
817 +                        if (ncomm >= MAXCOMM) {
818 +                                fprintf(stderr, "%s: too many comments\n",
819 +                                                argv[0]);
820 +                                return 1;
821 +                        }
822 +                        if (strstr(argv[++i], "-->") != NULL) {
823 +                                fprintf(stderr, "%s: illegal character in comment\n",
824 +                                                argv[0]);
825 +                                return 1;
826 +                        }
827 +                        commlist[ncomm++] = argv[i];
828 +                        continue;
829                  case 't':               /* transmission */
830                          if (i >= argc-1)
831                                  UsageExit(argv[0]);
# Line 654 | Line 835 | main(int argc, char *argv[])
835                                  return 1;
836                          }
837                          if (!strcmp(argv[i], "-tf"))
838 <                                data_file[ndataf].type = DTtransForward;
838 >                                data_file[ndataf].type = DTransFront;
839                          else if (!strcmp(argv[i], "-tb"))
840 <                                data_file[ndataf].type = DTtransBackward;
840 >                                data_file[ndataf].type = DTransBack;
841                          else
842                                  UsageExit(argv[0]);
843                          if (!strcmp(argv[++i], "-")) {
# Line 665 | Line 846 | main(int argc, char *argv[])
846                          }
847                          data_file[ndataf].fname = argv[i];
848                          data_file[ndataf].spectrum = cur_spectrum;
849 <                        ndataf++;
849 >                        ndataf += check_new_data_file();
850                          continue;
851                  case 'r':               /* reflection */
852                          if (i >= argc-1)
# Line 676 | Line 857 | main(int argc, char *argv[])
857                                  return 1;
858                          }
859                          if (!strcmp(argv[i], "-rf"))
860 <                                data_file[ndataf].type = DTreflForward;
860 >                                data_file[ndataf].type = DTreflFront;
861                          else if (!strcmp(argv[i], "-rb"))
862 <                                data_file[ndataf].type = DTreflBackward;
862 >                                data_file[ndataf].type = DTreflBack;
863                          else
864                                  UsageExit(argv[0]);
865                          if (!strcmp(argv[++i], "-")) {
# Line 686 | Line 867 | main(int argc, char *argv[])
867                                  argv[i] = (char *)stdin_name;
868                          }
869                          data_file[ndataf].fname = argv[i];
870 <                        data_file[ndataf++].spectrum = cur_spectrum;
870 >                        data_file[ndataf].spectrum = cur_spectrum;
871 >                        ndataf += check_new_data_file();
872                          continue;
873                  case 's':               /* spectrum name or input file */
874                          if (++i >= argc)
# Line 700 | Line 882 | main(int argc, char *argv[])
882                                  cur_spectrum = DSxbar31;
883                          else if (!strcasecmp(argv[i], "CIE-Z"))
884                                  cur_spectrum = DSzbar31;
885 +                        else if (!strcasecmp(argv[i], "CIE-u"))
886 +                                cur_spectrum = DSuprime;
887 +                        else if (!strcasecmp(argv[i], "CIE-v"))
888 +                                cur_spectrum = DSvprime;
889                          else if (!strcasecmp(argv[i], "NIR"))
890                                  cur_spectrum = DSnir;
891                          else {
# Line 735 | Line 921 | main(int argc, char *argv[])
921                  }
922                  break;
923          }
924 < doneOptions:                                    /* get XML input */
924 >                                                /* get XML input */
925          if (i >= argc) {
926                  if (xml_input == NULL)
927                          xml_input = def_template;
# Line 748 | Line 934 | doneOptions:                                   /* get XML input */
934          } else {
935                  xml_input = argv[i];
936          }
751        if ((xml_input == win6_template) & (angle_basis == ABdefault))
752                angle_basis = ABklemsFull;
937                                                  /* wrap it! */
938          return !wrapBSDF(argv[0]);
939   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines