This file is indexed.

/usr/lib/gpsman/files.tcl is in gpsman 6.4.4.2-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
#
# This file is part of:
#
#  gpsman --- GPS Manager: a manager for GPS receiver data
#
# Copyright (c) 1998-2013 Miguel Filgueiras migfilg@t-online.de
#
#    This program is free software; you can redistribute it and/or modify
#      it under the terms of the GNU General Public License as published by
#      the Free Software Foundation; either version 3 of the License, or
#      (at your option) any later version.
#
#      This program is distributed in the hope that it will be useful,
#      but WITHOUT ANY WARRANTY; without even the implied warranty of
#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#      GNU General Public License for more details.
#
#      You should have received a copy of the GNU General Public License
#      along with this program.
#
#  File: files.tcl
#  Last change:  6 October 2013
#
# See also files_foreign.tcl
#  and metadata.tcl for properties of known file formats
#
# Includes contributions by
#   - Brian Baulch (baulchb _AT_ onthenet.com.au) marked "BSB contribution"
#   - Matt Martin (matt.martin _AT_ ieee.org) marked "MGM contribution"
#   - Valere Robin (valere.robin _AT_ wanadoo.fr) marked "VR contribution"
#   - Rudolf Martin (rudolf.martin _AT_ gmx.de) marked "RM contribution"
#

array set FCOMMAND {
    format  "!Format:"
    datum   "!Datum:"
    pformat "!Position:"
    pfdatum "!PFDatum:"
    dates   "!Creation:"
    0   no
    1   yes
    WP   "!W:"
    RT   "!R:"
    RS   "!RS:"
    TR   "!T:"
    TS   "!TS:"
    LN   "!LN:"
    LS   "!LS:"
    LAP  "!LAP:"
    GR   "!G:"
    GRWP   "!GW:"
    GRRT   "!GR:"
    GRTR   "!GT:"
    GRLN   "!GL:"
    GRLAP  "!GLAP:"
    GRGR   "!GG:"
    nb   "!NB:"
    comment   "%"
    mapback    "!Image:"
    mapproj    "!Projection:"
    maptransf  "!Transf:"
    mapscale   "!Scale:"
    mapbackat  "!Image at:"
    mapbackcs  "!Image coords:"
    maporigin  "!Origin:"
    mapinfo    "!Map:"
}

set FCOMMARGS {RT RS TR LN LAP GR GRWP GRRT GRTR GRLN GRLAP GRGR nb format
               pformat pfdatum datum dates mapback mapproj maptransf
               maporigin mapscale mapbackat mapbackcs}

  # attribute-value pairs:
  #    name of attribute, data array name, var for default value, and proc
  #    to check value (Ignore if appropriate; proc should return 1 on error
  #    and will be called with the value as argument)
array set FATTRPAIRS {
    WP {{symbol WPSymbol DEFAULTSYMBOL BadSymbol}
        {dispopt WPDispOpt DEFAULTDISPOPT BadDispOpt}
        {alt WPAlt EMPTYSTR BadAltitude}
	{mapbak WPMBack EMPTYSTR Ignore}}
    RT {{width RTWidth DEFTRTWIDTH BadWidth}
        {colour RTColour DEFTRTCOLOUR BadColour}
	{mapbak RTMBack EMPTYSTR Ignore}}
    TR {{width TRWidth DEFTTRWIDTH BadWidth}
        {colour TRColour DEFTTRCOLOUR BadColour}
	{mapbak TRMBack EMPTYSTR Ignore}}
    LN {{width LNWidth DEFTLNWIDTH BadWidth}
        {colour LNColour DEFTLNCOLOUR BadColour}
	{mapbak LNMBack EMPTYSTR Ignore}}
    LAP {}
}

# needing a global variable (not array) for default colours
set DEFTRTCOLOUR $MAPCOLOUR(RT)
set DEFTTRCOLOUR $MAPCOLOUR(TR)
set DEFTLNCOLOUR $MAPCOLOUR(LN)

##### saving

proc WriteHeader {file pformt} {
    # write header to file in GPSMan format, using $pformt for positions
    global TimeOffset Datum FCOMMAND CREATIONDATE \
	    SFilePFrmt SFileDatum SFileHeader MESS

    if { $SFileHeader($file) } {
	if { $SFilePFrmt($file) != $pformt } {
	    WriteChgPFormat $file $pformt
	}
	if { $SFileDatum($file) != $Datum } {
	    WriteChgDatum $file $Datum
	}	
    } else {
	puts $file "$FCOMMAND(comment) $MESS(written) GPSManager [NowTZ]"
	puts $file "$FCOMMAND(comment) $MESS(editrisk)"
	puts $file ""
	puts $file "$FCOMMAND(format) $pformt $TimeOffset $Datum"
	puts $file "$FCOMMAND(dates) $FCOMMAND($CREATIONDATE)"
	puts $file ""
	set SFilePFrmt($file) $pformt
	set SFileDatum($file) $Datum
	set SFileHeader($file) 1
    }
    return
}

proc WriteChgPFormat {file pformt} {
    # write command for changing position format to file in GPSMan format
    global FCOMMAND SFilePFrmt

    puts $file "$FCOMMAND(pformat) $pformt"
    set SFilePFrmt($file) $pformt
    return
}

proc WriteChgDatum {file datum} {
    # write command for changing datum to file in GPSMan format
    global FCOMMAND SFilePFrmt SFileDatum

    puts $file "$FCOMMAND(datum) $datum"
    set SFileDatum($file) $datum
    return
}

proc FormatPosn {posn type} {
    # from position to string under given format type

    switch $type {
	latlong { return [format "%s\t%s" [lindex $posn 2] [lindex $posn 3]] }
	utm {
	    return [format "%s\t%s\t%s\t%s" [lindex $posn 2] \
		      [lindex $posn 3] [lindex $posn 4] [lindex $posn 5]] 
	}
	grid {
	    return [format "%s\t%s\t%s" [lindex $posn 2] \
		      [lindex $posn 3] [lindex $posn 4]]
	}
	nzgrid {
	    return [format "%s\t%s" [lindex $posn 2] [lindex $posn 3]]
	}
	mh {
	    return [lindex $posn 2]
	}
    }
}

proc SaveNB {file obs} {
    # write NB data to file in GPSMan format
    global FCOMMAND

    if { $obs != "" } {
	puts $file "$FCOMMAND(nb)\t$obs"
	puts $file ""
    }
    return
}

proc WriteAttrPairs {file always wh ix hidden} {
    # write attribute-value pairs and hidden information for item
    #  with index $ix
    #  $wh in {WP, RT, TR, LN}
    #  $always is a flag set when the pairs must be always written out,
    #      otherwise they will be written only if they differ from their
    #      default value
    # no newline is written!
    global FATTRPAIRS
    
    foreach fd $FATTRPAIRS($wh) {
	set an [lindex $fd 1] ; set dv [lindex $fd 2]
	global $an $dv
	set x [set [set an]($ix)]
	if { $always || $x != [set $dv] } {
	    puts -nonewline $file "\t[lindex $fd 0]=$x"
	}
    }
    foreach h $hidden {
	puts -nonewline $file "\t$h"
    }
    return
}

proc WriteWPsRSs {file ixs stages} {
    # write WPs with indices in list $ixs and RT $stages (may be void) to
    #  file in GPSMan format
    global WPName WPCommt WPObs WPPFrmt WPPosn WPDatum WPDate WPHidden \
	    CREATIONDATE SFilePFrmt SFileDatum POSTYPE

    foreach i $ixs st $stages {
	if { [SlowOpAborted] } { return }
	if { $i != -1 } {
	    if { $WPPFrmt($i) != $SFilePFrmt($file) } {
		WriteChgPFormat $file $WPPFrmt($i)
	    }
	    if { $WPDatum($i) != $SFileDatum($file) } {
		WriteChgDatum $file $WPDatum($i)
	    }
	    set p [FormatPosn $WPPosn($i) $POSTYPE($SFilePFrmt($file))]
	    if { $CREATIONDATE } {
		puts -nonewline $file \
			"$WPName($i)\t$WPCommt($i)\t$WPDate($i)\t$p"
	    } else {
		puts -nonewline $file "$WPName($i)\t$WPCommt($i)\t$p"
	    }
	    WriteAttrPairs $file 0 WP $i $WPHidden($i)
	    puts $file ""
	    SaveNB $file $WPObs($i)
	} else {
	    puts $file "UNDEF"
	}
	if { $st != "" } {
	    WriteRTStage $file $st
	}
    }
    puts $file ""
    return
}

proc WriteRTStage {file stage} {
    # write a non-empty RT stage to file in GPSMan format
    global FCOMMAND DataIndex

    set c [lindex $stage $DataIndex(RScommt)]
    set l [lindex $stage $DataIndex(RSlabel)]
    puts -nonewline $file "$FCOMMAND(RS)\t$c\t$l"
    foreach h [lindex $stage $DataIndex(RShidden)] {
	puts -nonewline $file "\t$h"
    }
    puts $file ""
    return
}

proc JustfLeft {n string} {
    # justify string left to get a $n-characters string 
    set k [expr $n-[string length $string]]
    if { $k > 0 } {
	set sp [format "%${k}s" " "]
	return [append string $sp]
    }
    return $string
}

proc SaveWP {file ixs} {
    # save data for WPs with indices in list $ixs to file in GPSMan format
    global FCOMMAND PositionFormat

    WriteHeader $file $PositionFormat
    puts $file $FCOMMAND(WP)
    WriteWPsRSs $file $ixs ""
    return
}

proc SaveRT {file ixs} {
    # save data for RTs with indices in list $ixs to file in GPSMan format
    global FCOMMAND RTIdNumber RTCommt RTObs RTWPoints RTStages \
	    PositionFormat MESS

    WriteHeader $file $PositionFormat
    foreach i $ixs {
	if { [SlowOpAborted] } { return }
	set wpixs [Apply "$RTWPoints($i)" IndexNamed WP]
	if { [Undefined $wpixs] } {
	    GMMessage [format $MESS(undefWP) $RTIdNumber($i)]
	} else {
	    puts -nonewline $file \
		    "$FCOMMAND(RT)\t$RTIdNumber($i)\t$RTCommt($i)"
	    WriteAttrPairs $file 1 RT $i ""
	    puts $file ""
	    SaveNB $file $RTObs($i)
	    WriteWPsRSs $file $wpixs $RTStages($i)
	}
    }
    return
}

proc SaveTR {file ixs} {
    # save data for TRs with indices in list $ixs to file in GPSMan format
    global FCOMMAND TRName TRObs TRDatum TRTPoints TRSegStarts TRHidden \
	    DataIndex SFileDatum

    WriteHeader $file DMS
    set ilt $DataIndex(TPlatDMS) ; set ilg $DataIndex(TPlongDMS)
    set idt $DataIndex(TPdate)
    set ial $DataIndex(TPalt) ; set idp $DataIndex(TPdepth)
    foreach i $ixs {
	if { [SlowOpAborted] } { return }
	if { $TRDatum($i) != $SFileDatum($file) } {
	    WriteChgDatum $file $TRDatum($i)
	}
	puts -nonewline $file [format "%s\t%s" $FCOMMAND(TR) $TRName($i)]
	WriteAttrPairs $file 1 TR $i $TRHidden($i)
	puts $file ""
	SaveNB $file $TRObs($i)
	set ssts $TRSegStarts($i)
	set tpn 0 ; set nsst [lindex $ssts 0]
	foreach tp $TRTPoints($i) {
	    if { [SlowOpAborted] } { return }
	    if { $nsst == $tpn } {
		puts $file $FCOMMAND(TS)
		set ssts [lreplace $ssts 0 0]
		set nsst [lindex $ssts 0]
	    }
	    incr tpn
	    set alt [lindex $tp $ial] ; set dep [lindex $tp $idp]
	    if { $dep == "" } {
		if { $alt == "" } {
		    puts $file [format "\t%s\t%s\t%s" [lindex $tp $idt] \
			    [lindex $tp $ilt] [lindex $tp $ilg]]
		} else {
		    puts $file [format "\t%s\t%s\t%s\t%s" [lindex $tp $idt] \
			    [lindex $tp $ilt] [lindex $tp $ilg] $alt]
		}
	    } else { 
		puts $file [format "\t%s\t%s\t%s\t%s\t%s" [lindex $tp $idt] \
			[lindex $tp $ilt] [lindex $tp $ilg] $alt $dep]
	    }
	}
	puts $file ""
    }
    return
}

