File: scat.c

package info (click to toggle)
wcstools 3.9.7-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,684 kB
  • sloc: ansic: 113,336; sh: 553; makefile: 245; lisp: 86; sed: 1
file content (4939 lines) | stat: -rw-r--r-- 145,483 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
/* File scat.c
 * January 24, 2017
 * By Jessica Mink, Harvard-Smithsonian Center for Astrophysics
 * Send bug reports to jmink@cfa.harvard.edu

   Copyright (C) 1996-2017
   Smithsonian Astrophysical Observatory, Cambridge, MA USA

   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 2
   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; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <math.h>
#include "libwcs/wcs.h"
#include "libwcs/lwcs.h"
#include "libwcs/fitsfile.h"
#include "libwcs/wcscat.h"

static char *RevMsg = "SCAT WCSTools 3.9.7, 26 April 2022, Jessica Mink (jmink@cfa.harvard.edu)";

static void PrintUsage();
	static int scatparm();
static void scatcgi();

static int ListCat ();
extern void setcenter();
static void SearchHead();
static int GetArea();
static void PrintGSClass();
static void PrintGSCBand();
static void PrintWebHelp();

static int verbose = 0;		/* Verbose/debugging flag */
static int afile = 0;		/* True to append output file */
static int wfile = 0;		/* True to print output file */
static int classd = -1;		/* Guide Star Catalog object classes */
static double maglim1 = MAGLIM1; /* Catalog bright magnitude limit */
static double maglim2 = MAGLIM2; /* Catalog faint magnitude limit */
static int sysout0 = 0;		/* Output coordinate system */
static double eqcoor = 0.0;	/* Equinox of search center */
static int degout0 = 0;		/* 1 if degrees output instead of hms */
static double ra0 = -99.0;	/* Initial center RA in degrees */
static double dec0 = -99.0;	/* Initial center Dec in degrees */
static double rad0 = 0.0;	/* Search box radius */
static double rad1 = 0.0;	/* Inner search annulus radius */
static double dra0 = 0.0;	/* Search box width */
static double ddec0 = 0.0;	/* Search box height */
static double epoch0 = 0.0;	/* Epoch for coordinates */
static double epoch1 = 0.0;	/* Earliest epoch for catalog search */
static double epoch2 = 0.0;	/* Latest epoch for catalog search */
static int syscoor = 0;		/* Input search coordinate system */
static int nstars = 0;		/* Number of brightest stars to list */
static int ndra = 3;		/* Number of decimal places in RA seconds */
static int nddec = 2;		/* Number of decimal places in Dec seconds */
static int nddeg = 7;		/* Number of decimal places in degree output */
static int printhead = 0;	/* 1 to print table heading */
static int printabhead = 1;	/* 1 to print tab table heading if no sources */
static int printprog = 0;	/* 1 to print program name and version */
static int printepoch = 0;	/* 1 to print epoch of entry */
static int nohead = 1;		/* 1 to print table heading */
static int searchcenter = 0;	/* 1 to print simpler format */
static int tabout = 0;		/* 1 for tab table to standard output */
static int catsort = SORT_UNSET; /* Default to sort stars by magnitude */
static int debug = 0;		/* True for extra information */
static char *objname;		/* Object name for output */
static char *keyword;		/* Column to add to tab table output */
static char *progname;		/* Name of program as executed */
static char *coorsys;		/* Coordinate system of search center */
static int match = 0;		/* If 1, match num exactly in BIN or ASC cats*/
static int printobj = 0;	/* If 1, print object name instead of number */
static char cpname[16];		/* Name of program for error messages */
static int oneline = 0;		/* If 1, print center and closest on 1 line */
static struct Star *srch;	/* Search center structure for catalog search */
static struct StarCat *srchcat; /* Search catalog structure */
static int readlist = 0;	/* If 1, search centers are from a list */
static int notprinted = 1;	/* If 1, print header */
static char *listfile;		/* Name of catalog file with search centers */
static int printxy = 0;		/* If 1, print X Y instead of object number */
static char *xstr, *ystr;	/* X and Y strings if printxy */
static int closest;		/* 1 if printing only closest star */
static double *gnum;		/* Catalog star numbers */
static double *gra;		/* Catalog star right ascensions */
static double *gdec;		/* Catalog star declinations */
static double *gpra;		/* Catalog star RA proper motions */
static double *gpdec;		/* Catalog star declination proper motions */
static double **gm;		/* Catalog magnitudes */
static double *gx;		/* Catalog star X positions on image */
static double *gy;		/* Catalog star Y positions on image */
static int *gc;			/* Catalog star object classes */
static char **gobj;		/* Catalog star object names */
static char **gobj1;		/* Catalog star object names */
static char **gpath;		/* Catalog star data pathnames */
static char **gpath1;		/* Catalog star data pathnames */
static int nalloc = 0;
static struct StarCat *starcat[5]; /* Star catalog data structure */
static double eqout = 0.0;	/* Equinox for output coordinates */
static int ncat = 0;		/* Number of reference catalogs to search */
static char *refcatname[5];	/* reference catalog names */
static char *ranges;		/* Catalog numbers to print */
static int http=0;		/* Set to one if http header needed on output */
static int padspt = 0;		/* Set to one to pad out long spectral type */
static int nmagmax = MAXNMAG;
static int sortmag = 0;
static int lofld = 0;		/* Length of object name field in output */
static int webdump = 0;
static int votab = 0;		/* If 1, print output as VOTable XML */
static int minid = 0;		/* Minimum number of plate IDs for USNO-B1.0 */
static int minpmqual = 0;	/* Minimum USNO-B1.0 proper motion quality */
static int rdra = 0;		/* If 1, dra is in ra units, not sky units */
static int idrun = 0;		/* If 1, 2MASS ID run from inside loop */
static char voerror[80];	/* Error for Virtual Observatory */
static int refcat = 0;		/* reference catalog switch */
static char title[80];	/* Title of reference Catalog */
static int nmag = 0;	/* Number of magnitudes in reference catalog */
static int mprop = 0;	/* 1 if proper motion in reference catalog */
static int sysref = 0;	/* Coordinate system of reference catalog */
static double eqref;	/* Equinox of catalog to be searched */
static double epref;	/* Epoch of catalog to be searched */
extern void setminpmqual();
extern void setminid();
extern void setrevmsg();

int
main (ac, av)
int ac;
char **av;
{
    FILE *fd;
    char *str, *str1;
    char rastr[32];
    char decstr[32];
    char line[200];
    char errmsg[256];
    int i, lcat;
    char *refcatn;
    char cs, cs1;
    int srchtype;
    char *newranges;
    int systemp = 0;		/* Input search coordinate system */
    int istar;
    char *blank;
    int imag;
    int lprop, lnmag;
    char listtitle[80];
    double epoch;
    char coorout[32];
    char *query;
    int ndcat;
    int lrange;
    char *rstr, *dstr, *astr, *cstr, *ccom, *cep2;

    ranges = NULL;
    keyword = NULL;
    objname = NULL;
    voerror[0] = (char) 0;
    for (i = 0; i < 5; i++)
	starcat[i] = NULL;

    /* Null out buffers before starting */
    gnum = NULL;
    gra = NULL;
    gdec = NULL;
    gpra = NULL;
    gpdec = NULL;
    gm = NULL;
    gx = NULL;
    gy = NULL;
    gc = NULL;
    gobj = NULL;
    gobj1 = NULL;
    gpath = NULL;
    gpath1 = NULL;
    setrevmsg (RevMsg);
    coorout[0] = (char) 0;

    /* Check name used to execute program and set catalog name accordingly */
    progname = ProgName (av[0]);
    for (i = 0; i < strlen (progname); i++) {
	if (progname[i] > 95 && progname[i] < 123)
	    cpname[i] = progname[i] - 32;
	else
	    cpname[i] = progname[i];
	}
    refcatn = ProgCat (progname);
    if (refcatn != NULL) {
	refcatname[ncat] = refcatn;
	ncat++;
	refcat = RefCat (refcatn,title,&sysref,&eqref,&epref,&mprop,&nmag);
	if (nmag > nmagmax)
	    nmagmax = nmag;
	ndcat = CatNdec (refcat);
	}
    else
	ndcat = -1;

    /* Set parameters from keyword=value arguments */
    if ((query = getenv ("QUERY_STRING")) != NULL && ac < 3) {
	http++;
        scatcgi (query);
	tabout++;
	}

    /* If not http and no arguments, print command list */
    else if (ac == 1)
        PrintUsage (NULL);

    /* Check for help or version command first */
    if (!http) {
	str = *(av+1);
	if (!str || !strcmp (str, "help") || !strcmp (str, "-help"))
	    PrintUsage (NULL);
	if (!strcmp (str, "version") || !strcmp (str, "-version"))
	    PrintUsage ("version");
	if (!strcasecmp (str, "webhelp"))
	    PrintWebHelp ();
	if (!strncasecmp (str, "band", 4) || !strncasecmp (str, "filt", 4))
	    PrintGSCBand ();
	if (!strncasecmp (str, "clas", 4) || !strncasecmp (str, "obje", 4))
	    PrintGSClass ();
	}

    /* crack arguments */
    for (av++; --ac > 0; av++) {

	/* Set parameters from keyword=value& arguments */
	if (strchr (*av, '&')) {
	    tabout++;
            scatcgi (*av);
	    }

	/* Set parameters from keyword=value arguments */
	else if (strchr (*av, '=')) {
            if (scatparm (*av))
		fprintf (stderr, "SCAT: %s is not a parameter.\n", *av);
	    refcat = CatCode (refcatname[ncat-1]);
	    if (nmag > nmagmax)
		nmagmax = nmag;
	    ndcat = CatNdec (refcat);
	    }

	/* Set search RA, Dec, and equinox if 2MASS ID */
	else if (tmcid (*av, &ra0, &dec0)) {
	    syscoor = WCS_J2000;
	    eqcoor = 2000.0;
	    if (epoch0 == 0.0)
		epoch0 = 2000.0;
	    if (eqout == 0.0)
		eqout = 2000.0;
	    idrun = 0;
	    ListCat (ranges, eqout);
	    idrun = 1;
	    ra0 = 0.0;
	    dec0 = 0.0;
	    }

	/* Set search RA, Dec, and equinox if colon in argument */
	else if (strsrch (*av,":") != NULL) {
	    if (ac < 2)
		PrintUsage (*av);
	    else {
		if (strlen (*av) < 32)
		    strcpy (rastr, *av);
		else {
		    strncpy (rastr, *av, 31);
		    rastr[31] = (char) 0;
		    }
		ac--;
		av++;
		if (strlen (*av) < 32)
		    strcpy (decstr, *av);
		else {
		    strncpy (decstr, *av, 31);
		    decstr[31] = (char) 0;
		    }
		ac--;
		ra0 = str2ra (rastr);
		dec0 = str2dec (decstr);
		if (ac < 1) {
		    syscoor = WCS_J2000;
		    eqcoor = 2000.0;
		    }
		else if ((syscoor = wcscsys (*(av+1))) >= 0) {
		    coorsys = *++av;
		    eqcoor = wcsceq (coorsys);
		    }
		else {
		    syscoor = WCS_J2000;
		    eqcoor = 2000.0;
		    }
		}
	    }

	/* Set range and make a list of star numbers from it */
	else if (isrange (*av)) {
	    if (ranges) {
		lrange = strlen(ranges) + strlen(*av) + 16;
		newranges = (char *) calloc (lrange, 1);
		strcpy (newranges, ranges);
		strcat (newranges, ",");
		strcat (newranges, *av);
		free (ranges);
		ranges = newranges;
		newranges = NULL;
		}
	    else {
		lrange = strlen(*av) + 16;
		ranges = (char *) calloc (lrange, 1);
		if (strchr (*av,'.'))
		    match = 1;
		strcpy (ranges, *av);
		}
	    if (strchr (*av, 'x') == NULL && ndcat > 0) {
		int n = ndcat;
		strcat (ranges, "x0.");
		while (n-- > 0)
		    strcat (ranges, "0");
		strcat (ranges, "1");
		}
	    }

	/* Set decimal degree center or star number */
	else if (isnum (*av)) {

	    if (ac > 1 && isnum (*(av+1))) {
		int ndec1, ndec2;

		/* Check for second number and coordinate system */
		if (ac > 2 && (systemp = wcscsys (*(av + 2))) > 0) {
		    rstr = *av++;
		    ac--;
		    dstr = *av++;
		    ac--;
		    cstr = *av;
		    eqcoor = wcsceq (cstr);
		    }

		/* Check for two numbers which aren't catalog numbers */
		else {
		    ndec1 = StrNdec (*av);
		    ndec2 = StrNdec (*(av+1));
		    if (ndcat > -1 && ndec1 == ndcat && ndec2 == ndcat)
			cstr = NULL;
		    else {
			rstr = *av++;
			ac--;
			dstr = *av;
			cstr = (char *) malloc (8);
			strcpy (cstr, "J2000");
			systemp = WCS_J2000;
			}
		    }
		}
	    else
		cstr = NULL;

	    /* Set decimal degree center */
	    if (cstr != NULL) {
		ra0 = atof (rstr);
		dec0 = atof (dstr);
		syscoor = systemp;
		eqcoor = wcsceq (cstr);
		}

	    /* Assume number to be star number if no coordinate system */
	    else {
		if (strchr (*av,'.'))
		    match = 1;
		if (ranges) {
		    lrange = strlen(ranges) + strlen(*av) + 2;
		    newranges = (char *)calloc (lrange, 1);
		    strcpy (newranges, ranges);
		    strcat (newranges, ",");
		    strcat (newranges, *av);
		    free (ranges);
		    ranges = newranges;
		    newranges = NULL;
		    }
		else {
		    lrange = strlen(*av) + 2;
		    ranges = (char *) calloc (lrange, 1);
		    strcpy (ranges, *av);
		    }
		}
	    }

	else if (*(str = *av) == '@') {
	    readlist = 1;
	    listfile = *av + 1;
	    }

	/* Otherwise, read command */
	else if ((*(str = *av)) == '-') {
	    char c;
	    while ((c = *++str))
	    switch (c) {

	    case 'v':	/* more verbosity */
		if (debug) {
		    webdump++;
		    debug = 0;
		    verbose = 0;
		    }
		else if (verbose)
		    debug++;
		else
		    verbose++;
		printprog++;
		break;

	    case 'a':	/* Get closest source */
		catsort = SORT_DIST;
		closest++;
		break;

    	    case 'b':	/* output coordinates in B1950 */
		sysout0 = WCS_B1950;
		eqout = 1950.0;
    		break;

	    case 'c':       /* Set reference catalog */
		if (ac < 2)
		    PrintUsage (str);
		lcat = strlen (*++av);
		refcatn = (char *) calloc (1, lcat + 2);
		strcpy (refcatn, *av);
		refcatname[ncat] = refcatn;
		refcat = RefCat (refcatn,title,&sysref,&eqref,&epref,&mprop,&nmag);
		if (refcat) {
		    if (nmag > nmagmax)
			nmagmax = nmag;
		    ndcat = CatNdec (refcat);
		    ncat = ncat + 1;
		    }
		ac--;
		break;

	    case 'd':	/* output in degrees instead of sexagesimal */
		degout0++;
		break;

	    case 'e':	/* Set ecliptic coordinate output */
		sysout0 = WCS_ECLIPTIC;
		break;

	    case 'f':	/* output in centering coordinates */
		searchcenter++;
		break;

	    case 'g':	/* Set galactic coordinate output and optional center */
		sysout0 = WCS_GALACTIC;
		break;

	    case 'h':	/* output descriptive header */
		if (tabout)
		    printabhead = 0;
		else {
		    printhead++;
		    printprog++;
		    }
		break;

	    case 'i':	/* ouput catalog object name instead of number */
		printobj++;
		if (!(*(str+1)) && ac > 1 && isnum (*(av+1))) {
		    lofld = atoi (*++av);
		    ac--;
		    }
		break;

    	    case 'j':	/* center coordinates on command line in J2000 */
		sysout0 = WCS_J2000;
		eqout = 2000.0;
    		break;

	    case 'k':	/* Keyword (column) to add to output from tab table */
		if (ac < 2)
		    PrintUsage (str);
		keyword = *++av;
		settabkey (keyword);
		ac--;
		break;

	    case 'l':	/* Print center and closest star on one line */
		oneline++;
		catsort = SORT_DIST;
		closest++;
		nstars = 1;
		break;

	    case 'm':	/* Magnitude limit */
		if (ac < 2)
		    PrintUsage (str);
		cs1 = *(str+1);
		if (cs1 != (char) 0) {
		    ++str;
		    if (cs1 > '9')
			sortmag = (int) cs1;
		    else
			sortmag = (int) cs1 - 48;
		    }
		av++;
		ac--;
		if ((ccom = strchr (*av, ',')) != NULL) {
		    *ccom = (char) 0;
		    maglim1 = atof (*av);
		    maglim2 = atof (ccom+1);
		    }
		else {
		    maglim2 = atof (*av);
		    if (ac > 1 && isnum (*(av+1))) {
			av++;
			ac--;
			maglim1 = maglim2;
			maglim2 = atof (*av);
			}
		    else if (MAGLIM1 == MAGLIM2)
			maglim1 = -2.0;
		    }
		break;

	    case 'n':	/* Number of brightest stars to read */
		if (ac < 2)
		    PrintUsage (str);
		nstars = atoi (*++av);
		ac--;
		break;

	    case 'o':	/* Object name */
		if (ac < 2)
		    PrintUsage (str);
		objname = *++av;
		ac--;
		break;

	    case 'p':	/* Sort by distance from center */
		catsort = SORT_DIST;
		printprog++;
		break;

    	    case 'q':	/* Output equinox in years */
    		if (ac < 2)
    		    PrintUsage (str);
		strcpy (coorout, *++av);
		if (coorout[0] == 'J' || coorout[0] == 'j')
		    sysout0 = WCS_J2000;
		else if (coorout[0] == 'B' || coorout[0] == 'b')
		    sysout0 = WCS_B1950;
		eqout = wcsceq (coorout);
    		ac--;
    		break;

    	    case 'r':	/* Search box or circle half-size in arcseconds */
    		if (ac < 2)
    		    PrintUsage (str);
		cs1 = *(str+1);
		if (cs1 != (char) 0)
		    ++str;
		av++;

		/* Separate 2 arguments for rectangles */
		if ((dstr = strchr (*av, ',')) != NULL) {
		    *dstr = (char) 0;
		    dstr++;
		    }

		/* Separate 2 arguments for annulus */
		if ((astr = strchr ((*av)+1, '-')) != NULL) {
		    *astr = (char) 0;
		    astr++;
		    }

		/* Convert radius or first argument to arcseconds */
		if (strchr (*av,':'))
		    rad0 = 3600.0 * str2dec (*av);
		else
		    rad0 = atof (*av);

		/* Convert outer radius of annulus to arcseconds */
		if (astr != NULL) {
		    rad1 = rad0;
		    if (strchr (astr,':'))
			rad0 = 3600.0 * str2dec (astr);
		    else
			rad0 = atof (astr);
		    }

		/* Convert second argument (=dec radius) to arcseconds */
		if (dstr != NULL) {
		    if (cs1 == 'r')
			rdra = 1;
		    else
			rdra = 0;
		    dra0 = rad0;
		    rad0 = 0.0;
		    if (strchr (dstr, ':'))
			ddec0 = 3600.0 * str2dec (dstr);
		    else
			ddec0 = atof (dstr);
		    if (ddec0 <= 0.0)
			ddec0 = dra0;
		    }
    		ac--;
    		break;

	    case 's':	/* sort by RA, Dec, magnitude or nothing */
		catsort = SORT_RA;
		if (ac > 1) {
		    str1 = *(av + 1);
		    cs = str1[0];
		    if (strchr ("ademinprs",(int)cs)) {
			cs1 = str1[1];
			av++;
			ac--;
			}
		    else
			cs = 'r';
		    }
		else
		    cs = 'r';
		switch (cs) {

		    /* Merge */
		    case 'e':
			catsort = SORT_MERGE;
			break;

		    /* Declination */
		    case 'd':
			catsort = SORT_DEC;
			break;

		    /* ID Number */
		    case 'i':
			catsort = SORT_ID;
			break;

		    /* Magnitude (brightest first) */
		    case 'm':
			catsort = SORT_MAG;
			if (cs1 != (char) 0) {
			    if (cs1 > '9')
				sortmag = (int) cs1;
			    else
				sortmag = (int) cs1 - 48;
			    }
			break;

		    /* No sorting */
		    case 'n':
			catsort = SORT_NONE;
			break;

		    /* Distance from search center (closest first) */
		    case 'a':
		    case 'p':
		    case 's':
			catsort = SORT_DIST;
			break;

		    /* Right ascension */
		    case 'r':
			catsort = SORT_RA;
			break;
		    default:
			catsort = SORT_RA;
		    }
		break;

	    case 't':	/* tab table to stdout */
		if (tabout) {
		    tabout = 0;
		    votab = 1;
		    degout0 = 1;
		    }
		else
		    tabout = 1;
		if (printhead) {
		    printabhead = 0;
		    printhead = 0;
		    printprog = 0;
		    }
		break;

	    case 'u':       /* Print following 2 numbers at start of line */
		if (ac > 2) {
		    printxy = 1;
		    xstr = *++av;
		    ac--;
		    ystr = *++av;
		    ac--;
		    }
		break;

    	    case 'w':	/* write output file */
    		wfile++;
    		break;

	    case 'x':       /* Guide Star object class */
		if (ac < 2)
		    PrintUsage (str);
		classd = (int) atof (*++av);
		setgsclass (classd);
		ac--;
		break;

	    case 'y':	/* Set output coordinate epoch */
		if (str[1] == 'j')
		    setdateform (EP_JD);
		else if (str[1] == 'm')
		    setdateform (EP_MJD);
		else if (str[1] == 'f')
		    setdateform (EP_FD);
		else if (str[1] == 'i')
		    setdateform (EP_ISO);
		else
		    setdateform (EP_EP);
		if (ac < 2)
		    PrintUsage (str);
		if ((cep2 = strchr (*(av+1), ','))) {
		    av++;
		    *cep2 = (char) 0;
		    cep2 = cep2 + 1;
		    if (strchr (*av, '.'))
			epoch1 = atof (*av);
		    else
			epoch1 = fd2ep (*av);
		    if (strchr (*av, '.'))
			epoch2 = atof (cep2);
		    else
			epoch2 = fd2ep (cep2);
		    ac--;
		    }
		else if (strchr (*(av+1), '.')) {
		    av++;
		    epoch0 = atof (*av);
		    ac--;
		    }
		else {
		    av++;
		    epoch0 = fd2ep (*av);
		    ac--;
		    }
		break;

	    case 'z':	/* Set append flag */
		afile++;
		wfile++;
		break;

	    default:
		sprintf (errmsg, "* Illegal command -%s-", *av);
		PrintUsage (errmsg);
		break;
	    }
	    }
	else {
	    lcat = strlen (*av);
	    refcatn = (char *) calloc (1, lcat + 2);
	    strcpy (refcatn, *av);
	    refcatname[ncat] = refcatn;
	    ncat = ncat + 1;
	    }
	}

    /* If 2MASS ID run from inside loop, quit now */
    if (idrun)
	exit (0);

    /* If no catalog has been specified, quit now */
    if (ncat < 1) {
	sprintf (errmsg, "* No catalog name given");
	PrintUsage (errmsg);
	}

    /* Set output equinox appropriately if output system is specified */
    if (eqout == 0.0) {
	if (eqcoor != 0.0)
	    eqout = eqcoor;
	if (sysout0 != 0) {
	    if (sysout0 == WCS_J2000)
		eqout = 2000.0;
	    if (sysout0 == WCS_B1950)
		eqout = 1950.0;
	    }
	}

    /* Set output epoch appropriately if output system is specified */
    if (sysout0 == 0) {
	if (strlen (coorout) > 0) {
	    sysout0 = wcscsys (coorout);
	    eqout = wcsceq (coorout);
	    }
	else if (syscoor != 0)
	    sysout0 = syscoor;
	}

    /* Set output equinox appropriately if output system is specified */
    if (eqout == 0.0) {
	if (eqcoor != 0.0)
	    eqout = eqcoor;
	if (sysout0 != 0) {
	    if (sysout0 == WCS_J2000)
		eqout = 2000.0;
	    if (sysout0 == WCS_B1950)
		eqout = 1950.0;
	    }
	}

    /* Set output epoch from output equinox if not otherwise set */
    if (epoch0 == 0.0)
	epoch0 = eqout;

    /* If http output, send header */
    if (votab) {
	printf ("Content-type: text/xml\n\n");
	printprog = 0;
	}
    else if (http) {
	printf ("Content-type: text/plain\n\n");
	}

    if (readlist) {

	/* Read search center list from starbase tab table catalog */
	if (istab (listfile)) {
	    ranges = NULL;
	    srchcat = tabcatopen (listfile, NULL, 10000);
	    if (srchcat != NULL) {
		srch = (struct Star *) calloc (1, sizeof (struct Star));
		for (istar = 1; istar <= srchcat->nstars; istar ++) {
		    if (tabstar (istar, srchcat, srch, verbose)) {
			if (verbose)
			    fprintf (stderr,"%s: Cannot read star %d\n",
				     cpname, istar);
                	break;
                	}
		    ra0 = srch->ra;
		    dec0 = srch->dec;
		    if (eqout > 0.0)
			eqcoor = eqout;
		    else
			eqcoor = srch->equinox;
		    if (epoch0 != 0.0)
			epoch = epoch0;
		    else
			epoch = srch->epoch;
		    if (sysout0)
			syscoor = sysout0;
		    else
			syscoor = srch->coorsys;
		    wcsconp (srch->coorsys, syscoor, srch->equinox, eqcoor,
			     srch->epoch,epoch,
			     &ra0,&dec0,&srch->rapm,&srch->decpm);
		    ListCat (ranges, eqout);
		    }
		tabcatclose (srchcat);
		}
	    }

	/* Read search center list from SAOTDC ASCII table catalog */
	else if (isacat (listfile)) {
	    ranges = NULL;
	    if (!(srchtype = RefCat (listfile,listtitle,&syscoor,&eqcoor,
				     &epoch,&lprop,&lnmag))) {
		if (lnmag > nmagmax)
		    nmagmax = lnmag;
		fprintf (stderr,"List catalog '%s' is missing\n", listfile);
		return (0);
		}
	    srchcat = ctgopen (listfile, srchtype);
	    if (srchcat != NULL) {
		srch = (struct Star *) calloc (1, sizeof (struct Star));
		for (istar = 1; istar <= srchcat->nstars; istar ++) {
		    if (ctgstar (istar, srchcat, srch)) {
			if (verbose)
			    fprintf (stderr,"%s: Cannot read star %d\n",
				 cpname, istar);
                	}
		    else {
			ra0 = srch->ra;
			dec0 = srch->dec;
			if (eqout > 0.0)
			    eqcoor = eqout;
			else
			    eqcoor = srch->equinox;
			if (epoch0 != 0.0)
			    epoch = epoch0;
			else
			    epoch = srch->epoch;
			if (sysout0)
			    syscoor = sysout0;
			else
			    syscoor = srch->coorsys;
			wcsconp (srch->coorsys, syscoor, srch->equinox, eqcoor,
				 srch->epoch,epoch,
				 &ra0,&dec0,&srch->rapm,&srch->decpm);
			ListCat (ranges, eqout);
			}
		    }
		ctgclose (srchcat);
		}
	    }
	else {
	    if (strcmp (listfile,"STDIN")==0 || strcmp (listfile,"stdin")==0)
                fd = stdin;
            else
                fd = fopen (listfile, "r");
            if (fd != NULL) {
                while (fgets (line, 200, fd)) {
		    blank = strchr (line, ' ');
		    if (blank)
			*blank = (char) 0;
		    blank = strchr (line, '\n');
		    if (blank)
			*blank = (char) 0;
		    ranges = (char *) calloc (strlen(line) + 1, 1);
		    match = 1;
		    strcpy (ranges, line);
		    ListCat (ranges, eqout);
		    }
		fclose (fd);
		}
	    }
	}
    else {
	if (sysout0 && !syscoor)
	    syscoor = sysout0;
	if (syscoor) {
	    if (eqout == 0.0) {
		if (eqcoor != 0.0)
		    eqout = eqcoor;
		else if (syscoor == WCS_B1950)
		    eqout = 1950.0;
		else
		    eqout = 2000.0;
		}
	    if (epoch0 == 0.0)
		epoch0 = eqout;
	    }
	ListCat (ranges, eqout);
	}

    for (i = 0; i < ncat; i++) {
	if (refcatname[i]) free (refcatname[i]);
	ctgclose (starcat[i]);
	}

    /* Free memory used for search results and return */
    if (gx) {
	free ((char *)gx);
	gx = NULL;
	}
    if (gy) {
	free ((char *)gy);
	gy = NULL;
	}
    if (gm) {
	for (imag = 0; i < nmagmax; i++) {
	    if (gm[imag]) {
		free ((char *)gm[imag]);
		gm[imag] = NULL;
		}
	    }
	free ((char *)gm);
	gm = NULL;
	}
    if (gra) {
	free ((char *)gra);
	gra = NULL;
	}
    if (gdec) {
	free ((char *)gdec);
	gdec = NULL;
	}
    if (gnum) {
	free ((char *)gnum);
	gnum = NULL;
	}
    if (gc) {
	free ((char *)gc);
	gc = NULL;
	}
    if (gobj) {
	for (i = 0; i < nalloc; i++) {
	    if (gobj[i] != NULL)
		free ((char *)gobj[i]);
	    }
	free ((char *)gobj);
	gobj = NULL;
	}
    if (gpath) {
	for (i = 0; i < nalloc; i++) {
	    if (gpath[i] != NULL)
		free ((char *)gpath[i]);
	    }
	free ((char *)gpath);
	gpath = NULL;
	}

    return (0);
}

