ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/wrapBSDF.c
Revision: 2.4
Committed: Sat Feb 14 00:39:21 2015 UTC (7 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +37 -19 lines
Log Message:
More fixes and indentation/alignment of XML tags

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.4 static const char RCSid[] = "$Id: wrapBSDF.c,v 2.3 2015/02/13 20:49:59 greg Exp $";
3 greg 2.1 #endif
4     /*
5 greg 2.2 * Wrap BSDF data in valid WINDOW XML file
6 greg 2.1 *
7 greg 2.2 * G. Ward February 2015
8 greg 2.1 */
9    
10     #include <ctype.h>
11     #include "rtio.h"
12     #include "rtprocess.h"
13     #include "ezxml.h"
14     #include "bsdf.h"
15     #include "bsdf_m.h"
16     /* XML template file names */
17     const char def_template[] = "minimalBSDFt.xml";
18 greg 2.3 const char win6_template[] = "WINDOW6BSDFt.xml";
19 greg 2.1
20     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 greg 2.2 const char *attr_unit = "meter";
26 greg 2.1 const char legal_units[] = "meter|foot|inch|centimeter|millimeter";
27     /* system materials & geometry */
28     const char *mgf_geometry = NULL;
29    
30     /* angle basis */
31     enum { ABdefault=-1, ABklemsFull=0, ABklemsHalf, ABklemsQuarter,
32     ABtensorTree3, ABtensorTree4, ABend };
33    
34     int angle_basis = ABdefault;
35    
36     /* field IDs and nicknames */
37     struct s_fieldID {
38     char nickName[4];
39 greg 2.3 short has_unit;
40     short win_need;
41 greg 2.1 const char *fullName;
42     } XMLfieldID[] = {
43 greg 2.3 {"m", 0, 1, "Manufacturer"},
44     {"n", 0, 1, "Name"},
45     {"c", 0, 0, "ThermalConductivity"},
46     {"ef", 0, 0, "EmissivityFront"},
47     {"eb", 0, 0, "EmissivityBack"},
48     {"tir", 0, 0, "TIR"},
49     {"eo", 0, 0, "EffectiveOpennessFraction"},
50     {"t", 1, 1, "Thickness"},
51     {"h", 1, 0, "Height"},
52     {"w", 1, 0, "Width"},
53     {"\0", 0, 0, NULL} /* terminator */
54 greg 2.1 };
55     /* field assignments */
56     #define MAXASSIGN 12
57     const char *field_assignment[MAXASSIGN];
58     int nfield_assign = 0;
59     #define FASEP ';'
60    
61     /* data file(s) & spectra */
62     enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward };
63    
64     enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 };
65    
66     #define MAXFILES 20
67    
68     struct s_dfile {
69     const char *fname; /* input data file name */
70     short type; /* BSDF data type */
71     short spectrum; /* BSDF sensor spectrum */
72     } data_file[MAXFILES];
73    
74     int ndataf = 0; /* number of data files */
75    
76     const char *spectr_file[MAXFILES]; /* custom spectral curve input */
77    
78     const char top_level_name[] = "WindowElement";
79    
80 greg 2.3 static char basis_definition[][256] = {
81 greg 2.1
82 greg 2.4 "\t<DataDefinition>\n"
83 greg 2.1 "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
84     "\t\t<AngleBasis>\n"
85     "\t\t\t<AngleBasisName>LBNL/Klems Full</AngleBasisName>\n"
86 greg 2.4 "\t\t\t</AngleBasis>\n"
87     "\t</DataDefinition>\n",
88 greg 2.1
89 greg 2.4 "\t<DataDefinition>\n"
90 greg 2.1 "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
91     "\t\t<AngleBasis>\n"
92     "\t\t\t<AngleBasisName>LBNL/Klems Half</AngleBasisName>\n"
93 greg 2.4 "\t\t\t</AngleBasis>\n"
94     "\t</DataDefinition>\n",
95 greg 2.1
96 greg 2.4 "\t<DataDefinition>\n"
97 greg 2.1 "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
98     "\t\t<AngleBasis>\n"
99     "\t\t\t<AngleBasisName>LBNL/Klems Quarter</AngleBasisName>\n"
100 greg 2.4 "\t\t\t</AngleBasis>\n"
101     "\t</DataDefinition>\n",
102 greg 2.1
103 greg 2.4 "\t<DataDefinition>\n"
104     "\t\t<IncidentDataStructure>TensorTree3</IncidentDataStructure>\n"
105     "\t</DataDefinition>\n",
106    
107     "\t<DataDefinition>\n"
108     "\t\t<IncidentDataStructure>TensorTree4</IncidentDataStructure>\n"
109     "\t</DataDefinition>\n",
110 greg 2.1 };
111    
112     /* Copy data from file descriptor to stdout and close */
113     static int
114     copy_and_close(int fd)
115     {
116     int ok = 1;
117     char buf[8192];
118     int n;
119    
120     if (fd < 0)
121     return 0;
122     while ((n = read(fd, buf, sizeof(buf))) > 0)
123     if (write(fileno(stdout), buf, n) != n) {
124     ok = 0;
125     break;
126     }
127     ok &= (n == 0);
128     close(fd);
129     return ok;
130     }
131    
132     /* Allocate and assign string from file or stream */
133     static char *
134     input2str(const char *inpspec)
135     {
136     FILE *fp = NULL;
137     char *str;
138     int len, pos, n;
139    
140     if (inpspec == NULL || !*inpspec)
141     return "";
142     if (inpspec == stdin_name) { /* read from stdin */
143     fp = stdin;
144 greg 2.2 } else if (inpspec[0] == '!') { /* read from command */
145 greg 2.1 fp = popen(inpspec+1, "r");
146     if (fp == NULL) {
147     fprintf(stderr, "Cannot start process '%s'\n",
148 greg 2.2 inpspec);
149 greg 2.1 return "";
150     }
151     } else { /* else load file */
152     int fd = open(inpspec, O_RDONLY);
153     if (fd < 0) {
154     fprintf(stderr, "%s: cannot open\n", inpspec);
155     return "";
156     }
157     len = lseek(fd, 0L, SEEK_END);
158 greg 2.3 if (len > 0) {
159     lseek(fd, 0L, SEEK_SET);
160     str = (char *)malloc(len+1);
161     if (str == NULL) {
162     close(fd);
163     goto memerr;
164     }
165     if (read(fd, str, len) != len) {
166     fprintf(stderr, "%s: read error\n", inpspec);
167     free(str);
168     close(fd);
169     return "";
170     }
171     str[len] = '\0';
172 greg 2.1 close(fd);
173 greg 2.3 return str;
174 greg 2.1 }
175 greg 2.3 fp = fdopen(fd, "r"); /* not a regular file */
176 greg 2.1 }
177     /* reading from stream */
178     str = (char *)malloc((len=8192)+1);
179     if (str == NULL)
180     goto memerr;
181     pos = 0;
182     while ((n = read(fileno(fp), str+pos, len-pos)) > 0)
183     if ((pos += n) >= len) { /* need more space? */
184     str = (char *)realloc(str, (len += len>>2) + 1);
185     if (str == NULL)
186     goto memerr;
187     }
188     if (n < 0) {
189     fprintf(stderr, "%s: read error\n", inpspec);
190     free(str);
191     str = "";
192     } else { /* tidy up result */
193     str[pos] = '\0';
194     str = (char *)realloc(str, (len=pos)+1);
195     if (str == NULL)
196     goto memerr;
197     }
198 greg 2.2 if (inpspec[0] != '!')
199 greg 2.1 fclose(fp);
200     else if (pclose(fp))
201 greg 2.2 fprintf(stderr, "Error running command '%s'\n", inpspec);
202 greg 2.1 return str;
203     memerr:
204     fprintf(stderr, "%s: error allocating memory\n", inpspec);
205     if (fp != NULL)
206 greg 2.2 (inpspec[0] == '!') ? pclose(fp) : fclose(fp);
207 greg 2.1 return "";
208     }
209    
210     /* Make material assignments in field_assignment to XML fields */
211     static int
212     mat_assignments(const char *caller, const char *fn, ezxml_t wtl)
213     {
214     int i;
215    
216     wtl = ezxml_child(wtl, "Material");
217     if (wtl == NULL) {
218     fprintf(stderr, "%s: missing <Material> tag\n", fn);
219     return 0;
220     }
221     for (i = 0; i < nfield_assign; i++) {
222     const char *fnext = field_assignment[i];
223     for ( ; ; ) {
224     ezxml_t fld;
225     char sbuf[512];
226     int j;
227    
228     while (isspace(*fnext))
229     ++fnext;
230     if (!*fnext)
231     break;
232 greg 2.3 for (j = 0; *fnext != '=' && !isspace(*fnext); ) {
233 greg 2.1 if (!*fnext | (*fnext == FASEP) |
234     (j >= sizeof(sbuf)-1)) {
235     fprintf(stderr,
236     "%s: bad tag name in assignment '%s'\n",
237     caller, field_assignment[i]);
238     return 0;
239     }
240     sbuf[j++] = *fnext++;
241     }
242 greg 2.2 sbuf[j] = '\0'; /* check known field */
243 greg 2.1 for (j = 0; XMLfieldID[j].nickName[0]; j++)
244 greg 2.2 if (!strcasecmp(sbuf, XMLfieldID[j].nickName) ||
245     !strcasecmp(sbuf, XMLfieldID[j].fullName)) {
246 greg 2.1 strcpy(sbuf, XMLfieldID[j].fullName);
247     break;
248     }
249     /* check if tag exists */
250     fld = ezxml_child(wtl, sbuf);
251     if (fld == NULL) { /* otherwise, create one */
252 greg 2.3 if (!XMLfieldID[j].nickName[0])
253     fprintf(stderr,
254     "%s: warning - adding tag <%s>\n",
255     fn, sbuf);
256 greg 2.1 fld = ezxml_add_child_d(wtl, sbuf, strlen(wtl->txt));
257     }
258     if (XMLfieldID[j].has_unit)
259     ezxml_set_attr(fld, "unit", attr_unit);
260 greg 2.3 XMLfieldID[j].win_need = 0;
261 greg 2.1 while (isspace(*fnext))
262     ++fnext;
263     if (*fnext++ != '=') {
264     fprintf(stderr,
265     "%s: missing '=' in assignment '%s'\n",
266     caller, field_assignment[i]);
267     return 0;
268     }
269 greg 2.3 for (j = 0; *fnext && *fnext != FASEP; ) {
270 greg 2.1 if (j >= sizeof(sbuf)-1) {
271     fprintf(stderr,
272     "%s: field too long in '%s'\n",
273     caller, field_assignment[i]);
274     return 0;
275     }
276     sbuf[j++] = *fnext++;
277     }
278     sbuf[j] = '\0';
279     ezxml_set_txt_d(fld, sbuf);
280     fnext += (*fnext == FASEP);
281     }
282     }
283 greg 2.3 /* check required WINDOW settings */
284     if (xml_input == win6_template)
285     for (i = 0; XMLfieldID[i].nickName[0]; i++)
286     if (XMLfieldID[i].win_need &&
287     !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) {
288     fprintf(stderr,
289     "%s: missing required '%s' assignment for WINDOW <%s>\n",
290     caller, XMLfieldID[i].nickName,
291     XMLfieldID[i].fullName);
292     return 0;
293     }
294 greg 2.1 return 1; /* no errors */
295     }
296    
297     /* Complete angle basis specification */
298     static int
299     finish_angle_basis(ezxml_t ab)
300     {
301     const char *bn = ezxml_txt(ezxml_child(ab, "AngleBasisName"));
302     int i, n = nabases;
303     char buf[32];
304    
305     if (!*bn) {
306     fputs("Internal error - missing <AngleBasisName>!\n", stderr);
307     return 0;
308     }
309     while (n-- > 0)
310     if (!strcasecmp(bn, abase_list[n].name))
311     break;
312     if (n < 0) {
313     fprintf(stderr, "Internal error - unknown angle basis '%s'", bn);
314     return 0;
315     }
316     for (i = 0; abase_list[n].lat[i].nphis; i++) {
317     ezxml_t tb, abb = ezxml_add_child(ab, "AngleBasisBlock",
318     strlen(ab->txt));
319     sprintf(buf, "%g", i ?
320     .5*(abase_list[n].lat[i].tmin + abase_list[n].lat[i+1].tmin) :
321     .0);
322 greg 2.4 ezxml_add_txt(abb, "\n\t\t\t\t");
323 greg 2.1 ezxml_set_txt_d(ezxml_add_child(abb,"Theta",strlen(abb->txt)), buf);
324     sprintf(buf, "%d", abase_list[n].lat[i].nphis);
325 greg 2.4 ezxml_add_txt(abb, "\n\t\t\t\t");
326 greg 2.1 ezxml_set_txt_d(ezxml_add_child(abb,"nPhis",strlen(abb->txt)), buf);
327 greg 2.4 ezxml_add_txt(abb, "\n\t\t\t\t");
328 greg 2.1 tb = ezxml_add_child(abb, "ThetaBounds", strlen(abb->txt));
329 greg 2.4 ezxml_add_txt(tb, "\n\t\t\t\t\t");
330 greg 2.1 sprintf(buf, "%g", abase_list[n].lat[i].tmin);
331 greg 2.4 ezxml_set_txt_d(ezxml_add_child(tb,"LowerTheta",strlen(tb->txt)), buf);
332     ezxml_add_txt(tb, "\n\t\t\t\t\t");
333 greg 2.1 sprintf(buf, "%g", abase_list[n].lat[i+1].tmin);
334 greg 2.4 ezxml_set_txt_d(ezxml_add_child(tb,"UpperTheta",strlen(tb->txt)), buf);
335     ezxml_add_txt(tb, "\n\t\t\t\t");
336     ezxml_add_txt(abb, "\n\t\t\t");
337     ezxml_add_txt(ab, "\n\t\t\t");
338 greg 2.1 }
339     return 1;
340     }
341    
342     /* Determine our angle basis from current tags */
343     static int
344     determine_angle_basis(const char *fn, ezxml_t wtl)
345     {
346     const char *ids;
347     int i;
348    
349     wtl = ezxml_child(wtl, "DataDefinition");
350     if (wtl == NULL)
351     return -1;
352     ids = ezxml_txt(ezxml_child(wtl, "IncidentDataStructure"));
353 greg 2.4 if (!ids[0])
354 greg 2.1 return -1;
355     for (i = 0; i < ABend; i++) {
356     ezxml_t parsed = ezxml_parse_str(basis_definition[i],
357     strlen(basis_definition[i]));
358     int match = 0;
359     if (!strcmp(ids, ezxml_txt(ezxml_child(parsed,
360     "IncidentDataStructure")))) {
361     const char *abn0 = ezxml_txt(
362     ezxml_child(ezxml_child(wtl,
363     "AngleBasis"), "AngleBasisName"));
364     const char *abn1 = ezxml_txt(
365     ezxml_child(ezxml_child(parsed,
366     "AngleBasis"), "AngleBasisName"));
367     match = !strcmp(abn0, abn1);
368     }
369     ezxml_free(parsed);
370     if (match)
371     return i;
372     }
373     return -1;
374     }
375    
376     /* Write out BSDF data block with surrounding tags */
377     static int
378     writeBSDFblock(const char *caller, struct s_dfile *df)
379     {
380     char *cp;
381    
382     puts("\t<WavelengthData>");
383     puts("\t\t<LayerNumber>System</LayerNumber>");
384     switch (df->spectrum) {
385     case DSvisible:
386     puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
387     puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
388     puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
389     break;
390     case DSxbar31:
391     puts("\t\t<Wavelength unit=\"Integral\">CIE-X</Wavelength>");
392     puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
393     puts("\t\t<DetectorSpectrum>ASTM E308 1931 X.dsp</DetectorSpectrum>");
394     break;
395     case DSzbar31:
396     puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</Wavelength>");
397     puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
398     puts("\t\t<DetectorSpectrum>ASTM E308 1931 Z.dsp</DetectorSpectrum>");
399     break;
400     case DSsolar:
401     puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>");
402     puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
403     puts("\t\t<DetectorSpectrum>None</DetectorSpectrum>");
404     break;
405     case DSnir:
406     puts("\t\t<Wavelength unit=\"Integral\">NIR</Wavelength>");
407     puts("\t\tSourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
408     puts("\t\t<DetectorSpectrum>PLACE_HOLDER</DetectorSpectrum>");
409     break;
410     default:
411     cp = strrchr(spectr_file[df->spectrum], '.');
412     if (cp != NULL)
413     *cp = '\0';
414     printf("\t\t<Wavelength unit=\"Integral\">%s</Wavelength>\n",
415     spectr_file[df->spectrum]);
416     if (cp != NULL)
417     *cp = '.';
418     puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
419     printf("\t\t<DetectorSpectrum>%s</DetectorSpectrum>\n",
420     spectr_file[df->spectrum]);
421     break;
422     }
423     puts("\t\t<WavelengthDataBlock>");
424     fputs("\t\t\t<WavelengthDataDirection>", stdout);
425     switch (df->type) {
426     case DTtransForward:
427     fputs("Transmission Front", stdout);
428     break;
429     case DTtransBackward:
430     fputs("Transmission Back", stdout);
431     break;
432     case DTreflForward:
433     fputs("Reflection Front", stdout);
434     break;
435     case DTreflBackward:
436     fputs("Reflection Back", stdout);
437     break;
438     default:
439     fprintf(stderr, "%s: internal - bad BSDF type (%d)\n", caller, df->type);
440     return 0;
441     }
442     puts("</WavelengthDataDirection>");
443     switch (angle_basis) {
444     case ABklemsFull:
445     puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>");
446     break;
447     case ABklemsHalf:
448     puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Half</ColumnAngleBasis>");
449     break;
450     case ABklemsQuarter:
451     puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Quarter</ColumnAngleBasis>");
452     break;
453     case ABtensorTree3:
454     case ABtensorTree4:
455     puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
456     break;
457     default:
458     fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis);
459     return 0;
460     }
461     puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
462     puts("\t\t\t<ScatteringData>");
463     fflush(stdout);
464     if (df->fname == stdin_name) {
465     copy_and_close(fileno(stdin));
466 greg 2.2 } else if (df->fname[0] != '!') {
467 greg 2.1 if (!copy_and_close(open(df->fname, O_RDONLY))) {
468     fprintf(stderr, "%s: error reading from '%s'\n",
469     caller, df->fname);
470     return 0;
471     }
472     } else if (system(df->fname+1)) {
473 greg 2.2 fprintf(stderr, "%s: error running '%s'\n", caller, df->fname);
474 greg 2.1 return 0;
475     }
476     puts("\t\t\t</ScatteringData>");
477     puts("\t\t</WavelengthDataBlock>");
478     puts("\t</WavelengthData>");
479     return 1;
480     }
481    
482     /* Write out XML, interpolating BSDF data block(s) */
483     static int
484     writeBSDF(const char *caller, ezxml_t fl)
485     {
486 greg 2.2 char *xml = ezxml_toxml(fl); /* store XML in string */
487 greg 2.1 int ei, i;
488     /* locate trailer */
489     for (ei = strlen(xml)-strlen("</Layer></Optical></WindowElement>");
490     ei >= 0; ei--)
491     if (!strncmp(xml+ei, "</Layer>", 8))
492     break;
493     if (ei < 0) {
494     fprintf(stderr, "%s: internal - cannot find trailer\n",
495     caller);
496     free(xml);
497     return 0;
498     }
499     fflush(stdout); /* write previous XML info. */
500     if (write(fileno(stdout), xml, ei) != ei) {
501     free(xml);
502     return 0;
503     }
504     for (i = 0; i < ndataf; i++) /* interpolate new data */
505     if (!writeBSDFblock(caller, &data_file[i])) {
506     free(xml);
507     return 0;
508     }
509     fputs(xml+ei, stdout); /* write trailer */
510 greg 2.2 free(xml); /* free string */
511 greg 2.4 fputc('\n', stdout);
512 greg 2.1 return (fflush(stdout) == 0);
513     }
514    
515     /* Insert BSDF data into XML wrapper */
516     static int
517     wrapBSDF(const char *caller)
518     {
519 greg 2.2 const char *xml_path = xml_input;
520 greg 2.1 ezxml_t fl, wtl;
521     /* load previous XML/template */
522 greg 2.2 if (xml_input == stdin_name) {
523     fl = ezxml_parse_fp(stdin);
524     } else if (xml_input[0] == '!') {
525     FILE *pfp = popen(xml_input+1, "r");
526     if (pfp == NULL) {
527     fprintf(stderr, "%s: cannot start process '%s'\n",
528     caller, xml_input);
529     return 0;
530     }
531     fl = ezxml_parse_fp(pfp);
532     if (pclose(pfp)) {
533     fprintf(stderr, "%s: error running '%s'\n",
534     caller, xml_input);
535     return 0;
536     }
537     } else {
538     xml_path = getpath((char *)xml_input, getrlibpath(), R_OK);
539     if (xml_path == NULL) {
540     fprintf(stderr, "%s: cannot find XML file named '%s'\n",
541     caller, xml_input==NULL ? "NULL" : xml_input);
542     return 0;
543     }
544     fl = ezxml_parse_file(xml_path);
545 greg 2.1 }
546     if (fl == NULL) {
547 greg 2.2 fprintf(stderr, "%s: cannot load XML '%s'\n", caller, xml_path);
548 greg 2.1 return 0;
549     }
550     if (ezxml_error(fl)[0]) {
551 greg 2.2 fprintf(stderr, "%s: error in XML '%s': %s\n", caller, xml_path,
552 greg 2.1 ezxml_error(fl));
553     goto failure;
554     }
555     if (strcmp(ezxml_name(fl), top_level_name)) {
556     fprintf(stderr, "%s: top level in XML '%s' not '%s'\n",
557     caller, xml_path, top_level_name);
558     goto failure;
559     }
560     wtl = ezxml_child(fl, "FileType");
561     if (wtl != NULL && strcmp(ezxml_txt(wtl), "BSDF")) {
562     fprintf(stderr, "%s: wrong FileType in XML '%s' (must be 'BSDF')",
563     caller, xml_path);
564     goto failure;
565     }
566     wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
567     if (wtl == NULL) {
568     fprintf(stderr, "%s: no optical layers in XML '%s'",
569     caller, xml_path);
570     goto failure;
571     }
572     /* make material assignments */
573 greg 2.3 if (!mat_assignments(caller, xml_path, wtl))
574 greg 2.1 goto failure;
575     if (mgf_geometry != NULL) { /* add geometry if specified */
576     ezxml_t geom = ezxml_child(wtl, "Geometry");
577     if (geom == NULL)
578 greg 2.3 geom = ezxml_add_child(wtl, "Geometry", strlen(wtl->txt));
579 greg 2.1 ezxml_set_attr(geom, "format", "MGF");
580     geom = ezxml_child(geom, "MGFblock");
581     if (geom == NULL) {
582     geom = ezxml_child(wtl, "Geometry");
583 greg 2.3 geom = ezxml_add_child(geom, "MGFblock", 0);
584 greg 2.1 }
585     ezxml_set_attr(geom, "unit", attr_unit);
586     ezxml_set_txt(geom, input2str(mgf_geometry));
587     if (geom->txt[0])
588     ezxml_set_flag(geom, EZXML_TXTM);
589     }
590     /* check basis */
591     if (angle_basis != ABdefault) {
592 greg 2.4 size_t offset = 0;
593 greg 2.1 ezxml_t ab, dd = ezxml_child(wtl, "DataDefinition");
594 greg 2.4 if (dd != NULL) {
595     offset = dd->off;
596 greg 2.3 fprintf(stderr,
597 greg 2.1 "%s: warning - replacing existing <DataDefinition> in '%s'\n",
598     caller, xml_path);
599 greg 2.4 ezxml_remove(dd);
600     } else
601     offset = strlen(wtl->txt);
602     dd = ezxml_insert(ezxml_parse_str(basis_definition[angle_basis],
603 greg 2.1 strlen(basis_definition[angle_basis])),
604 greg 2.4 wtl, offset);
605 greg 2.1 if ((ab = ezxml_child(dd, "AngleBasis")) != NULL &&
606     !finish_angle_basis(ab))
607     goto failure;
608     } else if ((angle_basis = determine_angle_basis(xml_path, wtl)) < 0) {
609     fprintf(stderr, "%s: need -a option to set angle basis\n",
610     caller);
611     goto failure;
612     }
613     /* write & add BSDF data blocks */
614     if (!writeBSDF(caller, fl))
615     goto failure;
616     ezxml_free(fl); /* all done */
617     return 1;
618     failure:
619     ezxml_free(fl);
620     return 0;
621     }
622    
623     /* Report usage and exit */
624     static void
625     UsageExit(const char *pname)
626     {
627     fputs("Usage: ", stderr);
628     fputs(pname, stderr);
629 greg 2.2 fputs(" [-W][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr);
630     fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp]", stderr);
631     fputs(" [input.xml]\n", stderr);
632 greg 2.1 exit(1);
633     }
634    
635     /* Load XML file and use to wrap BSDF data (or modify fields) */
636     int
637     main(int argc, char *argv[])
638     {
639     int cur_spectrum = DSvisible;
640     int ncust_spec = 0;
641     int used_stdin = 0;
642     int units_set = 0;
643     int i;
644     /* get/check arguments */
645     for (i = 1; i < argc && argv[i][0] == '-'; i++) {
646     switch (argv[i][1]) {
647     case 'W': /* customize for WINDOW 6 output */
648     xml_input = win6_template;
649     continue;
650     case 'f': /* field assignment(s) */
651     if (++i >= argc)
652     UsageExit(argv[0]);
653     if (nfield_assign >= MAXASSIGN) {
654     fprintf(stderr, "%s: too many -f options",
655     argv[0]);
656     return 1;
657     }
658     field_assignment[nfield_assign++] = argv[i];
659     continue;
660     case 'u': /* unit */
661     if (++i >= argc)
662     UsageExit(argv[0]);
663     if (units_set++) {
664     fprintf(stderr, "%s: only one -u option allowed\n",
665     argv[0]);
666     return 1;
667     }
668 greg 2.2 if (strstr(legal_units, argv[i]) == NULL) {
669     fprintf(stderr, "%s: -u unit must be one of (%s)\n",
670 greg 2.1 argv[0], legal_units);
671     return 1;
672     }
673     attr_unit = argv[i];
674     continue;
675     case 'a': /* angle basis */
676     if (++i >= argc)
677     UsageExit(argv[0]);
678     if (angle_basis != ABdefault) {
679     fprintf(stderr, "%s: only one -a option allowed\n",
680     argv[0]);
681     return 1;
682     }
683     if (!strcasecmp(argv[i], "kf"))
684     angle_basis = ABklemsFull;
685     else if (!strcasecmp(argv[i], "kh"))
686     angle_basis = ABklemsHalf;
687     else if (!strcasecmp(argv[i], "kq"))
688     angle_basis = ABklemsQuarter;
689     else if (!strcasecmp(argv[i], "t3"))
690     angle_basis = ABtensorTree3;
691     else if (!strcasecmp(argv[i], "t4"))
692     angle_basis = ABtensorTree4;
693     else
694     UsageExit(argv[0]);
695     continue;
696     case 't': /* transmission */
697     if (i >= argc-1)
698     UsageExit(argv[0]);
699     if (ndataf >= MAXFILES) {
700     fprintf(stderr, "%s: too many data files\n",
701     argv[0]);
702     return 1;
703     }
704     if (!strcmp(argv[i], "-tf"))
705     data_file[ndataf].type = DTtransForward;
706     else if (!strcmp(argv[i], "-tb"))
707     data_file[ndataf].type = DTtransBackward;
708     else
709     UsageExit(argv[0]);
710     if (!strcmp(argv[++i], "-")) {
711     if (used_stdin++) UsageExit(argv[i]);
712     argv[i] = (char *)stdin_name;
713     }
714     data_file[ndataf].fname = argv[i];
715     data_file[ndataf].spectrum = cur_spectrum;
716     ndataf++;
717     continue;
718     case 'r': /* reflection */
719     if (i >= argc-1)
720     UsageExit(argv[0]);
721     if (ndataf >= MAXFILES) {
722     fprintf(stderr, "%s: too many data files\n",
723     argv[0]);
724     return 1;
725     }
726     if (!strcmp(argv[i], "-rf"))
727     data_file[ndataf].type = DTreflForward;
728     else if (!strcmp(argv[i], "-rb"))
729     data_file[ndataf].type = DTreflBackward;
730     else
731     UsageExit(argv[0]);
732     if (!strcmp(argv[++i], "-")) {
733     if (used_stdin++) UsageExit(argv[i]);
734     argv[i] = (char *)stdin_name;
735     }
736     data_file[ndataf].fname = argv[i];
737     data_file[ndataf++].spectrum = cur_spectrum;
738     continue;
739     case 's': /* spectrum name or input file */
740     if (++i >= argc)
741     UsageExit(argv[0]);
742     if (!strcasecmp(argv[i], "Solar"))
743     cur_spectrum = DSsolar;
744     else if (!strcasecmp(argv[i], "Visible") ||
745     !strcasecmp(argv[i], "CIE-Y"))
746     cur_spectrum = DSvisible;
747     else if (!strcasecmp(argv[i], "CIE-X"))
748     cur_spectrum = DSxbar31;
749     else if (!strcasecmp(argv[i], "CIE-Z"))
750     cur_spectrum = DSzbar31;
751     else if (!strcasecmp(argv[i], "NIR"))
752     cur_spectrum = DSnir;
753     else {
754     if (!strcmp(argv[i], "-")) {
755     fprintf(stderr,
756     "%s: cannot read spectra from stdin",
757     argv[0]);
758     return 1;
759     }
760     cur_spectrum = ncust_spec;
761     spectr_file[ncust_spec++] = argv[i];
762     }
763     continue;
764     case 'g': /* MGF geometry file */
765     if (i >= argc-1)
766     UsageExit(argv[0]);
767     if (mgf_geometry != NULL) {
768     fprintf(stderr, "%s: only one -g option allowed\n",
769     argv[0]);
770     return 1;
771     }
772     if (!strcmp(argv[++i], "-")) {
773     if (used_stdin++) UsageExit(argv[i]);
774     argv[i] = (char *)stdin_name;
775     }
776     mgf_geometry = argv[i];
777     continue;
778     case '\0': /* input XML from stdin */
779     break;
780     default:
781     UsageExit(argv[0]);
782     break;
783     }
784     break;
785     }
786     doneOptions: /* get XML input */
787     if (i >= argc) {
788     if (xml_input == NULL)
789     xml_input = def_template;
790     } else if ((i < argc-1) | (xml_input != NULL)) {
791     fprintf(stderr, "%s: only one XML input allowed\n", argv[0]);
792     UsageExit(argv[0]);
793     } else if (!strcmp(argv[i], "-")) {
794     if (used_stdin++) UsageExit(argv[0]);
795     xml_input = stdin_name;
796     } else {
797     xml_input = argv[i];
798     }
799     if ((xml_input == win6_template) & (angle_basis == ABdefault))
800     angle_basis = ABklemsFull;
801     /* wrap it! */
802     return !wrapBSDF(argv[0]);
803     }