proc SaveLN {file ixs} {
    # save data for LNs with indices in list $ixs to file in GPSMan format
    global FCOMMAND LNName LNObs LNDatum LNPFrmt LNLPoints LNSegStarts \
	    LNWidth LNColour DataIndex SFileDatum SFilePFrmt

    WriteHeader $file $LNPFrmt([lindex $ixs 0])
    set iposn $DataIndex(LPposn) ; set ialt $DataIndex(LPalt)
    foreach i $ixs {
	if { [SlowOpAborted] } { return }
	if { $LNDatum($i) != $SFileDatum($file) } {
	    WriteChgDatum $file $LNDatum($i)
	}
	if { $LNPFrmt($i) != $SFilePFrmt($file) } {
	    WriteChgPFormat $file $LNPFrmt($i)
	}
	puts -nonewline $file [format "%s\t%s" $FCOMMAND(LN) $LNName($i)]
	WriteAttrPairs $file 1 LN $i ""
	puts $file ""
	SaveNB $file $LNObs($i)
	set ssts $LNSegStarts($i)
	set lpn 0 ; set nsst [lindex $ssts 0]
	foreach lp $LNLPoints($i) {
	    if { [SlowOpAborted] } { return }
	    if { $nsst == $lpn } {
		puts $file $FCOMMAND(LS)
		set ssts [lreplace $ssts 0 0]
		set nsst [lindex $ssts 0]
	    }
	    incr lpn
	    puts $file "\t[lindex $lp $iposn]\t[lindex $lp $ialt]"
	}
	puts $file ""
    }
    return
}

proc SaveLAP {file ixs} {
    # save data for LAPs with indices in list $ixs to file in GPSMan format
    global FCOMMAND LAPName LAPObs LAPDur LAPDist LAPBegPosn LAPEndPosn \
	LAPCals LAPTRIx LAPPFrmt LAPDatum SFileDatum SFilePFrmt

    WriteHeader $file $LAPPFrmt([lindex $ixs 0])
    foreach i $ixs {
	if { [SlowOpAborted] } { return }
	if { $LAPDatum($i) != $SFileDatum($file) } {
	    WriteChgDatum $file $LAPDatum($i)
	}
	if { $LAPPFrmt($i) != $SFilePFrmt($file) } {
	    WriteChgPFormat $file $LAPPFrmt($i)
	}
	puts -nonewline $file [format "%s\t%s\t" $FCOMMAND(LAP) $LAPName($i)]
	puts -nonewline $file "$LAPDur($i)\t$LAPDist($i)\t"
	puts -nonewline $file "[lrange $LAPBegPosn($i) 2 end]\t"
	puts -nonewline $file "[lrange $LAPEndPosn($i) 2 end]\t"
	puts $file "$LAPCals($i)\t$LAPTRIx($i)"
	SaveNB $file $LAPObs($i)
    }
    return
}

proc SaveGR {file ixs} {
    # save data for GRs with indices in list $ixs to file in GPSMan format
    global SFileHeader TYPES SUPPORTLAPS FCOMMAND MESS GRName GRObs GRConts

    set gtypes $TYPES
    if { $SUPPORTLAPS } { lappend gtypes LAP }
    foreach wh $gtypes {
	set l [GRsElements $ixs 1 $wh]
	if { $wh != "GR" } {
	    if { $l != "" } {
		Save$wh $file $l
		if { [SlowOpAborted] } { return }
	    }
	} else { set nixs $l }
    }
    if { ! $SFileHeader($file) } {
	puts $file "$FCOMMAND(comment) $MESS(written) GPSManager [NowTZ]"
	puts $file ""
    }
    foreach i $nixs {
	puts $file [format "%s\t%s" $FCOMMAND(GR) $GRName($i)]
	SaveNB $file $GRObs($i)
	foreach p $GRConts($i) {
	    if { [SlowOpAborted] } { return }
	    set c $FCOMMAND(GR[lindex $p 0])
	    foreach e [lindex $p 1] {
		puts $file [format "%s\t%s" $c "$e"]
		set c ""
	    }
	}
	puts $file ""
    }
    return
}

proc SaveWPDistBear {file w} {
    # save distances and bearings between WPs from window $w

    puts $file [$w.fr.fromto cget -text]
    puts $file ""
    puts $file [$w.fr.fr1.dist cget -text]
    puts $file [$w.fr.fr1.bear cget -text]
    puts $file ""
    return
}

proc SaveWPNearest {file w} {
    # save nearest WPs information from window $w
    global MESS

    puts $file [$w.fr.from cget -text]
    puts $file ""
    puts $file $MESS(WPNearest)
    set fb $w.fr.fr1.frbx
    foreach n [$fb.bxn get 0 end] d [$fb.bxd get 0 end] b [$fb.bxb get 0 end] {
	if { [SlowOpAborted] } { return }
	puts $file "$n\t$d\t$b"
    }
    puts $file ""   
    return
}

proc SaveRTComp {file w} {
    # save results of RT computation from window $w
    global MESS

    puts $file [$w.fr.fr1.ntitle cget -text]
    puts $file $MESS(RTcomp)
    set w $w.fr.fr3.fr31
    foreach n [$w.frbx.bxn get 0 end] \
	    p [$w.frbx.box get 0 end] \
	    d [$w.frbx.bxd get 0 end] \
	    b [$w.frbx.bxb get 0 end] \
	    da [$w.frbx.bxda get 0 end] \
	    sc [$w.frbx.bxsc get 0 end] \
	    sl [$w.frbx.bxsl get 0 end] {
	if { [SlowOpAborted] } { return }
	puts $file "$n\t$p\t$d\t$b\t$da\t$sc\t$sl"
    }
    puts $file [$w.frt.tt cget -text]
    puts $file ""
    return
}

proc SaveTRComp {file w} {
    # save results of TR computation from window $w
    global MESS

    puts $file [$w.fr.fr1.ntitle cget -text]
    puts $file $MESS(TRcomp)
    set w $w.fr.fr3
    foreach n [$w.frbx.bxn get 0 end] \
	    d [$w.frbx.bxd get 0 end] \
	    la [$w.frbx.bxlat get 0 end] \
	    lo [$w.frbx.bxlong get 0 end] \
	    a [$w.frbx.bal get 0 end] \
	    l [$w.frbx.bxl get 0 end] \
	    tl [$w.frbx.btl get 0 end] \
	    dt [$w.frbx.bdt get 0 end] \
	    s [$w.frbx.bsp get 0 end] \
	    b [$w.frbx.bbg get 0 end] {
	if { [SlowOpAborted] } { return }
	puts $file "$n\t$d\t$la\t$lo\t$a\t$l\t$tl\t$dt\t$s\t$b"
    }
    foreach k {td tt} {
	puts $file [$w.frt.$k cget -text]
    }
    if { [winfo exists $w.frtng] } {
	foreach k {td tt} {
	    puts $file [$w.frtng.$k cget -text]
	}
    }
    foreach k {avg max min} {
	puts $file [$w.frsp.$k cget -text]
    }
    # RM contribution
    foreach k {avg trt} {
	puts $file [$w.frrest.$k cget -text]
    }
    #===
    if { [winfo exists $w.frmxnalt] } {
	foreach k {mx mn} {
	    puts $file [$w.frmxnalt.$k cget -text]
	}
	if { [winfo exists $w.frcad] } {
	    foreach k {cumula cumuld thresh} {
		puts $file [$w.frcad.$k cget -text]
	    }
	}
    }
    puts $file "[$w.frd0.toend cget -text] [$w.frd0.max cget -text]"
    puts $file ""
    return
}

proc SavePVTData {file args} {
    # save real-time log (Garmin PVT) information
    global TXT

    puts $file $TXT(realtimelog)
    set fx .pvt.fri.frtbx
    foreach bname "d lat long alt fix epe eph epv velx vely velz trk" {
	puts -nonewline $file "[$fx.tit$bname cget -text]\t"
	set $bname [$fx.bx$bname get 0 end]
    }
    puts $file "" ; puts $file ""
    foreach xd $d xlat $lat xlong $long xalt $alt xfix $fix xepe $epe \
	    xeph $eph xepv $epv xvelx $velx xvely $vely xvelz $velz xtrk $trk {
	if { [SlowOpAborted] } { return }
	puts $file "$xd\t$xlat\t$xlong\t$xalt\t$xfix\t$xepe\t$xeph\t$xepv\t$xvelx\t$xvely\t$xvelz\t$xtrk"
    }
    puts $file ""
    return
}

proc SaveMapBkInfo {file args} {
    # save map background information
    #  $args: list with path to image file, projection data, transformation
    #    data, scale, and data on subsidary images
    # projection and transformation data are lists with a name and a sequence
    #  of attribute=value pairs
    # data on subsidary images consists of 2 lists of pairs: one with grid
    #  coordinates and path, and the other one with canvas coordinates of NW
    #  corner and path
    global Datum FCOMMAND MESS

    set args [lindex $args 0]
    puts $file "$FCOMMAND(comment) $MESS(written) GPSManager [NowTZ]"
    puts $file "$FCOMMAND(comment) $MESS(edityourrisk)"
    puts $file ""
    puts $file "$FCOMMAND(mapback) [lindex $args 0]"
    puts $file "$FCOMMAND(datum) $Datum"
    set pd [lindex $args 1]
    puts -nonewline $file "$FCOMMAND(mapproj) [lindex $pd 0]"
    foreach p [lrange $pd 1 end] {
	puts -nonewline $file "\t$p"
    }
    puts $file ""
    set pt [lindex $args 2]
    puts -nonewline $file "$FCOMMAND(maptransf) [lindex $pt 0]"
    foreach p [lrange $pt 1 end] {
	puts -nonewline $file "\t$p"
    }
    puts $file ""
    puts $file "$FCOMMAND(mapscale) [lindex $args 3]"
    puts $file ""
    foreach ixsp [lindex $args 4] {
	puts $file "$FCOMMAND(mapbackat) [lindex $ixsp 0] [lindex $ixsp 1]"
    }
    puts $file ""
    foreach csp [lindex $args 5] {
	puts $file "$FCOMMAND(mapbackcs) [lindex $csp 0] [lindex $csp 1]"
    }
    puts $file ""
    return
}

proc SaveMapInfo {file args} {
    # save map datum, projection, transformation, grid and scale
    #  $args is list with projection data, transformation data, position
    #    format of coordinates, datum for coordinates and scale
    # projection and transformation data are lists with a name and a sequence
    #  of attribute=value pairs
    global Datum FCOMMAND MESS

    set args [lindex $args 0]
    puts $file "$FCOMMAND(comment) $MESS(written) GPSManager [NowTZ]"
    puts $file "$FCOMMAND(comment) $MESS(edityourrisk)"
    puts $file ""
    puts $file $FCOMMAND(mapinfo)
    puts $file "$FCOMMAND(datum) $Datum"
    set pd [lindex $args 0]
    puts -nonewline $file "$FCOMMAND(mapproj) [lindex $pd 0]"
    foreach p [lrange $pd 1 end] {
	puts -nonewline $file "\t$p"
    }
    puts $file ""
    set pt [lindex $args 1]
    puts -nonewline $file "$FCOMMAND(maptransf) [lindex $pt 0]"
    foreach p [lrange $pt 1 end] {
	puts -nonewline $file "\t$p"
    }
    puts $file ""
    puts $file "$FCOMMAND(pformat) [lindex $args 2]"
    puts $file "$FCOMMAND(pfdatum) [lindex $args 3]"
    puts $file "$FCOMMAND(mapscale) [lindex $args 4]"
    puts $file ""
    return
}

proc SaveGREls {how args} {
    # save elements of group to file in GPSMan format
    # if file != "stdout" file will be closed at the end
    #  $how==all: all elements in all groups
    #  $how==select: groups and element types chosen from list
    #  $args not used but in call-back
    global GRName SFilePFrmt SFileDatum SFileHeader TYPES SUPPORTLAPS MESS TXT

    set gtypes $TYPES
    if { $SUPPORTLAPS } { lappend gtypes LAP }
    switch $how {
	all {
	    set ixs [array names GRName]
	    set whs $gtypes
	}
	select {
	    if { [set ixs [ChooseItems GR]] == "" } { return }
	    set ts ""
	    foreach k $gtypes { lappend ts $TXT(name$k) }
	    if { [set whs \
		    [GMChooseFrom many $MESS(putwhat) 6 $ts $gtypes]] == "" } {
	       return
	   }
	}
    }
    set f [GMOpenFile $TXT(saveto) GR w]
    if { $f != ".." } {
	set sid [SlowOpWindow $TXT(save)]
	set SFileHeader($f) 0
	if { [set i [lsearch -exact $whs GR]] != -1 } {
	    set whs [lreplace $whs $i $i]
	    set r 1
	} else { set r 0 }
	foreach wh $whs {
	    if { [set l [GRsElements $ixs $r $wh]] != "" } {
		Save${wh} $f $l
	    }
	}
	catch {
	    unset SFilePFrmt($f) ; unset SFileDatum($f)
	    unset SFileHeader($f)
	}
	if { $f != "stdout" } { close $f }
	SlowOpFinish $sid ""
    }
    return
}