static void
PrintUsage (command)

char	*command;	/* Command where error occurred or NULL */
{
    FILE *dev;	/* Output, stderr for command line, stdout for web */
    int catcode;
    char *srcname;
    char *catname;

    if (http) {
	dev = stdout;
	fprintf (dev, "Content-type: text/plain\n\n");
	}
    else
	dev = stderr;

    /* Print program name and version */
    fprintf (dev,"%s %s\n", progname, RevMsg);
    if (command != NULL && !strncasecmp (command, "ver", 3))
	exit (0);

    if (command != NULL) {
	if (command[0] == '*')
	    fprintf (dev, "%s\n", command);
	else
	    fprintf (dev, "* Missing argument for command: %c\n", command[0]);
	exit (1);
	}

    catname = ProgCat (progname);
    catcode = CatCode (catname);
    srcname = CatSource (catcode, catname);
    fprintf (stderr,"List %s in a region on the sky\n", srcname);

    if (catname == NULL)
	fprintf (dev,"Usage: %s -c catalog [arguments] ra dec system (J2000, B1950, etc.)\n", progname);
    else
	fprintf (dev,"Usage: %s [arguments] ra dec system (J2000, B1950, etc.)\n", progname);
    fprintf (dev,"  or : %s [arguments] list of catalog number ranges\n",
	progname);
    fprintf (dev,"  or : %s [arguments] @file of either positions or numbers)\n",
	progname);
    fprintf(dev,"  -a: List single closest catalog source\n");
    fprintf(dev,"  -b: Output B1950 (FK4) coordinates\n");
    if (!strcmp (progname, "scat"))
    fprintf(dev,"  -c name: Reference catalog (act, gsc, ua2, usa2, or local file\n");
    fprintf(dev,"  -d: Output RA and Dec in degrees instead of hms dms\n");
    fprintf(dev,"  -e: Output ecliptic coordinates\n");
    fprintf(dev,"  -f: Output search center for other programs\n");
    fprintf(dev,"  -g: Output galactic coordinates\n");
    fprintf(dev,"  -h: Print heading, else do not \n");
    fprintf(dev,"  -i [length]: Print catalog object name, not catalog number (length optional)\n");
    fprintf(dev,"  -j: Output J2000 (FK5) coordinates\n");
    fprintf(dev,"  -k kwd: Add this keyword to output from tab table search\n");
    fprintf(dev,"  -l: Print center and closest star on one line\n");
    fprintf(dev,"  -mx mag1[,mag2]: Magnitude #x limit(s) (only one set allowed, default none) \n");
    fprintf(dev,"  -n num: Number of brightest stars to print (-1=all as found)\n");
    fprintf(dev,"  -o name: Object name \n");
    fprintf(dev,"  -p: Sort by distance from search center\n");
    fprintf(dev,"  -q year: Equinox of output positions in FITS date format or years\n");
    fprintf(dev,"  -r rad: Search radius (<0=-half-width) in arcsec\n");
    fprintf(dev,"  -r radi-rado: Inner and outer edges of search annulus in arcsec\n");
    fprintf(dev,"  -r dx,dy: Search halfwidths in ra,dec in great circle arcseconds\n");
    fprintf(dev,"  -rr dra,ddec: Search halfwidths in ra,dec in arcsec of RA and Dec\n");
    fprintf(dev,"  -s d|e|mx|n|p|r: Sort by r=RA d=Dec mx=Mag#x n=none p=distance e=merge\n");
    fprintf(dev,"  -t: Tab table to standard output as well as file\n");
    fprintf(dev,"  -u x y: Print x y instead of number in front of non-tab entry\n");
    fprintf(dev,"  -v: Verbose\n");
    fprintf(dev,"  -w: Write output file search[objname].[catalog]\n");
    if (!strcmp (progname, "scat") || !strcmp (progname, "sgsc"))
	fprintf(dev,"  -x type: GSC object type (0=stars 3=galaxies -1=all -2=bands)\n");
    fprintf(dev,"  -y year: Epoch of output positions in FITS date format or years\n");
    fprintf(dev,"     year,year: First and last acceptable catalog entry epochs\n");
    fprintf(dev,"  -z: Append to output file search[objname].[catalog]\n");
    fprintf(dev,"   x: Number of magnitude must be same for sort and limits\n");
    fprintf(dev,"      and x may be omitted from either or both -m and -s m\n");
    if (command != NULL)
	exit (1);
    else
	exit (0);
}

#define TABMAX 64

static int
ListCat (ranges, eqout)

char	*ranges;	/* String with range of catalog numbers to list */
double	eqout;		/* Equinox for output coordinates */

