File: gistCmodule.c~

package info (click to toggle)
python-scipy 0.5.2-0.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 33,888 kB
  • ctags: 44,231
  • sloc: ansic: 156,256; cpp: 90,347; python: 89,604; fortran: 73,083; sh: 1,318; objc: 424; makefile: 342
file content (9581 lines) | stat: -rw-r--r-- 329,477 bytes parent folder | download
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
/* 
 *  $Id: gistCmodule.c 1593 2006-02-01 17:21:37Z oliphant $
 *  --------------------------------------------------------------------
 *  Copyright (c) 1996, 1997, The Regents of the University of California.
 *  All rights reserved.  See Legal.htm for full text and disclaimer. 
 *
 *  NAME:     gistCmodule.c
 *
 *  PURPOSE:  Gistmodule provides glue between Python and the Gist library 
 *            of graphics routines.  Much of the code is this module is 
 *            modeled after similar code in Dave Munro's Yorick interpreter.  
 *
 *  AUTHORS:  Lee Busby (originator)
 *            Zane Motteler (maintainer)
 *            Dave Munro (upgrade changes; event handling interface)
 *            Lila Chase (maintainer)
 *               Lawrence Livermore National Laboratory
 *            Michiel de Hoon (contributor; Windows/Cygwin ports) 
 *               University of Tokyo, Institute of Medical Science 
 *            Dave Grote (contributor:  bug fixes)
 *               Lawrence Berkeley National Laboratory
 *
 *  VERSION:  Original version was based on graph.c in Yorick 1.3.
 *            This version has been mostly upgraded to Yorick 1.5.
 *
 *  CHANGES:
 *  11/16/04 llc Fix GxPointClick; replace ifdef on this w/ MOUSE_CLICK.
 *               mouse function was not working because of this.
 *  11/09/04 mdh The Py_InitModule4 needs to be called even if the module
 *               has already been initialized.  Move the initialized test. 
 *  11/08/04 mdh Added a couple of changes for Mac OSX.
 *  11/04/04 dpg Changed TRY macro to return a MemoryError only if an 
 *               exception has not already been raised (in which case, it
 *               returns NULL). Also, fixed casting warning in contour's TRY 
 *               with PyTuple_GetSlice, changing 0 to (PyOject*) NULL.
 *  11/03/04 dpg Discovered that beginning with Numeric 23.4, which has
 *               stricter type checking (problems converting longs to ints),
 *               PyArray_INT had to be changed to PyArray_LONG. (else there
 *               were problems in plc, ...).
 *  04/22/03 llc Fix bug in PLF.
 *  04/08/03 llc Remove include of limits.h and unused defines;
 *               Replace DISPLAY_ENGINE with GpFXEngine, DISPLAY_MOUSE with
 *               GxPointClick, and DISPLAY_ZOOM_FACTOR with gxZoomFactor, and
 *               remove use NO_XLIB, and NO_MOUSE. 
 *  04/07/03 mdh Implement justify option for plt (align).
 *               Add functions as those in style.i in yorick to allow
 *               allow users to change style properties interactively:
 *               get_text_attributes, get_legend, get_line_attributes,
 *               get_axis_style, get_tick_style, get_system, 
 *               get_systems_list, get_style, ... similarly for set*.
 *  03/25/03 dpg Provide a way to change CGM file size via pldefault.
 *  03/12/03 llc Remove version and help comments; add to gist.py.
 *  02/25/03 llc Fix mouse command so data is returned.
 *  02/20/03 llc Correct mouse return; see MouseCallBack.
 *  02/13/03 llc Disable initial pygist printout upon import (except 
 *               when DEBUG flag is on).
 *  02/13/03 dpg Add plremove implementation. 
 *               Correct bug in pli (iMax anad JMax were reversed).
 *  01/17/03 llc Remove remaining warnings with gcc 2.96 by:
 *               - Reduce to one dummy for file
 *               - Remove dumpFreeList, Add1 cumsum, equal, greater, 
 *                 greater_equal, less, logical_and, not_equal, 
 *                 WeightedHist if not INCLUDE_EXTRA_GISTCODE.
 *  01/13/03 mdh Add back on omitted bug fix for Windows (wait_for_expose).
 *  12/27/02 mdh Windows/Cygwin adjustments (e.g. pyg_on_idle calls for
 *               Windows); fix formats for pointers.
 *  12/04/02 dhm more fixes to get pygist working under windows, _tkinter
 *               Macros for proper printing to stdout and stderr.
 *               Add pyg_unhook, pyg_idler, pyg_pending, and pyg_register.
 *  11/26/02 llc Add documentation on plg markers.
 *  11/25/02 dhm Rework of play resulted in change to PyOS_InputHook and
 *               other improvements (multi-line strings).
 *  11/24/02 dhm fix event/exception handling interface
 *  11/19/02 llc Use gist interface provided by Dave Munro for PyOS_InputHook.
 *               Remove old solutions now that play/unix has changed.
 *  11/15/02 mhd Add u_enterloop for third approach to interfacing Python
 *               to gist.  Save the last approach as USE_U_WAITER.
 *  11/13/02 llc Add back option to switch between two interfaces to Python.
 *               (using define USE_RL_GETC)
 *  11/11/02 llc Add Numeric to arrayobject.h include, so it is found
 *               automatically when the Python include directory is searched.
 *               (suggestion from MDH)
 *  11/08/02 llc Use the PyFPE_START_PROTECT and PyFPE_END_PROTECT 
 *               macros defined in the standard Python include pyfpe.h.
 *               Add a dummy argument to PyFPE_END_PROTECT.
 *  11/07/02 llc Clean up some compiler warnings:
 *               Get rid of SETERR macro; used just once.
 *               Correct format for writes of longs in debug_array, PrintColor.
 *               Add explict casts to ERRSS where required.
 *               Remove unused variables.
 *               Cast u_waiter properly, and note possible issue.
 *  11/01/02 mdh Replace rl_getc_function with PyOS_InputHook.
 *  09/04/02 llc Deep copy string returned from PyString_AsString
 *               in setkw_string.
 *  12/17/01 llc Add documentation lines to slice2.
 *  12/06/01 llc Remove fix for E_TEXT type entries in legend; these
 *               are legitimate.
 *  12/03/01 llc Discovered that new setting of zc in plf causes 
 *               unaligned accesses on DEC Alpha; go back to earlier
 *               form setting temporary zc1.
 *               With this change, test suite runs to completion.
 *  11/29/01 llc Set rgb for triples.
 *               bytscl: Change len to long.
 *  11/16/01 llc Update to 1.5's legend handling. 
 *  11/12/01 llc Unpack color triples.
 *               Colors changed from int to unsigned long (1.4 to 1.5).
 *               Check for negative colors.
 *  11/08/01 dhm Turn on idle function to do graphics tasks.
 *  11/05/01 llc Move all of the function documentation from gist.help
 *               to doc strings for each PyObject, so it can be accessed 
 *               with pydoc.
 *  11/03/01 llc Started from version with changes from Dave Munro:
 *               turn on rl_getc_function, and add readline.h include. 
 *               Must use with version of python built with readline.
 *
 * Modified in December, 1997 to coexist with the readline library used
 * in Python's interpreter. William Magro, Cornell Theory Center.
 *
 *  --------------------------------------------------------------------
 */

/*
 * TODO:
 *   Fri May 24 15:54:16 PDT 1996
 *   There is a significant potential problem with memory leaks in the
 *   current version of the Gist C module. There are many places where
 *   allocated objects don't get cleaned up correctly in the event of an
 *   untimely error. Typical code looks like this:
 * 
 *     PyObject *op = foo (x);
 *     TRY (something_which_might_fail (y));
 *     // Calculate, display, etc.
 *     Py_DECREF (op);
 *     Py_INCREF (Py_None);
 *     return Py_None;
 *   
 *   The problem, of course, is the "return 0" in the usual definition of
 *   the TRY() macro. If its function fails, then the premature return is
 *   taken, "op" never gets DECREF'ed, and its memory leaks. This idiom
 *   shows up all over the place in gistCmodule, and I don't know any
 *   really nice ways to fix it up.
 * 
 *   The best approach I've thought of so far is to redefine TRY() so that
 *   it does a "goto errexit" instead of returning immediately.  The code
 *   fragment above changes to:
 * 
 *     PyObject *op = foo (x);
 *     TRY (something_which_might_fail (y));
 *     // Calculate, display, etc.
 *     Py_DECREF (op);
 *     Py_INCREF (Py_None);
 *     return Py_None;
 *   errexit:
 *     Py_DECREF (op);
 *     return 0;
 *   
 *   The general case is complicated by the possibility of freeing the same
 *   pointer twice. The mechanism of choice for handling this circumstance
 *   would seem to be to adopt a strict convention of zeroing all pointers
 *   at initialization time and later when they are freed, and (of course)
 *   never free a nil pointer. Py_XDECREF() does part of the job (it won't
 *   free a nil pointer), but it needs a little help to finish the job by
 *   zeroing its argument. One other fine point is that not every function
 *   in the Python API sets an exception upon failure. This leads to the
 *   strange-looking error in the interpreter "Zero return without apparent
 *   error" (I'm quoting from memory, sorry.) Therefore the careful errexit
 *   code should check for a legitimate error with PyErr_Occurred(), and
 *   set a default error if there was none.
 * 
 *   All this is straightforward enough, but somewhat tedious and verbose
 *   to apply. And the payback is usually small, because this sort of thing
 *   only happens when there is a previous error to provoke it. And I wish I
 *   could think of a nicer way to do it than with goto and a bunch of even
 *   fancier macros.  So I will wait awhile....
 */

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <math.h>
#include <setjmp.h>
#include <string.h>

#include "Python.h"
#include "pyfpe.h"

#include "numpy/arrayobject.h"
#include "hlevel.h"

#include "pstdlib.h"
#include "play.h"
#include "pmin.h"
#include "style.h"

static int dummy = 0;


/* We add a component to the default Gist search path, for style and
   palette files in our Python distribution.
*/
static char *gistpath = 0, *oldgistpath = 0;
/*  
 *  10/30/01 llc The new gist directory is no longer in graphics.
 *               Original OUR_SPECIAL_DIR "/graphics/gist" 
 */
#define OUR_SPECIAL_DIR "/gist"

/* Mouse() related stuff */
static int MouseCallBack (Engine * engine, int system,
			  int release, double x, double y,
			  int butmod, double xn, double yn);
static double mouseX0, mouseY0, mouseX1, mouseY1;
static double mouseX0ndc, mouseY0ndc, mouseX1ndc, mouseY1ndc;
static int mouseButton, mouseModifier, mouseSystem, mouseError;
static char *defaultPrompts[2] = {
  "<Click mouse at point>", "<Press, drag, and release mouse>" };

/* The Gist library uses SINGLE_P in order to typedef GpReal, so use the
 * same setting for SINGLE_P as in your copy of libgist.a. (Note that
 * anything other than GpReal == double is untested.)
 */
#ifndef SINGLE_P
#  define Py_GpReal PyArray_DOUBLE
#else
#  define Py_GpReal PyArray_FLOAT
#endif
#define Py_GpColor PyArray_UBYTE

static PyObject *GistError;

static void clearFreeList (int n);
static void clearArrayList (void);
static void clearMemList (void);

/* PyErr_SetString returns a void; make an object and cast it */

/* 
 *  11/06/01 LLC: 
 *  On Teracluster, compiler warns of cast to int in some uses. 
 *
 *  ERRSS must evaluate to a non-null object connected to error
 *  The return value is used in SETKW and TRY.
 *
 *  EXPLANATION of ERRSS:
 *  ---------------------
 *  PyErr_SetString returns a void.  A trick is used here to 
 *  cast it into a NULL (0).  In the following:
 *     void c();
 *     char * y;
 *     y = (char *) (c(),0);
 *  y is 0 or NULL.
 */

#define ERRSS(s) ((PyObject *)(PyErr_SetString(GistError,s),0))
#define ERRMSG(s) (PyErr_SetString(GistError,s))
#define SETJMP0 if(setjmp(jmpbuf)){p_pending_events();return(0);}
#define SETKW(ob, target, func, s) \
  if(ob && ob != Py_None && !func(ob, &target, s)) return 0
#define BUILD_KWT(kd,K,kwt) if (-1 == build_kwt (kd, K, kwt)) return 0;
#define NELT(a) (sizeof(a)/sizeof(a[0]))
#define SAFE_FREE(p) (p = (p)?free(p),(void *)0:(void *)0)

/* Old definition of TRY: */
/* #define TRY(e) do{if(!(e))return 0;}while(0) */
/* New definition of TRY: (second argument m = 1 for memerr) */

#define TRY(e, m) do{if(!(e)){clearArrayList();clearFreeList(0);\
               clearMemList();return (PyErr_Occurred() ? 0 : m);}} while(0)
#define TRYS(e) {char * errstr; \
                 if( (errstr=(e)) != NULL ) { \
                       clearArrayList(); \
                       clearFreeList(0); \
                       clearMemList(); \
                       return ERRSS(errstr); \
                 }} 
#define DECL_ZERO(type, var) type var = 0
#define DECREF_AND_ZERO(p) do{Py_XDECREF(p);p=0;}while(0)

/* (DHM) proper Python printing to stdout and stderr */
#define TO_STDOUT PySys_WriteStdout
#define TO_STDERR PySys_WriteStderr
static void flush_stdout(void);
static void flush_stderr(void);
static void flush_std(const char *s);
static int pyg_puts(const char *);

static void
flush_std(const char *s)
{
  /* code from Python/sysmodule.c:PySys_WriteStdout */
  PyObject *pstdout, *error_type, *error_value, *error_traceback;
  PyErr_Fetch(&error_type, &error_value, &error_traceback);
  pstdout = PySys_GetObject("stdout");
  fflush(pstdout? PyFile_AsFile(pstdout) : stdout);
  PyErr_Restore(error_type, error_value, error_traceback);
}
static void
flush_stdout(void)
{
  flush_std("stdout");
}
static void
flush_stderr(void)
{
  flush_std("stderr");
}
static int
pyg_puts(const char *s)
{
  if (s) {
    long len = strlen(s);
    if (len > 0) {
      TO_STDOUT(s);
      if (s[len-1] == '\n') return 0;
    }
    TO_STDOUT("\n");
  }
  return 0;
}

/* %%%%%%%% */
/* Macros defining most of my uses of a PyArrayObject.  */

/* The number of dimensions of the array. */
#define A_NDIM(a) (((PyArrayObject *)a)->nd)

/* The length of the ith dimension of the array. */
#define A_DIM(a,i) (((PyArrayObject *)a)->dimensions[i])

/* The total number of elements in the array. */
#define A_SIZE(a) PyArray_Size((PyObject *)a)

/* The type number of the array. */
#define A_TYPE(a) (int)(((PyArrayObject *)a)->descr->type_num)

/* The pointer to the array data. */
#define A_DATA(a) (((PyArrayObject *)a)->data)

/* Object is non-null and a PyArrayObject */
#define isARRAY(a) ((a) && ( (PyObject *)a != Py_None) && PyArray_Check((PyArrayObject *)a))

/* Build an array from an object, possibly upcasting the type and copying
   the data to make it contiguous. */
/* Old version: */
/* #define GET_ARR(ap,op,type,dim) \
  TRY(ap=(PyArrayObject *)PyArray_ContiguousFromObject(op,type,dim,dim)) */
/* The new versions below try to add the new object to an appropriate
   list of objects. If they fail, routines which delete all objects
   created so far are called prior to return (see new definition of TRY). */
/* New version: */
#define GET_ARR(ap,op,type,dim,cast) \
  TRY(addToArrayList((PyObject *)(ap=\
  (PyArrayObject *)PyArray_ContiguousFromObject(op,type,dim,dim))), \
  (cast)PyErr_NoMemory ())
/* Build a new array */
#define NEW_ARR(ap,n,dims,type,cast) \
  TRY(addToArrayList((PyObject *)(ap=\
  (PyArrayObject *)PyArray_FromDims(n,dims,type))), \
  (cast)PyErr_NoMemory ())
/* Build an array from existing data */
#define RET_ARR(op,ndim,dim,type,data,cast)\
  TRY(addToArrayList(op=\
  PyArray_FromDimsAndData(ndim,dim,type,data)), \
  (cast)PyErr_NoMemory ())
/* Array owns its data so if DECREF'ed, its data will be freed */
#define SET_OWN(op) ( (PyArrayObject *) op)->flags |= OWN_DATA
/* Array does not own its data so if DECREF'ed, its data will not be freed */
#define UNSET_OWN(op) ( (PyArrayObject *) op)->flags &= ~OWN_DATA
/* Create a block of memory */
#define NEW_MEM(mem,n,type,cast) \
  TRY(addToMemList((void *)(mem=(type *)malloc(n*sizeof(type)))), \
  (cast)PyErr_NoMemory ())

/* %%%%%%%% */

/* Routines needed from outside (but not declared in a header file). */
extern int Py_AtExit (void (*func) (void));

/********************M E M O R Y   M A N A G E M E N T*****************/
typedef unsigned char Uchar;

/* struct arrayobject is used in slice2 routines */
typedef struct arrayobject {
   void * data ;
   int size ;
   char typecode ;
} ArrayObject;

#define SAVE -1
#define FREE0 0
#define FREE1 1

/* We have three lists of things to be released upon error return:        */
/* freeList = ArrayObjects (list 0 is slice2 list, list1 is _slice2_part) */
/* PyArrayList = list of NumPy arrays created                             */
/* PyMemList = list of heap pointers created by malloc                    */
/* Some of these items should not be purged upon normal return. The caller*/
/* is responsible for purging only temporaries upon return, and setting   */
/* list lengths to zero.                                                  */

#define MAX_NO_LISTS 2
#define MAX_LIST_SIZE 30

static ArrayObject * freeList [MAX_NO_LISTS] [MAX_LIST_SIZE];
static int freeListLen [MAX_NO_LISTS] = {0, 0};

#define ARRAY_LIST_SIZE 30

static PyObject * PyArrayList [ARRAY_LIST_SIZE];
static int array_list_length = 0;

#define MEM_LIST_SIZE 15

static void * PyMemList [MEM_LIST_SIZE];
static int mem_list_length = 0;


/*****************************************************************/
/*           ArrayObject creation                                */
/* allocateArray creates a complete new ArrayObject including a  */
/* newly malloc'ed data area set to zeros.                       */
/* arrayFromPointer creates a new ArrayObject pointing to a data */
/* area supplied by the client.                                  */
/* Each routine places the new structure on the specified        */
/* FreeList.                                                     */
/* Each returns NULL if it fails for any reason.                 */
/*****************************************************************/

static int addToFreeList (ArrayObject * x, int n);
static void freeArray (ArrayObject * a, int n);
static void removeFromFreeList (ArrayObject * x, int n);
#ifdef INCLUDE_EXTRA_GISTCODE 
static void dumpFreeList (int n);
#endif

static ArrayObject * allocateArray (int size, char tc, int nlist) {
   /* allocate an array object containing size components of type tc */
   ArrayObject * res;
   if (size <= 0)
      return (ArrayObject *) NULL;
   TRY (res = calloc (1, sizeof (ArrayObject)), 
      (ArrayObject *) PyErr_NoMemory ());
   res->size = size ;
   res->typecode = tc ;
   if (size != 0) {
      switch (tc) {
         case 'i' :
            if ( !(res->data = (void *) calloc (size, sizeof (int)))) {
               free (res) ;
               return (ArrayObject *) PyErr_NoMemory ();
               }
            break;
         case 'd' :
            if ( !(res->data = (void *) calloc (size, sizeof (double)))) {
               free (res) ;
               return (ArrayObject *) PyErr_NoMemory ();
               }
            break;
         case 'b' :
            if ( !(res->data = (void *) calloc (size, sizeof (Uchar)))) {
               free (res) ;
               return (ArrayObject *) PyErr_NoMemory ();
               }
            break;
         default :
            free (res) ;
            return (ArrayObject *) NULL ;
         }
      }
   else
      res->data = (void *)NULL;
   if (addToFreeList (res, nlist) != 0) {
      freeArray (res, nlist);
      res = (ArrayObject *) NULL ;
      }
   return res;
   }

static ArrayObject * copyArray (ArrayObject * a) {
   /* returns a copy of the argument array. Does not put it on */
   /* the free list.                                           */
   ArrayObject * res;
   int size;
   if ( a == (ArrayObject *) NULL || a->size <= 0) {
      return (ArrayObject *) NULL;
      }
   switch (a->typecode) {
      case 'b':
         size = sizeof (Uchar);
         break;
      case 'i':
         size = sizeof (int);
         break;
      case 'd':
         size = sizeof (double);
         break;
      default:
         return (ArrayObject *) NULL;
      }
   TRY (res = calloc (1, sizeof (ArrayObject)), 
      (ArrayObject *) PyErr_NoMemory ());
   TRY (res->data = calloc (a->size, size), 
      (ArrayObject *) PyErr_NoMemory ());
   TRY (memcpy (res->data, a->data, a->size * size),
      (ArrayObject *) ERRSS ("memcpy failed in copyArray."));
   res->size = a->size;
   res->typecode = a->typecode;
   return res;
   }

static ArrayObject * arrayFromPointer (int size, char tc, void * data,
   int nlist) {
   /* allocate an array object containing size components of type tc. */
   /* Caller supplies address of data and takes responsibility for    */
   /* its being the right size.                                       */
   ArrayObject * res;
   if (size <= 0)
      return (ArrayObject *) NULL;
   TRY (res = calloc (1, sizeof (ArrayObject)), 
      (ArrayObject *) PyErr_NoMemory ());
   res->size = size ;
   res->typecode = tc ;
   res->data = data ;
   if (addToFreeList (res, nlist) != 0) {
      freeArray (res, nlist);
      res = (ArrayObject *) NULL ;
      }
   return res ;
   }

/*****************************************************************/
/*               getting rid of ArrayObjects                     */
/* freeArray frees the array and its data space, and removes it  */
/* from the FreeList, if it's on it.                             */
/*****************************************************************/

static void freeArray (ArrayObject * a, int n) {
   /* deallocate an array object */
   if (a == (ArrayObject *) NULL)
      return;
   removeFromFreeList (a, n);
   SAFE_FREE (a->data);
   SAFE_FREE (a);
   }

/***********************************************************************
 * FREE LIST MAINTENANCE
 * Routines to create ArrayObjects may return a NULL object, which
 * sometimes is a memory error. Testing for this, and keeping track
 * of created objects in routines like _slice2_part so that they can
 * be removed before error return is a real hassle.
 * I have semi-automated the process with the following routines and
 * conventions.
 * Every time an ArrayObject is successfully created and returned
 * by a routine, it will be added to the freeList. Every time freeArray
 * frees a real object, that object will be removed from the freeList.
 * Prior to return from any routine, a call to clearFreeList will
 * then get rid of all unFreed objects.
 * Note the existence of two freeLists, 0 is for slice2 and 1 is
 * for _slice2_part.
 ************************************************************************/

static void clearFreeList (int n) {
   int i;
   if (n < 0 || n >= MAX_NO_LISTS) return;
   for (i = 0; i < freeListLen [n]; i++) {
      if (freeList [n] [i]) {
         SAFE_FREE (freeList [n] [i]->data);
         }
      SAFE_FREE (freeList [n] [i]);
      }
   freeListLen [n] = 0;
   return;
   }

#ifdef INCLUDE_EXTRA_GISTCODE 
static void dumpFreeList (int n) {
   /* Useful for debugging ??? */
   int i;
   TO_STDOUT ("-----------start-%d-----------\n", n); flush_stdout();
   for (i =0; i < freeListLen [n]; i++) {
      TO_STDOUT ("entry %p points to %c data (%d) at %p.\n",
         freeList [n] [i], freeList [n] [i]->typecode, freeList [n] [i]->size,
         freeList [n] [i]->data); flush_stdout();
      }
   TO_STDOUT ("----------finish-------------\n"); flush_stdout();
   }
#endif

static int addToFreeList (ArrayObject * x, int n) {
   /* A new element is always added at the end. */
   if (n < 0 || n >= MAX_NO_LISTS || freeListLen [n] >= MAX_LIST_SIZE) {
      return -1;
      }
   freeList [n] [ freeListLen [n] ++] = x;
   return 0;
   }

static void removeArrayOnly (ArrayObject * x, int n) {
   /* If the specified list exists, and the item is on it, */
   /* then remove the item, free the item only, and        */
   /* compress the list together.                          */
   int i;
   int found = 0;
   if (n < 0 || n >= MAX_NO_LISTS || x == NULL)
      return;
   for (i =0; i < freeListLen [n]; i++)
      if (!found && freeList [n] [i] == x) {
         SAFE_FREE (freeList [n] [i]);
         found = 1;
         }
      else if (found)
         freeList [n] [i - 1] = freeList [n] [i];
   if (found)
      freeListLen [n] --;
   return;
   }

static void removeFromFreeList (ArrayObject * x, int n) {
   /* If the specified list exists, and the item is on it, */
   /* then remove the item and compress the list together.  */
   int i;
   int found = 0;
   if (n < 0 || n >= MAX_NO_LISTS || x == NULL)
      return;
   for (i =0; i < freeListLen [n]; i++)
      if (!found && freeList [n] [i] == x) {
         found = 1;
         }
      else if (found)
         freeList [n] [i - 1] = freeList [n] [i];
   if (found) {
      freeListLen [n] --;
      }
   return;
   }

/*************************************************************************/
/*              ArrayList maintenance                                    */
/*************************************************************************/

static int addToArrayList (PyObject * obj) {
   if (obj == (PyObject *) NULL || array_list_length > ARRAY_LIST_SIZE)
      return 0;
   PyArrayList [array_list_length ++] = obj;
   return 1;
   }

static void clearArrayList (void) {
   /* DECREF's everything on the ArrayList; needs to be done if there */
   /* is an error.                                                    */
   int i;
   for (i = 0; i < array_list_length; i++)
      Py_DECREF (PyArrayList [i]);
   array_list_length = 0;
   return;
   }

static void removeFromArrayList (PyObject * obj) {
   int i;
   int found = 0;
   if (obj == (PyObject *) NULL)
      return;
   for (i = 0; i < array_list_length; i++)
      if (! found && PyArrayList [i] == obj) {
         Py_DECREF (obj);
         found = 1;
         }
      else if (found)
         PyArrayList [i - 1] = PyArrayList [i];
   if (found)
      array_list_length --;
   }

static void takeOffArrayList (PyObject * obj) {
   /* removes object from list without DECREFing. */
   int i;
   int found = 0;
   for (i = 0; i < array_list_length; i++)
      if (! found && PyArrayList [i] == obj) {
         found = 1;
         }
      else if (found)
         PyArrayList [i - 1] = PyArrayList [i];
   if (found)
      array_list_length --;
   }

/*************************************************************************/
/*              MemList maintenance                                    */
/*************************************************************************/
 
static int addToMemList (void * addr) {
   if (addr == (void *) NULL || mem_list_length >= MEM_LIST_SIZE)
      return 0;
   PyMemList [mem_list_length ++] = addr;
   return 1;
   }
   
static void clearMemList (void) {
   int i;
   for (i = 0; i < mem_list_length; i++) {
      SAFE_FREE (PyMemList [i]);
      }
   mem_list_length = 0;
   return;
   }

/******************************E N D***********************************/

/* This module's initialization routine */
void initgistC (void);

/* Routines in the Gist user interface */
static PyObject *animate (PyObject * self, PyObject * args);
static PyObject *bytscl (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *contour (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *current_window (PyObject * self, PyObject * args);
static PyObject *debug_array (PyObject * self, PyObject * args);
static PyObject *pyg_fma (PyObject * self, PyObject * args);
static PyObject *get_slice2_precision (PyObject * self, PyObject * args);
static PyObject *gridxy (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *hcp (PyObject * self, PyObject * args);
static PyObject *hcp_file (PyObject * self, PyObject * args, PyObject *kd);
static PyObject *hcp_finish (PyObject * self, PyObject * args);
static PyObject *hcpoff (PyObject * self, PyObject * args);
static PyObject *hcpon (PyObject * self, PyObject * args);
static PyObject *limits (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *logxy (PyObject * self, PyObject * args);
static PyObject *mesh_loc (PyObject * self, PyObject * args);
static PyObject *mfit (PyObject * self, PyObject * args);
static PyObject *mouse (PyObject * self, PyObject * args);
static PyObject *palette (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *pyg_pause (PyObject * self, PyObject * args);
static PyObject *plc (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *pldefault (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *pldj (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *pledit (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *plf (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *plfp (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *plg (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *pli (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *plm (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *plmesh (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *plq (PyObject * self, PyObject * args);
static PyObject *plremove (PyObject * self, PyObject * args);
static PyObject *plsys (PyObject * self, PyObject * args);
static PyObject *plt (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *plv (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *redraw (PyObject * self, PyObject * args);
static PyObject *set_slice2_precision (PyObject * self, PyObject * args);
static PyObject *slice2 (PyObject * self, PyObject * args);
static PyObject *unzoom (PyObject * self, PyObject * args);
static PyObject *viewport (PyObject * self, PyObject * args);
static PyObject *window (PyObject * self, PyObject * args, PyObject * kd);
static PyObject *zoom_factor (PyObject * self, PyObject * args);
static PyObject *pyg_unhook (PyObject * self, PyObject * args);
static PyObject *pyg_idler (PyObject * self, PyObject * args);
static PyObject *pyg_register (PyObject * self, PyObject * args);

/* Utility routines */
static GpColor *PushColors(double *z, long len, double zmin,
  double zmax, double scale, double offset);
static char *GetHCPname (int n);
static char *SetHCPname (int n, char *name);
static char *expand_pathname (const char *name);
static double Safe_dbl (double x);
static double *CopyLevels(double *levels, long nLevels);
static char *CheckDefaultWindow(void);
static int CheckPalette (void);
static int GetTypeface (char *s, int *f);
static int GrabByteScale ( PyObject **kwt, char **keywrds, double *scale,
  double *offset, double *zn, double *zx, double *z, int *reg, 
    int region, long iMax, long jMax, int zCompressed);
static int SetHCPDefault (void);
static int YPrompt(const char *s);
static int build_kwt (PyObject *kd, char *Keys[], PyObject * kwt[]);
static int set_def_reg (int nr, int nc);
static int set_limit (PyObject * ob, double *lim, int *flags, int fval);
static int set_pyMsh(PyObject *args, char *errstr, PyObject *tri);
static int set_reg (PyObject *rop);
static int set_tri (PyObject *top);
static int set_yx (PyObject *yop, PyObject *xop);
static int setkw_boolean (PyObject * v, int *t, char *kw);
static int setkw_color (PyObject * v, unsigned long *t, char *kw);
static int setkw_double (PyObject * v, double *t, char *kw);
static int setkw_fonttype (PyObject * v, int *t, char *kw);
static int setkw_integer (PyObject * v, int *t, char *kw);
static int setkw_justify (PyObject * v, int *t, char *kw);
static int setkw_linetype (PyObject * v, int *t, char *kw);
static int setkw_string (PyObject * v, char **t, char *kw);
static int setkw_xinteger (PyObject * v, int *t, char *kw);
static int setvu_mesh( PyObject *args, PyObject **vop, 
  PyObject **uop, char *errstr);
static int setz_mesh( PyObject *args, PyObject **zop, 
  char *errstr, PyObject *tri);
static int unpack_color_tuple (PyObject * ob, unsigned long color_triple[3]);
static int unpack_limit_tuple (PyObject * ob, double limits[], int *flags);
static int verify_kw (char *keyword, char * kwlist[]);
static long FindMeshZone(double xx, double yy, double *x, double *y, 
  int *reg, long ix, long jx);
static long Safe_strlen(const char *s);
static void AllocTmpLegend(long len);
static void CheckDefaultPalette (void);
static void CleanUpGraphics (void);
static void ForceNewline (void);
static void FreeTmpLegend(void);
static long escape_count(char *arg);
static void escape_cat(char *leg, char *arg);
static void GetPCrange (double *zmn, double *zmx, double *z, 
  int *reg, int region, long iMax, long jMax);
static void GetZCrange(double *zmn, double *zmx, double *z, 
  int *reg, int region, long iMax, long jMax, int zCompressed);
static int LegendAndHide(char *func, char *arg1, char *arg2, char *arg3,
  char *arg4, PyObject *kwt[], char *keys[]);
static void PermitNewline (int nSpaces);
static void PrintColor (char *line, unsigned long color, int suffix);
static void PrintFunc (const char *s);
static void PrintHideLegend (char *line, int type);
static void PrintInit (int (*puts) (const char *));
static void PrintMarks (char *line, int suffix);
static void PrintRegion (char *line, int suffix);
static void PrintSuffix (int suffix);
static void PrintTypeWidth (char *line, int suffix);
static void clear_pyMsh(void);
static void get_mesh(GaQuadMesh *m);

static int pyg_on_idle(void);
static void pyg_on_connect(int dis, int fd);
static void pyg_on_keyline(char *msg);
static void pyg_got_expose(void);
static void pyg_got_alarm(void *context);

/* Global variables */
/*
 * All the mesh routines share this pyMsh struct. (Why? Because the mesh
 * arrays are saved between calls to mesh routines, and in general, mesh
 * routines allow the mesh array arguments to be optional, defaulting to
 * the currently saved mesh.) PyMsh mirrors the Gist GaQuadMesh structure.
 * Gist documentation/code uses the variables iMax and jMax to designate
 * the number of elements in each dimension of the two 2D arrays X and Y
 * whose respective elements give the coordinates of the quadrilateral mesh
 * nodes. I sometimes use iMax, jMax in this file, but more often use "nr",
 * "nc" to designate the number of rows and columns in the arrays. The
 * following correspondence generally is true:
 *
 *      iMax == nc == A_DIM(array, 1)
 *      jMax == nr == A_DIM(array, 0)
 */
static struct {
  PyArrayObject *y, *x, *reg, *triangle;
} pyMsh = { 0, 0, 0, 0 };

static int already_initialized = 0;
static int paletteSize = 0;
static int maxColors = 200; /* maximum number of colors for GpReadPalette */
static int hcpDump= 1;      /* whiners can't figure out how to dump colors */
static int hcpPSdefault = 1;  /* default now .ps instead of .cgm */
static int hcpOnFMA = 0;
static int defaultDPI = 75;
static int defaultLegends= 1;
static char *defaultStyle = 0;
static char *defaultPalette = 0;

static int curPlotter = -1;
static int curElement = -1;

static char *hcpNames[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
static char *tmpLegend = 0;
static char *windowNames[8] = {
  "Pygist 0", "Pygist 1", "Pygist 2", "Pygist 3",
  "Pygist 4", "Pygist 5", "Pygist 6", "Pygist 7"
};

/* Next few variables are used by plq(), which does some fancy printing. */
/* static long prop3sizes[10] = {0, 8, 2, 5, 5, 3, 3, 7, 0, 0}; */
/* static long prop4sizes[10] = {0, 8, 1, 3, 1, 1, 3, 4, 4, 0}; */
/* static long prop5sizes[10] = {0, 3, 5, 2, 5, 6, 7, 9, 3, 5}; */
static int curIX = -1, curIXc = -1;
static char specialMarkers[5] = ".+*ox";
static int (*RawPrinter) (const char *s);
static int printLength = 79;	/* maximum number of characters on a line */
/* static int lenPrintBuf = 79; */
static char printBuf[80];
static long maxPrintLines = 5000;
static int printNow, permitNow;
static long printLines;
static double _slice2_precision = 0.0;

/* make p_abort work with SETJMP0
 * here's how it works:
 * p_abort calls pyg_abort_hook, which longjmps to SETJMP0 point
 * there, p_pending_events is called to process the
 * on_exception event that goes along with the p_abort
 */
static jmp_buf jmpbuf;
static jmp_buf pyg_jmpbuf;
static void pyg_abort_hook(void);
static void pyg_abort_hook(void)
{
  longjmp(pyg_jmpbuf, 1);
}
static int pyg_wait_flag = 0;
static void pyg_on_exception(int signal, char *errmsg);
static void pyg_on_exception(int signal, char *errmsg)
{
  pyg_wait_flag = 0;
  if (!errmsg) {
    PyErr_SetString (GistError, "unknown gist error");
  } else {
    PyErr_SetString (GistError, errmsg);
  }
}

static char gist_module_documentation[] =
"Gist Graphics Package, version1.5"
;

#define PYCF (PyCFunction)
/*#define PYCFWK (PyCFunctionWithKeywords)*/
#define PYCFWK (PyCFunction) /* Make the compiler shut up. */
#define KWFLG (METH_VARARGS | METH_KEYWORDS)


/*#######################################################################*/
/*           Auxiliary routines used by the slice2 suite                 */
/*#######################################################################*/

#ifdef INCLUDE_EXTRA_GISTCODE 
static ArrayObject * Add1 ( ArrayObject * i, int freei, int n) {
   /* add 1 to an integer array i and return the value */
   ArrayObject * res ;
   int * isrc ,
       * itar;
   if (i == (ArrayObject *) NULL)
      return (ArrayObject *) NULL;
   if (freei >= 0 && freei == n) {
      /* We don't need i any more and it's in the right list, so *
       * just increment it.                                      */
      for (isrc = (int *) (i->data);
           isrc < (int *) (i->data) + i->size; isrc ++)
         (* isrc) ++;
      return i;
      }
   else {
      TRY (res = allocateArray (i->size, 'i', n),
         (ArrayObject *) PyErr_NoMemory ());
      for (isrc = (int *) (i->data), itar = (int *) (res->data);
           isrc < (int *) (i->data) + i->size; isrc ++, itar ++)
         * itar = (* isrc) + 1;
      if (freei >= 0)
         freeArray (i, freei);
      return res;
      }
   }
#endif
 
static ArrayObject * concatenate (ArrayObject * x, ArrayObject * y,
   int freex, int freey, int n) {
   /* concatenates array objects x and y and returns the result */
   /* returns NULL if memory error or if both objects are NULL. */
   /* Note that the new array is put onto the free list.        */
   ArrayObject * result;
   int datasize;
   Uchar tc;
   if (x == (ArrayObject *) NULL && y == (ArrayObject *) NULL) {
      return (ArrayObject *) NULL;
      }
   tc = (y == (ArrayObject *) NULL) ? x->typecode : y->typecode;
   switch (tc) {
      case 'b':
         datasize = sizeof (Uchar);
         break;
      case 'i':
         datasize = sizeof (int);
         break;
      case 'd':
         datasize = sizeof (double);
         break;
      default:
         return (ArrayObject *) NULL;
      }
   if (y == (ArrayObject *) NULL) {
      TRY (result = allocateArray (x->size, x->typecode, n), 
         (ArrayObject *) PyErr_NoMemory ());
      TRY (memcpy (result->data, x->data, x->size * datasize),
         (ArrayObject *) ERRSS ("memcpy failed in concatenate."));
      if (freex >= 0)
         freeArray (x, freex);
      return result;
      }
   if (x == (ArrayObject *) NULL) {
      TRY (result = allocateArray (y->size, y->typecode, n), 
         (ArrayObject *) PyErr_NoMemory ());
      TRY (memcpy (result->data, y->data, y->size * datasize),
         (ArrayObject *) ERRSS ("memcpy failed in concatenate."));
      if (freey >= 0)
         freeArray (y, freey);
      return result;
      }
   if (x->typecode != y->typecode)
      return (ArrayObject *) NULL;
   TRY (result = allocateArray (x->size + y->size, tc, n), 
      (ArrayObject *) PyErr_NoMemory ());
   TRY (memcpy (result->data, x->data, x->size * datasize),
      (ArrayObject *) ERRSS ("memcpy failed in concatenate."));
   TRY (memcpy ( (void *) ( (Uchar *) (result->data) + x->size * datasize),
      y->data, y->size * datasize),
      (ArrayObject *) ERRSS ("memcpy failed in concatenate."));
   if (freex >= 0)
      freeArray (x, freex);
   if (freey >= 0)
      freeArray (y, freey);
   return result;
   }

#ifdef INCLUDE_EXTRA_GISTCODE

static ArrayObject * cumsum (ArrayObject * i, int freei, int n) {
   /* compute the cumulative sum of an integer array object */
   ArrayObject * res ;
   int * src,
       * tar,
       pre;
   if (i == (ArrayObject *) NULL)
      return (ArrayObject *) NULL;
   src = (int *) (i->data);
   TRY (res = allocateArray (i->size, 'i', n), 
      (ArrayObject *) PyErr_NoMemory ());
   for (src = (int *) (i->data), tar = (int *) (res->data), pre = 0;
        src < (int *) (i->data) + i->size; src ++, tar++) {
      * tar += * src + pre;
      pre = * tar;
      }
   if (freei >= 0)
      freeArray (i, freei);
   return res;
   }

static ArrayObject * equal (ArrayObject * i, ArrayObject * j,
   int freei, int freej, int n) {
   /* Compare two integer arrays for equality, return an unsigned *
    * character array of results.                                 */
   int * src1,
       * src2;
   ArrayObject * res;
   Uchar * tar;
   if (i == (ArrayObject *)NULL || j == (ArrayObject *)NULL ||
      i->size != j->size)
      return (ArrayObject *) NULL;
   TRY (res = allocateArray (i->size, 'b', n), 
      (ArrayObject *) PyErr_NoMemory ());
   for (src1 = (int *) (i->data), src2 = (int *) (j->data),
        tar = (Uchar *) (res->data); src1 < (int *) (i->data) + i->size;
        src1 ++, src2 ++, tar ++)
      * tar = (Uchar) (* src1 == * src2);
   if (freej >= 0)
      freeArray (j, freej);
   if (freei >= 0)
      freeArray (i, freei);
   return res ;
   }

static ArrayObject * greater (ArrayObject * d, double v, int freed, int n) {
   /* compare an array object of type 'd' with a double v; return an
    * array  of Uchar the same size with 1's where the compare succeeds. */
   double * dr ;
   Uchar * c ;
   ArrayObject * res ;
   if (d == (ArrayObject *) NULL)
      return (ArrayObject *) NULL;
   TRY (res = allocateArray (d->size, 'b', n), 
      (ArrayObject *) PyErr_NoMemory ());
   for (dr = (double *) (d->data), c = (Uchar *) (res->data);
        dr < (double *) (d->data) + d->size; dr ++, c++)
      * c = (Uchar) (* dr > v);
   if (freed >= 0)
      freeArray (d, freed);
   return res ;
   }

static ArrayObject * greater_equal (ArrayObject * d, double v,
   int freed, int n) {
   /* compare an array object of type 'd' with a double v; return an
    * array  of Uchar the same size with 1's where the compare succeeds. */
   double * dr ;
   Uchar * c ;
   ArrayObject * res ;
   if (d == (ArrayObject *) NULL)
      return (ArrayObject *) NULL;
   TRY (res = allocateArray (d->size, 'b', n), 
      (ArrayObject *) PyErr_NoMemory ());
   for (dr = (double *) (d->data), c = (Uchar *) (res->data);
        dr < (double *) (d->data) + d->size; dr ++, c++)
      * c = (Uchar) (* dr >= v);
   if (freed >= 0)
      freeArray (d, freed);
   return res ;
   }

static ArrayObject * less (ArrayObject * i, ArrayObject * j,
   int freei, int freej, int n) {
   int * src1,
       * src2;
   ArrayObject * res;
   Uchar * tar;
   if (i == (ArrayObject *) NULL || j == (ArrayObject *) NULL ||
      i->size != j->size)
      return (ArrayObject *) NULL;
   TRY (res = allocateArray (i->size, 'b', n), 
      (ArrayObject *) PyErr_NoMemory ());
   for (src1 = (int *) (i->data), src2 = (int *) (j->data),
        tar = (Uchar *) (res->data); src1 < (int *) (i->data) + i->size;
        src1 ++, src2 ++, tar ++)
      * tar = (Uchar) (* src1 < * src2);
   if (freej >= 0)
      freeArray (j, freej);
   if (freei >= 0)
      freeArray (i, freei);
   return res ;
   }

static ArrayObject * logical_and (ArrayObject * a, ArrayObject * b,
   int freea, int freeb, int n) {
   /* Take the logical and of two Uchar arrays */
   ArrayObject * res;
   Uchar * src1,
         * src2,
         * tar;
   if (a == (ArrayObject *) NULL || b == (ArrayObject *) NULL ||
      a->size != b->size || a->typecode != 'b' || b->typecode != 'b')
      return (ArrayObject *) NULL;
   src1 = (Uchar *) (a->data);
   src2 = (Uchar *) (b->data);
   if (freea == n) /* can use a as target */ {
      tar = (Uchar *) (a->data);
      freea = -1;
      }
   else if (freeb == n) /* can use b as target */ {
      tar = (Uchar *) (b->data);
      freeb = -1;
      }
   else { /* need new result as target */
      TRY (res = allocateArray (a->size, 'b', n),
         (ArrayObject *) PyErr_NoMemory ());
      tar = (Uchar *) (res->data);
      }
   /* We may be using res uninitialized here -- MDH */
   for (; tar < (Uchar *) (res->data) + res->size; src1 ++, src2 ++, tar ++)
      * tar = * src1 && * src2;
   if (freea >= 0)
      freeArray (a, freea);
   if (freeb >= 0)
      freeArray (b, freea);
   return (res);
   }
#endif

static ArrayObject * logical_not (ArrayObject * b, int freeb, int n) {
   /* Take the logical not of an int or Uchar array and return Uchar */
   ArrayObject * res;
   Uchar * usrc,
         * tar;
   int * isrc;
   if (b == (ArrayObject *) NULL)
      return (ArrayObject *) NULL;
   if (freeb >= 0 && freeb == n) {
      /* do it in place */
      if (b->typecode == 'b') {
         for (usrc = (Uchar *) (b->data);
              usrc < (Uchar *) (b->data) + b->size; usrc ++)
            * usrc = (Uchar) ! * usrc;
         return b;
         }
      else if (b->typecode == 'i') {
         for (tar = (Uchar *) (b->data), isrc = (int *) (b->data);
              tar < (Uchar *) (b->data) + b->size; isrc ++, tar ++)
            * tar = (Uchar) ! * isrc;
         b->typecode = 'b';
         return b;
         }
      else
         return (ArrayObject *) NULL;
      }
   else {
      TRY (res = allocateArray (b->size, 'b', n), 
         (ArrayObject *) PyErr_NoMemory ());
      if (b->typecode == 'b')
         for (usrc = (Uchar *) (b->data), tar = (Uchar *) (res->data);
              usrc < (Uchar *) (b->data) + b->size; usrc ++, tar++)
            * tar = (Uchar) ! * usrc;
      else if (b->typecode == 'i')
         for (isrc = (int *) (b->data), tar = (Uchar *) (res->data);
              isrc < (int *) (b->data) + b->size; isrc ++, tar++)
            * tar = (Uchar) ! * isrc;
      else {
         freeArray (res, n);
         return (ArrayObject *) NULL;
         }
      if (freeb >= 0)
         freeArray (b, freeb);
      return res ;
      }
   }

#ifdef INCLUDE_EXTRA_GISTCODE
static ArrayObject * not_equal (ArrayObject * i, int j, int freei, int n) {
   /* compare integer array against integer for not equal */
   int * src;
   ArrayObject * res;
   Uchar * tar;
   if (i == (ArrayObject *) NULL)
      return (ArrayObject *) NULL;
   TRY (res = allocateArray (i->size, 'b', n), 
      (ArrayObject *) PyErr_NoMemory ());
   for (src = (int *) (i->data), tar = (Uchar *) (res->data);
        src < (int *) (i->data) + i->size;
        src ++, tar ++)
      * tar = (Uchar) (* src != j);
   if (freei >= 0)
      freeArray (i, freei);
   return res ;
   }
#endif

static ArrayObject * SimpleHist (ArrayObject * i, int freei, int n) {
   /* returns an array which counts the number of occurrences of
    * each element of i, which must consist of non-negative
    * integers.                                             */
   ArrayObject * res ;
   int * src,
       * tar;
   int max;
   if (i == (ArrayObject *) NULL) {
      return (ArrayObject *) NULL;
      }
   for (src = (int *) (i->data), max = * (src ++);
        src < (int *) (i->data) + i->size; src++)
      if (* src > max)
         max = * src;
      else if (* src < 0) {
         return (ArrayObject *) NULL;
         }
   TRY (res = allocateArray (max + 1, 'i', n), 
      (ArrayObject *) PyErr_NoMemory ());
   for (src = (int *) (i->data), tar = (int *) (res->data);
        src < (int *) (i->data) + i->size; src++)
      tar [* src] += 1;
   if (freei >= 0)
      freeArray (i, freei);
   return res;
   }

static ArrayObject * take (ArrayObject * a, ArrayObject * i, int nelts,
   int freea, int freei, int n) {
   /* Return an array the same size as i, which consists of the
    * elements of a subscripted by the elements of i. if nelts is
    * not equal to 1, this acts as if a is an array of things
    * nelts long, and does the extraction accordingly. Currently
    * this is only implemented for type 'd'.
    * returns NULL in the event of any error.                      */
   ArrayObject * res;
   double * dtar,
          * dsrc;
   int * itar,
       * isrc;
   int * ind;
   Uchar * utar,
         * usrc;
   int j;
   if (a == (ArrayObject *) NULL || nelts <= 0 ||
       i == (ArrayObject *) NULL || i->size == 0) {
      return (ArrayObject *) NULL ;
      }
   TRY (res = allocateArray (nelts * i->size, a->typecode, n), 
      (ArrayObject *) PyErr_NoMemory ());
   ind = (int *) (i->data);
   switch (a->typecode) {
      case 'b' :
         for (usrc = (Uchar *) (a->data), utar = (Uchar *) (res->data);
              ind < (int *) (i->data) + i->size; utar ++, ind ++)
            if (* ind >= 0 && * ind < a->size)
               * utar = usrc [* ind];
            else {
               freeArray (res, n);
               return (ArrayObject *) NULL;
               }
         break ;
      case 'i' :
         for (isrc = (int *) (a->data), itar = (int *) (res->data);
              ind < (int *) (i->data) + i->size; itar ++, ind ++)
            if (* ind >= 0 && * ind < a->size)
               * itar = isrc [* ind];
            else {
               freeArray (res, n);
               return (ArrayObject *) NULL;
               }
         break ;
      case 'd' :
         if (nelts == 1)
            for (dsrc = (double *) (a->data), dtar = (double *) (res->data);
                 ind < (int *) (i->data) + i->size; dtar ++, ind ++)
               if (* ind >= 0 && * ind < a->size)
                  * dtar = dsrc [* ind];
               else {
                  freeArray (res, n);
                  return (ArrayObject *) NULL;
                  }
         else if (nelts > 0)
            for (dsrc = (double *) (a->data), dtar = (double *) (res->data);
                 ind < (int *) (i->data) + i->size; dtar += nelts, ind ++)
               if (* ind >= 0 && * ind * nelts < a->size)
                  for (j = 0; j < nelts; j++)
                     dtar [j] = dsrc [* ind * nelts + j];
               else {
                  freeArray (res, n);
                  return (ArrayObject *) NULL;
                  }
         else {
            freeArray (res, n);
            return (ArrayObject *) NULL;
            }
         break ;
      default :
         freeArray (res, n);
         return (ArrayObject *) NULL ;
      }
   if (freea >= 0)
      freeArray (a, freea);
   if (freei >= 0)
      freeArray (i, freea);
   return res ;
   }

#ifdef INCLUDE_EXTRA_GISTCODE
static ArrayObject * WeightedHist (ArrayObject *i, ArrayObject *w,
   int freei, int freew, int n) {
   /* weighted histogram: the kth entry of the result contains the     *
    * sum of all entries in w corresponding to entries in i containing *
    * the value k. (SimpleHist consists of all weights being 1.)       */
   ArrayObject * res ;
   int * src,
       * tar;
   Uchar * wgt;
   int max;
   if (i == (ArrayObject *) NULL || w == (ArrayObject *) NULL ||
      i->size > w->size)
      return (ArrayObject *) NULL;
   for (src = (int *) (i->data), max = * (src ++);
        src < (int *) (i->data) + i->size; src++)
      if (* src > max)
         max = * src;
      else if (* src < 0)
         return (ArrayObject *) NULL;
   TRY (res = allocateArray (max + 1, 'i', n), 
      (ArrayObject *) PyErr_NoMemory ());
   for (src = (int *) (i->data), tar = (int *) (res->data),
        wgt = (Uchar *) (w->data);
        src < (int *) (i->data) + i->size; src++, wgt++)
      tar [* src] += (int) (* wgt);
   if (freew >= 0)
      freeArray (w, freew);
   if (freei >= 0)
      freeArray (i, freei);
   return res;
   }
#endif

/*           _slice2_part is a big helper routine for slice2              */

/************************************************************************/

static int _slice2_part (ArrayObject * xyzc, ArrayObject * keep,
   ArrayObject * next, ArrayObject * dp, ArrayObject * prev,
   ArrayObject * last, ArrayObject * valc, ArrayObject ** xyzc_new,
   ArrayObject ** nvertc, ArrayObject ** valc_new, int freexyzc, 
   int freevalc, char atype) {
   /* auxiliary routine for slice2. returns 0 if there was any kind
    * of error, 1 otherwise.                                        */
   int nlist0, nlist1, i, j, i0, i1, listtmp;
   double dpl, dpu, dpu_dpl;
   ArrayObject * list = (ArrayObject *) NULL,
               * list0 = (ArrayObject *) NULL,
               * list1 = (ArrayObject *) NULL,
               * mask = (ArrayObject *) NULL,
               * ndxs = (ArrayObject *) NULL,
               * valc0 = (ArrayObject *) NULL,
               * valc1 = (ArrayObject *) NULL,
               * xold = (ArrayObject *) NULL,
               * xyz0 = (ArrayObject *) NULL,
               * xyz1 = (ArrayObject *) NULL;
   Uchar * keepd,
         * maskd;
   int * nextd=0,
       * nvertcd=0,
       * prevd=0,
       * list0d=0,
       * list1d=0,
       * listd=0,
       * xoldd=0;
   /*#######*/int * ndxsd;
   double * xyz0d=0,
          * valcd = (double *) NULL,
          * valc0d = (double *) NULL,
          * valc1d = (double *) NULL,
          * dpd=0,
          * xyzcd=0,
          * xyz1d=0,
          * xyzc_newd=0,
          * valc_newd = (double *) NULL;
   Uchar * valcc = (Uchar *) NULL,
         * valc0c = (Uchar *) NULL,
         * valc1c = (Uchar *) NULL,
         * valc_newc = (Uchar *) NULL;

   /* i = dmalloc_verify (0); */
   * xyzc_new = (ArrayObject *) NULL;
   * nvertc = (ArrayObject *) NULL;
   if (valc) {
      /* Don't touch the following if no values are coming in!! */
      * valc_new = (ArrayObject *) NULL;
      if (atype == 'd')
         valcd = (double *) (valc->data);
      else if (atype == 'b')
         valcc = (Uchar *) (valc->data);
      }
   dpd = (double *) (dp->data);
   xyzcd = (double *) (xyzc->data);
   TRY (mask = allocateArray (keep->size, 'b', 1), (int) NULL);
   maskd = (Uchar *) (mask->data);
   keepd = (Uchar *) (keep->data);
   nextd = (int *) (next->data);
   prevd = (int *) (prev->data);
   for (nlist0 = 0, nlist1 = 0, i = 0; i < keep->size; i ++) {
      i0 = ! keepd [i] && keepd [nextd [i]];
      i1 = ! keepd [i] && keepd [prevd [i]];
      nlist0 += i0;
      nlist1 += i1;
      maskd [i] = keepd [i] + i0 + i1;
      }
   if (nlist0 != 0) {
      TRY (list0 = allocateArray (nlist0, 'i', 1), (int) NULL);
      list0d = (int *) (list0->data);
      for (j = 0, i = 0; i < keep->size; i++)
         if ( ! keepd [i] && keepd [nextd [i]])
            list0d [j ++] = i;
      TRY (xyz0 = allocateArray (3 * nlist0, 'd', 1), (int) NULL);
      xyz0d = (double *) (xyz0->data);
      if (valc != (ArrayObject *)NULL) {
         TRY (valc0 = allocateArray (nlist0, atype, 1), (int) NULL);
         if (atype == 'd')
            valc0d = (double *) (valc0->data);
         else if (atype == 'b')
            valc0c = (Uchar *) (valc0->data);
         }
      for (i = 0; i < nlist0; i++) {
         dpl = dpd [list0d [i]];
         listtmp = nextd [list0d [i]];
         dpu = dpd [listtmp];
         dpu_dpl = dpu - dpl;
         xyz0d [3 * i] = (xyzcd [3 * list0d [i]] * dpu -
            xyzcd [3 * listtmp] * dpl) / dpu_dpl;
         xyz0d [3 * i + 1] = (xyzcd [3 * list0d [i] + 1] * dpu -
            xyzcd [3 * listtmp + 1] * dpl) / dpu_dpl;
         xyz0d [3 * i + 2] = (xyzcd [3 * list0d [i] + 2] * dpu -
            xyzcd [3 * listtmp + 2] * dpl) / dpu_dpl;
         if (valc != (ArrayObject *)NULL) {
            if (atype == 'd')
               valc0d [i] = (valcd [list0d [i]] * dpu -
                  valcd [listtmp] * dpl) / dpu_dpl;
            else if (atype == 'b')
               valc0c [i] = (valcc [list0d [i]] * dpu -
                  valcc [listtmp] * dpl) / dpu_dpl;
            }
         }
      }
   if (nlist1 != 0) {
      TRY (list1 = allocateArray (nlist1, 'i', 1), (int) NULL);
      list1d = (int *) (list1->data);
      for (j = 0, i = 0; i < keep->size; i++)
         if (! keepd [i] && keepd [prevd [i]])
            list1d [j ++] = i;
      TRY (xyz1 = allocateArray (3 * nlist1, 'd', 1), (int) NULL);
      xyz1d = (double *) (xyz1->data);
      if (valc != (ArrayObject *)NULL) {
         TRY (valc1 = allocateArray (nlist1, atype, 1), (int) NULL);
         if (atype == 'd')
            valc1d = (double *) (valc1->data);
         else if (atype == 'b')
            valc1c = (Uchar *) (valc1->data);
         }
      for (i = 0; i < nlist1; i++) {
         dpl = dpd [list1d [i]];
         listtmp = prevd [list1d [i]];
         dpu = dpd [listtmp];
         dpu_dpl = dpu - dpl;
         xyz1d [3 * i] = (xyzcd [3 * list1d [i]] * dpu -
            xyzcd [3 * listtmp] * dpl) / dpu_dpl;
         xyz1d [3 * i + 1] = (xyzcd [3 * list1d [i] + 1] * dpu -
            xyzcd [3 * listtmp + 1] * dpl) / dpu_dpl;
         xyz1d [3 * i + 2] = (xyzcd [3 * list1d [i] + 2] * dpu -
            xyzcd [3 * listtmp + 2] * dpl) / dpu_dpl;
         if (valc != (ArrayObject *)NULL) {
            if (atype == 'd')
               valc1d [i] = (valcd [list1d [i]]  * dpu -
                  valcd [listtmp] * dpl) / dpu_dpl;
            else if (atype == 'b')
               valc1c [i] = (valcc [list1d [i]]  * dpu -
                  valcc [listtmp] * dpl) / dpu_dpl;
            }
         }
      }
   TRY (list = allocateArray (mask->size, 'i', 1), (int) PyErr_NoMemory ());
   listd = (int *) (list->data);
   for (i = 0, i0 = 0; i < mask->size; i ++) {
      i0 += maskd [i];
      listd [i] = i0;
      }
   TRY (xold = allocateArray (listd [list->size - 1], 'i', 1), (int) NULL);
   xoldd = (int *) (xold->data);
   for (i = 0; i < mask->size; i ++) {
      if ( maskd [i] != 0) 
         xoldd [listd [i] - 1] = i;
      if ( maskd [i] == 2)
         xoldd [listd [i] - 2] = i;
      }
   /* Note: allocate return values in caller's list. */
   TRY (* xyzc_new = allocateArray (3 * xold->size, 'd', 0), (int) NULL);
   xyzc_newd = (double *) ( (* xyzc_new)->data);
   if (valc != (ArrayObject *)NULL) {
      TRY (* valc_new = allocateArray (xold->size, atype, 0), (int) NULL);
      if (atype == 'd')
         valc_newd = (double *) ( (* valc_new)->data);
      else if (atype == 'b')
         valc_newc = (Uchar *) ( (* valc_new)->data);
      }
   for (i = 0; i < xold->size; i++) {
      xyzc_newd [3 * i] = xyzcd [3 * xoldd [i]];
      xyzc_newd [3 * i + 1] = xyzcd [3 * xoldd [i] + 1];
      xyzc_newd [3 * i + 2] = xyzcd [3 * xoldd [i] + 2];
      if (valc != (ArrayObject *)NULL)  {
         if (atype == 'd')
            valc_newd [i] = valcd [xoldd [i]];
         else if (atype == 'b')
            valc_newc [i] = valcc [xoldd [i]];
         }
      }
   for (i = 0; i < nlist0; i ++) {
      xyzc_newd [3 * listd [list0d [i]] - 3] = xyz0d [3 * i];
      xyzc_newd [3 * listd [list0d [i]] - 2] = xyz0d [3 * i + 1];
      xyzc_newd [3 * listd [list0d [i]] - 1] = xyz0d [3 * i + 2];
      if (valc != (ArrayObject *)NULL)  {
         if (atype == 'd')
            valc_newd [listd [list0d [i]] - 1] = valc0d [i];
         else if (atype == 'b')
            valc_newc [listd [list0d [i]] - 1] = valc0c [i];
         }
      }
   freeArray (list0, 1);
   freeArray (xyz0, 1);
   if (valc != (ArrayObject *)NULL)
      freeArray (valc0, 1);
   for (i = 0; i < nlist1; i ++) {
      xyzc_newd [3 * (listd [list1d [i]] - maskd [list1d [i]])] =
         xyz1d [3 * i];
      xyzc_newd [3 * (listd [list1d [i]] - maskd [list1d [i]]) + 1] =
         xyz1d [3 * i + 1];
      xyzc_newd [3 * (listd [list1d [i]] - maskd [list1d [i]]) + 2] =
         xyz1d [3 * i + 2];
      if (valc != (ArrayObject *)NULL)  {
         if (atype == 'd')
            valc_newd [listd [list1d [i]] - maskd [list1d [i]]] = valc1d [i];
         else if (atype == 'b')
            valc_newc [listd [list1d [i]] - maskd [list1d [i]]] = valc1c [i];
         }
      }
   freeArray (mask, 1);
   freeArray (list1, 1);
   freeArray (xyz1, 1);
   if (valc != (ArrayObject *)NULL)
      freeArray (valc1, 1);
   TRY (ndxs = allocateArray ( ( (int *) (last->data)) [last->size - 1],
      'i', 1), (int) NULL);
   ndxsd = (int *) (ndxs->data);
   for (i = 0, i0 = 0; i < last->size; i0 = ( (int *) (last->data)) [i ++])
      for (j = 0; i0 + j < ( (int *) (last->data)) [i]; j++)
         ndxsd [i0 + j] = i;
   /* N. B. the following removes ndxs and xold. */
   /* Note: allocate return values in caller's list. */
   TRY (* nvertc = SimpleHist (take (ndxs, xold, 1, FREE1, FREE1, 1), FREE1, 0),
      (int) NULL);
   nvertcd = (int *) ( (* nvertc)->data);

   if (freexyzc >= 0)
      freeArray (xyzc, freexyzc);
   if (freevalc >= 0)
      freeArray (valc, freevalc);
   clearFreeList (1); /* just in case; should be OK */
   return 1;
   }

/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */

static char *CheckDefaultWindow(void)
{
  int i;
  for (i=0 ; i<8 ; i++) if (ghDevices[i].drawing) {
    if (!ghDevices[i].display && !ghDevices[i].hcp) {
      Drauing *drawing= ghDevices[i].drawing;
      ghDevices[i].drawing= 0;
      GdKillDrawing(drawing);
      curElement= -1;
    }
  }
  if (curPlotter<0) {
    for (i=0 ; i<8 ; i++) if (ghDevices[i].drawing)
      return ("graphics window killed -- use window command to re-select");
    ghDevices[0].drawing=
      GdNewDrawing(defaultStyle? defaultStyle : "work.gs");
    curElement= -1;
    if (!ghDevices[0].drawing)
      return ("failed to create drawing -- Gist work.gs style sheet missing");
    ghDevices[0].doLegends= defaultLegends;

    gist_private_map = gist_rgb_hint = 0;
    ghDevices[0].display=
      GpFXEngine(windowNames[0], 0, defaultDPI, (char *)0);
    if (!ghDevices[0].display)
      return ("failed to open X display or create X window");

    curPlotter= 0;
    GhSetPlotter(0);
  }
  return NULL;
}

static void CheckDefaultPalette (void)
{
  GpColorCell *palette;
  GhGetPalette (curPlotter, &palette);
  if (!palette)
    paletteSize = GhReadPalette (curPlotter,
       defaultPalette ? defaultPalette : "earth.gp", &palette, maxColors);
}

static int CheckPalette (void)
{
  int n = curPlotter;
  if (n >= 0 && !ghDevices[n].hcp) {
    if (!hcpDefault) {
      if (!SetHCPDefault ())
	return 0; /* Failure */
    }
    SetHCPPalette ();
  }
  return 1; /* Success */
}

/* Shutdown routine */
static void CleanUpGraphics (void)
{
  int n;
  if (hcpDefault) {
    GpKillEngine (hcpDefault);
  }
  for (n = 7; n >= 0; n--) {
    if (ghDevices[n].display)
      GpKillEngine (ghDevices[n].display);
    if (ghDevices[n].hcp)
      GpKillEngine (ghDevices[n].hcp);
  }
  if ( gistpath ){
    gistPathDefault = oldgistpath;
    free(gistpath);
  }
}

/* Search for 0-origin zone index of (xx,yy) in mesh (x,y).
 * reg==0 is allowed, and reg need not have overreach zones.
 * Return -1 if (xx,yy) not within mesh, zone index otherwise.
 */
static long FindMeshZone (double xx, double yy, double *x, double *y,
			  int *reg, long ix, long jx)
{
  int wind = 0;		/* winding number of zone around (xx,yy) */
  long i, i0, ijx = ix * jx;
  /* coordinates of zone corners relative to (xx,yy) */
  double x00, y00, x01, y01, x10, y10, x11, y11;

  i0 = 0;
  for (i = ix + 1; i < ijx; i++) {
    if ((++i0) == ix) {
      i0 = 1;
      i++;
    }			/* skip 1st point of each row */
    if (reg && !reg[i])
      continue;			/* skip non-existent zones */
    /* initialize 00 and 10 points at beginning of a row or
       after a non-existent zone */
    x00 = x[i - ix - 1] - xx;
    y00 = y[i - ix - 1] - yy;
    x10 = x[i - 1] - xx;
    y10 = y[i - 1] - yy;

    x01 = x[i - ix] - xx;
    y01 = y[i - ix] - yy;
    x11 = x[i] - xx;
    y11 = y[i] - yy;

    /* hopefully spend most of the time in these fast scan loops
       with all four points in one quadrant relative to (xx,yy) */
    if (ix < 8)
      goto test;		/* pointless if rows too short */
    if (x11 < 0.0) {
      if (x01 < 0.0 && x10 < 0.0 && x00 < 0.0) {
      left:
	for (i++;; i++) {
	  if (i >= ijx)
	    goto done;
	  if ((++i0) == ix) {
	    i0 = 0;
	    goto abort;
	  }
	  if (reg && !reg[i])
	    goto abort;
	  x01 = x[i - ix] - xx;
	  x11 = x[i] - xx;
	  if (x01 >= 0.0 || x11 >= 0.0)
	    break;
	}
	y00 = y[i - ix - 1] - yy;
	y10 = y[i - 1] - yy;
	y01 = y[i - ix] - yy;
	y11 = y[i] - yy;
	if (y11 < 0.0) {
	  if (y01 < 0.0 && y10 < 0.0 && y00 < 0.0)
	    goto low;
	} else {
	  if (y01 >= 0.0 && y10 >= 0.0 && y00 >= 0.0)
	    goto high;
	}
	x00 = x[i - ix - 1] - xx;
	x10 = x[i - 1] - xx;
	goto test;
      }
    } else {
      if (x01 >= 0.0 && x10 >= 0.0 && x00 >= 0.0) {
      right:
	for (i++;; i++) {
	  if (i >= ijx)
	    goto done;
	  if ((++i0) == ix) {
	    i0 = 0;
	    goto abort;
	  }
	  if (reg && !reg[i])
	    goto abort;
	  x01 = x[i - ix] - xx;
	  x11 = x[i] - xx;
	  if (x01 < 0.0 || x11 < 0.0)
	    break;
	}
	y00 = y[i - ix - 1] - yy;
	y10 = y[i - 1] - yy;
	y01 = y[i - ix] - yy;
	y11 = y[i] - yy;
	if (y11 < 0.0) {
	  if (y01 < 0.0 && y10 < 0.0 && y00 < 0.0)
	    goto low;
	} else {
	  if (y01 >= 0.0 && y10 >= 0.0 && y00 >= 0.0)
	    goto high;
	}
	x00 = x[i - ix - 1] - xx;
	x10 = x[i - 1] - xx;
	goto test;
      }
    }

    if (y11 < 0.0) {
      if (y01 < 0.0 && y10 < 0.0 && y00 < 0.0) {
      low:
	for (i++;; i++) {
	  if (i >= ijx)
	    goto done;
	  if ((++i0) == ix) {
	    i0 = 0;
	    goto abort;
	  }
	  if (reg && !reg[i])
	    goto abort;
	  y01 = y[i - ix] - yy;
	  y11 = y[i] - yy;
	  if (y01 >= 0.0 || y11 >= 0.0)
	    break;
	}
	x00 = x[i - ix - 1] - xx;
	x10 = x[i - 1] - xx;
	x01 = x[i - ix] - xx;
	x11 = x[i] - xx;
	if (x11 < 0.0) {
	  if (x01 < 0.0 && x10 < 0.0 && x00 < 0.0)
	    goto left;
	} else {
	  if (x01 >= 0.0 && x10 >= 0.0 && x00 >= 0.0)
	    goto right;
	}
	y00 = y[i - ix - 1] - yy;
	y10 = y[i - 1] - yy;
	goto test;
      }
    } else {
      if (y01 >= 0.0 && y10 >= 0.0 && y00 >= 0.0) {
      high:
	for (i++;; i++) {
	  if (i >= ijx)
	    goto done;
	  if ((++i0) == ix) {
	    i0 = 0;
	    goto abort;
	  }
	  if (reg && !reg[i])
	    goto abort;
	  y01 = y[i - ix] - yy;
	  y11 = y[i] - yy;
	  if (y01 < 0.0 || y11 < 0.0)
	    break;
	}
	x00 = x[i - ix - 1] - xx;
	x10 = x[i - 1] - xx;
	x01 = x[i - ix] - xx;
	x11 = x[i] - xx;
	if (x11 < 0.0) {
	  if (x01 < 0.0 && x10 < 0.0 && x00 < 0.0)
	    goto left;
	} else {
	  if (x01 >= 0.0 && x10 >= 0.0 && x00 >= 0.0)
	    goto right;
	}
	y00 = y[i - ix - 1] - yy;
	y10 = y[i - 1] - yy;
	goto test;
      }
    }

  test:
    /* compute counterclockwise crossings of x==xx for each of the
       four edges of the zone */
    wind = 0;
    if ((x00 < 0.0) ^ (x01 < 0.0)) {
      if (x00 * y01 > x01 * y00)
	wind++;
      else
	wind--;
    }
    if ((x01 < 0.0) ^ (x11 < 0.0)) {
      if (x01 * y11 > x11 * y01)
	wind++;
      else
	wind--;
    }
    if ((x11 < 0.0) ^ (x10 < 0.0)) {
      if (x11 * y10 > x10 * y11)
	wind++;
      else
	wind--;
    }
    if ((x10 < 0.0) ^ (x00 < 0.0)) {
      if (x10 * y00 > x00 * y10)
	wind++;
      else
	wind--;
    }
    if (wind)
      break;			/* this zone winds around (xx,yy) */

  abort:
    continue;
  }
done:

  return wind ? i : -1;
}

/* Used only by plq() */
static void ForceNewline (void)
{
  if (printNow) {
    if (printLines++ < maxPrintLines)
      RawPrinter (printBuf);
    printNow = permitNow = 0;
    printBuf[0] = '\0';
  }
}

/* Return name of current hardcopy file. */
static char *GetHCPname (int n)
{
  if (n >= 0 && n < 8)
    return ghDevices[n].hcp ? hcpNames[n] : hcpNames[8];
  else
    return hcpNames[8];
}

static void GetPCrange (double *zmn, double *zmx, double *z, int *reg,
			int region, long iMax, long jMax)
{
  double zmin = 0.0, zmax = 0.0;
  long i, j, k;
  int have_min_max = 0;
  
  for (i = 0, k = 0; i < iMax; i ++)  {
     for (j = 0; j < jMax; j ++) {
        if (reg ? (region ?
                   i != iMax - 1 && j != jMax - 1 &&
                   (reg [k] == region || reg [k+1] == region ||
                    reg [k + jMax] == region || reg [k + jMax + 1] == region) :
                   (reg [k] || (i != iMax - 1 && j != jMax - 1 &&
                    (reg [k+1] || reg [k + jMax] || reg [k + jMax + 1])))) : 1) {
           if (! have_min_max) {
              have_min_max = 1;
              zmin = zmax = z [k];
           }
           else {
              if (z [k] < zmin) zmin = z [k];
              else if (z [k] > zmax) zmax = z [k];
           }
        }
        k ++;
     }
  }

  if (!have_min_max)  {
     ERRMSG ( "Unable to find maximum and minimum of data??");
  }
  *zmn = zmin;
  *zmx = zmax;
}

/* Used by setkw_fonttype() */
static int GetTypeface (char *s, int *f)
{
  int face = 0;
  while (*s) {
    if (*s == 'B' && !(face & T_BOLD))
      face |= T_BOLD;
    else if (*s == 'I' && !(face & T_ITALIC))
      face |= T_ITALIC;
    else  {
      return (int) ERRSS ( "illegal font keyword suffix -- B is bold, I is italic");
    }
    s++;
  }
  *f = face;
  return 1;
}

static void GetZCrange(double *zmn, double *zmx, double *z, int *reg,
 int region, long iMax, long jMax, int zCompressed)
{
  double zmin= 0.0, zmax= 0.0;
  long i, j= iMax-1;
  long len= (zCompressed? j : iMax)*(jMax-1);

  if (zCompressed) {
    long len= (iMax-1)*(jMax-1);
    if (reg) reg+= iMax+1;
    for (i=0 ; i<len ; i++) {	/* first loop finds first z */
      if (reg? (region? (*reg==region) : (*reg!=0)) : 1) {
	zmin= zmax= z[i];
	break;
      }
      if (reg) {
	if (!(--j)) { reg+= 2; j= iMax-1; }
	else reg++;
      }
    }
    if (reg) {
      if (!(--j)) { reg+= 2; j= iMax-1; }
      else reg++;
    }
    for (i++ ; i<len ; i++) {	/* second loop judges extreme values */
      if (reg? (region? (*reg==region) : (*reg!=0)) : 1) {
	if (zmin>z[i]) zmin= z[i];
	else if (zmax<z[i]) zmax= z[i];
      }
      if (reg) {
	if (!(--j)) { reg+= 2; j= iMax-1; }
	else reg++;
      }
    }

  } else {
    z+= iMax+1;			/* set_yx guarantees at least 2-by-2 */
    if (reg) reg+= iMax+1;
    for (i=1 ; i<len ; i++) {	/* first loop finds first z */
      if (--j) {
	if (reg? (region? (*reg==region) : (*reg!=0)) : 1) {
	  zmin= zmax= z[i];
	  break;
	}
      } else {
	j= iMax;
      }
    }
    for (i++ ; i<len ; i++) {	/* second loop judges extreme values */
      if (--j) {
	if (reg? (region? (*reg==region) : (*reg!=0)) : 1) {
	  if (zmin>z[i]) zmin= z[i];
	  else if (zmax<z[i]) zmax= z[i];
	}
      } else {
	j= iMax;
      }
    }
  }

  *zmn= zmin;
  *zmx= zmax;
}

/* Called from bytscl, pli, plf, and plfp */
static int GrabByteScale (
  PyObject *kwt[], char *keywrds[], double *scale, double *offset,
  double *zn, double *zx, double *z, int *reg,
  int region, long iMax, long jMax, int zCompressed)
{
  int top;
  double zmin = 0.0, zmax = 0.0;
  int minGiven = 0, maxGiven = 0;

  /* get any parameters specified as keywords */
  if (kwt[0]) {
    SETKW(kwt[0],  top,    setkw_integer, keywrds[0]);
  } else {
    top = paletteSize - 1;
  }

  if (kwt[1] && kwt[1] != Py_None) {
    minGiven = 1;
    SETKW(kwt[1],  zmin,    setkw_double, keywrds[1]);
  }
  if (kwt[2] && kwt[2] != Py_None) {
    maxGiven = 1;
    SETKW(kwt[2],  zmax,    setkw_double, keywrds[2]);
  }

  /* fill in zmin and zmax from data if not specified */
  if (!minGiven || !maxGiven) {
    double zmn, zmx;
    GetZCrange(&zmn, &zmx, z, reg, region, iMax, jMax, zCompressed);
    if (!minGiven) zmin = zmn;
    if (!maxGiven) zmax = zmx;
  }

  /* adjust zmin and zmax to avert numerical catastrophes */
  if (zmin > zmax) { double tmp = zmin; zmin = zmax; zmax = tmp; }
  else if (zmin == zmax) {
    if (zmin > 0.0) { zmin = 0.9999*zmin; zmax = 1.0001*zmax; }
    if (zmin < 0.0) { zmin = 1.0001*zmin; zmax = 0.9999*zmax; }
    else { zmin = -0.0001; zmax = 0.0001; }
  }
  *zn = zmin;
  *zx = zmax;

  /* adjust top value if it is silly */
  if (top < 0 || top > 255) top = 255;

  /* (byte value)= scale*(z cut off at zmin, zmax)+offset
     maps from z to interval [0, top] */
  *scale = (double)top/(zmax-zmin);
  *offset = zmin-(0.4999/(*scale));	  /* zmin->0.5, zmax->top+0.5 */
  return 1;
}

/*  -------------------------------------------------------------------- */
/*  contour */

static char contour__doc__[] =
"[nc, yc, xc] = contour (level, z, y, x [, ireg] [, triangle = <vals>]\n"
"   [, region = num])\n"
"     returns the points on the contour curve that would have been\n"
"     plotted by plc.  Z, Y, X, and IREG are as for plc, and the\n"
"     triangle= and region= keywords are accepted and have the same\n"
"     meaning as for plc.  Unlike plc, the triangle array is an output\n"
"     as well as an input to contour; if supplied it may be modified\n"
"     to reflect any triangulations which were performed by contour.\n"
"\n"
"     either:\n"
"     LEVEL is a scalar z value to return the points at that contour\n"
"     level.  All such points lie on edges of the mesh.  If a contour\n"
"     curve closes, the final point is the same as the initial point\n"
"     (i.e.- that point is included twice in the returned list).\n"
"\n"
"     or:\n"
"     LEVEL is a pair of z values [z0,z1] to return the points of\n"
"     a set of polygons which outline the regions between the two\n"
"     contour levels.  These will include points on the mesh boundary\n"
"     which lie between the levels, in addition to the edge points\n"
"     for both levels.  The polygons are closed, simply connected,\n"
"     and will not contain more than about 4000 points (larger polygons\n"
"     are split into pieces with a few points repeated where the pieces\n"
"     join).\n"
"\n"
"     YC and XC are the output points on the curve(s), or None if there\n"
"     are no points. The return value NC is a list of the lengths of\n"
"     the polygons/polylines returned in (XC,YC), or None if there are\n"
"     none.  len(XC) == len(YC) == sum(NC).  For the level pair\n"
"     case, YC, XC, and NC are ready to be used as inputs to plfp.\n"
"\n"
"   KEYWORDS: triangle, region\n"
"\n"
"   SEE ALSO: plc, plfp\n";

#undef N_KEYWORDS
#define N_KEYWORDS 2
static char *cntrKeys[N_KEYWORDS+1]= { "triangle", "region", 0 };

static PyObject *contour (PyObject * self, PyObject * args, PyObject * kd)
{
  GaQuadMesh mesh;         /* object which gets sent to Gist      */
  int n,
      npt,
      i,
      nlevels,             /* number of contour levels (1 or 2)   */
      dims [2],            /* dimensions of mesh                  */
      ntotal,              /* number of coordinates sent back     */
      own_triangle = 0,    /* set to 1 if I create temp tri array */
      region = 0;
  long nparts,
       * np;
  PyObject * olevels,
           * zop,
           * kwt [NELT (cntrKeys) - 1],
           * retval;
  PyArrayObject * zap,
                * alevels;
  PyObject      * anp,
                * axcp,
                * aycp;
  double levels [2],
         * lev,
         * xcp,
         * ycp;
  double * z;
  char *errstr =
    "contour requires 2D arguments (levels, z [region = num, triangle = <vals>] )";

  if (!pyMsh.y)  {
    return ERRSS ("contour: no current mesh - use plmesh(y, x) to initialize");
  }

  n = PyTuple_Size (args);
  /* contour (levels, z [, region = num]) */
  if (n != 2)  {
     return ERRSS ("contour requires 2 positional parameters (levels and z).");
  }
  BUILD_KWT (kd, cntrKeys, kwt);
  TRY ( PyArg_ParseTuple (args, "OO", &olevels, &zop),
     ERRSS ("contour: unable to parse arguments."));

  GET_ARR (zap, zop, PyArray_DOUBLE, 2, PyObject *);
  dims [0] = A_DIM (zap, 0);
  dims [1] = A_DIM (zap, 1);
  if (dims [0] != A_DIM (pyMsh.y, 0) || dims [1] != A_DIM (pyMsh.y, 1)) {
     return ERRSS ("z array must have same dimensions as mesh in contour.");
  }
  /* Provide a triangle if none supplied */
  if ( !pyMsh.triangle )
     TRY (pyMsh.triangle = (PyArrayObject *) PyArray_FromDims (2, dims, PyArray_SHORT),
        ERRSS ("contour: unable to create triangle array."));

  /* LLC:  
   *  1.5 has new first keyword "triangle" before "region", so
   *  change kwt and cntrKeys indices from 0 to 1.
   *  Add setz_mesh call. 
   */

  /* kwt[0] ("triangle=") is handled by setz_mesh. */
  /* Skip levels arg */

  {  PyObject * newargs;
     n = PyTuple_Size(args);
     TRY (newargs = PyTuple_GetSlice (args, 1, n), (PyObject *) NULL);
     TRY (setz_mesh (newargs, &zop, errstr, kwt[0]), (PyObject *) NULL);
  }
  if (!pyMsh.y)  {
    return ERRSS ("No current mesh - set (y, x) first");
  }

  gistD.region = 0;
  SETKW (kwt [1], gistD.region, setkw_integer, cntrKeys[1]);

  get_mesh (&mesh);

  /* Figure out the contour levels */
  if (isARRAY (olevels)) {
     GET_ARR (alevels, olevels, PyArray_DOUBLE, 1, PyObject *);
     lev = (double *) A_DATA (alevels);
     nlevels = A_SIZE (alevels);
     if (nlevels > 2) {
        clearArrayList ();
        return ERRSS ("contour: only 1 or 2 levels allowed."); 
     }
     for (i = 0; i < nlevels; i++)
        levels [i] = lev [i];
     removeFromArrayList ( (PyObject *) alevels);
     }
  /* levels argument can be scalar--allow Int or Float */
  else if (PyFloat_Check (olevels) || PyInt_Check (olevels)) {
     nlevels = 1;
     if (PyFloat_Check (olevels))
        levels [0] = (double) PyFloat_AsDouble (olevels);
     else
        levels [0] = (double) PyInt_AsLong (olevels);
     }
  else {
     clearArrayList ();
     return ERRSS ("contour: levels argument is wrong type."); 
  }

  z = (double *) A_DATA (zap);
  ntotal = (nlevels == 2) ?
     GcInit2 (&mesh, gistD.region, z, levels, 30L, &nparts):
     GcInit1 (&mesh, region, z, levels [0], &nparts);

  /* The following is necessary because I must own references to objects */
  /* that go in the list to be returned. Py_None will be INCREF'ed 3     */
  /* times when it is put on this list.                                  */
  if ( !(retval = Py_BuildValue ("[O,O,O]", Py_None, Py_None, Py_None))) {
     clearArrayList ();
     return ERRSS ("contour: unable to create return value list."); 
  }
  if (ntotal == 0) {
     clearArrayList ();
     /* return a list [None, None, None] */
     return retval; }
  
  /* the tedium of all the SAFE_FREEs and Py_DECREF's has been */
  /* subsumed by the memory management routines.               */
  /* create three arrays and their data, make sure DECREF'able */
  npt = (int) nparts;
  NEW_MEM (np, npt, long, PyObject *);
  RET_ARR ( anp, 1, &npt, PyArray_LONG, (char *) np, PyObject *);
  SET_OWN (anp);
  NEW_MEM (xcp, ntotal, double, PyObject *);
  RET_ARR ( axcp, 1, &ntotal, PyArray_DOUBLE, (char *) xcp, PyObject *);
  SET_OWN (axcp);
  NEW_MEM (ycp, ntotal, double, PyObject *);
  RET_ARR ( aycp, 1, &ntotal, PyArray_DOUBLE, (char *) ycp, PyObject *);
  SET_OWN (aycp);

  i = GcTrace (np, xcp, ycp);
  if ( i != ntotal) {
     clearArrayList ();
     clearMemList ();
     return ERRSS ("contour: GcTrace has failed.");
  }
  /* For some reason, if PyList_SetItem fails, it returns -1. */
  if (own_triangle) {
     Py_DECREF (kwt [0]);
  }
  if (PyList_SetItem (retval, 0, anp) < 0 ||
      PyList_SetItem (retval, 1, aycp) < 0 ||
      PyList_SetItem (retval, 2, axcp) < 0) {
     clearArrayList ();
     clearMemList ();
     return ERRSS ("contour was unable to build return list.");
  }
  removeFromArrayList ( (PyObject *) zap);
  mem_list_length = 0;
  array_list_length = 0;
  return retval;
}

static int MouseCallBack (Engine * engine, int system,
			  int release, double x, double y,
			  int butmod, double xn, double yn)
{
  int n = curPlotter;
  if (n < 0 || ghDevices[n].display != engine) {

    pyg_wait_flag = 0;
    /*  2/24/03 LLC:  
     *  Remove setting mouseError (Yorick does not use mouseError):
     *     mouseError = 1;
     *  Setting mouseError prevents data return for mouse.
     *  In Yorick, button press results in one call to MouseCallBack.
     *  On button release, one MouseCallBack call is made to capture
     *  end mouse coordinates, and another MouseCallBack is made with 
     *  engine = 0, at which time the results are pushed on the stack.
     */
    return 1;
  } else if (mouseError || release==-1) {
    pyg_wait_flag = 0;
    mouseError = 1;  /* Leave this one for now. LLC */
    return 1;
  }
  if (!release) {
    mouseSystem = system;
    mouseButton = butmod;
    mouseX0 = x;
    mouseY0 = y;
    mouseX0ndc = xn;
    mouseY0ndc = yn;
  } else {
    mouseModifier = butmod;
    mouseX1 = x;
    mouseY1 = y;
    mouseX1ndc = xn;
    mouseY1ndc = yn;
    pyg_wait_flag = 0;  /* LLC: Should this be here? */
  }
  return 0;
}

/* Used only by plq() */
static void PermitNewline (int nSpaces)
{
  if (printNow + nSpaces > printLength)
    ForceNewline ();
  else
    while (nSpaces--)
      printBuf[printNow++] = ' ';
  printBuf[printNow] = '\0';
  permitNow = printNow;
}

/* Used only by plq() */
/* 11/12/01 llc Change color from int to unsigned long */ 

static void PrintColor (char *line, unsigned long color, int suffix)
{
  if (color >= 0) {
    sprintf (line, "color= %ld,", color);
    PrintFunc (line);
  } else if (color == P_FG)
    PrintFunc ("color= \"fg\"");
  else if (color == P_BG)
    PrintFunc ("color= \"bg\"");
  else if (color == P_RED)
    PrintFunc ("color= \"red\"");
  else if (color == P_GREEN)
    PrintFunc ("color= \"green\"");
  else if (color == P_BLUE)
    PrintFunc ("color= \"blue\"");
  else if (color == P_CYAN)
    PrintFunc ("color= \"cyan\"");
  else if (color == P_MAGENTA)
    PrintFunc ("color= \"magenta\"");
  else if (color == P_YELLOW)
    PrintFunc ("color= \"yellow\"");
  else if (color == P_GREEN)
    PrintFunc ("color= \"green\"");
  else
    PrintFunc ("color= <unknown>");
  PrintSuffix (suffix);
}

/* Used only by plq() */
static void PrintFunc (const char *s)
{
  long len = strlen (s);
  while (printNow + len > printLength) {
    if (permitNow) {
      char savec[2];
      int i = permitNow, j = 1;
      savec[0] = printBuf[i];
      printBuf[i++] = '\0';
      if (printLines++ < maxPrintLines)
	RawPrinter (printBuf);
      printBuf[0] = savec[0];
      while (i <= printNow)
	printBuf[j++] = printBuf[i++];
      printNow -= permitNow;
      permitNow = 0;
    } else {
      long nhere = printLength - printNow - 1;
      char movec = '\0';
      if (nhere > 0) {
	strncpy (&printBuf[printNow], s, nhere);
	s += nhere;
	len -= nhere;
      } else if (nhere < 0) {	/* only -1 is possible */
	movec = printBuf[printLength - 1];
      }
      strcpy (&printBuf[printLength - 1], "\\");
      if (printLines++ < maxPrintLines)
	RawPrinter (printBuf);
      if (nhere >= 0) {
	printNow = 0;
	printBuf[0] = '\0';
      } else {
	printNow = 1;
	printBuf[0] = movec;
	printBuf[1] = '\0';
      }
    }
  }
  strcpy (&printBuf[printNow], s);
  printNow += len;
}

/* Used only by plq() */
static void PrintHideLegend (char *line, int type)
{
  int offset = 0;
  char marker[5];
  marker[0] = '\0';
  sprintf (line, "hide= %d,", gistD.hidden);
  PrintFunc (line);
  ForceNewline ();
  if ((type == 1 || type == 7) && gistD.legend && gistD.legend[0] == '\001') {
    marker[0] = '\\';
    marker[1] = marker[2] = '0';
    marker[3] = '1';
    marker[4] = '\0';
    offset = 1;
  }
  sprintf (line, "legend= \"%s%.104s\",", marker,
	   gistD.legend ? gistD.legend + offset : "");
  PrintFunc (line);
  ForceNewline ();
}

/* Used only by plq() */
static void PrintInit (int (*puts) (const char *))
{
  RawPrinter = puts;
  printNow = permitNow = 0;
  printLines = 0;
  printBuf[0] = '\0';
}

/* Used only by plq() */
static void PrintMarks (char *line, int suffix)
{
  sprintf (line, "marks= %d,  mcolor= 0x%02lx,  ",
           gistA.dl.marks, gistA.m.color);
  PrintFunc (line);
  if (gistA.m.type <= ' ' || gistA.m.type >= 0xff)
    sprintf (line, "marker= '\\%o',", gistA.m.type);
  else
    sprintf (line, "marker= '%c',", gistA.m.type);
  PrintFunc (line);
  ForceNewline ();
  sprintf (line,
	   "  msize= %.2f, mspace= %.5f, mphase= %.5f",
	   Safe_dbl (gistA.m.size),
	   Safe_dbl (gistA.dl.mSpace), Safe_dbl (gistA.dl.mPhase));
  PrintFunc (line);
  PrintSuffix (suffix);
}

/* Used only by plq() */
static void PrintRegion (char *line, int suffix)
{
  sprintf (line, "region= %d", gistD.region);
  PrintFunc (line);
  PrintSuffix (suffix);
}

/* Used only by plq() */
static void PrintSuffix (int suffix)
{
  if (suffix == 1)
    PrintFunc (",  ");
  else if (suffix == 3)
    PrintFunc (",");
  if (suffix & 2)
    ForceNewline ();
}

/* Used only by plq() */
static void PrintTypeWidth (char *line, int suffix)
{
  if (gistA.l.type == L_NONE)
    PrintFunc ("type= \"none\"");
  else if (gistA.l.type == L_SOLID)
    PrintFunc ("type= \"solid\"");
  else if (gistA.l.type == L_DASH)
    PrintFunc ("type= \"dash\"");
  else if (gistA.l.type == L_DOT)
    PrintFunc ("type= \"dot\"");
  else if (gistA.l.type == L_DASHDOT)
    PrintFunc ("type= \"dashdot\"");
  else if (gistA.l.type == L_DASHDOTDOT)
    PrintFunc ("type= \"dashdotdot\"");
  else
    PrintFunc ("type= <unknown>");
  sprintf (line, ",  width= %.2f", Safe_dbl (gistA.l.width));
  PrintFunc (line);
  PrintSuffix (suffix);
}

static GpColor *PushColors(double *z, long len, double zmin, double zmax,
 double scale, double offset)
{
  long i;
  double zz;
  GpColor *zc = (GpColor *) malloc (len * sizeof(GpColor));
  if (!zc) return (GpColor *) PyErr_NoMemory();

  for (i = 0 ; i < len ; i++) {
    zz = z[i];
    if (zz < zmin) zz = zmin;
    else if (zz > zmax) zz = zmax;
    zc[i] = (int) ((zz - offset) * scale);
  }
  return zc;
}

/* Used only by plq() */
static double Safe_dbl (double x)
{
  if (x > 1000.0)
    return 1000.0;
  else if (x < -1000.0)
    return -1000.0;
  else
    return x;
}

static int SetHCPDefault (void)
{
  int i, j;
  FILE *f;
  char hcpName[16];

  if (!hcpPSdefault)
    strcpy(hcpName, "Aa00.cgm");
  else
    strcpy(hcpName, "Aa00.ps");

  for (j = 'A'; j <= 'Z'; j++) {
    hcpName[0] = j;
    for (i = 'a'; i <= 'z'; i++) {
      hcpName[1] = i;
      if ((f = fopen (hcpName, "rb")))
	fclose (f);
      else
	goto got1;
    }
  }
  return (int) ERRSS ( "you appear to have Aa00 through Zz00 hcp files -- clean up");

got1:
  if (!hcpPSdefault)
    hcpDefault = GpCGMEngine ("Pygist default", 0, hcpDump,
			    SetHCPname (-1, hcpName));
  else
    hcpDefault = GpPSEngine ("Pygist default", 0, hcpDump,
			    SetHCPname (-1, hcpName));

  if (!hcpDefault)  {
    return (int) ERRSS ("failed to create default hcp file");
  }

  return 1;
}

static char *SetHCPname (int n, char *name)
{
  char *now;
  if (n < 0 || n > 7)
    n = 8;
  now = hcpNames[n];
#ifdef WINDOWS
#ifndef CYGWIN
  hcpNames[n] = name;
#else
  hcpNames[n] = expand_pathname (name);
#endif
#else
  hcpNames[n] = expand_pathname (name);
#endif
  if (now)
    free (now);
  return hcpNames[n];
}

/* Used only by mouse() */
static int YPrompt(const char *s)
{
  TO_STDOUT("%s", s);
  flush_stdout();
  return 0;
}

/*  -------------------------------------------------------------------- */

static char animate__doc__[] =
"animate()\n"
"or animate( 0/1 )\n"
"     Without any arguments, toggle animation mode; with argument 0,\n"
"     turn off animation mode; with argument 1 turn on animation mode.\n"
"     In animation mode, the X window associated with a graphics window\n"
"     is actually an offscreen pixmap which is bit-blitted onscreen\n"
"     when an fma() command is issued.  This is confusing unless you are\n"
"     actually trying to make a movie, but results in smoother animation\n"
"     if you are.  Generally, you should turn animation on, run your movie,\n"
"     then turn it off.\n"
"\n"
"   SEE ALSO: window, fma, plg\n";

static PyObject *animate (PyObject * self, PyObject * args)
{
  int i = 3;			/* default is to toggle */

  SETJMP0;
  if (!PyArg_ParseTuple (args, "|i", &i))  {
    return ERRSS ("Animate takes zero or one argument.");
  }
  PyFPE_START_PROTECT("animate", return 0)
  TRYS(CheckDefaultWindow())
  GhFMAMode (2, i);
  PyFPE_END_PROTECT(dummy)
  Py_INCREF (Py_None);
  return Py_None;
}

/* Build an array of pointers to the keyword values in kwt[],
 * or nil, if that keyword wasn't given in the command arguments.
 * Return -1 on failure, number of keywords set otherwise.
 */
static int build_kwt (PyObject *kd, char *kwlist[], PyObject * kwt[])
{
  int i, n, nkw_set = 0;
  char *kw;
  PyObject *kob, *keylist;
  char *kword, errstr[256];

  for (i = 0; (kw = kwlist[i]); i++) kwt[i] = 0;
  if (!PyMapping_Check (kd))
    return 0; /* No keywords were passed. */

  /* Check that all keywords passed are legal for this command. */
  keylist = PyMapping_Keys (kd);
  n = PyList_Size (keylist);
  for (i = 0; i < n; i++) {
    kob = PySequence_GetItem (keylist, i);
    kword = PyString_AsString (kob);
    if (!verify_kw (kword, kwlist)) {
      sprintf (errstr, "Unrecognized keyword: %s", kword);
      (int) ERRSS (errstr);
      return -1;
    }
  }
  Py_DECREF(keylist);

  /* Ok, all keywords were legal.  Now store pointers to their value.
   * Note that PyDict_GetItemString() puts 0 in kwt[i] if
     that key isn't found. */
  for (i = 0; (kw = kwlist[i]); i++)
    if((kwt[i] = PyDict_GetItemString (kd, kw)) != 0)
      ++nkw_set;
    /* I tried PyMapping_GetItemString() above, but kept getting
     * "KeyError: wait" messages back from Python.
     */

  return nkw_set;
}

/*  -------------------------------------------------------------------- */
/*  bytscl */

static char bytscl__doc__[] =
"bytscl(z)\n"
"or bytscl(z, top=max_byte, cmin=lower_cutoff, cmax=upper_cutoff)\n"
"     Returns a char array of the same shape as Z, with values linearly\n"
"     scaled to the range 0 to one less than the current palette size.\n"
"     If MAX_BYTE is specified, the scaled values will run from 0 to\n"
"     MAX_BYTE instead.\n"
"     If LOWER_CUTOFF and/or UPPER_CUTOFF are specified, Z values outside\n"
"     this range are mapped to the cutoff value; otherwise the linear\n"
"     scaling maps the extreme values of Z to 0 and MAX_BYTE.\n"
"\n"
"   SEE ALSO: plf, pli, histeq_scale\n";

#undef N_KEYWORDS
#define N_KEYWORDS 3
static char *bsKeys[N_KEYWORDS+1]= { "top", "cmin", "cmax", 0 };

static PyObject *bytscl (PyObject * self, PyObject * args, PyObject * kd)
{
  PyObject *zop, *kwt[NELT (bsKeys) - 1];
  PyArrayObject *zap, *zcap;
  double *z, zmin, zmax, scale, offset;
  GpColor *zc, *zc1;
  int i;
  long len;

  if (!PyArg_ParseTuple (args, "O", &zop))  {
    return ERRSS ("bytscl requires exactly one non-keyword argument");
  }

  TRY (addToArrayList((PyObject *)(zap = (PyArrayObject *)
      PyArray_ContiguousFromObject (zop, PyArray_DOUBLE, 1, 0))),
      (PyObject *)PyErr_NoMemory ());
  z = (double *) A_DATA (zap);
  len = A_SIZE (zap);

  BUILD_KWT(kd, bsKeys, kwt);
  TRY (GrabByteScale(&kwt[0], &bsKeys[0], &scale, &offset, &zmin, &zmax,
     z, (int *) 0, 0, len + 1, 2L, 1), (PyObject *) NULL);
  TRY (zc = PushColors(z, len, zmin, zmax, scale, offset), (PyObject *) NULL);
  NEW_ARR (zcap, zap->nd, zap->dimensions, Py_GpColor, PyObject *);
  Py_DECREF (zap);
  zc1 = (GpColor *) A_DATA (zcap);
  for (i = 0; i < len; i++) zc1[i] = zc[i];

  array_list_length = 0;
  free(zc);
  return (PyObject *) zcap;
}

/* Zero out the global pyMsh struct, and free any mesh arrays. */
static void clear_pyMsh(void)
{
  Py_XDECREF (pyMsh.y);
  Py_XDECREF (pyMsh.x);
  Py_XDECREF (pyMsh.reg);
  Py_XDECREF (pyMsh.triangle);
  pyMsh.y = pyMsh.x = pyMsh.reg = pyMsh.triangle = 0;
}

/*  -------------------------------------------------------------------- */
/*  current_window */

static char current_window__doc__[] =
"n = current_window()\n"
"     Return the number of the current graphics window, or -1 if none.\n";

static PyObject *current_window (PyObject * self, PyObject * args)
{
  return PyInt_FromLong (curPlotter);
}

/*  -------------------------------------------------------------------- */

/* The following routine has been added to check the integrity of what */
/* we think might be a NumPy array, including looking at addresses.    */

static char debug_array__doc__[] =
"None.";

static PyObject *debug_array (PyObject * self, PyObject * args)
{
 PyObject *oarray;
 PyArrayObject * aarray;
 int i;
 int max;
 long mmax;
 TRY (PyArg_ParseTuple (args, "O", &oarray),
    ERRSS ("debug_array: argument should be one PyObject*."));
 TO_STDOUT("Value of input pointer is %p.", oarray); flush_stdout();
 TO_STDOUT(" Reference count %d, size %d.\n", oarray->ob_refcnt,
           oarray->ob_type->ob_size);
 flush_stdout();
 if (! isARRAY (oarray)) {
    return ERRSS ("debug_array: argument should be a NumPy array.");
 }
 aarray = (PyArrayObject *) oarray;
 TO_STDOUT("Data pointer: %p; nd %d; dim1 %d; type %c.\n", aarray->data,
   aarray->nd, aarray->dimensions [0], aarray->descr->type); flush_stdout();
 if (aarray->descr->type == 'i') {
    TO_STDOUT ("%d ", ( (int *)(aarray->data)) [0]); flush_stdout();
    for (i = 1, max = ( (int *)(aarray->data)) [0]; i < aarray->dimensions [0]; i ++){
       if ( ( (int *)(aarray->data)) [i] > max) max = ( (int *)(aarray->data)) [i];
       TO_STDOUT ("%d ", ( (int *)(aarray->data)) [i]);
       if (i % 10 == 0) TO_STDOUT ("\n");
       flush_stdout();
       }
    TO_STDOUT ("maximum value is %d.\n", max); flush_stdout();
    }
 else if (aarray->descr->type == 'l') {
    TO_STDOUT ("%ld ", ( (long *)(aarray->data)) [0]); flush_stdout();
    for (i = 1, mmax = ( (long *)(aarray->data)) [0]; i < aarray->dimensions [0]; i ++){
       if ( ( (long *)(aarray->data)) [i] > mmax) mmax = ( (long *)(aarray->data)) [i];
       TO_STDOUT ("%ld ", ( (long *)(aarray->data)) [i]);
       if (i % 10 == 0) TO_STDOUT ("\n");
       flush_stdout();
       }
    TO_STDOUT ("maximum value is %ld.\n", mmax); flush_stdout();
    }
 Py_INCREF (Py_None);
 return Py_None;
}

/* Expand tildes and environment variables in pathnames. */
static char *expand_pathname (const char *name)
{
  PyObject *m, *d, *xpnduser, *xpndvars;
  DECL_ZERO (PyObject *, p1);
  DECL_ZERO (PyObject *, p2);
  DECL_ZERO (PyObject *, p3);
  DECL_ZERO (PyObject *, p4);
  char *path, *errstr = (char *) NULL, *name2, *name3;

  if (!name) return 0;

  /* Next four object refs are borrowed, and should not be DECREF'ed.
   * I know that module "os" has already been imported by gist.py, thus
   * can safely call PyImport_AddModule.  Otherwise, would need to call
   * PyImport_ImportModule to get posixmodule initialized.
   */
  TRY (m = (PyObject *) PyImport_AddModule ("posixpath"), (char *) NULL);
  TRY (d = PyModule_GetDict (m), (char *) NULL);
  TRY (xpnduser = PyDict_GetItemString (d, "expanduser"), (char *) NULL);
  TRY (xpndvars = PyDict_GetItemString (d, "expandvars"), (char *) NULL);

  /*
   * Here's a scorecard to keep track of the variables which follow:
   * "p1" is the PyObject (tuple of length 1) built from the string "name".
   * "p2" is the PyObject after expansion of any leading ~.
   * "name2" is the C string corresponding to "p2".
   * "p3" is the PyObject (tuple of length 1) built from the string "name2".
   * "p4" is the PyObject after expansion of any environment variables.
   * "name3" is the C string corresponding to "p4".
   * Finally, "path" is a copy of "name3" in memory that we own.
  */

  if (!(p1 = Py_BuildValue ("(s)", name))) goto errexit;
  if (!(p2 = PyEval_CallObject (xpnduser, p1))) goto errexit;
  name2 = PyString_AsString (p2);
  if (!(p3 = Py_BuildValue ("(s)", name2))) goto errexit;
  if (!(p4 = PyEval_CallObject (xpndvars, p3))) goto errexit;
  name3 = PyString_AsString (p4);

  path = (char *) malloc (1 + strlen (name3));
  if (path) (void)strcpy (path, name3);

  DECREF_AND_ZERO(p1);
  DECREF_AND_ZERO(p2);
  DECREF_AND_ZERO(p3);
  DECREF_AND_ZERO(p4);
  return path;

errexit:
  if(!PyErr_Occurred()) ERRSS (errstr ? errstr : "error in expand_path") ; 
  DECREF_AND_ZERO(p1);
  DECREF_AND_ZERO(p2);
  DECREF_AND_ZERO(p3);
  DECREF_AND_ZERO(p4);
  return 0;
}

/*  -------------------------------------------------------------------- */

static char fma__doc__[] =
"fma()\n"
"     Frame advance the current graphics window.  The current picture\n"
"     remains displayed in the associated X window until the next element\n"
"     is actually plotted.\n"
"\n"
"   SEE ALSO: window, hcp, animate, plg\n";

static PyObject *pyg_fma (PyObject * self, PyObject * args)
{
  SETJMP0;

  TRYS(CheckDefaultWindow())

  if (hcpOnFMA) {
    if (!CheckPalette ())
      return NULL;
  }
  curElement = -1;
  GhFMA ();
  Py_INCREF (Py_None);
  return Py_None;
}

/*  -------------------------------------------------------------------- */

/* Set pointers in the GaQuadMesh struct from values in the current
 * pyMsh struct.  Naturally, pyMsh must be fully initialized before
 * this is called.
 */
static void get_mesh(GaQuadMesh *m)
{
  m->iMax = A_DIM (pyMsh.y, 1);
  m->jMax = A_DIM (pyMsh.y, 0);
  m->y = (double *) A_DATA (pyMsh.y);
  m->x = (double *) A_DATA (pyMsh.x);
  m->reg = (int *) A_DATA (pyMsh.reg);
  if (isARRAY (pyMsh.triangle))
    m->triangle = (short *) A_DATA (pyMsh.triangle);
  else
    m->triangle = 0; /* Gist will provide a default in this case. */
}

/*  -------------------------------------------------------------------- */

static char get_slice2_precision__doc__[] =
"None.";

static PyObject* get_slice2_precision (PyObject * self, PyObject * args)
{
 if (PyTuple_Size (args) > 0)  {
    return ERRSS ("get_slice2_precision takes no arguments.") ;
 }
 return Py_BuildValue ( "d", _slice2_precision) ;
}

/*  -------------------------------------------------------------------- */
/*  gridxy */

static char gridxy__doc__[] =
"gridxy( flag )\n"
"or gridxy( xflag, yflag )\n"
"     Turns on or off grid lines according to FLAG.  In the first form, both\n"
"     the x and y axes are affected.  In the second form, XFLAG and YFLAG\n"
"     may differ to have different grid options for the two axes.  In either\n"
"     case, a FLAG value of 0 means no grid lines (the default), a value of\n"
"     1 means grid lines at all major ticks (the level of ticks which get\n"
"     grid lines can be set in the style sheet), and a FLAG value of 2 means\n"
"     that the coordinate origin only will get a grid line.  In styles with\n"
"     multiple coordinate systems, only the current coordinate system is\n"
"     affected.  The keywords can be used to affect the style of the grid\n"
"     lines.\n"
"\n"
"     You can also turn the ticks off entirely.  (You might want to do this\n"
"     to plot your own custom set of tick marks when the automatic tick\n"
"     generating machinery will never give the ticks you want.  For example\n"
"     a latitude axis in degrees might reasonably be labeled `0, 30, 60,\n"
"     90', but the automatic machinery considers 3 an `ugly' number - only\n"
"     1, 2, and 5 are `pretty' - and cannot make the required scale.  In\n"
"     this case, you can turn off the automatic ticks and labels, and use\n"
"     plsys, pldj, and plt to generate your own.)\n"
"     To fiddle with the tick flags in this general manner, set the\n"
"     0x200 bit of FLAG (or XFLAG or YFLAG), and `or-in' the 0x1ff bits\n"
"     however you wish.  The meaning of the various flags is described\n"
"     in the `work.gs' Gist style sheet.  Additionally, you can use the\n"
"     0x400 bit to turn on or off the frame drawn around the viewport.\n"
"     Here are some examples:\n"
"        gridxy(0x233)          work.gs default setting\n"
"        gridxy(0, 0x200)       like work.gs, but no y-axis ticks or labels\n"
"        gridxy(0, 0x231)       like work.gs, but no y-axis ticks on right\n"
"        gridxy(0x62b)          boxed.gs default setting\n"
"\n"
"   KEYWORDS: color, type, width\n"
"\n"
"   SEE ALSO: window, plsys, limits, ylimits, logxy\n";

#undef N_KEYWORDS
#define N_KEYWORDS 6
static char *gridKeys[N_KEYWORDS+1]= {
  "color", "type", "width", "base60", "degrees", "hhmm", 0 };

static PyObject *gridxy (PyObject * self, PyObject * args, PyObject * kd)
{
  int xgrid = 0, ygrid = 0, narg;
  PyObject * kwt[NELT(gridKeys) - 1];

  SETJMP0;

  if (!PyArg_ParseTuple (args, "|ii", &xgrid, &ygrid)) {
    return ERRSS ("gridxy takes zero, one or two non-keyword arguments.");
  }
  /* If one argument is given, use it for both x and y. */
  if((narg = PyTuple_Size(args)) == 1)
    ygrid = xgrid;

  TRYS(CheckDefaultWindow())

  BUILD_KWT(kd, gridKeys, kwt);
  SETKW(kwt[0], gistD.ticks.horiz.gridStyle.color, setkw_color,    gridKeys[0]);
  SETKW(kwt[0], gistD.ticks.vert.gridStyle.color,  setkw_color,    gridKeys[0]);
  SETKW(kwt[1], gistD.ticks.horiz.gridStyle.type,  setkw_linetype, gridKeys[1]);
  SETKW(kwt[1], gistD.ticks.vert.gridStyle.type,   setkw_linetype, gridKeys[1]);
  SETKW(kwt[2], gistD.ticks.horiz.gridStyle.width, setkw_double,   gridKeys[2]);
  SETKW(kwt[2], gistD.ticks.vert.gridStyle.width,  setkw_double,   gridKeys[2]);

  if(narg > 0){
    gistD.ticks.horiz.flags &= ~(GRID_F | GRID_O);
    if (xgrid == 1)
      gistD.ticks.horiz.flags |= GRID_F;
    else if (xgrid == 2)
      gistD.ticks.horiz.flags |= GRID_O;
    if (xgrid & 0x200) {
      gistD.ticks.horiz.flags = (xgrid & 0x1ff);
      gistD.ticks.frame = (xgrid & 0x400) != 0;
    }
    gistD.ticks.vert.flags &= ~(GRID_F | GRID_O);
    if (ygrid & 1)
      gistD.ticks.vert.flags |= GRID_F;
    else if (ygrid & 2)
      gistD.ticks.vert.flags |= GRID_O;
    if (ygrid & 0x200) {
      gistD.ticks.vert.flags = (ygrid & 0x1ff);
      gistD.ticks.frame = (ygrid & 0x400) != 0;
    }
  }
  GdSetPort ();
#ifdef WINDOWS
  pyg_on_idle();
#endif
  Py_INCREF (Py_None);
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  hcp */

static char hcp__doc__[] =
"hcp(), or hcpon(), or hcpoff()\n"
"     The hcp command sends the picture displayed in the current graphics\n"
"     window to the hardcopy file.  (The name of the default hardcopy file\n"
"     can be specified using hcp_file; each individual graphics window may\n"
"     have its own hardcopy file as specified by the window command.)\n"
"     The hcpon command causes every fma (frame advance) command to do\n"
"     and implicit hcp, so that every frame is sent to the hardcopy file.\n"
"     The hcpoff command reverts to the default \"demand only\" mode.\n"
"\n"
"   SEE ALSO: window, fma, plg\n";

static PyObject *hcp (PyObject * self, PyObject * args)
{
  SETJMP0;
  PyFPE_START_PROTECT("hcp", return 0)
  TRYS(CheckDefaultWindow())
  CheckPalette ();
  GhHCP ();
  PyFPE_END_PROTECT(dummy)
  Py_INCREF (Py_None);
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  hcp_file */

static char hcp_file__doc__[] =
"filename = hcp_finish( [n] )\n"
"     Close the current hardcopy file and return the filename.\n"
"     If N is specified, close the hcp file associated with window N\n"
"     and return its name; use hcp_finish(-1) to close the default\n"
"     hardcopy file.\n"
"\n"
"   SEE ALSO: window, fma, hcp, hcp_out, plg\n";

#undef N_KEYWORDS
#define N_KEYWORDS 2
static char *hcpKeys[N_KEYWORDS+1]= { "dump", "ps", 0 };
 
static PyObject *hcp_file (PyObject * self, PyObject * args, PyObject *kd)
{
  Engine *engine = hcpDefault;
  char *hcp = 0;
  int gotDump = 0;
  PyObject * kwt[NELT(hcpKeys) - 1];

  if (!PyArg_ParseTuple (args, "|s", &hcp)) {
    return ERRSS ("Bad arguments for hcp_file.");
  }

  BUILD_KWT(kd, hcpKeys, kwt);
  gotDump = (kwt[0] != 0);
  SETKW(kwt[0], hcpDump,      setkw_boolean, hcpKeys[0]);
  SETKW(kwt[1], hcpPSdefault, setkw_boolean, hcpKeys[1]);

  if (hcp) {
    long len = strlen (hcp);

    if (engine) {
      hcpDefault = 0;
      GpKillEngine (engine);
      SetHCPname (-1, (char *) 0);
      engine = 0;
    }
    if (len > 3 && strcmp (&hcp[len - 3], ".ps") == 0) {
      engine = GpPSEngine ("Pygist default", 0, hcpDump, SetHCPname (-1, hcp));
      if (!engine) {
	return ERRSS ("failed to create PostScript file");
      }
    } else if (len > 0) {
      engine = GpCGMEngine ("Pygist default", 0, hcpDump, SetHCPname (-1, hcp));
      if (!engine) {
	return ERRSS ("failed to create binary CGM file");
      }
    }
    hcpDefault = engine;
  } else if (gotDump) {
    GhDumpColors (-1, 1, hcpDump);
  }
  Py_INCREF (Py_None);
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  hcp_finish */

static char hcp_finish__doc__[] =
"filename = hcp_finish( [n] )\n"
"     Close the current hardcopy file and return the filename.\n"
"     If N is specified, close the hcp file associated with window N\n"
"     and return its name; use hcp_finish(-1) to close the default\n"
"     hardcopy file.\n"
"\n"
"   SEE ALSO: window, fma, hcp, hcp_out, plg\n";

static PyObject *hcp_finish (PyObject * self, PyObject * args)
{
  /* just return name of current hcp file */
  int n = curPlotter;
  Engine *engine;
  PyObject *p;

  if (!PyArg_ParseTuple (args, "|i", &n)) {
    return ERRSS ("Bad argument for hcp_finish.");
  }
  if (n < -1 || n > 7) {
    return ERRSS ("hcp_finish argument must be -1 through 7 inclusive");
  }
  p = PyString_FromString (GetHCPname (n));

  if (n >= 0)
    engine = ghDevices[n].hcp ? ghDevices[n].hcp : hcpDefault;
  else
    engine = hcpDefault;
  if (engine) {
    if (engine == hcpDefault) {
      hcpDefault = 0;
    } else {
      ghDevices[n].hcp = 0;
    }
    GpKillEngine (engine);
    SetHCPname (n, (char *) 0);
  }
  return p;
}

/*  -------------------------------------------------------------------- */
/*  hcpoff */

static char hcpoff__doc__[] =
"hcp(), or hcpon(), or hcpoff()\n"
"     The hcp command sends the picture displayed in the current graphics\n"
"     window to the hardcopy file.  (The name of the default hardcopy file\n"
"     can be specified using hcp_file; each individual graphics window may\n"
"     have its own hardcopy file as specified by the window command.)\n"
"     The hcpon command causes every fma (frame advance) command to do\n"
"     and implicit hcp, so that every frame is sent to the hardcopy file.\n"
"     The hcpoff command reverts to the default `demand only' mode.\n"
"\n"
"   SEE ALSO: window, fma, plg\n";

static PyObject *hcpoff (PyObject * self, PyObject * args)
{
  SETJMP0;
  PyFPE_START_PROTECT("hcpoff", return 0)
  TRYS(CheckDefaultWindow())
  hcpOnFMA = 0;
  GhFMAMode (0, 2);
  PyFPE_END_PROTECT(dummy)
  Py_INCREF (Py_None);
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  hcpon */

static char hcpon__doc__[] =
"hcp(), or hcpon(), or hcpoff()\n"
"     The hcp command sends the picture displayed in the current graphics\n"
"     window to the hardcopy file.  (The name of the default hardcopy file\n"
"     can be specified using hcp_file; each individual graphics window may\n"
"     have its own hardcopy file as specified by the window command.)\n"
"     The hcpon command causes every fma (frame advance) command to do\n"
"     and implicit hcp, so that every frame is sent to the hardcopy file.\n"
"     The hcpoff command reverts to the default `demand only' mode.\n"
"\n"
"   SEE ALSO: window, fma, plg\n";

static PyObject *hcpon (PyObject * self, PyObject * args)
{
  SETJMP0;
  PyFPE_START_PROTECT("hcpon", return 0)
  TRYS(CheckDefaultWindow())
  hcpOnFMA = 1;
  GhFMAMode (1, 2);
  PyFPE_END_PROTECT(dummy)
  Py_INCREF (Py_None);
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  limits */

static char limits__doc__[] =
"old_limits = limits()\n"
"or old_limits = limits( xmin [, xmax, ymin, ymax,]\n"
"     [ square=0/1, nice=0/1, restrict=0/1 ] )\n"
"or limits( old_limits )\n"
"\n"
"     In the first form, restore all four plot limits to extreme values,\n"
"     and save the previous limits in the tuple old_limits.\n"
"\n"
"     In the second form, set the plot limits in the current coordinate\n"
"     system to XMIN, XMAX, YMIN, YMAX, which may each be a number to fix\n"
"     the corresponding limit to a specified value, or the string `e'\n"
"     to make the corresponding limit take on the extreme value of the\n"
"     currently displayed data. Arguments may be omitted from the right\n"
"     end only. (But see ``ylimits'' to set limits on the y-axis.)\n"
"\n"
"     If present, the square keyword determines whether limits marked as\n"
"     extreme values will be adjusted to force the x and y scales to be\n"
"     equal (square=1) or not (square=0, the default). If present, the\n"
"     nice keyword determines whether limits will be adjusted to nice\n"
"     values (nice=1) or not (nice=0, the default). There is a subtlety\n"
"     in the meaning of `extreme value' when one or both of the limits\n"
"     on the OPPOSITE axis have fixed values -- does the `extreme value'\n"
"     of the data include points which will not be plotted because their\n"
"     other coordinate lies outside the fixed limit on the opposite axis\n"
"     (restrict=0, the default), or not (restrict=1)?\n"
"\n"
"     Limits() always returns a tuple of 4 doubles and an integer;\n"
"     OLD_LIMITS[0:3] are the previous xmin, xmax, ymin, and ymax, and\n"
"     OLD_LIMITS[4] is a set of flags indicating extreme values and the\n"
"     square, nice, restrict, and log flags. This tuple can be saved and\n"
"     passed back to limits() in a future call to restore the limits to a\n"
"     previous state.\n"
"\n"
"     In an X window, the limits may also be adjusted interactively with\n"
"     the mouse. Drag left to zoom in and pan (click left to zoom in on a\n"
"     point without moving it), drag middle to pan, and click (and drag)\n"
"     right to zoom out (and pan). If you click just above or below the\n"
"     plot, these operations will be restricted to the x-axis; if you\n"
"     click just to the left or right, the operations are restricted to\n"
"     the y-axis. A shift-left click, drag, and release will expand the\n"
"     box you dragged over to fill the plot (other popular software zooms\n"
"     with this paradigm). If the rubber band box is not visible with\n"
"     shift-left zooming, try shift-middle or shift-right for alternate\n"
"     XOR masks. Such mouse-set limits are equivalent to a limits command\n"
"     specifying all four limits EXCEPT that the unzoom command can\n"
"     revert to the limits before a series of mouse zooms and pans.\n"
"\n"
"     The limits you set using the limits or ylimits functions carry over\n"
"     to the next plot -- that is, an fmaoperation does NOT reset the\n"
"     limits to extreme values.\n"
"\n"
"   SEE ALSO: plsys, ylimits, logxy, zoom_factor, unzoom, plg\n";

#undef N_KEYWORDS
#define N_KEYWORDS 3
static char *limKeys[N_KEYWORDS+1]= {
  "square", "nice", "restrict", 0 };

static PyObject *limits (PyObject * self, PyObject * args, PyObject * kd)
{
  /* NB-- If the plot has not been displayed yet, this will not retrieve
     the latest extreme values calculated by GdScan.  Nevertheless,
     it DOES retrieve the precise state of the limits at the time
     of this call, and retoring them will work correctly.  */

  double old_limits[4], new_limits[4];
  PyObject *xmin_ob = 0, *xmax_ob = 0;
  PyObject *ymin_ob = 0, *ymax_ob = 0;
  int nkw, old_flags, new_flags = 0, changed = 0, j;
  int square, nice, g_restrict;
  PyObject * kwt[NELT(limKeys) - 1];

  SETJMP0;
  if (!PyArg_ParseTuple (args, "|OOOO",
			 &xmin_ob, &xmax_ob, &ymin_ob, &ymax_ob)) {
    return ERRSS ("limits takes at most 4 non-keyword arguments.");
  }
  /* retrieve current limits and flags */
  GdGetLimits ();
  old_limits[0] = gistD.limits.xmin;
  old_limits[1] = gistD.limits.xmax;
  old_limits[2] = gistD.limits.ymin;
  old_limits[3] = gistD.limits.ymax;
  old_flags = gistD.flags;

  if (xmin_ob && PyTuple_Check (xmin_ob)) {  /* Restore saved limits. */
    if (PyMapping_Check (kd)) {
      return ERRSS ("Keywords not allowed when restoring saved limits.");
    }
    if (!unpack_limit_tuple (xmin_ob, new_limits, &new_flags)) {
      return ( PyObject * ) NULL; 
    }
    gistD.limits.xmin = new_limits[0];
    gistD.limits.xmax = new_limits[1];
    gistD.limits.ymin = new_limits[2];
    gistD.limits.ymax = new_limits[3];
    gistD.flags = new_flags;
    GdSetLimits ();
    return Py_BuildValue ("ddddi",
     old_limits[0], old_limits[1], old_limits[2], old_limits[3], old_flags);
  }

  if ((nkw = build_kwt (kd, limKeys, kwt)) > 0) { /* At least one keyword */
    SETKW(kwt[0], square,   setkw_boolean, limKeys[0]);
    SETKW(kwt[1], nice,     setkw_boolean, limKeys[1]);
    SETKW(kwt[2], g_restrict, setkw_boolean, limKeys[2]);

    if (kwt[0]) {
      if(square) gistD.flags |= D_SQUARE;
      else gistD.flags &= ~D_SQUARE;
    }
    if (kwt[1])  {
      if(nice) gistD.flags |= D_NICE;
      else gistD.flags &= ~D_NICE;
    }
    if (kwt[2])  {
      if(g_restrict) gistD.flags |= D_RESTRICT;
      else gistD.flags &= ~D_RESTRICT;
    }

    ++changed;

  } else if (-1 == nkw) { /* Error unpacking keyword dictionary */
    return 0;
  } else if (!xmin_ob) { /* No arguments nor keywords were passed. */
    gistD.flags = (D_XMIN | D_XMAX | D_YMIN | D_YMAX);
    ++changed;
  }

  if (xmin_ob) {
    j = set_limit (xmin_ob, &gistD.limits.xmin, &gistD.flags, D_XMIN);
    if(0 == j)  { /* Error */
      return ERRSS ("bad xmin argument: Use float or 'e'");
    }
    else if(1 == j) /* Xmin changed or set to extreme. */
      ++changed;
  }
  if (xmax_ob) {
    j = set_limit (xmax_ob, &gistD.limits.xmax, &gistD.flags, D_XMAX);
    if(0 == j)  { /* Error */
      return ERRSS ("bad xmax argument: Use float or 'e'");
    }
    else if(1 == j) /* Xmax changed or set to extreme. */
      ++changed;
  }
  if (ymin_ob) {
    j = set_limit (ymin_ob, &gistD.limits.ymin, &gistD.flags, D_YMIN);
    if(0 == j) { /* Error */
      return ERRSS ("bad ymin argument: Use float or 'e'");
    }
    else if(1 == j) /* Ymin changed or set to extreme. */
      ++changed;
  }
  if (ymax_ob) {
    j = set_limit (ymax_ob, &gistD.limits.ymax, &gistD.flags, D_YMAX);
    if(0 == j)  { /* Error */
      return ERRSS ("bad ymax argument: Use float or 'e'");
    }
    else if(1 == j) /* Ymax changed or set to extreme. */
      ++changed;
  }

  if (changed) GdSetLimits ();

#ifdef WINDOWS
  pyg_on_idle();
#endif

  return Py_BuildValue ("ddddi",
     old_limits[0], old_limits[1], old_limits[2], old_limits[3], old_flags);
}

/*  -------------------------------------------------------------------- */
/*  logxy */

static char logxy__doc__[] =
"logxy( xflag, yflag )\n"
"     Sets the linear/log axis scaling flags for the current coordinate\n"
"     system. XFLAG and YFLAG may be 0 to select linear scaling, or 1 to\n"
"     select log scaling. YFLAG may be omitted (but not XFLAG).\n"
"\n"
"   SEE ALSO: plsys, limits, ylimits, plg, gridxy\n";

static PyObject *logxy (PyObject * self, PyObject * args)
{
  int xflag = -1, yflag = -1, changed = 0;

  SETJMP0;
  if (!PyArg_ParseTuple (args, "|ii", &xflag, &yflag)) {
    return ERRSS ("Bad arguments for logxy.");
  }
  if (-1 != xflag)
    changed |= 1;
  if (-1 != yflag)
    changed |= 2;

  if (changed) {
    GdGetLimits ();
    if (changed & 1) {
      if (1 == xflag)
	gistD.flags |= D_LOGX;
      else
	gistD.flags &= ~D_LOGX;
    }
    if (changed & 2) {
      if (1 == yflag)
	gistD.flags |= D_LOGY;
      else
	gistD.flags &= ~D_LOGY;
    }
    GdSetLimits ();
  }
  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  mesh_loc */

static char mesh_loc__doc__[] =
"mesh_loc(y0, x0)\n"
"or mesh_loc(y0, x0, y, x)\n"
"or mesh_loc(y0, x0, y, x, ireg)\n"
"     Returns the zone index (=i+imax*(j-1)) of the zone of the mesh\n"
"     (X,Y) (with optional region number array IREG) containing the\n"
"     point (X0,Y0).  If (X0,Y0) lies outside the mesh, returns 0.\n"
"     Thus, eg- ireg(mesh_loc(x0, y0, y, x, ireg)) is the region number of\n"
"     the region containing (x0,y0).  If no mesh specified, uses default.\n"
"     X0 and Y0 may be arrays as long as they are conformable.\n"
"\n"
"   SEE ALSO: plmesh, moush, mouse\n";

static PyObject *mesh_loc (PyObject * self, PyObject * args)
{
  long *zone;
  int i, n;
  GaQuadMesh mesh;
  double *x0 = 0, *y0 = 0;
  PyObject *y0op, *x0op;
  PyArrayObject *y0ap = 0, *x0ap = 0, *rap = 0;
  char *errstr = "mesh_loc requires arguments (y0, x0 [ , y, x [ ,ireg ] ])";
  struct { double x, y; } dbuf;

  if (PyTuple_Size (args) < 2)  {
    return ERRSS ("mesh_loc requires at least two arguments");
  }
  TRY (setvu_mesh (args, &y0op, &x0op, errstr), (PyObject *) NULL);
  if (!pyMsh.y)  {
    return ERRSS ("No current mesh - set (y, x) first");
  }
  get_mesh (&mesh);

  if (isARRAY (y0op)) {
    TRY (addToArrayList((PyObject *)(y0ap = (PyArrayObject *)
        PyArray_ContiguousFromObject (y0op, PyArray_DOUBLE, 1, 0))),
        (PyObject *)PyErr_NoMemory ());
    n = A_SIZE ( y0ap );
    TRY (addToArrayList((PyObject *)(x0ap = (PyArrayObject *)
        PyArray_ContiguousFromObject (x0op, PyArray_DOUBLE, 1, 0))),
        (PyObject *)PyErr_NoMemory ());
    if (n != A_SIZE ( x0ap )) {
      clearArrayList();
      return ERRSS ("(y0, x0) must be same size");
    }
    y0 = (double *) A_DATA (y0ap);
    x0 = (double *) A_DATA (x0ap);
  } else if (PyFloat_Check (y0op)) {
    y0 = &dbuf.y;
    x0 = &dbuf.x;
    y0[0] = PyFloat_AsDouble (y0op);
    x0[0] = PyFloat_AsDouble (x0op);
    n = 1;
  } else {
    return ERRSS ("(y0, x0) must be floating point scalars or arrays.");
  }

  NEW_ARR (rap, 1, &n, PyArray_LONG, PyObject *);
  zone = (long *) A_DATA (rap);

  for (i = 0; i < n; i++)
    zone[i] = 1 + FindMeshZone (x0[i], y0[i], mesh.x, mesh.y, mesh.reg,
      mesh.iMax, mesh.jMax);

  if (isARRAY (y0op)) {
    Py_DECREF (y0ap);
    Py_DECREF (x0ap);
  }
  array_list_length = 0;
  return PyArray_Return (rap);
}

/*  -------------------------------------------------------------------- */
/*  mfit */

static char mfit__doc__[] =
"Computes multiquadric fit to data; used for contour plotting\n"
"of random data. Calling sequence from Python:\n"
"   zcplot = mfit (alpha, x, xcplot, y, ycplot, rsqmqd)\n"
"where alpha are the interpolation coefficients, x and y\n"
"are the original randomly distributed coordinates\n"
"(alpha, x, and y are all the same length, say nzcplot).\n"
"xcplot (nxcplot long) and ycplot (nycplot long) specify\n"
"an overlying rectangular mesh. rsqmod is a scalar peculiar\n"
"to the problem.\n";

static PyObject *mfit (PyObject * self, PyObject * args)
{
 int nxcplot,
     nycplot,
     nzcplot;
 double *x,
        *y,
        *alpha,
        *xcplot,
        *ycplot,
        *zcplot,
        rsqmqd;
 PyObject *oalpha,
          *ox,
          *oy,
          *oxcplot,
          *oycplot;
 PyArrayObject *aalpha,
               *ax,
               *ay,
               *axcplot,
               *aycplot,
               *azcplot;
 int i,
     j,
     k,
     l,
     dims [2];
 double sum;
 
 TRY (PyArg_ParseTuple (args, "OOOOOd", &oalpha, &ox, &oxcplot, &oy,
   &oycplot, &rsqmqd), ERRSS ("mfit: unable to parse arguments."));
 GET_ARR (aalpha, oalpha, PyArray_DOUBLE, 1, PyObject *);
 GET_ARR (ax, ox, PyArray_DOUBLE, 1, PyObject *);
 GET_ARR (axcplot, oxcplot, PyArray_DOUBLE, 1, PyObject *);
 GET_ARR (ay, oy, PyArray_DOUBLE, 1, PyObject *);
 GET_ARR (aycplot, oycplot, PyArray_DOUBLE, 1, PyObject *);
 /* There is no other error checking, really. It is intended that
  * this routine be called only from Python code, not by the user. */
 nzcplot = A_DIM (aalpha, 0);
 nxcplot = A_DIM (axcplot, 0);
 dims [0] = nxcplot;
 nycplot = A_DIM (aycplot, 0);
 dims [1] = nycplot;
 x = (double *) A_DATA (ax);
 y = (double *) A_DATA (ay);
 xcplot = (double *) A_DATA (axcplot);
 ycplot = (double *) A_DATA (aycplot);
 alpha = (double *) A_DATA (aalpha);
 TRY (azcplot = (PyArrayObject *) PyArray_FromDims (2, dims, PyArray_DOUBLE),
    ERRSS ("mfit: unable to create return array."));
 zcplot = (double *) A_DATA (azcplot);
 l = 0;
 for (i = 0; i < nxcplot; i++)
    for (j = 0; j < nycplot; j ++) {
       sum = 0.0;
       for (k = 0; k < nzcplot; k ++)
          sum += alpha [k] *
             sqrt ( (x [k] - xcplot [i]) * (x [k] - xcplot [i]) +
                    (y [k] - ycplot [j]) * (y [k] - ycplot [j]) + rsqmqd);
       zcplot [l ++] = sum;
    }
 clearArrayList ();
 return PyArray_Return (azcplot);
}

/*  -------------------------------------------------------------------- */
/*  mouse */

static char mouse__doc__[] =
"result = mouse(system, style, prompt)\n"
"     Displays a PROMPT, then waits for a mouse button to be pressed,\n"
"     then released.  Returns tuple of length eleven:\n"
"       result= [x_pressed, y_pressed, x_released, y_released,\n"
"                xndc_pressed, yndc_pressed, xndc_released, yndc_released,\n"
"                system, button, modifiers]\n"
"\n"
"     If SYSTEM>=0, the first four coordinate values will be relative to\n"
"     that coordinate system.\n"
"     For SYSTEM<0, the first four coordinate values will be relative to\n"
"     the coordinate system under the mouse when the button was pressed.\n"
"     The second four coordinates are always normalized device coordinates,\n"
"     which start at (0,0) in the lower left corner of the 8.5x11 sheet of\n"
"     paper the picture will be printed on, with 0.0013 NDC unit being\n"
"     1/72.27 inch (1.0 point).  Look in the style sheet for the location\n"
"     of the viewport in NDC coordinates (see the style keyword).\n"
"\n"
"     If STYLE is 0, there will be no visual cues that the mouse\n"
"     command has been called; this is intended for a simple click.\n"
"     If STYLE is 1, a rubber band box will be drawn; if STYLE is 2,\n"
"     a rubber band line will be drawn.  These disappear when the\n"
"     button is released.\n"
"\n"
"     Clicking a second button before releasing the first cancels the\n"
"     mouse function, which will then return nil.\n"
"     Ordinary text input also cancels the mouse function, which again\n"
"     returns nil.\n"
"\n"
"     The left button reverses forground for background (by XOR) in\n"
"     order to draw the rubber band (if any).  The middle and right\n"
"     buttons use other masks, in case the rubber band is not visible\n"
"     with the left button.\n"
"\n"
"     result[8] is the coordinate system in which the first four\n"
"     coordinates are to be interpreted.\n"
"     result[9] is the button which was pressed, 1 for left, 2\n"
"     for middle, and 3 for right (4 and 5 are also possible).\n"
"     result[10] is a mask representing the modifier keys which\n"
"     were pressed during the operation: 1 for shift, 2 for shift lock,\n"
"     4 for control, 8 for mod1 (alt or meta), 16 for mod2, 32 for mod3,\n"
"     64 for mod4, and 128 for mod5.\n"
"\n"
"   SEE ALSO: moush\n";

static PyObject *mouse (PyObject * self, PyObject * args)
{
/*  Don't know why this define is needed, but GxPointClick as the define 
 *  define was not working.
 */
#define MOUSE_CLICK
#ifdef MOUSE_CLICK 
  char *prompt = 0;
  int system = -1, style = 0;
  int n = curPlotter;

  SETJMP0;
  if (n < 0 || !ghDevices[n].display)  {
    return ERRSS ("no current graphics window for mouse function");
  }

  if (!PyArg_ParseTuple (args, "|iis", &system, &style, &prompt))  {
    return ERRSS ("call with (system, style, prompt)");
  }

  /* GhWaitDisplay (); */   /* otherwise can lock up */
  GhBeforeWait ();          /* be sure display is current */
  if (!prompt)
    YPrompt (defaultPrompts[style != 0]);
  else if (prompt[0])
    YPrompt (prompt);
  mouseError = 0;
  mouseError |= GxPointClick (ghDevices[n].display, style, system,
			       &MouseCallBack);
  if (!prompt || prompt[0])
    YPrompt ("\n");

  if (!mouseError) {
    pyg_wait_flag = 1;
    p_wait_while(&pyg_wait_flag);
  }

  if (mouseError) {
    Py_INCREF (Py_None);
    return Py_None;
  } else {
    return Py_BuildValue ("ddddddddiii",
      mouseX0,    mouseY0,    mouseX1,    mouseY1,
      mouseX0ndc, mouseY0ndc, mouseX1ndc, mouseY1ndc,
      mouseSystem, mouseButton, mouseModifier);
  }
#else
  return ERRSS ("no mouse function in this version of Python/Gist");
#endif
}

/*  -------------------------------------------------------------------- */
/*  palette */

static char palette__doc__[] =
"palette( filename )\n"
"or palette( source_window_number )\n"
"or palette( red, green, blue, ntsc=1/0 )\n"
"or palette( red, green, blue, gray )\n"
"or palette( red, green, blue, query=1 )\n"
"or palette( red, green, blue, gray, query=1 )\n"
"     Set (or retrieve with query=1) the palette for the current\n"
"     graphics window.  The FILENAME is the name of a Gist palette file;\n"
"     the standard palettes are `earth.gp', `stern.gp', `rainbow.gp',\n"
"     `heat.gp', `gray.gp', and `yarg.gp'.  Use the maxcolors keyword\n"
"     in the pldefault command to put an upper limit on the number of\n"
"     colors which will be read from the palette in FILENAME.\n"
"\n"
"     In the second form, the palette for the current window is copied\n"
"     from the SOURCE_WINDOW_NUMBER.  If the X colormap for the window is\n"
"     private, there will still be two separate X colormaps for the two\n"
"     windows, but they will have the same color values.\n"
"\n"
"     In the third form, RED, GREEN, and BLUE are 1-D arrays of the same\n"
"     length specifying the palette you wish to install; the values\n"
"     should vary between 0 and 255, and your palette should have no\n"
"     more than 240 colors.  If ntsc=0, monochrome devices (such as most\n"
"     laser printers) will use the average brightness to translate your\n"
"     colors into gray; otherwise, the NTSC (television) averaging will\n"
"     be used (.30*RED+.59*GREEN+.11*BLUE).  Alternatively, you can specify\n"
"     GRAY explicitly.\n"
"\n"
"     Ordinarily, the palette is not dumped to a hardcopy file\n"
"     (color hardcopy is still rare and expensive), but you can\n"
"     force the palette to dump using the window() or hcp_file() commands.\n"
"\n"
"     See the dump= keyword for the hcp_file() and window() commands if you\n"
"     are having trouble getting color in your hardcopy files.\n"
"\n"
"   SEE ALSO: window, fma, hcp, pldefault, plg\n";

#undef N_KEYWORDS
#define N_KEYWORDS 2
static char *paletteKeys[N_KEYWORDS+1]= { "ntsc", "query", 0 };

static PyObject *palette (PyObject * self, PyObject * args, PyObject * kd)
{
  GpColorCell *palette = 0;
  PyObject * kwt[NELT(paletteKeys) - 1];
  unsigned char *red = 0, *green = 0, *blue = 0, *gray = 0;
  PyObject *rop, *gop, *bop, *grayop;
  PyArrayObject *rap = 0, *gap = 0, *bap = 0, *grayap = 0;
  int nred = 0, ngreen = 0, nblue = 0, ngray = 0;
  int i, nColors=0, nDevice, query = 0, ntsc = 0, len_match;
  Engine *engine;
  int sourceDevice = -2;
  char *filename = 0;
  char *errstr = "palette takes a filename, or source window number, or\n"
    "red, blue, green [, gray] color arrays to specify or query the palette";

  SETJMP0;			/* See Xerror_longjmp() */

  BUILD_KWT(kd, paletteKeys, kwt);
  SETKW(kwt[0], ntsc,  setkw_boolean, paletteKeys[0]);
  SETKW(kwt[1], query, setkw_boolean, paletteKeys[1]);

  switch (PyTuple_Size (args)) {
  case 4: /* (red, green, blue, gray) given */
    TRY (grayop = PyTuple_GetItem (args, 3), (PyObject *) NULL);
    GET_ARR (grayap, grayop, Py_GpColor, 1, PyObject *);
    ngray = A_SIZE (grayap);
    gray = (GpColor *) A_DATA (grayap);
    /* Fall through. */
  case 3: /* (red, green, blue) given */
    TRY (PyArg_ParseTuple (args, "OOO", &rop, &gop, &bop), (PyObject *) NULL);
    GET_ARR (rap, rop, Py_GpColor, 1, PyObject *);
    nred = A_SIZE (rap);
    red = (GpColor *) A_DATA (rap);

    GET_ARR (gap, gop, Py_GpColor, 1, PyObject *);
    ngreen = A_SIZE (gap);
    green = (GpColor *) A_DATA (gap);

    GET_ARR (bap, bop, Py_GpColor, 1, PyObject *);
    nblue = A_SIZE (bap);
    blue = (GpColor *) A_DATA (bap);

    /* Check for matched array sizes and set nColors. */
    len_match = (nred == ngreen && nred == nblue);
    if(ngray) { len_match = (len_match && nred == ngray); }
    if(len_match){
      nColors = nred;
    }else{
      clearArrayList ();
      return ERRSS ("red, green, blue, and gray arguments must be same length");
    }

    break;
  case 1: /* (filename) or (source_window_number) given */
    if (query) {
       return ERRSS ("query requires (r,g,b) arrays as arguments");
    }
  
    if (PyArg_ParseTuple (args, "s", &filename)) {
      break; /* call was palette (filename) */
    } else if (PyArg_ParseTuple (args, "i", &sourceDevice)) {
      if (sourceDevice < 0 || sourceDevice >= 7 ||
	  (!(engine = ghDevices[sourceDevice].display) &&
	   !(engine = ghDevices[sourceDevice].hcp)))
        return ERRSS ("specified palette source window does not exist");
      break;
    } else {
      return ERRSS (errstr);
    }
  default:
    return ERRSS (errstr);
  }

  TRYS(CheckDefaultWindow())
  nDevice = curPlotter;
  engine = ghDevices[nDevice].display;
  if (!engine) engine = ghDevices[nDevice].hcp;

  if (query) { /* Retrieve the current palette. */
    nColors = GpGetPalette(engine, &palette);
    if (nColors > 256) {
      clearArrayList ();
      return ERRSS ("Gist palettes can never have more than 256 colors");
    }
    if (nColors > nred  || nColors > ngreen ||
        nColors > nblue || (ngray && nColors > ngray)) {
      clearArrayList ();
      return ERRSS ("arrays passed are too small to hold all the colors");
    }
    if (nColors > 0) {
      for (i = 0 ; i < nColors ; i++) {
	red[i]   = P_R(palette[i]);
	green[i] = P_G(palette[i]);
	blue[i]  = P_B(palette[i]);
      }
      if (ngray)
	for (i = 0 ; i < nColors ; i++)
	  gray[i]  = (P_R(palette[i])+P_G(palette[i])+P_B(palette[i]))/3;

    }
  } else { /* Set a new palette. */

    if (sourceDevice != nDevice) {
      /* be sure to preserve dump = 1 setting even if hcp palette
         is deleted */
      int dump;
      if (hcpDefault) dump = GhGetColorMode(hcpDefault);
      else dump = 0;
      GhDeletePalette(nDevice);
      paletteSize = 0;
      if (hcpDefault) GhDumpColors(-1, 1, dump);
    }
    if (red) {
      /* palette is unprotected against asynchronous interrupts...
	 fix this someday */
      palette = malloc (sizeof(GpColorCell) * nColors);
      for (i = 0 ; i < nColors ; i++) {
        palette[i] = P_RGB(red[i], green[i], blue[i]);
	/* if (gray) palette[i].gray = gray[i]; */
      }
      if (!gray) {
	if (ntsc)
	  GpPutNTSC(nColors, palette);
	else
	  GpPutGray(nColors, palette);
      }
      GhSetPalette (nDevice, palette, nColors);
      paletteSize = nColors;

    } else if (filename) {
      nColors = GhReadPalette(nDevice, filename, &palette, maxColors);
      if (nColors <= 0) {
        char s[1024];
	sprintf(s, "%s: Gist palette not found", filename);
        clearArrayList ();
	return ERRSS (s);
      }
    }
    paletteSize = nColors;
  }
  clearArrayList ();
  Py_INCREF (Py_None);
  return Py_None;
}

static void pyg_got_alarm(void *context)
{
  pyg_wait_flag = 0;
}

/*  -------------------------------------------------------------------- */

static char pause__doc__[] =
"pause( milliseconds )\n"
"     Pause for the specified number of milliseconds of wall clock\n"
"     time, or until input arrives from the keyboard.\n"
"     This is intended for use in creating animated sequences.\n";

static PyObject *pyg_pause (PyObject * self, PyObject * args)
{
  long timeout;

  if (!PyArg_ParseTuple (args, "i", &timeout)) {
    return ERRSS ("Pause requires one integer argument.");
  }
  if (timeout < 0)
    timeout = 0;

  p_set_alarm(0.001*timeout, pyg_got_alarm, 0);
  pyg_wait_flag = 1;
  p_wait_while(&pyg_wait_flag);

  Py_INCREF (Py_None);
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  plc */

static double *tmpLevels = 0; /* UPDATE */

static char plc__doc__[] =
"plc( z, y, x, levs=z_values )\n"
"or plc( z, y, x, ireg, levs=z_values )\n"
"or plc( z, levs=z_values )\n"
"     Plot contours of Z on the mesh Y versus X.  Y, X, and IREG are\n"
"     as for plm.  The Z array must have the same shape as Y and X.\n"
"     The function being contoured takes the value Z at each point\n"
"     (X,Y) -- that is, the Z array is presumed to be point-centered.\n"
"     The Y, X, and IREG arguments may all be omitted to default to the\n"
"     mesh set by the most recent plmesh call.\n"
"     The LEVS keyword is a list of the values of Z at which you want\n"
"     contour curves.  The default is eight contours spanning the\n"
"     range of Z.\n"
"     The following keywords are legal (each has a separate help entry):\n"
"\n"
"   KEYWORDS: legend, hide\n"
"             type, width, color, smooth\n"
"             marks, marker, mspace, mphase\n"
"             smooth, triangle, region\n"
"\n"
"   SEE ALSO: plg, plm, plc, plv, plf, pli, plt, pldj, plfp, plmesh\n"
"             limits, logxy, ylimits, fma, hcp\n";

#undef N_KEYWORDS
#define N_KEYWORDS 15
static char *plcKeys[N_KEYWORDS+1]= {
  "legend", "hide", "region", "color", "type", "width",
  "marks", "mcolor", "marker", "msize", "mspace", "mphase",
  "smooth", "triangle", "levs", 0 };

static PyObject *plc (PyObject * self, PyObject * args, PyObject * kd)
{
  GaQuadMesh mesh;
  PyArrayObject *zap;
  PyObject *zop;
  int i;  
  char *z_name= 0, *y_name= 0, *x_name= 0, *r_name= 0;
  long iMax = 0, jMax = 0, nLevels = 0;
  double *z = 0, *levels = 0; /* UPDATE */
  PyObject * kwt[NELT(plcKeys) - 1];
  char *errstr =
    "plc requires 2D arguments (z [ , y, x, ireg, levs = levels ] )";

  SETJMP0;			/* See Xerror_longjmp() */

  if (PyTuple_Size (args) == 0)  {
    return ERRSS ("plc requires at least one argument");
  }
  BUILD_KWT(kd, plcKeys, kwt);
  TRY (setz_mesh (args, &zop, errstr, kwt[13]), (PyObject *) NULL);
  if (!pyMsh.y)  {
    return ERRSS ("No current mesh - set (y, x) first");
  }
  GET_ARR (zap, zop, PyArray_DOUBLE, 2, PyObject *);
  jMax = A_DIM(zap, 0);
  iMax = A_DIM(zap, 1);
  if (A_DIM (pyMsh.y, 0) != jMax || A_DIM (pyMsh.y, 1) != iMax) {
    clearArrayList ();
    return ERRSS ("Z array must match (y, x) mesh arrays in shape");
  }
  z = (double *) A_DATA (zap);
  get_mesh (&mesh);
  if (mesh.iMax!=iMax || mesh.jMax!=jMax)  {
     return ERRSS ("z array must have same dimensions as mesh in plc");
  }

  /* set legend and hide in gistD */
  TRYS(CheckDefaultWindow())
  if ( !LegendAndHide("\001: plc, ", z_name, y_name, x_name, r_name, kwt, plcKeys) )
     return ERRSS ( "Error in plc: LegendAndHide" );

  /* set properties, starting from defaults for decorated polylines */
  GhGetLines();
  gistD.region = 0;

  SETKW(kwt[0],  gistD.legend,    setkw_string,   plcKeys[0]);
  SETKW(kwt[1],  gistD.hidden,    setkw_boolean,  plcKeys[1]);
  SETKW(kwt[2],  gistD.region,    setkw_integer,  plcKeys[2]);
  SETKW(kwt[3],  gistA.l.color,   setkw_color,    plcKeys[3]);
  if(kwt[3]) gistA.m.color = gistA.l.color;
  SETKW(kwt[4],  gistA.l.type,    setkw_linetype, plcKeys[4]);

  SETKW(kwt[5],  gistA.l.width,   setkw_double,   plcKeys[5]);
  SETKW(kwt[6],  gistA.dl.marks,  setkw_boolean,  plcKeys[6]);
  SETKW(kwt[7],  gistA.m.color,   setkw_color,    plcKeys[7]);
  SETKW(kwt[8],  gistA.m.type,    setkw_xinteger, plcKeys[8]);
  SETKW(kwt[9],  gistA.m.size,    setkw_double,   plcKeys[9]);

  SETKW(kwt[10], gistA.dl.mSpace, setkw_double,   plcKeys[10]);
  SETKW(kwt[11], gistA.dl.mPhase, setkw_double,   plcKeys[11]);
  SETKW(kwt[12], gistA.dl.smooth, setkw_boolean,  plcKeys[12]);
  /* kwt[13] ("triangle=") was handled by setz_mesh. */

  /* set contour levels */
  if(kwt[14]) { /* levs= keyword */
    PyArrayObject *lap;
    double *lev;

    GET_ARR (lap, kwt[14], PyArray_DOUBLE, 1, PyObject *);
    lev = (double *) A_DATA (lap);
    nLevels = A_SIZE (lap);
    levels = p_malloc (sizeof(double) * nLevels);
    for(i = 0; i < nLevels; i++)
      levels[i] = lev[i];
    if (levels)  {
      levels= CopyLevels(levels, nLevels);
    }
    removeFromArrayList ( (PyObject *) lap);
  } 

  if (!levels) {
    /* create a default set of contour levels now */
    int i;
    double zmin, zmax, step;

    nLevels= 8;
    levels= CopyLevels((double *)0, nLevels);
    GetPCrange(&zmin, &zmax, z, mesh.reg, gistD.region, iMax, jMax);

    step= (zmax-zmin)/8.0;
    levels[0]= zmin+0.5*step;
    for (i=1 ; i<8 ; i++) levels[i]= levels[i-1]+step;
  }

  curElement = -1;
  PyFPE_START_PROTECT("plc", return 0)
  curElement =
    GdContours (NOCOPY_MESH, &mesh, gistD.region, z, levels, (int)nLevels);
  PyFPE_END_PROTECT(dummy)
  Py_DECREF (zap);
  SAFE_FREE (levels);
  array_list_length = 0;
  mem_list_length = 0;
  if (curElement < 0)  {
    return ERRSS ("Gist GdContour plotter failed");
  }
  tmpLevels = 0; /* Gist now owns this pointer */

  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

static double *CopyLevels(double *levels, long nLevels)
{
  long i;
  double *tmp= tmpLevels;
  tmpLevels= 0;
  if (tmp) p_free(tmp);
  tmpLevels= p_malloc(sizeof(double)*nLevels);
  if(!tmpLevels) return tmpLevels; 
  for (i=0 ; i<nLevels ; i++) tmpLevels[i]= levels? levels[i] : 0.0;
  return tmpLevels;
}

/*  -------------------------------------------------------------------- */
/*  pldefault */

static char pldefault__doc__[] =
"pldefault( key1=value1, key2=value2, ... )\n"
"     Set default values for the various properties of graphical elements.\n"
"\n"
"     The keywords can be most of the keywords that can be passed to the\n"
"     plotting commands:\n"
"       plg:  color, type, width,\n"
"             marks, mcolor, msize, mspace, mphase,\n"
"             rays, rspace, rphase, arrowl, arroww\n"
"       pldj: color, type, width\n"
"       plt:  color, font, height, path, justify, opaque\n"
"       plm:  color, type, width\n"
"       plv:  color, hollow, width, aspect\n"
"       plc:  color, type, width,\n"
"             marks, mcolor, marker, msize, mspace, mphase\n"
"       plf:  edges, ecolor, ewidth\n"
"\n"
"     The initial default values are:\n"
"       color=`fg', type=`solid', width=1.0 (1/2 point),\n"
"       marks=1, mcolor=`fg', msize=1.0 (10 points),\n"
"          mspace=0.16, mphase=0.14,\n"
"       rays=0, arrowl=1.0 (10 points), arroww=1.0 (4 points),\n"
"          rspace=0.13, rphase=0.11375,\n"
"       font=`helvetica', height=12.0, path=0, justify=`NN', opaque=0,\n"
"       hollow= 0, aspect=0.125,\n"
"       edges=0, ecolor=`fg', ewidth=1.0 (1/2 point)\n"
"\n"
"     Additional default keywords are:\n"
"       dpi, style, legends  (see window command)\n"
"       palette              (to set default filename as in palette command)\n"
"       maxcolors            (default 200)\n"
"       cgmfilesize          (default 10 MB) in units of MBytes\n"
"\n"
"   SEE ALSO: window, plsys, plq, pledit, plg\n";

#undef N_KEYWORDS
#define N_KEYWORDS 30
static char *dfltKeys[N_KEYWORDS+1]= {
  "color", "type", "width",
  "marks", "mcolor", "marker", "msize", "mspace", "mphase",
  "rays", "arrowl", "arroww", "rspace", "rphase",
  "font", "height", "orient", "justify", "opaque",
  "hollow", "aspect", "dpi", "style", "legends", "palette", "maxcolors",
  "edges", "ecolor", "ewidth", "cgmfilesize", 0 };

static PyObject *pldefault (PyObject * self, PyObject * args, PyObject * kd)
{
  PyObject * kwt[NELT(dfltKeys) - 1];
  char *errstr = "pldefault takes no non-keyword arguments";
  int dpi, type;

  if(PyTuple_Size(args) > 0) {
    return ERRSS (errstr);
  }
  
  /* retrieve all default settings */
  GhGetLines();
  GhGetMesh();
  GhGetVectors();
  GhGetText();

  BUILD_KWT(kd, dfltKeys, kwt);
  SETKW(kwt[0],  gistA.l.color,     setkw_color,    dfltKeys[0]);
  SETKW(kwt[1],  gistA.l.type,      setkw_linetype, dfltKeys[1]);
  SETKW(kwt[2],  gistA.l.width,     setkw_double,   dfltKeys[2]);
  SETKW(kwt[3],  gistA.dl.marks,    setkw_boolean,  dfltKeys[3]);
  SETKW(kwt[4],  gistA.m.color,     setkw_color,    dfltKeys[4]);
  SETKW(kwt[5],  gistA.m.type,      setkw_xinteger, dfltKeys[5]);

  SETKW(kwt[6],  gistA.m.size,      setkw_double,   dfltKeys[6]);
  SETKW(kwt[7],  gistA.dl.mSpace,   setkw_double,   dfltKeys[7]);
  SETKW(kwt[8],  gistA.dl.mPhase,   setkw_double,   dfltKeys[8]);
  SETKW(kwt[9],  gistA.dl.rays,     setkw_boolean,  dfltKeys[9]);
  SETKW(kwt[10], gistA.dl.arrowL,   setkw_double,   dfltKeys[10]);

  SETKW(kwt[11], gistA.dl.arrowW,   setkw_double,   dfltKeys[11]);
  SETKW(kwt[12], gistA.dl.rSpace,   setkw_double,   dfltKeys[12]);
  SETKW(kwt[13], gistA.dl.rPhase,   setkw_double,   dfltKeys[13]);
  SETKW(kwt[14], gistA.t.font,      setkw_fonttype, dfltKeys[14]);
  if(kwt[15]) {
    SETKW(kwt[15], gistA.t.height,  setkw_double,   dfltKeys[15]);
    gistA.t.height *= ONE_POINT;
  }

  SETKW(kwt[16], gistA.t.orient,    setkw_integer,  dfltKeys[16]);
  if (!gistA.t.orient) {
    gistA.t.orient = TX_RIGHT;
  } else {
    if (gistA.t.orient == 1) gistA.t.orient = TX_UP;
    else if (gistA.t.orient == 2) gistA.t.orient = TX_LEFT;
    else if (gistA.t.orient == 3) gistA.t.orient = TX_DOWN;
    else {
      gistA.t.orient= TX_RIGHT;
      return ERRSS ("orient= keyword must be 0, 1, 2, or 3");
    }
  }

  SETKW(kwt[17], dummy,             setkw_justify,  dfltKeys[17]);
  SETKW(kwt[18], gistA.t.opaque,    setkw_boolean,  dfltKeys[18]);
  SETKW(kwt[19], gistA.vect.hollow, setkw_boolean,  dfltKeys[19]);
  SETKW(kwt[20], gistA.vect.aspect, setkw_double,   dfltKeys[20]);

  if(kwt[21]) {
    SETKW(kwt[21], dpi,             setkw_integer,  dfltKeys[21]);
    if (dpi<25) dpi = 25;
    else if (dpi>300) dpi = 300;
    defaultDPI = dpi;
  }

  if(kwt[22]) {
    char *style;
    SAFE_FREE(defaultStyle);
    SETKW(kwt[22], style,           setkw_string,   dfltKeys[22]);
    if(style && style[0]){
      NEW_MEM (defaultStyle, strlen(style) + 1, char, PyObject *);
      strcpy(defaultStyle, style);
    }
  }

  SETKW(kwt[23], defaultLegends,    setkw_boolean,  dfltKeys[23]);

  if(kwt[24]) {
    char *name;
    SAFE_FREE(defaultPalette);
    SETKW(kwt[24], name,            setkw_string,   dfltKeys[24]);
    if(name && name[0]){
      NEW_MEM (defaultPalette, strlen(name) + 1, char, PyObject *);
      strcpy(defaultPalette, name);
    }
  }

  SETKW(kwt[25], maxColors,         setkw_integer,  dfltKeys[25]);
  if(kwt[26]) {
    SETKW(kwt[26], type,            setkw_boolean,  dfltKeys[26]);
    gistA.e.type = type ? L_SOLID : L_NONE;
  }
  SETKW(kwt[27], gistA.e.color,     setkw_color,    dfltKeys[27]);
  SETKW(kwt[28], gistA.e.width,     setkw_double,   dfltKeys[28]);

  {
  int filesize;
  extern long gCGMFileSize;
  SETKW(kwt[29], filesize,     setkw_integer,   dfltKeys[29]);
  gCGMFileSize = ((long)1000000)*((long)filesize);
  }

  /* store all default settings */
  GhSetLines();
  GhSetMesh();
  GhSetVectors();
  GhSetText();
  GhSetFill();

  mem_list_length = 0;
  Py_INCREF (Py_None);
  return Py_None;
}

/*  -------------------------------------------------------------------- */

#undef N_KEYWORDS
#define N_KEYWORDS 5
static char *pldjKeys[N_KEYWORDS+1]= {
  "legend", "hide", "color", "type", "width", 0 };

static char pldj__doc__[] =
"pldj( x0, y0, x1, y1 )\n"
"     Plot disjoint lines from (X0,Y0) to (X1,Y1).  X0, Y0, X1, and Y1\n"
"     may have any dimensionality, but all must have the same number of\n"
"     elements.\n"
"     The following keywords are legal (each has a separate help entry):\n"
"\n"
"   KEYWORDS: legend, hide\n"
"             type, width, color\n"
"\n"
"   SEE ALSO: plg, plm, plc, plv, plf, pli, plt, pldj, plfp\n"
"             limits, logxy, ylimits, fma, hcp\n";

static PyObject *pldj (PyObject * self, PyObject * args, PyObject * kd)
{
  PyObject *op[4];
  PyArrayObject *ap[4];
  double *d[4];
  int i;
  char *x0_name= 0, *y0_name= 0, *x1_name= 0, *y1_name= 0;
  long n= 0;

  PyObject * kwt[NELT(pldjKeys) - 1];
  char *errstr = "pldj requires exactly four non-keyword arguments";

  SETJMP0;

  if (!PyArg_ParseTuple (args, "OOOO", &op[0], &op[1], &op[2], &op[3]))  {
    return ERRSS (errstr);
  }
  
  for (i=0; i<4; i++)
    TRY (addToArrayList ((PyObject *)(ap[i] = (PyArrayObject *)
        PyArray_ContiguousFromObject (op[i], PyArray_DOUBLE, 1, 0))),
        (PyObject *)PyErr_NoMemory ());

  n = A_SIZE ( ap[0] );
  for (i=1; i<4; i++)
    if ( A_SIZE (ap[i]) != n) {
      clearArrayList ();
      return ERRSS ("pldj arguments must all be the same size");
    }

  /* set legend and hide in gistD */
  TRYS(CheckDefaultWindow())

  /* set properties, starting from defaults for simple polylines */
  GhGetMesh();

  BUILD_KWT(kd, pldjKeys, kwt);
  if ( !LegendAndHide("pldj, ", x0_name, y0_name, x1_name, y1_name, kwt, pldjKeys) )
     return ERRSS ( "Error in pldj: LegendAndHide" );
  SETKW(kwt[0],  gistD.legend,    setkw_string,   pldjKeys[0]);
  SETKW(kwt[1],  gistD.hidden,    setkw_boolean,  pldjKeys[1]);
  SETKW(kwt[2],  gistA.l.color,   setkw_color,    pldjKeys[2]);
  SETKW(kwt[3],  gistA.l.type,    setkw_linetype, pldjKeys[3]);
  SETKW(kwt[4],  gistA.l.width,   setkw_double,   pldjKeys[4]);

  for (i=0; i<4; i++)
    d[i] = (double *) A_DATA (ap[i]);

  curElement = -1;
  PyFPE_START_PROTECT("pldj", return 0)
  curElement = GdDisjoint (n, d[0], d[1], d[2], d[3]);
  PyFPE_END_PROTECT(dummy)
  clearArrayList ();
  if (curElement < 0)  {
    return ERRSS ("Gist GdDisjoint plotter failed");
  }

  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

/*  -------------------------------------------------------------------- */

static char pledit__doc__[] =
"pledit( key1=value1, key2=value2, ... )\n"
"or pledit( n_element, key1=value1, key2=value2, ... )\n"
"or pledit( n_element, n_contour, key1=value1, key2=value2, ... )\n"
"     Changes some property of element number N_ELEMENT (and contour\n"
"     number N_CONTOUR of that element).  If N_ELEMENT and N_CONTOUR are\n"
"     omitted, the default is the most recently added element, or the\n"
"     element specified in the most recent plq query command.\n"
"\n"
"     The keywords can be any of the keywords that apply to the current\n"
"     element.  These are:\n"
"       plg:  color, type, width,\n"
"             marks, mcolor, marker, msize, mspace, mphase,\n"
"             rays, rspace, rphase, arrowl, arroww,\n"
"             closed, smooth\n"
"       pldj: color, type, width\n"
"       plt:  color, font, height, path, justify, opaque\n"
"       plm:  region, boundary, inhibit, color, type, width\n"
"       plf:  region\n"
"       plv:  region, color, hollow, width, aspect, scale\n"
"       plc:  region, color, type, width,\n"
"             marks, mcolor, marker, msize, mspace, mphase\n"
"             smooth, levs\n"
"     (For contours, if you aren't talking about a particular N_CONTOUR,\n"
"      any changes will affect ALL the contours.)\n"
"\n"
"     A plv (vector field) element can also take the scalem\n"
"     keyword to multiply all vector lengths by a specified factor.\n"
"\n"
"     A plt (text) element can also take the dx and/or dy\n"
"     keywords to adjust the text position by (dx,dy).\n"
"\n"
"   SEE ALSO: window, plsys, plq, pldefault, plg\n";

#undef N_KEYWORDS
#define N_KEYWORDS 36
static char *editKeys[N_KEYWORDS+1]= {
  "legend", "hide",
  "color", "type", "width",
  "marks", "mcolor", "marker", "msize", "mspace", "mphase",
  "rays", "arrowl", "arroww", "rspace", "rphase", "closed", "smooth",
  "font", "height", "orient", "justify", "opaque",
  "hollow", "aspect", "region", "boundary", "levs", "scale", "scalem",
  "dx", "dy", "edges", "ecolor", "ewidth", "inhibit", 0 };

static PyObject *pledit (PyObject * self, PyObject * args, PyObject * kd)
{
  int type = 0, n_element = 0, n_contour = 0;
  int changes = 0, resetLevs = 0; 
  PyObject *kwt[NELT (editKeys) - 1];
  char *legend = 0;

  switch (PyTuple_Size (args)) {
  case 2: /* (n_element, n_contour) given */
    TRY (PyArg_ParseTuple (args, "ii", &n_element, &n_contour),
       (PyObject *) NULL);
    break;
  case 1: /* (n_element) given */
    TRY (PyArg_ParseTuple (args, "i", &n_element), (PyObject *) NULL);
    break;
  case 0: /* () given */
    break;
  default:
    return ERRSS ("pledit function takes no more than two arguments");
  }

  /* Pygist uses 1-origin element numbering, Gist uses 0-origin */
  n_element--;
  n_contour--;

  if (n_element < 0) {
    if (curElement >= 0) {
      n_element = GdFindIndex (curElement);
      if (n_element < 0) {
	curElement = -1;
	return ERRSS ("lost current graphical element for pledit (BUG?)");
      }
    } else if (curElement == -6666) {
      n_element = curIX;
      n_contour = curIXc;
    } else {
      return ERRSS ("no current graphical element for pledit");
    }
  }
  if (n_element >= 0 || n_contour >= 0) {
    /* retrieve specified element */
    if (n_element >= 0)
      type = GdSetElement (n_element);
    if (n_contour >= 0) {
      if (type != E_CONTOURS)  {
	return ERRSS ("current graphical element is not contours in pledit");
      }
      type = GdSetContour (n_contour);
    }
    curElement = -6666;		/* differs from -1 to allow pledit after plq */
    curIX = n_element;		/* need these, too */
    curIXc = n_contour;
    if (type == E_LINES) type = 1;
    else if (type == E_DISJOINT) type = 2;
    else if (type == E_TEXT) type = 3;
    else if (type == E_MESH) type = 4;
    else if (type == E_FILLED) type = 5;
    else if (type == E_VECTORS) type = 6;
    else if (type == E_CONTOURS) type = 7;
    else if (type == E_CELLS) type = 8;
    else type = 0;
    if (type == 0)  {
      return ERRSS ("no such graphical element for pledit");
    }
  }

  BUILD_KWT(kd, editKeys, kwt);
  SETKW(kwt[0],   legend,          setkw_string,   editKeys[0]);
  SETKW(kwt[1],   gistD.hidden,    setkw_boolean,  editKeys[1]);
  if(kwt[2]){
    SETKW(kwt[2], gistA.l.color,   setkw_color,    editKeys[2]);
    gistA.m.color = gistA.f.color = gistA.t.color = gistA.l.color;
  }
  SETKW(kwt[3],   gistA.l.type,    setkw_linetype, editKeys[3]);
  SETKW(kwt[4],   gistA.l.width,   setkw_double,   editKeys[4]);
  SETKW(kwt[5],   gistA.dl.marks,  setkw_boolean,  editKeys[5]);
  SETKW(kwt[6],   gistA.m.color,   setkw_color,    editKeys[6]);
  SETKW(kwt[6],   gistA.m.color,   setkw_color,    editKeys[6]);
  SETKW(kwt[7],   gistA.m.type,    setkw_xinteger, editKeys[7]);
  SETKW(kwt[8],   gistA.m.size,    setkw_double,   editKeys[8]);
  SETKW(kwt[9],   gistA.dl.mSpace, setkw_double,   editKeys[9]);
  SETKW(kwt[10],  gistA.dl.mPhase, setkw_double,   editKeys[10]);
  SETKW(kwt[11],  gistA.dl.rays,   setkw_boolean,  editKeys[11]);
  SETKW(kwt[12],  gistA.dl.arrowL, setkw_double,   editKeys[12]);
  SETKW(kwt[13],  gistA.dl.arrowW, setkw_double,   editKeys[13]);
  SETKW(kwt[14],  gistA.dl.rSpace, setkw_double,   editKeys[14]);
  SETKW(kwt[15],  gistA.dl.rPhase, setkw_double,   editKeys[15]);
  SETKW(kwt[16],  gistA.dl.closed, setkw_boolean,  editKeys[16]);
  SETKW(kwt[17],  gistA.dl.smooth, setkw_boolean,  editKeys[17]);
  SETKW(kwt[18],  gistA.t.font,    setkw_fonttype, editKeys[18]);
  if(kwt[19]) {
    SETKW(kwt[19], gistA.t.height, setkw_double,   editKeys[19]);
    gistA.t.height *= ONE_POINT;
  }
  SETKW(kwt[20], gistA.t.orient,   setkw_integer,  editKeys[20]);
  if (!gistA.t.orient) {
    gistA.t.orient = TX_RIGHT;
  } else {
    if (gistA.t.orient == 1) gistA.t.orient = TX_UP;
    else if (gistA.t.orient == 2) gistA.t.orient = TX_LEFT;
    else if (gistA.t.orient == 3) gistA.t.orient = TX_DOWN;
    else {
      gistA.t.orient= TX_RIGHT;
      return ERRSS ("orient= keyword must be 0, 1, 2, or 3");
    }
  }

  SETKW(kwt[21], dummy,            setkw_justify,  editKeys[21]);
  SETKW(kwt[22], gistA.t.opaque,   setkw_boolean,  editKeys[22]);
  SETKW(kwt[23], gistA.vect.hollow, setkw_boolean,  editKeys[23]);
  SETKW(kwt[24], gistA.vect.aspect, setkw_double,   editKeys[24]);
  
  if (kwt[25]) {	/* region */
    if (type < 4 || type > 7)  {
      return ERRSS ("region = in pledit allowed only for plm, plf, plv, plc");
    }
    SETKW(kwt[25],  gistD.region,   setkw_integer,  editKeys[25]);
  }
  if (kwt[26]) {	/* boundary */
    if (type != 4)  {
      return ERRSS ("boundary = in pledit allowed only for plm");
    }
    SETKW(kwt[26],  gistD.boundary, setkw_boolean,  editKeys[26]);
  }

  if (kwt[27]) {	/* levs */
    double *levels;
    long nLevels = 0;
    PyArrayObject *lap;
    double *lev;
    int i;

    if (type != 7)  {
      return ERRSS ("levs = in pledit allowed only for plc");
    }

    GET_ARR (lap, kwt[27], PyArray_DOUBLE, 1, PyObject *);
    lev = (double *) A_DATA (lap);
    nLevels = A_SIZE (lap);
    if (0 == nLevels) {
      clearArrayList ();
      return ERRSS ("pledit cannot recompute default contour levels");
      }
    levels = malloc (sizeof(double) * nLevels);
    if(!levels) return PyErr_NoMemory();
    for(i = 0; i < nLevels; i++)
      levels[i] = lev[i];
    removeFromArrayList ( (PyObject *) lap);
    /* WARNING --
       this is a critical code section, since until GdEdit successfully
       completes, Gist owns a pointer to the freed levels -- no way to
       gracefully avoid this without "knowing" more about guts of Gist's
       data structures than seem reasonable here... */
    p_free (gistD.levels);
    gistD.levels = levels;
    gistD.nLevels = nLevels;
    changes |= CHANGE_Z;
    resetLevs = 1;
  }
  if (kwt[28]) {	/* scale */
    if (type != 6)  {
      return ERRSS ("scale = in pledit allowed only for plv");
    }
    SETKW(kwt[28],  gistD.scale,  setkw_double,  editKeys[28]);
  }
  if (kwt[29]) {	/* scalem */
    double scalem;
    if (type != 6)  {
      return ERRSS ("scalem = in pledit allowed only for plv");
    }
    SETKW(kwt[29],  scalem,       setkw_double,  editKeys[29]);
    PyFPE_START_PROTECT("pledit", return 0)
    gistD.scale *= scalem;
    PyFPE_END_PROTECT(dummy)
  }
  if (kwt[30]) {	/* dx */
    double x0;
    if (type != 3)  {
      return ERRSS ("dx = in pledit allowed only for plt");
    }
    SETKW(kwt[30],  x0,           setkw_double,  editKeys[30]);
    PyFPE_START_PROTECT("pledit", return 0)
    gistD.x0 += x0;
    PyFPE_END_PROTECT(dummy)
  }
  if (kwt[31]) {	/* dy */
    double y0;
    if (type != 3)  {
      return ERRSS ("dy = in pledit allowed only for plt");
    }
    SETKW(kwt[31],  y0,           setkw_double,  editKeys[31]);
    PyFPE_START_PROTECT("pledit", return 0)
    gistD.y0 += y0;
    PyFPE_END_PROTECT(dummy)
  }
  if (kwt[32]) {
    int edgetype = 0;
    SETKW(kwt[32],  edgetype,     setkw_boolean, editKeys[32]);
    gistA.e.type = edgetype ? L_SOLID : L_NONE;
  }
  SETKW(kwt[33],  gistA.e.color,  setkw_color,   editKeys[33]);
  SETKW(kwt[34],  gistA.e.width,  setkw_double,  editKeys[34]);

  if (kwt[35]) {	/* inhibit */
    if (type != 4)  {
      return ERRSS ("inhibit = in pledit allowed only for plm");
    }
    SETKW(kwt[35],  gistD.inhibit, setkw_integer, editKeys[35]);
  }
  if (legend) {
    /* Some jiggery-pokery necessary to get the old legend deleted properly,
       and the new legend allocated properly, so that Gist will delete it
       correctly when the graphical element is deleted.  */
    char *oldleg = gistD.legend;
    if (!(gistD.legend = p_malloc (strlen (legend) + 1)))
       return PyErr_NoMemory();
    strcpy (gistD.legend, legend);
    legend = oldleg;
  }
  GdEdit (changes);
  if (legend)
    p_free (legend);
  if ( resetLevs )  {
     tmpLevels = 0;
  }
  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  plf */

static char plf__doc__[] =
"plf( z, y, x )\n"
"or plf( z, y, x, ireg )\n"
"or plf( z )\n"
"     Plot a filled mesh Y versus X.  Y, X, and IREG are as for plm.\n"
"     The Z array must have the same shape as Y and X, or one smaller\n"
"     in both dimensions.  If Z is of type char, it is used `as is',\n"
"     otherwise it is linearly scaled to fill the current palette, as\n"
"     with the bytscl function.\n"
"     (See the bytscl function for explanation of top, cmin, cmax.)\n"
"     The mesh is drawn with each zone in the color derived from the Z\n"
"     function and the current palette; thus Z is interpreted as a\n"
"     zone-centered array.\n"
"     The Y, X, and IREG arguments may all be omitted to default to the\n"
"     mesh set by the most recent plmesh call.\n"
"     A solid edge can optionally be drawn around each zone by setting\n"
"     the EDGES keyword non-zero.  ECOLOR and EWIDTH determine the edge\n"
"     color and width.  The mesh is drawn zone by zone in order from\n"
"     IREG(2+imax) to IREG(jmax*imax) (the latter is IREG(imax,jmax)),\n"
"     so you can achieve 3D effects by arranging for this order to\n"
"     coincide with back-to-front order.  If Z is nil, the mesh zones\n"
"     are filled with the background color, which you can use to\n"
"     produce 3D wire frames.\n"
"     The following keywords are legal (each has a separate help entry):\n"
"\n"
"   KEYWORDS: legend, hide\n"
"             region, top, cmin, cmax, edges, ecolor, ewidth\n"
"\n"
"   SEE ALSO: plg, plm, plc, plv, plf, pli, plt, pldj, plfp, plmesh,\n"
"             limits, logxy, ylimits, fma, hcp, palette, bytscl, histeq_scale\n";

#undef N_KEYWORDS
#define N_KEYWORDS 9
static char *plfKeys[N_KEYWORDS+1]= {
  "legend", "hide", "region", "top", "cmin", "cmax",
  "edges", "ecolor", "ewidth", 0 };

static PyObject *plf (PyObject * self, PyObject * args, PyObject * kd)
{
  PyArrayObject *zap;
  PyObject *zop = 0;
  char *z_name= 0, *y_name= 0, *x_name= 0, *r_name= 0;
  long iMax= 0, jMax= 0;
  double *z = 0;
  GpColor *zc = 0;
  GaQuadMesh mesh;
  int convertedZ= 0;
  int rgb = 0;

  PyObject * kwt[NELT(plfKeys) - 1];
  char *errstr = "plf requires 2D arguments (z [ , y, x, ireg ] )";

  SETJMP0;			/* See Xerror_longjmp() */

  if (PyTuple_Size (args) == 0)  {
    return ERRSS ("plf requires at least one argument");
  }
  BUILD_KWT(kd, plfKeys, kwt);
  TRY (setz_mesh (args, &zop, errstr, 0), (PyObject *) NULL);
  if (!pyMsh.y)  {
    return ERRSS ("No current mesh - set (y, x) first");
  }

  get_mesh (&mesh);

  /*
   *  The first arg to plf, z, is of type unsigned char.
   *  The array size is (M-1,N-1) or
   *  (3,M-1,N-1), giving an (r,g,b) for each true color value.
   */

  if (isARRAY(zop) && (A_TYPE(zop) == Py_GpColor)) {

    if ( A_NDIM(zop) == 2 )  {
       /*  NXxNY */
       GET_ARR (zap, zop, Py_GpColor, 2, PyObject *);
       zc = (GpColor *) A_DATA (zap);
    }
    else if ( A_NDIM(zop) == 3 )  {
       /*  3xNXxNY */
       if ( A_DIM(zop,0) != 3 )  {
          return ERRSS ("expecting NXxNY or 3xNXxNY array as argument to plf");
       }
       GET_ARR (zap, zop, Py_GpColor, 3, PyObject *);
       zc = (GpColor *) A_DATA (zap);
       rgb = 1;
    }
    else  {
       return ERRSS ("expecting NXxNY or 3xNXxNY array as argument to plf"); 
    }

  } else {
    if (isARRAY(zop) && (A_TYPE(zop) == PyArray_DOUBLE)) {
      GET_ARR (zap, zop, PyArray_DOUBLE, 2, PyObject *);
      z = (double *) A_DATA (zap);
    } else {
      z = 0;
      zc = 0;
      zap = 0;
    }
  }

  if (zap) {
    jMax = A_DIM(zap, 0);
    iMax = A_DIM(zap, 1);
  } else {
    jMax = iMax = 0;
  }
  if ((z || zc) && ((mesh.iMax != iMax   || mesh.jMax != jMax) &&
		    (mesh.iMax != iMax+1 || mesh.jMax != jMax+1))) {
    removeFromArrayList ( (PyObject *) zap);
    return ERRSS (
      "z array must have same or 1 smaller dimensions as mesh in plf");
  }

  TRYS(CheckDefaultWindow())
  CheckDefaultPalette ();

  if ( !LegendAndHide("plf, ", z_name, y_name, x_name, r_name, kwt, plfKeys) )
     return ERRSS ( "Error in plf: LegendAndHide" );

  gistD.region = 0;
  SETKW(kwt[2],  gistD.region,    setkw_integer,  plfKeys[2]);

  if (!zc && z) {
    double zmin, zmax, scale, offset;

    TRY (GrabByteScale(&kwt[3], &plfKeys[3], &scale, &offset, &zmin, &zmax,
       z, mesh.reg, gistD.region, mesh.iMax, mesh.jMax,
       (int) (mesh.iMax != iMax) ), (PyObject *) NULL);
    TRY (zc = PushColors(z, iMax*jMax, zmin, zmax, scale, offset),
       (PyObject *) NULL);
    convertedZ= 1;
  }

  GhGetFill();

  SETKW(kwt[0],  gistD.legend,    setkw_string,   plfKeys[0]);
  SETKW(kwt[1],  gistD.hidden,    setkw_boolean,  plfKeys[1]);
  if (kwt[6]) {
    int edgetype = 0;
    SETKW(kwt[6],  edgetype,  setkw_boolean,  plfKeys[6]);
    gistA.e.type = edgetype ? L_SOLID : L_NONE;
  }
  SETKW(kwt[7],  gistA.e.color,   setkw_color,    plfKeys[7]);
  SETKW(kwt[8],  gistA.e.width,    setkw_double, plfKeys[8]);
  gistA.rgb = rgb;

  if (mesh.iMax==iMax) zc += rgb? 3*(iMax+1) : iMax+1;
  curElement = -1;
  PyFPE_START_PROTECT("plf", return 0)
  curElement = GdFillMesh(NOCOPY_MESH, &mesh, gistD.region, zc, iMax);
  PyFPE_END_PROTECT(dummy)
  clearArrayList ();
  /* 
   *  04/22/03 LLC 
   *  Do not free zc:  if (convertedZ && zc) free (zc);
   *  zc could be not on an 8-byte boundary on the alphas;
   *  free yields unaligned access messages.
   *  Also, I do not find it freed in yorick.
   */
  if (curElement < 0)  {
    return ERRSS ("Gist GdFillMesh plotter failed");
  }

  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  plfp */

static char plfp__doc__[] =
"plfp( z, y, x, n )\n"
"     Plot a list of filled polygons Y versus X, with colors Z.\n"
"     The N array is a 1D list of lengths (number of corners) of the\n"
"     polygons; the 1D colors array Z has the same length as N.  The\n"
"     X and Y arrays have length equal to the sum of all dimensions\n"
"     of N.\n"
"     The Z array must have the same shape as Y and X.  If Z is of\n"
"     type char, it is used `as is', otherwise it is linearly scaled\n"
"     to fill the current palette, as with the bytscl function.\n"
"     (See the bytscl function for explanation of top, cmin, cmax.)\n"
"     The following keywords are legal (each has a separate help entry):\n"
"\n"
"   KEYWORDS: legend, hide, top, cmin, cmax\n"
"\n"
"   SEE ALSO: plg, plm, plc, plv, plf, pli, plt, pldj\n"
"             limits, logxy, ylimits, fma, hcp\n";

#undef N_KEYWORDS
#define N_KEYWORDS 8
static char *plfpKeys[N_KEYWORDS+1]= {
  "legend", "hide", "top", "cmin", "cmax", "edges", "ecolor", "ewidth", 0 };

static PyObject *plfp (PyObject * self, PyObject * args, PyObject * kd)
{
  PyArrayObject *zap = 0, *yap, *xap, *nap;
  PyObject *zop, *yop, *xop, *nop;
  int i;
  long nz, nx, nn, np;
  long ny =0, *pn= 0;
  double *z = 0, *x, *y;
  GpColor *zc = 0;
  int convertedZ= 0;
  int rgb = 0;

  PyObject * kwt[NELT(plfpKeys) - 1];
  char *errstr = "plfp requires arguments (z, y, x, n)";

  SETJMP0;			/* See Xerror_longjmp() */

  if (!PyArg_ParseTuple (args, "OOOO", &zop, &yop, &xop, &nop))  {
    return ERRSS (errstr);
  }

  /*
   *  The first arg to plfp, z, is of type unsigned char.
   *  The array size is N or 3xN, giving an (r,g,b) for each 
   *  true color value.
   */

  if (isARRAY(zop) && (A_TYPE(zop) == Py_GpColor)) {

    if ( A_NDIM(zop) == 1 )  {
       /*  N */
       GET_ARR (zap, zop, Py_GpColor, 1, PyObject *);
       zc = (GpColor *) A_DATA (zap);
    }
    else if ( A_NDIM(zop) == 2 )  {
       /*  3xN */
       if ( A_DIM(zop,0) != 3 )  {
          return ERRSS ("expecting N or 3xN array as argument to plfp");
       }
       GET_ARR (zap, zop, Py_GpColor, 2, PyObject *);
       zc = (GpColor *) A_DATA (zap);
       rgb = 1;
    }
    else  {
       return ERRSS ("expecting N or 3xN array as argument to plfp"); 
    }

  } else if (isARRAY(zop) && (A_TYPE(zop) == PyArray_DOUBLE)) {
    GET_ARR (zap, zop, PyArray_DOUBLE, 1, PyObject *);
    z = (double *) A_DATA (zap);
  }

  GET_ARR (yap, yop, PyArray_DOUBLE, 1, PyObject *);
  GET_ARR (xap, xop, PyArray_DOUBLE, 1, PyObject *);
  GET_ARR (nap, nop, PyArray_LONG, 1, PyObject *);
  nn = A_SIZE (nap);
  nx = A_SIZE (xap);
  ny = A_SIZE (yap);
  nz = (zap) ? A_SIZE (zap) : nn;
  y = (double *) A_DATA (yap);
  x = (double *) A_DATA (xap);
  pn = (long *) A_DATA (nap);

  /* Error checking is complicated by required DECREF's on failure. */
  {
    char *es = 0;

    if (nx != ny) es = "len(x) != len(y)";
    if (nz && (nz != nn)) es = "len(n) != len(z)";
    for (np = i = 0; i < nn; i++) np += pn[i];
    if (np != ny) es = "sum(n) != len(y)";
    if (es) {
      clearArrayList ();
      return ERRSS (es);
    }
  }

  BUILD_KWT(kd, plfpKeys, kwt);

  TRYS(CheckDefaultWindow())
  CheckDefaultPalette ();
  /* would need to add plfp to quine list with YpQuine to get legend
     LegendAndHide("plfp, ", z_name, y_name, x_name, r_name, kwt, plfpKeys); */
  if ( !LegendAndHide((char *)0, (char *)0, (char *)0,
                (char *)0, (char *)0, kwt, plfpKeys) )
     return ERRSS ( "Error in plfp: LegendAndHide" );

  if (!zc && z) {
    double zmin, zmax, scale, offset;

    TRY (GrabByteScale(&kwt[2], &plfpKeys[2], &scale, &offset, &zmin, &zmax,
       z, (int *)0, 0, nz + 1, 2L, 1), (PyObject *) NULL);
    TRY (zc = PushColors(z, nz, zmin, zmax, scale, offset), (PyObject *) NULL);
    convertedZ= 1;
  }
  else {
    convertedZ= 0;
  }

  GhGetFill();

  SETKW(kwt[0],  gistD.legend,    setkw_string,   plfpKeys[0]);
  SETKW(kwt[1],  gistD.hidden,    setkw_boolean,  plfpKeys[1]);
  if (kwt[5]) {
    int edgetype = 0;
    SETKW(kwt[5],  edgetype,  setkw_boolean,  plfpKeys[5]);
    gistA.e.type = edgetype ? L_SOLID : L_NONE;
  }
  SETKW(kwt[6],  gistA.e.color,   setkw_color,    plfpKeys[6]);
  SETKW(kwt[7],  gistA.e.width,   setkw_double,   plfpKeys[7]);
  gistA.rgb = rgb;

  curElement = -1;
  PyFPE_START_PROTECT("plfp", return 0)
  curElement = GdFill (nz, zc, x, y, pn);
  PyFPE_END_PROTECT(dummy)
  clearArrayList ();
  if (convertedZ) free (zc);
  if (curElement < 0)  {
    return ERRSS ("Gist GdFill plotter failed");
  }

  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  plg */

static char plg__doc__[] =
"plg( y [, x] )\n"
"     Plot a graph of Y versus X.  Y and X must be 1-D arrays of equal\n"
"     length; if X is omitted, it defaults to [1, 2, ..., 1+range(len(Y))].\n"
"     The following keywords are legal (each has a separate help entry):\n"
"\n"
"   KEYWORDS: legend, hide\n"
"             type, width, color, closed, smooth\n"
"             marks, marker, mspace, mphase\n"
"             rays, arrowl, arroww, rspace, rphase\n"
"\n"
"   Example:    plg ( y, x, type=0, marker=character )\n"
"\n"
"   If character is '\\1', '\\2', '\\3', '\\4', or '\\5', you get point, plus,\n"
"   asterisk, circle, and cross, respectively.  If you make the marker size\n"
"   small enough (the default is small enough), then '\\1' will plot points\n"
"   on the X display, which is the most usual request.  For 2-5 or for large\n"
"   marker size, you get the characters on the X display, but in the\n"
"   hardcopy files (postscript or cgm) those special markers will be rendered\n"
"   nicely.\n"
"\n"
"   SEE ALSO: plg, plm, plc, plv, plf, pli, plt, pldj, plfp\n"
"             limits, logxy, ylimits, fma, hcp\n";

#undef N_KEYWORDS
#define N_KEYWORDS 19
static char *plgKeys[N_KEYWORDS+1]= {
  "legend", "hide", "color", "type", "width",
  "marks", "mcolor", "marker", "msize", "mspace", "mphase",
  "rays", "arrowl", "arroww", "rspace", "rphase",
  "closed", "smooth", "n", 0 };

static PyObject *plg (PyObject * self, PyObject * args, PyObject * kd)
{
  PyObject *xop = 0, *yop;
  PyArrayObject *xap, *yap;
  double *x= 0, *y= 0;

  int i;
  long length;
  PyObject * kwt[NELT(plgKeys) - 1];
  char *errstr =
    "plg requires one or two 1-D double arrays, of the same length";

  SETJMP0;			/* See Xerror_longjmp() */

  if (!PyArg_ParseTuple (args, "O|O", &yop, &xop)) {
    return ERRSS (errstr);
  }
  GET_ARR(yap, yop, PyArray_DOUBLE, 1, PyObject *);
  length = A_SIZE(yap);
  y = (double *) A_DATA(yap);

  TRYS(CheckDefaultWindow())

  GhGetLines();	/* Properties start from defaults for decorated polylines. */
  BUILD_KWT(kd, plgKeys, kwt);
  SETKW(kwt[0],  gistD.legend,    setkw_string,   plgKeys[0]);
  SETKW(kwt[1],  gistD.hidden,    setkw_boolean,  plgKeys[1]);
  SETKW(kwt[2],  gistA.l.color,   setkw_color,    plgKeys[2]);
  SETKW(kwt[2],  gistA.m.color,   setkw_color,    plgKeys[2]);
  SETKW(kwt[3],  gistA.l.type,    setkw_linetype, plgKeys[3]);
  SETKW(kwt[4],  gistA.l.width,   setkw_double,   plgKeys[4]);
  SETKW(kwt[5],  gistA.dl.marks,  setkw_boolean,  plgKeys[5]);
  SETKW(kwt[6],  gistA.m.color,   setkw_color,    plgKeys[6]);
  SETKW(kwt[7],  gistA.m.type,    setkw_xinteger, plgKeys[7]);
  SETKW(kwt[8],  gistA.m.size,    setkw_double,   plgKeys[8]);
  SETKW(kwt[9],  gistA.dl.mSpace, setkw_double,   plgKeys[9]);
  SETKW(kwt[10], gistA.dl.mPhase, setkw_double,   plgKeys[10]);
  SETKW(kwt[11], gistA.dl.rays,   setkw_boolean,  plgKeys[11]);
  SETKW(kwt[12], gistA.dl.arrowL, setkw_double,   plgKeys[12]);
  SETKW(kwt[13], gistA.dl.arrowW, setkw_double,   plgKeys[13]);
  SETKW(kwt[14], gistA.dl.rSpace, setkw_double,   plgKeys[14]);
  SETKW(kwt[15], gistA.dl.rPhase, setkw_double,   plgKeys[15]);
  SETKW(kwt[16], gistA.dl.closed, setkw_boolean,  plgKeys[16]);
  SETKW(kwt[17], gistA.dl.smooth, setkw_boolean,  plgKeys[17]);

  if (xop) {
    GET_ARR(xap, xop, PyArray_DOUBLE, 1, PyObject *);
    if(A_SIZE(xap) != length) {
      clearArrayList ();
      return ERRSS (errstr);
    }
    x = (double *) A_DATA(xap);
  } else {
    NEW_MEM (x, length, double, PyObject *);
    for (i = 0; i < length; i++)
	x[i] = (double) (1+i);
  }

  curElement = -1;
  PyFPE_START_PROTECT("plg", return 0)
  curElement = GdLines (length, x, y);
  PyFPE_END_PROTECT(dummy)

  clearArrayList ();
  clearMemList ();

  if (curElement < 0)  {
    return ERRSS ("Gist GdLines plotter failed");
  }

  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  pli */

static char pli__doc__[] =
"pli( z )\n"
"or pli( z, x1, y1 )\n"
"or pli( z, x0, y0, x1, y1 )\n"
"     Plot the image Z as a cell array -- an array of equal rectangular\n"
"     cells colored according to the 2-D array Z.  The first dimension\n"
"     of Z is plotted along x, the second dimension is along y.\n"
"     If Z is of type char, it is used `as is', otherwise it is linearly\n"
"     scaled to fill the current palette, as with the bytscl function.\n"
"     (See the bytscl function for explanation of top, cmin, cmax.)\n"
"     \n"
"     As for plf and plfp, Z may also be a 3D array with 1st dimension 3\n"
"     of char giving the [r,g,b] components of each color.  See the\n"
"     color keyword for cautions about using this if you do not have\n"
"     a true color display.\n"
"     \n"
"     If X1 and Y1 are given, they represent the coordinates of the\n"
"     upper right corner of the image.  If X0, and Y0 are given, they\n"
"     represent the coordinates of the lower left corner, which is at\n"
"     (0,0) by default.  If only the Z array is given, each cell will be\n"
"     a 1x1 unit square, with the lower left corner of the image at (0,0).\n"
"     The following keywords are legal (each has a separate help entry):\n"
"\n"
"   KEYWORDS: legend, hide, top, cmin, cmax\n"
"\n"
"   SEE ALSO: plg, plm, plc, plv, plf, pli, plt, pldj, plfp,\n"
"             limits, logxy, ylimits, fma, hcp, palette, bytscl, histeq_scale\n";

#undef N_KEYWORDS
#define N_KEYWORDS 5
static char *pliKeys[N_KEYWORDS+1]= {
  "legend", "hide", "top", "cmin", "cmax", 0 };

static PyObject *pli (PyObject * self, PyObject * args, PyObject * kd)
{
  PyArrayObject *zap;
  PyObject *zop = 0;
  char *z_name= 0;
  int nargs;
  double *z = 0, x0, y0, x1, y1;
  long iMax= 0, jMax= 0;
  GpColor *zc = 0;
  int convertedZ= 0;
  int rgb = 0;

  PyObject * kwt[NELT(pliKeys) - 1];
  char *errstr = "pli requires arguments (z [ , [ x0, y0, ] x1, y1 ] )";

  SETJMP0;			/* See Xerror_longjmp() */

  x0= y0= x1= y1= 0.0;

  switch (nargs = PyTuple_Size (args)) {
  case 5: /* (z, x0, y0, x1, y1) given */
    TRY (PyArg_ParseTuple (args, "Odddd", &zop, &x0, &y0, &x1, &y1), 
       (PyObject *) NULL);
    break;
  case 3: /* (z, x1, y1) given */
    TRY (PyArg_ParseTuple (args, "Odd", &zop, &x1, &y1), 
       (PyObject *) NULL);
    x0 = y0 = 0.0;
    break;
  case 1: /* (z) only given */
    TRY (PyArg_ParseTuple (args, "O", &zop), 
       (PyObject *) NULL);
    x0 = y0 = 0.0;
    break;
  default:
    return ERRSS (errstr);
  }

  /*  
   *  The first arg to pli, z, is of type unsigned char.
   *  The array size is (M-1,N-1) or 
   *  (3,M-1,N-1), giving an (r,g,b) for each true color value.
   */

  if (isARRAY(zop) && (A_TYPE(zop) == Py_GpColor)) {

    if ( A_NDIM(zop) == 2 )  {
       /*  NXxNY */
       GET_ARR (zap, zop, Py_GpColor, 2, PyObject *);
       zc = (GpColor *) A_DATA (zap);
    }
    else if ( A_NDIM(zop) == 3 )  {
       /*  3xNXxNY */
       if ( A_DIM(zop,0) != 3 )  {
          return ERRSS ("expecting NXxNY or 3xNXxNY array as argument to pli"); 
       }
       GET_ARR (zap, zop, Py_GpColor, 3, PyObject *);
       zc = (GpColor *) A_DATA (zap);
       rgb = 1;
    }
    else  {
       return ERRSS ("expecting NXxNY or 3xNXxNY array as argument to pli"); 
    }

  } else {
    GET_ARR (zap, zop, PyArray_DOUBLE, 2, PyObject *);
    z = (double *) A_DATA (zap);
  }

  iMax = A_DIM(zap, 1);
  jMax = A_DIM(zap, 0);

  if (1 == nargs) {
    x1 = (double) iMax;
    y1 = (double) jMax;
  }

  if (!z && !zc) return ERRSS ("pli needs at least one non-keyword argument");

  BUILD_KWT(kd, pliKeys, kwt);

  TRYS(CheckDefaultWindow())
  CheckDefaultPalette ();
  if ( !LegendAndHide("pli, ", z_name, (char *)0,(char *)0,(char *)0, kwt, pliKeys) )
     return ERRSS ( "Error in pli: LegendAndHide" );

  if (!zc) {
    /* need to generate colors array on stack now */
    double zmin, zmax, scale, offset;

    TRY (GrabByteScale(&kwt[2], &pliKeys[2], &scale, &offset, &zmin, &zmax,
       z, (int *)0, 0, iMax + 1, jMax + 1, 1), 
       (PyObject *) NULL);
    TRY (zc = PushColors(z, iMax*jMax, zmin, zmax, scale, offset), 
       (PyObject *) NULL);
    convertedZ= 1;
  }

  gistA.rgb = rgb;

  SETKW(kwt[0],  gistD.legend,    setkw_string,   pliKeys[0]);
  SETKW(kwt[1],  gistD.hidden,    setkw_boolean,  pliKeys[1]);

  curElement = -1;
  PyFPE_START_PROTECT("pli", return 0)
  curElement = GdCells (x0, y0, x1, y1, iMax, jMax, iMax, zc);
  PyFPE_END_PROTECT(dummy)
  removeFromArrayList ( (PyObject *) zap);
  if (convertedZ) free (zc);
  if (curElement < 0)  {
    return ERRSS ("Gist GdCells plotter failed");
  }

  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  plm */

static char plm__doc__[] =
"plm( y, x, boundary=0/1, inhibit=0/1/2 )\n"
"or plm( y, x, ireg, boundary=0/1, inhibit=0/1/2 )\n"
"or plm( boundary=0/1, inhibit=0/1/2 )\n"
"     Plot a mesh of Y versus X.  Y and X must be 2-D arrays with equal\n"
"     dimensions.  If present, IREG must be a 2-D region number array\n"
"     for the mesh, with the same dimensions as X and Y.  The values of\n"
"     IREG should be positive region numbers, and zero for zones which do\n"
"     not exist.  The first row and column of IREG never correspond to any\n"
"     zone, and should always be zero.  The default IREG is 1 everywhere\n"
"     else.  If present, the BOUNDARY keyword determines whether the\n"
"     entire mesh is to be plotted (boundary=0, the default), or just the\n"
"     boundary of the selected region (boundary=1).  If present, the\n"
"     INHIBIT keyword causes the (X(,j),Y(,j)) lines to not be plotted\n"
"     (inhibit=1), or the (X(i,),Y(i,)) lines to not be plotted (inhibit=2).\n"
"     By default (inhibit=0), mesh lines in both logical directions are\n"
"     plotted.\n"
"     The Y, X, and IREG arguments may all be omitted to default to the\n"
"     mesh set by the most recent plmesh call.\n"
"     The following keywords are legal (each has a separate help entry):\n"
"\n"
"   KEYWORDS: legend, hide\n"
"             type, width, color\n"
"             region\n"
"\n"
"   SEE ALSO: plg, plm, plc, plv, plf, pli, plt, pldj, plfp, plmesh\n"
"             limits, logxy, ylimits, fma, hcp\n";

#undef N_KEYWORDS
#define N_KEYWORDS 8
static char *plmKeys[N_KEYWORDS+1]= {
  "legend", "hide", "color", "type", "width", "region", "boundary",
  "inhibit", 0 };

static PyObject *plm (PyObject * self, PyObject * args, PyObject * kd)
{
  char *y_name= 0, *x_name= 0, *r_name= 0;
  GaQuadMesh mesh;
  PyObject *kwt[NELT(plmKeys) - 1];
  char *errstr = "plm takes 1-3 non-keyword arguments: (y, x, ireg).";

  SETJMP0;
  if (PyTuple_Size (args) > 0)
    TRY (set_pyMsh (args, errstr, 0), 
       (PyObject *) NULL);

  get_mesh(&mesh);

  BUILD_KWT(kd, plmKeys, kwt);

  /* set legend and hide in gistD */
  TRYS(CheckDefaultWindow())
  if ( !LegendAndHide("plm, ", y_name, x_name, r_name, (char *)0, kwt, plmKeys) )
     return ERRSS ( "Error in plm: LegendAndHide" );

  /* set properties, starting from defaults for meshes */
  GhGetMesh();
  gistD.region = 0;
  gistD.boundary = 0;
  gistD.inhibit = 0;

  SETKW(kwt[0],  gistD.legend,    setkw_string,   plmKeys[0]);
  SETKW(kwt[1],  gistD.hidden,    setkw_boolean,  plmKeys[1]);
  SETKW(kwt[2],  gistA.l.color,   setkw_color,    plmKeys[2]);
  SETKW(kwt[3],  gistA.l.type,    setkw_linetype, plmKeys[3]);
  SETKW(kwt[4],  gistA.l.width,   setkw_double,   plmKeys[4]);
  SETKW(kwt[5],  gistD.region,    setkw_integer,  plmKeys[5]);
  SETKW(kwt[6],  gistD.boundary,  setkw_boolean,  plmKeys[6]);
  SETKW(kwt[7],  gistD.inhibit,   setkw_integer,  plmKeys[7]);

  if (!pyMsh.y) {
    return ERRSS ("no current mesh - use plmesh(y, x) to initialize");
  }

  TRYS(CheckDefaultWindow())
  curElement = -1;
  PyFPE_START_PROTECT("plm", return 0)
  curElement = GdMesh(NOCOPY_MESH, &mesh, gistD.region, gistD.boundary,
		     gistD.inhibit);
  PyFPE_END_PROTECT(dummy)

  if (curElement < 0)  {
    return ERRSS ("Gist GdMesh plotter failed");
  }

  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  plmesh */

static char plmesh__doc__[] =
"plmesh( y, x, ireg, triangle=tri_array )\n"
"or plmesh()\n"
"     Set the default mesh for subsequent plm, plc, plv, and plf calls.\n"
"     In the second form, deletes the default mesh (until you do this,\n"
"     or switch to a new default mesh, the default mesh arrays persist and\n"
"     take up space in memory).  The Y, X, and IREG arrays should all be\n"
"     the same shape; Y and X will be converted to double, and IREG will\n"
"     be converted to int.  If IREG is omitted, it defaults to IREG(1,)=\n"
"     IREG(,1)= 0, IREG(2:,2:)=1; that is, region number 1 is the whole\n"
"     mesh.  The triangulation array TRI_ARRAY is used by plc; the\n"
"     correspondence between TRI_ARRAY indices and zone indices is the\n"
"     same as for IREG, and its default value is all zero.\n"
"     The IREG or TRI_ARRAY arguments may be supplied without Y and X\n"
"     to change the region numbering or triangulation for a given set of\n"
"     mesh coordinates.  However, a default Y and X must already have been\n"
"     defined if you do this.\n"
"     If Y is supplied, X must be supplied, and vice-versa.\n"
"\n"
"   SEE ALSO: plm, plc, plv, plf, plfp\n";

#undef N_KEYWORDS
#define N_KEYWORDS 1
static char *meshKeys[N_KEYWORDS+1]= { "triangle", 0 };

static PyObject *plmesh (PyObject * self, PyObject * args, PyObject * kd)
{
  PyObject * kwt[NELT(meshKeys) - 1];
  char *errstr = "plmesh takes 0-3 non-keyword arguments: (y, x, ireg).";

  BUILD_KWT(kd, meshKeys, kwt);
  TRY (set_pyMsh (args, errstr, kwt[0]), 
     (PyObject *) NULL);

  Py_INCREF (Py_None);
  return Py_None;
}

/*  -------------------------------------------------------------------- */

static char plq__doc__[] =
"plq()\n"
"or plq( n_element )\n"
"or plq( n_element, n_contour )\n"
"or legend_list = plq() **** RETURN VALUE NOT YET IMPLEMENTED ****\n"
"or properties = plq(n_element, n_contour)\n"
"     Called as a subroutine, prints the list of legends for the current\n"
"     coordinate system (with an `(H)' to mark hidden elements), or prints\n"
"     a list of current properties of element N_ELEMENT (such as line type,\n"
"     width, font, etc.), or of contour number N_CONTOUR of element number\n"
"     N_ELEMENT (which must be contours generated using the plc command).\n"
"     Elements and contours are both numbered starting with one; hidden\n"
"     elements or contours are included in this numbering.\n"
"\n"
"     The plq function always operates on the current coordinate system\n"
"     in the current graphics window; use window and plsys to change these.\n"
"\n"
"   SEE ALSO: window, plsys, pledit, pldefault, plg\n";

static PyObject *plq (PyObject * self, PyObject * args)
{
  int type, n_element = 0, n_contour = 0;

  switch (PyTuple_Size (args)) {
  case 2: /* (n_element, n_contour) given */
    TRY (PyArg_ParseTuple (args, "ii", &n_element, &n_contour), 
       (PyObject *) NULL);
    break;
  case 1: /* (n_element) given */
    TRY (PyArg_ParseTuple (args, "i", &n_element), 
       (PyObject *) NULL);
    break;
  case 0: /* () given */
    break;
  default:
    return ERRSS ("plq function takes no more than two arguments");
  }

  /* Pygist uses 1-origin element numbering, Gist uses 0-origin */
  n_element--;
  n_contour--;

  if (n_element >= 0) {
    /* retrieve specified element */
    type = GdSetElement (n_element);
    if (n_contour >= 0) {
      if (type != E_CONTOURS)  {
	return ERRSS ("current graphical element is not contours in pledit");
      }
      type = GdSetContour (n_contour);
    }
    curElement = -6666;		/* differs from -1 to allow pledit after plq */
    curIX = n_element;		/* need these, too */
    curIXc = n_contour;
    if (type == E_LINES) type = 1;
    else if (type == E_DISJOINT) type = 2;
    else if (type == E_TEXT) type = 3;
    else if (type == E_MESH) type = 4;
    else if (type == E_FILLED) type = 5;
    else if (type == E_VECTORS) type = 6;
    else if (type == E_CONTOURS) type = 7;
    else if (type == E_CELLS) type = 8;
    else if (type == E_POLYS) type = 9;
    else type = 0;

    if (1) {
      /* return printed summary of keyword values */
      char line[120];
      PrintInit (pyg_puts);

      if (type == 0) {
	sprintf (line, "<no such object>  element# %d", n_element + 1);
	PrintFunc (line);
	if (n_contour >= 0) {
	  sprintf (line, "  contour# %d", n_contour + 1);
	  PrintFunc (line);
	}
	ForceNewline ();

      } else if (type == 1) {
	sprintf (line, "plg  element# %d", n_element + 1);
	PrintFunc (line);
	if (n_contour >= 0) {
	  sprintf (line, "  contour# %d", n_contour + 1);
	  PrintFunc (line);
	  ForceNewline ();
	  sprintf (line, "  at level value %g", gistD.levels[n_contour]);
	  PrintFunc (line);
	}
	ForceNewline ();
	PrintHideLegend (line, type);
	PrintColor (line, gistA.l.color, 1);
	PrintTypeWidth (line, 3);
	PrintMarks (line, 3);
	sprintf (line, "rays= %d,", gistA.dl.rays);
	PrintFunc (line);
	ForceNewline ();
	sprintf (line,
		 "  arrowl= %.2f, arroww= %.2f, rspace= %.5f, rphase= %.5f,",
		 Safe_dbl (gistA.dl.arrowL), Safe_dbl (gistA.dl.arrowW),
		 Safe_dbl (gistA.dl.rSpace), Safe_dbl (gistA.dl.rPhase));
	PrintFunc (line);
	ForceNewline ();
	sprintf (line, "smooth= %d,  closed= %d",
		 gistA.dl.smooth, gistA.dl.closed);
	PrintFunc (line);
	ForceNewline ();

      } else if (type == 2) {
	sprintf (line, "pldj  element# %d", n_element + 1);
	PrintFunc (line);
	ForceNewline ();
	PrintHideLegend (line, type);
	PrintColor (line, gistA.l.color, 1);
	PrintTypeWidth (line, 2);

      } else if (type == 3) {
	sprintf (line, "plt  element# %d", n_element + 1);
	PrintFunc (line);
	ForceNewline ();
	PrintHideLegend (line, type);
	PrintColor (line, gistA.t.color, 3);
	sprintf (line, "text= %.80s", gistD.text);
	PrintFunc (line);
	ForceNewline ();

      } else if (type == 4) {
	sprintf (line, "plm  element# %d", n_element + 1);
	PrintFunc (line);
	ForceNewline ();
	PrintHideLegend (line, type);
	PrintColor (line, gistA.l.color, 1);
	PrintTypeWidth (line, 2);
	PrintRegion (line, 1);
	sprintf (line, "boundary= %d, inhibit= %d", gistD.boundary,
		 gistD.inhibit);
	PrintFunc (line);
	ForceNewline ();

      } else if (type == 5) {
	sprintf (line, "plf  element# %d", n_element + 1);
	PrintFunc (line);
	ForceNewline ();
	PrintHideLegend (line, type);
	sprintf (line, "edges= %d, e", gistA.e.type != L_NONE);
	PrintFunc (line);
	PrintColor (line, gistA.e.color, 1);
	sprintf (line, "ewidth= %.2f", Safe_dbl (gistA.e.width));
	PrintFunc (line);
	ForceNewline ();
	PrintRegion (line, 2);

      } else if (type == 6) {
	sprintf (line, "plv  element# %d", n_element + 1);
	PrintFunc (line);
	ForceNewline ();
	PrintHideLegend (line, type);
	PrintColor (line, gistA.l.color, 1);
	sprintf (line, "width= %.2f,", Safe_dbl (gistA.l.width));
	PrintFunc (line);
	ForceNewline ();
	sprintf (line, "hollow= %d,  aspect= %.4f,", gistA.vect.hollow,
		 Safe_dbl (gistA.vect.aspect));
	PrintFunc (line);
	ForceNewline ();
	PrintRegion (line, 3);
	sprintf (line, "scale= %g", gistD.scale);
	PrintFunc (line);
	ForceNewline ();

      } else if (type == 7) {
	int i;
	sprintf (line, "plc  element# %d", n_element + 1);
	PrintFunc (line);
	ForceNewline ();
	PrintHideLegend (line, type);
	PrintColor (line, gistA.l.color, 1);
	PrintTypeWidth (line, 3);
	PrintMarks (line, 3);
	sprintf (line, "smooth= %d,", gistA.dl.smooth);
	PrintFunc (line);
	ForceNewline ();
	PrintRegion (line, 2);
	sprintf (line, "%d contour levels, levs=", gistD.nLevels);
	PrintFunc (line);
	ForceNewline ();
	PrintFunc ("[");
	if (gistD.nLevels > 0) {
	  for (i = 0;; i++) {
	    sprintf (line, "%g", gistD.levels[i]);
	    PrintFunc (line);
	    if (i == gistD.nLevels - 1)
	      break;
	    PrintFunc (",");
	    PermitNewline (0);
	  }
	}
	PrintFunc ("]");
	ForceNewline ();

      } else if (type == 8) {
	sprintf (line, "pli  element# %d", n_element + 1);
	PrintFunc (line);
	ForceNewline ();
	PrintHideLegend (line, type);
	sprintf (line, "x0= %g,  y0= %g,  x1= %g,  y1= %g",
		 gistD.px, gistD.py, gistD.qx, gistD.qy);
	PrintFunc (line);
	ForceNewline ();

      } else if (type == 9) {
	sprintf (line, "plfp  element# %d", n_element + 1);
	PrintFunc (line);
	ForceNewline ();
	PrintHideLegend (line, type);
	sprintf (line, "%d polygons", gistD.n);
	PrintFunc (line);
	ForceNewline ();
      }
    } else {
      /* Future implementation of array return */
    }

  } else if (n_contour >= 0) {
       return ERRSS ("contour number cannot be specified without element number");

  } else {
    char line[16];
    int i, offset;
    /* print list of legends... */
    /* if (CalledAsSubroutine()) FUTURE */
    if (1) {
      /* ...to terminal */
      PrintInit (pyg_puts);
    } else {
     /* Future Implementation */
    }

    curElement = -1;
    for (i = 0; (type = GdSetElement (i)) != E_NONE; i++) {
      sprintf (line, "%s%2d: ", gistD.hidden ? "(H)" : "", i + 1);
      PrintFunc (line);
      offset = 0;
      if ((type == E_LINES || type == E_CONTOURS) && gistD.legend &&
	  gistD.legend[0] == '\001') {
	char marker[2];
	marker[1] = '\0';
	if (gistA.m.type >= ' ' && gistA.m.type < '\177')
	  marker[0] = (char) gistA.m.type;
	else if (gistA.m.type >= 1 && gistA.m.type <= 5)
	  marker[0] = specialMarkers[gistA.m.type - 1];
	else
	  marker[0] = '?';
	PrintFunc (marker);
	offset = 1;
      }
      if (gistD.legend)
	PrintFunc (gistD.legend + offset);
      ForceNewline ();
    }
  }
  Py_INCREF (Py_None);
  return Py_None;
}

/*  -------------------------------------------------------------------- */

static char plremove__doc__[] =
"plremove( n_element )\n"
"     Removes the element number N_ELEMENT from the display.\n"
"     If N_ELEMENT is omitted, the default is the most recently added\n"
"     element, or the element specified in the most recent plq query\n"
"     command.\n";

static PyObject *plremove(PyObject * self, PyObject * args)
{
  int type = 0, n_element = 0,result;

  switch (PyTuple_Size (args)) {
  case 1: /* (n_element) given */
    TRY (PyArg_ParseTuple (args, "i", &n_element), (PyObject *) NULL);
    break;
  case 0: /* () given */
    break;
  default:
    return ERRSS ("plremove function takes no more than one argument");
  }

  /* Pygist uses 1-origin element numbering, Gist uses 0-origin */
  n_element--;

  if (n_element < 0) {
    if (curElement >= 0) {
      n_element = GdFindIndex (curElement);
      if (n_element < 0) {
	curElement = -1;
	return ERRSS ("lost current graphical element for plremove (BUG?)");
      }
    } else if (curElement == -6666) {
      n_element = curIX;
    } else {
      return ERRSS ("no current graphical element for plremove");
    }
  }
  if (n_element >= 0) {
    /* retrieve specified element */
    type = GdSetElement (n_element);
  }
  curElement = -1;

  PyFPE_START_PROTECT("plremove", return 0)
  result = GdRemove();
  PyFPE_END_PROTECT(dummy)

  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

/*  -------------------------------------------------------------------- */

static char plsys__doc__[] =
"plsys( n )\n"
"     Set the current coordinate system to number N in the current\n"
"     graphics window.  If N equals 0, subsequent elements will be\n"
"     plotted in absolute NDC coordinates outside of any coordinate\n"
"     system.  The default style sheet `work.gs' defines only a single\n"
"     coordinate system, so the only other choice is N equal 1.  You\n"
"     can make up your own style sheet (using a text editor) which\n"
"     defines multiple coordinate systems.  You need to do this if\n"
"     you want to display four plots side by side on a single page,\n"
"     for example.  The standard style sheets `work2.gs' and `boxed2.gs'\n"
"     define two overlayed coordinate systems with the first labeled\n"
"     to the right of the plot and the second labeled to the left of\n"
"     the plot.  When using overlayed coordinate systems, it is your\n"
"     responsibility to ensure that the x-axis limits in the two\n"
"     systems are identical.\n"
"\n"
"   SEE ALSO: window, limits, plg\n";

static PyObject *plsys (PyObject * self, PyObject * args)
{
  int n = -9999, n0;
  char *errstr = "Error: plsys takes zero or one integer argument.";

  SETJMP0;
  if (!PyArg_ParseTuple (args, "|i", &n)) {
    return ERRSS ( errstr );
  }

  TRYS(CheckDefaultWindow())
  n0 = GdGetSystem();

  if (n != -9999){
    if (GdSetSystem (n) != E_SYSTEM && n != 0) {
      return ERRSS (
       "No such coordinate system exists in current graphics window.");
    }
  }
  return Py_BuildValue ("i",n0);
}

/*  -------------------------------------------------------------------- */
/*  plt */

static char plt__doc__[] =
"plt( text, x, y, tosys=0/1 )\n"
"     Plot TEXT (a string) at the point (X,Y).  The exact relationship\n"
"     between the point (X,Y) and the TEXT is determined by the\n"
"     justify keyword.  TEXT may contain newline (`\\n') characters\n"
"     to output multiple lines of text with a single call.  The\n"
"     coordinates (X,Y) are NDC coordinates (outside of any coordinate\n"
"     system) unless the tosys keyword is present and non-zero, in\n"
"     which case the TEXT will be placed in the current coordinate\n"
"     system.  However, the character height is NEVER affected by the\n"
"     scale of the coordinate system to which the text belongs.\n"
"     Note that the pledit command takes dx and/or dy keywords to\n"
"     adjust the position of existing text elements.\n"
"     The following keywords are legal (each has a separate help entry):\n"
"\n"
"   KEYWORDS: legend, hide\n"
"             color, font, height, opaque, path, justify\n"
"\n"
"   SEE ALSO: plg, plm, plc, plv, plf, pli, plt, pldj, plfp, pledit\n"
"             limits, ylimits, fma, hcp, pltitle\n";

#undef N_KEYWORDS
#define N_KEYWORDS 9
static char *pltKeys[N_KEYWORDS+1]= {
  "legend", "hide",
  "color", "font", "height", "orient", "justify", "opaque", "tosys", 0 };

static PyObject *plt (PyObject * self, PyObject * args, PyObject * kd)
{
  char *text = 0;
  double x = 0.0, y = 0.0;
  int toSys = 0;
  char* align = 0;
  PyObject *kwt[NELT (pltKeys) - 1];

  SETJMP0;			/* See Xerror_longjmp() */

  if (!PyArg_ParseTuple (args, "sdd", &text, &x, &y)) {
    return ERRSS ("plt requires exactly three non-keyword arguments");
  }

  BUILD_KWT(kd, pltKeys, kwt);

  /* set legend and hide in gistD */
  TRYS(CheckDefaultWindow())
  if ( !LegendAndHide((char *)0, (char *)0, (char *)0,
                (char *)0, (char *)0, kwt, pltKeys) )
     return ERRSS ( "Error in plt: LegendAndHide" );

  /* set properties, starting from defaults for vectors */
  GhGetText ();
  SETKW(kwt[0],  gistD.legend,    setkw_string,   pltKeys[0]);
  SETKW(kwt[1],  gistD.hidden,    setkw_boolean,  pltKeys[1]);
  SETKW(kwt[2],  gistA.t.color,   setkw_color,    pltKeys[2]);
  SETKW(kwt[3],  gistA.t.font,    setkw_fonttype, pltKeys[3]);
  SETKW(kwt[4],  gistA.t.height,  setkw_double,   pltKeys[4]);
  if(kwt[4])  gistA.t.height *= ONE_POINT;
  SETKW(kwt[5],  gistA.t.orient,  setkw_integer,  pltKeys[5]);
  if (!gistA.t.orient) {
    gistA.t.orient = TX_RIGHT;
  } else {
    if (gistA.t.orient == 1) gistA.t.orient = TX_UP;
    else if (gistA.t.orient == 2) gistA.t.orient = TX_LEFT;
    else if (gistA.t.orient == 3) gistA.t.orient = TX_DOWN;
    else {
      gistA.t.orient= TX_RIGHT;
      return ERRSS ("orient= keyword must be 0, 1, 2, or 3");
    }
  }
  SETKW(kwt[6],    align,        setkw_string,   pltKeys[6]);
  if (align)
  { if (strlen(align)!=2) 
      return ERRSS ("justify= keyword should consist of two characters");
    switch (align[0])
    { case 'N': gistA.t.alignH = TH_NORMAL; break;
      case 'L': gistA.t.alignH = TH_LEFT; break;
      case 'C': gistA.t.alignH = TH_CENTER; break;
      case 'R': gistA.t.alignH = TH_RIGHT; break;
      default:
        return ERRSS ("unknown first character in the justify= keyword");
    }
    switch (align[1])
    { case 'N': gistA.t.alignV = TV_NORMAL; break;
      case 'T': gistA.t.alignV = TV_TOP; break;
      case 'C': gistA.t.alignV = TV_CAP; break;
      case 'H': gistA.t.alignV = TV_HALF; break;
      case 'A': gistA.t.alignV = TV_BASE; break;
      case 'B': gistA.t.alignV = TV_BOTTOM; break;
      default:
        return ERRSS ("unknown second character in the justify= keyword");
    }
  }
  SETKW(kwt[7],  gistA.t.opaque,  setkw_boolean,  pltKeys[7]);
  SETKW(kwt[8],  toSys,           setkw_boolean,  pltKeys[8]);


  if (!text)
    text = "";
  curElement = -1;
  PyFPE_START_PROTECT("plt", return 0)
  curElement = GdText (x, y, text, toSys);
  PyFPE_END_PROTECT(dummy)
  if (curElement < 0)  {
    return ERRSS ("Gist GdText plotter failed");
  }

  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

/*  -------------------------------------------------------------------- */
/*  plv */

static char plv__doc__[] =
"plv( vy, vx, y, x, scale=dt )\n"
"or plv( vy, vx, y, x, ireg, scale=dt )\n"
"or plv( vy, vx, scale=dt )\n"
"     Plot a vector field (VX,VY) on the mesh (X,Y).  Y, X, and IREG are\n"
"     as for plm.  The VY and VX arrays must have the same shape as Y and X.\n"
"     The Y, X, and IREG arguments may all be omitted to default to the\n"
"     mesh set by the most recent plmesh call.\n"
"     The SCALE keyword is the conversion factor from the units of\n"
"     (VX,VY) to the units of (X,Y) -- a time interval if (VX,VY) is a velocity\n"
"     and (X,Y) is a position -- which determines the length of the\n"
"     vector `darts' plotted at the (X,Y) points.  If omitted, SCALE is\n"
"     chosen so that the longest ray arrows have a length comparable\n"
"     to a `typical' zone size.\n"
"     You can use the scalem keyword in pledit to make adjustments to the\n"
"     SCALE factor computed by default.\n"
"     The following keywords are legal (each has a separate help entry):\n"
"\n"
"   KEYWORDS: legend, hide\n"
"             type, width, color, smooth\n"
"             marks, marker, mspace, mphase\n"
"             triangle, region\n"
"\n"
"   SEE ALSO: plg, plm, plc, plv, plf, pli, plt, pldj, plfp, plmesh, pledit,\n"
"             limits, logxy, ylimits, fma, hcp\n";

#undef N_KEYWORDS
#define N_KEYWORDS 8
static char *plvKeys[N_KEYWORDS+1]= {
  "legend", "hide", "region",
  "color", "hollow", "width", "aspect", "scale", 0 };

static PyObject *plv (PyObject * self, PyObject * args, PyObject * kd)
{
  PyArrayObject *uap, *vap;
  PyObject *uop, *vop;
  char *v_name= 0, *u_name= 0, *y_name= 0, *x_name= 0;
  long iMax= 0, jMax= 0;
  double *u= 0, *v= 0, scale;
  GaQuadMesh mesh;

  PyObject * kwt[NELT(plvKeys) - 1];
  char *errstr =
    "plv requires 2D arguments (v, u [ , y, x, ireg, scale = dt ] )";

  SETJMP0;			/* See Xerror_longjmp() */

  BUILD_KWT(kd, plvKeys, kwt);

  TRYS(CheckDefaultWindow())
  if ( !LegendAndHide("plv, ", v_name, u_name, y_name, x_name, kwt, plvKeys) )
     return ERRSS ( "Error in plv: LegendAndHide" );

  if (PyTuple_Size (args) < 2)  {
    return ERRSS ("plv requires at least two arguments");
  }
  TRY (setvu_mesh (args, &vop, &uop, errstr), 
     (PyObject *) NULL);
  if (!pyMsh.y)  {
    return ERRSS ("No current mesh - set (y, x) first");
  }
  GET_ARR (vap, vop, PyArray_DOUBLE, 2, PyObject *);
  GET_ARR (uap, uop, PyArray_DOUBLE, 2, PyObject *);
  jMax = (A_DIM(vap, 0) == A_DIM(uap, 0)) ? A_DIM(vap, 0) : 0;
  iMax = (A_DIM(vap, 1) == A_DIM(uap, 1)) ? A_DIM(vap, 1) : 0;
  if (A_DIM (pyMsh.y, 0) != jMax || A_DIM (pyMsh.y, 1) != iMax ) {
    clearArrayList ();
    return ERRSS ("(v, u) arrays must match (y, x) mesh arrays in shape");
  }
  v = (double *) A_DATA (vap);
  u = (double *) A_DATA (uap);
  get_mesh (&mesh);
  if (mesh.iMax!=iMax || mesh.jMax!=jMax)
    return ERRSS ("v and u arrays must have same dimensions as mesh in plv");

  /* set legend and hide in gistD */
  TRYS(CheckDefaultWindow())
  if ( !LegendAndHide("plv, ", v_name, u_name, y_name, x_name, kwt, plvKeys) )
     return ERRSS ( "Error in plv: LegendAndHide" );

  /* set properties, starting from defaults for vectors */
  GhGetVectors();
  gistD.region = 0;

  SETKW(kwt[0],   gistD.legend,      setkw_string,  plvKeys[0]);
  SETKW(kwt[1],   gistD.hidden,      setkw_boolean, plvKeys[1]);
  SETKW(kwt[2],   gistD.region,      setkw_integer, plvKeys[2]);
  SETKW(kwt[3],   gistA.l.color,     setkw_color,   plvKeys[3]);
  if(kwt[3])      gistA.f.color = gistA.l.color;
  SETKW(kwt[4],   gistA.vect.hollow, setkw_boolean, plvKeys[4]);

  SETKW(kwt[5],   gistA.l.width,     setkw_double,  plvKeys[5]);
  SETKW(kwt[6],   gistA.vect.aspect, setkw_double,  plvKeys[6]);
  if(kwt[7]) { /* scale= keyword */
    SETKW(kwt[7], scale,             setkw_double,  plvKeys[7]);
  } else {
    /* set vector scale factor to make maximum vector length a
       "typical" zone dimension */
    double umin, umax, vmin, vmax, xmin, xmax, ymin, ymax;

    PyFPE_START_PROTECT("plv", return 0)
    GetPCrange(&xmin, &xmax, mesh.x, mesh.reg, gistD.region, iMax, jMax);
    GetPCrange(&ymin, &ymax, mesh.y, mesh.reg, gistD.region, iMax, jMax);
    GetPCrange(&umin, &umax, u, mesh.reg, gistD.region, iMax, jMax);
    GetPCrange(&vmin, &vmax, v, mesh.reg, gistD.region, iMax, jMax);

    umax -= umin;
    vmax -= vmin;
    if (vmax > umax) umax = vmax;
    xmax = (xmax - xmin) + (ymax - ymin);
    xmax /= (iMax + jMax);

    if (umax > 0.0) scale = xmax / umax;
    else scale = 1.0;
    PyFPE_END_PROTECT(dummy)
  }

  curElement = -1;
  PyFPE_START_PROTECT("plv", return 0)
  curElement = GdVectors(NOCOPY_MESH, &mesh, gistD.region, u, v, scale);
  PyFPE_END_PROTECT(dummy)
  clearArrayList ();
  if (curElement < 0)  {
    return ERRSS ("Gist GdVectors plotter failed");
  }
  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

static long Safe_strlen(const char *s)
{
  if (s) return strlen(s);
  else return 0;
}

static void AllocTmpLegend(long len)
{
  if (tmpLegend) FreeTmpLegend();
  tmpLegend = p_malloc(len+1);
  tmpLegend[0] = '\0';
}

static void FreeTmpLegend(void)
{
  if (tmpLegend) {
    char *legend= tmpLegend;
    p_free(legend);
    tmpLegend= 0;
  }
}

static long escape_count(char *arg)
{
  long n= 0;
  if (arg) while (*arg) {
    if (*arg=='!' || *arg=='_' || *arg=='^') n++;
    arg++;
  }
  return n;
}

static void escape_cat(char *leg, char *arg)
{
  while (*arg) {
    if (*arg=='!' || *arg=='_' || *arg=='^') *(leg++)= '!';
    *(leg++)= *(arg++);
  }
  *leg= '\0';
}

static int LegendAndHide(char *func, char *arg1, char *arg2, char *arg3,
                          char *arg4, PyObject *kwt[], char *keys[])
{
  /* check for hide= keyword */

  gistD.hidden= 0;
  SETKW(kwt[1], gistD.hidden, setkw_boolean, keys[1]);

  if (tmpLegend) FreeTmpLegend();

  /* check for legend= keyword -- put legend into tmpLegend */
  /* legend=[] is same as legend=string() */

  SETKW(kwt[0], tmpLegend, setkw_string, keys[0]);
    
  else if (func) {
    /* construct default legend from up to 4 quined arguments */
    long len0= Safe_strlen(func);
    long len1= Safe_strlen(arg1)+escape_count(arg1);
    long len2= Safe_strlen(arg2)+escape_count(arg2);
    long len3= Safe_strlen(arg3)+escape_count(arg3);
    long len4= Safe_strlen(arg4)+escape_count(arg4);
    AllocTmpLegend(len0+len1+len2+len3+len4+6);
    if (func) strcat(tmpLegend, func);
    if (arg1) {
      escape_cat(tmpLegend+len0, arg1);
      len0+= len1;
      if (arg2) {
        strcat(tmpLegend+len0, ", ");
        escape_cat(tmpLegend+len0+2, arg2);
        len0+= 2+len2;
        if (arg3) {
          strcat(tmpLegend+len0, ", ");
          escape_cat(tmpLegend+len0+2, arg3);
          len0+= 2+len3;
          if (arg4) {
            strcat(tmpLegend+len0, ", ");
            escape_cat(tmpLegend+len0+2, arg4);
            len0+= 2+len4;
          }
        }
      }
    }
  }

  /* Put tmpLegend into gistD.legend -- it will be copied out when the
     element is created.  Only danger is pledit, since GdEdit just
     copies the pointer, not the string -- handle this case specially.  */
  gistD.legend= tmpLegend;
  return 1;
}

#if 0
static void print_array_stats(PyArrayObject *op)
{
  int i,ne;
  double *dp;
  TO_STDERR("Data pointer: %p Base pointer: %p\n", op->data, op->base);
  TO_STDERR("Num dims: %d Flags: %d\n", op->nd, op->flags);
  TO_STDERR("Dims & strides:");
  for(i=0; i<op->nd; i++)
    TO_STDERR(" i: %d dim: %d stride: %d",i,op->dimensions[i], op->strides[i]);
  TO_STDERR("\n");
  ne = op->dimensions[0];
  for(i=1; i<op->nd; i++)
    ne *= op->dimensions[i];
  TO_STDERR("Data: (ne = %d)", ne);
  for(i=0,dp = (double *)op->data; i < ne; i++, dp++)
    TO_STDERR(" %.1g", *dp);
  TO_STDERR("\n\n");
  flush_stderr();
}
#endif

/*  -------------------------------------------------------------------- */

static char redraw__doc__[] =
"redraw()\n"
"     Redraw the X window associated with the current graphics window.\n"
"\n"
"   SEE ALSO: window, fma, hcp, plg\n";

static PyObject *redraw (PyObject * self, PyObject * args)
{
  SETJMP0;
  PyFPE_START_PROTECT("redraw", return 0)
  TRYS(CheckDefaultWindow())
  GhRedraw ();
  PyFPE_END_PROTECT(dummy)
  Py_INCREF (Py_None);
  return Py_None;
}

/* Create a default region array for the current mesh. */
static int set_def_reg (int nr, int nc)
{
  int i, ne, newlen, *p1;
  PyArrayObject *ra1;

  ne = nr * nc;
  newlen = ne + nc + 1;
  TRY (ra1 = (PyArrayObject *) PyArray_FromDims (1, &newlen, PyArray_LONG), 0);
  p1 = (int *) A_DATA (ra1);

  /* Fill in the data part of the new region array. */
  for (i = 0;      i <= nc; i++)    p1[i]      = 0;
  for (i = nc+1;   i <  ne; i++)    p1[i]      = 1;
  for (i = 0;      i <  nc; i++)    p1[ne + i] = 0;
  for (i = 2*nc;   i <  ne; i+=nc)  p1[i]      = 0;

  Py_XDECREF (pyMsh.reg);
  pyMsh.reg = ra1;
  return 1;
}

/* Set xmin, xmax, ymin, or ymax.  Return 2 if limit was unchanged
 * (e.g., user passed 'u' argument), return 1 if limit was reset,
 * return 0 if error. */
static int set_limit (PyObject * ob, double *lim, int *flags, int fval)
{
  if (PyString_Check (ob)) {
    char *s = PyString_AsString (ob);
    if (*s == 'e' || *s == 'E') {
      *flags |= fval;
    } else if (*s == 'u' || *s == 'U') {
      return 2; /* No change in this limit. */
    } else {
      return 0; /* Error */
    }
  } else if (PyFloat_Check (ob)) {
    *lim = PyFloat_AsDouble (ob);
    *flags &= ~fval;
  } else if (PyInt_Check (ob)) {
    *lim = (double) PyInt_AsLong (ob);
    *flags &= ~fval;
  } else {
    return 0; /* Error */
  }
  return 1; /* Limit was changed, either to specific value or extreme. */
}

/* Called from plmesh and plm, and indirectly from plc, plf, and plv,
 * which all allow the arguments (y, x, ireg).  Plc and plmesh also
 * allow the "triangle=" keyword, so handle that also.
 */
static int set_pyMsh(PyObject *args, char *errstr, PyObject *tri)
{
  PyObject *op1, *op2, *op3;

  if (!PyArg_ParseTuple (args, "|OOO", &op1, &op2, &op3))  {
    return (int) ERRSS (errstr);
  }

  switch (PyTuple_Size(args)) {
  case 3: /* Arguments were (y, x, ireg). */
    TRY (set_yx (op1, op2), 0);
    TRY (set_reg (op3), 0);
    break;

  case 2: /* Arguments were (y, x). */
    TRY(set_yx(op1, op2), 0);
    TRY(set_def_reg(A_DIM(op1, 0), A_DIM(op1, 1)), 0); /* Default region array. */
    break;

  case 1: /* Arguments were (ireg). */
    TRY(set_reg(op1), 0);
    break;

  case 0: /* Clear the mesh, unless "triangle" keyword was given. */
    if(tri) TRY (set_tri(tri), 0);
    else clear_pyMsh();
    break;

  default: /* This **REALLY** shouldn't happen. */
    return (int) ERRSS (errstr);
  }
  return 1;
}

/* Create a non-default region (mesh) array from the passed-in object. */
static int set_reg (PyObject *op)
{
  int i, ok, nr, nc, ne, newlen, *p2, *p1;
  PyArrayObject *ra2, *ra1;

  ok = (isARRAY(op) && (A_NDIM(op) == 2) && (A_TYPE(op) == PyArray_LONG));
  if (!ok) {
     return (int) ERRSS ("(ireg) must be a 2-D int array");
  }

  if (!pyMsh.y)  {
    return (int) ERRSS ("No current mesh - ireg not set - set (y, x) first");
  }
  nr = A_DIM (op, 0);
  nc = A_DIM (op, 1);
  if (A_DIM (pyMsh.y, 0) != nr || A_DIM (pyMsh.y, 1) != nc)  {
    return (int) ERRSS ("(ireg) must match (y, x) in shape");
  }

  ne = nr * nc;
  newlen = ne + nc + 1;
  NEW_ARR (ra1, 1, &newlen, PyArray_LONG, int);
  p1 = (int *) A_DATA (ra1);
  GET_ARR (ra2, op, PyArray_LONG, 2, int);
  p2 = (int *) A_DATA (ra2);

  /* Fill in the data part of the new region array. */
  for (i = 0;      i <= nc; i++)    p1[i]      = 0;
  for (i = nc+1;   i <  ne; i++)    p1[i]      = p2[i];
  for (i = 0;      i <  nc; i++)    p1[ne + i] = 0;
  for (i = 2*nc;   i <  ne; i+=nc)  p1[i]      = 0;

  Py_DECREF (ra2);
  Py_XDECREF (pyMsh.reg);
  array_list_length = 0;
  pyMsh.reg = ra1;
  takeOffArrayList ( (PyObject *) ra1);
  takeOffArrayList ( (PyObject *) ra2);
  return 1;
}

/*  -------------------------------------------------------------------- */

static char set_slice2_position__doc__[] =
"None.";

static PyObject *set_slice2_precision (PyObject * self, PyObject * args)
{
 if ( ! PyArg_ParseTuple (args, "d", &_slice2_precision))  {
    return ERRSS ("set_slice2_precision: bad value.");
 }
 Py_INCREF (Py_None);
 return Py_None;
}

/* Create a triangulation (mesh) array. */
static int set_tri (PyObject *top)
{
  int nr, nc;

  if (!pyMsh.y)  {
    return (int) ERRSS ("No current mesh - triangle not set - set (y, x) first");
  }
  nr = A_DIM (pyMsh.y, 0);
  nc = A_DIM (pyMsh.y, 1);

  Py_XDECREF (pyMsh.triangle);
  GET_ARR (pyMsh.triangle, top, PyArray_SHORT, 2, int);

  if (A_DIM (pyMsh.triangle, 0) != nr || A_DIM (pyMsh.triangle, 1) != nc) {
    removeFromArrayList ((PyObject *)pyMsh.triangle);
    return (int) ERRSS ("triangle array must match shape of (y, x).");
  }
  array_list_length = 0;
  return 1;
}

static int set_yx (PyObject *yop, PyObject *xop)
{
  int nr, nc;

  clear_pyMsh();
  GET_ARR (pyMsh.y, yop, PyArray_DOUBLE, 2, int);
  nr = A_DIM (pyMsh.y, 0);
  nc = A_DIM (pyMsh.y, 1);
  if (nr < 2 || nc < 2) {
    clearArrayList ();
    return (int) ERRSS ("(y, x) arrays must be at least 2X2");
  }
  GET_ARR (pyMsh.x, xop, PyArray_DOUBLE, 2, int);
  if (A_DIM (pyMsh.x, 0) != nr || A_DIM (pyMsh.x, 1) != nc) {
    clearArrayList ();
    return (int) ERRSS ("x array must match shape of y");
  }

#if 0
print_array_stats(xop);
print_array_stats(yop);
#endif

  array_list_length = 0;
  return 1;
}

static int setkw_boolean (PyObject * v, int *t, char *kw)
{
  char buf[256];
  char *format = "%s keyword requires argument of 0 (False) or 1 (True)";

  if (PyInt_Check (v)) {
    *t = (PyInt_AsLong (v) != 0);
    return 1;
  }
  (void)sprintf(buf, format, kw);
  return (int) ERRSS (buf);
}

/* Set value for "color=" keyword.  Value passed can be either a string,
 * an integer, or a triple.  All these setkw_*() functions return 0 on error,
 * non-zero otherwise. */

static int setkw_color (PyObject * v, unsigned long *t, char *kw) 
{
  unsigned long color = P_FG;
  unsigned long colors[3] = { 0, 0, 0 };

  if (PyString_Check (v)) {
    char *s = PyString_AsString (v);
    if (strcmp (s, "bg") == 0)
      color = P_BG;
    else if (strcmp (s, "fg") == 0)
      color = P_FG;
    else if (strcmp (s, "black") == 0)
      color = P_BLACK;
    else if (strcmp (s, "white") == 0)
      color = P_WHITE;
    else if (strcmp (s, "red") == 0)
      color = P_RED;
    else if (strcmp (s, "green") == 0)
      color = P_GREEN;
    else if (strcmp (s, "blue") == 0)
      color = P_BLUE;
    else if (strcmp (s, "cyan") == 0)
      color = P_CYAN;
    else if (strcmp (s, "magenta") == 0)
      color = P_MAGENTA;
    else if (strcmp (s, "yellow") == 0)
      color = P_YELLOW;
    else {
      char errstr[256];
      sprintf (errstr, "Unrecognized color keyword: %s: "
	       "Use fg, bg, or 8 primaries only", s);
      return (int) ERRSS (errstr);
    }
  } else if (PyInt_Check (v)) {
    int color1 = PyInt_AsLong (v);
    if ( color1 < 0 )  {
       color = (color1 & 0xff);  /* take right 8 bits */
    }
    else  {
       color = (unsigned long) color1;
    }
  } 

  /* Handle case of 3 element array for trucolor */

  else if ( v && PyTuple_Check (v) ) {  
     if (!unpack_color_tuple ( v, colors)) {
        return (int) NULL;
     }
     color = P_RGB(colors[0],colors[1],colors[2]);
  }
  else  {
    return (int) ERRSS ("Color keyword value must be string, integer, or a triple (r,g,b)");
  }
  *t = color;
  return 1;
}

static int setkw_double (PyObject * v, double *t, char *kw)
{
  char buf[256];
  char *format = "%s keyword requires floating point argument";

  if (PyFloat_Check (v)) {
    *t = PyFloat_AsDouble (v);
    return 1;
  } else if (PyInt_Check (v)) {
    *t = (double) PyInt_AsLong (v);
    return 1;
  }
  (void)sprintf(buf, format, kw);
  return (int) ERRSS (buf);
}

static int setkw_fonttype (PyObject * v, int *t, char *kw)
{
  char buf[256];
  char *format = "%s keyword requires string or integer argument";
  char *s;
  int font, face;

#undef SETFONT
#define SETFONT(s, n, font, face, type) \
if (GetTypeface (&s[n], &face)) font = type|face; else return 0

  if (PyString_Check (v)) {
    s = PyString_AsString (v);

    if (strncmp (s, "courier", 7) == 0) {
      SETFONT (s, 7, font, face, T_COURIER);
    } else if (strncmp (s, "times", 5) == 0) {
      SETFONT (s, 5, font, face, T_TIMES);
    } else if (strncmp (s, "helvetica", 9) == 0) {
      SETFONT (s, 9, font, face, T_HELVETICA);
    } else if (strncmp (s, "symbol", 6) == 0) {
      SETFONT (s, 6, font, face, T_SYMBOL);
    } else if (strncmp (s, "schoolbook", 10) == 0) {
      SETFONT (s, 10, font, face, T_NEWCENTURY);
    } else {
      return (int) ERRSS ("unrecognized font keyword");
    }
  } else if (PyInt_Check (v)) {
    font = PyInt_AsLong (v);
  } else {
    (void)sprintf(buf, format, kw);
    return (int) ERRSS (buf);
  }
  *t = font;
  return 1;
#undef SETFONT
}

static int setkw_integer (PyObject * v, int *t, char *kw)
{
  char buf[256];
  char *format = "%s keyword requires integer argument";

  if (PyInt_Check (v)) {
    *t = PyInt_AsLong (v);
    return 1;
  }
  (void)sprintf(buf, format, kw);
  return (int) ERRSS (buf);
}

static int setkw_justify (PyObject * v, int *t, char *kw)
{
  char *s;
  char buf[256];
  char *format = "%s keyword requires string or integer argument";

  if (PyString_Check (v)) {
    s = PyString_AsString (v);

    /* Inspect the first character. */
    if (*s == 'N') {
      gistA.t.alignH = TH_NORMAL;
      s++;
    } else if (*s == 'L') {
      gistA.t.alignH = TH_LEFT;
      s++;
    } else if (*s == 'C') {
      gistA.t.alignH = TH_CENTER;
      s++;
    } else if (*s == 'R') {
      gistA.t.alignH = TH_RIGHT;
      s++;
    } else {
      while (*s)
	s++;
    }

    /* Inspect the second character. */
    if (*s == 'N') {
      gistA.t.alignV = TV_NORMAL;
    } else if (*s == 'T') {
      gistA.t.alignV = TV_TOP;
    } else if (*s == 'C') {
      gistA.t.alignV = TV_CAP;
    } else if (*s == 'H') {
      gistA.t.alignV = TV_HALF;
    } else if (*s == 'A') {
      gistA.t.alignV = TV_BASE;
    } else if (*s == 'B') {
      gistA.t.alignV = TV_BOTTOM;
    } else {
      return (int) ERRSS ("unrecognized justify keyword");
    }

  } else if (PyInt_Check (v)) {
    int justify = PyInt_AsLong (v);
    gistA.t.alignH = justify & 3;
    gistA.t.alignV = justify >> 2;

  } else {
    (void)sprintf(buf, format, kw);
    return (int) ERRSS (buf);
  }
  return 1;
}

static int setkw_linetype (PyObject * v, int *t, char *kw)
{
  int type = 0;
  char *errstr =
    "bad linetype: Use \"none\", \"solid\", \"dash\", \"dot\",\n"
    "\"dashdot\", \"dashdotdot\", or 0-5, respectively.";

  if (PyString_Check (v)) {
    char *s = PyString_AsString (v);
    if (strcmp (s, "none") == 0)
      type = L_NONE;
    else if (strcmp (s, "solid") == 0)
      type = L_SOLID;
    else if (strcmp (s, "dash") == 0)
      type = L_DASH;
    else if (strcmp (s, "dot") == 0)
      type = L_DOT;
    else if (strcmp (s, "dashdot") == 0)
      type = L_DASHDOT;
    else if (strcmp (s, "dashdotdot") == 0)
      type = L_DASHDOTDOT;
    else
      return (int) ERRSS (errstr);
  } else if (PyInt_Check (v)) {
    type = PyInt_AsLong (v);
    if (type < 0) type = 0;
    else if (type>5) type= 1 + (type-1)%5;
  } else {
      return (int) ERRSS (errstr);
  }

  *t = type;
  return 1;
}

static int setkw_string (PyObject * v, char **t, char *kw)
{
  char buf[256];
  char *format = "%s keyword requires string argument";
  char *tmpString;

  /*
   *  09/04/02 llc To address the comment below on the string returned
   *               from PyString_AsString: 
   *               Lee Taylor explained that the string returned is a 
   *               string in a string object (a PyObject), so
   *               the string object, rather than the string, was allocated. 
   *               To be on the same side, create space and 
   *               copy the string out of the Python string object.
   *               Error surfaced in FreeTmpLegend from LegendAndHide
   *               with seg fault on the LX cluster when trying to free 
   *               tmpLegend,
   *               but correct here instead of there to make sure 
   *               the problem does not recur.
   */

  if (PyString_Check (v)) {
    tmpString = PyString_AsString (v);
    if ( tmpString == NULL )  {
       *t = NULL;
    }
    else {
       *t = (char *) malloc ( strlen ( tmpString ) + 1 ); 
       strcpy ( *t, tmpString );
    }
    /* Should I Py_INCREF(v)?  PyString_AsString() just returns the pointer
       to its internal string value, which I'm saving away.  Hopefully,
       Gist strcpy's the string into its own space, but I dunno.... */
    return 1;
  }
  (void)sprintf(buf, format, kw);
  return (int) ERRSS (buf);
}

/* "Extended" integer: Allow the user to input a character. */
static int setkw_xinteger (PyObject * v, int *t, char *kw)
{
  char buf[256];
  char *format = "%s keyword requires integer or single character argument";

  if (PyInt_Check (v)) {
    *t = PyInt_AsLong (v);
    return 1;
  } else if (PyString_Check (v)) {
    char *s = PyString_AsString (v);
    *t = (int) s[0];
    return 1;
  }
  (void)sprintf(buf, format, kw);
  return (int) ERRSS (buf);
}

/* Set v, u, and the (y, x, ireg) mesh variables.
 * Called from plv and from mesh_loc, which happens to take the same args.
 * Note that PyObject references returned in vop and uop are
 * borrowed, so should usually NOT be DECREF'ed.
 * Returns 0 on failure, 1 otherwise.
 */
static int setvu_mesh(
  PyObject *args, PyObject **vop, PyObject **uop, char *errstr)
{
  int n;
  PyObject *newargs;

  switch (n = PyTuple_Size(args)) {
  case 5: /* (v, u, y, x, ireg) given */
  case 4: /* (v, u, y, x) given */
    TRY (newargs = PyTuple_GetSlice (args, 2, n), 0);
    TRY (set_pyMsh (newargs, errstr, 0), 0);
    Py_DECREF (newargs);
    /* (Fall through.) */
  case 2: /* (v, u) only given */
    TRY ( *vop = PyTuple_GetItem (args, 0), 0); /* Borrowed reference returned */
    TRY ( *uop = PyTuple_GetItem (args, 1), 0);
    break;
  default:
    return (int) ERRSS (errstr);
  }
  return 1;
}

/* Set z and the (y, x, ireg) mesh variables.  Called from plc and plf.
 * Note that PyObject reference returned in zop is borrowed, so should
 * usually NOT be DECREF'ed.
 * Returns 0 on failure, 1 otherwise.
 */
static int setz_mesh (
  PyObject *args, PyObject **zop, char *errstr, PyObject *tri)
{
  int n;
  PyObject *newargs;

  switch (n = PyTuple_Size(args)) {
  case 4: /* (z, y, x, ireg) given */
  case 3: /* (z, y, x) given */
    TRY (newargs = PyTuple_GetSlice (args, 1, n), 0);
    TRY (set_pyMsh (newargs, errstr, tri), 0);
    Py_DECREF (newargs);
    /* (Fall through.) */
  case 1: /* (z) only given */
    TRY ( *zop = PyTuple_GetItem (args, 0), 0);
    break;
  default:
    return (int) ERRSS (errstr);
  }
  return 1;
}

static int unpack_color_tuple (PyObject * ob, unsigned long color_triple[3])
{
  int i, size = PyTuple_Size (ob);
  PyObject *item;
  if ( size != 3 ) {
    return (int) ERRSS ("Color tuple must have 3 colors");
  }
  for (i = 0; i < 3; i++) {
    if ((item = PyTuple_GetItem (ob, i)) == 0) {
      return (int) ERRSS ("Error unpacking color tuple.");
    }
    if (PyInt_Check (item)) {
      color_triple[i] = PyInt_AsLong (item);
    } else {
      return (int) ERRSS ("Color tuple: expected integer value ");
    }
  }
  return 1;
}

/* Used only by limits() */
static int unpack_limit_tuple (PyObject * ob, double limits[], int *flags)
{
  int i, size = PyTuple_Size (ob);
  PyObject *item;
  if (5 != size) {
    return (int) ERRSS ("Old limits must have four doubles + 1 integer");
  }
  for (i = 0; i < 4; i++) {
    if ((item = PyTuple_GetItem (ob, i)) == 0) {
      return (int) ERRSS ("Error unpacking limit tuple.");
    }
    if (PyFloat_Check (item)) {
      limits[i] = PyFloat_AsDouble (item);
    } else if (PyInt_Check (item)) {
      limits[i] = (double) PyInt_AsLong (item);
    } else {
      return (int) ERRSS ("Expected floating point value");
    }
  }
  if ((item = PyTuple_GetItem (ob, 4)) == 0) {
    return (int) ERRSS ("Error unpacking flags in limit tuple.");
  }
  if (PyInt_Check (item)) {
    *flags = (int) PyInt_AsLong (item);
  } else {
    return (int) ERRSS ("Expected integer value");
  }
  return 1;
}

/*  -------------------------------------------------------------------- */

static char slice2__doc__[] =
"[nvf, xyzvf, colorf] = slice2 ((plane, nverts, xyzverts, values = None)\n"
"     Slice a polygon list, returning in nvf and xyzvf only those\n"
"     polygons or parts of polygons on the positive side of PLANE.\n"
"     If PLANE is a scalar real, then VALUES must be a function\n"
"     defined on the vertices of the mesh, and the mesh will\n"
"     be sliced where the function has that value.\n"
"     The NVERTS, XYZVERTS, and VALUES arrays have the meanings\n"
"     of the return values from the slice3 function. It is legal\n"
"     to omit the VALUES argument (e.g.- if there is no fcolor\n"
"     function).\n"
"     In order to plot two intersecting slices, one could\n"
"     slice (for example) the horizontal plane twice (slice2x) -\n"
"     first with the plane of the vertical slice, then with minus\n"
"     that same plane.  Then, plot first the back part of the\n"
"     slice, then the vertical slice, then the front part of the\n"
"     horizontal slice.  Of course, the vertical plane could\n"
"     be the one to be sliced, and `back' and `front' vary\n"
"     depending on the view point, but the general idea always\n"
"     works.\n"
"     \n"
"     slice2_precision= precision\n"
"     Controls how slice2 (or slice2x) handles points very close to\n"
"     the slicing plane.  PRECISION should be a positive number or zero.\n"
"     Zero PRECISION means to clip exactly to the plane, with points\n"
"     exactly on the plane acting as if they were slightly on the side\n"
"     the normal points toward.  Positive PRECISION means that edges\n"
"     are clipped to parallel planes a distance PRECISION on either\n"
"     side of the given plane.  (Polygons lying entirely between these\n"
"     planes are completely discarded.)\n"
"     \n"
"     Default value is 0.0.\n";

static PyObject * slice2 (PyObject * self, PyObject * args)
/* slice2 (plane, nverts, xyzverts, values = None, _slice2x = 0)
   returns either a triple [nverts, xyzverts, values] or a sextuple
   [nverts, xyzverts, values, nvertb, xyzvertb, valueb]            */
{
 PyObject * oplane,
          * onverts,
          * oxyzverts,
          * ovalues = (PyObject *) NULL,
          * oreturn_value,
          /* return values */
          * ornverts,
          * orxyzverts,
          * orvalues = (PyObject *) NULL,
          /* additional slice2x return values */
          * ornvertb,
          * orxyzvertb,
          * orvalueb = (PyObject *) NULL;
 PyArrayObject * aplane = (PyArrayObject *) NULL,
               * anverts,
               * axyzverts,
               * avalues = (PyArrayObject *) NULL;
 ArrayObject * rnverts = (ArrayObject *) NULL,
             * rxyzverts = (ArrayObject *) NULL,
             * rvalues = (ArrayObject *) NULL,
             * rnvertb = (ArrayObject *) NULL,
             * rxyzvertb = (ArrayObject *) NULL,
             * rvalueb = (ArrayObject *) NULL,
             * dp = (ArrayObject *) NULL,
             * ndp = (ArrayObject *) NULL,
             * nvertc = (ArrayObject *) NULL,
             * xyzc = (ArrayObject *) NULL,
             * valuec = (ArrayObject *) NULL,
             /* values returned from _slice2_part */
             * rnvertc,
             * rxyzc,
             * rvaluec = (ArrayObject *) NULL,
             * nvertc0 = (ArrayObject *) NULL,
             * xyzc0 = (ArrayObject *) NULL,
             * valuec0 = (ArrayObject *) NULL,
             * keep = (ArrayObject *) NULL,
             * prev = (ArrayObject *) NULL,
             * next = (ArrayObject *) NULL,
             * last = (ArrayObject *) NULL,
             * nkeep,
             * nkeep2,
             * mask2,
             * list = (ArrayObject *) NULL,
             * listc = (ArrayObject *) NULL;
 double * planed=0,
        * xyzvertsd=0,
        * valuesd = (double *) NULL,
        * rxyzvertsd=0,
        * rvaluecd = (double *) NULL,
        * rvaluesd = (double *) NULL,
        * rxyzvertbd=0,
        * rvaluebd = (double *) NULL,
        * dpd = (double *) NULL,
        * ndpd=0,
        * xyzcd=0,
        * xyzc0d=0,
        * valuecd = (double *) NULL,
        * valuec0d = (double *) NULL;
 int * nvertsd=0,
     * rnvertsd=0,
     * rnvertbd=0,
     * nvertcd=0,
     * nvertc0d=0,
     * nkeepd=0,
     * nkeep2d=0,
     * prevd=0,
     * nextd=0,
     * lastd=0,
     * listd=0,
     * listcd=0;
 Uchar * keepd=0,
       * mask2d=0,
       * valuesc = (Uchar *) NULL,
       * rvaluecc = (Uchar *) NULL,
       * rvaluesc = (Uchar *) NULL,
       * rvaluebc = (Uchar *) NULL,
       * valuecc = (Uchar *) NULL,
       * valuec0c = (Uchar *) NULL;
 char atype = '\0';
 int _slice2x = 0,
     plane_is_scalar,
     isum,
     i, j, k,
     list_length = 0,
     list0_length = 0,
     list1_length = 0,
     list2_length = 0,
     listc_length = 0,
     listc_length1 = 0,
     listc0_length = 0,
     node,
     sumt,
     sumv,
     xdims [2];
 double dplane=0.;
 
 if (!PyArg_ParseTuple (args, "OOO|Oi", &oplane, &onverts, &oxyzverts,
    &ovalues, &_slice2x))  {
    return ERRSS ("slice2: unable to parse arguments.");
 }
 plane_is_scalar = PyFloat_Check (oplane);
 if (plane_is_scalar)
    dplane = PyFloat_AsDouble (oplane);
 else {
    GET_ARR (aplane, oplane, PyArray_DOUBLE, 1, PyObject *);
    }
 /* convert arguments to arrays */
 GET_ARR (anverts, onverts, PyArray_LONG, 1, PyObject *);
 GET_ARR (axyzverts, oxyzverts, PyArray_DOUBLE, 2, PyObject *);
 if (isARRAY (ovalues)) {
    if (A_TYPE (ovalues) == PyArray_DOUBLE) {
       GET_ARR (avalues, ovalues, PyArray_DOUBLE, 1, PyObject *);
       valuesd = (double *) A_DATA (avalues);
       atype = 'd';
       }
    else if (A_TYPE (ovalues) == Py_GpColor) {
       GET_ARR (avalues, ovalues, Py_GpColor, 1, PyObject *);
       valuesc = (Uchar *) A_DATA (avalues);
       atype = 'b';
       }
    else {
       clearFreeList (0);
       return ERRSS ("Data type for values must be 'b' or 'd'.");
       }
    }
 /* convert to our type of array for ease of handling */
 if (! plane_is_scalar) {
    planed = (double *) A_DATA (aplane);
    }
 nvertsd = (int *) A_DATA (anverts);
 xyzvertsd = (double *) A_DATA (axyzverts);


 for (isum = 0, i = 0; i < A_SIZE (anverts); i++)
    isum += nvertsd [i];
 if (avalues) {
    if (A_SIZE (avalues) == A_SIZE (anverts))
       node = 0;
    else if (A_SIZE (avalues) == isum)
       node = 1;
    else {
       clearFreeList (0);
       return ERRSS (
       "Number of data values must equal number of nodes or number of cells.");
       }
    }
 else
    node = 0;
 if (! plane_is_scalar) {
    TRY (dp = allocateArray (A_SIZE (axyzverts) / 3, 'd', 0), 
       PyErr_NoMemory());
    dpd = (double *) (dp->data);
    for (i = 0; i < dp->size; i ++) {
       dpd [i] = xyzvertsd [3 * i] * planed [0] +
          xyzvertsd [3 * i + 1] * planed [1] +
          xyzvertsd [3 * i + 2] * planed [2] - planed [3];
       }
    /* At this point we are done with plane */
    removeFromArrayList ( (PyObject *) aplane);
    }
 else if (plane_is_scalar && avalues && node == 1) {
    TRY (dp = allocateArray (A_SIZE (avalues), 'd', 0), 
       PyErr_NoMemory());
    dpd = (double *) (dp->data);
    if (atype == 'd')
       for (i = 0; i < dp->size; i ++) {
          dpd [i] = valuesd [i] - dplane;
          }
    else
       for (i = 0; i < dp->size; i ++) {
          dpd [i] = (double) valuesc [i] - dplane;
       }
    }
 else {
    clearFreeList (0);
    return ERRSS ("Not sure what kind of slice you're asking for.");
    }
 /* nkeep is an integer array whose ith entry tells you the number */
 /* of vertices for polygon i which are "above" the isosurface or  */
 /* plane doing the slicing.                                       */
 TRY (nkeep = allocateArray (A_SIZE (anverts), 'i', 0), PyErr_NoMemory());
 nkeepd = (int *) (nkeep->data);
 k = 0;
 for (i = 0; i < nkeep->size; i++) {
    for (j = 0; j < nvertsd [i]; j ++) {
       nkeepd [i] += (int) (dpd [k] >= _slice2_precision) ;
       k ++;
       }
    }
 for (i = 0; i < nkeep->size; i++) {
    /* Compute length of vertex lists */
    /* list1: indices of polygons cut by the plane or isosurface */
    /* list: indices of the vertices of the above polygons       */
    if (nkeepd [i] != 0 && nkeepd [i] != nvertsd [i]) {
       list1_length ++;
       list_length += nvertsd [i];
       }
    }
 if (list1_length != 0) {
   /* nvertc: The number of vertices in each cut polygon.            */
   /* xyzc: The vertex coordinates of each cut polygon.              */
   /* valuec (if given): The values of the function on each vertex   */
   /*    (if node == 1) or on each polygon (if node == 0).           */
   TRY (nvertc = allocateArray (list1_length, 'i', 0), PyErr_NoMemory());
   nvertcd = (int *) (nvertc ->data);
   TRY (list = allocateArray (list_length, 'i', 0), PyErr_NoMemory());
   listd = (int *) (list->data);
   TRY (xyzc = allocateArray (list_length * 3, 'd', 0), PyErr_NoMemory());
   xyzcd = (double *) (xyzc->data);
   if (avalues && node == 0) {
      TRY (valuec = allocateArray (list1_length, atype, 0), 
         PyErr_NoMemory());
      }
   else if (avalues && node == 1) {
      TRY (valuec = allocateArray (list_length, atype, 0), PyErr_NoMemory());
      }
   if (avalues)  {
      if (atype == 'd')
         valuecd = (double *) (valuec->data);
      else
         valuecc = (Uchar *) (valuec->data);
   }
   for (i = 0, k = 0, sumv = 0, sumt = 0; i < nkeep->size; i++) {
      if (nkeepd [i] != 0 && nkeepd [i] != nvertsd [i]) {
         nvertcd [k] = nvertsd [i];
         if (avalues && node == 0)  {
            if (atype == 'd')
               valuecd [k] = valuesd [i];
            else
               valuecc [k] = valuesc [i];
         }
         for (j = 0; j < nvertsd [i]; j ++) {
            listd [sumv + j] = sumt + j;
            xyzcd [3 * (sumv + j)] = xyzvertsd [3 * (sumt + j)];
            xyzcd [3 * (sumv + j) + 1] = xyzvertsd [3 * (sumt + j) + 1];
            xyzcd [3 * (sumv + j) + 2] = xyzvertsd [3 * (sumt + j) + 2];
            if (avalues && node == 1)  {
               if (atype == 'd')
                  valuecd [sumv + j] = valuesd [sumt + j];
               else
                  valuecc [sumv + j] = valuesc [sumt + j];
            }
            }
         k ++;
         sumv += nvertsd [i];
         }
      sumt += nvertsd [i];
      }
   }

 if (_slice2x) {
   /* doing a double slice */
   if (! _slice2_precision) 
      if (list1_length == 0) {
         mask2 = (ArrayObject *) NULL;
         mask2d = (void *) NULL;
         nvertc0 = (ArrayObject *) NULL;
         nvertc0d = (void *) NULL;
         valuec0 = (ArrayObject *) NULL;
         valuec0d = (void *) NULL;
         valuec0c = (void *) NULL;
         xyzc0 = (ArrayObject *) NULL;
         xyzc0d = (void *) NULL;
         }
      else {
         TRY (mask2 = logical_not (nkeep, SAVE, 0), PyErr_NoMemory());
         mask2d = (Uchar *) (mask2->data);
         TRY (addToFreeList (nvertc0 = copyArray (nvertc), 0) != -1,
            PyErr_NoMemory ());
         nvertc0d = (int *) (nvertc0->data);
         if (avalues) {
            TRY (addToFreeList (valuec0 = copyArray (valuec), 0) != -1,
               PyErr_NoMemory ());
            if (atype == 'd')
               valuec0d = (double *) (valuec0->data);
            else
               valuec0c = (Uchar *) (valuec0->data);
            }
         TRY (addToFreeList (xyzc0 = copyArray (xyzc), 0) != -1,
            PyErr_NoMemory ());
         xyzc0d = (double *) (xyzc0->data);
         }
   else {
      TRY (nkeep2 = allocateArray (A_SIZE (anverts), 'i', 0), PyErr_NoMemory());
      nkeep2d = (int *) (nkeep->data);
      TRY (mask2 = allocateArray (A_SIZE (anverts), 'b', 0), PyErr_NoMemory());
      mask2d = (Uchar *) (mask2->data);
      k = 0;
      for (i = 0; i < nkeep2->size; i++) {
         for (j = 0; j < nvertsd [i]; j ++) {
            nkeep2d [i] += (int) (dpd [k] >= _slice2_precision) ;
            k ++;
            }
         }
      for (i = 0; i < nkeep2->size; i++) {
         /* Compute length of vertex lists */
         /* list2: indices of polygons cut _slice2_precision below surface */
         /* listc: indices of the vertices of the above polygons           */
         mask2d [i] = ! nkeep2d [i];
         if (nkeep2d [i] != 0 && nkeep2d [i] < nvertsd [i]) {
            list2_length ++;
            listc_length += nvertsd [i];
            }
         }
      if (list2_length != 0) {
         TRY (nvertc0 = allocateArray (list2_length, 'i', 0), 
            PyErr_NoMemory());
         nvertc0d = (int *) (nvertc0->data);
         TRY (listc = allocateArray (listc_length, 'i', 0), 
            PyErr_NoMemory());
         listcd = (int *) (listc->data);
         TRY (xyzc0 = allocateArray (listc_length * 3, 'd', 0), 
            PyErr_NoMemory());
         xyzc0d = (double *) (xyzc0->data);
         if (avalues && node == 0) {
            TRY (valuec0 = allocateArray (list2_length, atype, 0), 
               PyErr_NoMemory());
            if (atype == 'd')
               valuec0d = (double *) (valuec0->data);
            else
               valuec0c = (Uchar *) (valuec0->data);
            }
         else if (avalues && node == 1) {
            TRY (valuec0 = allocateArray (listc_length, atype, 0),
               PyErr_NoMemory());
            if (atype == 'd')
               valuec0d = (double *) (valuec0->data);
            else
               valuec0c = (Uchar *) (valuec0->data);
            }
         for (i = 0, k = 0, sumv = 0; i < nkeep2->size; i++) {
            if (nkeep2d [i] != 0 && nkeep2d [i] != nvertsd [i]) {
               nvertc0d [k] = nvertsd [i];
               if (avalues && node == 0) {
                  if (atype == 'd')
                     valuec0d [k] = valuesd [i];
                  else
                     valuec0c [k] = valuesc [i];
               }
               for (j = 0; j < nvertsd [i]; j ++) {
                  listcd [sumv + k] = i + j;
                  xyzc0d [3 * (sumv + j)] = xyzvertsd [3 * (i + j)];
                  xyzc0d [3 * (sumv + j) + 1] = xyzvertsd [3 * (i + j) + 1];
                  xyzc0d [3 * (sumv + j) + 2] = xyzvertsd [3 * (i + j) + 2];
                  if (avalues && node == 1)  {
                     if (atype == 'd')
                        valuec0d [sumv + j] = valuesd [i + j];
                     else
                        valuec0c [sumv + j] = valuesc [i + j];
                  }
                  }
               k ++;
               sumv += nvertsd [i];
               }
            }
         }
      }
    /* N. B. It's a little confusing, but list2 is being reused here, */
    /* but listc has to stay around for awhile.                       */
    list2_length = 0;
    if (mask2 != (ArrayObject *) NULL)
       for (i = 0, list2_length = 0, listc_length1 = 0; i < mask2->size; i++) {
          list2_length += mask2d [i];
          listc_length1 += mask2d [i] * nvertsd [i];
          }
    if (list2_length != 0) {
      TRY (rnvertb = allocateArray (list2_length, 'i', 0), 
         PyErr_NoMemory());
      rnvertbd = (int *) (rnvertb->data);
      if (avalues && node == 0) {
         TRY (rvalueb = allocateArray (list2_length, atype, 0), 
            PyErr_NoMemory());
         if (atype == 'd')
            rvaluebd = (double *) (rvalueb->data);
         else
            rvaluebc = (Uchar *) (rvalueb->data);
         }
      else if (avalues && node != 0) {
         TRY (rvalueb = allocateArray (listc_length1, atype, 0),
            PyErr_NoMemory());
         if (atype == 'd')
            rvaluebd = (double *) (rvalueb->data);
         else
            rvaluebc = (Uchar *) (rvalueb->data);
         }
      TRY (rxyzvertb = allocateArray (3 * listc_length1, 'd', 0), 
         PyErr_NoMemory());
      rxyzvertbd = (double *) (rxyzvertb->data);
      for (i = 0, k = 0, sumv = 0, sumt = 0; i < A_SIZE (anverts); i ++) {
         if (mask2d [i] != 0) {
            rnvertbd [k] = nvertsd [i];
            if (avalues && node == 0) {
               if (atype == 'd')
                  rvaluebd [k] = valuesd [i];
               else 
                  rvaluebc [k] = valuesc [i];
            }
            for (j = 0; j < nvertsd [i]; j ++) {
               rxyzvertbd [3 * (sumv + j)] = xyzvertsd [3 * (sumt + j)];
               rxyzvertbd [3 * (sumv + j) + 1] = xyzvertsd [3 * (sumt + j) + 1];
               rxyzvertbd [3 * (sumv + j) + 2] = xyzvertsd [3 * (sumt + j) + 2];
               if (avalues && node != 0)  {
                  if (atype == 'd')
                     rvaluebd [(sumv + j)] = valuesd [(sumt + j)];
                  else
                     rvaluebc [(sumv + j)] = valuesc [(sumt + j)];
               }
               }
            k ++;
            sumv += nvertsd [i];
            }
         sumt += nvertsd [i];
         }
      }
    freeArray (mask2, 0);
   }
/* freeArray (nkeep, 0); Need nkeep and nverts instead of mask0 */

 for (i = 0; i < nkeep->size; i++) {
    /* list0: the uncut polygons.                               */
    /* listc0: the vertices of the uncut polygons.              */
    list0_length += (int) (nkeepd [i] == nvertsd [i]);
    listc0_length += (nkeepd [i] == nvertsd [i]) ? nvertsd [i] : 0;
    }
 if (list0_length < A_SIZE (anverts) ) {
    if (list0_length == 0) {
       rnverts = (ArrayObject *) NULL;
       rxyzverts = (ArrayObject *) NULL;
       rvalues = (ArrayObject *) NULL;
       rnvertsd = (void *) NULL;
       rxyzvertsd = (void *) NULL;
       rvaluesd = (void *) NULL;
       }
    else {
       /* Extract the uncut data. */
       TRY (rnverts = allocateArray (list0_length, 'i', 0), 
          PyErr_NoMemory());
       rnvertsd = (int *) (rnverts->data);
       TRY (rxyzverts = allocateArray (listc0_length * 3, 'd', 0), 
          PyErr_NoMemory());
       rxyzvertsd = (double *) (rxyzverts->data);
       if (avalues && node != 0) {
          TRY (rvalues = allocateArray (listc0_length, atype, 0), 
             PyErr_NoMemory());
          if (atype == 'd')
             rvaluesd = (double *) (rvalues->data);
          else
             rvaluesc = (Uchar *) (rvalues->data);
          }
       else if (avalues) {
          TRY (rvalues = allocateArray (list0_length, atype, 0),
             PyErr_NoMemory());
          if (atype == 'd')
             rvaluesd = (double *) (rvalues->data);
          else
             rvaluesc = (Uchar *) (rvalues->data);
          }
       for (i = 0, k = 0, sumv = 0, sumt = 0; i < nkeep->size; i++) {
          if (nkeepd [i] == nvertsd [i]) {
             rnvertsd [k] = nvertsd [i];
             if (avalues && node == 0)  {
                if (atype == 'd')
                   rvaluesd [k] = valuesd [i];
                else
                   rvaluesc [k] = valuesc [i];
             }
             for (j = 0; j < nvertsd [i]; j++) {
                rxyzvertsd [3 * (sumv + j)] = xyzvertsd [3 * (sumt + j)];
                rxyzvertsd [3 * (sumv + j) + 1] = xyzvertsd [3 * (sumt + j) + 1];
                rxyzvertsd [3 * (sumv + j) + 2] = xyzvertsd [3 * (sumt + j) + 2];
                if (avalues && node != 0)  {
                   if (atype == 'd')
                      rvaluesd [(sumv + j)] = valuesd [(sumt + j)];
                   else
                      rvaluesc [(sumv + j)] = valuesc [(sumt + j)];
                }
                }
             k ++;
             sumv += nvertsd [i];
             }
          sumt += nvertsd [i];
          }
       }
    }
 else {
    /* inputs unchanged. But copy them. */
    TRY (rnverts = arrayFromPointer (A_SIZE (anverts), 'i', A_DATA (anverts), 0),
       PyErr_NoMemory());
    rnvertsd = (int *) (rnverts->data);
    TRY (rxyzverts = arrayFromPointer (A_SIZE (axyzverts), 'd', A_DATA (axyzverts), 0),
       PyErr_NoMemory());
    rxyzvertsd = (double *) (rxyzverts->data);
    /* We've given the data pointers of these two or three arrays to others, so they
       no longer own their data. Clear the OWN_DATA flags, so that the
       DECREF applied when removed from the array list will not free their data. */
       UNSET_OWN (anverts);
       UNSET_OWN (axyzverts);
    if (avalues == (PyArrayObject *) NULL)
       rvalues = (ArrayObject *) NULL;
    else {
       TRY (rvalues = arrayFromPointer (A_SIZE (avalues), atype, A_DATA (avalues), 0),
          PyErr_NoMemory());
       if (atype == 'd')
          rvaluesd = (double *) (rvalues->data);
       else
          rvaluesc = (Uchar *) (rvalues->data);
       UNSET_OWN (avalues);
       }
    }
 /* Free these now to avoid leaking both the data memory and the */
 /* memory for each ArrayObject.                                 */
 freeArray (nkeep, 0);
 removeFromArrayList ( (PyObject *) anverts);
 removeFromArrayList ( (PyObject *) axyzverts);
 removeFromArrayList ( (PyObject *) avalues);

 /* done if no partially clipped polys */
 if (list1_length == 0 && listc_length == 0) {
    if (rnverts) {
       RET_ARR (ornverts, 1, & (rnverts->size), PyArray_LONG, (char *) rnvertsd,
          PyObject *);
       SET_OWN (ornverts);
       }
    else {
       Py_INCREF (Py_None);
       ornverts = Py_None;
       }
    if (rxyzverts) {
       xdims [0] = rxyzverts->size / 3;
       xdims [1] = 3;
       RET_ARR (orxyzverts, 2, xdims, PyArray_DOUBLE, (char *) rxyzvertsd,
          PyObject *);
       SET_OWN (orxyzverts);
       }
    else {
       Py_INCREF (Py_None);
       orxyzverts = Py_None;
       }
    if (rvalues) {
       if (atype == 'd') {
          RET_ARR (orvalues, 1, & (rvalues->size), atype,
             (char *) rvaluesd, PyObject *);
          }
       else {
          RET_ARR (orvalues, 1, & (rvalues->size), atype,
             (char *) rvaluesc, PyObject *);
          }
       SET_OWN (orvalues);
       }
    else {
       Py_INCREF (Py_None);
       orvalues = Py_None;
       }
    if (rxyzvertb == NULL) {
       /* We remove the following three objects only because addresses */
       /* are in use.                                                  */
       removeArrayOnly (rnverts, 0);
       removeArrayOnly (rxyzverts, 0);
       removeArrayOnly (rvalues, 0);
       clearFreeList (0);
       if (_slice2x) {
          TRY (addToArrayList (oreturn_value = PyList_New (6)),
             PyErr_NoMemory());
          if (PyList_SetItem (oreturn_value, 3, Py_None) < 0 ||
              PyList_SetItem (oreturn_value, 4, Py_None) < 0 ||
              PyList_SetItem (oreturn_value, 5, Py_None) < 0) {
             clearArrayList ();
             return ERRSS ("slice2: unable to assign to return list.");
             }
          else {
             Py_INCREF (Py_None);
             Py_INCREF (Py_None);
             Py_INCREF (Py_None);
             }
          }
       else {
          TRY (addToArrayList (oreturn_value = PyList_New (3)), 
             PyErr_NoMemory());
          }
       if (PyList_SetItem (oreturn_value, 0, ornverts) < 0 ||
           PyList_SetItem (oreturn_value, 1, orxyzverts) < 0 ||
           PyList_SetItem (oreturn_value, 2, orvalues) < 0) {
          clearArrayList ();
          return ERRSS ("slice2: unable to assign to return list.");
          }
       else {
          /* don't want to decref returning values, just clear the list */
          array_list_length = 0;
          return oreturn_value;
          }
       }
    else {
       /* Build the rest */
       RET_ARR (ornvertb, 1, & (rnvertb->size), PyArray_LONG, (char *) rnvertbd,
          PyObject *);
       SET_OWN (ornvertb);
       xdims [0] = rxyzvertb->size / 3;
       xdims [1] = 3;
       RET_ARR (orxyzvertb, 2, xdims, PyArray_DOUBLE, (char *) rxyzvertbd,
          PyObject *);
       SET_OWN (orxyzvertb);
       if (avalues) {
          if (atype == 'd') {
             RET_ARR (orvalueb, 1, & (rvalueb->size), atype,
                (char *) rvaluebd, PyObject *);
             }
          else {
             RET_ARR (orvalueb, 1, & (rvalueb->size), atype,
                (char *) rvaluebc, PyObject *);
             }
          SET_OWN (orvalueb);
          }
       else {
          Py_INCREF (Py_None);
          orvalueb = Py_None;
          }
       /* We remove the following six objects only because addresses */
       /* are in use.                                                */
       removeArrayOnly (rnverts, 0);
       removeArrayOnly (rxyzverts, 0);
       removeArrayOnly (rvalues, 0);
       removeArrayOnly (rnvertb, 0);
       removeArrayOnly (rxyzvertb, 0);
       removeArrayOnly (rvalueb, 0);
       clearFreeList (0);
       TRY (addToArrayList (oreturn_value = PyList_New (6)), 
          PyErr_NoMemory());
       if (PyList_SetItem (oreturn_value, 0, ornverts) < 0 ||
           PyList_SetItem (oreturn_value, 1, orxyzverts) < 0 ||
           PyList_SetItem (oreturn_value, 2, orvalues) < 0 ||
           PyList_SetItem (oreturn_value, 3, ornvertb) < 0 ||
           PyList_SetItem (oreturn_value, 4, orxyzvertb) < 0 ||
           PyList_SetItem (oreturn_value, 5, orvalueb) < 0) {
          clearArrayList ();
          return ERRSS ("slice2: unable to assign to return list.");
          }
       else {
          /* don't want to decref returning values, just clear the list */
          array_list_length = 0;
          return oreturn_value;
          }
       }
    }

 if (list1_length != 0) {
    /* get dot products and keep list for the clipped polys */
    ndpd = (double *) malloc (list_length * sizeof (double));
    for (i = 0; i < list_length; i++)
       ndpd [i] = dpd [listd [i]] - _slice2_precision;
    freeArray (dp, 0);
    TRY (dp = arrayFromPointer (list_length, 'd', ndpd, 0),
       PyErr_NoMemory());
    dpd = ndpd;
    freeArray (keep, 0);
    TRY (keep = allocateArray (dp->size, 'b', 0), 
       PyErr_NoMemory());
    keepd = (Uchar *) (keep->data);
    TRY (prev = allocateArray (dp->size, 'i', 0), 
       PyErr_NoMemory());
    prevd = (int *) (prev->data);
    TRY (next = allocateArray (dp->size, 'i', 0), 
       PyErr_NoMemory());
    nextd = (int *) (next->data);
    for (i = 0; i < dp->size; i ++) {
       keepd [i] = (Uchar) (dpd [i] >= 0.0);
       prevd [i] = i - 1;
       nextd [i] = i - 1;
       }
    TRY (last = allocateArray (nvertc->size, 'i', 0), 
       PyErr_NoMemory());
    lastd = (int *) (last->data);
    for (i = 0, isum = 0; i < nvertc->size; i++) {
       isum += nvertcd [i];
       lastd [i] = isum;
       prevd [lastd [i] - nvertcd [i]] = lastd [i] - 1;
       }
    freeArray (nvertc, 0);
    for (i = 0; i < next->size; i++) {
       nextd [prevd [i]] = i;
       }
    if (avalues && node == 1) {
       TRY (_slice2_part (xyzc, keep, next, dp, prev, last, valuec, & rxyzc,
          & rnvertc, & rvaluec, FREE0, FREE0, atype), 
          PyErr_NoMemory());
       }
    else {
       TRY (_slice2_part (xyzc, keep, next, dp, prev, last,
          (ArrayObject *) NULL, & rxyzc, & rnvertc, & rvaluec, FREE0, SAVE, atype), 
          PyErr_NoMemory());
       if (valuec) {
          TRY (rvaluec = allocateArray (valuec->size, atype, 0), PyErr_NoMemory());
          if (atype == 'd') {
             rvaluecd = (double *) rvaluec->data;
             for ( i = 0; i < valuec->size; i ++)
                rvaluecd [i] = valuecd [i];
             }
          else {
             rvaluecc = (Uchar *) rvaluec->data;
             for ( i = 0; i < valuec->size; i ++)
                rvaluecc [i] = valuecc [i];
             }
          }
       }
    TRY (rnverts = concatenate (rnverts, rnvertc, FREE0, FREE0, 0), 
       PyErr_NoMemory());
    rnvertsd = (int *) rnverts->data;
    if (avalues) {
       if (rvaluec == (ArrayObject *) NULL)
          TRY (rvalues = concatenate (rvalues, valuec, FREE0, FREE0, 0),
             PyErr_NoMemory());
       else
          TRY (rvalues = concatenate (rvalues, rvaluec, FREE0, FREE0, 0), 
             PyErr_NoMemory());
       if (atype == 'd')
          rvaluesd = (double *) rvalues->data;
       else
          rvaluesc = (Uchar *) rvalues->data;
       }
    else {
       Py_INCREF (Py_None);
       rvalues = (ArrayObject *) Py_None;
       }
    TRY (rxyzverts = concatenate (rxyzverts, rxyzc, FREE0, FREE0, 0), 
       PyErr_NoMemory());
    rxyzvertsd = (double *) rxyzverts->data;
    freeArray (last, 0);
    freeArray (prev, 0);
    freeArray (next, 0);
    }

 freeArray (list, 0);

 if (_slice2x || list1_length == 0) {
    if (! _slice2_precision)
       for (i = 0; i < keep->size; i++)
          keepd [i] = ! keepd [i];
    else {
       TRY (ndp = allocateArray (listc_length, 'd', 0), 
          PyErr_NoMemory());
       ndpd = (double *) (ndp->data);
       freeArray (keep, 0);
       TRY (keep = allocateArray (listc_length, 'b', 0), 
          PyErr_NoMemory());
       keepd = (Uchar *) (keep->data);
       for (i = 0; i < listc_length; i ++) {
          ndpd [i] = dpd [listcd [i]] + _slice2_precision;
          keepd [i] = ndpd [i] >= 0.0;
          }
       freeArray (dp, 0);
       dp = ndp;
       }
    TRY (prev = allocateArray (keep->size, 'i', 0), 
       PyErr_NoMemory());
    prevd = (int *) (prev->data);
    TRY (next = allocateArray (keep->size, 'i', 0), 
       PyErr_NoMemory());
    nextd = (int *) (next->data);
    for (i = 0; i < keep->size; i ++) {
        prevd [i] = i - 1;
        nextd [i] = i - 1;
       }
    TRY (last = allocateArray (nvertc0->size, 'i', 0), 
       PyErr_NoMemory());
    lastd = (int *) (last->data);
    for (i = 0, isum = 0; i < nvertc0->size; i++) {
       isum += nvertc0d [i];
       lastd [i] = isum;
       prevd [lastd [i] - nvertc0d [i]] = lastd [i] - 1;
       }
    for (i = 0; i < next->size; i ++)
       nextd [prevd [i]] = i;
    if (avalues && node == 1) {
       TRY (_slice2_part (xyzc0, keep, next, dp, prev, last, valuec0,
          & rxyzc, & rnvertc, & rvaluec, FREE0, FREE0, atype), 
          PyErr_NoMemory());
       }
    else {
       TRY (_slice2_part (xyzc0, keep, next, dp, prev, last,
          (ArrayObject *) NULL, & rxyzc, & rnvertc, & rvaluec,
          FREE0, FREE0, atype), 
          PyErr_NoMemory());
       if (valuec0) {
          TRY (rvaluec = allocateArray (valuec0->size, atype, 0), PyErr_NoMemory());
          if (atype == 'd') {
             rvaluecd = (double *) rvaluec->data;
             for ( i = 0; i < valuec0->size; i ++)
                rvaluecd [i] = valuec0d [i];
             }
          else {
             rvaluecc = (Uchar *) rvaluec->data;
             for ( i = 0; i < valuec0->size; i ++)
                rvaluecc [i] = valuec0c [i];
             }
          }
       }
    if (rnvertb == (ArrayObject *) NULL) {
       rnvertb = rnvertc;
       rnvertbd = (int *) rnvertb->data ;
       if (avalues) {
          rvalueb = rvaluec;
          if (atype == 'd')
             rvaluebd = (double *) rvalueb->data;
          else
             rvaluebc = (Uchar *) rvalueb->data;
          }
       else {
          Py_INCREF (Py_None);
          rvalueb = (ArrayObject *) Py_None;
          }
       rxyzvertb = rxyzc;
       rxyzvertbd = (double *) rxyzvertb->data;
       }
    else if (rnvertc != (ArrayObject *) NULL) {
       TRY (rnvertb = concatenate (rnvertb, rnvertc, FREE0, FREE0, 0), 
          PyErr_NoMemory());
       rnvertbd = (int *) rnvertb->data ;
       if (avalues) {
          TRY (rvalueb = concatenate (rvalueb, rvaluec, FREE0, FREE0, 0), 
             PyErr_NoMemory());
          if (atype == 'd')
             rvaluebd = (double *) rvalueb->data;
          else
             rvaluebc = (Uchar *) rvalueb->data;
          }
       else {
          Py_INCREF (Py_None);
          rvalueb = (ArrayObject *) Py_None;
          }
       TRY (rxyzvertb = concatenate (rxyzvertb, rxyzc, FREE0, FREE0, 0), 
          PyErr_NoMemory());
       rxyzvertbd = (double *) rxyzvertb->data;
       }
    freeArray (prev, 0);
    freeArray (next, 0);
    freeArray (last, 0);
    }
  freeArray (listc, 0);
  freeArray (keep, 0);

 /* All done, set up return values. */
 RET_ARR (ornverts, 1, & (rnverts->size), PyArray_LONG, (char *) rnvertsd,
    PyObject *);
 SET_OWN (ornverts);
 xdims [0] = rxyzverts->size / 3;
 xdims [1] = 3;
 RET_ARR (orxyzverts, 2, xdims, PyArray_DOUBLE, (char *) rxyzvertsd,
    PyObject *);
 SET_OWN (orxyzverts);
 if (rvalues != (ArrayObject *) Py_None) {
    i = rvalues->size;
    if (atype == 'd') {
       RET_ARR (orvalues, 1, & i, atype, (char *) rvaluesd,
          PyObject *);
       }
    else {
       RET_ARR (orvalues, 1, & i, atype, (char *) rvaluesc,
          PyObject *);
       }
    SET_OWN (orvalues);
    }
 else {
    Py_INCREF (Py_None);
    orvalues = Py_None;
    }
 if (rxyzvertb == NULL) {
    /* We remove the following three objects only because addresses */
    /* are in use.                                                  */
    removeArrayOnly (rnverts, 0);
    removeArrayOnly (rxyzverts, 0);
    removeArrayOnly (rvalues, 0);
    clearFreeList (0);
    TRY (addToArrayList (oreturn_value = PyList_New (3)), 
       PyErr_NoMemory());
    if (PyList_SetItem (oreturn_value, 0, ornverts) < 0 ||
        PyList_SetItem (oreturn_value, 1, orxyzverts) < 0 ||
        PyList_SetItem (oreturn_value, 2, orvalues) < 0) {
       clearArrayList ();
       return ERRSS ("slice2: unable to assign to return list.");
       }
    else {
       /* don't want to decref returning values, just clear the list */
       array_list_length = 0;
       return oreturn_value;
       }
    }
 else {
    /* Build the rest */
    RET_ARR (ornvertb, 1, & (rnvertb->size), PyArray_LONG, (char *) rnvertbd,
       PyObject *);
    SET_OWN (ornvertb);
    xdims [0] = rxyzvertb->size / 3;
    xdims [1] = 3;
    RET_ARR (orxyzvertb, 2, xdims, PyArray_DOUBLE, (char *) rxyzvertbd,
       PyObject *);
    SET_OWN (orxyzvertb);
    if (rvalueb != (ArrayObject *) Py_None) {
       if (atype == 'd') {
          RET_ARR (orvalueb, 1, & (rvalueb->size), atype,
             (char *) rvaluebd, PyObject *);
          }
       else {
          RET_ARR (orvalueb, 1, & (rvalueb->size), atype,
             (char *) rvaluebc, PyObject *);
          }
       SET_OWN (orvalueb);
       }
    else {
       Py_INCREF (Py_None);
       orvalueb = Py_None;
       }
    /* We remove the following six objects only because addresses */
    /* are in use.                                                */
    removeArrayOnly (rnverts, 0);
    removeArrayOnly (rxyzverts, 0);
    removeArrayOnly (rvalues, 0);
    removeArrayOnly (rnvertb, 0);
    removeArrayOnly (rxyzvertb, 0);
    removeArrayOnly (rvalueb, 0);
    clearFreeList (0);
    TRY (addToArrayList (oreturn_value = PyList_New (6)), 
       PyErr_NoMemory());
    if (PyList_SetItem (oreturn_value, 0, ornverts) < 0 ||
        PyList_SetItem (oreturn_value, 1, orxyzverts) < 0 ||
        PyList_SetItem (oreturn_value, 2, orvalues) < 0 ||
        PyList_SetItem (oreturn_value, 3, ornvertb) < 0 ||
        PyList_SetItem (oreturn_value, 4, orxyzvertb) < 0 ||
        PyList_SetItem (oreturn_value, 5, orvalueb) < 0) {
       clearArrayList ();
       return ERRSS ("slice2: unable to assign to return list.");
       }
    else {
       /* don't want to decref returning values, just clear the list */
       array_list_length = 0;
       return oreturn_value;
       }
    }
} /* end of slice2 */

/*  -------------------------------------------------------------------- */

static char unzoom__doc__[] =
"unzoom()\n"
"     Restore limits to their values before zoom and pan operations\n"
"     performed interactively using the mouse.\n"
"     Use    old_limits = limits()\n"
"            ...\n"
"            limits( old_limits )\n"
"     to save and restore plot limits generally.\n"
"\n"
"   SEE ALSO: limits, ylimits, zoom_factor, plg\n";

static PyObject *unzoom (PyObject * self, PyObject * args)
{
  GdRevertLimits (1);
  Py_INCREF (Py_None);
#ifdef WINDOWS
  pyg_on_idle();
#endif
  return Py_None;
}

/* Linear search for keyword in an array of allowable keywords */
static int verify_kw (char *keyword, char * kwlist[])
{
  int i;

  for (i = 0; kwlist[i]; i++){
    if (0 == strcmp (keyword, kwlist[i])){
      return 1; /* found it */
    }
  }
  return 0; /* didn't find it */
}

/*  -------------------------------------------------------------------- */

static char viewport__doc__[] =
"Return viewport.";

static PyObject *viewport (PyObject * self, PyObject * args)
{
  double xmin, xmax, ymin, ymax;
  xmin = gistD.trans.viewport.xmin;
  xmax = gistD.trans.viewport.xmax;
  ymin = gistD.trans.viewport.ymin;
  ymax = gistD.trans.viewport.ymax;
  return Py_BuildValue ("dddd", xmin, xmax, ymin, ymax);
}

/*  -------------------------------------------------------------------- */
/*  window */

static char window__doc__[] =
"window( [n] [, display = `host:server.screen', dpi=100/75, wait=0/1,\n"
"                       private=0/1, hcp=`hcp_filename', dump=0/1,\n"
"                       legends=1/0, style=`style_sheet_filename' ] )\n"
"     select window N as the current graphics output window.  N may\n"
"     range from 0 to 7, inclusive.  Each graphics window corresponds to\n"
"     an X window, and optionally has its own associated hardcopy file.\n"
"     If N is omitted, it defaults to the current coordinate system.\n"
"\n"
"     The X window will appear on your default display at 75 dpi, unless\n"
"     you specify the display and/or dpi keywords.  A dpi=100 X window\n"
"     is larger than a dpi=75 X window; both represent the same thing\n"
"     on paper.  Use display=`' to create a graphics window which has\n"
"     no associated X window (you should do this if you want to make\n"
"     plots in a non-interactive batch mode).\n"
"\n"
"     By default, an X window will attempt to use shared colors, which\n"
"     permits several Pygist graphics windows (including windows from\n"
"     multiple instances of Python) to use a common palette.  You can\n"
"     force an X window to post its own colormap (set its colormap\n"
"     attribute) with the private=1 keyword.  You will most likely have\n"
"     to fiddle with your window manager to understand how it handles\n"
"     colormap focus if you do this.  Use private=0 to return to shared\n"
"     colors.\n"
"\n"
"     By default, Python will not wait for the X window to become visible;\n"
"     code which creates a new window, then plots a series of frames to\n"
"     that window should use wait=1 to assure that all frames are actually\n"
"     plotted.\n"
"\n"
"     By default, a graphics window does NOT have a hardcopy file\n"
"     of its own -- any request for hardcopy are directed to the\n"
"     default hardcopy file, so hardcopy output from any window goes\n"
"     to a single file.  By specifying the hcp keyword, however, a\n"
"     hardcopy file unique to this window will be created.  If the\n"
"     `hcp_filename' ends in `.ps', the hardcopy file will be a PostScript\n"
"     file; otherwise, hardcopy files are in binary CGM format.  Use\n"
"     hcp=`' to revert to the default hardcopy file (closing the window\n"
"     specific file, if any).  The legends keyword, if present, controls\n"
"     whether the curve legends are (legends=1, the default) or are not\n"
"     (legends=0) dumped to the hardcopy file.  The dump keyword, if\n"
"     present, controls whether all colors are converted to a gray scale\n"
"     (dump=0, the default), or the current palette is dumped at the\n"
"     beginning of each page of hardcopy output.  (The legends keyword\n"
"     applies to all pictures dumped to hardcopy from this graphics\n"
"     window.  The dump keyword applies only to the specific hardcopy\n"
"     file defined using the hcp keyword -- use the dump keyword in the\n"
"     hcp_file command to get the same effect in the default hardcopy\n"
"     file.)\n"
"\n"
"     If both display=`'; and hcp=`', the graphics window will be\n"
"     entirely eliminated.\n"
"\n"
"     The style keyword, if present, specifies the name of a Gist style\n"
"     sheet file; the default is `work.gs'.  The style sheet determines\n"
"     the number and location of coordinate systems, tick and label styles,\n"
"     and the like.  Other choices include `axes.gs', `boxed.gs',\n"
"     `work2.gs', and `boxed2.gs'.\n"
"\n"
"     Window(...) returns the current window number.\n"
"\n"
"   SEE ALSO: plsys, hcp_file, fma, hcp, redraw, palette, animate, plg,\n"
"             winkill, gridxy\n";

#undef N_KEYWORDS
#define N_KEYWORDS 11
static char *windowKeys[N_KEYWORDS+1]= {
  "display", "dpi", "private", "hcp", "legends", "dump", "style", "wait",
  "width", "height", "rgb", 0 };

static PyObject *window (PyObject * self, PyObject * args, PyObject * kd)
{
  int n, nGiven;
  Drauing *drawing;
  GpColorCell *palette;
  PyObject * kwt[NELT(windowKeys) - 1];
  int nColors = 0;
  int wait_for_expose = 0;
  int rgb = 0;

  SETJMP0;			/* See Xerror_longjmp() */

  if (!PyArg_ParseTuple (args, "|i", &n))  {
    return ERRSS ("window takes zero or one non-keyword integer argument."); 
  }

  if(PyTuple_Size(args) == 1) { /* Window() was called with an argument. */
    if (n < 0 || n > 7)  {
      return ERRSS ("graphics windows are numbered from 0 to 7");
    }
    nGiven = (!ghDevices[n].display && !ghDevices[n].hcp);
  } else { /* No argument was given. */
    n = curPlotter;
    nGiven = (n < 0);
    if (nGiven)
      n = 0;
  }

  BUILD_KWT(kd, windowKeys, kwt);

  curElement = -1;

  /* get current palette for this graphics window */
  nColors = GhGetPalette (n, &palette);

    /* check for width and height specs */
  if (kwt[8]) {
    extern int gx75width, gx100width;
    int width;
    SETKW(kwt[8], width, setkw_integer, windowKeys[8]);
    if (width>30) gx75width= gx100width= width;
    else { gx75width= 450; gx100width= 600; }
  }
  if (kwt[9]) {
    extern int gx75height, gx100height;
    int height;
    SETKW(kwt[9], height, setkw_integer, windowKeys[9]);
    if (height>30) gx75height= gx100height= height;
    else { gx75height= 450; gx100height= 600; }
  }

  if (nGiven || kwt[0] || kwt[1] || kwt[2]) {
    /* display= and/or dpi= keywords */
    char *display = 0;
    int dpi = defaultDPI;
    int privmap = 0;
    Engine *engine = ghDevices[n].display;	/* current display engine */

    SETKW(kwt[0], display, setkw_string, windowKeys[0]);
    if (kwt[1]) {
      if (engine)  {
	return ERRSS ("cannot change dpi of an existing graphics window");
      }
      SETKW(kwt[1], dpi, setkw_integer, windowKeys[1]);
      if (dpi<25) dpi = 25;
      else if (dpi>300) dpi = 300;
    }
    if (kwt[2]) {
      /* private= keyword -- turn on/off private X window colormap */
      if (engine)  {
        return ERRSS ("cannot give existing graphics window private colormap");
      }
      if (!(nGiven? (!display || display[0]) : (display && display[0])))  {
        return ERRSS ("private= keyword not legal without display engine");
      }
      SETKW(kwt[2], privmap, setkw_boolean, windowKeys[2]);
    }
    if (kwt[10]) {
      /* rgb= keyword -- maybe make this a true color window */
      if (engine)  {
        return ERRSS ("cannot use rgb= on existing graphics window");
      }
      if (!(nGiven? (!display || display[0]) : (display && display[0])))  {
        return ERRSS ("rgb= keyword not legal without display engine");
      }
      SETKW(kwt[2], rgb, setkw_boolean, windowKeys[2]);
    }

    if (engine) {
      ghDevices[n].display = 0;
      GpKillEngine (engine);
    }
    if (nGiven ? (!display || display[0]) : (display && display[0]))  {
      gist_private_map = privmap;
      gist_rgb_hint = rgb;
      engine= GpFXEngine(windowNames[n], 0, dpi, display);
      if (!engine)  {
	return ERRSS ("failed to open X display or create X window");
      } else {
        wait_for_expose = 1;
      }
      ghDevices[n].display = engine;
      if (palette)
	GhSetPalette (n, palette, nColors);
    }
  }

  if (kwt[3]) {
    /* hcp= keyword -- make a new hcp file */
    Engine *engine = ghDevices[n].hcp;
    char *hcp = 0;

    SETKW(kwt[3], hcp, setkw_string, windowKeys[3]);
    if (engine) {
      ghDevices[n].hcp = 0;
      GpKillEngine (engine);
      SetHCPname (n, (char *) 0);
    }
    if (hcp && hcp[0]) {
      long len = strlen (hcp);

      if (len > 3 && strcmp (&hcp[len - 3], ".ps") == 0) {
	engine = GpPSEngine (windowNames[n], 0, hcpDump, SetHCPname (n, hcp));
	if (!engine)  {
	  return ERRSS ("failed to create PostScript file");
        }
      } 
      else {
	engine = GpCGMEngine (windowNames[n], 0, hcpDump, SetHCPname (n, hcp));
	if (!engine)  {
	  return ERRSS ("failed to create binary CGM file");
        }
      }

      ghDevices[n].hcp = engine;
      if (palette)
	GhSetPalette (n, palette, nColors);
    }
  }

  if (kwt[4] || kwt[3] || nGiven || kwt[0] || kwt[1]) {
      /* legends= keyword -- turn on/off legend dumping to hcp file */
    int legends;
    SETKW(kwt[4], legends, setkw_boolean, windowKeys[4]);
    if (kwt[4])
      ghDevices[n].doLegends = legends;
    else
      ghDevices[n].doLegends = defaultLegends;
  }
  if (kwt[5]) {
    /* dump= keyword -- turn on/off colormap dumping to hcp file */
    int dump;
    if (!ghDevices[n].hcp)  {
      return ERRSS (
	"dump= keyword not legal without hcp engine -- use hcp_file");
    }
    SETKW(kwt[5], dump, setkw_boolean, windowKeys[5]);
    GhDumpColors (n, 1, dump);
  }

  if (!ghDevices[n].display && !ghDevices[n].hcp) {
    /* shut down this graphics window completely */
    drawing = ghDevices[n].drawing;
    ghDevices[n].drawing = 0;
    if (drawing)
      GdKillDrawing (drawing);
    GhDeletePalette (n);
    paletteSize = 0;
    if (n == curPlotter) {
      /* highest numbered remaining window becomes current window */
      for (n = 7; n >= 0; n--)
	if (ghDevices[n].display || ghDevices[n].hcp)
	  break;
      curPlotter = n;
      GhSetPlotter (n);
      if (n >= 0) {
	Engine *engine = ghDevices[n].display;
	if (!engine)
	  engine = ghDevices[n].hcp;
	if (engine)
	  paletteSize = GpGetPalette (engine, &palette);
      }
    }
  } 
  else {
    if (kwt[6]) {
      /* style= keyword -- make new drawing */
      char *style = 0;
      SETKW(kwt[6], style, setkw_string, windowKeys[6]);
      drawing = ghDevices[n].drawing;
      if (drawing) {
	ghDevices[n].drawing = 0;
	GdKillDrawing (drawing);
      }
      if (!style || !style[0])
	style = defaultStyle;
      ghDevices[n].drawing = drawing = GdNewDrawing (style ? style : "work.gs");

    } else if (!ghDevices[n].drawing) {
      /* supply default drawing */
      ghDevices[n].drawing = drawing =
	  GdNewDrawing (defaultStyle ? defaultStyle : "work.gs");

    } else {
      drawing = ghDevices[n].drawing;
    }

    if (!drawing) {
      ghDevices[n].drawing = drawing = GdNewDrawing ("work.gs");
      if (drawing)  {
	return ERRSS ("failed to create drawing -- bad style sheet name?");
      }
      else  {
	return ERRSS (
	  "failed to create drawing -- Gist work.gs style sheet missing");
      }
    }
    /* make this window current */
    curPlotter = n;
    GhSetPlotter (n);
    paletteSize = nColors;

    /* wait= keyword -- pause until X window is exposed */
#ifdef WINDOWS
    /* The wait causes PythonWin to hang. Since it doesn't seem to be needed
     * anyway, just leave it out. Note that wait=1 is used in gistdemolow.
     */
    wait_for_expose = 0;
#else
    if (kwt[7] && wait_for_expose) {
      int wait;
      SETKW(kwt[7], wait, setkw_boolean, windowKeys[7]);


      wait_for_expose = wait_for_expose && (1 == wait);
    } else {
      wait_for_expose = 0;
    }
#endif
  }

  /* under MS Windows, the first expose event occurs synchronously inside
   * the GpFXEngine call -- hence the oops==2 logic */
  if (wait_for_expose) {
    int oops = gist_expose_wait(ghDevices[n].display, pyg_got_expose);
    if (oops==1 || pyg_wait_flag) {
      /* hopefully this is impossible */
      return ERRSS ("window,wait=1 while already waiting for a window");
    }
    /*
     *  Pause for window to pop up  
     */
    if (oops != 2) {
      pyg_wait_flag = 1;
      p_wait_while(&pyg_wait_flag);
    }
  }

  return Py_BuildValue ("i",n);
}

static void
pyg_got_expose(void)
{
  pyg_wait_flag = 0;
}

/*  -------------------------------------------------------------------- */

static char zoom_factor__doc__[] =
"zoom_factor( factor )\n"
"     Set the zoom factor for mouse-click zoom in and zoom out operations.\n"
"     The default FACTOR is 1.5; FACTOR should always be greater than 1.0.\n"
"\n"
"   SEE ALSO: limits, ylimits, unzoom, plg\n";

static PyObject *zoom_factor (PyObject * self, PyObject * args)
{
  if (!PyArg_ParseTuple (args, "d", &gxZoomFactor)) {
    return ERRSS ("Zoomfactor takes one floating point argument.");
  }
  if (gxZoomFactor < 0.0)
    gxZoomFactor = -gxZoomFactor;
  if (gxZoomFactor < 0.05)
    gxZoomFactor = 0.05;
  else if (gxZoomFactor > 20.0)
    gxZoomFactor = 20.0;
  Py_INCREF (Py_None);
  return Py_None;
}

static char pyg_unhook__doc__[] =
"pyg_unhoook( )\n"
"     Remove pygist PyOS_InputHook if present (for _tkinter).\n";

static PyObject *pyg_unhook (PyObject * self, PyObject * args)
{
  /* _tkinter needs PyOs_InputHook */
#if defined(CYGWIN) || defined(MACOSX)
  if (PyOS_InputHook == p_pending_events) PyOS_InputHook = 0;
#else
  if (PyOS_InputHook == p_wait_stdin) PyOS_InputHook = 0;
#endif
  Py_INCREF (Py_None);
  return Py_None;
}

/* pyg_idler
 * needs to be called by tcl's event loop
 * via tcl command "after idle pyg_idler" in order to make pygist
 * function together with _tkinter
 * -- this technique replaces PyOS_InputHook, which _tkinter needs
 *    (1) if PyOS_InputHook is already set when gistC loads,
 *        you *must* arrange to have pyg_idler called at idle time
 *    (2) if gistC loads before _tkinter, you *must* call pyg_unhook
 *        first in order for _tkinter to function properly
 */
static char pyg_idler__doc__[] =
"pyg_idler( )\n"
"     Do any deferred pygist window operations.\n";

static PyObject *pyg_idler (PyObject * self, PyObject * args)
{
  p_on_idle(0);
  Py_INCREF (Py_None);
  return Py_None;
}

static char pyg_pending__doc__[] =
"pyg_pending( )\n"
"     Handle any pending pygist window events.\n";

static PyObject *pyg_pending (PyObject * self, PyObject * args)
{
  p_pending_events();
  Py_INCREF (Py_None);
  return Py_None;
}

static PyObject *pyg_connector = 0;
static PyObject *pyg_keyhandler = 0;

static char pyg_register__doc__[] =
"pyg_register( connector, keyhandler )\n"
"     connector(dis,fd) will be called on gist connect/disconnect.\n"
"     keyhandler(line) will be called when line typed in gist window.\n";

/* wire up pyg_on_connect function */
static PyObject *pyg_register (PyObject * self, PyObject * args)
{
  PyObject *func1, *func2;
  if (!PyArg_ParseTuple(args, "|OO", &func1, &func2) ||
      (func1 && !PyCallable_Check(func1)) ||
      (func2 && !PyCallable_Check(func2)) ) {
    return ERRSS ("pyg_register takes two function arguments.");
  }
  if (pyg_connector) { Py_DECREF (pyg_connector); }
  pyg_connector = func1;
  Py_INCREF (func1);
  if (pyg_keyhandler) { Py_DECREF (pyg_keyhandler); }
  pyg_keyhandler = func2;
  if (func2) Py_INCREF (func2);
  Py_INCREF (Py_None);
  return Py_None;
}


/* Following functions correspond to those in style.i in yorick, which allow
 * users to change style properties interactively.
 * The routines get_style and set_style are meant to be called by the user; the
 * other functions are for internal use.
 * Michiel de Hoon 2003.04.06. */

static PyObject* get_text_attributes(GpTextAttribs *attributes)
{ char *alignH;
  char *alignV;
  char *orient;
  PyObject *dictionary;
  /* Py_BuildValue does not accept unsigned long; build it separately */
  PyObject *color = PyLong_FromUnsignedLong(attributes->color);
  /* If PyLong_FromUnsignedLong fails, then Py_BuildValue will fail also.
   * Therefore, no need to check for color==NULL explicitly. */
  switch (attributes->alignH)
  { default:
    case TH_NORMAL: alignH = "normal"; break;
    case TH_LEFT: alignH = "left"; break;
    case TH_CENTER: alignH = "center"; break;
    case TH_RIGHT: alignH = "right"; break;
  }
  switch (attributes->alignV)
  { default:
    case TV_NORMAL: alignV = "normal"; break;
    case TV_TOP: alignV = "top"; break;
    case TV_CAP: alignV = "cap"; break;
    case TV_HALF: alignV = "half"; break;
    case TV_BASE: alignV = "base"; break;
    case TV_BOTTOM: alignV = "bottom"; break;
  }
  switch (attributes->orient)
  { default:
    case TX_RIGHT: orient = "right"; break;
    case TX_UP: orient = "up"; break;
    case TX_LEFT: orient = "left"; break;
    case TX_DOWN: orient = "down"; break;
  }
  dictionary =
    Py_BuildValue("{s:i,s:d,s:s,s:s,s:s,s:O}",
		    "font",attributes->font,
		    "height",attributes->height,
		    "orient",orient,
		    "alignH",alignH,
		    "alignV",alignV,
		    "color",color);
  /* Py_BuildValue with O increased the reference count of color by one */
  Py_XDECREF(color);
  return dictionary;
}

static PyObject* get_legend(GeLegendBox *legends)
{ PyObject *dictionary;
  PyObject *textStyle = get_text_attributes(&(legends->textStyle));
  /* If get_text_attributes fails, then Py_BuildValue will fail also.
   * Therefore, no need to check for textStyle==NULL explicitly. */
  dictionary =
    Py_BuildValue("{s:d,s:d,s:d,s:d,s:i,s:i,s:i,s:O}",
                    "x", legends->x,
                    "y", legends->y,
                    "dx", legends->dx,
                    "dy", legends->dy,
                    "nchars", legends->nchars,
                    "nlines", legends->nlines,
                    "nwrap", legends->nwrap,
                    "textStyle", textStyle);
  /* Py_BuildValue with O increased the reference count of textStyle by one */
  Py_XDECREF(textStyle);
  return dictionary;
}

static PyObject* get_line_attributes(GpLineAttribs *attributes)
{ PyObject *dictionary;
  /* Py_BuildValue does not accept unsigned long; build it separately */
  PyObject *color = PyLong_FromUnsignedLong(attributes->color);
  /* If PyLong_FromUnsignedLong fails, then Py_BuildValue will fail also.
   * Therefore, no need to check for color==NULL explicitly. */
  char* type;
  switch (attributes->type)
  { default:
    case L_NONE: type = "none"; break;
    case L_SOLID: type = "solid"; break;
    case L_DASH: type = "dash"; break;
    case L_DOT: type = "dot"; break;
    case L_DASHDOT: type = "dashdot"; break;
    case L_DASHDOTDOT: type = "dashdotdot"; break;
  }
  dictionary = Py_BuildValue("{s:d,s:s,s:O}",
                              "width", attributes->width,
                              "type", type,
	                      "color", color);
  /* Py_BuildValue with O increased the reference count of color by one */
  Py_XDECREF(color);
  return dictionary;
}

static PyObject* get_axis_style(GaAxisStyle *axis)
{ PyObject *dictionary;
  int dimensions[] = {TICK_LEVELS};
  /* If any of these fail, then Py_BuildValue will fail also.
   * Therefore, no need to check for NULL pointers explicitly. */
  PyObject *tickStyle = get_line_attributes(&(axis->tickStyle));
  PyObject *gridStyle = get_line_attributes(&(axis->gridStyle));
  PyObject *textStyle = get_text_attributes(&(axis->textStyle));
  PyArrayObject *tickLen =
    (PyArrayObject *)PyArray_FromDims(1,dimensions,PyArray_DOUBLE);
  if (tickLen)
  { int i;
    double* data = (double*) A_DATA(tickLen);
    for (i = 0; i < TICK_LEVELS; i++) data[i] = axis->tickLen[i];
  }
  dictionary =
    Py_BuildValue(
      "{s:d,s:d,s:d,s:d,s:i,s:i,s:i,s:d,s:d,s:d,s:d,s:O,s:O,s:O,s:O}",
		   "nMajor", axis->nMajor,
		   "nMinor", axis->nMinor,
		   "logAdjMajor", axis->logAdjMajor,
		   "logAdjMinor", axis->logAdjMinor,
		   "nDigits", axis->nDigits,
		   "gridLevel", axis->gridLevel,
		   "flags", axis->flags,
                   "tickOff", axis->tickOff,
                   "labelOff", axis->labelOff,
                   "xOver", axis->xOver,
                   "yOver", axis->yOver,
		   "tickStyle", tickStyle,
		   "gridStyle", gridStyle,
		   "textStyle", textStyle,
		   "tickLen", tickLen
		 );
  /* Py_BuildValue with O increased the reference count of color by one */
  Py_XDECREF(tickStyle);
  Py_XDECREF(gridStyle);
  Py_XDECREF(textStyle);
  Py_XDECREF(tickLen);
  return dictionary;
}

static PyObject* get_tick_style(GaTickStyle *ticks)
{ PyObject *frameStyle = get_line_attributes(&(ticks->frameStyle));
  PyObject *horizontal = get_axis_style(&(ticks->horiz));
  PyObject *vertical = get_axis_style(&(ticks->vert));
  /* If any of these fail, then Py_BuildValue will fail also.
   * Therefore, no need to check for NULL pointers explicitly. */
  PyObject *dictionary =
    Py_BuildValue("{s:i,s:O,s:O,s:O}",
		   "frame", ticks->frame,
                   "frameStyle", frameStyle,
                   "horizontal", horizontal,
                   "vertical", vertical);
  Py_XDECREF(frameStyle);
  Py_XDECREF(horizontal);
  Py_XDECREF(vertical);
  return dictionary;
}

static PyObject* get_system(GfakeSystem *systems)
{ PyObject *dictionary;
  PyObject *ticks = get_tick_style(&systems->ticks);
  /* Find the viewport */
  int dimensions[] = {4};
  PyArrayObject *viewport =
    (PyArrayObject *)PyArray_FromDims(1,dimensions,PyArray_DOUBLE);
  if (viewport)
  { int i;
    double *data = (double*) A_DATA(viewport);
    for (i = 0; i < 4; i++) data[i] = systems->viewport[i];
  }
  /* Now put all this in the dictionary */
  dictionary =
    Py_BuildValue("{s:O,s:s,s:O}",
		   "viewport", viewport,
                   "legend", systems->legend,
                   "ticks", ticks);
  Py_XDECREF(viewport);
  Py_XDECREF(ticks);
  return dictionary;
}

static PyObject* get_systems_list(int n, GfakeSystem *systems)
{ int i;
  PyObject *list = PyList_New(n);
  if (!list) return NULL;
  for (i = 0; i < n; i++)
  { PyObject *system = get_system(&(systems[i]));
    if(!system || PyList_SetItem(list,i,system)==-1)
    /* Py_DECREF applied to the list will apply Py_DECREF to its items */
    { Py_DECREF(list);
      return NULL;
    }
  }
  return list;
}

static char get_style__doc__[] =
"dictionary = get_style()\n"
"     Returns a nested dictionary that contains the style information that is\n"
"     usually stored in the style files (*.gs). By modifying values stored in\n"
"     this dictionary and calling set_style, the style of the current drawing\n"
"     can be changed. The returned dictionary has the following structure:\n"
"\n"
"     ['landscape']: Set orientation to portrait (0) or landscape (1)\n"
"     ['legend']:\n"
"       ['x']: NDC horizontal location of the legend box\n"
"       ['y']: NDC vertical location of the legend box\n"
"       ['dx']: NDC horizontal offset to 2nd column\n"
"       ['dy']: NDC vertical offset to 2nd column\n"
"       ['nchars']: Maximum number of characters on a line\n"
"       ['nlines']: Maximum number of lines\n"
"       ['nwrap']: Maximum number of lines to wrap long legends\n"
"       ['textStyle']:\n"
"         ['height']: Character height in NDC, default 0.0156 (12pt)\n"
"         ['font']: Text font, specified by an integer:\n"
"                    Courier: 0\n"
"                    Times: 4\n"
"                    Helvetica: 8\n"
"                    Symbol: 12\n"
"                    New Century: 16\n"
"                    (add 1 for bold, 2 for italic)\n"
"         ['color']: Text color\n"
"         ['orient']: Text path ('right', 'up', 'left', 'down')\n"
"         ['alignH']: Horizontal alignment\n"
"                     ('normal', 'left', 'center', 'right')\n"
"         ['alignV']: Vertical alignment\n"
"                     ('normal', 'top', 'cap', 'half', 'base', 'bottom')\n"
"     ['contourlegend']:\n"
"       ['x']: NDC horizontal location of the contour legend box\n"
"       ['y']: NDC vertical location of the contour legend box\n"
"       ['dx']: NDC horizontal offset to 2nd column in the contour legend box\n"
"       ['dy']: NDC vertical offset to 2nd column in the contour legend box\n"
"       ['nchars']: Maximum number of characters on a line\n"
"       ['nlines']: Maximum number of lines\n"
"       ['nwrap']: Maximum number of lines to wrap long legends\n"
"       ['textStyle']:\n"
"         ['height']: Character height in NDC, default 0.0156 (12pt)\n"
"         ['font']: Text font, specified by an integer:\n"
"                    Courier: 0\n"
"                    Times: 4\n"
"                    Helvetica: 8\n"
"                    Symbol: 12\n"
"                    New Century: 16\n"
"                    (add 1 for bold, 2 for italic)\n"
"         ['color']: Text color\n"
"         ['orient']: Text path ('right', 'up', 'left', 'down')\n"
"         ['alignH']: Horizontal alignment\n"
"                     ('normal', 'left', 'center', 'right')\n"
"         ['alignV']: Vertical alignment\n"
"                     ('normal', 'top', 'cap', 'half', 'base', 'bottom')\n"
"     ['systems']: returns a list of systems, each of which is a dictionary\n"
"                  with the following keys:\n"
"       ['legend']: default legend\n"
"       ['viewport']: Viewport size, array([left, right, bottom, top]) \n"
"       ['ticks']:\n"
"         ['frame']: Switch the frame on (1) or off (0)\n"
"         ['frameStyle']:\n"
"           ['color']: Color of the frame\n"
"           ['width']: Line width of the frame\n"
"           ['type']: Line style of the frame:\n"
"                   ('none', 'solid', 'dash', 'dot', 'dashdot', 'dashdotdot')\n"
"         ['horizontal']:\n"
"           ['nDigits']: Number of digits for the tick labels\n"
"           ['nMinor']: Number of minor tick marks\n"
"           ['nMajor']: Number of major tick marks\n"
"           ['logAdjMinor']: Adjustment factor for nMinor for a log scale\n"
"           ['logAdjMajor']: Adjustment factor for nMajor for a log scale\n"
"           ['flags']: Integer, given by the sum of\n"
"                       1: There are ticks at the bottom edge of the viewport\n"
"                       2: There are ticks at the upper edge of the viewport\n"
"                       4: Ticks are centered on the axis\n"
"                       8: Ticks are go inward from the axis\n"
"                      16: Ticks are go outward from the axis\n"
"                      32: There are labels at the bottom edge\n"
"                      64: There are labels at the upper edge\n"
"                     128: There is a full grid\n"
"                     256: There is a single grid line at the origin\n"
"                     512: Alternative tick generator is used\n"
"                    1024: Alternative label generator is used\n"
"           ['xOver']: Horizontal position of the overflow label\n"
"           ['yOver']: Vertical position of the overflow label\n"
"           ['labelOff']: Offset from the edge of the viewport to the labels\n"
"           ['tickOff']: Offset from the edge of the viewport to the ticks\n"
"           ['tickLen']: Tick lengths in NDC\n"
"           ['tickStyle']:\n"
"             ['color']: Color of the ticks\n"
"             ['width']: Line width of the ticks\n"
"             ['type']: Line type of the ticks\n"
"                   ('none', 'solid', 'dash', 'dot', 'dashdot', 'dashdotdot')\n"
"           ['gridLevel']: Level of the ticks at which the grid is drawn\n"
"           ['gridStyle']:\n"
"             ['color']: Color of the grid\n"
"             ['width']: Line width of the grid\n"
"             ['type']: Line type of the grid\n"
"                   ('none', 'solid', 'dash', 'dot', 'dashdot', 'dashdotdot')\n"
"         ['vertical']:\n"
"           ['nDigits']: Number of digits for the tick labels\n"
"           ['nMinor']: Number of minor tick marks\n"
"           ['nMajor']: Number of major tick marks\n"
"           ['logAdjMinor']: Adjustment factor for nMinor for a log scale\n"
"           ['logAdjMajor']: Adjustment factor for nMajor for a log scale\n"
"           ['flags']: Integer, given by the sum of\n"
"                       1: There are ticks at the left edge of the viewport\n"
"                       2: There are ticks at the right edge of the viewport\n"
"                       4: Ticks are centered on the axis\n"
"                       8: Ticks are go inward from the axis\n"
"                      16: Ticks are go outward from the axis\n"
"                      32: There are labels at the left edge\n"
"                      64: There are labels at the right edge\n"
"                     128: There is a full grid\n"
"                     256: There is a single grid line at the origin\n"
"                     512: Alternative tick generator is used\n"
"                    1024: Alternative label generator is used\n"
"           ['xOver']: Horizontal position of the overflow label\n"
"           ['yOver']: Vertical position of the overflow label\n"
"           ['labelOff']: Offset from the edge of the viewport to the labels\n"
"           ['tickOff']: Offset from the edge of the viewport to the ticks\n"
"           ['tickLen']: Tick lengths in NDC\n"
"           ['tickStyle']:\n"
"             ['color']: Color of the ticks\n"
"             ['width']: Line width of the ticks\n"
"             ['type']: Line type of the ticks\n"
"                   ('none', 'solid', 'dash', 'dot', 'dashdot', 'dashdotdot')\n"
"           ['gridLevel']: Level of the ticks at which the grid is drawn\n"
"           ['gridStyle']:\n"
"             ['color']: Color of the grid\n"
"             ['width']: Line width of the grid\n"
"             ['type']: Line type of the grid\n"
"                   ('none', 'solid', 'dash', 'dot', 'dashdot', 'dashdotdot')\n"
"\n"
"   SEE ALSO: set_style\n";

static PyObject *get_style (PyObject * self, PyObject * args)
{
  int i;
  int n;
  int landscape;
  GeLegendBox legendboxes[2];
  GfakeSystem *fakesystems;
  PyObject *dictionary; /* return value */
  PyObject *legends[2];
  PyObject *systems;
  if (!PyArg_ParseTuple(args, "")) return NULL;
  /* Call the Gist function first to find out how many systems there are */
  n = raw_style(0, &landscape, 0, 0);
  if (n==0) return ERRSS ("no current drawing");
  if (n==-1) return ERRSS ("unknown error in raw_style");
  /* Now that we know how many systems there are, save enough space for them */
  fakesystems = (GfakeSystem*)malloc(n*sizeof(GfakeSystem));
  if(!fakesystems) return PyErr_NoMemory();
  for (i = 0; i < n; i++) fakesystems[i].legend = 0;
  /* Call the Gist function to get the style information */
  if (raw_style(0, &landscape, fakesystems, legendboxes)==-1)
    return ERRSS ("unknown error in raw_style");
  /* Put the style information in a dictionary */
  legends[0] = get_legend(&(legendboxes[0]));
  legends[1] = get_legend(&(legendboxes[1]));
  systems = get_systems_list(n, fakesystems);
  dictionary =
    Py_BuildValue("{s:i,s:O,s:O,s:O}",
                   "landscape", landscape,
                   "legend", legends[0],
                   "contourlegend", legends[1],
                   "systems", systems);
  /* Free the allocated memory */
  for (i = 0; i < n; i++) p_free(fakesystems[i].legend);
  free(fakesystems);
  Py_XDECREF(legends[0]);
  Py_XDECREF(legends[1]);
  Py_XDECREF(systems);
  if(!dictionary) return PyErr_NoMemory();
  return dictionary;
}

/*----------------------------------------------------------------------------*/

int set_line_attributes(PyObject *dictionary, GpLineAttribs *attributes)
{ PyObject *width;
  PyObject *color;
  PyObject *type;
  char* ctype;
  /*--------------------------------------------------------------------------*/
  width = PyDict_GetItemString(dictionary,"width");
  if (!width)
  { ERRMSG("key width not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  type = PyDict_GetItemString(dictionary,"type");
  if (!type)
  { ERRMSG("key type not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  color = PyDict_GetItemString(dictionary,"color");
  if (!color)
  { ERRMSG("key color not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  /* OK, we've got everything */
  if (PyFloat_Check(width)) attributes->width = PyFloat_AsDouble(width);
  else if (PyInt_Check(width)) attributes->width = (double) PyInt_AsLong(width);
  else
  { ERRMSG("width should be of type Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (!PyString_Check(type))
  { ERRMSG("type should be of type String");
    return 0;
  }
  ctype = PyString_AsString(type);
  if(!strcmp(ctype,"none")) attributes->type = L_NONE;
  else if (!strcmp(ctype,"solid")) attributes->type = L_SOLID;
  else if (!strcmp(ctype,"dash")) attributes->type = L_DASH;
  else if (!strcmp(ctype,"dot")) attributes->type = L_DOT;
  else if (!strcmp(ctype,"dashdot")) attributes->type = L_DASHDOT;
  else if (!strcmp(ctype,"dashdotdot")) attributes->type = L_DASHDOTDOT;
  else
  { ERRMSG("unrecognized value for type");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (PyInt_Check(color))
  { long lcolor = PyInt_AsLong(color);
    if (lcolor<0)
    { ERRMSG("number for color should be non-negative");
      return 0;
    }
    attributes->color = (unsigned long)lcolor;
  }
  else if (PyLong_Check(color))
    attributes->color = PyLong_AsUnsignedLong(color);
  else
  { ERRMSG("color should be a non-negative number");
    return 0;
  }
  return 1;
}

int set_text_attributes(PyObject* dictionary, GpTextAttribs *attributes)
{ PyObject *font;
  PyObject *height;
  PyObject *orient;
  PyObject *alignH;
  PyObject *alignV;
  PyObject *color;
  char* salignH;
  char* salignV;
  char* sorient;
  /*--------------------------------------------------------------------------*/
  font = PyDict_GetItemString(dictionary,"font");
  if (!font)
  { ERRMSG("key font not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  height = PyDict_GetItemString(dictionary,"height");
  if (!height)
  { ERRMSG("key height not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  orient = PyDict_GetItemString(dictionary,"orient");
  if (!orient)
  { ERRMSG("key orient not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  alignH = PyDict_GetItemString(dictionary,"alignH");
  if (!alignH)
  { ERRMSG("key alignH not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  alignV = PyDict_GetItemString(dictionary,"alignV");
  if (!alignV)
  { ERRMSG("key alignV not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  color = PyDict_GetItemString(dictionary,"color");
  if (!color)
  { ERRMSG("key color not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (!PyInt_Check(font))
  { ERRMSG("font should be an Integer");
    return 0;
  }
  attributes->font = (int) PyInt_AsLong(font);
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(height))
    attributes->height = PyFloat_AsDouble(height);
  else if (PyInt_Check(height))
    attributes->height = (double) PyInt_AsLong(height);
  else
  { ERRMSG("height should be a Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (!PyString_Check(orient))
  { ERRMSG("orient should be a String");
    return 0;
  }
  sorient = PyString_AsString(orient);
  if (!strcmp(sorient,"right")) attributes->orient = TX_RIGHT;
  else if (!strcmp(sorient,"up")) attributes->orient = TX_UP;
  else if (!strcmp(sorient,"left")) attributes->orient = TX_LEFT;
  else if (!strcmp(sorient,"down")) attributes->orient = TX_DOWN;
  else
  { ERRMSG("orient should be 'right', 'up', 'left', or 'down'");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (!PyString_Check(alignH))
  { ERRMSG("alignH should be a String");
    return 0;
  }
  salignH = PyString_AsString(alignH);
  if (!strcmp(salignH,"normal")) attributes->alignH = TH_NORMAL;
  else if (!strcmp(salignH,"left")) attributes->alignH = TH_LEFT;
  else if (!strcmp(salignH,"center")) attributes->alignH = TH_CENTER;
  else if (!strcmp(salignH,"right")) attributes->alignH = TH_RIGHT;
  else
  { ERRMSG("alignH should be 'normal', 'left', 'center', or 'right'");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (!PyString_Check(alignV))
  { ERRMSG("alignV should be a String");
    return 0;
  }
  salignV = PyString_AsString(alignV);
  if (!strcmp(salignV,"normal")) attributes->alignV = TV_NORMAL;
  else if (!strcmp(salignV,"top")) attributes->alignV = TV_TOP;
  else if (!strcmp(salignV,"cap")) attributes->alignV = TV_CAP;
  else if (!strcmp(salignV,"half")) attributes->alignV = TV_HALF;
  else if (!strcmp(salignV,"base")) attributes->alignV = TV_BASE;
  else if (!strcmp(salignV,"bottom")) attributes->alignV = TV_BOTTOM;
  else
  { ERRMSG("alignV should be 'normal', 'top', 'cap', 'half', 'base', or 'bottom'");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (PyInt_Check(color))
  { long lcolor = PyInt_AsLong(color);
    if (lcolor<0)
    { ERRMSG("number for color should be non-negative");
      return 0;
    }
    attributes->color = (unsigned long)lcolor;
  }
  else if (PyLong_Check(color))
    attributes->color = PyLong_AsUnsignedLong(color);
  else
  { ERRMSG("color should be a non-negative number");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  return 1;
}

int set_axis_style(PyObject *dictionary, GaAxisStyle *axis)
{ int i;
  double *data;
  PyObject *nMajor;
  PyObject *nMinor;
  PyObject *logAdjMajor;
  PyObject *logAdjMinor;
  PyObject *nDigits;
  PyObject *gridLevel;
  PyObject *flags;
  PyObject *tickOff;
  PyObject *labelOff;
  PyObject *xOver;
  PyObject *yOver;
  PyObject *tickStyle;
  PyObject *gridStyle;
  PyObject *textStyle;
  PyArrayObject *tickLen;
  /*--------------------------------------------------------------------------*/
  nMajor = PyDict_GetItemString(dictionary,"nMajor");
  if (!nMajor)
  { ERRMSG("key nMajor not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  nMinor = PyDict_GetItemString(dictionary,"nMinor");
  if (!nMinor)
  { ERRMSG("key nMinor not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  logAdjMajor = PyDict_GetItemString(dictionary,"logAdjMajor");
  if (!logAdjMajor)
  { ERRMSG("key logAdjMajor not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  logAdjMinor = PyDict_GetItemString(dictionary,"logAdjMinor");
  if (!logAdjMinor)
  { ERRMSG("key logAdjMinor not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  nDigits = PyDict_GetItemString(dictionary,"nDigits");
  if (!nDigits)
  { ERRMSG("key nDigits not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  gridLevel = PyDict_GetItemString(dictionary,"gridLevel");
  if (!gridLevel)
  { ERRMSG("key gridLevel not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  flags = PyDict_GetItemString(dictionary,"flags");
  if (!flags)
  { ERRMSG("key flags not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  tickOff = PyDict_GetItemString(dictionary,"tickOff");
  if (!tickOff)
  { ERRMSG("key tickOff not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  labelOff = PyDict_GetItemString(dictionary,"labelOff");
  if (!labelOff)
  { ERRMSG("key labelOff not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  xOver = PyDict_GetItemString(dictionary,"xOver");
  if (!xOver)
  { ERRMSG("key xOver not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  yOver = PyDict_GetItemString(dictionary,"yOver");
  if (!yOver)
  { ERRMSG("key yOver not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  tickStyle = PyDict_GetItemString(dictionary,"tickStyle");
  if (!tickStyle)
  { ERRMSG("key tickStyle not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  textStyle = PyDict_GetItemString(dictionary,"textStyle");
  if (!textStyle)
  { ERRMSG("key textStyle not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  gridStyle = PyDict_GetItemString(dictionary,"gridStyle");
  if (!gridStyle)
  { ERRMSG("key gridStyle not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  tickLen = (PyArrayObject*) PyDict_GetItemString(dictionary,"tickLen");
  if (!tickLen)
  { ERRMSG("key tickLen not found in dictionary");
    return 0;
  }
  /* OK, we've got everything */
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(nMajor)) axis->nMajor = PyFloat_AsDouble(nMajor);
  else if (PyInt_Check(nMajor)) axis->nMajor = (double) PyInt_AsLong(nMajor);
  else
  { ERRMSG("nMajor should be of type Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(nMinor)) axis->nMinor = PyFloat_AsDouble(nMinor);
  else if (PyInt_Check(nMinor)) axis->nMinor = (double) PyInt_AsLong(nMinor);
  else
  { ERRMSG("nMinor should be of type Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(logAdjMajor))
    axis->logAdjMajor = PyFloat_AsDouble(logAdjMajor);
  else if (PyInt_Check(logAdjMajor))
    axis->logAdjMajor = (double) PyInt_AsLong(logAdjMajor);
  else
  { ERRMSG("logAdjMajor should be of type Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(logAdjMinor))
    axis->logAdjMinor = PyFloat_AsDouble(logAdjMinor);
  else if (PyInt_Check(logAdjMinor))
    axis->logAdjMinor = (double) PyInt_AsLong(logAdjMinor);
  else
  { ERRMSG("logAdjMinor should be of type Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (!PyInt_Check(nDigits))
  { ERRMSG("nDigits should be of type Integer");
    return 0;
  }
  axis->nDigits = (int)PyInt_AsLong(nDigits);
  /*--------------------------------------------------------------------------*/
  if (!PyInt_Check(gridLevel))
  { ERRMSG("gridLevel should be of type Integer");
    return 0;
  }
  axis->gridLevel = (int)PyInt_AsLong(gridLevel);
  /*--------------------------------------------------------------------------*/
  if (!PyInt_Check(flags))
  { ERRMSG("flags should be of type Integer");
    return 0;
  }
  axis->flags = (int)PyInt_AsLong(flags);
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(tickOff)) axis->tickOff = PyFloat_AsDouble(tickOff);
  else if (PyInt_Check(tickOff)) axis->tickOff = (double) PyInt_AsLong(tickOff);
  else
  { ERRMSG("tickOff should be of type Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(labelOff))
    axis->labelOff = PyFloat_AsDouble(labelOff);
  else if (PyInt_Check(labelOff))
    axis->labelOff = (double) PyInt_AsLong(labelOff);
  else
  { ERRMSG("labelOff should be of type Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(xOver)) axis->xOver = PyFloat_AsDouble(xOver);
  else if (PyInt_Check(xOver)) axis->xOver = (double) PyInt_AsLong(xOver);
  else
  { ERRMSG("xOver should be of type Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(yOver)) axis->yOver = PyFloat_AsDouble(yOver);
  else if (PyInt_Check(yOver)) axis->yOver = (double) PyInt_AsLong(yOver);
  else
  { ERRMSG("yOver should be of type Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if(!set_line_attributes(tickStyle,&(axis->tickStyle))) return 0;
  if(!set_text_attributes(textStyle,&(axis->textStyle))) return 0;
  if(!set_line_attributes(gridStyle,&(axis->gridStyle))) return 0;
  /*--------------------------------------------------------------------------*/
  /* Get the tick lengths from the array */
  if (!PyArray_Check(tickLen))
  { ERRMSG("tickLen is not a NumPy array");
    return 0;
  }
  if (tickLen->nd != 1)
  { ERRMSG("tickLen should be one-dimensional");
    return 0;
  }
  if (tickLen->descr->type_num != PyArray_DOUBLE)
  { ERRMSG("tickLen array should be of type float");
    return 0;
  }
  if(tickLen->dimensions[0]!=TICK_LEVELS)
  { ERRMSG("tickLen array has incorrect length");
    return 0;
  }
  data = (double*) A_DATA(tickLen);
    for (i = 0; i < TICK_LEVELS; i++) axis->tickLen[i] = data[i];
  return 1;
}

int set_tick_style(PyObject *dictionary, GaTickStyle *ticks)
{ PyObject *frame;
  PyObject *frameStyle;
  PyObject *horizontal;
  PyObject *vertical;
  /*--------------------------------------------------------------------------*/
  frame = PyDict_GetItemString(dictionary,"frame");
  if (!frame)
  { ERRMSG("key frame not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  frameStyle = PyDict_GetItemString(dictionary,"frameStyle");
  if (!frameStyle)
  { ERRMSG("key frameStyle not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  horizontal = PyDict_GetItemString(dictionary,"horizontal");
  if (!horizontal)
  { ERRMSG("key horizontal not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  vertical = PyDict_GetItemString(dictionary,"vertical");
  if (!vertical)
  { ERRMSG("key vertical not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  /* OK, we've got everything */
  if (!PyInt_Check(frame))
  { ERRMSG("frame should be of type integer");
    return 0;
  }
  ticks->frame = (int) PyInt_AsLong(frame);
  /*--------------------------------------------------------------------------*/
  if(!set_line_attributes(frameStyle,&(ticks->frameStyle))) return 0;
  if(!set_axis_style(horizontal,&(ticks->horiz))) return 0;
  if(!set_axis_style(vertical,&(ticks->vert))) return 0;
  /*--------------------------------------------------------------------------*/
  return 1;
}

int set_system(PyObject *dictionary, GfakeSystem *system)
{ int i;
  PyObject *ticks;
  PyObject *legend;
  PyArrayObject *viewport;
  double *data;
  /*--------------------------------------------------------------------------*/
  ticks = PyDict_GetItemString(dictionary,"ticks");
  if (!ticks)
  { ERRMSG("key ticks not found in dictionary in style");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  legend = PyDict_GetItemString(dictionary,"legend");
  if (!legend)
  { ERRMSG("key legend not found in dictionary in style");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  viewport = (PyArrayObject*) PyDict_GetItemString(dictionary,"viewport");
  if (!viewport)
  { ERRMSG("key viewport not found in dictionary in style");
    return 0;
  }
  /* OK, we've got everything */
  /*--------------------------------------------------------------------------*/
  if(!set_tick_style(ticks,&(system->ticks))) return 0;
  /* PyString_AsString returns a char pointer to the data inside legend.
   * So system->legend should not be freed later on */
  if (!PyString_Check(legend))
  { ERRMSG("legend should be a string");
    return 0;
  }
  system->legend = PyString_AsString(legend);
  /*--------------------------------------------------------------------------*/
  /* Find the viewport */
  if (!PyArray_Check(viewport))
  { ERRMSG("viewport is not a NumPy array");
    return 0;
  }
  if (viewport->nd != 1)
  { ERRMSG("viewport should be one-dimensional");
    return 0;
  }
  if (viewport->descr->type_num != PyArray_DOUBLE)
  { ERRMSG("viewport array should be of type float");
    return 0;
  }
  if(viewport->dimensions[0]!=4)
  { ERRMSG("viewport array should have length 4");
    return 0;
  }
  data = (double*) A_DATA(viewport);
    for (i = 0; i < 4; i++) system->viewport[i] = data[i];
  /*--------------------------------------------------------------------------*/
  return 1;
}

int set_systems_list(PyObject *list, int n, GfakeSystem systems[])
{ int i;
  for (i = 0; i < n; i++)
  { PyObject *system = PyList_GetItem(list,i);
    if(!system)
    { ERRMSG("Error retrieving system from list in style dictionary");
      return 0;
    }
    if(!set_system(system,&(systems[i]))) return 0;
  }
  return 1;
}

int set_legend(PyObject *dictionary, GeLegendBox *legend)
{ PyObject *x;
  PyObject *y;
  PyObject *dx;
  PyObject *dy;
  PyObject *nchars;
  PyObject *nlines;
  PyObject *nwrap;
  PyObject *textStyle;
  /*--------------------------------------------------------------------------*/
  x = PyDict_GetItemString(dictionary,"x");
  if (!x)
  { ERRMSG("key x not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  y = PyDict_GetItemString(dictionary,"y");
  if (!y)
  { ERRMSG("key y not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  dx = PyDict_GetItemString(dictionary,"dx");
  if (!dx)
  { ERRMSG("key dx not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  dy = PyDict_GetItemString(dictionary,"dy");
  if (!dy)
  { ERRMSG("key dy not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  nchars = PyDict_GetItemString(dictionary,"nchars");
  if (!nchars)
  { ERRMSG("key nchars not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  nlines = PyDict_GetItemString(dictionary,"nlines");
  if (!nlines)
  { ERRMSG("key nlines not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  nwrap = PyDict_GetItemString(dictionary,"nwrap");
  if (!nwrap)
  { ERRMSG("key nwrap not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  textStyle = PyDict_GetItemString(dictionary,"textStyle");
  if (!textStyle)
  { ERRMSG("key textStyle not found in dictionary");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(x)) legend->x = PyFloat_AsDouble(x);
  else if (PyInt_Check(x)) legend->x = (double) PyInt_AsLong(x);
  else
  { ERRMSG("x should be a Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(y)) legend->y = PyFloat_AsDouble(y);
  else if (PyInt_Check(y)) legend->y = (double) PyInt_AsLong(y);
  else
  { ERRMSG("y should be a Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(dx)) legend->dx = PyFloat_AsDouble(dx);
  else if (PyInt_Check(dx)) legend->dx = (double) PyInt_AsLong(dx);
  else
  { ERRMSG("dx should be a Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (PyFloat_Check(dy)) legend->dy = PyFloat_AsDouble(dy);
  else if (PyInt_Check(dy)) legend->dy = (double) PyInt_AsLong(dy);
  else
  { ERRMSG("dy should be a Float");
    return 0;
  }
  /*--------------------------------------------------------------------------*/
  if (!PyInt_Check(nchars))
  { ERRMSG("nchars should be an Integer");
    return 0;
  }
  legend->nchars = (int) PyInt_AsLong(nchars);
  /*--------------------------------------------------------------------------*/
  if (!PyInt_Check(nlines))
  { ERRMSG("nlines should be an Integer");
    return 0;
  }
  legend->nlines = (int) PyInt_AsLong(nlines);
  /*--------------------------------------------------------------------------*/
  if (!PyInt_Check(nwrap))
  { ERRMSG("nwrap should be an Integer");
    return 0;
  }
  legend->nwrap = (int) PyInt_AsLong(nwrap);
  /*--------------------------------------------------------------------------*/
  if(!set_text_attributes(textStyle, &(legend->textStyle))) return 0;
  /*--------------------------------------------------------------------------*/
  return 1;
}

static char set_style__doc__[] =
"set_style(dictionary)\n"
"     Takes a nested dictionary containing the (possibly modified) style\n"
"     information that is usually stored in the style files (*.gs), and\n"
"     applies it to the current plot. A starting point for the dictionary can\n"
"     be obtained by calling get_style. The nested dictionary has the\n"
"     following structure:\n"
"\n"
"     ['landscape']: Set orientation to portrait (0) or landscape (1)\n"
"     ['legend']:\n"
"       ['x']: NDC horizontal location of the legend box\n"
"       ['y']: NDC vertical location of the legend box\n"
"       ['dx']: NDC horizontal offset to 2nd column\n"
"       ['dy']: NDC vertical offset to 2nd column\n"
"       ['nchars']: Maximum number of characters on a line\n"
"       ['nlines']: Maximum number of lines\n"
"       ['nwrap']: Maximum number of lines to wrap long legends\n"
"       ['textStyle']:\n"
"         ['height']: Character height in NDC, default 0.0156 (12pt)\n"
"         ['font']: Text font, specified by an integer:\n"
"                    Courier: 0\n"
"                    Times: 4\n"
"                    Helvetica: 8\n"
"                    Symbol: 12\n"
"                    New Century: 16\n"
"                    (add 1 for bold, 2 for italic)\n"
"         ['color']: Text color\n"
"         ['orient']: Text path ('right', 'up', 'left', 'down')\n"
"         ['alignH']: Horizontal alignment\n"
"                     ('normal', 'left', 'center', 'right')\n"
"         ['alignV']: Vertical alignment\n"
"                     ('normal', 'top', 'cap', 'half', 'base', 'bottom')\n"
"     ['contourlegend']:\n"
"       ['x']: NDC horizontal location of the contour legend box\n"
"       ['y']: NDC vertical location of the contour legend box\n"
"       ['dx']: NDC horizontal offset to 2nd column in the contour legend box\n"
"       ['dy']: NDC vertical offset to 2nd column in the contour legend box\n"
"       ['nchars']: Maximum number of characters on a line\n"
"       ['nlines']: Maximum number of lines\n"
"       ['nwrap']: Maximum number of lines to wrap long legends\n"
"       ['textStyle']:\n"
"         ['height']: Character height in NDC, default 0.0156 (12pt)\n"
"         ['font']: Text font, specified by an integer:\n"
"                    Courier: 0\n"
"                    Times: 4\n"
"                    Helvetica: 8\n"
"                    Symbol: 12\n"
"                    New Century: 16\n"
"                    (add 1 for bold, 2 for italic)\n"
"         ['color']: Text color\n"
"         ['orient']: Text path ('right', 'up', 'left', 'down')\n"
"         ['alignH']: Horizontal alignment\n"
"                     ('normal', 'left', 'center', 'right')\n"
"         ['alignV']: Vertical alignment\n"
"                     ('normal', 'top', 'cap', 'half', 'base', 'bottom')\n"
"     ['systems']: returns a list of systems, each of which is a dictionary\n"
"                  with the following keys:\n"
"       ['legend']: default legend\n"
"       ['viewport']: Viewport size, array([left, right, bottom, top]) \n"
"       ['ticks']:\n"
"         ['frame']: Switch the frame on (1) or off (0)\n"
"         ['frameStyle']:\n"
"           ['color']: Color of the frame\n"
"           ['width']: Line width of the frame\n"
"           ['type']: Line style of the frame:\n"
"                   ('none', 'solid', 'dash', 'dot', 'dashdot', 'dashdotdot')\n"
"         ['horizontal']:\n"
"           ['nDigits']: Number of digits for the tick labels\n"
"           ['nMinor']: Number of minor tick marks\n"
"           ['nMajor']: Number of major tick marks\n"
"           ['logAdjMinor']: Adjustment factor for nMinor for a log scale\n"
"           ['logAdjMajor']: Adjustment factor for nMajor for a log scale\n"
"           ['flags']: Integer, given by the sum of"
"                       1: There are ticks at the bottom edge of the viewport\n"
"                       2: There are ticks at the upper edge of the viewport\n"
"                       4: Ticks are centered on the axis\n"
"                       8: Ticks are go inward from the axis\n"
"                      16: Ticks are go outward from the axis\n"
"                      32: There are labels at the bottom edge\n"
"                      64: There are labels at the upper edge\n"
"                     128: There is a full grid\n"
"                     256: There is a single grid line at the origin\n"
"                     512: Alternative tick generator is used\n"
"                    1024: Alternative label generator is used\n"
"           ['xOver']: Horizontal position of the overflow label\n"
"           ['yOver']: Vertical position of the overflow label\n"
"           ['labelOff']: Offset from the edge of the viewport to the labels\n"
"           ['tickOff']: Offset from the edge of the viewport to the ticks\n"
"           ['tickLen']: Tick lengths in NDC\n"
"           ['tickStyle']:\n"
"             ['color']: Color of the ticks\n"
"             ['width']: Line width of the ticks\n"
"             ['type']: Line type of the ticks\n"
"                   ('none', 'solid', 'dash', 'dot', 'dashdot', 'dashdotdot')\n"
"           ['gridLevel']: Level of the ticks at which the grid is drawn\n"
"           ['gridStyle']:\n"
"             ['color']: Color of the grid\n"
"             ['width']: Line width of the grid\n"
"             ['type']: Line type of the grid\n"
"                   ('none', 'solid', 'dash', 'dot', 'dashdot', 'dashdotdot')\n"
"         ['vertical']:\n"
"           ['nDigits']: Number of digits for the tick labels\n"
"           ['nMinor']: Number of minor tick marks\n"
"           ['nMajor']: Number of major tick marks\n"
"           ['logAdjMinor']: Adjustment factor for nMinor for a log scale\n"
"           ['logAdjMajor']: Adjustment factor for nMajor for a log scale\n"
"           ['flags']: Integer, given by the sum of"
"                       1: There are ticks at the left edge of the viewport\n"
"                       2: There are ticks at the right edge of the viewport\n"
"                       4: Ticks are centered on the axis\n"
"                       8: Ticks are go inward from the axis\n"
"                      16: Ticks are go outward from the axis\n"
"                      32: There are labels at the left edge\n"
"                      64: There are labels at the right edge\n"
"                     128: There is a full grid\n"
"                     256: There is a single grid line at the origin\n"
"                     512: Alternative tick generator is used\n"
"                    1024: Alternative label generator is used\n"
"           ['xOver']: Horizontal position of the overflow label\n"
"           ['yOver']: Vertical position of the overflow label\n"
"           ['labelOff']: Offset from the edge of the viewport to the labels\n"
"           ['tickOff']: Offset from the edge of the viewport to the ticks\n"
"           ['tickLen']: Tick lengths in NDC\n"
"           ['tickStyle']:\n"
"             ['color']: Color of the ticks\n"
"             ['width']: Line width of the ticks\n"
"             ['type']: Line type of the ticks\n"
"                   ('none', 'solid', 'dash', 'dot', 'dashdot', 'dashdotdot')\n"
"           ['gridLevel']: Level of the ticks at which the grid is drawn\n"
"           ['gridStyle']:\n"
"             ['color']: Color of the grid\n"
"             ['width']: Line width of the grid\n"
"             ['type']: Line type of the grid\n"
"                   ('none', 'solid', 'dash', 'dot', 'dashdot', 'dashdotdot')\n"
"\n"
"   SEE ALSO: get_style\n";

static PyObject *set_style (PyObject * self, PyObject * args)
{
  PyObject *dictionary;
  PyObject *landscape;
  PyObject *systems;
  PyObject *contourlegend;
  PyObject *legend;
  long n;
  GeLegendBox legendboxes[2];
  GfakeSystem *fakesystems;
  int ilandscape;
  int ok = PyArg_ParseTuple(args,"O!",&PyDict_Type,&dictionary);
  if (!ok) return NULL;
  /* PyDict_GetItemString borrows the reference -- don't Py_DECREF */
  landscape = PyDict_GetItemString(dictionary,"landscape");
  if (!landscape) return ERRSS("key landscape not found in dictionary");
  systems = PyDict_GetItemString(dictionary,"systems");
  if (!systems) return ERRSS("key systems not found in dictionary");
  legend = PyDict_GetItemString(dictionary,"legend");
  if (!legend) return ERRSS("key legend not found in dictionary");
  contourlegend = PyDict_GetItemString(dictionary,"contourlegend");
  if (!contourlegend)
    return ERRSS("key contourlegend not found in dictionary");
  if (!PyInt_Check(landscape))
    return ERRSS("landscape should be of type integer");
  ilandscape = (int) PyInt_AsLong(landscape);
  n = PyList_Size(systems);
  fakesystems = (GfakeSystem*)malloc(n*sizeof(GfakeSystem));
  if(!set_systems_list(systems, n, fakesystems)) return NULL;
  if(!set_legend(legend,&legendboxes[0])) return NULL;
  if(!set_legend(contourlegend,&legendboxes[1])) return NULL;
  if(raw_style(n,&ilandscape,fakesystems,legendboxes)==-1)
    return ERRSS("unknown error in raw_style");
  free(fakesystems);
  return PyInt_FromLong(n);
}


#define Py_Try(BOOLEAN) {if (!(BOOLEAN)) return NULL;}
#define GET_ARR2(ap,op,type,dim) \
  Py_Try(ap=(PyArrayObject *)PyArray_ContiguousFromObject(op,type,dim,dim))
#define SETERR(s) if(!PyErr_Occurred()) ERRSS(s)

static char arr_find_mask__doc__ [] =
""
;

static PyObject * arr_find_mask (PyObject * self, PyObject * args)
{
    /* find_mask (fs, node_edges): This function is used to calculate
       a mask whose corresponding entry is 1 precisely if an edge
       of a cell is cut by an isosurface, i. e., if the function
       fs is one on one of the two vertices of an edge and zero
       on the other (fs = 1 represents where some function on
       the mesh was found to be negative by the calling routine).
       fs is ntotal by nv, where nv is the number of vertices
       of a cell (4 for a tetrahedren, 5 for a pyramid, 6 for a prism).
       node_edges is a nv by ne array, where ne is the number of
       edges on a cell (6 for a tet, 8 for a pyramid, 9 for a prism).
       The entries in each row are 1 precisely if the corresponding edge
       is incident on the vertex. The exclusive or of the rows
       which correspond to nonzero entries in fs contains 1 in
       entries corresponding to edges where fs has opposite values
       on the vertices.                                            */

    PyObject * fso, * node_edgeso ;
    PyArrayObject * fsa, * node_edgesa, * maska ;
    int * fs, * node_edges, * mask ;
    int i, j, k, l, ll, ifs, imask, ntotal, ne, nv, ans_size ;

    Py_Try (PyArg_ParseTuple ( args, "OO", & fso, & node_edgeso ) ) ;
    GET_ARR2 (fsa, fso, PyArray_INT, 2) ;
    GET_ARR2 (node_edgesa, node_edgeso, PyArray_INT, 2) ;
    fs = (int *) A_DATA (fsa) ;
    node_edges = (int *) A_DATA (node_edgesa) ;
    ntotal = A_DIM (fsa, 0) ;
    nv = A_DIM (fsa, 1) ;
    if ( nv != A_DIM (node_edgesa, 0) ) {
	SETERR ("2nd dimension of 1st arg and 1st dimension of 2nd not equal.");
	Py_DECREF (fsa) ;
	Py_DECREF (node_edgesa) ;
	return (NULL) ;
    }
    ne = A_DIM (node_edgesa, 1) ;
    ans_size = ntotal * ne ;
    Py_Try (maska = (PyArrayObject *) PyArray_FromDims
	    (1, & ans_size, PyArray_INT)) ;
    mask = (int *) A_DATA (maska) ;

    for (i = 0, ifs = 0, imask = 0 ; i < ntotal ;
	 i ++, imask += ne, ifs += nv) {
	for (j = ifs, k = 0; k < nv; j ++, k ++) {
	    if ( fs [j] ) {
		for ( l = imask , ll = 0; ll < ne ; l ++ , ll ++) {
		    mask [l] ^= node_edges [j % nv * ne + ll] ;
		}
            }
	}
    }

    return PyArray_Return (maska) ;

}

/*
 *  10/30/01 llc Moved PyMethodDef to end, after doc strings are defined.
 *               Also move initgistC, which uses gist_methods.
 */

static struct PyMethodDef gist_methods[] =
{ 
  { "animate",        PYCF   animate,        1,     animate__doc__ },
  { "bytscl",         PYCFWK bytscl,         KWFLG, bytscl__doc__ },
  { "contour",        PYCFWK contour,        KWFLG, contour__doc__ },
  { "current_window", PYCF   current_window, 1,     current_window__doc__ },
  { "debug_array",    PYCF   debug_array,    1,     debug_array__doc__ },
  { "find_mask",      PYCF   arr_find_mask,  1,     arr_find_mask__doc__},
  { "fma",            PYCF   pyg_fma,        1,     fma__doc__ },
  { "gridxy",         PYCFWK gridxy,         KWFLG, gridxy__doc__ },
  { "get_slice2_precision", PYCF get_slice2_precision, 1, get_slice2_precision__doc__ },
  { "hcp",            PYCF   hcp,            1,     hcp__doc__ },
  { "hcp_file",       PYCFWK hcp_file,       KWFLG, hcp_file__doc__ },
  { "hcp_finish",     PYCF   hcp_finish,     1,     hcp_finish__doc__ },
  { "hcpoff",         PYCF   hcpoff,         1,     hcpoff__doc__ },
  { "hcpon",          PYCF   hcpon,          1,     hcpon__doc__ },
  { "limits",         PYCFWK limits,         KWFLG, limits__doc__ },
  { "logxy",          PYCF   logxy,          1,     logxy__doc__ },
  { "mesh_loc",       PYCF   mesh_loc,       1,     mesh_loc__doc__ },
  { "mfit",           PYCF   mfit,           1,     mfit__doc__ },
  { "mouse",          PYCF   mouse,          1,     mouse__doc__ },
  { "palette",        PYCFWK palette,        KWFLG, palette__doc__ },
  { "pause",          PYCF   pyg_pause,      1,     pause__doc__ },
  { "plc",            PYCFWK plc,            KWFLG, plc__doc__ },
  { "pldefault",      PYCFWK pldefault,      KWFLG, pldefault__doc__ },
  { "pldj",           PYCFWK pldj,           KWFLG, pldj__doc__ },
  { "pledit",         PYCFWK pledit,         KWFLG, pledit__doc__ },
  { "plf",            PYCFWK plf,            KWFLG, plf__doc__ },
  { "plfp",           PYCFWK plfp,           KWFLG, plfp__doc__ },
  { "plg",            PYCFWK plg,            KWFLG, plg__doc__ },
  { "pli",            PYCFWK pli,            KWFLG, pli__doc__ },
  { "plm",            PYCFWK plm,            KWFLG, plm__doc__ },
  { "plmesh",         PYCFWK plmesh,         KWFLG, plmesh__doc__ },
  { "plq",            PYCF   plq,            1,     plq__doc__ },
  { "plremove",       PYCF   plremove,       1,     plremove__doc__ },
  { "plsys",          PYCF   plsys,          1,     plsys__doc__ },
  { "plt",            PYCFWK plt,            KWFLG, plt__doc__ },
  { "plv",            PYCFWK plv,            KWFLG, plv__doc__ },
  { "redraw",         PYCF   redraw,         1,     redraw__doc__ },
  { "set_slice2_precision", PYCF set_slice2_precision, 1, set_slice2_position__doc__ },
  { "slice2",         PYCF   slice2,         1,     slice2__doc__ },
  { "unzoom",         PYCF   unzoom,         1,     unzoom__doc__ },
  { "viewport",       PYCF   viewport,       1,     viewport__doc__ },
  { "window",         PYCFWK window,         KWFLG, window__doc__ },
  { "zoom_factor",    PYCF   zoom_factor,    1,     zoom_factor__doc__ },
  { "pyg_unhook",     PYCF   pyg_unhook,     1,     pyg_unhook__doc__ },
  { "pyg_idler",      PYCF   pyg_idler,      1,     pyg_idler__doc__ },
  { "pyg_pending",    PYCF   pyg_pending,    1,     pyg_pending__doc__ },
  { "pyg_register",   PYCF   pyg_register,   1,     pyg_register__doc__ },
  { "get_style",      PYCF   get_style,      1,     get_style__doc__ },
  { "set_style",      PYCF   set_style,      1,     set_style__doc__ },
  { 0, 0 }
};

/* Initialize the module.  This should be the only symbol with
   external linkage. */

void initgistC (void)
{
  PyObject *m, *d, *sys_path;
  int i, n;

  m = Py_InitModule4 ("gistC", gist_methods,
		      gist_module_documentation,
		      (PyObject *) 0, PYTHON_API_VERSION);
  if (already_initialized)
    return;
  d = PyModule_GetDict (m);
  GistError = PyString_FromString ("gist.error");
  PyDict_SetItemString (d, "error", GistError);
  if (PyErr_Occurred ()) {
    Py_FatalError ("Cannot initialize module gist");
  }

  import_array();

  {
    /* DHM: in principal, gist might use argv[0] to try to figure out
     *      the PATH to this executable, from which it (in principle)
     *      might be able to find the g/ subdirectory for GISTPATH
     *      - this is done better below
     *      the other reason is that the argv[] might contain standard
     *      X resource switches, which, however, gist does not use */
    int argc = 0;
    char **argv = 0;
    g_initializer(&argc, argv);
  }

  if (0 != Py_AtExit (CleanUpGraphics)) {
    TO_STDERR("Gist: Warning: Exit procedure not registered\n");
    flush_stderr();
  }
  /* Default is to put occasional markers on curves. */
  GhGetLines ();
  gistA.dl.marks = 1;
  GhSetLines ();

  /* Default text is 14 point Helvetica. */
  GhGetText ();
  gistA.t.font = T_HELVETICA;
  gistA.t.height = 14.0 * ONE_POINT;
  GhSetText ();

/* Find that component of sys.path which ends in "/graphics/gist", and
   add it to gistPathDefault.
*/
  m = (PyObject *) PyImport_AddModule ("sys");
  d = PyModule_GetDict (m);
  sys_path = PyDict_GetItemString (d, "path");
  n = PySequence_Length(sys_path); /* sys.path is a list of strings. */
  for(i=0; i<n; i++){
    PyObject *op;
    char *s;
    op = PySequence_GetItem( sys_path, i );
    s = PyString_AsString (op);
    if( strstr(s, OUR_SPECIAL_DIR)){
      gistpath = (char *) malloc(strlen(gistPathDefault) + strlen(s) + 2);
      if( gistpath ){
	oldgistpath = gistPathDefault;
        strcpy(gistpath, gistPathDefault);
        strcat(gistpath, ":");
        strcat(gistpath, s);
        gistPathDefault = gistpath;
      }
    break;
    }
  }

  {
    /* set up play p_abort to work with SETJMP0 macro used here */
    /* do not call p_handler -- would disturb python signal handling */
    p_xhandler(pyg_abort_hook, pyg_on_exception);

    /* note that g_on_keyline might be useful, especially for Windows */
    g_on_keyline = pyg_on_keyline;

    /*  Provide a way for gist to process its events
     * - the PyOS_InputHook is actually for exclusive use of the
     *   _tkinter module, so if it is already set, leave it alone
     */
    if (!PyOS_InputHook)
#if defined(CYGWIN) || defined(MACOSX)
      PyOS_InputHook = p_pending_events;
#else
      PyOS_InputHook = p_wait_stdin;
#endif
    p_on_connect = pyg_on_connect;

    /* turn on idle function to do graphics tasks */
    /* Call p_idler to set up the idle callback   */
    p_idler(pyg_on_idle);
  }

  already_initialized = 1;

  if ( setjmp ( pyg_jmpbuf ) )  {
     p_pending_events();
     return;
  }
}

static int
pyg_on_idle(void)
{
  /* 
   *  Gist does all its drawing in GhBeforeWait, which should be called
   *  at idle time.
   */

  GhBeforeWait();
#ifdef CYGWIN
  p_pending_events();
#endif
  return 0;
}

/* p_on_connect(dis,fd)
 * called by gist/play when p_connect or p_disconnect makes or breaks
 * a display connection
 *   dis = 0 for connect, 1 for disconnect
 *   fd = file descriptor of X socket under UNIX/X11
 *        -1 under MS Windows
 * useful for wiring up pyg_pending to events arriving on fd
 *   under UNIX/X11, this requires createfilehandler _tkinter method
 *   under Windows, pyg_pending is unnecessary because each gist
 *     window has its own class and gets its messages delivered to
 *     the play/win/pscr.c w_winproc function automatically
 *
 * note that pyg_connector is set by calling pyg_register
 */
static void
pyg_on_connect(int dis, int fd)
{
  PyObject *args, *res;
  if (fd<0 || !pyg_connector) return;
  args = Py_BuildValue("(ii)", dis, fd);
  res = PyEval_CallObject(pyg_connector, args);
  Py_DECREF(args);
  /* nothing to be done if res==NULL?? */
  Py_XDECREF(res);
}

/* g_on_keyline(msg) is called when msg is typed in the gist
 * GpFXEngine window; the callback occurs when RET is pressed */
static void
pyg_on_keyline(char *msg)
{
  PyObject *args, *res;
  if (!pyg_keyhandler) return;
  args = Py_BuildValue("(s)", msg);
  res = PyEval_CallObject(pyg_keyhandler, args);
  Py_DECREF(args);
  /* nothing to be done if res==NULL?? */
  Py_XDECREF(res);
}

#ifdef __cplusplus
}
#endif