proc SaveFile {how what args} {
    # save information to file in GPSMan format
    #  $how==comp: computation results;
    #            $what in {WPDistBear, WPNearest, RTComp, TRComp, PVTData}
    #            $args: normally the window containing the information; see
    #              procs Save$what
    #  $how==all: all items of type $what
    #            $what in $TYPES or $what=="LAP" or $what=="Data"
    #  $how==state: as "all" but without slow operation dialog
    #  $how==select: items of type $what chosen from list
    #            $what in $TYPES or $what=="LAP"
    #  $how==mapback: map background image information
    #            (used when saving state: no slow operation dialog)
    #            $what: MapBkInfo
    #            $args: see proc SaveMapBkInfo
    #  $how==mapinfo: map projection, transformation, position format and scale
    #            (used when saving state: no slow operation dialog)
    #            $what: MapInfo
    #            $args: see proc SaveMapInfo

    eval [list SaveFileTo {} $how $what $args]
    return
}

proc SaveFileTo {f how what args} {
    # save information to file $f in GPSMan format
    # if $f=="" ask user to select output file
    # if $f!="stdout" file will be closed at the end
    # file can be appended to if it exists only when $how=="comp" or
    #  $APPGMDATAFILE is non-zero
    # see proc SaveFile for the description of the arguments
    global SFilePFrmt SFileDatum SFileHeader TXT TYPES SUPPORTLAPS File \
	APPGMDATAFILE

    set stypes $TYPES
    if { $SUPPORTLAPS } { lappend stypes LAP }
    if { $how == "state" } {
	set slow 0
    } else { set slow 1 }
    if { $APPGMDATAFILE } {
	set mode wapp
    } else { set mode w }
    switch $how {
	all -  state {
	    set lp [AllIndicesForType $what $stypes]
	}
	select {
	    if { [set ixs [ChooseItems $what]] == "" } { return }
	    set lp [list [list $what $ixs]]
	}
	comp { set mode wapp }
	mapinfo -  mapback { set slow 0 }
    }
    set oldf $f
    if { $f != "" || [set f [GMOpenFile $TXT(saveto) $what $mode]] != ".." } {
	if { $slow } {
	    set sid [SlowOpWindow $TXT(save)]
	} else { SetCursor . watch }
	set SFileHeader($f) 0
	switch $how {
	    comp -  mapinfo {
		Save${what} $f $args
	    }
	    mapback {
		Save${what} $f $args
		if { $oldf == "" } {
		    Define backgrnd "" $File(MapBkInfo) ""
		}
	    }
	    default {
		foreach p $lp {
		    Save[lindex $p 0] $f [lindex $p 1]
		}
	    }
	}
	catch {
	    unset SFilePFrmt($f) ; unset SFileDatum($f)
	    unset SFileHeader($f)
	}
	if { $f != "stdout" } { close $f }
	if { $slow } {
	    SlowOpFinish $sid ""
	} else { ResetCursor . }
    }
    return
}

## saving/restoring state

proc SaveState {} {
    # save current state of interface and current data
    # slow operation dialog not used
    global USvState USvData USvMap MESS Number MapEmpty Map TYPES EdWindow \
	    MapPFormat MapPFDatum MapPFNeedsDatum MapFont Travelling Travel \
	    GMEd SUPPORTLAPS File

    set stypes $TYPES
    if { $SUPPORTLAPS } { lappend stypes LAP }
    set todispl "" ; set toopen ""
    if { $Number(Data) != 0 } {
	if { [catch {set f [open $USvData w]}] } {
	    GMMessage [format $MESS(cantwrtsstate) $USvData]
	    return
	}
	set saveddata $USvData
	SaveFileTo $f state Data
	foreach wh $stypes {
	    global ${wh}Displ

	    set ns "" ; set os ""
	    foreach ix [array names ${wh}Displ] {
		set name [NameOf $wh $ix]
		if { [set [set wh]Displ($ix)] } {
		    lappend ns $name
		}
		if { [winfo exists .gm${wh}sh$ix] } {
		    lappend os $name
		}
	    }
	    if { [winfo exists $EdWindow($wh)] && $GMEd($wh,Index) != -1 } {
		set os [linsert $os 0 [NameOf $wh $GMEd($wh,Index)]]
	    }
	    if { $ns != "" } { lappend todispl [list $wh $ns] }
	    if { $os != "" } { lappend toopen [list $wh $os] }
	}
    } else { set saveddata "" }
    if { ! $MapEmpty } {
	if { [catch {set f [open $USvMap w]}] } {
	    GMMessage [format $MESS(cantwrtsstate) $USvMap]
	    return
	}
	set savedmap $USvMap
	if { [$Map find withtag mapimage] != "" } {
	    set mapfile Back
	    SaveMapBack $f
	} else {
	    set mapfile Params
	    SaveMapParams $f
	}
    } else { set savedmap "" }
    if { [catch {set f [open $USvState w]}] } {
	GMMessage [format $MESS(cantwrtsstate) $USvState]
	return
    }
    puts $f ""
    puts $f "# $MESS(written) GPSManager [NowTZ]"
    puts $f "# $MESS(editrisk)"
    puts $f ""
    puts $f "set saveddata \"$saveddata\""
    puts $f "set todispl \{$todispl\}"
    puts $f "set toopen \{$toopen\}"
    puts $f ""
    puts $f "set savedmap \"$savedmap\""
    if { $savedmap != "" } {
	puts $f "set mapfile \"$mapfile\""
	puts $f "set mappformat \"$MapPFormat\""
	puts $f "set mappfdatum \"$MapPFDatum\""
	puts $f "set mappfndatum \"$MapPFNeedsDatum\""
	puts $f "set mapfont \"$MapFont\""
    }
    puts $f ""
    puts $f "set Travelling \"$Travelling\""
    puts $f "array set Travel \{"
    foreach p $Travel(tosave) {
	foreach n [array names Travel $p] {
	    puts $f "   $n \{$Travel($n)\}"
	}
    }
    puts $f "\}"
    puts $f ""
    puts $f "array set File \{"
    foreach n [array names File] {
	puts $f "   $n \{$File($n)\}"
    }
    puts $f "\}"
    puts $f ""
    # must be the last instruction:
    puts $f "if \{ \[catch \{cd \"[pwd]\"\}\] \} \{"
    puts $f "  set saved \"[pwd]\""
    puts $f "\} else \{ set saved 1 \}"
    puts $f ""
    close $f
    return
}

proc RestoreState {} {
    # restore saved state and clear it if the user agrees
    global USvState MESS CREATIONDATE Proc DELSTATE MapPFormat Map MapFont \
	    Travelling Travel MapEmpty ASKPROJPARAMS Datum MapPFDatum \
	    MapPFNeedsDatum File

    if { [file size $USvState] == 0 } { return }
    # assumed to be in system encoding
    source $USvState
    if { [catch {set saved}] } {
	GMMessage [format $MESS(corruptsstate) $USvState]
	return
    }
    if { $saved != 1 } {
	GMMessage [format $MESS(couldntcd) $saved]
    }
    if { $DELSTATE == "always" || \
	    ( $DELSTATE == "ask" && [GMConfirm $MESS(delsstate)] ) } {
	set del 1
    } else { set del 0 }
    if { $savedmap != "" } {
	if { $mapfile == "Back" } {
	    set r [LoadMapFixedBk $savedmap]
	    if { $r == -1 } { return }
	    if { [lindex $r 0] != 1 } {
		GMMessage "$MESS(badfile): $savedmap"
		return
	    }
	    set MapEmpty 0
	    eval LoadMapBackGeoRef [lrange $r 1 end]
	} else {
	    # $mapfile == "Params"
	    set r [LoadMapInfo $savedmap]
	    if { $r == -1 } { return }
	    eval LoadMapParams $r
	}
	set MapPFormat $mappformat
	# compatibility with 6.1.2 and older versions
	if { [catch {set mappfdatum}] } {
	    set MapPFDatum $Datum
	    ChangeMapPFormat $MapPFormat
	} else {
	    set MapPFDatum $mappfdatum ; set MapPFNeedsDatum $mappfndatum
	}
	if { ! [catch {set mapfont}] } { set MapFont $mapfont }
	if { $del } { file delete -- $savedmap }
    }
    if { $saveddata != "" } {
	if { [catch {set f [open $saveddata r]}] } {
	    GMMessage [format $MESS(cantrdsstate) $saveddata]
	    return
	}
	if { [LoadFileFrom $f Data] == 0 } { return }
	if { $del } { file delete -- $saveddata }
    }
    set a $ASKPROJPARAMS ; set ASKPROJPARAMS 0
    foreach p $todispl {
	set wh [lindex $p 0]
	foreach n [lindex $p 1] {
	    if { [set ix [IndexNamed $wh $n]] != -1 } {
		PutMap $wh $ix
	    }
	}
    }
    set ASKPROJPARAMS $a
    foreach p $toopen {
	set wh [lindex $p 0]
	if { $wh == "LAP" } {
	    set opts "change revert forget cancel"
	} else { set opts "change revert create forget cancel" }
	foreach n [lindex $p 1] {
	    if { [set ix [IndexNamed $wh $n]] != -1 } {
		$Proc($wh) $ix $opts [ItemData $wh $ix]
		set opts ""
	    }
	}
    }
    if { $del } { file delete -- $USvState }
    # delete logo
    $Map delete dummy
    if { $Travelling } {
	set Travelling 0
	ToTravel
    }
    return
}

## saving canvas to Postscript or other format