{
    double cra = 0.0;	/* Search center long or RA in degrees */
    double cdec = 0.0;	/* Search center lat or Dec in degrees */
    double crao, cdeco;	/* Output center long/lat or RA/Dec in degrees */
    double epout = 0.0;
    int ng;		/* Number of catalog stars */
    int ns;		/* Number of brightest catalog stars actually used */
    struct Range *range = NULL; /* Range of catalog numbers to list */
    int nfind;		/* Number of stars to find */
    int i, is, j, ngmax, nc;
    double das, dds, drs;
    double gnmax;
    int degout;
    double maxnum;
    FILE *fd = NULL;
    char rastr[32], decstr[32];	/* coordinate strings */
    char numstr[80];	/* Catalog number */
    char cstr[32];	/* Coordinate system */
    char *catalog;
    double drad = 0.0;
    double dradi = 0.0;
    double dra, ddec, mag1, mag2;
    double gdist, da, dd, dec, gdmax;
    double epoch = 0;
    double date, time;
    double era, edec, epmr, epmd;
    int nim, nct;
    int nlog;
    int magsort;
    int typecol;
    int band;
    int imag, nmagr;
    int sysout = 0;
    char sortletter = (char) 0;
    char headline[160];
    char filename[80];
    char string[TABMAX];
    char temp[80];
    char *dtemp;
    char isp[4];
    int ngsc = 0;
    int smag;
    int nns;
    int nid;
    int icat, nndec, nnfld, nsfld;
    int gcset;
    int ndist;
    int distsort;
    int lrv;
    int lobj;
    int nf;
    char tstr[32];
    double flux;
    double pra = 0.0;
    double pdec = 0.0;
    char magname[16];
    char *date1, *date2;
    void ep2dt();
    void PrintNum();
    int LenNum();

    /* Drop out if no catalog is specified */
    if (ncat < 1) {
	fprintf (stderr, "No catalog specified\n");
	exit (-1);
	}

    /* Allocate space for returned catalog information */
    if (ranges != NULL) {
	int nfdef = 9;

	/* Allocate and fill list of numbers to read */
	range = RangeInit (ranges, nfdef);
	ngmax = rgetn (range) * 4;
	}
    else if (nstars != 0)
	ngmax = nstars;
    else
	ngmax = MAXCAT;

    if (ngmax > nalloc) {

	/* Free currently allocated buffers if more entries are needed */
	if (nalloc > 0) {
	    if (gm) {
		for (imag = 0; imag < nmagmax; imag++)
		    if (gm[imag]) free ((char *) gm[imag]);
		free ((char *)gm);
		}
	    if (gra) free ((char *)gra);
	    if (gdec) free ((char *)gdec);
	    if (gpra) free ((char *)gpra);
	    if (gpdec) free ((char *)gpdec);
	    if (gnum) free ((char *)gnum);
	    if (gc) free ((char *)gc);
	    if (gx) free ((char *)gx);
	    if (gy) free ((char *)gy);
	    if (gobj) {
		for (is = 1; is < nalloc; is++) {
		    if (gobj[is] != NULL)
			free ((char *)gobj[is]);
		    }
		free ((char *)gobj);
		}
	    if (gpath) {
		for (is = 1; is < nalloc; is++) {
		    if (gpath[is] != NULL)
			free ((char *)gpath[is]);
		    }
		free ((char *)gpath);
		}
	    }
	gm = NULL;
	gra = NULL;
	gdec = NULL;
	gpra = NULL;
	gpdec = NULL;
	gnum = NULL;
	gc = NULL;
	gx = NULL;
	gy = NULL;
	gobj = NULL;
	gpath = NULL;

	if (!(gnum = (double *) calloc (ngmax, sizeof(double))))
	    fprintf (stderr, "Could not calloc %d bytes for gnum\n",
		    ngmax*sizeof(double));
	if (!(gra = (double *) calloc (ngmax, sizeof(double))))
	    fprintf (stderr, "Could not calloc %d bytes for gra\n",
		     ngmax*sizeof(double));
	if (!(gdec = (double *) calloc (ngmax, sizeof(double))))
	    fprintf (stderr, "Could not calloc %d bytes for gdec\n",
		     ngmax*sizeof(double));
	if (!(gm = (double **) calloc (nmagmax, sizeof(double *))))
	    fprintf (stderr, "Could not calloc %d bytes for gm\n",
		     nmagmax*sizeof(double *));
	else {
	    for (imag = 0; imag < nmagmax; imag++) {
		if (!(gm[imag] = (double *) calloc (ngmax, sizeof(double))))
		    fprintf (stderr, "Could not calloc %d bytes for gm\n",
			     ngmax*sizeof(double));
		}
	    }
	if (!(gc = (int *) calloc (ngmax, sizeof(int))))
	    fprintf (stderr, "Could not calloc %d bytes for gc\n",
		     ngmax*sizeof(int));
	if (!(gx = (double *) calloc (ngmax, sizeof(double))))
	    fprintf (stderr, "Could not calloc %d bytes for gx\n",
		     ngmax*sizeof(double));
	if (!(gy = (double *) calloc (ngmax, sizeof(double))))
	    fprintf (stderr, "Could not calloc %d bytes for gy\n",
		     ngmax*sizeof(double));
	if (!(gobj = (char **) calloc (ngmax, sizeof(char *))))
	    fprintf (stderr, "Could not calloc %d bytes for obj\n",
		     ngmax*sizeof(char *));
	else {
	    for (i = 0; i < ngmax; i++)
		gobj[i] = NULL;
	    }
	if (!(gpath = (char **) calloc (ngmax, sizeof(char *))))
	    fprintf (stderr, "Could not calloc %d bytes for path\n",
		     ngmax*sizeof(char *));
	else {
	    for (i = 0; i < ngmax; i++)
		gpath[i] = NULL;
	    }
	if (!(gpra = (double *) calloc (ngmax, sizeof(double))))
	    fprintf (stderr, "Could not calloc %d bytes for gpra\n",
		     ngmax*sizeof(double));
	if (!(gpdec = (double *) calloc (ngmax, sizeof(double))))
	    fprintf (stderr, "Could not calloc %d bytes for gpdec\n",
		     ngmax*sizeof(double));
	if (gnum==NULL || gra==NULL || gdec==NULL || gm==NULL || gc==NULL ||
	   gx==NULL || gy==NULL || gobj==NULL || gpra == NULL || gpdec == NULL){
	    if (gm) {
		for (imag = 0; imag < nmagmax; imag++)
		    if (gm[imag]) free ((char *) gm[imag]);
		free ((char *)gm);
		gm = NULL;
		}
	    if (gra) free ((char *)gra);
	    gra = NULL;
	    if (gdec) free ((char *)gdec);
	    gdec = NULL;
	    if (gpra) free ((char *)gpra);
	    gpra = NULL;
	    if (gpdec) free ((char *)gpdec);
	    gpdec = NULL;
	    if (gnum) free ((char *)gnum);
	    gnum = NULL;
	    if (gc) free ((char *)gc);
	    gc = NULL;
	    if (gx) free ((char *)gx);
	    gx = NULL;
	    if (gy) free ((char *)gy);
	    gy = NULL;
	    if (gobj) {
		for (is = 1; is < nalloc; is++) {
		    if (gobj[is] != NULL)
			free ((char *)gobj[is]);
		    }
		free ((char *)gobj);
		}
	    gobj = NULL;
	    if (gpath) {
		for (is = 1; is < nalloc; is++) {
		    if (gpath[is] != NULL)
			free ((char *)gpath[is]);
		    }
		free ((char *)gpath);
		}
	    gpath = NULL;
	    nalloc = 0;
	    return (0);
	    }

	/* Initialize catalog entry values */
	for (i = 0; i < ngmax; i++) {
	    for (imag = 0; imag < nmagmax; imag++)
		gm[imag][i] = 99.0;
	    gra[i] = 0.0;
	    gdec[i] = 0.0;
	    gpra[i] = 0.0;
	    gpdec[i] = 0.0;
	    gnum[i] = 0.0;
	    gc[i] = 0;
	    gx[i] = 0.0;
	    gy[i] = 0.0;
	    }
	nalloc = ngmax;
	}

    /* Start of per catalog loop */
    for (icat = 0; icat < ncat; icat++) {
	if (ncat > 1)
	   nohead = 1;
	nndec = 0;
	isp[2] = (char) 0;
	isp[3] = (char) 0;

	/* Skip this catalog if no name is given */
	if (refcatname[icat] == NULL || strlen (refcatname[icat]) == 0) {
	    fprintf (stderr, "Catalog %d not specified\n", icat);
	    continue;
	    }

        if (printprog && notprinted) {
	    if (closest)
	        printf ("\n%s %s  Find closest star\n", progname, RevMsg);
	    else
	        printf ("\n%s %s\n", progname, RevMsg);
	    }

	/* Figure out which catalog we are searching */
	if (ncat > 1 || refcat == 0) {
	    if (!(refcat = RefCat (refcatname[icat],title,&sysref,&eqref,
				   &epref,&mprop,&nmag))) {
		fprintf (stderr,"ListCat: Catalog '%s' is missing\n", refcatname[icat]);
		return (0);
		}
	    }

	if (webdump)
	    nlog = -1;
	else if (debug)
	    nlog = 1;
	else if (verbose) {
	    if (refcat==UAC || refcat==UA1 || refcat==UA2 || refcat==UB1 ||
		refcat==USAC || refcat==USA1 || refcat==USA2 || refcat==GSC ||
		refcat==GSCACT || refcat==TMPSC || refcat==TMPSCE ||
		refcat==TMIDR2 || refcat==TMXSC || refcat == YB6)
		nlog = 1000;
	    else
		nlog = 100;
	    }
	else
	    nlog = 0;

	/* If more magnitudes are needed, allocate space for them */
	if (nmag > nmagmax) {
	    if (gm) {
		for (imag = 0; imag < nmagmax; imag++)
		    free ((char *) gm[imag]);
		free ((char *)gm);
		gm = NULL;
		}
	    nmagmax = nmag;
	    if (!(gm = (double **) calloc (nmagmax, sizeof(double *))))
		fprintf (stderr, "Could not calloc %d bytes for gm\n",
			 nmagmax*sizeof(double *));
	    else {
		for (imag = 0; imag < nmagmax; imag++) {
		    if (!(gm[imag] = (double *) calloc (ngmax, sizeof(double))))
			fprintf (stderr, "Could not calloc %d bytes for gm\n",
				 ngmax*sizeof(double));
		    }
		}
	    }

	/* Set output coordinate system from command line or catalog */
	if (sysout0)
	    sysout = sysout0;
	else if (srch!= NULL && srch->epoch != 0.0)
	    sysout = srch->coorsys;
	if (!sysout)
	    sysout = sysref;
	if (!sysout)
	    sysout = WCS_J2000;

	/* Set equinox from command line, search catalog, or searched catalog */
	if (eqout == 0.0) {
	    if (srch!= NULL && srch->equinox != 0.0)
		eqout = srch->equinox;
	    else if (sysout0 == WCS_J2000)
		eqout = 2000.0;
	    else if (sysout0 == WCS_B1950)
		eqout = 1950.0;
	    else if (eqcoor != 0.0)
		eqout = eqcoor;
	    else
		eqout = epref;
	    if (eqout == 0.0)
		eqout = 2000.0;
	    }

	/* Set epoch from command line, search catalog, or searched catalog */
	epout = epoch0;
	if (epout == 0.0) {
	    if (srch!= NULL && srch->epoch != 0.0)
		epout = srch->epoch;
	    else if (sysout0 == WCS_J2000)
		epout = 2000.0;
	    else if (sysout0 == WCS_B1950)
		epout = 1950.0;
	    else if (mprop != 1)
		epout = epref;
	    else if (eqout != 0.0)
		epout = eqout;
	    else {
		if (sysout == WCS_B1950)
		    epout = 1950.0;
		else
		    epout = 2000.0;
		}
	    }

	/* Set degree flag for output */
	if (sysout == WCS_ECLIPTIC || sysout == WCS_GALACTIC)
	    degout = 1;
	else
	    degout = degout0;
	setlimdeg (degout);

	/* Find stars specified by number */
	if (ranges != NULL) {

	    /* Default to keep stars by order read */
	    if (catsort == SORT_UNSET)
		catsort = SORT_NONE;

	    nfind = rgetn (range);
	    for (i = 0; i < nfind; i++)
		gnum[i] = rgetr8 (range);
	    wfile = 0;

	    /* Find the specified catalog stars */
	    if (nstars < 0)
		nlog = -1;
	    ng = ctgrnum (refcatname[icat], refcat,
		      nfind, sysout, eqout, epout, match, &starcat[icat],
		      gnum, gra, gdec, gpra, gpdec, gm, gc, gobj, gpath, nlog);
	    if (nlog < 0)
		return (ng);

	    if (gobj == NULL)
		gobj1 = NULL;
	    else if (gobj[0] == NULL)
		gobj1 = NULL;
	    else
		gobj1 = gobj;
	    if (gpath == NULL)
		gpath1 = NULL;
	    else if (gpath[0] == NULL)
		gpath1 = NULL;
	    else
		gpath1 = gpath;
	    ns = ng;

	    /* Set flag if any proper motions are non-zero */
	    if (mprop == 1 && !oneline) {
		mprop = 0;
		for (i = 0; i < ng; i++) {
		    if (gpra[i] != 0.0 || gpdec[i] != 0.0) {
			mprop = 1;
			break;
			}
		    }
		}
	    if (mprop == 0 && starcat[icat] != NULL && starcat[icat]->entrv > 0)
		mprop = 2;

	    for (i = 0; i < ns; i++ ) {
		gx[i] = 0.0;
		gy[i] = 1.0;
		}

	    /* Find largest catalog number printed */
	    if (starcat[icat] != NULL && starcat[icat]->nnfld != 0)
		nnfld = starcat[icat]->nnfld;
	    else if (refcat == SKYBOT) {
		nnfld = 6;
		for (i = 0; i < ng; i++) {
		    lobj = strlen (gobj[i]);
		    if (lobj > nnfld)
			nnfld = lobj;
		    }
		}
	    else {
		maxnum = 0.0;
		for (i = 0; i < ns; i++ ) {
		    if (gnum[i] > maxnum)
			maxnum = gnum[i];
		    }
		nnfld = CatNumLen (refcat, maxnum, nndec);
		}

	    /* Check to see if epoch is contained in entries */
	    if (starcat[icat] != NULL && starcat[icat]->nepoch != 0)
		printepoch = 1;
	    else
		printepoch = 0;

	    /* Check to see whether gc is set at all */
	    gcset = 0;
	    for (i = 0; i < ns; i++ ) {
		if (gc[i] != 0) {
		    gcset = 1;
		    break;
		    }
		}

	    /* Set flag for plate, class, or type column */
	    if (refcat == BINCAT || refcat == SAO  || refcat == PPM ||
		refcat == BSC || refcat == SDSS || refcat == SKY2K ||
		refcat == IRAS || refcat == HIP)
		typecol = 1;
	    else if ((refcat == GSC || refcat == GSCACT) && classd < -1)
		typecol = 3;
	    else if (refcat == GSC || refcat == GSCACT ||
		refcat == UJC || refcat == UB1 ||
		refcat == USAC || refcat == USA1   || refcat == USA2 ||
		refcat == UAC  || refcat == UA1    || refcat == UA2 ||
		refcat == BSC  || (refcat == TABCAT&&gcset))
		typecol = 2;
	    else if (starcat[icat] != NULL && starcat[icat]->sptype > 0)
		typecol = 1;
	    else
		typecol = 0;
	    if (mprop == 2)
		nmagr = nmag - 1;
	    else
		nmagr = nmag;
	    if (printepoch)
		nmagr = nmagr - 1;

	    /* Write out entries for use as image centers */
	    if (searchcenter) {
		gnmax = gnum[0];
		for (i = 1; i < ns; i++ ) {
		    if (gnum[i] > gnmax) gnmax = gnum[i];
		    }
		for (i = 0; i < ns; i++ ) {
		    if (sysref == WCS_XY) {
			num2str (rastr, gra[i], 10, 5);
			num2str (decstr, gdec[i], 10, 5);
			}
		    else if (degout) {
			deg2str (rastr, 32, gra[i], nddeg);
			deg2str (decstr, 32, gdec[i], nddeg);
			}
		    else {
			ra2str (rastr, 32, gra[i], ndra);
			dec2str (decstr, 32, gdec[i], nddec);
			}
		    wcscstr (cstr, sysout, eqout, epout);
		    if (printobj && gobj1 != NULL)
			printf ("%s %s %s %s\n",
				gobj[i], rastr, decstr, cstr);
		    else {
			CatNum (refcat, -nnfld, nndec, gnum[i], numstr);
			printf ("%s_%s %s %s %s\n",
				refcatname[icat],numstr,rastr,decstr,cstr);
			}
		    }
		return (ns);
		}

	    /* Sort catalogued objects, if requested */
	    if (ns > 1) {

		switch (catsort) {

		    /* Merge found catalog objects within rad of each other */
		    case SORT_MERGE:
			if (verbose)
			    fprintf (stderr, "SCAT: Merging %d stars\n", ns);
			if (rad0 == 0.0)
			    rad0 = 1.5;
			nns = MergeStars (gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,
				     ns,nmagmax,rad0,nlog);
			ns = nns;
			break;

		    /* Sort found catalog objects from closest to furthest */
		    case SORT_DIST:
			XSortStars (gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,
				    ns,nmagmax);
			break;

		    /* Sort found catalog objects by right ascension */
		    case SORT_RA:
			RASortStars (gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,
				     ns,nmagmax);
			break;

		    /* Sort found catalog objects by declination */
		    case SORT_DEC:
			DecSortStars(gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,
				     ns,nmagmax);
			break;

		    /* Sort found catalog objects from brightest to faintest */
		    case SORT_MAG:
			MagSortStars(gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,
				     ns,nmagmax,sortmag);
			break;

		    /* Sort found catalog objects by id number */
		    case SORT_ID:
			IDSortStars(gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,
				     ns,nmagmax);
			break;
		    }
		}
	    }

	/* Find catalog entries specified by date */
	else if (dec0 < -90.0 && epoch1 > 0.0 && epoch2 > 0.0) {

	    /* Default to keep stars by order read */
	    if (catsort == SORT_UNSET)
		catsort = SORT_NONE;

	    wfile = 0;

	    /* Find the specified catalog stars */
	    ng = ctgrdate (refcatname[icat], refcat,
		      sysout,eqout,epout,&starcat[icat],epoch1,epoch2,
		      ngmax,gnum,gra,gdec,gpra,gpdec,gm,gc,gobj,nlog);

	    if (ng < 1) {
		date1 = ep2fd (epoch1);
		date2 = ep2fd (epoch2);
		fprintf (stderr, "SCAT: No entries between %s and %s\n",
			 date1, date2);
		continue;
		}

	    if (gobj == NULL)
		gobj1 = NULL;
	    else if (gobj[0] == NULL)
		gobj1 = NULL;
	    else
		gobj1 = gobj;
	    /* if (ng > nfind)
		ns = nfind;
	    else */
		ns = ng;

	    /* Set flag if any proper motions are non-zero */
	    if (mprop == 1 && !oneline) {
		mprop = 0;
		for (i = 0; i < ng; i++) {
		    if (gpra[i] != 0.0 || gpdec[i] != 0.0) {
			mprop = 1;
			break;
			}
		    }
		}
	    if (mprop == 0 && starcat[icat] != NULL && starcat[icat]->entrv > 0)
		mprop = 2;

	    for (i = 0; i < ns; i++ ) {
		gx[i] = 0.0;
		gy[i] = 1.0;
		}

	    /* Find largest catalog number printed */
	    if (starcat[icat] != NULL && starcat[icat]->nnfld != 0)
		nnfld = starcat[icat]->nnfld;
	    else if (refcat == SKYBOT) {
		nnfld = 6;
		for (i = 0; i < ng; i++) {
		    lobj = strlen (gobj[i]);
		    if (lobj > nnfld)
			nnfld = lobj;
		    }
		}
	    else {
		maxnum = 0.0;
		for (i = 0; i < ns; i++ ) {
		    if (gnum[i] > maxnum)
			maxnum = gnum[i];
		    }
		nnfld = CatNumLen (refcat, maxnum, nndec);
		}

	    printepoch = 1;

	    /* Check to see whether gc is set at all */
	    gcset = 0;
	    for (i = 0; i < ns; i++ ) {
		if (gc[i] != 0) {
		    gcset = 1;
		    break;
		    }
		}

	    /* Set flag for plate, class, or type column */
	    if (refcat == BINCAT || refcat == SAO  || refcat == PPM ||
		refcat == BSC || refcat == SDSS || refcat == SKY2K)
		typecol = 1;
	    else if ((refcat == GSC || refcat == GSCACT) && classd < -1)
		typecol = 3;
	    else if (refcat == GSC || refcat == GSCACT ||
		refcat == UJC || refcat == UB1 ||
		refcat == USAC || refcat == USA1   || refcat == USA2 ||
		refcat == UAC  || refcat == UA1    || refcat == UA2 ||
		refcat == BSC  || (refcat == TABCAT&&gcset))
		typecol = 2;
	    else if (starcat[icat] != NULL && starcat[icat]->sptype > 0)
		typecol = 1;
	    else
		typecol = 0;
	    if (mprop == 2)
		nmagr = nmag - 1;
	    else
		nmagr = nmag;
	    if (printepoch)
		nmagr = nmagr - 1;


	    /* Sort catalogued objects, if requested */
	    if (ns > 1) {

		switch (catsort) {

		    /* Sort found catalog objects in image by right ascension */
		    case SORT_RA:
			RASortStars (gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,
				     ns,nmagmax);
			break;

		    /* Sort found catalog objects by declination */
		    case SORT_DEC:
			DecSortStars(gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,
				     ns,nmagmax);
			break;

		    /* Sort found catalog objects from brightest to faintest */
		    case SORT_MAG:
			MagSortStars(gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,
				     ns,nmagmax,sortmag);
			break;

		    /* Sort found catalog objects by ID number */
		    case SORT_ID:
			IDSortStars(gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,
				    ns,nmagmax);
			break;
		    }
		}
	    }

	/* Find stars specified by location */
	else {

	    /* Default to sort stars by magnitude */
	    if (catsort == SORT_UNSET)
		catsort = SORT_MAG;

	    /* Set search radius if finding closest star */
	    if (rad0 == 0.0 && dra0 == 0.0) {
		if (closest)
		    rad0 = CatRad (refcat);
		else
		    rad0 = 10.0;
		}

	    /* Set limits from defaults and command line information */
	    if (GetArea (verbose,syscoor,eqcoor,sysout,eqout,epout,
		     &cra,&cdec,&dra,&ddec,&drad,&dradi,&crao,&cdeco))
		return (0);

	    if (srch != NULL) {
		if (srchcat->stnum <= 0 && strlen (srch->objname) > 0) {
		    if (objname == NULL)
			objname = (char *) malloc (32);
		    strcpy (objname, srch->objname);
		    }
		else {
		    if (objname == NULL)
			objname = (char *) calloc (1,32);
		    nsfld = CatNumLen (TXTCAT, srch->num, srchcat->nndec);
		    CatNum (TXTCAT, nsfld, srchcat->nndec, srch->num, objname);
		    }
		}
	    nnfld = CatNumLen (refcat, 0.0, nndec);

	    /* Print search center and size in input and output coordinates */
	    if (verbose || (printhead && !oneline)) {
		if (sysout != syscoor || eqcoor != eqout)
		    SearchHead (icat,syscoor,eqcoor,epout,
				cra,cdec,dra,ddec,drad,dradi,nnfld,degout);
		SearchHead (icat,sysout,eqout,epout,
			crao,cdeco,dra,ddec,drad,dradi,nnfld,degout);
		if (!closest) {
		    if (sysref != syscoor && sysref != sysout) {
			double cra2 = cra;
			double cdec2 = cdec;
			wcscon (syscoor, sysref, 0.0, 0.0, &cra2, &cdec2, epout);
			SearchHead (icat,sysref,eqref, epref,cra2,cdec2,
				    dra,ddec,drad,dradi,nnfld,degout);
			}
		    }
		}

	    /* Set the magnitude limits for the catalog search */
	    if (maglim2 == 0.0) {
		mag1 = 0.0;
		mag2 = 0.0;
		}
	    else {
		mag1 = maglim1;
		mag2 = maglim2;
		}

	    /* Find the nearby reference stars, in ra/dec */
	    if (catsort == SORT_DIST)
		distsort = 1;
	    else
		distsort = 0;
	    if (sortmag > 9) {
		sortletter = (char) sortmag;
		sortmag = CatMagNum (sortmag, refcat);
		}
	    else if (sortmag > 0)
		sortletter = (char) (48 + sortmag);
	    else
		sortletter = ' ';
	    ng = ctgread (refcatname[icat], refcat, distsort, crao, cdeco,
		      dra,ddec,drad,dradi,sysout,eqout,epout,mag1,mag2,
		      sortmag,ngmax,&starcat[icat],
		      gnum,gra,gdec,gpra,gpdec,gm,gc,gobj,nlog);
	    if (ngmax < 1)
		return (ng);

	    if ((verbose || printhead) && ncat == 1 && ng < 1) {
		fprintf (stderr, "No stars found in %s\n",refcatname[icat]);
		return (0);
		}

	    if (gobj[0] == NULL)
		gobj1 = NULL;
	    else
		gobj1 = gobj;
	    if (ng > ngmax)
		ns = ngmax;
	    else
		ns = ng;

	    /* Set flag if any proper motions are non-zero */
	    if (mprop == 1 && !oneline) {
		mprop = 0;
		for (i = 0; i < ns; i++) {
		    if (gpra[i] != 0.0 || gpdec[i] != 0.0) {
			mprop = 1;
			break;
			}
		    }
		}

	    /* Convert coordinates to output coordinate system */
	    if (syscoor != sysout) {
		if (mprop == 1) {
		    for (i = 0; i < ns; i++) {
    			wcsconp (syscoor,sysout,eqout,eqout,epout,epout,
				 &gra[i],&gdec[i],&gpra[i],&gpdec[i]);
			}
		    }
		else {
		    for (i = 0; i < ns; i++) {
    			wcscon (syscoor,sysout,eqout,eqout,
				&gra[i],&gdec[i],epout);
			}
		    }
		}

	    /* Set flag if radial velocity is included in catalog entry */
	    if (mprop == 0 && starcat[icat] != NULL && starcat[icat]->entrv > 0)
		mprop = 2;

	    /* Check to see if epoch is contained in entries */
	    if (starcat[icat] != NULL && starcat[icat]->nepoch != 0)
		printepoch = 1;
	    else
		printepoch = 0;

	    /* Find largest catalog number to be printed */
	    if (starcat[icat] != NULL && starcat[icat]->nnfld != 0)
		nnfld = starcat[icat]->nnfld;
	    else if (refcat == SKYBOT) {
		nnfld = 6;
		for (i = 0; i < ng; i++) {
		    lobj = strlen (gobj[i]);
		    if (lobj > nnfld)
			nnfld = lobj;
		    }
		}
	    else {
		maxnum = 0.0;
		for (i = 0; i < ns; i++ ) {
		    if (gnum[i] > maxnum)
			maxnum = gnum[i];
		    }
		nnfld = CatNumLen (refcat, maxnum, nndec);
		}

	    /* Check to see whether gc is set at all */
	    gcset = 0;
	    for (i = 0; i < ns; i++ ) {
		if (gc[i] != 0) {
		    gcset = 1;
		    break;
		    }
		}

	    /* Set flag for plate, class, or type column */
	    if (refcat == BINCAT || refcat == SAO  || refcat == PPM ||
		refcat == BSC || refcat == SDSS || refcat == SKY2K)
		typecol = 1;
	    else if ((refcat == GSC || refcat == GSCACT) && classd < -1)
		typecol = 3;
	    else if (refcat == GSC || refcat == GSCACT ||
		refcat == UJC ||  refcat == UB1 ||
		refcat == USAC || refcat == USA1   || refcat == USA2 ||
		refcat == UAC  || refcat == UA1    || refcat == UA2 ||
		refcat == BSC  || (refcat == TABCAT&&gcset))
		typecol = 2;
	    else if (starcat[icat] != NULL && starcat[icat]->sptype > 0)
		typecol = 1;
	    else
		typecol = 0;

	    /* Set number of magnitudes (n-1 if radial velocity present) */
	    if (mprop == 2)
		nmagr = nmag - 1;
	    else
		nmagr = nmag;
	    if (printepoch)
		nmagr = nmagr - 1;

	    /* Compute distance from star to search center */
	    for (i = 0; i < ns; i++ ) {
		gx[i] = wcsdist (crao, cdeco, gra[i], gdec[i]);
		gy[i] = 1.0;
		}

	    /* Sort catalogued objects, if requested */
	    if (ns > 1) {

		/* Sort found catalog objects from closest to furthest */
		if (catsort == SORT_DIST)
		    XSortStars (gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,ns,
				nmagmax);

		/* Sort found catalog objects by right ascension */
		else if (catsort == SORT_RA)
		    RASortStars (gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,ns,
				 nmagmax);

		/* Sort found catalog objects by declination */
		else if (catsort == SORT_DEC)
		    DecSortStars(gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,ns,
				 nmagmax);

		/* Sort found catalog objects from brightest to faintest */
		else if (catsort == SORT_MAG)
		    MagSortStars(gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,ns,
				 nmagmax,sortmag);

		/* Sort found catalog objects by ID number */
		else if (catsort == SORT_ID)
		    IDSortStars(gnum,gra,gdec,gpra,gpdec,gx,gy,gm,gc,gobj1,
				ns,nmagmax);
		}

	    /* Print one line with search center and found star */
	    if (oneline) {
		if (ns > 0) {
		    das = dra0 * 3600.0;
		    dds = ddec0 * 3600.0;
		    drs = rad0;
		    if (drs < 0.0)
			drs = -drs;
		    else if (drs == 0.0)
			drs = sqrt (das*das + dds*dds);
		    if (das <= 0.0) {
			das = drs;
			dds = drs;
			}
		    if (srchcat != NULL) {
			smag = srchcat->nmag;
			if (srchcat->entrv > 0)
			    smag = smag - 1;
			if (srchcat->nepoch)
			    smag = smag - 1;
			}
		    else
			smag = 1;

		    if (nohead && tabout) {

			/* Write tab table heading */
			catalog = CatName (refcat, refcatname[icat]);
			printf ("catalog	%s\n", catalog);
			if (listfile != NULL)
			    printf ("search	%s\n", listfile);
			if (sysout == WCS_GALACTIC)
			    printf ("radecsys	galactic\n");
			else if (sysout == WCS_ECLIPTIC)
			    printf ("radecsys	ecliptic\n");
			else if (sysout == WCS_B1950)
			    printf ("radecsys	fk4\n");
			else
			    printf ("radecsys	fk5\n");
			printf ("equinox	%.4f\n", eqout);
			if (!printepoch)
			    printf ("epoch	%.4f\n", epout);
			if (minid != 0)		/* Min number of plate IDs for USNO-B1.0 */
			    printf ("minid	%d\n", minid);
			if (minpmqual > 0)	/* Min proper motion quality for USNO-B1.0 */
			    printf ("minpmq	%d\n", minpmqual);
			if (dra0 > 0.0 || rad0 < 0) {
			    if (syscoor == WCS_GALACTIC) {
				printf ("glonsec	%.2f\n", das);
				printf ("glatsec	%.2f\n", dds);
				}
			    else if (syscoor == WCS_ECLIPTIC) {
				printf ("elonsec	%.2f\n", das);
				printf ("elatsec	%.2f\n", dds);
				}
			    else {
				printf ("drasec	%.2f\n", das);
				printf ("ddecsec	%.2f\n", dds);
				}
			    }
			else if (rad0 > 0)
			    printf ("radsec	%.2f\n", drs);
			if (mprop==1 || (srchcat != NULL && srchcat->mprop==1)) {
			    if (degout)
				printf ("pmunit	mas/yr\n");
			    else {
				printf ("rpmunit	mas/yr\n");
				printf ("dpmunit	mas/yr\n");
				}
			    }
		        printf ("program	%s %s\n", progname, RevMsg);

		        /* Write column headings */
		        if (srch != NULL) {
			    if (srchcat->keyid[0] > 0) {
				printf ("%s", srchcat->keyid);
				nc = strlen (srchcat->keyid);
				}
			    else {
				printf ("srch_id");
				nc = 7;
				}
			    if (srchcat->nnfld > nc) {
				for (i = nc; i < srchcat->nnfld; i++)
				    printf (" ");
				}
			    printf ("	");
			    }
		        printf ("srch_ra     	srch_dec    	");
		        if (srchcat != NULL) {
			    if (smag > 0) {
				for (imag = 0; imag < smag; imag++) {
				    if (strlen (srchcat->keymag[imag]) >0)
					printf ("s_%s	", srchcat->keymag[imag]);
				    else if (smag > 1)
					printf ("s_mag%d	", imag+1);
				    else
					printf ("s_mag	");
				    }
				}
			    if (srchcat->nepoch)
				printf ("epoch    	");
			    if (srchcat->sptype) {
				if (strlen (srch->isp) > 2) {
				    padspt = 1;
				    if (strlen (srchcat->keytype) >0)
					printf ("s_%s 	",srchcat->keytype);
				    else
					printf ("s_type  	");
				    }
				else {
				    padspt = 0;
				    if (strlen (srchcat->keytype) >0)
					printf ("s_%s	",srchcat->keytype);
				    else
					printf ("s_type	");
				    }
				}
			    if (srchcat->entrv > 0) {
				if ((lrv = strlen (srchcat->keyrv)) >0) {
				    sprintf (tstr, "%s	            ",
					     srchcat->keyrv);
				    tstr[9] = (char)0;
				    printf ("%s	", tstr);
				    }
				else
				    printf ("s_rv    	");
				}
			    if (srchcat->mprop > 0)
				    printf ("s_pra 	s_pdec	");
				
			    }
			if (refcat == TABCAT && starcat[icat]->keyid[0] >0) {
			    strcpy (headline, starcat[icat]->keyid);
			    strcat (headline, "                ");
			    }
			else
			    CatID (headline, refcat);
			headline[nnfld] = (char) 0;
			printf ("%s", headline);
			printf ("	ra          	dec        	");
			if (refcat == GSC2)
			    printf ("magf 	magj 	magn 	magv	");
			else if (refcat == HIP)
			    printf ("magb 	magv 	parlx	parer	");
			else if (refcat == SKYBOT)
			    printf ("magv 	gdist 	hdist	");
			else if (refcat == IRAS)
			    printf ("f10m 	f25m 	f60m 	f100m	");
			else if (refcat == TMPSC || refcat == TMIDR2)
			    printf ("magj   	magh    	magk   		");
			else if (refcat == TMPSCE)
			    printf ("magje 	maghe	magke	");
			else if (refcat == TMXSC)
			    printf ("magj  	magh	magk	size	");
			else if (refcat == UB1)
			    printf ("magb1	magr1	magb2	magr2	magn 	");
			else if (refcat == YB6)
			    printf ("magb 	magr 	magj 	magh 	magk 	");
			else if (refcat == SDSS)
			    printf ("magu 	magg 	magr 	magi 	magz 	");
			else if (refcat == UCAC2)
			    printf ("raerr	decerr	magj 	magh 	magk 	magc 	");
			else if (refcat == UCAC3)
			    printf ("raerr	decerr	magb 	magr 	magi 	magj 	magh 	magk 	magm 	maga	");
			else if (refcat == UCAC4)
			    printf ("raerr	decerr	magb 	magr 	magi 	magj 	magh 	magk 	magm 	maga	");
			else if (nmagr > 0) {
			    for (imag = 0; imag < nmagr; imag++) {
				if (starcat[icat] != NULL &&
				    strlen (starcat[icat]->keymag[imag]) >0)
				    printf ("%s	", starcat[icat]->keymag[imag]);
				else if (nmagr > 1)
				    printf ("mag%d  ", imag+1);
				else
				    printf ("mag    ");
				}
			    }
			if (typecol == 1)
			    printf ("spt   	");
			if (mprop == 1)
			    printf ("pmra  	pmdec 	");
			if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4)
			    printf ("epmra	epmdec	ni	nc	");
			if (refcat == UB1)
			    printf ("pm	ni	sg	");
			if (refcat == GSC2)
			    printf ("class	");
			if ((starcat[icat]!=NULL && starcat[icat]->entrv>0) &&
			    mprop == 2)
			    printf ("velocity	");
			printf ("n 	dra");
			for (i = 3; i < LenNum(das,2); i++)
			    printf (" ");
			printf ("	ddec");
			for (i = 4; i < LenNum(dds,2); i++)
			    printf (" ");
			printf ("	drad");
			for (i = 4; i < LenNum(drs,2); i++)
			    printf (" ");
			printf ("\n");
			if (srch != NULL) {
			    printf ("-------");
			    if (srchcat->nnfld > 7) {
				for (i = 8; i < srchcat->nnfld; i++)
				    printf ("-");
				}
			    printf ("	");
			    }
			printf ("------------	------------	");
			if (srchcat != NULL) {
			    for (imag = 0; imag < smag; imag++)
				printf ("-----	");
			    if (srchcat->nepoch)
				printf ("---------	");
			    if (srchcat->sptype != 0) {
				if (padspt)
				    printf ("-------	");
				else
				    printf ("----	");
				}
			    if (srchcat->entrv > 0)
				printf ("---------	");
			    if (srchcat->mprop == 1)
				printf ("------	------	");
			    }
			strcpy (headline,"----------------------");
			headline[nnfld] = (char) 0;
			printf ("%s", headline);
			printf ("	------------	------------	");
			if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4)
			    printf ("-----	-----	");
			if (refcat == GSC2)
			    printf ("-----	-----	-----	-----	");
			else if (refcat == HIP || refcat == IRAS)
			    printf ("-----	-----	-----	-----	");
			else if (refcat == TMPSC || refcat == TMIDR2)
			    printf ("-------	-------	-------	");
			else if (refcat == TMPSCE)
			    printf ("-------	-------	-------	-------	-------	-------	");
			else if (refcat == TMXSC)
			    printf ("-------	-------	-------	------	");
			else if (nmagr > 0) {
			    for (imag = 0; imag < nmagr; imag++)
				printf ("-----	");
			    }
			if (typecol == 1)
			    printf ("---	");
			if ((starcat[icat]!=NULL && starcat[icat]->entrv>0) &&
			    mprop == 2)
			    printf ("---------	");
			if (mprop == 1)
			    printf ("------	------	");
			if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4)
			    printf ("-----	-----	--	--	");
			if (refcat == UB1)
			    printf ("--	--	--	");
			printf ("--	");
			for (i = 0; i < LenNum(das,2); i++)
			    printf ("-");
			printf ("	");
			for (i = 0; i < LenNum(dds,2); i++)
			    printf ("-");
			printf ("	");
			for (i = 0; i < LenNum(drs,2); i++)
			    printf ("-");
			printf ("\n");
			nohead = 0;
			}
		    if (srch != NULL) {
			if (srchcat->keyid[0] > 0 && strlen (srch->objname))
			    strcpy (numstr, srch->objname);
			else if (srchcat->stnum <= 0 &&
			    strlen (srch->objname) > 0)
			    strcpy (numstr, srch->objname);
			else
			    CatNum (TXTCAT,-srchcat->nnfld,srchcat->nndec,
				    srch->num,numstr);
		        if (tabout)
			    printf ("%s	", numstr);
		        else
			    printf ("%s ", numstr);
			}
		    if (degout) {
			num2str (rastr, crao, 12, nddeg);
			num2str (decstr, cdeco, 12, nddeg);
			}
		    else {
			ra2str (rastr, 32, crao, ndra);
			dec2str (decstr, 32, cdeco, nddec);
			}
		    if (tabout)
			printf ("%s	%s", rastr, decstr);
		    else
			printf ("%s %s", rastr, decstr);
		    if (srchcat != NULL && srch != NULL) {
			if (smag > 0) {
			    for (imag = 0; imag < smag; imag++) {
				if (tabout) {
				    if (srch->xmag[imag] > 100.0)
					printf ("	%5.2fL", srch->xmag[imag]-100.0);
				    else
					printf ("	%5.2f", srch->xmag[imag]);
				    }
				else {
				    if (srch->xmag[imag] > 100.0)
					printf (" %5.2fL", srch->xmag[imag]-100.0);
				    else
					printf (" %5.2f", srch->xmag[imag]);
				    }
				}
			    }
			if (srchcat->nepoch) {
			    ep2dt (srch->epoch, &date, &time);
			    if (tabout)
				printf ("	%9.4f", date);
			    else
				printf (" %9.4f", date);
			    }
			if (srchcat->sptype != 0) {
			    if (padspt) {
				for (i = 0; i < 7; i++) {
				    if (srch->isp[i] == (char) 0)
					srch->isp[i] = ' ';
				    }
				srch->isp[7] = (char) 0;
				if (tabout)
				    printf ("	%7s", srch->isp);
				else
				    printf ("  %7s ", srch->isp);
				}
			    else if (tabout)
				printf ("	%s", srch->isp);
			    else
				printf ("  %s ", srch->isp);
			    }
			if (srchcat->entrv > 0) {
			    if (tabout)
				printf ("	%9.2f", srch->radvel);
			    else
				printf (" %9.2f", srch->radvel);
			    }
			if (srchcat->mprop == 1) {
			    pra = srch->rapm * 3600000.0 * cosdeg (srch->dec);
			    pdec = srch->decpm * 3600000.0;
			    if (tabout)
				printf ("	%6.1f	%6.1f", pra, pdec);
			    else
				printf (" %6.1f %6.1f", pra, pdec);
			    }
			}
		    if (mprop == 1) {
			pra = gpra[0] * 3600000.0 * cosdeg (gdec[0]);
			pdec = gpdec[0] * 3600000.0;
			}

		    /* Set up object name or number to print */
		    if (refcat == SDSS || refcat == GSC2 || refcat == SKYBOT)
			strcpy (numstr, gobj[0]);
		    else if (starcat[icat] != NULL) {
			if (starcat[icat]->stnum < 0 && gobj1 != NULL) {
			    strncpy (numstr, gobj1[0], 79);
			    if (lofld > 0) {
				for (j = 0; j < lofld; j++) {
				    if (!numstr[j])
					numstr[j] = ' ';
				    }
				}
			    }
			else
			    CatNum (refcat,-nnfld,starcat[icat]->nndec,gnum[0],numstr);
			}
		    else
			CatNum (refcat, -nnfld, nndec, gnum[0], numstr);
		    /* if (gobj1 != NULL) {
			if (strlen (gobj1[0]) > 0)
			    strcpy (numstr, gobj1[0]);
			}
		    if (starcat[icat] != NULL)
			CatNum (refcat,-nnfld,starcat[icat]->nndec,gnum[0],numstr);
		    else
			CatNum (refcat,-nnfld,nndec,gnum[0],numstr);  */
		    if (degout) {
			num2str (rastr, gra[0], 12, nddeg);
			num2str (decstr, gdec[0], 12, nddeg);
			}
		    else {
			ra2str (rastr, 32, gra[0], ndra);
			dec2str (decstr, 32, gdec[0], nddec);
			}
		    if (tabout)
			printf ("	%s	%s	%s",
			        numstr, rastr, decstr);
		    else
			printf (" %s %s %s",
			        numstr, rastr, decstr);
		    if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4) {
			era = gm[nmagr][0] * cosdeg (gdec[i]) * 3600.0;
			edec = gm[nmagr+1][0] * 3600.0;
			printf ("	%5.3f	%5.3f", era, edec);
			}
		    if (refcat == GSC2) {
			for (j = 0; j < 4; j++) {
			    if (gm[j][0] > 90.0) gm[j][0] = 99.99;
			    }
			if (tabout)
			    printf ("	%5.2f	%5.2f	%5.2f	%5.2f",
				    gm[0][0],gm[1][0],gm[2][0],gm[3][0]);
			else
			    printf (" %5.2f %5.2f %5.2f %5.2f %5.2f",
				    gm[0][0],gm[1][0],gm[2][0],gm[4][0],gm[5][0]);
			}
		    else if (refcat == HIP) {
			if (tabout)
			    printf ("	%5.2f	%5.2f	%5.2f	%5.2f",
				    gm[0][0], gm[1][0], gm[2][0], gm[3][0]);
			else
			    printf (" %5.2f %5.2f %5.2f %5.2f",
				    gm[0][0], gm[1][0], gm[2][0], gm[3][0]);
			}
		    else if (refcat == IRAS) {
			for (imag = 0; imag < 4; imag++) {
			    if (gm[imag][0] > 100.0) {
				flux = 1000.0 * pow (10.0,-(gm[imag][0]-100.0)/2.5);
				if (tabout)
				    printf ("	%.2fL", flux);
				else
				    printf (" %5.2fL", flux);
				}
			    else {
				flux = 1000.0 * pow (10.0,-gm[imag][0]/2.5);
				if (tabout)
				    printf ("	%.2f ", flux);
				else
				    printf (" %5.2f ", flux);
				}
			    }
			}
		    else if (refcat == TMPSC || refcat == TMIDR2 ||
			     refcat == TMPSCE || refcat == TMXSC) {
			for (imag = 0; imag < 3; imag++) {
			    if (gm[imag][0] > 100.0) {
				if (tabout)
				    printf ("	%6.3fL", gm[imag][0]-100.0);
				else
				    printf (" %6.3fL", gm[imag][0]-100.0);
				}
			    else {
				if (tabout)
				    printf ("	%6.3f ", gm[imag][0]);
				else
				    printf (" %6.3f ", gm[imag][0]);
				}
			    }
			if (refcat == TMPSCE) {
			    for (imag = 3; imag < 6; imag++) {
				if (tabout)
				    printf ("	%6.3f ", gm[imag][0]);
				else
				    printf (" %6.3f ", gm[imag][0]);
				}
			    }
			if (refcat == TMXSC) {
			    if (tabout)
				printf("	%6.1f",((double)gc[0])* 0.1);
			    else
				    printf (" %6.1f", ((double)gc[0])*0.1);
			    }
			}
		    else if (nmagr > 0) {
			for (imag = 0; imag < nmagr; imag++) {
			    if (tabout)
				printf ("	%5.2f", gm[imag][0]);
			    else
				printf (" %5.2f", gm[imag][0]);
			    }
			}
		    if (typecol == 1) {
			isp[0] = gc[0] / 1000;
			isp[1] = gc[0] % 1000;
			if (isp[0] == ' ' && isp[1] == ' ') {
			    isp[0] = '_';
			    isp[1] = '_';
			    }
			if (tabout)
			    printf ("	%2s ", isp);
			else
			    printf (" %2s ", isp);
			}
		    if (starcat[icat] != NULL && starcat[icat]->entrv>0) {
			if (tabout)
			    printf ("	%9.2f", gm[nmagr][0]);
			else
			    printf (" %9.2f", gm[nmagr][0]);
			}
		    if (starcat[icat] != NULL && starcat[icat]->nepoch>0) {
			if (starcat[icat]->entrv>0)
			    dtemp = DateString (gm[nmagr+1][0], tabout);
			else
			    dtemp = DateString (gm[nmagr][0], tabout);
			printf ("%s", dtemp);
			free (dtemp);
			}
		    if (mprop == 1) {
			if (tabout)
			    printf ("	%6.1f	%6.1f", pra, pdec);
			else
			    printf (" %6.1f %6.1f", pra, pdec);
			}
		    if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4) {
			epmr = gm[nmagr+2][0] * cosdeg (gdec[i]) * 3600000.0;
			epmd = gm[nmagr+3][0] * 3600000.0;
			nim = gc[0] / 1000;
			nct = gc[0] % 1000;
			if (tabout)
			    printf ("	%5.1f	%5.1f	%2d	%2d",
				    epmr, epmd, nim, nct);
			else
			    printf (" %5.1f %5.1f %3d %3d",
				    epmr, epmd, nim, nct);
			}
		    if (refcat == UB1) {
			if (tabout)
			    printf ("	%2d	%2d	%2d",
				    gc[0]%10000/100, gc[0]%100, gc[0]/10000);
			else
			    printf (" %2d %2d %2d",
				    gc[0]%10000/100, gc[0]%100, gc[0]/10000);
			}
		    if (refcat == GSC2) {
			if (tabout)
			    printf ("	%d", gc[0]);
			else
			    printf ("  %2d  ", gc[0]);
			}

		    /* Number of stars in search radius */
		    if (tabout)
			printf ("	%d", ng);
		    else
			printf (" %d", ng);
		    dec = (gdec[0] + cdeco) * 0.5;
		    if (degout) {
			if ((gra[0] - crao) > 180.0)
			    da = gra[0] - crao - 360.0;
			else if ((gra[0] - crao) < -180.0)
			    da = gra[0] - crao + 360.0;
			else
			    da = gra[0] - crao;
			dd = gdec[0] - cdeco;
			gdist = sqrt (da*da + dd*dd);
			ndist = 5;
			}
		    else {
			if ((gra[0] - crao) > 180.0)
			    da = 3600.0*(gra[0]-crao-360.0)*cos(degrad(dec));
			else if ((gra[0] - crao) < -180.0)
			    da = 3600.0*(gra[0]+360.0-crao)*cos(degrad(dec));
			else
			    da = 3600.0 * (gra[0] - crao) * cos (degrad (dec));
			dd = 3600.0 * (gdec[0] - cdeco);
			gdist = 3600.0 * gx[0];
			ndist = 2;
			}
		    if (tabout)
			printf ("	");
		    else
			printf (" ");
		    PrintNum (das, da, ndist);
		    if (tabout)
			printf ("	");
		    else
			printf (" ");
		    PrintNum (dds, dd, ndist);
		    if (tabout)
			printf ("	");
		    else
			printf (" ");
		    PrintNum (drs, gdist, ndist);
		    printf ("\n");
		    }
		notprinted = 0;
		continue;
		}

	    /* List the brightest or closest MAXSTARS reference stars */
	    if (sortmag > 0 && sortmag <= nmag)
		magsort = sortmag - 1;
	    else
		magsort = 0;
	    CatMagName (sortmag, refcat, magname);
	    if (ng > ngmax) {
		if ((verbose || printhead) && !closest) {
		    if (distsort) {
			if (ng > 1)
			    printf ("Closest %d / %d %s (closer than %.2f arcsec)",
				ns, ng, title, 3600.0*gx[ns-1]);
		        else
			    printf ("Closest of %d %s",ng, title);
			}
		    else if (maglim1 > 0.0) {
			double magmin, magmax;
			magmin = gm[magsort][0];
			magmax = gm[magsort][0];
			for (is = 0; is < ns; is++) {
			    if (gm[magsort][is] > magmax)
				magmax = gm[magsort][is];
			    if (gm[magsort][is] < magmin)
				magmax = gm[magsort][is];
			    }
			printf ("%d / %d %s (%s between %.2f and %.2f)",
			    ns, ng, title, magname, magmin, magmax);
			}
		    else {
			double magmax;
			magmax = gm[magsort][0];
			for (is = 0; is < ns; is++) {
			    if (gm[magsort][is] > magmax)
				magmax = gm[magsort][is];
			    }
			printf ("%d / %d %s (%s brighter than %.2f)",
		 	    ns, ng, title, magname, magmax);
			}
		    printf ("\n");
		    }
		}
	    else {
	        if (verbose || printhead) {
		    if (maglim1 > 0.0)
			printf ("%d %s, %s between %.2f and %.2f\n",
			    ng, title, magname, maglim1, maglim2);
		    else if (maglim2 > 0.0)
			printf ("%d %s, %s brighter than %.2f\n",
			    ng, title, magname, maglim2);
		    else if (verbose)
			printf ("%d %s\n", ng, title);
		    }
		}

	    /* Open result catalog file */
	    if (wfile && icat == 0) {
		if (objname)
		    strcpy (filename,objname);
		else
		    strcpy (filename,"search");
		for (i = 0; i < ncat; i++) {
		    strcat (filename,".");
		    strcat (filename,refcatname[icat]);
		    }
		if (printxy)
		    strcat (filename,".match");

		if (afile)
		    fd = fopen (filename, "a");
		else
		    fd = fopen (filename, "w");

		/* Free result arrays and return if cannot write file */
		if (fd == NULL) {
		    if (afile)
			fprintf (stderr, "%s:  cannot append to file %s\n",
				 cpname, filename);
		    else
			fprintf (stderr, "%s:  cannot write file %s\n",
				 cpname, filename);
        	    return (0);
		    }
		}
            }

	/* Write heading */
	if (votab) {
	    nf = vothead(refcat,refcatname[icat],mprop,typecol,ns,cra,cdec,drad);
	    if (strlen (voerror) > 0) {
		printf ("<TR>\n<TD>ERROR: %s</TD>", voerror);
		for (i = 1; i < nf; i++)
		    printf ("<TD/>");
		printf ("\n</TR>/n");
		vottail ();
		return (ns);
		}
	    }

	else if (ng == 0 && (!tabout || (tabout && !printabhead))) {
	    if (verbose)
		printf ("No %s Stars Found\n", title);
	    }

	else if (tabout && nohead) {
	    catalog = CatName (refcat, refcatname[icat]);
	    sprintf (headline, "catalog	%s", catalog);
	    if (wfile)
		fprintf (fd, "%s\n", headline);
	    else
		printf ("%s\n", headline);

	    if (!ranges) {
		if (sysout == WCS_GALACTIC) {
		    num2str (rastr, crao, 12, nddeg);
		    if (wfile)
			fprintf (fd, "glon	%s\n", rastr);
		    else
			printf ("glon	%s\n", rastr);
		    num2str (decstr, cdeco, 12, nddeg);
		    if (wfile)
			fprintf (fd, "glat	%s\n", decstr);
		    else
			printf ("glat	%s\n", decstr);
		    }
		else if (sysout == WCS_ECLIPTIC) {
		    num2str (rastr, crao, 12, nddeg);
		    if (wfile)
			fprintf (fd, "elon	%s\n", rastr);
		    else
			printf ("elon	%s\n", rastr);
		    num2str (decstr, cdeco, 12, nddeg);
		    if (wfile)
			fprintf (fd, "elat	%s\n", decstr);
		    else
			printf ("elat	%s\n", decstr);
		    }
		else if (degout) {
		    num2str (rastr, crao, 12, nddeg);
		    if (wfile)
			fprintf (fd, "ra	%s\n", rastr);
		    else
			printf ("ra	%s\n", rastr);
		    num2str (decstr, cdeco, 12, nddeg);
		    if (wfile)
			fprintf (fd, "dec	%s\n", decstr);
		    else
			printf ("dec	%s\n", decstr);
		    }
		else {
		    ra2str (rastr, 32, crao, ndra);
		    if (wfile)
			fprintf (fd, "ra	%s\n", rastr);
		    else
			printf ("ra	%s\n", rastr);
		    dec2str (decstr, 32, cdeco, nddec);
		    if (wfile)
			fprintf (fd, "dec	%s\n", decstr);
		    else
			printf ("dec	%s\n", decstr);
		    }
		if (syscoor == WCS_GALACTIC && syscoor != sysout) {
		    num2str (rastr, cra, 12, nddeg);
		    if (wfile)
			fprintf (fd, "glon	%s\n", rastr);
		    else
			printf ("glon	%s\n", rastr);
		    num2str (decstr, cdec, 12, nddeg);
		    if (wfile)
			fprintf (fd, "glat	%s\n", decstr);
		    else
			printf ("glat	%s\n", decstr);
		    }
		if (syscoor == WCS_ECLIPTIC && syscoor != sysout) {
		    num2str (rastr, cra, 12, nddeg);
		    if (wfile)
			fprintf (fd, "elon	%s\n", rastr);
		    else
			printf ("elon	%s\n", rastr);
		    num2str (decstr, cdec, 12, nddeg);
		    if (wfile)
			fprintf (fd, "elat	%s\n", decstr);
		    else
			printf ("elat	%s\n", decstr);
		    }
		}

	    /* Minimum number of plate IDs for USNO-B1.0 catalog */
	    if (minid != 0) {
		sprintf (headline, "minid	%d", minid);
		if (wfile)
		    fprintf (fd, "%s\n", headline);
		if (tabout)
		     printf ("%s\n", headline);
		}

	    /* Minimum proper motion quality for USNO-B1.0 catalog */
	    if (minpmqual > 0) {
		sprintf (headline, "minpmq	%d", minpmqual);
		if (wfile)
       		    fprintf (fd, "%s\n", headline);
       		if (tabout)
		    printf ("%s\n", headline);
		}
	
	    if (wfile) {
		if (sysout == WCS_GALACTIC)
		    fprintf (fd,"radecsys	galactic\n");
		else if (sysout == WCS_ECLIPTIC)
		    fprintf (fd,"radecsys	ecliptic\n");
		else if (sysout == WCS_B1950)
		    fprintf (fd,"radecsys	fk4\n");
		else
		    fprintf (fd,"radecsys	fk5\n");
		fprintf (fd, "equinox	%.4f\n", eqout);
		if (!printepoch)
		    fprintf (fd, "epoch	%.4f\n", epout);
		}
	    else {
		if (sysout == WCS_GALACTIC)
		    printf ("radecsys	galactic\n");
		else if (sysout == WCS_ECLIPTIC)
		    printf ("radecsys	ecliptic\n");
		else if (sysout == WCS_B1950)
		    printf ("radecsys	fk4\n");
		else
		    printf ("radecsys	fk5\n");
		printf ("equinox	%.4f\n", eqout);
		if (!printepoch)
		    printf ("epoch	%.4f\n", epout);
		}

	    if (mprop == 1) {
		if (wfile) {
		    fprintf (fd, "rpmunit	mas/year\n");
		    fprintf (fd, "dpmunit	mas/year\n");
		    }
		else {
		    printf ("rpmunit	mas/year\n");
		    printf ("dpmunit	mas/year\n");
		    }
		}

	    das = dra0  / 3600.0;
	    dds = ddec0 / 3600.0;
	    drs = rad0;
	    if (drs < 0.0)
		drs = -drs;
	    else if (drs == 0.0)
		drs = sqrt (das*das + dds*dds);
	    if (das <= 0.0) {
		das = drs;
		dds = drs;
		}
 	    if (dra0 > 0.0 || rad0 < 0.0) {
		if (syscoor == WCS_GALACTIC || syscoor == WCS_ECLIPTIC) {
		    if (wfile) {
			fprintf (fd, "dlonsec	%.2f\n", das);
			fprintf (fd, "dlatsec	%.2f\n", dds);
			}
		    else {
			printf ("dlonsec	%.2f\n", das);
			printf ("dlatsec	%.2f\n", dds);
			}
		    }
		else {
		    if (wfile) {
			fprintf (fd, "drasec	%.2f\n", das);
			fprintf (fd, "ddecsec	%.2f\n", dds);
			}
		    else {
			printf ("drasec	%.2f\n", das);
			printf ("ddecsec	%.2f\n", dds);
			}
		    }
		}
	    else if (rad0 > 0) {
		if (wfile)
		    fprintf (fd, "radsec	%.2f\n", drs);
		else
		    printf ("radsec	%.2f\n", drs);
		}

	    if (catsort > 0) {
		switch (catsort) {
		    case SORT_DEC:
			if (wfile)
			    fprintf (fd, "catsort	dec\n");
			else
			    printf ("catsort	dec\n");
			break;
		    case SORT_DIST:
			if (wfile)
			    fprintf (fd, "catsort	dist\n");
			else
			    printf ("catsort	dist\n");
			break;
		    case SORT_MAG:
			if (wfile)
			    fprintf (fd, "catsort	mag%c\n", sortletter);
			else
			    printf ("catsort	mag%c\n", sortletter);
			break;
		    case SORT_RA:
			if (wfile)
			    fprintf (fd, "catsort	ra\n");
			else
			    printf ("catsort	ra\n");
			break;
		    case SORT_ID:
			if (wfile)
			    fprintf (fd, "catsort	id\n");
			else
			    printf ("catsort	id\n");
			break;
		    default:
			break;
		    }
		}

	    if (wfile)
		fprintf (fd, "program	%s %s\n", progname, RevMsg);
	    else
		printf ("program	%s %s\n", progname, RevMsg);

	    /* Print column headings */
	    if (refcat == TABCAT && strlen(starcat[icat]->keyid) > 0)
		sprintf (headline,"%s          ", starcat[icat]->keyid);
	    else if (refcat == SKYBOT) {
		strcpy (headline, "object");
		for (i = 6; i < nnfld; i++)
		    strcat (headline, " ");
		}
	    else
		CatID (headline, refcat);
	    headline[nnfld] = (char) 0;

	    if (sysout == WCS_GALACTIC)
		strcat (headline,"	long_gal   	lat_gal  ");
	    else if (sysout == WCS_ECLIPTIC)
		strcat (headline,"	long_ecl   	lat_ecl  ");
	    else if (sysout == WCS_B1950)
		strcat (headline,"	ra1950      	dec1950  ");
	    else
		strcat (headline,"	ra      	dec      ");
	    if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4)
		strcat (headline,"	raerr	decerr");
	    if (refcat == USAC || refcat == USA1 || refcat == USA2 ||
		refcat == UAC  || refcat == UA1  || refcat == UA2)
		strcat (headline,"	magb	magr	plate");
	    else if (refcat == TMPSC || refcat == TMIDR2)
		strcat (headline,"	magj  	magh  	magk  ");
	    else if (refcat == TMPSCE)
		strcat (headline,"	magj  	magh  	magk 	magje 	maghe 	magke ");
	    else if (refcat == TMXSC)
		strcat (headline,"	magj  	magh  	magk  	size  ");
	    else if (refcat == UB1)
		strcat (headline, "	magb1	magr1	magb2	magr2	magn 	pm	ni	sg");
	    else if (refcat == YB6)
		strcat (headline, "	magb 	magr 	magj 	magh 	magk ");
	    else if (refcat == SDSS)
		strcat (headline, "	magu 	magg 	magr 	magi 	magz ");
	    else if (refcat == GSC2)
		strcat (headline,"	magf	magj	magn  	magv ");
	    else if (refcat == UCAC2)
		strcat (headline,"	magj	magh	magk  	magc ");
	    else if (refcat == UCAC3 || refcat == UCAC4)
		strcat (headline,"	magb 	magr 	magi 	magj 	magh 	magk 	magm 	maga ");
	    else if (refcat == SKYBOT)
		strcat (headline,"	magv	gdist	hdist ");
	    else if (refcat == IRAS)
		strcat (headline,"	f10m  	f25m  	f60m   	f100m ");
	    else if (refcat == HIP)
		strcat (headline,"	magb	magv	parlx 	parer");
	    else if (refcat==TYCHO || refcat==TYCHO2 || refcat==ACT)
		strcat (headline,"	magb	magv");
	    else if (refcat==TYCHO2E)
		strcat (headline,"	magb 	magv 	magbe	magve");
	    else if (refcat==SKY2K)
		strcat (headline,"	magb 	magv 	magph	magpv");
	    else if (refcat==HIP)
		strcat (headline,"	magb 	magv 	prllx	parer");
	    else if (refcat==SKYBOT)
		strcat (headline,"	magv 	gdist  	hdist");
	    else if (refcat == GSC || refcat == GSCACT)
		strcat (headline,"	mag	class	band	N");
	    else if (refcat == UJC)
		strcat (headline,"	mag	plate");
	    else if (nmagr > 0) {
		for (imag = 0; imag < nmagr; imag++) {
		    if (starcat[icat] != NULL &&
			strlen (starcat[icat]->keymag[imag]) > 0)
			sprintf (temp, "	%s ", starcat[icat]->keymag[imag]);
		    else if (nmagr > 1)
			sprintf (temp, "	mag%d ", imag);
		    else
			sprintf (temp, "	mag  ");
		    strcat (headline, temp);
		    }
		}
	    if (typecol == 1)
		strcat (headline,"	type");
	    if (printepoch)
		strcat (headline, "	epoch     ");
	    if (mprop == 2)
		strcat (headline," 	velocity");
	    if (mprop == 1)
		strcat (headline,"	pmra  	pmdec ");
	    if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4)
		strcat (headline,"	epmra	epmdec	ni	nc");
	    if (refcat == GSC2)
		strcat (headline,"	class");
	    if (ranges == NULL)
		strcat (headline,"	arcsec");
	    if (refcat == TABCAT && keyword != NULL) {
		strcat (headline,"	");
		strcat (headline, keyword);
		}
	    if (catsort == SORT_MERGE)
		strcat (headline,"	nmatch");
	    if (gobj1 != NULL && refcat != GSC2 && refcat != SDSS && refcat != SKYBOT) {
		if (starcat[icat] == NULL ||
		    (starcat[icat] != NULL && starcat[icat]->stnum > 0))
		    strcat (headline,"	object");
		}
	    if (gobj1 != NULL && refcat == TXTCAT) {
		strcat (headline,"	pathname");
		}
	    if (printxy)
		strcat (headline, "	X      	Y      ");
	    if (wfile)
		fprintf (fd, "%s\n", headline);
	    else
		printf ("%s\n", headline);

	    strcpy (headline, "-------------------------");
	    headline[nnfld] = (char) 0;
	    strcat (headline,"	------------	------------");
	    if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4)
		strcat (headline,"	-----	-----");
	    if (refcat == TMPSC || refcat == TMIDR2)
		strcat (headline,"	-------	-------	-------");
	    else if (refcat == TMPSCE)
		strcat (headline,"	-------	-------	-------	------	------	------");
	    else if (refcat == TMXSC)
		strcat (headline,"	-------	-------	-------	------");
	    else if (refcat == IRAS)
		strcat (headline,"	-----	-----	-----	-----");
	    else if (refcat == HIP)
		strcat (headline,"	-----	-----	-----	-----");
	    else if (refcat == GSC2)
		strcat (headline,"	-----	-----	-----	-----");
	    else if (nmagr > 0) {
		for (imag = 0; imag < nmagr; imag++)
		    strcat (headline,"	-----");
		}
	    if (refcat == GSC || refcat == GSCACT)
		strcat (headline,"	-----	----	-");
	    else if (refcat == UB1)
		strcat (headline,"	--	--	--");
	    else if (typecol == 1)
		strcat (headline,"	----");
	    else if (typecol == 2)
		strcat (headline,"	-----");
	    if (printepoch)
		strcat (headline, "	----------");
	    if (mprop == 2)
		strcat (headline,"	--------");
	    if (mprop == 1)
		strcat (headline,"	------	------");
	    if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4)
		strcat (headline,"	-----	-----	--	--");
	    if (refcat == GSC2)
		strcat (headline,"	-----");
	    if (ranges == NULL)
		strcat (headline, "	------");
	    if (refcat == TABCAT && keyword != NULL)
		strcat (headline,"	------");
	    if (catsort == SORT_MERGE)
		strcat (headline,"	------");
	    if (gobj1 != NULL && refcat != GSC2 && refcat != SDSS && refcat != SKYBOT) {
		if (starcat[icat] == NULL ||
		    starcat[icat]->stnum > 0)
		     strcat (headline,"	------");
		}
	    if (gobj1 != NULL && refcat == TXTCAT) {
		strcat (headline,"	--------------");
		}
	    if (printxy)
		strcat (headline, "	-------	-------");
	    if (wfile)
		fprintf (fd, "%s\n", headline);
	    else
		printf ("%s\n", headline);
	    nohead = 0;
	    }

	else if (printhead && nohead) {
	    if (printxy)
		strcpy (headline, "  X     Y   ");
	    else {
		if (refcat == GSC || refcat == GSCACT)
		    strcpy (headline, "GSC_number ");
		else if (refcat == GSC2)
		    strcpy (headline, "GSC2_id        ");
		else if (refcat == USAC)
		    strcpy (headline, "USNO_SA_number ");
		else if (refcat == USA1)
		    strcpy (headline, "USNO_SA1_number");
		else if (refcat == USA2)
		    strcpy (headline, "USNO_SA2_number");
		else if (refcat == UAC)
		    strcpy (headline, "USNO_A_number  ");
		else if (refcat == UA1)
		    strcpy (headline, "USNO_A1_number ");
		else if (refcat == UA2)
		    strcpy (headline, "USNO_A2_number ");
		else if (refcat == UB1)
		    strcpy (headline, "USNO_B1_number ");
		else if (refcat == YB6)
		    strcpy (headline, "USNO_YB6_number ");
		else if (refcat == SDSS)
		    strcpy (headline, "SDSS_number          ");
		else if (refcat == SKYBOT)
		    strcpy (headline, "Object          ");
		else if (refcat == UCAC1)
		    strcpy (headline, "UCAC1_num    ");
		else if (refcat == UCAC2)
		    strcpy (headline, "UCAC2_num    ");
		else if (refcat == UCAC3)
		    strcpy (headline, "UCAC3_num    ");
		else if (refcat == UCAC4)
		    strcpy (headline, "UCAC4_num    ");
		else if (refcat == TMPSC || refcat == TMPSCE)
		    strcpy (headline, "2MASS_num.  ");
		else if (refcat == TMXSC)
		    strcpy (headline, "2MASS_XSC   ");
		else if (refcat == TMIDR2)
		    strcpy (headline, "2MIDR2_num.");
		else if (refcat == UJC)
		    strcpy (headline, " UJ_number    ");
		else if (refcat == SAO)
		    strcpy (headline, "SAO_number ");
		else if (refcat == PPM)
		    strcpy (headline, "PPM_number ");
		else if (refcat == SKY2K)
		    strcpy (headline, "SKY_id    ");
		else if (refcat == BSC)
		    strcpy (headline, "BSC_number ");
		else if (refcat == IRAS)
		    strcpy (headline, "IRASnum  ");
		else if (refcat == TYCHO)
		    strcpy (headline, "Tycho_number ");
		else if (refcat == TYCHO2 || refcat == TYCHO2E)
		    strcpy (headline, "Tycho2_num  ");
		else if (refcat == HIP)
		    strcpy (headline, "Hip_num ");
		else if (refcat == ACT)
		    strcpy (headline, "ACT_number  ");
		else if (nnfld > 5) {
		    strcpy (headline, "Number      ");
		    headline[nnfld+3] = (char) 0;
		    }
		else {
		    strcpy (headline, "ID       ");
		    headline[nnfld+3] = (char) 0;
		    }
		}
	    if (sysout == WCS_B1950) {
		if (degout) {
		    if (eqout == 1950.0)
			strcat (headline, "   RA1950     Dec1950   ");
		    else {
			sprintf (temp, " RAB%7.2f   DecB%7.2f  ", eqout, eqout);
			strcat (headline, temp);
			}
		    }
		else {
		    if (eqout == 1950.0)
			strcat (headline, "RA1950      Dec1950    ");
		    else {
			sprintf (temp, "RAB%7.2f  DecB%7.2f ", eqout, eqout);
			strcat (headline, temp);
			}
		    }
		}
	    else if (sysout == WCS_ECLIPTIC)
		strcat (headline, "Ecl Lon    Ecl Lat  ");
	    else if (sysout == WCS_GALACTIC)
		strcat (headline, "Gal Lon    Gal Lat  ");
	    else {
		if (degout) {
		    if (eqout == 2000.0)
			strcat (headline, "   RA2000     Dec2000 ");
		    else {
			sprintf (temp," RAJ%7.2f    DecJ%7.2f ", eqout, eqout);
			strcat (headline, temp);
			}
		    }
		else {
		    if (eqout == 2000.0)
			strcat (headline, " RA2000       Dec2000   ");
		    else {
			sprintf (temp,"RAJ%7.2f   DecJ%7.2f  ", eqout, eqout);
			strcat (headline, temp);
			}
		    }
		}
	    if (refcat == USAC || refcat == USA1 || refcat == USA2 ||
		refcat == UAC  || refcat == UA1  || refcat == UA2)
		strcat (headline, "  MagB  MagR");
	    else if (refcat == UB1)
		strcat (headline, "  MagB1  MagR1  MagB2  MagR2  MagN ");
	    else if (refcat == YB6)
		strcat (headline, "  MagB   MagR   MagJ   MagH   MagK ");
	    else if (refcat == SDSS)
		strcat (headline, "  Magu   Magg   Magr   Magi   Magz ");
	    else if (refcat == UJC)
		strcat (headline, "  Mag ");
	    else if (refcat == GSC || refcat == GSCACT)
		strcat (headline, "   Mag ");
	    else if (refcat==SAO || refcat==PPM || refcat==BSC || refcat==UCAC1)
		strcat (headline, "    Mag");
	    else if (refcat==SKY2K)
		strcat (headline, "   MagB   MagV  MagPh  MagPv");
	    else if (refcat==UCAC2)
		strcat (headline, "   MagJ   MagH   MagK   MagC");
	    else if (refcat == UCAC3 || refcat == UCAC4)
		strcat (headline,"  MagB  MagR  MagI  MagJ  MagH  MagK  MagM  MagA");
	    else if (refcat==SKYBOT)
		strcat (headline, "     MagV   GDist  HDist");
	    else if (refcat==TMPSC || refcat == TMIDR2)
		strcat (headline, "   MagJ    MagH    MagK  ");
	    else if (refcat==TMPSCE)
		strcat (headline, "   MagJ    MagH    MagK   MagJe  MagHe  MagKe");
	    else if (refcat==TMXSC)
		strcat (headline, "   MagJ    MagH    MagK  ");
	    else if (refcat==IRAS)
		strcat (headline, "  f10m   f25m   f60m   f100m");
	    else if (refcat==GSC2)
		strcat (headline, "  MagF  MagJ  MagN  MagV");
	    else if (refcat==HIP)
		strcat (headline, "  MagB  MagV  Parlx Parer");
	    else if (refcat==TYCHO || refcat==TYCHO2 || refcat==ACT)
		strcat (headline, "   MagB   MagV  ");
	    else if (refcat==TYCHO2E) 
		strcat (headline, "   MagB   MagV MagBe MagVe");
	    else if (nmagr > 0) {
		for (imag = 0; imag < nmagr; imag++) {
		    if (starcat != NULL &&
			strlen(starcat[icat]->keymag[imag]) > 0) {
			strcat (headline," ");
			sprintf (temp, "%s", starcat[icat]->keymag[imag]);
			strcat (headline, temp);
			}
		    else if (nmagr > 1) {
			sprintf (temp, " Mag%d", imag+1);
			strcat (headline, temp);
			}
		    else
			strcat (headline, " Mag");
		    }
		}
	    if (refcat == USAC || refcat == USA1 || refcat == USA2 ||
		refcat == UAC  || refcat == UA1  || refcat == UA2 ||
		refcat == UJC)
		strcat (headline, "  Plate");
	    else if (refcat == UB1)
		strcat (headline, " PM NI SG");
	    else if (refcat==GSC2)
		strcat (headline, " Class");
	    else if (refcat == GSC || refcat == GSCACT)
		strcat (headline, " Class Band N");
	    else if (refcat==TMXSC)
		strcat (headline, "   Size");
	    else if (typecol == 1)
		strcat (headline, " Type");
	    else if (gcset && refcat != UCAC2 && refcat != UCAC3 && refcat != UCAC4)
		strcat (headline, "     Peak");
	    if (printepoch)
		strcat (headline, "   Epoch   ");
	    /* if (mprop == 1)
		strcat (headline, "   pmRA  pmDec"); */
	    if (mprop == 2)
		strcat (headline, " Velocity");
	    if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4)
		strcat (headline, " nim ncat");
	    if (ranges == NULL)
		strcat (headline, "  Arcsec");
	    if (gobj1 != NULL && refcat != GSC2 && refcat != SDSS && refcat != SKYBOT) {
		if (starcat[icat] == NULL || starcat[icat]->stnum > 0)
		    strcat (headline,"  Object");
		}
	    if (gobj1 != NULL && refcat == TXTCAT) {
		strcat (headline,"  Pathname");
		}
	    if (catsort == SORT_MERGE)
	        strcat (headline, "  Nmatch");
	    if (wfile)
		fprintf (fd, "%s\n", headline);
	    else
		printf ("%s\n", headline);
	    }
	nohead = 0;

    /* Find maximum separation for formatting */
    gdmax = 0.0;
    for (i = 0; i < ns; i++) {
	if (gx[i] > 0.0) {
	    gdist = 3600.0 * gx[i];
	    if (gdist > gdmax)
		gdmax = gdist;
	    }
	}

    string[0] = (char) 0;
    if (closest) ns = 1;
    for (i = 0; i < ns; i++) {

	/* Set source position epoch passed as a magnitude */
	if (printepoch) {
	    if (mprop == 2)
		epoch = gm[nmagr+1][i];
	    else
		epoch = gm[nmagr][i];
	    if (epoch == 99.0)
		epoch = 0.0;
	    }

	/* Check entry epoch to see if it is within specified range */
	if ((epoch1 > 0.0 && epoch2 > 0.0) &&
	    (epoch < epoch1 || epoch > epoch2))
	    continue;
	else if (epoch1 > 0.0 && epoch < epoch1)
	    continue;
	else if (epoch2 > 0.0 && epoch > epoch2)
	    continue;

	/* Set spectra type (or other 2-character code) */
	if (typecol == 1) {
	    isp[0] = gc[i] / 1000;
	    isp[1] = gc[i] % 1000;
	    if (isp[0] == ' ' && isp[1] == ' ') {
		isp[0] = '_';
		isp[1] = '_';
		}
	    }

	/* For HST Guide Star Catalog, set number of entries and band */
	if (refcat == GSC || refcat == GSCACT) {
	    ngsc = gc[i] / 10000;
	    gc[i] = gc[i] - (ngsc * 10000);
	    band = gc[i] / 100;
	    gc[i] = gc[i] - (band * 100);
	    }

	/* For USNO-B1.0 catalog, drop sources on too few plates */
	if (refcat == UB1 && minid > 0) {
	    nid = gc[i]%100;
	    if (nid < minid)
		continue;
	    }

	/* For USNO UCAC2 and UCAC3 catalogs, set errors and number of epochs */
	if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4) {
	    era = gm[nmagr][i] * cosdeg (gdec[i]) * 3600.0;
	    edec = gm[nmagr+1][i] * 3600.0;
	    epmr = gm[nmagr+2][i] * cosdeg (gdec[i]) * 3600000.0;
	    epmd = gm[nmagr+3][i] * 3600000.0;
	    nim = gc[i] / 1000;
	    nct = gc[i] % 1000;
	    }

	if (gy[i] > 0.0) {
	    if (degout) {
		deg2str (rastr, 32, gra[i], nddeg);
		deg2str (decstr, 32, gdec[i], nddeg);
		}
	    else {
		ra2str (rastr, 32, gra[i], ndra);
		dec2str (decstr, 32, gdec[i], nddec);
		}
	    if (gx[i] > 0.0)
		gdist = 3600.0 * gx[i];
	    else
		gdist = 0.0;

	    /* Convert proper motion to milliarcsec/year from deg/year */
	    if (mprop == 1) {
		pra = gpra[i] * 3600000.0 * cosdeg (gdec[i]);
		pdec = gpdec[i] * 3600000.0;
		}

	    /* Set up object name or number to print */
	    if (starcat[icat] != NULL) {
		if (starcat[icat]->stnum < 0 && gobj1 != NULL) {
		    strncpy (numstr, gobj1[i], 32);
		    if (lofld > 0) {
			for (j = 0; j < lofld; j++) {
			    if (!numstr[j])
				numstr[j] = ' ';
			    }
			}
		    }
		else
		    CatNum (refcat,-nnfld,starcat[icat]->nndec,gnum[i],numstr);
		}

	    else if (refcat == SDSS || refcat == GSC2 || refcat == SKYBOT) {
		strcpy (numstr, gobj[i]);
		lobj = strlen (gobj[i]);
		if (lobj < nnfld) {
		    for (j = lobj; j < nnfld; j++)
			strcat (numstr, " ");
		    }
		}
	    else
		CatNum (refcat, -nnfld, nndec, gnum[i], numstr);

	    if (votab) {
		sprintf (headline, "<tr>\n<td>%s</td><td>%s</td><td>%s</td>",
			numstr,rastr,decstr);
		if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4) {
		    sprintf (temp,"<td>%5.1f</td><td>%5.1f</td>", era, edec);
	            strcat (headline, temp);
		    }
		for (imag = 0; imag < nmagr; imag++) {
		    sprintf (temp, "<td>%.2f</td>", gm[imag][i]);
		    strcat (headline, temp);
		    }
		if (refcat == USAC || refcat == USA1 || refcat == USA2 ||
		    refcat == UAC  || refcat == UA1  || refcat == UA2) {
		    sprintf (temp, "<td>%d</td>", gc[i]);
		    strcat (headline, temp);
		    }
		else if (refcat == GSC || refcat == GSCACT) {
		    sprintf (temp, "<td>%d</td><td>%d</td><td>%d</td>",
			    gc[i], band, ngsc);
		    strcat (headline, temp);
		    }
		if (typecol == 1) {
		    sprintf (temp, "<td>%2s</td>", isp);
		    strcat (headline, temp);
		    }
		if (mprop == 2) {
		    sprintf (temp, "<td>%8.2f</td>", gm[nmagr][i]);
	            strcat (headline, temp);
		    }
		if (mprop == 1) {
	            sprintf (temp, "<td>%6.1f</td><td>%6.1f</td>", pra, pdec);
	            strcat (headline, temp);
		    }
		if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4) {
		    sprintf (temp,"<td>%5.1f</td><td>%5.1f</td><td>%2d</td><td>%2d</td>",
				epmr, epmd, nim, nct);
	            strcat (headline, temp);
		    }
		sprintf (temp, "<td>%.5f</td>\n</tr>", gdist / 3600.0);
	        strcat (headline, temp);

		printf ("%s\n", headline);
		}

	    /* Print or write tab-delimited output line for one star */
	    else if (tabout) {
		if (refcat == USAC || refcat == USA1 || refcat == USA2 ||
		    refcat == UAC  || refcat == UA1  || refcat == UA2)
		    sprintf (headline, "%s	%s	%s	%.1f	%.1f	%d",
		     numstr, rastr, decstr, gm[0][i], gm[1][i], gc[i]);
		else if (refcat == GSC || refcat == GSCACT)
	            sprintf (headline,
			 "%s	%s	%s	%.2f	%d	%d	%d",
			 numstr, rastr, decstr, gm[0][i], gc[i], band, ngsc);
		else if (refcat==TMPSC || refcat==TMIDR2 ||
			 refcat == TMPSCE || refcat==TMXSC) {
		    sprintf (headline, "%s	%s	%s", numstr, rastr, decstr);
		    for (imag = 0; imag < 3; imag++) {
			if (gm[imag][i] > 100.0)
			    sprintf (temp, "	%6.3fL", gm[imag][i]-100.0);
			else
			    sprintf (temp, "	%6.3f ", gm[imag][i]);
			strcat (headline, temp);
			}
		    if (refcat == TMPSCE) {
			for (imag = 3; imag < 6; imag++) {
			    sprintf (temp, "	%5.3f ", gm[imag][i]);
			    strcat (headline, temp);
			    }
			}
		    }
		else if (refcat == GSC2) {
		    for (j = 0; j < 4; j++) {
			if (gm[j][i] > 90.0) gm[j][i] = 99.99;
			}
		    sprintf (headline, "%s	%s	%s	%.2f	%.2f	%.2f	%.2f",
		     numstr,rastr,decstr,gm[0][i],gm[1][i],gm[2][i],gm[3][i]);
		    }
		else if (refcat == IRAS) {
		    sprintf (headline, "%s	%s	%s", numstr, rastr, decstr);
		    for (imag = 0; imag < 4; imag++) {
			if (gm[imag][i] > 100.0) {
			    flux = 1000.0 * pow (10.0, -(gm[imag][i]-100.0) / 2.5);
			    sprintf (temp, "	%.2fL", flux);
			    }
			else {
			    flux = 1000.0 * pow (10.0, -gm[imag][i] / 2.5);
			    sprintf (temp, "	%.2f ", flux);
			    }
			strcat (headline, temp);
			}
		    }
		else if (refcat == UB1)
		    sprintf (headline, "%s	%s	%s	%.2f	%.2f	%.2f	%.2f	%.2f	%2d	%2d	%2d",
		     numstr,rastr,decstr,gm[0][i],gm[1][i],gm[2][i],gm[3][i],gm[4][i],
		     gc[i]%10000/100, gc[i]%100, gc[i]/10000);
		else if (refcat == YB6)
		    sprintf (headline, "%s	%s	%s	%.2f	%.2f	%.2f	%.2f	%.2f",
		     numstr,rastr,decstr,gm[0][i],gm[1][i],gm[2][i],gm[3][i],gm[4][i]);
		else if (refcat == SDSS)
		    sprintf (headline, "%s	%s	%s	%.2f	%.2f	%.2f	%.2f	%.2f",
		     numstr,rastr,decstr,gm[0][i],gm[1][i],gm[2][i],gm[3][i],gm[4][i]);
		else if (refcat == HIP)
		    sprintf (headline, "%s	%s	%s	%.2f	%.2f	%.2f	%.2f",
		     numstr,rastr,decstr,gm[0][i],gm[1][i],gm[2][i],gm[3][i]);
		else if (refcat == UJC)
	            sprintf (headline, "%s	%s	%s	%.2f	%d",
		     numstr, rastr, decstr, gm[0][i], gc[i]);
		else if (refcat==SAO || refcat==PPM || refcat == BSC)
	            sprintf (headline, "%s	%s	%s	%.2f",
		     numstr, rastr, decstr, gm[0][i]);
		else if (refcat==TYCHO || refcat==TYCHO2 || refcat==ACT)
	            sprintf (headline, "%s	%s	%s	%.2f	%.2f",
		     numstr, rastr, decstr, gm[0][i], gm[1][i]);
		else if (refcat==TYCHO2E)
	            sprintf (headline, "%s	%s	%s	%.2f	%.2f	%.2f	%.2f",
		     numstr, rastr, decstr, gm[0][i], gm[1][i], gm[2][i], gm[3][i]);
		else {
	            sprintf (headline, "%s	%s	%s", numstr, rastr, decstr);
		    if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4) {
			sprintf (temp,"	%5.3f	%5.3f", era, edec);
	        	strcat (headline, temp);
			}

		    for (imag = 0; imag < nmagr; imag++) {
			sprintf (temp, "	%.2f", gm[imag][i]);
			strcat (headline, temp);
			}
		    if (typecol == 2) {
			sprintf (temp, "	%d", gc[i]);
			strcat (headline, temp);
			}
		    }
		if (typecol == 1) {
		    sprintf (temp, "	%2s", isp);
		    strcat (headline, temp);
		    }
		if (mprop == 2) {
		    if (printepoch) {
			dtemp = DateString (epoch, tabout);
        		strcat (headline, dtemp);
			free (dtemp);
			}
		    sprintf (temp, "	%8.2f", gm[nmagr][i]);
	            strcat (headline, temp);
		    }
		else if (printepoch) {
		    dtemp = DateString (epoch, tabout);
		    strcat (headline, dtemp);
		    free (dtemp);
		    }
		if (mprop == 1) {
	            sprintf (temp, "	%6.1f	%6.1f", pra, pdec);
	            strcat (headline, temp);
		    }
		if (refcat == TABCAT && gcset) {
	            sprintf (temp, "	%d", gc[i]);
	            strcat (headline, temp);
		    }
		else if (refcat == TMXSC) {
		    sprintf (temp,"	%6.1f", ((double)gc[i])* 0.1);
		    strcat (headline, temp);
		    }
		else if (refcat == GSC2) {
	            sprintf (temp, "	%d  ", gc[i]);
		    strcat (headline, temp);
		    }
		else if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4) {
		    sprintf (temp, "	%5.1f	%5.1f	%2d	%2d",
			    epmr, epmd, nim, nct);
		    strcat (headline, temp);
		    }

		if (ranges == NULL) {
	            sprintf (temp, "	%.2f", gdist);
	            strcat (headline, temp);
		    }
		if (refcat == TABCAT && keyword != NULL) {
		    strcat (headline, "	");
		    if (gobj[i] != NULL)
			strcat (headline, gobj[i]);
		    else
			strcat (headline, "___");
		    }
		if (catsort == SORT_MERGE) {
	            sprintf (temp, "	%d", (int)gx[i]);
	            strcat (headline, temp);
		    }
		if ((refcat == BINCAT || refcat == TXTCAT) &&
		     gobj1 != NULL && gobj[i] != NULL) {
		    if (starcat[icat] == NULL || starcat[icat]->stnum > 0) {
			strcat (headline, "	");
			strcat (headline, gobj[i]);
			}
		    }
		if (refcat == TXTCAT && gpath1 != NULL && gpath[i] != NULL) {
		    strcat (headline, "	");
		    strcat (headline, gpath[i]);
		    }
		if (printxy) {
		    strcat (headline, "	");
		    strcat (numstr, xstr);
		    strcat (headline, "	");
		    strcat (numstr, ystr);
		    }
		if (wfile)
		    fprintf (fd, "%s\n", headline);
		else
		    printf ("%s\n", headline);
		}

	    /* Print or write space-delimited output line for one star */
	    else {
		if (printxy) {
		    strcpy (numstr, xstr);
		    strcat (numstr, " ");
		    strcat (numstr, ystr);
		    }
		if (refcat == USAC || refcat == USA1 || refcat == USA2 ||
		    refcat == UAC  || refcat == UA1  || refcat == UA2)
		    sprintf (headline,"%s %s %s %5.1f %5.1f ",
			     numstr,rastr,decstr,gm[0][i],gm[1][i]);
		else if (refcat == TMPSC || refcat == TMIDR2 ||
			 refcat == TMPSCE || refcat == TMXSC) {
		    sprintf (headline, "%s %s %s", numstr, rastr, decstr);
		    for (imag = 0; imag < 3; imag++) {
			if (gm[imag][i] > 100.0)
			    sprintf (temp, " %6.3fL", gm[imag][i]-100.0);
			else
			    sprintf (temp, " %6.3f ", gm[imag][i]);
			strcat (headline, temp);
			}
		    if (refcat == TMPSCE) {
			for (imag = 3; imag < 6; imag++) {
			    sprintf (temp, " %5.3f ", gm[imag][i]);
			    strcat (headline, temp);
			    }
			}
		    }
		else if (refcat == IRAS) {
		    sprintf (headline, "%s %s %s", numstr, rastr, decstr);
		    for (imag = 0; imag < 4; imag++) {
			if (gm[imag][i] > 100.0) {
			    flux = 1000.0 * pow (10.0, -(gm[imag][i]-100.0) / 2.5);
			    sprintf (temp, " %5.2fL", flux);
			    }
			else {
			    flux = 1000.0 * pow (10.0, -gm[imag][i] / 2.5);
			    sprintf (temp, " %5.2f ", flux);
			    }
			strcat (headline, temp);
			}
		    }
		else if (refcat == GSC2) {
		    for (j = 0; j < 4; j++) {
			if (gm[j][i] > 90.0) gm[j][i] = 99.99;
			}
		    sprintf (headline, "%s %s %s %5.2f %5.2f %5.2f %5.2f",
			     numstr, rastr, decstr, gm[0][i], gm[1][i],
			     gm[2][i], gm[3][i]);
		    }
		else if (refcat == HIP)
		    sprintf (headline, "%s %s %s %5.2f %5.2f %5.2f %5.2f",
			     numstr, rastr, decstr, gm[0][i], gm[1][i],
			     gm[2][i], gm[3][i]);
		else if (refcat == GSC || refcat == GSCACT)
	            sprintf (headline, "%s %s %s %6.2f",
			     numstr, rastr, decstr, gm[0][i]);
		else if (refcat == UJC)
		    sprintf (headline,"%s %s %s %6.2f",
			     numstr, rastr, decstr, gm[0][i]);
		else if (refcat == UCAC1)
		    sprintf (headline,"%s  %s %s %6.2f",
			     numstr,rastr,decstr,gm[0][i]);
		else if (refcat == UCAC2)
		    sprintf (headline,"%s  %s %s %6.2f %6.2f %6.2f %6.2f",
			     numstr,rastr,decstr,gm[0][i],
			     gm[1][i], gm[2][i], gm[3][i]);
		else if (refcat == UCAC3 || refcat == UCAC4) {
		    sprintf (headline,"%s  %s %s", numstr,rastr,decstr);
		    for (imag = 0; imag < nmagr; imag++) {
			sprintf (temp, " %.2f", gm[imag][i]);
			strcat (headline, temp);
			}
		    }
		else if (refcat==SAO || refcat==PPM || refcat == BSC)
		    sprintf (headline,"  %s  %s %s %6.2f",
			     numstr,rastr,decstr,gm[0][i]);
		else if (refcat==TYCHO || refcat==TYCHO2 || refcat==ACT)
		    sprintf (headline,"%s %s %s %6.2f %6.2f ",
			     numstr,rastr,decstr,gm[0][i],gm[1][i]);
		else if (refcat==TYCHO2E)
		    sprintf (headline,"%s %s %s %6.2f %6.2f %5.2f %5.2f",
			     numstr,rastr,decstr,gm[0][i],gm[1][i],gm[2][i],gm[3][i]);
		else {
		    sprintf (headline,"%s %s %s ", numstr, rastr, decstr);
		    for (imag = 0; imag < nmagr; imag++) {
			sprintf (temp, " %6.2f", gm[imag][i]);
			strcat (headline, temp);
			}
		    }
		if (refcat == USAC || refcat == USA1 || refcat == USA2 ||
		    refcat == UAC  || refcat == UA1  || refcat == UA2 ||
		    refcat == UJC) {
		    sprintf (temp," %4d", gc[i]);
		    strcat (headline, temp);
		    }
		else if (refcat == UB1) {
		    sprintf (temp," %2d %2d %2d",
			    gc[i]%10000/100, gc[i]%100, gc[i]/10000);
		    strcat (headline, temp);
		    }
		else if (refcat == GSC || refcat == GSCACT) {
	            sprintf (temp, " %4d %4d %2d", gc[i], band, ngsc);
		    strcat (headline, temp);
		    }
		else if (refcat == GSC2) {
	            sprintf (temp, " %2d  ", gc[i]);
		    strcat (headline, temp);
		    }
		else if (typecol == 1) {
		    sprintf (temp, "  %2s", isp);
		    strcat (headline, temp);
		    }
		else if (refcat == TMXSC) {
		    sprintf (temp," %6.1f", ((double)gc[i])* 0.1);
		    strcat (headline, temp);
		    }
		else if (gcset && refcat != UCAC2 && refcat != UCAC3 && refcat != UCAC4) {
		    sprintf (temp," %7d",gc[i]);
		    strcat (headline, temp);
		    }
		if (mprop == 2) {
		    if (printepoch) {
			dtemp = DateString (epoch, tabout);
			strcat (headline, dtemp);
			free (dtemp);
			}
		    sprintf (temp, " %9.2f", gm[nmagr][i]);
		    strcat (headline, temp);
		    }
		else if (printepoch) {
		    dtemp = DateString (epoch, tabout);
		    strcat (headline, dtemp);
		    free (dtemp);
		    }
	    	/* if (mprop == 1) {
	    	    sprintf (temp, " %6.1f %6.1f", pra, pdec);
	    	    strcat (headline, temp);
		    } */

		if (refcat == UCAC2 || refcat == UCAC3 || refcat == UCAC4) {
		    sprintf (temp, "  %2d   %2d", nim, nct);
		    strcat (headline, temp);
		    }

		/* Add distance from search center */
		if (ranges == NULL) {
		    if (gdmax < 100.0)
			sprintf (temp, "  %5.2f", gdist);
		    else if (gdmax < 1000.0)
			sprintf (temp, "  %6.2f", gdist);
		    else if (gdmax < 10000.0)
			sprintf (temp, "  %7.2f", gdist);
		    else if (gdmax < 100000.0)
			sprintf (temp, "  %8.2f", gdist);
		    else
			sprintf (temp, "  %.2f", gdist);
		    strcat (headline, temp);
		    }

		/* Add specified keyword or object name */
		if (refcat == TABCAT && keyword != NULL) {
		    if (gobj[i] != NULL)
			sprintf (temp, "  %s", gobj[i]);
		    else
			sprintf (temp, "  ___");
		    strcat (headline, temp);
		    }
		else if ((refcat == BINCAT || refcat == TXTCAT) &&
			 gobj1 != NULL && gobj[i] != NULL) {
		    if (starcat[icat] == NULL || starcat[icat]->stnum > 0) {
			sprintf (temp, "  %s", gobj[i]);
			strcat (headline, temp);
			}
		    }

		/* Add data pathname if present */
		if (refcat == TXTCAT && gpath1 != NULL && gpath[i] != NULL) {
		    sprintf (temp, " %s", gpath[i]);
		    strcat (headline, temp);
		    }

		/* Add number of original entries in merged output */
		if (catsort == SORT_MERGE) {
	            sprintf (temp, "  %2d", (int)gx[i]);
	            strcat (headline, temp);
		    }

		/* Write to file or standard output */
		if (wfile)
		    fprintf (fd, "%s\n", headline);
		else
		    printf ("%s\n", headline);
		}
	    }
	}

	/* If searching more than one catalog, separate them with blank line */
	if (ncat > 0 && icat < ncat-1)
	    printf ("\n");

	/* Free memory used for object names in current catalog */
	if (gobj1 != NULL) {
	    for (i = 0; i < ns; i++) {
		if (gobj[i] != NULL) free (gobj[i]);
		gobj[i] = NULL;
		}
	    }
	if (votab) vottail ();
	}

    /* Close output file */
    if (wfile)
	fclose (fd);

    return (ns);
    }


