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