1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006
|
/*
* Argyll Color Management System
*
* Gretag i1Pro implementation functions
*/
/*
* Author: Graeme W. Gill
* Date: 14/9/2020
*
* Copyright 2006 - 2021 Graeme W. Gill
* All rights reserved.
*
* This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
* see the License2.txt file for licencing details.
*/
/*
If you make use of the instrument driver code here, please note
that it is the author(s) of the code who are responsibility
for its operation. Any problems or queries regarding driving
instruments with the Argyll drivers, should be directed to
the Argyll's author(s), and not to any other party.
If there is some instrument feature or function that you
would like supported here, it is recommended that you
contact Argyll's author(s) first, rather than attempt to
modify the software yourself, if you don't have firm knowledge
of the instrument communicate protocols. There is a chance
that an instrument could be damaged by an incautious command
sequence, and the instrument companies generally cannot and
will not support developers that they have not qualified
and agreed to support.
*/
/*
Notes:
Naming of spectral values:
sensor - the 16 bit values from the sensor including any dummy/shielded/other values
raw - the floating point values of the spectral section
absraw - raw after scaling for integration time and gain settings.
For an RGB LED display, the i1Pro3 emissive sensitivity is
aproximately 225 max raw counts per int. second per cd/m^2.
Target is 45000 max, and saturation is about 53400.
For an 'A' type source it is about 99.2
cd/m^2 = count / (sensitivity * int_time)
The current i1Pro3(LA) instruments appear to have identical emissive sensitivity
as the i1Pro3(SA).
Hi-resolution mode is almost certainly less accurate than standard resolution.
For emissive measurement this is because the diffraction grating
has a rather bumpy spectral response, so that up-sampling the reference
correction is not very accurate. Unlike the i1pro2 though, the illuminant
(being LED) is also bumpy, and can't be uses as a smooth reference to
correct these errors. (There is a static correction which will only be
accurate for my specific instrument.)
For reflective measurement a high-res result is unusable due to the
complex and intricate calibration and measurement processing
that is uses to compute M0, M1 and M2 responses. For this reason
it is disabled in reflection measurement mode.
(The reflection measurement could be re-worked along the lines
of ArgyllCMS FWA correction so as to support hi-res., but it would
almost certainly not exactly match the results of the manufacturers driver
for FWA/OBE papers.)
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <math.h>
#if defined(UNIX)
# include <utime.h>
#else
# include <sys/utime.h>
#endif
#include <sys/stat.h>
#ifndef SALONEINSTLIB
#include "copyright.h"
#include "aconfig.h"
#include "numlib.h"
#include "rspl.h"
#else /* SALONEINSTLIB */
#include <fcntl.h>
#include "sa_config.h"
#include "numsup.h"
#include "rspl1.h"
#endif /* SALONEINSTLIB */
#include "cgats.h"
#include "xspect.h"
#include "insttypes.h"
#include "conv.h"
#include "icoms.h"
#include "sort.h"
/* Configuration */
#define USE_THREAD /* [def] Use thread to wait for events from the instrument */
#undef WAIT_FOR_DELAY_TRIGGER /* [und] Hack to diagnose threading problems */
#define ENABLE_NONVCAL /* [def] Enable saving calibration state to a file */
#define ENABLE_ZEBRA /* [def] Enable Zebra ruler */
#define ENABLE_DYNBLKTC /* [def] Enable dynamic black temperature compensation */
#define STRICT_IMITATE /* [und] Imitate foibles of manufacturers driver */
#define HIGH_RES /* [def] Enable high resolution spectral mode code. Disable */
/* to break dependency on rspl library. */
#define CALTEMPLIM 10.0 /* [10.0] Calibration max temperature delta */
#define WLCALTOUT (24 * 60 * 60) /* [24 Hrs] Wavelength calibration timeout in seconds */
#define DCALTOUT ( 1 * 60 * 60) /* [1 Hr] i1pro3 Dark Calibration timeout in seconds */
#define WCALTOUT ( 1 * 60 * 60) /* [1 Hr] White Calibration timeout in seconds */
#define MAXSCANTIME 30.0 /* [30] Maximum scan time in seconds */
#define SW_THREAD_TIMEOUT (10 * 60.0) /* [10 Min] Switch read thread timeout */
#undef D_PLOT /* [und] Use plots to show EE info for -D7 or higher */
#undef D_STRAYPLOT /* [und] Use plots to show EE info for -D7 or higher */
#undef DEBUG /* [und] Turn on debug printfs */
#undef PLOT_DEBUG /* [und] Use plot to show readings & processing */
#undef PLOT_REFRESH /* [und] Plot refresh rate measurement info */
#undef PLOT_UPDELAY /* [und] Plot data used to determine display update delay */
#undef DUMP_SCANV /* [und] Dump scan readings to a file "i1pdump.txt" */
#undef DUMP_DARKM /* [und] Append raw dark readings to file "i1pddump.txt" */
#undef APPEND_MEAN_EMMIS_VAL /* [und] Append averaged uncalibrated reading to file "i1pdump.txt" */
#undef TEST_DARK_INTERP /* [und] Test out the dark interpolation (need DEBUG for plot) */
#undef PATREC_DEBUG /* [und] Print & Plot patch/flash recognition information */
#undef PATREC_PLOT_ALLBANDS /* [und] Plot all bands of scan */
#undef PATREC_SAVETRIMMED /* [und] Saved trimmed raw to file "i1pro3_raw_trimed_N.csv */
#undef IGNORE_WHITE_INCONS /* [und] Ignore define reference reading inconsistency */
#undef HIGH_RES_PLOT /* [und] Plot created hi-res filters */
#undef HIGH_RES_PLOT_WAVFILT /* [und] High resolution raw2wav filters */
#undef HIGH_RES_PLOT_STRAYL /* [und] High resolution stray value upsample result */
#undef FAKE_EEPROM /* Get [und] EEPROM data from i1pro3_fake_eeprom.h */
#define NAE_INTT 1.8 /* Default starting integration time for emis. non-adaptive */
/* This sets the initial maximum to about 110 cd/m^2 */
#define MAX_INT_TIME 2.0 /* [2.0] Maximum integration time, sets adaptive max */
/* (There are very slight improvements in S/N with longer max) */
/* High res mode settings */
#define HIGHRES_SHORT 380.0 /* [380] Same as std. res., since grating efficiency is poor */
#define HIGHRES_LONG 730.0 /* [730] outside this range. */
#define HIGHRES_WIDTH (10.0/3.0) /* 3.3333 spacing seems a good choice */
#define HIGHRES_REF_MIN 380.0 /* [380] Same as std. res. */
#define MX_NSEN 282 /* Maximum nsen value we can cope with */
#define MX_NRAW 128 /* Maximum nsen value we can cope with */
#define MX_NWAV 120 /* Maximum nsen value we can cope with (need 106) */
#if defined(DEBUG) \
|| defined(D_PLOT) \
|| defined(PLOT_DEBUG) \
|| defined(PLOT_REFRESH) \
|| defined(PLOT_UPDELAY) \
|| defined(PATREC_DEBUG) \
|| defined(HIGH_RES_PLOT) \
|| defined(HIGH_RES_PLOT_WAVFILT) \
|| defined(HIGH_RES_PLOT_STRAYL)
# pragma message("######### i1pro3_imp.c DEBUG enabled !!!!! ########")
# include <plot.h>
#endif
/* - - - - - - - - - - - - - - - - - - */
#include "i1pro3.h"
#include "i1pro3_imp.h"
#include "xrga.h"
/* - - - - - - - - - - - - - - - - - - */
#define LED_OFF_TIME 1000 /* msec to allow LEDS to cool before illuminated measure ? */
#define LEDTURNONMEAS 77 /* Number of native reflective measurements to throw away */
/* to allow the illumination LEDs to come up to temperature ? */
#define USE_RD_SYNC /* [def] Use mutex syncronisation, else depend on TRIG_DELAY */
#define TRIG_DELAY 20 /* [20] Measure trigger delay to allow pending read, msec */
#define PATCH_CONS_THR 0.08 /* Default measurement consistency threshold */
#define MIN_SAMPLES 6 /* [6] Minimum number of scan samples in a patch */
#define POL_MIN_SAMPLES 4 /* [4] Be more generous due to lower sampling rate */
/* - - - - - - - - - - - - - - - - - - */
#if defined(DEBUG) || defined(PLOT_DEBUG) || defined(PATREC_DEBUG)
# include <plot.h>
#endif
#if defined(DEBUG) || defined(PLOT_DEBUG) || defined(HIGH_RES_PLOT) || defined(PATREC_DEBUG)
static int disdebplot = 0;
#define DISDPLOT disdebplot = 1;
#define ENDPLOT disdebplot = 0;
#else
#define DISDPLOT
#define ENDPLOT
#endif /* DEBUG */
#if defined(DEBUG) || defined(PLOT_DEBUG) || defined(PATREC_DEBUG)
/* ============================================================ */
/* Debugging support */
/* Plot three CCD spectra */
static void plot_raw3(double *y1, double *y2, double *y3) {
int i;
double xx[128];
if (disdebplot)
return;
for (i = 0; i < 128; i++)
xx[i] = (double)i;
do_plot(xx, y1, y2, y3, 128);
}
/* Plot a CCD spectra */
static void plot_raw(double *data) {
plot_raw3(data, NULL, NULL);
}
/* Plot two CCD spectra */
static void plot_raw2(double *data1, double *data2) {
plot_raw3(data1, data2, NULL);
}
/* Linear interpolate a wav. Return cv value on clip */
static double wav_lerp_cv(i1pro3imp *m, int hires, double *ary, double wl, double cv) {
int jj;
double wl0, wl1, bl;
double rv;
jj = (int)floor(XSPECT_DIX(m->wl_short[hires], m->wl_long[hires], m->nwav[hires], wl));
if (jj < 0)
jj = 0;
else if (jj > (m->nwav[hires]-2))
jj = m->nwav[hires]-2;
wl0 = XSPECT_WL(m->wl_short[hires], m->wl_long[hires], m->nwav[hires], jj);
wl1 = XSPECT_WL(m->wl_short[hires], m->wl_long[hires], m->nwav[hires], jj+1);
bl = (wl - wl0)/(wl1 - wl0);
if (bl < 0.0 || bl > 1.0)
return cv;
rv = (1.0 - bl) * ary[jj] + bl * ary[jj+1];
return rv;
}
/* Plot a converted spectra */
static void plot_wav(i1pro3imp *m, int hires, double *data) {
int i;
double xx[128];
double yy[128];
if (disdebplot)
return;
for (i = 0; i < m->nwav[hires]; i++) {
xx[i] = XSPECT_WL(m->wl_short[hires], m->wl_long[hires], m->nwav[hires], i);
yy[i] = data[i];
}
do_plot(xx, yy, NULL, NULL, m->nwav[hires]);
}
/* Plot two converted spectra for the current res. mode */
void plot_wav2(i1pro3imp *m, int hires, double *data1, double *data2) {
int i;
double xx[128];
double y1[128];
double y2[128];
if (disdebplot)
return;
for (i = 0; i < m->nwav[hires]; i++) {
xx[i] = XSPECT_WL(m->wl_short[hires], m->wl_long[hires], m->nwav[hires], i);
y1[i] = data1[i];
y2[i] = data2[i];
}
do_plot(xx, y1, y2, NULL, m->nwav[hires]);
}
/* Plot three converted spectra for the current res. mode */
void plot_wav3(i1pro3imp *m, int hires, double *data1, double *data2, double *data3) {
int i;
double xx[128];
double y1[128];
double y2[128];
double y3[128];
if (disdebplot)
return;
for (i = 0; i < m->nwav[hires]; i++) {
xx[i] = XSPECT_WL(m->wl_short[hires], m->wl_long[hires], m->nwav[hires], i);
y1[i] = data1[i];
y2[i] = data2[i];
y3[i] = data3[i];
}
do_plot(xx, y1, y2, y3, m->nwav[hires]);
}
/* Plot N converted spectra for the current res. mode */
static void plot_wav_N(i1pro3imp *m, int hires, double **data, int nwav) {
int i, j, k;
double xx[128];
double *yy[MXGPHS];
if (disdebplot)
return;
for (j = 0; j < m->nwav[hires]; j++)
xx[j] = XSPECT_WL(m->wl_short[hires], m->wl_long[hires], m->nwav[hires], j);
for (i = 0; i < nwav; i += MXGPHS) {
for (k = 0; k < MXGPHS; k++) {
if ((i + k) >= nwav)
yy[k] = NULL;
else
yy[k] = data[i+k];
}
do_plotNpwz(xx, yy, m->nwav[hires], NULL, NULL, 0, 1, 0);
}
}
/* Plot std and high res wav spectra */
void plot_wav_sh(i1pro3imp *m, double *std, double *hi) {
int j;
double xx[MX_NWAV], y1[MX_NWAV], y2[MX_NWAV];
for (j = 0; j < (m->nwav[1]); j++) {
xx[j] = XSPECT_WL(m->wl_short[1], m->wl_long[1], m->nwav[1], j);
y1[j] = wav_lerp_cv(m, 0, std, xx[j], 0.0);
y2[j] = hi[j];
}
do_plot(xx, y1, y2, NULL, m->nwav[1]);
}
#endif /* PLOT_DEBUG */
/* ============================================================ */
/* Implementation struct */
/* Add an implementation structure */
i1pro3_code add_i1pro3imp(i1pro3 *p) {
i1pro3imp *m;
if ((m = (i1pro3imp *)calloc(1, sizeof(i1pro3imp))) == NULL) {
a1logd(p->log,1,"add_i1pro3imp malloc %ld bytes failed (1)\n",sizeof(i1pro3imp));
return I1PRO3_INT_MALLOC;
}
m->p = p;
m->imode = inst_mode_ref_spot;
m->mmode = i1p3_refl_spot;
amutex_init(m->lock); /* USB control port lock */
m->lo_secs = 2000000000; /* A very long time */
m->msec = msec_time();
p->m = (void *)m;
return I1PRO3_OK;
}
/* Shutdown instrument, and then destroy */
/* implementation structure */
void del_i1pro3imp(i1pro3 *p) {
a1logd(p->log,5,"i1pro3_del called\n");
#ifdef ENABLE_NONVCAL
/* Touch it so that we know when the instrument was last open */
i1pro3_touch_calibration(p);
#endif /* ENABLE_NONVCAL */
if (p->m != NULL) {
int i, j;
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s;
i1pro3_code ev;
/* Terminate event monitor thread */
if (m->th != NULL) {
m->th_term = 1; /* Tell thread to exit on error */
i1pro3_terminate_event(p);
for (i = 0; m->th_termed == 0 && i < 5; i++)
msec_sleep(50); /* Wait for thread to terminate */
if (i >= 5) {
a1logd(p->log,5,"i1pro3 event thread termination failed\n");
m->th->terminate(m->th); /* Try and force thread to terminate */
}
m->th->del(m->th);
usb_uninit_cancel(&m->sw_cancel); /* Don't need cancel token now */
a1logd(p->log,5,"i1pro3 event thread terminated\n");
}
if (m->trig_thread != NULL) {
m->trig_thread->del(m->trig_thread);
a1logd(p->log,5,"i1pro3 trigger thread terminated\n");
}
usb_uninit_cancel(&m->rd_sync); /* Don't need sync token now */
usb_uninit_cancel(&m->rd_sync2); /* Don't need sync token now */
/* Free any per mode data */
for (i = 0; i < i1p3_no_modes; i++) {
s = &m->ms[i];
free_dmatrix(s->idark_data, 0, 1, -1, m->nraw-1);
free_dvector(s->cal_factor[0], 0, m->nwav[0]-1);
free_dvector(s->cal_factor[1], 0, m->nwav[1]-1);
free_dvector(s->raw_white, -1, m->nraw-1);
free_dvector(s->calsp_illcr[0][0], 0, m->nwav[0]-1);
free_dvector(s->calsp_illcr[1][0], 0, m->nwav[1]-1);
free_dvector(s->calsp_illcr[0][1], 0, m->nwav[0]-1);
free_dvector(s->calsp_illcr[1][1], 0, m->nwav[1]-1);
free_dvector(s->iavg2aillum[0], 0, m->nwav[0]-1);
free_dvector(s->iavg2aillum[1], 0, m->nwav[1]-1);
free_dvector(s->sc_calsp_nn_white[0], 0, m->nwav[0]-1);
free_dvector(s->sc_calsp_nn_white[1], 0, m->nwav[1]-1);
free_dvector(s->cal_l_uv_diff[0], 0, m->nwav[0]-1);
free_dvector(s->cal_l_uv_diff[1], 0, m->nwav[1]-1);
free_dvector(s->cal_s_uv_diff[0], 0, m->nwav[0]-1);
free_dvector(s->cal_s_uv_diff[1], 0, m->nwav[1]-1);
free_dvector(s->pol_calsp_white[0], 0, m->nwav[0]-1);
free_dvector(s->pol_calsp_white[1], 0, m->nwav[1]-1);
}
/* Free all high res cal data */
free_dvector(m->m0_fwa[1], 0, m->nwav[1]-1);
free_dvector(m->m1_fwa[1], 0, m->nwav[1]-1);
free_dvector(m->m2_fwa[1], 0, m->nwav[1]-1);
free_dvector(m->fwa_cal[1], 0, m->nwav[1]-1);
free_dvector(m->fwa_std[1], 0, m->nwav[1]-1);
/* Free all high res raw2wav resampling filters */
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
if (m->mtx[i][j].index != NULL)
free(m->mtx[i][j].index);
if (m->mtx[i][j].nocoef != NULL)
free(m->mtx[i][j].nocoef);
if (m->mtx[i][j].coef != NULL)
free(m->mtx[i][j].coef);
}
}
/* Free straylight arrays */
for (i = 0; i < 2; i++) {
if (m->straylight[i] != NULL)
free_dmatrix(m->straylight[i], 0, m->nwav[i]-1, 0, m->nwav[i]-1);
}
/* Free allocated eeprom data */
free_dvector(m->white_ref[1], 0, m->nwav[1]-1);
free_dvector(m->emis_coef[1], 0, m->nwav[1]-1);
free_dvector(m->amb_coef[1], 0, m->nwav[1]-1);
free(m);
p->m = NULL;
}
}
/* ============================================================ */
/* High level functions */
#ifdef FAKE_EEPROM
# pragma message("######### i1pro3_imp.c FAKE EEPROM compiled !!!!! ########")
# include "i1pro3_fake_eeprom.h"
#endif
#ifndef NEVER
void i1pro3_test_refl(i1pro3 *p); /* test code */
void i1pro3_test_led_instr(i1pro3 *p); /* test code */
void i1pro3_test_measure_instr(i1pro3 *p); /* test code */
void i1pro3_test_emiscalib(i1pro3 *p); /* test code */
#endif
/* Initialise our software state from the hardware */
i1pro3_code i1pro3_imp_init(i1pro3 *p) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_code ev = I1PRO3_OK;
unsigned char *eeprom; /* EEProm contents */
char *envv;
a1logd(p->log,5,"i1pro3_init:\n");
m->native_calstd = xcalstd_xrga;
m->target_calstd = xcalstd_native; /* Default to native calibration */
/* Honor Environment override */
if ((envv = getenv("ARGYLL_XCALSTD")) != NULL) {
if (strcmp(envv, "XRGA") == 0)
m->target_calstd = xcalstd_xrga;
else if (strcmp(envv, "XRDI") == 0)
m->target_calstd = xcalstd_xrdi;
else if (strcmp(envv, "GMDI") == 0)
m->target_calstd = xcalstd_gmdi;
}
if (p->dtype != instI1Pro3)
return I1PRO3_UNKNOWN_MODEL;
m->trig = inst_opt_trig_user;
m->scan_toll_ratio = 1.0;
/* Take conservative approach to when the light was last on. */
/* Assume it might have been on right before init was called again. */
m->llamponoff = msec_time();
usb_init_cancel(&m->sw_cancel); /* Init event cancel token */
usb_init_cancel(&m->rd_sync); /* Init reading sync token */
usb_init_cancel(&m->rd_sync2); /* Init reading sync token */
msec_sleep(100);
/* Get the fw version */
if ((ev = i1pro3_fwver(p, &m->fwver, m->fwvstr)) != I1PRO3_OK)
return ev;
a1logd(p->log,2,"Firmware rev = %.2f, '%s'\n",m->fwver/100.0,m->fwvstr);
/* Get other hw parameters */
if ((ev = i1pro3_getparams(p, &m->minintclks, NULL, &m->intclkp)) != I1PRO3_OK)
return ev;
a1logd(p->log,2,"Sub-clock divider = %d, integration clock = %f usec\n",
m->minintclks, 1e6 * m->intclkp);
/* Get EEPROM data from i1pro3_fake_eeprom.h */
#ifdef FAKE_EEPROM
m->eesize = FAKE_EEPROM_SIZE;
if ((eeprom = (unsigned char *)malloc(m->eesize)) == NULL) {
a1logd(p->log,1,"Malloc %d bytes for eeprom failed\n",m->eesize);
return I1PRO3_INT_MALLOC;
}
memcpy(eeprom, fake_eeprom_data, m->eesize);
#else
/* Set the EEProm size (seems to be hard coded ?) */
m->eesize = 16 * 1024;
if ((eeprom = (unsigned char *)malloc(m->eesize)) == NULL) {
a1logd(p->log,1,"Malloc %d bytes for eeprom failed\n",m->eesize);
return I1PRO3_INT_MALLOC;
}
/* Read the EEProm */
if ((ev = i1pro3_readEEProm(p, eeprom, 0, m->eesize)) != I1PRO3_OK) {
free(eeprom);
return ev;
}
#endif /* FAKE_EEPROM */
/* Get the Chip ID (This doesn't work until after reading the EEProm ?) */
if ((ev = i1pro3_getchipid(p, m->hw_chipid)) != I1PRO3_OK) {
free(eeprom);
return ev;
}
/* Parse the i1pro3 data */
if ((ev = i1pro3_parse_eeprom(p, eeprom, m->eesize)) != I1PRO3_OK) {
free(eeprom);
return ev;
}
free(eeprom); eeprom = NULL;
/* Make the default indicator state be off */
if ((ev = i1pro3_indLEDoff(p)) != I1PRO3_OK)
return ev;
/* Set the default LED currents */
if ((ev = i1pro3_setledcurrents(p,
m->ee_led_w_cur, m->ee_led_b_cur, m->ee_led_luv_cur, m->ee_led_suv_cur, m->ee_led_gwl_cur
)) != I1PRO3_OK)
return ev;
/* Set up the current state of each mode */
{
int i, j;
i1pro3_state *s;
/* First set state to basic configuration */
/* (We assume it's been zero'd) */
for (i = 0; i < i1p3_no_modes; i++) {
s = &m->ms[i];
memset(s, 0, sizeof(i1pro3_state)); /* Default everything to zero */
s->mode = i;
/* Default to an emissive configuration */
s->targoscale = 1.0; /* Default full scale */
s->want_wlcalib = 1; /* Do an initial calibration */
s->want_dcalib = 1;
s->want_calib = 1;
s->idark_data = dmatrixz(0, 1, -1, m->nraw-1);
s->idark_int_time[0] = m->min_int_time;
s->idark_int_time[1] = MAX_INT_TIME > 4.0 ? 4.0 : MAX_INT_TIME;
s->cal_factor[0] = dvectorz(0, m->nwav[0]-1);
s->cal_factor[1] = dvectorz(0, m->nwav[1]-1);
s->raw_white = dvectorz(-1, m->nraw-1);
s->pol_calsp_white[0] = dvectorz(0, m->nwav[0]-1);
s->pol_calsp_white[1] = dvectorz(0, m->nwav[1]-1);
s->calsp_illcr[0][0] = dvectorz(0, m->nwav[0]-1);
s->calsp_illcr[1][0] = dvectorz(0, m->nwav[1]-1);
s->calsp_illcr[0][1] = dvectorz(0, m->nwav[0]-1);
s->calsp_illcr[1][1] = dvectorz(0, m->nwav[1]-1);
s->iavg2aillum[0] = dvectorz(0, m->nwav[0]-1);
s->iavg2aillum[1] = dvectorz(0, m->nwav[1]-1);
s->sc_calsp_nn_white[0] = dvectorz(0, m->nwav[0]-1);
s->sc_calsp_nn_white[1] = dvectorz(0, m->nwav[1]-1);
s->cal_l_uv_diff[0] = dvectorz(0, m->nwav[0]-1);
s->cal_l_uv_diff[1] = dvectorz(0, m->nwav[1]-1);
s->cal_s_uv_diff[0] = dvectorz(0, m->nwav[0]-1);
s->cal_s_uv_diff[1] = dvectorz(0, m->nwav[1]-1);
s->min_wl = HIGHRES_REF_MIN; /* Same as std. res. */
}
/* Then add mode specific settings */
for (i = 0; i < i1p3_no_modes; i++) {
s = &m->ms[i];
switch(i) {
case i1p3_refl_spot:
s->reflective = 1;
s->dc_use = i1p3_dc_none; /* Dark cal is on the fly */
s->mc_use = i1p3_mc_reflective; /* Reflective calibration block */
s->inttime = m->min_int_time; /* Maximize scan rate */
{
s->dcaltime = 2 * 10 * m->min_int_time;
s->wcaltime = 2 * 22 * m->min_int_time;
s->wscaltime = 2 * 323 * m->min_int_time; // First white calib is longer..
}
s->dreadtime = s->dcaltime;
s->wreadtime = s->wcaltime;
break;
case i1p3_refl_spot_pol:
s->reflective = 1;
s->pol = 1;
s->dc_use = i1p3_dc_none; /* Dark cal is on the fly */
s->mc_use = i1p3_mc_polreflective; /* Pol. reflective calibration block */
s->targoscale = 0.3;
s->inttime = s->iinttime = 4.0 * m->min_int_time; /* Use 1/4 max scan rate */
s->dcaltime = 2 * 8 * 5 * m->min_int_time; /* 8 measurements */
s->wcaltime = 2 * 17 * 5 * m->min_int_time; /* 17 measurements */
s->dreadtime = s->dcaltime;
s->wreadtime = s->wcaltime;
break;
case i1p3_refl_scan:
s->reflective = 1;
s->scan = 1;
s->dc_use = i1p3_dc_none; /* Dark cal is on the fly */
s->mc_use = i1p3_mc_reflective; /* Reflective calibration block */
s->inttime = m->min_int_time; /* Maximize scan rate */
{
s->dcaltime = 2 * 10 * m->min_int_time;
s->wcaltime = 2 * 22 * m->min_int_time;
s->wscaltime = 2 * 323 * m->min_int_time; // First white calib is longer..
}
s->dreadtime = s->dcaltime;
s->maxscantime = MAXSCANTIME;
break;
case i1p3_refl_scan_pol:
s->reflective = 1;
s->scan = 1;
s->pol = 1;
s->dc_use = i1p3_dc_none; /* Dark cal is on the fly */
s->mc_use = i1p3_mc_polreflective; /* Pol. reflective calibration block */
s->targoscale = 0.3;
s->inttime = s->iinttime = 4.0 * m->min_int_time; /* Use 1/4 max scan rate */
s->dcaltime = 2 * 8 * 5 * m->min_int_time; /* 8 measurements */
s->wcaltime = 2 * 17 * 5 * m->min_int_time; /* 17 measurements */
s->dreadtime = s->dcaltime;
s->maxscantime = MAXSCANTIME;
break;
case i1p3_emiss_spot_na: /* Emissive spot not adaptive */
s->emiss = 1;
s->adaptive = 0;
s->dc_use = i1p3_dc_adaptive;
s->mc_use = i1p3_mc_pcalfactor;
vect_cpy(s->cal_factor[0], m->emis_coef[0], m->nwav[0]);
vect_cpy(s->cal_factor[1], m->emis_coef[1], m->nwav[1]);
s->cal_valid = 1; /* Scale factor is valid */
s->inttime = s->iinttime = NAE_INTT; /* Default disp integration time */
s->dreadtime = 0.2; /* Dark samples measurement time */
s->wreadtime = 2.0; /* Minimum sample measurement time */
s->dcaltime = 0.5; /* Dark calibration (short) measurement time */
s->dlcaltime = 4.0; /* Dark calibration long measurement time */
break;
case i1p3_emiss_spot: /* Emissive adaptive spot */
s->emiss = 1;
s->adaptive = 1;
s->dc_use = i1p3_dc_adaptive;
s->mc_use = i1p3_mc_pcalfactor;
vect_cpy(s->cal_factor[0], m->emis_coef[0], m->nwav[0]);
vect_cpy(s->cal_factor[1], m->emis_coef[1], m->nwav[1]);
s->cal_valid = 1; /* Scale factor is valid */
s->inttime = m->min_int_time;
s->dreadtime = 0.2; /* Dark samples measurement time */
s->wreadtime = 2.0; /* Minimum sample measurement time */
s->dcaltime = 0.5; /* Dark calibration (short) measurement time */
s->dlcaltime = 4.0; /* Dark calibration long measurement time */
break;
case i1p3_emiss_scan:
s->emiss = 1;
s->scan = 1;
s->dc_use = i1p3_dc_adaptive;
s->mc_use = i1p3_mc_pcalfactor;
s->targoscale = 0.90; /* Allow extra 10% margine for drift */
vect_cpy(s->cal_factor[0], m->emis_coef[0], m->nwav[0]);
vect_cpy(s->cal_factor[1], m->emis_coef[1], m->nwav[1]);
s->cal_valid = 1; /* Scale factor is valid */
s->targoscale = 0.90; /* Allow extra 10% margine for drift */
s->inttime = s->iinttime = 2.0 * m->min_int_time; /* Lower if needed */
s->dreadtime = 0.2; /* Dark samples measurement time */
s->dcaltime = 0.5; /* Dark calibration (short) measurement time */
s->dlcaltime = 4.0; /* Dark calibration long measurement time */
s->maxscantime = MAXSCANTIME;
break;
case i1p3_amb_spot:
s->emiss = 1;
s->ambient = 1;
s->adaptive = 1;
s->dc_use = i1p3_dc_adaptive;
s->mc_use = i1p3_mc_pcalfactor;
vect_cpy(s->cal_factor[0], m->emis_coef[0], m->nwav[0]);
vect_mul(s->cal_factor[0], m->amb_coef[0], m->nwav[0]);
vect_cpy(s->cal_factor[1], m->emis_coef[1], m->nwav[1]);
vect_mul(s->cal_factor[1], m->amb_coef[1], m->nwav[1]);
s->cal_valid = 1; /* Scale factor is valid */
s->inttime = m->min_int_time;
s->dreadtime = 0.2; /* Dark samples measurement time */
s->wreadtime = 2.0; /* Minimum sample measurement time */
s->dcaltime = 0.5; /* Dark calibration (short) measurement time */
s->dlcaltime = 4.0; /* Dark calibration long measurement time */
break;
case i1p3_amb_flash: /* This is intended for measuring flashes */
s->emiss = 1;
s->ambient = 1;
s->scan = 1;
s->flash = 1;
s->dc_use = i1p3_dc_adaptive;
s->mc_use = i1p3_mc_pcalfactor;
vect_cpy(s->cal_factor[0], m->emis_coef[0], m->nwav[0]);
vect_mul(s->cal_factor[0], m->amb_coef[0], m->nwav[0]);
vect_cpy(s->cal_factor[1], m->emis_coef[1], m->nwav[1]);
vect_mul(s->cal_factor[1], m->amb_coef[1], m->nwav[1]);
s->cal_valid = 1; /* Calibration is valid */
s->inttime = m->min_int_time; /* Maximize scan rate and max level */
s->dreadtime = 0.2; /* Dark samples measurement time */
s->dcaltime = 0.5; /* Dark calibration (short) measurement time */
s->dlcaltime = 4.0; /* Dark calibration long measurement time */
s->maxscantime = MAXSCANTIME;
break;
case i1p3_trans_spot:
s->trans = 1;
s->adaptive = 1;
s->dc_use = i1p3_dc_adaptive;
s->mc_use = i1p3_mc_mcalfactor;
s->inttime = m->min_int_time;
s->dreadtime = 0.2; /* Dark samples measurement time */
s->wreadtime = 2.0; /* Minimum sample measurement time */
s->dcaltime = 0.5; /* Dark calibration (short) measurement time */
s->dlcaltime = 4.0; /* Dark calibration long measurement time */
break;
case i1p3_trans_scan:
s->trans = 1;
s->scan = 1;
s->adaptive = 0;
s->dc_use = i1p3_dc_adaptive;
s->mc_use = i1p3_mc_mcalfactor;
s->targoscale = 0.90; /* Allow extra 10% margine for drift */
s->inttime = s->iinttime = 2.0 * m->min_int_time; /* Lower if needed */
s->dreadtime = 0.2; /* Dark samples measurement time */
s->dcaltime = 0.5; /* Dark calibration (short) measurement time */
s->dlcaltime = 4.0; /* Dark calibration long measurement time */
s->maxscantime = MAXSCANTIME;
break;
}
}
}
#ifdef ENABLE_NONVCAL
/* Restore the all modes calibration from the local system */
i1pro3_restore_calibration(p);
/* Touch it so that we know when the instrument was last opened */
i1pro3_touch_calibration(p);
#endif
/* Compute all the wavelength re-sampling filters. */
/* (Incorporates wl_cal if it is valid) */
if ((ev = i1pro3_compute_wav_filters(p, m->ms[m->mmode].wl_cal_raw_off,
m->ms[m->mmode].wl_cal_wav_off, 1)) != I1PRO3_OK) {
a1logd(p->log,2,"i1pro3_compute_wav_filters() failed\n");
return ev;
}
if (p->log->verb >= 1) {
a1logv(p->log,1,"Instrument Type: %s%s\n",inst_name(p->dtype),
m->aperture ? " Plus" : "");
a1logv(p->log,1,"EE version: %d\n",m->version);
a1logv(p->log,1,"Serial Number: %d\n",m->serno);
a1logv(p->log,1,"Firmware version: %d\n",m->fwver);
a1logv(p->log,1,"Chip ID: %02x-%02x%02x%02x%02x%02x%02x%02x\n",
m->hw_chipid[0], m->hw_chipid[1], m->hw_chipid[2], m->hw_chipid[3],
m->hw_chipid[4], m->hw_chipid[5], m->hw_chipid[6], m->hw_chipid[7]);
a1logv(p->log,1,"Date manufactured: %d-%d-%d\n",
m->ee_dom1 % 100, (m->ee_dom1/100) % 100, m->ee_dom1/10000);
a1logv(p->log,1,"Aperture: %s mm\n",m->aperture ? "8.0" : "4.5");
a1logv(p->log,1,"Ambient Measurement ? : %s\n",
m->capabilities & I1PRO3_CAP_AMBIENT ? "Yes" : "No");
a1logv(p->log,1,"Wavelength Calibration ?: %s\n",
m->capabilities & I1PRO3_CAP_WL_LED ? "Yes" : "No");
a1logv(p->log,1,"Zebra Ruler ? : %s\n",
m->capabilities & I1PRO3_CAP_ZEB_RUL ? "Yes" : "No");
a1logv(p->log,1,"Indicator LEDs ? : %s\n",
m->capabilities & I1PRO3_CAP_IND_LED ? "Yes" : "No");
a1logv(p->log,1,"Head Sensor ? : %s\n",
m->capabilities & I1PRO3_CAP_HEAD_SENS ? "Yes" : "No");
a1logv(p->log,1,"Polarized Measurement ? : %s\n",
m->capabilities & I1PRO3_CAP_POL ? "Yes" : "No");
}
#ifdef USE_THREAD
/* Setup the event monitoring thread */
/* (If we start this too early, it wrecks instrument initialization, */
/* and we get the wrong scaling on the last 6 reflective measurement */
/* auxiliary values!) */
if ((m->th = new_athread(i1pro3_event_thread, (void *)p)) == NULL)
return I1PRO3_INT_THREADFAILED;
#endif
return ev;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Return a pointer to the serial number */
char *i1pro3_imp_get_serial_no(i1pro3 *p) {
i1pro3imp *m = (i1pro3imp *)p->m;
return m->sserno;
}
/* Set the measurement mode. It may need calibrating */
i1pro3_code i1pro3_imp_set_mode(
i1pro3 *p,
i1p3_mode mmode, /* Operating mode */
inst_mode imode /* Full mode mask for options */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
a1logd(p->log,2,"i1pro3_imp_set_mode called with mode no %d and imask 0x%x\n",mmode,imode);
switch(mmode) {
case i1p3_refl_spot:
case i1p3_refl_scan:
break;
case i1p3_refl_spot_pol:
case i1p3_refl_scan_pol:
if ((m->capabilities & I1PRO3_CAP_POL) == 0)
return I1PRO3_INT_ILLEGALMODE;
break;
case i1p3_emiss_spot_na:
case i1p3_emiss_spot:
case i1p3_emiss_scan:
break;
case i1p3_amb_spot:
case i1p3_amb_flash:
if ((m->capabilities & I1PRO3_CAP_AMBIENT) == 0)
return I1PRO3_INT_ILLEGALMODE;
break;
case i1p3_trans_spot:
case i1p3_trans_scan:
break;
default:
return I1PRO3_INT_ILLEGALMODE;
}
m->imode = imode;
m->mmode = mmode;
m->spec_en = (imode & inst_mode_spectral) != 0;
if ((imode & inst_mode_highres) != 0) {
i1pro3_code rv;
if ((rv = i1pro3_set_highres(p)) != I1PRO3_OK)
return rv;
} else {
i1pro3_set_stdres(p); /* Ignore any error */
}
return I1PRO3_OK;
}
/* Check and ivalidate calibration */
i1pro3_code i1pro3_check_calib(i1pro3 *p) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *cs = &m->ms[m->mmode];
double btemp;
time_t curtime = time(NULL);
i1pro3_code ev = I1PRO3_OK;
a1logd(p->log,2,"i1pro3_check_calib: checking mode %d\n",m->mmode);
if ((ev = i1pro3_getboardtemp(p, &btemp)) != I1PRO3_OK)
return ev;
/* Timout calibrations that are too old or temperature has changed too much */
if (m->capabilities & I1PRO3_CAP_WL_LED) {
if ((curtime - cs->wl_date) > WLCALTOUT) {
a1logd(p->log,2,"Invalidating wavelength cal as %d secs from last cal\n",curtime - cs->wl_date);
cs->wl_valid = 0;
}
if (fabs(btemp - cs->wl_temp) > CALTEMPLIM) {
a1logd(p->log,2,"Invalidating wavelength cal as %d secs from last cal\n",btemp - cs->wl_temp);
cs->wl_valid = 0;
}
}
if (cs->dc_use != i1p3_dc_none && (curtime - cs->ddate) > DCALTOUT) {
a1logd(p->log,2,"Invalidating dark cal as %d secs from last cal\n",curtime - cs->ddate);
cs->dark_valid = 0;
}
if (cs->dc_use != i1p3_dc_none && fabs(btemp - cs->dtemp) > CALTEMPLIM) {
a1logd(p->log,2,"Invalidating dark cal as %f degrees delta from last cal\n",btemp - cs->dtemp);
cs->dark_valid = 0;
}
if (cs->mc_use != i1p3_mc_pcalfactor && (curtime - cs->cdate) > WCALTOUT) {
a1logd(p->log,2,"Invalidating white cal as %d secs from last cal\n",curtime - cs->cdate);
cs->cal_valid = 0;
}
if (p->log->debug >= 5) {
a1logd(p->log,2,"i1pro3_check_calib result:\n");
a1logd(p->log,1," reflective = %d, adaptive = %d, emiss = %d, trans = %d, scan = %d\n",
cs->reflective, cs->adaptive, cs->emiss, cs->trans, cs->scan);
a1logd(p->log,1," wl_valid = %d, dark_valid = %d, cal_valid = %d\n",
cs->wl_valid, cs->dark_valid, cs->cal_valid);
a1logd(p->log,1," want_wlcalib = %d, want_calib = %d, want_dcalib = %d, noinitcalib = %d\n",
cs->want_wlcalib, cs->want_calib,cs->want_dcalib, m->noinitcalib);
}
return ev;
}
/* Return needed and available inst_cal_type's */
i1pro3_code i1pro3_imp_get_n_a_cals(i1pro3 *p, inst_cal_type *pn_cals, inst_cal_type *pa_cals) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *cs = &m->ms[m->mmode];
inst_cal_type n_cals = inst_calt_none;
inst_cal_type a_cals = inst_calt_none;
i1pro3_code ev = I1PRO3_OK;
if ((ev = i1pro3_check_calib(p)) != I1PRO3_OK)
return ev;
if (m->capabilities & I1PRO3_CAP_WL_LED) {
if (!cs->wl_valid
|| (cs->want_wlcalib && !m->noinitcalib)) {
a1logd(p->log,2," wl calib is invalid or want calib\n");
n_cals |= inst_calt_wavelength;
}
a_cals |= inst_calt_wavelength;
}
if (cs->reflective) {
if (!cs->cal_valid
|| (cs->want_calib && !m->noinitcalib)) {
a1logd(p->log,2," reflective calib is invalid or want calib\n");
n_cals |= inst_calt_ref_white;
}
a_cals |= inst_calt_ref_white;
}
if (cs->emiss) {
if ((!cs->dark_valid)
|| (cs->want_dcalib && !m->noinitcalib)) {
a1logd(p->log,2," emissive dark calib is invalid or want calib\n");
n_cals |= inst_calt_em_dark;
}
a_cals |= inst_calt_em_dark;
}
if (cs->emiss && !cs->adaptive && !cs->scan) {
if (!cs->done_dintsel) {
a1logd(p->log,2," non-adaptive emission int. time calib is invalid\n");
n_cals |= inst_calt_emis_int_time;
}
a_cals |= inst_calt_emis_int_time;
}
if (cs->trans) {
if ((!cs->dark_valid)
|| (cs->want_dcalib && !m->noinitcalib)) {
a1logd(p->log,2," transmissive dark calib is invalid or want calib\n");
n_cals |= inst_calt_trans_dark;
}
a_cals |= inst_calt_trans_dark;
if (!cs->cal_valid
|| (cs->want_calib && !m->noinitcalib)) {
a1logd(p->log,2," transmissive white calib is invalid or want calib\n");
n_cals |= inst_calt_trans_vwhite;
}
a_cals |= inst_calt_trans_vwhite;
}
if (pn_cals != NULL)
*pn_cals = n_cals;
if (pa_cals != NULL)
*pa_cals = a_cals;
a1logd(p->log,3,"i1pro3_imp_get_n_a_cals: returning n_cals 0x%x, a_cals 0x%x\n",n_cals, a_cals);
return I1PRO3_OK;
}
/* - - - - - - - - - - - - - - - - */
/* Calibrate for the current mode. */
/* Request an instrument calibration of the current mode. */
i1pro3_code i1pro3_imp_calibrate(
i1pro3 *p,
inst_cal_type *calt, /* Calibration type to do/remaining */
inst_cal_cond *calc, /* Current condition/desired condition */
inst_calc_id_type *idtype, /* Condition identifier type */
char id[CALIDLEN] /* Condition identifier (ie. white reference ID) */
) {
i1pro3_code ev = I1PRO3_OK;
i1pro3imp *m = (i1pro3imp *)p->m;
int mmode = m->mmode;
i1pro3_state *cs = &m->ms[m->mmode];
int sx1, sx2, sx3, sx;
time_t cdate = time(NULL); /* Current date to use */
int nummeas = 0;
int i, k;
inst_cal_type needed, available;
a1logd(p->log,2,"i1pro3_imp_calibrate called with calt 0x%x, calc 0x%x\n",*calt, *calc);
if ((ev = i1pro3_imp_get_n_a_cals(p, &needed, &available)) != I1PRO3_OK)
return ev;
/* Translate inst_calt_all/needed into something specific */
if (*calt == inst_calt_all
|| *calt == inst_calt_needed
|| *calt == inst_calt_available) {
if (*calt == inst_calt_all)
*calt = (needed & inst_calt_n_dfrble_mask) | inst_calt_ap_flag;
else if (*calt == inst_calt_needed)
*calt = needed & inst_calt_n_dfrble_mask;
else if (*calt == inst_calt_available)
*calt = available & inst_calt_n_dfrble_mask;
a1logd(p->log,4,"i1pro3_imp_calibrate: doing calt 0x%x\n",*calt);
if ((*calt & inst_calt_n_dfrble_mask) == 0) /* Nothing todo */
return I1PRO3_OK;
}
/* See if it's a calibration we understand */
if (*calt & ~available & inst_calt_all_mask) {
a1logd(p->log,4,"i1pro3_imp_calibrate: unsupported, calt 0x%x, available 0x%x\n",*calt,available);
return I1PRO3_UNSUPPORTED;
}
if (*calt & inst_calt_ap_flag) {
sx1 = 0; sx2 = sx3 = i1p3_no_modes; /* Go through all the modes */
} else {
sx1 = m->mmode; sx2 = sx1 + 1; /* Just current mode */
sx3 = i1p3_no_modes; /* no extra mode */
}
/* Go through the modes we are going to cover */
for (sx = sx1; sx < sx2; (++sx >= sx2 && sx3 != i1p3_no_modes) ? sx = sx3, sx2 = sx+1, sx3 = i1p3_no_modes : 0) {
i1pro3_state *s = &m->ms[sx];
m->mmode = sx; /* A lot of functions we call rely on this, so fake it */
a1logd(p->log,2,"\nCalibrating mode %d\n", s->mode);
/* Wavelength calibration: */
if (s->wl_date != cdate
&& (m->capabilities & I1PRO3_CAP_WL_LED)
&& (*calt & (inst_calt_wavelength | inst_calt_ap_flag))
&& ((*calc & inst_calc_cond_mask) == inst_calc_man_ref_white)) {
double *wlraw;
double optscale;
double *abswav;
a1logd(p->log,2,"\nDoing wavelength calibration\n");
wlraw = dvectorz(-1, m->nraw-1);
/* Make sure LEDs have cooled down for 1 second */
i1pro3_delay_llampoff(p, LED_OFF_TIME);
if ((ev = i1pro3_wl_measure(p, wlraw, &s->wl_temp)) != I1PRO3_OK) {
a1logd(p->log,2,"i1pro3_wl_measure() failed\n");
return ev;
}
// It's not cleare whether wl or cells is best offset to use...
s->wl_cal_raw_off = s->wl_cal_wav_off = 0.0;
/* Find the best fit of the measured values to the reference spectrum */
if ((ev = i1pro3_match_wl_meas(p, &s->wl_cal_wav_off, NULL, wlraw)) != I1PRO3_OK)
// if ((ev = i1pro3_match_wl_meas(p, NULL, &s->wl_cal_raw_off, wlraw)) != I1PRO3_OK)
{
a1logd(p->log,2,"i1pro3_match_wl_meas() failed\n");
return ev;
}
free_dvector(wlraw, -1, m->nraw-1);
/* Compute normal & hi-res wav filters for the value we just created */
if ((ev = i1pro3_compute_wav_filters(p, s->wl_cal_raw_off, s->wl_cal_wav_off, 1))
!= I1PRO3_OK) {
a1logd(p->log,2,"i1pro3_compute_wav_filters() failed\n");
return ev;
}
s->want_wlcalib = 0;
s->wl_valid = 1;
s->wl_date = cdate;
*calt &= ~inst_calt_wavelength;
/* Save the calib to all modes */
a1logd(p->log,5,"Saving wavelength calib to all modes\n");
for (i = 0; i < i1p3_no_modes; i++) {
i1pro3_state *ss = &m->ms[i];
if (ss == cs)
continue;
ss->want_wlcalib = s->want_wlcalib;
ss->wl_valid = s->wl_valid;
ss->wl_date = s->wl_date;
ss->wl_temp = s->wl_temp;
ss->wl_cal_raw_off = s->wl_cal_raw_off;
ss->wl_cal_wav_off = s->wl_cal_wav_off;
}
}
/* Black calibration for all emissive or transmissive. */
/* We do an adaptive calibration since this lets us track */
/* temperature changes most effectively. */
/* The black is interpolated from readings with two extreme integration times */
if (s->ddate != cdate
&& (*calt & (inst_calt_em_dark
| inst_calt_trans_dark
| inst_calt_ap_flag))
/* Any condition conducive to dark calib */
&& ((*calc & inst_calc_cond_mask) == inst_calc_man_ref_white
|| (*calc & inst_calc_cond_mask) == inst_calc_man_em_dark
|| (*calc & inst_calc_cond_mask) == inst_calc_man_am_dark
|| (*calc & inst_calc_cond_mask) == inst_calc_man_trans_dark)
/* Just emissive and transmissive */
&& (s->emiss || s->trans)) {
int refinst = 0;
double ctemp;
int i, j, k;
a1logd(p->log,2,"\nDoing emis/trans black calibration\n");
if ((ev = i1pro3_adapt_emis_cal(p, &ctemp)) != I1PRO3_OK) {
a1logd(p->log,2,"i1pro3_refl_cal failed\n");
return ev;
}
s->want_dcalib = 0;
s->dark_valid = 1;
s->dtemp = ctemp;
s->ddate = cdate;
*calt &= ~(inst_calt_em_dark
| inst_calt_trans_dark);
/* Save the calib to all similar modes */
a1logd(p->log,5,"Saving adaptive black calib to similar modes\n");
for (i = 0; i < i1p3_no_modes; i++) {
i1pro3_state *ss = &m->ms[i];
if (ss == s || ss->ddate == s->ddate)
continue;
if (ss->emiss || ss->trans) {
ss->want_dcalib = s->want_dcalib;
ss->dark_valid = s->dark_valid;
ss->dtemp = s->dtemp;
ss->ddate = s->ddate;
for (j = 0; j < 2; j++) {
ss->idark_int_time[j] = s->idark_int_time[j];
vect_cpy(ss->idark_data[j]-1, s->idark_data[j]-1, m->nraw+1);
}
}
}
a1logd(p->log,5,"Done adaptive interpolated black calibration\n");
}
/* Reflective white reference calibrate for spot or scan */
if (s->cdate != cdate
&& (*calt & (inst_calt_ref_white | inst_calt_ap_flag))
&& ((*calc & inst_calc_cond_mask) == inst_calc_man_ref_white && s->reflective)) {
a1logd(p->log,2,"\nDoing initial reflective white calibration\n");
if (m->filt == inst_opt_filter_pol) {
s->inttime = s->iinttime; /* Start with default integration time */
/* Do polarized reflectance calibration */
if ((ev = i1pro3_pol_refl_cal(p)) != I1PRO3_OK) {
a1logd(p->log,2,"i1pro3_refl_cal failed\n");
return ev;
}
} else {
/* Do reflectance calibration */
if ((ev = i1pro3_refl_cal(p)) != I1PRO3_OK) {
a1logd(p->log,2,"i1pro3_refl_cal failed\n");
return ev;
}
}
s->want_calib = 0;
s->cal_valid = 1;
s->cdate = cdate;
*calt &= ~(inst_calt_ref_white);
/* Save the calib to all similar modes */
a1logd(p->log,5,"Saving reflection white calib to similar modes\n");
for (i = 0; i < i1p3_no_modes; i++) {
i1pro3_state *ss = &m->ms[i];
if (ss == s || ss->cdate == s->cdate)
continue;
if (ss->reflective
&& (s->pol == ss->pol)
&& (s->dcaltime == ss->dcaltime)
&& (s->wcaltime == ss->wcaltime)
&& (s->wscaltime == ss->wscaltime)) {
ss->want_calib = s->want_calib;
ss->cal_valid = s->cal_valid;
ss->cdate = s->cdate;
if (s->pol) {
vect_cpy(ss->pol_calraw_white, s->pol_calraw_white, m->nraw);
vect_cpy(ss->pol_calsp_ledm, s->pol_calsp_ledm, m->nwav[0]);
vect_cpy(ss->pol_calsp_white[0], s->pol_calsp_white[0], m->nwav[0]);
vect_cpy(ss->pol_calsp_white[1], s->pol_calsp_white[1], m->nwav[1]);
} else {
vect_cpy(ss->calraw_white[0], s->calraw_white[0], m->nraw);
vect_cpy(ss->calraw_white[1], s->calraw_white[1], m->nraw);
vect_cpy(ss->calsp_ledm[0], s->calsp_ledm[0], m->nwav[0]);
vect_cpy(ss->calsp_ledm[1], s->calsp_ledm[1], m->nwav[0]);
vect_cpy(ss->calsp_illcr[0][0], s->calsp_illcr[0][0], m->nwav[0]);
vect_cpy(ss->calsp_illcr[1][0], s->calsp_illcr[1][0], m->nwav[1]);
vect_cpy(ss->calsp_illcr[0][1], s->calsp_illcr[0][1], m->nwav[0]);
vect_cpy(ss->calsp_illcr[1][1], s->calsp_illcr[1][1], m->nwav[1]);
vect_cpy(ss->iavg2aillum[0], s->iavg2aillum[0], m->nwav[0]);
vect_cpy(ss->iavg2aillum[1], s->iavg2aillum[1], m->nwav[1]);
vect_cpy(ss->sc_calsp_nn_white[0], s->sc_calsp_nn_white[0], m->nwav[0]);
vect_cpy(ss->sc_calsp_nn_white[1], s->sc_calsp_nn_white[1], m->nwav[1]);
vect_cpy(ss->cal_l_uv_diff[0], s->cal_l_uv_diff[0], m->nwav[0]);
vect_cpy(ss->cal_l_uv_diff[1], s->cal_l_uv_diff[1], m->nwav[1]);
vect_cpy(ss->cal_s_uv_diff[0], s->cal_s_uv_diff[0], m->nwav[0]);
vect_cpy(ss->cal_s_uv_diff[1], s->cal_s_uv_diff[1], m->nwav[1]);
}
}
}
}
/* If we are doing a transmissive white reference calibrate */
if (s->cdate != cdate
&& (*calt & (inst_calt_trans_vwhite | inst_calt_ap_flag))
&& ((*calc & inst_calc_cond_mask) == inst_calc_man_trans_white && s->trans)) {
int refinst = 0;
int i, j, k;
a1logd(p->log,2,"\nDoing transmission white calibration\n");
if ((s->emiss || s->trans) && s->scan)
s->inttime = s->iinttime; /* Start with default integration time */
ev = i1pro3_trans_cal(p);
if (ev == I1PRO3_CAL_TRANSWHITEWARN) {
m->transwarn |= 1;
ev = I1PRO3_OK;
}
if (ev != I1PRO3_OK) {
a1logd(p->log,2,"i1pro3_trans_cal failed\n");
return ev;
}
s->cal_valid = 1;
s->cdate = cdate;
s->want_calib = 0;
*calt &= ~(inst_calt_trans_vwhite);
/* Save the calib to all similar modes */
a1logd(p->log,5,"Saving transmission white calib to similar modes\n");
for (i = 0; i < i1p3_no_modes; i++) {
i1pro3_state *ss = &m->ms[i];
if (ss == s || ss->cdate == s->cdate)
continue;
if (ss->trans) {
ss->want_calib = s->want_calib;
ss->cal_valid = s->cal_valid;
ss->cdate = s->cdate;
vect_cpy(ss->cal_factor[0], s->cal_factor[0], m->nwav[0]);
vect_cpy(ss->cal_factor[1], s->cal_factor[1], m->nwav[1]);
vect_cpy(ss->raw_white-1, s->raw_white-1, m->nraw+1);
}
}
a1logd(p->log,5,"Done transmission white calibration\n");
}
/* Deal with a display integration time selection */
if (s->diseldate != cdate
&& (*calt & (inst_calt_emis_int_time | inst_calt_ap_flag))
&& (*calc & inst_calc_cond_mask) == inst_calc_emis_white
&& (s->emiss && !s->adaptive && !s->scan)) {
double inttime = 0.05;
double **raw_sample;
int nummeas;
double _psample[MX_NRAW+1], *psample = _psample + 1;
double maxval;
a1logd(p->log,2,"\nDoing display integration time calibration\n");
s->inttime = s->iinttime; /* Start with default integration time */
/* returns raw, black subtracted, linearized */
ev = i1pro3_spot_simple_emis_raw_meas(p, &raw_sample, &nummeas, &inttime, 0.25, 0);
if (ev != I1PRO3_RD_SENSORSATURATED) {
return ev;
} else if (ev == I1PRO3_OK) {
i1pro3_average_rawmmeas(p, psample, raw_sample, nummeas);
maxval = vect_max(psample, m->nraw);
a1logd(p->log,4," display int. calib. maxval %f\n",maxval);
inttime = inttime * s->targoscale * m->sens_target/maxval;
if (inttime < m->min_int_time)
inttime = m->min_int_time;
if (inttime < s->inttime) { /* Decrease, no increase of na inttime */
s->inttime = inttime;
a1logd(p->log,5,"Display integration time reduced to %f\n",s->inttime);
}
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeas);
}
s->done_dintsel = 1;
s->diseldate = cdate;
*calt &= ~inst_calt_emis_int_time;
a1logd(p->log,5,"Done display integration time calibration\n");
}
} /* Look at next mode */
m->mmode = mmode; /* Restore actual mode */
/* Make sure there's the right condition for any remaining calibrations. */
if (*calt & (inst_calt_ref_white)) {
*idtype = inst_calc_id_ref_sn;
sprintf(id, "%d",m->ee_serno);
if ((*calc & inst_calc_cond_mask) != inst_calc_man_ref_white) {
/* Calibrate using white tile */
*calc = inst_calc_man_ref_white;
return I1PRO3_CAL_SETUP;
}
} else if (*calt & inst_calt_wavelength) { /* Wavelength calibration */
*idtype = inst_calc_id_ref_sn;
sprintf(id, "%d",m->ee_serno);
if ((*calc & inst_calc_cond_mask) != inst_calc_man_ref_white) {
/* Have to calibrate using white tile */
*calc = inst_calc_man_ref_white;
return I1PRO3_CAL_SETUP;
}
} else if (*calt & inst_calt_em_dark) { /* Emissive Dark calib */
*idtype = inst_calc_id_none;
id[0] = '\000';
if ((*calc & inst_calc_cond_mask) != inst_calc_man_em_dark) {
/* Any sort of dark reference */
*calc = inst_calc_man_em_dark;
return I1PRO3_CAL_SETUP;
}
} else if (*calt & inst_calt_trans_dark) { /* Transmissvice dark */
*idtype = inst_calc_id_none;
id[0] = '\000';
if ((*calc & inst_calc_cond_mask) != inst_calc_man_trans_dark) {
*calc = inst_calc_man_trans_dark;
return I1PRO3_CAL_SETUP;
}
} else if (*calt & inst_calt_trans_vwhite) {/* Transmissvice white for emulated transmission */
*idtype = inst_calc_id_none;
id[0] = '\000';
if ((*calc & inst_calc_cond_mask) != inst_calc_man_trans_white) {
*calc = inst_calc_man_trans_white;
return I1PRO3_CAL_SETUP;
}
} else if (*calt & inst_calt_emis_int_time) {
*idtype = inst_calc_id_none;
id[0] = '\000';
if ((*calc & inst_calc_cond_mask) != inst_calc_emis_white) {
*calc = inst_calc_emis_white;
return I1PRO3_CAL_SETUP;
}
}
/* Go around again if we've still got calibrations to do */
if (*calt & inst_calt_all_mask) {
return I1PRO3_CAL_SETUP;
}
/* We must be done */
#ifdef ENABLE_NONVCAL
/* Save the calibration to a file */
i1pro3_save_calibration(p);
#endif
if (m->transwarn) {
*calc = inst_calc_message;
if (m->transwarn & 2) {
*idtype = inst_calc_id_trans_low;
strcpy(id, "Warning: Transmission light source is too low for accuracy!");
} else {
*idtype = inst_calc_id_trans_wl;
strcpy(id, "Warning: Transmission light source is low at some wavelengths!");
}
m->transwarn = 0;
}
a1logd(p->log,2,"Finished cal with dark_valid = %d, cal_valid = %d\n",cs->dark_valid, cs->cal_valid);
return I1PRO3_OK;
}
/* Interpret an icoms error into a I1PRO3 error */
int icoms2i1pro3_err(int se) {
if (se != ICOM_OK)
return I1PRO3_COMS_FAIL;
return I1PRO3_OK;
}
/* - - - - - - - - - - - - - - - - */
/* Measure a display update delay. It is assumed that */
/* white_stamp(init) has been called, and then a */
/* white to black change has been made to the displayed color, */
/* and this will measure the time it took for the update to */
/* be noticed by the instrument, up to 2.0 seconds. */
/* (It is assumed that white_change() will be called at the time the patch */
/* changes color.) */
/* inst_misread will be returned on failure to find a transition to black. */
#define NDMXTIME 2.0 /* Maximum time to take */
#define NDSAMPS 1000 /* Debug samples >= 2.0/0.0025 */
typedef struct {
double sec;
double rgb[3];
double tot;
} i1rgbdsamp;
i1pro3_code i1pro3_imp_meas_delay(
i1pro3 *p,
int *pdispmsec, /* Return display update delay in msec */
int *pinstmsec) { /* Return instrument latency in msec */
i1pro3_code ev = I1PRO3_OK;
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
int refinst = 0;
int i, j, k, mm;
double **multimeas; /* Spectral measurements */
int nummeas;
double rgbw[3] = { 610.0, 520.0, 460.0 };
double inttime;
double rstart;
i1rgbdsamp *samp;
double stot, etot, del, thr;
double stime, etime;
int dispmsec, instmsec;
int hr = m->highres;
if (pinstmsec != NULL)
*pinstmsec = 0;
if ((rstart = usec_time()) < 0.0) {
a1loge(p->log, inst_internal_error, "i1pro3_imp_meas_delay: No high resolution timers\n");
return inst_internal_error;
}
/* Read the samples */
inttime = m->min_int_time;
nummeas = (int)(NDMXTIME/inttime + 0.5);
multimeas = dmatrix(0, nummeas-1, -1, m->nwav[hr]-1);
if ((samp = (i1rgbdsamp *)calloc(sizeof(i1rgbdsamp), nummeas)) == NULL) {
a1logd(p->log, 1, "i1pro3_meas_delay: malloc failed\n");
return I1PRO3_INT_MALLOC;
}
/* We rely on the measurement code setting m->trigstamp when the */
/* trigger packet is sent to the instrument */
if ((ev = i1pro3_spot_simple_emis_meas(p, multimeas, nummeas, &inttime, hr)) != inst_ok) {
free_dmatrix(multimeas, 0, nummeas-1, 0, m->nwav[hr]-1);
free(samp);
return ev;
}
if (m->whitestamp < 0.0) {
a1logd(p->log, 1, "i1pro3_meas_delay: White transition wasn't timestamped\n");
return inst_internal_error;
}
/* Convert the samples to RGB */
/* Add 10 msec fudge factor */
for (i = 0; i < nummeas; i++) {
samp[i].sec = i * inttime + (m->trigstamp - m->whitestamp)/1000000.0 + 0.01;
samp[i].rgb[0] = samp[i].rgb[1] = samp[i].rgb[2] = 0.0;
for (j = 0; j < m->nwav[hr]; j++) {
double wl = XSPECT_WL(m->wl_short[hr], m->wl_long[hr], m->nwav[hr], j);
//printf("~1 samp %d wl %d = %f\n",i, j, multimeas[i][j]);
for (k = 0; k < 3; k++) {
double tt = (double)(wl - rgbw[k]);
tt = (50.0 - fabs(tt))/50.0;
if (tt < 0.0)
tt = 0.0;
samp[i].rgb[k] += sqrt(tt) * multimeas[i][j];
}
}
samp[i].tot = samp[i].rgb[0] + samp[i].rgb[1] + samp[i].rgb[2];
}
free_dmatrix(multimeas, 0, nummeas-1, 0, m->nwav[hr]-1);
a1logd(p->log, 3, "i1pro3_meas_delay: Read %d samples for refresh calibration\n",nummeas);
/* Over the first 100msec, locate the maximum value */
stime = samp[0].sec;
stot = -1e9;
for (i = 0; i < nummeas; i++) {
if (samp[i].tot > stot)
stot = samp[i].tot;
if ((samp[i].sec - stime) > 0.1)
break;
}
/* Over the last 100msec, locate the maximum value */
etime = samp[nummeas-1].sec;
etot = -1e9;
for (i = nummeas-1; i >= 0; i--) {
if (samp[i].tot > etot)
etot = samp[i].tot;
if ((etime - samp[i].sec) > 0.1)
break;
}
del = etot - stot;
thr = stot + 0.30 * del; /* 30% of transition threshold */
#ifdef PLOT_UPDELAY
a1logd(p->log, 0, "i1pro3_meas_delay: start tot %f end tot %f del %f, thr %f\n", stot, etot, del, thr);
#endif
#ifdef PLOT_UPDELAY
/* Plot the raw sensor values */
{
double xx[NDSAMPS];
double y1[NDSAMPS];
double y2[NDSAMPS];
double y3[NDSAMPS];
double y4[NDSAMPS];
for (i = 0; i < nummeas && i < NDSAMPS; i++) {
xx[i] = samp[i].sec;
y1[i] = samp[i].rgb[0];
y2[i] = samp[i].rgb[1];
y3[i] = samp[i].rgb[2];
y4[i] = samp[i].tot;
//printf("%d: %f sec -> %f R+G+B\n",i,samp[i].sec, samp[i].tot);
}
printf("Display update delay measure sensor values and time (sec)\n");
do_plot6(xx, y1, y2, y3, y4, NULL, NULL, nummeas);
}
#endif
/* Check that there has been a transition */
if (del < 5.0) {
free(samp);
a1logd(p->log, 1, "i1pro3_meas_delay: can't detect change from black to white\n");
return I1PRO3_RD_NOTRANS_FOUND;
}
/* Working from the start, locate the time at which the level was above the threshold */
for (i = 0; i < (nummeas-1); i++) {
if (samp[i].tot > thr)
break;
}
a1logd(p->log, 2, "i1pro3_meas_delay: stoped at sample %d time %f\n",i,samp[i].sec);
/* Compute overall delay */
dispmsec = (int)(samp[i].sec * 1000.0 + 0.5); /* Display update time */
instmsec = (int)((m->trigstamp - rstart)/1000.0 + 0.5); /* Reaction time */
#ifdef PLOT_UPDELAY
a1logd(p->log, 0, "i1pro3_meas_delay: disp %d, trig %d msec\n",dispmsec,instmsec);
#else
a1logd(p->log, 2, "i1pro3_meas_delay: disp %d, trig %d msec\n",dispmsec,instmsec);
#endif
if (dispmsec < 0) /* This can happen if the patch generator delays it's return */
dispmsec = 0;
if (pdispmsec != NULL)
*pdispmsec = dispmsec;
if (pinstmsec != NULL)
*pinstmsec = instmsec;
#ifdef PLOT_UPDELAY
a1logd(p->log, 0, "i1pro3_meas_delay: returning %d & %d msec\n",dispmsec,instmsec);
#else
a1logd(p->log, 2, "i1pro3_meas_delay: returning %d & %d msec\n",dispmsec,instmsec);
#endif
free(samp);
return I1PRO3_OK;
}
#undef NDSAMPS
#undef NDMXTIME
/* Timestamp the white patch change during meas_delay() */
inst_code i1pro3_imp_white_change(i1pro3 *p, int init) {
i1pro3imp *m = (i1pro3imp *)p->m;
if (init)
m->whitestamp = -1.0;
else {
if ((m->whitestamp = usec_time()) < 0.0) {
a1loge(p->log, inst_internal_error, "i1pro3_imp_wite_change: No high resolution timers\n");
return inst_internal_error;
}
}
return inst_ok;
}
/* - - - - - - - - - - - - - - - - */
/* Measure a patch or strip in the current mode. */
i1pro3_code i1pro3_imp_measure(
i1pro3 *p,
ipatch *vals, /* Pointer to array of instrument patch value */
int nvals, /* Number of values */
instClamping clamp /* Clamp XYZ/Lab to be +ve */
) {
i1pro3_code ev = I1PRO3_OK;
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
double **specrd = NULL; /* Cooked spectral patch values */
double duration = 0.0; /* Possible flash duration value */
int user_trig = 0;
int hr = m->highres;
if (s->reflective)
hr = 0; /* hi-res doesn't work well for reflective... */
a1logd(p->log,2,"i1pro3_imp_measure: Taking %d measurments in %s%s%s%s%s mode called\n", nvals,
s->emiss ? "Emission" : s->trans ? "Trans" : "Refl",
s->emiss && s->ambient ? " Ambient" : "",
s->scan ? " Scan" : "",
s->flash ? " Flash" : "",
s->adaptive ? " Adaptive" : "");
if ((ev = i1pro3_check_calib(p)) != I1PRO3_OK) /* Check if calibration have expired */
return ev;
/* Is the used calibration state invalid ? */
if ((s->dc_use != i1p3_dc_none && !s->dark_valid)
|| !s->cal_valid) {
a1logd(p->log,3,"dc_use %d dark_valid %d, cal_valid %d\n",s->dc_use,s->dark_valid,s->cal_valid);
a1logd(p->log,3,"i1pro3_imp_measure need calibration\n");
return I1PRO3_RD_NEEDS_CAL;
}
if (nvals <= 0
|| (!s->scan && nvals > 1)) {
a1logd(p->log,2,"i1pro3_imp_measure wrong number of patches\n");
return I1PRO3_INT_WRONGPATCHES;
}
/* Buffer for returned measurements */
specrd = dmatrix(0, nvals-1, 0, m->nwav[hr]-1);
if (m->trig == inst_opt_trig_user_switch) {
m->hide_event = 1; /* Supress instrument events */
#ifdef USE_THREAD
{
int currcount = m->switch_count; /* Variable set by thread */
while (currcount == m->switch_count) {
inst_code rc;
int cerr;
/* Don't trigger on user key if scan, only trigger */
/* on instrument event */
if (p->uicallback != NULL
&& (rc = p->uicallback(p->uic_cntx, inst_armed)) != inst_ok) {
if (rc == inst_user_abort) {
ev = I1PRO3_USER_ABORT;
break; /* Abort */
}
if (!s->scan && rc == inst_user_trig) {
ev = I1PRO3_USER_TRIG;
user_trig = 1;
break; /* Trigger */
}
}
msec_sleep(100);
}
}
#else
/* Throw one away in case the event was pressed prematurely */
i1pro3_waitfor_event_th(p, NULL, 0.01);
for (;;) {
inst_code rc;
i1pro3_eve;
int nerrs = 0;
int cerr;
if ((ev = i1pro3_waitfor_event_th(p, &ecode, 0.1)) != I1PRO3_OK
&& ev != I1PRO3_INT_BUTTONTIMEOUT) {
if (++nerrs < 5)
continue; /* Retry for more robustness */
break;
}
if (ev == I1PRO3_OK)
break; /* event triggered */
/* Don't trigger on user key if scan, only trigger */
/* on instrument event */
if (p->uicallback != NULL
&& (rc = p->uicallback(p->uic_cntx, inst_armed)) != inst_ok) {
if (rc == inst_user_abort) {
ev = I1PRO3_USER_ABORT;
break; /* Abort */
}
if (!s->scan && rc == inst_user_trig) {
ev = I1PRO3_USER_TRIG;
user_trig = 1;
break; /* Trigger */
}
}
}
#endif
a1logd(p->log,3,"############# triggered ##############\n");
if (p->uicallback) /* Notify of trigger */
p->uicallback(p->uic_cntx, inst_triggered);
m->hide_event = 0; /* Enable event events again */
} else if (m->trig == inst_opt_trig_user) {
if (p->uicallback == NULL) {
a1logd(p->log, 1, "hcfr: inst_opt_trig_user but no uicallback function set!\n");
ev = I1PRO3_UNSUPPORTED;
} else {
for (;;) {
inst_code rc;
if ((rc = p->uicallback(p->uic_cntx, inst_armed)) != inst_ok) {
if (rc == inst_user_abort) {
ev = I1PRO3_USER_ABORT; /* Abort */
break;
}
if (rc == inst_user_trig) {
ev = I1PRO3_USER_TRIG;
user_trig = 1;
break; /* Trigger */
}
}
msec_sleep(200);
}
}
a1logd(p->log,3,"############# triggered ##############\n");
if (p->uicallback) /* Notify of trigger */
p->uicallback(p->uic_cntx, inst_triggered);
/* Progromatic Trigger */
} else {
/* Check for abort */
if (p->uicallback != NULL
&& (ev = p->uicallback(p->uic_cntx, inst_armed)) == inst_user_abort)
ev = I1PRO3_USER_ABORT; /* Abort */
}
if (ev != I1PRO3_OK && ev != I1PRO3_USER_TRIG) {
free_dmatrix(specrd, 0, nvals-1, 0, m->nwav[hr]-1);
a1logd(p->log,2,"i1pro3_imp_measure user aborted, terminated, command, or failure\n");
return ev; /* User abort, term, command or failure */
}
/* Take a measurement reading using the current mode. */
/* Converts to completely processed output readings. */
a1logd(p->log,2,"Do main measurement reading\n");
if (s->emiss || s->trans) {
if (s->scan) {
if ((ev = i1pro3_scan_emis_meas(p, &duration, specrd, nvals)) !=I1PRO3_OK) {
free_dmatrix(specrd, 0, nvals-1, 0, m->nwav[hr]-1);
a1logd(p->log,2,"i1pro3_imp_measure failed at i1pro3_spot_adapt_emis_meas\n");
return ev;
}
} else {
if ((ev = i1pro3_spot_adapt_emis_meas(p, specrd)) !=I1PRO3_OK) {
free_dmatrix(specrd, 0, nvals-1, 0, m->nwav[hr]-1);
a1logd(p->log,2,"i1pro3_imp_measure failed at i1pro3_spot_adapt_emis_meas\n");
return ev;
}
}
}
if (s->reflective) {
if (m->filt == inst_opt_filter_pol) {
if (s->scan) {
/* Do a pol strip reflectance measurement and return spectrum of each patch */
if ((ev = i1pro3_pol_strip_refl_meas(p, specrd, nvals, hr)) !=I1PRO3_OK) {
free_dmatrix(specrd, 0, nvals-1, 0, m->nwav[hr]-1);
a1logd(p->log,2,"i1pro3_imp_measure failed at i1pro3_pol_strip_refl_meas\n");
return ev;
}
} else {
/* Do a pol spot reflectance measurement and return spectrum */
if ((ev = i1pro3_pol_spot_refl_meas(p, specrd, hr)) !=I1PRO3_OK) {
free_dmatrix(specrd, 0, nvals-1, 0, m->nwav[hr]-1);
a1logd(p->log,2,"i1pro3_imp_measure failed at i1pro3_pol_spot_refl_meas\n");
return ev;
}
}
} else {
if (s->scan) {
/* Do a strip reflectance measurement and return spectrum of each patch */
if ((ev = i1pro3_strip_refl_meas(p, specrd, nvals, hr)) !=I1PRO3_OK) {
free_dmatrix(specrd, 0, nvals-1, 0, m->nwav[hr]-1);
a1logd(p->log,2,"i1pro3_imp_measure failed at i1pro3_strip_refl_meas\n");
return ev;
}
} else {
/* Do a spot reflectance measurement and return spectrum */
if ((ev = i1pro3_spot_refl_meas(p, specrd, hr)) !=I1PRO3_OK) {
free_dmatrix(specrd, 0, nvals-1, 0, m->nwav[hr]-1);
a1logd(p->log,2,"i1pro3_imp_measure failed at i1pro3_spot_refl_meas\n");
return ev;
}
}
}
}
/* Transfer spectral and convert to XYZ */
if ((ev = i1pro3_conv2XYZ(p, vals, nvals, specrd, hr, clamp)) != I1PRO3_OK) {
free_dmatrix(specrd, 0, nvals-1, 0, m->nwav[hr]-1);
a1logd(p->log,2,"i1pro3_imp_measure failed at i1pro3_conv2XYZ\n");
return ev;
}
free_dmatrix(specrd, 0, nvals-1, 0, m->nwav[hr]-1);
if (nvals > 0)
vals[0].duration = duration; /* Possible flash duration */
a1logd(p->log,3,"i1pro3_imp_measure sucessful return\n");
if (user_trig)
return I1PRO3_USER_TRIG;
return ev;
}
/* - - - - - - - - - - - - - - - - */
/*
Determining the refresh rate for a refresh type display.
This is easy when the max sample rate of the i1 is above
the nyquist of the display, and will always be the case
for the range we are prepared to measure (up to 100Hz)
when using the i1Pro3, but is a problem for other
instruments, so there is generic code here to
work around this problem by detecting when
we are measuring an alias of the refresh rate, and
average the aliasing corrected measurements.
If there is no aparent refresh, or the refresh rate is not determinable,
return a period of 0.0 and inst_ok;
*/
i1pro3_code i1pro3_measure_rgb(i1pro3 *p, double *inttime, double *rgb);
#ifndef PSRAND32L
# define PSRAND32L(S) ((S) * 1664525L + 1013904223L)
#endif
#undef FREQ_SLOW_PRECISE /* [und] Interpolate then autocorrelate, else autc & filter */
#define NFSAMPS 160 /* [80] Number of samples to read */
#define PBPMS 20 /* bins per msec */
#define PERMIN ((1000 * PBPMS)/40) /* 40 Hz */
#define PERMAX ((1000 * PBPMS)/4) /* 4 Hz*/
#define NPER (PERMAX - PERMIN + 1)
#define PWIDTH (8 * PBPMS) /* 8 msec bin spread to look for peak in */
#define MAXPKS 20 /* Number of peaks to find */
#define TRIES 8 /* Number of different sample rates to try */
i1pro3_code i1pro3_imp_meas_refrate(
i1pro3 *p,
double *ref_rate
) {
i1pro3_code ev = I1PRO3_OK;
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
int i, j, k, mm;
double **multimeas; /* Spectral measurements */
int nummeas;
double rgbw[3] = { 610.0, 520.0, 460.0 };
double inttime;
static unsigned int randn = 0x12345678;
struct {
double sec;
double rgb[3];
} samp[NFSAMPS * 2];
int nfsamps; /* Actual samples read */
double minv[3]; /* Minimum reading */
double maxv[3]; /* Maximum reading */
double maxt; /* Time range */
#ifdef FREQ_SLOW_PRECISE
int nbins;
double *bins[3]; /* PBPMS sample bins */
#else
double tcorr[NPER]; /* Temp for initial autocorrelation */
int ntcorr[NPER]; /* Number accumulated */
#endif
double corr[NPER]; /* Filtered correlation for each period value */
double mincv, maxcv; /* Max and min correlation values */
double crange; /* Correlation range */
double peaks[MAXPKS]; /* Peak wavelength */
double peakh[MAXPKS]; /* Peak heighheight */
int npeaks; /* Number of peaks */
double pval; /* Period value */
double rfreq[TRIES]; /* Computed refresh frequency for each try */
double rsamp[TRIES]; /* Sampling rate used to measure frequency */
int tix = 0; /* try index */
int hr = m->highres;
a1logd(p->log,2,"i1pro3_imp_meas_refrate called\n");
if (ref_rate != NULL)
*ref_rate = 0.0;
if (!s->emiss) {
a1logd(p->log,2,"i1pro3_imp_meas_refrate not in emissive mode\n");
return I1PRO3_UNSUPPORTED;
}
for (mm = 0; mm < TRIES; mm++) {
rfreq[mm] = 0.0;
npeaks = 0; /* Number of peaks */
nummeas = NFSAMPS;
multimeas = dmatrix(0, nummeas-1, -1, m->nwav[hr]-1);
if (mm == 0)
inttime = m->min_int_time;
else {
double rval, dmm;
randn = PSRAND32L(randn);
rval = (double)randn/4294967295.0;
dmm = ((double)mm + rval - 0.5)/(TRIES - 0.5);
inttime = m->min_int_time * (1.0 + dmm * 0.80);
}
if ((ev = i1pro3_spot_simple_emis_meas(p, multimeas, nummeas, &inttime, hr)) != inst_ok) {
free_dmatrix(multimeas, 0, nummeas-1, 0, m->nwav[hr]-1);
return ev;
}
rsamp[tix] = 1.0/inttime;
/* Convert the samples to RGB */
for (i = 0; i < nummeas && i < NFSAMPS; i++) {
samp[i].sec = i * inttime;
samp[i].rgb[0] = samp[i].rgb[1] = samp[i].rgb[2] = 0.0;
for (j = 0; j < m->nwav[hr]; j++) {
double wl = XSPECT_WL(m->wl_short[hr], m->wl_long[hr], m->nwav[hr], j);
//printf("~1 multimeas %d %d = %f\n",i, j, multimeas[i][j]);
for (k = 0; k < 3; k++) {
double tt = (double)(wl - rgbw[k]);
tt = (40.0 - fabs(tt))/40.0;
if (tt < 0.0)
tt = 0.0;
samp[i].rgb[k] += sqrt(tt) * multimeas[i][j];
}
}
}
free_dmatrix(multimeas, 0, nummeas-1, 0, m->nwav[hr]-1);
nfsamps = i;
a1logd(p->log, 3, "i1pro3_meas_refrate: Read %d samples for refresh calibration\n",nfsamps);
#ifdef NEVER
/* Plot the raw sensor values */
{
double xx[NFSAMPS];
double y1[NFSAMPS];
double y2[NFSAMPS];
double y3[NFSAMPS];
for (i = 0; i < nfsamps; i++) {
xx[i] = samp[i].sec;
y1[i] = samp[i].rgb[0];
y2[i] = samp[i].rgb[1];
y3[i] = samp[i].rgb[2];
// printf("%d: %f -> %f\n",i,samp[i].sec, samp[i].rgb[0]);
}
printf("Fast scan sensor values and time (sec)\n");
do_plot6(xx, y1, y2, y3, NULL, NULL, NULL, nfsamps);
}
#endif
/* Locate the smallest values and maximum time */
maxt = -1e6;
minv[0] = minv[1] = minv[2] = 1e20;
maxv[0] = maxv[1] = maxv[2] = -11e20;
for (i = nfsamps-1; i >= 0; i--) {
if (samp[i].sec > maxt)
maxt = samp[i].sec;
for (j = 0; j < 3; j++) {
if (samp[i].rgb[j] < minv[j])
minv[j] = samp[i].rgb[j];
if (samp[i].rgb[j] > maxv[j])
maxv[j] = samp[i].rgb[j];
}
}
/* Re-zero the sample times, and normalise the readings */
for (i = nfsamps-1; i >= 0; i--) {
samp[i].sec -= samp[0].sec;
if (samp[i].sec > maxt)
maxt = samp[i].sec;
for (j = 0; j < 3; j++) {
samp[i].rgb[j] -= minv[j];
}
}
#ifdef FREQ_SLOW_PRECISE /* Interp then autocorrelate */
/* Create PBPMS bins and interpolate readings into them */
nbins = 1 + (int)(maxt * 1000.0 * PBPMS + 0.5);
for (j = 0; j < 3; j++) {
if ((bins[j] = (double *)calloc(sizeof(double), nbins)) == NULL) {
a1loge(p->log, inst_internal_error, "i1pro3_meas_refrate: malloc failed\n");
return I1PRO3_INT_MALLOC;
}
}
/* Do the interpolation */
for (k = 0; k < (nfsamps-1); k++) {
int sbin, ebin;
sbin = (int)(samp[k].sec * 1000.0 * PBPMS + 0.5);
ebin = (int)(samp[k+1].sec * 1000.0 * PBPMS + 0.5);
for (i = sbin; i <= ebin; i++) {
double bl;
#if defined(__APPLE__) && defined(__POWERPC__)
gcc_bug_fix(i);
#endif
bl = (i - sbin)/(double)(ebin - sbin); /* 0.0 to 1.0 */
for (j = 0; j < 3; j++) {
bins[j][i] = (1.0 - bl) * samp[k].rgb[j] + bl * samp[k+1].rgb[j];
}
}
}
#ifdef NEVER
/* Plot interpolated values */
{
double *xx;
double *y1;
double *y2;
double *y3;
xx = malloc(sizeof(double) * nbins);
y1 = malloc(sizeof(double) * nbins);
y2 = malloc(sizeof(double) * nbins);
y3 = malloc(sizeof(double) * nbins);
if (xx == NULL || y1 == NULL || y2 == NULL || y3 == NULL) {
a1loge(p->log, inst_internal_error, "i1pro3_meas_refrate: malloc failed\n");
for (j = 0; j < 3; j++)
free(bins[j]);
return I1PRO3_INT_MALLOC;
}
for (i = 0; i < nbins; i++) {
xx[i] = i / (double)PBPMS; /* msec */
y1[i] = bins[0][i];
y2[i] = bins[1][i];
y3[i] = bins[2][i];
}
printf("Interpolated fast scan sensor values and time (msec) for inttime %f\n",inttime);
do_plot6(xx, y1, y2, y3, NULL, NULL, NULL, nbins);
free(xx);
free(y1);
free(y2);
free(y3);
}
#endif /* PLOT_REFRESH */
/* Compute auto-correlation at 1/PBPMS msec intervals */
/* from 25 msec (40Hz) to 100msec (10 Hz) */
mincv = 1e48, maxcv = -1e48;
for (i = 0; i < NPER; i++) {
int poff = PERMIN + i; /* Offset to corresponding sample */
corr[i] = 0;
for (k = 0; (k + poff) < nbins; k++) {
corr[i] += bins[0][k] * bins[0][k + poff]
+ bins[1][k] * bins[1][k + poff]
+ bins[2][k] * bins[2][k + poff];
}
corr[i] /= (double)k; /* Normalize */
if (corr[i] > maxcv)
maxcv = corr[i];
if (corr[i] < mincv)
mincv = corr[i];
}
/* Free the bins */
for (j = 0; j < 3; j++)
free(bins[j]);
#else /* !FREQ_SLOW_PRECISE Fast - autocorrellate then filter */
/* Upsample by a factor of 2 */
for (i = nfsamps-1; i >= 0; i--) {
j = 2 * i;
samp[j].sec = samp[i].sec;
samp[j].rgb[0] = samp[i].rgb[0];
samp[j].rgb[1] = samp[i].rgb[1];
samp[j].rgb[2] = samp[i].rgb[2];
if (i > 0) {
j--;
samp[j].sec = 0.5 * (samp[i].sec + samp[i-1].sec);
samp[j].rgb[0] = 0.5 * (samp[i].rgb[0] + samp[i-1].rgb[0]);
samp[j].rgb[1] = 0.5 * (samp[i].rgb[1] + samp[i-1].rgb[1]);
samp[j].rgb[2] = 0.5 * (samp[i].rgb[2] + samp[i-1].rgb[2]);
}
}
nfsamps = 2 * nfsamps - 1;
/* Do point by point correllation of samples */
for (i = 0; i < NPER; i++) {
tcorr[i] = 0.0;
ntcorr[i] = 0;
}
for (j = 0; j < (nfsamps-1); j++) {
for (k = j+1; k < nfsamps; k++) {
double del, cor;
int bix;
del = samp[k].sec - samp[j].sec;
bix = (int)(del * 1000.0 * PBPMS + 0.5);
if (bix < PERMIN)
continue;
if (bix > PERMAX)
break;
bix -= PERMIN;
cor = samp[j].rgb[0] * samp[k].rgb[0]
+ samp[j].rgb[1] * samp[k].rgb[1]
+ samp[j].rgb[2] * samp[k].rgb[2];
//printf("~1 j %d k %d, del %f bix %d cor %f\n",j,k,del,bix,cor);
tcorr[bix] += cor;
ntcorr[bix]++;
}
}
/* Divide out count and linearly interpolate */
j = 0;
for (i = 0; i < NPER; i++) {
if (ntcorr[i] > 0) {
tcorr[i] /= ntcorr[i];
if ((i - j) > 1) {
if (j == 0) {
for (k = j; k < i; k++)
tcorr[k] = tcorr[i];
} else { /* Linearly interpolate from last value */
double ww = (double)i-j;
for (k = j+1; k < i; k++) {
double bl = (k-j)/ww;
tcorr[k] = (1.0 - bl) * tcorr[j] + bl * tcorr[i];
}
}
}
j = i;
}
}
if (j < (NPER-1)) {
for (k = j+1; k < NPER; k++) {
tcorr[k] = tcorr[j];
}
}
#ifdef PLOT_REFRESH
/* Plot unfiltered auto correlation */
{
double xx[NPER];
double y1[NPER];
for (i = 0; i < NPER; i++) {
xx[i] = (i + PERMIN) / (double)PBPMS; /* msec */
y1[i] = tcorr[i];
}
printf("Unfiltered auto correlation (msec)\n");
do_plot6(xx, y1, NULL, NULL, NULL, NULL, NULL, NPER);
}
#endif /* PLOT_REFRESH */
/* Apply a gausian filter */
#define FWIDTH 100
{
double gaus_[2 * FWIDTH * PBPMS + 1];
double *gaus = &gaus_[FWIDTH * PBPMS];
double bb = 1.0/pow(2, 5.0);
double fw = inttime * 1000.0;
int ifw;
//printf("~1 sc = %f = %f msec\n",1.0/inttime, fw);
//printf("~1 fw = %f, ifw = %d\n",fw,ifw);
fw *= 0.9;
ifw = (int)ceil(fw * PBPMS);
if (ifw > FWIDTH * PBPMS)
error("i1pro3: Not enough space for lanczos 2 filter");
for (j = -ifw; j <= ifw; j++) {
double x, y;
x = j/(PBPMS * fw);
if (fabs(x) > 1.0)
y = 0.0;
else
y = 1.0/pow(2, 5.0 * x * x) - bb;
gaus[j] = y;
//printf("~1 gaus[%d] = %f\n",j,y);
}
for (i = 0; i < NPER; i++) {
double sum = 0.0;
double wght = 0.0;
for (j = -ifw; j <= ifw; j++) {
double w;
int ix = i + j;
if (ix < 0)
ix = -ix;
if (ix > (NPER-1))
ix = 2 * NPER-1 - ix;
w = gaus[j];
sum += w * tcorr[ix];
wght += w;
}
//printf("~1 corr[%d] wgt = %f\n",i,wght);
corr[i] = sum / wght;
}
}
/* Compute min & max */
mincv = 1e48, maxcv = -1e48;
for (i = 0; i < NPER; i++) {
if (corr[i] > maxcv)
maxcv = corr[i];
if (corr[i] < mincv)
mincv = corr[i];
}
#endif /* !FREQ_SLOW_PRECISE Fast - autocorrellate then filter */
crange = maxcv - mincv;
a1logd(p->log,3,"Correlation value range %f - %f = %f = %f%%\n",mincv, maxcv,crange, 100.0 * (maxcv-mincv)/maxcv);
#ifdef PLOT_REFRESH
/* Plot this measuremnts auto correlation */
{
double xx[NPER];
double y1[NPER];
for (i = 0; i < NPER; i++) {
xx[i] = (i + PERMIN) / (double)PBPMS; /* msec */
y1[i] = corr[i];
}
printf("Auto correlation (msec)\n");
do_plot6(xx, y1, NULL, NULL, NULL, NULL, NULL, NPER);
}
#endif /* PLOT_REFRESH */
#define PFDB 4 // normally 4
/* If there is sufficient level and distict correlations */
if (crange/maxcv >= 0.1) {
a1logd(p->log,PFDB,"Searching for peaks\n");
/* Locate all the peaks starting at the longest correllation */
for (i = (NPER-1-PWIDTH); i >= 0 && npeaks < MAXPKS; i--) {
double v1, v2, v3;
v1 = corr[i];
v2 = corr[i + PWIDTH/2]; /* Peak */
v3 = corr[i + PWIDTH];
if (fabs(v3 - v1)/crange < 0.05
&& (v2 - v1)/crange > 0.025
&& (v2 - v3)/crange > 0.025
&& (v2 - mincv)/crange > 0.5) {
double pkv; /* Peak value */
int pki; /* Peak index */
double ii, bl;
#ifdef PLOT_REFRESH
a1logd(p->log,PFDB,"Max between %f and %f msec\n",
(i + PERMIN)/(double)PBPMS,(i + PWIDTH + PERMIN)/(double)PBPMS);
#endif
/* Locate the actual peak */
pkv = -1.0;
pki = 0;
for (j = i; j < (i + PWIDTH); j++) {
if (corr[j] > pkv) {
pkv = corr[j];
pki = j;
}
}
#ifdef PLOT_REFRESH
a1logd(p->log,PFDB,"Peak is at %f msec, %f corr\n", (pki + PERMIN)/(double)PBPMS, pkv);
#endif
/* Interpolate the peak value for higher precision */
/* j = bigest */
if (corr[pki-1] > corr[pki+1]) {
j = pki-1;
k = pki+1;
} else {
j = pki+1;
k = pki-1;
}
bl = (corr[pki] - corr[j])/(corr[pki] - corr[k]);
bl = (bl + 1.0)/2.0;
ii = bl * pki + (1.0 - bl) * j;
pval = (ii + PERMIN)/(double)PBPMS;
#ifdef PLOT_REFRESH
a1logd(p->log,PFDB,"Interpolated peak is at %f msec\n", pval);
#endif
peaks[npeaks] = pval;
peakh[npeaks] = corr[pki];
npeaks++;
i -= PWIDTH;
}
#ifdef NEVER
if (v2 > v1 && v2 > v3) {
printf("Peak rehjected:\n");
printf("(v3 - v1)/crange = %f < 0.05 ?\n",fabs(v3 - v1)/crange);
printf("(v2 - v1)/crange = %f > 0.025 ?\n",(v2 - v1)/crange);
printf("(v2 - v3)/crange = %f > 0.025 ?\n",(v2 - v3)/crange);
printf("(v2 - mincv)/crange = %f > 0.5 ?\n",(v2 - mincv)/crange);
}
#endif
}
a1logd(p->log,3,"Number of peaks located = %d\n",npeaks);
} else {
a1logd(p->log,3,"All rejected, crange/maxcv = %f < 0.06\n",crange/maxcv);
}
#undef PFDB
a1logd(p->log,3,"Number of peaks located = %d\n",npeaks);
if (npeaks > 1) { /* Compute aparent refresh rate */
int nfails;
double div, avg, ano;
/* Try and locate a common divisor amongst all the peaks. */
/* This is likely to be the underlying refresh rate. */
for (k = 0; k < npeaks; k++) {
for (j = 1; j < 25; j++) {
avg = ano = 0.0;
div = peaks[k]/(double)j;
if (div < 5.0)
continue; /* Skip anything higher than 200Hz */
//printf("~1 trying %f Hz\n",1000.0/div);
for (nfails = i = 0; i < npeaks; i++) {
double rem, cnt;
rem = peaks[i]/div;
cnt = floor(rem + 0.5);
rem = fabs(rem - cnt);
#ifdef PLOT_REFRESH
a1logd(p->log, 3, "remainder for peak %d = %f\n",i,rem);
#endif
if (rem > 0.06) {
if (++nfails > 2)
break; /* Fail this divisor */
} else {
avg += peaks[i]; /* Already weighted by cnt */
ano += cnt;
}
}
if (nfails == 0 || (nfails <= 2 && npeaks >= 6))
break; /* Sucess */
/* else go and try a different divisor */
}
if (j < 25)
break; /* Success - found common divisor */
}
if (k >= npeaks) {
a1logd(p->log,3,"Failed to locate common divisor\n");
} else {
pval = 0.001 * avg/ano;
if (pval < inttime) {
a1logd(p->log,3,"Discarding frequency %f > sample rate %f\n",1.0/pval, 1.0/inttime);
} else {
pval = 1.0/pval; /* Convert to frequency */
rfreq[tix++] = pval;
a1logd(p->log,3,"Located frequency %f sum %f dif %f\n",pval, pval + 1.0/inttime, fabs(pval - 1.0/inttime));
}
}
}
}
if (tix >= 3) {
for (mm = 0; mm < tix; mm++) {
a1logd(p->log, 3, "Try %d, samp %f Hz, Meas %f Hz, Sum %f Hz, Dif %f Hz\n",mm,rsamp[mm],rfreq[mm], rsamp[mm] + rfreq[mm], fabs(rsamp[mm] - rfreq[mm]));
}
/* Decide if we are above the nyquist, or whether */
/* we have aliases of the fundamental */
{
double brange = 1e38;
double brate = 0.0;
int bsplit = -1;
double min, max, avg, range;
int split, mul, niia;
/* Compute fundamental and sub aliases at all possible splits. */
/* Skip the reading at the split. */
for (split = tix; split >= -1; split--) {
min = 1e38; max = -1e38; avg = 0.0; niia = 0;
for (mm = 0; mm < tix; mm++) {
double alias;
if (mm == split)
continue;
if (mm < split)
alias = rfreq[mm];
else
alias = fabs(rsamp[mm] - rfreq[mm]);
avg += alias;
niia++;
if (alias < min)
min = alias;
if (alias > max)
max = alias;
}
avg /= (double)niia;
range = (max - min)/(max + min);
//printf("~1 split %d avg = %f, range = %f\n",split,avg,range);
if (range < brange) {
brange = range;
brate = avg;
bsplit = split;
}
}
/* Compute sub and add aliases at all possible splits */
/* Skip the reading at the split. */
for (split = tix; split >= -1; split--) {
min = 1e38; max = -1e38; avg = 0.0; niia = 0;
for (mm = 0; mm < tix; mm++) {
double alias;
if (mm == split)
continue;
if (mm < split)
alias = fabs(rsamp[mm] - rfreq[mm]);
else
alias = rsamp[mm] + rfreq[mm];
avg += alias;
niia++;
if (alias < min)
min = alias;
if (alias > max)
max = alias;
}
avg /= (double)niia;
range = (max - min)/(max + min);
//printf("~1 split %d avg = %f, range = %f\n",100 + split,avg,range);
if (range < brange) {
brange = range;
brate = avg;
bsplit = 100 + split;
}
}
a1logd(p->log, 3, "Selected split %d range %f\n",bsplit,brange);
/* Hmm. Could reject result and re-try if brange is too large ? ( > 0.005 ?) */
if (brange > 0.05) {
a1logd(p->log, 3, "Readings are too inconsistent (brange %.1f%%) - should retry ?\n",brange * 100.0);
} else {
if (ref_rate != NULL)
*ref_rate = brate;
/* Error against my 85Hz CRT - GWG */
// a1logd(p->log, 1, "Refresh rate %f Hz, error = %.4f%%\n",brate,100.0 * fabs(brate - 85.0)/(85.0));
return I1PRO3_OK;
}
}
} else {
a1logd(p->log, 3, "Not enough tries suceeded to determine refresh rate\n");
}
return I1PRO3_RD_NOREFR_FOUND;
}
#undef NFSAMPS
#undef PBPMS
#undef PERMIN
#undef PERMAX
#undef NPER
#undef PWIDTH
/* - - - - - - - - - - - - - */
/* Save/Restore support code */
#ifdef ENABLE_NONVCAL
/* non-volatile save/checksum/restore state */
typedef struct {
int op; /* Operation, 0 = just checksum, 1 = write & checksum, 2 = read & checksum */
i1pro3 *p;
int ef; /* Error flag, 1 = write or read failed, 2 = close failed, 3 = malloc */
unsigned int chsum; /* Checksum */
int nbytes; /* Number of bytes checksummed */
unsigned char *buf; /* Temporary buffer */
unsigned int bsize; /* Buffer size */
} i1pnonv;
static void update_chsum(i1pnonv *x, unsigned char *p, int nn) {
int i;
for (i = 0; i < nn; i++, p++)
x->chsum = ((x->chsum << 5) | (((1 << 5)-1) & (x->chsum >> (32-5)))) + *p;
x->nbytes += nn;
}
/* Just checksum, write & checksum, or read & checksum a chunk of data */
static void nv_op(i1pnonv *x, FILE *fp, unsigned char *bp, unsigned int size) {
int i;
if (x->ef != 0)
return;
if (x->op == 0) { /* Read into dumy buffer */
if (size > x->bsize) {
if ((x->buf = realloc(x->buf, size)) == NULL) {
a1logd(x->p->log,1,"nv_op: realloc size %u failed at line %d",size);
x->ef = 3;
return;
}
x->bsize = size;
}
bp = x->buf;
}
if (x->op == 1) { /* Write */
if (fwrite((void *)bp, 1, size, fp) != size) {
x->ef = 1;
return;
}
} else { /* Read or just checksum */
if (fread((void *)bp, 1, size, fp) != size) {
x->ef = 1;
return;
}
}
for (i = 0; i < size; i++)
x->chsum = ((x->chsum << 13) | (x->chsum >> (32-13))) + bp[i];
x->nbytes += size;
}
/* Operate on an array of ints to/from the file. Set the error flag to nz on error */
static void nv_ints(i1pnonv *x, FILE *fp, int *dp, int n) {
unsigned char *bp = (unsigned char *)dp;
unsigned int size = sizeof(int) * n;
nv_op(x, fp, bp, size);
}
/* Operate on an array of doubles to the file. Set the error flag to nz on error */
static void nv_doubles(i1pnonv *x, FILE *fp, double *dp, int n) {
unsigned char *bp = (unsigned char *)dp;
unsigned int size = sizeof(double) * n;
nv_op(x, fp, bp, size);
}
/* Operate on an array of time_t's to the file. Set the error flag to nz on error */
/* (This will cause file checksum fail if different executables on the same */
/* system have different time_t values) */
static void nv_time_ts(i1pnonv *x, FILE *fp, time_t *dp, int n) {
unsigned char *bp = (unsigned char *)dp;
unsigned int size = sizeof(time_t) * n;
nv_op(x, fp, bp, size);
}
/* Write an array of ints to the file. Set the error flag to nz on error */
static void write_ints(i1pnonv *x, FILE *fp, int *dp, int n) {
if (fwrite((void *)dp, sizeof(int), n, fp) != n) {
x->ef = 1;
}
}
/* Read an array of ints from the file. Set the error flag to nz on error */
static void read_ints(i1pnonv *x, FILE *fp, int *dp, int n) {
if (fread((void *)dp, sizeof(int), n, fp) != n) {
x->ef = 1;
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Do a calibration checksum/write & checksum/read & checksum operation */
i1pro3_code i1pro3_nv_op(i1pro3 *p, i1pnonv *x, FILE *fp) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s;
i1pro3_code ev = I1PRO3_OK;
int op = x->op;
int i;
a1logd(p->log,2,"i1pro3_nv_op %d\n",x->op);
/* Read the Argyll version and structure signature on just checksum */
if (op == 0)
x->op = 2;
nv_ints(x, fp, &m->nv_av, 1);
nv_ints(x, fp, &m->nv_ss, 1);
nv_ints(x, fp, &m->nv_serno, 1);
nv_ints(x, fp, &m->nv_nraw, 1);
nv_ints(x, fp, &m->nv_nwav0, 1);
nv_ints(x, fp, &m->nv_nwav1, 1);
x->op = op;
/* Do operation for each mode */
for (i = 0; i < i1p3_no_modes; i++) {
s = &m->ms[i];
/* wl calibration is used by all modes */
nv_ints(x, fp, &s->wl_valid, 1);
nv_time_ts(x, fp, &s->wl_date, 1);
nv_doubles(x, fp, &s->wl_temp, 1);
nv_doubles(x, fp, &s->wl_cal_raw_off, 1);
nv_doubles(x, fp, &s->wl_cal_wav_off, 1);
/* Selected modes save/restore inttime */
if (s->pol
|| (s->emiss && !s->scan && s->adaptive)
|| (s->emiss && s->scan)
|| (s->trans && s->scan))
nv_doubles(x, fp, &s->inttime, 1);
/* Dark calibration */
if (s->dc_use != i1p3_dc_none) {
nv_ints(x, fp, &s->dark_valid, 1);
nv_doubles(x, fp, &s->dtemp, 1);
nv_time_ts(x, fp, &s->ddate, 1);
}
if (s->dc_use == i1p3_dc_adaptive) {
nv_doubles(x, fp, s->idark_data[0]-1, m->nraw+1);
nv_doubles(x, fp, s->idark_data[1]-1, m->nraw+1);
}
/* Main calibration is always used */
nv_ints(x, fp, &s->cal_valid, 1);
nv_time_ts(x, fp, &s->cdate, 1);
if (s->mc_use == i1p3_mc_mcalfactor) {
nv_doubles(x, fp, s->cal_factor[0], m->nwav[0]);
nv_doubles(x, fp, s->cal_factor[1], m->nwav[1]);
}
if (s->mc_use == i1p3_mc_mcalfactor) {
nv_doubles(x, fp, s->raw_white-1, m->nraw+1);
}
if (s->mc_use == i1p3_mc_reflective) {
nv_doubles(x, fp, s->calraw_white[0], m->nraw);
nv_doubles(x, fp, s->calraw_white[1], m->nraw);
nv_doubles(x, fp, s->calsp_ledm[0], m->nwav[0]);
nv_doubles(x, fp, s->calsp_ledm[1], m->nwav[0]);
nv_doubles(x, fp, s->calsp_illcr[0][0], m->nwav[0]);
nv_doubles(x, fp, s->calsp_illcr[1][0], m->nwav[1]);
nv_doubles(x, fp, s->calsp_illcr[0][1], m->nwav[0]);
nv_doubles(x, fp, s->calsp_illcr[1][1], m->nwav[1]);
nv_doubles(x, fp, s->iavg2aillum[0], m->nwav[0]);
nv_doubles(x, fp, s->iavg2aillum[1], m->nwav[1]);
nv_doubles(x, fp, s->sc_calsp_nn_white[0], m->nwav[0]);
nv_doubles(x, fp, s->sc_calsp_nn_white[1], m->nwav[1]);
nv_doubles(x, fp, s->cal_l_uv_diff[0], m->nwav[0]);
nv_doubles(x, fp, s->cal_l_uv_diff[1], m->nwav[1]);
nv_doubles(x, fp, s->cal_s_uv_diff[0], m->nwav[0]);
nv_doubles(x, fp, s->cal_s_uv_diff[1], m->nwav[1]);
}
if (s->mc_use == i1p3_mc_polreflective) {
nv_doubles(x, fp, s->pol_calraw_white, m->nraw);
nv_doubles(x, fp, s->pol_calsp_ledm, m->nwav[0]);
nv_doubles(x, fp, s->pol_calsp_white[0], m->nwav[0]);
nv_doubles(x, fp, s->pol_calsp_white[1], m->nwav[1]);
}
}
return ev;
}
/* Save calibration for all modes, stored on local filesystem */
i1pro3_code i1pro3_save_calibration(i1pro3 *p) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_code ev = I1PRO3_OK;
int i;
char nmode[10];
char cal_name[100]; /* Name */
char **cal_paths = NULL;
int no_paths = 0;
FILE *fp;
i1pnonv x = { 0 };
strcpy(nmode, "w");
#if !defined(O_CREAT) && !defined(_O_CREAT)
# error "Need to #include fcntl.h!"
#endif
#if defined(O_BINARY) || defined(_O_BINARY)
strcat(nmode, "b");
#endif
/* Create the file name */
sprintf(cal_name, "ArgyllCMS/.i1p3_%d.cal", m->serno);
if ((no_paths = xdg_bds(NULL, &cal_paths, xdg_cache, xdg_write, xdg_user, xdg_none,
cal_name)) < 1) {
a1logd(p->log,1,"i1pro3_save_calibration xdg_bds returned no paths\n");
return I1PRO3_INT_CAL_SAVE;
}
a1logd(p->log,2,"i1pro3_save_calibration saving to file '%s'\n",cal_paths[0]);
if (create_parent_directories(cal_paths[0])
|| (fp = fopen(cal_paths[0], nmode)) == NULL) {
a1logd(p->log,2,"i1pro3_save_calibration failed to open file for writing\n");
xdg_free(cal_paths, no_paths);
return I1PRO3_INT_CAL_SAVE;
}
x.p = p; /* Context */
x.op = 1; /* Write & checksum */
x.ef = 0;
x.chsum = 0;
x.nbytes = 0;
/* Setup id info */
m->nv_av = ARGYLL_VERSION;
m->nv_ss = sizeof(i1pro3_state) + sizeof(i1pro3imp); /* A crude structure signature */
m->nv_serno = m->serno;
m->nv_nraw = m->nraw;
m->nv_nwav0 = m->nwav[0];
m->nv_nwav1 = m->nwav[1];
/* Write data and compute checksum */
i1pro3_nv_op(p, &x, fp);
a1logd(p->log,3,"nbytes = %d, Checkum = 0x%x\n",x.nbytes,x.chsum);
write_ints(&x, fp, (int *)&x.chsum, 1);
if (x.ef == 0 && fclose(fp) != 0)
x.ef = 2;
if (x.ef != 0) {
a1logd(p->log,2,"Writing calibration file failed with %d\n",x.ef);
delete_file(cal_paths[0]);
return I1PRO3_INT_CAL_SAVE;
} else {
a1logd(p->log,2,"Writing calibration file succeeded\n");
}
xdg_free(cal_paths, no_paths);
return ev;
}
/* Restore the all modes calibration from the local system */
i1pro3_code i1pro3_restore_calibration(i1pro3 *p) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_code ev = I1PRO3_OK;
int i, j;
char nmode[10];
char cal_name[100]; /* Name */
char **cal_paths = NULL;
int no_paths = 0;
FILE *fp;
i1pnonv x = { 0 };
int chsum;
strcpy(nmode, "r");
#if !defined(O_CREAT) && !defined(_O_CREAT)
# error "Need to #include fcntl.h!"
#endif
#if defined(O_BINARY) || defined(_O_BINARY)
strcat(nmode, "b");
#endif
/* Create the file name */
sprintf(cal_name, "ArgyllCMS/.i1p3_%d.cal" SSEPS "color/.i1p3_%d.cal", m->serno, m->serno);
if ((no_paths = xdg_bds(NULL, &cal_paths, xdg_cache, xdg_read, xdg_user, xdg_none,
cal_name)) < 1) {
a1logd(p->log,2,"i1pro3_restore_calibration xdg_bds failed to locate file'\n");
return I1PRO3_INT_CAL_RESTORE;
}
a1logd(p->log,2,"i1pro3_restore_calibration restoring from file '%s'\n",cal_paths[0]);
/* Check the last modification time */
{
struct sys_stat sbuf;
if (sys_stat(cal_paths[0], &sbuf) == 0) {
m->lo_secs = time(NULL) - sbuf.st_mtime;
a1logd(p->log,2,"i1pro3_restore_calibration: %d secs from instrument last open\n",m->lo_secs);
} else {
a1logd(p->log,2,"i1pro3_restore_calibration: stat on file failed\n");
}
}
if ((fp = fopen(cal_paths[0], nmode)) == NULL) {
a1logd(p->log,2,"i1pro3_restore_calibration failed to open file for reading\n");
xdg_free(cal_paths, no_paths);
return I1PRO3_INT_CAL_RESTORE;
}
x.op = 0; /* Checksum */
x.ef = 0;
x.chsum = 0;
x.nbytes = 0;
/* Compute checksum */
i1pro3_nv_op(p, &x, fp);
a1logd(p->log,3,"nbytes = %d, Checkum = 0x%x\n",x.nbytes,x.chsum);
read_ints(&x, fp, (int *)&chsum, 1);
/* Check the file identification */
if (x.ef != 0
|| m->nv_av != ARGYLL_VERSION
|| m->nv_ss != (sizeof(i1pro3_state) + sizeof(i1pro3imp))
|| m->nv_serno != m->serno
|| m->nv_nraw != m->nraw
|| m->nv_nwav0 != m->nwav[0]
|| m->nv_nwav1 != m->nwav[1]) {
a1logd(p->log,2,"Identification didn't verify\n");
goto reserr;
}
a1logd(p->log,3,"i1pro3_restore_calibration id is OK\n");
/* Check the checksum */
if (x.ef != 0
|| x.chsum != chsum) {
a1logd(p->log,2,"Checksum didn't verify, bytes %d, got 0x%x, expected 0x%x\n",x.nbytes,x.chsum, chsum);
goto reserr;
}
a1logd(p->log,3,"i1pro3_restore_calibration checksum is OK\n");
/* Now that we're happy, read the data. */
free(x.buf);
x.buf = NULL;
x.bsize = 0;
rewind(fp);
x.op = 2; /* Read */
x.ef = 0;
x.chsum = 0;
x.nbytes = 0;
i1pro3_nv_op(p, &x, fp);
read_ints(&x, fp, (int *)&chsum, 1);
/* Just to be sure... */
if (x.ef != 0
|| x.chsum != chsum) {
error("i1pro3: Checksum didn't verify 2nd time, bytes %d, got 0x%x, expected 0x%x\n",x.nbytes,x.chsum, chsum);
}
a1logd(p->log,3,"i1pro3_restore_calibration done OK\n");
reserr:;
fclose(fp);
xdg_free(cal_paths, no_paths);
return ev;
}
i1pro3_code i1pro3_touch_calibration(i1pro3 *p) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_code ev = I1PRO3_OK;
char cal_name[100]; /* Name */
char **cal_paths = NULL;
int no_paths = 0;
int rv;
/* Locate the file name */
sprintf(cal_name, "ArgyllCMS/.i1p3_%d.cal" SSEPS "color/.i1p3_%d.cal", m->serno, m->serno);
if ((no_paths = xdg_bds(NULL, &cal_paths, xdg_cache, xdg_read, xdg_user, xdg_none,
cal_name)) < 1) {
a1logd(p->log,2,"i1pro3_restore_calibration xdg_bds failed to locate file'\n");
return I1PRO3_INT_CAL_TOUCH;
}
a1logd(p->log,2,"i1pro3_touch_calibration touching file '%s'\n",cal_paths[0]);
if ((rv = sys_utime(cal_paths[0], NULL)) != 0) {
a1logd(p->log,2,"i1pro3_touch_calibration failed with %d\n",rv);
xdg_free(cal_paths, no_paths);
return I1PRO3_INT_CAL_TOUCH;
}
xdg_free(cal_paths, no_paths);
return ev;
}
#endif /* ENABLE_NONVCAL */
/* =============================================== */
/* High res support code */
#ifdef HIGH_RES
/* Upsample stray light */
void i1pro3_compute_hr_straylight(i1pro3 *p) {
i1pro3imp *m = (i1pro3imp *)p->m;
int refl;
double twidth = HIGHRES_WIDTH;
int i, j, k, cx, sx;
double **slp; /* 2D Array of stray light values */
/* Then the 2D stray light using linear interpolation */
slp = dmatrix(0, m->nwav[0]-1, 0, m->nwav[0]-1);
/* Interpolate points in one dimension */
for (i = 0; i < m->nwav[0]; i++) { /* Output wavelength */
for (j = 0; j < m->nwav[0]; j++) { /* Input wavelength */
slp[i][j] = m->straylight[0][i][j];
/* Use interpolate/extrapolate for middle points */
if (j == (i-1) || j == i || j == (i+1)) {
int j0, j1;
double w0, w1;
if (j == (i-1)) {
if (j <= 0)
j0 = j+3, j1 = j+4;
else if (j >= (m->nwav[0]-3))
j0 = j-2, j1 = j-1;
else
j0 = j-1, j1 = j+3;
} else if (j == i) {
if (j <= 1)
j0 = j+2, j1 = j+3;
else if (j >= (m->nwav[0]-2))
j0 = j-3, j1 = j-2;
else
j0 = j-2, j1 = j+2;
} else if (j == (i+1)) {
if (j <= 2)
j0 = j+1, j1 = j+2;
else if (j >= (m->nwav[0]-1))
j0 = j-4, j1 = j-3;
else
j0 = j-3, j1 = j+1;
}
w1 = (j - j0)/(j1 - j0);
w0 = 1.0 - w1;
slp[i][j] = w0 * m->straylight[0][i][j0]
+ w1 * m->straylight[0][i][j1];
}
}
}
/* Interpolate points in other dimension */
for (i = 0; i < m->nwav[1]; i++) { /* Output wavelength */
for (j = 0; j < m->nwav[1]; j++) { /* Input wavelength */
double p0, p1;
int x0, x1, y0, y1;
double xx, yy, w0, w1, v0, v1;
double vv;
/* Do linear interp with clipping at ends */
p0 = XSPECT_WL(m->wl_short[1], m->wl_long[1], m->nwav[1], i);
p1 = XSPECT_WL(m->wl_short[1], m->wl_long[1], m->nwav[1], j);
xx = (m->nwav[0]-1.0) * (p0 - m->wl_short[0])/(m->wl_long[0] - m->wl_short[0]);
x0 = (int)floor(xx);
if (x0 <= 0)
x0 = 0;
else if (x0 >= (m->nwav[0]-2))
x0 = m->nwav[0]-2;
x1 = x0 + 1;
w1 = xx - (double)x0;
w0 = 1.0 - w1;
yy = (m->nwav[0]-1.0) * (p1 - m->wl_short[0])/(m->wl_long[0] - m->wl_short[0]);
y0 = (int)floor(yy);
if (y0 <= 0)
y0 = 0;
else if (y0 >= (m->nwav[0]-2))
y0 = m->nwav[0]-2;
y1 = y0 + 1;
v1 = yy - (double)y0;
v0 = 1.0 - v1;
vv = w0 * v0 * slp[x0][y0]
+ w0 * v1 * slp[x0][y1]
+ w1 * v0 * slp[x1][y0]
+ w1 * v1 * slp[x1][y1];
m->straylight[1][i][j] = vv * HIGHRES_WIDTH/10.0;
if (m->straylight[1][i][j] > 0.0)
m->straylight[1][i][j] = 0.0;
}
}
/* Fix primary wavelength weight and neighbors */
for (i = 0; i < m->nwav[1]; i++) { /* Output wavelength */
double sum;
if (i > 0)
m->straylight[1][i][i-1] = 0.0;
m->straylight[1][i][i] = 0.0;
if (i < (m->nwav[1]-1))
m->straylight[1][i][i+1] = 0.0;
for (sum = 0.0, j = 0; j < m->nwav[1]; j++)
sum += m->straylight[1][i][j];
m->straylight[1][i][i] = 1.0 - sum; /* Total sum should be 1.0 */
}
#ifdef HIGH_RES_PLOT_STRAYL
/* Plot original and upsampled reference */
{
double *x1 = dvectorz(0, m->nwav[1]-1);
double *y1 = dvectorz(0, m->nwav[1]-1);
double *y2 = dvectorz(0, m->nwav[1]-1);
for (i = 0; i < m->nwav[1]; i++) { /* Output wavelength */
double wli = XSPECT_WL(m->wl_short[1], m->wl_long[1], m->nwav[1], i);
int i1 = XSPECT_IX(m->wl_short[0], m->wl_long[0], m->nwav[0], wli);
for (j = 0; j < m->nwav[1]; j++) {
double wl = XSPECT_WL(m->wl_short[1], m->wl_long[1], m->nwav[1], j);
x1[j] = wl;
y1[j] = m->straylight[1][i][j];
if (y1[j] == 0.0)
y1[j] = -8.0;
else
y1[j] = log10(fabs(y1[j]));
if (wli < m->wl_short[0] || wli > m->wl_long[0]
|| wl < m->wl_short[0] || wl > m->wl_long[0]) {
y2[j] = -8.0;
} else {
double x, wl1, wl2;
for (k = 0; k < (m->nwav[0]-1); k++) {
wl1 = XSPECT_WL(m->wl_short[0], m->wl_long[0], m->nwav[0], k);
wl2 = XSPECT_WL(m->wl_short[0], m->wl_long[0], m->nwav[0], k+1);
if (wl >= wl1 && wl <= wl2)
break;
}
x = (wl - wl1)/(wl2 - wl1);
y2[j] = m->straylight[0][i1][k] + (m->straylight[0][i1][k+1]
- m->straylight[0][i1][k]) * x;
if (y2[j] == 0.0)
y2[j] = -8.0;
else
y2[j] = log10(fabs(y2[j]));
}
}
do_plot(x1, y1, y2, NULL, m->nwav[1]);
}
free_dvector(x1, 0, m->nwav[1]-1);
free_dvector(y1, 0, m->nwav[1]-1);
free_dvector(y2, 0, m->nwav[1]-1);
}
#endif /* HIGH_RES_PLOT_STRAYL */
free_dmatrix(slp, 0, m->nwav[0]-1, 0, m->nwav[0]-1);
}
/* Linear spectra upsample */
static void linear_upsample(i1pro3 *p, double *hi, double *lo, int clip, char *debug) {
i1pro3imp *m = (i1pro3imp *)p->m;
double _lo[36];
int i;
if (hi == lo) { /* Allow for out == in */
vect_cpy(_lo, lo, 36);
lo = _lo;
}
/* For each output wavelength */
for (i = 0; i < m->nwav[1]; i++) {
double y[2], yw;
double x[2], xw;
int j, k;
xw = XSPECT_WL(m->wl_short[1], m->wl_long[1], m->nwav[1], i);
/* Locate lowres index below it */
j = (int)floor(XSPECT_DIX(m->wl_short[0], m->wl_long[0], m->nwav[0], xw));
if (j < 0)
j = 0;
if (j > (m->nwav[0]-2))
j = (m->nwav[0]-2);
/* Setup the surrounding point values */
for (k = 0; k < 2; k++) {
x[k] = XSPECT_WL(m->wl_short[0], m->wl_long[0], m->nwav[0], j+k);
y[k] = lo[j+k];
}
/* Compute interpolated value using Lagrange: */
yw = (y[1] * (xw - x[0]) + y[0] * (x[1] - xw))/(x[1] - x[0]);
hi[i] = yw;
}
if (clip)
vect_clip(hi, hi, 0.0, DBL_MAX, m->nwav[1]);
#ifdef HIGH_RES_PLOT
if (debug != NULL) {
printf("%s std res (black), high res(red)\n",debug);
plot_wav_sh(m, lo, hi);
}
#endif // HIGH_RES_PLOT
}
/* Simple/fast spectra upsample */
/* We use a point by point Lagrange interpolation */
static void fast_upsample(i1pro3 *p, double *hi, double *lo, int clip, char *debug) {
i1pro3imp *m = (i1pro3imp *)p->m;
double _lo[36];
int i;
if (hi == lo) { /* Allow for out == in */
vect_cpy(_lo, lo, 36);
lo = _lo;
}
/* For each output wavelength */
for (i = 0; i < m->nwav[1]; i++) {
double y[4], yw;
double x[4], xw;
int j, k;
xw = XSPECT_WL(m->wl_short[1], m->wl_long[1], m->nwav[1], i);
/* Locate lowres index below it */
j = (int)floor(XSPECT_DIX(m->wl_short[0], m->wl_long[0], m->nwav[0], xw)) -1;
if (j < 0)
j = 0;
if (j > (m->nwav[0]-4))
j = (m->nwav[0]-4);
/* Setup the surrounding point values */
for (k = 0; k < 4; k++) {
x[k] = XSPECT_WL(m->wl_short[0], m->wl_long[0], m->nwav[0], j+k);
y[k] = lo[j+k];
}
/* Compute interpolated value using Lagrange: */
yw = y[0] * (xw-x[1]) * (xw-x[2]) * (xw-x[3])
/((x[0]-x[1]) * (x[0]-x[2]) * (x[0]-x[3]))
+ y[1] * (xw-x[0]) * (xw-x[2]) * (xw-x[3])
/((x[1]-x[0]) * (x[1]-x[2]) * (x[1]-x[3]))
+ y[2] * (xw-x[0]) * (xw-x[1]) * (xw-x[3])
/((x[2]-x[0]) * (x[2]-x[1]) * (x[2]-x[3]))
+ y[3] * (xw-x[0]) * (xw-x[1]) * (xw-x[2])
/((x[3]-x[0]) * (x[3]-x[1]) * (x[3]-x[2]));
hi[i] = yw;
}
if (clip)
vect_clip(hi, hi, 0.0, DBL_MAX, m->nwav[1]);
#ifdef HIGH_RES_PLOT
if (debug != NULL) {
printf("%s std res (black), high res(red)\n",debug);
plot_wav_sh(m, lo, hi);
}
#endif // HIGH_RES_PLOT
}
/* We super-sample to a common denominator of the current and target */
/* sampling interval, and then downfilter using triangular filtering. */
/* The super-sample interpolation uses a variation on 1D rspl */
/* in which there are 3 weighted fitting goals: */
/* 1) Smoothness */
/* 2) Fit to linear interpolated values */
/* 3) Fit of triangular filtered values to input values */
/* The balanance between 1) & 2) sets the smoothness/accuracy tradeoff, */
/* while 3) is given high weight to ensure fidelity to the orginal data. */
#define NN 2 /* [2] super-sampling down multiplier */
#undef UPSAMPLE_PLOT
#undef UPSAMPLE_DEBUG /* Verbose output */
static void good_upsample(i1pro3 *p, double *hi, double *lo, int clip, char *debug) {
i1pro3imp *m = (i1pro3imp *)p->m;
int nnus, nnds = NN; /* upsampling and down sampling multipliers */
double vrange; /* value range */
int nig; /* Number of super-samples */
double ss_width, ss_short, ss_long; /* ss range */
double t1, t2;
int ii;
double cw; /* Curvature weighting */
double dw; /* Data weight */
int ndiag = 3; /* Number of diagonals needed on A matrix */
double **A; /* A matrix of interpoint weights */
double *b; /* b vector for RHS of simultabeous equation */
double *x; /* Grid values */
double tw; /* Total triangle weight */
double slo[MX_NWAV]; /* Scaled std. res. input values */
double chlo[MX_NWAV], cher[MX_NWAV];
double alo[MX_NWAV];/* Adjusted scaled std. res. input values */
int j,n,i,k;
int itter;
double maxe = 1e38; /* Itteration error */
/* Compute up and down sampling ratios */
t1 = (m->wl_width[0] * NN)/m->wl_width[1];
nnus = (int)floor(t1 + 0.5);
if (fabs(t1 - (double)nnus) > 1e-6)
error("Assert in %s at line %d, hi-res is not multiple of std res\n",__FILE__,__LINE__);
t1 = (m->wl_short[1] - m->wl_short[0])/m->wl_width[1];
ii = floor(t1 + 0.5);
if (fabs(t1 - (double)ii) > 1e-6)
error("Assert in %s at line %d, hi-res is not aligned to std res\n",__FILE__,__LINE__);
if (ndiag < (2 * nnus -1))
ndiag = 2 * nnus - 1;
/* Compute super-sample range */
ss_width = m->wl_width[0]/(double)nnus;
t1 = m->wl_short[0] - m->wl_width[0] + ss_width;
t2 = m->wl_short[1] - m->wl_width[1] + ss_width;
if (t2 < t1)
t1 = t2;
ss_short = t1;
t1 = m->wl_long[0] + m->wl_width[0] - ss_width;
t2 = m->wl_long[1] + m->wl_width[1] - ss_width;
if (t2 > t1)
t1 = t2;
ss_long = t1;
nig = (int)floor((ss_long - ss_short)/ss_width + 0.5);
#ifdef UPSAMPLE_DEBUG
printf("nnus %d nnds %d ndiag %d\n",nnus, nnds, ndiag);
printf("ss_width %f ss_short %f ss_long %f nig %d\n",ss_width, ss_short, ss_long, nig);
#endif
/* Figure out the data range */
t1 = DBL_MAX;
t2 = -DBL_MAX;
for (i = 0; i < m->nwav[0]; i++) {
if (lo[i] < t1)
t1 = lo[i];
if (lo[i] > t2)
t2 = lo[i];
}
vrange = 0.5 * (t2 - t1);
/* Normalize curve weight to grid resolution. */
cw = 5e-6 * pow((nig-1),4.0) / (nig - 2); /* [5e-6] */
dw = 1.0;
#ifdef UPSAMPLE_DEBUG
printf("vrange = %f\n",vrange);
printf("cw = %f dw = %f\n",cw,dw);
#endif
x = dvectorz(0, nig); /* Solution vector */
A = dmatrix(0, nig, 0, ndiag-1); /* Diagonal of the A matrix */
b = dvector(0, nig); /* RHS */
for (i = 0; i < m->nwav[0]; i++) {
slo[i] = lo[i]/vrange;
alo[i] = slo[i];
}
/* Ensure fit to original data by itterating */
for (itter = 0; itter < 30 && maxe > 0.005; itter++) {
/* Clear setup matrix/vector */
for (i = 0; i < nig; i++)
vect_set(A[i], 0.0, ndiag);
vect_set(b, 0.0, nig);
/* Accumulate data dependent factors */
tw = nnus * nnus; /* Total weight */
for (j = 0; j < m->nwav[0]; j++) {
double tv = 0.0;
double wl;
int jj;
/* super sample value that center of data point falls on */
wl = XSPECT_WL(m->wl_short[0], m->wl_long[0], m->nwav[0], j);
jj = XSPECT_IX(ss_short, ss_long, nig, wl);
/* For each partial derivative */
for (ii = -nnus+1; ii < nnus; ii++) {
double wii = (nnus - abs(ii))/tw;
if ((jj + ii) < 0
|| (jj + ii) >= nig) {
continue;
}
b[jj + ii] -= 2.0 * -alo[j] * wii * dw;
/* For each component of triangular integration */
for (i = -nnus+1; i < nnus; i++) {
double wi;
if ((i - ii) < 0)
continue; /* Skip due to symetry */
wi = (nnus - abs(i))/tw;
A[jj + ii][i - ii] += 2.0 * wii * wi * dw;
}
}
}
/* Accumulate curvature dependent factors */
for (i = 0; i < nig; i++) {
double tcw = cw;
if ((i-2) >= 0) { /* Curvature of cell below */
A[i][0] += 2.0 * tcw;
}
if ((i-1) >= 0 && (i+1) < nig) { /* Curvature of this cell */
A[i][0] += 8.0 * tcw;
A[i][1] += -4.0 * tcw;
}
if ((i+2) < nig) { /* Curvature of cell above */
A[i][0] += 2.0 * tcw;
A[i][1] += -4.0 * tcw;
A[i][2] += 2.0 * tcw;
}
}
#ifdef UPSAMPLE_DEBUG
printf("b, A matrix:\n");
for (i = 0; i < nig; i++) {
printf("b[%d] = %f\n",i,b[i]);
for (k = 0; k < ndiag; k++)
printf(" A[%d][%d] = %f\n",i,k,A[i][k]);
}
#endif /* UPSAMPLE_DEBUG */
/* Apply Cholesky decomposition to A[][] to create L[][] */
for (i = 0; i < nig; i++) {
double sm;
for (n = 0; n < ndiag; n++) {
sm = A[i][n];
for (k = 1; (n+k) < ndiag && (i-k) >= 0; k++)
sm -= A[i-k][n+k] * A[i-k][k];
if (n == 0) {
if (sm <= 0.0)
error("Assert in %s at line %d, good_upsample loss of resolution\n",__FILE__,__LINE__);
A[i][0] = sqrt(sm);
} else {
A[i][n] = sm/A[i][0];
}
}
}
/* Solve L . y = b, storing y in x */
for (i = 0; i < nig; i++) {
double sm;
sm = b[i];
for (k = 1; k < ndiag && (i-k) >= 0; k++)
sm -= A[i-k][k] * x[i-k];
x[i] = sm/A[i][0];
}
/* Solve LT . x = y */
for (i = nig-1; i >= 0; i--) {
double sm;
sm = x[i];
for (k = 1; k < ndiag && (i+k) < nig; k++)
sm -= A[i][k] * x[i+k];
x[i] = sm/A[i][0];
}
#ifdef UPSAMPLE_DEBUG
printf("Solution vector:\n");
for (i = 0; i < nig; i++) {
printf("x[%d] = %f\n",i,x[i]);
}
#endif /* DEBUG */
/* Compute low res. triangle integration values */
tw = nnus * nnus; /* Total weight */
maxe = -1e38;
for (j = 0; j < m->nwav[0]; j++) {
double tv = 0.0;
double wl, er;
int jj;
wl = XSPECT_WL(m->wl_short[0], m->wl_long[0], m->nwav[0], j);
jj = XSPECT_IX(ss_short, ss_long, nig, wl);
/* (This is an optimized triangle integration) */
for (i = -nnus+1; i < nnus; i++) {
double wi = (nnus - abs(i))/tw;
tv += wi * x[jj + i];
}
chlo[j] = tv;
}
vect_sub3(cher, slo, chlo, m->nwav[0]);
maxe = vect_max_mag(cher, m->nwav[0]);
vect_scaleadd(alo, cher, 1.70, m->nwav[0]);
}
#ifdef UPSAMPLE_PLOT /* Plot low res input vs. super sample */
printf("itter %d: lowres in, lowres check (maxe %f):\n",itter,maxe);
plot_wav2(m, 0, slo, chlo);
#endif
free_dvector(b, 0, nig);
free_dmatrix(A, 0, nig, 0, ndiag-1);
/* Compute hires output values */
tw = nnds * nnds; /* Total weight */
tw /= vrange; /* but rescale to input range */
for (j = 0; j < m->nwav[1]; j++) {
double tv = 0.0;
double wl;
int jj;
wl = XSPECT_WL(m->wl_short[1], m->wl_long[1], m->nwav[1], j);
jj = XSPECT_IX(ss_short, ss_long, nig, wl);
for (i = 0; i < nnds; i++) {
double wi, wi1;
wi = nnds - i;
wi1 = nnds - i - 1;
/* Area of trapezoid */
tv += 0.5 * (wi * x[jj + i] + wi1 * x[jj + i + 1]); /* +ve side of triangle */
tv += 0.5 * (wi * x[jj - i] + wi1 * x[jj - i - 1]); /* -ve side of triangle */
}
hi[j] = tv/tw;
}
#ifdef UPSAMPLE_PLOT /* Plot low-res vs. super samplea v.s hi-res */
{
double xx[1000], y1[1000], y2[1000], y3[1000];
xspect sp0, sp1;
sp0.spec_n = m->nwav[0];
sp0.spec_wl_short = m->wl_short[0];
sp0.spec_wl_long = m->wl_long[0];
sp0.norm = 1.0;
vect_cpy(sp0.spec, lo, m->nwav[0]);
sp1.spec_n = m->nwav[1];
sp1.spec_wl_short = m->wl_short[1];
sp1.spec_wl_long = m->wl_long[1];
sp1.norm = 1.0;
vect_cpy(sp1.spec, hi, m->nwav[1]);
for (j = 0; j < nig; j++) {
xx[j] = XSPECT_WL(ss_short, ss_long, nig, j);
y1[j] = value_xspect_lin(&sp0, xx[j]);
y2[j] = x[j] * vrange;
y3[j] = value_xspect_lin(&sp1, xx[j]);
}
printf("low-res (bk) super sample (r) hi-res (g)\n");
// do_plot(xx, y1, y2, y3, nig);
do_plot(xx, y1, y2, NULL, nig);
}
#endif
free_dvector(x, 0, nig); /* Solution vector */
if (clip)
vect_clip(hi, hi, 0.0, DBL_MAX, m->nwav[1]);
#ifdef HIGH_RES_PLOT
if (debug != NULL) {
printf("%s std res (black), high res(red)\n",debug);
plot_wav_sh(m, lo, hi);
}
#endif // HIGH_RES_PLOT
}
#undef NN
#undef UPSAMPLE_PLOT
#undef UPSAMPLE_DEBUG
/* Hack to correct for emissive bumpiness in calibration upsample. */
/* We correct by difference to smooth incendescent lamp spectra. */
/* This is a hack because it's not clear that a correction for one */
/* instance of an instrument is accurate for a different one... */
/* The alternative would be to add an optional calibration step. */
/* (We avoided this in the i1Pro2 by using its own incancescent source) */
void correct_emis_coef(i1pro3 *p, double *hi) {
i1pro3imp *m = (i1pro3imp *)p->m;
xspect cor = { 106, 380.0, 730.0, 1.0,
{
1.142277, 0.968452, 1.077103, 1.222850, 1.234457, 1.137349, 1.029250,
0.987417, 0.979528, 0.997226, 1.007795, 1.022262, 1.029800, 1.028760,
1.017607, 1.003028, 0.992135, 0.989818, 0.993231, 0.996651, 1.001012,
1.004000, 1.006764, 1.009298, 1.010818, 1.012484, 1.013909, 1.012393,
1.008904, 1.005176, 1.002034, 0.999666, 0.999555, 1.000680, 1.000192,
0.999204, 0.999757, 0.999311, 0.999327, 1.002582, 1.003998, 1.001595,
1.004868, 1.007035, 1.003921, 1.000437, 1.001987, 1.004170, 1.001211,
0.998845, 1.001844, 1.001962, 0.993726, 1.002693, 1.000163, 1.000108,
0.997594, 0.995878, 0.998601, 1.002356, 1.002480, 0.999815, 0.999394,
1.001485, 1.003212, 1.002075, 1.000501, 0.999974, 0.999697, 0.999111,
0.998460, 0.999582, 1.001289, 1.002462, 1.001140, 0.999102, 0.997950,
0.998813, 1.000245, 1.000950, 1.000685, 1.000102, 0.999426, 0.998706,
0.998156, 0.998011, 0.999170, 1.000538, 1.000933, 0.999226, 0.997238,
0.996281, 0.998018, 1.001278, 1.003377, 1.001986, 0.997233, 0.992499,
0.992259, 0.999155, 1.008205, 1.015060, 1.013560, 1.004068, 0.995199,
1.000362
}
};
int i;
for (i = 0; i < cor.spec_n; i++) {
double wl = XSPECT_WL(m->wl_short[1], m->wl_long[1], m->nwav[1], i);
hi[i] *= value_xspect_poly(&cor, wl);
}
}
/* Clear low entries of a spectrum */
void clear_low_wav(i1pro3 *p, double *wav, int sno, int hr) {
i1pro3imp *m = (i1pro3imp *)p->m;
double cowl, hix;
int hno;
if (hr == 0) {
vect_set(wav, 0.0, sno);
return;
}
cowl = XSPECT_WL(m->wl_short[0], m->wl_long[0], m->nwav[0], sno - 0.5); /* Cuttoff */
cowl -= 0.5 * m->wl_width[1]; /* Center at hr */
hix = XSPECT_DIX(m->wl_short[1], m->wl_long[1], m->nwav[1], cowl);
hno = 1 + (int)ceil(hix); /* hr cuttof at or below sr cuttoff */
vect_set(wav, 0.0, hno);
}
/* Clear low (short) entries of a spectrum with slope */
void clear_low_wav2(i1pro3 *p, double *wav, double swl, double lwl, int hr) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i;
i = XSPECT_DIX(m->wl_short[hr], m->wl_long[hr], m->nwav[hr], lwl);
for (; i >= 0; i--) {
double wl = XSPECT_WL(m->wl_short[hr], m->wl_long[hr], m->nwav[hr], i);
double ra = (wl - swl)/(lwl - swl);
if (ra > 1.0)
ra = 1.0;
else if (ra < 0.0)
ra = 0.0;
wav[i] *= ra;
}
}
/* Clear high entries of a spectrum */
void clear_high_wav(i1pro3 *p, double *wav, int sno, int hr) {
i1pro3imp *m = (i1pro3imp *)p->m;
double cowl, hix;
int hno;
if (hr == 0) {
vect_set(wav + sno, 0.0, m->nwav[0] - sno);
return;
}
cowl = XSPECT_WL(m->wl_short[0], m->wl_long[0], m->nwav[0], sno - 1.5); /* Cuttoff */
cowl += 0.5 * m->wl_width[1]; /* Center at hr */
hix = XSPECT_DIX(m->wl_short[1], m->wl_long[1], m->nwav[1], cowl);
hno = 1 + (int)floor(hix); /* hr cuttof at or above sr cuttoff */
vect_set(wav + hno, 0.0, m->nwav[1] - hno);
}
/* Clear high (long) entries of a spectrum with slope */
void clear_high_wav2(i1pro3 *p, double *wav, double swl, double lwl, int hr) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i;
i = XSPECT_DIX(m->wl_short[hr], m->wl_long[hr], m->nwav[hr], swl);
for (; i < m->nwav[hr]; i++) {
double wl = XSPECT_WL(m->wl_short[hr], m->wl_long[hr], m->nwav[hr], i);
double ra = (lwl - wl)/(lwl - swl);
if (ra > 1.0)
ra = 1.0;
else if (ra < 0.0)
ra = 0.0;
wav[i] *= ra;
}
}
/* Compute smooth edged sum over wl range */
double sum_wav2(i1pro3 *p, double *wav, double sx, double ss, double ee, double ex, int hr) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i, ix1, ix2;
double sum = 0.0;
ix1 = XSPECT_DIX(m->wl_short[hr], m->wl_long[hr], m->nwav[hr], sx);
ix2 = XSPECT_DIX(m->wl_short[hr], m->wl_long[hr], m->nwav[hr], ex) + 1;
for (i = ix1; i < ix2; i++) {
double wl = XSPECT_WL(m->wl_short[hr], m->wl_long[hr], m->nwav[hr], i);
double ra, rb;
ra = (wl - sx)/(ss - sx);
rb = (ex - wl)/(ex - ee);
if (ra > 1.0)
ra = 1.0;
else if (ra < 0.0)
ra = 0.0;
if (rb > 1.0)
rb = 1.0;
else if (rb < 0.0)
rb = 0.0;
sum += ra * rb * wav[i];
}
return sum;
}
#endif /* HIGH_RES */
/* =============================================== */
/* i1Pro3 wavelength calibration */
/*
The i1Pro3 has a wavelength reference LED/thin film filter and
stores a reference raw spectrum of it in its
calibrated state, together with two intepolation tables
defining the raw bin no. to wavelength conversion,
one for reflection mode, and one for emission mode.
[ It's a puzzle as to why there are two tables. The emission
table is more linear, while by comparisson the reflection
mode has some subltle wiggles in the conversion. Possible
reasons for this could be:
* It's for backwards compatibility with previous X-Rite
instruments, which in the past used a flawed wavelength
reference.
* A different standard wavelength reference was used to
calibrate emission and reflection, and these references
have historical differences.
* There's subtle something going on in the physics of the
emission and reflection modes and the nature of
the diffraction grating that causes the wavelength
calibration to be different. ]
By measuring the wavelength LED/filter and finding
the best positional match against the reference
spectrum, a CCD bin offset can be computed
to compensate for any shift in the optical or
physical alignment of spectrum against CCD.
[ The X-Rite driver applies a correction in nm
rather than CCD bin shift. It's a puzzle why
they do this, since a first order assumption based
on the physics would be that the correction is best
done by CCD bin shift. ]
To use the adjustment, the raw to wave subsampling
filters need to be regenerated, and to ensure that
the instrument returns readings very close to the
manufacturers driver, the same underlying filter
creation mathematics needs to be used.
The manufacturers filter weights are the accumulated
third order Lagrange polynomial weights of the
integration of a 20 nm wide triange spectrum
centered at each output wavelength, discretely
integrated between the range of the middle two points
of the Lagrange interpolator. The triangle response
being integrated has an area of exactly 1.0.
*/
/* Invert a raw2wavlength table value. */
static double inv_raw2wav(double wl_cal[128], double inv) {
double outv;
outv = vect_rev_lerp(wl_cal, inv, 128);
outv = 127.0 - 127.0 * outv;
return outv;
}
/* Return the uncalibrated wavelength given a raw bin value index */
/* using reflective ee_wl_cal. */
static double i1pro3_raw2wav_runc(i1pro3 *p, double raw) {
i1pro3imp *m = (i1pro3imp *)p->m;
double ov;
raw = (127.0 - raw)/127.0;
ov = vect_lerp(m->ee_wl_cal1, raw, 128);
return ov;
}
/* Return the uncalibrated wavelength given a raw bin value index */
/* using emissive ee_wl_cal. */
static double i1pro3_raw2wav_eunc(i1pro3 *p, double raw) {
i1pro3imp *m = (i1pro3imp *)p->m;
double ov;
raw = (127.0 - raw)/127.0;
ov = vect_lerp(m->ee_wl_cal2, raw, 128);
return ov;
}
/* return the calibrated wavelength given a raw bin value for the given mode */
static double i1pro3_raw2wav(i1pro3 *p, int refl, double raw) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
double *wl_cal = refl ? m->ee_wl_cal1 : m->ee_wl_cal2;
double ov;
/* Correct for CCD offset and scale back to reference */
raw = raw - m->wl_raw_off;
//printf("~1 i1pro3_raw2wav: in %f - wl_raw_off %f = %f\n", raw, m->wl_raw_off, raw - m->wl_raw_off);
raw = (127.0 - raw)/127.0; /* wl_cal[] expects +ve correlation ? */
ov = vect_lerp(wl_cal, raw, 128);
ov = ov - m->wl_wav_off;
//printf("~1 returning %f\n",ov);
return ov;
}
/* Powell minimisation contxt for WL calibration */
typedef struct {
double ref_max; /* reference maximum level */
double *wl_ref; /* Wavlength reference samples */
int wl_ref_n; /* Number of wavelength references */
double *wl_meas; /* Wavelength measurement samples */
int wl_meas_n; /* Number of wavelength measurement samples */
int plot; /* Plot each try */
} wlcal_cx;
/* Powell minimisation callback function */
/* Parameters being optimized are offset and scale */
static double wlcal_opt1(void *vcx, double tp[]) {
#ifdef PLOT_DEBUG
int pix = 0;
double xx[1024];
double y1[1024]; /* interpolate ref */
double y2[1024]; /* Measurement */
double y3[1024]; /* Error */
#endif
wlcal_cx *cx = (wlcal_cx *)vcx;
double vv, rv = 0.0;
int si, i;
si = (int)tp[1];
/* i = Measurement index */
for (i = si; i < cx->wl_meas_n; i++) {
double xv; /* offset & scaled measurement index */
int ix; /* Lagrange base offset */
double yv;
if (i < 0)
continue;
xv = ((double)i - tp[1]); /* fitted measurement location in reference no scale */
ix = ((int)xv) - 1; /* Reference index of Lagrange for this xv */
if (ix < 0)
continue;
if ((ix + 4) > cx->wl_ref_n)
break;
/* Compute interpolated value of reference using Lagrange: */
yv = cx->wl_ref[ix+0] * (xv-(ix+1)) * (xv-(ix+2)) * (xv-(ix+3))
/((0.0-1.0) * (0.0-2.0) * (0.0-3.0))
+ cx->wl_ref[ix+1] * (xv-(ix+0)) * (xv-(ix+2)) * (xv-(ix+3))
/((1.0-0.0) * (1.0-2.0) * (1.0-3.0))
+ cx->wl_ref[ix+2] * (xv-(ix+0)) * (xv-(ix+1)) * (xv-(ix+3))
/((2.0-0.0) * (2.0-1.0) * (2.0-3.0))
+ cx->wl_ref[ix+3] * (xv-(ix+0)) * (xv-(ix+1)) * (xv-(ix+2))
/((3.0-0.0) * (3.0-1.0) * (3.0-2.0));
vv = yv - tp[0] * cx->wl_meas[i];
/* Weight error linearly with magnitude, to emphasise peak error */
/* rather than what's happening down in the noise */
vv = vv * vv * (yv + 1.0)/(cx->ref_max+1.0);
#ifdef PLOT_DEBUG
if (cx->plot) {
xx[pix] = (double)i;
y1[pix] = yv;
y2[pix] = tp[0] * cx->wl_meas[i];
// y3[pix] = 2000.0 * (0.02 + yv/cx->ref_max); /* Weighting */
y3[pix] = 0.5 * vv; /* Error squared */
pix++;
}
#endif
rv += vv;
}
#ifdef PLOT_DEBUG
if (cx->plot) {
printf("Params %f %f -> err %f, Interp Ref (Bk), Meas samples (R), Error (G)\n", tp[0], tp[1], rv);
do_plot(xx, y1, y2, y3, pix);
}
#endif
//printf("~1 %f %f -> %f\n", tp[0], tp[1], rv);
return rv;
}
#ifdef SALONEINSTLIB
/* Do a rudimetrary 2d optimization that uses exaustive */
/* search with hierarchical step sizes */
static int wloptimize(double *cparm,
double *ss,
double tol,
double (*funk)(void *fdata, double tp[]),
void *fdata
) {
double range[2][2]; /* [dim][min/max] */
double val[2]; /* Current test values */
double bfit = 1e38; /* Current best fit values */
int dim;
for (dim = 0; dim < 2; dim++) {
range[dim][0] = cparm[dim] - ss[dim];
range[dim][1] = cparm[dim] + ss[dim];
val[dim] = cparm[dim];
}
/* Until we reach the tollerance */
for (;;) {
double mstep = 1e38;
for (dim = 0; dim < 2; dim++) {
double stepsz;
stepsz = (range[dim][1] - range[dim][0])/10.0;
if (stepsz < mstep)
mstep = stepsz;
/* Search in this dimension */
for (val[dim] = range[dim][0]; val[dim] <= range[dim][1]; val[dim] += stepsz) {
double fit;
fit = funk(fdata, val);
if (fit < bfit) {
cparm[dim] = val[dim];
bfit = fit;
}
}
val[dim] = cparm[dim];
range[dim][0] = val[dim] - stepsz;
range[dim][1] = val[dim] + stepsz;
}
if (mstep <= tol)
break;
}
return 0;
}
#endif /* SALONEINSTLIB */
/* Given a raw measurement of the wavelength LED, */
/* Compute the base offset that best fits it to the reference */
/* Returns both raw and wav offset, but only one should be used for correction */
/* (Also sets wl_refpeakloc & wl_refpeakwl) */
i1pro3_code i1pro3_match_wl_meas(i1pro3 *p, double *pwav_off, double *praw_off, double *wlraw) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_code ev = I1PRO3_OK;
int i;
int rploc, mploc; /* Peak offset of ref. and meas. */
int moff; /* Base index */
double lhalf, rhalf;
double fwhm; /* Measured half width */
double rmax, mmax;
double magscale;
double raw_off, wav_off;
/* Do simple match first - locate maximum */
rmax = -1e6;
rploc = -1;
for (i = 0; i < m->nraw; i++) {
if (m->wl_led_spec[i] > rmax) {
rmax = m->wl_led_spec[i]; /* Max of reference */
rploc = i;
}
}
mmax = -1e6;
mploc = -1;
for (i = 0; i < m->nraw; i++) {
if (wlraw[i] > mmax) {
mmax = wlraw[i]; /* Max of measurement */
mploc = i;
}
}
if (mploc < 0 || mploc >= m->nraw) {
a1logd(p->log,1,"Couldn't locate WL measurement peak\n");
return I1PRO3_WL_SHAPE;
}
/* Check magnitude is sufficient (not sure this is right, typically 5900 > 882) */
a1logd(p->log,2,"Measured WL level = %f, minimum needed = %f\n",mmax, m->wl_cal_min_level);
if (mmax < m->wl_cal_min_level) {
a1logd(p->log,1,"i1pro3_match_wl_meas peak magnitude too low\n");
return I1PRO3_WL_TOOLOW;
}
/* Locate the half peak values */
for (i = 1; i < mploc; i++) {
if (wlraw[i] > (mmax/2.0)) { /* Use linear interp */
lhalf = (wlraw[i] - mmax/2.0)/(wlraw[i] - wlraw[i-1]);
lhalf = lhalf * (i-1.0) + (1.0 - lhalf) * (double)i;
break;
}
}
if (i >= mploc) {
a1logd(p->log,1,"Couldn't locate WL left half level\n");
return I1PRO3_WL_SHAPE;
}
for (; i < m->nraw; i++) {
if (wlraw[i] < (mmax/2.0)) { /* Use linear interp */
rhalf = (mmax/2.0 - wlraw[i])/(wlraw[i-1] - wlraw[i]);
rhalf = rhalf * (i-1.0) + (1.0 - rhalf) * (double)i;
break;
}
}
if (i >= m->nraw) {
a1logd(p->log,1,"Couldn't locate WL righ half level\n");
return I1PRO3_WL_SHAPE;
}
a1logd(p->log,5,"WL half levels at %f (%f nm) and %f (%f nm)\n",lhalf, i1pro3_raw2wav_runc(p, lhalf), rhalf, i1pro3_raw2wav_runc(p, rhalf));
fwhm = i1pro3_raw2wav_runc(p, lhalf) - i1pro3_raw2wav_runc(p, rhalf);
a1logd(p->log,3, "WL spectrum fwhm = %f\n",fwhm);
if (fwhm < (m->wl_cal_fwhm - m->wl_cal_fwhm_tol)
|| fwhm > (m->wl_cal_fwhm + m->wl_cal_fwhm_tol)) {
a1logd(p->log,1,"WL fwhm %f is out of range %f .. %f\n",fwhm,m->wl_cal_fwhm - m->wl_cal_fwhm_tol,m->wl_cal_fwhm + m->wl_cal_fwhm_tol);
return I1PRO3_WL_SHAPE;
}
moff = mploc - rploc; /* rough measured raw offset */
a1logd(p->log,3, "Preliminary WL peak match at ref base offset %d into measurement\n", moff);
magscale = rmax/mmax; /* Initial scale to make them match */
#ifdef PLOT_DEBUG
/* Plot the match */
{
double xx[1024];
double y1[1024];
double y2[1024];
for (i = 0; i < m->nraw; i++) {
xx[i] = (double)i;
y1[i] = 0.0;
if (i >= moff && (i - moff) < m->nraw) {
y1[i] = m->wl_led_spec[i- moff];
}
y2[i] = wlraw[i] * magscale;
}
printf("Simple WL match, ref = black, meas = red:\n");
do_plot(xx, y1, y2, NULL, m->nraw);
}
#endif
/* Now do a good match */
/*
Do Lagrange interpolation on the reference curve,
and use a minimizer to find the best fit (minimum weighted y error)
by optimizing the magnitude, offset and scale.
*/
{
wlcal_cx cx;
double cparm[2]; /* fit parameters */
double ss[2]; /* Search range */
double athresh;
cparm[0] = magscale;
ss[0] = 0.2;
cparm[1] = (double)moff;
ss[1] = 4.0; /* == +- 12 nm */
cx.ref_max = rmax;
cx.wl_ref = m->wl_led_spec;
cx.wl_ref_n = m->nraw;
cx.wl_meas = wlraw;
cx.wl_meas_n = m->nraw;
// cx.plot = 1; /* Plot each trial */
/* We could use the scale to adjust the whole CCD range, */
/* but the manufacturers driver doesn't seem to do this, */
/* and it may be making the calibration sensitive to any */
/* changes in the WL LED/filter spectrum shape. Instead we */
/* minimize the error weighted for the peak of the shape. */
#ifdef SALONEINSTLIB
if (wloptimize(cparm, ss, 1e-7, wlcal_opt1, &cx))
a1logw(p->log,"wlcal_opt1 failed\n");
#else
if (powell(NULL, 2, cparm, ss, 1e-6, 1000, wlcal_opt1, &cx, NULL, NULL))
a1logw(p->log,"wlcal_opt1 failed\n");
#endif
a1logd(p->log,3,"WL best fit parameters: %f %f\n", cparm[0], cparm[1]);
raw_off = cparm[1];
#ifdef PLOT_DEBUG
/* Plot the final result */
printf("Best WL match, ref = black, meas = red, err = green:\n");
cx.plot = 1;
wlcal_opt1(&cx, cparm);
#endif
/* If we have calibrated on the ambient cap, correct */
/* for the emissive vs. reflective raw2wav scaling factor */
athresh = 15000.0;
if (m->aperture) /* 8mm reduces brightness */
athresh *= 0.316;
if (m->filt == inst_opt_filter_pol) /* Polarizer reduces brightness */
athresh *= 0.333;
if (mmax < athresh) {
raw_off += 0.1549;
a1logd(p->log,3,"Adjusted raw correction to %f to account for measurement using ambient cap\n",raw_off);
}
/* Check that the correction in nm is not excessive. */
/* (Use emissive nm lookup since it is more linear in its conversion) */
m->wl_refpeakloc = rploc; /* For later conversions from wl off to raw off */
m->wl_refpeakwl = i1pro3_raw2wav_eunc(p, rploc);
//printf(" wl_refpeakloc %d wl_refpeakwl %f\n",m->wl_refpeakloc,m->wl_refpeakwl);
wav_off = i1pro3_raw2wav_eunc(p, rploc + raw_off) - m->wl_refpeakwl;
a1logd(p->log,2, "Final WL raw offset = %f, wav offset %f nm\n",raw_off, wav_off);
if (fabs(wav_off)> m->wl_err_max) {
a1logd(p->log,1,"Final WL correction of %f nm is too big\n",wav_off);
return I1PRO3_WL_ERR2BIG;
}
/* Do a verification plot */
/* Plot the measurement against calibrated wavelength, */
/* and reference measurement verses reference wavelength */
#ifdef PLOT_DEBUG
{
double xx[1024];
double y1[1024]; /* interpolate ref */
double y2[1024]; /* Measurement */
int ii;
/* i = index into measurement */
for (ii = 0, i = 0; i < m->nraw; i++) {
double raw;
double mwl; /* Measurment wavelength */
double rraw; /* Reference raw value */
int ix; /* Lagrange base offset */
int k;
double yv;
raw = (double)i;
raw = raw - raw_off;
mwl = i1pro3_raw2wav_runc(p, raw); // Using m->ee_wl_cal1
xx[ii] = mwl;
y1[ii] = cparm[0] * wlraw[i];
y2[ii] = 0.0;
/* Compute the reference index corresponding to this wavelength */
rraw = inv_raw2wav(m->ee_wl_cal1, mwl);
/* Use Lagrange to interpolate the reference level for this wavelength */
ix = ((int)rraw) - 1; /* Reference index of Lagrange for this xv */
if (ix < 0)
continue;
if ((ix + 3) >= m->nraw)
break;
/* Compute interpolated value of reference using Lagrange: */
yv = m->wl_led_spec[ix+0] * (rraw-(ix+1)) * (rraw-(ix+2)) * (rraw-(ix+3))
/((0.0-1.0) * (0.0-2.0) * (0.0-3.0))
+ m->wl_led_spec[ix+1] * (rraw-(ix+0)) * (rraw-(ix+2)) * (rraw-(ix+3))
/((1.0-0.0) * (1.0-2.0) * (1.0-3.0))
+ m->wl_led_spec[ix+2] * (rraw-(ix+0)) * (rraw-(ix+1)) * (rraw-(ix+3))
/((2.0-0.0) * (2.0-1.0) * (2.0-3.0))
+ m->wl_led_spec[ix+3] * (rraw-(ix+0)) * (rraw-(ix+1)) * (rraw-(ix+2))
/((3.0-0.0) * (3.0-1.0) * (3.0-2.0));
y2[ii] = yv;
ii++;
}
printf("Verification fit in nm:\n");
do_plot(xx, y1, y2, NULL, ii);
}
#endif
if (praw_off != NULL)
*praw_off = raw_off;
if (pwav_off != NULL)
*pwav_off = wav_off;
}
return ev;
}
/* Recompute normal & hi-res wav filters using the values given, */
/* if they are sufficiently different from current or force. */
i1pro3_code i1pro3_compute_wav_filters(i1pro3 *p, double wl_raw_off, double wl_wav_off, int force) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_code ev = I1PRO3_OK;
int ref, hr;
double rdiff, wdiff;
a1logd(p->log,3,"i1pro3_compute_wav_filter() raw %f wav %f force %d\n",wl_raw_off, wl_wav_off, force);
/* Check them */
rdiff = fabs(m->wl_raw_off - wl_raw_off);
wdiff = fabs(m->wl_wav_off - wl_wav_off);
if (!force && rdiff < 0.03 && wdiff < 0.09999) {
a1logd(p->log,3,"i1pro3_compute_wav_filter() ignored because rdiff %f wdiff %f\n",rdiff,wdiff);
return I1PRO3_OK;
}
/* Use them */
m->wl_raw_off = wl_raw_off;
m->wl_wav_off = wl_wav_off;
/* Compute the emissive, reflective standard and hi-res raw->wav sampling filters */
for (hr = 0; hr < 2; hr++) {
for (ref = 0; ref < 2; ref++) {
if ((ev = i1pro3_compute_wav_filter(p, hr, ref)) != I1PRO3_OK) {
a1logd(p->log,2,"i1pro3_compute_wav_filter() failed\n");
return ev;
}
}
}
return I1PRO3_OK;
}
/* Set the current wl_raw/wav_off for the given board temperature and then */
/* recompute normal & hi-res wav filters if the wl offset has changed. */
i1pro3_code i1pro3_recompute_wav_filters_for_temp(i1pro3 *p, double temp) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
double wl_raw_off = 0.0, wl_wav_off = 0.0;
a1logd(p->log,2,"i1pro3_recompute_wav_filters_for_temp got del temp %f\n",temp - s->wl_temp);
if (m->wl_tempcoef == 0.0 || (temp - s->wl_temp) == 0.0)
return I1PRO3_OK;
if (s->wl_cal_raw_off != 0.0) {
double delwl = - (temp - s->wl_temp) * m->wl_tempcoef;
double delraw = inv_raw2wav(m->ee_wl_cal2, m->wl_refpeakwl + delwl) - m->wl_refpeakloc;
wl_raw_off = s->wl_cal_raw_off + delraw;
a1logd(p->log,2," del wl offset %f del raw %f\n",delwl,delraw);
#ifdef NEVER // verify raw offset in wl
delwl = i1pro3_raw2wav_eunc(p, m->wl_refpeakloc + delraw)
- i1pro3_raw2wav_eunc(p, m->wl_refpeakloc);
a1logd(p->log,2," verify del wl %f\n",delwl);
#endif
} else {
double delwl = - (temp - s->wl_temp) * m->wl_tempcoef;
wl_wav_off = s->wl_cal_wav_off + delwl;
a1logd(p->log,2," del wl offset %f\n",delwl);
}
return i1pro3_compute_wav_filters(p, wl_raw_off, wl_wav_off, 0);
}
/* Compute standard/high res. downsampling filters for the current mode */
/* given the current wl_raw_off/wl_wav_off, and set them as current, */
/* using triangular filters of the lagrange interpolation of the */
/* CCD values (i.e. the same type of filter used by the OEM driver) */
/* [ Interestingly, the resulting filter shape is a bit like lanczos2, */
/* but not identical. ] */
i1pro3_code i1pro3_compute_wav_filter(i1pro3 *p, int hr, int refl) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
double twidth; /* Target filter width */
int six, eix; /* raw starting index and one past end index */
int wlix; /* current wavelenght index */
double *wlcop; /* This wavelength base filter coefficient pointer */
double trh, trx; /* Triangle height and triangle equation x weighting */
int i, j, k;
int r2wt = refl; /* raw2wav lookup table to use */
// printf("i1pro3_compute_wav_filter called hr %d refl %d with correction %f raw %f wav\n",hr,refl,m->wl_raw_off, m->wl_wav_off);
a1logd(p->log,2,"i1pro3_compute_wav_filter called hr %d refl %d with correction %f raw %f wav\n",hr,refl,m->wl_raw_off,m->wl_wav_off);
twidth = (m->wl_long[hr] - m->wl_short[hr])/(m->nwav[hr] - 1.0); /* Filter width */
trh = 1.0/twidth; /* Triangle height */
trx = trh/twidth; /* Triangle equation x weighting */
/* Allocate space for the filter coefficients */
if (m->mtx[hr][refl].index == NULL) {
if ((m->mtx[hr][refl].index = (int *)calloc(m->nwav[hr], sizeof(int))) == NULL) {
a1logd(p->log,1,"i1pro3: malloc index failed!\n");
return I1PRO3_INT_MALLOC;
}
if ((m->mtx[hr][refl].nocoef = (int *)calloc(m->nwav[hr], sizeof(int))) == NULL) {
a1logd(p->log,1,"i1pro3: malloc nocoef failed!\n");
return I1PRO3_INT_MALLOC;
}
if ((m->mtx[hr][refl].coef = (double *)calloc(16 * m->nwav[hr], sizeof(double)))
== NULL) {
a1logd(p->log,1,"i1pro3: malloc coef failed!\n");
return I1PRO3_INT_MALLOC;
}
}
/* For each output wavelength */
wlcop = m->mtx[hr][refl].coef;
for (wlix = 0; wlix < m->nwav[hr]; wlix++) {
double owl = wlix/(m->nwav[hr]-1.0) * (m->wl_long[hr] - m->wl_short[hr]) + m->wl_short[hr];
int lip; /* Lagrange interpolation position */
//printf("Generating filter for %.1f nm width %.1f nm\n",owl, twidth);
//printf("~1 coefix = %d\n",wlcop - m->mtx[hr][refl].coef);
/* The filter is based on a triangle centered at owl and extending */
/* from owl - twidth to owl + twidth. We therefore need to locate the */
/* raw values that will overlap this range */
/* Do a dumb search from high to low nm to find raw index range */
for (six = 0; six < m->nraw; six++) {
//printf("~1 (raw2wav ix %d -> %f <? (owl %f + twidth %f) %f\n",six,i1pro3_raw2wav(p, r2wt, (double)six),owl,twidth,owl + twidth);
if (i1pro3_raw2wav(p, r2wt, (double)six) < (owl + twidth))
break;
}
if (six < 2 || six >= m->nraw) {
a1loge(p->log,1,"i1pro3: compute_wav_filters() six %d, exceeds raw range to cover output filter %.1f nm width %.1f nm\n",six, owl, twidth);
return I1PRO3_INT_ASSERT;
}
eix = six;
six -= 2; /* Outside */
/* Continue search for other edge */
for (; eix < m->nraw; eix++) {
if (i1pro3_raw2wav(p, r2wt, (double)eix) <= (owl - twidth))
break;
}
if (eix > (m->nraw - 2) ) {
a1loge(p->log,1,"i1pro3: compute_wav_filters() eix %d, exceeds raw range to cover output filter %.1f nm width %.1f nm\n",eix, owl, twidth);
return I1PRO3_INT_ASSERT;
}
eix += 2; /* Outside */
//for (j = six; j < eix; j++) printf("Using raw %d @ %.1f nm del %.1f\n",j, i1pro3_raw2wav(p, r2wt, (double)j), i1pro3_raw2wav(p, r2wt, (double)j) - owl);
/* Set start index for this wavelength */
m->mtx[hr][refl].index[wlix] = six;
/* Set number of filter coefficients */
m->mtx[hr][refl].nocoef[wlix] = eix - six;
if (m->mtx[hr][refl].nocoef[wlix] > 16) {
a1loge(p->log,1,"i1pro3: compute_wav_filters() too many filter %d\n",
m->mtx[hr][refl].nocoef[wlix]);
return I1PRO3_INT_ASSERT;
}
/* Start with zero filter weightings */
for (i = 0; i < m->mtx[hr][refl].nocoef[wlix]; i++)
wlcop[i] = 0.0;
/* For each Lagrange interpolation position (adjacent CCD locations) */
/* create the Lagrange and then acumulate the integral of the convolution */
/* of the overlap of the central region, with the triangle of our */
/* underlying re-sampling filter. */
/* (If we were to run out of enough source points for the Lagrange to */
/* encompas the region, then in theory we could use the Lagrange to */
/* extrapolate beyond the end from points within.) */
for (lip = six; (lip + 3) < eix; lip++) {
double rwav[4]; /* Relative wavelength of these Lagrange points */
double den[4]; /* Denominator values for points */
double num[4][4]; /* Numerator polinomial components x^3, x^2, x, 1 */
double ilow, ihigh; /* Integration points */
/* Relative wavelengths to owl of each basis point */
for (i = 0; i < 4; i++)
rwav[i] = i1pro3_raw2wav(p, r2wt, (double)lip + i) - owl;
//printf("\n~1 rwav = %f %f %f %f\n", rwav[0], rwav[1], rwav[2], rwav[3]);
/* Compute each basis points Lagrange denominator values */
den[0] = (rwav[0]-rwav[1]) * (rwav[0]-rwav[2]) * (rwav[0]-rwav[3]);
den[1] = (rwav[1]-rwav[0]) * (rwav[1]-rwav[2]) * (rwav[1]-rwav[3]);
den[2] = (rwav[2]-rwav[0]) * (rwav[2]-rwav[1]) * (rwav[2]-rwav[3]);
den[3] = (rwav[3]-rwav[0]) * (rwav[3]-rwav[1]) * (rwav[3]-rwav[2]);
//printf("~1 denominators = %f %f %f %f\n", den[0], den[1], den[2], den[3]);
/* Compute each basis points Langrange numerator components. */
/* We make the numerator have polinomial form, so that it is easy */
/* to compute the integral equation from it. */
num[0][0] = 1.0;
num[0][1] = -rwav[1] - rwav[2] - rwav[3];
num[0][2] = rwav[1] * rwav[2] + rwav[1] * rwav[3] + rwav[2] * rwav[3];
num[0][3] = -rwav[1] * rwav[2] * rwav[3];
num[1][0] = 1.0;
num[1][1] = -rwav[0] - rwav[2] - rwav[3];
num[1][2] = rwav[0] * rwav[2] + rwav[0] * rwav[3] + rwav[2] * rwav[3];
num[1][3] = -rwav[0] * rwav[2] * rwav[3];
num[2][0] = 1.0;
num[2][1] = -rwav[0] - rwav[1] - rwav[3];
num[2][2] = rwav[0] * rwav[1] + rwav[0] * rwav[3] + rwav[1] * rwav[3];
num[2][3] = -rwav[0] * rwav[1] * rwav[3];
num[3][0] = 1.0;
num[3][1] = -rwav[0] - rwav[1] - rwav[2];
num[3][2] = rwav[0] * rwav[1] + rwav[0] * rwav[2] + rwav[1] * rwav[2];
num[3][3] = -rwav[0] * rwav[1] * rwav[2];
//printf("~1 num %d = %f %f %f %f\n", 0, num[0][0], num[0][1], num[0][2], num[0][3]);
//printf("~1 num %d = %f %f %f %f\n", 1, num[1][0], num[1][1], num[1][2], num[1][3]);
//printf("~1 num %d = %f %f %f %f\n", 2, num[2][0], num[2][1], num[2][2], num[2][3]);
//printf("~1 num %d = %f %f %f %f\n", 3, num[3][0], num[3][1], num[3][2], num[3][3]);
/* Now compute the integral difference between the two middle points */
/* of the Lagrange over the triangle shape, and accumulate the resulting */
/* Lagrange weightings to the filter coefficients. */
/* For high and then low side of the triangle. */
for (k = 0; k < 2; k++) {
ihigh = rwav[1];
ilow = rwav[2];
/* Over just the central portion, if it overlaps the triangle. */
if ((k == 0 && ilow <= twidth && ihigh >= 0.0) /* Portion is +ve side */
|| (k == 1 && ilow <= 0.0 && ihigh >= -twidth)) { /* Portion is -ve side */
if (k == 0) {
if (ilow < 0.0)
ilow = 0.0;
if (ihigh > twidth)
ihigh = twidth;
//printf("~1 doing +ve triangle between %f %f\n",ilow,ihigh);
} else {
if (ilow < -twidth)
ilow = -twidth;
if (ihigh > 0.0)
ihigh = 0.0;
//printf("~1 doing -ve triangle between %f %f\n",ilow,ihigh);
}
/* For each Lagrange point */
for (i = 0; i < 4; i++) {
double xnum[5]; /* Expanded numerator components */
double nvall, nvalh; /* Numerator low and high values */
/* Because the y value is a function of x, we need to */
/* expand the Lagrange 3rd order polinomial into */
/* a 4th order polinomial using the triangle edge equation */
/* y = trh +- trx * x */
for (j = 0; j < 4; j++)
xnum[j] = (k == 0 ? -trx : trx) * num[i][j];
xnum[j] = 0.0;
for (j = 0; j < 4; j++)
xnum[j+1] += trh * num[i][j];
/* The 4th order equation becomes a 5th order one */
/* when we convert it to an integral, ie. x^4 becomes x^5/5 etc. */
for (j = 0; j < 4; j++)
xnum[j] /= (5.0 - (double)j); /* Integral denom. */
/* Compute ihigh integral as 5th order polynomial */
nvalh = xnum[0];
nvalh = nvalh * ihigh + xnum[1];
nvalh = nvalh * ihigh + xnum[2];
nvalh = nvalh * ihigh + xnum[3];
nvalh = nvalh * ihigh + xnum[4];
nvalh = nvalh * ihigh;
/* Compute ilow integral as 5th order polynomial */
nvall = xnum[0];
nvall = nvall * ilow + xnum[1];
nvall = nvall * ilow + xnum[2];
nvall = nvall * ilow + xnum[3];
nvall = nvall * ilow + xnum[4];
nvall = nvall * ilow;
/* Compute ihigh - ilow and add to filter weightings */
wlcop[lip -six + i] += (nvalh - nvall)/den[i];
//printf("~1 k = %d, comp %d weight += %e den %e now %e\n",k,lip-six+i,(nvalh - nvall)/den[i], den[i], wlcop[lip-six+i]);
}
}
}
}
//printf("~1 Weightings for %.1f nm are:\n",owl);
//for (i = 0; i < m->mtx[hr][refl].nocoef[wlix]; i++)
//printf("~1 comp %d rix %d weight %g\n",i,m->mtx[0][refl].index[wlix] + i, wlcop[i]);
//printf("\n\n");
wlcop += m->mtx[hr][refl].nocoef[wlix]; /* Next group of weightings */
}
#ifdef HIGH_RES_PLOT_WAVFILT
/* Plot standard res. raw->wav re-sampling filters */
{
int cx;
double *xx, *ss;
double **yy;
xx = dvectorz(-1, m->nraw-1); /* X index */
yy = dmatrixz(0, 5, -1, m->nraw-1); /* Curves distributed amongst 5 graphs */
for (i = 0; i < m->nraw; i++)
xx[i] = i;
/* For each output wavelength */
for (cx = j = 0; j < m->nwav[0]; j++) {
int sx;
i = j % 5;
/* For each matrix value */
sx = m->mtx[0][refl].index[j]; /* Starting raw index */
for (k = 0; k < m->mtx[0][refl].nocoef[j]; k++, cx++, sx++) {
yy[5][sx] += 0.5 * m->mtx[0][refl].coef[cx]; /* Sum of coefs */
yy[i][sx] = m->mtx[0][refl].coef[cx];
}
}
printf("raw->wav filter curves %s:\n",refl ? "refl" : "emis");
do_plot6(xx, yy[0], yy[1], yy[2], yy[3], yy[4], yy[5], m->nraw);
free_dvector(xx, -1, m->nraw-1);
free_dmatrix(yy, 0, 2, -1, m->nraw-1);
}
#endif /* PLOT_DEBUG */
return ev;
}
/* Dump the contents of a raw->wav filter to stdout */
i1pro3_code i1pro3_dump_wav_filters(i1pro3 *p, int hr, int refl) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
int cx;
int j, k;
i1pro3_code ev = I1PRO3_OK;
/* For each output wavelength */
for (cx = j = 0; j < m->nwav[hr]; j++) {
/* For each matrix value */
for (k = 0; k < m->mtx[hr][refl].nocoef[j]; k++, cx++) {
printf("wl %d ix %d coef %.15e\n", j, k, m->mtx[hr][refl].coef[cx]);
}
}
return ev;
}
/* =============================================== */
/* return nz if high res is supported at all */
int i1pro3_imp_highres(i1pro3 *p) {
#ifdef HIGH_RES
return 1;
#else
return 0;
#endif /* HIGH_RES */
}
/* Set to high resolution mode */
/* Note that it's important to set mode first... */
i1pro3_code i1pro3_set_highres(i1pro3 *p) {
int i;
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
#ifdef HIGH_RES
if (!s->reflective)
m->highres = 1;
else
ev = I1PRO3_UNSUPPORTED;
#else
ev = I1PRO3_UNSUPPORTED;
#endif /* HIGH_RES */
return ev;
}
/* Set to standard resolution mode */
i1pro3_code i1pro3_set_stdres(i1pro3 *p) {
int i;
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_code ev = I1PRO3_OK;
#ifdef HIGH_RES
m->highres = 0;
#else
ev = I1PRO3_UNSUPPORTED;
#endif /* HIGH_RES */
return ev;
}
/* =============================================== */
/* Modify the scan consistency tolerance */
i1pro3_code i1pro3_set_scan_toll(i1pro3 *p, double toll_ratio) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_code ev = I1PRO3_OK;
if (toll_ratio < 1e-6)
toll_ratio = 1e-6;
m->scan_toll_ratio = toll_ratio;
return I1PRO3_OK;
}
/* Optical adjustment weights */
static double opt_adj_weights[21] = {
1.4944496665144658e-282, 2.0036175483913455e-070, 1.2554893022685038e+232,
2.3898157055642966e+190, 1.5697625128432372e-076, 6.6912978722191457e+281,
1.2369092402930559e+277, 1.4430907501246712e-153, 3.0017439193018232e+238,
1.2978311824382444e+161, 5.5068703318775818e-311, 7.7791723264455314e-260,
6.4560484084110176e+170, 8.9481529920968425e+165, 1.3565405878488529e-153,
2.0835868791190880e-076, 5.4310198502711138e+241, 4.8689849775675438e+275,
9.2709981544886391e+122, 3.7958270103353899e-153, 7.1366083837501666e-154
};
/* Convert from spectral to XYZ, and transfer to the ipatch array. */
/* Apply XRGA conversion if needed */
i1pro3_code i1pro3_conv2XYZ(
i1pro3 *p,
ipatch *vals, /* Values to return */
int nvals, /* Number of values */
double **specrd, /* Spectral readings */
int hr, /* 0 for std. res., 1 for high-res */
instClamping clamp /* Clamp XYZ/Lab to be +ve */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
xsp2cie *conv; /* Spectral to XYZ conversion object */
int i, j, k;
int six = 0; /* Starting index */
int nwl = m->nwav[hr]; /* Number of wavelength */
double wl_short = m->wl_short[hr]; /* Starting wavelength */
double sms; /* Weighting */
if (s->emiss)
conv = new_xsp2cie(icxIT_none, 0.0, NULL, icxOT_CIE_1931_2, NULL, icSigXYZData, (icxClamping)clamp);
else
conv = new_xsp2cie(icxIT_D50, 0.0, NULL, icxOT_CIE_1931_2, NULL, icSigXYZData, (icxClamping)clamp);
if (conv == NULL)
return I1PRO3_INT_CIECONVFAIL;
/* Don't report any wavelengths below the minimum for this mode */
if ((s->min_wl-1e-3) > wl_short) {
double wl = 0.0;
for (j = 0; j < m->nwav[hr]; j++) {
wl = XSPECT_WL(m->wl_short[hr], m->wl_long[hr], m->nwav[hr], j);
if (wl >= (s->min_wl-1e-3))
break;
}
six = j;
wl_short = wl;
nwl -= six;
}
a1logd(p->log,5,"i1pro3_conv2XYZ got wl_short %f, wl_long %f, nwav %d, min_wl %f\n",
m->wl_short[hr], m->wl_long[hr], m->nwav[hr], s->min_wl);
a1logd(p->log,5," after skip got wl_short %f, nwl = %d\n", wl_short, nwl);
for (sms = 0.0, i = 1; i < 21; i++)
sms += opt_adj_weights[i];
sms *= opt_adj_weights[0];
for (i = 0; i < nvals; i++) {
vals[i].loc[0] = '\000';
vals[i].mtype = inst_mrt_none;
vals[i].mcond = inst_mrc_none;
vals[i].XYZ_v = 0;
vals[i].sp.spec_n = 0;
vals[i].duration = 0.0;
vals[i].sp.spec_n = nwl;
vals[i].sp.spec_wl_short = wl_short;
vals[i].sp.spec_wl_long = m->wl_long[hr];
if (s->emiss) {
/* Leave spectral values as mW/m^2 */
for (j = six, k = 0; j < m->nwav[hr]; j++, k++) {
vals[i].sp.spec[k] = specrd[i][j] * sms;
}
vals[i].sp.norm = 1.0;
/* Set the XYZ */
conv->convert(conv, vals[i].XYZ, &vals[i].sp);
vals[i].XYZ_v = 1;
if (s->ambient) {
if (s->flash)
vals[i].mtype = inst_mrt_ambient_flash;
else
vals[i].mtype = inst_mrt_ambient;
} else {
if (s->flash)
vals[i].mtype = inst_mrt_emission_flash;
else
vals[i].mtype = inst_mrt_emission;
}
} else {
/* Scale spectral values to percentage reflectance/transmission */
for (j = six, k = 0; j < m->nwav[hr]; j++, k++)
vals[i].sp.spec[k] = 100.0 * specrd[i][j] * sms;
vals[i].sp.norm = 100.0;
/* Set the XYZ */
conv->convert(conv, vals[i].XYZ, &vals[i].sp);
vals[i].XYZ_v = 1;
vals[i].XYZ[0] *= 100.0;
vals[i].XYZ[1] *= 100.0;
vals[i].XYZ[2] *= 100.0;
if (s->trans)
vals[i].mtype = inst_mrt_transmissive;
else {
vals[i].mtype = inst_mrt_reflective;
if (m->filt == inst3_filter_D50)
vals[i].mcond = inst_mrc_D50;
else if (m->filt == inst3_filter_UVCut)
vals[i].mcond = inst_mrc_uvcut;
else if (m->filt == inst3_filter_pol)
vals[i].mcond = inst_mrc_pol;
}
}
/* Don't return spectral if not asked for */
if (!m->spec_en) {
vals[i].sp.spec_n = 0;
}
}
conv->del(conv);
/* Apply any XRGA conversion */
ipatch_convert_xrga(vals, nvals, xcalstd_nonpol, m->target_calstd, m->native_calstd, clamp);
/* Apply custom filter compensation */
if (m->custfilt_en)
ipatch_convert_custom_filter(vals, nvals, &m->custfilt, clamp);
return I1PRO3_OK;
}
/* Compute the number of measurements needed, given the target */
/* measurement time and integration time. Will return a minimum, of 1 */
int i1pro3_comp_nummeas(
i1pro3 *p,
double meas_time,
double int_time
) {
int nmeas;
if (int_time <= 0.0 || meas_time <= 0.0)
return 1;
nmeas = (int)floor(meas_time/int_time + 0.5);
if (nmeas < 1)
nmeas = 1;
return nmeas;
}
/* Set the noinitcalib mode */
void i1pro3_set_noinitcalib(i1pro3 *p, int v, int losecs) {
i1pro3imp *m = (i1pro3imp *)p->m;
/* Ignore disabling init calib if more than losecs since instrument was open */
if (v && losecs != 0 && m->lo_secs >= losecs) {
a1logd(p->log,3,"initcalib disable ignored because %d >= %d secs\n",m->lo_secs,losecs);
return;
}
m->noinitcalib = v;
}
/* Set the trigger config */
void i1pro3_set_trig(i1pro3 *p, inst_opt_type trig) {
i1pro3imp *m = (i1pro3imp *)p->m;
m->trig = trig;
}
/* Return the trigger config */
inst_opt_type i1pro3_get_trig(i1pro3 *p) {
i1pro3imp *m = (i1pro3imp *)p->m;
return m->trig;
}
/* Switch thread handler */
int i1pro3_event_thread(void *pp) {
int nfailed = 0;
i1pro3 *p = (i1pro3 *)pp;
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_code rv = I1PRO3_OK;
a1logd(p->log,3,"Switch thread started\n");
// for (nfailed = 0;nfailed < 5;)
/* Try indefinitely, in case instrument is put to sleep */
for (;;) {
i1pro3_eve ecode;
rv = i1pro3_waitfor_event_th(p, &ecode, SW_THREAD_TIMEOUT);
a1logd(p->log,8,"Event handler triggered with rv %d, th_term %d\n",rv,m->th_term);
if (m->th_term) {
m->th_termed = 1;
break;
}
if (rv == I1PRO3_INT_BUTTONTIMEOUT) {
nfailed = 0;
continue;
}
if (rv != I1PRO3_OK) {
nfailed++;
a1logd(p->log,3,"Event thread failed with 0x%x\n",rv);
continue;
}
if (ecode == i1pro3_eve_switch_press) {
m->switch_count++;
if (!m->hide_event && p->eventcallback != NULL) {
p->eventcallback(p->event_cntx, inst_event_switch);
}
} else if (ecode == i1pro3_eve_adapt_change) {
if (p->eventcallback != NULL) {
p->eventcallback(p->event_cntx, inst_event_mconf);
}
}
}
a1logd(p->log,3,"Switch thread returning\n");
return rv;
}
/* ============================================================ */
/* Low level i1pro3 commands */
/* USB Instrument commands */
/* Get the firmware version number and/or string */
i1pro3_code
i1pro3_fwver(
i1pro3 *p,
int *no, /* If !NULL, return version * 100 */
char str[50] /* If !NULL, return version string */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char pbuf[57] = { 0 }; /* Recieve buffer */
int slen = 56; /* Buffer length */
int rlen = 0; /* Receved message length */
int se, rv = I1PRO3_OK;
int stime = 0;
int majv, minv, strl;
a1logd(p->log,2,"\ni1pro3_fwver: @ %d msec\n",(stime = msec_time()) - m->msec);
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_IN | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_get_firm_ver, 0, 0, pbuf, slen, &rlen, 2.0);
amutex_unlock(m->lock);
/* We expect a short read error */
if ((se & ICOM_SHORT) && rlen >= 21)
se &= ~ICOM_SHORT;
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_getfwrev: failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
majv = pbuf[0];
minv = pbuf[1];
strl = read_ORD32_le(&pbuf[2]);
if (strl >= 50)
strl = 49;
if (strl >= rlen - 6)
strl = (rlen -6);
if (no != NULL) {
*no = majv * 100 + minv;
}
if (str != NULL) {
strncpy(str, (char *)&pbuf[6], strl);
str[strl] = '\000';
}
a1logd(p->log,2, "i1pro3_fwver: FW Ver. = %d.%d str = '%s', ICOM err 0x%x (%d msec)\n",
majv, minv, &pbuf[6], se, msec_time()-stime);
return rv;
}
/* Get hw parameters */
/* Return pointers may be NULL if not needed. */
i1pro3_code
i1pro3_getparams(
i1pro3 *p,
unsigned int *minintclks, /* Sub clock divider ratio ??? Or min_int_time ? */
unsigned int *eesize, /* EE size, but not used/wrong ?? */
double *intclkp /* Integration clock period ??? */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char pbuf[12]; /* reply bytes read */
unsigned int _minintclks;
unsigned int _eesize;
double _intclkp;
int se, rv = I1PRO3_OK;
int stime = 0;
a1logd(p->log,2,"\ni1pro3_getparams: @ %d msec\n", (stime = msec_time()) - m->msec);
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_IN | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_getparams, 0, 0, pbuf, 12, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_getparams: failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
_minintclks = read_ORD32_le(&pbuf[0]);
_eesize = read_ORD32_le(&pbuf[4]); /* Doesn't seem to be valid */
_intclkp = read_FLT32_le(&pbuf[8]);
a1logd(p->log,2,"i1pro3_getparams: returning %u, %u, %.17g ICOM err 0x%x (%d msec)\n",
_minintclks, _eesize, _intclkp, se, msec_time()-stime);
if (minintclks != NULL) *minintclks = _minintclks; /* 165 */
if (eesize != NULL) *eesize = _eesize;
if (intclkp != NULL) *intclkp = _intclkp; /* 1.5277777492883615e-5 */
return rv;
}
/* Read from the EEProm */
i1pro3_code
i1pro3_readEEProm(
i1pro3 *p,
unsigned char *buf, /* Where to read it to */
int addr, /* Address in EEprom to read from */
int size /* Number of bytes to read (max 65535) */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int rwbytes; /* Data bytes read or written */
unsigned char pbuf[8]; /* Write EEprom parameters */
int len = 8; /* Message length */
int se = 0, rv = I1PRO3_OK;
int stime;
if (size >= 0x10000)
return I1PRO3_INT_EETOOBIG;
a1logd(p->log,2,"\ni1pro3_readEEProm: address 0x%x size 0x%x @ %d msec\n",
addr, size, (stime = msec_time()) - m->msec);
write_INR32_le(&pbuf[0], addr);
write_INR16_le(&pbuf[4], size);
pbuf[6] = pbuf[7] = 0; /* Ignored */
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_OUT | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_read_EE, 0, 0, pbuf, len, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_readEEProm: read failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
/* Now read the bytes */
se = p->icom->usb_read(p->icom, NULL, 0x81, buf, size, &rwbytes, 5.0);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_readEEProm: read failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
if (rwbytes != size) {
a1logd(p->log,1,"i1pro3_readEEProm: 0x%x bytes, short read error\n",rwbytes);
return I1PRO3_HW_EE_SHORTREAD;
}
if (p->log->debug >= 6) {
a1logd(p->log,6,"i1pro3_readEEProm: EE data:\n");
adump_bytes(p->log," ",buf, 0, size);
}
a1logd(p->log,2,"i1pro3_readEEProm: 0x%x bytes, ICOM err 0x%x (%d msec)\n",
rwbytes, se, msec_time()-stime);
return rv;
}
/* Get the Chip ID */
/* (It returns all zero's unless you've read the EEProm first ?) */
i1pro3_code
i1pro3_getchipid(
i1pro3 *p,
unsigned char chipid[8]
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int se, rv = I1PRO3_OK;
a1logd(p->log,2,"\ni1pro3_getchipid: called\n");
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_IN | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_get_chipid, 0, 0, chipid, 8, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_getchipid: failed with ICOM err 0x%x\n",se);
return rv;
}
a1logd(p->log,2,"i1pro3_getchipid: returning %02X-%02X%02X%02X%02X%02X%02X%02X ICOM err 0x%x\n",
chipid[0], chipid[1], chipid[2], chipid[3],
chipid[4], chipid[5], chipid[6], chipid[7], se);
return rv;
}
/* Set the measurement illumination LEDs currents. */
/* All values are 0..255 */
/* Assume these are: Green wl, white 1, white 2, blue, UV in some order... */
i1pro3_code
i1pro3_setledcurrents(
i1pro3 *p,
int led0c, /* */
int led1c, /* */
int led2c, /* */
int led3c, /* */
int led4c /* */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char pbuf[5];
int se, rv = I1PRO3_OK;
int stime = 0;
a1logd(p->log,2,"\ni1pro3_setledcurrents: %d, %d, %d, %d, %d @ %d msec\n",
led0c, led1c, led2c, led3c, led4c,
(stime = msec_time()) - m->msec);
write_ORD8(&pbuf[0], led0c);
write_ORD8(&pbuf[1], led1c);
write_ORD8(&pbuf[2], led2c);
write_ORD8(&pbuf[3], led3c);
write_ORD8(&pbuf[4], led4c);
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_OUT | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_set_led_currents, 0, 0, pbuf, 5, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_setledcurrents: failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
a1logd(p->log,2,"i1pro3_setledcurrents: returning ICOM err 0x%x (%d msec)\n",
se,msec_time()-stime);
return rv;
}
/* Get Adapter Type */
i1pro3_code
i1pro3_getadaptype(
i1pro3 *p,
i1p3_adapter *atype
) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char pbuf[1];
int se, rv = I1PRO3_OK;
int _atype;
a1logd(p->log,2,"\ni1pro3_getadaptype: called\n");
if ((m->capabilities & I1PRO3_CAP_HEAD_SENS) == 0) {
a1logd(p->log,2,"i1pro3_getadaptype: not supported by instrument\n");
return I1PRO3_OK;
}
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_IN | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_get_adap_type, 0, 0, pbuf, 1, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_getadaptype: failed with ICOM err 0x%x\n",se);
return rv;
}
_atype = read_ORD8(pbuf);
a1logd(p->log,2,"i1pro3_getadaptype: returning %d ICOM err 0x%x\n", _atype, se);
if (atype != NULL)
*atype = (i1p3_adapter)_atype;
return rv;
}
/* Get the board temperature */
/* Return pointers may be NULL if not needed. */
i1pro3_code
i1pro3_getboardtemp(
i1pro3 *p,
double *btemp /* Return temperature in degrees C */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char pbuf[4]; /* reply bytes read */
double _btemp;
int se, rv = I1PRO3_OK;
int stime = 0;
a1logd(p->log,2,"\ni1pro3_geboardtemp: @ %d msec\n", (stime = msec_time()) - m->msec);
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_IN | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_get_board_temp, 0, 0, pbuf, 4, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_geboardtemp: failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
_btemp = read_FLT32_le(&pbuf[0]);
a1logd(p->log,2,"i1pro3_geboardtemp: returning %g ICOM err 0x%x (%d msec)\n",
_btemp, se, msec_time()-stime);
if (btemp != NULL) *btemp = _btemp;
return rv;
}
/* Set the scan start indicator parameters */
/* This works with any measurement type (emis/refl, timed or manual length) */
// Default is 0,0 or 1,1.
i1pro3_code
i1pro3_setscanstartind(
i1pro3 *p,
int starttime, /* 2 x min_int_time units after measure command to turn Green indicator on */
int endtime /* 2 x min_int_time units after measure command to turn Green indicator off */
/* endtime must be > starttime or command is ignored */
/* endtime == 255 keeps green on until measure ends */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char pbuf[2];
int se, rv = I1PRO3_OK;
int stime = 0;
a1logd(p->log,2,"\ni1pro3_setscanparams: %d, %d @ %d msec\n",
starttime, endtime, (stime = msec_time()) - m->msec);
if ((m->capabilities & I1PRO3_CAP_IND_LED) == 0) {
a1logd(p->log,2,"i1pro3_setscanparams: not supported by instrument\n");
return inst_ok;
}
write_ORD8(&pbuf[0], starttime);
write_ORD8(&pbuf[1], endtime);
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_OUT | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_set_scan_ind, 0, 0, pbuf, 2, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_setscanparams: failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
a1logd(p->log,2,"i1pro3_setscanparams: returning ICOM err 0x%x (%d msec)\n",
se,msec_time()-stime);
return rv;
}
/* Set the tint multiplier */
/*
For the emissive command:
This multiplies the reflective integration time if >= 2.
The value is rounded down to the nearest even value internally.
If == 2 then every odd sample carries the value,
if == 4 every fourth sample carries the value, etc.
Not very useful ? (change int time instead).
For the reflective command:
This multiplies the reflective integration time if >= 2.
The value is rounded down to the nearest even value internally.
If == 2 the even sample is zero and the x2 int value is delivered in
the odd side of each sensor data. If == 4, the x4 integration sample
is delivered in the odd side of every second sensor data, etc.
Doesn't affect the UV Led muxing.
Maybe useful with polarization filter to improve S/N ratio ?
*/
i1pro3_code
i1pro3_settintmult(
i1pro3 *p,
int tintm /* */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char pbuf[1];
int se, rv = I1PRO3_OK;
int stime = 0;
unsigned int irrc = 0;
a1logd(p->log,2,"\ni1pro3_settintmult: %d @ %d msec\n",
tintm, (stime = msec_time()) - m->msec);
write_ORD8(&pbuf[0], tintm);
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_OUT | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_set_tint_mul, 0, 0, pbuf, 1, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_settintmult: failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
/* Get last error code. What to do with it ?? */
i1pro3_getlasterr(p, &irrc);
a1logd(p->log,2,"i1pro3_settintmult: irrc 0x%x returning ICOM err 0x%x (%d msec)\n",
irrc, se,msec_time()-stime);
return rv;
}
/* Delayed trigger implementation, called from thread below */
/* We assume that the measurement parameters have been set in */
/* the i1pro3imp structure c_* values */
static int
i1pro3_delayed_trigger(
void *pp
) {
i1pro3 *p = (i1pro3 *)pp;
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char pbuf[12]; /* 8 or 12 bytes to write */
int se, rv = I1PRO3_OK;
int stime;
if (m->c_refinst) {
write_ORD32_le(pbuf + 0, m->c_nummeas);
write_ORD32_le(pbuf + 4, m->c_measflags);
} else {
write_ORD32_le(pbuf + 0, m->c_nummeas);
write_ORD32_le(pbuf + 4, m->c_intclocks);
write_ORD32_le(pbuf + 8, m->c_measflags);
}
#ifdef USE_RD_SYNC
a1logd(p->log,7,"\ni1pro3_delayed_trigger: waiting for meas. sync 0x%x\n",&m->rd_sync);
p->icom->usb_wait_io(p->icom, &m->rd_sync); /* Wait for meas or zebra read to start */
a1logd(p->log,7,"i1pro3_delayed_trigger: got meas. sync\n");
#else
/* Delay the trigger */
a1logd(p->log,2,"\ni1pro3_delayed_trigger: start sleep @ %d msec\n",
msec_time() - m->msec);
msec_sleep(m->trig_delay);
#endif
m->tr_t1 = msec_time(); /* Diagnostic */
a1logd(p->log,2,"i1pro3_delayed_trigger: trigger @ %d msec\n",
(stime = msec_time()) - m->msec);
amutex_lock(m->lock);
msec_sleep(1); // ??
m->trigstamp = usec_time();
if (m->c_refinst) {
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_OUT | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_meas_refl, 0, 0, pbuf, 8, NULL, 2.0);
} else {
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_OUT | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_meas_emis, 0, 0, pbuf, 12, NULL, 2.0);
}
amutex_unlock(m->lock);
m->tr_t2 = msec_time(); /* Diagnostic */
m->trig_se = se;
m->trig_rv = icoms2i1pro3_err(se);
a1logd(p->log,2,"i1pro3_delayed_trigger: done ICOM err 0x%x (%d msec)\n",
se,msec_time()-stime);
return 0;
}
/* Trigger either an "emissive" or "reflective" measurement after the delay in msec. */
/* The actual return code will be in m->trig_rv after the delay */
/* This allows us to start the measurement read before the trigger, */
/* ensuring that process scheduling latency can't cause the read to fail. */
/* An "emissive" measurement returns 268 bytes per measurement, composed of */
/* 128 live values and 6 dummy/covered values. */
/* A "reflective" measurement returns 564 bytes per measurement, composed */
/* of two 268 sub-measurements + illuminent LED tracking information. */
/* These two measurements are for two different illuminants, thereby multiplexing */
/* the with/without UV measurement values. */
i1pro3_code
i1pro3_trigger_measure(
i1pro3 *p,
int refinst, /* NZ to trigger a "reflective" measurement */
int zebra, /* NZ if zebra ruler read is being used as well */
int nummeas, /* Number of measurements to make, 0 for infinite ? */
int intclocks, /* Number of integration clocks (ignored but used if refinst) */
int flags, /* Measurement mode flags. Differs with refinst */
int delay /* Delay before triggering command in msec */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int rv = I1PRO3_OK;
a1logd(p->log,2,"\ni1pro3_trigger_measure: triggering %s measurement with nummeas %d intclocks %d flags 0x%x after %dmsec "
"delay @ %d msec\n", refinst ? "reflective" : "emissive", nummeas, intclocks, flags, delay, msec_time() - m->msec);
/* NOTE := would be better here to create a reusable thread once and retrigger ! */
if (m->trig_thread != NULL)
m->trig_thread->del(m->trig_thread);
m->c_refinst = refinst;
m->c_nummeas = nummeas;
m->c_intclocks = intclocks;
m->c_measflags = flags;
m->tr_t1 = m->tr_t2 = m->tr_t3 = m->tr_t4 = m->tr_t5 = m->tr_t6 = m->tr_t7 = 0;
m->trig_delay = delay;
if ((m->trig_thread = new_athread(i1pro3_delayed_trigger, (void *)p)) == NULL) {
a1logd(p->log,1,"i1pro3_trigger_measure: creating delayed trigger Rev E thread failed\n");
return I1PRO3_INT_THREADFAILED;
}
#ifdef WAIT_FOR_DELAY_TRIGGER /* hack to diagnose threading problems */
while (m->tr_t2 == 0) {
Sleep(1);
}
#endif
a1logd(p->log,2,"i1pro3_trigger_measure: scheduled triggering OK\n");
return rv;
}
/* Gather a measurements results. */
/* A buffer full of bytes is returned. */
/* It appears that the read can be pending before triggering though. */
/* Scan reads will also terminate if there is too great a delay beteween each read ? */
static i1pro3_code
i1pro3_gathermeasurement(
i1pro3 *p,
int refinst, /* NZ if we are gathering a measure reflective else emissive */
int zebra, /* NZ if zebra ruler read is being used as well */
int scanflag, /* NZ if in scan mode to continue reading */
int xmeas, /* Expected number of measurements, ignored if scanflag */
unsigned char *buf, /* Where to read it to */
int bsize, /* Bytes available in buffer */
int *nummeas /* Return number of readings measured */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char *ibuf = buf; /* Incoming buffer */
int nsen = refinst ? m->nsen2 : m->nsen1; /* Number of sensor 16 bit words */
double top, extra; /* Time out period */
int ixsize; /* Total expected bytes to read */
int xsize; /* Remaining expected bytes to read */
int rwbytes; /* Data bytes read or written */
int ameas = 0; /* actual measurements */
int asize = 0; /* actual size measured */
int stime = 0;
int se, rv = I1PRO3_OK;
unsigned int irrc = 0;
if ((bsize % (2 * nsen)) != 0) {
a1logd(p->log,1,"i1pro3_gathermeasurement: buffer was not a multiple of sens size\n");
return I1PRO3_INT_ODDREADBUF;
}
a1logd(p->log,2,"\ni1pro3_gathermeasurement: xmeas %d, refinst %d, scanflag %d, address %p bsize 0x%x "
"@ %d msec\n",xmeas, refinst, scanflag, buf, bsize, (stime = msec_time()) - m->msec);
extra = 2.0; /* Extra timeout margin */
if (scanflag == 0) {
ixsize = xmeas * 2 * nsen;
} else {
ixsize = bsize;
xmeas = bsize / (2 * nsen);
}
for (xsize = ixsize; xsize > 0;) {
int size; /* number of bytes to attempt to read */
size = xsize;
if (size > 0x10000) /* Read max at once */
size = 0x10000;
if (size > bsize) { /* oops, no room for read */
unsigned char tbuf[MX_NSEN * 2];
/* One sample at a time.. */
top = extra + (m->c_refinst ? 2.0 : 1.0) * m->c_intclocks * m->intclkp;
/* We need to clean up, so soak up all the data and throw it away */
while ((se = p->icom->usb_read(p->icom, NULL, 0x81, tbuf, 2 * nsen, &rwbytes, top)) == ICOM_OK)
;
a1logd(p->log,1,"i1pro3_gathermeasurement: buffer was too short for scan\n");
return I1PRO3_INT_MEASBUFFTOOSMALL;
}
m->tr_t6 = msec_time(); /* Diagnostic, start of subsequent reads */
if (m->tr_t3 == 0) m->tr_t3 = m->tr_t6; /* Diagnostic, start of first read */
top = extra + size/(2.0 * nsen) * (m->c_refinst ? 2.0 : 1.0) * m->c_intclocks * m->intclkp;
a1logd(p->log,7,"i1pro3_gathermeasurement: size %d timeout set to %f secs\n",size,top);
/* Tell trigger command or zebra read when we have started.. */
se = p->icom->usb_read(p->icom,
xsize == ixsize ? (zebra ? &m->rd_sync2 : &m->rd_sync) : NULL,
0x81, buf, size, &rwbytes, top);
m->tr_t5 = m->tr_t7;
m->tr_t7 = msec_time(); /* Diagnostic, end of subsequent reads */
if (m->tr_t4 == 0) {
m->tr_t5 = m->tr_t2;
m->tr_t4 = m->tr_t7; /* Diagnostic, end of first read */
}
a1logd(p->log,7,"i1pro3_gathermeasurement: returned @ %d msec\n",msec_time());
if (se == ICOM_SHORT) { /* Expect this to terminate scan reading */
a1logd(p->log,2,"i1pro3_gathermeasurement: short read, read %d bytes, asked for %d\n",
rwbytes,size);
a1logd(p->log,2,"i1pro3_gathermeasurement: trig & rd times %d %d %d %d)\n",
m->tr_t2-m->tr_t1, m->tr_t3-m->tr_t2, m->tr_t4-m->tr_t3, m->tr_t6-m->tr_t5);
} else if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
if (m->trig_rv != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_gathermeasurement: trigger failed, ICOM err 0x%x\n",
m->trig_se);
return m->trig_rv;
}
if (se & ICOM_TO)
a1logd(p->log,1,"i1pro3_gathermeasurement: timed out with top = %f\n",top);
a1logd(p->log,1,"i1pro3_gathermeasurement: failed, bytes read 0x%x, ICOM err 0x%x\n",
rwbytes, se);
return rv;
}
/* Track where we're up to */
buf += rwbytes;
bsize -= rwbytes;
xsize -= rwbytes;
asize += rwbytes;
/* Either we're scanning and expect to get a short read at the end of the scan, */
/* or something went wrong with a non-scan measurement. */
if (rwbytes != size) {
break;
}
}
/* Get last error code. What to do with it ?? */
i1pro3_getlasterr(p, &irrc);
/* Not scanning, so expect to read exactly what we asked for */
if (scanflag == 0) {
if (asize != ixsize) {
a1logd(p->log,1,"i1pro3_gathermeasurement: unexpected length read, got %d expected %d\n"
,asize,ixsize);
return I1PRO3_HW_ME_SHORTREAD;
}
/* Scanning, but expect bytes to be a multiple of measurement */
} else {
if ((asize % (2 * nsen)) != 0) {
a1logd(p->log,1,"i1pro3_gathermeasurement: unexpected length read, got %d expected %d\n"
,asize,(asize/(2 * nsen) + 1) * 2 * nsen);
return I1PRO3_HW_ME_SHORTREAD;
}
}
ameas = asize / (2 * nsen);
if (p->log->debug >= 6) {
a1logd(p->log,6,"i1pro3_gathermeasurement: measurement data:\n");
adump_bytes(p->log," ",ibuf, 0, asize);
}
a1logd(p->log,2,"i1pro3_gathermeasurement: read %d readings %d bytes, irrc 0x%x ICOM err 0x%x (%d msec)\n",
ameas, asize, irrc, se, msec_time()-stime);
a1logd(p->log,2,"i1pro3_gathermeasurement: (trig & rd times %d %d %d %d)\n",
m->tr_t2-m->tr_t1, m->tr_t3-m->tr_t2, m->tr_t4-m->tr_t3, m->tr_t6-m->tr_t5);
if (nummeas != NULL) *nummeas = ameas;
return rv;
}
/* Gather a zebra stripe results. */
/* A buffer full of bytes is returned. */
/* This is called in a thread. */
/* This is assumed to be in scan mode */
static i1pro3_code
i1pro3_gatherzebra(
i1pro3 *p,
unsigned char *buf, /* Where to read it to */
int bsize, /* Bytes available in buffer */
int *bread /* Return number of bytes read */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char *ibuf = buf; /* Incoming buffer */
double top, extra; /* Time out period */
int ixsize; /* Total expected bytes to read */
int xsize; /* Remaining expected bytes to read */
int rwbytes; /* Data bytes read or written */
int ameas = 0; /* actual measurements */
int asize = 0; /* actual size measured */
int stime = 0;
int se, rv = I1PRO3_OK;
a1logd(p->log,2,"\ni1pro3_gatherzebra: bsize 0x%x "
"@ %d msec\n",bsize, (stime = msec_time()) - m->msec);
extra = 2.0; /* Extra timeout margin */
ixsize = bsize;
for (xsize = bsize; xsize > 0;) {
int size; /* number of bytes to attempt to read */
size = xsize;
if (size > 65536) /* Read max at once */
size = 65536;
if (size > bsize) { /* oops, no room for read */
unsigned char tbuf[1024];
/* 1024 bytes at a time.. */
top = extra + m->intclkp * 4 * 1024;
/* We need to clean up, so soak up all the data and throw it away */
while ((se = p->icom->usb_read(p->icom, NULL, 0x84, tbuf, 1024, &rwbytes, top)) == ICOM_OK)
;
a1logd(p->log,1,"i1pro3_gatherzebra: buffer was too short for scan\n");
return I1PRO3_INT_MEASBUFFTOOSMALL;
}
top = extra + m->intclkp * 4 * size;
a1logd(p->log,7,"i1pro3_gatherzebra: size %d timeout set to %f secs\n",size,top);
/* Tell trigger command when we have started.. */
se = p->icom->usb_read(p->icom, xsize == ixsize ? &m->rd_sync : NULL,
0x84, buf, size, &rwbytes, top);
if (se == ICOM_SHORT) { /* Expect this to terminate scan reading */
a1logd(p->log,2,"i1pro3_gatherzebra: short read, read %d bytes, asked for %d\n",
rwbytes,size);
} else if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
if (se & ICOM_TO)
a1logd(p->log,1,"i1pro3_gatherzebra: timed out with top = %f\n",top);
a1logd(p->log,1,"i1pro3_gatherzebra: failed, bytes read 0x%x, ICOM err 0x%x\n",
rwbytes, se);
return rv;
}
/* Track where we're up to */
buf += rwbytes;
bsize -= rwbytes;
xsize -= rwbytes;
asize += rwbytes;
/* We expect to get a short read at the end of the scan */
if (rwbytes != size) {
break;
}
}
if (p->log->debug >= 6) {
a1logd(p->log,6,"i1pro3_gatherzebra: zebra data:\n");
adump_bytes(p->log," ",ibuf, 0, asize);
}
a1logd(p->log,2,"i1pro3_gatherzebra: read %d bytes ICOM err 0x%x (%d msec)\n",
asize, se, msec_time()-stime);
if (bread != NULL) *bread = asize;
return rv;
}
/* Delayed simulated event implementation, called from thread below */
static int
i1pro3_delayed_simulate_event(
void *pp
) {
i1pro3 *p = (i1pro3 *)pp;
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char buf[1]; /* 1 byte to write */
int se, rv = I1PRO3_OK;
a1logd(p->log,2,"\ni1pro3_delayed_simulate_event: 0x%x, delay %d msec\n",
m->seve_code, m->seve_delay);
msec_sleep(m->seve_delay);
write_ORD8(&buf[0], m->seve_code);
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_OUT | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_sim_event, 0, 0, buf, 1, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK)
a1logd(p->log,1,"i1pro3_delayed_simulate_event: event 0x%x failed with ICOM err 0x%x\n",
m->seve_code,se);
else
a1logd(p->log,2,"i1pro3_delayed_simulate_event: 0x%x done, ICOM err 0x%x\n",m->seve_code,se);
m->seve_se = se;
m->seve_rv = icoms2i1pro3_err(se);
return 0;
}
/* Simulating an event. If delay > 0, send simulated event after delay msec */
i1pro3_code i1pro3_simulate_event(i1pro3 *p, i1pro3_eve ecode, int delay) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char buf[1]; /* 1 byte to write */
int se, rv = I1PRO3_OK;
a1logd(p->log,2,"\ni1pro3_simulate_event: 0x%x\n",ecode);
if (delay > 0) {
m->seve_delay = delay;
m->seve_code = ecode;
m->seve_se = 0;
m->seve_rv = 0;
if ((m->seve_thread = new_athread(i1pro3_delayed_simulate_event, (void *)p)) == NULL) {
a1logd(p->log,1,"i1pro3_simulate_event: creating delayed eevent thread failed\n");
return I1PRO3_INT_THREADFAILED;
}
return rv;
}
write_ORD8(&buf[0], ecode);
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_OUT | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_sim_event, 0, 0, buf, 1, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK)
a1logd(p->log,1,"i1pro3_simulate_event: event 0x%x failed with ICOM err 0x%x\n",ecode,se);
else
a1logd(p->log,2,"i1pro3_simulate_event: 0x%x done, ICOM err 0x%x\n",ecode,se);
return rv;
}
/* Wait for a reply triggered by an instrument event */
i1pro3_code i1pro3_waitfor_event(i1pro3 *p, i1pro3_eve *ecode, double top) {
i1pro3imp *m = (i1pro3imp *)p->m;
int rwbytes; /* Data bytes read */
unsigned char buf[1]; /* Result */
int se, rv = I1PRO3_OK;
int stime = 0;
i1pro3_eve _ecode;
a1logd(p->log,2,"\ni1pro3_waitfor_event: read 1 byte from event hit port @ %d msec\n",
(stime = msec_time()) - m->msec);
/* Send this only once ? */
if (!m->get_events_sent) {
/* Maybe the parameter is an event mask (Typical value ix 0x7) ? */
write_ORD8(&buf[0], m->ee_button_bytes);
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_OUT | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_get_events, 0, 0, buf, 1, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_waitfor_event_th: failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
m->get_events_sent = 1;
}
/* Now read bytes */
se = p->icom->usb_read(p->icom, NULL, 0x83, buf, 1, &rwbytes, top);
if (se & ICOM_TO) {
a1logd(p->log,2,"i1pro3_waitfor_event: read 0x%x bytes, timed out (%d msec)\n",
rwbytes,msec_time()-stime);
return I1PRO3_INT_BUTTONTIMEOUT;
}
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_waitfor_event: failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
if (rwbytes != 1) {
a1logd(p->log,1,"i1pro3_waitfor_event: read 0x%x bytes, short read error (%d msec)\n",
rwbytes,msec_time()-stime);
return I1PRO3_HW_SW_SHORTREAD;
}
_ecode = (i1pro3_eve) read_ORD8(&buf[0]);
if (p->log->debug >= 2) {
char sbuf[100];
if (_ecode == i1pro3_eve_none)
strcpy(sbuf, "None");
else if (_ecode == i1pro3_eve_switch_press)
strcpy(sbuf, "Button press");
else if (_ecode == i1pro3_eve_switch_release)
strcpy(sbuf, "Button release");
else if (_ecode == i1pro3_eve_adapt_change)
strcpy(sbuf, "Adaptor change");
else
sprintf(sbuf,"Unknown 0x%x",_ecode);
a1logd(p->log,2,"i1pro3_waitfor_event: Event %s @ %d msec ICOM err 0x%x\n",
sbuf, (stime = msec_time()) - m->msec, se);
}
return rv;
}
/* Wait for a reply triggered by an instrument event (thread version) */
/* Returns I1PRO3_OK if the switch has been pressed or some other event such */
/* as an adapter type change, or I1PRO3_INT_BUTTONTIMEOUT if */
/* no event has occured before the time expired, */
/* or some other error. */
i1pro3_code i1pro3_waitfor_event_th(i1pro3 *p, i1pro3_eve *ecode, double top) {
i1pro3imp *m = (i1pro3imp *)p->m;
int rwbytes; /* Data bytes read */
unsigned char buf[1]; /* Result */
int se, rv = I1PRO3_OK;
int stime = 0;
i1pro3_eve _ecode;
a1logd(p->log,2,"\ni1pro3_waitfor_event_th: read 1 byte from event hit port @ %d msec\n",
(stime = msec_time()) - m->msec);
/* Send this only once ? */
if (!m->get_events_sent) {
/* Maybe the parameter is an event mask (Typical value ix 0x7) ? */
write_ORD8(&buf[0], m->ee_button_bytes);
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_OUT | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_get_events, 0, 0, buf, 1, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_waitfor_event_th_setup: failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
m->get_events_sent = 1;
}
/* Now read bytes */
se = p->icom->usb_read(p->icom, &m->sw_cancel, 0x83, buf, 1, &rwbytes, top);
if (se & ICOM_TO) {
a1logd(p->log,2,"i1pro3_waitfor_event_th: read 0x%x bytes, timed out (%d msec)\n",
rwbytes,msec_time()-stime);
return I1PRO3_INT_BUTTONTIMEOUT;
}
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_waitfor_event_th: failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
if (rwbytes != 1) {
a1logd(p->log,1,"i1pro3_waitfor_event_th: read 0x%x bytes, short read error (%d msec)\n",
rwbytes,msec_time()-stime);
return I1PRO3_HW_SW_SHORTREAD;
}
_ecode = (i1pro3_eve) read_ORD8(&buf[0]);
if (p->log->debug >= 2) {
char sbuf[100];
if (_ecode == i1pro3_eve_none)
strcpy(sbuf, "None");
else if (_ecode == i1pro3_eve_switch_press)
strcpy(sbuf, "Button press");
else if (_ecode == i1pro3_eve_switch_release)
strcpy(sbuf, "Button release");
else if (_ecode == i1pro3_eve_adapt_change)
strcpy(sbuf, "Adaptor change");
else
sprintf(sbuf,"Unknown 0x%x",_ecode);
a1logd(p->log,2,"i1pro3_waitfor_event_th: Event %s @ %d msec ICOM err 0x%x\n",
sbuf, (stime = msec_time()) - m->msec, se);
}
if (ecode != NULL)
*ecode = _ecode;
return rv;
}
/* Terminate event handling by cancelling the thread i/o */
i1pro3_code
i1pro3_terminate_event(
i1pro3 *p
) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char pbuf[8]; /* 8 bytes to write */
int se, rv = I1PRO3_OK;
a1logd(p->log,2,"i1pro3_terminate_event: called\n");
if (m->th_termed == 0) {
a1logd(p->log,3,"i1pro3 terminate event thread failed, canceling I/O\n");
p->icom->usb_cancel_io(p->icom, &m->sw_cancel);
}
return rv;
}
/* Get last error code */
/* Return pointers may be NULL if not needed. */
i1pro3_code
i1pro3_getlasterr(
i1pro3 *p,
unsigned int *errc
) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned char pbuf[4]; /* reply bytes read */
unsigned int _errc;
int se, rv = I1PRO3_OK;
int stime = 0;
a1logd(p->log,2," i1pro3_getlasterr: @ %d msec\n", (stime = msec_time()) - m->msec);
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_IN | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_get_last_err, 0, 0, pbuf, 4, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1," i1p3cc_get_last_err: failed with ICOM err 0x%x (%d msec)\n",se, msec_time()-stime);
return rv;
}
_errc = read_ORD32_le(&pbuf[0]);
a1logd(p->log,2," i1p3cc_get_last_err: returning %u ICOM err 0x%x (%d msec)\n",
_errc, se, msec_time()-stime);
if (errc != NULL) *errc = _errc;
return rv;
}
/* Send a raw indicator LED sequence.
The instruction sends a 32 bit le length of the data to follow.
The data is sent to e.p. 2.
Usually groups of 3 bytes.
If < 3 bytes, indicator is turned off ?
First byte = color & pulse type
Off:
1xxx xxxx ??
0xx0 00 xx Off ??
Red:
0xxx 11x1 Blink once, ignore timing & repeats
0xxx 1110 Pulse up & down using timing & repeats
Green:
0xxx 10x1 Blink once, ignore timing & repeats
0xxx 1010 Pulse up & down using timing & repeats
White:
0xx1 00x1 Blink once, ignore timing & repeats
0xx1 0010 Pulse up & down using timing & repeats
Second byte = timing:
Blink:
ms 4 bits sets blink time:
0 .. 8 = time, number-1 of 75 msec periods.
>= 9 = infinity
Pulse: ls 4 bits = cycle period divider out of 11 seconds
ie. 1 = 11 s, 2 = 5.5 s, 5 = 2.2 s. 0xA = 1.1 s, 14 = 0.5 sec
Last byte is count
Blink:
Ignored (always 1)
Pulse:
Number of pulses, 0 = infinity.
Examples seen in traces:
11 fa 00
01 fa 04 12 05 00 White blink then white pulse ?
0a 14 02 12 05 00 Two fast green pulse then white pulses
06 05 01 11 fa 00
Functions found:
05 01 11 fa 00
fa 00
14 04 11 fa 00
14 01 01 fa 19 12 05 00
14 02 80 fa 01
14 02 12 05 00
14 01 06 06 14 02 12 05
00 01
14 01 10 14 01 80 fa
fa 04 12 05 00
*/
static int
i1pro3_indLEDseq(void *pp, unsigned char *buf, int size) {
i1pro3 *p = (i1pro3 *)pp;
i1pro3imp *m = (i1pro3imp *)p->m;
int rwbytes; /* Data bytes written */
unsigned char pbuf[4]; /* Number of bytes being sent */
int se, rv = I1PRO3_OK;
write_INR32_le(pbuf, size);
a1logd(p->log,2,"\ni1pro3_indLEDseq: length %d bytes\n", size);
if ((m->capabilities & I1PRO3_CAP_IND_LED) == 0) {
a1logd(p->log,2,"i1pro3_indLEDseq: not supported by instrument\n");
return inst_ok;
}
amutex_lock(m->lock);
msec_sleep(1); // ??
se = p->icom->usb_control(p->icom,
IUSB_ENDPOINT_OUT | IUSB_REQ_TYPE_VENDOR | IUSB_REQ_RECIP_DEVICE,
i1p3cc_set_ind, 0, 0, pbuf, 4, NULL, 2.0);
amutex_unlock(m->lock);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_indLEDseq: failed with ICOM err 0x%x\n",rv);
return rv;
}
a1logd(p->log,2,"i1pro3_geteesize: command got ICOM err 0x%x\n", se);
/* Now write the bytes */
se = p->icom->usb_write(p->icom, NULL, 0x02, buf, size, &rwbytes, 5.0);
if ((rv = icoms2i1pro3_err(se)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_indLEDseq: data write failed with ICOM err 0x%x\n",se);
return rv;
}
if (rwbytes != size) {
a1logd(p->log,1,"i1pro3_indLEDseq: wrote 0x%x bytes, short write error\n",rwbytes);
return I1PRO3_HW_LED_SHORTWRITE;
}
a1logd(p->log,2,"i1pro3_indLEDseq: wrote 0x%x bytes LED sequence, ICOM err 0x%x\n", size, rv);
return rv;
}
/* Turn indicator LEDs off */
static int
i1pro3_indLEDoff(void *pp) {
i1pro3 *p = (i1pro3 *)pp;
int rv = I1PRO3_OK;
unsigned char seq[] = { 0x00 };
a1logd(p->log,2,"i1pro3_indLEDoff: called\n");
rv = i1pro3_indLEDseq(p, seq, sizeof(seq));
a1logd(p->log,2,"i1pro3_indLEDoff: returning ICOM err 0x%x\n",rv);
return rv;
}
/* ============================================================ */
/* Parse the EEProm contents */
/* Initialise the calibration from the EEProm contents. */
i1pro3_code i1pro3_parse_eeprom(i1pro3 *p, unsigned char *buf, unsigned int len) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1data3 *d;
int rv = I1PRO3_OK;
ORD32 crc;
int boff; /* Block start offset */
int i, j;
a1logd(p->log,2,"i1pro3_parse_eeprom: called with %d bytes\n",len);
/* Create class to handle EEProm parsing */
if ((d = new_i1data3(p, buf, len)) == NULL)
{ d->del(d); return I1PRO3_INT_CREATE_EEPROM_STORE; }
/* - - - - - - - - - - - - - - - - - - */
/* Parse block 0, the base information */
boff = 0;
d->init_crc(d);
if (d->get_32_ints(d, &m->ee_crc_0, boff + 0x0, 1, 0) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_version, boff + 0x4, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
a1logd(p->log,2,"EE version = %u\n",m->ee_version);
if (m->ee_version == 0)
warning("Operation of i1Pro3 Rev 0 will not be accurate");
if (m->ee_version > 1)
warning("Operation of i1Pro3 Rev %d has not be verified");
/* Hmm. we could do this.. */
// { d->del(d); return I1PRO3_HW_EE_VERSION; }
if (d->get_32_ints(d, &m->ee_unk01, boff + 0x8, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
/* Block offsets for the remaining blocks */
if (d->get_16_ints(d, &m->ee_bk1_off, boff + 0xc, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, &m->ee_bk2_off, boff + 0xe, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, &m->ee_bk3_off, boff + 0x10, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, &m->ee_bk4_off, boff + 0x12, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, &m->ee_bk5_off, boff + 0x14, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, &m->ee_bk6_off, boff + 0x16, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
/* Check the CRC */
crc = d->get_crc(d);
a1logd(p->log,3,"i1pro3_parse_eeprom: block 0 crc = 0x%x, should be 0x%x - %s\n",
crc, m->ee_crc_0, crc == m->ee_crc_0 ? "OK": "BAD");
if (crc != m->ee_crc_0)
{ d->del(d); return I1PRO3_HW_EE_CHKSUM; }
#ifdef NEVER /* We're not using or maintaining the EEProm last cal. log */
/* (We also don't compute the checksum if we do) */
# pragma message("######### i1pro3_imp.c reading EEProm last cal log ########")
/* Block 1 */
boff = m->ee_bk1_off;
d->init_crc(d);
if (d->get_32_ints(d, &m->ee_logA_crc, boff + 0x0, 1, 0) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logA_remspotcalc, boff + 0x4, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logA_scanmeasc, boff + 0x8, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_logA_lamptime, boff + 0xc, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logA_emspotcalc, boff + 0x10, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logA_emscancalc, boff + 0x14, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, m->ee_logA_darkreading, boff + 0x18, 128, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, m->ee_logA_whitereading, boff + 0x218, 128, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logA_caldate, boff + 0x418, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logA_calcount, boff + 0x41c, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logA_caldate2, boff + 0x420, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logA_calcount2, boff + 0x424, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (m->ee_version >= 1) {
if (d->get_32_ints(d, m->ee1_logA_unk02, boff + 0x428, 128, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
}
crc = d->get_crc(d);
a1logd(p->log,3,"i1pro3_parse_eeprom: block 1 crc = 0x%x, should be 0x%x - %s\n",
crc, m->ee_logA_crc, crc == m->ee_logA_crc ? "OK": "BAD");
/* Block 2 */
boff = m->ee_bk2_off;
d->init_crc(d);
if (d->get_32_ints(d, &m->ee_logB_crc, boff + 0x0, 1, 0) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logB_remspotcalc, boff + 0x4, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_scanmeasc, boff + 0x8, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_logB_lamptime, boff + 0xc, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logB_emspotcalc, boff + 0x10, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logB_emscancalc, boff + 0x14, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, m->ee_logB_darkreading, boff + 0x18, 128, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, m->ee_logB_whitereading, boff + 0x218, 128, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logB_caldate, boff + 0x418, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logB_calcount, boff + 0x41c, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logB_caldate2, boff + 0x420, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_logB_calcount2, boff + 0x424, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (m->ee_version >= 1) {
if (d->get_32_ints(d, m->ee1_logB_unk02, boff + 0x428, 128, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
}
crc = d->get_crc(d);
a1logd(p->log,3,"i1pro3_parse_eeprom: block 2 crc = 0x%x, should be 0x%x - %s\n",
crc, m->ee_logB_crc, crc == m->ee_logB_crc ? "OK": "BAD");
#endif /* NEVER */
/* Block 3 */
boff = m->ee_bk3_off;
d->init_crc(d);
if (d->get_32_ints(d, &m->ee_crc_3, boff + 0x0, 1, 0) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk03, boff + 0x4, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk04, boff + 0x8, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->rget_32_doubles(d, m->ee_lin, boff + 0xc, 4, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ee_white_ref, boff + 0x1c, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ee_uv_white_ref, boff + 0xac, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ee_emis_coef, boff + 0x13c, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ee_amb_coef, boff + 0x1cc, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk05, boff + 0x25c, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk06, boff + 0x260, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk07, boff + 0x264, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk08, boff + 0x268, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_wl_cal_min_level, boff + 0x26c, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk09, boff + 0x270, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk10, boff + 0x274, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk11, boff + 0x278, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk12, boff + 0x27c, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk13, boff + 0x280, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_wlcal_intt, boff + 0x284, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_sens_sat, boff + 0x288, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_serno, boff + 0x28c, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_dom1, boff + 0x290, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_dom2, boff + 0x294, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, m->ee_unk14, boff + 0x298, 5, 0) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk15, boff + 0x2ac, 1, 0) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_devtype, boff + 0x2b0, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk16, boff + 0x2b4, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_8_char(d, m->ee_chipid, boff + 0x2b8, 8, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_capabilities, boff + 0x2c0, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk17, boff + 0x2c4, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_bk_v_limit, boff + 0x2c8, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_bk_f_limit, boff + 0x2cc, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_unk18, boff + 0x2d0, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_unk19, boff + 0x2d4, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_unk20, boff + 0x2d8, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_unk21, boff + 0x2dc, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_unk22, boff + 0x2e0, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_unk23, boff + 0x2e4, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_unk24, boff + 0x2e8, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_unk25, boff + 0x2ec, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, m->ee_wlcal_spec, boff + 0x2f0, 128, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, m->ee_uv_wlcal_spec, boff + 0x3f0, 128, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_wlcal_max, boff + 0x4f0, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_fwhm, boff + 0x4f4, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_fwhm_tol, boff + 0x4f8, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_unk35, boff + 0x4fc, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, m->ee_straylight, boff + 0x500, 1296, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_straylight_scale, boff + 0xf20, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ee_wl_cal1, boff + 0xf24, 128, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ee_wl_cal2, boff + 0x1124, 128, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_unk26, boff + 0x1324, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, &m->ee_led_w_cur, boff + 0x1328, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, &m->ee_led_b_cur, boff + 0x132a, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, &m->ee_led_luv_cur, boff + 0x132c, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, &m->ee_led_suv_cur, boff + 0x132e, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, &m->ee_led_gwl_cur, boff + 0x1330, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, &m->ee_button_bytes, boff + 0x1332, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
/* Load LED drift model directly into organized array */
if (d->get_32_doubles(d, m->ledm_poly[1][2][0], boff + 0x1334, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[1][2][1], boff + 0x13c4, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[1][2][2], boff + 0x1454, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
vect_set(m->ledm_poly[1][2][3], 0.0, 36);
if (d->get_32_doubles(d, m->ledm_poly[1][3][0], boff + 0x1478, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[1][3][1], boff + 0x1508, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[1][3][2], boff + 0x1598, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
vect_set(m->ledm_poly[1][3][3], 0.0, 36);
if (d->get_32_doubles(d, m->ledm_poly[1][4][0], boff + 0x15bc, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[1][4][1], boff + 0x164c, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[1][4][2], boff + 0x16dc, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
vect_set(m->ledm_poly[1][4][3], 0.0, 36);
if (d->get_32_doubles(d, m->ledm_poly[1][5][0], boff + 0x1700, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[1][5][1], boff + 0x1790, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[1][5][2], boff + 0x1820, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
vect_set(m->ledm_poly[1][5][3], 0.0, 36);
if (d->get_32_doubles(d, m->ledm_poly[1][6][0], boff + 0x1844, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[1][6][1], boff + 0x18d4, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[1][6][2], boff + 0x1964, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
vect_set(m->ledm_poly[1][6][3], 0.0, 36);
if (d->get_32_doubles(d, m->ledm_poly[1][7][0], boff + 0x1988, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[1][7][1], boff + 0x1a18, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[1][7][2], boff + 0x1aa8, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
vect_set(m->ledm_poly[1][7][3], 0.0, 36);
if (d->get_32_doubles(d, m->ledm_poly[1][1][0], boff + 0x1acc, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[1][1][1], boff + 0x1b5c, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[1][1][2], boff + 0x1bec, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[1][1][3], boff + 0x1c7c, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[1][0][0], boff + 0x1ca0, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[1][0][1], boff + 0x1d30, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[1][0][2], boff + 0x1dc0, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[1][0][3], boff + 0x1e50, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[0][2][0], boff + 0x1e74, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[0][2][1], boff + 0x1f04, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[0][2][2], boff + 0x1f94, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
vect_set(m->ledm_poly[0][2][3], 0.0, 36);
if (d->get_32_doubles(d, m->ledm_poly[0][3][0], boff + 0x1fb8, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[0][3][1], boff + 0x2048, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[0][3][2], boff + 0x20d8, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
vect_set(m->ledm_poly[0][3][3], 0.0, 36);
if (d->get_32_doubles(d, m->ledm_poly[0][4][0], boff + 0x20fc, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[0][4][1], boff + 0x218c, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[0][4][2], boff + 0x221c, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
vect_set(m->ledm_poly[0][4][3], 0.0, 36);
if (d->get_32_doubles(d, m->ledm_poly[0][5][0], boff + 0x2240, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[0][5][1], boff + 0x22d0, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[0][5][2], boff + 0x2360, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
vect_set(m->ledm_poly[0][5][3], 0.0, 36);
if (d->get_32_doubles(d, m->ledm_poly[0][6][0], boff + 0x2384, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[0][6][1], boff + 0x2414, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[0][6][2], boff + 0x24a4, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
vect_set(m->ledm_poly[0][6][3], 0.0, 36);
if (d->get_32_doubles(d, m->ledm_poly[0][7][0], boff + 0x24c8, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[0][7][1], boff + 0x2558, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[0][7][2], boff + 0x25e8, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
vect_set(m->ledm_poly[0][7][3], 0.0, 36);
if (d->get_32_doubles(d, m->ledm_poly[0][1][0], boff + 0x260c, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[0][1][1], boff + 0x269c, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[0][1][2], boff + 0x272c, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[0][1][3], boff + 0x27bc, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[0][0][0], boff + 0x27e0, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[0][0][1], boff + 0x2870, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ledm_poly[0][0][2], boff + 0x2900, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_u8_doubles(d, m->ledm_poly[0][0][3], boff + 0x2990, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles_padded(d, m->ee_m0_fwa, boff + 0x29b4, 18, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles_padded(d, m->ee_m1_fwa, boff + 0x29fc, 18, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles_padded(d, m->ee_m2_fwa, boff + 0x2a44, 18, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_suv_inttarg, boff + 0x2a8c, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_luv_inttarg, boff + 0x2a90, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee_sluv_bl, boff + 0x2a94, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles_padded(d, m->ee_fwa_cal, boff + 0x2a98, 18, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles_padded(d, m->ee_fwa_std, boff + 0x2ae0, 18, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ee_ref_nn_illum, boff + 0x2b28, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, m->ee_ref_uv_illum, boff + 0x2bb8, 36, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (m->ee_version >= 1) {
if (d->get_16_ints(d, &m->ee1_pol_led_luv_cur, boff + 0x2c48, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_16_ints(d, &m->ee1_pol_led_suv_cur, boff + 0x2c4a, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_doubles(d, &m->ee1_wltempcoef, boff + 0x2c4c, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
}
/* Check the CRC */
crc = d->get_crc(d);
a1logd(p->log,3,"i1pro3_parse_eeprom: block 3 crc = 0x%x, should be 0x%x - %s\n",
crc, m->ee_crc_3, crc == m->ee_crc_3 ? "OK": "BAD");
if (crc != m->ee_crc_3)
{ d->del(d); return I1PRO3_HW_EE_CHKSUM; }
/* Block 4 */
boff = m->ee_bk4_off;
d->init_crc(d);
if (d->get_32_ints(d, m->ee_unk27, boff + 0x0, 4, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, m->ee_unk28, boff + 0x10, 4, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk29, boff + 0x20, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk30, boff + 0x24, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, &m->ee_unk31, boff + 0x28, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
/* Block 5 */
boff = m->ee_bk5_off;
d->init_crc(d);
if (d->get_8_asciiz(d, m->ee_supplier, boff + 0x0, 16, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, m->ee_unk32, boff + 0x10, 4, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
if (d->get_32_ints(d, m->ee_unk33, boff + 0x20, 256, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
/* Block 6 */
boff = m->ee_bk6_off;
d->init_crc(d);
if (d->get_16_ints(d, &m->ee_unk34, boff + 0x0, 1, 1) == NULL)
{ d->del(d); return I1PRO3_HW_EE_RANGE; }
d->del(d); d = NULL;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Organize EEProm/calibration info: */
/* Convert stray light compensation from ints to floats */
m->straylight[0] = dmatrixz(0, 35, 0, 35);
for (i = 0; i < 36; i++) {
for (j = 0; j < 36; j++) {
m->straylight[0][i][j] = m->ee_straylight_scale * m->ee_straylight[i * 36 + j]/32767.0;
if (i == j)
m->straylight[0][i][j] += 1.0;
}
}
/* Because the reference illumination spectrum level */
/* is inversely proportional to the aperure area, */
/* we could heuristically estimate the aperture size:
double sum = 0.0, aperture;
for (i = 0; i < 36; i++)
sum += m->ee_ref_nn_illum[i];
aperture = sqrt(6.9e9/sum);
*/
if (m->ee_devtype == 0x20)
m->aperture = 1;
/* Copy (some) ee values to working values */
m->version = m->ee_version;
m->serno = m->ee_serno;
m->capabilities = m->ee_capabilities;
/* Set nature of spectrometer system */
m->nsen1 = 134; // emiss/non-mux: 6 extra samples over CCD
m->nsen2 = 282; // refl/muxed: 2 x 134 + 14 extra
m->nraw = 128; // CCD
m->nwav[0] = 36;
m->wl_width[0] = 10.0; /* Standard res. width */
m->wl_short[0] = 380.0; /* Standard res. range */
m->wl_long[0] = 730.0;
/* Fill high res in too */
m->wl_width[1] = HIGHRES_WIDTH;
m->wl_short[1] = HIGHRES_SHORT;
m->wl_long[1] = HIGHRES_LONG;
m->nwav[1] = (int)((m->wl_long[1]-m->wl_short[1])/HIGHRES_WIDTH + 0.5) + 1;
if (m->nsen2 > MX_NSEN)
error("Assert in %s at line %d nsen2 %d > MX_NSEN %d\n",__FILE__,__LINE__,m->nsen2,MX_NSEN);
if (m->nraw > MX_NRAW)
error("Assert in %s at line %d nraw %d > MX_NRAW %d\n",__FILE__,__LINE__,m->nraw,MX_NRAW);
if (m->nwav[1] > MX_NWAV)
error("Assert in %s at line %d nwav[1] %d > MX_NWAV %d\n",__FILE__,__LINE__,m->nwav[1],MX_NWAV);
/* Set standard res. references and create high res. */
m->white_ref[0] = m->ee_white_ref;
m->white_ref[1] = dvector(0, m->nwav[1]-1);
good_upsample(p, m->white_ref[1], m->white_ref[0], 1, "White ref");
m->emis_coef[0] = m->ee_emis_coef;
m->emis_coef[1] = dvector(0, m->nwav[1]-1);
good_upsample(p, m->emis_coef[1], m->emis_coef[0], 1, "Emis ref");
correct_emis_coef(p, m->emis_coef[1]); /* Hacky... */
m->amb_coef[0] = m->ee_amb_coef;
m->amb_coef[1] = dvector(0, m->nwav[1]-1);
good_upsample(p, m->amb_coef[1], m->amb_coef[0], 1, "Amb ref");
m->straylight[1] = dmatrixz(0, m->nwav[1]-1, 0, m->nwav[1]-1);
i1pro3_compute_hr_straylight(p);
m->m0_fwa[0] = m->ee_m0_fwa;
m->m0_fwa[1] = dvector(0, m->nwav[1]-1);
good_upsample(p, m->m0_fwa[1], m->m0_fwa[0], 1, "M0 FWA");
clear_low_wav(p, m->m0_fwa[1], 2, 1);
m->m1_fwa[0] = m->ee_m1_fwa;
m->m1_fwa[1] = dvector(0, m->nwav[1]-1);
fast_upsample(p, m->m1_fwa[1], m->m1_fwa[0], 1, "M1 FWA");
clear_low_wav(p, m->m1_fwa[1], 2, 1);
m->m2_fwa[0] = m->ee_m2_fwa;
m->m2_fwa[1] = dvector(0, m->nwav[1]-1);
fast_upsample(p, m->m2_fwa[1], m->m2_fwa[0], 1, "M2 FWA");
clear_low_wav(p, m->m2_fwa[1], 2, 1);
m->fwa_cal[0] = m->ee_fwa_cal;
m->fwa_cal[1] = dvector(0, m->nwav[1]-1);
fast_upsample(p, m->fwa_cal[1], m->fwa_cal[0], 1, "FWA cal");
m->fwa_std[0] = m->ee_fwa_std;
m->fwa_std[1] = dvector(0, m->nwav[1]-1);
fast_upsample(p, m->fwa_std[1], m->fwa_std[0], 1, "FWA std");
clear_low_wav(p, m->fwa_std[1], 1, 1);
/* Assumed minimum integration time */
/* minintclks = 165 */
/* intclkp = 1.5277777492883615e-5 */
/* min_int_time = 2.5208332863257965e-3 */
m->min_int_time = m->minintclks * m->intclkp;
/* This isn't set in the EEProm. intclk count is 32 bit, so not limited by HW. */
/* so pick a number... */
m->max_int_time = MAX_INT_TIME;
m->sens_sat = m->ee_sens_sat;
m->sens_target = 0.843 * m->sens_sat; /* (OEM has it hard coded as 45000) */
a1logd(p->log,2,"sens_target %d, sens_sat %d\n", m->sens_target, m->sens_sat);
/* Wavelength measurement parameters */
m->wl_cal_inttime = m->ee_wlcal_intt;
m->wl_cal_min_level = 500.0;
m->wl_cal_fwhm = m->ee_fwhm;
m->wl_cal_fwhm_tol = m->ee_fwhm_tol;
for (i = 0; i < 128; i++)
m->wl_led_spec[i] = (double)m->ee_wlcal_spec[i];
m->wl_err_max = m->ee_wlcal_max;
if (m->ee_version >= 1)
m->wl_tempcoef = m->ee1_wltempcoef; /* 0.0 if unknown */
m->wl_refpeakloc = 73; /* default - will be updated by i1pro3_match_wl_meas() */
m->wl_refpeakwl = 527.5; /* default - will be updated by i1pro3_match_wl_meas() */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Process, check and be verbose about the EEProm/calibration info */
a1logd(p->log,2,"Serial number = %u\n",m->ee_serno);
sprintf(m->sserno,"%u",m->ee_serno);
a1logd(p->log,2,"Supplier = '%s'\n",m->ee_supplier);
a1logd(p->log,2,"HW Id = %02x-%02x%02x%02x%02x%02x%02x%02x\n",
m->hw_chipid[0], m->hw_chipid[1], m->hw_chipid[2], m->hw_chipid[3],
m->hw_chipid[4], m->hw_chipid[5], m->hw_chipid[6], m->hw_chipid[7]);
a1logd(p->log,2,"EE Id = %02x-%02x%02x%02x%02x%02x%02x%02x\n",
m->ee_chipid[0], m->ee_chipid[1], m->ee_chipid[2], m->ee_chipid[3],
m->ee_chipid[4], m->ee_chipid[5], m->ee_chipid[6], m->ee_chipid[7]);
if (memcmp(m->hw_chipid, m->ee_chipid, 8) != 0) {
a1logd(p->log,1,"i1pro3_parse_eeprom: HW ChipId doesn't match EE ChipID\n");
return I1PRO3_HW_EE_CHIPID;
}
a1logd(p->log,2, "Date of manufacture = %d-%d-%d\n",
m->ee_dom1 % 100, (m->ee_dom1/100) % 100, m->ee_dom1/10000);
a1logd(p->log,2, "Non-linearity factors: %s\n",debPdvf(4, "%g", m->ee_lin));
#ifdef D_PLOT /* [und] Use plots to show EE info -D value is high enough */
# pragma message("######### i1pro3_imp.c D_PLOT defined !!!!! ########")
/* White tile, emissive/ambient calibrations */
if (p->log->debug >= 7) {
int i;
double xx[128];
double y1[128], y2[128];
printf("ee_white_ref - White tile (Black) and ee_uv_white_ref (Red)\n");
for (i = 0; i < 36; i++) {
xx[i] = XSPECT_WL(380.0, 730.0, 36, i);
y1[i] = m->ee_white_ref[i];
y2[i] = m->ee_uv_white_ref[i];
}
do_plot(xx, y1, y2, NULL, 36);
printf("ee_emis_coef (Black)\n");
for (i = 0; i < 36; i++) {
xx[i] = XSPECT_WL(380.0, 730.0, 36, i);
y1[i] = m->ee_emis_coef[i];
}
do_plot(xx, y1, NULL, NULL, 36);
printf("ee_amb_coef (Black)\n");
for (i = 0; i < 36; i++) {
xx[i] = XSPECT_WL(380.0, 730.0, 36, i);
y1[i] = m->ee_amb_coef[i];
}
do_plot(xx, y1, NULL, NULL, 36);
printf("ee_wlcal_spec (Black), ee_uv_wlcal_spec (red)\n");
for (i = 0; i < 128; i++) {
xx[i] = i;
y1[i] = m->ee_wlcal_spec[i];
y2[i] = m->ee_uv_wlcal_spec[i];
}
do_plot(xx, y1, y2, NULL, 128);
}
#endif
#ifdef D_STRAYPLOT /* Use plots to show EE info -D value is high enough */
/* Stray light values */
if (p->log->debug >= 7) {
int i, j;
double xx[36];
double *yy[36];
for (i = 0; i < 36; i++)
xx[i] = XSPECT_WL(380.0, 730.0, 36, i);
for (j = 0; j < 36; j += MXGPHS) {
double w1, w2;
int n = MXGPHS;
if ((j + n) > 36)
n = 36 - j;
w1 = XSPECT_WL(380.0, 730.0, 36, j);
w2 = XSPECT_WL(380.0, 730.0, 36, j + n -1);
for (i = 0; i < n; i++)
yy[i] = m->straylight[0][j+i];
for (; i < 36; i++)
yy[i] = NULL;
a1logd(p->log,7,"Stray Light matrix %f - %f: \n",w1, w2);
do_plotNpwz(xx, yy, 36, NULL, NULL, 0, 1, 0);
}
}
#else
/* Stray light values */
if (p->log->debug >= 9) {
int i;
for(i = 0; i < 36; i++) {
double sum = 0.0;
for (j = 0; j < 36; j++) {
sum += m->straylight[0][i][j];
a1logd(p->log,7," Wt %d = %f\n",j, m->straylight[0][i][j]);
}
a1logd(p->log,7," Sum = %f\n",sum);
}
}
#endif
#ifdef D_PLOT /* [und] Use plots to show EE info -D value is high enough */
/* raw to wav conversion graphs */
if (p->log->debug >= 7) {
int i;
double xx[128];
double y1[128], y2[128];
printf(" raw -> wl refl (Black) emis (Red)\n");
for (i = 0; i < 128; i++) {
xx[i] = i;
y1[i] = m->ee_wl_cal1[i];
y2[i] = m->ee_wl_cal2[i];
}
do_plot(xx, y1, y2, NULL, 128);
}
#endif
#ifdef D_PLOT /* [und] Use plots to show EE info -D value is high enough */
if (p->log->debug >= 7) {
int i;
double xx[36];
double y1[36], y2[36], y3[36];
printf("M0:m0_fwa (Black) M1:m1_fwa (Red) M2:m2_fwa (Green) FWA values\n");
for (i = 0; i < 18; i++) {
xx[i] = XSPECT_WL(380.0, 730.0, 36, i);
y1[i] = m->ee_m0_fwa[i];
y2[i] = m->ee_m1_fwa[i];
y3[i] = m->ee_m2_fwa[i];
}
do_plot(xx, y1, y2, y3, 18);
printf("ee_fwa_cal UV cal ?\n");
for (i = 0; i < 18; i++) {
xx[i] = XSPECT_WL(380.0, 730.0, 36, i); // ???
y1[i] = m->ee_fwa_cal[i];
}
do_plot(xx, y1, NULL, NULL, 18);
printf("ee_fwa_std FWA standard ?\n");
for (i = 0; i < 18; i++) {
xx[i] = XSPECT_WL(380.0, 730.0, 36, i);
y1[i] = m->ee_fwa_std[i];
}
do_plot(xx, y1, NULL, NULL, 18);
printf("ee_ref_nn_illum (Black) ee_ref_uv_illum (Red)\n");
for (i = 0; i < 36; i++) {
xx[i] = XSPECT_WL(380.0, 730.0, 36, i);
y1[i] = m->ee_ref_nn_illum[i];
y2[i] = m->ee_ref_uv_illum[i];
}
do_plot(xx, y1, y2, NULL, 36);
}
#endif
return inst_ok;
}
/* ============================================================ */
/* EEProm contents parsing support. */
/* Unlike the USB protocol, this is all big endian. */
/* Generate the crc-32-c lookup table */
static void init_crc32c_table(ORD32 *table) {
int ix, i;
ORD32 mask;
ORD32 val;
/* Note that 0x82f63b78 is the reverse of Poly 0x1edc6f41 */
for (ix = 0; ix < 256; ix++) {
for (val = 0, mask = 1; mask < 0x100; mask <<= 1) {
if ((ix & mask) != 0)
val ^= 1;
if (val & 1)
val = (val >> 1) ^ 0x82f63b78;
else
val >>= 1;
}
table[ix] = val;
}
}
/* Compute incremental CRC-32-C. */
/* For complete result start with 0xffffffff, and complement the final value */
static ORD32 comp_crc32c(ORD8 *buf, int len, ORD32 crc) {
static ORD32 table[256] = { 0 };
int i;
if (table[1] == 0)
init_crc32c_table(table);
for (i = 0; i < len; i++)
crc = (crc >> 8) ^ table[(crc ^ buf[i]) & 0xff];
return crc;
}
/* Return a pointer to an array of chars containing data from 8 bits. */
/* If rv is NULL, the returned value will have been allocated, othewise */
/* the rv will be returned. Return NULL if out of range. */
static unsigned char *i1data3_get_8_char(i1data3 *d, unsigned char *rv, int off, int count, int c) {
int i;
if (count <= 0
|| off < 0
|| (off + count * 1) > d->len)
return NULL;
if (c) {
d->crc = comp_crc32c(d->buf + off, 1 * count, d->crc);
}
if (rv == NULL) {
if ((rv = (unsigned char *)malloc(sizeof(int) * count)) == NULL)
return NULL;
}
for (i = 0; i < count; i++, off += 1) {
rv[i] = d->buf[off];
}
return rv;
}
/* Return a pointer to an nul terminated string containing data from 8 bits. */
/* If rv is NULL, the returned value will have been allocated, othewise */
/* the rv will be returned. Return NULL if out of range. */
/* An extra space and a nul terminator will be added to the eeprom data */
static char *i1data3_get_8_asciiz(i1data3 *d, char *rv, int off, int count, int c) {
int i;
if (count <= 0
|| off < 0
|| (off + count * 1) > d->len)
return NULL;
if (c) d->crc = comp_crc32c(d->buf + off, 1 * count, d->crc);
if (rv == NULL) {
if ((rv = (char *)malloc(sizeof(int) * (count + 1))) == NULL)
return NULL;
}
for (i = 0; i < count; i++, off += 1) {
rv[i] = (char)d->buf[off];
}
rv[i] = '\000';
return rv;
}
/* Return a pointer to an array of ints containing data from 8 bits. */
/* If rv is NULL, the returned value will have been allocated, othewise */
/* the rv will be returned. Return NULL if out of range. */
static int *i1data3_get_8_ints(i1data3 *d, int *rv, int off, int count, int c) {
int i;
if (count <= 0
|| off < 0
|| (off + count * 1) > d->len)
return NULL;
if (c) d->crc = comp_crc32c(d->buf + off, 1 * count, d->crc);
if (rv == NULL) {
if ((rv = (int *)malloc(sizeof(int) * count)) == NULL)
return NULL;
}
for (i = 0; i < count; i++, off += 1) {
rv[i] = ((signed char *)d->buf)[off];
}
return rv;
}
/* Return a pointer to an array of ints containing data from unsigned 8 bits. */
/* If rv is NULL, the returned value will have been allocated, othewise */
/* the rv will be returned. Return NULL if out of range. */
static int *i1data3_get_u8_ints(i1data3 *d, int *rv, int off, int count, int c) {
int i;
if (count <= 0
|| off < 0
|| (off + count * 1) > d->len)
return NULL;
if (c) d->crc = comp_crc32c(d->buf + off, 1 * count, d->crc);
if (rv == NULL) {
if ((rv = (int *)malloc(sizeof(int) * count)) == NULL)
return NULL;
}
for (i = 0; i < count; i++, off += 1) {
rv[i] = d->buf[off];
}
return rv;
}
/* Return a pointer to an array of ints containing data from 16 bits. */
/* If rv is NULL, the returned value will have been allocated, othewise */
/* the rv will be returned. Return NULL if out of range. */
static int *i1data3_get_16_ints(i1data3 *d, int *rv, int off, int count, int c) {
int i;
if (count <= 0
|| off < 0
|| (off + count * 2) > d->len)
return NULL;
if (c) d->crc = comp_crc32c(d->buf + off, 2 * count, d->crc);
if (rv == NULL) {
if ((rv = (int *)malloc(sizeof(int) * count)) == NULL)
return NULL;
}
for (i = 0; i < count; i++, off += 2) {
rv[i] = read_INR16_be(d->buf + off);
}
return rv;
}
/* Return a pointer to an array of ints containing data from unsigned 16 bits. */
/* If rv is NULL, the returned value will have been allocated, othewise */
/* the rv will be returned. Return NULL if out of range. */
static int *i1data3_get_u16_ints(i1data3 *d, int *rv, int off, int count, int c) {
int i;
if (count <= 0
|| off < 0
|| (off + count * 2) > d->len)
return NULL;
if (c) d->crc = comp_crc32c(d->buf + off, 2 * count, d->crc);
if (rv == NULL) {
if ((rv = (int *)malloc(sizeof(int) * count)) == NULL)
return NULL;
}
for (i = 0; i < count; i++, off += 2) {
rv[i] = read_ORD16_be(d->buf + off);
}
return rv;
}
/* Return a pointer to an array of ints containing data from 32 bits. */
/* If rv is NULL, the returned value will have been allocated, othewise */
/* the rv will be returned. Return NULL if out of range. */
static int *i1data3_get_32_ints(i1data3 *d, int *rv, int off, int count, int c) {
int i;
if (count <= 0
|| off < 0
|| (off + count * 4) > d->len)
return NULL;
if (c) d->crc = comp_crc32c(d->buf + off, 4 * count, d->crc);
if (rv == NULL) {
if ((rv = (int *)malloc(sizeof(int) * count)) == NULL)
return NULL;
}
for (i = 0; i < count; i++, off += 4) {
rv[i] = read_INR32_be(d->buf + off);
}
return rv;
}
/* Return a pointer to an array of unsigned ints containing data from unsigned 32 bits. */
/* If rv is NULL, the returned value will have been allocated, othewise */
/* the rv will be returned. Return NULL if out of range. */
static unsigned int *i1data3_get_u32_uints(i1data3 *d, unsigned int *rv, int off, int count, int c) {
int i;
if (count <= 0
|| off < 0
|| (off + count * 4) > d->len)
return NULL;
if (c) d->crc = comp_crc32c(d->buf + off, 4 * count, d->crc);
if (rv == NULL) {
if ((rv = (unsigned int *)malloc(sizeof(unsigned int) * count)) == NULL)
return NULL;
}
for (i = 0; i < count; i++, off += 4) {
rv[i] = read_ORD32_be(d->buf + off);
}
return rv;
}
/* Return a pointer to an array of doubles containing data from 32 bits. */
/* If rv is NULL, the returned value will have been allocated, othewise */
/* the rv will be returned. Return NULL if out of range or malloc failure. */
static double *i1data3_get_32_doubles(i1data3 *d, double *rv, int off, int count, int c) {
int i;
if (count <= 0
|| off < 0
|| (off + count * 4) > d->len)
return NULL;
if (c) d->crc = comp_crc32c(d->buf + off, 4 * count, d->crc);
if (rv == NULL) {
if ((rv = (double *)malloc(sizeof(double) * count)) == NULL)
return NULL;
}
for (i = 0; i < count; i++, off += 4) {
rv[i] = read_FLT32_be(d->buf + off);
}
return rv;
}
/* Return a pointer to an array of doubles containing data from 32 bits, */
/* with the array filled in reverse order. */
/* If rv is NULL, the returned value will have been allocated, othewise */
/* the rv will be returned. Return NULL if out of range or malloc failure. */
static double *i1data3_rget_32_doubles(i1data3 *d, double *rv, int off, int count, int c) {
int i;
if (count <= 0
|| off < 0
|| (off + count * 4) > d->len)
return NULL;
if (c) d->crc = comp_crc32c(d->buf + off, 4 * count, d->crc);
if (rv == NULL) {
if ((rv = (double *)malloc(sizeof(double) * count)) == NULL)
return NULL;
}
for (i = count-1; i >= 0; i--, off += 4) {
rv[i] = read_FLT32_be(d->buf + off);
}
return rv;
}
/* Return a pointer to an array of doubles containing data from 32 bits with zero padding */
static double *i1data3_get_32_doubles_padded(
i1data3 *d, double *rv, int off, int count, int retcount, int c) {
int i;
if (count <= 0
|| off < 0
|| (off + count * 4) > d->len)
return NULL;
if (c) d->crc = comp_crc32c(d->buf + off, 4 * count, d->crc);
if (rv == NULL) {
if ((rv = (double *)malloc(sizeof(double) * retcount)) == NULL)
return NULL;
}
for (i = 0; i < count; i++, off += 4) {
rv[i] = read_FLT32_be(d->buf + off);
}
for (; i < retcount; i++, off += 4) {
rv[i] = 0.0;
}
return rv;
}
/* Return a pointer to an array of doubles containing data from u8 bits/255.0. */
static double *i1data3_get_u8_doubles(i1data3 *d, double *rv, int off, int count, int c) {
int i;
if (count <= 0
|| off < 0
|| (off + count * 1) > d->len)
return NULL;
if (c) d->crc = comp_crc32c(d->buf + off, 1 * count, d->crc);
if (rv == NULL) {
if ((rv = (double *)malloc(sizeof(double) * count)) == NULL)
return NULL;
}
for (i = 0; i < count; i++, off += 1) {
rv[i] = d->buf[off]/255.0;
}
return rv;
}
/* Init the crc and checksum value */
void i1data3_init_crc(i1data3 *d) {
d->crc = 0xffffffff;
d->chsum = 0x0;
}
/* Return the crc value */
ORD32 i1data3_get_crc(i1data3 *d) {
return ~d->crc;
}
/* Return the checksum value */
ORD32 i1data3_get_chsum(i1data3 *d) {
return d->chsum;
}
/* Destroy ourselves */
static void i1data3_del(i1data3 *d) {
del_a1log(d->log); /* Unref it */
free(d);
}
/* Constructor for i1data3 */
i1data3 *new_i1data3(i1pro3 *p, unsigned char *buf, int len) {
i1data3 *d;
if ((d = (i1data3 *)calloc(1, sizeof(i1data3))) == NULL) {
a1loge(p->log, 1, "new_i1data3: malloc failed!\n");
return NULL;
}
d->p = p;
d->log = new_a1log_d(p->log); /* Take reference */
d->buf = buf;
d->len = len;
d->get_8_char = i1data3_get_8_char;
d->get_8_asciiz = i1data3_get_8_asciiz;
d->get_8_ints = i1data3_get_8_ints;
d->get_u8_ints = i1data3_get_u8_ints;
d->get_16_ints = i1data3_get_16_ints;
d->get_u16_ints = i1data3_get_u16_ints;
d->get_32_ints = i1data3_get_32_ints;
d->get_u32_uints = i1data3_get_u32_uints;
d->get_32_doubles = i1data3_get_32_doubles;
d->rget_32_doubles = i1data3_rget_32_doubles;
d->get_32_doubles_padded = i1data3_get_32_doubles_padded;
d->get_u8_doubles = i1data3_get_u8_doubles;
d->init_crc = i1data3_init_crc;
d->get_crc = i1data3_get_crc;
d->get_chsum = i1data3_get_chsum;
d->del = i1data3_del;
return d;
}
/* =======================================================================*/
/* Higher level measurement operations */
/* =======================================================================*/
/* Delay the given number of msec from last time the LEDs were */
/* turned off during reflective measurement */
void i1pro3_delay_llampoff(i1pro3 *p, unsigned int delay) {
i1pro3imp *m = (i1pro3imp *)p->m;
unsigned int timssinceoff;
if ((timssinceoff = (msec_time() - m->llamponoff)) < delay) {
a1logd(p->log,3,"i1pro3_delay_llampoff: sleep %d msec\n",delay - timssinceoff);
msec_sleep(delay - timssinceoff);
}
}
/* Take a wavelength reference measurement */
/* (Measure and subtracts black, linearizes) */
i1pro3_code i1pro3_wl_measure(
i1pro3 *p,
double *raw, /* Return array [nraw] of raw values */
double *temp /* Return the board temperature */
) {
i1pro3_code ev = I1PRO3_OK;
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1p3_adapter atype;
int nummeas;
double inttime;
double **raw_black, **raw_green;
double *raw_wlmeas, maxval;
nummeas = 1;
inttime = m->wl_cal_inttime; /* 0.1 secs */
a1logd(p->log,3,"i1pro3_wl_measure called\n");
if (m->capabilities & I1PRO3_CAP_HEAD_SENS) {
if ((ev = i1pro3_getadaptype(p, &atype)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_getadaptype failed\n");
return ev;
}
a1logd(p->log,4," adapter type = %d\n",atype);
/* Are we in correct state for calibration ? */
if (atype != i1p3_ad_cal
&& atype != i1p3_ad_m3cal) {
a1logd(p->log,1,"i1pro3_wl_measure: Need to be on calibration tile\n");
return I1PRO3_SPOS_CAL;
}
}
/* Read emissive black */
if ((ev = i1pro3_do_measure(p, i1p3mm_em, &raw_black, &nummeas, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black failed\n");
return ev;
}
a1logd(p->log,4," Got nummeas %d inttim %f from black\n",nummeas,inttime);
/* Read emissive wl with same params */
if ((ev = i1pro3_do_measure(p, i1p3mm_em_wl, &raw_green, &nummeas, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of green failed\n");
i1pro3_free_raw(p, i1p3mm_em, raw_black, nummeas);
}
a1logd(p->log,4," Got nummeas %d inttim %f from green\n",nummeas,inttime);
/* Compute black subtracted green */
vect_sub3(raw, raw_green[0], raw_black[0], m->nraw);
/* Check if green is saturated */
maxval = vect_max(raw, m->nraw);
if (maxval > m->sens_sat) {
a1logd(p->log,1," green measure %f is saturated\n",maxval);
i1pro3_free_raw(p, i1p3mm_em, raw_black, nummeas);
i1pro3_free_raw(p, i1p3mm_em_wl, raw_green, nummeas);
return I1PRO3_RD_SENSORSATURATED;
}
/* Check if we have sufficient signal */
if (maxval < m->wl_cal_min_level) {
a1logd(p->log,1," green measure %f is too small\n",maxval);
i1pro3_free_raw(p, i1p3mm_em, raw_black, nummeas);
i1pro3_free_raw(p, i1p3mm_em_wl, raw_green, nummeas);
return I1PRO3_WL_TOOLOW;
}
/* Linearize */
i1pro3_vect_lin(p, raw);
/* Record the board temperature we calibrated at */
if ((ev = i1pro3_getboardtemp(p, temp)) != I1PRO3_OK) {
i1pro3_free_raw(p, i1p3mm_em, raw_black, nummeas);
i1pro3_free_raw(p, i1p3mm_em_wl, raw_green, nummeas);
return ev;
}
#ifdef PLOT_DEBUG
printf(" raw WL, black subtr, linearized:\n");
plot_raw(raw);
#endif
i1pro3_free_raw(p, i1p3mm_em, raw_black, nummeas);
i1pro3_free_raw(p, i1p3mm_em_wl, raw_green, nummeas);
return ev;
}
/* Do reflectance calibration */
i1pro3_code i1pro3_refl_cal(
i1pro3 *p
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
int nummeasB, nummeasW;
double inttime;
double **raw_black1, **raw_black2, **raw_white;
double *avg_black;
double **wav_white;
double **wav_ledm;
double **wav_cor_ill;
double **wav_avg_white; /* Averaged, drift corrected */
double **wav_avg_sl_white; /* Averaged, drift corrected, possibly straylight */
double sum_wav_avg_sl_white;
double sum_ref_nn_illum;
double sum_diffvals;
int hr = 0;
a1logd(p->log,3,"i1pro3_refl_cal\n");
if (m->capabilities & I1PRO3_CAP_HEAD_SENS) {
i1p3_adapter atype;
if ((ev = i1pro3_getadaptype(p, &atype)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_getadaptype failed\n");
return ev;
}
a1logd(p->log,4," adapter type = %d\n",atype);
/* Are we in correct state for calibration ? */
if (atype != i1p3_ad_cal) {
a1logd(p->log,1,"Need to be on calibration tile\n");
return I1PRO3_SPOS_CAL;
}
}
/* Make sure LEDs have cooled down for 1 second */
i1pro3_delay_llampoff(p, LED_OFF_TIME);
/* Correct wl calibration for any temperature change */
if (m->ee_version >= 1) {
double temp;
if ((ev = i1pro3_getboardtemp(p, &temp)) != I1PRO3_OK) {
error(" i1pro3_getboardtemp failed\n");
return ev;
}
if ((ev = i1pro3_recompute_wav_filters_for_temp(p, temp)) != I1PRO3_OK) {
error(" i1pro3_recompute_wav_filters_for_temp failed\n");
return ev;
}
}
/* - - - - - - - - - - - - - - - - - - - - - */
a1logd(p->log,3," i1pro3_refl_cal part 1\n");
/* This first measurement is only used to establish a */
/* reference led model for latter measurements - calsp_ledm. */
/* The actual white measurement is not used for anything */
/* (except we use it for patch recognition processing purposes.) */
/* This whole thing could be thrown away and the next measurement */
/* used instead... */
inttime = s->inttime;
/* Read reflective black */
nummeasB = i1pro3_comp_nummeas(p, s->dcaltime, inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black1, &nummeasB, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black failed\n");
return ev;
}
/* Read reflective white using all LEDs */
nummeasW = i1pro3_comp_nummeas(p, s->wscaltime, inttime);
if ((ev =i1pro3_do_measure(p, i1p3mm_rf_wh, &raw_white, &nummeasW, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of white failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return ev;
}
a1logd(p->log,4," Got nummeas %d inttim %f from white\n",nummeasW,inttime);
/* Check black */
if (i1pro3_multimeas_check_black(p, raw_black1, nummeasB, inttime)) {
a1logd(p->log,1," black is too bright\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return I1PRO3_RD_DARKNOTVALID;
}
/* Average the black measurements together */
avg_black = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas(p, avg_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
/* Subtract black from white */
i1pro3_multimeas_sub_black(p, raw_white, nummeasW, avg_black);
free_dvector(avg_black, -1, m->nraw-1);
/* Check if white is saturated */
if (i1pro3_multimeas_check_sat(p, NULL, raw_white, nummeasW)) {
a1logd(p->log,1," white is saturated\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
return I1PRO3_RD_SENSORSATURATED;
}
/* Check if white is consistent */
if (i1pro3_multimeas_check_raw_consistency_x(p, raw_white, nummeasW)) {
a1logd(p->log,1," white is inconsistent\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
return I1PRO3_RD_WHITEREADINCONS;
}
/* Linearize white */
i1pro3_multimeas_lin(p, raw_white, nummeasW);
/* Save average odd & even raw white measure for patch recognition reflection calc. */
i1pro3_average_eorawmmeas(p, s->calraw_white, raw_white, nummeasW);
/* Separate the odd and even. */
/* Even will be 0..nummeasW/2, odd nummeasW/2 .. nummeasW */
if (i1pro3_unshuffle(p, raw_white, nummeasW))
return I1PRO3_INT_MALLOC;
/* Compute LED model for non-uv and uv patches */
wav_ledm = dmatrix(0, nummeasW-1, 0, 35);
i1pro3_comp_ledm(p, wav_ledm, raw_white, nummeasW/2, 0);
i1pro3_comp_ledm(p, wav_ledm + nummeasW/2, raw_white + nummeasW/2, nummeasW/2, 1);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
/* Compute average of led models */
i1pro3_average_wavmmeas(p, s->calsp_ledm[0], wav_ledm, nummeasW/2, 0);
i1pro3_average_wavmmeas(p, s->calsp_ledm[1], wav_ledm + nummeasW/2, nummeasW/2, 0);
free_dmatrix(wav_ledm, 0, nummeasW-1, 0, 35);
/* - - - - - - - - - - - - - - - - - - - - - */
a1logd(p->log,3," i1pro3_refl_cal part 2\n");
/* Read reflective black 1 */
nummeasB = i1pro3_comp_nummeas(p, s->dcaltime, inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black1, &nummeasB, &inttime, NULL, NULL))
!= I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black 1failed\n");
return ev;
}
/* Read reflective white using all LEDs */
nummeasW = i1pro3_comp_nummeas(p, s->wcaltime, inttime);
if ((ev =i1pro3_do_measure(p, i1p3mm_rf_wh, &raw_white, &nummeasW, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of white failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return ev;
}
/* Read reflective black 2 */
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black2, &nummeasB, &inttime, NULL, NULL))
!= I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black 2 failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
return ev;
}
/* Check blacks */
if (i1pro3_multimeas_check_black(p, raw_black1, nummeasB, inttime)
|| i1pro3_multimeas_check_black(p, raw_black2, nummeasB, inttime)) {
a1logd(p->log,1," black is too bright\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return I1PRO3_RD_DARKNOTVALID;
}
/* Average the blacks together */
avg_black = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas_2(p, avg_black, raw_black1, nummeasB, raw_black2, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black2, nummeasB);
/* Subtract black from white */
i1pro3_multimeas_sub_black(p, raw_white, nummeasW, avg_black);
free_dvector(avg_black, -1, m->nraw-1);
/* Check if white is saturated */
if (i1pro3_multimeas_check_sat(p, NULL, raw_white, nummeasW)) {
a1logd(p->log,1," white is saturated\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
return I1PRO3_RD_SENSORSATURATED;
}
/* Check if white is consistent */
if (i1pro3_multimeas_check_raw_consistency_x(p, raw_white, nummeasW)) {
a1logd(p->log,1," white is iconsistent\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
return I1PRO3_RD_WHITEREADINCONS;
}
/* Linearize white and normalize to integration time */
i1pro3_multimeas_lin(p, raw_white, nummeasW);
i1pro3_normalize_rawmmeas(p, raw_white, nummeasW, inttime);
/* Separate the odd and even. */
/* Even will be 0..nummeasW/2, odd nummeasW/2 .. nummeasW */
if (i1pro3_unshuffle(p, raw_white, nummeasW))
return I1PRO3_INT_MALLOC;
/* Compute LED model for non-uv and uv patches from first samples (?) */
// ~~~~ this doesn't seem correct. Shouldn't we compute the model for all
// ~~~~ and then average them ??
wav_ledm = dmatrix(0, 1, 0, MX_NWAV-1); /* Allow space for upsample */
i1pro3_comp_ledm(p, &wav_ledm[0], raw_white, 1, 0);
i1pro3_comp_ledm(p, &wav_ledm[1], raw_white + nummeasW/2, 1, 1);
/* Convert these into drift correction factors */
vect_div3_safe(wav_ledm[0], s->calsp_ledm[0], wav_ledm[0], 36);
vect_div3_safe(wav_ledm[1], s->calsp_ledm[1], wav_ledm[1], 36);
for (hr = 0; hr < 2; hr++) {
/* Convert from raw to wav nn & uv */
wav_white = dmatrix(-9, nummeasW-1, -9, m->nwav[hr]-1);
i1pro3_absraw_to_abswav(p, hr, 1, wav_white, raw_white, nummeasW);
wav_avg_white = dmatrix(0, 1, 0, m->nwav[hr]-1);
/* Compute average of white measurement */
i1pro3_average_wavmmeas(p, wav_avg_white[0], wav_white, nummeasW/2, hr);
i1pro3_average_wavmmeas(p, wav_avg_white[1], wav_white + nummeasW/2, nummeasW/2, hr);
free_dmatrix(wav_white, -9, nummeasW-1, -9, m->nwav[hr]-1);
if (hr) { /* Second time around upsample drift correction */
fast_upsample(p, wav_ledm[0], wav_ledm[0], 0, "nn drift corn");
fast_upsample(p, wav_ledm[1], wav_ledm[1], 0, "uv drift corn");
}
/* Apply LED drift correction to avg whites */
vect_mul(wav_avg_white[0], wav_ledm[0], m->nwav[hr]);
vect_mul(wav_avg_white[1], wav_ledm[1], m->nwav[hr]);
clear_low_wav(p, wav_avg_white[0], 4, hr);
/* Make a copy of avg_white and apply stray light to copy, and zero short wl's */
wav_avg_sl_white = dmatrix(0, 1, 0, m->nwav[hr]-1);
vect_cpy(wav_avg_sl_white[0], wav_avg_white[0], m->nwav[hr]);
vect_cpy(wav_avg_sl_white[1], wav_avg_white[1], m->nwav[hr]);
i1pro3_straylight(p, hr, wav_avg_sl_white, 2);
clear_low_wav(p, wav_avg_sl_white[0], 4, hr);
if (hr == 0) {
/* Correction factor from measured white illumination to reference measured */
/* illumination with straylight correction */
vect_div3(s->calsp_illcr[0][0], m->ee_ref_nn_illum, wav_avg_sl_white[0], 36);
vect_div3(s->calsp_illcr[0][1], m->ee_ref_uv_illum, wav_avg_sl_white[1], 36);
vect_set(s->calsp_illcr[0][0], 1.0, 4); /* Reduce upsample artefacts */
/* Create upsampled versions of illuminant correction */
fast_upsample(p, s->calsp_illcr[1][0], s->calsp_illcr[0][0], 0, "nn illum corn");
fast_upsample(p, s->calsp_illcr[1][1], s->calsp_illcr[0][1], 0, "uv illum corn");
/* Clear short wl's */
vect_set(s->calsp_illcr[0][0], 0.0, 4);
clear_low_wav(p, s->calsp_illcr[1][0], 4, 1);
}
wav_cor_ill = dmatrix(0, 1, 0, m->nwav[hr]-1);
/* Apply illumination correction to non-sl light corrected */
vect_mul3(wav_cor_ill[0], s->calsp_illcr[hr][0], wav_avg_white[0], m->nwav[hr]);
vect_mul3(wav_cor_ill[1], s->calsp_illcr[hr][1], wav_avg_white[1], m->nwav[hr]);
/* Stray light correct illum corrected and zero short wl's */
i1pro3_straylight(p, hr, wav_cor_ill, 2);
clear_low_wav(p, wav_cor_ill[0], 4, hr);
/* The use of the value wav_cor_ill[] is a bit doubtful! */
/* It's going to be extremely close to ee_ref_nn/uv_illum, */
/* and in fact is different by only 35 parts per million... */
/* white_tile/(nn + UV corrected absolute illumination) */
/* = 1.0/(2 x average LED illumination as if measured by sensor) */
/* (Note that hi-res is overridden below with upsample) */
vect_add3(s->iavg2aillum[hr], wav_cor_ill[0], wav_cor_ill[1], m->nwav[hr]);
vect_div3(s->iavg2aillum[hr], m->white_ref[hr], s->iavg2aillum[hr], m->nwav[hr]);
free_dmatrix(wav_cor_ill, 0, 1, 0, m->nwav[hr]-1);
/* Scale compute scale so that integral of it matches the nn reference illuminant */
sum_wav_avg_sl_white = vect_sum(wav_avg_sl_white[0], m->nwav[hr]);
sum_ref_nn_illum = vect_sum(m->ee_ref_nn_illum, 36);
if (hr) /* Allow for discrete integral difference.. */
sum_wav_avg_sl_white *= m->wl_width[1]/m->wl_width[0];
vect_scale(s->sc_calsp_nn_white[hr], wav_avg_sl_white[0],
sum_ref_nn_illum/sum_wav_avg_sl_white, m->nwav[hr]);
free_dmatrix(wav_avg_sl_white, 0, 1, 0, m->nwav[hr]-1);
free_dmatrix(wav_avg_white, 0, 1, 0, m->nwav[hr]-1);
}
free_dmatrix(wav_ledm, 0, 1, 0, MX_NWAV-1);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
/* - - - - - - - - - - - - - - - - - - - - - */
a1logd(p->log,3," i1pro3_refl_cal part 3\n");
/* Read reflective black 1 */
nummeasB = i1pro3_comp_nummeas(p, s->dcaltime, inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black1, &nummeasB, &inttime, NULL, NULL))
!= I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black 1failed\n");
return ev;
}
/* Read reflective white using all LEDs except short UV */
/* i.e. we're reading whit + long UV */
nummeasW = i1pro3_comp_nummeas(p, s->wcaltime, inttime);
if ((ev =i1pro3_do_measure(p, i1p3mm_rf_whs, &raw_white, &nummeasW, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of white failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return ev;
}
/* Read reflective black 2 */
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black2, &nummeasB, &inttime, NULL, NULL))
!= I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black 2 failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
return ev;
}
/* Check blacks */
if (i1pro3_multimeas_check_black(p, raw_black1, nummeasB, inttime)
|| i1pro3_multimeas_check_black(p, raw_black2, nummeasB, inttime)) {
a1logd(p->log,1," black is too bright\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return I1PRO3_RD_DARKNOTVALID;
}
/* Average the blacks together */
avg_black = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas_2(p, avg_black, raw_black1, nummeasB, raw_black2, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black2, nummeasB);
/* Subtract black from white */
i1pro3_multimeas_sub_black(p, raw_white, nummeasW, avg_black);
free_dvector(avg_black, -1, m->nraw-1);
/* Check if white is saturated */
if (i1pro3_multimeas_check_sat(p, NULL, raw_white, nummeasW)) {
a1logd(p->log,1," white is saturated\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
return I1PRO3_RD_SENSORSATURATED;
}
/* Check if white is consistent */
if (i1pro3_multimeas_check_raw_consistency_x(p, raw_white, nummeasW)) {
a1logd(p->log,1," white is iconsistent\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
return I1PRO3_RD_WHITEREADINCONS;
}
/* Linearize white and normalize */
i1pro3_multimeas_lin(p, raw_white, nummeasW);
i1pro3_normalize_rawmmeas(p, raw_white, nummeasW, inttime);
/* Separate the odd and even. */
/* Even will be 0..nummeasW/2, odd nummeasW/2 .. nummeasW */
if (i1pro3_unshuffle(p, raw_white, nummeasW))
return I1PRO3_INT_MALLOC;
/* Compute LED model for non-uv and uv patches from first samples (?) */
wav_ledm = dmatrix(0, 1, 0, MX_NWAV-1); /* Allow space for upsample */
i1pro3_comp_ledm(p, &wav_ledm[0], raw_white, 1, 0);
i1pro3_comp_ledm(p, &wav_ledm[1], raw_white + nummeasW/2, 1, 1);
/* Convert these into drift correction factors */
vect_div3_safe(wav_ledm[0], s->calsp_ledm[0], wav_ledm[0], 36);
vect_div3_safe(wav_ledm[1], s->calsp_ledm[1], wav_ledm[1], 36);
for (hr = 0; hr < 2; hr++) {
/* Convert from raw to wav nn & uv */
wav_white = dmatrix(-9, nummeasW-1, -9, m->nwav[hr]-1);
i1pro3_absraw_to_abswav(p, hr, 1, wav_white, raw_white, nummeasW);
/* Compute average of white measurement */
wav_avg_white = dmatrix(0, 1, 0, m->nwav[hr]-1);
i1pro3_average_wavmmeas(p, wav_avg_white[0], wav_white, nummeasW/2, hr);
i1pro3_average_wavmmeas(p, wav_avg_white[1], wav_white + nummeasW/2, nummeasW/2, hr);
free_dmatrix(wav_white, -9, nummeasW-1, -9, m->nwav[hr]-1);
if (hr) { /* Second time around upsample drift correction */
fast_upsample(p, wav_ledm[0], wav_ledm[0], 0, "nn drift corn");
fast_upsample(p, wav_ledm[1], wav_ledm[1], 0, "uv drift corn");
}
/* Apply LED drift correction to avg whites */
vect_mul(wav_avg_white[0], wav_ledm[0], m->nwav[hr]);
vect_mul(wav_avg_white[1], wav_ledm[1], m->nwav[hr]);
/* Apply stray light correction and clear nn short wl's */
i1pro3_straylight(p, hr, wav_avg_white, 2);
if (hr)
clear_low_wav2(p, wav_avg_white[0], 405.0, 415.0, hr);
else
vect_set(wav_avg_white[0], 0.0, 4);
/* Compute difference between uv and nn */
vect_sub3(s->cal_l_uv_diff[hr], wav_avg_white[1], wav_avg_white[0], m->nwav[hr]);
/* Clear all except short wavelength values */
if (hr)
clear_high_wav2(p, s->cal_l_uv_diff[hr], 485.0, 505.0, hr);
else
vect_set(s->cal_l_uv_diff[hr] + 12, 0.0, 36 - 12);
sum_diffvals = vect_sum(s->cal_l_uv_diff[hr], m->nwav[hr]);
if (hr) /* Allow for discrete integral difference.. */
sum_diffvals *= m->wl_width[1]/m->wl_width[0];
vect_scale1(s->cal_l_uv_diff[hr], m->ee_luv_inttarg/sum_diffvals, m->nwav[hr]);
free_dmatrix(wav_avg_white, 0, 1, 0, m->nwav[hr]-1);
}
free_dmatrix(wav_ledm, 0, 1, 0, MX_NWAV-1);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
/* - - - - - - - - - - - - - - - - - - - - - */
a1logd(p->log,3," i1pro3_refl_cal part 4\n");
/* Read reflective black 1 */
nummeasB = i1pro3_comp_nummeas(p, s->dcaltime, inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black1, &nummeasB, &inttime, NULL, NULL))
!= I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black 1failed\n");
return ev;
}
/* Read reflective white using all LEDs except long UV */
/* i.e. we're reading whit + short UV */
nummeasW = i1pro3_comp_nummeas(p, s->wcaltime, inttime);
if ((ev =i1pro3_do_measure(p, i1p3mm_rf_whl, &raw_white, &nummeasW, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of white failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return ev;
}
/* Read reflective black 2 */
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black2, &nummeasB, &inttime, NULL, NULL))
!= I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black 2 failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
return ev;
}
/* Check blacks */
if (i1pro3_multimeas_check_black(p, raw_black1, nummeasB, inttime)
|| i1pro3_multimeas_check_black(p, raw_black2, nummeasB, inttime)) {
a1logd(p->log,1," black is too bright\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return I1PRO3_RD_DARKNOTVALID;
}
/* Average the blacks together */
avg_black = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas_2(p, avg_black, raw_black1, nummeasB, raw_black2, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black2, nummeasB);
/* Subtract black from white */
i1pro3_multimeas_sub_black(p, raw_white, nummeasW, avg_black);
free_dvector(avg_black, -1, m->nraw-1);
/* Check if white is saturated */
if (i1pro3_multimeas_check_sat(p, NULL, raw_white, nummeasW)) {
a1logd(p->log,1," white is saturated\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
return I1PRO3_RD_SENSORSATURATED;
}
/* Check if white is consistent */
if (i1pro3_multimeas_check_raw_consistency_x(p, raw_white, nummeasW)) {
a1logd(p->log,1," white is iconsistent\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
return I1PRO3_RD_WHITEREADINCONS;
}
/* Linearize white and normalize */
i1pro3_multimeas_lin(p, raw_white, nummeasW);
i1pro3_normalize_rawmmeas(p, raw_white, nummeasW, inttime);
/* Separate the odd and even. */
/* Even will be 0..nummeasW/2, odd nummeasW/2 .. nummeasW */
if (i1pro3_unshuffle(p, raw_white, nummeasW))
return I1PRO3_INT_MALLOC;
/* Compute LED model for non-uv and uv patches from first samples (?) */
wav_ledm = dmatrix(0, 1, 0, MX_NWAV-1); /* Allow space for upsample */
i1pro3_comp_ledm(p, &wav_ledm[0], raw_white, 1, 0);
i1pro3_comp_ledm(p, &wav_ledm[1], raw_white + nummeasW/2, 1, 1);
/* Convert these into drift correction factors */
vect_div3_safe(wav_ledm[0], s->calsp_ledm[0], wav_ledm[0], 36);
vect_div3_safe(wav_ledm[1], s->calsp_ledm[1], wav_ledm[1], 36);
for (hr = 0; hr < 2; hr++) {
/* Convert from raw to wav nn & uv */
wav_white = dmatrix(-9, nummeasW-1, -9, m->nwav[hr]-1);
i1pro3_absraw_to_abswav(p, hr, 1, wav_white, raw_white, nummeasW);
/* Compute average of white measurement */
wav_avg_white = dmatrix(0, 1, 0, m->nwav[hr]-1);
i1pro3_average_wavmmeas(p, wav_avg_white[0], wav_white, nummeasW/2, hr);
i1pro3_average_wavmmeas(p, wav_avg_white[1], wav_white + nummeasW/2, nummeasW/2, hr);
free_dmatrix(wav_white, -9, nummeasW-1, -9, m->nwav[hr]-1);
if (hr) { /* Second time around upsample drift correction */
fast_upsample(p, wav_ledm[0], wav_ledm[0], 0, "nn drift corn");
fast_upsample(p, wav_ledm[1], wav_ledm[1], 0, "uv drift corn");
}
/* Apply LED drift correction to avg whites */
vect_mul(wav_avg_white[0], wav_ledm[0], m->nwav[hr]);
vect_mul(wav_avg_white[1], wav_ledm[1], m->nwav[hr]);
/* Apply stray light correction and clear nn short wl's */
i1pro3_straylight(p, hr, wav_avg_white, 2);
if (hr)
clear_low_wav2(p, wav_avg_white[0], 405.0, 415.0, hr);
else
vect_set(wav_avg_white[0], 0.0, 4);
/* Compute difference between uv and nn */
vect_sub3(s->cal_s_uv_diff[hr], wav_avg_white[1], wav_avg_white[0], m->nwav[hr]);
/* Clear all except short wavelength values */
if (hr)
clear_high_wav2(p, s->cal_s_uv_diff[hr], 485.0, 505.0, hr);
else
vect_set(s->cal_s_uv_diff[hr] + 12, 0.0, 36 - 12);
sum_diffvals = vect_sum(s->cal_s_uv_diff[hr], m->nwav[hr]);
if (hr) /* Allow for discrete integral difference.. */
sum_diffvals *= m->wl_width[1]/m->wl_width[0];
vect_scale1(s->cal_s_uv_diff[hr], m->ee_suv_inttarg/sum_diffvals, m->nwav[hr]);
free_dmatrix(wav_avg_white, 0, 1, 0, m->nwav[hr]-1);
}
free_dmatrix(wav_ledm, 0, 1, 0, MX_NWAV-1);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_white, nummeasW);
return ev;
}
/* Do a spot reflectance measurement */
i1pro3_code i1pro3_spot_refl_meas(
i1pro3 *p,
double **specrd, /* Cooked spectral patch value to return */
int hr /* 1 for high-res. */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
int nummeasB, nummeasW;
double inttime;
double **raw_black1, **raw_sample; /* raw measurements */
double *raw_avg_black; /* raw average black */
double **proc_sample; /* processed sample values */
double **eproc_sample; /* eproc_sample[enumsample][-9, nwav] */
int enummeas; /* Number of even samples */
double **oproc_sample; /* oproc_sample[onumsample][-9, nwav] */
int onummeas; /* Number of odd samples */
double *m0 = NULL, *m1 = NULL, *m2 = NULL;
int i;
a1logd(p->log,3,"i1pro3_spot_refl_meas\n");
if (m->capabilities & I1PRO3_CAP_HEAD_SENS) {
i1p3_adapter atype;
if ((ev = i1pro3_getadaptype(p, &atype)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_getadaptype failed\n");
return ev;
}
/* Do we have a reflection adapter fitted ? */
if ((atype & i1p3_ad_standard) == 0) {
a1logd(p->log,1,"Expect a standard measurement adapter\n");
return I1PRO3_SPOS_STD;
}
}
/* Make sure LEDs have cooled down for 1 second */
i1pro3_delay_llampoff(p, LED_OFF_TIME);
/* Correct wl calibration for any temperature change */
if (m->ee_version >= 1) {
double temp;
if ((ev = i1pro3_getboardtemp(p, &temp)) != I1PRO3_OK) {
error(" i1pro3_getboardtemp failed\n");
return ev;
}
if ((ev = i1pro3_recompute_wav_filters_for_temp(p, temp)) != I1PRO3_OK) {
error(" i1pro3_recompute_wav_filters_for_temp failed\n");
return ev;
}
}
inttime = s->inttime;
/* Read reflective black */
nummeasB = i1pro3_comp_nummeas(p, s->dreadtime, inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black1, &nummeasB, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black failed\n");
return ev;
}
/* Read reflective sample using all LEDs */
nummeasW = i1pro3_comp_nummeas(p, s->wreadtime, inttime);
if ((ev =i1pro3_do_measure(p, i1p3mm_rf_wh, &raw_sample, &nummeasW, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of sample failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return ev;
}
/* Don't check black for measurement */
/* Average the black together */
raw_avg_black = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas(p, raw_avg_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
//dump_dvector(stdout, "raw_avg_black", " ", raw_avg_black, 128);
//printf("Black:\n"); plot_raw(raw_avg_black);
/* Subtract black from sample */
i1pro3_multimeas_sub_black(p, raw_sample, nummeasW, raw_avg_black);
free_dvector(raw_avg_black, -1, m->nraw-1);
/* Check if sample is saturated */
if (i1pro3_multimeas_check_sat(p, NULL, raw_sample, nummeasW)) {
a1logd(p->log,1," sample is saturated\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_sample, nummeasW);
return I1PRO3_RD_SENSORSATURATED;
}
/* Check if sample is consistent */
if (i1pro3_multimeas_check_raw_consistency_x(p, raw_sample, nummeasW)) {
a1logd(p->log,1," sample is inconsistent\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_sample, nummeasW);
return I1PRO3_RD_READINCONS;
}
/* Linearize sample and normalize to inttime */
i1pro3_multimeas_lin(p, raw_sample, nummeasW);
i1pro3_normalize_rawmmeas(p, raw_sample, nummeasW, inttime);
/* Convert from raw to wav */
proc_sample = dmatrix(0, nummeasW-1, -9, m->nwav[hr]-1);
i1pro3_absraw_to_abswav(p, hr, 1, proc_sample, raw_sample, nummeasW);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_sample, nummeasW);
/* Split into odd and even wav samples */
/* (For scan with zebra this will be much more complex...) */
enummeas = nummeasW/2;
eproc_sample = dmatrix(0, enummeas, -9, m->nwav[hr]-1);
onummeas = nummeasW/2;
oproc_sample = dmatrix(0, onummeas, -9, m->nwav[hr]-1);
for (i = 0; i < nummeasW; i += 2) {
vect_cpy(eproc_sample[i/2]-9, proc_sample[i + 0]-9, 9+m->nwav[hr]);
vect_cpy(oproc_sample[i/2]-9, proc_sample[i + 1]-9, 9+m->nwav[hr]);
}
free_dmatrix(proc_sample, 0, nummeasW-1, -9, m->nwav[hr]-1);
if (m->filt == inst_opt_filter_none)
m0 = specrd[0];
else if (m->filt == inst_opt_filter_D50)
m1 = specrd[0];
else if (m->filt == inst_opt_filter_UVCut)
m2 = specrd[0];
else {
a1logd(p->log,1," wrong filter 0x%x\n",m->filt);
free_dmatrix(eproc_sample, 0, enummeas, -9, m->nwav[hr]-1);
free_dmatrix(oproc_sample, 0, onummeas, -9, m->nwav[hr]-1);
return ev;
}
/* Convert raw samples to patch reflective spectral values.*/
if ((ev = i1pro3_comp_refl_value(
p,
m0, m1, m2,
eproc_sample, enummeas,
oproc_sample, onummeas,
hr)) != I1PRO3_OK) {
a1logd(p->log,1," conversion to calibrated spectral failed\n");
free_dmatrix(eproc_sample, 0, enummeas, -9, m->nwav[hr]-1);
free_dmatrix(oproc_sample, 0, onummeas, -9, m->nwav[hr]-1);
return ev;
}
free_dmatrix(eproc_sample, 0, enummeas, -9, m->nwav[hr]-1);
free_dmatrix(oproc_sample, 0, onummeas, -9, m->nwav[hr]-1);
return ev;
}
/* Do a strip reflectance measurement */
i1pro3_code i1pro3_strip_refl_meas(
i1pro3 *p,
double **specrd, /* Cooked spectral patches values to return */
int nvals, /* Number of patches expected */
int hr /* 1 for high-res. */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
int nummeasB, nummeasW;
double inttime;
double **raw_black1, **raw_sample; /* raw measurements */
double **wav_sample; /* processed wav measurements */
double **ewav_sample; /* ewav_sample[enumsample][-9, nwav] */
double **owav_sample; /* owav_sample[onumsample][-9, nwav] */
int **p2m = NULL; /* zebra list of sample indexes in mm. order */
int npos = 0; /* Number of position slots */
double **filt_sample; /* filtered raw measurements */
int nfiltsamp; /* Number of filtered raw measurements */
double *raw_avg_black; /* raw average black */
i1pro3_patch *patch; /* List of patch locations within scan data */
double *m0 = NULL, *m1 = NULL, *m2 = NULL;
int i, j, k;
a1logd(p->log,3,"i1pro3_strip_refl_meas\n");
if (m->capabilities & I1PRO3_CAP_HEAD_SENS) {
i1p3_adapter atype;
if ((ev = i1pro3_getadaptype(p, &atype)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_getadaptype failed\n");
return ev;
}
/* Do we have a reflection adapter fitted ? */
if ((atype & i1p3_ad_standard) == 0) {
a1logd(p->log,1,"Expect a standard measurement adapter\n");
return I1PRO3_SPOS_STD;
}
}
/* Make sure LEDs have cooled down for 1 second */
i1pro3_delay_llampoff(p, LED_OFF_TIME);
inttime = s->inttime;
/* Correct wl calibration for any temperature change */
if (m->ee_version >= 1) {
double temp;
if ((ev = i1pro3_getboardtemp(p, &temp)) != I1PRO3_OK) {
error(" i1pro3_getboardtemp failed\n");
return ev;
}
if ((ev = i1pro3_recompute_wav_filters_for_temp(p, temp)) != I1PRO3_OK) {
error(" i1pro3_recompute_wav_filters_for_temp failed\n");
return ev;
}
}
/* Read reflective black */
nummeasB = i1pro3_comp_nummeas(p, s->dreadtime, inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black1, &nummeasB, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black failed\n");
return ev;
}
/* Read strip reflective sample using all LEDs. Read zebra ruler if possible too. */
nummeasW = 0;
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_wh, &raw_sample, &nummeasW, &inttime, &p2m, &npos)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of sample failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return ev;
}
a1logd(p->log,2," i1pro3_do_measure strip returned %d nummeas\n",nummeasW);
/* Don't check black for measurement */
/* Average the black together */
raw_avg_black = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas(p, raw_avg_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
//dump_dvector(stdout, "raw_avg_black", " ", raw_avg_black, 128);
//printf("Black:\n"); plot_raw(raw_avg_black);
/* Subtract black from samples */
i1pro3_multimeas_sub_black(p, raw_sample, nummeasW, raw_avg_black);
free_dvector(raw_avg_black, -1, m->nraw-1);
/* Check if samples are saturated */
if (i1pro3_multimeas_check_sat(p, NULL, raw_sample, nummeasW)) {
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_sample, nummeasW);
a1logd(p->log,1," sample is saturated\n");
return I1PRO3_RD_SENSORSATURATED;
}
/* Linearize samples */
i1pro3_multimeas_lin(p, raw_sample, nummeasW);
/* Create copy of samples to filter */
nfiltsamp = nummeasW;
filt_sample = dmatrix(0, nfiltsamp-1, -9, m->nraw-1);
copy_dmatrix(filt_sample, raw_sample, 0, nummeasW-1, -9, m->nraw-1);
/* convert to reflection and filter UV even/odd mux noise out */
/* for use in patch recognition. */
i1pro3_filter_uvmux(p, filt_sample, nfiltsamp, hr);
#undef PLOT_FILLINS
/* Convert time based patch recognition proxy into position based one */
if (p2m != NULL) {
double **pos_sample; /* Position based raw samples */
#ifdef PLOT_FILLINS
double **plot = dmatrixz(0, 2, 0, npos-1);
#endif
pos_sample = dmatrix(0, npos-1, 0, m->nraw-1);
/* Average samples values into position slots */
for (i = 0; i < npos; i++) {
vect_set(pos_sample[i], 0.0, m->nraw);
if (p2m[i][-1] > 0) {
for (j = 0; j < p2m[i][-1]; j++)
vect_add(pos_sample[i], filt_sample[p2m[i][j]], m->nraw);
vect_scale1(pos_sample[i], 1.0/(double)j, m->nraw);
}
}
#ifdef PLOT_FILLINS
for (i = 0; i < npos; i++) {
plot[0][i] = (double)i;
plot[1][i] = pos_sample[i][50];
}
#endif
/* Now interpolate any empty slots */
for (i = 0; i < npos; i++) {
double bf;
if (p2m[i][-1] != 0)
continue;
for (j = i-1; j >= 0; j--) /* Search for lower non-empty */
if (p2m[j][-1] != 0)
break;
for (k = i+1; k < npos; k++) /* Search for upper non-empty */
if (p2m[k][-1] != 0)
break;
if (j < 0) {
j = k;
bf = 0.5;
} else if (k >= npos) {
k = j;
bf = 0.5;
} else {
bf = (i - j) / (double)(k - j);
}
vect_blend(pos_sample[i], pos_sample[j], pos_sample[k], bf, m->nraw);
}
#ifdef PLOT_FILLINS
for (i = 0; i < npos; i++)
plot[2][i] = pos_sample[i][50];
printf("Fill-ins on band 50:\n");
do_plot(plot[0], plot[1], plot[2], NULL, npos);
#endif
/* Switch position proxy for time one */
free_dmatrix(filt_sample, 0, nfiltsamp-1, -9, m->nraw-1);
filt_sample = pos_sample;
nfiltsamp = npos;
}
/* Room for returned patch information */
if ((patch = malloc(sizeof(i1pro3_patch) * nvals)) == NULL) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free_dmatrix(filt_sample, 0, nfiltsamp-1, -9, m->nraw-1);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_sample, nummeasW); raw_sample = NULL;
a1logd(p->log,1,"i1pro3_strip_refl_meas malloc %ld bytes failed\n",sizeof(i1pro3_patch) * nvals);
return I1PRO3_INT_MALLOC;
}
/* Locate patch boundaries */
if ((ev = i1pro3_locate_patches(p, patch, nvals, filt_sample, nfiltsamp, p2m)) != I1PRO3_OK) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free(patch);
free_dmatrix(filt_sample, 0, nfiltsamp-1, -9, m->nraw-1);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_sample, nummeasW); raw_sample = NULL;
a1logd(p->log,1," i1pro3_do_measure of sample failed\n");
return ev;
}
#ifdef PATREC_DEBUG
for (i = 0; i < nvals; i++) {
int acount = patch[i].no;
if (p2m != NULL) { /* Count underlying samples in patch */
for (acount = k = 0; k < patch[i].no; k++)
acount += p2m[patch[i].ss + k][-1];
}
printf("patch %d: @ %d len %d (%d)\n",i,patch[i].ss,patch[i].no,acount);
}
#endif // NEVER
free_dmatrix(filt_sample, 0, nfiltsamp-1, -9, m->nraw-1);
/* Allocate array of pointers to even/odd samples of max possible length */
if ((ewav_sample = malloc(sizeof(double *) * (nummeasW/2+1))) == NULL) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free(patch);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_sample, nummeasW); raw_sample = NULL;
a1logd(p->log,1,"i1pro3_strip_refl_meas malloc %ld bytes failed\n",sizeof(double *) * (nummeasW/2+1));
return I1PRO3_INT_MALLOC;
}
if ((owav_sample = malloc(sizeof(double *) * (nummeasW/2+1))) == NULL) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free(ewav_sample);
free(patch);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_sample, nummeasW); raw_sample = NULL;
a1logd(p->log,1,"i1pro3_strip_refl_meas malloc %ld bytes failed\n",sizeof(double *) * (nummeasW/2+1));
return I1PRO3_INT_MALLOC;
}
/* Normalize raw samples */
i1pro3_normalize_rawmmeas(p, raw_sample, nummeasW, inttime);
/* Convert samples from raw to wav */
wav_sample = dmatrix(0, nummeasW-1, -9, m->nwav[hr]-1);
i1pro3_absraw_to_abswav(p, hr, 1, wav_sample, raw_sample, nummeasW);
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_sample, nummeasW); raw_sample = NULL;
/* Process each patch into spectral values */
for (i = 0; i < nvals; i++) {
int eix, enummeas; /* Number of even samples */
int oix, onummeas; /* Number of odd samples */
/* Split into odd and even wav samples */
if (p2m != NULL) {
/* Copy list of patches in p2m list */
for (eix = oix = j = 0; j < patch[i].no; j++) {
int *plist = p2m[patch[i].ss + j]; /* List of patches in this slot */
for (k = 0; k < plist[-1]; k++) {
int ix = plist[k];
if ((ix & 1) == 0)
ewav_sample[eix++] = wav_sample[ix];
else
owav_sample[oix++] = wav_sample[ix];
}
}
enummeas = eix;
onummeas = oix;
} else {
/* Count number of even/odd samples in patch */
enummeas = patch[i].no/2; /* Round down */
onummeas = patch[i].no/2;
if ((patch[i].no & 1) != 0) { /* Assign any odd sample */
if ((patch[i].ss & 1) != 0)
onummeas++;
else
enummeas++;
}
/* Copy even/odd samples pointers to wav values */
for (eix = oix = j = 0; j < patch[i].no; j++) {
if (((patch[i].ss + j) & 1) == 0)
ewav_sample[eix++] = wav_sample[j];
else
owav_sample[oix++] = wav_sample[j];
}
}
#ifdef NEVER /* Check we're not mixing up odd & even samples.. */
printf("%d Even samples:\n",enummeas);
plot_wav_N(m, hr, ewav_sample, enummeas);
printf("%d Odd samples:\n",onummeas);
plot_wav_N(m, hr, owav_sample, onummeas);
#endif // NEVER
/* Check if each patch is consistent */
if (i1pro3_multimeas_check_wav_consistency2(p, hr, inttime, ewav_sample, enummeas,
owav_sample, onummeas)) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free(ewav_sample);
free(owav_sample);
free(patch);
free_dmatrix(wav_sample, 0, nummeasW-1, -9, m->nwav[hr]-1);
a1logd(p->log,1," sample is inconsistent\n");
return I1PRO3_RD_READINCONS;
}
/* Convert raw samples to patch reflective spectral values.*/
if (m->filt == inst_opt_filter_none)
m0 = specrd[i];
else if (m->filt == inst_opt_filter_D50)
m1 = specrd[i];
else if (m->filt == inst_opt_filter_UVCut)
m2 = specrd[i];
else {
a1logd(p->log,1," wrong filter 0x%x\n",m->filt);
if (p2m != NULL) del_zebix_list(p2m, npos);
free(ewav_sample);
free(owav_sample);
free(patch);
free_dmatrix(wav_sample, 0, nummeasW-1, -9, m->nwav[hr]-1);
return ev;
}
if ((ev = i1pro3_comp_refl_value(
p,
m0, m1, m2,
ewav_sample, enummeas,
owav_sample, onummeas,
hr)) != I1PRO3_OK) {
a1logd(p->log,1," conversion to calibrated spectral failed\n");
if (p2m != NULL) del_zebix_list(p2m, npos);
free(ewav_sample);
free(owav_sample);
free(patch);
free_dmatrix(wav_sample, 0, nummeasW-1, -9, m->nwav[hr]-1);
return ev;
}
}
if (p2m != NULL)
del_zebix_list(p2m, npos);
free(ewav_sample);
free(owav_sample);
free(patch);
free_dmatrix(wav_sample, 0, nummeasW-1, -9, m->nwav[hr]-1);
return ev;
}
/* - - - - - - - - - - - - - - - - - - - */
/* Do polarized reflectance calibration */
i1pro3_code i1pro3_pol_refl_cal(
i1pro3 *p
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
int nummeasB, nummeasW;
double inttime;
double **raw_black1, **raw_white;
double *avg_black, *raw_avg_white;
#ifdef STRICT_IMITATE
double refillumsum[36]; /* Sum of nn & uv illumination references */
double nnweight[36]; /* nn ref illum. blend weighting */
double uvweight[36]; /* uv ref illum. blend weighting */
#endif
int hr;
int i;
a1logd(p->log,3,"i1pro3_pol_refl_cal\n");
if (m->capabilities & I1PRO3_CAP_HEAD_SENS) {
i1p3_adapter atype;
if ((ev = i1pro3_getadaptype(p, &atype)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_getadaptype failed\n");
return ev;
}
a1logd(p->log,4," adapter type = %d\n",atype);
/* Are we in correct state for polarized calibration ? */
if (atype != i1p3_ad_m3cal) {
a1logd(p->log,1,"Need polarizer and to be on calibration tile\n");
return I1PRO3_SPOS_POLCAL;
}
}
/* Make sure LEDs have cooled down for 1 second */
i1pro3_delay_llampoff(p, LED_OFF_TIME);
/* Correct wl calibration for any temperature change */
if (m->ee_version >= 1) {
double temp;
if ((ev = i1pro3_getboardtemp(p, &temp)) != I1PRO3_OK) {
error(" i1pro3_getboardtemp failed\n");
return ev;
}
if ((ev = i1pro3_recompute_wav_filters_for_temp(p, temp)) != I1PRO3_OK) {
error(" i1pro3_recompute_wav_filters_for_temp failed\n");
return ev;
}
}
/* Check the integration time is suitable for the levels returned in this mode */
{
double meastime;
double *avg_white;
double maxval;
int no_minints;
inttime = m->min_int_time;
meastime = 2 * 10 * m->min_int_time;
/* Read reflective black */
nummeasB = i1pro3_comp_nummeas(p, meastime, inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black1, &nummeasB, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black failed\n");
return ev;
}
/* Read reflective white using all LEDs but with pol UV levels */
nummeasW = i1pro3_comp_nummeas(p, meastime, inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_whp, &raw_white, &nummeasW, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of white failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return ev;
}
/* Check black */
if (i1pro3_multimeas_check_black(p, raw_black1, nummeasB, inttime)) {
a1logd(p->log,1," black is too bright\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return I1PRO3_RD_DARKNOTVALID;
}
/* Average the black measurements together */
avg_black = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas(p, avg_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
/* Average the white measurements together */
avg_white = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas(p, avg_white, raw_white, nummeasW);
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_white, nummeasW);
/* Subtract black from white */
vect_sub(avg_white, avg_black, m->nraw);
/* Highest raw value achieved */
maxval = vect_max(avg_white, m->nraw);
a1logd(p->log,5," i1pro3_pol_refl_cal: maxval %f target %f\n",
maxval, s->targoscale * m->sens_target);
free_dvector(avg_white, -1, m->nraw-1);
free_dvector(avg_black, -1, m->nraw-1);
/* Compute an integration time that achieves targoscale */
inttime = inttime * s->targoscale * m->sens_target/maxval;
/* Round up to even multiple of min_inttime. */
/* (Polarizing filter reduces sensitivity by about 5.9) */
no_minints = 2 * (int)ceil(0.5 * inttime/m->min_int_time);
a1logd(p->log,5," i1pro3_pol_refl_cal: raw no_minints %d\n",no_minints);
if (no_minints < 2)
no_minints = 2;
else if (no_minints > 10)
no_minints = 10;
inttime = no_minints * m->min_int_time;
a1logd(p->log,5," i1pro3_pol_refl_cal: no_minints %d inttime %f\n",no_minints,inttime);
s->inttime = inttime;
}
inttime = s->inttime;
/* Read reflective black */
nummeasB = i1pro3_comp_nummeas(p, s->dcaltime, inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black1, &nummeasB, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black failed\n");
return ev;
}
a1logd(p->log,4," Got nummeas %d inttime %f from black\n",nummeasB,inttime);
/* Read reflective white using all LEDs but with pol UV levels */
nummeasW = i1pro3_comp_nummeas(p, s->wcaltime, inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_whp, &raw_white, &nummeasW, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of white failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return ev;
}
a1logd(p->log,4," Got nummeas %d inttim %f from white\n",nummeasW,inttime);
/* Check black */
if (i1pro3_multimeas_check_black(p, raw_black1, nummeasB, inttime)) {
a1logd(p->log,1," black is too bright\n");
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_white, nummeasW);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return I1PRO3_RD_DARKNOTVALID;
}
/* Average the black measurements together */
avg_black = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas(p, avg_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
/* Subtract black from white */
i1pro3_multimeas_sub_black(p, raw_white, nummeasW, avg_black);
free_dvector(avg_black, -1, m->nraw-1);
/* Check if white is saturated */
if (i1pro3_multimeas_check_sat(p, NULL, raw_white, nummeasW)) {
a1logd(p->log,1," white is saturated\n");
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_white, nummeasW);
return I1PRO3_RD_SENSORSATURATED;
}
/* Check if white is consistent */
if (i1pro3_multimeas_check_raw_consistency_x(p, raw_white, nummeasW)) {
a1logd(p->log,1," white is inconsistent\n");
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_white, nummeasW);
return I1PRO3_RD_WHITEREADINCONS;
}
/* Linearize white */
i1pro3_multimeas_lin(p, raw_white, nummeasW);
/* Save average raw white measure for patch recognition reflection calc. */
raw_avg_white = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas(p, raw_avg_white, raw_white, nummeasW);
vect_cpy(s->pol_calraw_white, raw_avg_white, m->nraw);
free_dvector(raw_avg_white, -1, m->nraw-1);
#ifdef STRICT_IMITATE
/* Compute reference illumination blend weightings. Weights sum to 1.0, */
/* and are equal above 450nm, with full weight to the UV region in uvweight */
/* and zero weight in nnweight. */
vect_add3(refillumsum, m->ee_ref_nn_illum, m->ee_ref_uv_illum, 36);
vect_div3(nnweight, m->ee_ref_nn_illum, refillumsum, 36);
vect_div3(uvweight, m->ee_ref_uv_illum, refillumsum, 36);
#endif
//printf("nn and uv blend weightings:\n");
//plot_wav2(m, 0, nnweight, uvweight);
/* Accumulate average sum of led model illumination */
vect_set(s->pol_calsp_ledm, 0.0, 36);
for (i = 0; i < nummeasW; i++) {
double nn_ledm[36], *pnn_ledm[1] = { nn_ledm }; /* nn led model for this measurement */
double uv_ledm[36], *puv_ledm[1] = { uv_ledm }; /* uv led model for this measurement */
//double ouv_ledm[36];
/* Compute nn and uv models for the given paux data */
i1pro3_comp_ledm(p, pnn_ledm, raw_white + i, 1, 0);
i1pro3_comp_ledm(p, puv_ledm, raw_white + i, 1, 1);
//vect_cpy(ouv_ledm, uv_ledm, 36);
#ifdef STRICT_IMITATE
/* Scale them by the same reference blending ratio. */
/* Hmm. So we end up with the average of the models above 450 nm */
/* and the uv portion of the uv led model. */
/* This seems pointless because the result is almost */
/* identical to the uv_ledm, the only slight discrepancy */
/* being in the blend crossover region 410 - 440nm. It also doesn't */
/* seem to correspond to the reality that the effective illumination */
/* is the average of the mux'd nn and uv led spectrum. */
/* So we SHOULD actually use the mean of the nn and uv models */
vect_mul(uv_ledm, uvweight, 36);
vect_mul(nn_ledm, nnweight, 36);
/* This measurements led model */
vect_add(uv_ledm, nn_ledm, 36);
#else
/* Use (presumed) correct average of nn & uv as illum cal reference */
vect_blend(uv_ledm, uv_ledm, nn_ledm, 0.5, 36);
#endif
//printf("Computed this meas led model, uv_ledm\n");
//plot_wav(m, 0, uv_ledm);
/* Sum to calibration ledm reference */
vect_add(s->pol_calsp_ledm, uv_ledm, 36);
}
/* Complete average ledm calculations */
vect_scale1(s->pol_calsp_ledm, 1.0/nummeasW, 36);
//printf(" avg ledm:\n");
//plot_wav(m, 0, s->pol_calsp_ledm);
//dump_dvector_fmt(stdout, "pol_calsp_ledm", " ", s->pol_calsp_ledm, 36, "%g");
/* Compute white reference in std and hi-res */
for (hr = 0; hr < 2; hr++) {
double **wav_white;
/* Convert from raw to wav */
wav_white = dmatrix(-9, nummeasW-1, -9, m->nwav[hr]-1);
i1pro3_absraw_to_abswav(p, hr, 1, wav_white, raw_white, nummeasW);
/* Apply stray light compensation */
i1pro3_straylight(p, hr, wav_white, nummeasW);
/* Accumulate average sums of white mesured */
vect_set(s->pol_calsp_white[hr], 0.0, m->nwav[hr]);
for (i = 0; i < nummeasW; i++) {
/* Sum white */
vect_add(s->pol_calsp_white[hr], wav_white[i], m->nwav[hr]);
}
/* Complete average white calculations */
vect_scale1(s->pol_calsp_white[hr], 1.0/nummeasW, m->nwav[hr]);
//printf(" avg white hr %d:\n",hr);
//plot_wav(m, hr, s->pol_calsp_white[hr]);
//dump_dvector_fmt(stdout, "pol_calsp_white", " ", s->pol_calsp_white[hr], m->nwav[hr], "%g");
free_dmatrix(wav_white, -9, nummeasW-1, -9, m->nwav[hr]-1);
}
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_white, nummeasW);
return ev;
}
/* Do a polarized spot reflectance measurement */
i1pro3_code i1pro3_pol_spot_refl_meas(
i1pro3 *p,
double **specrd, /* Cooked spectral patch value to return */
int hr /* 1 for high-res. */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
int nummeasB, nummeasW;
double inttime;
double **raw_black1, **raw_sample; /* raw measurements */
double *raw_avg_black; /* raw average black */
double **proc_sample; /* processed sample values */
int i;
a1logd(p->log,3,"i1pro3_pol_spot_refl_meas\n");
if (m->capabilities & I1PRO3_CAP_HEAD_SENS) {
i1p3_adapter atype;
if ((ev = i1pro3_getadaptype(p, &atype)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_getadaptype failed\n");
return ev;
}
/* Do we have a polarized filter fitted ? */
if ((atype & i1p3_ad_m3) == 0) {
a1logd(p->log,1,"Expect a polarization filter\n");
return I1PRO3_SPOS_POL;
}
}
/* Make sure LEDs have cooled down for 1 second */
i1pro3_delay_llampoff(p, LED_OFF_TIME);
/* Correct wl calibration for any temperature change */
if (m->ee_version >= 1) {
double temp;
if ((ev = i1pro3_getboardtemp(p, &temp)) != I1PRO3_OK) {
error(" i1pro3_getboardtemp failed\n");
return ev;
}
if ((ev = i1pro3_recompute_wav_filters_for_temp(p, temp)) != I1PRO3_OK) {
error(" i1pro3_recompute_wav_filters_for_temp failed\n");
return ev;
}
}
inttime = s->inttime;
/* Read reflective black */
nummeasB = i1pro3_comp_nummeas(p, s->dreadtime, inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black1, &nummeasB, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black failed\n");
return ev;
}
/* Read reflective sample using all LEDs */
nummeasW = i1pro3_comp_nummeas(p, s->wreadtime, inttime);
if ((ev =i1pro3_do_measure(p, i1p3mm_rf_whp, &raw_sample, &nummeasW, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of sample failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return ev;
}
/* Don't check black for measurement */
/* Average the black together */
raw_avg_black = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas(p, raw_avg_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
/* Subtract black from sample */
i1pro3_multimeas_sub_black(p, raw_sample, nummeasW, raw_avg_black);
free_dvector(raw_avg_black, -1, m->nraw-1);
/* Check if sample is saturated */
if (i1pro3_multimeas_check_sat(p, NULL, raw_sample, nummeasW)) {
a1logd(p->log,1," sample is saturated\n");
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_sample, nummeasW);
return I1PRO3_RD_SENSORSATURATED;
}
/* Check if sample is consistent */
if (i1pro3_multimeas_check_raw_consistency_x(p, raw_sample, nummeasW)) {
a1logd(p->log,1," sample is inconsistent\n");
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_sample, nummeasW);
return I1PRO3_RD_READINCONS;
}
/* Linearize sample */
i1pro3_multimeas_lin(p, raw_sample, nummeasW);
/* Convert from raw to wav */
proc_sample = dmatrix(-9, nummeasW-1, -9, m->nwav[hr]-1);
i1pro3_absraw_to_abswav(p, hr, 1, proc_sample, raw_sample, nummeasW);
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_sample, nummeasW);
/* Convert to calibrated reflectance */
if ((ev = i1pro3_comp_pol_refl_value(p, specrd[0], proc_sample, nummeasW, hr)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_comp_pol_refl_value failed\n");
free_dmatrix(proc_sample, -9, nummeasW-1, -9, m->nwav[hr]-1);
return ev;
}
free_dmatrix(proc_sample, -9, nummeasW-1, -9, m->nwav[hr]-1);
return ev;
}
/* Do a polarized strip reflectance measurement */
i1pro3_code i1pro3_pol_strip_refl_meas(
i1pro3 *p,
double **specrd, /* Cooked spectral patches values to return */
int nvals, /* Number of patches expected */
int hr /* 1 for hr */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
int nummeasB, nummeasW;
double inttime;
double **raw_black1, **raw_sample; /* raw measurements */
double **wav_sample; /* processed wav measurements */
double **pwav_sample; /* pointer to a patches wav values */
int **p2m = NULL; /* zebra list of sample indexes in mm. order */
int npos = 0; /* Number of position slots */
double **filt_sample; /* filtered raw measurements */
int nfiltsamp; /* Number of filtered raw measurements */
double *raw_avg_black; /* raw average black */
i1pro3_patch *patch; /* List of patch locations within scan data */
double *m0 = NULL, *m1 = NULL, *m2 = NULL;
int i, j, k;
a1logd(p->log,3,"i1pro3_pol_strip_refl_meas\n");
if (m->capabilities & I1PRO3_CAP_HEAD_SENS) {
i1p3_adapter atype;
if ((ev = i1pro3_getadaptype(p, &atype)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_getadaptype failed\n");
return ev;
}
/* Do we have a polarized filter fitted ? */
if ((atype & i1p3_ad_m3) == 0) {
a1logd(p->log,1,"Expect a polarization filter\n");
return I1PRO3_SPOS_POL;
}
}
/* Make sure LEDs have cooled down for 1 second */
i1pro3_delay_llampoff(p, LED_OFF_TIME);
inttime = s->inttime;
/* Correct wl calibration for any temperature change */
if (m->ee_version >= 1) {
double temp;
if ((ev = i1pro3_getboardtemp(p, &temp)) != I1PRO3_OK) {
error(" i1pro3_getboardtemp failed\n");
return ev;
}
if ((ev = i1pro3_recompute_wav_filters_for_temp(p, temp)) != I1PRO3_OK) {
error(" i1pro3_recompute_wav_filters_for_temp failed\n");
return ev;
}
}
/* Read reflective black */
nummeasB = i1pro3_comp_nummeas(p, s->dreadtime, inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_black, &raw_black1, &nummeasB, &inttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of black failed\n");
return ev;
}
/* Read strip reflective sample using all LEDs. Read zebra ruler if possible too. */
nummeasW = 0;
if ((ev = i1pro3_do_measure(p, i1p3mm_rf_whp, &raw_sample, &nummeasW, &inttime, &p2m, &npos)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of sample failed\n");
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
return ev;
}
a1logd(p->log,2," i1pro3_do_measure strip returned %d nummeas\n",nummeasW);
/* Don't check black for measurement */
/* Average the black together */
raw_avg_black = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas(p, raw_avg_black, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_rf_black, raw_black1, nummeasB);
//dump_dvector(stdout, "raw_avg_black", " ", raw_avg_black, 128);
//printf("Black:\n"); plot_raw(raw_avg_black);
/* Subtract black from samples */
i1pro3_multimeas_sub_black(p, raw_sample, nummeasW, raw_avg_black);
free_dvector(raw_avg_black, -1, m->nraw-1);
/* Check if samples are saturated */
if (i1pro3_multimeas_check_sat(p, NULL, raw_sample, nummeasW)) {
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_sample, nummeasW);
a1logd(p->log,1," sample is saturated\n");
return I1PRO3_RD_SENSORSATURATED;
}
/* Linearize samples */
i1pro3_multimeas_lin(p, raw_sample, nummeasW);
/* Create copy of samples for patch recognition */
nfiltsamp = nummeasW;
filt_sample = dmatrix(0, nfiltsamp-1, -9, m->nraw-1);
copy_dmatrix(filt_sample, raw_sample, 0, nummeasW-1, -9, m->nraw-1);
/* Spectrally normalize recognition samples */
for (i = 0; i < nummeasW; i++)
vect_div3(filt_sample[i], raw_sample[i], s->pol_calraw_white, m->nraw);
#undef PLOT_FILLINS
/* Convert time based patch recognition proxy into position based one */
if (p2m != NULL) {
double **pos_sample; /* Position based raw samples */
#ifdef PLOT_FILLINS
double **plot = dmatrixz(0, 2, 0, npos-1);
#endif
pos_sample = dmatrix(0, npos-1, 0, m->nraw-1);
/* Average samples values into position slots */
for (i = 0; i < npos; i++) {
vect_set(pos_sample[i], 0.0, m->nraw);
if (p2m[i][-1] > 0) {
for (j = 0; j < p2m[i][-1]; j++)
vect_add(pos_sample[i], filt_sample[p2m[i][j]], m->nraw);
vect_scale1(pos_sample[i], 1.0/(double)j, m->nraw);
}
}
#ifdef PLOT_FILLINS
for (i = 0; i < npos; i++) {
plot[0][i] = (double)i;
plot[1][i] = pos_sample[i][50];
}
#endif
/* Now interpolate any empty slots */
for (i = 0; i < npos; i++) {
double bf;
if (p2m[i][-1] != 0)
continue;
for (j = i-1; j >= 0; j--) /* Search for lower non-empty */
if (p2m[j][-1] != 0)
break;
for (k = i+1; k < npos; k++) /* Search for upper non-empty */
if (p2m[k][-1] != 0)
break;
if (j < 0) {
j = k;
bf = 0.5;
} else if (k >= npos) {
k = j;
bf = 0.5;
} else {
bf = (i - j) / (double)(k - j);
}
vect_blend(pos_sample[i], pos_sample[j], pos_sample[k], bf, m->nraw);
}
#ifdef PLOT_FILLINS
for (i = 0; i < npos; i++)
plot[2][i] = pos_sample[i][50];
printf("Fill-ins on band 50:\n");
do_plot(plot[0], plot[1], plot[2], NULL, npos);
#endif
/* Switch position proxy for time one */
free_dmatrix(filt_sample, 0, nfiltsamp-1, -9, m->nraw-1);
filt_sample = pos_sample;
nfiltsamp = npos;
}
/* Room for returned patch information */
if ((patch = malloc(sizeof(i1pro3_patch) * nvals)) == NULL) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free_dmatrix(filt_sample, 0, nfiltsamp-1, -9, m->nraw-1);
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_sample, nummeasW); raw_sample = NULL;
a1logd(p->log,1,"i1pro3_pol_strip_refl_meas malloc %ld bytes failed\n",sizeof(i1pro3_patch) * nvals);
return I1PRO3_INT_MALLOC;
}
/* Locate patch boundaries */
if ((ev = i1pro3_locate_patches(p, patch, nvals, filt_sample, nfiltsamp, p2m)) != I1PRO3_OK) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free(patch);
free_dmatrix(filt_sample, 0, nfiltsamp-1, -9, m->nraw-1);
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_sample, nummeasW); raw_sample = NULL;
a1logd(p->log,1," i1pro3_do_measure of sample failed\n");
return ev;
}
#ifdef PATREC_DEBUG
for (i = 0; i < nvals; i++) {
int nosamp = patch[i].no;
if (p2m != NULL) { /* Count underlying samples in patch */
for (nosamp = k = 0; k < patch[i].no; k++)
nosamp += p2m[patch[i].ss + k][-1];
}
printf("patch %d: @ %d len %d (%d)\n",i,patch[i].ss,patch[i].no,nosamp);
}
#endif // NEVER
free_dmatrix(filt_sample, 0, nfiltsamp-1, -9, m->nraw-1);
/* Allocate array of pointers to samples of max possible length */
if ((pwav_sample = malloc(sizeof(double *) * nummeasW)) == NULL) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free(patch);
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_sample, nummeasW); raw_sample = NULL;
a1logd(p->log,1,"i1pro3_pol_strip_refl_meas malloc %ld bytes failed\n",sizeof(double *) * (nummeasW/2+1));
return I1PRO3_INT_MALLOC;
}
/* Convert samples from raw to wav */
wav_sample = dmatrix(0, nummeasW-1, -9, m->nwav[hr]-1);
i1pro3_absraw_to_abswav(p, hr, 1, wav_sample, raw_sample, nummeasW);
i1pro3_free_raw(p, i1p3mm_rf_whp, raw_sample, nummeasW); raw_sample = NULL;
/* Process each patch into spectral values */
for (i = 0; i < nvals; i++) {
int nummeas; /* Number of samples */
/* Split into odd and even wav samples */
if (p2m != NULL) {
/* Copy list of patches in p2m list */
for (nummeas = j = 0; j < patch[i].no; j++) {
int *plist = p2m[patch[i].ss + j]; /* List of patches in this slot */
for (k = 0; k < plist[-1]; k++, nummeas++)
pwav_sample[nummeas] = wav_sample[plist[k]];
}
} else {
/* Copy samples pointers to wav values */
for (nummeas = j = 0; j < patch[i].no; j++, nummeas++)
pwav_sample[nummeas] = wav_sample[j];
}
/* Check if each patch is consistent */
if (i1pro3_multimeas_check_wav_consistency(p, hr, inttime, pwav_sample, nummeas)) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free(pwav_sample);
free(patch);
free_dmatrix(wav_sample, 0, nummeasW-1, -9, m->nwav[hr]-1);
a1logd(p->log,1," sample is inconsistent\n");
return I1PRO3_RD_READINCONS;
}
/* Convert raw samples to patch reflective spectral values.*/
if ((ev = i1pro3_comp_pol_refl_value(p, specrd[i], pwav_sample, nummeas, hr)) != I1PRO3_OK) {
a1logd(p->log,1," conversion to calibrated spectral failed\n");
if (p2m != NULL) del_zebix_list(p2m, npos);
free(pwav_sample);
free(patch);
free_dmatrix(wav_sample, 0, nummeasW-1, -9, m->nwav[hr]-1);
return ev;
}
}
if (p2m != NULL)
del_zebix_list(p2m, npos);
free(pwav_sample);
free(patch);
free_dmatrix(wav_sample, 0, nummeasW-1, -9, m->nwav[hr]-1);
return ev;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Do an adaptive emissive/ambient/transmissive calibration */
i1pro3_code i1pro3_adapt_emis_cal(
i1pro3 *p,
double *btemp /* Return the board temperature */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
double inttime1, inttime2;
int nummeas1, nummeas2;
double **raw_black1, **raw_black2, **raw_black3; /* raw measurements */
double *avg_black1, *avg_black2; /* raw average black */
a1logd(p->log,3,"i1pro3_adapt_emis_cal\n");
if (m->capabilities & I1PRO3_CAP_HEAD_SENS) {
i1p3_adapter atype;
if ((ev = i1pro3_getadaptype(p, &atype)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_getadaptype failed\n");
return ev;
}
a1logd(p->log,4," adapter type = %d\n",atype);
/* Are we in correct state for calibration ? */
if (atype != i1p3_ad_cal) {
a1logd(p->log,1,"Need to be on calibration tile\n");
return I1PRO3_SPOS_CAL;
}
}
if ((ev = i1pro3_getboardtemp(p, btemp)) != I1PRO3_OK) {
error(" i1pro3_getboardtemp failed\n");
return ev;
}
/* Correct wl calibration for any temperature change */
if (m->ee_version >= 1) {
if ((ev = i1pro3_recompute_wav_filters_for_temp(p, *btemp)) != I1PRO3_OK) {
error(" i1pro3_recompute_wav_filters_for_temp failed\n");
return ev;
}
}
/* Read short reflective black */
{
inttime1 = s->idark_int_time[0];
nummeas1 = i1pro3_comp_nummeas(p, s->dcaltime, inttime1);
}
a1logd(p->log,2,"\nDoing adaptive interpolated black calibration, nummeas %d of int_time %f\n", nummeas1, s->idark_int_time[0]);
if ((ev = i1pro3_do_measure(p, i1p3mm_em, &raw_black1, &nummeas1, &inttime1, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure of short adaptive black failed\n");
return ev;
}
/* Read long reflective black */
{
inttime2 = s->idark_int_time[1];
nummeas2 = i1pro3_comp_nummeas(p, s->dlcaltime, inttime2);
}
a1logd(p->log,2,"\nDoing adaptive interpolated black calibration %d of int_time %f\n", nummeas2, s->idark_int_time[1]);
if ((ev = i1pro3_do_measure(p, i1p3mm_em, &raw_black2, &nummeas2, &inttime2, NULL, NULL)) != I1PRO3_OK) {
i1pro3_free_raw(p, i1p3mm_em, raw_black1, nummeas1);
a1logd(p->log,1," i1pro3_do_measure of long adaptive black failed\n");
return ev;
}
/* Read short reflective black again */
{
a1logd(p->log,2,"\nDoing adaptive interpolated black calibration, nummeas %d of int_time %f\n", nummeas1, s->idark_int_time[0]);
if ((ev = i1pro3_do_measure(p, i1p3mm_em, &raw_black3, &nummeas1, &inttime1, NULL, NULL)) != I1PRO3_OK) {
i1pro3_free_raw(p, i1p3mm_em, raw_black1, nummeas1);
i1pro3_free_raw(p, i1p3mm_em, raw_black2, nummeas2);
a1logd(p->log,1," i1pro3_do_measure of short adaptive 2 black failed\n");
return ev;
}
}
/* Check black */
if (i1pro3_multimeas_check_black(p, raw_black1, nummeas1, inttime1)
|| i1pro3_multimeas_check_black(p, raw_black2, nummeas2, inttime2)
|| i1pro3_multimeas_check_black(p, raw_black3, nummeas1, inttime1)) {
a1logd(p->log,1," black is too bright\n");
i1pro3_free_raw(p, i1p3mm_em, raw_black1, nummeas1);
i1pro3_free_raw(p, i1p3mm_em, raw_black2, nummeas2);
if (raw_black1 != raw_black3) i1pro3_free_raw(p, i1p3mm_em, raw_black3, nummeas1);
return I1PRO3_RD_DARKNOTVALID;
}
/* Average the black measurements together */
avg_black1 = dvector(-1, m->nraw-1);
avg_black2 = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas_2(p, avg_black1, raw_black1, nummeas1, raw_black3, nummeas1);
i1pro3_average_rawmmeas(p, avg_black2, raw_black2, nummeas2);
i1pro3_free_raw(p, i1p3mm_em, raw_black1, nummeas1);
i1pro3_free_raw(p, i1p3mm_em, raw_black2, nummeas2);
if (raw_black1 != raw_black3) i1pro3_free_raw(p, i1p3mm_em, raw_black3, nummeas1);
/* Compute black ref dynamic value */
vect_sub3(s->idark_data[1]-1, avg_black2-1, avg_black1-1, m->nraw+1);
vect_scale1(s->idark_data[1]-1, 1.0/(inttime2 - inttime1), m->nraw+1);
/* Compute black ref static value */
vect_scale(s->idark_data[0]-1, s->idark_data[1]-1, inttime1, m->nraw+1);
vect_sub3(s->idark_data[0]-1, avg_black1-1, s->idark_data[0]-1, m->nraw+1);
free_dvector(avg_black1, -1, m->nraw-1);
free_dvector(avg_black2, -1, m->nraw-1);
return ev;
}
/* Do a tranmissive white calibration */
/* Return I1PRO3_CAL_TRANSWHITEWARN if any of the transmission wavelengths are low. */
i1pro3_code i1pro3_trans_cal(
i1pro3 *p
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
double btemp;
double **raw_white;
double *raw_avg_white;
int nummeas;
int hr;
a1logd(p->log,3,"i1pro3_trans_cal\n");
if (m->capabilities & I1PRO3_CAP_HEAD_SENS) {
i1p3_adapter atype;
if ((ev = i1pro3_getadaptype(p, &atype)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_getadaptype failed\n");
return ev;
}
a1logd(p->log,4," adapter type = %d\n",atype);
/* Are we in correct state for calibration ? */
if ((atype & i1p3_ad_standard) == 0) {
a1logd(p->log,1,"Expect a standard measurement adapter\n");
return I1PRO3_SPOS_CAL;
}
}
if ((ev = i1pro3_getboardtemp(p, &btemp)) != I1PRO3_OK) {
error(" i1pro3_getboardtemp failed\n");
return ev;
}
/* Correct wl calibration for any temperature change */
if (m->ee_version >= 1) {
if ((ev = i1pro3_recompute_wav_filters_for_temp(p, btemp)) != I1PRO3_OK) {
error(" i1pro3_recompute_wav_filters_for_temp failed\n");
return ev;
}
}
/* Do all the hard work here... */
if ((ev = i1pro3_spot_adapt_emis_raw_meas(p, &raw_white, &nummeas)) != I1PRO3_OK) {
return ev;
}
/* Save raw white for pattern recognition normalization */
raw_avg_white = dvector(-1, m->nraw-1);
i1pro3_average_rawmmeas(p, raw_avg_white, raw_white, nummeas);
vect_cpy(s->raw_white, raw_avg_white, m->nraw);
free_dvector(raw_avg_white, -1, m->nraw-1);
/* Do std res. and high res. */
for (hr = 0; hr < 2; hr++) {
double avgwh;
double **proc_sample;
double wav[MX_NWAV];
int j;
/* Convert from raw to wav, apply straylight and average */
proc_sample = dmatrix(0, nummeas-1, -1, m->nwav[hr]-1);
i1pro3_absraw_to_abswav(p, hr, 0, proc_sample, raw_white, nummeas);
i1pro3_straylight(p, hr, proc_sample, nummeas);
i1pro3_average_wavmmeas(p, wav, proc_sample, nummeas, hr);
avgwh = vect_avg(wav, m->nwav[hr]);
if (avgwh < 5000.0) {
free_dmatrix(proc_sample, 0, nummeas-1, -1, m->nwav[hr]-1);
i1pro3_free_raw(p, i1p3mm_em, raw_white, nummeas);
return I1PRO3_RD_TRANSWHITELEVEL;
}
/* Check white and safely invert */
for (j = 0; j < m->nwav[hr]; j++) {
/* If reference is < 0.4% of average */
if (wav[j]/avgwh < 0.004) {
s->cal_factor[hr][j] = 1.0/(0.004 * avgwh);
ev = I1PRO3_CAL_TRANSWHITEWARN;
} else {
s->cal_factor[hr][j] = 1.0/wav[j];
}
}
//if (ev == I1PRO3_CAL_TRANSWHITEWARN) plot_wav(m, hr, wav);
free_dmatrix(proc_sample, 0, nummeas-1, -1, m->nwav[hr]-1);
}
i1pro3_free_raw(p, i1p3mm_em, raw_white, nummeas);
return ev;
}
/* Do an adaptive spot emission/ambient/transmissive measurement */
i1pro3_code i1pro3_spot_adapt_emis_meas(
i1pro3 *p,
double **specrd /* Cooked spectral patch value to return */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
int nummeas;
double **raw_sample; /* raw measurements */
double **proc_sample; /* processed sample values */
int hr = m->highres; /* High res. */
int i;
if (m->capabilities & I1PRO3_CAP_HEAD_SENS) {
i1p3_adapter atype;
if ((ev = i1pro3_getadaptype(p, &atype)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_getadaptype failed\n");
return ev;
}
if (s->ambient) {
/* Do we have ambient adapter fitted ? */
if (atype != i1p3_ad_ambient) {
a1logd(p->log,1,"Expect ambient measurement adapter\n");
return I1PRO3_SPOS_AMB;
}
} else { /* Emision or Transmission */
/* Do we have normal adapter fitted ? */
if ((atype & i1p3_ad_standard) == 0) {
a1logd(p->log,1,"Expect an standard measurement adapter\n");
return I1PRO3_SPOS_STD;
}
}
}
/* Do all the hard work here, so that it is reusable. */
if ((ev = i1pro3_spot_adapt_emis_raw_meas(p, &raw_sample, &nummeas)) != I1PRO3_OK) {
return ev;
}
/* Convert from raw to wav, apply straylight and average */
proc_sample = dmatrix(0, nummeas-1, -1, m->nwav[hr]-1);
i1pro3_absraw_to_abswav(p, hr, 0, proc_sample, raw_sample, nummeas);
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeas);
i1pro3_straylight(p, hr, proc_sample, nummeas);
i1pro3_average_wavmmeas(p, specrd[0], proc_sample, nummeas, hr);
/* Scale by calibration factor */
vect_mul(specrd[0], s->cal_factor[hr], m->nwav[hr]);
free_dmatrix(proc_sample, 0, nummeas-1, -1, m->nwav[hr]-1);
return ev;
}
/* Do scan emission/ambient/transmissive measurement */
/* Emissive scan is assumed to be a patch strip. */
/* Transmissive scan is assumed to be a patch strip. */
/* Ambient scan is assumed to be a flash. */
i1pro3_code i1pro3_scan_emis_meas(
i1pro3 *p,
double *duration, /* If flash, return duration */
double **specrd, /* Cooked spectral patch values to return */
int nvals /* Number of patches expected */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
double btemp;
double binttime, inttime;
int nummeasB, nummeasW;
double **raw_black1, **raw_black2, **raw_sample; /* raw measurements */
double black[MX_NRAW], _psample[MX_NRAW+1], *psample = _psample + 1;
double maxval;
int **p2m = NULL; /* zebra list of sample indexes in mm. order */
int npos = 0; /* Number of position slots */
int hr = m->highres; /* High res. */
int i;
if (m->capabilities & I1PRO3_CAP_HEAD_SENS) {
i1p3_adapter atype;
if ((ev = i1pro3_getadaptype(p, &atype)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_getadaptype failed\n");
return ev;
}
if (s->ambient) {
/* Do we have ambient adapter fitted ? */
if (atype != i1p3_ad_ambient) {
a1logd(p->log,1,"Expect ambient measurement adapter\n");
return I1PRO3_SPOS_AMB;
}
} else { /* Emision or Transmission */
/* Do we have normal adapter fitted ? */
if ((atype & i1p3_ad_standard) == 0) {
a1logd(p->log,1,"Expect an standard measurement adapter\n");
return I1PRO3_SPOS_STD;
}
}
}
if ((ev = i1pro3_getboardtemp(p, &btemp)) != I1PRO3_OK) {
error(" i1pro3_getboardtemp failed\n");
return ev;
}
/* Correct wl calibration for any temperature change */
if (m->ee_version >= 1) {
if ((ev = i1pro3_recompute_wav_filters_for_temp(p, btemp)) != I1PRO3_OK) {
error(" i1pro3_recompute_wav_filters_for_temp failed\n");
return ev;
}
}
/* Read short int time, and check/adjust measurement integration time */
binttime = m->min_int_time;
nummeasB = i1pro3_comp_nummeas(p, s->dreadtime, binttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_em, &raw_black1, &nummeasB, &binttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure failed\n");
return ev;
}
/* If we are assuming a strip, then we should be starting over the media */
/* which should be white, so use this to adjust the integration time a little */
if (!s->flash) {
i1pro3_average_rawmmeas(p, psample, raw_black1, nummeasB);
i1pro3_comp_simple_emis_black(p, black, raw_black1, nummeasB, binttime);
vect_sub(psample, black, m->nraw); /* Subtract black from psample */
i1pro3_vect_lin(p, psample); /* Linearize */
maxval = vect_max(psample, m->nraw);
a1logd(p->log,4," emis/trans short meas maxval %f\n",maxval);
if (maxval > m->sens_sat) {
a1logd(p->log,1," sample is saturated\n");
i1pro3_free_raw(p, i1p3mm_em, raw_black1, nummeasB);
return I1PRO3_RD_SENSORSATURATED;
} else {
double ainttime;
if (maxval <= 0.0)
maxval = 1.0;
ainttime = binttime * s->targoscale * m->sens_target/maxval;
if (ainttime < m->min_int_time)
ainttime = m->min_int_time;
/* Only lower integration time */
if (ainttime < s->inttime) {
s->inttime = ainttime;
a1logd(p->log,3," adjusted trans/emis scan inttime to %f\n",s->inttime);
}
}
}
inttime = s->inttime;
/* Main measure */
nummeasW = 0;
a1logd(p->log,2,"\nDoing emissive scan measure int_time %f\n", inttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_em, &raw_sample, &nummeasW, &inttime, &p2m, &npos)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure failed\n");
i1pro3_free_raw(p, i1p3mm_em, raw_black1, nummeasB);
return ev;
}
/* Read 2nd short int time */
if ((ev = i1pro3_do_measure(p, i1p3mm_em, &raw_black2, &nummeasB, &binttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure failed\n");
if (p2m != NULL) del_zebix_list(p2m, npos);
i1pro3_free_raw(p, i1p3mm_em, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeasW);
return ev;
}
/* Construct our black */
i1pro3_comp_emis_black(p, black, raw_black1, raw_black2, nummeasB, binttime,
raw_sample, nummeasW, inttime, btemp);
i1pro3_free_raw(p, i1p3mm_em, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_em, raw_black2, nummeasB);
//printf("~1 raw black, sample:\n");
//i1pro3_average_rawmmeas(p, psample, raw_sample, nummeasW);
//plot_raw2(black, psample);
/* Subtract black from sample */
i1pro3_multimeas_sub_black(p, raw_sample, nummeasW, black);
/* Check if sample is saturated */
if (i1pro3_multimeas_check_sat(p, NULL, raw_sample, nummeasW)) {
a1logd(p->log,1," sample is saturated\n");
if (p2m != NULL) del_zebix_list(p2m, npos);
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeasW);
return I1PRO3_RD_SENSORSATURATED;
}
/* Linearize and int time normalize sample */
i1pro3_multimeas_lin(p, raw_sample, nummeasW);
i1pro3_normalize_rawmmeas(p, raw_sample, nummeasW, inttime);
/* If not flash, deal wth patch recognition */
if (!s->flash) {
double **filt_sample; /* spetral normalize raw measurements */
int nfiltsamp; /* Number of normalized raw measurements */
double *norm; /* Normalizing spectrum */
i1pro3_patch *patch; /* List of patch locations within scan data */
double **wav_sample; /* processed wav measurements */
double **pwav_sample; /* pointer to a patches wav values */
/* If not transmission, construct a normalizing white */
if (!s->trans) {
vect_set(psample, 0.0, m->nraw);
for (i = 0; i < nummeasW; i++)
vect_max_elem(psample, raw_sample[i], m->nraw);
norm = psample;
} else {
norm = s->raw_white; /* Transmission white raw reference */
}
nfiltsamp = nummeasW;
filt_sample = dmatrix(0, nfiltsamp-1, -9, m->nraw-1);
/* Spectrally normalize recognition samples */
for (i = 0; i < nummeasW; i++)
vect_div3(filt_sample[i], raw_sample[i], norm, m->nraw);
/* Convert time based patch recognition proxy into position based one */
if (p2m != NULL) {
double **pos_sample; /* Position based raw samples */
int j, k;
pos_sample = dmatrix(0, npos-1, 0, m->nraw-1);
/* Average samples values into position slots */
for (i = 0; i < npos; i++) {
vect_set(pos_sample[i], 0.0, m->nraw);
if (p2m[i][-1] > 0) {
for (j = 0; j < p2m[i][-1]; j++)
vect_add(pos_sample[i], filt_sample[p2m[i][j]], m->nraw);
vect_scale1(pos_sample[i], 1.0/(double)j, m->nraw);
}
}
/* Now interpolate any empty slots */
for (i = 0; i < npos; i++) {
double bf;
if (p2m[i][-1] != 0)
continue;
for (j = i-1; j >= 0; j--) /* Search for lower non-empty */
if (p2m[j][-1] != 0)
break;
for (k = i+1; k < npos; k++) /* Search for upper non-empty */
if (p2m[k][-1] != 0)
break;
if (j < 0) {
j = k;
bf = 0.5;
} else if (k >= npos) {
k = j;
bf = 0.5;
} else {
bf = (i - j) / (double)(k - j);
}
vect_blend(pos_sample[i], pos_sample[j], pos_sample[k], bf, m->nraw);
}
/* Switch position proxy for time one */
free_dmatrix(filt_sample, 0, nfiltsamp-1, -9, m->nraw-1);
filt_sample = pos_sample;
nfiltsamp = npos;
}
/* Room for returned patch information */
if ((patch = malloc(sizeof(i1pro3_patch) * nvals)) == NULL) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free_dmatrix(filt_sample, 0, nfiltsamp-1, -9, m->nraw-1);
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeasW); raw_sample = NULL;
a1logd(p->log,1,"i1pro3_scan_emis_meas malloc %ld bytes failed\n",sizeof(i1pro3_patch) * nvals);
return I1PRO3_INT_MALLOC;
}
/* Locate patch boundaries */
if ((ev = i1pro3_locate_patches(p, patch, nvals, filt_sample, nfiltsamp, p2m)) != I1PRO3_OK) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free(patch);
free_dmatrix(filt_sample, 0, nfiltsamp-1, -9, m->nraw-1);
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeasW); raw_sample = NULL;
a1logd(p->log,1," i1pro3_scan_emis_meas patch recognition failed\n");
return ev;
}
free_dmatrix(filt_sample, 0, nfiltsamp-1, -9, m->nraw-1);
/* Allocate array of pointers to samples of max possible length */
if ((pwav_sample = malloc(sizeof(double *) * nummeasW)) == NULL) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free(patch);
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeasW); raw_sample = NULL;
a1logd(p->log,1,"i1pro3_scan_emis_meas malloc %ld bytes failed\n",sizeof(double *) * (nummeasW/2+1));
return I1PRO3_INT_MALLOC;
}
/* Convert all samples from raw to wav */
wav_sample = dmatrix(0, nummeasW-1, -9, m->nwav[hr]-1);
i1pro3_absraw_to_abswav(p, hr, 0, wav_sample, raw_sample, nummeasW);
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeasW); raw_sample = NULL;
i1pro3_straylight(p, hr, wav_sample, nummeasW);
/* Process each patch into spectral values */
for (i = 0; i < nvals; i++) {
int j, nummeas;
if (p2m != NULL) {
int k, ix;
/* Copy list of patches in p2m list */
for (ix = j = 0; j < patch[i].no; j++) {
int *plist = p2m[patch[i].ss + j]; /* List of patches in this slot */
for (k = 0; k < plist[-1]; k++) {
pwav_sample[ix++] = wav_sample[plist[k]];
}
}
nummeas = ix;
} else {
/* Copy samples pointers to wav values */
for (j = 0; j < patch[i].no; j++)
pwav_sample[j] = wav_sample[patch[i].ss + j];
nummeas = patch[i].no;
}
/* Check if each patch is consistent */
if (i1pro3_multimeas_check_wav_consistency(p, hr, inttime, pwav_sample, nummeas)) {
if (p2m != NULL) del_zebix_list(p2m, npos);
free(pwav_sample);
free(patch);
free_dmatrix(wav_sample, 0, nummeasW-1, -9, m->nwav[hr]-1);
a1logd(p->log,1," sample is inconsistent\n");
return I1PRO3_RD_READINCONS;
}
/* Average samples into patch value */
i1pro3_average_wavmmeas(p, specrd[i], pwav_sample, nummeas, hr);
/* Scale by calibration factor */
vect_mul(specrd[i], s->cal_factor[hr], m->nwav[hr]);
}
if (p2m != NULL)
del_zebix_list(p2m, npos);
free(pwav_sample);
free(patch);
free_dmatrix(wav_sample, 0, nummeasW-1, -9, m->nwav[hr]-1);
/* Flash recognition */
} else {
double raw_avg[MX_NRAW], *praw_avg[1] = { raw_avg };
double **wav_sample; /* processed wav measurements */
if ((ev = i1pro3_extract_patches_flash(p, duration, raw_avg, raw_sample, nummeasW, inttime)) != I1PRO3_OK) {
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeasW); raw_sample = NULL;
a1logd(p->log,1,"i1pro3_scan_emis_meas extract_patches_flash failed\n");
return ev;
}
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeasW);
/* Convert from raw to wav, apply straylight */
wav_sample = dmatrix(0, 0, -1, m->nwav[hr]-1);
i1pro3_absraw_to_abswav(p, hr, 0, wav_sample, praw_avg, 1);
i1pro3_straylight(p, hr, wav_sample, 1);
/* Scale by calibration factor */
vect_mul(wav_sample[0], s->cal_factor[hr], m->nwav[hr]);
vect_cpy(specrd[0], wav_sample[0], m->nwav[hr]);
free_dmatrix(wav_sample, 0, 0, -1, m->nwav[hr]-1);
}
return ev;
}
/* =======================================================================*/
/* Medium level support functions */
/* =======================================================================*/
/* Number of window size tries to use */
#define WIN_TRIES 20
/* Range of raw bands to detect transitions */
#define BL 20 /* Start */
#define BH 101 /* End+1 */
#define NFB 7 /* [7] Number of filtered bands (must be odd and < 10) */
/* Locate the required number of ref/emis/trans patch locations, */
/* and return a list of the patch boundaries */
i1pro3_code i1pro3_locate_patches(
i1pro3 *p,
i1pro3_patch *patches, /* Return patches[tnpatch] patch locations */
int tnpatch, /* Target number of patches to recognise */
double **rawmeas, /* Array of [nummeas][nraw] value to locate in */
int nummeas, /* number of raw samples */
int **p2m /* If not NULL, contains nummeas zebra ruler patch indexes */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
int i, j, k, pix;
double *maxval; /* Maximum input value for each wavelength */
int fbands[NFB][2]; /* Start & end+1 raw indexes of filter bands */
double **fraw; /* NFB filtered raw bands */
double *slope; /* Accumulated absolute difference between i and i+1 */
double *fslope; /* Filtered slope */
i1pro3_patch *pat; /* Possible patch information */
int npat, apat = 0, fpat; /* Number of potential, number allocated, number found */
double fmaxslope = 0.0;
double maxslope = 0.0;
double minslope = 1e38;
double thresh = 0.4; /* Slope threshold */
int minsamples = s->pol ? POL_MIN_SAMPLES : MIN_SAMPLES;
int try; /* Thresholding try */
double avglength; /* Average length of patches */
double maxlength; /* Max length of of patches */
int *sizepop; /* Size popularity of potential patches */
int msthr; /* Median search threshold */
double median; /* median potential patch width */
double window; /* +/- around median to accept */
double highest = -1e6;
double white_avg; /* Average of (aproximate) white data */
i1pro3_code rv = I1PRO3_OK;
#ifdef PATREC_DEBUG
double *pplot[10];
double **plot;
#endif
a1logd(p->log,2,"i1pro3_locate_patches looking for %d patches out of %d samples\n",tnpatch,nummeas);
maxval = dvectorz(0, m->nraw-1);
/* Discover the maximum input value for normalisation */
for (j = 0; j < m->nraw; j++) {
for (i = 0; i < nummeas; i++) {
if (rawmeas[i][j]
> maxval[j])
maxval[j] = rawmeas[i][j];
}
if (maxval[j] < 1.0)
maxval[j] = 1.0;
}
#ifdef PATREC_DEBUG
plot = dmatrixz(0, 10, 0, nummeas-1); /* Up to 11 values */
#ifdef PATREC_PLOT_ALLBANDS
for (j = 0; j < (m->nraw-10); j += 10) /* Plot all the bands */
#else
for (j = 24; j < (111-10); j += 30) /* Do some of the bands */
#endif
{
for (k = 0; k < 10; k ++) {
if (j + k >= m->nraw) {
pplot[k] = NULL;
continue;
}
for (i = 0; i < nummeas; i++)
plot[k][i] = rawmeas[i][j+k]/maxval[j+k];
pplot[k] = plot[k];
}
for (i = 0; i < nummeas; i++)
plot[10][i] = (double)i;
printf("Raw Bands %d - %d\n",j,j+9);
do_plot10(plot[10], pplot[0], pplot[1], pplot[2], pplot[3], pplot[4], pplot[5], pplot[6], pplot[7], pplot[8], pplot[9], nummeas, 0);
}
#endif /* PATREC_DEBUG */
/* Compute the range of each filter band */
{
double fbwidth; /* Number of raw in each band */
double st;
fbwidth = (BH - BL)/(double)(NFB/2+1);
for (st = BL, i = 0; i < NFB; i++) {
fbands[i][0] = (int)(floor(st));
fbands[i][1] = (int)(floor(st + fbwidth));
st += 0.5 * fbwidth;
}
//printf("fbwidth = %f\n",fbwidth);
//for (i = 0; i < NFB; i++) printf("~1 band %d is %d - %d\n",i,fbands[i][0],fbands[i][1]);
}
fraw = dmatrixz(0, nummeas-1, 0, NFB-1);
/* Weighted box average raw to create filtered bands */
for (i = 0; i < nummeas; i++) {
for (j = 0; j < NFB; j++) {
fraw[i][j] = 0.0;
for (k = fbands[j][0]; k < fbands[j][1]; k++) {
fraw[i][j] += rawmeas[i][k]/maxval[k];
}
fraw[i][j] /= (double)(fbands[j][1] - fbands[j][0]);
}
}
slope = dvectorz(0, nummeas-1);
/* Compute sliding window average and deviation that contains */
/* our output point, and chose the average with the minimum deviation. */
#define FW 5 /* [5] Number of delta's to average */
for (i = FW-1; i < (nummeas-FW); i++) { /* Samples */
double basl, bdev; /* Best average slope, Best deviation */
double sl[2 * FW -1];
double asl[FW], dev[FW];
int slopen = 0;
double slopeth = 0.0;
int m, pp;
for (pp = 0; pp < 2; pp++) { /* For each pass */
for (j = 0; j < NFB; j++) { /* For each band */
/* Compute differences for the range of our windows */
for (k = 0; k < (2 * FW -1); k++)
sl[k] = fraw[i+k-FW+1][j] - fraw[i+k+-FW+2][j];
/* For each window offset, compute average and deviation squared */
bdev = 1e38;
for (k = 0; k < FW; k++) {
/* Compute average of this window offset */
asl[k] = 0.0;
for (m = 0; m < FW; m++) /* For slope in window */
asl[k] += sl[k+m];
asl[k] /= (double)FW;
/* Compute deviation squared */
dev[k] = 0.0;
for (m = 0; m < FW; m++) {
double tt;
tt = sl[k+m] - asl[k];
dev[k] += tt * tt;
}
if (dev[k] < bdev)
bdev = dev[k];
}
/* Weight the deviations with a triangular weighting */
/* to skew slightly towards the center */
for (k = 0; k < FW; k++) {
double wt;
wt = fabs(2.0 * k - (FW -1.0))/(FW-1.0);
dev[k] += wt * bdev;
}
/* For each window offset, choose the one to use. */
bdev = 1e38;
basl = 0.0;
for (k = 0; k < FW; k++) {
/* Choose window average with smallest deviation squared */
if (dev[k] < bdev) {
bdev = dev[k];
basl = fabs(asl[k]);
}
}
if (pp == 0) { /* First pass, compute average slope over bands */
slope[i] += basl;
} else { /* Second pass, average slopes of bands over threshold */
if (basl > slopeth) {
slope[i] += basl;
slopen++;
}
}
} /* Next band */
if (pp == 0) {
slopeth = 1.0 * slope[i]/j; /* Compute threshold */
slope[i] = 0.0;
} else {
if (slopen > 0)
slope[i] /= slopen; /* Compute average of those over threshold */
}
} /* Next pass */
}
#undef FW
/* Normalise the slope values */
/* Locate the minumum and maximum values */
maxslope = 0.0;
minslope = 1e38;
for (i = 4; i < (nummeas-4); i++) {
double avs;
if (slope[i] > maxslope)
maxslope = slope[i];
/* Simple moving average for min comp. */
avs = 0.0;
for (j = -2; j <= 2; j++)
avs += slope[i+j];
avs /= 5.0;
if (avs < minslope)
minslope = avs;
}
/* Normalise the slope */
maxslope *= 0.5;
minslope *= 3.0;
for (i = 0; i < nummeas; i++) {
slope[i] = (slope[i] - minslope) / (maxslope - minslope);
if (slope[i] < 0.0)
slope[i] = 0.0;
else if (slope[i] > 1.0)
slope[i] = 1.0;
}
fslope = dvectorz(0, nummeas-1);
/* "Automatic Gain control" the raw slope information. */
#define LFW 40 /* [40] Half width of triangular filter */
for (i = 0; i < nummeas; i++) {
double sum, twt;
sum = twt = 0.0;
for (j = -LFW; j <= LFW; j++) {
double wt;
if ((i+j) < 0 || (i+j) >= nummeas)
continue;
wt = ((LFW-abs(j))/(double)LFW);
sum += wt * slope[i+j];
twt += wt;
}
fslope[i] = sum/twt;
if (fslope[i] > fmaxslope)
fmaxslope = fslope[i];
}
#undef LFW
free_dvector(fslope, 0, nummeas-1); fslope = NULL;
/* Locate the minumum and maximum slope values */
maxslope = 0.0;
minslope = 1e38;
for (i = 4; i < (nummeas-4); i++) {
double avs;
if (slope[i] > maxslope)
maxslope = slope[i];
/* Simple moving average for min comp. */
avs = 0.0;
for (j = -2; j <= 2; j++)
avs += slope[i+j];
avs /= 5.0;
if (avs < minslope)
minslope = avs;
}
/* Normalise the slope again */
maxslope *= 0.3;
minslope *= 3.0;
for (i = 0; i < nummeas; i++) {
slope[i] = (slope[i] - minslope) / (maxslope - minslope);
if (slope[i] < 0.0)
slope[i] = 0.0;
else if (slope[i] > 1.0)
slope[i] = 1.0;
}
#ifdef PATREC_DEBUG
printf("Slope filter output + filtered raw:\n");
for (j = 0; j < NFB; j++) {
for (i = 0; i < nummeas; i++)
plot[j][i] = fraw[i][j];
pplot[j] = plot[j];
}
for (; j < 10; j++)
pplot[j] = NULL;
for (i = 0; i < nummeas; i++)
plot[10][i] = (double)i;
do_plot10(plot[10], slope, pplot[0], pplot[1], pplot[2], pplot[3], pplot[4], pplot[5], pplot[6], pplot[7], pplot[8], nummeas, 0);
#endif /* PATREC_DEBUG */
sizepop = ivectorz(0, nummeas-1);
/* Now threshold the measurements into possible patches */
apat = 2 * nummeas;
if ((pat = (i1pro3_patch *)malloc(sizeof(i1pro3_patch) * apat)) == NULL) {
free_dmatrix(fraw, 0, nummeas-1, 0, NFB-1);
free_ivector(sizepop, 0, nummeas-1);
free_dvector(slope, 0, nummeas-1);
free_dvector(maxval, 0, m->nraw-1);
a1logd(p->log, 1, "i1pro3: malloc of patch structures failed!\n");
return I1PRO3_INT_MALLOC;
}
avglength = maxlength = 0.0;
for (npat = i = 0; i < (nummeas-1); i++) {
if (slope[i] > thresh)
continue;
/* Start of a new patch */
if (npat >= apat) {
apat *= 2;
if ((pat = (i1pro3_patch *)realloc(pat, sizeof(i1pro3_patch) * apat)) == NULL) {
free_dmatrix(fraw, 0, nummeas-1, 0, NFB-1);
free_ivector(sizepop, 0, nummeas-1);
free_dvector(slope, 0, nummeas-1);
free_dvector(maxval, 0, m->nraw-1);
a1logd(p->log, 1, "i1pro3: reallloc of patch structures failed!\n");
return I1PRO3_INT_MALLOC;
}
}
pat[npat].ss = i;
pat[npat].no = 2;
pat[npat].use = 0;
for (i++; i < (nummeas-1); i++) {
if (slope[i] > thresh)
break;
pat[npat].no++;
}
avglength += (double) pat[npat].no;
if (pat[npat].no > maxlength)
maxlength = pat[npat].no;
npat++;
}
a1logd(p->log,7,"Number of patches = %d\n",npat);
/* We don't count the first and last patches, as we assume they are white leader & tail */
if (npat < (tnpatch + 2)) {
free_dmatrix(fraw, 0, nummeas-1, 0, NFB-1);
free_ivector(sizepop, 0, nummeas-1);
free_dvector(slope, 0, nummeas-1);
free_dvector(maxval, 0, m->nraw-1);
free(pat);
a1logd(p->log,2,"Patch recog failed - unable to detect enough possible patches (%d < %d)\n",npat, tnpatch + 2);
return I1PRO3_RD_NOTENOUGHPATCHES;
} else if (npat >= (5 * tnpatch + 2)) {
free_dmatrix(fraw, 0, nummeas-1, 0, NFB-1);
free_ivector(sizepop, 0, nummeas-1);
free_dvector(slope, 0, nummeas-1);
free_dvector(maxval, 0, m->nraw-1);
free(pat);
a1logd(p->log,2,"Patch recog failed - detecting too many possible patches (%d >= %d)\n",npat , 5 * tnpatch + 2);
return I1PRO3_RD_TOOMANYPATCHES;
}
avglength /= (double)npat;
for (i = 0; i < npat; i++) {
a1logd(p->log,7,"Raw patch %d, start %d, length %d\n",i, pat[i].ss, pat[i].no);
}
/* Accumulate popularity ccount of possible patches */
for (i = 1; i < (npat-1); i++)
sizepop[pat[i].no]++;
/* Locate the median potential patch width */
msthr = npat - tnpatch; /* Excess number of patches */
if (msthr > 2)
msthr = 2; /* Skip possible header/tail patches */
msthr += tnpatch/2; /* Expected median largest patch no. */
/* Search from largest to smallest for median */
for (j = 0, i = maxlength; i > 0; i--) {
j += sizepop[i];
if (j >= msthr)
break;
}
median = (double)i;
a1logd(p->log,7,"Median patch width %f\n",median);
/* Now decide which patches to use. */
/* Try a widening window around the median. */
for (window = 0.1, try = 0; try < WIN_TRIES; window *= 1.3, try++) {
int bgcount = 0, bgstart = 0;
int gcount, gstart;
double wmin = median/(1.0 + window);
double wmax = median * (1.0 + window);
a1logd(p->log,7,"Window = %f - %f\n",wmin, wmax);
/* Track which is the largest contiguous group that */
/* is within our window */
gcount = gstart = 0;
for (i = 1; i < npat; i++) {
if (i < (npat-1) && pat[i].no <= wmax) { /* Small enough */
if (pat[i].no >= wmin) { /* And big enough */
if (gcount == 0) { /* Start of new group */
gcount++;
gstart = i;
a1logd(p->log,7,"Start group at %d\n",gstart);
} else {
gcount++; /* Continuing new group */
a1logd(p->log,7,"Continue group at %d, count %d\n",gstart,gcount);
}
}
} else { /* Too big or end of patches, end this group */
a1logd(p->log,7,"Terminating group group at %d, count %d\n",gstart,gcount);
if (gcount > bgcount) { /* New biggest group */
bgcount = gcount;
bgstart = gstart;
a1logd(p->log,7,"New biggest\n");
}
gcount = gstart = 0; /* End this group */
}
}
a1logd(p->log,7,"Biggest group is at %d, count %d\n",bgstart,bgcount);
/* Tag the patches that we would use */
for (fpat = 0, i = bgstart; i < npat; i++) {
if (pat[i].no <= wmax && pat[i].no >= wmin) {
pat[i].use = 1;
fpat++;
}
}
if (bgcount == tnpatch) { /* We're done */
break;
}
if (bgcount > tnpatch) {
a1logd(p->log,2,"Patch recog failed - detected too many consistent patches\n");
rv = I1PRO3_RD_TOOMANYPATCHES;
break;
}
}
if (try >= WIN_TRIES) {
a1logd(p->log,2,"Patch recog failed - unable to find enough consistent patches\n");
rv = I1PRO3_RD_NOTENOUGHPATCHES;
}
if (p->log->debug >= 7) {
a1logd(p->log,7,"Got %d patches out of potential %d, want %d:\n",fpat, npat, tnpatch);
a1logd(p->log,7,"Average patch length %f\n",avglength);
for (i = 1; i < (npat-1); i++) {
int nosamp = pat[i].no;
if (!pat[i].use)
continue;
if (p2m != NULL) { /* Count underlying samples in patch */
for (nosamp = k = 0; k < pat[i].no; k++)
nosamp += p2m[pat[i].ss + k][-1];
}
a1logd(p->log,7,"Patch %d, start %d, length %d (%d):\n",i, pat[i].ss, pat[i].no, nosamp);
}
}
/* Check every patch has enough samples before trimming */
for (k = 1; k < (npat-1); k++) {
int nosamp = pat[k].no;
if (!pat[k].use)
continue;
if (p2m != NULL) { /* Count underlying samples in patch */
for (nosamp = i = 0; i < pat[k].no; i++)
nosamp += p2m[pat[k].ss + i][-1];
}
if (nosamp < minsamples) {
a1logd(p->log,2,"Patch recog failed - too few samples (%d < %d)\n",nosamp, minsamples);
rv = I1PRO3_RD_NOTENOUGHSAMPLES;
}
}
/* Now trim the patches by shrinking their windows */
for (k = 1; k < (npat-1); k++) {
int nno, trim;
if (pat[k].use == 0)
continue;
// nno = (pat[k].no * 3 + 0)/4; /* Trim to 75% & round down */
nno = (pat[k].no * 2 + 0)/3; /* [def] Trim to 66% & round down */
// nno = (pat[k].no * 2 + 0)/4; /* Trim to 50% & round down */
trim = (pat[k].no - nno + 1)/2;
pat[k].ss += trim;
pat[k].no = nno;
}
#ifdef PATREC_SAVETRIMMED /* Save debugging file */
{
static int filen = 0; /* Debug file index */
char fname[100];
FILE *fp;
sprintf(fname, "i1pro3_raw_trimed_%d.csv",filen++);
if ((fp = fopen(fname, "w")) == NULL)
error("Unable to open debug output file '%'",fname);
/* Create fake "slope" value that marks patches */
for (i = 0; i < nummeas; i++)
slope[i] = 1.0;
for (k = 1; k < (npat-1); k++) {
if (pat[k].use == 0)
continue;
for (i = pat[k].ss; i < (pat[k].ss + pat[k].no); i++)
slope[i] = 0.0;
}
for (i = 0; i < nummeas; i++) {
fprintf(fp, "%f\t",slope[i]);
for (j = 0; j < m->nraw; j++)
fprintf(fp, "%f\t", rawmeas[i][j]/maxval[j]);
fprintf(fp, "\n");
}
fclose(fp);
}
#endif
#ifdef PATREC_DEBUG
printf("After trimming got:\n");
for (j = 0, i = 1; i < (npat-1); i++) {
int acount = pat[i].no;
if (!pat[i].use)
continue;
if (p2m != NULL) { /* Count underlying samples in patch */
for (acount = k = 0; k < pat[i].no; k++)
acount += p2m[pat[i].ss + k][-1];
}
printf("Patch %d (%d), start %d, length %d (%d):\n",i, j, pat[i].ss, pat[i].no, acount);
j++;
}
/* Create fake "slope" value that marks patches */
for (i = 0; i < nummeas; i++)
slope[i] = 1.0;
for (k = 1; k < (npat-1); k++) {
if (pat[k].use == 0)
continue;
for (i = pat[k].ss; i < (pat[k].ss + pat[k].no); i++)
slope[i] = 0.0;
}
printf("Trimmed output:\n");
#ifdef PATREC_PLOT_ALLBANDS
for (j = 0; j < (m->nraw-9); j += 9) /* Plot all the bands */
#else
for (j = 24; j < (111-9); j += 30) /* Do some of the bands */
#endif
{
for (k = 0; k < 9; k ++) {
if (j + k >= m->nraw) {
pplot[k] = NULL;
continue;
}
for (i = 0; i < nummeas; i++)
plot[k][i] = rawmeas[i][j+k]/maxval[j+k];
pplot[k] = plot[k];
}
for (i = 0; i < nummeas; i++)
plot[10][i] = (double)i;
printf("Raw Bands %d - %d\n",j,j+8);
do_plot10(plot[10], slope, pplot[0], pplot[1], pplot[2], pplot[3], pplot[4], pplot[5], pplot[6], pplot[7], pplot[8], nummeas, 0);
}
free_dmatrix(plot, 0, 10, 0, nummeas-1);
#endif /* PATREC_DEBUG */
free_dmatrix(fraw, 0, nummeas-1, 0, NFB-1);
free_dvector(slope, 0, nummeas-1);
free_ivector(sizepop, 0, nummeas-1);
free_dvector(maxval, 0, m->nraw-1);
if (rv != I1PRO3_OK) {
free(pat);
return rv;
}
/* Now copy the patch locates to return value */
for (pix = 0, k = 1; k < (npat-1) && pix < tnpatch; k++) {
double maxavg = -1e38; /* Track min and max averages of readings for consistency */
double minavg = 1e38;
double avgoverth = 0.0; /* Average over saturation threshold */
double cons; /* Consistency */
int nosamp = pat[k].no;
if (!pat[k].use)
continue;
/* Check there are enough samples after trimming */
if (p2m != NULL) { /* Count underlying samples in patch */
for (nosamp = i = 0; i < pat[k].no; i++)
nosamp += p2m[pat[k].ss + i][-1];
}
if (nosamp < minsamples) {
free(pat);
a1logd(p->log,2,"Patch recog failed - too few trimmed samples (%d, need %d)\n",nosamp,MIN_SAMPLES);
return I1PRO3_RD_NOTENOUGHSAMPLES;
}
patches[pix++] = pat[k]; /* struct copy */
}
free(pat);
return I1PRO3_OK;
}
#undef BL
#undef BH
#undef BW
#undef WIN_TRIES
#undef MIN_SAMPLES
/* Turn a raw set of reflective samples into one calibrated patch spectral value */
/* Samples have been black subtracted and linearized */
i1pro3_code i1pro3_comp_refl_value(
i1pro3 *p,
double *m0_spec, /* Return m0 spectra ('A' illuminant) if not NULL */
double *m1_spec, /* Return m1 spectra ('D50' illuminant) if not NULL */
double *m2_spec, /* Return m2 spectra (UV cut) if not NULL */
double **eproc_sample, /* eproc_sample[enumsample][-9, nwav] */
int enummeas, /* Number of even samples */
double **oproc_sample, /* oproc_sample[onumsample][-9, nwav] */
int onummeas, /* Number of odd samples */
int hr /* High res flag */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
double **edriftcorr; /* led model, drift compensation corrections */
double **odriftcorr; /* led model, drift compensation corrections */
double **avg_nd_sample; /* average nn/uv not drift corrected samples */
double **avg_sample; /* average nn/uv samples, progresively corrected */
double *avg_uv_drcor; /* Average uv drift correction values */
double **matA_3x36, **imatA_36x3;
double c12timesc7[36];
double _sumc0c1c2[MX_NWAV], *sumc0c1c2[1] = { _sumc0c1c2 };
double **uv_weight, uvbl_val;
double fwa_edi_resp[MX_NWAV];
double fwa_e_resp[MX_NWAV];
double sc_avg_sample[MX_NWAV]; /* Scaled avg_sample */
double cv_A[MX_NWAV];
double uv_ratio_scale, uv_ratio;
double cv_B[MX_NWAV];
double cv_C[MX_NWAV];
double cv_BmC[MX_NWAV];
double cv_D[MX_NWAV];
double cv_E[MX_NWAV];
double cv_F[MX_NWAV];
double cv_G[MX_NWAV];
double cv_H[MX_NWAV];
double cv_I[MX_NWAV];
double cv_J[MX_NWAV];
double cv_K[MX_NWAV];
double cv_L[MX_NWAV];
double cv_M[MX_NWAV];
double cv_O[MX_NWAV];
double cv_P[MX_NWAV];
double cv_Q[MX_NWAV];
double m0[MX_NWAV], m1[MX_NWAV], m2[MX_NWAV];
int i;
a1logd(p->log,3,"i1pro3_comp_refl_value\n");
/* Compute average of sample measurement with no drift correction */
avg_nd_sample = dmatrix(0, 1, 0, m->nwav[hr]-1);
i1pro3_average_wavmmeas(p, avg_nd_sample[0], eproc_sample, enummeas, hr);
i1pro3_average_wavmmeas(p, avg_nd_sample[1], oproc_sample, onummeas, hr);
/* Compute LED model for each measurement */
edriftcorr = dmatrix(0, enummeas-1, -9, MX_NWAV);
odriftcorr = dmatrix(0, onummeas-1, -9, MX_NWAV);
i1pro3_comp_ledm(p, edriftcorr, eproc_sample, enummeas, 0);
i1pro3_comp_ledm(p, odriftcorr, oproc_sample, onummeas, 1);
/* Average uv drift compensation values at std res and then hr res */
avg_uv_drcor = dvectorz(0, MX_NWAV-1);
/* Compute drift correction for each measurement */
/* and apply it to each measurement */
for (i = 0; i < enummeas; i++) { /* For nn */
vect_div3_safe(edriftcorr[i], s->calsp_ledm[0], edriftcorr[i], 36);
if (hr)
fast_upsample(p, edriftcorr[i], edriftcorr[i], 0, "nn drift corn");
vect_mul(eproc_sample[i], edriftcorr[i], m->nwav[hr]);
}
for (i = 0; i < onummeas; i++) { /* For uv */
vect_div3_safe(odriftcorr[i], s->calsp_ledm[1], odriftcorr[i], 36);
vect_add(avg_uv_drcor, odriftcorr[i], 36);
if (hr)
fast_upsample(p, odriftcorr[i], odriftcorr[i], 0, "nn drift corn");
vect_mul(oproc_sample[i], odriftcorr[i], m->nwav[hr]);
}
/* Compute std res avg_uv_drcor */
vect_scale1(avg_uv_drcor, 1.0/(double)onummeas, 36);
/* Compute average of drift corrected sample */
avg_sample = dmatrix(0, 1, 0, m->nwav[hr]-1);
i1pro3_average_wavmmeas(p, avg_sample[0], eproc_sample, enummeas, hr);
i1pro3_average_wavmmeas(p, avg_sample[1], oproc_sample, onummeas, hr);
/* Scale average drift corrected sample by illumination correction */
vect_mul(avg_sample[0], s->calsp_illcr[hr][0], m->nwav[hr]);
vect_mul(avg_sample[1], s->calsp_illcr[hr][1], m->nwav[hr]);
free_dmatrix(edriftcorr, 0, enummeas-1, -9, MX_NWAV);
free_dmatrix(odriftcorr, 0, onummeas-1, -9, MX_NWAV);
/* Stray light correct average drift and illumination corrected sample values */
i1pro3_straylight(p, hr, avg_sample, 2);
clear_low_wav(p, avg_sample[0], 4, hr); /* Zero first 4 nn samples */
/* Now that we have the averaged values, we can compute the rest of the conversion */
/* Like ledm, do matrix computations leading to uvbl_val at std res. */
/* Setup matrix */
matA_3x36 = dmatrix(0, 2, 0, 36-1);
vect_cpy(matA_3x36[0], s->sc_calsp_nn_white[0], 36); /* nn white sum scaled to ref illum */
vect_cpy(matA_3x36[1], s->cal_l_uv_diff[0], 36); /* long UV diff spectrum */
vect_cpy(matA_3x36[2], s->cal_s_uv_diff[0], 36); /* short UV diff spectrum */
/* Sum of the three components of the matrix */
vect_add3(_sumc0c1c2, matA_3x36[0], matA_3x36[1], 36);
vect_add(_sumc0c1c2, matA_3x36[2], 36);
/* Normalize it by 1.0/(drift correction * uv illum correction) */
vect_mul3(c12timesc7, avg_uv_drcor, s->calsp_illcr[0][1], 36);
vect_div(matA_3x36[0], c12timesc7, 36);
vect_div(matA_3x36[1], c12timesc7, 36);
vect_div(matA_3x36[2], c12timesc7, 36);
/* Pseudo-Invert using lu decomposition */
imatA_36x3 = dmatrix(0, 36-1, 0, 2);
if (lu_psinvert(imatA_36x3, matA_3x36, 3, 36)) {
a1logd(p->log,1,"i1pro3_do_measure: invert failed at line %d\n",__LINE__);
return I1PRO3_INT_ASSERT;
}
/* Compute weighting of (inverse drift/illum corrected) c0, c1, c2 */
/* that best approximates the sum of c0, c1, c2. */
/* These will all be close to 1.0 ... */
uv_weight = dmatrix(0, 0, 0, 2);
if (matrix_mult(uv_weight, 1, 3,
sumc0c1c2, 1, 36, imatA_36x3, 36, 3)) {
a1logd(p->log,1,"i1pro3_do_measure: matrix_mult failed at line %d\n",__LINE__);
return I1PRO3_INT_ASSERT;
}
free_dmatrix(imatA_36x3, 0, 35, 0, 2);
free_dmatrix(matA_3x36, 0, 2, 0, 36-1);
/* Weighted blend of long and short UV inverse correction factors. */
/* (Could we fix above to work using inverse correction factor, to */
/* eliminate inverses here ?) */
uvbl_val = 1.0/((m->ee_sluv_bl/uv_weight[0][2] + (1.0 - m->ee_sluv_bl)/uv_weight[0][1]));
/* now back to the samples .... */
/* FWA response due to UV */
vect_sub3(fwa_edi_resp, avg_sample[1], avg_sample[0], m->nwav[hr]);
/* Converted to emission calibrated spectrum */
vect_mul(fwa_edi_resp, m->emis_coef[hr], m->nwav[hr]);
/* - - - - - - - */
/* Non Drift and Illuminant corrected samples: */
/* Stray light correct nn/uv !drift & !illum sample values */
i1pro3_straylight(p, hr, avg_nd_sample, 2);
clear_low_wav(p, avg_nd_sample[0], 4, hr); /* Zero first 4 nn samples */
/* FWA response due to UV */
vect_sub3(fwa_e_resp, avg_nd_sample[1], avg_nd_sample[0], m->nwav[hr]);
free_dmatrix(avg_nd_sample, 0, 1, 0, m->nwav[hr]-1);
/* Converted to emission calibrated spectrum */
vect_mul(fwa_e_resp, m->emis_coef[hr], m->nwav[hr]);
/* - - - - - - - - */
/* 2/(2 x average actual nn & uv illumination)
x average of drift & ill & stray corrd, normalized nn sample */
vect_mul3(sc_avg_sample, avg_sample[0], s->iavg2aillum[hr], m->nwav[hr]);
vect_scale1(sc_avg_sample, 2.0, m->nwav[hr]);
/* sc_avg_sample
x drift & stray corrd long uv - nn, scaled to have ee_luv_inttarg sum
x Emissive calibration factor / White reference tile reflectivity
*/
/* cv_A */
vect_mul3(cv_A, sc_avg_sample, m->emis_coef[hr], m->nwav[hr]);
vect_div(cv_A, m->white_ref[hr], m->nwav[hr]);
vect_mul(cv_A, s->cal_l_uv_diff[hr], m->nwav[hr]);
/* We're subtracting the reflected long uv response from measured */
/* difference between uv and nn */
if (hr) {
double wl1, wl2;
double wl3, wl4;
double sr = m->wl_width[1]/m->wl_width[0];
wl1 = 442.0; /* [441] */
wl2 = 450.0; /* [450] */
wl3 = 540.0; /* [540] */
wl4 = 548.0; /* [549] */
uv_ratio = ( sr * sum_wav2(p, fwa_e_resp, wl1, wl2, wl3, wl4, 1)
- sr * sum_wav2(p, cv_A, wl1, wl2, wl3, wl4, 1)/uv_weight[0][1])
* uvbl_val;
} else {
uv_ratio = ( vect_sum(fwa_e_resp + 7, 16 - 7)
- vect_sum(cv_A + 7, 16 - 7)/uv_weight[0][1])
* uvbl_val;
}
free_dmatrix(uv_weight, 0, 0, 0, 2);
/* Threshold the ratio below 10, and linearly interpolated */
/* transition from the 10 to 30 */
uv_ratio_scale = 1.0;
if (uv_ratio < 10.0)
uv_ratio_scale = 0.0;
else if (uv_ratio < 30.0)
uv_ratio_scale = (uv_ratio - 10.0)/20.0;
uv_ratio *= uv_ratio_scale;
/* Scale spectrum by ratio */
vect_scale(cv_B, m->fwa_std[hr], uv_ratio, m->nwav[hr]);
//cv_C = avg_uv_drcor * calsp_illcr[1] * cv_B / uvbl_val
if (hr)
fast_upsample(p, avg_uv_drcor, avg_uv_drcor, 0, "avg_uv_drcor");
vect_mul3(cv_C, avg_uv_drcor, s->calsp_illcr[hr][1], m->nwav[hr]);
vect_mul3(cv_C, cv_C, cv_B, m->nwav[hr]);
vect_scale1(cv_C, 1.0/uvbl_val, m->nwav[hr]);
// cv_D = fwa_edi_resp + cv_B - cv_C
vect_sub3(cv_BmC, cv_B, cv_C, m->nwav[hr]);
vect_add3(cv_D, fwa_edi_resp, cv_BmC, m->nwav[hr]);
// cv_E = avg_sample nn + uv
vect_add3(cv_E, avg_sample[1], avg_sample[0], m->nwav[hr]);
free_dmatrix(avg_sample, 0, 1, 0, m->nwav[hr]);
// cv_F = iavg2aillum * (cv_E + (cv_B - cv_C)/emis_coef)
vect_div3(cv_F, cv_BmC, m->emis_coef[hr], m->nwav[hr]);
vect_add(cv_F, cv_E, m->nwav[hr]);
vect_mul(cv_F, s->iavg2aillum[hr], m->nwav[hr]);
// cv_G = <m2_fwa> * (cv_D - <fwa_cal> * cv_F)
vect_mul3(cv_G, m->fwa_cal[hr], cv_F, m->nwav[hr]);
vect_sub3(cv_G, cv_D, cv_G, m->nwav[hr]);
vect_mul(cv_G, m->m2_fwa[hr], m->nwav[hr]);
// cv_H = 1.0
// cv_I = cv_H - <m2_fwa> * <fwa_cal>
vect_mul3(cv_I, m->m2_fwa[hr], m->fwa_cal[hr], m->nwav[hr]);
vect_set(cv_H, 1.0, m->nwav[hr]);
vect_sub3(cv_I, cv_H, cv_I, m->nwav[hr]);
// cv_J = clip+ve(cv_G / cv_I) * uv_ratio_scale
vect_div3(cv_J, cv_G, cv_I, m->nwav[hr]);
vect_clip(cv_J, cv_J, 0.0, DBL_MAX, m->nwav[hr]);
vect_scale1(cv_J, uv_ratio_scale, m->nwav[hr]);
// Result: clip(m2 = cv_F - CV_J)
// Note m2 is uv-excluded. m0 & m1 are based on top of this.
vect_sub3(m2, cv_F, cv_J, m->nwav[hr]);
vect_clip(m2, m2, 0.0, DBL_MAX, m->nwav[hr]);
if (m2_spec != NULL) {
vect_cpy(m2_spec, m2, m->nwav[hr]);
}
if (m1_spec != NULL) {
// m1: D50 illum.
// cv_L = <m1_fwa> x <fwa_cal> x m2
vect_mul3(cv_L, m->m1_fwa[hr], m->fwa_cal[hr], m->nwav[hr]);
vect_mul(cv_L, m2, m->nwav[hr]);
// cv_K = <m1_fwa> * cv_D
vect_mul3(cv_K, m->m1_fwa[hr], cv_D, m->nwav[hr]);
// cv_M = cvlip+ve(cv_K - cv_L) * uv_ratio_scale
vect_sub3(cv_M, cv_K, cv_L, m->nwav[hr]);
vect_clip(cv_M, cv_M, 0.0, DBL_MAX, m->nwav[hr]);
vect_scale1(cv_M, uv_ratio_scale, m->nwav[hr]);
// Result: clip(m1 = cv_M + m2) */
vect_add3(m1, cv_M, m2, m->nwav[hr]);
vect_clip(m1, m1, 0.0, DBL_MAX, m->nwav[hr]);
vect_cpy(m1_spec, m1, m->nwav[hr]);
}
if (m0_spec != NULL) {
// m0: legacy, tungsten illum.
// cv_P = <m0_fwa> x <fwa_cal> x m2
vect_mul3(cv_P, m->m0_fwa[hr], m->fwa_cal[hr], m->nwav[hr]);
vect_mul(cv_P, m2, m->nwav[hr]);
// cv_O = <m0_fwa> x cv_D */
vect_mul3(cv_O, m->m0_fwa[hr], cv_D, m->nwav[hr]);
// cv_Q = cvlip+ve(cv_O - cv_P) * uv_ratio_scale
vect_sub3(cv_Q, cv_O, cv_P, m->nwav[hr]);
vect_clip(cv_Q, cv_Q, 0.0, DBL_MAX, m->nwav[hr]);
vect_scale1(cv_Q, uv_ratio_scale, m->nwav[hr]);
// Result: clip(m0 = cv_Q + m2) */
vect_add3(m0, cv_Q, m2, m->nwav[hr]);
vect_clip(m0, m0, 0.0, DBL_MAX, m->nwav[hr]);
vect_cpy(m0_spec, m0, m->nwav[hr]);
}
free_dvector(avg_uv_drcor, 0, MX_NWAV-1);
return ev;
}
/* Turn a set of polarized reflective samples into one calibrated patch spectral value */
/* Samples have been black subtracted, linearized and converted to wav */
i1pro3_code i1pro3_comp_pol_refl_value(
i1pro3 *p,
double *spec, /* Return calibrated spectra */
double **proc_sample, /* proc_sample[enumsample][-9, nwav] */
int nummeas, /* Number of samples */
int hr
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
#ifdef STRICT_IMITATE
double refillumsum[36]; /* Sum of nn & uv illumination references */
double nnweight[36]; /* nn ref illum. blend weighting */
double uvweight[36]; /* uv ref illum. blend weighting */
#endif
int i;
a1logd(p->log,3,"i1pro3_comp_pol_refl_value\n");
#ifdef STRICT_IMITATE
/* Compute reference illumination blend weightings. Weights sum to 1.0, */
/* and are equal above 450nm, with full weight to the UV region in uvweight */
/* and zero weight in nnweight. */
vect_add3(refillumsum, m->ee_ref_nn_illum, m->ee_ref_uv_illum, 36);
vect_div3(nnweight, m->ee_ref_nn_illum, refillumsum, 36);
vect_div3(uvweight, m->ee_ref_uv_illum, refillumsum, 36);
#endif
/* Compute reflectance for each sample and accumulate */
vect_set(spec, 0.0, m->nwav[hr]);
for (i = 0; i < nummeas; i++) {
double nn_ledm[36], *pnn_ledm[1] = { nn_ledm }; /* nn led model for this measurement */
double uv_ledm[MX_NWAV], *puv_ledm[1] = { uv_ledm };/* uv led model for this measurement */
i1pro3_comp_ledm(p, pnn_ledm, proc_sample + i, 1, 0);
i1pro3_comp_ledm(p, puv_ledm, proc_sample + i, 1, 1);
#ifdef STRICT_IMITATE
/* Same calculation as pol calibration - see comments there */
vect_mul(uv_ledm, uvweight, 36);
vect_mul(nn_ledm, nnweight, 36);
/* This measurements led model */
vect_add(uv_ledm, nn_ledm, 36);
#else
/* Use (presumed) correct average of nn & uv as illum cal reference */
vect_blend(uv_ledm, uv_ledm, nn_ledm, 0.5, 36);
#endif
/* Compute led drift compensation value */
vect_div3(nn_ledm, s->pol_calsp_ledm, uv_ledm, 36);
if (hr)
fast_upsample(p, nn_ledm, nn_ledm, 0, "pol ledm compensation");
/* Illumination compensated value */
vect_mul(proc_sample[i], nn_ledm, m->nwav[hr]);
i1pro3_straylight(p, hr, proc_sample + i, 1);
/* Convert to calibrated reflectance value */
vect_muldiv(proc_sample[i], m->white_ref[hr], s->pol_calsp_white[hr], m->nwav[hr]);
/* Sum to average reflectance of sample */
vect_add(spec, proc_sample[i], m->nwav[hr]);
}
/* Complete average calculations of return value */
vect_scale1(spec, 1.0/nummeas, m->nwav[hr]);
return ev;
}
/* Filter dynamic black shield value to smooth out randomness */
double i1pro3_dynsh_filt(
i1pro3 *p,
time_t meastime,
double temp,
double inttime,
double sv
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int ix = m->dynsh_ix = (m->dynsh_ix + 1) % N_DYNSHVAL;
int i, nav;
double twt, rsv;
double tlimit = 2.0;
/* Add the new measurement */
m->dynsh[ix].meastime = meastime;
m->dynsh[ix].temp = temp;
m->dynsh[ix].inttime = inttime;
m->dynsh[ix].sv = sv;
/* Compute weighted average. We weight samples with long integration times */
/* and close temperatures. */
rsv = twt = 0.0;
for (nav = i = 0; i < N_DYNSHVAL; i++) {
double tdel, wt;
if ((meastime - m->dynsh[i].meastime) > DCALTOUT)
continue; /* Too old */
tdel = fabs(temp - m->dynsh[i].temp);
if (tdel >= tlimit)
continue;
wt = m->dynsh[i].inttime * (tlimit - tdel);
rsv += wt * m->dynsh[i].sv;
twt += wt;
nav++;
}
rsv /= twt;
a1logd(p->log,8," i1pro3_dynsh_filt got sv %f returning %f from avg of %d\n",sv,rsv,nav);
return rsv;
}
/* Construct a black to subtract from an emissive measurement. */
void i1pro3_comp_emis_black(
i1pro3 *p,
double *black, /* Return raw black to subtract */
double **raw_black1, /* Before and after short measurements */
double **raw_black2,
int nummeasB, /* Number of samples in short measurements */
double sinttime, /* Integration time of short measurements (typicall min_int) */
double **raw_sample, /* Longer sample measurement */
int nummeasS, /* Number of samples in longer measurement */
double linttime, /* Integration time of longer measurement */
double btemp /* Current board temperature */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
// double _psample[MX_NRAW_1], *psample = _psample + 1;
double sb, sv; /* Shield base and variable values */
double sa, da, bl; /* Dynamic adjustment, blend factor */
int i;
/* Average the short shielded values */
sb = 0.0;
for (i = 0; i < nummeasB; i++) {
sb += raw_black1[i][-1];
sb += raw_black2[i][-1];
}
sb /= (2.0 * nummeasB);
/* Average the long shield values */
sv = 0.0;
for (i = 0; i < nummeasS; i++)
sv += raw_sample[i][-1];
sv /= (double) nummeasS;
/* Compute shield variable and base values */
if (linttime > (20.0 * sinttime)) {
sv = (sv - sb)/(linttime - sinttime);
/* The sv value is quite noisy, so filter it using any recent measurements */
sv = i1pro3_dynsh_filt(p, time(NULL), btemp, linttime, sv);
} else {
sv = s->idark_data[1][-1]; /* Use calibration value so adj == 1.0 */
}
sb = sb - sv * sinttime;
sa = sb/s->idark_data[0][-1]; /* Base static temp. adjustment */
da = sv/s->idark_data[1][-1]; /* Base dynamic temp. adustment */
#ifdef ENABLE_DYNBLKTC
/* The dynamic adjustment may make the short term */
/* repeatability worse by a factor of about 10 without filtering. */
/* But it significantly improves the medium and long term stability. */
if (linttime < 0.05)
bl = 0.0;
else if (linttime > 0.5)
bl = 1.0;
else
bl = (linttime - 0.05)/(0.5 - 0.05);
da = (1.0 - bl) * 1.0 + bl * da;
#else
da = 1.0;
#endif
/* Construct our black */
vect_scale(black, s->idark_data[0], sa, m->nraw);
vect_scaleadd(black, s->idark_data[1], linttime * da, m->nraw);
//printf("~1 raw black, sample:\n");
//i1pro3_average_rawmmeas(p, psample, raw_sample, nummeasW);
//plot_raw2(black, psample);
}
/* Do an adaptive emission type measurement and return the processed raw values. */
/* We don't check the adapter type. */
/* It is checked for consistency. */
/* It is scaled for integration time. */
/* To compute to calibrated wav need to:
convert from raw to wav
apply straylight
apply wav calibration factors
possibly average together
*/
i1pro3_code i1pro3_spot_adapt_emis_raw_meas(
i1pro3 *p,
double ***praw, /* Return raw measurements. */
/* Call i1pro3_free_raw(p, i1p3mm_em, raw, nummeas) when done */
int *pnummeas /* Return number of raw measurements */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
double btemp;
double binttime, ainttime;
int nummeasB, nummeasW;
double **raw_black1, **raw_black2, **raw_sample; /* raw measurements */
double black[MX_NRAW], _psample[MX_NRAW+1], *psample = _psample+1;
double maxval;
int i;
if ((ev = i1pro3_getboardtemp(p, &btemp)) != I1PRO3_OK) {
error(" i1pro3_getboardtemp failed\n");
return ev;
}
/* Correct wl calibration for any temperature change */
if (m->ee_version >= 1) {
if ((ev = i1pro3_recompute_wav_filters_for_temp(p, btemp)) != I1PRO3_OK) {
error(" i1pro3_recompute_wav_filters_for_temp failed\n");
return ev;
}
}
/* Preliminary measure to calculate target integration time */
/* An integration time of 0.05 should be good up to about */
/* 5000 cd/m^2 for typical displays, and we fall back */
/* to min_int sample if level is higher than this. */
ainttime = 0.05;
nummeasW = i1pro3_comp_nummeas(p, 0.25, ainttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_em, &raw_sample, &nummeasW, &ainttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure failed\n");
return ev;
}
i1pro3_average_rawmmeas(p, psample, raw_sample, nummeasW);
i1pro3_comp_simple_emis_black(p, black, raw_sample, nummeasW, ainttime);
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeasW);
vect_sub(psample, black, m->nraw); /* Subtract black from psample */
i1pro3_vect_lin(p, psample); /* Linearize */
maxval = vect_max(psample, m->nraw);
a1logd(p->log,4," adaptive pmeas maxval %f\n",maxval);
if (maxval > m->sens_sat) {
ainttime = 0.0;
} else {
if (maxval <= 0.0)
maxval = 1.0;
ainttime = ainttime * s->targoscale * m->sens_target/maxval;
if (ainttime < m->min_int_time)
ainttime = m->min_int_time;
else if (ainttime > m->max_int_time)
ainttime = m->max_int_time;
}
a1logd(p->log,3," adaptive inttime %f\n",ainttime);
{
/* Read short int time */
binttime = m->min_int_time;
nummeasB = i1pro3_comp_nummeas(p, s->dreadtime, binttime);
}
if ((ev = i1pro3_do_measure(p, i1p3mm_em, &raw_black1, &nummeasB, &binttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure failed\n");
return ev;
}
/* If preliminary measure was saturated, so use short measure to compute ainttime */
if (ainttime == 0.0) {
i1pro3_average_rawmmeas(p, psample, raw_black1, nummeasB);
i1pro3_comp_simple_emis_black(p, black, raw_black1, nummeasB, binttime);
vect_sub(psample, black, m->nraw); /* Subtract black from psample */
i1pro3_vect_lin(p, psample); /* Linearize */
maxval = vect_max(psample, m->nraw);
a1logd(p->log,4," short meas maxval %f\n",maxval);
if (maxval > m->sens_sat) {
a1logd(p->log,1," sample is saturated\n");
i1pro3_free_raw(p, i1p3mm_em, raw_black1, nummeasB);
return I1PRO3_RD_SENSORSATURATED;
} else {
if (maxval <= 0.0)
maxval = 1.0;
ainttime = binttime * s->targoscale * m->sens_target/maxval;
if (ainttime < m->min_int_time)
ainttime = m->min_int_time;
else if (ainttime > m->max_int_time)
ainttime = m->max_int_time;
}
a1logd(p->log,3," adaptive inttime #2 %f\n",ainttime);
}
/* For non-adaptive we should only reduce integration time */
if (!s->adaptive) {
if (ainttime < s->inttime) {
s->inttime = ainttime;
a1logd(p->log,5,"Reduced display integration time to %f\n",s->inttime);
}
ainttime = s->inttime;
}
/* Main measure */
nummeasW = i1pro3_comp_nummeas(p, s->wreadtime, ainttime);
a1logd(p->log,2,"\nDoing adaptive measure nummeas %d of int_time %f\n", nummeasW, ainttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_em, &raw_sample, &nummeasW, &ainttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure failed\n");
i1pro3_free_raw(p, i1p3mm_em, raw_black1, nummeasB);
return ev;
}
/* Read 2nd short int time */
if ((ev = i1pro3_do_measure(p, i1p3mm_em, &raw_black2, &nummeasB, &binttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure failed\n");
i1pro3_free_raw(p, i1p3mm_em, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeasW);
return ev;
}
/* Construct our black */
i1pro3_comp_emis_black(p, black, raw_black1, raw_black2, nummeasB, binttime,
raw_sample, nummeasW, ainttime, btemp);
i1pro3_free_raw(p, i1p3mm_em, raw_black1, nummeasB);
i1pro3_free_raw(p, i1p3mm_em, raw_black2, nummeasB);
//printf("~1 raw black, sample:\n");
//i1pro3_average_rawmmeas(p, psample, raw_sample, nummeasW);
//plot_raw2(black, psample);
/* Subtract black from sample */
i1pro3_multimeas_sub_black(p, raw_sample, nummeasW, black);
/* Check if sample is saturated */
if (i1pro3_multimeas_check_sat(p, NULL, raw_sample, nummeasW)) {
a1logd(p->log,1," sample is saturated\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_sample, nummeasW);
return I1PRO3_RD_SENSORSATURATED;
}
/* Check if sample is consistent */
if (i1pro3_multimeas_check_raw_consistency(p, raw_sample, nummeasW)) {
a1logd(p->log,1," sample is inconsistent\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_sample, nummeasW);
return I1PRO3_RD_READINCONS;
}
/* Linearize and normalize integration time */
i1pro3_multimeas_lin(p, raw_sample, nummeasW);
i1pro3_normalize_rawmmeas(p, raw_sample, nummeasW, ainttime);
/* Return result */
if (praw != NULL)
*praw = raw_sample;
if (pnummeas != NULL)
*pnummeas = nummeasW;
return ev;
}
/* Construct a simple (staticaly) adjusted black to subtract from an emissive measurement. */
void i1pro3_comp_simple_emis_black(
i1pro3 *p,
double *black, /* Return raw black to subtract */
double **raw_sample, /* Sample measurements */
int nummeas, /* Number of samples */
double inttime /* Integration time */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
double sb; /* Shield base values */
double sa; /* Dynamic adjustment, blend factor */
int i;
/* Average the shielded values */
sb = 0.0;
for (i = 0; i < nummeas; i++)
sb += raw_sample[i][-1];
sb /= (double) nummeas;
sb = sb - s->idark_data[1][-1] * inttime; /* Zero int. time base shield value */
sa = sb/s->idark_data[0][-1]; /* Base static temp. adjustment */
/* Construct our black */
vect_scale(black, s->idark_data[0], sa, m->nraw);
vect_scaleadd(black, s->idark_data[1], inttime, m->nraw);
}
/* Do a simple emission measurement and return the processed raw values */
/* We don't check the adapter type. */
/* To compute to calibrated wav values need to:
convert from raw to wav
apply straylight
apply wav calibration factors
*/
i1pro3_code i1pro3_spot_simple_emis_raw_meas(
i1pro3 *p,
double ***praw, /* Return raw measurements. */
/* Call i1pro3_free_raw(p, i1p3mm_em, raw, nummeas) when done */
int *pnummeas, /* Return number of raw measurements */
double *pinttime, /* Integration time to use */
double meastime, /* Measurement time to use */
int donorm /* Normalize for integration time */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
int nummeas;
double **raw_sample; /* raw measurements */
double black[MX_NRAW];
int i;
nummeas = i1pro3_comp_nummeas(p, meastime, *pinttime);
a1logd(p->log,2,"\nDoing simple measure nummeas %d of int_time %f\n", nummeas, *pinttime);
if ((ev = i1pro3_do_measure(p, i1p3mm_em, &raw_sample, &nummeas, pinttime, NULL, NULL)) != I1PRO3_OK) {
a1logd(p->log,1," i1pro3_do_measure failed\n");
return ev;
}
/* Construct simple black */
i1pro3_comp_simple_emis_black(p, black, raw_sample, nummeas, *pinttime);
/* Subtract black from sample */
i1pro3_multimeas_sub_black(p, raw_sample, nummeas, black);
/* Check if sample is saturated */
if (i1pro3_multimeas_check_sat(p, NULL, raw_sample, nummeas)) {
a1logd(p->log,1," sample is saturated\n");
i1pro3_free_raw(p, i1p3mm_rf_wh, raw_sample, nummeas);
return I1PRO3_RD_SENSORSATURATED;
}
/* Linearize and normalize samples */
i1pro3_multimeas_lin(p, raw_sample, nummeas);
if (donorm)
i1pro3_normalize_rawmmeas(p, raw_sample, nummeas, *pinttime);
/* Return result */
if (praw != NULL)
*praw = raw_sample;
if (pnummeas != NULL)
*pnummeas = nummeas;
return ev;
}
/* Do a simple emission measurement and return the processed wav values */
i1pro3_code i1pro3_spot_simple_emis_meas(
i1pro3 *p,
double **specrd, /* Return array [numpatches][nwav] of spectral reading values */
int numpatches, /* Number of sample to measure */
double *inttime, /* Integration time to use/used */
int hr /* High resolution flag */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
i1pro3_code ev = I1PRO3_OK;
double **raw_sample;
int nummeas = numpatches;
double meastime = nummeas * *inttime;
int i;
if (!s->emiss || s->scan) {
a1logd(p->log,1,"i1pro3_spot_simple_emis_meas in unexpected mode\n");
return I1PRO3_INT_WRONGMODE;
}
if ((ev = i1pro3_spot_simple_emis_raw_meas(p, &raw_sample, &nummeas, inttime, meastime, 1)) != I1PRO3_OK) {
return ev;
}
if (nummeas != numpatches) /* Hmm. */
error("Assert in %s at line %d nummeas %d != numpatches %d\n",__FILE__,__LINE__,nummeas, numpatches);
i1pro3_absraw_to_abswav(p, hr, 2, specrd, raw_sample, nummeas);
i1pro3_free_raw(p, i1p3mm_em, raw_sample, nummeas);
i1pro3_straylight(p, hr, specrd, nummeas);
/* Scale by calibration factor */
for (i = 0; i < nummeas; i++)
vect_mul(specrd[i], s->cal_factor[hr], m->nwav[hr]);
return ev;
}
/* Recognise any flashes in the readings, and */
/* and average their raw values together as well as summing their duration. */
/* Return nz on an error */
i1pro3_code i1pro3_extract_patches_flash(
i1pro3 *p,
double *duration, /* return duration */
double *pavg, /* return patch average [nraw] */
double **multimeas, /* Array of [nummeas][nraw] value to extract from */
int nummeas, /* number of readings made */
double inttime /* Integration time (used to compute duration) */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i, j, k, pix;
double minval, maxval; /* min and max input value at wavelength of maximum input */
double mean; /* Mean of the max wavelength band */
int maxband; /* Band of maximum value */
double thresh; /* Level threshold */
int fsampl; /* Index of the first sample over the threshold */
int nsampl; /* Number of samples over the threshold */
double *aavg; /* ambient average [-1 nraw] */
double finttime; /* Flash integration time */
#ifdef PATREC_DEBUG
double **plot;
#endif
a1logd(p->log,2,"i1pro3_extract_patches_flash looking for flashes in %d measurements\n",nummeas);
/* Discover the maximum input value for flash dection */
maxval = -1e6;
maxband = 0;
for (j = 0; j < m->nraw; j ++) {
for (i = 0; i < nummeas; i++) {
if (multimeas[i][j] > maxval) {
maxval = multimeas[i][j];
maxband = j;
}
}
}
if (maxval <= 0.0) {
a1logd(p->log,2,"No flashes found in measurement\n");
return I1PRO3_RD_NOFLASHES;
}
minval = 1e6;
mean = 0.0;
for (i = 0; i < nummeas; i++) {
mean += multimeas[i][maxband];
if (multimeas[i][maxband] < minval)
minval = multimeas[i][maxband];
}
mean /= (double)nummeas;
/* Set the threshold at 5% from mean towards max */
thresh = (3.0 * mean + maxval)/4.0;
a1logd(p->log,7,"i1pro3_extract_patches_flash band %d minval %f maxval %f, mean = %f, thresh = %f\n",maxband,minval,maxval,mean, thresh);
#ifdef PATREC_DEBUG
/* Plot out 6 lots of 6 values each */
plot = dmatrixz(0, 6, 0, nummeas-1);
for (j = maxband -3; j>= 0 && j < (m->nraw-6); j += 100) /* Do one set around max */
{
for (k = 0; k < 6; k ++) {
for (i = 0; i < nummeas; i++) {
plot[k][i] = multimeas[i][j+k]/maxval;
}
}
for (i = 0; i < nummeas; i++)
plot[6][i] = (double)i;
printf("Bands %d - %d\n",j,j+5);
do_plot6(plot[6], plot[0], plot[1], plot[2], plot[3], plot[4], plot[5], nummeas);
}
free_dmatrix(plot,0,6,0,nummeas-1);
#endif /* PATREC_DEBUG */
#ifdef PATREC_DEBUG
/* Plot just the pulses */
{
int start, end;
plot = dmatrixz(0, 6, 0, nummeas-1);
for(j = 0, start = -1, end = 0;;) {
for (start = -1, i = end; i < nummeas; i++) {
if (multimeas[i][maxband] >= thresh) {
if (start < 0)
start = i;
} else if (start >= 0) {
end = i;
break;
}
}
if (start < 0)
break;
start -= 3;
if (start < 0)
start = 0;
end += 4;
if (end > nummeas)
end = nummeas;
for (i = start; i < end; i++, j++) {
int q;
plot[6][j] = (double)j;
#ifdef NEVER /* Plot +/-3 around maxband */
for (q = 0, k = maxband -3; k < (maxband+3) && k >= 0 && k < m->nraw; k++, q++) {
plot[q][j] = multimeas[i][k]/maxval;
}
#else
/* plot max of bands in 6 segments */
for (q = 0; q < 6; q++) {
int ss, ee;
plot[q][j] = -1e60;
ss = q * (m->nraw/6);
ee = (q+1) * (m->nraw/6);
for (k = ss; k < ee; k++) {
if (multimeas[i][k]/maxval > plot[q][j])
plot[q][j] = multimeas[i][k]/maxval;
}
}
#endif
}
}
do_plot6(plot[6], plot[0], plot[1], plot[2], plot[3], plot[4], plot[5], j);
free_dmatrix(plot,0,6,0,nummeas-1);
}
#endif
/* Locate the first sample over the threshold, and the */
/* total number of samples in the pulses. */
fsampl = -1;
for (nsampl = i = 0; i < nummeas; i++) {
for (j = 0; j < m->nraw; j++) {
if (multimeas[i][j] >= thresh)
break;
}
if (j < m->nraw) {
if (fsampl < 0)
fsampl = i;
nsampl++;
}
}
a1logd(p->log,7,"Number of flash patches = %d\n",nsampl);
if (nsampl == 0)
return I1PRO3_RD_NOFLASHES;
/* See if there are as many samples before the first flash */
if (nsampl < 6)
nsampl = 6;
/* Average nsample samples of ambient */
i = (fsampl-3-nsampl);
if (i < 0)
return I1PRO3_RD_NOAMBB4FLASHES;
a1logd(p->log,7,"Ambient samples %d to %d \n",i,fsampl-3);
aavg = dvectorz(-1, m->nraw-1);
for (nsampl = 0; i < (fsampl-3); i++) {
for (j = 0; j < m->nraw; j++)
aavg[j] += multimeas[i][j];
nsampl++;
}
/* Integrate all the values over the threshold, */
/* and also one either side of flash */
for (j = 0; j < m->nraw; j++)
pavg[j] = 0.0;
for (k = 0, i = 1; i < (nummeas-1); i++) {
int sample = 0;
for (j = 0; j < m->nraw; j++) {
if (multimeas[i-1][j] >= thresh) {
sample = 1;
break;
}
if (multimeas[i][j] >= thresh) {
sample = 1;
break;
}
if (multimeas[i+1][j] >= thresh) {
sample = 1;
break;
}
}
if (j < m->nraw) {
a1logd(p->log,7,"Integrating flash sample no %d \n",i);
for (j = 0; j < m->nraw; j++)
pavg[j] += multimeas[i][j];
k++;
}
}
for (j = 0; j < m->nraw; j++)
pavg[j] = pavg[j]/(double)k - aavg[j]/(double)nsampl;
a1logd(p->log,7,"Number of flash patches integrated = %d\n",k);
finttime = inttime * (double)k;
if (duration != NULL)
*duration = finttime;
/* Convert to cd/m^2 seconds */
for (j = 0; j < m->nraw; j++)
pavg[j] *= finttime;
free_dvector(aavg, -1, m->nraw-1);
return I1PRO3_OK;
}
/* =======================================================================*/
/* Lower level reading processing */
/* =======================================================================*/
/* Check a raw black for sanity */
/* Returns nz if not sane */
int i1pro3_multimeas_check_black(
i1pro3 *p,
double **rawmmeas, /* Array of [nummeas][-1 nraw] values to check */
int nummeas,
double inttime
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i, j;
double avg = 0.0; /* Average raw value */
double avgd = 0.0; /* Average dummy value */
double limit;
if (rawmmeas == NULL)
return 0;
for (i = 0; i < nummeas; i++) {
avgd += rawmmeas[i][-1];
for (j = 0; j < m->nraw; j++) {
avg += rawmmeas[i][j];
}
}
avgd /= (double)nummeas;
avg /= (double)nummeas * m->nraw;
limit = avgd + m->ee_bk_f_limit + m->ee_bk_v_limit * inttime;
a1logd(p->log,4,"i1pro3_multimeas_check_black %d meas: avgd %f avg %f limit %f\n",nummeas,avgd,avg,limit);
if (avg >= limit)
return 1;
return 0;
}
/* Subtract black from a raw unscaled multimeas */
void i1pro3_multimeas_sub_black(
i1pro3 *p,
double **rawmmeas, /* Array of [nummeas][nraw] values to subtract from */
int nummeas,
double *black /* Black [nraw] to subtract */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i;
for (i = 0; i < nummeas; i++)
vect_sub(rawmmeas[i], black, m->nraw);
}
/* Check for saturation of a raw unscaled multimeas */
/* This should be after black subtraction but before linearization. */
/* Returns nz if saturated */
int i1pro3_multimeas_check_sat(
i1pro3 *p,
double *pmaxval, /* If not NULL, return the maximum value */
double **rawmmeas, /* Array of [nummeas][nraw] values to check */
int nummeas
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i, j;
double maxval = -1e9;
int satcount = 0;
for (i = 0; i < nummeas; i++) {
for (j = 0; j < m->nraw; j++) {
if (rawmmeas[i][j] > maxval)
maxval = rawmmeas[i][j];
if (rawmmeas[i][j] > m->sens_sat)
satcount++;
}
}
a1logd(p->log,6,"i1pro3_multimeas_check_sat: maxval %f satcount %d\n",maxval,satcount);
if (pmaxval != NULL)
*pmaxval = maxval;
/* Count of 10 is hard coded */
if (satcount > (10 * nummeas))
return 1;
return 0;
}
/* Check for consistency of a raw unscaled multimeas */
/* (Don't use on black subtracted black measurement.) */
/* Returns nz if inconsistent */
int i1pro3_multimeas_check_raw_consistency(
i1pro3 *p,
double **rawmmeas, /* Array of [nummeas][nraw] values to check */
int nummeas
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i;
double *avgs, aavg, aavgd, maxdev;
int maxi;
double limit = PATCH_CONS_THR * m->scan_toll_ratio;
int rv = 0;
/* Compute the average raw value of each measurement */
avgs = dvector(0, nummeas-1);
for (i = 0; i < nummeas; i++)
avgs[i] = vect_avg(rawmmeas[i], m->nraw);
/* Average of averages */
aavgd = aavg = vect_avg(avgs, nummeas);
/* Can't expect consistency for very low levels due to noise */
if (aavgd < 40.0)
aavgd = 40.0;
maxdev = 0;
for (i = 0; i < nummeas; i++) {
double dev = fabs(avgs[i] - aavg)/aavgd;
if (dev > maxdev) {
maxdev = dev;
maxi = i;
}
}
if (maxdev > limit) {
a1logd(p->log,1,"i1pro3_multimeas_check_raw_consistency aavg %f aavg %f maxdev %f > %f\n",aavg,aavg,maxdev,limit);
rv = 1;
} else {
a1logd(p->log,5,"i1pro3_multimeas_check_raw_consistency aavg %f aavg %f maxdev %f limit %f\n",aavg,aavg,maxdev,limit);
}
free_dvector(avgs, 0, nummeas-1);
return rv;
}
/* Check for consistency of a raw unscaled multimeas with UV muxed values */
/* Returns nz if inconsistent */
int i1pro3_multimeas_check_raw_consistency_x(
i1pro3 *p,
double **rawmmeas, /* Array of [nummeas][nraw] values to check */
int nummeas
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i;
double eavg, oavg, hdif;
int neavg, noavg;
double *avgs, aavg, aavgd, maxdev;
int maxi;
double limit = PATCH_CONS_THR * m->scan_toll_ratio;
int rv = 0;
/* Compute the average raw value of each measurement */
avgs = dvector(0, nummeas-1);
for (i = 0; i < nummeas; i++)
avgs[i] = vect_avg(rawmmeas[i], m->nraw);
/* Compute even/odd averages */
eavg = oavg = 0.0;
neavg = noavg = 0;
for (i = 0; i < nummeas; i++) {
if ((i & 1) == 0) {
eavg += avgs[i];
neavg++;
} else {
oavg += avgs[i];
noavg++;
}
}
if (neavg == 0 || noavg == 0) {
a1logd(p->log,1,"i1pro3_multimeas_check_raw_consistency_x: too few patches (%d, %d)\n",neavg, noavg);
free_dvector(avgs, 0, nummeas-1);
return 1;
}
eavg /= (double)neavg;
oavg /= (double)noavg;
hdif = 0.5 * (eavg - oavg); /* Half even above odd */
/* Correct averages for average even/odd difference */
for (i = 0; i < nummeas; i++) {
if ((i & 1) == 0)
avgs[i] -= hdif;
else
avgs[i] += hdif;
}
/* Average of averages */
aavgd = aavg = vect_avg(avgs, nummeas);
/* Can't expect consistency for very low levels due to noise */
if (aavgd < 40.0)
aavgd = 40.0;
/* Maximum difference from average */
maxdev = 0;
for (i = 0; i < nummeas; i++) {
double dev = fabs(avgs[i] - aavg)/aavgd;
if (dev > maxdev) {
maxdev = dev;
maxi = i;
}
}
if (maxdev > limit) {
a1logd(p->log,1,"i1pro3_multimeas_check_raw_consistency_x aavg %f aavg %f maxdev %f > %f\n",aavg,aavg,maxdev,limit);
rv = 1;
} else {
a1logd(p->log,5,"i1pro3_multimeas_check_raw_consistency_x aavg %f aavg %f maxdev %f limit %f\n",aavg,aavg,maxdev,limit);
}
free_dvector(avgs, 0, nummeas-1);
return rv;
}
/* Check for consistency of wav values. */
/* Returns nz if inconsistent */
int i1pro3_multimeas_check_wav_consistency(
i1pro3 *p,
int highres, /* 0 for std res, 1 for high res */
double inttime,
double **wav, /* Array of [nwav1m][nwav] values to check */
int nummeas
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i;
double *avgs, aavg, aavgd, maxdev;
int nwav = m->nwav[highres];
int maxi;
double limit = PATCH_CONS_THR * m->scan_toll_ratio;
int rv = 0;
/* Compute the average wav scale wav value of each measurement */
avgs = dvector(0, nummeas-1);
for (i = 0; i < nummeas; i++)
avgs[i] = vect_avg(wav[i], nwav) * inttime;
/* Average of averages */
aavgd = aavg = vect_avg(avgs, nummeas);
/* Can't expect consistency for very low levels due to noise */
if (aavgd < 40.0)
aavgd = 40.0;
maxdev = 0;
for (i = 0; i < nummeas; i++) {
double dev = fabs(avgs[i] - aavg)/aavgd;
if (dev > maxdev) {
maxdev = dev;
maxi = i;
}
}
if (maxdev > limit) {
a1logd(p->log,1,"wav_consistency2 aavg %f aavg %f maxdev %f > %f\n",aavg,aavg,maxdev,limit);
rv = 1;
} else {
a1logd(p->log,6,"wav_consistency2 aavg %f aavg %f maxdev %f limit %f\n",aavg,aavg,maxdev,limit);
}
free_dvector(avgs, 0, nummeas-1);
return rv;
}
/* Check for consistency of a even/odd wav values. */
/* Returns nz if inconsistent */
int i1pro3_multimeas_check_wav_consistency2(
i1pro3 *p,
int highres, /* 0 for std res, 1 for high res */
double inttime,
double **wav1, /* Array of [nwav1m][nwav] values to check */
int nwav1m,
double **wav2, /* Array of [nwav2m][nwav] values to check */
int nwav2m
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i;
double eavg, oavg, hdif;
double *avgs, aavg, aavgd, maxdev;
int nwav = m->nwav[highres];
int nummeas = nwav1m + nwav2m;
int maxi;
double limit = PATCH_CONS_THR * m->scan_toll_ratio;
int rv = 0;
if (nwav1m == 0 || nwav2m == 0) {
a1logd(p->log,1,"i1pro3_multimeas_check_wav_consistency2: too few patches (%d, %d)\n",nwav1m, nwav2m);
return 1;
}
/* Compute the average raw scale wav value of each measurement */
avgs = dvector(0, nummeas-1);
for (i = 0; i < nwav1m; i++)
avgs[i] = vect_avg(wav1[i], nwav) * inttime;
for (i = 0; i < nwav2m; i++)
avgs[nwav1m + i] = vect_avg(wav2[i], nwav) * inttime;
/* Compute even/odd averages */
eavg = oavg = 0.0;
for (i = 0; i < nwav1m; i++)
eavg += avgs[i];
for (i = 0; i < nwav2m; i++)
oavg += avgs[nwav1m + i];
eavg /= (double)nwav1m;
oavg /= (double)nwav2m;
hdif = 0.5 * (eavg - oavg); /* Half even above odd */
/* Correct averages for average even/odd difference */
for (i = 0; i < nwav1m; i++)
avgs[i] -= hdif;
for (i = 0; i < nwav2m; i++)
avgs[nwav1m + i] += hdif;
/* Average of averages */
aavgd = aavg = vect_avg(avgs, nummeas);
/* Can't expect consistency for very low levels due to noise */
if (aavgd < 40.0)
aavgd = 40.0;
maxdev = 0;
for (i = 0; i < nummeas; i++) {
double dev = fabs(avgs[i] - aavg)/aavgd;
if (dev > maxdev) {
maxdev = dev;
maxi = i;
}
}
if (maxdev > limit) {
a1logd(p->log,1,"wav_consistency2 aavg %f aavg %f maxdev %f > %f\n",aavg,aavg,maxdev,limit);
rv = 1;
} else {
a1logd(p->log,6,"wav_consistency2 aavg %f aavg %f maxdev %f limit %f\n",aavg,aavg,maxdev,limit);
}
free_dvector(avgs, 0, nummeas-1);
return rv;
}
/* Take a set of raw, black subtracted, unscaled values */
/* and linearize them. */
void i1pro3_vect_lin(
i1pro3 *p,
double *raw
) {
i1pro3imp *m = (i1pro3imp *)p->m;
double *polys = m->ee_lin;
int j;
for (j = 0; j < m->nraw; j++) {
double fval, lval;
fval = raw[j];
lval = ((polys[0] * fval + polys[1]) * fval + polys[2]) * fval + polys[3];
raw[j] = lval;
}
}
/* Take a set of raw, black subtracted, unscaled multimeas values */
/* and linearize them. */
void i1pro3_multimeas_lin(
i1pro3 *p,
double **rawmmeas, /* Array of [nummeas][nraw] values to linearize */
int nummeas
) {
i1pro3imp *m = (i1pro3imp *)p->m;
double *polys = m->ee_lin;
int i, j;
for (i = 0; i < nummeas; i++) {
for (j = 0; j < m->nraw; j++) {
double fval, lval;
fval = rawmmeas[i][j];
lval = ((polys[0] * fval + polys[1]) * fval + polys[2]) * fval + polys[3];
rawmmeas[i][j] = lval;
}
}
}
/* Normalize a set of raw measurements by dividing by integration time */
void i1pro3_normalize_rawmmeas(
i1pro3 *p,
double **rawmmeas, /* raw measurements to correct array [nummeas][nraw] */
int nummeas, /* Number of measurements */
double int_time /* Integration time */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i;
double dd = 1.0/int_time;
/* For each measurement */
for (i = 0; i < nummeas; i++)
vect_scale1(rawmmeas[i], dd, m->nraw);
}
/* Average a raw multimeas into a single vector */
/* NOTE averages auxv value too! */
void i1pro3_average_rawmmeas(
i1pro3 *p,
double *avg, /* return average [-1 nraw] */
double **rawmmeas, /* Array of [nummeas][-1 nraw] value to average */
int nummeas
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i;
vect_set(avg-1, 0.0, m->nraw+1);
for (i = 0; i < nummeas; i++)
vect_add(avg-1, rawmmeas[i]-1, m->nraw+1);
vect_scale1(avg-1, 1.0/nummeas, m->nraw+1);
}
/* Average two raw multimeas into a single vector */
void i1pro3_average_rawmmeas_2(
i1pro3 *p,
double *avg, /* return average [-1 nraw] */
double **rawmmeas1, /* Array of [nummeas][-1 nraw] value to average */
int nummeas1,
double **rawmmeas2, /* Array of [nummeas][-1 nraw] value to average */
int nummeas2
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i;
vect_set(avg-1, 0.0, m->nraw+1);
for (i = 0; i < nummeas1; i++)
vect_add(avg-1, rawmmeas1[i]-1, m->nraw+1);
for (i = 0; i < nummeas2; i++)
vect_add(avg-1, rawmmeas2[i]-1, m->nraw+1);
vect_scale1(avg-1, 1.0/(nummeas1 + nummeas2), m->nraw+1);
}
/* Average a raw multimeas into odd & even single vectors */
/* (Doesn't average aux values) */
void i1pro3_average_eorawmmeas(
i1pro3 *p,
double avg[2][128], /* return average [2][nraw] */
double **rawmmeas, /* Array of [nummeas][nraw] raw values to average */
int nummeas /* (must be even) */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i;
if (nummeas & 1)
error("i1pro3_average_eorawmmeas: odd nummeas in %s line %d\n",__FILE__,__LINE__);
vect_set(avg[0], 0.0, m->nraw);
vect_set(avg[1], 0.0, m->nraw);
for (i = 0; i < nummeas; i += 2) {
vect_add(avg[0], rawmmeas[i + 0], m->nraw);
vect_add(avg[1], rawmmeas[i + 1], m->nraw);
}
vect_scale1(avg[0], 0.5/nummeas, m->nraw);
vect_scale1(avg[1], 0.5/nummeas, m->nraw);
}
/* Average a wav multimeas into a single vector */
void i1pro3_average_wavmmeas(
i1pro3 *p,
double *avg, /* return average [nwav[]] */
double **wavmmeas, /* Array of [nummeas][nwav[]] raw values to average */
int nummeas,
int hr /* 0 if standard res., 1 if hires */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i;
vect_set(avg, 0.0, m->nwav[hr]);
for (i = 0; i < nummeas; i++)
vect_add(avg, wavmmeas[i], m->nwav[hr]);
vect_scale1(avg, 1.0/nummeas, m->nwav[hr]);
}
/* Convert an absraw array from raw wavelengths to output wavelenths */
/* for a given [std res, high res] and [emis/tras, reflective] mode */
/* (Copies aux data from raw to wav if refl >= 0) */
void i1pro3_absraw_to_abswav(
i1pro3 *p,
int hr, /* 0 for std res, 1 for high res */
int refl, /* 0 for emis/trans, 1 for reflective, 2 or 3 for no aux copy. */
double **abswav, /* Desination array [nummeas] [-x nwav[]] */
double **absraw, /* Source array [nummeas] [-x nraw] */
int nummeas /* Number of measurements */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i, j, k, cx, sx;
int refix = refl & 1;
/* For each measurement */
for (i = 0; i < nummeas; i++) {
/* For each output wavelength */
for (cx = j = 0; j < m->nwav[hr]; j++) {
double oval = 0.0;
/* For each matrix value */
sx = m->mtx[hr][refix].index[j]; /* Starting index */
for (k = 0; k < m->mtx[hr][refix].nocoef[j]; k++, cx++, sx++) {
oval += m->mtx[hr][refix].coef[cx] * absraw[i][sx];
}
abswav[i][j] = oval;
}
/* Copy auxiliary data */
if ((refl & 2) == 0) {
if (refix)
vect_cpy(&abswav[i][-9], &absraw[i][-9], 9);
else
vect_cpy(&abswav[i][-1], &absraw[i][-1], 1);
}
}
}
/* Apply stray light correction to a wavmmeas */
void i1pro3_straylight(
i1pro3 *p,
int hr, /* 0 for std res, 1 for high res */
double **wavmmeas, /* wav measurements to correct array [nummeas][nwav[]] */
int nummeas /* Number of measurements */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i, j, k;
double *tm; /* Temporary array */
tm = dvector(0, m->nwav[hr]-1);
/* For each measurement */
for (i = 0; i < nummeas; i++) {
vect_cpy(tm, wavmmeas[i], m->nwav[hr]);
for (j = 0; j < m->nwav[hr]; j++) {
double oval = 0.0;
/* For each matrix value */
for (k = 0; k < m->nwav[hr]; k++)
oval += m->straylight[hr][j][k] * tm[k];
wavmmeas[i][j] = oval;
}
}
free_dvector(tm, 0, m->nwav[hr]-1);
}
/* Take a set of odd/even raw or wav measurements and reshuffle them into */
/* first half block and second half block. */
/* Return nz if malloc failed */
int i1pro3_unshuffle(
i1pro3 *p,
double **mmeas, /* Array of [nummeas][xxx] values to unshuffle */
int nummeas /* Must be even */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int s, d;
double **sp;
if (nummeas <= 2)
return 0; /* Nothing to do */
if (nummeas & 1)
error("i1pro3_unshuffle: odd nummeas in %s line %d\n",__FILE__,__LINE__);
/* We take advantage of our matrix construction to swap pointers. */
if ((sp = (double **)malloc(sizeof(double *) * nummeas)) == NULL) {
a1logd(p->log,1,"i1pro3_unshuffle malloc %ld bytes failed\n",sizeof(double *) * nummeas);
return 1;
}
memmove((char *)sp, (char *)mmeas, sizeof(double *) * nummeas);
for (s = 0; s < nummeas; s++) {
d = (s/2) + ((s & 1) != 0 ? nummeas/2 : 0); /* Slot to put s in */
mmeas[d] = sp[s];
}
free(sp);
return 0;
}
/* Compute i1pro3 calibration LED model given auxiliary information */
/* from the spectral and other sensors. */
/* We always use std res. for led modelling. */
void i1pro3_comp_ledm(
i1pro3 *p,
double **wavmmeas_ledm, /* Array of [nummeas][36] values to return */
double **wavmmeas_aux, /* Array of [nummeas][-9] aux values to use */
int nummeas, /* Number of measurements */
int uv /* 0 for non-uv, 1 for uv-illuminated LED model */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int i, j, k;
for (i = 0; i < nummeas; i++) {
for (j = 0; j < 36; j++) {
wavmmeas_ledm[i][j] = 0.0;
/* Each band result is the weighted sum of */
/* a polynomial model driven by each auxililay value */
for (k = 0; k < 8; k++) {
double auxv = wavmmeas_aux[i][-9 + k];
double val;
if (k < 2) {
val = m->ledm_poly[uv][k][0][j]
+ auxv * m->ledm_poly[uv][k][1][j]
+ auxv * auxv * m->ledm_poly[uv][k][2][j];
val *= m->ledm_poly[uv][k][3][j];
} else {
val = m->ledm_poly[uv][k][0][j]
+ auxv * m->ledm_poly[uv][k][1][j];
val *= m->ledm_poly[uv][k][2][j];
}
wavmmeas_ledm[i][j] += val;
}
}
}
}
/* Take a set of odd/even raw measurements, convert to approx. reflectance */
/* and filter out any UV multiplexing noise using a windowed comb filter. */
#define CWIDTH 3 /* [3] +/- comb width */
#undef DEBUG_UVFILT
void i1pro3_filter_uvmux(
i1pro3 *p,
double **raw, /* Array of [nummeas][-9, nraw] values to filter */
int nummeas, /* Must be even */
int hr
) {
i1pro3imp *m = (i1pro3imp *)p->m;
i1pro3_state *s = &m->ms[m->mmode];
double *tres; /* Temporary results */
#ifdef DEBUG_UVFILT
double avgs[2][128]; /* Debug plot */
#endif
int i, j;
a1logd(p->log,4,"i1pro3_filter_uvmux called with %d samples\n",nummeas);
if (nummeas < 2) /* Hmm. */
return;
/* Convert to approximate reflection values */
for (i = 0; i < nummeas; i += 2) {
vect_div(raw[i + 0], s->calraw_white[0], m->nraw);
vect_div(raw[i + 1], s->calraw_white[1], m->nraw);
}
#ifdef DEBUG_UVFILT
printf("Unfiltered average even, odd:\n");
i1pro3_average_eorawmmeas(p, avgs, raw, nummeas);
plot_raw2(avgs[0], avgs[1]);
#endif
/* Do the filtering */
tres = dvector(0, nummeas-1);
if (nummeas >= (2 * CWIDTH + 1)) { /* If comb filter will fit */
for (j = 60; j < m->nraw; j++) { /* For needed bands */
for (i = 0; i < nummeas; i ++) { /* Filter each entry in band */
double eavg = 0.0, oavg = 0.0;
int neavg = 0, noavg = 0;
int ii = i - CWIDTH; /* Start */
int jj = i + CWIDTH + 1; /* End + 1 */
int kk;
if (ii < 0) /* Clip comb */
ii = 0;
if (jj > nummeas)
jj = nummeas;
for (kk = ii; kk < jj; kk++) { /* Compute window even/odd averages */
if ((kk & 1) == 0) {
eavg += raw[kk][j];
neavg++;
} else {
oavg += raw[kk][j];
noavg++;
}
}
eavg /= (double)neavg;
oavg /= (double)noavg;
if ((i & 1) == 0) /* Subtract even/odd difference */
tres[i] = raw[i][j] - 0.0 * (eavg - oavg);
else
tres[i] = raw[i][j] - 1.0 * (oavg - eavg);
}
for (i = 0; i < nummeas; i++) /* Transfer result */
raw[i][j] = tres[i];
}
}
free_dvector(tres, 0, nummeas-1);
#ifdef DEBUG_UVFILT
printf("Filtered average even, odd:\n");
i1pro3_average_eorawmmeas(p, avgs, raw, nummeas);
plot_raw2(avgs[0], avgs[1]);
#endif
}
#undef CWIDTH
/* ----------------------------------------------------------------- */
/* Zebra ruler read handler. */
/* Caller will have setup p->zebra_* values.. */
static int i1pro3_zebra_thread(void *pp) {
i1pro3 *p = (i1pro3 *)pp;
i1pro3imp *m = (i1pro3imp *)p->m;
/* We need to start zebra read after the measurement read, or it */
/* won't happen at all.. */
#ifdef USE_RD_SYNC
a1logd(p->log,7,"\ni1pro3_zebra_thread: waiting for measure sync2 0x%x 0x%x\n",&m->rd_sync2,m->rd_sync2.condx);
p->icom->usb_wait_io(p->icom, &m->rd_sync2); /* Wait for meas read to start */
a1logd(p->log,7,"i1pro3_zebra_thread: got measure sync\n");
#else
msec_sleep(m->trig_delay/2);
#endif
m->zebra_rv = i1pro3_gatherzebra(p, m->zebra_buf, m->zebra_bsize, &m->zebra_bread);
return 0;
}
/* Process a buffer of raw zebra values. */
static i1pro3_code i1pro3_zebra_proc(
i1pro3 *p,
int ***p_p2m, /* If valid, return list of sample indexes in even position order */
int *p_npos, /* Number of position slots */
unsigned char *zeb, /* Array [numzeb] bytes of zebra ruler samples (0,1,2,3) values */
int numzeb, /* Number of zebra ruler samples/bytes.*/
double inttime, /* Corresponding measurement integration time */
int nummeas /* Number of measures */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int dval[16] = { /* Convert sensor change into delta value */
/* 0, 0 */ 0, /* Either change in count or 9 for illegal */
/* 0, 1 */ -1,
/* 0, 2 */ +1,
/* 0, 3 */ 9,
/* 1, 0 */ +1,
/* 1, 1 */ 0,
/* 1, 2 */ 9,
/* 1, 3 */ -1,
/* 2, 0 */ -1,
/* 2, 1 */ 9,
/* 2, 2 */ 0,
/* 2, 3 */ +1,
/* 3, 0 */ 9,
/* 3, 1 */ +1,
/* 3, 2 */ -1,
/* 3, 3 */ 0
};
int ntrans, nilleg;
double transpsec;
int count, mincount, maxcount;
double maxdist;
double *t2p_t; /* Time to position, time [ntrans] */
double *t2p_p; /* Time to position, position [ntrans] */
#define POSRES 0.3 /* [0.2] Position slot width in mm */
int **p2m; /* Array of pointers to sample lists */
/* list[-2] = index allocation length (ie. not counting first 2 values) */
/* list[-1] = number of indexes length */
/* list[0] = First measurement index */
/* list[0+n] = n'th measurement index */
int npos;
int i;
a1logd(p->log,6,"\ni1pro3_zebra_proc: processing %d zebra values\n",numzeb);
/* First pass to count transitions and check validity */
count = ntrans = nilleg = 0;
mincount = maxcount = 0;
for (i = 0; i < (numzeb-1); i++) {
int trv = (zeb[i] << 2) | zeb[i+1];
int vv = dval[trv];
if (vv == 9)
nilleg++;
else if (vv != 0) {
ntrans++;
count += vv;
if (count < mincount)
mincount = count;
if (count > maxcount)
maxcount = count;
}
}
/* Assume that if there is less than 1 transition per second, that it is invalid */
transpsec = ntrans/(numzeb * m->intclkp);
a1logd(p->log,6,"i1pro3_zebra_proc: nilleg %d ntrans %d transpsec %f\n",nilleg,ntrans,transpsec);
if (nilleg != 0 || ntrans < 10 || transpsec < 1.0) {
a1logd(p->log,2,"i1pro3_zebra_proc: invalid zebra ruler read\n");
if (p_p2m != NULL)
*p_p2m = NULL;
if (p_npos != NULL)
*p_npos = 0;
return I1PRO3_OK;
}
a1logd(p->log,6,"i1pro3_zebra_proc: mincount %d maxcount %d\n",mincount,maxcount);
t2p_t = dvector(0, ntrans); /* Time to position mapping, time values */
t2p_p = dvector(0, ntrans); /* Time to position mapping, position */
/* Convert so that overall +ve distance is in the direction */
/* first moved, and that the distances are all +ve */
t2p_t[0] = 0.0;
t2p_p[0] = -mincount * 0.5;
count = ntrans = 0;
for (i = 0; i < (numzeb-1); i++) {
int trv = (zeb[i] << 2) | zeb[i+1];
int vv = dval[trv];
if (vv == 0 || vv == 9)
continue;
ntrans++;
count += vv;
t2p_t[ntrans] = i * m->intclkp; /* Time */
t2p_p[ntrans] = (count - mincount) * 0.5; /* Position in mm */
}
ntrans++;
maxdist = (maxcount - mincount) * 0.5;
if ((t2p_p[9] - t2p_p[0]) < 0.0) { /* If we started moving backwards */
for (i = 0; i < ntrans; i++)
t2p_p[i] = maxdist - t2p_p[i]; /* Make distances +ve */
}
a1logd(p->log,6,"i1pro3_zebra_proc: maxdist %f\n",maxdist);
if (p->log->debug >= 6) {
a1logd(p->log,6,"t2p ix, position, time:\n");
for (i = 0; i < ntrans; i++)
a1logd(p->log,6," %d %f %f\n",i,t2p_t[i],t2p_p[i]);
}
#ifdef PATREC_DEBUG
printf("Zebra time vs. position:\n");
do_plot(t2p_t, t2p_p, NULL, NULL, ntrans);
#endif // NEVER
/* Create position to time mapping. We create a list in */
/* increments of 0.2 mm and then add measurement samples */
/* that fall into each slot. */
npos = (int)floor(maxdist/POSRES + 1.5);
if ((p2m = (int **)malloc(sizeof(int *) * npos)) == NULL) {
a1logd(p->log,1,"i1pro3_zebra_proc malloc %d bytes failed\n",sizeof(int *) * npos);
return I1PRO3_INT_MALLOC;
}
/* Allocate enough space for 10 measurements per 0.5 mm */
for (i = 0; i < npos; i++) {
int *ary;
if ((ary = (int *)malloc(sizeof(int) * (2 + 10))) == NULL) {
a1logd(p->log,1,"i1pro3_zebra_proc malloc %d bytes failed\n",sizeof(int) * 12);
return I1PRO3_INT_MALLOC;
}
p2m[i] = ary+2;
p2m[i][-2] = 10;
p2m[i][-1] = 0;
}
/* For each measurement sample, compute center time, */
/* then interpolate distance */
for (i = 0; i < nummeas; i++) {
double tm;
double ps; /* Position in mm */
int slot;
tm = (i + 0.5) * inttime; /* Time at center of integration period */
ps = vect_lerp2(t2p_t, t2p_p, tm, ntrans);
slot = (int)floor(ps/POSRES + 0.5);
/* In case time period covered by zebra is different to time period of measurement... */
if (slot < 0)
slot = 0;
else if (slot >= npos)
slot = npos-1;
//fprintf(stderr,"Sample %d time %f position %f slot %d\n",i,tm,ps,slot);
if (p2m[slot][-1] >= p2m[slot][-2]) {
int *ary;
int size = 2 * p2m[slot][-2];
if ((ary = (int *)realloc(p2m[slot]-2, sizeof(int) * (2 + size))) == NULL) {
a1logd(p->log,1,"1pro3_zebra_proc realloc %d bytes failed\n",sizeof(int) * (2 + size));
return I1PRO3_INT_MALLOC;
}
p2m[slot] = ary+2;
p2m[slot][-2] = size;
}
p2m[slot][p2m[slot][-1]++] = i; /* Add to slot list */
}
free_dvector(t2p_t, 0, ntrans);
free_dvector(t2p_p, 0, ntrans);
/* Dump slot info */
if (p->log->debug >= 9) {
a1logd(p->log,9,"Patch zebra slot info:\n");
for (i = 0; i < npos; i++) {
int j;
a1logd(p->log,9," %d:",i);
for (j = 0; j < p2m[i][-1]; j++)
a1logd(p->log,9," %d",p2m[i][j]);
a1logd(p->log,9,"\n");
}
}
if (p_p2m != NULL)
*p_p2m = p2m;
if (p_npos != NULL)
*p_npos = npos;
return I1PRO3_OK;
}
#undef POSRES
/* Free up a p2m list returned by i1pro3_zebra_proc() */
void del_zebix_list(int **p2m, int npos) {
int i;
for (i = 0; i < npos; i++) {
if (p2m[i] != NULL)
free(p2m[i] - 2);
}
free(p2m);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Take a measurement and return floating point raw values. */
/* This is core measurement function */
i1pro3_code i1pro3_do_measure(
i1pro3 *p,
i1p3_mmode mm, /* Measurement mode */
double ***praw, /* Return array [nummeas][-1, nraw] for emissive, */
/* Return array [nummeas][-9, nraw] for reflective, */
int *pnummeas, /* Number of measurements to make/returned, 0 for scan. */
double *pinttime, /* Integration time to use/used */
int ***p_p2m, /* If valid, return zeb list of sample indexes in mm. order */
int *p_npos /* Number of position slots */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
int nummeas = *pnummeas; /* Requested number of measurements */
double inttime = *pinttime; /* Requested integration time */
int intclocks = 0; /* Integration clocks for emissive */
int tint = 1; /* tint for reflective */
int nnmeas; /* Number of native measurements */
int nnskip = 0; /* Number of native measurements to skip */
int nsen; /* Number of 16 bit sample per native measurement */
unsigned char *buf = NULL; /* Where to read data to */
int bsize; /* Bytes available in buffer */
unsigned char *zbuf = NULL; /* Where to read zebra ruler data to */
int zbsize; /* Bytes read into zebra ruler buffer */
athread *zebra_th = NULL; /* Zebra ruler read thread */
int refmode;
int scanmode = 0;
int flags = 0; /* Command flags */
int anummeas = 0; /* Actual number of native measurements taken */
double **raw;
unsigned char *zeb; /* Zebra ruler values to return */
int numzeb; /* Number of zebra ruler samples */
unsigned int errc;
int i, j;
i1pro3_code ev = I1PRO3_OK;
a1logd(p->log,4,"\ni1pro3_do_measure: mm %d nummeas %d inttime %f\n",mm,*pnummeas,*pinttime);
#ifndef ENABLE_ZEBRA
p_p2m = NULL;
#endif
if (inttime < m->min_int_time)
inttime = m->min_int_time;
/* Reflective mode */
if (mm & i1p3cm_refl) {
refmode = 1;
nsen = m->nsen2;
/* Compute integration multiplier */
/* Valid values are 1, 2, 4, 6, 8, etc. */
tint = (int)floor(inttime / m->min_int_time + 0.5);
if (tint > 1)
tint = 2 * (int)floor(inttime / (2.0 * m->min_int_time) + 0.5);
else if (tint < 1)
tint = 1;
inttime = m->min_int_time * tint;
intclocks = (int)floor(inttime/m->intclkp + 0.5);
a1logd(p->log,4,"i1pro3_do_measure: refl, tint %d inttime %f\n",tint,inttime);
if (tint == 1 && (nummeas & 1) != 0) {
error("i1pro3_do_measure: nummeas must be even (tint %d nummeas %d)",tint,nummeas);
}
if ((ev = i1pro3_settintmult(p, tint)) != I1PRO3_OK)
error("i1pro3_settintmult failed with %d\n",ev);
/* set the illuminating LED currents */
if (mm & i1p3cm_ill) {
int led_luv_cur = m->ee_led_luv_cur;
int led_suv_cur = m->ee_led_suv_cur;
if (mm == i1p3mm_rf_whl) /* No long UV */
led_luv_cur = 0;
if (mm == i1p3mm_rf_whs) /* No short UV */
led_suv_cur = 0;
if (mm == i1p3mm_rf_whp) { /* Polarized UV levels */
if (m->ee_version >= 1) {
led_luv_cur = m->ee1_pol_led_luv_cur;
led_suv_cur = m->ee1_pol_led_suv_cur;
}
}
a1logd(p->log,4,"i1pro3_do_measure: led currents %d %d %d %d %d\n",
m->ee_led_w_cur, m->ee_led_b_cur, led_luv_cur, led_suv_cur, m->ee_led_gwl_cur);
if ((ev = i1pro3_setledcurrents(p,
m->ee_led_w_cur, m->ee_led_b_cur, led_luv_cur, led_suv_cur, m->ee_led_gwl_cur
)) != I1PRO3_OK)
return ev;
nnskip = LEDTURNONMEAS; /* Hard coded LED turn on delay in native meas count */
/* Quantize up when tint */
if (tint > 1)
nnskip = (tint/2) * ((nnskip + (tint/2)-1) / (tint/2));
a1logd(p->log,4,"i1pro3_do_measure: nnskip %d\n",nnskip);
}
if (mm == i1p3mm_rf_whp) { /* Polarized ILL flag */
flags |= I1PRO3_RMF_CILL;
} else if (mm & i1p3cm_ill)
flags |= I1PRO3_RMF_ILL;
if (p_p2m != NULL)
flags |= I1PRO3_RMF_ZEBRA;
/* Emissive mode */
} else {
refmode = 0;
nsen = m->nsen1;
intclocks = (int)floor(inttime/m->intclkp + 0.5);
inttime = intclocks * m->intclkp;
if (mm == i1p3mm_em_wl)
flags |= I1PRO3_EMF_WL;
a1logd(p->log,4,"i1pro3_do_measure: emis, inttime %f\n",inttime);
if (p_p2m != NULL)
flags |= I1PRO3_EMF_ZEBRA;
}
/* Allow MAXSCANTIME for a scan */
if (nummeas == 0) {
int delay = TRIG_DELAY; /* Scan start reaction time in msec */
int dmeas, omeas; /* Delay and On LED indicator params */
scanmode = 1;
nummeas = (int)floor(MAXSCANTIME/inttime + 0.5);
if (nummeas < 1)
nummeas = 1;
/* Compute reaction time from trigger_measure() to actial measurement */
if (mm & i1p3cm_ill) {
delay += ceil(1000.0 * nnskip * 2.0 * m->min_int_time);
}
a1logd(p->log,4,"i1pro3_do_measure: trig delay %d msec, reaction delay %d msec\n",
TRIG_DELAY, delay);
/* Indicate to the user with a (typically) audible indication. */
if (p->eventcallback != NULL) {
issue_scan_ready((inst *)p, delay);
} else {
msec_beep(delay, 1000, 200); /* delay then 1KHz for 200 msec */
}
/* Also use built in LED indicators */
dmeas = (int)ceil(0.001 * delay/(2.0 * m->min_int_time)); /* Reaction delay */
omeas = dmeas + (int)ceil(0.2/(2.0 * m->min_int_time)); /* 0.2 sec LED on time */
if ((ev = i1pro3_setscanstartind(p, dmeas, omeas)) != I1PRO3_OK)
return ev;
a1logd(p->log,4,"i1pro3_do_measure: LED ind. dmeas %d omeas %d\n",dmeas, omeas);
} else { /* Disable the measure indicator */
if ((ev = i1pro3_setscanstartind(p, 0, 0)) != I1PRO3_OK)
return ev;
}
/* Number of native measurements */
if (refmode) {
/* Two measures per native measure if tint == 1 */
nnmeas = nummeas * tint / 2;
nnmeas += nnskip;
} else {
nnmeas = nummeas;
}
/* Allocate the byte buffer space */
bsize = 2 * nsen * nnmeas ;
a1logd(p->log,4,"i1pro3_do_measure: nummeas %d nnmeas %d bsize %d\n",nummeas,nnmeas,bsize);
if ((buf = (unsigned char *)malloc(sizeof(unsigned char) * bsize)) == NULL) {
a1logd(p->log,1,"i1pro3_do_measure malloc %d bytes failed\n",bsize);
return I1PRO3_INT_MALLOC;
}
if (p_p2m != NULL) {
int mxnzsamp;
mxnzsamp = nnmeas * (refmode ? 2 : 1) * intclocks; /* Total number of clocks */
zbsize = (mxnzsamp + 3)/4; /* 2 bits per clock to bytes */
a1logd(p->log,4,"i1pro3_do_measure: zbsize %d\n",zbsize);
if ((zbuf = (unsigned char *)malloc(sizeof(unsigned char) * zbsize)) == NULL) {
a1logd(p->log,1, "i1pro3_do_measure malloc %d bytes failed\n",zbsize);
free(buf);
return I1PRO3_INT_MALLOC;
}
}
/* Do measurement */
{
a1logd(p->log,1,"i1pro3_trigger_measure about to call usb_reinit_cancel()\n");
usb_reinit_cancel(&m->rd_sync); /* Prepare to sync rd and trigger */
a1logd(p->log,1,"i1pro3_trigger_measure done usb_reinit_cancel()\n");
if (p_p2m != NULL) {
a1logd(p->log,1,"i1pro3_trigger_measure about to call usb_reinit_cancel(2)\n");
usb_reinit_cancel(&m->rd_sync2); /* Prepare to sync rd and trigger */
a1logd(p->log,1,"i1pro3_trigger_measure done usb_reinit_cancel(2)\n");
}
}
/* Start thread that will trigger measure when signaled by gather */
if ((ev = i1pro3_trigger_measure(p, refmode, p_p2m != NULL ? 1 : 0,
scanmode ? 0 : nnmeas, intclocks, flags, TRIG_DELAY)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_trigger_measure failed with %d\n",ev);
free(buf);
return ev;
}
/* Start zebra stripe read - this releases measurement trigger. */
if (p_p2m != NULL) {
m->zebra_buf = zbuf;
m->zebra_bsize = zbsize;
m->zebra_bread = 0;
if ((zebra_th = new_athread(i1pro3_zebra_thread, (void *)p)) == NULL) {
a1logd(p->log,1,"i1pro3_trigger_measure new_athread() failed\n");
free(buf);
return I1PRO3_INT_THREADFAILED;
}
}
/* Start measurement gather - this releases the zebra read/trigger command... */
if ((ev = i1pro3_gathermeasurement(p, refmode, p_p2m != NULL ? 1 : 0,
scanmode, nnmeas, buf, bsize, &anummeas)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_gathermeasurement failed with %d\n",ev);
zebra_th->wait(zebra_th);
zebra_th->del(zebra_th);
free(buf);
return ev;
}
/* Should do this ? */
if ((ev = i1pro3_getlasterr(p, &errc)) != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_getlasterr failed with %d\n",ev);
zebra_th->wait(zebra_th);
zebra_th->del(zebra_th);
free(buf);
return ev;
}
/* If we had turned the LEDs on, record when they were turned off */
if (mm == i1p3mm_em_wl
|| (mm & i1p3cm_ill)) {
m->llamponoff = msec_time();
}
/* Set nummeas to the actual number of measurements */
if (refmode) {
if (tint > 1) /* Round down to give number of whole measurements */
nummeas = (anummeas - nnskip)/(tint / 2);
else
nummeas = 2 * (anummeas - nnskip);
if (nummeas < 0) {
zebra_th->wait(zebra_th);
zebra_th->del(zebra_th);
return I1PRO3_RD_SHORTMEAS;
}
raw = dmatrix(0, nummeas-1, -9, m->nraw-1);
} else {
nummeas = anummeas;
raw = dmatrix(0, nummeas-1, -1, m->nraw-1);
}
a1logd(p->log,4,"i1pro3_do_measure: nummeas actually done %d\n",nummeas);
/* Copy all the measurement data */
for (j = 0; j < nummeas; j++) {
unsigned char *b;
double tt;
if (refmode) {
/* Just use the odd (second) value from each measurement buffer. */
if (tint > 1) {
b = buf + 2 * nsen * (nnskip + j * tint/2 + tint/2-1);
/* This illum. auxiliary could be some sort of voltage. */
/* This is updated with each measurement */
tt = (double)read_ORD16_le(b + 2 * 0);
raw[j][-9] = (tt/13107.0 + 6.5)/2.988;
/* This one looks like a 3490 K NTC resistor */
/* This is updated with each measurement */
tt = (double)read_ORD16_le(b + 2 * 2);
tt = 65535.0/tt - 1.0;
tt = log(1.0/(tt * exp(-11.70551735703505)));
raw[j][-8] = 3490.0/tt -273.15;
/* These seem to be the output of a spectral sensor, i.e. AS7262/3 ? AS7341 ? */
/* These are updates every 11th min-int period, i.e. about 27.9 msec. */
for (i = 0; i < 6; i++)
raw[j][-7+i] = (double)read_ORD16_le(b + 2 * (276 + i));
/* Only odd measurement is valid */
/* Average 5 covered cell values. Skip 6th, as it may not be completely covered. */
raw[j][-1] = 0.0;
for (i = 0; i < 5; i++)
raw[j][-1] += (double)read_ORD16_le(b + 2 * (142 + i));
raw[j][-1] /= 5.0;
/* 128 sensor values */
for (i = 0; i < m->nraw; i++)
raw[j][i] = (double)read_ORD16_le(b + 2 * (142 + 6 + i));
/* Each measurement buffer produces two spectra, the even (first) being */
/* UV-excluded, and the odd (second) being UV included */
} else {
b = buf + 2 * nsen * (nnskip + j/2);
/* Even measurement, UV excluded */
/* This illum. auxiliary could be some sort of voltage. */
/* This is updated with each measurement */
tt = (double)read_ORD16_le(b + 2 * 0);
raw[j][-9] = (tt/13107.0 + 6.5)/2.988;
/* This one looks like a 3490 K NTC resistor */
/* This is updated with each measurement */
tt = (double)read_ORD16_le(b + 2 * 2);
tt = 65535.0/tt - 1.0;
tt = log(1.0/(tt * exp(-11.70551735703505)));
raw[j][-8] = 3490.0/tt -273.15;
/* These seem to be the output of a spectral sensor, i.e. AS7262/3 ? AS7341 ? */
/* These are updates every 11th min-int period, i.e. about 27.9 msec. */
for (i = 0; i < 6; i++)
raw[j][-7+i] = (double)read_ORD16_le(b + 2 * (276 + i));
/* Average 5 covered cell values. Skip 6th, as it may not be completely covered. */
raw[j][-1] = 0.0;
for (i = 0; i < 5; i++)
raw[j][-1] += (double)read_ORD16_le(b + 2 * (6 + i));
raw[j][-1] /= 5.0;
/* 128 sensor values */
for (i = 0; i < m->nraw; i++) {
raw[j][i] = (double)read_ORD16_le(b + 2 * (6 + 6 + i));
}
/* Odd measurement, UV included */
j++;
/* Illum. aux values are the same */
for (i = -9; i < -1; i++)
raw[j][+i] = raw[j-1][+i];
/* Average 5 covered cell values. Skip 6th, as it may not be completely covered. */
raw[j][-1] = 0.0;
for (i = 0; i < 5; i++)
raw[j][-1] += (double)read_ORD16_le(b + 2 * (142 + i));
raw[j][-1] /= 5.0;
/* 128 sensor values */
for (i = 0; i < m->nraw; i++) {
raw[j][i] = (double)read_ORD16_le(b + 2 * (142 + 6 + i));
}
}
/* Emissive measurement - no skip */
} else {
b = buf + j * 2 * nsen;
/* Average 5 covered cell values. Skip 6th, as it may not be completely covered. */
raw[j][-1] = 0.0;
for (i = 0; i < 5; i++)
raw[j][-1] += (double)read_ORD16_le(b + 2 * i);
raw[j][-1] /= 5.0;
/* 128 sensor values */
for (i = 0; i < m->nraw; i++)
raw[j][i] = (double)read_ORD16_le(b + 2 * (6 + i));
}
}
/* Copy and process the zebra ruler data */
if (p_p2m != NULL) {
int zskip;
int ii, jj;
zebra_th->wait(zebra_th);
zebra_th->del(zebra_th);
if (m->zebra_rv != I1PRO3_OK) {
a1logd(p->log,1,"i1pro3_zebra_thread failed with %d\n",m->zebra_rv);
return m->zebra_rv;
}
/* Allocate a byte for each zebra ruler value */
/* We expect one zebra bit per clock cycle, */
/* but in tint mode we can get extra zebra bits after the last */
/* valid measurement, which we discard. */
numzeb = nummeas * intclocks;
if ((zeb = (unsigned char *)malloc(sizeof(unsigned char) * numzeb)) == NULL) {
a1logd(p->log,1,"i1pro3_do_measure malloc %d bytes failed\n",numzeb);
free(buf);
return I1PRO3_INT_MALLOC;
}
/* Number of z samples to skip */
zskip = nnskip * (refmode ? 2 : 1) * m->minintclks;
/* Copy zebra ruler bits into bytes */
for (jj = ii = i = 0; i < m->zebra_bread; i++) { /* For each byte */
unsigned int ival, oval;
int k;
ival = zbuf[i];
for (k = 0; k < 4; k++, ii++) { /* For each 2 bit */
oval = ival & 3;
ival >>= 2;
if (ii >= zskip) {
if (jj >= numzeb) { /* More than we expect */
a1logd(p->log,6,"i1pro3_do_measure - more zebra bytes than measurements\n");
break;
}
zeb[jj] = oval;
jj++;
}
}
if (k < 4) {
i++;
break;
}
}
free(zbuf);
/* It seems that sometimes we get fewer zebra bytes than expected for */
/* the number of measurements. Adjust acordingly */
if (jj < numzeb) {
a1logd(p->log,6,"i1pro3_do_measure - fewer zebra bytes than measurements\n");
printf("~1 fewer zebra bytes than expected\n");
numzeb = jj;
}
if (i > m->zebra_bread || i < (m->zebra_bread - (2 * intclocks)/8)) {
error("Assert in %s at %d: zebra/meas mismatch: i %d != bread %d\n",__FILE__,__LINE__,jj,numzeb,i,m->zebra_bread);
}
if (p->log->debug >= 9) {
a1logd(p->log,9,"zebra raw data:\n");
adump_bytes(p->log, "", zeb, 0, numzeb);
}
/* Process the raw data into a list of meas patches at each 0.2 mm location */
if ((ev = i1pro3_zebra_proc(p, p_p2m, p_npos, zeb, numzeb, inttime, nummeas)) != I1PRO3_OK)
return ev;
free(zeb);
}
free(buf);
*praw = raw; /* Return measurement data we allocated */
*pinttime = inttime;
*pnummeas = nummeas;
return ev;
}
/* Free up the raw matrix returned by i1pro3_do_measure */
void i1pro3_free_raw(
i1pro3 *p,
i1p3_mmode mm, /* Measurement mode */
double **raw, /* Matrix to free */
int nummeas /* Number of measurements */
) {
i1pro3imp *m = (i1pro3imp *)p->m;
if (raw == NULL)
return;
if (mm & i1p3cm_refl) {
free_dmatrix(raw, 0, nummeas-1, -9, m->nraw-1);
} else {
free_dmatrix(raw, 0, nummeas-1, -1, m->nraw-1);
}
}
|