/* Get a center and radius for a search area.  If the image center is not
 * given in the system of the reference catalog, convert it.
 * Return 0 if OK, else -1
 */

static int
GetArea (verbose,syscoor,eqcoor,sysout,eqout,epout,cra,cdec,dra,ddec,drad,dradi,
	 crao,cdeco)

int	verbose;	/* Extra printing if =1 */
int	syscoor;	/* Coordinate system of input search coordinates */
double	eqcoor;		/* Equinox in years of input coordinates */
int	sysout;		/* Coordinate system of output coordinates */
double	eqout;		/* Equinox in years of output coordinates */
double	epout;		/* Epoch in years of output coordinates (0=eqcoor */
double	*cra;		/* Search center longitude/right ascension (degrees returned)*/
double	*cdec;		/* Search center latitude/declination (degrees returned) */
double	*dra;		/* Longitude/RA half-width (degrees returned) */
double	*ddec;		/* Latitude/Declination half-width (degrees returned) */
double	*drad;		/* Radius to search in degrees (0=box) (returned) */
double	*dradi;		/* Radius of inner edge of search annulus (returned) */
double	*crao;		/* Output search center longitude/right ascension */
double	*cdeco;		/* Output search center latitude/declination */
{
    char rstr[32], dstr[32], cstr[32], dstri[32], dstro[32];

    *cra = ra0;
    *cdec = dec0;
    if (verbose) {
	if (syscoor == WCS_XY) {
	    num2str (rstr, *cra, 10, 5);
            num2str (dstr, *cdec, 10, 5);
	    }
	else if (syscoor==WCS_ECLIPTIC || syscoor==WCS_GALACTIC || degout0) {
	    deg2str (rstr, 32, *cra, nddeg);
            deg2str (dstr, 32, *cdec, nddeg);
	    }
	else {
	    ra2str (rstr, 32, *cra, ndra);
            dec2str (dstr, 32, *cdec, nddec);
	    }
	wcscstr (cstr, syscoor, 0.0, 0.0);
	fprintf (stderr,"Center:  %s   %s %s\n", rstr, dstr, cstr);
	}

    *crao = *cra;
    *cdeco = *cdec;
    if (syscoor != sysout || eqcoor != eqout) {
	wcscon (syscoor, sysout, eqcoor, eqout, crao, cdeco, epout);
	if (verbose) {
	    if (syscoor == WCS_ECLIPTIC || syscoor == WCS_GALACTIC || degout0) {
		deg2str (rstr, 32, *crao, nddeg);
        	deg2str (dstr, 32, *cdeco, nddeg);
		}
	    else {
		ra2str (rstr, 32, *crao, ndra);
        	dec2str (dstr, 32, *cdeco, nddec);
		}
	    wcscstr (cstr, sysout, 0.0, 0.0);
	    fprintf (stderr,"Center:  %s   %s %s\n", rstr, dstr, cstr);
	    }
	}

    /* Set search box radius from command line, if it is there */
    if (dra0 > 0.0) {
	*drad = 0.0;
	if (syscoor == WCS_XY) {
	    *dra = dra0;
	    *ddec = ddec0;
	    }
	else {
	    *ddec = ddec0 / 3600.0;
	    if (*cdec < 90.0 && *cdec > -90.0) {
		if (rdra)
		    *dra = dra0 / 3600.0;
		else
		    *dra = (dra0 / 3600.0) / cos (degrad (*cdec));
		}
	    else
		*dra = 180.0;
	    }
	}

    /* Search box */
    else if (rad0 < 0.0) {
	*drad = 0.0;
	if (syscoor == WCS_XY) {
	    *dra = -rad0;
	    *ddec = -rad0;
	    }
	else {
	    *ddec = -rad0 / 3600.0;
	    *dra = *ddec / cos (degrad (*cdec));
	    }
	}

    /* Search circle */
    else if (rad0 > 0.0) {
	if (syscoor == WCS_XY)
	    *drad = rad0;
	else
	    *drad = rad0 / 3600.0;
	*dra = *drad / cos (degrad (*cdec));
	*ddec = *drad;
	if (rad1 > 0.0)
	    *dradi = rad1 / 3600.0;
	else
	    *dradi = 0.0;
	}
    else {
	if (verbose)
	    fprintf (stderr, "GetArea: Illegal radius, rad= %.5f\n",rad0);
	return (-1);
	}

    if (verbose) {
	if (syscoor == WCS_XY) {
	    num2str (rstr, *dra, 10, 5); 
	    num2str (dstr, *ddec, 10, 5); 
	    num2str (dstro, *drad, 10, 5); 
	    num2str (dstri, *dradi, 10, 5); 
	    }
	else if (degout0) {
	    deg2str (rstr, 32, *dra, 6); 
	    deg2str (dstr, 32, *ddec, 6); 
	    deg2str (dstro, 32, *drad, 6); 
	    deg2str (dstri, 32, *dradi, 6); 
	    }
	else {
	    dec2str (rstr, 32, *dra, 2); 
	    dec2str (dstr, 32, *ddec, 2); 
	    dec2str (dstro, 32, *drad, 2); 
	    dec2str (dstri, 32, *dradi, 2); 
	    }
	if (*drad == 0.0)
	    fprintf (stderr,"Area:    %s x %s\n", rstr, dstr);
	else if (*dradi > 0.0)
	    fprintf (stderr, "Radius: %s-%s\n", dstri, dstro);
	else
	    fprintf (stderr, "Radius: %s\n", dstro);
	}

    return (0);
}