proc SaveCanvas {cv bounds fmt dest} {
    # save canvas $c to file or printer under format $fmt
    #  $bounds is bounding box of visible part of canvas
    #    (should be set to "" if there is no need for it)
    #  $fmt is either PS, or in $ImgOutFormats (if the Img library is loaded)
    #    and probably will be PS if $dest==printer
    #  $dest in {file, printer}
    # items with tag "temp" can be created in and deleted from $cv
    # if $cv==$Map inclusion of map scale is an option
    # when printing, a temporary file named "print.tmp" is created in the
    #  user GPSMan temporary files directory
    global PRINTCMD PAPERSIZE PAGEWIDTH PAGEHEIGHT GFMode GFRot \
	GFPaper GFLegend GFOnlyVisible GFScale GFPrintOption MESS TXT \
	MAPCOLOUR Map MpW MAPSCLENGTH USERTMPDIR MapFont FixedFont

    set leg "=$TXT(legend)"
    set cms [list $TXT(colour) $TXT(grey) $TXT(mono)]
    set GFOnlyVisible 0 ; set GFMode $TXT(colour)
    switch $fmt {
	PS {
	    set vs {GFLegend GFMode GFRot GFPaper}
	    set pps [array names PAGEWIDTH]
	    if { [set i [lsearch -exact $pps $PAPERSIZE]] != 0 } {
		set pps [linsert [lreplace $pps $i $i] 0 $PAPERSIZE]
	    }
	    set ds [list $leg $cms [list $TXT(portr) $TXT(landsc)] $pps]
	}
	jpeg {
	    set vs {GFLegend GFMode}
	    set ds [list $leg [lreplace $cms 2 2]]
	}
	default {
	    set vs {GFLegend}
	    set ds [list $leg]
	}
    }
    if { $bounds != {} && $fmt == "PS" } {
	set vs [linsert $vs 1 GFOnlyVisible]
	set ds [linsert $ds 1 "@$TXT(psvisible)"]
    }
    if { $cv == $Map } {
	set GFScale 1
	set vs [linsert $vs 1 GFScale]
	set ds [linsert $ds 1 "@$TXT(incscale)"]
	set font $MapFont
    } else {
	set GFScale 0
	set font $FixedFont
    }
    switch $dest {
	file {
	    set fn [GMGetFileName $TXT(saveto) Plot w $vs $ds]
	}
	printer {
	    if { $PRINTCMD == "" } {
		GMMessage $MESS(noprintcmd)
		return
	    }
	    lappend vs GFPrintOption
	    lappend ds "=$TXT(printopt)"
	    if { ! [GMChooseParams $TXT(print) $vs $ds] } { return }
	    set fn [file join $USERTMPDIR print.tmp]
	}
    }
    if { $fn != ".." } {
	SetCursor . watch
	foreach m $cms tm {color gray mono} {
	    if { $GFMode == $m } {
		set GFMode $tm
		break
	    }
	}
	if { $GFOnlyVisible && $mbs != {} } {
	    set mbs $bounds
	} else { set mbs [$cv bbox all] }
	foreach {x0 y0 xf yf} $mbs { break }
	set sm [expr ($xf+$x0)/2.0]
	if { $GFScale } {
	    # only applies when $cv == $Map
	    set m $MAPSCLENGTH ; set m2 [expr $m/2.0]
	    scan [$MpW.frm.frmap3.fr3.cv.val cget -text] "%f %s" sc su
	    if { [set xs0 [expr $sm-$m2]] < $x0 } {
		set m $m2
		if { [set xs0 [expr $sm-$m/2.0]] < $x0 } {
		    set x0 $xs0
		}
		set sc [expr $sc/2.0]
		if { int($sc) != $sc } {
		    set sc [format "%.1f" $sc]
		} else { set sc [expr int($sc)] }
	    }
	    if { [set xsf [expr $xs0+$m]] > $xf } {
		set xf $xsf
	    }
	    if { $GFOnlyVisible } {
		set ys [expr $yf-100]
	    } else { set ys [expr $yf+60] ; set yf [expr $ys+10] }
	    set yd [expr $ys-7.5] ; set ylf [expr $ys+7.5]
	    $Map create line $xs0 $ys $xsf $ys -arrow both \
		    -fill $MAPCOLOUR(mapsel) -tags temp
	    $Map create line $xs0 $yd $xs0 $ylf -fill $MAPCOLOUR(mapsel) \
		    -tags temp
	    $Map create line $xsf $yd $xsf $ylf -fill $MAPCOLOUR(mapsel) \
		    -tags temp
            $Map create text $sm [expr $ys-8] -font $font \
		    -fill $MAPCOLOUR(mapsel) -text "$sc $su" -tags temp
	}
	if { $GFLegend != "" } {
	    if { $GFOnlyVisible } {
		set yt [expr $yf-60]
	    } else {
		set yt [expr $yf+20] ; set yf [expr $yt+5]
	    }
	    $cv create text $sm $yt -font $font \
		    -fill $MAPCOLOUR(mapleg) -text $GFLegend -tags temp
	}
	switch $fmt {
	    PS {
		set rot [expr [string compare $GFRot $TXT(portr)]!=0]
		set h [expr $yf-$y0+100] ; set w [expr $xf-$x0+100]
		if { $h > $w } {
		    set sc "-pageheight"
		    if { $rot } {
			set sca $PAGEWIDTH($GFPaper)
		    } else { set sca $PAGEHEIGHT($GFPaper) }
		} else {
		    set sc "-pagewidth"
		    if { $rot } {
			set sca $PAGEHEIGHT($GFPaper)
		    } else { set sca $PAGEWIDTH($GFPaper) }
		}
		if { $GFOnlyVisible } {
		    $cv postscript -file $fn -colormode $GFMode $sc $sca \
			    -rotate $rot
		} else {
		    $cv postscript -file $fn -width $w -height $h \
			    -colormode $GFMode $sc $sca -rotate $rot \
			    -x [expr $x0-50] -y [expr $y0-50]
		}
	    }
	    default {
		raise [winfo toplevel $cv]
		update
		after 500
		while { [catch {set im [image create photo -format window \
			                             -data $Map]}] } {
		    if { ! [GMConfirm $MESS(unobscmap)] } {
			ResetCursor .
			return
		    }
		    after 2000
		}
		if { $fmt == "jpeg" && $GFMode == "gray" } {
		    if { [catch {$im write $fn -format jpeg -grayscale}] } {
			GMMessage [format $MESS(cantwrtimg) $fmt]
		    }
		} else {
		    if { [catch {$im write $fn -format $fmt}] } {
			GMMessage [format $MESS(cantwrtimg) $fmt]
		    }
		}
		image delete $im
	    }
	}
	$cv delete temp
	if { $dest == "printer" } {
	    if { $GFPrintOption != "" } {
		exec $PRINTCMD $GFPrintOption [glob $fn]
	    } else { exec $PRINTCMD [glob $fn] }
	    file delete -- $fn
	}
	ResetCursor .
    }
    return
}

##### loading

proc OpenInputFileFails {what fmt} {
    # open file for loading/importing and set initial values
    #  $what in $TYPES or $what=="LAP" or $what=="Data"
    #  $fmt is such that "in" is in $FILEFORMAT($fmt,mode)
    # return 0 unless operation is to be cancelled
    global CREATIONDATE GFVisible TXT MESS FILEFORMAT SHOWFILEITEMS

    set params "" ; set parwidgts ""
    if { ! [catch {set pars $FILEFORMAT($fmt,in,params)}] } {
	set vrcvct "" ; set dvs $FILEFORMAT($fmt,in,pdefts)
	foreach p $pars dv $dvs {
	    if { [regexp {^global:(.+)$} $dv m dvv] } {
		global $dvv
		set dv [set $dvv]
	    }
	    set ff $FILEFORMAT($fmt,param,$p)
	    if { [llength $ff] < 4 } { BUG bad FILEFORMAT param }
	    foreach "var cv ct widgt" $FILEFORMAT($fmt,param,$p) { break }
	    global $var
	    set $var $dv
	    lappend params $var
	    # this needs the global TXT
	    lappend parwidgts [subst $widgt]
	    lappend vrcvct $var $cv $ct
	}
    } else { set pars "" }
    switch $fmt {
	GPSMan {
	    set for loadfrm ; set cr $CREATIONDATE
	}
	GPStrans {
	    # some code for loading GPSMan is used for importing GPStrans
	    set for importfrm ; set cr 1
	}
	default {
	    # incorporates MGM contribution
	    # incorporates VR contribution
	    set for importfrm ; set cr 0
	}
    }
    # mapping items is always an option
    set GFVisible $SHOWFILEITEMS
    lappend params GFVisible
    lappend parwidgts "@$TXT(mapitems)"
    while 1 {
	set f [GMOpenFileParms $TXT($for) $what r $params $parwidgts]
	if { $f == ".." } {
	    EndWPRenaming
	    return 1
	}
	set ok 1
	if { $pars != "" } {
	    set vrctvl ""
	    foreach p $pars dv $dvs "var cv ct" $vrcvct {
		set val [set $var]
		if { [catch {eval $cv}] } {
		    set ms badconv ; set ok 0
		    break
		}
		lappend vrctvl $var $ct $val
	    }
	    if { $ok } {
		foreach p $pars dv $dvs "var ct val" $vrctvl {
		    if { [catch {set r [eval $ct]}] || $r == 0 } {
			set ms badprmval ; set ok 0
			break
		    }
		    set $var $val
		}
	    }
	}
	if { $ok } {
	    break
	}
	GMMessage [format $MESS($ms) $p $val] wait
	set $var $dv
    }
    InitInputFileVars $f $what $cr $GFVisible
    return 0
}

proc InitInputFileVars {f what cr vis} {
    # initialise input file $f global variables
    #  $what in $TYPES or $what=="LAP" or $what=="Data"
    #  $cr set if creation date is in use
    #  $vis set if must display items read in
    global LFileLNo LFileCreat LFileBuffFull LFileEOF LFileDefs LFileVisible \
	    LFileTimeOff LFileSlowId LChannel TXT

    set sid [SlowOpWindow $TXT(fread)]
    set LChannel($what) $f ; set LFileSlowId($f) $sid
    set LFileLNo($f) 0 ; set LFileCreat($f) $cr ; set LFileTimeOff($f) ""
    set LFileBuffFull($f) 0 ; set LFileEOF($f) 0
    set LFileDefs($f) 0 ; set LFileVisible($f) $vis
    return 0
}

proc CloseInputFile {what} {
    # close file opened for loading/importing and unset some global variables
    # if file is "stdin" it will not be closed
    #  $what in $TYPES or $what=="LAP"
    global LFilePFrmt LFileDatum LFileLNo LFileCreat \
	    LFileId LFileOther LFileBuff LFileBuffFull LFileEOF LFileDefs \
	    LFileVisible LFileTimeOff LFileSlowId LChannel

    set f $LChannel($what)
    if { $f != "stdin" } { close $f }
    set sid $LFileSlowId($f)
    foreach v "LFilePFrmt($f) LFileDatum($f) LFileTimeOff($f) \
	    LFileLNo($f) LFileCreat($f) LFileDefs($f) LFileId($f) \
	    LFileOther($f) LFileBuff($f) LFileBuffFull($f) \
	    LFileEOF($f) LFileVisible($f) LFileSlowId($f) \
            LChannel($what)" {
	catch { unset $v }
    }
    SlowOpFinish $sid ""
    return
}

# encodings in Tcl

proc TclEncodingName {enc} {
    # tries to convert $enc to a Tcl encoding name
    global MESS

    set lenc [string tolower $enc]
    switch -glob -- $lenc {
	iso* {
	    regsub {^iso[-_]} $lenc iso enc
	}
	utf8 {
	    return utf-8
	}
	utf16* {
	    # not supported by Tcl 8.4
	    set enc utf-16
	}
	latin1 -  us-ascii {
	    return iso8859-1
	}
	latin[2-4] {
	    set enc iso8859-[string index $lenc 5]
	}
	latin[56] {
	    set enc iso8859-[expr [string index $lenc 5]+4]
	}
	latin[89] {
	    set enc iso8859-[expr [string index $lenc 5]+6]
	}
	latin10 {
	    set enc iso8859-16
	}
	mac* -  x11controlchars {
	    # keep the original name for uppercase letters
	}
	default {
	    set enc $lenc
	}
    }
    if { [lsearch -exact [encoding names] $enc] == -1 } {
	GMMessage [format $MESS(unknownenc) $enc]
    }
    return $enc
}

# buffered reading

proc ReadFileNL {file} {
    # buffered read from file a line (may be empty) at a time
    # uses and sets:
    #   buffer LFileBuff, flag LFileBuffFull set if buffer contents not used
    #   flag LFileEOF, line counter LFileLNo
    global LFileBuffFull LFileBuff LFileLNo LFileEOF

    if { $LFileEOF($file) } { return "" }
    if { $LFileBuffFull($file) } {
	set LFileBuffFull($file) 0
    } else {
	if { [eof $file] } {
	    set LFileEOF($file) 1
	    return ""
	}
	gets $file LFileBuff($file) ; incr LFileLNo($file)
    }
    return $LFileBuff($file)
}

proc ReadFile {file} {
    # buffered read from file a line at a time skipping empty lines
    # uses and sets:
    #   buffer LFileBuff, flag LFileBuffFull set if buffer contents not used
    #   flag LFileEOF, line counter LFileLNo
    global LFileBuffFull LFileBuff LFileLNo LFileEOF

    if { $LFileEOF($file) } { return "" }
    if { $LFileBuffFull($file) } {
	set LFileBuffFull($file) 0
    } else {
	if { [eof $file] } {
	    set LFileEOF($file) 1
	    return ""
	}
	gets $file LFileBuff($file) ; incr LFileLNo($file)
	while { $LFileBuff($file) == "" } {
	    if { [eof $file] } { 
		set LFileEOF($file) 1
		return ""
	    }
	    gets $file LFileBuff($file) ; incr LFileLNo($file)
	}
    }
    return $LFileBuff($file)
}

# reading utilities

proc ChopInString {m s} {
   # deletes prefix of length $m and leading spaces and tabs from string $s

   return [string trimleft [string range "$s" [string length "$m"] end] " \t"]
}

proc CleanLine {m importing} {
    # deletes leading/trailing blanks, and, if not importing, comments
    global FCOMMAND

    set m [string trim $m " "]
    if { ! $importing && [string first $FCOMMAND(comment) $m] == 0 } {
	return ""
    }
    return $m
}

# parsing

proc MakeComment {commt} {
    # try to change comment to fit to acceptable character set and length;
    #  on failure return ""
    global COMMENTLENGTH NOLOWERCASE

    if { ! [CheckChars Ignore $commt] } {
	if { $NOLOWERCASE } {
	    set commt [string toupper $commt]
	}
	regsub -all {:\.} $commt "" commt
    }
    if { ! [CheckChars Ignore $commt] } {
	return ""
    }
    if { [string length $commt] > $COMMENTLENGTH } {
	set commt [string trim $commt "\ \t"]
    }
    if { [string length $commt] > $COMMENTLENGTH } {
	set commt [string range $commt 0 [expr $COMMENTLENGTH-1]]
    }
    return $commt
}

proc FindPFormat {line} {
    # parse position format; position format names cannot have blanks
    global NONGRIDFMTS GRIDS

    if { [regexp {^([^ ]+)} $line x pf] } {
	foreach f $NONGRIDFMTS {
	    if { $f == $pf } { return $f }
	}
	foreach i $GRIDS {
	    if { $i == $pf } { return $i }
	}
    }
    return BAD
}

