ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/wrapBSDF.c
Revision: 2.5
Committed: Sat Feb 14 18:33:07 2015 UTC (7 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +8 -2 lines
Log Message:
Further tidying up of output

File Contents

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