static void
SearchHead (icat,sys,eq,ep,cra,cdec,dra,ddec,drad,dradi,nnfld,degout)

int	icat;		/* Number of catalog in list */
int	sys;		/* Coordinate system */
double	eq;		/* Equinox */
double	ep;		/* Epoch */
double	cra, cdec;	/* Center coordinates of search box in degrees */
double	dra, ddec;	/* Width and height of search box in degrees */
double	drad;		/* Radius of search region in degrees */
double	dradi;		/* Inner edge of annulus in degrees (ignore if 0) */
int	nnfld;		/* Number of characters in ID field */
int	degout;		/* Ouput in degrees if 1 */
{
    char rastr[32];
    char decstr[32];
    char cstr[16];
    char oform[16];
    int refcat;
    char *catname;

    if (sys == WCS_XY) {
	num2str (rastr, cra, 10, 5);
	num2str (decstr, cdec, 10, 5);
	}
    else if (sys == WCS_ECLIPTIC || sys == WCS_GALACTIC || degout) {
	deg2str (rastr, 32, cra, nddeg);
	deg2str (decstr, 32, cdec, nddeg);
	}
    else {
	ra2str (rastr, 32, cra, ndra);
	dec2str (decstr, 32, cdec, nddec);
	}

    /* Set type of catalog being searched */
    if (!(refcat = CatCode (refcatname[icat]))) {
	fprintf (stderr,"ListCat: Catalog '%s' is missing\n",refcatname[icat]);
	return;
	}

    /* Label search center */
    sprintf (oform, "%%%ds", nnfld);
    if (objname)
	printf (oform, objname);
    else {
	catname = CatName (refcat, refcatname[icat]);
	printf (oform, catname);
	}
    wcscstr (cstr, sys, eq, ep);
    printf (" %s %s %s", rastr, decstr, cstr);
    if (degout) {
	if (drad != 0.0) {
	    if (dradi > 0.0)
		printf (" r= %.2f - %.2f", dradi,drad);
	    else
		printf (" r= %.2f", drad);
	    }
	else
	    printf (" +- %.2f %.2f", dra, ddec);
	}
    else {
	if (drad != 0.0) {
	    dec2str (decstr, 32, drad, 1);
	    if (dradi > 0.0) {
		dec2str (rastr, 32, dradi, 1);
		printf (" r= %s - %s", rastr, decstr);
		}
	    else
		printf (" r= %s", decstr);
	    }
	else {
	    dec2str (rastr, 32, dra, 1);
	    dec2str (decstr, 32, ddec, 1);
	    printf (" +- %s %s", rastr, decstr);
	    }
	}
    if (classd == 0)
	printf (" stars");
    else if (classd == 3)
	printf (" nonstars");
    if (ep != 0.0)
	printf (" at epoch %9.4f\n", ep);
    else
	printf ("\n");
    return;
}