proc BadFilePTArgs {file name list params f cf} {
    # parse arguments of projection or transformation command
    #  $f in {Proj, Transf}
    #  $cf in {PROJ, TRANSF}
    # LFile${f}($file) is set to list with name and list of pairs with
    #  parameter name and value
    # return 0 if correct
    global LFile${f} MESS

    set l ""
    foreach av [lrange $list 1 end] {
	set fp [split $av =]
	if { [llength $fp] != 2 } {
	    GMMessage "$MESS(badfield): $f"
	    return 1
	}
	set fn [lindex $fp 0]
	if { [set i [lsearch -exact $params $fn]] == -1 } {
	    if { $name == "UTM" && $fn == "long0" } {
		# parameter name changed after version 6.0.1
		set fn m_0
		set i [lsearch -exact $params $fn]
	    } else {
		GMMessage "$MESS(badattr): $fn"
		return 1
	    }
	}
	set params [lreplace $params $i $i]
	lappend l [list $fn [lindex $fp 1]]
    }
    if { $params != "" } {
	GMMessage "$MESS(missattrs): $params"
	return 1
    }
    set LFile${f}($file) [list $name $l]
    return 0
}

proc FilePTComm {file f cf line} {
    # parse projection or transformation command
    #  $f in {Proj, Transf}
    #  $cf in {PROJ, TRANSF}
    # LFile${f}($file) is set to parsed data here or by proc BadFilePTArgs
    # return 1 if correct
    global MAPKNOWN${cf}S MAP${cf}DATA MESS LFile${f}

    set l [split $line \t]
    set p [lindex $l 0]
    if { [lsearch -exact [set MAPKNOWN${cf}S] $p] == -1 } {
	GMMessage "$MESS(unkn$f): $p"
	return 0
    }
    if { [catch {set MAP${cf}DATA($p)}] } {
	# for particular cases of projections with fixed parameters
	set LFile${f}($file) $p
	return 1
    }
    if { [BadFilePTArgs $file $p $l [set MAP${cf}DATA($p)] $f $cf] } {
	GMMessage $MESS(bad${f}args)
	return 0
    }
    return 1
}

proc FindArgs {commd line file} {
    # parse arguments of command when reading file in GPSMan format
    global LFilePFrmt LFileDatum LFileLNo LFileCreat LFileId LFileOther \
	LFileEOF LFileDefs LFilePath LFileOrigin LFileScale LFileIxPath \
	LFileTimeOff MESS FromTimeOffset

    switch $commd {
	RT {
	    if { [set tix [string first "\t" $line]] != -1 } {
		set LFileId($file) [string range $line 0 [expr $tix-1]]
		# comment and attributes
		set LFileOther($file) [string range $line [expr $tix+1] end]
	    } else {
		set LFileId($file) $line
		set LFileOther($file) ""
	    }
	    return RT
	}
	RS {
	    set l [split $line \t]
	    if { [llength $l] >= 2 } {
		set d [list [lindex $l 0] [lindex $l 1] [lrange $l 2 end]]
		set LFileOther($file) [FormData RS "commt label hidden" $d]
		return RS
	    }
	}
	TR -  LN -  LAP {
	    set l [split $line \t]
	    set LFileId($file) [lindex $l 0]
	    # attributes or other fields
	    set LFileOther($file) [lreplace $l 0 0]
	    return $commd
	}
	GR -  GRWP -  GRRT -  GRTR -  GRLN -  GRLAP -
	GRGR {
	    set LFileId($file) [lindex [split $line \t] 0]
	    return $commd
	}
	nb {
	    set t $line
	    while { [set line [ReadFileNL $file]] != "" && \
		    ! $LFileEOF($file) } {
		set t [format "%s\n%s" $t $line]
	    }
	    set LFileOther($file) [string trim $t " \n\t"]
	    return nb
	}
	format {
	    if { [set pf [FindPFormat $line]] != "BAD" } {
		set line [ChopInString $pf $line]
		if { [scan $line "%f" off] == 1 && abs($off) < 14.5 } {
		    if { $LFileTimeOff($file) != "" && \
			     $off != $LFileTimeOff($file) } {
			GMMessage $MESS(twotimeoffsets)
		    } else {
			regsub {[+-]?[0-9]+\.?[0-9]*} $line "" line
			set line [string trimleft $line "\ \t"]
			if { [DatumRefId $line] != -1 } {
			    set LFilePFrmt($file) $pf
			    if { $FromTimeOffset != "" } {
				set LFileTimeOff($file) $FromTimeOffset
			    } else { set LFileTimeOff($file) $off }
			    set LFileDatum($file) $line
			    set LFileDefs($file) 7
			    return format
			}
			GMMessage "$MESS(unkndatum): $line"
		    }
		}
	    }
	}
	dates {
	    switch $line {
		yes { set LFileCreat($file) 1 ; return dates }
		no { set LFileCreat($file) 0 ; return dates }
	    }
	}	
	pformat {
	    if { [set pf [FindPFormat $line]] != "BAD" } {
		set LFilePFrmt($file) $pf
		set LFileDefs($file) [expr $LFileDefs($file)|6]
		return pformat
	    }
	}
	datum -  pfdatum {
	    if { [DatumRefId $line] != -1 } {
		set LFileDatum($file) $line
		set LFileDefs($file) [expr $LFileDefs($file)|1]
		return $commd
	    }
	    GMMessage "$MESS(unkndatum): $line"
	}
	mapback {
	    if { $line != "" } {
		set LFilePath($file) $line
		return mapback
	    }
	}
	mapproj {
	    if { [FilePTComm $file Proj PROJ $line] } {
		# LFileProj set by proc FilePTComm
		return mapproj
	    }
	}
	maptransf {
	    if { [FilePTComm $file Transf TRANSF $line] } {
		# LFileTransf set by proc FilePTComm
		return maptransf
	    }
	}
	maporigin {
	    GMMessage $MESS(oldfilefmt)
	    return BADC
	}
	mapscale {
	    if { [scan $line %f s] == 1 && $s > 0 } {
		set LFileScale($file) $s
		return mapscale
	    }
	}
	mapbackat -  mapbackcs {
	    if { [scan $line %d,%d%s x y p] == 3 } {
		set LFileIxPath($file) [list $x $y $p]
		return $commd
	    }
	    GMMessage "$MESS(mbkbadat): $line"
	}
    }
    return BADC
}

proc ProcCommand {line file} {
    # parse command when reading file in GPSMan format
    global FCOMMAND FCOMMARGS

    if { [string first ! $line] } { return BAD }
    foreach t "WP TS LS mapinfo" {
	if { $line == $FCOMMAND($t) } { return $t }
    }
    foreach c $FCOMMARGS {
	if { [string first $FCOMMAND($c) $line] == 0 } {
	    if { $c != "RS" } {
		set line [ChopInString $FCOMMAND($c) $line]
	    } else {
		# first 2 fields may be empty: delete only command and 1st tab
		set line [string range "$line" \
			    [string length "$FCOMMAND($c)"] end]
		regsub {[ ]*\t} $line "" line
	    }
	    return [FindArgs $c $line $file]
	}
    }
    return BADC
}

proc FindCommand {file} {
    # parse command when reading from file in GPSMan format
    global LFileEOF

    while { 1 } {
	set m [ReadFile $file]
	if { $LFileEOF($file) } { return EOF }
	set m [string trim [CleanLine $m 0] "\t"]
	if { [string length $m]>0 && ! [regexp {^%.*} $m] } {
	    return [ProcCommand $m $file]
	}
    }
}

proc ReadNB {file} {
    # parse NB when reading from file in GPSMan format
    global LFileBuffFull LFileOther

    if { [FindCommand $file] != "nb" } {
	set LFileBuffFull($file) 1
	return ""
    }
    return $LFileOther($file)
}

proc FindHeader {file} {
    # parse commands at the beginning of file in GPSMan format;
    #  succeed if "format" command found, which may be preceded by
    #  "dates", "position format" or "datum" commands; fail otherwise

    while { 1 } {
	switch [FindCommand $file] {
	    format { return 1 }
	    dates -		
	    pformat -
	    datum { }
	    default { return 0 }
	}
    }
}

