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" |
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 */ |
26 |
|
const char legal_units[] = "meter|foot|inch|centimeter|millimeter"; |
27 |
|
/* system materials & geometry */ |
28 |
|
const char *mgf_geometry = NULL; |
29 |
< |
|
29 |
> |
/* comment list */ |
30 |
> |
#define MAXCOMM 30 |
31 |
> |
const char *commlist[MAXCOMM]; |
32 |
> |
int ncomm = 0; |
33 |
|
/* angle bases */ |
34 |
|
enum { ABdefault=-1, ABklemsFull=0, ABklemsHalf, ABklemsQuarter, |
35 |
|
ABtensorTree3, ABtensorTree4, ABend }; |
36 |
|
|
37 |
|
int angle_basis = ABdefault; |
38 |
|
|
39 |
+ |
int correct_solid_angle = 0; |
40 |
+ |
|
41 |
+ |
const char *klems_basis_name[] = { |
42 |
+ |
"LBNL/Klems Full", |
43 |
+ |
"LBNL/Klems Half", |
44 |
+ |
"LBNL/Klems Quarter", |
45 |
+ |
}; |
46 |
|
/* field IDs and nicknames */ |
47 |
|
struct s_fieldID { |
48 |
|
char nickName[4]; |
52 |
|
} XMLfieldID[] = { |
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"}, |
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 ';' |
60 |
– |
|
71 |
|
/* data file(s) & spectra */ |
72 |
|
enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward }; |
73 |
|
|
74 |
< |
enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 }; |
74 |
> |
enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5, |
75 |
> |
DSuprime=-6, DSvprime=-7 }; |
76 |
|
|
77 |
|
#define MAXFILES 20 |
78 |
|
|
84 |
|
|
85 |
|
int ndataf = 0; /* number of data files */ |
86 |
|
|
87 |
+ |
int unlink_datafiles = 0; /* unlink data files when done */ |
88 |
+ |
|
89 |
|
const char *spectr_file[MAXFILES]; /* custom spectral curve input */ |
90 |
|
|
91 |
|
const char top_level_name[] = "WindowElement"; |
122 |
|
"\t</DataDefinition>\n", |
123 |
|
}; |
124 |
|
|
125 |
+ |
/* Check that the last-added data file is unique */ |
126 |
+ |
static int |
127 |
+ |
check_new_data_file() |
128 |
+ |
{ |
129 |
+ |
int i = ndataf; |
130 |
+ |
|
131 |
+ |
while (i-- > 0) |
132 |
+ |
if ((data_file[i].spectrum == data_file[ndataf].spectrum) & |
133 |
+ |
(data_file[i].type == data_file[ndataf].type)) { |
134 |
+ |
fprintf(stderr, |
135 |
+ |
"%s: warning - ignoring duplicate component\n", |
136 |
+ |
data_file[ndataf].fname); |
137 |
+ |
return 0; |
138 |
+ |
} |
139 |
+ |
return 1; |
140 |
+ |
} |
141 |
+ |
|
142 |
|
/* Copy data from file descriptor to stdout and close */ |
143 |
|
static int |
144 |
|
copy_and_close(int fd) |
184 |
|
fprintf(stderr, "%s: cannot open\n", inpspec); |
185 |
|
return ""; |
186 |
|
} |
187 |
+ |
#if !defined(_WIN32) && !defined(_WIN64) |
188 |
+ |
/* XXX somehow broken on Windows */ |
189 |
|
len = lseek(fd, 0L, SEEK_END); |
190 |
|
if (len > 0) { |
191 |
|
lseek(fd, 0L, SEEK_SET); |
204 |
|
close(fd); |
205 |
|
return str; |
206 |
|
} |
207 |
+ |
#endif |
208 |
|
fp = fdopen(fd, "r"); /* not a regular file */ |
209 |
|
} |
210 |
|
/* reading from stream */ |
322 |
|
if (xml_input == win6_template) |
323 |
|
for (i = 0; XMLfieldID[i].nickName[0]; i++) |
324 |
|
if (XMLfieldID[i].win_need && |
325 |
< |
!ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) { |
325 |
> |
!ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) |
326 |
|
fprintf(stderr, |
327 |
< |
"%s: missing required '%s' assignment for WINDOW <%s>\n", |
327 |
> |
"%s: warning - missing '%s' assignment for WINDOW <%s>\n", |
328 |
|
caller, XMLfieldID[i].nickName, |
329 |
|
XMLfieldID[i].fullName); |
330 |
< |
return 0; |
298 |
< |
} |
299 |
< |
return 1; /* no errors */ |
330 |
> |
return 1; |
331 |
|
} |
332 |
|
|
333 |
|
/* Complete angle basis specification */ |
409 |
|
return -1; |
410 |
|
} |
411 |
|
|
412 |
< |
/* Filter Klems angle basis, applying appropriate solid angle correction */ |
412 |
> |
/* Filter Klems angle basis, factoring out incident projected solid angle */ |
413 |
|
static int |
414 |
|
filter_klems_matrix(FILE *fp) |
415 |
|
{ |
416 |
< |
#define MAX_COLUMNS 145 |
386 |
< |
float col_corr[MAX_COLUMNS]; |
387 |
< |
const char *bn; |
416 |
> |
const char *bn = klems_basis_name[angle_basis]; |
417 |
|
int i, j, n = nabases; |
418 |
|
/* get angle basis */ |
390 |
– |
switch (angle_basis) { |
391 |
– |
case ABklemsFull: bn = "LBNL/Klems Full"; break; |
392 |
– |
case ABklemsHalf: bn = "LBNL/Klems Half"; break; |
393 |
– |
case ABklemsQuarter: bn = "LBNL/Klems Quarter"; break; |
394 |
– |
default: |
395 |
– |
return 0; |
396 |
– |
} |
419 |
|
while (n-- > 0) |
420 |
|
if (!strcasecmp(bn, abase_list[n].name)) |
421 |
|
break; |
422 |
|
if (n < 0) |
423 |
|
return 0; |
402 |
– |
if (abase_list[n].nangles > MAX_COLUMNS) { |
403 |
– |
fprintf(stderr, "Internal error - too many Klems columns!\n"); |
404 |
– |
return 0; |
405 |
– |
} |
406 |
– |
/* get correction factors */ |
407 |
– |
for (j = abase_list[n].nangles; j--; ) |
408 |
– |
col_corr[j] = 1.f / io_getohm(j, &abase_list[n]); |
424 |
|
/* read/correct/write matrix */ |
425 |
|
for (i = 0; i < abase_list[n].nangles; i++) { |
426 |
+ |
const double corr = 1./io_getohm(i, &abase_list[n]); |
427 |
|
for (j = 0; j < abase_list[n].nangles; j++) { |
428 |
|
double d; |
429 |
|
if (fscanf(fp, "%lf", &d) != 1) |
430 |
|
return 0; |
431 |
< |
printf(" %f", d*col_corr[j]); |
431 |
> |
if (d < -1e-3) { |
432 |
> |
fputs("Negative BSDF data!\n", stderr); |
433 |
> |
return 0; |
434 |
> |
} |
435 |
> |
printf(" %.3e", d*corr*(d > 0)); |
436 |
|
} |
437 |
|
fputc('\n', stdout); |
438 |
|
} |
439 |
< |
return 1; |
440 |
< |
#undef MAX_COLUMNS |
439 |
> |
while ((i = getc(fp)) != EOF) |
440 |
> |
if (!isspace(i)) { |
441 |
> |
fputs("Unexpected data past EOF\n", stderr); |
442 |
> |
return 0; |
443 |
> |
} |
444 |
> |
return 1; /* all is good */ |
445 |
|
} |
446 |
|
|
447 |
|
/* Write out BSDF data block with surrounding tags */ |
448 |
|
static int |
449 |
|
writeBSDFblock(const char *caller, struct s_dfile *df) |
450 |
|
{ |
451 |
< |
int klems_data = 0; |
451 |
> |
int correct_klems = correct_solid_angle; |
452 |
|
char *cp; |
453 |
|
|
454 |
|
puts("\t<WavelengthData>"); |
456 |
|
switch (df->spectrum) { |
457 |
|
case DSvisible: |
458 |
|
puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>"); |
459 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
459 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
460 |
|
puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>"); |
461 |
|
break; |
462 |
|
case DSxbar31: |
463 |
|
puts("\t\t<Wavelength unit=\"Integral\">CIE-X</Wavelength>"); |
464 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
464 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
465 |
|
puts("\t\t<DetectorSpectrum>ASTM E308 1931 X.dsp</DetectorSpectrum>"); |
466 |
|
break; |
467 |
|
case DSzbar31: |
468 |
|
puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</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 Z.dsp</DetectorSpectrum>"); |
471 |
|
break; |
472 |
+ |
case DSuprime: |
473 |
+ |
puts("\t\t<Wavelength unit=\"Integral\">CIE-u</Wavelength>"); |
474 |
+ |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
475 |
+ |
break; |
476 |
+ |
case DSvprime: |
477 |
+ |
puts("\t\t<Wavelength unit=\"Integral\">CIE-v</Wavelength>"); |
478 |
+ |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
479 |
+ |
break; |
480 |
|
case DSsolar: |
481 |
|
puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>"); |
482 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
482 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
483 |
|
puts("\t\t<DetectorSpectrum>None</DetectorSpectrum>"); |
484 |
|
break; |
485 |
|
case DSnir: |
486 |
|
puts("\t\t<Wavelength unit=\"Integral\">NIR</Wavelength>"); |
487 |
< |
puts("\t\tSourceSpectrum>PLACE_HOLDER</SourceSpectrum>"); |
487 |
> |
puts("\t\t<SourceSpectrum>PLACE_HOLDER</SourceSpectrum>"); |
488 |
|
puts("\t\t<DetectorSpectrum>PLACE_HOLDER</DetectorSpectrum>"); |
489 |
|
break; |
490 |
|
default: |
495 |
|
spectr_file[df->spectrum]); |
496 |
|
if (cp != NULL) |
497 |
|
*cp = '.'; |
498 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
498 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
499 |
|
printf("\t\t<DetectorSpectrum>%s</DetectorSpectrum>\n", |
500 |
|
spectr_file[df->spectrum]); |
501 |
|
break; |
520 |
|
return 0; |
521 |
|
} |
522 |
|
puts("</WavelengthDataDirection>"); |
491 |
– |
klems_data = 1; |
523 |
|
switch (angle_basis) { |
524 |
|
case ABklemsFull: |
494 |
– |
puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>"); |
495 |
– |
break; |
525 |
|
case ABklemsHalf: |
497 |
– |
puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Half</ColumnAngleBasis>"); |
498 |
– |
break; |
526 |
|
case ABklemsQuarter: |
527 |
< |
puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Quarter</ColumnAngleBasis>"); |
527 |
> |
fputs("\t\t\t<ColumnAngleBasis>", stdout); |
528 |
> |
fputs(klems_basis_name[angle_basis], stdout); |
529 |
> |
puts("</ColumnAngleBasis>"); |
530 |
> |
fputs("\t\t\t<RowAngleBasis>", stdout); |
531 |
> |
fputs(klems_basis_name[angle_basis], stdout); |
532 |
> |
puts("</RowAngleBasis>"); |
533 |
|
break; |
534 |
|
case ABtensorTree3: |
535 |
|
case ABtensorTree4: |
536 |
|
puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>"); |
537 |
< |
klems_data = 0; |
537 |
> |
correct_klems = 0; |
538 |
|
break; |
539 |
|
default: |
540 |
|
fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis); |
543 |
|
puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>"); |
544 |
|
puts("\t\t\t<ScatteringData>"); |
545 |
|
fflush(stdout); |
546 |
< |
if (klems_data) { |
546 |
> |
if (correct_klems) { /* correct Klems matrix data */ |
547 |
|
FILE *fp = stdin; |
548 |
|
if (df->fname[0] == '!') |
549 |
|
fp = popen(df->fname+1, "r"); |
601 |
|
free(xml); |
602 |
|
return 0; |
603 |
|
} |
604 |
+ |
puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
605 |
+ |
for (i = 0; i < ncomm; i++) |
606 |
+ |
printf("<!-- %s -->\n", commlist[i]); |
607 |
|
fflush(stdout); /* write previous XML info. */ |
608 |
|
if (write(fileno(stdout), xml, ei) != ei) { |
609 |
|
free(xml); |
617 |
|
fputs(xml+ei, stdout); /* write trailer */ |
618 |
|
free(xml); /* free string */ |
619 |
|
fputc('\n', stdout); |
620 |
< |
return (fflush(stdout) == 0); |
620 |
> |
if (fflush(stdout) != 0) |
621 |
> |
return 0; |
622 |
> |
/* unlink data files if req. */ |
623 |
> |
for (i = ndataf*(unlink_datafiles != 0); i--; ) |
624 |
> |
if (data_file[i].fname != stdin_name && |
625 |
> |
data_file[i].fname[0] != '!') |
626 |
> |
unlink(data_file[i].fname); |
627 |
> |
if (unlink_datafiles > 1 && mgf_geometry != NULL && |
628 |
> |
mgf_geometry != stdin_name && |
629 |
> |
mgf_geometry[0] != '!') |
630 |
> |
unlink(mgf_geometry); |
631 |
> |
return 1; |
632 |
|
} |
633 |
|
|
634 |
|
/* Insert BSDF data into XML wrapper */ |
746 |
|
{ |
747 |
|
fputs("Usage: ", stderr); |
748 |
|
fputs(pname, stderr); |
749 |
< |
fputs(" [-W][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr); |
750 |
< |
fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp]", stderr); |
749 |
> |
fputs(" [-W][-c][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr); |
750 |
> |
fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp][-C comm]", stderr); |
751 |
|
fputs(" [input.xml]\n", stderr); |
752 |
|
exit(1); |
753 |
|
} |
792 |
|
} |
793 |
|
attr_unit = argv[i]; |
794 |
|
continue; |
795 |
+ |
case 'U': /* unlink data files when done */ |
796 |
+ |
unlink_datafiles = 1 + (argv[i][2] == 'U'); |
797 |
+ |
continue; |
798 |
|
case 'a': /* angle basis */ |
799 |
|
if (++i >= argc) |
800 |
|
UsageExit(argv[0]); |
816 |
|
else |
817 |
|
UsageExit(argv[0]); |
818 |
|
continue; |
819 |
+ |
case 'c': /* correct solid angle */ |
820 |
+ |
correct_solid_angle = 1; |
821 |
+ |
continue; |
822 |
+ |
case 'C': /* comment */ |
823 |
+ |
if (ncomm >= MAXCOMM) { |
824 |
+ |
fprintf(stderr, "%s: too many comments\n", |
825 |
+ |
argv[0]); |
826 |
+ |
return 1; |
827 |
+ |
} |
828 |
+ |
if (strchr(argv[++i], '>') != NULL) { |
829 |
+ |
fprintf(stderr, "%s: illegal character in comment\n", |
830 |
+ |
argv[0]); |
831 |
+ |
return 1; |
832 |
+ |
} |
833 |
+ |
commlist[ncomm++] = argv[i]; |
834 |
+ |
continue; |
835 |
|
case 't': /* transmission */ |
836 |
|
if (i >= argc-1) |
837 |
|
UsageExit(argv[0]); |
852 |
|
} |
853 |
|
data_file[ndataf].fname = argv[i]; |
854 |
|
data_file[ndataf].spectrum = cur_spectrum; |
855 |
< |
ndataf++; |
855 |
> |
ndataf += check_new_data_file(); |
856 |
|
continue; |
857 |
|
case 'r': /* reflection */ |
858 |
|
if (i >= argc-1) |
873 |
|
argv[i] = (char *)stdin_name; |
874 |
|
} |
875 |
|
data_file[ndataf].fname = argv[i]; |
876 |
< |
data_file[ndataf++].spectrum = cur_spectrum; |
876 |
> |
data_file[ndataf].spectrum = cur_spectrum; |
877 |
> |
ndataf++; |
878 |
|
continue; |
879 |
|
case 's': /* spectrum name or input file */ |
880 |
|
if (++i >= argc) |
888 |
|
cur_spectrum = DSxbar31; |
889 |
|
else if (!strcasecmp(argv[i], "CIE-Z")) |
890 |
|
cur_spectrum = DSzbar31; |
891 |
+ |
else if (!strcasecmp(argv[i], "CIE-u")) |
892 |
+ |
cur_spectrum = DSuprime; |
893 |
+ |
else if (!strcasecmp(argv[i], "CIE-v")) |
894 |
+ |
cur_spectrum = DSvprime; |
895 |
|
else if (!strcasecmp(argv[i], "NIR")) |
896 |
|
cur_spectrum = DSnir; |
897 |
|
else { |
940 |
|
} else { |
941 |
|
xml_input = argv[i]; |
942 |
|
} |
873 |
– |
if ((xml_input == win6_template) & (angle_basis == ABdefault)) |
874 |
– |
angle_basis = ABklemsFull; |
943 |
|
/* wrap it! */ |
944 |
|
return !wrapBSDF(argv[0]); |
945 |
|
} |