void
PrintNum (maxnum, num, ndec)

double	maxnum;	/* Maximum value of number to print */
double	num;	/* Number to print */
int	ndec;	/* Number of decimal places in output */
{
    char nform[8];
    int LenNum();

    if (ndec > 0)
	sprintf (nform, "%%%d.%df", LenNum (maxnum,ndec), ndec);
    else
	sprintf (nform, "%%%dd", LenNum (maxnum,ndec));
    printf (nform, num);
}


int
LenNum (maxnum, ndec)

double	maxnum;	/* Maximum value of number to print */
int	ndec;	/* Number of decimal places in output */
{
    if (ndec <= 0)
	ndec = -1;
    if (maxnum < 9.999)
	return (ndec + 3);
    else if (maxnum < 99.999)
	return (ndec + 4);
    else if (maxnum < 999.999)
	return (ndec + 5);
    else if (maxnum < 9999.999)
	return (ndec + 6);
    else
	return (ndec + 7);
}



/* Set parameter values from cgi query line as kw=val&kw=val... */

static void
scatcgi (qstring)

char *qstring;
{
    char *pstring, *parend;

    pstring = qstring;
    while ((parend = strchr (pstring, '&')) != NULL) {
	*parend = (char) 0;
	if (scatparm (pstring))
	    fprintf (stderr, "SCATCGI: %s is not a parameter.\n", pstring);
	pstring = parend + 1;
	}
    if (scatparm (pstring)) {
	if (!votab)
	    fprintf (stderr, "SCATCGI: %s is not a parameter.\n", pstring);
	}
    return;
}