proc Clean {coord} {
    # replace degree, minute and second characters by space in coordinate

    regsub -all {['"]} $coord "\ " nc
    # replace degree symbol
    while { [set i [string first \xb0 $nc]] != -1 } {
	set nc \
	  "[string range $nc 0 [expr $i-1]] [string range $nc [expr $i+1] end]"
    }
    return $nc
}

proc ReadPosn {fs i file} {
    # parse position when reading file in GPSMan format
    #  $fs is list of fields, and position starts at index $i
    # sets $PosnLength to number of fields
    global LFilePFrmt LFileDatum PosnLength MESS TXT POSTYPE

    set pformt $LFilePFrmt($file)
    switch [set ptype $POSTYPE($pformt)] {
	latlong {
	    set PosnLength 2
	    set lat [Clean [lindex $fs $i]]
	    set long [Clean [lindex $fs [expr $i+1]]]
	    if { ! [CheckLat GMMessage $lat $pformt] || \
		 ! [CheckLong GMMessage $long $pformt] } {
	       return -1
	    }
	    set latd [Coord $pformt $lat S]
	    set longd [Coord $pformt $long W]
	    return [list $latd $longd $lat $long]
	}
	utm {
	    set PosnLength 4
	    set ze [lindex $fs $i] ; incr i
	    set zn [lindex $fs $i] ; incr i
	    set eng [lindex $fs $i] ; incr i
	    set nng [lindex $fs $i]
	    if { ! [CheckZE GMMessage $ze] || \
		 ! [CheckZN GMMessage $zn] || \
		 ! [CheckNumber GMMessage $eng] || \
		 ! [CheckNumber GMMessage $nng] } {
	       return -1
	    }
	    set pd [UTMToDegrees $ze $zn $eng $nng $LFileDatum($file)]
	    return [lappend pd $ze $zn $eng $nng]
	}
	grid -
	nzgrid {
	    if { $ptype == "grid" } {
		set gr 1
		set zone [lindex $fs $i] ; incr i
		if { ! [CheckZone GMMessage $zone $pformt] } { return -1 }
	    } else { set gr 0 ; set zone "" }
	    set eng [lindex $fs $i] ; incr i
	    set nng [lindex $fs $i]
	    if { ! [CheckFloat GMMessage $eng] || \
		 ! [CheckFloat GMMessage $nng] } {
	       return -1
	    }
	    if { [BadDatumFor $pformt $LFileDatum($file) GMMessage] != 0 } {
		return -1
	    }
	    set pd [GridToDegrees $pformt $zone $eng $nng $LFileDatum($file)]
	    if { $pd == 0 } {
		GMMessage $MESS(outofgrid)
		return -1
	    }
	    if { $gr } {
		set PosnLength 3
		return [lappend pd $zone $eng $nng]
	    } else {
		set PosnLength 2
		return [lappend pd $eng $nng]
	    }
	}
	mh {
	    set PosnLength 1
	    set mhl [lindex $fs $i]
	    if { ! [CheckMHLocator GMMessage $mhl] } { return -1 }
	    return [linsert [MHLocToDegrees $mhl] end $mhl]
	}
    }
}

proc LoadAttrPairs {wh fields ix hidden} {
    # parse attribute-value pairs in list $fields from index $ix
    #  $wh in {WP, RT, TR, LN}
    #  $hidden is 0 if no hidden attributes are allowed
    # return pair with info on open attributes and list of hidden attributes
    #  or "" on error
    global FATTRPAIRS MESS DataIndex

    set pairs $FATTRPAIRS($wh)
    set os "" ; set hs ""
    foreach f [lrange $fields $ix end] {
	set fp [split $f =] ; set l [llength $fp]
	set fn [lindex $fp 0]
	if { [set i [lsearch -glob $pairs "$fn *"]] == -1 } {
	    if { $hidden } {
		lappend hs $f
	    } else {
		GMMessage [format $MESS(badattr) $wh $f]
		return ""
	    }
	} elseif { $l > 2 } {
	    GMMessage [format $MESS(badattr) $wh $f]
	    return ""
	} else {
	    set fd [lindex $pairs $i]
	    set fv [lindex $fp 1]
	    if { [[lindex $fd 3] $fv] } {
		GMMessage [format $MESS(badattrval) $wh $f]
		return ""
	    }
	    lappend os [list $DataIndex([lindex $fd 1]) $fv]
	}
    }
    return [list $os $hs]
}

proc InsertAttrPairs {as data} {
    # insert information from attribute-value pairs into data list

    foreach p $as {
	set k [lindex $p 0]
	set data [lreplace $data $k $k [lindex $p 1]]
    }
    return $data
}

proc LoadWPs {file outRT importing how} {
    # parse WPs data from file
    #  $outRT set if not in the context of reading a RT
    #  $importing set if reading from file in GPStrans instead of GPSMan format
    #  $how in {normal, inGR, inRTGR, void} meaning: keep all data, keep
    #    data for WPs with indices in $LFileIxs($file,WP), save data to
    #    LFileWPs($file) for possible use in RTs in GRs, discard data
    # if ! $outRT && ! $importing assume $LFileId($file) has the identifier
    #  of the RT being loaded
    global LFileLNo LFileBuffFull LFileCreat LFilePFrmt \
	LFileOther LFileDatum LFileEOF LFileVisible LFileWPs LFileIxs \
	LFileRTStages LFileId LFileTimeOff PosnLength DEFAULTSYMBOL \
	DEFAULTDISPOPT CREATIONDATE MESS DataIndex TimeOffset DateFormat


    if { ! $importing } {
	if { $LFileCreat($file) && $LFileTimeOff($file) == "" } {
	    GMMessage $MESS(notimeoffset)
	    set LFileTimeOff($file) 0
	}
	set adjtimeoff [expr $TimeOffset != $LFileTimeOff($file)]
	set adjsecs [expr int(3600*($LFileTimeOff($file)-$TimeOffset))]
    } else { set adjtimeoff 0 }
    
    set wps "" ; set ix -1 ; set count 0
    while { 1 } {
	if { [SlowOpAborted] } { return "" }
	set m [ReadFile $file]
	if { $LFileEOF($file) } { return $wps }
	if { [set m [CleanLine $m $importing]] == "" } { continue }
	if { ! $importing && [string first ! $m] == 0 } {
	    switch -glob [ProcCommand $m $file] {
		WP -  RT -  TR -  LN -  LAP -
		GR {
		    set LFileBuffFull($file) 1
		    return $wps
		}
		RS {
		    if { $outRT } {
			GMMessage "$MESS(badRS) $LFileLNo($file)"
			return ""
		    }
		    lappend LFileRTStages($file) \
			    [list $count $LFileOther($file)]
		}
		nb {
		    if { $how != "void" } {
			GMMessage "$MESS(badcommdWP) $LFileLNo($file)"
			return ""
		    }
		}
		GR?? -
		map* -
		BADC {
		    GMMessage "$MESS(badcommdWP) $LFileLNo($file)"
		    return ""
		}
	    }
	    continue
	}
	if { $how == "void" } { continue }
	if { $importing } {
	    if { [regexp {^[RT].*} $m] } {
		set LFileBuffFull($file) 1
		return $wps
	    }
	    if { [string first W $m] != 0 && [string first \t $m] != 1 } {
		GMMessage "$MESS(notabsWP) $LFileLNo($file)"
		return ""
	    }
	    set m [string range $m 2 end]
	}
	set as [split $m \t]
	set n [llength $as]
	if { $n == 1 } {
	    if { $importing || $outRT } {
		set m notabsWP
	    } else {
		# WP name only, in a RT in GPSMan format
		#  this is no longer supported
		set m RTnoWPname
	    }
	    GMMessage "$MESS($m) $LFileLNo($file)"
	    return ""
	}
	if { $n < [expr 3+$LFileCreat($file)] } {
	    GMMessage "$MESS(nofieldsWP) $LFileLNo($file)"
	    return ""
	}
	set name [string trim [lindex $as 0] " "]
	if { ! [CheckName Ignore $name] } {
	    if { [set nname [AskForName $name]] == "" } { return "" }
	    set chgdname $name
	    set name $nname
	} else { set chgdname "" }
	set commt [string trim [lindex $as 1] " "]
	if { ! [CheckComment Ignore $commt] } {
	    set commt [MakeComment $commt]
	}
	set i 2
	if { $LFileCreat($file) && [incr i] && \
		 [set date [lindex $as 2]] != "" } {
	    if { [set sd [ScanDate $date]] == "" } {
		GMMessage [format $MESS(baddateas) $date]
		return ""
	    }
	    if { $adjtimeoff } {
		set secs [expr [eval DateToSecs $sd]+$adjsecs]
		set sd [DateIntsFromSecs $secs]
		set date [eval FormatDate $DateFormat $sd]
	    }
	} else { set date [Now] }
	set posn [ReadPosn $as $i $file]
	if { $posn == -1 } { return "" }
	incr i $PosnLength
	set hs ""
	if { $n > $i } {
	    if { $importing } {
		GMMessage "$MESS(excfieldsWP) $LFileLNo($file)"
		return ""
	    }
	    # optional attribute-value pairs
	    if { [set as [LoadAttrPairs WP $as $i 1]] == "" } { return "" }
	    set hs [lindex $as 1] ; set as [lindex $as 0]
	} else { set as "" }
	set fs "Name Commt PFrmt Posn Datum Date Hidden"
	set data [FormData WP $fs [list $name $commt $LFilePFrmt($file) $posn \
			             $LFileDatum($file) $date $hs]]
	if { $chgdname != "" } {
	    set data [AddOldNameToObs WP $data $chgdname]
	}
	set data [InsertAttrPairs $as $data]
	# check for a NB
	if { ! $importing } {
	    set m [CleanLine [ReadFile $file] 0]
	    while { ! $LFileEOF($file) && [string length $m]==0 } {
		set m [CleanLine [ReadFile $file] 0]
	    }
	    if { ! $LFileEOF($file) && [string first ! $m]==0 && \
		    [ProcCommand $m $file] == "nb" } {
		set i $DataIndex(WPObs)
		set data [lreplace $data $i $i $LFileOther($file)]
	    } else {
		set LFileBuffFull($file) 1
	    }
	}
	set ix [IndexNamed WP $name]
	switch $how {
	    normal {
		set name [StoreWP $ix $name $data $LFileVisible($file)]
		lappend wps $name
	    }
	    inGR {
		if { [lsearch -exact $LFileIxs($file,WP) $ix] != -1 } {
		    set name [StoreWP $ix $name $data $LFileVisible($file)]
		    lappend wps $name
		}
	    }
	    inRTGR {
		lappend LFileWPs($file) [list $name $ix $data]
		lappend wps $name
	    }
	}
	incr count
    }
    # not used
    return
}

proc BadRTRead {file importing how} {
    # parse RTs data from file in GPSMan or GPStrans format
    #  $how in {normal, inGR, void}: keep data on all RTs, keep data for RTs
    #    with indices in $LFileIxs($file,RT), discard data
    global LFileLNo LFileId LFileOther LFileVisible LFileIxs LFileWPs \
	    LFileRTStages MAXWPINROUTE MESS

    # this global is used in LoadWPs
    set id $LFileId($file)
    set as [split $LFileOther($file) "\t"]
    set commt [lindex $as 0] ; set as [lreplace $as 0 0]
    if { ! [CheckComment Ignore $commt] } { set commt [MakeComment $commt] }
    if { $as != "" } {
	if { [set as [LoadAttrPairs RT $as 0 0]] == "" } { return 1 }
	set as [lindex $as 0]
    }
    if { $importing } {
	set obs ""
    } else { set obs [ReadNB $file] }
    set LFileRTStages($file) ""
    switch $how {
	normal {
	    set wpshow normal
	}
	inGR {
	    set wpshow inRTGR
	}
	void {
	    LoadWPs $file 0 $importing void
	    return 0
	}
    }
    set wps [LoadWPs $file 0 $importing $wpshow]
    if { [SlowOpAborted] } { return 1 }
    set cstages $LFileRTStages($file)
    set n [llength $wps]
    if { $n == 0 } {
	GMMessage "$MESS(badWPsRT) $LFileLNo($file)"
	return 1
    }
    if { $n > $MAXWPINROUTE } {
	GMMessage [format $MESS(toomuchWPs) $MAXWPINROUTE]
    }
    set stages "" ; set i 1
    foreach cst $cstages {
	if { [SlowOpAborted] } { return 1 }
	if { [set ct [lindex $cst 0]] > $n || $ct == 0 } {
	    GMMessage "$MESS(badWPsRSs) $LFileLNo($file)"
	    return 1
	}
	while { $ct > $i } {
	    lappend stages ""  ;  incr i
	}
	lappend stages [lindex $cst 1] ; incr i
    }
    set ix [IndexNamed RT $id]
    if { $how == "inGR" } {
	if { [lsearch -exact $LFileIxs($file,RT) $ix] == -1 } { return 0 }
	set nwps ""
	foreach n $wps {
	    if { [SlowOpAborted] } { return 1 }
	    # that there is an entry for the WP was checked in LoadWPs
	    foreach e $LFileWPs($file) {
		if { [lindex $e 0] == $n } {
		    set name [StoreWP [lindex $e 1] $n [lindex $e 2] \
			                            $LFileVisible($file)]
		    lappend nwps $name
		    break
		}
	    }
	}
	set wps $nwps
    }
    set data [FormData RT "IdNumber Commt Obs WPoints Stages" \
	               [list $id $commt $obs $wps $stages]]
    set data [InsertAttrPairs $as $data]
    StoreRT $ix $id $data $wps $LFileVisible($file)
    return 0
}

proc LoadTPs {file importing e0} {
    # parse TR points data from file in either GPSMan or GPStrans format
    #  $e0 is the expected contents of first field (either "" or "T")
    # return list with list of TR points and list of indices (!=0) of TPs
    #  starting segments (always "" when importing)
    global LFileLNo LFileBuffFull LFilePFrmt LFileDatum LFileEOF LFileTimeOff \
	TimeOffset DateFormat MESS

    if { ! $importing } {
	if { $LFileTimeOff($file) == "" } {
	    GMMessage $MESS(notimeoffset)
	    set LFileTimeOff($file) 0
	}
	set adjtimeoff [expr $TimeOffset != $LFileTimeOff($file)]
	set adjsecs [expr int(3600*($LFileTimeOff($file)-$TimeOffset))]
    } else { set adjtimeoff 0 }

    set tps ""
    set tpn 0 ; set segsts ""
    while { 1 } {
	if { [SlowOpAborted] } { return "" }
	if { $importing } {
	    set line [ReadFileNL $file]
	    if { $LFileEOF($file) || $line == "" } { return [list $tps ""] }
	} else {
	    set line [ReadFile $file]
	    if { $LFileEOF($file) } { return [list $tps $segsts] }
	}
	if { [set line [CleanLine $line $importing]] == "" } { continue }
	if { ! $importing && [string first ! $line] == 0 } {
	    switch [ProcCommand $line $file] {
		format -  datum -  pformat -  dates - 
		WP -  RT -  TR -  LN -  LAP -
		GR {
		    set LFileBuffFull($file) 1
		    return [list $tps $segsts]
		}
		comment { }
		TS {
		    if { $tpn != 0 } { lappend segsts $tpn }
		}
		default {
		    GMMessage "$MESS(badcommdTP) $LFileLNo($file)"
		    return ""
		}
	    }
	    continue
	}
	incr tpn
	set fs [split $line \t]
	if { [lindex $fs 0] != $e0 } {
	    GMMessage "$MESS(badTP) $LFileLNo($file)"
	    return ""
	}
	set ad error ; set dep ""
	if { [set l [llength $fs]] == 4 } {
	    set ad ""
	} elseif { ! $importing } {
	    if { $l == 5 } {
		set ad [lindex $fs 4]
		if { [BadAltitude $ad] } { set ad error }
	    } elseif { $l == 6 } {
		# "?"s used in 5.0, 5.1
		if { [set ad [lindex $fs 4]] == "?" } {
		    set ad ""
		} elseif { [BadAltitude $ad] } { set ad error }
		set dep [lindex $fs 5]
		if { $dep != "?" && [BadAltitude $dep] } { set ad error }
	    }
	}
	if { $ad == "" } {
	    if { $dep == "" } {
		set ns "latd longd latDMS longDMS date secs"
	    } else {
		set ad $dep
		set ns "latd longd latDMS longDMS date secs depth"
	    }
	} elseif { $ad == "error" } {
	    GMMessage "$MESS(badTP) $LFileLNo($file)"
	    return ""
	} elseif { $dep == "" } {
	    set ns "latd longd latDMS longDMS date secs alt"
	} else {
	    set ad [list $ad $dep]
	    set ns "latd longd latDMS longDMS date secs alt depth"
	}
	if { [set sd [ScanDate [string trim [lindex $fs 1] " "]]] == "" } {
	    GMMessage [format $MESS(baddateas) [lindex $fs 1]]
	    return ""
	}
	set secs [eval DateToSecs $sd]
	if { $adjtimeoff } {
	    set secs [expr $secs+$adjsecs]
	    set sd [DateIntsFromSecs $secs]
	}
	set date [list [eval FormatDate $DateFormat $sd] $secs]
	set posn [ReadPosn $fs 2 $file]
	if { $posn == -1 } { return "" }
	if { $LFilePFrmt($file) != "DMS" } {
	    set posn [FormatLatLong [lindex $posn 0] [lindex $posn 1] DMS]
	}
	set tp [FormData TP $ns [concat $posn $date $ad]]
	lappend tps $tp
    }
    # not used
    return
}

proc BadTRRead {file importing how} {
    # parse TRs data from file in either GPSMan or GPStrans format
    #  $how in {normal, inGR, void}: keep data on all TRs, keep data for TRs
    #    with indices in $LFileIxs($file,TR), discard data
    global LFileLNo LFileDatum LFileId LFileOther LFileVisible LFileIxs MESS \
	    MYGPS

    set hs ""
    if { $importing } {
	set e0 T ; set obs "" ; set as ""
	after 1000 ; set id [Now]
    } else {
	set e0 ""
	set id $LFileId($file)
	if { [set as $LFileOther($file)] != "" } {
	    if { [set as [LoadAttrPairs TR $as 0 1]] == "" } {
		return 1
	    }
	    set hs [lindex $as 1] ; set as [lindex $as 0]
	    if { $hs != "" && $MYGPS == "Garmin" } {
		foreach "as hs" [HiddenRecover TR $as $hs] {}
	    }
	}
	set obs [ReadNB $file]
    }
    # save current datum: a new one may appear at the end of the track
    set datum $LFileDatum($file)
    set ltps [LoadTPs $file $importing $e0]
    if { [SlowOpAborted] } { return 1 }
    set tps [lindex $ltps 0]
    if { $tps != "" } {
	if { $how == "void" } { return 0 }
	set ix [IndexNamed TR $id]
	if { $how != "inGR" || \
		[lsearch -exact $LFileIxs($file,TR) $ix] != -1 } {
	    set data [FormData TR "Name Obs Datum TPoints SegStarts Hidden" \
		             [list $id $obs $datum $tps [lindex $ltps 1] $hs]]
	    set data [InsertAttrPairs $as $data]
	    StoreTR $ix $id $data $LFileVisible($file)
	}
	return 0
    }
    GMMessage "$MESS(badTPsTR) $LFileLNo($file)"
    return 1
}

proc LoadLPs {file} {
    # parse LN points data from file in GPSMan format
    # return list with list of LN points and list of indices (!=0) of LPs
    #  starting segments
    global LFileLNo LFileBuffFull LFilePFrmt LFileDatum LFileEOF MESS

    set lps ""
    set lpn 0 ; set segsts ""
    while { 1 } {
	if { [SlowOpAborted] } { return "" }
	set line [ReadFile $file]
	if { $LFileEOF($file) } { return [list $lps $segsts] }
	if { [set line [CleanLine $line 0]] == "" } { continue }
	if { [string first ! $line]==0 } {
	    switch [ProcCommand $line $file] {
		format -  datum -  pformat -  dates - 
		WP -  RT -  TR -  LN -  LAP -
		GR {
		    set LFileBuffFull($file) 1
		    return [list $lps $segsts]
		}
		comment { }
		LS {
		    if { $lpn != 0 } { lappend segsts $lpn }
		}
		default {
		    GMMessage "$MESS(badcommdLP) $LFileLNo($file)"
		    return ""
		}
	    }
	    continue
	}
	incr lpn
	set fs [split $line \t]
	foreach "b posn alt" $fs { break }
	foreach "latd longd" $posn { break }
	if { $b != "" || [llength $fs] != 3 || \
		! [CheckLat GMMessage $latd DDD] || \
		! [CheckLong GMMessage $longd DDD] || \
		($alt != "" && [BadAltitude $alt]) } {
	    GMMessage "$MESS(badLP) $LFileLNo($file)"
	    return ""
	}
	# no check on user visible position, for speed
	lappend lps [FormData LP "posn alt" [list $posn $alt]]
    }
    # not used
    return
}

proc BadLNRead {file importing how} {
    # parse LNs data from file in GPSMan format
    #  $how in {normal, inGR, void}: keep data on all LNs, keep data for LNs
    #    with indices in $LFileIxs($file,LN), discard data
    global LFileLNo LFileDatum LFileId LFileOther LFileVisible LFileIxs \
	    LFilePFrmt MESS

    set id $LFileId($file)
    if { [set as [LoadAttrPairs LN $LFileOther($file) 0 0]] == "" } {
	return 1
    }
    set as [lindex $as 0]
    set obs [ReadNB $file]
    # save current datum: a new one may appear at the end of the line
    set datum $LFileDatum($file)
    if { [SlowOpAborted] } { return 1 }
    set lpss [LoadLPs $file]
    set lps [lindex $lpss 0]
    if { $lps != "" } {
	if { $how == "void" } { return 0 }
	set ix [IndexNamed LN $id]
	if { $how != "inGR" || \
		[lsearch -exact $LFileIxs($file,LN) $ix] != -1 } {
	    set pfrmt $LFilePFrmt($file)
	    set data [FormData LN "Name Obs Datum PFrmt LPoints SegStarts" \
  		    [list $id $obs $datum $pfrmt $lps [lindex $lpss 1]]]
	    set data [InsertAttrPairs $as $data]
	    StoreLN $ix $id $data $LFileVisible($file)
	}
	return 0
    }
    GMMessage "$MESS(badLPsLN) $LFileLNo($file)"
    return 1
}

proc BadLAPRead {file importing how} {
    # parse LAP data from file in GPSMan format
    #  $how in {normal, inGR}: keep data on all LAPs, keep data for LAPs
    #    with indices in $LFileIxs($file,LAP)
    global LFileLNo LFileDatum LFileId LFileOther LFilePFrmt LFileIxs \
	LFileTimeOff TimeOffset DateFormat MESS SUPPORTLAPS

    if { ! $SUPPORTLAPS } {
	GMMessage $MESS(lapncnsdrd)
	return 0
    }
    set id $LFileId($file)
    if { $LFileTimeOff($file) == "" } {
	GMMessage $MESS(notimeoffset)
	set LFileTimeOff($file) 0
    }
    if { [set sd [ScanDate $id]] == "" } {
	GMMessage [format $MESS(baddateas) $id]
	return ""
    }
    set secs [eval DateToSecs $sd]
    if { $TimeOffset != $LFileTimeOff($file) } {
	set secs [expr $secs+int(3600*($LFileTimeOff($file)-$TimeOffset))]
	set id [eval FormatDate $DateFormat [DateIntsFromSecs $secs]]
    }
    set start [list $id $secs]
    set n [llength [set as $LFileOther($file)]]
    foreach "dur dist begp endp cals trix" $as { break }
    if { $n != 6 || ! [CheckTime GMMessage $dur] || \
	     ( $dist != "" && ! [CheckFloat GMMessage $dist] ) || \
	     ( $begp != "" && [set begp [ReadPosn $begp 0 $file]] == -1 ) || \
	     ( $endp != "" && [set endp [ReadPosn $endp 0 $file]] == -1 ) || \
	     ( $cals != "" && ! [CheckNumber GMMessage $cals] ) || \
	     ( $trix != "" && \
		   ( ! [CheckNumber GMMessage $trix] || $trix > 255 )) } {
	GMMessage "$MESS(badLAP) $LFileLNo($file)"
	return 1
    }
    set ix [IndexNamed LAP $id]
    if { $how != "inGR" || \
	     [lsearch -exact $LFileIxs($file,LAP) $ix] != -1 } {
	set pfrmt $LFilePFrmt($file) ; set datum $LFileDatum($file)
	set obs [ReadNB $file]
	set data [FormData LAP \
	      "Name Obs Start Dur Dist BegPosn EndPosn Cals TRIx PFrmt Datum" \
		  [list $id $obs $start $dur $dist $begp $endp $cals $trix \
		       $pfrmt $datum]]
	StoreLAP $ix $id $data
    }
    return 0
}

proc LoadGRElements {file id} {
    # parse GR elements from file in GPSMan format
    #  $id is name of GR
    global LFileEOF LFileLNo LFileId LFileBuffFull TYPES SUPPORTLAPS MESS

    set gtypes [linsert $TYPES end LAP]
    foreach wh $gtypes { set l($wh) "" }
    set wh 0
    while 1 {
	if { [SlowOpAborted] } { return "" }
	set line [ReadFile $file]
	if { $LFileEOF($file) } { break }
	set line [CleanLine $line 0]
	if { $line != "" } {
	    if { [string first ! $line] == 0 } {
		switch -glob [set c [ProcCommand $line $file]] {
		    format -  datum -  pformat -  dates - 
		    WP -  RT -  TR -  LN -  LAP -
		    GR {
			set LFileBuffFull($file) 1
			break
		    }
		    GRWP {
			set wh WP
			set n [GetReplNameInGR $LFileId($file) $id]
			if { ! [CheckName Ignore $n] } {
			    GMMessage "$MESS(badGRel) $LFileLNo($file)"
			    return ""
			}
			lappend l(WP) $n
		    }
		    GR* {
			regsub GR $c "" wh
			lappend l($wh) $LFileId($file)
		    }
		    RS -
		    map* -
		    BADC {
			GMMessage "$MESS(badcommdGRel) $LFileLNo($file)"
			return ""
		    }
		}
	    } else {
		if { $wh == 0 } {
		    GMMessage "$MESS(notypeforGRel) $LFileLNo($file)"
		    return ""
		}
		set fs [split $line \t]
		if { [lindex $fs 0] != "" || [llength $fs]!=2 } {
		    GMMessage "$MESS(badGRel) $LFileLNo($file)"
		    return ""
		}
		set n [lindex $fs 1]
		if { $wh == "WP" } {
		    set n [GetReplNameInGR $n $id]
		    if { ! [CheckName Ignore $n] } {
			GMMessage "$MESS(badGRel) $LFileLNo($file)"
			return ""
		    }
		}
		lappend l($wh) $n
	    }
	}
    }
    set els ""
    foreach wh $gtypes {
	if { $l($wh) != "" && \
	     ( $wh != "LAP" || $SUPPORTLAPS ) } {
	    lappend els [list $wh $l($wh)]
	}
    }
    return $els
}

proc BadGRRead {file keep} {
    # parse GRs data from file in GPSMan format
    #  $keep is true if data is to be kept
    global LFileLNo LFileId LFileVisible MESS

    set id $LFileId($file) ; set obs [ReadNB $file]
    set els [LoadGRElements $file $id]
    if { [SlowOpAborted] } { return 1 }
    if { $els != "" } {
	if { $keep } {
	    set data [FormData GR "Name Obs Conts" [list $id $obs $els]]
	    StoreGR [IndexNamed GR $id] $id $data $LFileVisible($file)
	}
	return 0
    }
    GMMessage "$MESS(badGRels) $LFileLNo($file)"
    return 1
}

proc LoadAll {file} {
    # load all items from file in GPSMan format
    # return 0 on error
    global LFileLNo LFileDefs MESS FCOMMAND

    set r 0
    while { 1 } {
	if { [SlowOpAborted] } { return 0 }
	switch [set wh [FindCommand $file]] {
	    WP {
		if { $LFileDefs($file) != 7 } {
		    GMMessage $MESS(noheader)
		    break
		}
		LoadWPs $file 1 0 normal
		set r 1
	    }
	    RT -  TR -  LN -  LAP {
		if { $LFileDefs($file) != 7 } {
		    GMMessage $MESS(noheader)
		    break
		}
		if { [Bad${wh}Read $file 0 normal] } { break }
		set r 1
	    }
	    GR {
		if { [BadGRRead $file 1] } { break }
		set r 1
	    }
	    GRWP -  GRRT -  GRTR -  GRLN -  GRLAP -
	    GRGR {
		GMMessage "$MESS(GRelout) $LFileLNo($file)"
	    }
	    format -
	    dates -
	    pformat -
	    datum { }
	    EOF { set r 1 ; break }
	    pfdatum -  NBC -
	    BAD {
		GMMessage "$MESS(loaderr) $LFileLNo($file)"
		break 
	    }
	    BADC {
		GMMessage "$MESS(unkncommd) $LFileLNo($file)"
		break 
	    }
	}
    }
    return $r
}

proc LoadFile {what} {
    # open file in GPSMan format and load all items in it
    #  $what in $TYPES or $what=="LAP" or $what==Data identifies menu
    #  (see proc LoadAll)
    # return 0 on error
    global LChannel

    if { [InitWPRenaming] == 0 || [OpenInputFileFails $what GPSMan] } {
	return 0
    }
    set r [LoadAll $LChannel($what)]
    CloseInputFile $what
    EndWPRenaming
    return $r
}

proc LoadFileFrom {f what} {
    # open file in GPSMan format and load all items in it
    #  $what in $TYPES or $what=="LAP" or $what==Data
    #  (see proc LoadAll)
    # return 0 on error
    global LChannel CREATIONDATE SHOWFILEITEMS

    if { [InitWPRenaming] == 0 } { return 0 }
    InitInputFileVars $f $what $CREATIONDATE $SHOWFILEITEMS
    set r [LoadAll $f]
    CloseInputFile $what
    EndWPRenaming
    return $r
}

proc LoadGRElsIn {types lixs fmt} {
    # load items of $types (part of $TYPES except GR) to replace
    #  items with given indices, with -1 meaning new items should be accepted
    #  $lixs is list of lists of indices (and maybe -1) aligned with $types
    # $fmt is "GPSMan"
    global LChannel LFileLNo LFileDefs LFileWPs LFileIxs MESS FCOMMAND TYPES

    set file $LChannel(GR)
    foreach wh $TYPES {
	set how$wh void
    }
    foreach wh $types ixs $lixs {
	set LFileIxs($file,$wh) $ixs
	set how$wh inGR
    }
    while { 1 } {
	switch [set wh [FindCommand $file]] {
	    WP {
		if { $LFileDefs($file) != 7 } {
		    GMMessage $MESS(noheader)
		    break
		}
		LoadWPs $file 1 0 $howWP
	    }
	    RT -  TR -  LN -  LAP {
		if { $LFileDefs($file) != 7 } {
		    GMMessage $MESS(noheader)
		    break
		}
		if { [Bad${wh}Read $file 0 [set how$wh]] } { break }
	    }
	    GR {
		if { [BadGRRead $file 0] } { break }
	    }
	    GRWP -  GRRT -  GRTR -  GRLN -  GRLAP -
	    GRGR {
		GMMessage "$MESS(GRelout) $LFileLNo($file)"
	    }
	    format -
	    dates -
	    pformat -
	    datum { }
	    EOF { break }
	    pfdatum -  NBC -
	    BAD {
		GMMessage "$MESS(loaderr) $LFileLNo($file)"
		break
	    }
	    BADC {
		GMMessage "$MESS(unkncommd) $LFileLNo($file)"
		break
	    }
	}
    }
    foreach wh $types {
	catch { unset LFileIxs($file,$wh) }
    }
    catch { unset LFileWPs($file) }
    return
}

proc LoadGREls {what} {
    # load data from file in GPSMan format according to contents of GR(s)
    #  $what in $TYPES identifies menu, not being used
    global TYPES

    InputToGR $TYPES "" OpenInputFileFails LoadGRElsIn CloseInputFile GPSMan
    return
}

proc LoadMapFixedBk {path} {
    # try to load map background information
    # if file does not start with !Image command, return 0
    #  otherwise, return -1 on error or abort, or on success:
    #  "1 path datum pd td scale lixp lcsp" if new format
    #    Before release 5.3 this returned also
    #  "2 path datum origin scale lixp" if old format with !Origin: command
    #    but this command is not accepted anymore by proc FindArgs
    #  where
    #    pd, pt are pairs with name and list of pairs with parameter
    #     name and value
    #    lixp is list of grid indices and path for each subsidiary image in
    #     grid
    #    lcsp is list of canvas coordinates (NW) and path for each subsidiary
    #     image not in grid
    global LFileLNo LFileBuffFull LFileEOF LFilePath LFileDatum LFileDefs \
	LFileProj LFileTransf LFileOrigin LFileScale LFileIxPath LFileTimeOff \
	MESS TXT

    if { $path == "" } {
	set f [GMOpenFile $TXT(loadfrm) MapBkInfo r]
    } else {
	if { [catch {set f [open $path r]}] } {
	    GMMessage "$MESS(cantread) $path"
	    return -1
	}
    }
    set r -1
    if { $f != ".." } {
	set LFileLNo($f) 0 ; set LFileBuffFull($f) 0 ; set LFileEOF($f) 0
	set LFileTimeOff($f) ""
	if { [FindCommand $f] != "mapback" } {
	    set r 0
	} else {
	    SetCursor . watch
	    set LFileDefs($f) 0
	    if { [FindCommand $f] != "datum" } {
		GMMessage $MESS(mbkbaddatum)
	    } else {
		switch [FindCommand $f] {
		    maporigin {
			set r [list 2 $LFilePath($f) $LFileDatum($f) \
				$LFileOrigin($f)]
		    }
		    mapproj {
			if { [FindCommand $f] != "maptransf" } {
			    GMMessage $MESS(mbkbadtransf)
			} else {
			    set r [list 1 $LFilePath($f) $LFileDatum($f) \
				    $LFileProj($f) $LFileTransf($f)]
			}
		    }
		    default {
			GMMessage $MESS(mbkbadproj)
		    }
		}
		if { $r != -1 } {
		    if { [FindCommand $f] != "mapscale" } {
			GMMessage $MESS(mbkbadscale)
			set r -1
		    } else {
			set lg "" ; set lc ""
			while 1 {
			    switch [FindCommand $f] {
				mapbackat {
				    lappend lg $LFileIxPath($f)
				}
				mapbackcs {
				    lappend lc $LFileIxPath($f)
				}
				default {
				    break
				}
			    }
			}
			lappend r $LFileScale($f) $lg $lc
		    }
		}
	    }
	    ResetCursor .
	}
	foreach v "LFileLNo($f) LFileBuffFull($f) LFileEOF($f) LFilePath($f) \
		LFileDefs($f) LFileDatum($f) LFileProj($f) LFileTransf($f) \
		LFileOrigin($f) LFileScale($f) LFileIxPath($f) \
                LFileTimeOff($f)" {
	    catch { unset $v }
	}
	close $f
    }
    return $r
}

proc LoadMapInfo {path} {
    # try to load map information
    # if file does not start with !Map command, return 0
    #  otherwise, return -1 on error or abort, or on success a list with
    #    datum pd td pformat pfdatum scale 
    #  where
    #    pd, pt are pairs with name and list of pairs with parameter
    #     name and value for projection and transformation
    # for files generated by 6.1.2 or older $pfdatum defaults to $datum
    global LFileLNo LFileBuffFull LFileEOF LFileDatum LFileDefs \
	    LFileProj LFileTransf LFilePFrmt LFileScale LFileTimeOff MESS TXT

    if { $path == "" } {
	set f [GMOpenFile $TXT(loadfrm) MapInfo r]
    } else {
	if { [catch {set f [open $path r]}] } {
	    GMMessage "$MESS(cantread) $path"
	    return -1
	}
    }
    set r -1
    if { $f != ".." } {
	set LFileLNo($f) 0 ; set LFileBuffFull($f) 0 ; set LFileEOF($f) 0
	set LFileTimeOff($f) ""
	if { [FindCommand $f] != "mapinfo" } {
	    GMMessage $MESS(badmapinfo)
	} else {
	    SetCursor . watch
	    set LFileDefs($f) 0
	    if { [FindCommand $f] != "datum" } {
		GMMessage $MESS(mbkbaddatum)
	    } elseif { [FindCommand $f] != "mapproj" || \
			   [FindCommand $f] != "maptransf" || \
			   [FindCommand $f] != "pformat" } {
		GMMessage $MESS(badmapinfo)
	    } else {
		set datum $LFileDatum($f)
		if { [set c [FindCommand $f]] != "pfdatum" } {
		    # file by 6.1.2 or older
		    set LFileDatum($f) $datum
		} else { set c [FindCommand $f] }
		if { $c != "mapscale" } {
		    GMMessage $MESS(badmapinfo)
		} else {
		    set r [list $datum $LFileProj($f) $LFileTransf($f) \
			$LFilePFrmt($f) $LFileDatum($f) $LFileScale($f)]
		}
	    }
	}
	ResetCursor .
	foreach v "LFileLNo($f) LFileBuffFull($f) LFileEOF($f) \
		LFileDefs($f) LFileDatum($f) LFileProj($f) LFileTransf($f) \
		LFilePFrmt($f) LFileScale($f) LFileTimeOff($f)" {
	    catch { unset $v }
	}
	close $f
    }
    return $r
}

array set LSqIStates {
    start       {format datum pformat}
    start,nxt   {point pformat datum}
    pformat     {pformat}
    pformat,nxt {datum}
    datum       {datum}
    datum,nxt   {pformat}
    point       {}
}

proc LoadLeastSquaresInfo {} {
    # read geodetic and pixel coordinates of points for geo-referencing
    #  a map image using a least squares fit
    # file starts with optional datum and position format commands, or a
    #  format command, (defaults are "WGS 84" and DDD), followed by a line
    #  for each point with fields separated by tabulations; such lines
    #  either start with a $FCOMMAND(WP) field, followed by fields with
    #  an existing WP name and the pixel coordinates (x, y, with y
    #  increasing downwards), or have a position, in the selected/default
    #  datum and format, and the pixel coordinates (as before)
    # the minimum number of points is given by the global
    #    $MAPTRANSFNPTS(LeastSquares) defined in metadata.tcl
    # return 0 on failure, or a list with list of latd, longd and datum,
    #  a list of pairs with pixel coordinates (x, y, in Tcl sign convention
    #  with origin at upper left corner), a list with waypoints names (empty
    #  for explicit positions), and the input file path
    global LSqIStates LFileLNo LFileEOF LFileBuffFull LFileDatum LFileDefs \
	LFileDatum LFilePFrmt PosnLength TXT MESS MAPTRANSFNPTS FCOMMAND File \
	WPPosn WPDatum GFShowInfo GFVisible SHOWFILEITEMS

    set GFVisible $SHOWFILEITEMS
    set prms {GFShowInfo GFVisible}
    set pws [list "@$TXT(showfitinfo)" "@$TXT(mapfitWPs)"]
    set f [GMOpenFileParms $TXT(loadfrm) MapBkInfo r $prms $pws]
    if { $f == ".." } {
	return 0
    }
    set fpath $File(MapBkInfo)
    set LFileLNo($f) 0 ; set LFileBuffFull($f) 0 ; set LFileEOF($f) 0
    set LFileDefs($f) 0
    # defaults
    set LFileDatum($f) "WGS 84" ; set LFilePFrmt($f) DDD
    set state start ; set err 0
    set ldds "" ; set xys "" ; set names ""
    while { ! $LFileEOF($f) } {
	if { [set line [CleanLine [ReadFile $f] 0]] == "" } { continue }
	if { [string first ! $line] == 0 && \
		 [string first $FCOMMAND(WP) $line] != 0 } {
	    set c [ProcCommand $line $f]
	    if { [set ix [lsearch -exact $LSqIStates($state) $c]] == -1 } {
		incr err ; break
	    }
	    set state [lindex $LSqIStates($state,nxt) $ix]
	} else {
	    set state point
	    set fs [split $line \t]
	    if { [lindex $fs 0] == $FCOMMAND(WP) } {
		set name [string trim [lindex $fs 1]]
		if { ! [CheckName GMMessage $name] } { incr err ; break }
		if { [set ix [IndexNamed WP $name]] == -1 } {
		    GMMessage [format $MESS(undefinedWP) $name]
		    incr err ; break
		}
		set p $WPPosn($ix)
		set datum $WPDatum($ix)
		set fix 2
	    } elseif { [set p [ReadPosn $fs 0 $f]] != -1 } {
		set name ""
		set datum  $LFileDatum($f)
		set fix $PosnLength
	    } else { incr err ; break }
	    set x [lindex $fs $fix]
	    set y [lindex $fs [expr $fix+1]]
	    if { ! [CheckSignedFloat GMMessage $x] || \
		     ! [CheckSignedFloat GMMessage $y] } {
		incr err ; break
	    }
	    lappend ldds [list [lindex $p 0] [lindex $p 1] $datum]
	    lappend xys [list $x $y]
	    lappend names $name
	}
    }
    close $f
    if { $err } {
	GMMessage "$MESS(loaderr) $LFileLNo($f)"
	set r 0
    } elseif { [regexp {^>=([0-9]+)$} $MAPTRANSFNPTS(LeastSquares) x n] && \
		   [lindex $xys [expr $n-1]] == "" } {
	GMMessage [format $MESS(needNpoints) $n]
	set r 0
    } else {
	set r [list $ldds $xys $names $fpath]
    }
    foreach v "LNo EOF BuffFull Datum Defs Datum PFrmt" {
	catch { unset LFile${v}($f) }
    }
    return $r
}

## BSB contribution
proc LoadAutoMapInfo {} {
    # Load index of mapfiles and bounds

    global ImageIndex MESS TXT

    set f [GMOpenFile $TXT(loadfrm) MapBkInfo r]
    if { $f != ".." } {
	set index -1
	while {! [eof $f]} {
	    incr index
	    set line [gets $f]
	    set ImageIndex($index) [split $line "\t"]
	}
    }
    return
}