/* Set parameter values from the command line as keyword=value
 * Return 1 if successful, else 0 */

static int
scatparm (parstring)

char *parstring;
{
    char *parname;
    char *parvalue;
    char *parequal;
    char *temp;
    char *refcatn;
    int lcat, lrange;

    /* Check for request for help */
    if (!strncasecmp (parstring,"help", 4)) {
	PrintWebHelp ();
	}

    /* Check for request for command line help */
    if (!strncasecmp (parstring,"comhelp", 7)) {
	PrintUsage (NULL);
	}

    /* Check for request for GSC bandpasses */
    if (!strncasecmp (parstring, "band", 4) ||
	!strncasecmp (parstring, "filt", 4)) {
	PrintGSCBand ();
	}

    /* Check for request for GSC object classes */
    if (!strncasecmp (parstring, "clas", 4) ||
	!strncasecmp (parstring, "obje", 4)) {
	PrintGSClass ();
	}

    /* Check for scat version request */
    if (!strcasecmp (parstring, "version")) {
	PrintUsage ("version");
	}

    /* Separate parameter name and value */
    parname = parstring;
    if ((parequal = strchr (parname,'=')) == NULL)
	return (0);
    *parequal = (char) 0;
    parvalue = parequal + 1;

    /* Get closest source */
    if (!strcasecmp (parname, "closest")) {
	if (!strncasecmp (parvalue, "y", 1)) {
	    catsort = SORT_DIST;
	    nstars = 1;
	    closest++;
	    }
	}

    /* Set range of source numbers to print */
    else if (!strncasecmp (parname,"num",3)) {
	if (ranges) {
	    temp = ranges;
	    lrange = strlen(ranges) + strlen(parvalue) + 2;
	    ranges = (char *) malloc (lrange);
	    strcpy (ranges, temp);
	    strcat (ranges, ",");
	    strcat (ranges, parvalue);
	    free (temp);
	    }
	else {
	    lrange = strlen (parvalue) + 2;
	    ranges = (char *) malloc (lrange);
	    if (strchr (parvalue,'.'))
		match = 1;
	    strcpy (ranges, parvalue);
	    }
	}

    /* Radius in arcseconds */
    else if (!strncasecmp (parname,"degree",6)) {
	if (strchr (parvalue, 'y'))
	     degout0 = 1;
	else
	     degout0 = 0;
	}

    /* Radius in arcseconds */
    else if (!strncasecmp (parname,"rad",3)) {
	if (strchr (parvalue,':'))
	    rad0 = 3600.0 * str2dec (parvalue);
	else if (isnum (parvalue))
	    rad0 = atof (parvalue);
	else {
	    sprintf (voerror, "Search radius %s (seconds) is not a number", parvalue);
	    return (-1);
	    }
	}

    /* Radius in arcminutes */
    else if (!strncasecmp (parname,"mrad",4)) {
	if (strchr (parvalue,':'))	/* hours:minutes:seconds */
	    rad0 = 3600.0 * str2dec (parvalue);
	else if (isnum (parvalue))
	    rad0 = 60.0 * atof (parvalue);
	else {
	    sprintf (voerror, "Search radius %s (minutes) is not a number", parvalue);
	    return (-1);
	    }
	}

    /* Inner Annulus radius in arcseconds */
    else if (!strncasecmp (parname,"inrad",5)) {
	if (strchr (parvalue,':'))	/* hours:minutes:seconds */
	    rad1 = 3600.0 * str2dec (parvalue);
	else if (isnum (parvalue))
	    rad1 = atof (parvalue);
	else {
	    sprintf (voerror, "Search radius %s is not a number", parvalue);
	    return (-1);
	    }
	}

    /* Radius in degrees */
    else if (!strncasecmp (parname,"sr",2) ||
	     !strncasecmp (parname,"drad",4)) {
	if (strchr (parvalue,':'))
	    rad0 = 3600.0 * str2dec (parvalue);
	else if (isnum (parvalue))
	    rad0 = 3600.0 * atof (parvalue);
	else {
	    sprintf (voerror, "Search radius %s (degrees) is not a number", parvalue);
	    return (-1);
	    }
	votab = 1;
	degout0 = 1;
	}

    /* Search center right ascension */
    else if (!strcasecmp (parname,"ra")) {
	if (!isnum (parvalue) && !strchr (parvalue,':')) {
	    sprintf (voerror, "Right ascension %s is not a number", parvalue);
	    return (-1);
	    }
	ra0 = str2ra (parvalue);
	}

    /* Search center declination */
    else if (!strcasecmp (parname,"dec")) {
	if (!isnum (parvalue) && !strchr (parvalue,':')) {
	    sprintf (voerror, "Declination %s is not a number", parvalue);
	    return (-1);
	    }
	dec0 = str2dec (parvalue);
	}

    /* Search center coordinate system */
    else if (!strncasecmp (parname,"sys",3)) {
	syscoor = wcscsys (parvalue);
	eqcoor = wcsceq (parvalue);
	}

    /* Output coordinate system */
    else if (!strcasecmp (parname, "outsys")) {

	/* B1950 (FK4) coordinates */
	if (!strcasecmp (parvalue, "B1950") ||
	    !strcasecmp (parvalue, "FK4")) {
	    sysout0 = WCS_B1950;
	    eqout = 1950.0;
    	    }

	/* J2000 (FK5) coordinates */
	else if (!strcasecmp (parvalue, "J2000") ||
	    !strcasecmp (parvalue, "FK5")) {
	    sysout0 = WCS_J2000;
	    eqout = 2000.0;
    	    }

	/* Galactic coordinates */
	else if (!strncasecmp (parvalue, "GAL", 3))
	    sysout0 = WCS_GALACTIC;

	/* Ecliptic coordinates */
	else if (!strncasecmp (parvalue, "ECL", 3))
	    sysout0 = WCS_ECLIPTIC;
	}

    /* Set reference catalog */
    else if (!strcasecmp (parname, "catalog")) {
	lcat = strlen (parvalue) + 2;
	refcatn = (char *) malloc (lcat);
	strcpy (refcatn, parvalue);
	refcatname[ncat] = refcatn;
	ncat = ncat + 1;
	}

    /* Set output coordinate epoch */
    else if (!strcasecmp (parname, "epoch"))
	epoch0 = fd2ep (parvalue);

    /* Output equinox in years */
    else if (!strcasecmp (parname, "equinox"))
    	eqout = fd2ep (parvalue);

    /* Output in degrees instead of sexagesimal */
    else if (!strcasecmp (parname, "cformat")) {
	if (!strncasecmp (parvalue, "deg", 3))
	    degout0 = 1;
	else if (!strncasecmp (parvalue, "rad", 3))
	    degout0 = 2;
	else
	    degout0 = 0;
	}

    /* Number of decimal places in output positions */
    else if (!strcasecmp (parname, "ndec")) {
	if (isnum (parvalue)) {
	    if (degout0) {
		nddeg = atoi (parvalue);
		if (nddeg < 0 || nddeg > 10)
		    nddeg = 7;
		}
	    else {
		ndra = atoi (parvalue);
		if (ndra < 0 || ndra > 10)
		    ndra = 3;
		nddec = ndra - 1;
		}
	    }
	}

    /* Minimum proper motion quality for USNO-B1.0 catalog */
    else if (!strcasecmp (parname, "minpmq")) {
	if (isnum (parvalue)) {
	    minid = atoi (parvalue);
	    setminpmqual (minid);
	    }
	}

    /* Format for epoch of catalog source position */
    else if (!strcasecmp (parname, "dateform")) {
	if (parvalue[0] == 'j')
	    setdateform (EP_JD);
	else if (parvalue[0] == 'm')
	    setdateform (EP_MJD);
	else if (parvalue[0] == 'f')
	    setdateform (EP_FD);
	else if (parvalue[0] == 'i')
	    setdateform (EP_ISO);
	else
	    setdateform (EP_EP);
	}

    /* Minimum number of plate ID's for USNO-B1.0 catalog */
    else if (!strcasecmp (parname, "minid")) {
	if (isnum (parvalue)) {
	    minid = atoi (parvalue);
	    setminid (minid);
	    }
	}

    /* Output in VOTable XML instead of tab-separated table */
    else if (!strcasecmp (parname, "format")) {
	if (!strncasecmp (parvalue, "vot", 3)) {
	    votab = 1;
	    degout0 = 1;
	    }
	else
	    votab = 0;
	}

    /* Print center and closest star on one line */
    else if (!strcasecmp (parname, "oneline")) {
	if (parvalue[0] == 'y' || parvalue[0] == 'Y') {
	    oneline++;
	    catsort = SORT_DIST;
	    closest++;
	    nstars = 1;
	    }
	}

    /* Magnitude limit */
    else if (!strncasecmp (parname,"mag",3)) {
	maglim2 = atof (parvalue);
	if (MAGLIM1 == MAGLIM2)
	    maglim1 = -2.0;
	}
    else if (!strncasecmp (parname,"max",3))
	maglim2 = atof (parvalue);
    else if (!strncasecmp (parname,"min",3))
	maglim1 = atof (parvalue);

    /* Number of brightest stars to read */
    else if (!strncasecmp (parname,"nstar",5))
	nstars = atoi (parvalue);

    /* Object name */
    else if (!strcmp (parname, "object") || !strcmp (parname, "OBJECT")) {
	lcat = strlen (parvalue) + 2;
	objname = (char *) malloc (lcat);
	strcpy (objname, parvalue);
	}

    /* Magnitude by which to sort */
    else if (!strcasecmp (parname, "sortmag")) {
	if (isnum (parvalue+1))
	    sortmag = atoi (parvalue);
	}

    /* Output sorting */
    else if (!strcasecmp (parname, "sort")) {

	/* Sort by distance from center */
	if (!strncasecmp (parvalue,"di",2))
	    catsort = SORT_DIST;

	/* Sort by RA */
	else if (!strncasecmp (parvalue,"r",1))
	    catsort = SORT_RA;

	/* Sort by Dec */
	else if (!strncasecmp (parvalue,"de",2))
		catsort = SORT_DEC;

	/* Sort by ID */
	else if (!strncasecmp (parvalue,"id",2))
		catsort = SORT_ID;

	/* Sort by magnitude */
	else if (!strncasecmp (parvalue,"m",1)) {
	    catsort = SORT_MAG;
	    if (strlen (parvalue) > 1) {
		if (isnum (parvalue+1))
		    sortmag = atoi (parvalue+1);
		}
	    }

	/* No sort */
	else if (!strncasecmp (parvalue,"n",1))
	    catsort = SORT_NONE;
	else
	    catsort = SORT_NONE;
	}

    /* Search box half-width in RA */
    else if (!strcasecmp (parname,"dra")) {
	if (strchr (parvalue,':'))
	    dra0 = 3600.0 * str2ra (parvalue);
	else
	    dra0 = atof (parvalue);
	if (ddec0 <= 0.0)
	    ddec0 = dra0;
	}

    /* Search box half-height in Dec */
    else if (!strcasecmp (parname,"ddec")) {
	if (strchr (parvalue,':'))
	    ddec0 = 3600.0 * str2dec (parvalue);
	else
	    ddec0 = atof (parvalue);
	if (dra0 <= 0.0)
	    dra0 = ddec0;
	}

    /* Guide Star object class */
    else if (!strncasecmp (parname,"cla",3)) {
	classd = (int) atof (parvalue);
	setgsclass (classd);
	}

    /* Catalog to be searched */
    else if (!strncasecmp (parname,"cat",3)) {
	lcat = strlen (parvalue) + 2;
	refcatn = (char *) malloc (lcat);
	strcpy (refcatn, parvalue);
	refcatname[ncat] = refcatn;
	ncat = ncat + 1;
	}
    else {
	*parequal = '=';
	return (1);
	}
    *parequal = '=';
    return (0);
}


static void
PrintWebHelp ()

{
    FILE *out;	/* Output, stderr for command line, stdout for web */
    if (http) {
	out = stdout;
	fprintf (out, "Content-type: text/plain\n\n");
	}
    else
	out = stderr;
    fprintf (out, "Web catalog searching using WCSTools %s %s\n", progname, RevMsg);
    fprintf (out, "Results will be returned as a tab-separated Starbase table\n");
    fprintf (out, "Enter a sequence of keyword=value, separated by &, in one line\n");
    fprintf (out, "Help at http://tdc-www.harvard.edu/software/wcstools/scat/\n");
    fprintf (out, "\n");
    fprintf (out, "keyword  value description\n");
    fprintf (out, "-------  ------------------------------------------------\n");
    fprintf (out, "catalog  gsc (HST GSC),  ua2 (USNO-A2.0),  gsc2 (GSC II),\n");
    fprintf (out, "         gsca (GSC-ACT), ty2 (Tycho-2),    tmc (2MASS PSC),\n");
    fprintf (out, "         ub1 (USNO-B1.0), ucac2 (UCAC2), ppm, sao, etc.\n");
    fprintf (out, "ra       right ascension in degrees or hh:mm:ss.sss\n");
    fprintf (out, "dec      declination in degrees or [+/-]dd:mm:ss.sss\n");
    fprintf (out, "sys      coordinate system (B1950, J2000, Ecliptic, Galactic\n");
    fprintf (out, "outsys   output coordinate system if not same as above\n");
    fprintf (out, "nstar    maximum number of stars to be returned\n");
    fprintf (out, "rad      search radius in arcseconds or dd:mm:ss.sss\n");
    fprintf (out, "         (negate for square box)\n");
    fprintf (out, "inrad    inner annulus radius in arcseconds or dd:mm:ss.sss\n");
    fprintf (out, "sr       search radius in degrees or dd:mm:ss.sss\n");
    fprintf (out, "         (negate for square box)\n");
    fprintf (out, "dra      search halfwidth in RA arcseconds or dd:mm:ss.sss\n");
    fprintf (out, "ddec     search halfheight in Dec arcseconds or dd:mm:ss.sss\n");
    fprintf (out, "sort     dist   distance from search center\n");
    fprintf (out, "         ra     right ascension\n");
    fprintf (out, "         dec    declination\n");
    fprintf (out, "         mag    magnitude (brightest first)\n");
    fprintf (out, "         none   no sort\n");
    fprintf (out, "epoch    output epoch in fractional years\n");
    fprintf (out, "equinox  output equinox in fractional years\n");
    fprintf (out, "cformat  col    output coordinates hh:mm:ss dd:mm:ss\n");
    fprintf (out, "         deg    output coordinates in degrees\n");
    fprintf (out, "         rad    output coordinates in radians\n");
    fprintf (out, "format   tab    tab-separated table returned\n");
    fprintf (out, "         vot    VOTable XML returned\n");
    fprintf (out, "min      minimum magnitude\n");
    fprintf (out, "max      maximum magnitude\n");
    fprintf (out, "num      range of catalog numbers (n-nxn or n,n,n)\n");
    fprintf (out, "\n");
    fprintf (out, "help     List scat web parameters (this list)\n");
    fprintf (out, "version  Return scat version\n");
    fprintf (out, "band     Return list of HST GSC bandpass codes\n");
    fprintf (out, "class    Return list of HST GSC object class codes\n");
    fprintf (out, "comhelp  Return list of scat command line arguments\n");
    exit (0);
}

static void
PrintGSCBand ()

{
    FILE *out;	/* Output, stderr for command line, stdout for web */
    if (http) {
	out = stdout;
	fprintf (out, "Content-type: text/plain\n\n");
	}
    else
	out = stderr;
    fprintf (out, "HST Guide Star Catalog Bandpass Codes\n");
    fprintf (out, "Code Bandpass Emulsion/Filter Notes\n");
    fprintf (out, " 0      J     IIIaJ+GG395     SERC-J/EJ\n");
    fprintf (out, " 1      V     IIaD+W12        Pal Quick-V\n");
    fprintf (out, " 3      B     -               Johnson\n");
    fprintf (out, " 4      V     -               Johnson\n");
    fprintf (out, " 5      R     IIIaF+RG630     -\n");
    fprintf (out, " 6      V495  IIaD+GG495      Pal QV/AAO XV\n");
    fprintf (out, " 7      O     103aO+no filt   POSS-I Blue\n");
    fprintf (out, " 8      E     103aE+redplex   POSS-I Red\n");
    fprintf (out, " 9      R     IIIaF+RG630     -\n");
    fprintf (out, "10      -     IIaD+GG495+yel  GPO Astrograph\n");
    fprintf (out, "11      -     103aO+blue      Black Birch Astrograph\n");
    fprintf (out, "12      -     103aO+blue      Black Birch Astrograph (GSC cal)\n");
    fprintf (out, "13      -     103aG+GG495+yel Black Birch Astrograph\n");
    fprintf (out, "14      -     103aG+GG495+yel Black Birch Astrograph (GSC cal)\n");
    fprintf (out, "16      J     IIIaJ+GG495     -\n");
    fprintf (out, "18      V     IIIaJ+GG385     POSS-II Blue\n");
    fprintf (out, "19      U     -               Johnson\n");
    fprintf (out, "20      R     -               Johnson\n");
    fprintf (out, "21      I     -               Johnson\n");
    fprintf (out, "22      U     -               Cape\n");
    fprintf (out, "23      R     -               Kron\n");
    fprintf (out, "24      I     -               Kron\n");
    exit (0);
}

static void
PrintGSClass ()

{
    FILE *out;	/* Output, stderr for command line, stdout for web */
    if (http) {
	out = stdout;
	fprintf (out, "Content-type: text/plain\n\n");
	}
    else
	out = stderr;
    fprintf (out, "HST Guide Star Catalog Object Classes\n");
    fprintf (out, "0: Stellar\n");
    fprintf (out, "3: Non-Stellar\n");
    fprintf (out, "5: Not really an object\n");
    exit (0);
}

/* Oct 18 1996	New program based on imtab
 * Nov 13 1996	Set maximum nstar from command line if greater than default
 * Nov 14 1996	Set limits from subroutine
 * Nov 19 1996	Fix usage
 * Dec 12 1996	Allow bright as well as faint magnitude limit
 * Dec 12 1996	Fix header for UAC
 * Dec 12 1996	Fix header for UAC magnitudes
 * Dec 13 1996	Write plate into header if selected
 * Dec 18 1996	Allow WCS sky coordinate format as input argument for center
 * Dec 18 1996	Add option to print entries for specified catalog numbers
 * Dec 30 1996	Clean up closest star message
 * Dec 30 1996	Print message instead of heading if no stars are found
 *
 * Jan 10 1997	Fix bug in RASort Stars which did not sort magnitudes
 * Mar 12 1997	Add USNO SA-1.0 catalog as USAC
 * Apr 25 1997	Fix bug in uacread
 * May 29 1997	Add option to add keyword to tab table output
 * Nov 12 1997	Fix DEC in header to print Dec string instead of RA string
 * Nov 17 1997	Initialize both magnitude limits
 * Dec  8 1997	Set up program to be called by various names
 * Dec 12 1997	Fix center coordinate printing in heading
 *
 * Apr 10 1998	Fix bug search USNO A-1.0 catalog created by last revision
 * Apr 10 1998	Set search radius only if argument negative
 * Apr 14 1998	Version 2.2: to match other software
 * Apr 23 1998	Add output in galactic or ecliptic coordinates; use wcscon()
 * Apr 28 1998	Change coordinate system flags to WCS_*
 * Jun  2 1998	Fix bug in tabread()
 * Jun 24 1998	Add string lengths to ra2str() and dec2str() calls
 * Jun 30 1998	Fix declaration of GetArea()
 * Jul  1 1998	Allow specification of center different from output system
 * Jul  9 1998	Adjust all report headings
 * Jul 30 1998	Realign heading and fix help
 * Aug  6 1998	Do not include fitshead.h; it is in wcs.h
 * Sep 10 1998	Add SAOTDC binary format catalogs
 * Sep 15 1998	Adjust output format for binary format catalogs
 * Sep 16 1998	Fix bug creating output filename
 * Sep 21 1998	Do not print distance to search center if not searching
 * Sep 21 1998	Print epoch if not that of equinox
 * Sep 24 1998	Increase search radius for closest star
 * Sep 24 1998	Add second magnitude for Tycho Catalogue
 * Oct 15 1998	Add ability to read TDC ASCII catalog files
 * Oct 16 1998	Add ability to read any TDC binary catalog file
 * Oct 21 1998	Add object name to TDC binary catalogs
 * Oct 21 1998	Use wcscat.h common
 * Oct 23 1998	Allow up to 10 catalogs to be searched at once
 * Oct 26 1998	Return object name in same operation as object position
 * Oct 27 1998	Fix RefCat() calls
 * Oct 29 1998	Add GSC class selection argument x; it should have been there
 * Oct 30 1998	Read object name if accessing catalogs by number, too
 * Nov 20 1998	Implement USNO A-2.0 and SA-2.0 catalogs; differentiate from A1
 * Nov 30 1998	Add version and help commands for consistency
 * Dec  8 1998	Add Hipparcos and ACT catalogs
 * Dec 14 1998	Fix format for UJC
 * Dec 21 1998	Fix format for BINCAT and TXTCAT format catalogs

 * Jan 20 1999	Add option to print search center as output
 * Jan 21 1999	Drop option of adding coordinates to -b -e -g -j
 * Jan 21 1999	Improve command parser to accept fractional degree centers
 * Jan 21 1999	Set output coordinate system for TDC ASCII and Binary catalogs
 * Jan 26 1999	Add option of range of star numbers
 * Feb  1 1999	Add switch to get sequence number unless . in number
 * Feb  2 1999	Vary number of decimal places according to input ASCII catalog
 * Feb  2 1999	Set output equinox, epoch, coorsys from catalog if not set
 * Feb 10 1999	Increase search radius for closest star in smaller catalogs
 * Feb 12 1999	Add ACT catalog from CDROM
 * Feb 18 1999	Make printing name in search centers optional
 * Apr 13 1999	Fix progname to drop / when full pathname
 * May 12 1999	Add option to search from a TDC ASCII catalog
 * May 19 1999	Add option to print search center and closest star on 1 line
 * May 19 1999	Format catalog number using CatNum()
 * May 21 1999	Allow option of setting epoch from search catalog
 * May 28 1999	Allow search from starbase table catalog as well as ASCII
 * May 28 1999	Write epoch of coordinates into tab header if not = equinox
 * Jun  4 1999	Improve labelling for search from file
 * Jun  4 1999	Use calloc() instead of malloc() for proper initialization
 * Jun  4 1999	Allow rectangular in addition to square and circular searches
 * Jun  7 1999	Allow radius input in sexagesimal
 * Jun 30 1999	Use isrange() to check for a range of source numbers
 * Jul  1 1999	Allow any legal FITS date format for epoch
 * Aug 16 1999	Be more careful checking for 2nd -r argument
 * Aug 20 1999	Change u from USNO plate to print X Y 
 * Aug 23 1999	Fix closest star search by setting search radius not box
 * Aug 25 1999	Add the Bright Star Catalog
 * Sep  8 1999	Clean up code
 * Sep 10 1999	Do all searches through catread() and catrnum()
 * Sep 16 1999	Fix galactic coordinate header
 * Sep 16 1999	Add distsort argument to catread() call
 * Sep 21 1999	Change description of search area from square to region
 * Oct 15 1999	Fix calls to catopen() and catclose()
 * Oct 22 1999	Drop unused variables after lint
 * Oct 22 1999	Change catread() to ctgread() to avoid system conflict
 * Oct 22 1999	Increase default r for closest search to 1800 arcsec
 * Nov 19 1999	Use CatNum when concocting names from numbers
 * Nov 29 1999	Include fitsfile.h for date conversion
 *
 * Jan 11 2000	Get nndec for Starbase catalogs
 * Feb 10 2000	Use reference catalog coordinate system by default
 * Feb 15 2000	Use MAXCAT from lwcs.h instead of MAXREF
 * Feb 28 2000	Drop Peak column if not set in TAB catalogs
 * Mar 10 2000	Move catalog selection from executable name to subroutine
 * Mar 14 2000	Lowercase all header keywords
 * Mar 14 2000	Add proper motion, if present, to Starbase output
 * Mar 27 2000	Drop unused subroutine setradius() declaration
 * Mar 28 2000	Clean up output for catalog IDs and GSC classes
 * Apr  3 2000	Allocate search return buffers only once per execution
 * May 26 2000	Add Tycho 2 catalog
 * May 26 2000	Always use CatNumLen() to get ID number field size
 * May 31 2000	Do not sort if only one star
 * Jun  2 2000	Set to NULL when freeing
 * Jun 23 2000	Add degree output for one-line matches (-l)
 * Jun 26 2000	Add XY output
 * Jul 13 2000	Add star catalog data structure to ctgread() argument list
 * Jul 13 2000	Precess search catalog sources to specified system and epoch
 * Jul 25 2000	Pass address of star catalog data structure address
 * Sep  1 2000	Call CatNum with -nnfld to print leading zeroes on search ctrs
 * Sep 21 2000	Print spectral type instead of plate number of USNO A catalogs
 * Sep 25 2000	Print spectral type and second magnitude if present in one-line
 * Oct 26 2000	Print proper motion in msec/year and masec/year unless degout
 * Nov  3 2000	Print proper motion in sec/century and arcsec/century
 * Nov  9 2000	Set output equinox and epoch from -j and -b flags
 * Nov 17 2000	Add keyword=value command line parameter decoding
 * Nov 22 2000	Pass starcat to ctgrnum()
 * Nov 28 2000	Add CGI query decoding
 * Dec 15 2000	Add code to deal with coordinates without systems
 * Dec 18 2000	Change two-argument box size separator from space to comma
 * Dec 18 2000	Always allocate proper motion arrays
 * Dec 29 2000	Set debug flag if two v's encountered as command line arguments
 *
 * Jan  2 2001	Fix proper motion test; fix box size in heading
 * Mar  1 2001	If printing x and y, add .match extension to output file
 * Mar  1 2001	Print output file as tab/Starbase only if -t
 * Mar  1 2001	Add -z option to append to output file
 * Mar 23 2001	If catalog system, equinox, and epoch not set, set to J2000
 * Mar 23 2001	Set epoch and equinox to match search coords if not set
 * Apr 24 2001	Add HST GSC band output
 * May 22 2001	Rewrite sort options to include declination and no sort
 * May 23 2001	Add GSC-ACT catalog (updated HST GSC)
 * May 30 2001	Add 2MASS Point Source Catalog
 * Jun  5 2001	Read tab table search catalog one line at a time
 * Jun  6 2001	Set output equinox like output epoch; fix one-line tab bugs
 * Jun  7 2001	Add arguments to RefCat()
 * Jun 13 2001	Make -r argument radius if positive box half-width if negative
 * Jun 13 2001	Print id and magnitude column headings for tab table searches
 * Jun 13 2001	Print headings only of first of list of star numbers
 * Jun 14 2001	Initialize gobj elements to NULL
 * Jun 18 2001	Fix printing of long spectral type from search catalog
 * Jun 22 2001	Add GSC2 and modify Hipparcos output
 * Jun 25 2001	Add fifth digit to IRAS 3rd and 4th magnitudes
 * Jun 26 2001	If nstars < 0, print from catalog subroutine
 * Jul  3 2001	Add help to CGI response
 * Jul  5 2001	Add hundredths digit to magnitude limit
 * Jul 18 2001	Fix CGI help response; add web help to command line
 * Aug  8 2001	Add support for radial velocities
 * Aug 24 2001	Add support for radial velocities in one-line reports
 * Sep 12 2001	Use 2-D array of magnitudes, rather than multiple vectors
 * Sep 13 2001	Add sort magnitude option as -m* (no space)
 * Sep 14 2001	Add sort magnitude options to web interface
 * Sep 17 2001	Add limits for 2MASS Point Source Catalog
 * Sep 19 2001	Fix bug dealing with IRAS limited fluxes in one-line output
 * Sep 20 2001	Print appropriate limiting magnitude and name
 * Oct 19 2001	Allow FITS or fractional year for epoch (-y) or equinox (-q)
 * Oct 24 2001	Convert RA half-width to sky arcseconds by dividing by cos(dec)
 * Oct 24 2001	Improve error reporting
 * Nov 13 2001	Add option to set object name field length using -i
 * Nov 14 2001	Print 7 decimal places if degrees printed to avoid losing precision
 * Nov 20 2001	Check all allocated memory before freeing it
 * Nov 20 2001	Change temp to newranges when reallocating ranges
 * Dec 11 2001	Ignore CGI query string if there are command line arguments without error
 *
 * Feb  1 2002	Print spectral type for TDC format catalogs, if present
 * Mar  2 2002	Add web query sr= option for search radius in degrees
 * Apr  3 2002	Add magnitude number to sort options
 * Apr  8 2002	Add magnitude number to magnitude limit setting
 * Apr 10 2002	Fix magnitude number bug and add magnitude letter
 * Aug  2 2002	Fix bug printing UJC magnitudes
 * Aug  5 2002	Add proper motion of searched catalog in one-line output
 * Aug  5 2002	Handle magnitudes and proper motions for search catalogs
 * Aug  6 2002	Rewrite to handle vectors of magnitudes
 * Aug  6 2002	Fix proper motion units to sec|arcsec/year
 * Sep 18 2002	Add VOTable output option
 * Sep 27 2002	Fix bug which printed only one line of results for spaced output
 * Sep 27 2002	Fix bug which mischecked proper motions
 * Oct 18 2002	Never print RevMsg when outputting VOTable format
 * Oct 26 2002	Clean up output
 * Oct 30 2002	Print out coordinate epoch
 * Nov  4 2002	Add entry-by-entry epoch filter
 * Dec  6 2002	Fix epoch output format bug
 *
 * Jan 26 2003	Add USNO-B1.0 Catalog
 * Jan 28 2003	Assume dra on command line in RA arcseconds, not sky
 * Jan 28 2003	Add option to set number of decimal places over web
 * Jan 29 2003	Add options to set min ID and min PM qual for USNO-B1.0
 * Jan 29 2003	Add header lines if USNO-B1.0 ID or PM quality limits
 * Jan 29 2003	Print magnitude headers from Starbase files
 * Jan 30 2003	Put proper motion back into one line output
 * Feb  4 2003	Fix bug freeing magnitude vectors prior to reallocating them
 * Feb 20 2003	Fix typos and drop unused variables after lint
 * Mar  3 2003	Fix proper motion spacing bug in one line tab table output
 * Mar 10 2003	Always make search box in angle on sky
 * Mar 10 2003	Add letter or number of magnitude if sorted
 * Mar 25 2003	Output coordinate system can be different from search system
 * Mar 25 2003	Fix headings print search, catalog, and output coordinates
 * Apr 13 2003	Set revision message for subroutines using setrevmsg()
 * Apr 24 2003	Add UCAC1 catalog
 * May 27 2003	Add TMIDR2 to TMPSC formats
 * May 28 2003	Print radecsys in tab table header
 * May 30 2003	Add UCAC2 catalog
 * Jun  2 2003	Print proper motions as mas/year to make them more useful
 * June 4 2003	Fix bug so filed input numbers are matched to catalog numbers
 * June 4 2003	Fix bug so -rr command works
 * Jun 11 2003	Always print proper motion as f6.1
 * Jun 13 2003	Fix comparison of character to pointer
 * Aug  7 2003	Allow sorting of stars read by number; NOSORT -> SORT_NONE
 * Aug 11 2003	Fix bug so one-line search output gets mag heads correct
 * Aug 19 2003	Fix help listing to note that magnitude limits are comma-separated
 * Aug 20 2003	Print 4 decimal places in equinox and epoch in heading
 * Aug 22 2003	Add -r second radius giving search annulus
 * Sep 23 2003	Add -s e to merge all catalog objects less than rad0 apart
 * Nov 22 2003	Add class to GSC II output
 * Dec  3 2003	Fix bugs in single line tab-separated output headers
 * Dec  3 2003	Add support for USNO YB6 Catalog
 * Dec 10 2003	Fix limiting magnitudes for search catalogs
 *
 * Jan  5 2004	Add support for SDSS Photmetric Catalog
 * Jan 12 2004	Add support for 2MASS Extended Source Catalog
 * Jan 22 2004	Call setlimdeg() to optionally print limits in degrees
 * Jan 23 2004	Print radius in degrees in verbose mode if degout is set
 * Feb 23 2004	Fix bug which printed too many ---'s for 2MASS PSC in tab mode
 * Mar  4 2004	Fix bug in setting merge flag
 * Mar 16 2004	Use star count returned from catalog merging subroutine
 * Mar 17 2004	RA-sort in MergeStars(); add logging
 * May 12 2004	Exit with error message if no catalog name is given
 * Aug 30 2004	Fix two subroutine declarations
 * Aug 30 2004	Drop out if RefCat cannot find catalog
 * Sep 10 2004	Do not print anything if no stars found and not in verbose
 *		or tab table output mode
 * Sep 17 2004	Use -h to turn off header if no stars found in tab table mode
 * Oct 20 2004	Fix -rr and -r two argument definitions to match reality
 * Nov 17 2004	Fix main output loop bug and print type name in one line output
 * Nov 19 2004	Add star/galaxy code to USNO-B1.0 output
 *
 * Apr 19 2005	Fix minor format bug when printing tabbed epoch
 * May 12 2005	Add 2MASS ID decoding
 * Jul 27 2005	Add dateform=[i,f,m,e] to format output dates using DateString()
 * Aug  1 2005	Always set epoch; test only if a limit is present
 * Aug  3 2005	If nstars<0, print catalog information as found, unsorted
 * Aug  5 2005	Add 2MASS and Tycho-2 catalogs with errors
 *
 * Jan  5 2006	Set output equinox correctly; match epoch to set equinox
 * Jan  6 2006	Print search center in input system as well as output system
 * Jan  6 2006	Precess search center if input and output equinox different
 * Mar 15 2006	Clean up VOTable code
 * Mar 17 2006	Add VOTable error reporting
 * Apr 13 2006	Add sort by ID number
 * Jun  6 2006	Add 2 spaces per magnitude header
 * Jun  8 2006	Print object name if no number present in one-line output
 * Jun 21 2006	Initialize uninitialized variables
 * Jun 27 2006	Print ___ if requested keyword in Starbase file not present
 * Sep 26 2006	Increase length of rastr and destr from 16 to 32
 * Sep 26 2006	Allow coordinates on command line to be any length
 * Nov  6 2006	Print SDSS number as character string; it is now 18 digits long
 *
 * Jan 10 2007	Declare RevMsg static, not const
 * Jan 10 2007	Drop extra argument to IDSortStars (last one)
 * Mar 13 2007	Print GSC2 ID from object name, not number
 * May 21 2007	Raise maximum object name length from 32 to 79
 * Jul  6 2007	Drop a magnitude if epoch present in search catalog
 * Jul  9 2007	Print 5 magnitudes for GSC 2.3, not 4; add B
 * Jul  9 2007	Fix bug so GSC2 or SDSS numbers print correctly in 1-line output
 * Jul 20 2007	Add SkyBot report format
 * Aug 24 2007	Add mrad for search radius in minutes; add number checking, too
 *
 * Apr  9 2009	Add "degrees" as CGI parameter
 * Aug 17 2009	Print only four magnitudes for GSC2: F,J,N,V
 * Sep 25 2009	Add one more format to non-tab GSC2 output magnitudes
 * Oct 22 2009	Add UCAC3 catalog headings
 * Nov  5 2009	Add UCAC3 errors and number of measurements
 *
 * Apr 30 2010	Set GSC2 magnitudes to 99.99 if > 90
 *
 * Apr 21 2011	Fix tabs in TMPSCE output
 *
 * Feb  1 2013	Add -p option to Usage()
 *
 * Feb 15 2013	Add UCAC4 catalog
 *
 * Jun 23 2016	Fix typos in format ($ for %) (via Ole Streicher)
 *
 * Jan 24 2017	Add data pathname to output line from text catalogs
 */