summaryrefslogtreecommitdiffstats
path: root/ast/frameset.c
blob: 550e985177a66ca0b6610f9678138b7db9692609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
/*
*class++
*  Name:
*     FrameSet

*  Purpose:
*     Set of inter-related coordinate systems.

*  Constructor Function:
c     astFrameSet
f     AST_FRAMESET

*  Description:
*     A FrameSet consists of a set of one or more Frames (which
*     describe coordinate systems), connected together by Mappings
*     (which describe how the coordinate systems are inter-related). A
*     FrameSet makes it possible to obtain a Mapping between any pair
*     of these Frames (i.e. to convert between any of the coordinate
*     systems which it describes).  The individual Frames are
*     identified within the FrameSet by an integer index, with Frames
*     being numbered consecutively from one as they are added to the
*     FrameSet.
*
*     Every FrameSet has a "base" Frame and a "current" Frame (which
*     are allowed to be the same). Any of the Frames may be nominated
*     to hold these positions, and the choice is determined by the
*     values of the FrameSet's Base and Current attributes, which hold
*     the indices of the relevant Frames.  By default, the first Frame
*     added to a FrameSet is its base Frame, and the last one added is
*     its current Frame.
*
*     The base Frame describes the "native" coordinate system of
*     whatever the FrameSet is used to calibrate (e.g. the pixel
*     coordinates of an image) and the current Frame describes the
*     "apparent" coordinate system in which it should be viewed
*     (e.g. displayed, etc.). Any further Frames represent a library
*     of alternative coordinate systems, which may be selected by
*     making them current.
*
*     When a FrameSet is used in a context that requires a Frame,
*     (e.g. obtaining its Title value, or number of axes), the current
*     Frame is used. A FrameSet may therefore be used in place of its
*     current Frame in most situations.
*
*     When a FrameSet is used in a context that requires a Mapping,
*     the Mapping used is the one between its base Frame and its
*     current Frame. Thus, a FrameSet may be used to convert "native"
*     coordinates into "apparent" ones, and vice versa. Like any
c     Mapping, a FrameSet may also be inverted (see astInvert), which
f     Mapping, a FrameSet may also be inverted (see AST_INVERT), which
*     has the effect of interchanging its base and current Frames and
*     hence of reversing the Mapping between them.
*
*     Regions may be added into a FrameSet (since a Region is a type of
*     Frame), either explicitly or as components within CmpFrames. In this
*     case the Mapping between a pair of Frames within a FrameSet will
*     include the effects of the clipping produced by any Regions included
*     in the path between the Frames.

*  Inheritance:
*     The FrameSet class inherits from the Frame class.

*  Attributes:
*     In addition to those attributes common to all Frames, every
*     FrameSet also has the following attributes:
*
*     - AllVariants: List of all variant mappings store with current Frame
*     - Base: FrameSet base Frame index
*     - Current: FrameSet current Frame index
*     - Nframe: Number of Frames in a FrameSet
*     - Variant: Name of variant mapping in use by current Frame

*     Every FrameSet also inherits any further attributes that belong
*     to its current Frame, regardless of that Frame's class. (For
*     example, the Equinox attribute, defined by the SkyFrame class, is
*     inherited by any FrameSet which has a SkyFrame as its current
*     Frame.) The set of attributes belonging to a FrameSet may therefore
*     change when a new current Frame is selected.

*  Functions:
c     In addition to those functions applicable to all Frames, the
c     following functions may also be applied to all FrameSets:
f     In addition to those routines applicable to all Frames, the
f     following routines may also be applied to all FrameSets:
*
c     - astAddFrame: Add a Frame to a FrameSet to define a new coordinate
c     system
c     - astAddVariant: Add a variant Mapping to the current Frame
c     - astGetFrame: Obtain a pointer to a specified Frame in a FrameSet
c     - astGetMapping: Obtain a Mapping between two Frames in a FrameSet
c     - astMirrorVariants: Make the current Frame mirror variant Mappings in another Frame
c     - astRemapFrame: Modify a Frame's relationship to the other Frames in a
c     FrameSet
c     - astRemoveFrame: Remove a Frame from a FrameSet
f     - AST_ADDFRAME: Add a Frame to a FrameSet to define a new coordinate
f     system
f     - AST_ADDVARIANT: Add a variant Mapping to the current Frame
f     - AST_GETFRAME: Obtain a pointer to a specified Frame in a FrameSet
f     - AST_GETMAPPING: Obtain a Mapping between two Frames in a FrameSet
f     - AST_MIRRORVARIANTS: Make the current Frame mirror variant Mappings in another Frame
f     - AST_REMAPFRAME: Modify a Frame's relationship to the other Frames in a
f     FrameSet
f     - AST_REMOVEFRAME: Remove a Frame from a FrameSet

*  Copyright:
*     Copyright (C) 1997-2006 Council for the Central Laboratory of the
*     Research Councils

*  Licence:
*     This program is free software: you can redistribute it and/or
*     modify it under the terms of the GNU Lesser General Public
*     License as published by the Free Software Foundation, either
*     version 3 of the License, or (at your option) any later
*     version.
*
*     This program is distributed in the hope that it will be useful,
*     but WITHOUT ANY WARRANTY; without even the implied warranty of
*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*     GNU Lesser General Public License for more details.
*
*     You should have received a copy of the GNU Lesser General
*     License along with this program.  If not, see
*     <http://www.gnu.org/licenses/>.

*  Authors:
*     RFWS: R.F. Warren-Smith (Starlink)

*  History:
*     16-FEB-1996 (RFWS):
*        Original version.
*     5-JUN-1996 (RFWS):
*        Tidied up, etc.
*     2-JUL-1996 (RFWS):
*        Fixed bug in astRemoveFrame which caused the base/current
*        Frame index to be wrong.
*     12-JUL-1996 (RFWS):
*        Over-ride the astReportPoints method to provide
*        Frame-specific formatting.
*     12-AUG-1996 (RFWS):
*        Upgraded to provide a public interface, plus improvements to
*        astAlign and the handling of nodes as Frames are
*        added/removed.
*     11-SEP-1996 (RFWS):
*        Added Gap.
*     25-SEP-1996 (RFWS):
*        Added I/O facilities.
*     30-MAY-1997 (RFWS):
*        Add special treatment for the ID attribute (which is not
*        derived from the current Frame).
*     10-JUN-1997 (RFWS):
*        Rationalised the astConvert implementation.
*     11-JUN-1997 (RFWS):
*        Added the FindFrame implementation.
*     27-JUN-1997 (RFWS):
*        Fixed bug which caused certain Mapping attributes to be
*        handled by the current Frame instead of by the member
*        functions defined by this class.
*     3-JUL-1997 (RFWS):
*        Fixed bug: failing to extend the invert array in
*        astRemapFrame.
*     10-JUL-1997 (RFWS):
*        Over-ride the astSimplify method.
*     14-NOV-1997 (RFWS):
*        Fixed error in loop implementing search over domains in
*        FindFrame.
*     20-NOV-1997 (RFWS):
*        Fixed bug in default Base and Current attribute values when a
*        FrameSet has been inverted.
*     20-NOV-1997 (RFWS):
*        Modified astConvert to use the current Frame of the "to"
*        FrameSet as the destination coordinate system (instead of the
*        base Frame) and to modify its Base attribute instead of its
*        Current attribute.
*     22-DEC-1997 (RFWS):
*        Further modified astConvert to convert from the Current Frame
*        of the "from" FrameSet and to modify its Base
*        attribute. Frame search order also reversed if the Invert
*        attribute is non-zero for either FrameSet.
*     19-JAN-1998 (RFWS):
*        Installed the TidyNodes function.
*     20-JAN-1998 (RFWS):
*        Implemented preservation of FrameSet integrity when attribute
*        values associated with the current Frame are modified.
*     24-FEB-1998 (RFWS):
*        Added the ForceCopy function to allow integrity to be preserved
*        when there are multiple references to the same Frame.
*     25-FEB-1998 (RFWS):
*        Over-ride the astUnformat method.
*     24-MAR-1998 (RFWS):
*        Fixed unterminated comment causing problems in CombineMaps.
*     6-APR-1998 (RFWS):
*        Fixed another unterminated comment in CombineMaps.
*     27-MAY-1998 (RFWS):
*        Fixed bug: failure to record new invert flag value after
*        simplifying a CmpMap in TidyNodes.
*     17-DEC-2002 (DSB):
*        Override accessors for Frame attributes Top, Bottom, Epoch,
*        System, AlignSystem and ActiveUnit.
*     8-JAN-2003 (DSB):
*        Changed private InitVtab method to protected astInitFrameSetVtab
*        method.
*     24-JAN-2004 (DSB):
*        o  Override the astFields method.
*        o  Add argument "fmt" to Abbrev.
*     23-MAR-2004 (DSB):
*        Modified astGetMapping and Span to include the clipping effect of
*        any Regions in the path between the two supplied Frames.
*     24-AUG-2004 (DSB):
*        - Override various methods inherited from Frame (astAngle,
*	astAxAngle, astAxDistance, astAxOffset, astCheckPerm, astOffset2,
*	astResolve, astSystemCode, astSystemString, astValidateSystem,
*	astValidateAxisSelection). These should have been overridden a
*       long time ago!
*     8-SEP-2004 (DSB):
*       Override astResolvePoints.
*     12-MAY-2005 (DSB):
*        Override astNormBox method.
*     12-AUG-2005 (DSB):
*        Override ObsLat and ObsLon accessor methods.
*     14-FEB-2006 (DSB):
*        Override astGetObjSize.
*     15-MAY-2006 (DSB):
*        Override astEqual.
*     30-JUN-2006 (DSB):
*        Allow astAbbrev to have a null "str1" value.
*     22-JUN-2007 (DSB):
*        Modify VSet to avoid using the args va_list twice since the
*        first use (by the parent VSet function) invalidates the va_list
*        causing a segvio to be generated by the second use (when
*        formatting an error message).
*     11-JAN-2008 (DSB):
*        Override the astRate method.
*     17-NOV-2008 (DSB):
*        Correct parent class in invocation of astMAKE_ISA.
*     14-JAN-2009 (DSB):
*        Override the astIntersect method.
*     18-JUN-2009 (DSB):
*        Override ObsAlt accessor methods.
*     30-OCT-2009 (DSB):
*        Make the Ident attribute relate to the FrameSet, not the current
*        Frame.
*     22-MAR-2011 (DSB):
*        Override astFrameGrid method.
*     29-APR-2011 (DSB):
*        Prevent astFindFrame from matching a subclass template against a
*        superclass target.
*     2-SEP-2011 (DSB):
*        Fix FrameSet implememntation of astEqual (mapping comparison
*        tests were logically inverted).
*     3-OCT-2012 (DSB):
*        Fix bug in AppendAxes that could cause internal Mappings to
*        be inverted unintentionally when astAddFrame is called with
*        iframe=AST__ALLFRAMES.
*     29-APR-2013 (DSB):
*        Added attributes AllVariants and Variant. Also added methods
*        astAddVariant and astMirrorVariants.
*     25-SEP-2014 (DSB):
*        Allow Base and Current attributes to be specified by giving a
*        Domain name.
*     17-APR-2015 (DSB):
*        Added Centre.
*     28-APR-2015 (DSB):
*        astAdFrame now takes deep copies of the supplied mapping and
*        frame, rather than just cloning their pointers. So the modified
*        FrameSet is now independent of the supplied Mapping and Frame
*        objects.
*     26-OCT-2016 (DSB):
*        Override the AxNorm method.
*class--
*/

/* Module Macros. */
/* ============== */
/* Set the name of the class we are implementing. This indicates to
   the header files that define class interfaces that they should make
   "protected" symbols available. */
#define astCLASS FrameSet

#define GETALLVARIANTS_BUFF_LEN 200

/*
*  Name:
*     MAKE_CLEAR

*  Purpose:
*     Define a function to clear an attribute value for a FrameSet.

*  Type:
*     Private macro.

*  Synopsis:
*     #include "frameset.h"
*     MAKE_CLEAR(attribute)

*  Class Membership:
*     Defined by the FrameSet class.

*  Description:
*     This macro expands to an implementation of a private member function
*     of the form:
*
*        static void Clear<Attribute>( AstFrame *this )
*
*     that clears the value of a specified attribute for the current Frame
*     of a FrameSet (this). This function is intended to over-ride the
*     astClear<Attribute> method inherited from the Frame class.

*  Parameters:
*     attribute
*        Name of the attribute, as it appears in the function name.
*/

/* Define the macro. */
#define MAKE_CLEAR(attribute) \
static void Clear##attribute( AstFrame *this_frame, int *status ) { \
\
/* Local Variables: */ \
   AstFrame *fr;                 /* Pointer to current Frame */ \
   AstFrameSet *this;            /* Pointer to the FrameSet structure */ \
\
/* Check the global error status. */ \
   if ( !astOK ) return; \
\
/* Obtain a pointer to the FrameSet structure. */ \
   this = (AstFrameSet *) this_frame; \
\
/* Obtain a pointer to the current Frame and invoke its astClear<Attribute> \
   method. Annul the Frame pointer afterwards. */ \
   fr = astGetFrame( this, AST__CURRENT ); \
   astClear##attribute( fr ); \
   fr = astAnnul( fr ); \
}

/*
*  Name:
*     MAKE_CLEAR_AXIS

*  Purpose:
*     Define a function to clear an attribute value for a FrameSet axis.

*  Type:
*     Private macro.

*  Synopsis:
*     #include "frameset.h"
*     MAKE_CLEAR_AXIS(attribute)

*  Class Membership:
*     Defined by the FrameSet class.

*  Description:
*     This macro expands to an implementation of a private member function
*     of the form:
*
*        static void Clear<Attribute>( AstFrame *this, int axis )
*
*     that clears the value of a specified attribute for an axis of
*     the current Frame of a FrameSet (this). This function is
*     intended to over-ride the astClear<Attribute> method inherited
*     from the Frame class.

*  Parameters:
*     attribute
*        Name of the attribute, as it appears in the function name.
*/

/* Define the macro. */
#define MAKE_CLEAR_AXIS(attribute) \
static void Clear##attribute( AstFrame *this_frame, int axis, int *status ) { \
\
/* Local Variables: */ \
   AstFrame *fr;                 /* Pointer to current Frame */ \
   AstFrameSet *this;            /* Pointer to the FrameSet structure */ \
\
/* Check the global error status. */ \
   if ( !astOK ) return; \
\
/* Obtain a pointer to the FrameSet structure. */ \
   this = (AstFrameSet *) this_frame; \
\
/* Validate the axis index supplied. */ \
   (void) astValidateAxis( this, axis, 1, "astClear" #attribute ); \
\
/* Obtain a pointer to the FrameSet's current Frame and invoke its \
   astClear<Attribute> method. Annul the Frame pointer afterwards. */ \
   fr = astGetFrame( this, AST__CURRENT ); \
   astClear##attribute( fr, axis ); \
   fr = astAnnul( fr ); \
}

/*
*  Name:
*     MAKE_GET

*  Purpose:
*     Define a function to get an attribute value for a FrameSet.

*  Type:
*     Private macro.

*  Synopsis:
*     #include "frameset.h"
*     MAKE_GET(attribute,type)

*  Class Membership:
*     Defined by the FrameSet class.

*  Description:
*     This macro expands to an implementation of a private member function
*     of the form:
*
*        static <type> Get<Attribute>( AstFrame *this )
*
*     that gets the value of a specified attribute for the current Frame
*     of a FrameSet (this). This function is intended to over-ride the
*     astGet<Attribute> method inherited from the Frame class.

*  Parameters:
*     attribute
*        Name of the attribute, as it appears in the function name.
*     type
*        The C type of the attribute.
*/

/* Define the macro. */
#define MAKE_GET(attribute,type) \
static type Get##attribute( AstFrame *this_frame, int *status ) { \
\
/* Local Variables: */ \
   AstFrame *fr;                 /* Pointer to current Frame */ \
   AstFrameSet *this;            /* Pointer to the FrameSet structure */ \
   type result;                  /* Value to return */ \
\
/* Check the global error status. */ \
   if ( !astOK ) return (type) 0; \
\
/* Obtain a pointer to the FrameSet structure. */ \
   this = (AstFrameSet *) this_frame; \
\
/* Obtain a pointer to the current Frame and invoke its \
   astGet<Attribute> method.  Annul the Frame pointer afterwards. */ \
   fr = astGetFrame( this, AST__CURRENT ); \
   result = astGet##attribute( fr ); \
   fr = astAnnul( fr ); \
\
/* If an error occurred, clear the result value. */ \
   if ( !astOK ) result = (type) 0; \
\
/* Return the result. */ \
   return result; \
}

/*
*  Name:
*     MAKE_GET_AXIS

*  Purpose:
*     Define a function to get an attribute value for a FrameSet axis.

*  Type:
*     Private macro.

*  Synopsis:
*     #include "frameset.h"
*     MAKE_GET_AXIS(attribute,type)

*  Class Membership:
*     Defined by the FrameSet class.

*  Description:
*     This macro expands to an implementation of a private member function
*     of the form:
*
*        static <type> Get<Attribute>( AstFrame *this, int axis )
*
*     that gets the value of a specified attribute for an axis of the
*     current Frame of a FrameSet (this). This function is intended to
*     over-ride the astGet<Attribute> method inherited from the Frame
*     class.

*  Parameters:
*     attribute
*        Name of the attribute, as it appears in the function name.
*     type
*        The C type of the attribute.
*/

/* Define the macro. */
#define MAKE_GET_AXIS(attribute,type) \
static type Get##attribute( AstFrame *this_frame, int axis, int *status ) { \
\
/* Local Variables: */ \
   AstFrame *fr;                 /* Pointer to current Frame */ \
   AstFrameSet *this;            /* Pointer to the FrameSet structure */ \
   type result;                  /* Value to return */ \
\
/* Check the global error status. */ \
   if ( !astOK ) return (type) 0; \
\
/* Obtain a pointer to the FrameSet structure. */ \
   this = (AstFrameSet *) this_frame; \
\
/* Validate the axis index supplied. */ \
   (void) astValidateAxis( this, axis, 1, "astGet" #attribute ); \
\
/* Obtain a pointer to the FrameSet's current Frame and invoke its \
   astGet<Attribute> method.  Annul the Frame pointer afterwards. */ \
   fr = astGetFrame( this, AST__CURRENT ); \
   result = astGet##attribute( fr, axis ); \
   fr = astAnnul( fr ); \
\
/* If an error occurred, clear the result value. */ \
   if ( !astOK ) result = (type) 0; \
\
/* Return the result. */ \
   return result; \
}

/*
*  Name:
*     MAKE_SET

*  Purpose:
*     Define a function to set an attribute value for a FrameSet.

*  Type:
*     Private macro.

*  Synopsis:
*     #include "frameset.h"
*     MAKE_SET(attribute,type)

*  Class Membership:
*     Defined by the FrameSet class.

*  Description:
*     This macro expands to an implementation of a private member function
*     of the form:
*
*        static void Set<Attribute>( AstFrame *this, <type> value )
*
*     that sets the value of a specified attribute for the current Frame
*     of a FrameSet (this). This function is intended to over-ride the
*     astSet<Attribute> method inherited from the Frame class.

*  Parameters:
*     attribute
*        Name of the attribute, as it appears in the function name.
*     type
*        The C type of the attribute.
*/

/* Define the macro. */
#define MAKE_SET(attribute,type) \
static void Set##attribute( AstFrame *this_frame, type value, int *status ) { \
\
/* Local Variables: */ \
   AstFrame *fr;                 /* Pointer to current Frame */ \
   AstFrameSet *this;            /* Pointer to the FrameSet structure */ \
\
/* Check the global error status. */ \
   if ( !astOK ) return; \
\
/* Obtain a pointer to the FrameSet structure. */ \
   this = (AstFrameSet *) this_frame; \
\
/* Obtain a pointer to the FrameSet's current Frame and invoke its \
   astSet<Attribute> method.  Annul the Frame pointer afterwards. */ \
   fr = astGetFrame( this, AST__CURRENT ); \
   astSet##attribute( fr, value ); \
   fr = astAnnul( fr ); \
}

/*
*  Name:
*     MAKE_SET_AXIS

*  Purpose:
*     Define a function to set an attribute value for a FrameSet axis.

*  Type:
*     Private macro.

*  Synopsis:
*     #include "frameset.h"
*     MAKE_SET_AXIS(attribute,type)

*  Class Membership:
*     Defined by the FrameSet class.

*  Description:
*     This macro expands to an implementation of a private member function
*     of the form:
*
*        static void Set<Attribute>( AstFrame *this, int axis, <type> value )
*
*     that sets the value of a specified attribute for an axis of the
*     current Frame of a FrameSet (this). This function is intended to
*     over-ride the astSet<Attribute> method inherited from the Frame
*     class.

*  Parameters:
*     attribute
*        Name of the attribute, as it appears in the function name.
*     type
*        The C type of the attribute.
*/

/* Define the macro. */
#define MAKE_SET_AXIS(attribute,type) \
static void Set##attribute( AstFrame *this_frame, int axis, type value, int *status ) { \
\
/* Local Variables: */ \
   AstFrame *fr;                 /* Pointer to current Frame */ \
   AstFrameSet *this;            /* Pointer to the FrameSet structure */ \
\
/* Check the global error status. */ \
   if ( !astOK ) return; \
\
/* Obtain a pointer to the FrameSet structure. */ \
   this = (AstFrameSet *) this_frame; \
\
/* Validate the axis index supplied. */ \
   (void) astValidateAxis( this, axis, 1, "astSet" #attribute ); \
\
/* Obtain a pointer to the FrameSet's current Frame and invoke its \
   astSet<Attribute> method.  Annul the Frame pointer afterwards. */ \
   fr = astGetFrame( this, AST__CURRENT ); \
   astSet##attribute( fr, axis, value ); \
   fr = astAnnul( fr ); \
}

/*
*  Name:
*     MAKE_TEST

*  Purpose:
*     Define a function to test if an attribute value is set for a FrameSet.

*  Type:
*     Private macro.

*  Synopsis:
*     #include "frameset.h"
*     MAKE_TEST(attribute)

*  Class Membership:
*     Defined by the FrameSet class.

*  Description:
*     This macro expands to an implementation of a private member function
*     of the form:
*
*        static int Test<Attribute>( AstFrame *this )
*
*     that returns a boolean result (0 or 1) to indicate if the value
*     of a specified attribute for the current Frame of a FrameSet
*     (this) is set. This function is intended to over-ride the
*     astTest<Attribute> method inherited from the Frame class.

*  Parameters:
*     attribute
*        Name of the attribute, as it appears in the function name.
*/

/* Define the macro. */
#define MAKE_TEST(attribute) \
static int Test##attribute( AstFrame *this_frame, int *status ) { \
\
/* Local Variables: */ \
   AstFrame *fr;                 /* Pointer to current Frame */ \
   AstFrameSet *this;            /* Pointer to FrameSet structure */ \
   int result;                   /* Result to return */ \
\
/* Check the global error status. */ \
   if ( !astOK ) return 0; \
\
/* Obtain a pointer to the FrameSet structure. */ \
   this = (AstFrameSet *) this_frame; \
\
/* Obtain a pointer to the FrameSet's current Frame and invoke its \
   astTest<Attribute> method.  Annul the Frame pointer afterwards. */ \
   fr = astGetFrame( this, AST__CURRENT ); \
   result = astTest##attribute( fr ); \
   fr = astAnnul( fr ); \
\
/* If an error occurred, clear the result value. */ \
   if ( !astOK ) result = 0; \
\
/* Return the result. */ \
   return result; \
}

/*
*  Name:
*     MAKE_TEST_AXIS

*  Purpose:
*     Define a function to test if an attribute value is set for a FrameSet
*     axis.

*  Type:
*     Private macro.

*  Synopsis:
*     #include "frameset.h"
*     MAKE_TEST_AXIS(attribute)

*  Class Membership:
*     Defined by the FrameSet class.

*  Description:
*     This macro expands to an implementation of a private member function
*     of the form:
*
*        static int Test<Attribute>( AstFrame *this, int axis )
*
*     that returns a boolean result (0 or 1) to indicate if the value
*     of a specified attribute for an axis of the current Frame of a
*     FrameSet (this) is set. This function is intended to over-ride
*     the astTest<Attribute> method inherited from the Frame class.

*  Parameters:
*     attribute
*        Name of the attribute, as it appears in the function name.
*/

/* Define the macro. */
#define MAKE_TEST_AXIS(attribute) \
static int Test##attribute( AstFrame *this_frame, int axis, int *status ) { \
\
/* Local Variables: */ \
   AstFrame *fr;                 /* Pointer to current Frame */ \
   AstFrameSet *this;            /* Pointer to the FrameSet structure */ \
   int result;                   /* Value to return */ \
\
/* Check the global error status. */ \
   if ( !astOK ) return 0; \
\
/* Obtain a pointer to the FrameSet structure. */ \
   this = (AstFrameSet *) this_frame; \
\
/* Validate the axis index supplied. */ \
   (void) astValidateAxis( this, axis, 1, "astTest" #attribute ); \
\
/* Obtain a pointer to the FrameSet's current Frame and invoke its \
   astTest<Attribute> method.  Annul the Frame pointer afterwards. */ \
   fr = astGetFrame( this, AST__CURRENT ); \
   result = astTest##attribute( fr, axis ); \
   fr = astAnnul( fr ); \
\
/* If an error occurred, clear the result value. */ \
   if ( !astOK ) result = 0; \
\
/* Return the result. */ \
   return result; \
}

/* Header files. */
/* ============= */
/* Interface definitions. */
/* ---------------------- */

#include "globals.h"             /* Thread-safe global data access */
#include "error.h"               /* Error reporting facilities */
#include "memory.h"              /* Memory allocation facilities */
#include "object.h"              /* Base Object class */
#include "mapping.h"             /* Coordinate Mappings */
#include "unitmap.h"             /* Unit Mappings */
#include "permmap.h"             /* Coordinate permutation Mappings */
#include "cmpmap.h"              /* Compound Mappings */
#include "frame.h"               /* Parent Frame class */
#include "frameset.h"            /* Interface definition for this class */
#include "cmpframe.h"            /* Compound coordinate frames */

/* Error code definitions. */
/* ----------------------- */
#include "ast_err.h"             /* AST error codes */

/* C header files. */
/* --------------- */
#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

/* Module Variables. */
/* ================= */

/* Address of this static variable is used as a unique identifier for
   member of this class. */
static int class_check;

/* Pointers to parent class methods which are extended by this class. */
static int (* parent_getobjsize)( AstObject *, int * );
static void (* parent_clear)( AstObject *, const char *, int * );
static int (* parent_getusedefs)( AstObject *, int * );
static void (* parent_vset)( AstObject *, const char *, char **, va_list, int * );

#if defined(THREAD_SAFE)
static int (* parent_managelock)( AstObject *, int, int, AstObject **, int * );
#endif

/* Define macros for accessing each item of thread specific global data. */
#ifdef THREAD_SAFE

/* Define how to initialise thread-specific globals. */
#define GLOBAL_inits \
   globals->Class_Init = 0; \
   globals->GetAttrib_Buff[ 0 ] = 0; \
   globals->Integrity_Frame = NULL; \
   globals->Integrity_Method = ""; \
   globals->Integrity_Lost = 0; \
   globals->GetAllVariants_Buff[ 0 ] = 0; \

/* Create the function that initialises global data for this module. */
astMAKE_INITGLOBALS(FrameSet)

/* Define macros for accessing each item of thread specific global data. */
#define class_init astGLOBAL(FrameSet,Class_Init)
#define class_vtab astGLOBAL(FrameSet,Class_Vtab)
#define getattrib_buff astGLOBAL(FrameSet,GetAttrib_Buff)
#define integrity_frame astGLOBAL(FrameSet,Integrity_Frame)
#define integrity_method astGLOBAL(FrameSet,Integrity_Method)
#define integrity_lost astGLOBAL(FrameSet,Integrity_Lost)
#define getallvariants_buff astGLOBAL(FrameSet,GetAllVariants_Buff)



/* If thread safety is not needed, declare and initialise globals at static
   variables. */
#else

/* Buffer returned by GetAttrib. */
static char getattrib_buff[ AST__FRAMESET_GETATTRIB_BUFF_LEN + 1 ];

/* Variables associated with preserving FrameSet integrity. */
static AstFrame *integrity_frame = NULL; /* Pointer to copy of current Frame */
static const char *integrity_method = ""; /* Name of method being used */
static int integrity_lost = 0;   /* Current Frame modified? */


/* Define the class virtual function table and its initialisation flag
   as static variables. */
static AstFrameSetVtab class_vtab;   /* Virtual function table */
static int class_init = 0;       /* Virtual function table initialised? */

/* String buffers. */
static char getallvariants_buff[ AST__FRAMESET_GETALLVARIANTS_BUFF_LEN + 1 ];

#endif


/* Prototypes for Private Member Functions. */
/* ======================================== */
static AstAxis *GetAxis( AstFrame *, int, int * );
static AstFrame *GetFrame( AstFrameSet *, int, int * );
static AstFrame *PickAxes( AstFrame *, int, const int[], AstMapping **, int * );
static AstFrameSet *Convert( AstFrame *, AstFrame *, const char *, int * );
static AstFrameSet *ConvertX( AstFrame *, AstFrame *, const char *, int * );
static AstFrameSet *FindFrame( AstFrame *, AstFrame *, const char *, int * );
static AstLineDef *LineDef( AstFrame *, const double[2], const double[2], int * );
static AstMapping *CombineMaps( AstMapping *, int, AstMapping *, int, int, int * );
static AstMapping *GetMapping( AstFrameSet *, int, int, int * );
static AstMapping *RemoveRegions( AstMapping *, int * );
static AstMapping *Simplify( AstMapping *, int * );
static AstObject *Cast( AstObject *, AstObject *, int * );
static AstPointSet *FrameGrid( AstFrame *, int, const double *, const double *, int * );
static AstPointSet *ResolvePoints( AstFrame *, const double [], const double [], AstPointSet *, AstPointSet *, int * );
static AstPointSet *Transform( AstMapping *, AstPointSet *, int, AstPointSet *, int * );
static AstSystemType SystemCode( AstFrame *, const char *, int * );
static AstSystemType ValidateSystem( AstFrame *, AstSystemType, const char *, int * );
static const char *Abbrev( AstFrame *, int, const char *, const char *, const char *, int * );
static const char *Format( AstFrame *, int, double, int * );
static const char *GetAttrib( AstObject *, const char *, int * );
static const char *GetDomain( AstFrame *, int * );
static const char *GetFormat( AstFrame *, int, int * );
static const char *GetLabel( AstFrame *, int, int * );
static const char *GetSymbol( AstFrame *, int, int * );
static const char *GetTitle( AstFrame *, int * );
static const char *GetUnit( AstFrame *, int, int * );
static const char *GetAllVariants( AstFrameSet *, int * );
static const char *SystemString( AstFrame *, AstSystemType, int * );
static const int *GetPerm( AstFrame *, int * );
static double Angle( AstFrame *, const double[], const double[], const double[], int * );
static double AxAngle( AstFrame *, const double[], const double[], int, int * );
static double AxDistance( AstFrame *, int, double, double, int * );
static double AxOffset( AstFrame *, int, double, double, int * );
static double Distance( AstFrame *, const double[], const double[], int * );
static double Centre( AstFrame *, int, double, double, int * );
static double Gap( AstFrame *, int, double, int *, int * );
static double Offset2( AstFrame *, const double[2], double, double, double[2], int * );
static double Rate( AstMapping *, double *, int, int, int * );
static int *MapSplit( AstMapping *, int, const int *, AstMapping **, int * );
static int Equal( AstObject *, AstObject *, int * );
static int Fields( AstFrame *, int, const char *, const char *, int, char **, int *, double *, int * );
static int ForceCopy( AstFrameSet *, int, int * );
static int GetActiveUnit( AstFrame *, int * );
static int GetBase( AstFrameSet *, int * );
static int GetCurrent( AstFrameSet *, int * );
static int GetDigits( AstFrame *, int * );
static int GetDirection( AstFrame *, int, int * );
static int GetIsLinear( AstMapping *, int * );
static int GetMatchEnd( AstFrame *, int * );
static int GetMaxAxes( AstFrame *, int * );
static int GetMinAxes( AstFrame *, int * );
static int GetNaxes( AstFrame *, int * );
static int GetNframe( AstFrameSet *, int * );
static int GetNin( AstMapping *, int * );
static int GetNout( AstMapping *, int * );
static int GetObjSize( AstObject *, int * );
static int GetPermute( AstFrame *, int * );
static int GetPreserveAxes( AstFrame *, int * );
static int GetTranForward( AstMapping *, int * );
static int GetTranInverse( AstMapping *, int * );
static int GetVarFrm( AstFrameSet *, int, int * );
static int IsUnitFrame( AstFrame *, int * );
static int LineContains( AstFrame *, AstLineDef *, int, double *, int * );
static int LineCrossing( AstFrame *, AstLineDef *, AstLineDef *, double **, int * );
static int Match( AstFrame *, AstFrame *, int, int **, int **, AstMapping **, AstFrame **, int * );
static int Span( AstFrameSet *, AstFrame **, int, int, int, AstMapping **, int *, int * );
static int SubFrame( AstFrame *, AstFrame *, int, const int *, const int *, AstMapping **, AstFrame **, int * );
static int TestActiveUnit( AstFrame *, int * );
static int TestAttrib( AstObject *, const char *, int * );
static int TestBase( AstFrameSet *, int * );
static int TestCurrent( AstFrameSet *, int * );
static int TestDigits( AstFrame *, int * );
static int TestDirection( AstFrame *, int, int * );
static int TestDomain( AstFrame *, int * );
static int TestFormat( AstFrame *, int, int * );
static int TestLabel( AstFrame *, int, int * );
static int TestMatchEnd( AstFrame *, int * );
static int TestMaxAxes( AstFrame *, int * );
static int TestMinAxes( AstFrame *, int * );
static int TestPermute( AstFrame *, int * );
static int TestPreserveAxes( AstFrame *, int * );
static int TestSymbol( AstFrame *, int, int * );
static int TestTitle( AstFrame *, int * );
static int TestUnit( AstFrame *, int, int * );
static int Unformat( AstFrame *, int, const char *, double *, int * );
static int ValidateAxis( AstFrame *, int, int, const char *, int * );
static int ValidateFrameIndex( AstFrameSet *, int, const char *, int * );
static void AddFrame( AstFrameSet *, int, AstMapping *, AstFrame *, int * );
static void AppendAxes( AstFrameSet *, AstFrame *, int * );
static void AxNorm( AstFrame *, int, int, int, double *, int * );
static void CheckPerm( AstFrame *, const int *, const char *, int * );
static void Clear( AstObject *, const char *, int * );
static void ClearAttrib( AstObject *, const char *, int * );
static void ClearBase( AstFrameSet *, int * );
static void ClearCurrent( AstFrameSet *, int * );
static void ClearDigits( AstFrame *, int * );
static void ClearDirection( AstFrame *, int, int * );
static void ClearDomain( AstFrame *, int * );
static void ClearFormat( AstFrame *, int, int * );
static void ClearLabel( AstFrame *, int, int * );
static void ClearMatchEnd( AstFrame *, int * );
static void ClearMaxAxes( AstFrame *, int * );
static void ClearMinAxes( AstFrame *, int * );
static void ClearPermute( AstFrame *, int * );
static void ClearPreserveAxes( AstFrame *, int * );
static void ClearSymbol( AstFrame *, int, int * );
static void ClearTitle( AstFrame *, int * );
static void ClearUnit( AstFrame *, int, int * );
static void Copy( const AstObject *, AstObject *, int * );
static void Delete( AstObject *, int * );
static void Dump( AstObject *, AstChannel *, int * );
static void Intersect( AstFrame *, const double[2], const double[2], const double[2], const double[2], double[2], int * );
static void LineOffset( AstFrame *, AstLineDef *, double, double, double[2], int * );
static void MatchAxes( AstFrame *, AstFrame *, int *, int * );
static void MatchAxesX( AstFrame *, AstFrame *, int *, int * );
static void Norm( AstFrame *, double[], int * );
static void NormBox( AstFrame *, double[], double[], AstMapping *, int * );
static void Offset( AstFrame *, const double[], const double[], double, double[], int * );
static void Overlay( AstFrame *, const int *, AstFrame *, int * );
static void PermAxes( AstFrame *, const int[], int * );
static void PrimaryFrame( AstFrame *, int, AstFrame **, int *, int * );
static void RecordIntegrity( AstFrameSet *, int * );
static void AddVariant( AstFrameSet *, AstMapping *, const char *, int * );
static void MirrorVariants( AstFrameSet *, int, int * );
static void RemapFrame( AstFrameSet *, int, AstMapping *, int * );
static void RemoveFrame( AstFrameSet *, int, int * );
static void RemoveMirrors( AstFrameSet *, int, int * );
static void ReportPoints( AstMapping *, int, AstPointSet *, AstPointSet *, int * );
static void Resolve( AstFrame *, const double [], const double [], const double [], double [], double *, double *, int * );
static void RestoreIntegrity( AstFrameSet *, int * );
static void SetActiveUnit( AstFrame *, int, int * );
static void SetAttrib( AstObject *, const char *, int * );
static void SetAxis( AstFrame *, int, AstAxis *, int * );
static void SetBase( AstFrameSet *, int, int * );
static void SetCurrent( AstFrameSet *, int, int * );
static void SetDigits( AstFrame *, int, int * );
static void SetDirection( AstFrame *, int, int, int * );
static void SetDomain( AstFrame *, const char *, int * );
static void SetFormat( AstFrame *, int, const char *, int * );
static void SetLabel( AstFrame *, int, const char *, int * );
static void SetMatchEnd( AstFrame *, int, int * );
static void SetMaxAxes( AstFrame *, int, int * );
static void SetMinAxes( AstFrame *, int, int * );
static void SetPermute( AstFrame *, int, int * );
static void SetPreserveAxes( AstFrame *, int, int * );
static void SetSymbol( AstFrame *, int, const char *, int * );
static void SetTitle( AstFrame *, const char *, int * );
static void SetUnit( AstFrame *, int, const char *, int * );
static void TidyNodes( AstFrameSet *, int * );
static void VSet( AstObject *, const char *, char **, va_list, int * );
static void ValidateAxisSelection( AstFrame *, int, const int *, const char *, int * );

static double GetBottom( AstFrame *, int, int * );
static int TestBottom( AstFrame *, int, int * );
static void ClearBottom( AstFrame *, int, int * );
static void SetBottom( AstFrame *, int, double, int * );

static double GetTop( AstFrame *, int, int * );
static int TestTop( AstFrame *, int, int * );
static void ClearTop( AstFrame *, int, int * );
static void SetTop( AstFrame *, int, double, int * );

static double GetEpoch( AstFrame *, int * );
static int TestEpoch( AstFrame *, int * );
static void ClearEpoch( AstFrame *, int * );
static void SetEpoch( AstFrame *, double, int * );

static double GetObsAlt( AstFrame *, int * );
static int TestObsAlt( AstFrame *, int * );
static void ClearObsAlt( AstFrame *, int * );
static void SetObsAlt( AstFrame *, double, int * );

static double GetObsLat( AstFrame *, int * );
static int TestObsLat( AstFrame *, int * );
static void ClearObsLat( AstFrame *, int * );
static void SetObsLat( AstFrame *, double, int * );

static double GetObsLon( AstFrame *, int * );
static int TestObsLon( AstFrame *, int * );
static void ClearObsLon( AstFrame *, int * );
static void SetObsLon( AstFrame *, double, int * );

static int GetUseDefs( AstObject *, int * );

static AstSystemType GetSystem( AstFrame *, int * );
static int TestSystem( AstFrame *, int * );
static void ClearSystem( AstFrame *, int * );
static void SetSystem( AstFrame *, AstSystemType, int * );

static AstSystemType GetAlignSystem( AstFrame *, int * );
static int TestAlignSystem( AstFrame *, int * );
static void ClearAlignSystem( AstFrame *, int * );
static void SetAlignSystem( AstFrame *, AstSystemType, int * );

static const char *GetVariant( AstFrameSet *, int * );
static int TestVariant( AstFrameSet *, int * );
static void ClearVariant( AstFrameSet *, int * );
static void SetVariant( AstFrameSet *, const char *, int * );

#if defined(THREAD_SAFE)
static int ManageLock( AstObject *, int, int, AstObject **, int * );
#endif

/* Member functions. */
/* ================= */
static const char *Abbrev( AstFrame *this_frame, int axis, const char *fmt,
                           const char *str1, const char *str2, int *status ) {
/*
*  Name:
*     Abbrev

*  Purpose:
*     Abbreviate a formatted FrameSet axis value by skipping leading fields.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     const char *Abbrev( AstFrame *this, int axis, const char *fmt,
*                         const char *str1, const char *str2, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astAbbrev
*     method inherited from the Frame class).

*  Description:
*     This function compares two FrameSet axis values that have been
*     formatted (using astFormat) and determines if they have any
*     redundant leading fields (i.e. leading fields in common which
*     can be suppressed when tabulating the values or plotting them on
*     the axis of a graph).

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     axis
*        The number of the FrameSet axis for which the values have
*        been formatted (axis numbering starts at zero for the first
*        axis).
*     fmt
*        Pointer to a constant null-terminated string containing the
*        format specification used to format the two values.
*     str1
*        Pointer to a constant null-terminated string containing the
*        first formatted value. If this is null, the returned pointer
*        points to the start of the final field in str2.
*     str2
*        Pointer to a constant null-terminated string containing the
*        second formatted value.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     A pointer into the "str2" string which locates the first
*     character in the first field that differs between the two
*     formatted values.
*
*     If the two values have no leading fields in common, the returned
*     value will point at the start of string "str2". If the two
*     values are equal, it will point at the terminating null at the
*     end of this string.

*  Notes:
*     - This function assumes that the format specification used was
*     the same when both values were formatted and that they both
*     apply to the same FrameSet axis.
*     - A pointer to the start of "str2" will be returned if this
*     function is invoked with the global error status set, or if it
*     should fail for any reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   const char *result;           /* Pointer value to return */

/* Check the global error status. */
   if ( !astOK ) return str2;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index. */
   (void) astValidateAxis( this, axis, 1, "astAbbrev" );

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astAbbrev method to perform the processing. Annul the Frame
   pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astAbbrev( fr, axis, fmt, str1, str2 );
   fr = astAnnul( fr );

/* If an error occurred, clear the result. */
   if ( !astOK ) result = str2;

/* Return the result. */
   return result;
}

static void AddFrame( AstFrameSet *this, int iframe, AstMapping *map,
                      AstFrame *frame, int *status ) {
/*
*++
*  Name:
c     astAddFrame
f     AST_ADDFRAME

*  Purpose:
*     Add a Frame to a FrameSet to define a new coordinate system.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "frameset.h"
c     void astAddFrame( AstFrameSet *this, int iframe, AstMapping *map,
c                       AstFrame *frame )
f     CALL AST_ADDFRAME( THIS, IFRAME, MAP, FRAME, STATUS )

*  Class Membership:
*     FrameSet method.

*  Description:
c     This function adds a new Frame and an associated Mapping to a
f     This routine adds a new Frame and an associated Mapping to a
*     FrameSet so as to define a new coordinate system, derived from
*     one which already exists within the FrameSet. The new Frame then
*     becomes the FrameSet's current Frame.
*
c     This function
f     This routine
*     may also be used to merge two FrameSets, or to append extra axes
*     to every Frame in a FrameSet.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the FrameSet.
c     iframe
f     IFRAME = INTEGER (Given)
*        The index of the Frame within the FrameSet which describes
*        the coordinate system upon which the new one is to be based.
*        This value should lie in the range from 1 to the number of
*        Frames already in the FrameSet (as given by its Nframe
*        attribute). As a special case, AST__ALLFRAMES may be supplied,
*        in which case the axes defined by the supplied Frame are appended
*        to every Frame in the FrameSet (see the Notes section for details).
c     map
f     MAP = INTEGER (Given)
*        Pointer to a Mapping which describes how to convert
*        coordinates from the old coordinate system (described by the
c        Frame with index "iframe") into coordinates in the new
f        Frame with index IFRAME) into coordinates in the new
*        system. The Mapping's forward transformation should perform
*        this conversion, and its inverse transformation should
*        convert in the opposite direction. The supplied Mapping is ignored
c        if parameter "iframe"is equal to AST__ALLFRAMES.
f        if parameter IFRAME is equal to AST__ALLFRAMES.
c     frame
f     FRAME = INTEGER (Given)
*        Pointer to a Frame that describes the new coordinate system.
*        Any class of Frame may be supplied (including Regions and
*        FrameSets).
*
c        This function may also be used to merge two FrameSets by
c        supplying a pointer to a second FrameSet for this parameter
f        This routine may also be used to merge two FrameSets by
f        supplying a pointer to a second FrameSet for this argument
*        (see the Notes section for details).
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Notes:
*     - Deep copies of the supplied
c     "mapping" and "frame"
f     MAPPING and FRAME
*     objects are stored within the modified FrameSet. So any changes made
*     to the FrameSet after calling this method will have no effect on the
*     supplied Mapping and Frame objects.
*     - A value of AST__BASE or AST__CURRENT may be given for the
c     "iframe" parameter to specify the base Frame or the current
f     IFRAME argument to specify the base Frame or the current
*     Frame respectively.
c     - This function sets the value of the Current attribute for the
f     - This routine sets the value of the Current attribute for the
*     FrameSet so that the new Frame subsequently becomes the current
*     Frame.
*     - The number of input coordinate values accepted by the supplied
*     Mapping (its Nin attribute) must match the number of axes in the
c     Frame identified by the "iframe" parameter. Similarly, the
f     Frame identified by the IFRAME argument. Similarly, the
*     number of output coordinate values generated by this Mapping
*     (its Nout attribute) must match the number of axes in the new
*     Frame.
*     - As a special case, if a pointer to a FrameSet is given for the
c     "frame" parameter, this is treated as a request to merge a pair of
f     FRAME argument, this is treated as a request to merge a pair of
*     FrameSets.  This is done by appending all the new Frames (in the
c     "frame" FrameSet) to the original FrameSet, while preserving
f     FRAME FrameSet) to the original FrameSet, while preserving
*     their order and retaining all the inter-relationships
*     (i.e. Mappings) between them. The two sets of Frames are
*     inter-related within the merged FrameSet by using the Mapping
*     supplied. This should convert between the Frame identified by
c     the "iframe" parameter (in the original FrameSet) and the current
c     Frame of the "frame" FrameSet. This latter Frame becomes the
f     the IFRAME argument (in the original FrameSet) and the current
f     Frame of the FRAME FrameSet. This latter Frame becomes the
*     current Frame in the merged FrameSet.
*     - As another special case, if a value of AST__ALLFRAMES is supplied
*     for parameter
c     "iframe",
f     IFRAME,
*     then the supplied Mapping is ignored, and the axes defined by the
*     supplied Frame are appended to each Frame in the FrameSet. In detail,
*     each Frame in the FrameSet is replaced by a CmpFrame containing the
*     original Frame and the Frame specified by parameter
c     "frame".
f     FRAME.
*     In addition, each Mapping in the FrameSet is replaced by a CmpMap
*     containing the original Mapping and a UnitMap in parallel. The Nin and
*     Nout attributes of the UnitMap are set equal to the number of axes
*     in the supplied Frame. Each new CmpMap is simplified using
c     astSimplify
f     AST_SIMPLIFY
*     before being stored in the FrameSet.



*--
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to Frame identified by "iframe" */
   AstFrameSet *frameset;        /* Pointer to new FrameSet (if given) */
   AstMapping *inode_map;        /* Temporarily saved Mapping pointer */
   AstMapping *next_map;         /* Temporarily saved Mapping pointer */
   int current;                  /* Current Frame index in merged FrameSet */
   int current_node;             /* Node number for current Frame */
   int ifr;                      /* Loop counter for Frames */
   int inode;                    /* Loop counter for nodes */
   int inode_invert;             /* Temporarily saved invert flag value */
   int inode_link;               /* Temporarily saved link value */
   int naxes;                    /* Number of Frame axes */
   int ncoord;                   /* Number of Mapping coordinates per point */
   int next;                     /* Number of next node in path */
   int next_invert;              /* Temporarily saved invert flag value */
   int next_link;                /* Temporarily saved link value */
   int nframe;                   /* Number of Frames in merged FrameSet */
   int nnode;                    /* Number of nodes in merged FrameSet */
   int node_zero;                /* Location of "node zero" after merging */

/* Check the global error status. */
   if ( !astOK ) return;

/* First handle cases where we are appending axes to the existing
   Frames in a FrameSet. */
   if( iframe == AST__ALLFRAMES ) {
      AppendAxes( this, frame, status );
      return;
   }

/* Now handle cases where we are adding a new Frame into the FrameSet.
   Initialise variables to avoid "used of uninitialised variable"
   messages from dumb compilers. */
   inode_map = NULL;
   next_map = NULL;
   inode_invert = 0;
   next = 0;
   next_invert = 0;
   next_link = 0;

/* Validate and translate the Frame index supplied. */
   iframe = astValidateFrameIndex( this, iframe, "astAddFrame" );

/* Obtain a pointer to the Frame from which the new coordinate system
   will be derived and determine how many axes it has. Annul the Frame
   pointer afterwards. */
   if ( astOK ) {
      fr = astGetFrame( this, iframe );
      naxes = astGetNaxes( fr );
      fr = astAnnul( fr );

/* Obtain the number of input coordinate values per point for the
   Mapping supplied and check that this matches the number of axes
   obtained above. Report an error if it does not. */
      ncoord = astGetNin( map );
      if ( astOK && ( naxes != ncoord ) ) {
         astError( AST__NCPIN, "astAddFrame(%s): Bad number of %s input "
                   "coordinate values (%d).", status, astGetClass( this ),
                   astGetClass( map ), ncoord );
         astError( AST__NCPIN, "The %s given should accept %d coordinate "
                   "value%s for each input point.", status, astGetClass( map ), naxes,
                   ( naxes == 1 ) ? "" : "s" );
      }
   }

/* Similarly, obtain the number of output coordinate values per point
   for the Mapping and check that this equals the number of axes for
   the Frame supplied. Report an error if necessary. */
   if ( astOK ) {
      ncoord = astGetNout( map );
      naxes = astGetNaxes( frame );
      if ( astOK && ( ncoord != naxes ) ) {
         astError( AST__NCPIN, "astAddFrame(%s): Bad number of %s output "
                   "coordinate values (%d).", status, astGetClass( this ),
                   astGetClass( map ), ncoord );
         astError( AST__NCPIN, "The %s given should generate %d "
                   "coordinate value%s for each output point.", status,
                   astGetClass( map ), naxes, ( naxes == 1 ) ? "" : "s" );
      }
   }

/* Normal Frame supplied. */
/* ====================== */
/* Check that the Frame supplied is not a FrameSet (handling a
   FrameSet is a special case which is addressed later). */
   if ( !astIsAFrameSet( frame ) && astOK ) {

/* Increase the size of the FrameSet's arrays to accommodate one new
   Frame. */
      this->frame = astGrow( this->frame, this->nframe + 1,
                             sizeof( AstFrame * ) );
      this->varfrm = astGrow( this->varfrm, this->nframe + 1,
                             sizeof( int ) );
      this->node = astGrow( this->node, this->nframe + 1, sizeof( int ) );
      this->map = astGrow( this->map, this->nnode, sizeof( AstMapping * ) );
      this->link = astGrow( this->link, this->nnode, sizeof( int ) );
      this->invert = astGrow( this->invert, this->nnode, sizeof( int ) );
      if ( astOK ) {

/* Copy pointers to the Frame and Mapping supplied and store these pointers
   in the FrameSet arrays. */
         this->frame[ this->nframe ] = astCopy( frame );
         this->map[ this->nnode - 1 ] = astCopy( map );

/* Indicate the Frame does not reflect the variant Mappings of any other
   Frame. */
         this->varfrm[ this->nframe ] = 0;

/* Associate the Frame with the Mapping via the "node" array. */
         this->node[ this->nframe ] = this->nnode;

/* Add a "link" value which identifies the node from which the new
   node is derived and store the current value of the Invert attribute
   for the Mapping. */
         this->link[ this->nnode - 1 ] = this->node[ iframe - 1 ];
         this->invert[ this->nnode - 1 ] = astGetInvert( map );

/* If successful, increment the FrameSet's Frame and node counts and
   set the Current attribute so that the new Frame becomes the current
   Frame. */
         if ( astOK ) {
            this->nframe++;
            this->nnode++;
            astSetCurrent( this, this->nframe );

/* If an error occurred while filling the FrameSet's arrays, clear any values
   that may have been added, annulling any copied pointers. */
         } else {
            this->frame[ this->nframe ] =
                  astAnnul( this->frame[ this->nframe ] );
            this->node[ this->nframe ] = -1;
            this->map[ this->nnode - 1 ] =
                  astAnnul( this->map[ this->nnode - 1 ] );
            this->link[ this->nnode - 1 ] = -1;
	 }
      }

/* FrameSet supplied. */
/* ================== */
/* If the Frame supplied is a FrameSet, we handle this as a special
   case by merging the two FrameSets (so that the final result
   contains references to all the Frames from both FrameSets). */
   } else if ( astOK ) {

/* Obtain a pointer to the FrameSet structure containing the new Frame
   references and calculate how many Frames and nodes the combined
   FrameSet will contain. */
      frameset = (AstFrameSet *) frame;
      nframe = this->nframe + frameset->nframe;
      nnode = this->nnode + frameset->nnode;

/* Extend the original FrameSet's arrays to accommodate the new Frames
   and nodes. */
      this->frame = astGrow( this->frame, nframe, sizeof( AstFrame * ) );
      this->varfrm = astGrow( this->varfrm, nframe, sizeof( int ) );
      this->node = astGrow( this->node, nframe, sizeof( int ) );
      this->map = astGrow( this->map, nnode - 1, sizeof( AstMapping * ) );
      this->link = astGrow( this->link, nnode - 1, sizeof( int ) );
      this->invert = astGrow( this->invert, nnode - 1, sizeof( int ) );

/* If OK, loop to transfer the new Frame data into the new array
   elements, cloning each Frame pointer. Increment each "node" value
   to allow for the new node numbering which will apply when the new
   node data is appended to the new arrays. */
      if ( astOK ) {
         for ( ifr = 1; ifr <= frameset->nframe; ifr++ ) {
            this->frame[ this->nframe + ifr - 1 ] =
               astCopy( frameset->frame[ ifr - 1 ] );
            this->node[ this->nframe + ifr - 1 ] =
               frameset->node[ ifr - 1 ] + this->nnode;
            if( frameset->varfrm[ ifr - 1 ] > 0 ) {
               this->varfrm[ this->nframe + ifr - 1 ] =
                  frameset->varfrm[ ifr - 1 ] + this->nframe;
            } else {
               this->varfrm[ this->nframe + ifr - 1 ] = 0;
            }
         }

/* Similarly, transfer the new node data, cloning each Mapping
   pointer. Increment each "link" value to allow for the new node
   numbering. */
         for ( inode = 1; inode < frameset->nnode; inode++ ) {
            this->map[ this->nnode + inode - 1 ] =
               astCopy( frameset->map[ inode - 1 ] );
            this->link[ this->nnode + inode - 1 ] =
               frameset->link[ inode - 1 ] + this->nnode;
            this->invert[ this->nnode + inode - 1 ] =
               frameset->invert[ inode - 1 ];
         }

/* In transferring the node data (above), we left an empty array
   element which will later be filled with data corresponding to node
   zero in the new FrameSet (there are no data to be copied for this
   node). Initialise the data for this element to null values. */
         this->map[ this->nnode - 1 ] = NULL;
         this->link[ this->nnode - 1 ] = -1;
         this->invert[ this->nnode - 1 ] = -1;

/* Determine which is the current Frame in the new FrameSet and
   convert this into the corresponding Frame number in the combined
   one. */
         current = astGetCurrent( frameset ) + this->nframe;

/* We must now form a new link between this Frame and Frame "iframe"
   (using the Mapping supplied) in order to inter-relate the Frames
   from the two FrameSets. However, this cannot be done immediately
   because in general the node corresponding to Frame "current" will
   already have a link pointing to another node. Moreover, the node
   which was originally node zero (in the new FrameSet) still has
   no data in our merged FrameSet.

   To overcome this, we must re-structure the links within the
   transferred data.  We do this by starting at the node corresponding
   to Frame "current" and working back through each link until the
   original node zero is reached. At each step along this path, we
   reverse the direction of the link. This involves shifting the
   associated data by one step along the path, so that it becomes
   associated with the next node. This results in the final
   (initialised-to-null) node acquiring some data, and the starting
   node being left free to receive our new link.

   We compensate for reversing the links by reversing the sense of the
   "invert" flag associated with each Mapping along the path, so that
   the overall structure of the FrameSet is unchanged. */

/* Identify the starting node (the one corresponding to Frame
   "current"). */
         if ( astOK ) {
            current_node = this->node[ current - 1 ];

/* Obtain the value which a "link" element will now have if it
   originally identified node zero in the new FrameSet. We will use
   this value to detect the end of the path. */
            node_zero = this->nnode;

/* If we are not already at "node zero", save the data for the current
   node. */
            if ( current_node != node_zero ) {
               inode_map = this->map[ current_node - 1 ];
               inode_link = this->link[ current_node - 1 ];
               inode_invert = this->invert[ current_node - 1 ];

/* Reset the node's data to null values (pending setting up the new
   link using the Mapping supplied). */
               this->map[ current_node - 1 ] = NULL;
               this->link[ current_node - 1 ] = -1;
               this->invert[ current_node - 1 ] = -1;

/* Identify the next node in the path. */
               next = inode_link;
            }

/* Follow the path until "node zero" is reached. */
            inode = current_node;
            while( inode != node_zero ) {

/* If the next node on the path is not "node zero", save its data
   (because we are about to write over it). */
               if ( next != node_zero ) {
                  next_map = this->map[ next - 1 ];
                  next_link = this->link[ next - 1 ];
                  next_invert = this->invert[ next - 1 ];
               }

/* Reverse the link from the current node to the "next" node. This
   involves transferring the "map" and "invert" values to the "next"
   node and inverting the sense of the latter to compensate. Make the
   "next" node point back to the current one. */
               this->map[ next - 1 ] = inode_map;
               this->link[ next - 1 ] = inode;
               this->invert[ next - 1 ] = !inode_invert;

/* Move on to consider the next node. */
               inode = next;

/* If we have not reached "node zero" yet, transfer the node data we
   saved above into the variables from which it will be transferred to
   the following node on the next pass through this loop. */
               if ( inode != node_zero ) {
                  inode_map = next_map;
                  inode_link = next_link;
                  inode_invert = next_invert;

/* Identify the node that follows the next one. */
                  next = inode_link;
               }
            }

/* Once the necessary links have been re-structured, establish the new
   link that inter-relates the Frames from the two FrameSets. */
            this->map[ current_node - 1 ] = astCopy( map );
            this->link[ current_node - 1 ] = this->node[ iframe - 1 ];
            this->invert[ current_node - 1 ] = astGetInvert( map );
         }

/* If successful, update the Frame and node counts and make the
   appropriate Frame current. */
         if ( astOK ) {
            this->nframe = nframe;
            this->nnode = nnode;
            astSetCurrent( this, current );

/* If an error occurred, loop through all the new Frame and node array
   elements and clear them, ensuring that any remaining Object
   pointers are annulled. */
         } else {
            for ( ifr = 1; ifr <= frameset->nframe; ifr++ ) {
               this->frame[ this->nframe + ifr - 1 ] =
                  astAnnul( this->frame[ this->nframe + ifr - 1 ] );
               this->node[ this->nframe + ifr - 1 ] = -1;
               this->varfrm[ this->nframe + ifr - 1 ] = 0;
            }
            for ( inode = 0; inode < frameset->nnode; inode++ ) {
               this->map[ this->nnode + inode - 1 ] =
                  astAnnul( this->map[ this->nnode + inode - 1 ] );
               this->link[ this->nnode + inode - 1 ] = -1;
               this->invert[ this->nnode + inode - 1 ] = -1;
            }
         }
      }
   }
}

static void AddVariant( AstFrameSet *this, AstMapping *map,
                        const char *name, int *status ) {
/*
*++
*  Name:
c     astAddVariant
f     AST_ADDVARIANT

*  Purpose:
*     Store a new variant Mapping for the current Frame in a FrameSet.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "frameset.h"
c     void astAddVariant( AstFrameSet *this, AstMapping *map,
c                         const char *name, int *status )
f     CALL AST_ADDVARIANT( THIS, MAP, NAME, STATUS )

*  Class Membership:
*     FrameSet method.

*  Description:
c     This function
f     This routine
*     allows a new variant Mapping to be stored with the current Frame
*     in a FrameSet. See the "Variant" attribute for more details. It can
*     also be used to rename the currently selected variant Mapping.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the FrameSet.
c     map
f     MAP = INTEGER (Given)
*        Pointer to a Mapping which describes how to convert
*        coordinates from the current Frame to the new variant of the
*        current Frame. If
c        NULL
f        AST__NULL
*        is supplied, then the name associated with the currently selected
*        variant of the current Frame is set to the value supplied for
c        "name", but no new variant is added.
f        NAME, but no new variant is added.
c     name
f     NAME = CHARACTER * ( * ) (Given)
*        The name to associate with the new variant Mapping (or the currently
*        selected variant Mapping if
c        "map" is NULL).
f        MAP is AST__NULL).
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Notes:
*     - The newly added Variant becomes the current variant on exit (this is
*     equivalent to setting the Variant attribute to the value supplied for
c     "name).
f     NAME).
*     - An error is reported if a variant with the supplied name already
*     exists in the current Frame.
*     - An error is reported if the current Frame is a mirror for the
*     variant Mappings in another Frame. This is only the case if the
c     astMirrorVariants function
f     AST_MIRRORVARIANTS routine
*     has been called to make the current Frame act as a mirror.

*--
*/

/* Local Variables: */
   AstCmpMap *map2;
   AstFrame *frm;
   AstFrame *tfrm;
   AstFrame *vfrm;
   AstFrameSet *tfs;
   AstFrameSet *vfs;
   AstMapping *map1;
   AstMapping *map3;
   char *myname;
   const char *dom;
   int icur;
   int ifrm;
   int new;
   int nfrm;

/* Check the global error status. */
   if ( !astOK ) return;

/* Get the one-based index of the current Frame. */
   icur = astGetCurrent( this );

/* Report an error if the current Frame is just a mirror. */
   if( this->varfrm[ icur - 1 ] > 0 && astOK ) {
      astError( AST__MIRRO, "astAddVariant(%s): Illegal attempt to "
                "add a variant Mapping to a mirror Frame (programming "
                "error).", status, astGetClass( this ) );
   }

/* Get a copy of the supplied string and clean it. */
   myname = astStore( NULL, name, strlen( name ) + 1 );
   astRemoveLeadingBlanks( myname );
   astChrCase( NULL, myname, 1, 0 );
   if( astOK ) {
      myname[ astChrLen( myname ) ] = 0;

/* Get the Variants FrameSet for the current Frame in "this". */
      frm = astGetFrame( this, icur );
      vfs = astGetFrameVariants( frm );

/* If current Frame of this has no Variant FrameSet, create a Variants
   FrameSet containing a copy of the current Frame (retain its Domain
   as the default variant name). */
      if( !vfs ) {
         tfrm = astCopy( frm );
         vfs = astFrameSet( tfrm, " ", status );
         tfrm = astAnnul( tfrm );
         new = 1;
      } else {
         new = 0;
      }

/* Check the Variants FrameSet does not already contain a Frame with
   a Domain equal to the supplied name. */
      nfrm = astGetNframe( vfs );
      for( ifrm = 0; ifrm < nfrm && astOK; ifrm++ ) {
         vfrm = astGetFrame( vfs, ifrm + 1 );
         dom = astGetDomain( vfrm );
         if( astOK && !strcmp( dom, myname ) ) {
            astError( AST__BDVNM, "astAddVariant(%s): Cannot add a "
                      "variant %s Frame with name '%s' because one "
                      "already exists in the %s (programming "
                      "error).", status, astGetClass( this ),
                      astGetDomain( frm ), myname,  astGetClass( this ) );
         }
         vfrm = astAnnul( vfrm );
      }

/* If no Mapping was supplied, just set the name of the currently
   selected variant. The names are stored in the Domain attribute of
   the Frames in the variants FrameSet, so set teh DOmain for the current
   Frame. */
      if( !map ){
         vfrm = astGetFrame( vfs, AST__CURRENT );
         astSetDomain( vfrm, name );
         vfrm = astAnnul( vfrm );

/* If a Mapping was supplied.... */
      } else {

/* Get the Mapping from the current Frame in the variants FrameSet to the
   current Frame in "this". Temporarily match the Domains so that
   astConvert can work. */
         vfrm = astGetFrame( vfs, AST__CURRENT );
         dom = astGetDomain( frm );
         if( dom ) dom = astStore( NULL, dom, strlen( dom ) + 1 );
         astSetDomain( frm, astGetDomain( vfrm ) );
         tfs = astConvert( vfrm, frm, "" );
         astSetDomain( frm, dom );
         if( tfs ) {
            map1 = astGetMapping( tfs, AST__BASE, AST__CURRENT );
            tfs = astAnnul( tfs );

/* Concatenate it with the supplied Mapping to get the mapping from the
   current Frame in the Variants FrameSet to the new variant Frame. */
            map2 = astCmpMap( map1, map, 1, " ", status );
            map3 = astSimplify( map2 );

/* Add a copy of parent Frame into Variants FrameSet, using the above
   mapping to connect it to the original current Variants Frame. Set
   its Domain to the supplied name. Re-instate the original current Frame
   afterwards. Remove the variant frame info before adding it. */
            (void) astAnnul( vfrm );
            vfrm = astCopy( frm );
            astSetFrameVariants( vfrm, NULL );
            astSetDomain( vfrm, name );
            icur = astGetCurrent( vfs );
            astAddFrame( vfs, AST__CURRENT, map3, vfrm );
            astSetCurrent( vfs, icur );

/* Free resources. */
            map1 = astAnnul( map1 );
            map2 = astAnnul( map2 );
            map3 = astAnnul( map3 );

/* Report an error if a Mapping cannot be found from the new variant Frame
   to the current Frame in "this". */
         } else if( astOK ) {
            astError( AST__INTER, "astAddVariant(%s): Cannot convert "
                      "from a %s with Domain '%s' to a %s with Domain "
                      "'%s' (internal programming error).", status,
                      astGetClass( this ), astGetClass( vfrm ),
                      astGetDomain( vfrm ), astGetClass( frm ),
                      astGetDomain( frm ) );
         }

/* Free resources. */
         dom = astFree( (void *) dom );
         vfrm = astAnnul( vfrm );
      }

/* If all is well, and the Variants FrameSet is new, store a pointer to
   it in the current Frame of "this". */
      if( new ) astSetFrameVariants( frm, vfs );

/* Make the new Variant the current variant. */
      if( map ) astSetVariant( this, name );

/* Free remaining resources. */
      frm = astAnnul( frm );
      vfs = astAnnul( vfs );
   }
   myname = astFree( myname );
}

static double Angle( AstFrame *this_frame, const double a[],
                     const double b[], const double c[], int *status ) {
/*
*  Name:
*     Angle

*  Purpose:
*     Calculate the angle subtended by two points at a third point.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     double Angle( AstFrame *this, const double a[], const double b[],
*                   const double c[], int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astAngle
*     method inherited from the Frame class).

*  Description:
*     This function finds the angle at point B between the line joining points
*     A and B, and the line joining points C and B. These lines will in fact be
*     geodesic curves appropriate to the Frame in use. For instance, in
*     SkyFrame, they will be great circles.

*  Parameters:
*     this
*        Pointer to the Frame.
*     a
*        An array of double, with one element for each Frame axis
*        (Naxes attribute) containing the coordinates of the first point.
*     b
*        An array of double, with one element for each Frame axis
*        (Naxes attribute) containing the coordinates of the second point.
*     c
*        An array of double, with one element for each Frame axis
*        (Naxes attribute) containing the coordinates of the third point.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     astAngle
*        The angle in radians, from the line AB to the line CB. If the
*        Frame is 2-dimensional, it will be in the range $\pm \pi$,
*        and positive rotation is in the same sense as rotation from
*        the positive direction of axis 2 to the positive direction of
*        axis 1. If the Frame has more than 2 axes, a positive value will
*        always be returned in the range zero to $\pi$.

*  Notes:
*     - A value of AST__BAD will also be returned if points A and B are
*     co-incident, or if points B and C are co-incident.
*     - A value of AST__BAD will also be returned if this function is
*     invoked with the AST error status set, or if it should fail for
*     any reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   double result;                /* Value to return */

/* Check the global error status. */
   if ( !astOK ) return AST__BAD;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astAngle method. Annul the Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astAngle( fr, a, b, c );
   fr = astAnnul( fr );

/* If an error occurred, clear the result. */
   if ( !astOK ) result = AST__BAD;

/* Return the result. */
   return result;
}

static void AppendAxes( AstFrameSet *this, AstFrame *frame, int *status ) {
/*
*  Name:
*     AppendAxes

*  Purpose:
*     Append axes to every Frame in a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void AppendAxes( AstFrameSet *this, AstFrame *frame, int *status )

*  Class Membership:
*     FrameSet member function

*  Description:
*     This function replaces every Frame in the FrameSet with a CmpFrame
*     holding the original Frame and the supplied Frame. It also replaces
*     every Mapping in the FrameSet with a parallel CmpMap holding the
*     original Mapping and a UnitMap. The Nin and Nout attributes of every
*     UnitMap are equal to the number of axes in the supplied Frame. Each
*     CmpMap is simplified before being stored in the FrameSet.


*  Parameters:
*     this
*        Pointer to the Frame.
*     frame
*        Pointer to a Frame holding the new axes to add to every Frame in
*        the FrameSet.
*     status
*        Pointer to the inherited status variable.

*/

/* Local Variables: */
   AstCmpFrame *frm;             /* Pointer to new Frame */
   AstCmpMap *map;               /* UnitMap to new Mapping */
   AstUnitMap *umap;             /* UnitMap to feed the new axes */
   int iframe;                   /* Frame index */
   int imap;                     /* Mapping index */
   int inv_orig;                 /* Original value of Invert attribute */

/* Check the global error status. */
   if ( !astOK ) return;

/* Loop round every Frame in the FrameSet. */
   for ( iframe = 0; iframe < this->nframe; iframe++ ) {

/* Create a CmpFrame holding the original Frame and the new Frame. */
      frm = astCmpFrame( this->frame[ iframe ], frame, " ", status );

/* Annul the original Frame pointer and store the new CmpFrame pointer. */
      (void) astAnnul( this->frame[ iframe ] );
      this->frame[ iframe ] = (AstFrame *) frm;
   }

/* Create a UnitMap with the number of inputs and outputs equal to the
   number of axes in the supplied Frame. */
   umap = astUnitMap( astGetNaxes( frame ), " ", status );

/* Loop round every Mapping in the FrameSet. */
   for ( imap = 0; imap < this->nnode - 1; imap++ ) {

/* The Invert attribute of the Mapping may have been changed via a
   different pointer since it was first added into the FrameSet. To
   ensure that the FrameSet continues to behave as was originally
   intended, we set the Invert attribute back to the value it had when
   the Mapping was first added into the FrameSet. First, note the
   current value of the Invert flag so that it can be re-instated later. */
      inv_orig = astGetInvert( this->map[ imap ] );
      astSetInvert( this->map[ imap ], this->invert[ imap ] );

/*  Create a parallel CmpMap holding the original Mapping and the UnitMap. */
      map = astCmpMap( this->map[ imap ], umap, 0, " ", status );

/* Re-instate the original value for the Invert flag, and then annul the
   original Mapping pointer. */
      astSetInvert( this->map[ imap ], inv_orig );
      (void) astAnnul( this->map[ imap ] );

/* Simplify the new Mapping, and store it in the FrameSet. */
      this->map[ imap ] = astSimplify( map );

/* Store a copy of the Invert attribute that should be used with this
   Mapping within the FrameSet (just in case it  is modified via some
   excternal reference). */
      this->invert[ imap ] = astGetInvert( this->map[ imap ] );

/* Annul the un-simplified Mapping pointer. */
      map = astAnnul( map );
   }

/* Annul the UnitMap pointer. */
   umap = astAnnul( umap );
}

static double AxAngle( AstFrame *this_frame, const double a[], const double b[], int axis, int *status ) {
/*
*  Name:
*     AxAngle

*  Purpose:
*     Returns the angle from an axis, to a line through two points.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     double AxAngle( AstFrame *this, const double a[], const double b[], int axis, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astAxAngle
*     method inherited from the Frame class).

*  Description:
*     This function finds the angle, as seen from point A, between the positive
*     direction of a specified axis, and the geodesic curve joining point
*     A to point B.

*  Parameters:
*     this
*        Pointer to the Frame.
*     a
*        An array of double, with one element for each Frame axis
*        (Naxes attribute) containing the coordinates of the first point.
*     b
*        An array of double, with one element for each Frame axis
*        (Naxes attribute) containing the coordinates of the second point.
*     axis
*        The number of the Frame axis from which the angle is to be
*        measured (one-based).
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*        The angle in radians, from the positive direction of the
*        specified axis, to the line AB. If the Frame is 2-dimensional,
*        it will be in the range $\pm \pi$, and positive rotation is in
*        the same sense as rotation from the positive direction of axis 2
*        to the positive direction of axis 1. If the Frame has more than 2
*        axes, a positive value will always be returned in the range zero
*        to $\pi$.

*  Notes:
*     - The geodesic curve used by this function is the path of
*     shortest distance between two points, as defined by the
*     astDistance function.
*     - This function will return "bad" coordinate values (AST__BAD)
*     if any of the input coordinates has this value, or if the require
*     position angle is undefined.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   double result;                /* Value to return */

/* Check the global error status. */
   if ( !astOK ) return AST__BAD;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index. */
   (void) astValidateAxis( this, axis - 1, 1, "astAxAngle" );

/* Obtain a pointer to the FrameSet's current Frame and invoke the
   astAxAngle method for this Frame. Annul the Frame pointer
   afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astAxAngle( fr, a, b, axis );
   fr = astAnnul( fr );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = AST__BAD;

/* Return the result. */
   return result;
}

static double AxDistance( AstFrame *this_frame, int axis, double v1, double v2, int *status ) {
/*
*  Name:
*     AxDistance

*  Purpose:
*     Find the distance between two axis values.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     double AxDistance( AstFrame *this, int axis, double v1, double v2, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astAxDistance
*     method inherited from the Frame class).

*  Description:
*     This function returns a signed value representing the axis increment
*     from axis value v1 to axis value v2.
*
*     For a simple Frame, this is a trivial operation returning the
*     difference between the two axis values. But for other derived classes
*     of Frame (such as a SkyFrame) this is not the case.

*  Parameters:
*     this
*        Pointer to the Frame.
*     axis
*        The index of the axis to which the supplied values refer. The
*        first axis has index 1.
*     v1
*        The first axis value.
*     v2
*        The second axis value.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The distance between the two axis values.

*  Notes:
*     - This function will return a "bad" result value (AST__BAD) if
*     any of the input vaues has this value.
*     - A "bad" value will also be returned if this function is
*     invoked with the AST error status set, or if it should fail for
*     any reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   double result;                /* Value to return */

/* Check the global error status. */
   if ( !astOK ) return AST__BAD;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index. */
   (void) astValidateAxis( this, axis - 1, 1, "astAxDistance" );

/* Obtain a pointer to the FrameSet's current Frame and invoke the
   astAxDistance method for this Frame. Annul the Frame pointer
   afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astAxDistance( fr, axis, v1, v2 );
   fr = astAnnul( fr );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = AST__BAD;

/* Return the result. */
   return result;
}

static void AxNorm( AstFrame *this_frame, int axis, int oper, int nval,
                    double *values, int *status ){
/*
*  Name:
*     AxNorm

*  Purpose:
*     Normalise an array of axis values.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void AxNorm( AstFrame *this, int axis, int oper, int nval,
*                  double *values, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astAxNorm
*     method inherited from the Frame class).

*  Description:
*     This function modifies a supplied array of axis values so that
*     they are normalised in the manner indicated by parameter "oper".
*
*     No normalisation is possible for a simple Frame and so the supplied
*     values are returned unchanged. However, this may not be the case for
*     specialised sub-classes of Frame. For instance, a SkyFrame has a
*     discontinuity at zero longitude and so a longitude value can be
*     expressed in the range [-Pi,+PI] or the range [0,2*PI].

*  Parameters:
*     this
*        Pointer to the Frame.
*     axis
*        The index of the axis to which the supplied values refer. The
*        first axis has index 1.
*     oper
*        Indicates the type of normalisation to be applied. If zero is
*        supplied, the normalisation will be the same as that performed by
*        function astNorm. If 1 is supplied, the normalisation will be
*        chosen automatically so that the resulting list has the smallest
*        range.
*     nval
*        The number of points in the values array.
*     values
*        On entry, the axis values to be normalised. Modified on exit to
*        hold the normalised values.
*     status
*        Pointer to the inherited status variable.

*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index. */
   (void) astValidateAxis( this, axis - 1, 1, "astAxNorm" );

/* Obtain a pointer to the FrameSet's current Frame and invoke the
   astAxNorm method for this Frame. Annul the Frame pointer
   afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   astAxNorm( fr, axis, oper, nval, values );
   fr = astAnnul( fr );
}

static double AxOffset( AstFrame *this_frame, int axis, double v1, double dist, int *status ) {
/*
*  Name:
*     AxOffset

*  Purpose:
*     Add an increment onto a supplied axis value.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     double AxOffset( AstFrame *this, int axis, double v1, double dist, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astAxOffset
*     method inherited from the Frame class).

*  Description:
*     This function returns an axis value formed by adding a signed axis
*     increment onto a supplied axis value.
*
*     For a simple Frame, this is a trivial operation returning the
*     sum of the two supplied values. But for other derived classes
*     of Frame (such as a SkyFrame) this is not the case.

*  Parameters:
*     this
*        Pointer to the Frame.
*     axis
*        The index of the axis to which the supplied values refer. The
*        first axis has index 1.
*     v1
*        The original axis value.
*     dist
*        The axis increment to add to the original axis value.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The incremented axis value.

*  Notes:
*     - This function will return a "bad" result value (AST__BAD) if
*     any of the input vaues has this value.
*     - A "bad" value will also be returned if this function is
*     invoked with the AST error status set, or if it should fail for
*     any reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   double result;                /* Value to return */

/* Check the global error status. */
   if ( !astOK ) return AST__BAD;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index. */
   (void) astValidateAxis( this, axis - 1, 1, "astAxOffset" );

/* Obtain a pointer to the FrameSet's current Frame and invoke the
   astAxOffset method for this Frame. Annul the Frame pointer
   afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astAxOffset( fr, axis, v1, dist );
   fr = astAnnul( fr );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = AST__BAD;

/* Return the result. */
   return result;
}

static AstObject *Cast( AstObject *this_object, AstObject *obj, int *status ) {
/*
*  Name:
*     Cast

*  Purpose:
*     Cast an Object into an instance of a sub-class.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstObject *Cast( AstObject *this, AstObject *obj, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astCast
*     method inherited from the Frame class).

*  Description:
*     This function returns a deep copy of an ancestral component of the
*     supplied object. The required class of the ancestral component is
*     specified by another object. Specifically, if "this" and "new" are
*     of the same class, a copy of "this" is returned. If "this" is an
*     instance of a subclass of "obj", then a copy of the component
*     of "this" that matches the class of "obj" is returned. Otherwise,
*     a NULL pointer is returned without error.

*  Parameters:
*     this
*        Pointer to the Object to be cast.
*     obj
*        Pointer to an Object that defines the class of the returned Object.
*        The returned Object will be of the same class as "obj".

*  Returned Value:
*     A pointer to the new Object. NULL if "this" is not a sub-class of
*     "obj", or if an error occurs.

*  Notes:
*     - A NULL pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables; */
   AstFrame *cfrm;
   AstObject *new;
   astDECLARE_GLOBALS
   int generation_gap;

/* Initialise */
   new = NULL;

/* Check inherited status */
   if( !astOK ) return new;

/* Get a pointer to the thread specific global data structure. */
   astGET_GLOBALS(NULL);

/* See how many steps up the class inheritance ladder it is from "obj"
   to this class (FrameSet). A positive value is returned if FrameSet
   is a sub-class of "obj". A negative value is returned if "obj" is
   a sub-class of FrameSet. Zero is returned if "obj" is a FrameSet.
   AST__COUSIN is returned if "obj" is not on the same line of descent
   as FrameSet. */
   generation_gap = astClassCompare( (AstObjectVtab *) &class_vtab,
                                     astVTAB( obj ) );

/* If "obj" is a FrameSet or a sub-class of FrameSet, we can cast by
   truncating the vtab for "this" so that it matches the vtab of "obJ",
   and then taking a deep copy of "this". */
   if( generation_gap <= 0 && generation_gap != AST__COUSIN ) {
      new = astCastCopy( this_object, obj );

/* If "obj" is not a FrameSet or a sub-class of FrameSet (e.g. a Frame or
   some sub-class of Frame), we attempt to cast the current Frame into
   the class indicated by "obj". */
   } else {
      cfrm = astGetFrame( (AstFrameSet *) this_object, AST__CURRENT );
      new = astCast( cfrm, obj );
      cfrm = astAnnul( cfrm );
   }

/* Return the new pointer. */
   return new;
}

static double Centre( AstFrame *this_frame, int axis, double value, double gap, int *status ) {
/*
*  Name:
*     Centre

*  Purpose:
*     Find a "nice" central value for tabulating Frame axis values.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     double  Centre( AstFrame *this_frame, int axis, double value,
*                     double gap, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astCentre method
*     inherited from the Frame class).

*  Description:
*     This function returns an axis value which produces a nice formatted
*     value suitable for a major tick mark on a plot axis, close to the
*     supplied axis value.

*  Parameters:
*     this
*        Pointer to the Frame.
*     axis
*        The number of the axis (zero-based) for which a central value
*        is to be found.
*     value
*        An arbitrary axis value in the section that is being plotted.
*     gap
*        The gap size.

*  Returned Value:
*     The nice central axis value.

*  Notes:
*     - A value of zero is returned if the supplied gap size is zero.
*     - A value of zero will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   double result;                /* Value to return */

/* Check the global error status. */
   if ( !astOK ) return 0.0;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index. */
   (void) astValidateAxis( this, axis, 1, "astCentre" );

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astCentre method to obtain the required value. Annul the
   Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astCentre( fr, axis, value, gap );
   fr = astAnnul( fr );

/* If an error occurred, clear the result. */
   if ( !astOK ) result = 0.0;

/* Return the result. */
   return result;
}

static void CheckPerm( AstFrame *this_frame, const int *perm, const char *method, int *status ) {
/*
*  Name:
*     CheckPerm

*  Purpose:
*     Check that an array contains a valid permutation.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void CheckPerm( AstFrame *this, const int *perm, const char *method, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astCheckPerm
*     method inherited from the Frame class).

*  Description:
*     This function checks the validity of a permutation array that
*     will be used to permute the order of a Frame's axes. If the
*     permutation specified by the array is not valid, an error is
*     reported and the global error status is set. Otherwise, the
*     function returns without further action.

*  Parameters:
*     this
*        Pointer to the Frame.
*     perm
*        Pointer to an array of integers with the same number of
*        elements as there are axes in the Frame. For each axis, the
*        corresponding integer gives the (zero based) axis index to be
*        used to identify the information for that axis (using the
*        un-permuted axis numbering). To be valid, the integers in
*        this array should therefore all lie in the range zero to
*        (naxes-1) inclusive, where "naxes" is the number of Frame
*        axes, and each value should occur exactly once.
*     method
*        Pointer to a constant null-terminated character string
*        containing the name of the method that invoked this function
*        to validate a permutation array. This method name is used
*        solely for constructing error messages.
*     status
*        Pointer to the inherited status variable.

*  Notes:
*     - Error messages issued by this function refer to the external
*     (public) numbering system used for axes (which is one-based),
*     whereas zero-based axis indices are used internally.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astCheckPerm method. Annul the Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   astCheckPerm( fr, perm, method );
   fr = astAnnul( fr );

}

static void Clear( AstObject *this_object, const char *attrib, int *status ) {
/*
*  Name:
*     Clear

*  Purpose:
*     Clear attribute values for a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void Clear( AstObject *this, const char *attrib, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the public astClear method
*     inherited from the Object class).

*  Description:
*     This function clears the values of a specified set of attributes
*     for a FrameSet. Clearing an attribute cancels any value that has
*     previously been explicitly set for it, so that the standard
*     default attribute value will subsequently be used instead. This
*     also causes the astTest function to return the value zero for
*     the attribute, indicating that no value has been set.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     attrib
*        Pointer to a null-terminated character string containing a
*        comma-separated list of the names of the attributes to be
*        cleared.
*     status
*        Pointer to the inherited status variable.

*  Notes:
*     - This function preserves the integrity of the FrameSet (if
*     possible) by appropriately remapping its current Frame to take
*     account of its changed attribute values.
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Declare the thread specific global data */
   AstFrame *save_frame;         /* Saved pointer to integrity Frame */
   AstFrameSet *this;            /* Pointer to FrameSet structure */
   const char *save_method;      /* Saved pointer to method name */
   int ok;                       /* Status OK? */
   int save_lost;                /* Saved integrity modified flag */

/* Check the global error status. */
   if ( !astOK ) return;

/* Get a pointer to the structure holding thread-specific global data. */
   astGET_GLOBALS(this_object);

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_object;

/* To allow this function to be invoked recursively, we first save any
   existing FrameSet integrity information in local variables. */
   save_frame = integrity_frame;
   save_lost= integrity_lost;
   save_method = integrity_method;

/* Set the name of the method being used (for use in error
   messages). */
   integrity_method = "astClear";

/* Record the initial integrity state of the FrameSet. */
   RecordIntegrity( this, status );

/* Invoke the parent astClear method to clear the FrameSet's attribute
   values and note if this succeeds. */
   (*parent_clear)( this_object, attrib, status );
   ok = astOK;

/* Restore the FrameSet's integrity. */
   RestoreIntegrity( this, status );

/* If integrity could not be restored, then add contextual error
   information. */
   if ( !astOK && ok ) {
      astError( astStatus, "Unable to accommodate clearing the \"%s\" "
                           "attribute(s).", status, attrib );
   }

/* Restore any saved FrameSet integrity information. */
   integrity_frame = save_frame;
   integrity_lost = save_lost;
   integrity_method = save_method;
}

static void ClearAttrib( AstObject *this_object, const char *attrib, int *status ) {
/*
*  Name:
*     ClearAttrib

*  Purpose:
*     Clear an attribute value for a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void ClearAttrib( AstObject *this, const char *attrib )

*  Class Membership:
*     FrameSet member function (over-rides the astClearAttrib protected
*     method inherited from the Frame class).

*  Description:
*     This function clears the value of a specified attribute for a
*     FrameSet, so that the default value will subsequently be used.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     attrib
*        Pointer to a null terminated string specifying the attribute
*        name.  This should be in lower case with no surrounding white
*        space.
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Declare the thread specific global data */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */

/* Check the global error status. */
   if ( !astOK ) return;


/* Get a pointer to the structure holding thread-specific global data. */
   astGET_GLOBALS(this_object);
/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_object;

/* Check the attribute name and clear the appropriate attribute. */

/* We first handle attributes that apply to the FrameSet as a whole
   (rather than to the current Frame). */

/* Base. */
/* ----- */
   if ( !strcmp( attrib, "base" ) ) {
      astClearBase( this );

/* Current. */
/* -------- */
/* Since this determines the choice of current Frame, we must restore
   the integrity state of the FrameSet before changing this attribute
   and record the new integrity state afterwards. */
   } else if ( !strcmp( attrib, "current" ) ) {
      RestoreIntegrity( this, status );
      astClearCurrent( this );
      RecordIntegrity( this, status );

/* ID. */
/* --- */
   } else if ( !strcmp( attrib, "id" ) ) {
      astClearID( this );

/* Ident. */
/* ------ */
   } else if ( !strcmp( attrib, "ident" ) ) {
      astClearIdent( this );

/* Invert. */
/* ------- */
/* Since this affects the choice of current Frame, we must restore the
   integrity state of the FrameSet before changing this attribute and
   record the new integrity state afterwards. */
   } else if ( !strcmp( attrib, "invert" ) ) {
      RestoreIntegrity( this, status );
      astClearInvert( this );
      RecordIntegrity( this, status );

/* Report. */
/* ------- */
   } else if ( !strcmp( attrib, "report" ) ) {
      astClearReport( this );

/* Variant. */
/* -------- */
   } else if ( !strcmp( attrib, "variant" ) ) {
      astClearVariant( this );

/* If the name was not recognised, test if it matches any of the
   read-only attributes of this class. If it does, then report an
   error. */
   } else if ( !strcmp( attrib, "allvariants" ) ||
               !strcmp( attrib, "class" ) ||
               !strcmp( attrib, "nframe" ) ||
               !strcmp( attrib, "nin" ) ||
               !strcmp( attrib, "nobject" ) ||
               !strcmp( attrib, "nout" ) ||
               !strcmp( attrib, "refcount" ) ||
               !strcmp( attrib, "tranforward" ) ||
               !strcmp( attrib, "traninverse" ) ) {
      astError( AST__NOWRT, "astClear: Invalid attempt to clear the \"%s\" "
                "value for a %s.", status, attrib, astGetClass( this ) );
      astError( AST__NOWRT, "This is a read-only attribute." , status);

/* Pass unrecognised attributes on to the FrameSet's current Frame for
   further interpretation. */
   } else {

/* Force a copy to be made of the current Frame, if needed, to make it
   independent of other Frames within the FrameSet. */
      (void) ForceCopy( this, AST__CURRENT, status );

/* Obtain a pointer to the current Frame and invoke its astClearAttrib
   method. Annul the Frame pointer afterwards. */
      fr = astGetFrame( this, AST__CURRENT );
      astClearAttrib( fr, attrib );
      fr = astAnnul( fr );

/* Note that the current Frame has been modified. */
      integrity_lost = 1;
   }
}

static void ClearBase( AstFrameSet *this, int *status ) {
/*
*+
*  Name:
*     astClearBase

*  Purpose:
*     Clear the value of the Base attribute of a FrameSet.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "frameset.h"
*     void astClearBase( AstFrameSet *this )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function clears the value of the Base attribute of a
*     FrameSet. This value is an index that identifies the base Frame
*     for the FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*-
*/

/* Local Variables: */
   int invert;                    /* FrameSet is inverted? */

/* Check the global error status. */
   if ( !astOK ) return;

/* Determine if the FrameSet has been inverted. */
   invert = astGetInvert( this );

/* If it has not been inverted, clear the base Frame index, otherwise
   clear the current Frame index instead. */
   if ( astOK ) *( invert ? &this->current : &this->base ) = -INT_MAX;
}

static void ClearCurrent( AstFrameSet *this, int *status ) {
/*
*+
*  Name:
*     astClearCurrent

*  Purpose:
*     Clear the value of the Current attribute for a FrameSet.

*  Type:
*     Protected function.

*  Synopsis:
*     #include "frameset.h"
*     int astClearCurrent( AstFrameSet *this )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function clears the value of the Current attribute for a
*     FrameSet. This attribute is an index that identifies the current
*     Frame for the FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*-
*/

/* Local Variables: */
   int invert;                   /* FrameSet is inverted? */

/* Check the global error status. */
   if ( !astOK ) return;

/* Determine if the FrameSet has been inverted. */
   invert = astGetInvert( this );

/* If it has not been inverted, clear the current frame index,
   otherwise clear the base Frame index instead. */
   if ( astOK ) *( invert ? &this->base : &this->current ) = -INT_MAX;
}

static void ClearVariant( AstFrameSet *this, int *status ) {
/*
*+
*  Name:
*     astClearVariant

*  Purpose:
*     Clear the value of the Variant attribute of a FrameSet.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "frameset.h"
*     void astClearVariant( AstFrameSet *this )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function clears the value of the Variant attribute of a
*     FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*-
*/

/* Local Variables: */
   AstFrame *frm;
   int icur;

/* Check the global error status. */
   if ( !astOK ) return;

/* Get the one-based index of the Frame to use. */
   icur = GetVarFrm( this, astGetCurrent( this ), status );

/* Get a pointer to the current Frame in the FrameSet. */
   frm = astGetFrame( this, icur );

/* Replace any Variants FrameSet in the Frame with a NULL pointer. */
   astSetFrameVariants( frm, NULL );

/* Annul the current Frame pointer. */
   frm = astAnnul( frm );

}

static AstMapping *CombineMaps( AstMapping *mapping1, int invert1,
                                AstMapping *mapping2, int invert2,
                                int series, int *status ) {
/*
*  Name:
*     CombineMaps

*  Purpose:
*     Combine two Mappings with specified Invert flags into a CmpMap.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstMapping *CombineMaps( AstMapping *mapping1, int invert1,
*                              AstMapping *mapping2, int invert2,
*                              int series )

*  Class Membership:
*     FrameSet member function.

*  Description:
*     This function combines two Mappings into a CmpMap (compound
*     Mapping) as if their Invert flags were set to specified values
*     when the CmpMap is created. However, the individual Mappings are
*     returned with their Invert flag values unchanged from their
*     original state.

*  Parameters:
*     mapping1
*        Pointer to the first Mapping.
*     invert1
*        The (boolean) Invert flag value required for the first Mapping.
*     mapping2
*        Pointer to the second Mapping.
*     invert2
*        The (boolean) Invert flag value required for the second Mapping.
*     series
*        Whether the Mappings are to be combined in series (as opposed to
*        in parallel).

*  Returned Value:
*     A pointer to the resulting compound Mapping (a CmpMap).

*  Notes:
*     - This function is a wrap-up for the astCmpMap constructor and
*     temporarily assigns the required Invert flag values while
*     creating the required CmpMap. However, it also takes account of
*     the possibility that the two Mapping pointers supplied may point
*     at the same Mapping.
*     - A null Object pointer (AST__NULL) will be returned if this
*     function is invoked with the AST error status set, or if it
*     should fail for any reason.
*/

/* Local Variables: */
   AstMapping *map1;             /* First temporary Mapping pointer */
   AstMapping *map2;             /* Second temporary Mapping pointer */
   AstMapping *result;           /* Pointer to result Mapping */
   int copy;                     /* Copy needed? */
   int inv1;                     /* First original Invert flag value */
   int inv2;                     /* Second original Invert flag value */
   int set1;                     /* First Invert flag originally set? */
   int set2;                     /* Second Invert flag originally set? */

/* Initialise */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Limit incoming values to 0 or 1. */
   invert1 = ( invert1 != 0 );
   invert2 = ( invert2 != 0 );

/* Obtain the Invert flag values for each Mapping. */
   inv1 = astGetInvert( mapping1 );
   inv2 = astGetInvert( mapping2 );

/* Also determine if these values are explicitly set. */
   set1 = astTestInvert( mapping1 );
   set2 = astTestInvert( mapping2 );

/* If both Mappings are actually the same but we need different Invert
   flag values to be set, then this can only be achieved by making a
   copy. Note if this is necessary. */
   copy = ( ( mapping1 == mapping2 ) && ( invert1 != invert2 ) );

/* Clone the first Mapping pointer. Do likewise for the second but
   make a copy instead if necessary. */
   map1 = astClone( mapping1 );
   map2 = copy ? astCopy( mapping2 ) : astClone( mapping2 );

/* If the Invert value for the first Mapping needs changing, make the
   change. */
   if ( invert1 != inv1 ) {
      if ( invert1 ) {
         astSetInvert( map1, 1 );
      } else {
         astClearInvert( map1 );
      }
   }

/* Similarly, change the Invert flag for the second Mapping if
   necessary. */
   if ( invert2 != inv2 ) {
      if ( invert2 ) {
         astSetInvert( map2, 1 );
      } else {
         astClearInvert( map2 );
      }
   }

/* Combine the two Mappings into a CmpMap. */
   result = (AstMapping *) astCmpMap( map1, map2, series, "", status );

/* If the first Mapping's Invert value was changed, restore it to its
   original state. */
   if ( invert1 != inv1 ) {
      if ( set1 ) {
         astSetInvert( map1, inv1 );
      } else {
         astClearInvert( map1 );
      }
   }

/* Similarly, restore the second Mapping's Invert value if
   necessary. This step is not needed, however, if a copy was made. */
   if ( ( invert2 != inv2 ) && !copy ) {
      if ( set2 ) {
         astSetInvert( map2, inv2 );
      } else {
         astClearInvert( map2 );
      }
   }

/* Annul the temporary Mapping pointers. */
   map1 = astAnnul( map1 );
   map2 = astAnnul( map2 );

/* If an error occurred, then annul the result pointer. */
   if ( !astOK ) result = astAnnul( result );

/* Return the result. */
   return result;
}

static AstFrameSet *Convert( AstFrame *from, AstFrame *to,
                             const char *domainlist, int *status ) {
/*
*  Name:
*     Convert

*  Purpose:
*     Determine how to convert between two coordinate systems.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstFrameSet *Convert( AstFrame *from, AstFrame *to,
*                           const char *domainlist, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the public astConvert
*     method inherited fromm the Frame class).

*  Description:
*     This function compares two FrameSets and determines whether it
*     is possible to convert between the coordinate systems which
*     their current Frames represent. If conversion is possible, it
*     returns a FrameSet which describes the conversion and which may
*     be used (as a Mapping) to transform coordinate values in either
*     direction.
*
*     If conversion is possible, the Base attributes of both FrameSets
*     will be modified on exit to identify the Frames which were used
*     as the intermediate coordinate system.

*  Parameters:
*     from
*        Pointer to a FrameSet whose current Frame represents the
*        "source" coordinate system.  Note that the Base attribute of
*        the FrameSet may be modified by this function.
*     to
*        Pointer to a FrameSet whose current Frame represents the
*        "destination" coordinate system.  Note that the Base
*        attribute of the FrameSet may be modified by this function.
*     domainlist
*        Pointer to a null-terminated character string containing a
*        comma-separated list of Frame domains. This may be used to
*        define a priority order for the different intermediate
*        coordinate systems that might be used to perform the
*        conversion.
*
*        The function will first try to obtain a conversion by making
*        use only of intermediate Frames whose Domain attribute
*        matches the first domain in this list. If this fails, the
*        second domain in the list will be used, and so on, until
*        conversion is achieved. A blank domain (e.g. two consecutive
*        commas) indicates that all Frames should be considered,
*        regardless of their Domain attributes. The list is
*        case-insensitive and all white space is ignored.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*        If the requested coordinate conversion is possible, the
*        function returns a pointer to a FrameSet which describes the
*        conversion. Otherwise, a null Object pointer (AST__NULL) is
*        returned without error.
*
*        If a FrameSet is returned, it will contain two Frames. Frame
*        number 1 (its base Frame) will describe the source coordinate
*        system, corresponding to the "from" parameter. Frame number 2
*        (its current Frame) will describe the destination coordinate
*        system, corresponding to the "to" parameter. The Mapping
*        which inter-relates these Frames will perform the required
*        conversion between the two coordinate systems.

*  Notes:
*     - Either of the "from" and "to" pointers may identify a basic
*     Frame instead of a FrameSet, in which case the function behaves
*     as if it were a FrameSet containing only a single Frame.
*     - A NULL pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.

* Implementation Notes:
*    - This function is simply a wrap-up for the ConvertX function
*    which performs the required processing but swaps the order of the
*    first two arguments. This is a trick to allow the astConvert
*    method to be over-ridden by derived classes on the basis of the
*    class of either of the first two arguments.
*/

/* Check the inherited status. */
   if ( !astOK ) return NULL;

/* Invoke the private "ConvertX" member function with the first two
   arguments swapped. */
   return ConvertX( to, from, domainlist, status );
}

static AstFrameSet *ConvertX( AstFrame *to, AstFrame *from,
                              const char *domainlist, int *status ) {
/*
*  Name:
*     ConvertX

*  Purpose:
*     Determine how to convert between two coordinate systems.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstFrameSet *ConvertX( AstFrame *to, AstFrame *from,
*                            const char *domainlist )

*  Class Membership:
*     FrameSet member function (over-rides the protected "astConvertX"
*     method inherited from the Frame class).

*  Description:
*     This function performs the processing for the public astConvert
*     method (as inherited from the Frame class and over-ridden by the
*     FrameSet class) and has exactly the same interface except that
*     the order of the first two arguments is swapped. This is a trick
*     to allow the astConvert method to be over-ridden by derived
*     classes on the basis of the class of either of its first two
*     arguments.
*
*     See the astConvert method for details of the interface.
*/

/* Local Variables: */
   AstFrame *from_frame;         /* Pointer to "from" Frame */
   AstFrame *to_frame;           /* Pointer to "to" Frame */
   AstFrameSet *cvt;             /* Pointer to conversion FrameSet */
   AstFrameSet *result;          /* Pointer to FrameSet to be returned */
   AstMapping *from_map;         /* Pointer to "from" Mapping */
   AstMapping *map;              /* Pointer to conversion Mapping */
   AstMapping *result_map;       /* Pointer to result Mapping */
   AstMapping *tmp;              /* Temporary Mapping pointer */
   AstMapping *to_map;           /* Pointer to "to" Mapping */
   char *domain;                 /* Pointer to individual domain string */
   char *domain_end;             /* Pointer to final null of domain string */
   char *domainlist_copy;        /* Pointer to copy of domains list */
   int *from_order;              /* List of Frame indices in search order */
   int *to_order;                /* List of Frame indices in search order */
   int best_score;               /* Score from best match */
   int from_base;                /* Index of "from" base Frame */
   int from_current;             /* Index of "from" current Frame */
   int from_index;               /* Index of "from" Frame */
   int from_isframe;             /* "from" is a Frame (not a FrameSet)? */
   int from_nframe;              /* Number of "from" Frames */
   int from_number;              /* Loop counter for "from" Frames */
   int iframe_from;              /* Index of best "from" Frame */
   int iframe_to;                /* Index of best "to" Frame */
   int match;                    /* Possible match found? */
   int n;                        /* Count of Frames */
   int perfect;                  /* Perfect match found? */
   int score;                    /* Score from latest match */
   int to_base;                  /* Index of "to" base Frame */
   int to_current;               /* Index of "to" current Frame */
   int to_index;                 /* Index of "to" Frame */
   int to_isframe;               /* "to" is a Frame (not a FrameSet)? */
   int to_nframe;                /* Number of "to" Frames */
   int to_number;                /* Loop counter for "to" Frames */

/* Initialise. */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Initialise variables to avoid "used of uninitialised variable"
   messages from dumb compilers. */
   result_map = NULL;
   iframe_from = 0;
   iframe_to = 0;

/* Determine the number of Frames in "from" and the indices of its
   base and current Frames. Use values of 1 if "from" is a Frame and
   not a FrameSet. */
   from_isframe = !astIsAFrameSet( from );
   from_nframe = from_isframe ? 1 : astGetNframe( from );
   from_base = from_isframe ? 1 : astGetBase( from );
   from_current = from_isframe ? 1 : astGetCurrent( from );

/* Obtain similar values for "to". */
   to_isframe = !astIsAFrameSet( to );
   to_nframe = to_isframe ? 1 : astGetNframe( to );
   to_base = to_isframe ? 1 : astGetBase( to );
   to_current = to_isframe ? 1 : astGetCurrent( to );

/* Allocate memory for arrays which will hold the indices of "from"
   and "to" Frames. */
   from_order = astMalloc( sizeof( int ) * (size_t) from_nframe );
   to_order = astMalloc( sizeof( int ) * (size_t) to_nframe );

/* Make a temporary copy of the domains list. */
   domainlist_copy = astStore( NULL, domainlist,
                               strlen( domainlist ) + (size_t) 1 );
   if ( astOK ) {

/* Fill the "from_order" array with the indices of all the Frames in
   "from", in the order in which they will be used for searching. Use
   the base Frame first. */
      n = 0;
      from_order[ n++ ] = from_base;

/* Then add all the "from" Frames in the appropriate order, omitting
   the base and current Frames. */
      if ( !astGetInvert( from ) ) {
         for ( from_index = 1; from_index <= from_nframe; from_index++ ) {
            if ( ( from_index != from_base ) &&
                 ( from_index != from_current ) ) {
               from_order[ n++ ] = from_index;
            }
         }
      } else {
         for ( from_index = from_nframe; from_index >= 1; from_index-- ) {
            if ( ( from_index != from_base ) &&
                 ( from_index != from_current ) ) {
               from_order[ n++ ] = from_index;
            }
         }
      }

/* Finish with the current Frame, if different from the base Frame. */
      if ( from_current != from_base ) from_order[ n++ ] = from_current;

/* Repeat this process for the "to" Frame. */
      n = 0;
      to_order[ n++ ] = to_base;
      if ( !astGetInvert( to ) ) {
         for ( to_index = 1; to_index <= to_nframe; to_index++ ) {
            if ( ( to_index != to_base ) && ( to_index != to_current ) ) {
               to_order[ n++ ] = to_index;
            }
         }
      } else {
         for ( to_index = to_nframe; to_index >= 1; to_index-- ) {
            if ( ( to_index != to_base ) && ( to_index != to_current ) ) {
               to_order[ n++ ] = to_index;
            }
         }
      }
      if ( to_current != to_base ) to_order[ n++ ] = to_current;

/* Loop to inspect each comma-separated field in the domains list
   until an error occurs, all the domains are used up, or a match is
   found. */
      domain = domainlist_copy;
      match = 0;
      while ( astOK && domain && !match ) {

/* Change the comma at the end of each field to a null to terminate
   the domain. */
         if ( ( domain_end = strchr( domain, ',' ) ) ) *domain_end = '\0';

/* For any given domain, we will ignore imperfect matches in favour of
   better ones by assigning a score to each match. Initialise the best
   score value for the current domain. */
         best_score = -1;

/* Loop through each Frame in "to". Quit looping early if an error
   occurs or a perfect match is found. */
         perfect = 0;
         for ( to_number = 0;
               astOK && !perfect && ( to_number < to_nframe );
               to_number++ ) {

/* Permute the "to" Frame number into a Frame index to implement the
   required search order, and obtain a pointer to the required "to"
   Frame. */
            to_index = to_order[ to_number ];
            to_frame = to_isframe ? astClone( to ) :
                                    astGetFrame( to, to_index );

/* Loop through each Frame in "from". Quit looping early if an error
   occurs or a perfect match is found. */
            for ( from_number = 0;
                  astOK && !perfect && ( from_number < from_nframe );
                  from_number++ ) {

/* Permute the "from" Frame number into a Frame index to implement the
   required search order, and obtain a pointer to the required "from"
   Frame. */
               from_index = from_order[ from_number ];
               from_frame = from_isframe ? astClone( from ) :
                                           astGetFrame( from, from_index );

/* Attempt to obtain a FrameSet which describes the conversion between
   the selected "from" and "to" Frames and test if successful. If so,
   we have a potential route to construct the overall Mapping we
   want. */
               cvt = astConvert( from_frame, to_frame, domain );
               if ( astOK && cvt ) {

/* Extract the required Mapping from the returned FrameSet. */
                  map = astGetMapping( cvt, AST__BASE, AST__CURRENT );

/* If necessary, prefix the Mapping between the "from" current Frame
   and the individual "from" Frame we have selected. */
                  if ( from_index != from_current ) {
                     from_map = astGetMapping( from, AST__CURRENT,
                                               from_index );
                     tmp = (AstMapping *) astCmpMap( from_map, map, 1, "", status );
                     from_map = astAnnul( from_map );
                     map = astAnnul( map );
                     map = tmp;
                  }

/* Similarly, if necessary, append the Mapping between the selected
   "to" Frame and the "to" current Frame. */
                  if ( to_index != to_current ) {
                     to_map = astGetMapping( to, to_index, AST__CURRENT );
                     tmp = (AstMapping *) astCmpMap( map, to_map, 1, "", status );
                     to_map = astAnnul( to_map );
                     map = astAnnul( map );
                     map = tmp;
                  }

/* Simplify the resulting overall Mapping (this is done here because
   it may sometimes affect the attribute values used to assign a score
   below). */
                  tmp = astSimplify( map );
                  map = astAnnul( map );
                  map = tmp;

/* Assign a score that favours Mappings with both transformations
   available over those with only one, and Mappings with only a
   forward transformation over those with only an inverse
   transformation. */
                  score = ( astGetTranForward( map ) ? 2 : 0 ) +
                          ( astGetTranInverse( map ) ? 1 : 0 );

/* If the new score is better than the previous one (or is the first
   one), note that we have a possible match. */
                  if ( astOK && ( score > best_score ) ) {
                     match = 1;

/* Update the best score and note if it indicates a perfect match (in
   which case we can stop searching at this point). */
                     best_score = score;
                     perfect = ( best_score >= 3 );

/* Annul any previous result Mapping pointer and replace it with this
   better one. */
                     if ( result_map ) result_map = astAnnul( result_map );
                     result_map = astClone( map );

/* Note which "from" and "to" Frames were used. */
                     iframe_from = from_index;
                     iframe_to = to_index;
                  }

/* Annul pointers to the intermediate Objects. */
                  map = astAnnul( map );
                  cvt = astAnnul( cvt );
               }
               from_frame = astAnnul( from_frame );
            }
            to_frame = astAnnul( to_frame );
         }

/* Go on to consider the next field in the domains list. */
         domain = domain_end ? domain_end + 1 : NULL;
      }
   }

/* Free the memory allocated for temporary arrays. */
   domainlist_copy = astFree( domainlist_copy );
   from_order = astFree( from_order );
   to_order = astFree( to_order );

/* If a result is being returned, then obtain a pointer to the current
   "from" Frame and use it to start constructing the result
   FrameSet. */
   if ( result_map ) {
      from_frame = from_isframe ? astClone( from ) :
                                  astGetFrame( from, AST__CURRENT );
      result = astFrameSet( from_frame, "", status );
      from_frame = astAnnul( from_frame );

/* Similarly. obtain a pointer to the current "to" frame and add it to
   the result FrameSet (related to the base Frame by the result
   Mapping). */
      to_frame = to_isframe ? astClone( to ) :
                              astGetFrame( to, AST__CURRENT );
      astAddFrame( result, AST__BASE, result_map, to_frame );
      to_frame = astAnnul( to_frame );

/* Annul the result Mapping pointer. */
      result_map = astAnnul( result_map );
   }

/* If successful, and a FrameSet is being returned, then set the base
   Frames of "from" and "to" (if they are FrameSets) to indicate the
   route used to generate the result Mapping. */
   if ( astOK && result ) {
      if ( !from_isframe ) astSetBase( from, iframe_from );
      if ( !to_isframe ) astSetBase( to, iframe_to );
   }

/* If an error occurred, annul the returned FrameSet pointer. */
   if ( !astOK && result ) result = astAnnul( result );

/* Return the result. */
   return result;
}

static double Distance( AstFrame *this_frame,
                        const double point1[], const double point2[], int *status ) {
/*
*  Name:
*     Distance

*  Purpose:
*     Calculate the distance between two points.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     double Distance( AstFrame *this,
*                      const double point1[], const double point2[], int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astDistance
*     method inherited from the Frame class).

*  Description:
*     This function finds the distance between two points whose
*     FrameSet coordinates are given. The distance calculated is that
*     along the geodesic curve that joins the two points.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     point1
*        An array of double, with one element for each FrameSet axis
*        containing the coordinates of the first point.
*     point2
*        An array of double, with one element for each FrameSet axis
*        containing the coordinates of the second point.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The distance between the two points.

*  Notes:
*     - This function will return a "bad" result value (AST__BAD) if
*     any of the input coordinates has this value.
*     - A "bad" value will also be returned if this function is
*     invoked with the AST error status set or if it should fail for
*     any reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   double result;                /* Value to return */

/* Check the global error status. */
   if ( !astOK ) return AST__BAD;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astDistance method. Annul the Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astDistance( fr, point1, point2 );
   fr = astAnnul( fr );

/* If an error occurred, clear the result. */
   if ( !astOK ) result = AST__BAD;

/* Return the result. */
   return result;
}

static int Equal( AstObject *this_object, AstObject *that_object, int *status ) {
/*
*  Name:
*     Equal

*  Purpose:
*     Test if two FrameSets are equivalent.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int Equal( AstObject *this, AstObject *that, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astEqual protected
*     method inherited from the Mapping class).

*  Description:
*     This function returns a boolean result (0 or 1) to indicate whether
*     two FrameSets are equivalent.

*  Parameters:
*     this
*        Pointer to the first FrameSet.
*     that
*        Pointer to the second FrameSet.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     One if the FrameSets are equivalent, zero otherwise.

*  Notes:
*     - The two FrameSets are considered equivalent if all the encapsulated
*     Frames are equal and all the encapsulated Mappings are equal.
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*/

/* Local Variables: */
   AstFrameSet *that;            /* Pointer to the second FrameSet structure */
   AstFrameSet *this;            /* Pointer to the first FrameSet structure */
   int i;                        /* Loop index */
   int result;                   /* Result value to return */

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Checks that the second object is of the same class as the first . */
   if( !strcmp( astGetClass( this_object ), astGetClass( that_object ) ) ){

/* Obtain pointers to the two FrameSet structures. */
      this = (AstFrameSet *) this_object;
      that = (AstFrameSet *) that_object;

/* Check the number of nodes and frames are equal. Also check the indices
   of the base and current Frames are equal */
      if( this->nframe == that->nframe &&
          this->nnode == that->nnode &&
          this->base == that->base &&
          this->current == that->current ) {

/* Check the Frames and nodes are equal. */
          result = 1;
          for ( i = 0; i < this->nframe; i++ ) {
             if( !astEqual( this->frame[ i ], that->frame[ i ] ) ||
                 this->node[ i ] != that->node[ i ] ){
                result = 0;
                break;
             }
          }

/* Check the Mappings, links and invert flags are equal. */
         if( result ) {
            for ( i = 0; i < this->nnode - 1; i++ ) {
               if( !astEqual( this->map[ i ], that->map[ i ] ) ||
                   this->link[ i ] != that->link[ i ] ||
                   this->invert[ i ] != that->invert[ i ] ) {
                  result = 0;
                  break;
               }
            }
         }
      }
   }

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = 0;

/* Return the result, */
   return result;
}

static int Fields( AstFrame *this_frame, int axis, const char *fmt,
                   const char *str, int maxfld, char **fields,
                   int *nc, double *val, int *status ) {
/*
*+
*  Name:
*     astFields

*  Purpose:
*     Identify numerical fields within a formatted FrameSet axis value.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "frameset.h"
*     int astFields( AstFrame *this, int axis, const char *fmt,
*                    const char *str, int maxfld, char **fields,
*                    int *nc, double *val )

*  Class Membership:
*     FrameSet member function (over-rides the protected astFields
*     method inherited from the Frame class).

*  Description:
*     This function identifies the numerical fields within a FrameSet axis
*     value that has been formatted using astAxisFormat. It assumes that
*     the value was formatted using the supplied format string. It also
*     returns the equivalent floating point value.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     axis
*        The number of the FrameSet axis for which the values have been
*        formatted (axis numbering starts at zero for the first axis).
*     fmt
*        Pointer to a constant null-terminated string containing the
*        format used when creating "str".
*     str
*        Pointer to a constant null-terminated string containing the
*        formatted value.
*     maxfld
*        The maximum number of fields to identify within "str".
*     fields
*        A pointer to an array of at least "maxfld" character pointers.
*        Each element is returned holding a pointer to the start of the
*        corresponding field  in "str" (in the order in which they occur
*        within "str"), or NULL if no corresponding field can be found.
*     nc
*        A pointer to an array of at least "maxfld" integers. Each
*        element is returned holding the number of characters in the
*        corresponding field, or zero if no corresponding field can be
*        found.
*     val
*        Pointer to a location at which to store the value
*        equivalent to the returned field values. If this is NULL,
*        it is ignored.

*  Returned Value:
*     The number of fields succesfully identified and returned.

*  Notes:
*     - Leading and trailing spaces are ignored.
*     - If the formatted value is not consistent with the supplied format
*     string, then a value of zero will be returned, "fields" will be
*     returned holding NULLs, "nc" will be returned holding zeros, and
*     "val" is returned holding VAL__BAD.
*     - Fields are counted from the start of the formatted string. If the
*     string contains more than "maxfld" fields, then trailing fields are
*     ignored.
*     - If this function is invoked with the global error status set, or
*     if it should fail for any reason, then a value of zero will be returned
*     as the function value, and "fields", "nc" and "val"  will be returned
*     holding their supplied values
*-
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   int result;                   /* Result field count to return */

/* Check the global error status. */
   if ( !astOK ) return 0;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index. */
   (void) astValidateAxis( this, axis, 1, "astFields" );

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astFields method to perform the processing. Annul the Frame
   pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astFields( fr, axis, fmt, str, maxfld, fields, nc, val );
   fr = astAnnul( fr );

/* If an error occurred, clear the result. */
   if ( !astOK ) result = 0;

/* Return the result. */
   return result;
}

static AstFrameSet *FindFrame( AstFrame *target_frame, AstFrame *template,
                               const char *domainlist, int *status ) {
/*
*  Name:
*     FindFrame

*  Purpose:
*     Find a coordinate system with specified characteristics.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstFrameSet *FindFrame( AstFrame *target, AstFrame *template,
*                             const char *domainlist, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astFindFrame method
*     inherited from the Frame class).

*  Description:
*     This function uses a "template" Frame to search a FrameSet to
*     identify a coordinate system which has a specified set of
*     characteristics. If a suitable coordinate system can be found,
*     the function returns a pointer to a FrameSet which describes the
*     required coordinate system and how to convert coordinates to and
*     from it.

*  Parameters:
*     target
*        Pointer to the target FrameSet.  Note that if a suitable
*        coordinate system is found, then the FrameSet's Current
*        attribute will be modified to indicate which Frame was used
*        to obtain attribute values which were not specified by the
*        template.
*     template
*        Pointer to the template Frame, which should be an instance of
*        the type of Frame you wish to find.
*     domainlist
*        Pointer to a null-terminated character string containing a
*        comma-separated list of Frame domains. This may be used to
*        establish a priority order for the different types of
*        coordinate system that might be found.
*
*        The function will first try to find a suitable coordinate
*        system whose Domain attribute equals the first domain in this
*        list. If this fails, the second domain in the list will be
*        used, and so on, until a result is obtained. A blank domain
*        (e.g. two consecutive commas) indicates that any coordinate
*        system is acceptable (subject to the template) regardless of
*        its domain.
*
*        This list is case-insensitive and all white space is ignored.
*        If you do not wish to restrict the domain in this way, you
*        should supply an empty string.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     If the search is successful, the function returns a pointer to a
*     FrameSet which contains the Frame found and a description of how
*     to convert to (and from) the coordinate system it
*     represents. Otherwise, a null Object pointer (AST__NULL) is
*     returned without error.
*
*     If a FrameSet is returned, it will contain two Frames. Frame
*     number 1 (its base Frame) represents the target coordinate
*     system and will be the same as the (base Frame of the)
*     target. Frame number 2 (its current Frame) will be a Frame
*     representing the coordinate system which the function found. The
*     Mapping which inter-relates these two Frames will describe how
*     to convert between their respective coordinate systems.

*  Notes:
*     - A null Object pointer (AST__NULL) will be returned if this
*     function is invoked with the AST error status set, or if it
*     should fail for any reason.
*/

/* Local Variables: */
   AstFrame *base_frame;         /* Pointer to target base Frame */
   AstFrame *frame;              /* Pointer to result Frame */
   AstFrame *selected_frame;     /* Pointer to selected target Frame */
   AstFrameSet *found;           /* FrameSet pointer (result of search) */
   AstFrameSet *result;          /* Pointer to result FrameSet */
   AstFrameSet *target;          /* Pointer to target FrameSet structure */
   AstMapping *map;              /* Pointer to result Mapping */
   AstMapping *prefix;           /* Pointer to prefix Mapping */
   AstMapping *tmp;              /* Temporary Mapping pointer */
   char *domain;                 /* Pointer to individual domain field */
   char *domain_end;             /* Pointer to null at end of domain */
   char *domainlist_copy;        /* Pointer to copy of domains list */
   int *target_order;            /* Array of indices defining search order */
   int match;                    /* Match obtained? */
   int n;                        /* Count of target_order elements */
   int target_base;              /* Index of target base Frame */
   int target_current;           /* Index of target current Frame */
   int target_index;             /* Index of selected target Frame */
   int target_nframe;            /* Number of Frames in target FrameSet */
   int target_number;            /* Loop index for search */

/* Initialise. */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Initialise variables to avoid "used of uninitialised variable"
   messages from dumb compilers. */
   target_index = 0;

/* Obtain a pointer to the target FrameSet structure. */
   target = (AstFrameSet *) target_frame;

/* Determine the number of Frames in the target FrameSet and the
   indices of the current and base Frames. */
   target_nframe = astGetNframe( target );
   target_current = astGetCurrent( target );
   target_base = astGetBase( target );

/* Allocate an array to hold a list of all the target Frame indices. */
   target_order = astMalloc( sizeof( int ) * (size_t) target_nframe );

/* Make a temporary copy of the domains list. */
   domainlist_copy = astStore( NULL, domainlist,
                               strlen( domainlist ) + (size_t) 1 );
   if ( astOK ) {

/* Form a list of the indices of all the Frames in the target in the
   order they will be searched for a match. Add the current Frame
   index first. */
      n = 0;
      target_order[ n++ ] = target_current;

/* Follow this by the base Frame index, if different. */
      if ( target_base != target_current ) target_order[ n++ ] = target_base;

/* Then add all the remaining target Frame indices. */
      for ( target_index = 1; target_index <= target_nframe; target_index++ ) {
         if ( ( target_index != target_current ) &&
              ( target_index != target_base ) ) {
            target_order[ n++ ] = target_index;
         }
      }

/* Loop to inspect each comma-separated field in the domains list
   until an error occurs, all the domains are used up, or a match is
   found. */
      domain = domainlist_copy;
      match = 0;
      while ( astOK && domain && !match ) {

/* Change the comma at the end of each field to a null to terminate
   the domain. */
         if ( ( domain_end = strchr( domain, ',' ) ) ) *domain_end = '\0';

/* Loop to try and match each target Frame in turn, in the order
   identified above. Quit the loop early if an error occurs or a match
   is found. */
         for ( target_number = 0;
               astOK && !match && ( target_number < target_nframe );
               target_number++ ) {

/* Permute the target Frame number into a Frame index to implement the
   required search order. Then obtain a pointer to the selected target
   Frame. */
            target_index = target_order[ target_number ];
            selected_frame = astGetFrame( target, target_index );

/* Search the target Frame using the template supplied, together with
   the current domain. */
            found = astFindFrame( selected_frame, template, domain );

/* Note if a match is found, and extract pointers to the conversion
   Mapping and the result Frame from the FrameSet produced. */
            if ( astOK && found ) {
               match = 1;
               map = astGetMapping( found, AST__BASE, AST__CURRENT );
               frame = astGetFrame( found, AST__CURRENT );

/* Obtain a pointer to the Mapping between the target base Frame and
   the selected target Frame, and prefix this Mapping to the one
   obtained above. */
               prefix = astGetMapping( target, AST__BASE, target_index );
               tmp = (AstMapping *) astCmpMap( prefix, map, 1, "", status );
               prefix = astAnnul( prefix );
               map = astAnnul( map );
               map = tmp;

/* Simplify the resulting Mapping. */
               tmp = astSimplify( map );
               map = astAnnul( map );
               map = tmp;

/* Obtain a pointer to the target base Frame, and use this to start
   building the result FrameSet. */
               base_frame = astGetFrame( target, AST__BASE );
               result = astFrameSet( base_frame, "", status );
               base_frame = astAnnul( base_frame );

/* Add the result Frame, which is related to the base Frame by the
   result Mapping. */
               astAddFrame( result, AST__BASE, map, frame );

/* Annul pointers to all intermediate Objects. */
               map = astAnnul( map );
               frame = astAnnul( frame );
               found = astAnnul( found );
            }
            selected_frame = astAnnul( selected_frame );
         }

/* Go on to consider the next field in the domains list. */
         domain = domain_end ? domain_end + 1 : NULL;
      }
   }

/* Free the temporary arrays. */
   target_order = astFree( target_order );
   domainlist_copy = astFree( domainlist_copy );

/* If a result is being returned, set the current Frame of the target
   to indicate where the result Frame was found. */
   if ( astOK && result ) astSetCurrent( target, target_index );

/* If an error occurred, annul any result FrameSet pointer. */
   if ( !astOK && result ) result = astAnnul( result );

/* Return the result. */
   return result;
}

static AstPointSet *FrameGrid( AstFrame *this_frame, int size, const double *lbnd,
                               const double *ubnd, int *status ){
/*
*  Name:
*     FrameGrid

*  Purpose:
*     Return a grid of points covering a rectangular area of a Frame.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstPointSet *FrameGrid( AstFrame *this_frame, int size,
*                             const double *lbnd, const double *ubnd,
*                             int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astFrameGrid
*     method inherited from the Frame class).

*  Description:
*     This function returns a PointSet containing positions spread
*     approximately evenly throughtout a specified rectangular area of
*     the Frame.

*  Parameters:
*     this
*        Pointer to the Frame.
*     size
*        The preferred number of points in the returned PointSet. The
*        actual number of points in the returned PointSet may be
*        different, but an attempt is made to stick reasonably closely to
*        the supplied value.
*     lbnd
*        Pointer to an array holding the lower bound of the rectangular
*        area on each Frame axis. The array should have one element for
*        each Frame axis.
*     ubnd
*        Pointer to an array holding the upper bound of the rectangular
*        area on each Frame axis. The array should have one element for
*        each Frame axis.

*  Returned Value:
*     A pointer to a new PointSet holding the grid of points.

*  Notes:
*     - A NULL pointer is returned if an error occurs.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   AstPointSet *result;          /* Value to return */

/* Check the global error status. */
   if ( !astOK ) return NULL;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astFrameGrid method. Annul the Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astFrameGrid( fr, size, lbnd, ubnd );
   fr = astAnnul( fr );

/* If an error occurred, clear the result. */
   if ( !astOK ) result = astAnnul( result );

/* Return the result. */
   return result;
}

static int ForceCopy( AstFrameSet *this, int iframe, int *status ) {
/*
*  Name:
*     ForceCopy

*  Purpose:
*     Force a copy to be made of a Frame, if necessary.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int ForceCopy( AstFrameSet *this, int iframe, int *status )

*  Class Membership:
*     FrameSet member function.

*  Description:
*     This function examines a Frame within a FrameSet, identified by its
*     Frame index. If the same Frame is found to be referenced a second time
*     within the FrameSet, then the original reference is replaced with an
*     independent copy of the Frame.
*
*     This process supports the preservation of FrameSet integrity in cases
*     where the same Frame is referenced more than once. After using this
*     function, the nominated Frame's attributes may be modified without
*     danger of affecting other parts of the FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     iframe
*        The index of the Frame to be examined.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     One if a copy of the nominated Frame was made, otherwise zero.

*  Notes:
*     - Using this function a second time on the same Frame will have no
*     effect, since the first usage will make the Frame independent of any
*     other Frames within the FrameSet.
*     - A value of zero will be returned if this function is invoked with
*     the global error status set, or if it should fail for any reason.
*/

/* Local Variables: */
   astDECLARE_GLOBALS           /* Declare the thread specific global data */
   AstFrame *frame;             /* Pointer to Frame */
   AstFrame *tmp;               /* Temporary Frame pointer */
   int ifr;                     /* Loop counter for Frames */
   int result;                  /* Value to return */

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get a pointer to the structure holding thread-specific global data. */
   astGET_GLOBALS(this);

/* Validate and translate the Frame index supplied. */
   iframe = astValidateFrameIndex( this, iframe, integrity_method );

/* If OK, obtain the corresponding Frame pointer (don't clone it). */
   if ( astOK ) {
      frame = this->frame[ iframe - 1 ];

/* Loop to inspect each Frame in the FrameSet. */
      for ( ifr = 1; ifr <= this->nframe; ifr++ ) {

/* If the same Frame is referenced anywhere else, then make a copy of it. */
         if ( ( ifr != iframe ) && ( this->frame[ ifr - 1 ] == frame ) ) {
            tmp = astCopy( frame );

/* If successful, replace the original reference to the Frame with a pointer
   to this copy and annul the original pointer. */
            if ( astOK ) {
               this->frame[ iframe - 1 ] = tmp;
               frame = astAnnul( frame );

/* Set the returned result. */
               if ( astOK ) result = 1;
            }

/* Quit looping once a copy has been made. */
            break;
         }
      }
   }

/* Return the result. */
   return result;
}

static const char *Format( AstFrame *this_frame, int axis, double value, int *status ) {
/*
*  Name:
*     Format

*  Purpose:
*     Format a coordinate value for a FrameSet axis.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     const char *Format( AstFrame *this, int axis, double value, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astFormat method
*     inherited from the Frame class).

*  Description:
*     This function returns a pointer to a string containing the
*     formatted (character) version of a coordinate value for a
*     FrameSet axis. The formatting applied is that specified by a
*     previous invocation of the astSetFormat method. A suitable
*     default format is applied if necessary.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     axis
*        The number of the axis (zero-based) for which formatting is
*        to be performed.
*     value
*        The coordinate value to be formatted.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     A pointer to a null-terminated string containing the formatted
*     value.

*  Notes:
*     - The returned string pointer may point at memory allocated
*     within the FrameSet object, or at static memory. The contents of
*     the string may be over-written or the pointer may become invalid
*     following a further invocation of the same function or deletion
*     of the FrameSet. A copy of the string should therefore be made
*     if necessary.
*     - A NULL pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   const char *result;           /* Pointer value to return */

/* Check the global error status. */
   if ( !astOK ) return NULL;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index. */
   (void) astValidateAxis( this, axis, 1, "astFormat" );

/* Obtain a pointer to the FrameSet's current Frame and invoke the
   astFormat method for this Frame. Annul the Frame pointer
   afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astFormat( fr, axis, value );
   fr = astAnnul( fr );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = NULL;

/* Return the result. */
   return result;
}

static double Gap( AstFrame *this_frame, int axis, double gap, int *ntick, int *status ) {
/*
*  Name:
*     Gap

*  Purpose:
*     Find a "nice" gap for tabulating FrameSet axis values.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     double Gap( AstFrame *this, int axis, double gap, int *ntick, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astGap method
*     inherited from the Frame class).

*  Description:
*     This function returns a gap size which produces a nicely spaced
*     series of formatted values for a FrameSet axis, the returned gap
*     size being as close as possible to the supplied target gap
*     size. It also returns a convenient number of divisions into
*     which the gap can be divided.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     axis
*        The number of the axis (zero-based) for which a gap is to be found.
*     gap
*        The target gap size.
*     ntick
*        Address of an int in which to return a convenient number of
*        divisions into which the gap can be divided.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The nice gap size.

*  Notes:
*     - A value of zero is returned if the target gap size is zero.
*     - A negative gap size is returned if the supplied gap size is negative.
*     - A value of zero will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   double result;                /* Gap value to return */

/* Check the global error status. */
   if ( !astOK ) return 0.0;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index. */
   (void) astValidateAxis( this, axis, 1, "astGap" );

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astGap method to obtain the required gap value. Annul the
   Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astGap( fr, axis, gap, ntick );
   fr = astAnnul( fr );

/* If an error occurred, clear the result. */
   if ( !astOK ) result = 0.0;

/* Return the result. */
   return result;
}

static int GetObjSize( AstObject *this_object, int *status ) {
/*
*  Name:
*     GetObjSize

*  Purpose:
*     Return the in-memory size of an Object.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int GetObjSize( AstObject *this, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astGetObjSize protected
*     method inherited from the parent class).

*  Description:
*     This function returns the in-memory size of the supplied FrameSet,
*     in bytes.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The Object size, in bytes.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*/

/* Local Variables: */
   AstFrameSet *this;         /* Pointer to FrameSet structure */
   int result;                /* Result value to return */
   int iframe;                /* Loop counter for Frames */
   int inode;                 /* Loop counter for nodes */

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Obtain a pointers to the FrameSet structure. */
   this = (AstFrameSet *) this_object;

/* Invoke the GetObjSize method inherited from the parent class, and then
   add on any components of the class structure defined by thsi class
   which are stored in dynamically allocated memory. */
   result = (*parent_getobjsize)( this_object, status );

   for ( iframe = 0; iframe < this->nframe; iframe++ ) {
      result += astGetObjSize( this->frame[ iframe ] );
   }

   for ( inode = 0; inode < this->nnode - 1; inode++ ) {
      result += astGetObjSize( this->map[ inode ] );
   }

   result += astTSizeOf( this->frame );
   result += astTSizeOf( this->varfrm );
   result += astTSizeOf( this->node );
   result += astTSizeOf( this->map );
   result += astTSizeOf( this->link );
   result += astTSizeOf( this->invert );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = 0;

/* Return the result, */
   return result;
}

static const char *GetAttrib( AstObject *this_object, const char *attrib, int *status ) {
/*
*  Name:
*     GetAttrib

*  Purpose:
*     Get the value of a specified attribute for a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     const char *GetAttrib( AstObject *this, const char *attrib, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astGetAttrib
*     method inherited from the Frame class).

*  Description:
*     This function returns a pointer to the value of a specified
*     attribute for a FrameSet, formatted as a character string.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     attrib
*        Pointer to a null-terminated string containing the name of
*        the attribute whose value is required. This name should be in
*        lower case, with all white space removed.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     - Pointer to a null-terminated string containing the attribute
*     value.

*  Notes:
*     - The returned string pointer may point at memory allocated
*     within the FrameSet, or at static memory. The contents of the
*     string may be over-written or the pointer may become invalid
*     following a further invocation of the same function or any
*     modification of the FrameSet. A copy of the string should
*     therefore be made if necessary.
*     - A NULL pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Declare the thread specific global data */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   const char *result;           /* Pointer value to return */
   int base;                     /* Base attribute value */
   int current;                  /* Current attribute value */
   int invert;                   /* Invert attribute value */
   int nframe;                   /* Nframe attribute value */
   int nin;                      /* Nin attribute value */
   int nobject;                  /* Nobject attribute value */
   int nout;                     /* Nout attribute value */
   int ref_count;                /* RefCount attribute value */
   int report;                   /* Report attribute value */
   int tranforward;              /* TranForward attribute value */
   int traninverse;              /* TranInverse attribute value */

/* Initialise. */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get a pointer to the structure holding thread-specific global data. */
   astGET_GLOBALS(this_object);

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_object;

/* Compare "attrib" with each recognised attribute name in turn,
   obtaining the value of the required attribute. If necessary, write
   the value into "getattrib_buff" as a null-terminated string in an appropriate
   format.  Set "result" to point at the result string. */

/* We first handle attributes that apply to the FrameSet as a whole
   (rather than to the current Frame). */

/* AllVariants. */
/* ------------ */
   if ( !strcmp( attrib, "allvariants" ) ) {
      result = astGetAllVariants( this );

/* Base. */
/* ----- */
   } else if ( !strcmp( attrib, "base" ) ) {
      base = astGetBase( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", base );
         result = getattrib_buff;
      }

/* Class. */
/* ------ */
   } else if ( !strcmp( attrib, "class" ) ) {
      result = astGetClass( this );

/* Current. */
/* -------- */
   } else if ( !strcmp( attrib, "current" ) ) {
      current = astGetCurrent( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", current );
         result = getattrib_buff;
      }

/* ID. */
/* --- */
   } else if ( !strcmp( attrib, "id" ) ) {
      result = astGetID( this );

/* Ident. */
/* ------ */
   } else if ( !strcmp( attrib, "ident" ) ) {
      result = astGetIdent( this );

/* Invert. */
/* ------- */
   } else if ( !strcmp( attrib, "invert" ) ) {
      invert = astGetInvert( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", invert );
         result = getattrib_buff;
      }

/* Nframe. */
/* ------- */
   } else if ( !strcmp( attrib, "nframe" ) ) {
      nframe = astGetNframe( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", nframe );
         result = getattrib_buff;
      }

/* Nin. */
/* ---- */
   } else if ( !strcmp( attrib, "nin" ) ) {
      nin = astGetNin( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", nin );
         result = getattrib_buff;
      }

/* Nobject. */
/* -------- */
   } else if ( !strcmp( attrib, "nobject" ) ) {
      nobject = astGetNobject( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", nobject );
         result = getattrib_buff;
      }

/* Nout. */
/* ----- */
   } else if ( !strcmp( attrib, "nout" ) ) {
      nout = astGetNout( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", nout );
         result = getattrib_buff;
      }

/* RefCount. */
/* --------- */
   } else if ( !strcmp( attrib, "refcount" ) ) {
      ref_count = astGetRefCount( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", ref_count );
         result = getattrib_buff;
      }

/* Report. */
/* ------- */
   } else if ( !strcmp( attrib, "report" ) ) {
      report = astGetReport( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", report );
         result = getattrib_buff;
      }

/* TranForward. */
/* ------------ */
   } else if ( !strcmp( attrib, "tranforward" ) ) {
      tranforward = astGetTranForward( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", tranforward );
         result = getattrib_buff;
      }

/* TranInverse. */
/* ------------ */
   } else if ( !strcmp( attrib, "traninverse" ) ) {
      traninverse = astGetTranInverse( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", traninverse );
         result = getattrib_buff;
      }

/* Variant. */
/* -------- */
   } else if ( !strcmp( attrib, "variant" ) ) {
      result = astGetVariant( this );

/* Pass unrecognised attributes on to the FrameSet's current Frame for
   further interpretation. */
   } else {

/* Obtain a pointer to the current Frame and invoke its astGetAttrib
   method. Annul the Frame pointer afterwards. */
      fr = astGetFrame( this, AST__CURRENT );
      result = astGetAttrib( fr, attrib );
      fr = astAnnul( fr );
   }

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = NULL;

/* Return the result. */
   return result;
}

static AstAxis *GetAxis( AstFrame *this_frame, int axis, int *status ) {
/*
*  Name:
*     GetAxis

*  Purpose:
*     Obtain a pointer to a specified Axis from a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstAxis *GetAxis( AstFrame *this, int axis, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astGetAxis method
*     inherited from the Frame class).

*  Description:
*     This function returns a pointer to the Axis object associated
*     with one of the axes of the current Frame of a FrameSet. This
*     object describes the quantity which is represented along that
*     axis.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     axis
*        The number of the axis (zero-based) for which an Axis pointer
*        is required.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     A pointer to the requested Axis object.

*  Notes:
*     - The reference count of the requested Axis object will be
*     incremented by one to reflect the additional pointer returned by
*     this function.
*     - A NULL pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstAxis *result;              /* Pointer to Axis */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */

/* Check the global error status. */
   if ( !astOK ) return NULL;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index. */
   (void) astValidateAxis( this, axis, 1, "astGetAxis" );

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astGetAxis method to obtain the required Axis
   pointer. Annul the Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astGetAxis( fr, axis );
   fr = astAnnul( fr );

/* If an error occurred, annul the result. */
   if ( !astOK ) result = astAnnul( result );

/* Return the result. */
   return result;
}

static int GetBase( AstFrameSet *this, int *status ) {
/*
*+
*  Name:
*     astGetBase

*  Purpose:
*     Obtain the value of the Base attribute for a FrameSet.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "frameset.h"
*     int astGetBase( AstFrameSet *this )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function returns the value of the Base attribute for a
*     FrameSet. This value is an index that identifies the base Frame
*     in the FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.

*  Returned Value:
*     The Base attribute value.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*-
*/

/* Local Variables: */
   int invert;                   /* FrameSet is inverted? */
   int result;                   /* Value to return */

/* Initialise */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Determine if the FrameSet has been inverted. */
   invert = astGetInvert( this );

/* If it has not been inverted, return the base Frame index, otherwise
   return the index of the current Frame instead. Provide defaults if
   necessary. */
   if ( astOK ) {
      if ( !invert ) {
         result = ( this->base != -INT_MAX ) ? this->base : 1;
      } else {
         result = ( this->current != -INT_MAX ) ? this->current :
                                                  astGetNframe( this );
      }
   }

/* If an error occurred, clear the result. */
   if ( !astOK ) result = 0;

/* Return the result. */
   return result;
}

static int GetCurrent( AstFrameSet *this, int *status ) {
/*
*+
*  Name:
*     astGetCurrent

*  Purpose:
*     Obtain the value of the Current attribute for a FrameSet.

*  Type:
*     Protected function.

*  Synopsis:
*     #include "frameset.h"
*     int astGetCurrent( AstFrameSet *this )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function returns the value of the Current attribute for a
*     FrameSet.  This attribute is an index that identifies the
*     current Frame in a FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.

*  Returned Value:
*     Value of the Current attribute.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*-
*/

/* Local Variables: */
   int invert;                   /* FrameSet is inverted? */
   int result;                   /* Value to return */

/* Initialise */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Determine if the FrameSet has been inverted. */
   invert = astGetInvert( this );

/* If it has not been inverted, return the current Frame index,
   otherwise return the index of the base Frame instead. Provide
   defaults if necessary. */
   if ( astOK ) {
      if ( !invert ) {
         result = ( this->current != -INT_MAX ) ? this->current :
                                                  astGetNframe( this );
      } else {
         result = ( this->base != -INT_MAX ) ? this->base : 1;
      }
   }

/* If an error occurred, clear the result. */
   if ( !astOK ) result = 0;

/* Return the result. */
   return result;
}

static AstFrame *GetFrame( AstFrameSet *this, int iframe, int *status ) {
/*
*++
*  Name:
c     astGetFrame
f     AST_GETFRAME

*  Purpose:
*     Obtain a pointer to a specified Frame in a FrameSet.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "frameset.h"
c     AstFrame *astGetFrame( AstFrameSet *this, int iframe )
f     RESULT = AST_GETFRAME( THIS, IFRAME, STATUS )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function returns a pointer to a specified Frame in a
*     FrameSet.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the FrameSet.
c     iframe
f     IFRAME = INTEGER (Given)
*        The index of the required Frame within the FrameSet.  This
*        value should lie in the range from 1 to the number of Frames
*        in the FrameSet (as given by its Nframe attribute).
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Returned Value:
c     astGetFrame()
f     AST_GETFRAME = INTEGER
*        A pointer to the requested Frame.

*  Notes:
*     - A value of AST__BASE or AST__CURRENT may be given for the
c     "iframe" parameter to specify the base Frame or the current
f     IFRAME argument to specify the base Frame or the current
*     Frame respectively.
*     - This function increments the RefCount attribute of the
*     selected Frame by one.
*     - A null Object pointer (AST__NULL) will be returned if this
c     function is invoked with the AST error status set, or if it
f     function is invoked with STATUS set to an error value, or if it
*     should fail for any reason.
*--
*/

/* Local Variables: */
   AstFrame *result;             /* Pointer value to return */

/* Initialise. */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Validate and translate the Frame index supplied. */
   iframe = astValidateFrameIndex( this, iframe, "astGetFrame" );

/* If OK, clone a pointer to the requested Frame. */
   if ( astOK ) result = astClone( this->frame[ iframe - 1 ] );

/* Return the result. */
   return result;
}

static int GetIsLinear( AstMapping *this_mapping, int *status ){
/*
*  Name:
*     GetIsLinear

*  Purpose:
*     Return the value of the IsLinear attribute for a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "mapping.h"
*     void GetIsLinear( AstMapping *this, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astGetIsLinear
*     method inherited from the Mapping class).

*  Description:
*     This function returns the value of the IsLinear attribute for a
*     FrameSet, which is the IsLinear value of he base->current Mapping.

*  Parameters:
*     this
*        Pointer to the Frame.
*     status
*        Pointer to the inherited status variable.
*/
/* Local Variables: */
   AstMapping *map;
   int result;

/* Check global status */
   if( !astOK ) return 0;

/* Get the Mapping. */
   map = astGetMapping( (AstFrameSet *) this_mapping, AST__BASE,
                        AST__CURRENT );

/* Get its IsLinear attribute value. */
   result = astGetIsLinear( map );

/* Free the Mapping. */
   map = astAnnul( map );

/* Return the result. */
   return result;
}

static AstMapping *GetMapping( AstFrameSet *this, int iframe1, int iframe2, int *status ) {
/*
*++
*  Name:
c     astGetMapping
f     AST_GETMAPPING

*  Purpose:
*     Obtain a Mapping that converts between two Frames in a FrameSet.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "frameset.h"
c     AstMapping *astGetMapping( AstFrameSet *this, int iframe1, int iframe2 )
f     RESULT = AST_GETMAPPING( THIS, IFRAME1, IFRAME2, STATUS )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function returns a pointer to a Mapping that will convert
*     coordinates between the coordinate systems represented by two
*     Frames in a FrameSet.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the FrameSet.
c     iframe1
f     IFRAME1 = INTEGER (Given)
*        The index of the first Frame in the FrameSet. This Frame describes
*        the coordinate system for the "input" end of the Mapping.
c     iframe2
f     IFRAME2 = INTEGER (Given)
*        The index of the second Frame in the FrameSet. This Frame
*        describes the coordinate system for the "output" end of the
*        Mapping.
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Returned Value:
c     astGetMapping()
f     AST_GETMAPPING = INTEGER
*        Pointer to a Mapping whose forward transformation converts
*        coordinates from the first coordinate system to the second
*        one, and whose inverse transformation converts coordinates in
*        the opposite direction.

*  Notes:
*     - The returned Mapping will include the clipping effect of any
*     Regions which occur on the path between the two supplied Frames
*     (this includes the two supplied Frames themselves).
c     - The values given for the "iframe1" and "iframe2" parameters
f     - The values given for the IFRAME1 and IFRAME2 arguments
*     should lie in the range from 1 to the number of Frames in the
*     FrameSet (as given by its Nframe attribute). A value of
*     AST__BASE or AST__CURRENT may also be given to identify the
*     FrameSet's base Frame or current Frame respectively.  It is
c     permissible for both these parameters to have the same value, in
f     permissible for both these arguments to have the same value, in
*     which case a unit Mapping (UnitMap) is returned.
*     - It should always be possible to generate the Mapping
*     requested, but this does necessarily guarantee that it will be
*     able to perform the required coordinate conversion. If
*     necessary, the TranForward and TranInverse attributes of the
*     returned Mapping should be inspected to determine if the
*     required transformation is available.
*     - A null Object pointer (AST__NULL) will be returned if this
c     function is invoked with the AST error status set, or if it
f     function is invoked with STATUS set to an error value, or if it
*     should fail for any reason.
*--
*/

/* Local Variables: */
   AstFrame *fr;                 /* Temporary pointer to Frame */
   AstFrame **frames;            /* Pointer to array of Frames */
   AstMapping **path;            /* Pointer to array of conversion Mappings */
   AstMapping *copy;             /* Pointer to copy of Mapping */
   AstMapping *result;           /* Result pointer to be returned */
   AstMapping *tmp;              /* Temporary pointer for joining Mappings */
   int *forward;                 /* Pointer to array of Mapping directions */
   int ipath;                    /* Loop counter for conversion path steps */
   int iframe;                   /* Frame index */
   int inode;                    /* Node index */
   int npath;                    /* Number of steps in conversion path */

/* Initialise. */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Validate and translate the Frame indices supplied. */
   iframe1 = astValidateFrameIndex( this, iframe1, "astGetMapping" );
   iframe2 = astValidateFrameIndex( this, iframe2, "astGetMapping" );

/* Allocate memory to hold an array of Mapping pointers and associated
   direction flags - a maximum of one element for each Mapping and one
   for each Frame in the FrameSet. */
   path = astMalloc( sizeof( AstMapping * ) * (size_t) ( this->nnode - 1 +
                                                         this->nframe ) );
   forward = astMalloc( sizeof( int ) * (size_t) ( this->nnode - 1 +
                                                   this->nframe ) );

/* Allocate memory to hold a list of the Frame pointers (if any) associated
   with each node. */
   frames = astMalloc( sizeof( AstFrame * )  * (size_t) ( this->nnode ) );

/* If OK, set up an array of Frame pointers indexed by node index. If a
   node has no associated Frame store a NULL pointer. This is needed so
   that we can find Frame pointers quickly within the Span function. Note,
   we simply copy the pointers rather than cloning them, so they do not
   need to be annulled when finished with. */
   if ( astOK ) {
      for( inode = 0; inode < this->nnode; inode++ ) frames[ inode ] = NULL;
      for( iframe = 0; iframe < this->nframe; iframe++ ) {
         frames[ this->node[ iframe ] ] = this->frame[ iframe ];
      }

/* Obtain the Mapping pointers and direction flags needed to convert
   coordinates between the nodes associated with the two specified
   Frames. */
      npath = Span( this, frames, this->node[ iframe1 - 1 ],
                    this->node[ iframe2 - 1 ], -1, path, forward, status ) - 1;

/* If this failed, it indicates a corrupt FrameSet object, so report
   an error. */
      if ( npath < 0 ) {
         astError( AST__FRSIN, "astGetMapping(%s): Invalid or corrupt "
                   "%s - could not find conversion path between Frames "
                   "%d and %d.", status, astGetClass( this ), astGetClass( this ),
                   iframe1, iframe2 );

/* If the conversion path is of zero length (i.e. the two Frames are
   the same) then we will return a Mapping which is equivalent to the
   Frame. Most classes of Frame are equivalent to a UnitMap. However, we do
   not hard-wire this equivalence since some classes of Frame (e.g. Regions
   or CmpFrames containing Regions) do not correspond to a UnitMap. Instead
   we use the astIsUnitFrame method on the Frame to determine if the
   Frame is equivalent to a UnitMap.Is os, create a suitable UnitMap. If
   not, return the Frame itself (a form of Mapping). */
      } else if ( npath == 0 ) {
         fr = astGetFrame( this, iframe1 );
         if( astIsUnitFrame( fr ) ){
            result = (AstMapping *) astUnitMap( astGetNaxes( fr ), "", status );
         } else {
            result = (AstMapping *) astClone( fr );
         }
         fr = astAnnul( fr );

/* If the conversion path involves at least one non-trivial Mapping,
   make a copy of the first Mapping, inverting the copy if
   necessary. */
      } else {
         result = astCopy( path[ 0 ] );
         if ( !forward[ 0 ] ) astInvert( result );

/* Now loop to concatenate any further Mappings. First make a copy of
   each additional Mapping and invert the copy if necessary. */
         for ( ipath = 1; ipath < npath; ipath++ ) {
            copy = astCopy( path[ ipath ] );
            if ( !forward[ ipath ] ) astInvert( copy );

/* Concatenate the copy with the result so far, then annul the pointer
   to the copy and save the pointer to the new result. */
            tmp = (AstMapping *) astCmpMap( result, copy, 1, "", status );
            result = astAnnul( result );
            copy = astAnnul( copy );
            result = tmp;
         }
      }
   }

/* Free the memory allocated for the conversion path information. */
   path = astFree( path );
   forward = astFree( forward );
   frames = astFree( frames );

/* If an error occurred, annul the returned Mapping. */
   if ( !astOK ) result = astAnnul( result );

/* Return the result. */
   return result;
}

static int GetNaxes( AstFrame *this_frame, int *status ) {
/*
*  Name:
*     GetNaxes

*  Purpose:
*     Determine how many axes a FrameSet has.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int GetNaxes( AstFrame *this, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astGetNaxes method
*     inherited from the Frame class).

*  Description:
*     This function returns the number of axes for a FrameSet. This is equal
*     to the number of axes in its current Frame.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The number of FrameSet axes (zero or more).

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   int result;                   /* Result to be returned */

/* Check the global error status. */
   if ( !astOK ) return 0;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame. */
   fr = astGetFrame( this, AST__CURRENT );

/* Obtain the number of axes in this Frame. */
   result = astGetNaxes( fr );

/* Annul the current Frame pointer. */
   fr = astAnnul( fr );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = 0;

/* Return the result. */
   return result;
}

static int GetNframe( AstFrameSet *this, int *status ) {
/*
*+
*  Name:
*     astGetNframe

*  Purpose:
*     Determine the number of Frames in a FrameSet.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "frameset.h"
*     int astGetNframe( AstFrameSet *this )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function returns the number of Frames in a FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.

*  Returned Value:
*     The number of Frames in the FrameSet (always 1 or more).

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*-
*/

/* Check the global error status. */
   if ( !astOK ) return 0;

/* Return the Frame count. */
   return this->nframe;
}

static int GetNin( AstMapping *this_mapping, int *status ) {
/*
*  Name:
*     GetNin

*  Purpose:
*     Get the number of input coordinates for a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int GetNin( AstMapping *this, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astGetNin method
*     inherited from the Frame class).

*  Description:
*     This function returns the number of input coordinate values
*     required per point by a FrameSet when used to transform a set of
*     points (i.e. the number of dimensions of the space in which the
*     input points reside).
*
*     The value returned is equal to the number of axes in the
*     FrameSet's base Frame.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     Number of coordinate values required.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to base Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   int result;                   /* Result to be returned */

/* Check the global error status. */
   if ( !astOK ) return 0;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_mapping;

/* Obtain a pointer to the FrameSet's base Frame. */
   fr = astGetFrame( this, AST__BASE );

/* Obtain the number of axes in this Frame. */
   result = astGetNaxes( fr );

/* Annul the base Frame pointer. */
   fr = astAnnul( fr );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = 0;

/* Return the result. */
   return result;
}

static int GetNout( AstMapping *this_mapping, int *status ) {
/*
*  Name:
*     GetNout

*  Purpose:
*     Get the number of output coordinates for a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int GetNout( AstMapping *this, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astGetNout method
*     inherited from the Frame class).

*  Description:
*     This function returns the number of output coordinate values
*     generated per point by a FrameSet when used to transform a set
*     of points (i.e. the number of dimensions of the space in which
*     the output points reside).
*
*     The value returned is equal to the number of axes in the
*     FrameSet's current Frame.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     Number of coordinate values generated.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Check the global error status. */
   if ( !astOK ) return 0;

/* Return the number of axes in the FrameSet's current Frame. */
   return GetNaxes( (AstFrame *) this_mapping, status );
}

static const int *GetPerm( AstFrame *this_frame, int *status ) {
/*
*  Name:
*     GetPerm

*  Purpose:
*     Access the axis permutation array for the current Frame of a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     const int *GetPerm( AstFrame *this, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astGetPerm protected
*     method inherited from the Frame class).

*  Description:
*     This function returns a pointer to the axis permutation array
*     for the current Frame of a FrameSet. This array constitutes a
*     lookup-table that converts between an axis number supplied
*     externally and the corresponding index in the Frame's internal
*     axis arrays.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     Pointer to the current Frame's axis permutation array (a
*     constant array of int).  Each element of this contains the
*     (zero-based) internal axis index to be used in place of the
*     external index which is used to address the permutation
*     array. If the current Frame has zero axes, this pointer will be
*     NULL.

*  Notes:
*     - The pointer returned by this function gives direct access to
*     data internal to the Frame object. It remains valid only so long
*     as the Frame exists. The permutation array contents may be
*     modified by other functions which operate on the Frame and this
*     may render the returned pointer invalid.
*     - A NULL pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to FrameSet structure */
   const int *result;            /* Result pointer value */

/* Check the global error status. */
   if ( !astOK ) return NULL;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and then obtain a
   pointer to its axis permutation array. Annul the Frame pointer
   afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astGetPerm( fr );
   fr = astAnnul( fr );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = NULL;

/* Return the result. */
   return result;
}

static int GetTranForward( AstMapping *this_mapping, int *status ) {
/*
*  Name:
*     GetTranForward

*  Purpose:
*     Determine if a FrameSet defines a forward coordinate transformation.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int GetTranForward( AstMapping *this )

*  Class Membership:
*     Frameset member function (over-rides the astGetTranForward
*     protected method inherited from the Frame class).

*  Description:
*     This function returns a value indicating whether a FrameSet is
*     able to perform a coordinate transformation in the "forward"
*     direction.

*  Parameters:
*     this
*        Pointer to the FrameSet.

*  Returned Value:
*     Zero if the forward coordinate transformation is not defined, or
*     1 if it is.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   AstMapping *map;              /* Pointer to base->current Mapping */
   int result;                   /* Result to be returned */

/* Check the global error status. */
   if ( !astOK ) return 0;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_mapping;

/* Obtain the Mapping between the base and current Frames in the
   FrameSet (note this takes account of whether the FrameSet has been
   inverted). */
   map = astGetMapping( this, AST__BASE, AST__CURRENT );

/* Determine whether the required transformation is defined. */
   result = astGetTranForward( map );

/* Annul the Mapping pointer. */
   map = astAnnul( map );

/* If an error occurred, clear the returned result. */
   if ( !astOK ) result = 0;

/* Return the result. */
   return result;
}

static int GetTranInverse( AstMapping *this_mapping, int *status ) {
/*
*  Name:
*     GetTranInverse

*  Purpose:
*     Determine if a FrameSet defines an inverse coordinate transformation.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int GetTranInverse( AstMapping *this )

*  Class Membership:
*     Frameset member function (over-rides the astGetTranInverse
*     protected method inherited from the Frame class).

*  Description:
*     This function returns a value indicating whether a FrameSet is
*     able to perform a coordinate transformation in the "inverse"
*     direction.

*  Parameters:
*     this
*        Pointer to the FrameSet.

*  Returned Value:
*     Zero if the inverse coordinate transformation is not defined, or
*     1 if it is.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   AstMapping *map;              /* Pointer to base->current Mapping */
   int result;                   /* Result to be returned */

/* Check the global error status. */
   if ( !astOK ) return 0;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_mapping;

/* Obtain the Mapping between the base and current Frames in the
   FrameSet (note this takes account of whether the FrameSet has been
   inverted). */
   map = astGetMapping( this, AST__BASE, AST__CURRENT );

/* Determine whether the required transformation is defined. */
   result = astGetTranInverse( map );

/* Annul the Mapping pointer. */
   map = astAnnul( map );

/* If an error occurred, clear the returned result. */
   if ( !astOK ) result = 0;

/* Return the result. */
   return result;
}

static int GetUseDefs( AstObject *this_object, int *status ) {
/*
*  Name:
*     GetUseDefs

*  Purpose:
*     Get the value of the UseDefs attribute for a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int GetUseDefs( AstObject *this_object, int *status ) {

*  Class Membership:
*     FrameSet member function (over-rides the protected astGetUseDefs
*     method inherited from the Frame class).

*  Description:
*     This function returns the value of the UseDefs attribute for a FrameSet,
*     supplying a suitable default.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     - The USeDefs value.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   int result;                   /* Value to return */

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_object;

/* If the UseDefs value for the FrameSet has been set explicitly, use the
   Get method inherited from the parent Frame class to get its value> */
   if( astTestUseDefs( this ) ) {
      result = (*parent_getusedefs)( this_object, status );

/* Otherwise, supply a default value equal to the UseDefs value of the
   current Frame. */
   } else {
      fr = astGetFrame( this, AST__CURRENT );
      result = astGetUseDefs( fr );
      fr = astAnnul( fr );
   }

/* Return the result. */
   return result;
}

static int GetVarFrm( AstFrameSet *this, int iframe, int *status ) {
/*
*  Name:
*     GetVarFrm

*  Purpose:
*     Get the index of the variants Frame for a nominated Frame.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int GetVarFrm( AstFrameSet *this, int iframe, int *status ) {

*  Class Membership:
*     Private function.

*  Description:
*     This function returns the index of the variants Frame associated
*     with a nominated mirror Frame. See astMirrorVariants.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     iframe
*        The one-based index of the nominated Frame that may potentially be
*        a mirror for the variant Mappings in another Frame.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The one-based Frame index of the Frame that defines the variant
*     Mappings associated with Frame "iframe". This will be the same as
*     "iframe" unless the nominated Frame is a mirror for another Frame.
*/

/* Local Variables: */
   int result;                   /* Value to return */

/* Initialise. */
   result = AST__NOFRAME;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Initialise the returned value. */
   result = iframe;

/* If the nominated Frame is mirroring another Frame, return the index of
   the mirrored Frame. Walk up the chain until we reach a Frame which is
   not a mirror for another Frame. */
   while( this->varfrm[ result - 1 ] > 0 ) {
      if( this->varfrm[ result - 1 ] == result ) {
         astError( AST__INTER, "GetVarFrm(FrameSet): FrameSet is corrupt "
                   "(internal programming error).", status );
         break;
      } else {
         result = this->varfrm[ result - 1 ];
      }
   }

/* Return the result. */
   return result;
}

static const char *GetVariant( AstFrameSet *this, int *status ) {
/*
*+
*  Name:
*     astGetVariant

*  Purpose:
*     Obtain the value of the Variant attribute for a FrameSet.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "frameset.h"
*     const char *astGetVariant( AstFrameSet *this )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function returns the value of the Variant attribute for a
*     FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.

*  Returned Value:
*     The Variant attribute value.

*  Notes:
*     - A NULL value will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*-
*/

/* Local Variables: */
   AstFrame *frm;
   AstFrame *vfs;
   const char *result;
   int icur;
   int iuse;

/* Initialise */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get the one-based index of the current Frame. */
   icur = astGetCurrent( this );

/* The current Frame may mirror the variant Mappings in another Frame,
   rather than defining any variant Mappings itself. Get the one-based
   index of the Frame that defines the variant Mappings to use. */
   iuse = GetVarFrm( this, icur, status );

/* Get a pointer to the Variants FrameSet in the used Frame. */
   frm = astGetFrame( this, iuse );
   vfs = astGetFrameVariants( frm );

/* If the current Frame has no Variants FrameSet, return the Domain name
   of the current Frame. */
   if( !vfs ) {
      result = astGetDomain( this );

/* Otherwise, return the Domain name of the current Frame in the Variants
   FrameSet. Then annul the Variants FrameSet pointer. */
   } else {
      result = astGetDomain( vfs );
      vfs = astAnnul( vfs );
   }

/* Annul the current Frame pointer. */
   frm = astAnnul( frm );

/* If an error occurred, clear the result. */
   if ( !astOK ) result = NULL;

/* Return the result. */
   return result;
}

void astInitFrameSetVtab_(  AstFrameSetVtab *vtab, const char *name, int *status ) {
/*
*+
*  Name:
*     astInitFrameSetVtab

*  Purpose:
*     Initialise a virtual function table for a FrameSet.

*  Type:
*     Protected function.

*  Synopsis:
*     #include "frameset.h"
*     void astInitFrameSetVtab( AstFrameSetVtab *vtab, const char *name )

*  Class Membership:
*     FrameSet vtab initialiser.

*  Description:
*     This function initialises the component of a virtual function
*     table which is used by the FrameSet class.

*  Parameters:
*     vtab
*        Pointer to the virtual function table. The components used by
*        all ancestral classes will be initialised if they have not already
*        been initialised.
*     name
*        Pointer to a constant null-terminated character string which contains
*        the name of the class to which the virtual function table belongs (it
*        is this pointer value that will subsequently be returned by the Object
*        astClass function).
*-
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Pointer to thread-specific global data */
   AstFrameVtab *frame;          /* Pointer to Frame component of Vtab */
   AstMappingVtab *mapping;      /* Pointer to Mapping component of Vtab */
   AstObjectVtab *object;        /* Pointer to Object component of Vtab */

/* Check the local error status. */
   if ( !astOK ) return;

/* Get a pointer to the thread specific global data structure. */
   astGET_GLOBALS(NULL);

/* Initialize the component of the virtual function table used by the
   parent class. */
   astInitFrameVtab( (AstFrameVtab *) vtab, name );

/* Store a unique "magic" value in the virtual function table. This
   will be used (by astIsAFrameSet) to determine if an object belongs
   to this class.  We can conveniently use the address of the (static)
   class_check variable to generate this unique value. */
   vtab->id.check = &class_check;
   vtab->id.parent = &(((AstFrameVtab *) vtab)->id);

/* Initialise member function pointers. */
/* ------------------------------------ */
/* Store pointers to the member functions (implemented here) that
   provide virtual methods for this class. */
   vtab->AddFrame = AddFrame;
   vtab->AddVariant = AddVariant;
   vtab->ClearBase = ClearBase;
   vtab->ClearCurrent = ClearCurrent;
   vtab->GetBase = GetBase;
   vtab->GetCurrent = GetCurrent;
   vtab->GetFrame = GetFrame;
   vtab->GetMapping = GetMapping;
   vtab->GetNframe = GetNframe;
   vtab->GetAllVariants = GetAllVariants;
   vtab->MirrorVariants = MirrorVariants;
   vtab->RemapFrame = RemapFrame;
   vtab->RemoveFrame = RemoveFrame;
   vtab->SetBase = SetBase;
   vtab->SetCurrent = SetCurrent;
   vtab->TestBase = TestBase;
   vtab->TestCurrent = TestCurrent;
   vtab->ValidateFrameIndex = ValidateFrameIndex;

   vtab->ClearVariant = ClearVariant;
   vtab->GetVariant = GetVariant;
   vtab->SetVariant = SetVariant;
   vtab->TestVariant = TestVariant;

/* Save the inherited pointers to methods that will be extended, and
   replace them with pointers to the new member functions. */
   object = (AstObjectVtab *) vtab;

   parent_clear = object->Clear;
   parent_getobjsize = object->GetObjSize;
   object->GetObjSize = GetObjSize;
   object->Clear = Clear;

   parent_vset = object->VSet;
   object->VSet = VSet;

   parent_getusedefs = object->GetUseDefs;
   object->GetUseDefs = GetUseDefs;

#if defined(THREAD_SAFE)
   parent_managelock = object->ManageLock;
   object->ManageLock = ManageLock;
#endif

/* Store replacement pointers for methods which will be over-ridden by
   new member functions implemented here. */
   mapping = (AstMappingVtab *) vtab;
   frame = (AstFrameVtab *) vtab;

   object->ClearAttrib = ClearAttrib;
   object->GetAttrib = GetAttrib;
   object->SetAttrib = SetAttrib;
   object->TestAttrib = TestAttrib;

   object->GetUseDefs = GetUseDefs;
   object->Equal = Equal;
   object->Cast = Cast;

   mapping->GetIsLinear = GetIsLinear;
   mapping->GetNin = GetNin;
   mapping->GetNout = GetNout;
   mapping->GetTranForward = GetTranForward;
   mapping->GetTranInverse = GetTranInverse;
   mapping->Rate = Rate;
   mapping->ReportPoints = ReportPoints;
   mapping->RemoveRegions = RemoveRegions;
   mapping->Simplify = Simplify;
   mapping->Transform = Transform;
   mapping->MapSplit = MapSplit;

   frame->Abbrev = Abbrev;
   frame->Angle = Angle;
   frame->AxAngle = AxAngle;
   frame->AxDistance = AxDistance;
   frame->AxNorm = AxNorm;
   frame->AxOffset = AxOffset;
   frame->CheckPerm = CheckPerm;
   frame->ClearDigits = ClearDigits;
   frame->ClearDirection = ClearDirection;
   frame->ClearDomain = ClearDomain;
   frame->ClearFormat = ClearFormat;
   frame->ClearLabel = ClearLabel;
   frame->ClearMatchEnd = ClearMatchEnd;
   frame->ClearMaxAxes = ClearMaxAxes;
   frame->ClearMinAxes = ClearMinAxes;
   frame->ClearPermute = ClearPermute;
   frame->ClearPreserveAxes = ClearPreserveAxes;
   frame->ClearSymbol = ClearSymbol;
   frame->ClearTitle = ClearTitle;
   frame->ClearUnit = ClearUnit;
   frame->Convert = Convert;
   frame->ConvertX = ConvertX;
   frame->Distance = Distance;
   frame->Fields = Fields;
   frame->FindFrame = FindFrame;
   frame->Format = Format;
   frame->FrameGrid = FrameGrid;
   frame->Centre = Centre;
   frame->Gap = Gap;
   frame->GetAxis = GetAxis;
   frame->GetDigits = GetDigits;
   frame->GetDirection = GetDirection;
   frame->GetDomain = GetDomain;
   frame->GetFormat = GetFormat;
   frame->GetLabel = GetLabel;
   frame->GetMatchEnd = GetMatchEnd;
   frame->GetMaxAxes = GetMaxAxes;
   frame->GetMinAxes = GetMinAxes;
   frame->GetNaxes = GetNaxes;
   frame->GetPerm = GetPerm;
   frame->GetPermute = GetPermute;
   frame->GetPreserveAxes = GetPreserveAxes;
   frame->GetSymbol = GetSymbol;
   frame->GetTitle = GetTitle;
   frame->GetUnit = GetUnit;
   frame->Intersect = Intersect;
   frame->IsUnitFrame = IsUnitFrame;
   frame->LineContains = LineContains;
   frame->LineCrossing = LineCrossing;
   frame->LineDef = LineDef;
   frame->LineOffset = LineOffset;
   frame->Match = Match;
   frame->MatchAxes = MatchAxes;
   frame->MatchAxesX = MatchAxesX;
   frame->Norm = Norm;
   frame->NormBox = NormBox;
   frame->Offset = Offset;
   frame->Offset2 = Offset2;
   frame->Overlay = Overlay;
   frame->PermAxes = PermAxes;
   frame->PickAxes = PickAxes;
   frame->PrimaryFrame = PrimaryFrame;
   frame->Resolve = Resolve;
   frame->ResolvePoints = ResolvePoints;
   frame->SetAxis = SetAxis;
   frame->SetDigits = SetDigits;
   frame->SetDirection = SetDirection;
   frame->SetDomain = SetDomain;
   frame->SetFormat = SetFormat;
   frame->SetLabel = SetLabel;
   frame->SetMatchEnd = SetMatchEnd;
   frame->SetMaxAxes = SetMaxAxes;
   frame->SetMinAxes = SetMinAxes;
   frame->SetPermute = SetPermute;
   frame->SetPreserveAxes = SetPreserveAxes;
   frame->SetSymbol = SetSymbol;
   frame->SetTitle = SetTitle;
   frame->SetUnit = SetUnit;
   frame->SubFrame = SubFrame;
   frame->SystemCode = SystemCode;
   frame->SystemString = SystemString;
   frame->TestDigits = TestDigits;
   frame->TestDirection = TestDirection;
   frame->TestDomain = TestDomain;
   frame->TestFormat = TestFormat;
   frame->TestLabel = TestLabel;
   frame->TestMatchEnd = TestMatchEnd;
   frame->TestMaxAxes = TestMaxAxes;
   frame->TestMinAxes = TestMinAxes;
   frame->TestPermute = TestPermute;
   frame->TestPreserveAxes = TestPreserveAxes;
   frame->TestSymbol = TestSymbol;
   frame->TestTitle = TestTitle;
   frame->TestUnit = TestUnit;
   frame->Unformat = Unformat;
   frame->ValidateAxis = ValidateAxis;
   frame->ValidateAxisSelection = ValidateAxisSelection;
   frame->ValidateSystem = ValidateSystem;

   frame->GetActiveUnit = GetActiveUnit;
   frame->SetActiveUnit = SetActiveUnit;
   frame->TestActiveUnit = TestActiveUnit;

   frame->GetTop = GetTop;
   frame->SetTop = SetTop;
   frame->TestTop = TestTop;
   frame->ClearTop = ClearTop;

   frame->GetBottom = GetBottom;
   frame->SetBottom = SetBottom;
   frame->TestBottom = TestBottom;
   frame->ClearBottom = ClearBottom;

   frame->GetEpoch = GetEpoch;
   frame->SetEpoch = SetEpoch;
   frame->TestEpoch = TestEpoch;
   frame->ClearEpoch = ClearEpoch;

   frame->GetSystem = GetSystem;
   frame->SetSystem = SetSystem;
   frame->TestSystem = TestSystem;
   frame->ClearSystem = ClearSystem;

   frame->GetAlignSystem = GetAlignSystem;
   frame->SetAlignSystem = SetAlignSystem;
   frame->TestAlignSystem = TestAlignSystem;
   frame->ClearAlignSystem = ClearAlignSystem;

   frame->ClearObsLat = ClearObsLat;
   frame->TestObsLat = TestObsLat;
   frame->GetObsLat = GetObsLat;
   frame->SetObsLat = SetObsLat;

   frame->ClearObsAlt = ClearObsAlt;
   frame->TestObsAlt = TestObsAlt;
   frame->GetObsAlt = GetObsAlt;
   frame->SetObsAlt = SetObsAlt;

   frame->ClearObsLon = ClearObsLon;
   frame->TestObsLon = TestObsLon;
   frame->GetObsLon = GetObsLon;
   frame->SetObsLon = SetObsLon;

/* Declare the copy constructor, destructor and class dump
   functions. */
   astSetDelete( vtab, Delete );
   astSetCopy( vtab, Copy );
   astSetDump( vtab, Dump, "FrameSet",
               "Set of inter-related coordinate systems" );

/* If we have just initialised the vtab for the current class, indicate
   that the vtab is now initialised, and store a pointer to the class
   identifier in the base "object" level of the vtab. */
   if( vtab == &class_vtab ) {
      class_init = 1;
      astSetVtabClassIdentifier( vtab, &(vtab->id) );
   }
}

static void Intersect( AstFrame *this_frame, const double a1[2],
                       const double a2[2], const double b1[2],
                       const double b2[2], double cross[2],
                       int *status ) {
/*
*  Name:
*     Intersect

*  Purpose:
*     Find the point of intersection between two geodesic curves.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void Intersect( AstFrame *this_frame, const double a1[2],
*                      const double a2[2], const double b1[2],
*                      const double b2[2], double cross[2],
*                      int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astIntersect method
*     inherited from the Frame class).

*  Description:
*     This function finds the coordinate values at the point of
*     intersection between two geodesic curves. Each curve is specified
*     by two points on the curve.

*  Parameters:
*     this
*        Pointer to the SkyFrame.
*     a1
*        An array of double, with one element for each Frame axis.
*        This should contain the coordinates of a point on the first
*        geodesic curve.
*     a2
*        An array of double, with one element for each Frame axis.
*        This should contain the coordinates of a second point on the
*        first geodesic curve.
*     b1
*        An array of double, with one element for each Frame axis.
*        This should contain the coordinates of a point on the second
*        geodesic curve.
*     b2
*        An array of double, with one element for each Frame axis.
*        This should contain the coordinates of a second point on
*        the second geodesic curve.
*     cross
*        An array of double, with one element for each Frame axis
*        in which the coordinates of the required intersection
*        point will be returned. These will be AST__BAD if the curves do
*        not intersect.
*     status
*        Pointer to the inherited status variable.

*  Notes:
*     - The geodesic curve used by this function is the path of
*     shortest distance between two points, as defined by the
*     astDistance function.
*     - This function will return "bad" coordinate values (AST__BAD)
*     if any of the input coordinates has this value.
*     - For SkyFrames each curve will be a great circle, and in general
*     each pair of curves will intersect at two diametrically opposite
*     points on the sky. The returned position is the one which is
*     closest to point "a1".
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke the
   astIntersect method for this Frame. Annul the Frame pointer
   afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   astIntersect( fr, a1, a2, b1, b2, cross );
   fr = astAnnul( fr );

}

static int IsUnitFrame( AstFrame *this_frame, int *status ){
/*
*  Name:
*     IsUnitFrame

*  Purpose:
*     Is this Frame equivalent to a UnitMap?

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int IsUnitFrame( AstFrame *this, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astIsUnitFrame
*     method inherited from the Frame class).

*  Description:
*     This function returns a flag indicating if the supplied Frame is
*     equivalent to a UnitMap when treated as a Mapping (note, the Frame
*     class inherits from Mapping and therefore every Frame is also a Mapping).

*  Parameters:
*     this
*        Pointer to the Frame.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     A non-zero value is returned if the supplied Frame is equivalent to
*     a UnitMap when treated as a Mapping.

*-
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to FrameSet's current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   int result;                   /* Result to be returned */

/* Initialise the returned value. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame. */
   fr = astGetFrame( this, AST__CURRENT );

/* Invoke the astIsUnitFrame method for this Frame. */
   result = astIsUnitFrame( fr );

/* Annul the Frame pointer. */
   fr = astAnnul( fr );

/* If an error occurred, clean up by clearing the returned result. */
   if ( !astOK ) result = 0;

/* Return the result. */
   return result;
}

static int LineContains( AstFrame *this_frame, AstLineDef *l, int def, double *point, int *status ) {
/*
*  Name:
*     LineContains

*  Purpose:
*     Determine if a line contains a point.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int LineContains( AstFrame *this, AstLineDef *l, int def, double *point, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astLineContains
*     method inherited from the Frame class).

*  Description:
*     This function determines if the supplied point is on the supplied
*     line within the supplied Frame.

*  Parameters:
*     this
*        Pointer to the Frame.
*     l
*        Pointer to the structure defining the line.
*     def
*        Should be set non-zero if the "point" array was created by a
*        call to astLineCrossing (in which case it may contain extra
*        information following the axis values),and zero otherwise.
*     point
*        Point to an array containing the axis values of the point to be
*        tested, possibly followed by extra cached information (see "def").
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     A non-zero value is returned if the line contains the point.

*  Notes:
*     - The pointer supplied for "l" should have been created using the
*     astLineDef method. These structures contained cached information about
*     the lines which improve the efficiency of this method when many
*     repeated calls are made. An error will be reported if the structure
*     does not refer to the Frame specified by "this".
*     - Zero will be returned if this function is invoked with the global
*     error status set, or if it should fail for any reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   int result;                   /* Returned value */

/* Initialise */
   result =0;

/* Obtain a pointer to the FrameSet's current Frame and then invoke the
   method. Annul the Frame pointer afterwards. */
   fr = astGetFrame( (AstFrameSet *) this_frame, AST__CURRENT );
   result = astLineContains( fr, l, def, point );
   fr = astAnnul( fr );

/* Return the result. */
   return result;
}

static int LineCrossing( AstFrame *this_frame, AstLineDef *l1, AstLineDef *l2,
                         double **cross, int *status ) {
/*
*  Name:
*     LineCrossing

*  Purpose:
*     Determine if two lines cross.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int LineCrossing( AstFrame *this, AstLineDef *l1, AstLineDef *l2,
*                       double **cross, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astLineCrossing
*     method inherited from the Frame class).

*  Description:
*     This function determines if the two suplied line segments cross,
*     and if so returns the axis values at the point where they cross.
*     A flag is also returned indicating if the crossing point occurs
*     within the length of both line segments, or outside one or both of
*     the line segments.

*  Parameters:
*     this
*        Pointer to the Frame.
*     l1
*        Pointer to the structure defining the first line.
*     l2
*        Pointer to the structure defining the second line.
*     cross
*        Pointer to a location at which to put a pointer to a dynamically
*        alocated array containing the axis values at the crossing. If
*        NULL is supplied no such array is returned. Otherwise, the returned
*        array should be freed using astFree when no longer needed. If the
*        lines are parallel (i.e. do not cross) then AST__BAD is returned for
*        all axis values. Note usable axis values are returned even if the
*        lines cross outside the segment defined by the start and end points
*        of the lines. The order of axes in the returned array will take
*        account of the current axis permutation array if appropriate. Note,
*        sub-classes such as SkyFrame may append extra values to the end
*        of the basic frame axis values. A NULL pointer is returned if an
*        error occurs.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     A non-zero value is returned if the lines cross at a point which is
*     within the [start,end) segment of both lines. If the crossing point
*     is outside this segment on either line, or if the lines are parallel,
*     zero is returned. Note, the start point is considered to be inside
*     the length of the segment, but the end point is outside.

*  Notes:
*     - The pointers supplied for "l1" and "l2" should have been created
*     using the astLineDef method. These structures contained cached
*     information about the lines which improve the efficiency of this method
*     when many repeated calls are made. An error will be reported if
*     either structure does not refer to the Frame specified by "this".
*     - Zero will be returned if this function is invoked with the global
*     error status set, or if it should fail for any reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   int result;                   /* Returned value */

/* Initialise */
   result =0;

/* Obtain a pointer to the FrameSet's current Frame and then invoke the
   method. Annul the Frame pointer afterwards. */
   fr = astGetFrame( (AstFrameSet *) this_frame, AST__CURRENT );
   result = astLineCrossing( fr, l1, l2, cross );
   fr = astAnnul( fr );

/* Return the result. */
   return result;
}

static AstLineDef *LineDef( AstFrame *this_frame, const double start[2],
                            const double end[2], int *status ) {
/*
*  Name:
*     LineDef

*  Purpose:
*     Creates a structure describing a line segment in a 2D Frame.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstLineDef *LineDef( AstFrame *this, const double start[2],
*                             const double end[2], int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astLineDef
*     method inherited from the Frame class).

*  Description:
*     This function creates a structure containing information describing a
*     given line segment within the supplied 2D Frame. This may include
*     information which allows other methods such as astLineCrossing to
*     function more efficiently. Thus the returned structure acts as a
*     cache to store intermediate values used by these other methods.

*  Parameters:
*     this
*        Pointer to the Frame. Must have 2 axes.
*     start
*        An array of 2 doubles marking the start of the line segment.
*     end
*        An array of 2 doubles marking the end of the line segment.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     Pointer to the memory structure containing the description of the
*     line. This structure should be freed using astFree when no longer
*     needed. A NULL pointer is returned (without error) if any of the
*     supplied axis values are AST__BAD.

*  Notes:
*     - A null pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstLineDef *result;           /* Returned value */

/* Initialise */
   result = NULL;

/* Obtain a pointer to the FrameSet's current Frame and then invoke the
   method. Annul the Frame pointer afterwards. */
   fr = astGetFrame( (AstFrameSet *) this_frame, AST__CURRENT );
   result = astLineDef( fr, start, end );
   fr = astAnnul( fr );

/* Return the result. */
   return result;
}

static void LineOffset( AstFrame *this_frame, AstLineDef *line, double par,
                        double prp, double point[2], int *status ){
/*
*  Name:
*     LineOffset

*  Purpose:
*     Find a position close to a line.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frame.h"
*     void LineOffset( AstFrame *this, AstLineDef *line, double par,
*                      double prp, double point[2], int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astLineOffset
*     method inherited from the Frame class).

*  Description:
*     This function returns a position formed by moving a given distance along
*     the supplied line, and then a given distance away from the supplied line.

*  Parameters:
*     this
*        Pointer to the Frame.
*     line
*        Pointer to the structure defining the line.
*     par
*        The distance to move along the line from the start towards the end.
*     prp
*        The distance to move at right angles to the line. Positive
*        values result in movement to the left of the line, as seen from
*        the observer, when moving from start towards the end.
*     status
*        Pointer to the inherited status variable.

*  Notes:
*     - The pointer supplied for "line" should have been created using the
*     astLineDef method. This structure contains cached information about the
*     line which improves the efficiency of this method when many repeated
*     calls are made. An error will be reported if the structure does not
*     refer to the Frame specified by "this".
*/


/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */

/* Obtain a pointer to the FrameSet's current Frame and then invoke the
   method. Annul the Frame pointer afterwards. */
   fr = astGetFrame( (AstFrameSet *) this_frame, AST__CURRENT );
   astLineOffset( fr, line, par, prp, point );
   fr = astAnnul( fr );
}

static const char *GetAllVariants( AstFrameSet *this, int *status ) {
/*
*  Name:
*     GetAllVariants

*  Purpose:
*     Get a pointer to a list of the variant Mappings for the current Frame.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     const char *getAllVariants( AstFrameSet *this )

*  Class Membership:
*     FrameSet member function.

*  Description:
*     This function returns a space separated list of names for all the
*     variant Mappings associated with the current Frame. See attribute
*     "Variant". If the current Frame has no variant Mappings, the return
*     value contains just the Domain name of the current Frame in the
*     supplied FrameSet.

*  Parameters:
*     this
*        Pointer to the Frame.

*  Returned Value:
*     A pointer to a null-terminated string containing the list.

*  Notes:
*     - The returned string pointer may point at memory allocated
*     within the FrameSet, or at static memory. The contents of the
*     string may be over-written or the pointer may become invalid
*     following a further invocation of the same function or any
*     modification of the Frame. A copy of the string should
*     therefore be made if necessary.
*     - A NULL pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   astDECLARE_GLOBALS
   AstFrame *frm;
   AstFrame *vfrm;
   AstFrameSet *vfs;
   const char *dom;
   const char *result;
   int ifrm;
   int nc;
   int icur;
   int nfrm;

/* Check the global error status. */
   if ( !astOK ) return NULL;

/* Get a pointer to the structure holding thread-specific global data. */
   astGET_GLOBALS( this );

/* Get the one-based index of the Frame that defines the available
   variant Mappings. */
   icur = GetVarFrm( this, astGetCurrent( this ), status );

/* Get the variants FrameSet from the Frame selected above. */
   frm = astGetFrame( this, icur );
   vfs = astGetFrameVariants( frm );

/* If the Frame does not have a variants FrameSet, just return the DOmain
   name from the current Frame. */
   if( !vfs ) {
      result = astGetDomain( this );

/* If a variants FrameSet was found, form a space sperated list of the
   Domain names in the FrameSet, stored in the static "getallvariants_buff"
   string. */
   } else if( astOK ){
      nc = 0;

      nfrm = astGetNframe( vfs );
      for( ifrm = 0; ifrm < nfrm; ifrm++ ) {
         vfrm = astGetFrame( vfs, ifrm + 1 );
         dom = astGetDomain( vfrm );
         if( astOK ){
            if( ( nc + strlen(dom) + 1 ) < GETALLVARIANTS_BUFF_LEN ) {
               nc += sprintf( getallvariants_buff + nc, "%s ", dom  );
            } else {
               astError( AST__INTER, "astGetAllVariants(%s): Buffer "
                         "overflow - too many variants.", status,
                         astGetClass(this) );
            }
         }
         vfrm = astAnnul( vfrm );
      }

/* Remove the final space. */
      getallvariants_buff[ nc - 1 ] = 0;

/* Return a pointer to the buffer. */
      result = getallvariants_buff;

/* Annul the pointer to the variants FrameSet. */
      vfs = astAnnul( vfs );
   }

/* Free the pointer to the current Frame. */
   frm = astAnnul( frm );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = NULL;

/* Return the result. */
   return result;
}

#if defined(THREAD_SAFE)
static int ManageLock( AstObject *this_object, int mode, int extra,
                       AstObject **fail, int *status ) {
/*
*  Name:
*     ManageLock

*  Purpose:
*     Manage the thread lock on an Object.

*  Type:
*     Private function.

*  Synopsis:
*     #include "object.h"
*     AstObject *ManageLock( AstObject *this, int mode, int extra,
*                            AstObject **fail, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astManageLock protected
*     method inherited from the parent class).

*  Description:
*     This function manages the thread lock on the supplied Object. The
*     lock can be locked, unlocked or checked by this function as
*     deteremined by parameter "mode". See astLock for details of the way
*     these locks are used.

*  Parameters:
*     this
*        Pointer to the Object.
*     mode
*        An integer flag indicating what the function should do:
*
*        AST__LOCK: Lock the Object for exclusive use by the calling
*        thread. The "extra" value indicates what should be done if the
*        Object is already locked (wait or report an error - see astLock).
*
*        AST__UNLOCK: Unlock the Object for use by other threads.
*
*        AST__CHECKLOCK: Check that the object is locked for use by the
*        calling thread (report an error if not).
*     extra
*        Extra mode-specific information.
*     fail
*        If a non-zero function value is returned, a pointer to the
*        Object that caused the failure is returned at "*fail". This may
*        be "this" or it may be an Object contained within "this". Note,
*        the Object's reference count is not incremented, and so the
*        returned pointer should not be annulled. A NULL pointer is
*        returned if this function returns a value of zero.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*    A local status value:
*        0 - Success
*        1 - Could not lock or unlock the object because it was already
*            locked by another thread.
*        2 - Failed to lock a POSIX mutex
*        3 - Failed to unlock a POSIX mutex
*        4 - Bad "mode" value supplied.

*  Notes:
*     - This function attempts to execute even if an error has already
*     occurred.
*/

/* Local Variables: */
   AstFrameSet *this;    /* Pointer to FrameSet structure */
   int i;                /* Loop count */
   int result;           /* Returned status value */

/* Initialise */
   result = 0;

/* Check the supplied pointer is not NULL. */
   if( !this_object ) return result;

/* Obtain a pointers to the FrameSet structure. */
   this = (AstFrameSet *) this_object;

/* Invoke the ManageLock method inherited from the parent class. */
   if( !result ) result = (*parent_managelock)( this_object, mode, extra,
                                                fail, status );

/* Invoke the astManageLock method on any Objects contained within
   the supplied Object. */
   for( i = 0; i < this->nframe; i++ ) {
      if( !result ) result = astManageLock( this->frame[ i ], mode,
                                            extra, fail );
   }

   for ( i = 0; i < this->nnode - 1; i++ ) {
      if( !result ) result = astManageLock( this->map[ i ], mode, extra,
                                            fail );
   }

   return result;

}
#endif

static int *MapSplit( AstMapping *this_map, int nin, const int *in, AstMapping **map, int *status ){
/*
*  Name:
*     MapSplit

*  Purpose:
*     Create a Mapping representing a subset of the inputs of an existing
*     FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int *MapSplit( AstMapping *this, int nin, const int *in, AstMapping **map, int *status )

*  Class Membership:
*     FrameSet method (over-rides the protected astMapSplit method
*     inherited from the Mapping class).

*  Description:
*     This function creates a new Mapping by picking specified inputs from
*     an existing FrameSet. This is only possible if the specified inputs
*     correspond to some subset of the FrameSet outputs. That is, there
*     must exist a subset of the FrameSet outputs for which each output
*     depends only on the selected FrameSet inputs, and not on any of the
*     inputs which have not been selected. If this condition is not met
*     by the supplied FrameSet, then a NULL Mapping is returned.

*  Parameters:
*     this
*        Pointer to the FrameSet to be split (the FrameSet is not actually
*        modified by this function).
*     nin
*        The number of inputs to pick from "this".
*     in
*        Pointer to an array of indices (zero based) for the inputs which
*        are to be picked. This array should have "nin" elements. If "Nin"
*        is the number of inputs of the supplied FrameSet, then each element
*        should have a value in the range zero to Nin-1.
*     map
*        Address of a location at which to return a pointer to the new
*        Mapping. This Mapping will have "nin" inputs (the number of
*        outputs may be different to "nin"). A NULL pointer will be
*        returned if the supplied FrameSet has no subset of outputs which
*        depend only on the selected inputs.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     A pointer to a dynamically allocated array of ints. The number of
*     elements in this array will equal the number of outputs for the
*     returned Mapping. Each element will hold the index of the
*     corresponding output in the supplied FrameSet. The array should be
*     freed using astFree when no longer needed. A NULL pointer will
*     be returned if no output Mapping can be created.

*  Notes:
*     - If this function is invoked with the global error status set,
*     or if it should fail for any reason, then NULL values will be
*     returned as the function value and for the "map" pointer.
*/

/* Local Variables: */
   AstMapping *bcmap;   /* Base->current Mapping */
   int *result;         /* Returned pointer */

/* Initialise */
   result = NULL;
   *map = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get the Mapping from base to current Frame and try to split it. */
   bcmap = astGetMapping( (AstFrameSet *) this_map, AST__BASE, AST__CURRENT );
   result = astMapSplit( bcmap, nin, in, map );
   bcmap = astAnnul( bcmap );

/* Free returned resources if an error has occurred. */
   if( !astOK ) {
      result = astFree( result );
      *map = astAnnul( *map );
   }

/* Return the list of output indices. */
   return result;
}

static int Match( AstFrame *this_frame, AstFrame *target, int matchsub,
                  int **template_axes, int **target_axes,
                  AstMapping **map, AstFrame **result, int *status ) {
/*
*  Name:
*     Match

*  Purpose:
*     Determine if conversion is possible between two coordinate systems.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int Match( AstFrame *template, AstFrame *target, int matchsub,
*                int **template_axes, int **target_axes,
*                AstMapping **map, AstFrame **result, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astMatch
*     method inherited from the Frame class).

*  Description:
*     This function matches the current Frame of a "template" FrameSet
*     to a "target" frame and determines whether it is possible to
*     convert coordinates between them.  If it is, a Mapping that
*     performs the transformation is returned along with a new Frame
*     that describes the coordinate system that results when this
*     Mapping is applied to the current Frame of the target
*     FrameSet. In addition, information is returned to allow the axes
*     in this "result" Frame to be associated with the corresponding
*     axes in the target and template Frames from which they are
*     derived.

*  Parameters:
*     template
*        Pointer to the template FrameSet, whose current Frame
*        describes the coordinate system (or set of possible
*        coordinate systems) into which we wish to convert our
*        coordinates.
*     target
*        Pointer to the target Frame. This describes the coordinate
*        system in which we already have coordinates.
*     matchsub
*        If zero then a match only occurs if the template is of the same
*        class as the target, or of a more specialised class. If non-zero
*        then a match can occur even if this is not the case (i.e. if the
*        target is of a more specialised class than the template). In
*        this latter case, the target is cast down to the class of the
*        template. NOTE, this argument is handled by the global method
*        wrapper function "astMatch_", rather than by the class-specific
*        implementations of this method.
*     template_axes
*        Address of a location where a pointer to int will be returned
*        if the requested coordinate conversion is possible. This
*        pointer will point at a dynamically allocated array of
*        integers with one element for each axis of the "result" Frame
*        (see below). It must be freed by the caller (using astFree)
*        when no longer required.
*
*        For each axis in the result Frame, the corresponding element
*        of this array will return the index of the axis in the
*        template FrameSet's current Frame from which it is
*        derived. If it is not derived from any template FrameSet
*        axis, a value of -1 will be returned instead.
*     target_axes
*        Address of a location where a pointer to int will be returned
*        if the requested coordinate conversion is possible. This
*        pointer will point at a dynamically allocated array of
*        integers with one element for each axis of the "result" Frame
*        (see below). It must be freed by the caller (using astFree)
*        when no longer required.
*
*        For each axis in the result Frame, the corresponding element
*        of this array will return the index of the target Frame axis
*        from which it is derived. If it is not derived from any
*        target Frame axis, a value of -1 will be returned instead.
*     map
*        Address of a location where a pointer to a new Mapping will
*        be returned if the requested coordinate conversion is
*        possible. If returned, the forward transformation of this
*        Mapping may be used to convert coordinates between the target
*        Frame and the result Frame (see below) and the inverse
*        transformation will convert in the opposite direction.
*     result
*        Address of a location where a pointer to a new Frame will be
*        returned if the requested coordinate conversion is
*        possible. If returned, this Frame describes the coordinate
*        system that results from applying the returned Mapping
*        (above) to the "target" coordinate system. In general, this
*        Frame will combine attributes from (and will therefore be
*        more specific than) both the target Frame and the current
*        Frame of the template FrameSet. In particular, when the
*        template allows the possibility of transformaing to any one
*        of a set of alternative coordinate systems, the "result"
*        Frame will indicate which of the alternatives was used.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     A non-zero value is returned if the requested coordinate
*     conversion is possible. Otherwise zero is returned (this will
*     not in itself result in an error condition).

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to FrameSet's current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   int match;                    /* Result to be returned */

/* Initialise the returned values. */
   *template_axes = NULL;
   *target_axes = NULL;
   *map = NULL;
   *result = NULL;
   match = 0;

/* Check the global error status. */
   if ( !astOK ) return match;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame. */
   fr = astGetFrame( this, AST__CURRENT );

/* Invoke the astMatch method for this Frame. */
   match =astMatch( fr, target, matchsub, template_axes, target_axes,
                    map, result );

/* Annul the Frame pointer. */
   fr = astAnnul( fr );

/* If an error occurred, clean up by freeing any allocated memory,
   annulling returned objects and clearing the returned result. */
   if ( !astOK ) {
      *template_axes = astFree( *template_axes );
      *target_axes = astFree( *target_axes );
      *map = astAnnul( *map );
      *result = astAnnul( *result );
      match = 0;
   }

/* Return the result. */
   return match;
}

static void MatchAxes( AstFrame *frm1_frame, AstFrame *frm2, int *axes,
                       int *status ) {
/*
*  Name:
*     MatchAxes

*  Purpose:
*     Find any corresponding axes in two Frames.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void MatchAxes( AstFrame *frm1, AstFrame *frm2, int *axes )
*                     int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astMatchAxes
*     method inherited from the Frame class).

*  Description:
*     This function looks for corresponding axes within two supplied
*     Frames. An array of integers is returned that contains an element
*     for each axis in the second supplied Frame. An element in this array
*     will be set to zero if the associated axis within the second Frame
*     has no corresponding axis within the first Frame. Otherwise, it
*     will be set to the index (a non-zero positive integer) of the
*     corresponding axis within the first supplied Frame.

*  Parameters:
*     frm1
*        Pointer to the first Frame.
*     frm2
*        Pointer to the second Frame.
*     axes
*        Pointer to an
*        integer array in which to return the indices of the axes (within
*        the second Frame) that correspond to each axis within the first
*        Frame. Axis indices start at 1. A value of zero will be stored
*        in the returned array for each axis in the first Frame that has
*        no corresponding axis in the second Frame.
*
*        The number of elements in this array must be greater than or
*        equal to the number of axes in the first Frame.
*     status
*        Pointer to inherited status value.

*  Notes:
*     -  Corresponding axes are identified by the fact that a Mapping
*     can be found between them using astFindFrame or astConvert. Thus,
*     "corresponding axes" are not necessarily identical. For instance,
*     SkyFrame axes in two Frames will match even if they describe
*     different celestial coordinate systems
*/

/* Local Variables: */
   AstFrame *frm1;

/* Check the global error status. */
   if ( !astOK ) return;

/* Get a pointer to the current Frame in the FrameSet. */
   frm1 = astGetFrame( (AstFrameSet *) frm1_frame, AST__CURRENT );

/* Invoke the astMatchAxesX on the second Frame. */
   astMatchAxesX( frm2, frm1, axes );

/* Free resources */
   frm1 = astAnnul( frm1 );
}

static void MatchAxesX( AstFrame *frm2_frame, AstFrame *frm1, int *axes,
                        int *status ) {
/*
*  Name:
*     MatchAxesX

*  Purpose:
*     Find any corresponding axes in two Frames.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void MatchAxesX( AstFrame *frm2, AstFrame *frm1, int *axes )
*                      int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astMatchAxesX
*     method inherited from the Frame class).

*     This function looks for corresponding axes within two supplied
*     Frames. An array of integers is returned that contains an element
*     for each axis in the second supplied Frame. An element in this array
*     will be set to zero if the associated axis within the second Frame
*     has no corresponding axis within the first Frame. Otherwise, it
*     will be set to the index (a non-zero positive integer) of the
*     corresponding axis within the first supplied Frame.

*  Parameters:
*     frm2
*        Pointer to the second Frame.
*     frm1
*        Pointer to the first Frame.
*     axes
*        Pointer to an integer array in which to return the indices of
*        the axes (within the first Frame) that correspond to each axis
*        within the second Frame. Axis indices start at 1. A value of zero
*        will be stored in the returned array for each axis in the second
*        Frame that has no corresponding axis in the first Frame.
*
*        The number of elements in this array must be greater than or
*        equal to the number of axes in the second Frame.
*     status
*        Pointer to inherited status value.

*  Notes:
*     -  Corresponding axes are identified by the fact that a Mapping
*     can be found between them using astFindFrame or astConvert. Thus,
*     "corresponding axes" are not necessarily identical. For instance,
*     SkyFrame axes in two Frames will match even if they describe
*     different celestial coordinate systems
*/

/* Local Variables: */
   AstFrame *frm2;

/* Check the global error status. */
   if ( !astOK ) return;

/* Get a pointer to the current Frame in the FrameSet. */
   frm2 = astGetFrame( (AstFrameSet *) frm2_frame, AST__CURRENT );

/* Invoke the astMatchAxesX on the current Frame. */
   astMatchAxesX( frm2, frm1, axes );

/* Free resources */
   frm2 = astAnnul( frm2 );
}

static void MirrorVariants( AstFrameSet *this, int iframe, int *status ) {
/*
*++
*  Name:
c     astMirrorVariants
f     AST_MIRRORVARIANTS

*  Purpose:
*     Make the current Frame mirror the variant Mappings in another Frame.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "frameset.h"
c     void astMirrorVariants( AstFrameSet *this, int iframe, int *status )
f     CALL AST_MIRRORVARIANTS( THIS, IFRAME, STATUS )

*  Class Membership:
*     FrameSet method.

*  Description:
c     This function
f     This routine
*     indicates that all access to the Variant attribute of the current
*     Frame should should be forwarded to some other nominated Frame in
*     the FrameSet. For instance, if a value is set subsequently for the
*     Variant attribute of the current Frame, the current Frame will be left
*     unchanged and the setting is instead applied to the nominated Frame.
*     Likewise, if the value of the Variant attribute is requested, the
*     value returned is the value stored for the nominated Frame rather
*     than the current Frame itself.
*
*     This provides a mechanism for propagating the effects of variant
*     Mappings around a FrameSet. If a new Frame is added to a FrameSet
*     by connecting it to an pre-existing Frame that has two or more variant
*     Mappings, then it may be appropriate to set the new Frame so that it
*     mirrors the variants Mappings of the pre-existing Frame. If this is
*     done, then it will be possible to select a specific variant Mapping
*     using either the pre-existing Frame or the new Frame.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the FrameSet.
c     iframe
f     IFRAME = INTEGER (Given)
*        The index of the Frame within the FrameSet which is to be
*        mirrored by the current Frame. This value should lie in the range
*        from 1 to the number of Frames in the FrameSet (as given by its
*        Nframe attribute). If AST__NOFRAME is supplied (or the current
*        Frame is specified), then any mirroring established by a previous
*        call to this
c        function
f        routine
*        is disabled.
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Notes:
*     - Mirrors can be chained. That is, if Frame B is set to be a mirror
*     of Frame A, and Frame C is set to be a mirror of Frame B, then
*     Frame C will act as a mirror of Frame A.
*     - Variant Mappings cannot be added to the current Frame if it is
*     mirroring another Frame. So calls to the
c     astAddVariant function
f     AST_ADDVARIANT routine
*     will cause an error to be reported if the current Frame is
*     mirroring another Frame.
*     - A value of AST__BASE may be given for the
c     "iframe" parameter
f     IFRAME argument
*     to specify the base Frame.
*     - Any variant Mappings explicitly added to the current Frame using
c     astAddVariant
f     AST_ADDVARIANT
*     will be ignored if the current Frame is mirroring another Frame.

*--
*/

/* Local Variables: */
   int icur;

/* Check the global error status. */
   if ( !astOK ) return;

/* Get the current Frame index. */
   icur = astGetCurrent( this );

/* If AST__NOFRAME, disable any mirroring. */
   if( iframe == AST__NOFRAME ) {
      this->varfrm[ icur - 1 ] = 0;

/* Otherwise, validate and translate the Frame index supplied. */
   } else {
      iframe = astValidateFrameIndex( this, iframe, "astMirrorVariants" );

/* If the current Frame has been specified, disable any mirroring. */
      if( iframe == icur ) {
         this->varfrm[ icur - 1 ] = 0;

/* Otherwise, store the one-based variants frame index. */
      } else {
         this->varfrm[ icur - 1 ] = iframe;
      }
   }
}

static void Norm( AstFrame *this_frame, double value[], int *status ) {
/*
*  Name:
*     Norm

*  Purpose:
*     Normalise a set of FrameSet coordinates.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void Norm( AstAxis *this, double value[], int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astNorm method
*     inherited from the Frame class).

*  Description:
*     This function converts a set of coordinate values for the
*     current Frame of a FrameSet, which might potentially be
*     unsuitable for display to a user (for instance, may lie outside
*     the expected range of values) into a set of acceptable
*     alternative values suitable for display.
*
*     Typically, for Frames whose axes represent cyclic values (such
*     as angles or positions on the sky), this function wraps an
*     arbitrary set of coordinates, so that they lie within the first
*     cycle (say zero to 2*pi or -pi/2 to +pi/2). For Frames with
*     ordinary linear axes, without constraints, this function will
*     typically return the original coordinate values unchanged.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     value
*        An array of double, with one element for each FrameSet axis.
*        This should contain the initial set of coordinate values,
*        which will be modified in place.
*     status
*        Pointer to the inherited status variable.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to the current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astNorm method to obtain the new values. Annul the Frame
   pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   astNorm( fr, value );
   fr = astAnnul( fr );
}

static void NormBox( AstFrame *this_frame, double lbnd[], double ubnd[],
                     AstMapping *reg, int *status ) {
/*
*  Name:
*     NormBox

*  Purpose:
*     Extend a box to include effect of any singularities in the Frame.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void astNormBox( AstFrame *this, double lbnd[], double ubnd[],
*                      AstMapping *reg, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astNormBox method inherited
*     from the Frame class).

*  Description:
*     This function modifies a supplied box to include the effect of any
*     singularities in the co-ordinate system represented by the Frame.
*     For a normal Cartesian coordinate system, the box will be returned
*     unchanged. Other classes of Frame may do other things. For instance,
*     a SkyFrame will check to see if the box contains either the north
*     or south pole and extend the box appropriately.

*  Parameters:
*     this
*        Pointer to the Frame.
*     lbnd
*        An array of double, with one element for each Frame axis
*        (Naxes attribute). Initially, this should contain a set of
*        lower axis bounds for the box. They will be modified on exit
*        to include the effect of any singularities within the box.
*     ubnd
*        An array of double, with one element for each Frame axis
*        (Naxes attribute). Initially, this should contain a set of
*        upper axis bounds for the box. They will be modified on exit
*        to include the effect of any singularities within the box.
*     reg
*        A Mapping which should be used to test if any singular points are
*        inside or outside the box. The Mapping should leave an input
*        position unchanged if the point is inside the box, and should
*        set all bad if the point is outside the box.
*     status
*        Pointer to the inherited status variable.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to the current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astNormBox method to obtain the new values. Annul the Frame
   pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   astNormBox( fr, lbnd, ubnd, reg );
   fr = astAnnul( fr );
}

static void Offset( AstFrame *this_frame, const double point1[],
                    const double point2[], double offset, double point3[], int *status ) {
/*
*  Name:
*     Offset

*  Purpose:
*     Calculate an offset along a geodesic curve.

*  Type:
*     Public virtual function.

*  Synopsis:
*     #include "frameset.h"
*     void Offset( AstFrame *this,
*                  const double point1[], const double point2[],
*                  double offset, double point3[], int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astOffset
*     method inherited from the Frame class).

*  Description:
*     This function finds the FrameSet coordinate values of a point
*     which is offset a specified distance along the geodesic curve
*     between two other points.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     point1
*        An array of double, with one element for each FrameSet axis.
*        This should contain the coordinates of the point marking the
*        start of the geodesic curve.
*     point2
*        An array of double, with one element for each FrameSet axis
*        This should contain the coordinates of the point marking the
*        end of the geodesic curve.
*     offset
*        The required offset from the first point along the geodesic
*        curve. If this is positive, it will be towards the second
*        point. If it is negative, it will be in the opposite
*        direction. This offset need not imply a position lying
*        between the two points given, as the curve will be
*        extrapolated if necessary.
*     point3
*        An array of double, with one element for each FrameSet axis
*        in which the coordinates of the required point will be
*        returned.
*     status
*        Pointer to the inherited status variable.

*  Notes:
*     - The geodesic curve used by this function is the path of
*     shortest distance between two points, as defined by the
*     astDistance function.
*     - This function will return "bad" coordinate values (AST__BAD)
*     if any of the input coordinates has this value.
*     - "Bad" coordinate values will also be returned if the two
*     points supplied are coincident (or otherwise fail to uniquely
*     specify a geodesic curve) but the requested offset is non-zero.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astOffset method. Annul the Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   astOffset( fr, point1, point2, offset, point3 );
   fr = astAnnul( fr );
}

static double Offset2( AstFrame *this_frame, const double point1[2],
                       double angle, double offset, double point2[2], int *status ){
/*
*  Name:
*     Offset2

*  Purpose:
*     Calculate an offset along a geodesic curve in a 2D Frame.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     double Offset2( AstFrame *this, const double point1[2], double angle,
*                     double offset, double point2[2], int *status );

*  Class Membership:
*     FrameSet member function (over-rides the protected astOffset2
*     method inherited from the Frame class).

*  Description:
*     This function finds the Frame coordinate values of a point which
*     is offset a specified distance along the geodesic curve at a
*     given angle from a specified starting point. It can only be
*     used with 2-dimensional Frames.
*
*     For example, in a basic Frame, this offset will be along the
*     straight line joining two points. For a more specialised Frame
*     describing a sky coordinate system, however, it would be along
*     the great circle passing through two sky positions.

*  Parameters:
*     this
*        Pointer to the Frame.
*     point1
*        An array of double, with one element for each Frame axis
*        (Naxes attribute). This should contain the coordinates of the
*        point marking the start of the geodesic curve.
*     angle
*        The angle (in radians) from the positive direction of the second
*        axis, to the direction of the required position, as seen from
*        the starting position. Positive rotation is in the sense of
*        rotation from the positive direction of axis 2 to the positive
*        direction of axis 1.
*     offset
*        The required offset from the first point along the geodesic
*        curve. If this is positive, it will be in the direction of the
*        given angle. If it is negative, it will be in the opposite
*        direction.
*     point2
*        An array of double, with one element for each Frame axis
*        in which the coordinates of the required point will be returned.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The direction of the geodesic curve at the end point. That is, the
*     angle (in radians) between the positive direction of the second
*     axis and the continuation of the geodesic curve at the requested
*     end point. Positive rotation is in the sense of rotation from
*     the positive direction of axis 2 to the positive direction of axis 1.

*  Notes:
*     - The geodesic curve used by this function is the path of
*     shortest distance between two points, as defined by the
*     astDistance function.
*     - An error will be reported if the Frame is not 2-dimensional.
*     - This function will return "bad" coordinate values (AST__BAD)
*     if any of the input coordinates has this value.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   double result;                /* Value to return */

/* Check the global error status. */
   if ( !astOK ) return AST__BAD;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke the
   astOffset2 method for this Frame. Annul the Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astOffset2( fr, point1, angle, offset, point2 );
   fr = astAnnul( fr );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = AST__BAD;

/* Return the result. */
   return result;
}

static void Overlay( AstFrame *template_frame, const int *template_axes,
                     AstFrame *result, int *status ) {
/*
*  Name:
*     Overlay

*  Purpose:
*     Overlay the attributes of a template FrameSet on to another Frame.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void Overlay( AstFrame *template, const int *template_axes,
*                   AstFrame *result, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astOverlay
*     method inherited from the Frame class).

*  Description:
*     This function overlays attributes from the current Frame of a
*     FrameSet on to another Frame, so as to over-ride selected
*     attributes of that second Frame. Normally only those attributes
*     which have been specifically set in the template will be
*     transferred. This implements a form of defaulting, in which a
*     Frame acquires attributes from the template, but retains its
*     original attributes (as the default) if new values have not
*     previously been explicitly set in the template.

*  Parameters:
*     template
*        Pointer to the template FrameSet, for whose current Frame
*        values should have been explicitly set for any attribute
*        which is to be transferred.
*     template_axes
*        Pointer to an array of int, with one element for each axis of
*        the "result" Frame (see below). For each axis in the result
*        frame, the corresponding element of this array should contain
*        the (zero-based) index of the axis in the current Frame of
*        the template FrameSet to which it corresponds. This array is
*        used to establish from which template Frame axis any
*        axis-dependent attributes should be obtained.
*
*        If any axis in the result Frame is not associated with a
*        template Frame axis, the corresponding element of this array
*        should be set to -1.
*
*        If a NULL pointer is supplied, the template and result axis
*        indices are assumed to be identical.
*     result
*        Pointer to the Frame which is to receive the new attribute values.
*     status
*        Pointer to the inherited status variable.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *template;        /* Pointer to the FrameSet structure */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   template = (AstFrameSet *) template_frame;

/* Obtain a pointer to the current Frame and invoke its astOverlay
   method to overlay its attributes. Annul the Frame pointer
   afterwards. */
   fr = astGetFrame( template, AST__CURRENT );
   astOverlay( fr, template_axes, result );
   fr = astAnnul( fr );
}

static void PermAxes( AstFrame *this_frame, const int perm[], int *status ) {
/*
*  Name:
*     PermAxes

*  Purpose:
*     Permute the order of a FrameSet's axes.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void PermAxes( AstFrame *this, const int perm[], int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astPermAxes method
*     inherited from the Frame class).

*  Description:
*     This function permutes the order in which the axes in the
*     current Frame of a FrameSet occur.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     perm
*        An array of int (with one element for each axis of the
*        FrameSet's current Frame) which lists the axes in their new
*        order. Each element of this array should be a (zero-based)
*        axis index identifying the axes according to their old
*        (un-permuted) order.
*     status
*        Pointer to the inherited status variable.

*  Notes:
*     - Only genuine permutations of the axis order are permitted, so
*     each axis must be referenced exactly once in the "perm" array.
*     - If more than one axis permutation is applied to the same Frame
*     in a FrameSet, the effects are cumulative.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   AstPermMap *map;              /* Pointer to axis permutation Mapping */
   int *invperm;                 /* Pointer to inverse permutation array */
   int axis;                     /* Loop counter for axes */
   int naxes;                    /* Number of FrameSet axes */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the permutation array, to check that it describes a
   genuine permutation. */
   astCheckPerm( this, perm, "astPermAxes" );

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astPermAxes method to permute its axes. Annul the Frame
   pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   astPermAxes( fr, perm );
   fr = astAnnul( fr );

/* Obtain the number of axes in the FrameSet's current Frame and allocate
   memory to hold an inverse permutation array. */
   naxes =  astGetNaxes( this );
   invperm = astMalloc( sizeof( int ) * (size_t) naxes );

/* Fill the inverse permutation array with values that will invert the
   axis permutation supplied. */
   if ( astOK ) {
      for ( axis = 0; axis < naxes; axis++ ) invperm[ perm[ axis ] ] = axis;

/* Create a PermMap that will permute coordinate values in the same way as
   the current Frame's axes have been permuted. */
      map = astPermMap( naxes, invperm, naxes, perm, NULL, "", status );

/* Modify the Frame's relationship to the rest of the Frames in the
   FrameSet so that the correct coordinate values remain associated
   with the permuted axes. */
      astRemapFrame( this, AST__CURRENT, map );

/* Annul the PermMap and free the inverse permutation array. */
      map = astAnnul( map );
   }
   invperm = astFree( invperm );
}

static AstFrame *PickAxes( AstFrame *this_frame, int naxes, const int axes[],
                           AstMapping **map, int *status ) {
/*
*  Name:
*     PickAxes

*  Purpose:
*     Create a new Frame by picking axes from a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstFrame *PickAxes( AstFrame *this, int naxes, const int axes[],
*                         AstMapping **map, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astPickAxes protected
*     method inherited from the Frame class).

*  Description:
*     This function creates a new Frame whose axes are copies of axes
*     picked from the current Frame of an existing FrameSet. Other
*     Frame attributes are also copied from this current Frame to the
*     new Frame. Zero or more of the original axes may be picked in
*     any order, but each can be used only once. Additional axes (with
*     default characteristics) may be included in the new Frame if
*     required.
*
*     Optionally, a Mapping that converts between the original Frame's
*     axes and those of the new Frame may also be returned.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     naxes
*        The number of axes required in the new Frame.
*     axes
*        Pointer to an array of int with naxes elements. This should
*        contain (zero based) axis indices specifying the axes which
*        are to be included in the new Frame, in the order
*        required. Each axis index may occur only once.
*
*        If additional (default) axes are also to be included, the
*        corresponding elements of this array should be set to -1.
*     map
*        Address of a location to receive a pointer to a new
*        Mapping. This will be a PermMap (or a UnitMap as a special
*        case) that describes the axis permutation that has taken
*        place between the current Frame of the FrameSet and the new
*        Frame.  The forward transformation will convert from the
*        original FrameSet's axes to the new one's, and vice versa.
*
*        If this Mapping is not required, a NULL value may be supplied
*        for this parameter.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     Pointer to the new Frame.

*  Notes:
*     - The class of object returned may differ from that of the
*     original current Frame, depending on which axes are
*     selected. For example, if a single axis is picked from a
*     SkyFrame (which always has two axes), the resulting Frame cannot
*     be a valid SkyFrame, so will revert to the parent class (Frame)
*     instead.
*     - The new Frame contains a deep copy of all the data selected
*     from the original current Frame. Modifying the new Frame will
*     therefore not affect the FrameSet or the Frames it contains.
*     - A NULL pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrame *frame;              /* Pointer to Frame to be returned */
   AstFrameSet *this;            /* Pointer to FrameSet structure */

/* Initialise the returned pointers. */
   if ( map ) *map = NULL;
   frame = NULL;

/* Check the global error status. */
   if ( !astOK ) return frame;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Check that a valid set of axes is being selected . */
   astValidateAxisSelection( this, naxes, axes, "astPickAxes" );

/* Obtain a pointer to the FrameSet's current Frame and use its
   astPickAxes method to obtain the required new Frame and
   Mapping. Annul the current Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   frame = astPickAxes( fr, naxes, axes, map );
   fr = astAnnul( fr );

/* If an error occurred, annul the Mapping pointer (if requested) and
   the new Frame pointer. */
   if ( !astOK ) {
      if ( map ) *map = astAnnul( *map );
      frame = astAnnul( frame );
   }

/* Return the pointer to the new Frame. */
   return frame;
}

static void PrimaryFrame( AstFrame *this_frame, int axis1,
                          AstFrame **frame, int *axis2, int *status ) {
/*
*  Name:
*     PrimaryFrame

*  Purpose:
*     Uniquely identify a primary Frame and one of its axes.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void PrimaryFrame( AstFrame *this, int axis1, AstFrame **frame,
*                        int *axis2, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected
*     astPrimaryFrame method inherited from the Frame class).

*  Description:
*     This function returns information about the underlying (primary)
*     Frame corresponding to a specified axis of the current Frame of
*     a FrameSet, when this current Frame may be a compound Frame
*     composed of more than one simpler one.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     axis1
*        An axis index (zero-based) identifying the axis of the
*        FrameSet's current Frame for which information is required.
*     frame
*        Address of a location to receive a pointer to the underlying
*        (primary) frame to which the requested axis belongs
*        (i.e. this will not be a compound Frame).
*     axis2
*        Pointer to an int which is to receive the axis index within
*        "frame" which identifies the axis being referred to, using
*        the axis order that applied when the primary Frame was
*        originally constructed (i.e. this function undoes all
*        subsequent axis pemutations and the effects of combining
*        Frames, in order to reveal the original underlying axis
*        order).
*     status
*        Pointer to the inherited status variable.

*  Notes:
*     - This protected method is provided so that class
*     implementations can distinguish the axes of Frames from one
*     another (e.g. can distinguish a longitude axis as being
*     different from a latitide axis) even after their order has been
*     permuted and they have been combined with axes from other
*     Frames.
*     - The reference count of the primary Frame will be incremented
*     by one to reflect the new pointer returned.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */

/* Initialise the returned values. */
   *frame = NULL;
   *axis2 = 0;

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index supplied. */
   (void) astValidateAxis( this, axis1, 1, "astPrimaryFrame" );

/* Obtain a pointer to the FrameSet's current Frame and invoke its
   astPrimaryFrame method. Annul the Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   astPrimaryFrame( fr, axis1, frame, axis2 );
   fr = astAnnul( fr );

/* If an error occurred, annul the returned object and clear the
   returned axis value. */
   if ( !astOK ) {
      *frame = astAnnul( *frame );
      *axis2 = 0;
   }
}

static double Rate( AstMapping *this_mapping, double *at, int ax1, int ax2, int *status ){
/*
*  Name:
*     Rate

*  Purpose:
*     Calculate the rate of change of a Mapping output.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     result = Rate( AstMapping *this, double *at, int ax1, int ax2, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astRate method
*     inherited from the Frame class).

*     This function evaluates the rate of change of a specified output of
*     the supplied Mapping with respect to a specified input, at a
*     specified input position.
*
*     The result is estimated by interpolating the function using a
*     fourth order polynomial in the neighbourhood of the specified
*     position. The size of the neighbourhood used is chosen to minimise
*     the RMS residual per unit length between the interpolating
*     polynomial and the supplied Mapping function.

*  Parameters:
*     this
*        Pointer to the Mapping to be applied.
*     at
*        The address of an array holding the axis values at the position
*        at which the rate of change is to be evaluated. The number of
*        elements in this array should equal the number of inputs to the
*        Mapping.
*     ax1
*        The index of the Mapping output for which the rate of change is to
*        be found (output numbering starts at 0 for the first output).
*     ax2
*        The index of the Mapping input which is to be varied in order to
*        find the rate of change (input numbering starts at 0 for the first
*        input).
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     astRate()
*        The rate of change of Mapping output "ax1" with respect to input
*        "ax2", evaluated at "at", or AST__BAD if the value cannot be
*        calculated.

*  Notes:
*     - A value of AST__BAD will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*-
*/

/* Local Variables: */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   AstMapping *map;              /* Pointer to the base->current Mapping */
   double result;                /* Returned rate of change */

/* Check the global error status. */
   if ( !astOK ) return AST__BAD;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_mapping;

/* Obtain the Mapping between the base and current Frames in the
   FrameSet (note this takes account of whether the FrameSet has been
   inverted). */
   map = astGetMapping( this, AST__BASE, AST__CURRENT );

/* Invoke the astRate method on the Mapping. */
   result = astRate( map, at, ax1, ax2 );

/* Annul the Mapping pointer. */
   map = astAnnul( map );

/* Return a pointer to the output PointSet. */
   return result;
}

static void RecordIntegrity( AstFrameSet *this, int *status ) {
/*
*+
*  Name:
*     RecordIntegrity

*  Purpose:
*     Record the current integrity state of a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void RecordIntegrity( AstFrameSet *this, int *status )

*  Class Membership:
*     FrameSet member function.

*  Description:
*     This function makes a record of the current integrity state of a
*     FrameSet by taking a copy of its current Frame (it stores a
*     pointer to this copy in a static variable). If the current Frame
*     is subsequently modified, the RestoreIntegrity function can then
*     attempt to restore the FrameSet's integrity to this recorded
*     state by appropriately remapping its current Frame.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*-
*     status
*        Pointer to the inherited status variable.
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Declare the thread specific global data */
   AstFrame *current;            /* Pointer to current Frame */

/* Get a pointer to the structure holding thread-specific global data. */
   astGET_GLOBALS(this);

/* Initialise the record of the FrameSet's integrity. */
   integrity_frame = NULL;
   integrity_lost = 0;

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet's current Frame. */
   current = astGetFrame( this, AST__CURRENT );

/* Make a copy of this Frame, storing its pointer. */
   integrity_frame = astCopy( current );

/* Annul the current Frame pointer. */
   current = astAnnul( current );
}

static void RemapFrame( AstFrameSet *this, int iframe, AstMapping *map, int *status ) {
/*
*++
*  Name:
c     astRemapFrame
f     AST_REMAPFRAME

*  Purpose:
*     Modify a Frame's relationship to other Frames in a FrameSet.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "frameset.h"
c     void astRemapFrame( AstFrameSet *this, int iframe, AstMapping *map )
f     CALL AST_REMAPFRAME( THIS, IFRAME, MAP, STATUS )

*  Class Membership:
*     FrameSet method.

*  Description:
c     This function modifies the relationship (i.e. Mapping) between a
f     This routine modifies the relationship (i.e. Mapping) between a
*     specified Frame in a FrameSet and the other Frames in that
*     FrameSet.
*
*     Typically, this might be required if the FrameSet has been used
*     to calibrate (say) an image, and that image is re-binned. The
*     Frame describing the image will then have undergone a coordinate
*     transformation, and this should be communicated to the associated
c     FrameSet using this function.
f     FrameSet using this routine.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the FrameSet.
c     iframe
f     IFRAME = INTEGER (Given)
*        The index within the FrameSet of the Frame to be modified.
*        This value should lie in the range from 1 to the number of
*        Frames in the FrameSet (as given by its Nframe attribute).
c     map
f     MAP = INTEGER (Given)
*        Pointer to a Mapping whose forward transformation converts
*        coordinate values from the original coordinate system
*        described by the Frame to the new one, and whose inverse
*        transformation converts in the opposite direction.
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Notes:
*     - A value of AST__BASE or AST__CURRENT may be given for the
c     "iframe" parameter to specify the base Frame or the current
f     IFRAME argument to specify the base Frame or the current
*     Frame respectively.
*     - The relationship between the selected Frame and any other
c     Frame within the FrameSet will be modified by this function,
f     Frame within the FrameSet will be modified by this routine,
*     but the relationship between all other Frames in the FrameSet
*     remains unchanged.
*     - The number of input coordinate values accepted by the Mapping
*     (its Nin attribute) and the number of output coordinate values
*     generated (its Nout attribute) must be equal and must match the
*     number of axes in the Frame being modified.
*     - If a simple change of axis order is required, then the
c     astPermAxes function may provide a more straightforward method
f     AST_PERMAXES routine may provide a more straightforward method
*     of making the required changes to the FrameSet.
c     - This function cannot be used to change the number of Frame
f     - This routine cannot be used to change the number of Frame
*     axes. To achieve this, a new Frame must be added to the FrameSet
c     (astAddFrame) and the original one removed if necessary
c     (astRemoveFrame).
f     (AST_ADDFRAME) and the original one removed if necessary
f     (AST_REMOVEFRAME).
*     - Any variant Mappings associated with the remapped Frame (except
*     for the current variant) will be lost as a consequence of calling this
*     method (see attribute "Variant").
*--
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to Frame */
   int icur;                     /* Index of original current Frame */
   int naxes;                    /* Number of Frame axes */
   int nin;                      /* Number of Mapping input coordinates */
   int nout;                     /* Number of Mapping output coordinates */
   int varfrm;                   /* The index of the variants frame */

/* Check the global error status. */
   if ( !astOK ) return;

/* Validate and translate the Frame index supplied. */
   iframe = astValidateFrameIndex( this, iframe, "astRemapFrame" );

/* Variant Mappings from a source node to a destination node are stored
   within the Frame object associated with the destination node. But
   remapping a Frame causes the Frame to be dissociated from its original
   node, and associated with a new node, leaving the original node
   without any Frame in which to store its variant mappings. So we are
   forced to remove the variant Mappings if the Frame is remapped. We do
   this by clearing the Variant attribute before the Frame is remapped.
   This will leave the current variant as the sole Mapping between the
   original source and destination nodes. However, if the Frame being
   remapped is just a mirror for another Frame, then we do not need to
   do this since the Frame being mirrored is not itself being remapped
   and so can retain its variant mappings. So we temporarily prevent the
   remapped Frame from acting as a mirror before we clear the Variant
   attribute. */
   icur = astGetCurrent( this );
   astSetCurrent( this, iframe );

   varfrm = this->varfrm[ iframe - 1 ];
   this->varfrm[ iframe - 1 ] = 0;

   astClearVariant( this );

   this->varfrm[ iframe - 1 ] = varfrm;
   astSetCurrent( this, icur );

/* Obtain the number of input and output coordinates per point for the
   Mapping supplied. */
   nin = astGetNin( map );
   nout = astGetNout( map );

/* Obtain a pointer to the specified Frame and determine how many axes
   it has. Annul the Frame pointer afterwards. */
   fr = astGetFrame( this, iframe );
   naxes = astGetNaxes( fr );
   fr = astAnnul( fr );

/* Check that the number of input coordinates matches the number of
   Frame axes and report an error if necessary. */
   if ( astOK ) {
      if ( nin != naxes ) {
         astError( AST__NCPIN, "astRemapFrame(%s): Bad number of %s input "
                   "coordinate values (%d).", status, astGetClass( this ),
                   astGetClass( map ), nin );
         astError( AST__NCPIN, "The %s given should accept %d coordinate "
                   "value%s for each input point.", status, astGetClass( map ), naxes,
                   ( naxes == 1 ) ? "" : "s" );

/* Similarly, check that the number of output coordinates matches the
   number of Frame axes. */
      } else if ( nout != naxes ) {
         astError( AST__NCPIN, "astRemapFrame(%s): Bad number of %s output "
                   "coordinate values (%d).", status, astGetClass( this ),
                   astGetClass( map ), nout );
         astError( AST__NCPIN, "The %s given should generate %d "
                   "coordinate value%s for each output point.", status,
                   astGetClass( map ), naxes, ( naxes == 1 ) ? "" : "s" );
      }
   }

/* If there is more than one Frame present in the FrameSet, extend the
   FrameSet arrays to hold a new node. */
   if ( astOK && ( this->nframe > 1 ) ) {
      this->map = astGrow( this->map, this->nnode, sizeof( AstMapping * ) );
      this->link = astGrow( this->link, this->nnode, sizeof( int ) );
      this->invert = astGrow( this->invert, this->nnode, sizeof( int ) );

/* Clone and store a pointer to the Mapping. */
      if ( astOK ) {
         this->map[ this->nnode - 1 ] = astClone( map );

/* Add a new "link" element showing that the new node is derived from
   that of the old Frame and store the current value of the Invert
   attribute for the Mapping. */
         this->link[ this->nnode - 1 ] = this->node[ iframe - 1 ];
         this->invert[ this->nnode - 1 ] = astGetInvert( map );

/* Increment the node count and associate the modified Frame with the
   new node. */
         if ( astOK ) {
            this->nnode++;
            this->node[ iframe - 1 ] = this->nnode - 1;

/* Tidy the resulting set of nodes, because the node originally
   referenced by the Frame may no longer be needed. This also
   simplifies any compound Mapping which may result if this node is
   removed. */
            TidyNodes( this, status );
         }
      }
   }
}

static void RemoveFrame( AstFrameSet *this, int iframe, int *status ) {
/*
*++
*  Name:
c     astRemoveFrame
f     AST_REMOVEFRAME

*  Purpose:
*     Remove a Frame from a FrameSet.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "frameset.h"
c     void astRemoveFrame( AstFrameSet *this, int iframe )
f     CALL AST_REMOVEFRAME( THIS, IFRAME, STATUS )

*  Class Membership:
*     FrameSet method.

*  Description:
c     This function removes a Frame from a FrameSet. All other Frames
f     This routine removes a Frame from a FrameSet. All other Frames
*     in the FrameSet have their indices re-numbered from one (if
*     necessary), but are otherwise unchanged.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the FrameSet.
c     iframe
f     IFRAME = INTEGER (Given)
*        The index within the FrameSet of the Frame to be removed.
*        This value should lie in the range from 1 to the number of
*        Frames in the FrameSet (as given by its Nframe attribute).
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Notes:
*     - Removing a Frame from a FrameSet does not affect the
*     relationship between other Frames in the FrameSet, even if they
*     originally depended on the Frame being removed.
*     - The number of Frames in a FrameSet cannot be reduced to zero.
*     An error will result if an attempt is made to remove the only
*     remaining Frame.
*     - A value of AST__BASE or AST__CURRENT may be given for the
c     "iframe" parameter to specify the base Frame or the current
f     IFRAME argument to specify the base Frame or the current
*     Frame respectively.
*     - If a FrameSet's base or current Frame is removed, the Base or
*     Current attribute (respectively) of the FrameSet will have its
*     value cleared, so that another Frame will then assume its role
*     by default.
*     - If any other Frame is removed, the base and current Frames
*     will remain the same. To ensure this, the Base and/or Current
*     attributes of the FrameSet will be changed, if necessary, to
*     reflect any change in the indices of these Frames.
*--
*/

/* Local Variables: */
   int ifr;                      /* Loop counter for Frames */
   int ii;                       /* Base/current Frame index */

/* Check the global error status. */
   if ( !astOK ) return;

/* Validate and translate the Frame index supplied. */
   iframe = astValidateFrameIndex( this, iframe, "astRemoveFrame" );
   if ( astOK ) {

/* Reject any attempt to remove the final Frame from the FrameSet. */
      if ( this->nframe == 1 ) {
         astError( AST__REMIN, "astRemoveFrame(%s): Invalid attempt to "
                   "remove the only Frame in a %s.", status, astGetClass( this ),
                   astGetClass( this ) );

/* If OK, annul the pointer to the selected Frame. */
      } else {
         this->frame[ iframe - 1 ] = astAnnul( this->frame[ iframe - 1 ] );

/* Ensure that the variant Mappings in the Frame being removed are not
   mirrored by any other Frames in the FrameSet. */
         RemoveMirrors( this, iframe, status );

/* Any Frames that are mirroring variants in Frames higher than the
   removed Frame need to have their mirror frame indices decremented. */
         for ( ifr = 1; ifr <= this->nframe; ifr++ ) {
            if( this->varfrm[ ifr - 1 ] > iframe ) this->varfrm[ ifr - 1 ]--;
         }

/* Loop to move all subsequent Frame pointers down in the FrameSet's
   "frame" array to close the resulting gap. Also move the associated
   "node" and "varfrm" array contents in the same way. */
         for ( ifr = iframe; ifr < this->nframe; ifr++ ) {
            this->frame[ ifr - 1 ] = this->frame[ ifr ];
            this->node[ ifr - 1 ] = this->node[ ifr ];
            this->varfrm[ ifr - 1 ] = this->varfrm[ ifr ];
         }
         this->frame[ this->nframe - 1 ] = NULL;
         this->node[ this->nframe - 1 ] = -1;
         this->varfrm[ this->nframe - 1 ] = 0;

/* Decrement the Frame count. */
         this->nframe--;

/* Tidy the nodes in the FrameSet. */
         TidyNodes( this, status );

/* If the Base attribute is set and the removed Frame was the base
   Frame, then clear the attribute value so that a new base Frame will
   be selected by default. */
         if ( astTestBase( this ) ) {
            ii = astGetBase( this );
            if ( iframe == ii ) {
               astClearBase( this );

/* If the index of the removed Frame is smaller than the base Frame
   index, then decrement the Base attribute so that the same base
   Frame will be used in future. */
            } else if ( iframe < ii ) {
               astSetBase( this, ii - 1 );
            }
         }

/* Repeat the above procedure for the current Frame. */
         if ( astTestCurrent( this ) ) {
            ii = astGetCurrent( this );
            if ( iframe == ii ) {
               astClearCurrent( this );
            } else if ( iframe < ii ) {
               astSetCurrent( this, ii - 1 );
            }
         }
      }
   }
}

static void RemoveMirrors( AstFrameSet *this, int iframe, int *status ) {
/*
*  Name:
*     RemoveMirrors

*  Purpose:
*     Ensure no other Frames act as mirrors for a specified Frame.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void RemoveMirrors( AstFrameSet *this, int iframe, int *status )

*  Class Membership:
*     Private function.

*  Description:
*     This function searchs the FrameSet for Frames that are currently
*     acting as mirrors for the variant Mappings in the Frame with index
*     "iframe", and disables mirroring in any found Frames. It should be
*     used when "iframe" has its variant Mappings removed.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     iframe
*        One-based index of a Frame that has had its variant Mappings
*        removed.
*     status
*        Pointer to the inherited status variable.

*/

/* Local Variables: */
   int *frmlist;
   int ifr;
   int nfrm;

/* Check the global error status. */
   if ( !astOK ) return;

/* Iniitalise a list to hold the indices of the FRames that mirror
   "iframe". */
   nfrm = 0;
   frmlist = NULL;

/* Check each Frame in the FrameSet. */
   for( ifr = 1; ifr <= this->nframe; ifr++ ) {

/* Get the index of the Frame that defines the variant Mappings to use
   with Frame "ifr". If this is "iframe", then add "ifr" to the list of
   Frames that need to be "de-mirrored". We cannot "de-mirror" the Frame
   immediately as doing so may break a chain of mirrors, resulting in the
   Frames higher up the chain no longer being associated with "iframe". */
      if( GetVarFrm( this, ifr, status ) == iframe ) {
         frmlist = astGrow( frmlist, nfrm + 1, sizeof( *frmlist ) );
         if( astOK ) frmlist[ nfrm++ ] = ifr;
      }
   }

/* Loop round all the Frames found above that mirror "iframe". */
   for( ifr = 0; ifr < nfrm; ifr++ ) {

/* Indicate that the Frame no longer mirrors any other Frame. */
      this->varfrm[ frmlist[ ifr ] - 1 ] = 0;
   }

/* Free the list. */
   frmlist = astFree( frmlist );
}

static AstMapping *RemoveRegions( AstMapping *this_mapping, int *status ) {
/*
*  Name:
*     RemoveRegions

*  Purpose:
*     Remove any Regions from a Mapping.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstMapping *RemoveRegions( AstMapping *this, int *status )

*  Class Membership:
*     FrameSet method (over-rides the astRemoveRegions method inherited
*     from the Mapping class).

*  Description:
*     This function searches the supplied Mapping (which may be a
*     compound Mapping such as a FrameSet) for any component Mappings
*     that are instances of the AST Region class. It then creates a new
*     Mapping from which all Regions have been removed. If a Region
*     cannot simply be removed (for instance, if it is a component of a
*     parallel FrameSet), then it is replaced with an equivalent UnitMap
*     in the returned Mapping.
*
*     The implementation provided by the FrameSet class invokes the
*     astRemoveRegions method on all the component Frames and Mappings,
*     and joins the results together into a new FrameSet.

*  Parameters:
*     this
*        Pointer to the original Region.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     A pointer to the modified mapping.

*  Notes:
*     - A NULL pointer value will be returned if this function is
*     invoked with the AST error status set, or if it should fail for
*     any reason.
*/

/* Local Variables: */
   AstFrame **newfrms;           /* Array of new Frames */
   AstFrameSet *new;             /* Pointer to new FrameSet */
   AstFrameSet *this;            /* Pointer to FrameSet structure */
   AstMapping **newmaps;         /* Array of new Mappings */
   AstMapping *result;           /* Result pointer to return */
   int changed;                  /* Has any mapping been changed? */
   int i;                        /* Loop count */
   int nax;                      /* Number of Frame axes */

/* Initialise. */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get a pointer to the FrameSet. */
   this = (AstFrameSet *) this_mapping;

/* Allocate arrays to hold the modified Mapping and Frame pointers. */
   newmaps = astMalloc( sizeof( AstMapping *)*( this->nnode - 1 ) );
   newfrms = astMalloc( sizeof( AstFrame *)*( this->nframe ) );
   if( astOK ) {

/* Invoke the astRemoveRegions method on all the component Mappings. */
      changed = 0;
      for( i = 0; i < this->nnode - 1; i++ ) {
         newmaps[ i ] = astRemoveRegions( this->map[ i ] );

/* Note if any Mapping was changed. */
         if( newmaps[ i ] != this->map[ i ] ) {
            changed = 1;

/* The implementation of the astRemoveRegions method provided by the
   Region class returns a Frame rather than a UnitMap. But we need
   Mappings here, not Frames. So if the new Mapping is a Frame, replace
   it with an equivalent UnitMap. */
            if( astIsAFrame( newmaps[ i ] ) ) {
               nax = astGetNin( newmaps[ i ] );
               (void) astAnnul( newmaps[ i ] );
               newmaps[ i ] = (AstMapping *) astUnitMap( nax, " ", status );
            }
         }
      }

/* Invoke the astRemoveRegions method on all the component Frames. */
      for( i = 0; i < this->nframe; i++ ) {
         newfrms[ i ] = astRemoveRegions( this->frame[ i ] );

/* Note if any Frame was changed. */
         if( newfrms[ i ] != this->frame[ i ] ) changed = 1;
      }

/* If no component was modified, just return a clone of the supplied
   pointer. */
      if( ! changed ) {
         result = astClone( this );

/* Otherwise, we need to create a new FrameSet to return. We take a deep
   copy of the supplied FrameSet and then modify the Mappings and Frames
   so that we retain any extra information in the supplied FrameSet. */
      } else {
         new = astCopy( this );

         for( i = 0; i < this->nnode - 1; i++ ) {
            (void) astAnnul( new->map[ i ] );
            new->map[ i ] = astClone( newmaps[ i ] );
         }

         for( i = 0; i < this->nframe; i++ ) {
            (void) astAnnul( new->frame[ i ] );
            new->frame[ i ] = astClone( newfrms[ i ] );
         }

         result = (AstMapping *) new;
      }

/* Free resources. */
      for( i = 0; i < this->nnode - 1; i++ ) {
         newmaps[ i ] = astAnnul( newmaps[ i ] );
      }

      for( i = 0; i < this->nframe; i++ ) {
         newfrms[ i ] = astAnnul( newfrms[ i ] );
      }

   }

   newfrms = astFree( newfrms );
   newmaps = astFree( newmaps );

/* Annul the returned Mapping if an error has occurred. */
   if( !astOK ) result = astAnnul( result );

/* Return the result. */
   return result;
}

static void ReportPoints( AstMapping *this_mapping, int forward,
                          AstPointSet *in_points, AstPointSet *out_points, int *status ) {
/*
*  Name:
*     ReportPoints

*  Purpose:
*     Report the effect of transforming a set of points using a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "mapping.h"
*     void ReportPoints( AstMapping *this, int forward,
*                        AstPointSet *in_points, AstPointSet *out_points, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astReportPoints
*     method inherited from the Frame class).

*  Description:
*     This function reports the coordinates of a set of points before
*     and after being transformed by a FrameSet, by writing them to
*     standard output.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     forward
*        A non-zero value indicates that the FrameSet's forward
*        coordinate transformation has been applied, while a zero
*        value indicates the inverse transformation.
*     in_points
*        Pointer to a PointSet which is associated with the
*        coordinates of a set of points before the FrameSet was
*        applied.
*     out_points
*        Pointer to a PointSet which is associated with the
*        coordinates of the same set of points after the FrameSet has
*        been applied.
*     status
*        Pointer to the inherited status variable.
*/

/* Local Variables: */
   AstFrame *base_frame;         /* Pointer to current Frame */
   AstFrame *current_frame;      /* Pointer to base Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   double **ptr_in;              /* Pointer to array of input data pointers */
   double **ptr_out;             /* Pointer to array of output data pointers */
   int coord;                    /* Loop counter for coordinates */
   int ncoord_in;                /* Number of input coordinates per point */
   int ncoord_out;               /* Number of output coordinates per point */
   int npoint;                   /* Number of points to report */
   int npoint_in;                /* Number of input points */
   int npoint_out;               /* Number of output points */
   int point;                    /* Loop counter for points */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_mapping;

/* Obtain the numbers of points and coordinates associated with each
   PointSet. */
   npoint_in = astGetNpoint( in_points );
   npoint_out = astGetNpoint( out_points );
   ncoord_in = astGetNcoord( in_points );
   ncoord_out = astGetNcoord( out_points );

/* Obtain the pointers that give access to the coordinate data
   associated with each PointSet. */
   ptr_in = astGetPoints( in_points );
   ptr_out = astGetPoints( out_points );

/* In the event that both PointSets don't contain equal numbers of
   points (this shouldn't actually happen), simply use the minimum
   number. */
   npoint = ( npoint_in < npoint_out ) ? npoint_in : npoint_out;

/* Obtain pointers to the FrameSet's base and current Frames. */
   base_frame = astGetFrame( this, AST__BASE );
   current_frame = astGetFrame( this, AST__CURRENT );

/* Loop to report the effect of the transformation on each point in
   turn. */
   if ( astOK ) {
      for ( point = 0; point < npoint; point++ ) {

/* Report the input coordinates (in parentheses and separated by
   commas). Format each value for display using the appropriate
   Frame's astFormat method. */
         printf( "(" );
         for ( coord = 0; coord < ncoord_in; coord++ ) {
            printf( "%s%s", coord ? ", " : "",
                    astFormat( forward ? base_frame : current_frame,
                               coord, ptr_in[ coord ][ point ] ) );
         }

/* Similarly report the output coordinates, this time formatting
   values using the other Frame's astFormat method. */
         printf( ") --> (" );
         for ( coord = 0; coord < ncoord_out; coord++ ) {
            printf( "%s%s", coord ? ", " : "",
                    astFormat( forward ? current_frame : base_frame,
                               coord, ptr_out[ coord ][ point ] ) );
         }
         printf( ")\n" );
      }
   }

/* Annul the Frame pointers. */
   base_frame = astAnnul( base_frame );
   current_frame = astAnnul( current_frame );
}

static void Resolve( AstFrame *this_frame, const double point1[],
                     const double point2[], const double point3[],
                     double point4[], double *d1, double *d2, int *status ){
/*
*  Name:
*     Resolve

*  Purpose:
*     Resolve a vector into two orthogonal components

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void Resolve( AstFrame *this, const double point1[],
*                   const double point2[], const double point3[],
*                   double point4[], double *d1, double *d2, int *status );

*  Class Membership:
*     FrameSet member function (over-rides the protected astResolve
*     method inherited from the Frame class).

*  Description:
*     This function resolves a vector into two perpendicular components.
*     The vector from point 1 to point 2 is used as the basis vector.
*     The vector from point 1 to point 3 is resolved into components
*     parallel and perpendicular to this basis vector. The lengths of the
*     two components are returned, together with the position of closest
*     aproach of the basis vector to point 3.

*  Parameters:
*     this
*        Pointer to the Frame.
*     point1
*        An array of double, with one element for each Frame axis
*        (Naxes attribute). This marks the start of the basis vector,
*        and of the vector to be resolved.
*     point2
*        An array of double, with one element for each Frame axis
*        (Naxes attribute). This marks the end of the basis vector.
*     point3
*        An array of double, with one element for each Frame axis
*        (Naxes attribute). This marks the end of the vector to be
*        resolved.
*     point4
*        An array of double, with one element for each Frame axis
*        in which the coordinates of the point of closest approach of the
*        basis vector to point 3 will be returned.
*     d1
*        The address of a location at which to return the distance from
*        point 1 to point 4 (that is, the length of the component parallel
*        to the basis vector). Positive values are in the same sense as
*        movement from point 1 to point 2.
*     d2
*        The address of a location at which to return the distance from
*        point 4 to point 3 (that is, the length of the component
*        perpendicular to the basis vector). The value is always positive.
*     status
*        Pointer to the inherited status variable.

*  Notes:
*     - Each vector used in this function is the path of
*     shortest distance between two points, as defined by the
*     astDistance function.
*     - This function will return "bad" coordinate values (AST__BAD)
*     if any of the input coordinates has this value, or if the required
*     output values are undefined.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astResolve method. Annul the Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   astResolve( fr, point1, point2, point3, point4, d1, d2 );
   fr = astAnnul( fr );

}

static AstPointSet *ResolvePoints( AstFrame *this_frame, const double point1[],
                                   const double point2[], AstPointSet *in,
                                   AstPointSet *out, int *status ) {
/*
*  Name:
*     ResolvePoints

*  Purpose:
*     Resolve a set of vectors into orthogonal components

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstPointSet *astResolvePoints( AstFrame *this, const double point1[],
*                                    const double point2[], AstPointSet *in,
*                                    AstPointSet *out )

*  Class Membership:
*     FrameSet member function (over-rides the astResolvePoints method
*     inherited from the Frame class).

*  Description:
*     This function takes a Frame and a set of vectors encapsulated
*     in a PointSet, and resolves each one into two orthogonal components,
*     returning these two components in another PointSet.
*
*     This is exactly the same as the public astResolve method, except
*     that this method allows many vectors to be processed in a single call,
*     thus reducing the computational cost of overheads of many
*     individual calls to astResolve.

*  Parameters:
*     this
*        Pointer to the Frame.
*     point1
*        An array of double, with one element for each Frame axis
*        (Naxes attribute). This marks the start of the basis vector,
*        and of the vectors to be resolved.
*     point2
*        An array of double, with one element for each Frame axis
*        (Naxes attribute). This marks the end of the basis vector.
*     in
*        Pointer to the PointSet holding the ends of the vectors to be
*        resolved.
*     out
*        Pointer to a PointSet which will hold the length of the two
*        resolved components. A NULL value may also be given, in which
*        case a new PointSet will be created by this function.

*  Returned Value:
*     Pointer to the output (possibly new) PointSet. The first axis will
*     hold the lengths of the vector components parallel to the basis vector.
*     These values will be signed (positive values are in the same sense as
*     movement from point 1 to point 2. The second axis will hold the lengths
*     of the vector components perpendicular to the basis vector. These
*     values will always be positive.

*  Notes:
*     - The number of coordinate values per point in the input
*     PointSet must match the number of axes in the supplied Frame.
*     - If an output PointSet is supplied, it must have space for
*     sufficient number of points and 2 coordinate values per point.
*     - A null pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstPointSet *result;          /* Pointer to output PointSet */
   AstFrameSet *this;            /* Pointer to FrameSet structure */
   AstFrame *fr;                 /* Pointer to current Frame */

/* Initialise. */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astResolvePoints method. Annul the Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astResolvePoints( this, point1, point2, in, out );
   fr = astAnnul( fr );

/* Return a pointer to the output PointSet. */
   return result;

}

static void RestoreIntegrity( AstFrameSet *this, int *status ) {
/*
*+
*  Name:
*     RestoreIntegrity

*  Purpose:
*     Restore a previous integrity state for a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void RestoreIntegrity( AstFrameSet *this )

*  Class Membership:
*     FrameSet member function.

*  Description:
*     This function restores a FrameSet to a previous integrity state,
*     as recorded (in static variables) by a previous invocation of
*     the RecordIntegrity function. It does this by appropriately
*     remapping the FrameSet's current Frame, if this appears
*     necessary.

*  Parameters:
*     this
*        Pointer to the FrameSet.

*  Notes:
*     - The previous record of the FrameSet's integrity state (as
*     recorded by RecordIntegrity) is deleted by this function, even
*     if it is invoked with the global error status set.
*     - An error will result if the previous integrity state cannot be
*     restored.
*-
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Declare the thread specific global data */
   AstFrame *current;            /* Pointer to current Frame */
   AstFrameSet *cvt;             /* Pointer to conversion FrameSet */
   AstMapping *map;              /* Pointer to conversion Mapping */
   int flags;                    /* Flags associated with current frame */

/* Get a pointer to the structure holding thread-specific global data. */
   astGET_GLOBALS(this);

/* Check that a previous record of the FrameSet's integrity state has
   been made. Do not modify the FrameSet if it appears that the
   previous integrity state has not been lost (i.e. that the current
   Frame has not been modified), nor if there is only one Frame
   present. Check the global error status. */
   if ( integrity_frame && integrity_lost && ( astGetNframe( this ) > 1 ) &&
        astOK ) {

/* Obtain a pointer to the current Frame. */
      current = astGetFrame( this, AST__CURRENT );

/* Since we need to obtain a conversion between the recorded copy of
   this Frame and the current one, we must match their Domain
   attributes (otherwise conversion cannot be performed). Do this by
   changing the recorded copy as necessary. */
      if ( astTestDomain( current ) ) {
         astSetDomain( integrity_frame, astGetDomain( current ) );
      } else {
         astClearDomain( integrity_frame );
      }

/* Temporarily set both Frames AST__INTFLAG flag to indicate that the
   following call to astConvert is part of the process of restoring a
   FrameSet's integrity. Some classes of Frame (e.g. DSBSpecFrames) may
   choose to return a different Mapping in this case. */
      astSetFrameFlags( integrity_frame, astGetFrameFlags( integrity_frame )
                                         | AST__INTFLAG );
      flags = astGetFrameFlags( current );
      astSetFrameFlags( current, flags | AST__INTFLAG );

/* Obtain the required conversion FrameSet, restore the original frame
   flags and annul the current Frame pointer. */
      cvt = astConvert( integrity_frame, current, "" );
      astSetFrameFlags( current, flags );
      current = astAnnul( current );

/* If no conversion could be found, then the FrameSet's integrity
   state cannot be restored, so report an error. */
      if ( !cvt ) {
         if( astOK ) {
            astError( AST__ILOST, "%s(%s): Cannot maintain %s integrity.", status,
                      integrity_method, astGetClass( this ),
                      astGetClass( this ) );
         }

/* Otherwise, obtain a pointer to the conversion Mapping. */
      } else {
         map = astGetMapping( cvt, AST__BASE, AST__CURRENT );

/* If the Mapping is not a UnitMap (i.e. a null Mapping), then use it
   to remap the FrameSet's current Frame. */
         if ( strcmp( astGetClass( map ), "UnitMap" ) ) {
            astRemapFrame( this, AST__CURRENT, map );
         }

/* Annul the conversion Mapping and Frameset pointers. */
         map = astAnnul( map );
         cvt = astAnnul( cvt );
      }
   }

/* Delete the recorded integrity information by annulling the original
   copy of the current Frame (thus deleting it) and resetting the
   associated modification flag. */
   if ( integrity_frame ) integrity_frame = astAnnul( integrity_frame );
   integrity_lost = 0;
}

static void SetAttrib( AstObject *this_object, const char *setting, int *status ) {
/*
*  Name:
*     SetAttrib

*  Purpose:
*     Set an attribute value for a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void SetAttrib( AstObject *this, const char *setting, int *status )

*  Class Membership:
*     FrameSet member function (extends the astSetAttrib method
*     inherited from the Frame class).

*  Description:
*     This function assigns an attribute value for a FrameSet, the
*     attribute and its value being specified by means of a string of
*     the form:
*
*        "attribute= value "
*
*     Here, "attribute" specifies the attribute name and should be in
*     lower case with no white space present. The value to the right
*     of the "=" should be a suitable textual representation of the
*     value to be assigned and this will be interpreted according to
*     the attribute's data type.  White space surrounding the value is
*     only significant for string attributes.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     setting
*        Pointer to a null terminated string specifying the new
*        attribute value.
*     status
*        Pointer to the inherited status variable.

*  Attributes:
*     The set of attribute values is not fixed and is determined by
*     the current Frame. In addition, the FrameSet class defines the
*     following attributes:
*
*        Base (integer)
*        Current (integer)

*  Notes:
*     - This protected method is intended to be invoked by the Object
*     astSet method and makes additional attributes accessible to it.
*     - All attribute settings passed to this function are simply
*     passed on to the corresponding method for the FrameSet's current
*     Frame.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to a Frame within the FrameSet */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   astDECLARE_GLOBALS            /* Declare the thread specific global data */
   const char *dom;              /* Pointer to Domain string */
   int base;                     /* Base attribute value */
   int base_off;                 /* Offset of Base value string */
   int current;                  /* Current attribute value */
   int current_off;              /* Offset of Current value string */
   int id;                       /* Offset of ID string */
   int invert;                   /* Invert attribute value */
   int len;                      /* Length of setting string */
   int nc;                       /* Number of characters read by astSscanf */
   int nfrm;                     /* Number of Frames in FrameSet */
   int report;                   /* Report attribute value */
   int variant;                  /* Offset of Variant string */

/* Check the global error status. */
   if ( !astOK ) return;

/* Get a pointer to the structure holding thread-specific global data. */
   astGET_GLOBALS(this_object);

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_object;

/* Obtain the length of the setting string. */
   len = strlen( setting );

/* Test for each recognised attribute in turn, using "astSscanf" to parse the
   setting string and extract the attribute value (or an offset to it in the
   case of string values). In each case, use the value set in "nc" to check
   that the entire string was matched. Once a value has been obtained, use the
   appropriate method to set it. */

/* We first handle attributes that apply to the FrameSet as a whole
   (rather than to the current Frame). */

/* Base. */
/* ----- */
/* Read as an integer. */
   if ( nc = 0,
        ( 1 == astSscanf( setting, "base= %d %n", &base, &nc ) )
          && ( nc >= len ) ) {
      astSetBase( this, base );

/* Also allow a string. */
   } else if ( nc = 0,
               ( 0 == astSscanf( setting, "base= %n%*s %n", &base_off, &nc ) )
                 && ( nc >= len ) ) {

/* Check for "AST__CURRENT" or "Current". */
      if ( astChrMatch( "AST__CURRENT", setting + base_off ) ||
           astChrMatch( "Current", setting + base_off ) ) {
         astSetBase( this, AST__CURRENT );

/* Check for "AST__BASE" or "Base" (possible, although not very
   useful). */
      } else if ( astChrMatch( "AST__BASE", setting + base_off ) ||
                  astChrMatch( "Base", setting + base_off ) ) {

/* If the FrameSet contains a Frame with the given Domain name, make it
   the base Frame. */
      } else {
         nfrm = astGetNframe( this );
         for( base = 1; base <= nfrm; base++ ) {
            fr = astGetFrame( this, base );
            dom = astGetDomain( fr );
            fr = astAnnul( fr );
            if( astChrMatch( dom, setting + base_off ) ) break;
         }

         if( base <= nfrm ) {
            astSetBase( this, base );

/* Report an error if the value wasn't recognised. */
         } else {
            astError( AST__ATTIN, "astSetAttrib(%s): Invalid index value for "
                      "Base Frame \"%s\".", status,
                      astGetClass( this ),  setting + base_off );
         }
      }

/* Current. */
/* -------- */
/* Since this determines the choice of current Frame, we must restore
   the integrity state of the FrameSet before changing this attribute
   and record the new integrity state afterwards. */

/* Read as an integer. */
   } else if ( nc = 0,
               ( 1 == astSscanf( setting, "current= %d %n", &current, &nc ) )
                 && ( nc >= len ) ) {
      RestoreIntegrity( this, status );
      astSetCurrent( this, current );
      RecordIntegrity( this, status );

/* Also allow a string. */
   } else if ( nc = 0,
               ( 0 == astSscanf( setting, "current= %n%*s %n",
                              &current_off, &nc ) )
                 && ( nc >= len ) ) {

/* Check for "AST__BASE" or "Base". */
      if ( astChrMatch( "AST__BASE", setting + current_off ) ||
           astChrMatch( "Base", setting + current_off ) ) {
         RestoreIntegrity( this, status );
         astSetCurrent( this, AST__BASE );
         RecordIntegrity( this, status );

/* Check for "AST__CURRENT" or "Current" (possible, although not very
   useful). */
      } else if ( astChrMatch( "AST__CURRENT", setting + current_off ) ||
                  astChrMatch( "Current", setting + current_off ) ) {

/* If the FrameSet contains a Frame with the given Domain name, make it
   the current Frame. */
      } else {
         nfrm = astGetNframe( this );
         for( current = 1; current <= nfrm; current++ ) {
            fr = astGetFrame( this, current );
            dom = astGetDomain( fr );
            fr = astAnnul( fr );
            if( astChrMatch( dom, setting + current_off ) ) break;
         }

         if( current <= nfrm ) {
            RestoreIntegrity( this, status );
            astSetCurrent( this, current );
            RecordIntegrity( this, status );

/* Report an error if the value wasn't recognised. */
         } else {
            astError( AST__ATTIN, "astSetAttrib(%s): Invalid index value for "
                      "Current Frame \"%s\".", status,
                      astGetClass( this ),  setting + current_off );
         }
      }

/* ID. */
/* --- */
   } else if ( nc = 0, ( 0 == astSscanf( setting, "id=%n%*[^\n]%n", &id, &nc ) )
                       && ( nc >= len ) ) {
      astSetID( this, setting + id );

/* Ident. */
/* ------ */
   } else if ( nc = 0, ( 0 == astSscanf( setting, "ident=%n%*[^\n]%n", &id, &nc ) )
                       && ( nc >= len ) ) {
      astSetIdent( this, setting + id );

/* Invert. */
/* ------- */
/* Since this affects the choice of current Frame, we must restore the
   integrity state of the FrameSet before changing this attribute and
   record the new integrity state afterwards. */
   } else if ( nc = 0,
        ( 1 == astSscanf( setting, "invert= %d %n", &invert, &nc ) )
        && ( nc >= len ) ) {
      RestoreIntegrity( this, status );
      astSetInvert( this, invert );
      RecordIntegrity( this, status );

/* Report. */
/* ------- */
   } else if ( nc = 0,
        ( 1 == astSscanf( setting, "report= %d %n", &report, &nc ) )
        && ( nc >= len ) ) {
      astSetReport( this, report );

/* Variant. */
/* -------- */
   } else if ( nc = 0,
               ( 0 == astSscanf( setting, "variant=%n%*[^\n]%n", &variant, &nc ) )
               && ( nc >= len ) ) {
      astSetVariant( this, setting + variant );

/* Define a macro to see if the setting string matches any of the
   read-only attributes of this class. */
#define MATCH(attrib) \
        ( nc = 0, ( 0 == astSscanf( setting, attrib "=%*[^\n]%n", &nc ) ) && \
                  ( nc >= len ) )

/* If the attribute was not recognised, use this macro to report an error
   if a read-only attribute has been specified. */
   } else if ( MATCH( "allvariants" ) ||
               MATCH( "class" ) ||
               MATCH( "nframe" ) ||
               MATCH( "nin" ) ||
               MATCH( "nobject" ) ||
               MATCH( "nout" ) ||
               MATCH( "refcount" ) ||
               MATCH( "tranforward" ) ||
               MATCH( "traninverse" ) ) {
      astError( AST__NOWRT, "astSet: The setting \"%s\" is invalid for a %s.", status,
                setting, astGetClass( this ) );
      astError( AST__NOWRT, "This is a read-only attribute." , status);

/* Pass unrecognised settings on to the FrameSet's current Frame for
   further interpretation. */
   } else {

/* Force a copy to be made of the current Frame, if needed, to make it
   independent of other Frames within the FrameSet. */
      (void) ForceCopy( this, AST__CURRENT, status );

/* Obtain a pointer to the current Frame and invoke its astSetAttrib
   method. Annul the Frame pointer afterwards. */
      fr = astGetFrame( this, AST__CURRENT );
      astSetAttrib( fr, setting );
      fr = astAnnul( fr );

/* Note that the current Frame has been modified. */
      integrity_lost = 1;
   }

/* Undefine macros local to this function. */
#undef MATCH
}

static void SetAxis( AstFrame *this_frame, int axis, AstAxis *newaxis, int *status ) {
/*
*  Name:
*     SetAxis

*  Purpose:
*     Set a new Axis for a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void SetAxis( AstFrame *this, int axis, AstAxis *newaxis, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astSetAxis method
*     inherited from the Frame class).

*  Description:
*     This function allows a new Axis object to be associated with one
*     of the axes of the current Frame in a FrameSet, replacing the
*     previous one. Each Axis object contains a description of the
*     quantity represented along one of the Frame's axes, so this
*     function allows this description to be exchanged for another
*     one.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     axis
*        The index (zero-based) of the axis whose associated Axis
*        object is to be replaced.
*     newaxis
*        Pointer to the new Axis object.
*     status
*        Pointer to the inherited status variable.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index supplied. */
   (void) astValidateAxis( this, axis, 1, "astSetAxis" );

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astSetAxis method to assign the new Axis object. Annul the
   Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   astSetAxis( fr, axis, newaxis );
   fr = astAnnul( fr );
}

static void SetBase( AstFrameSet *this, int iframe, int *status ) {
/*
*+
*  Name:
*     astSetBase

*  Purpose:
*     Set a value for the Base attribute of a FrameSet.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "frameset.h"
*     void astSetBase( AstFrameSet *this, int iframe )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function sets a value for the Base attribute of a FrameSet. This
*     value is an index that identifies the base Frame for the FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     iframe
*        Value to be set for the Base attribute.

*  Notes:
*     - A value of AST__BASE or AST__CURRENT may be given for the
*     "iframe" parameter to identify the base Frame or the current
*     Frame respectively.
*-
*/

/* Local Variables: */
   int invert;                    /* FrameSet is inverted? */

/* Check the global error status. */
   if ( !astOK ) return;

/* Validate and translate the Frame index supplied. */
   iframe = astValidateFrameIndex( this, iframe, "astSetBase" );

/* Determine if the FrameSet has been inverted. */
   invert = astGetInvert( this );

/* If it has not been inverted, set the base Frame index, otherwise
   set the current Frame index instead. */
   if ( astOK ) *( invert ? &this->current : &this->base ) = iframe;
}

static void SetCurrent( AstFrameSet *this, int iframe, int *status ) {
/*
*+
*  Name:
*     astSetCurrent

*  Purpose:
*     Set a value for the Current attribute of a FrameSet.

*  Type:
*     Protected function.

*  Synopsis:
*     #include "frameset.h"
*     int astSetCurrent( AstFrameSet *this, int iframe )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function sets a value for the Current attribute of a
*     FrameSet. This attribute is an index that identifies the current
*     Frame for the FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     iframe
*        Value to be set for the Current attribute.

*  Notes:
*     - A value of AST__BASE or AST__CURRENT may be given for the
*     "iframe" parameter to identify the base Frame or the current
*     Frame respectively.
*-
*/

/* Local Variables: */
   int invert;                   /* FrameSet is inverted? */

/* Check the global error status. */
   if ( !astOK ) return;

/* Validate and translate the Frame index supplied. */
   iframe = astValidateFrameIndex( this, iframe, "astSetCurrent" );

/* Determine if the FrameSet has been inverted. */
   invert = astGetInvert( this );

/* If it has not been inverted, set the current frame index, otherwise
   set the base Frame index instead. */
   if ( astOK ) *( invert ? &this->base : &this->current ) = iframe;
}

static void SetVariant( AstFrameSet *this, const char *variant, int *status ) {
/*
*+
*  Name:
*     astSetVariant

*  Purpose:
*     Set a value for the Variant attribute of a FrameSet.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "frameset.h"
*     void astSetVariant( AstFrameSet *this,  const char *variant )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function sets a value for the Variant attribute of a FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     variant
*        Value to be set for the Variant attribute.

*  Notes:
*     - An error will be reported if the supplied variant name cannot be
*     found in the Variants FrameSet associated with the current Frame.

*-
*/

/* Local Variables: */
   AstCmpMap *map6;
   AstCmpMap *map5;
   AstCmpMap *map4;
   AstFrame *frm;
   AstFrame *vfrm;
   AstFrameSet *tfs;
   AstFrameSet *vfs;
   AstMapping *map0;
   AstMapping *map2;
   AstMapping *map3;
   AstMapping *map1;
   char *myvar;
   const char *dom;
   int icur;
   int ifrm;
   int inode;
   int inv0;
   int inv;
   int nfrm;

/* Check the global error status. */
   if ( !astOK ) return;

/* Get a copy of the supplied string and clean it. */
   myvar = astStore( NULL, variant, strlen( variant ) + 1 );
   astRemoveLeadingBlanks( myvar );
   astChrCase( NULL, myvar, 1, 0 );
   if( astOK ) {
      myvar[ astChrLen( myvar ) ] = 0;

/* Get the one-based index of the Frame that defines the available
   variant Mappings. */
      icur = GetVarFrm( this, astGetCurrent( this ), status );

/* Get the variants FrameSet from the Frame selected above. */
      frm = astGetFrame( this, icur );
      vfs = astGetFrameVariants( frm );

/* If there is no variants FrameSet in the Frame, the only allowed value
   for "Variant" is the Domain name of the current Frame. */
      if( ! vfs ) {
         dom = astGetDomain( this );
         if( astOK && strcmp( myvar, dom ) ) {
            astError( AST__ATTIN, "astSetVariant(%s): Unknown Frame "
                      "variant '%s' requested.", status, astGetClass( this ),
                      myvar );
         }

/* If there is a variants FrameSet in the Frame... */
      } else {

/* Find the index of the Frame in the Variants FrameSet that has a Domain
   equal to myvar. */
         nfrm = astGetNframe( vfs );
         for( ifrm = 0; ifrm < nfrm; ifrm++ ) {
            vfrm = astGetFrame( vfs, ifrm + 1 );
            dom = astGetDomain( vfrm );
            vfrm = astAnnul( vfrm );
            if( !astOK || !strcmp( myvar, dom ) ) break;
         }

/* Report an error if no such Frame found. */
         if( ifrm == nfrm && astOK ) {
            astError( AST__ATTIN, "astSetVariant(%s): Unknown Frame "
                      "variant '%s' requested - available variants are "
                      "'%s'.", status, astGetClass(this), myvar,
                      astGetAllVariants(this) );

/* Otherwise, get a Mapping from the current Frame in "this" to the
   currently selected Variant Frame. We cannot assume that they are the
   same as attributes of the current Frame (e.g. System) may have been
   changed since the variant was added. If the required Frame is already
   the current Frame, there is nothing more to do since the required
   variant is already selected. */
         } else if( ifrm + 1 != astGetCurrent( vfs ) ){
            vfrm = astGetFrame( vfs, AST__CURRENT );
            dom = astGetDomain( frm );
            if( dom ) dom = astStore( NULL, dom, strlen( dom ) + 1 );
            astSetDomain( frm, astGetDomain( vfrm ) );
            tfs = astConvert( frm, vfrm, "" );
            astSetDomain( frm, dom );
            if( tfs ) {
               map1 = astGetMapping( tfs, AST__BASE, AST__CURRENT );
               tfs = astAnnul( tfs );
               vfrm = astAnnul( vfrm );

/* Get the Mapping from the original Variant Frame to the requested variant
   Frame. */
               map2 = astGetMapping( vfs, AST__CURRENT, ifrm + 1 );

/* Get a Mapping from the new variant Frame to the current Frame in "this". */
               vfrm = astGetFrame( vfs, ifrm + 1 );
               astSetDomain( frm, astGetDomain( vfrm ) );
               tfs = astConvert( vfrm, frm, "" );
               astSetDomain( frm, dom );
               if( tfs ) {
                  map3 = astGetMapping( tfs, AST__BASE, AST__CURRENT );
                  tfs = astAnnul( tfs );

/* Concatentate the three Mappings, to get the Mapping from the old
   variant Frame to the new variant Frame. */
                  map4 = astCmpMap( map1, map2, 1, " ", status );
                  map5 = astCmpMap( map4, map3, 1, " ", status );

/* Now we modify the Mapping in the FrameSet. First get the index of the node
   with which the Frame is associated. */
                  inode = this->node[ icur - 1 ];

/* Get the Mapping that generates the node values, and its Invert flag. */
                  map0 = this->map[ inode - 1 ];
                  inv0 = this->invert[ inode - 1 ];

/* Temporarily reset the invert flag in the Mapping to account for any
   changes made to the Mapping via other pointers. */
                  inv = astGetInvert( map0 );
                  astSetInvert( map0, inv0 );

/* Concatentate with "map5" to get the Mapping form the the parent node
   to the new variant of the current node. */
                  map6 = astCmpMap( map0, map5, 1, " ", status );

/* Simplify it and use it to replace the Mapping in the FrameSet structure. */
                  this->map[ inode - 1 ] = astSimplify( map6 );
                  this->invert[ inode - 1 ] = astGetInvert( this->map[ inode - 1 ] );

/* Re-instate the original Invert flag and free the old Mapping pointer. */
                  astSetInvert( map0, inv );
                  map0 = astAnnul( map0 );

/* Make the variant Frame the current Frame within the Variants FrameSet. */
                  astSetCurrent( vfs, ifrm + 1 );

/* Free resources. */
                  map6 = astAnnul( map6 );
                  map5 = astAnnul( map5 );
                  map4 = astAnnul( map4 );
                  map3 = astAnnul( map3 );

/* Report an error if a Mapping cannot be found from the new variant Frame
   to the current Frame in "this". */
               } else if( astOK ) {
                  astError( AST__INTER, "astSetVariant(%s): Cannot convert "
                            "from a %s with Domain '%s' to a %s with Domain "
                            "'%s' (internal programming error).", status,
                            astGetClass( this ), astGetClass( vfrm ),
                            astGetDomain( vfrm ), astGetClass( frm ),
                            astGetDomain( frm ) );
               }

/* Free resources. */
               map2 = astAnnul( map2 );
               map1 = astAnnul( map1 );

/* Report an error if a Mapping cannot be found from the current Frame in
   "this" to the current Variant Frame. */
            } else if( astOK ) {
               astError( AST__INTER, "astSetVariant(%s): Cannot convert "
                         "from a %s with Domain '%s' to a %s with Domain "
                         "'%s' (internal programming error).", status,
                         astGetClass( this ), astGetClass( frm ),
                         astGetDomain( frm ), astGetClass( vfrm ),
                         astGetDomain( vfrm ) );
            }

/* Free resources. */
            vfrm = astAnnul( vfrm );
            dom = astFree( (void *) dom );
         }

/* Annul the pointer to the Variants FrameSet. */
         vfs = astAnnul( vfs );
      }

/* Annul the pointer to the current Frame in "this". */
      frm = astAnnul( frm );
   }

/* Free the memory holding the cleaned variant name. */
   myvar = astFree( myvar );
}

static AstMapping *Simplify( AstMapping *this_mapping, int *status ) {
/*
*  Name:
*     Simplify

*  Purpose:
*     Simplify the Mappings in a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstMapping *Simplify( AstMapping *this, int *status )

*  Class Membership:
*     FrameSet method (over-rides the astSimplify method inherited
*     from the Frame class).

*  Description:
*     This function simplifies the Mappings in a FrameSet to eliminate
*     redundant computational steps, or to merge separate steps which
*     can be performed more efficiently in a single operation.

*  Parameters:
*     this
*        Pointer to the original FrameSet.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     A new pointer to the (possibly simplified) FrameSet. If
*     simplification was not possible, this will be a cloned pointer
*     to the original FrameSet.

*  Notes:
*     - A NULL pointer value will be returned if this function is
*     invoked with the AST error status set, or if it should fail for
*     any reason.
*/

/* Local Variables: */
   AstFrameSet *new;             /* Pointer to new (simpler?) FrameSet */
   AstFrameSet *this;            /* Pointer to original FrameSet structure */
   AstMapping *map;              /* Pointer to Mapping */
   AstMapping *result;           /* Result pointer to return */
   AstMapping *tmp;              /* Temporary Mapping pointer */
   int inode;                    /* Loop counter for FrameSet nodes */
   int inv;                      /* Mapping Invert attribute value */
   int invert;                   /* Invert flag value */
   int set;                      /* Invert attribute set? */
   int simpler;                  /* Simplification achieved? */

/* Initialise. */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_mapping;

/* Make a copy of the FrameSet, since we may alter it (this is a deep
   copy, which is a minor limitation of the current implementation). */
   new = astCopy( this );

/* Loop to examine each of the Mappings between the Frames in the
   copy. */
   simpler = 0;
   for ( inode = 1; astOK && ( inode < new->nnode ); inode++ ) {

/* Obtain the Mapping pointer and associated invert flag. */
      map = new->map[ inode - 1 ];
      invert = new->invert[ inode - 1 ];

/* Determine if the Mapping's Invert attribute is set, and obtain its
   value. */
      set = astTestInvert( map );
      inv = astGetInvert( map );

/* If necessary, set the required value for the Invert attribute. */
      if ( inv != invert ) astSetInvert( map, invert );

/* Simplify the Mapping. */
      tmp = astSimplify( map );

/* If necessary, restore the original state of the Mapping's Invert
   attribute. */
      if ( inv != invert ) {
         if ( set ) {
            astSetInvert( map, inv );
         } else {
            astClearInvert( map );
         }
      }

/* Test if simplification was performed. */
      if ( astOK ) {
         if ( tmp != map ) {

/* If so, annul the original Mapping pointer and substitute the new
   one. Also set a new invert flag to accompany it. */
            (void) astAnnul( new->map[ inode - 1 ] );
            new->map[ inode - 1 ] = astClone( tmp );
            new->invert[ inode - 1 ] = astGetInvert( tmp );

/* Note if any Mapping within the FrameSet is simplified. */
            simpler = 1;
         }
      }

/* Annul the pointer to the simplified Mapping. */
      tmp = astAnnul( tmp );
   }

/* If simplification was possible, clone a pointer to the new
   FrameSet. Otherwise clone a pointer to the original one. */
   if ( astOK ) result = astClone( simpler ? new : this );

/* Annul the new FrameSet pointer. */
   new = astAnnul( new );

/* If an error occurred, annul the returned pointer. */
   if ( !astOK ) result = astAnnul( result );

/* Return the result. */
   return result;
}

static int Span( AstFrameSet *this, AstFrame **frames, int inode1, int inode2,
                 int avoid, AstMapping **map, int *forward, int *status ) {
/*
*  Name:
*     Span

*  Purpose:
*     Find a path between two nodes in a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int Span( AstFrameSet *this, AstFrame **frames, int inode1, int inode2,
*               int avoid, AstMapping **map, int *forward, int *status )

*  Class Membership:
*     FrameSet member function.

*  Description:
*     This function searches a FrameSet to identify a path between two
*     specified nodes. It returns an array of pointers to each Mapping
*     in the path, along with direction information, so that an
*     overall Mapping between the two nodes can be constructed.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     frames
*        Pointer to an array of Frame pointers, indexed by node index.
*        Nodes which have no associated Frame will have a NULL pointer
*        stored in this array.
*     inode1
*        Zero based index of the starting node.
*     inode2
*        Zero based index of the ending node.
*     avoid
*        Zero based index which identifies a node which is to be
*        avoided (i.e. the initial step in the path should not be via
*        this node). This value is required because the function
*        invokes itself recursively; it provides a mechanism to
*        prevent searches proceeding back down paths that have already
*        been searched. External callers should provide a value of -1,
*        which indicates that all possible paths should initially be
*        explored.
*     map
*        Pointer to the start of an array that will be filled with a
*        series of pointers to Mappings which must be applied in turn
*        in order to transform between the two Frames. External
*        callers should ensure that this array contains at least as many
*        elements as there are Mappings and Frames in the FrameSet (one less
*        than the number of nodes plus the number of Frames).
*
*        Note that the pointers are simply copies of addresses from
*        the FrameSet's "map" array. They are not cloned, so should
*        not be annulled by the caller.
*     forward
*        Pointer to the start of an array of int that will be filled
*        with boolean flags (0 or 1) to indicate whether the forward
*        (as opposed to the inverse) transformation should be used for
*        each Mapping returned in order to effect the transformation
*        between the starting and ending nodes. This array should be the
*        same size as the "map" array.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The function returns one more than the number of Mappings
*     required to perform the transformation, or zero if it was not
*     possible to find a path between the two nodes.

*  Notes:
*     - If a node has an associated Frame, the Frame usually represents a
*     UnitMap and so can be ignored. The exception is if the Frame is
*     actually a Region (or a CmpFrame containing a Region), in which case
*     it represents a Mapping which returns bad values if the input position
*     is outside the region. This form of Mapping should not be ignored, and
*     so the returned list of Mappings includes the effect of any Frames
*     along the path which are not equivalent to a UnitMap. This
*     equivalence is determined by invoking the astSimplify method on the
*     Frame.
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*     - On the assumption that the FrameSet has been consistently
*     constructed, there should be exactly one path between any pair
*     of its nodes.  It should not, therefore, ever fail to find a
*     path except when invoked recursively to explore a subset of the
*     FrameSet's nodes (this should not be visible to an external
*     caller).  Failure to find a path does not in itself result in an
*     error condition.
*/

/* Local Variables: */
   AstFrame *frame;          /* Pointer to Frame associated with inode1 */
   int fwd;                  /* Forward Mapping identified? */
   int inode;                /* Loop counter for nodes */
   int inv;                  /* Inverse Mapping identified? */
   int invert;               /* Original Mapping Invert value */
   int nextra;               /* No. of extra Mappings to add to path */
   int result;               /* Count of mappings (to be returned) */

/* Check the global error status. */
   if ( !astOK ) return 0;

/* See if the two nodes are the same. */
   result = ( inode1 == inode2 );

/* If so, we need to consider the Mapping represented by any Frame
   associated with the node. Most classes of Frames are equivalent to a
   UnitMap and so can be ignored. But some (e.g. the Region class) are not
   equivalent to a UnitMap and so needs to be included in the returned
   Mapping list. */
   if( result ) {
      result = 1;

/* If inode1 is associated with a Frame, which is not equivalent to a
   UnitMap, add the Frame as the first Mapping into the returned list. The
   "forward" value is irrelevant since the forward and inverse transformations
   of Frames are the same. */
      frame = frames[ inode1 ];
      if( frame ) {
         if( !astIsUnitFrame( frame ) ) {
            result++;
            *map = (AstMapping *) frame;
            *forward = 1;
         }
      }

/* If the nodes are different, we now attempt to find the next step in
   the path between them. Loop through all available nodes looking for
   the next one to transform to (i.e. one that is directly related by
   a Mapping to our starting node). */
   } else {
      for ( inode = 0; inode < this->nnode; inode++ ) {

/* Do not consider node "avoid". This prevents us re-tracing our steps
   backwards when this function is invoked recursively. */
         if ( inode != avoid ) {

/* Test if inode is derived from inode1 (if so, the Mapping associated
   with inode will convert from inode1 to inode when applied in the
   forward direction). */
            fwd = ( inode > 0 ) && ( this->link[ inode - 1 ] == inode1 );

/* Test if inode1 is derived from inode (if so, the Mapping associated
   with inode1 will convert from inode1 to inode when applied in the
   inverse direction). */
            inv = ( inode1 > 0 ) && ( this->link[ inode1 - 1 ] == inode );

/* If the nodes are directly related, we try to find a path from inode to
   inode2 without going back through inode1. */
            if ( fwd || inv ) {

/* If node1 is associated with a Frame, we need to include the Frame
   as a Mapping in the returned list unless the Frame is equivalent to a
   UnitMap. Note the number of slots to be reserved for node1 when we call
   Span recursively below. */
               nextra = 1;
               frame = frames[ inode1 ];
               if( frame && !astIsUnitFrame( frame ) ) nextra = 2;

/* Invoke this function recursively to try and find a path from inode
   to inode2 without going back through inode1. If this is possible, a
   non-zero result will be returned. Store the returned Mappings and
   direction information in the arrays supplied, but leave extra space to
   insert information about the Mapping between nodes inode1 and inode. */
               result = Span( this, frames, inode, inode2, inode1,
                              map + nextra, forward + nextra, status );

/* If a path was found, increment the Mapping count to account for the
   one that transforms between nodes inode1 and inode and insert
   information for this Mapping into the output arrays. */
               if ( result ) {
                  result++;
                  nextra--;
                  map[ nextra ] = this->map[ ( fwd ? inode : inode1 ) - 1 ];
                  forward[ nextra ] = fwd;

/* Obtain the original value of the Invert attribute for the Mapping
   between nodes inode1 and inode (recorded when the Mapping was first
   added to the FrameSet). Test if this value has now changed. If so,
   some external code has inverted the Mapping via another pointer, so
   invert the returned direction information to compensate for
   this. */
                  invert = this->invert[ ( fwd ? inode : inode1 ) - 1 ];
                  if ( invert != astGetInvert( map[ nextra ] ) ) {
                     forward[ nextra ] = !forward[ nextra ];
                  }

/* If inode1 is associated with a non-unit Frame Mapping, add the Frame
   Mapping in as the first Mapping in the returned list. The "forward" value
   is irrelevant since the forward and inverse transformations of Frames
   are the same. */
                  if( nextra ) {
                     result++;
                     *map = (AstMapping *) frame;
                     *forward = 1;
                  }

/* Quit searching once a path has been found. */
                  break;
	       }
	    }
         }
      }
   }

/* Return the result, which is one more than the number of mappings
   found (i.e. steps in the path), or zero if no path was found (this
   should only occur when invoked recursively to explore an
   unsuccessful sub-path). */
   return result;
}

static int SubFrame( AstFrame *this_frame, AstFrame *template,
                     int result_naxes,
                     const int *target_axes, const int *template_axes,
                     AstMapping **map, AstFrame **result, int *status ) {
/*
*  Name:
*     SubFrame

*  Purpose:
*     Select axes from a FrameSet and convert to the new coordinate system.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int SubFrame( AstFrame *target, AstFrame *template, int result_naxes,
*                   const int *target_axes, const int *template_axes,
*                   AstMapping **map, AstFrame **result, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astSubFrame
*     method inherited from the Frame class).

*  Description:
*     This function selects a requested sub-set (or super-set) of the
*     axes from the current Frame of a "target" FrameSet and creates a
*     new Frame with copies of the selected axes assembled in the
*     requested order. It then optionally overlays the attributes of a
*     "template" Frame on to the result. It returns both the resulting
*     Frame and a Mapping that describes how to convert between the
*     coordinate systems described by the current Frame of the target
*     FrameSet and the result Frame. If necessary, this Mapping takes
*     account of any differences in the Frames' attributes due to the
*     influence of the template.

*  Parameters:
*     target
*        Pointer to the target FrameSet, from whose current Frame the
*        axes are to be selected.
*     template
*        Pointer to the template Frame, from which new attributes for
*        the result Frame are to be obtained. Optionally, this may be
*        NULL, in which case no overlaying of template attributes will
*        be performed.
*     result_naxes
*        Number of axes to be selected from the target FrameSet. This
*        number may be greater than or less than the number of axes in
*        the FrameSet's current Frame (or equal).
*     target_axes
*        Pointer to an array of int with result_naxes elements, giving
*        a list of the (zero-based) axis indices of the axes to be
*        selected from the current Frame of the target FrameSet. The
*        order in which these are given determines the order in which
*        the axes appear in the result Frame. If any of the values in
*        this array is set to -1, the corresponding result axis will
*        not be derived from the target FrameSet, but will be assigned
*        default attributes instead.
*     template_axes
*        Pointer to an array of int with result_naxes elements. This
*        should contain a list of the template axes (given as
*        zero-based axis indices) with which the axes of the result
*        Frame are to be associated. This array determines which axes
*        are used when overlaying axis-dependent attributes of the
*        template on to the result. If any element of this array is
*        set to -1, the corresponding result axis will not receive any
*        template attributes.
*
*        If the template argument is given as NULL, this array is not
*        used and a NULL pointer may also be supplied here.
*     map
*        Address of a location to receive a pointer to the returned
*        Mapping.  The forward transformation of this Mapping will
*        describe how to convert coordinates from the coordinate
*        system described by the current Frame of the target FrameSet
*        to that described by the result Frame. The inverse
*        transformation will convert in the opposite direction.
*     result
*        Address of a location to receive a pointer to the result Frame.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     A non-zero value is returned if coordinate conversion is
*     possible between the current Frame of the target FrameSet and
*     the result Frame. Otherwise zero is returned and *map and
*     *result are returned as NULL (but this will not in itself result
*     in an error condition). In general, coordinate conversion should
*     always be possible if no template Frame is supplied but may not
*     always be possible otherwise.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to FrameSet's current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   int match;                    /* Result to be returned */

/* Initialise. */
   *map = NULL;
   *result = NULL;
   match = 0;

/* Check the global error status. */
   if ( !astOK ) return match;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame. */
   fr = astGetFrame( this, AST__CURRENT );

/* Invoke the astSubFrame method for this Frame. */
   match = astSubFrame( fr, template, result_naxes, target_axes, template_axes,
                        map, result );

/* Annul the Frame pointer. */
   fr = astAnnul( fr );

/* If an error occurred, clean up by annulling any returned objects and clear
   the returned result. */
   if ( !astOK ) {
      *map = astAnnul( *map );
      *result = astAnnul( *result );
      match = 0;
   }

/* Return the result. */
   return match;
}

static AstSystemType SystemCode( AstFrame *this_frame, const char *system, int *status ) {
/*
*  Name:
*     SystemCode

*  Purpose:
*     Convert a string into a coordinate system type code.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstSystemType SystemCode( AstFrame *this, const char *system, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astSystemCode
*     method inherited from the Frame class).

*  Description:
*     This function converts a string used for the external description of
*     a coordinate system into a Frame coordinate system type code (System
*     attribute value). It is the inverse of the astSystemString function.

*  Parameters:
*     this
*        Pointer to the Frame.
*     system
*        Pointer to a constant null-terminated string containing the
*        external description of the coordinate system.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The System type code.

*  Notes:
*     - A value of AST__BADSYSTEM is returned if the coordinate system
*     description was not recognised. This does not produce an error.
*     - A value of AST__BADSYSTEM is also returned if this function
*     is invoked with the global error status set or if it should fail
*     for any reason.
*/

/* Local Variables: */
   AstSystemType result;      /* Result value to return */
   AstFrame *fr;              /* Pointer to FrameSet's current Frame */
   AstFrameSet *this;         /* Pointer to the FrameSet structure */

/* Initialise. */
   result = AST__BADSYSTEM;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke the
   astSystemCode method for this Frame. Annul the Frame pointer afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astSystemCode( fr, system );
   fr = astAnnul( fr );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = AST__BADSYSTEM;

/* Return the result. */
   return result;
}

static const char *SystemString( AstFrame *this_frame, AstSystemType system, int *status ) {
/*
*  Name:
*     SystemString

*  Purpose:
*     Convert a coordinate system type code into a string.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     const char *SystemString( AstFrame *this, AstSystemType system, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astSystemString
*     method inherited from the Frame class).

*  Description:
*     This function converts a Frame coordinate system type code
*     (System attribute value) into a string suitable for use as an
*     external representation of the coordinate system type.

*  Parameters:
*     this
*        Pointer to the Frame.
*     system
*        The coordinate system type code.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     Pointer to a constant null-terminated string containing the
*     textual equivalent of the type code supplied.

*  Notes:
*     - A NULL pointer value is returned if the coordinate system
*     code was not recognised. This does not produce an error.
*     - A NULL pointer value is also returned if this function is
*     invoked with the global error status set or if it should fail
*     for any reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to FrameSet's current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   const char *result;           /* Pointer value to return */

/* Initialise. */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke the
   astSystemString method for this Frame. Annul the Frame pointer
   afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astSystemString( fr, system );
   fr = astAnnul( fr );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = NULL;

/* Return the result pointer. */
   return result;

}

static int TestAttrib( AstObject *this_object, const char *attrib, int *status ) {
/*
*  Name:
*     TestAttrib

*  Purpose:
*     Test if a specified attribute value is set for a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int TestAttrib( AstObject *this, const char *attrib, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astTestAttrib protected
*     method inherited from the Frame class).

*  Description:
*     This function returns a boolean result (0 or 1) to indicate whether
*     a value has been set for one of a FrameSet's attributes.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     attrib
*        Pointer to a null terminated string specifying the attribute
*        name.  This should be in lower case with no surrounding white
*        space.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     One if a value has been set, otherwise zero.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   int result;                   /* Result value to return */

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_object;

/* Check the attribute name and test the appropriate attribute. */

/* We first handle attributes that apply to the FrameSet as a whole
   (rather than to the current Frame). */

/* Base. */
/* ----- */
   if ( !strcmp( attrib, "base" ) ) {
      result = astTestBase( this );

/* Current. */
/* -------- */
   } else if ( !strcmp( attrib, "current" ) ) {
      result = astTestCurrent( this );

/* ID. */
/* --- */
   } else if ( !strcmp( attrib, "id" ) ) {
      result = astTestID( this );

/* Ident. */
/* ------ */
   } else if ( !strcmp( attrib, "ident" ) ) {
      result = astTestIdent( this );

/* Invert. */
/* ------- */
   } else if ( !strcmp( attrib, "invert" ) ) {
      result = astTestInvert( this );

/* Report. */
/* ------- */
   } else if ( !strcmp( attrib, "report" ) ) {
      result = astTestReport( this );

/* Variant. */
/* -------- */
   } else if ( !strcmp( attrib, "variant" ) ) {
      result = astTestVariant( this );

/* If the name is not recognised, test if it matches any of the
   read-only attributes of this class. If it does, then return
   zero. */
   } else if ( !strcmp( attrib, "allvariants" ) ||
               !strcmp( attrib, "class" ) ||
               !strcmp( attrib, "nframe" ) ||
               !strcmp( attrib, "nin" ) ||
               !strcmp( attrib, "nobject" ) ||
               !strcmp( attrib, "nout" ) ||
               !strcmp( attrib, "refcount" ) ||
               !strcmp( attrib, "tranforward" ) ||
               !strcmp( attrib, "traninverse" ) ) {
      result = 0;

/* Pass unrecognised attributes on to the FrameSet's current Frame for
   further interpretation. */
   } else {

/* Obtain a pointer to the current Frame and invoke its astTestAttrib
   method. Annul the Frame pointer afterwards. */
      fr = astGetFrame( this, AST__CURRENT );
      result = astTestAttrib( fr, attrib );
      fr = astAnnul( fr );
   }

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = 0;

/* Return the result, */
   return result;
}

static int TestBase( AstFrameSet *this, int *status ) {
/*
*+
*  Name:
*     astTestBase

*  Purpose:
*     Determine if a value has been set for the Base attribute of a FrameSet.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "frameset.h"
*     int astTestBase( AstFrameSet *this )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function returns a boolean result to indicate if a value
*     has been set for the Base attribute of a FrameSet. This
*     attribute is an index that identifies the base Frame in the
*     FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.

*  Returned Value:
*     Zero or 1, depending on whether a value has been set.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*-
*/

/* Local Variables: */
   int invert;                   /* FrameSet is inverted? */
   int result;                   /* Value to return */

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Determine if the FrameSet has been inverted. */
   invert = astGetInvert( this );

/* If it has not been inverted, test the base Frame index, otherwise
   test the index of the current Frame instead. */
   if ( astOK ) {
      if ( !invert ) {
         result = ( this->base != -INT_MAX );
      } else {
         result = ( this->current != -INT_MAX );
      }
   }

/* Return the result. */
   return result;
}

static int TestCurrent( AstFrameSet *this, int *status ) {
/*
*+
*  Name:
*     astTestCurrent

*  Purpose:
*     Test if a value has been set for the Current attribute of a FrameSet.

*  Type:
*     Protected function.

*  Synopsis:
*     #include "frameset.h"
*     int astTestCurrent( AstFrameSet *this )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function returns a boolean result to indicate whether a
*     value has been set for the Current attribute of a FrameSet.
*     This attribute is an index that identifies the current Frame in
*     a FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.

*  Returned Value:
*     Zero or 1, depending on whether a value has been set.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*-
*/

/* Local Variables: */
   int invert;                   /* FrameSet is inverted? */
   int result;                   /* Value to return */

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Determine if the FrameSet has been inverted. */
   invert = astGetInvert( this );

/* If it has not been inverted, test the current Frame index,
   otherwise test the index of the base Frame instead. */
   if ( astOK ) {
      if ( !invert ) {
         result = ( this->current != -INT_MAX );
      } else {
         result = ( this->base != -INT_MAX );
      }
   }

/* Return the result. */
   return result;
}

static int TestVariant( AstFrameSet *this, int *status ) {
/*
*+
*  Name:
*     astTestVariant

*  Purpose:
*     Determine if a value has been set for the Variant attribute of a FrameSet.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "frameset.h"
*     int astTestVariant( AstFrameSet *this )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function returns a boolean result to indicate if a value
*     has been set for the Variant attribute of a FrameSet.

*  Parameters:
*     this
*        Pointer to the FrameSet.

*  Returned Value:
*     Zero or 1, depending on whether a value has been set.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*-
*/

/* Local Variables: */
   AstFrame *frm;
   AstFrameSet *vfs;
   int result;
   int icur;

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get the one-based index of the Frame to use. */
   icur = GetVarFrm( this, astGetCurrent( this ), status );

/* Get a pointer to the Variants FrameSet in the current Frame. */
   frm = astGetFrame( this, icur );
   vfs = astGetFrameVariants( frm );

/* If it is null, return zero, otherwise 1. */
   result = vfs ? 1 : 0;

/* Annul pointers. */
   if( vfs ) vfs = astAnnul( vfs );
   frm = astAnnul( frm );

/* Return the result. */
   return result;
}

static void TidyNodes( AstFrameSet *this, int *status ) {
/*
*  Name:
*     TidyNodes

*  Purpose:
*     Tidy the nodes in a FrameSet.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void TidyNodes( AstFrameSet *this, int *status )

*  Class Membership:
*     FrameSet member function.

*  Description:
*     This function tidies the nodes in a FrameSet, removing any that
*     are unnecessary or represent dead-ends. It should be used after
*     any changes have been made to a FrameSet that may have reduced
*     the number of references to any of its nodes (either by Frames
*     or by other nodes).

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     status
*        Pointer to the inherited status variable.
*/

/* Local Variables: */
   AstMapping *newmap;           /* Pointer to simplified Mapping */
   AstMapping *tmpmap;           /* Pointer to new compound Mapping */
   int ifr;                      /* Loop counter for Frames */
   int inode;                    /* Loop counter for nodes */
   int last_link[ 2 ];           /* Last nodes to reference via "link" array */
   int link_ref;                 /* Number of "link" array references */
   int needed;                   /* Node still required? */
   int next;                     /* Node which references the removed one */
   int remove;                   /* Node to be removed */
   int suspect;                  /* Loop counter for testing nodes */

/* Check the global error status. */
   if ( !astOK ) return;

/* Loop to search for unnecessary nodes until no more are found. */
   needed = 0;
   while ( !needed ) {

/* Inspect each node (including node zero, which does not actually
   have a Mapping associated with it) to see how many times it is
   referenced. */
      for ( suspect = 0; suspect < this->nnode; suspect++ ) {
         link_ref = 0;

/* Test for at least one reference from within the "node" array which
   associates Frames with particular nodes. */
         for ( ifr = 1; ifr <= this->nframe; ifr++ ) {
            if ( ( needed = ( this->node[ ifr - 1 ] == suspect ) ) ) break;
         }

/* If no references were found above, look for references in the
   "link" array that inter-connects all the nodes. */
         if ( !needed ) {
            for ( inode = 1; inode < this->nnode; inode ++ ) {
               if ( this->link[ inode - 1 ] == suspect ) {

/* Node zero must be retained if it has more than two links
   referencing it, while other nodes only require more than one. */
                  if ( ( needed = ( link_ref >= ( suspect ? 1 : 2 ) ) ) ) break;

/* Remember (up to) the first two nodes which reference the current one. */
                  last_link[ link_ref++ ] = inode;
               }
            }
         }

/* If there were insufficient references to retain this node, we must
   now decide why it should be removed. */
         if ( !needed ) {

/* If there is no Frame associated with a node and there are less than
   two links to it (for node zero), or less then one link (for other
   nodes), then the there is no route to anything else via this node.
   It is a dead-end. */
            if ( link_ref < ( suspect ? 1 : 2 ) ) {

/* To tidy up, we remove the affected node or, for node zero, the
   remaining one that references it. Annul the Mapping associated with
   the node being removed. */
               remove = suspect ? suspect : last_link[ 0 ];
               this->map[ remove - 1 ] = astAnnul( this->map[ remove - 1 ] );

/* If an unnecessary node is not a dead-end, then it is a redundant
   node which simply joins two Mappings. */
            } else {

/* To tidy up, we remove the affected node or, for node zero, the
   first one that references it. */
               remove = suspect ? suspect : last_link[ 0 ];

/* We then produce a compound Mapping which spans the gap by
   concatenating the Mappings associated with the node being removed
   and the remaining one which references it. For node zero, the first
   of these Mappings must be inverted because there are no out-going
   Mappings from node zero. */
               next = suspect ? last_link[ 0 ] : last_link[ 1 ];
               tmpmap = CombineMaps( this->map[ remove - 1 ],
                                     this->invert[ remove - 1 ] != !suspect,
                                     this->map[ next - 1 ],
                                     this->invert[ next - 1 ], 1, status );

/* Simplify this compound Mapping. */
               newmap = astSimplify( tmpmap );
               tmpmap = astAnnul( tmpmap );

/* Annul the individual Mapping pointers. */
               this->map[ remove - 1 ] = astAnnul( this->map[ remove - 1 ] );
               this->map[ next - 1 ] = astAnnul( this->map[ next - 1 ] );

/* Install the new compound Mapping and its Invert flag. */
               this->map[ next - 1 ] = newmap;
               this->invert[ next - 1 ] = astGetInvert( newmap );

/* Transfer the "link" value from the removed node to the one which
   takes its place. */
               this->link[ next - 1 ] = this->link[ remove - 1 ];
            }

/* Loop to move all subsequent node data down in the "map", "invert"
   and "link" arrays to close the gap where a node has been
   removed. */
            for ( inode = remove; inode < this->nnode - 1; inode ++ ) {
               this->map [ inode - 1 ] = this->map[ inode ];
               this->link [ inode - 1 ] = this->link[ inode ];
               this->invert[ inode - 1 ] = this->invert[ inode ];
            }
            this->map[ this->nnode - 2 ] = NULL;
            this->link[ this->nnode - 2 ] = -1;
            this->invert[ this->nnode - 2 ] = -1;

/* Decrement the node count. */
            this->nnode--;

/* Loop to adjust each entry in the "node" array for the change in
   node numbering, re-directing references to the removed node towards
   the new node zero. */
            for ( ifr = 1; ifr <= this->nframe; ifr++ ) {
               if ( this->node[ ifr - 1 ] > remove ) {
                  this->node[ ifr - 1 ]--;
               } else if ( this->node[ ifr - 1 ] == remove ) {
                  this->node[ ifr - 1 ] = 0;
               }
            }

/* Similarly adjust each entry in the "link" array. */
            for ( inode = 1; inode < this->nnode; inode++ ) {
               if ( this->link[ inode - 1 ] > remove ) {
                  this->link[ inode - 1 ]--;
               } else if ( this->link[ inode - 1 ] == remove ) {
                  this->link[ inode - 1 ] = 0;
               }
            }

/* Once a node has been removed, other nodes (perhaps already tested)
   may no longer be needed, so quit the testing loop and start testing
   again with node zero. The process terminates when no more
   unnecessary nodes can be found. */
            break;
         }
      }
   }
}

static AstPointSet *Transform( AstMapping *this_mapping, AstPointSet *in,
                               int forward, AstPointSet *out, int *status ) {
/*
*  Name:
*     Transform

*  Purpose:
*     Transform a set of points.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     AstPointSet *Transform( AstMapping *this, AstPointSet *in,
*                             int forward, AstPointSet *out, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the astTransform method
*     inherited from the Frame class).

*  Description:
*     This function takes a FrameSet and a set of points encapsulated
*     in a PointSet, and applies either the forward or inverse
*     coordinate transformation (if defined by the FrameSet) to the
*     points. The forward transformation converts between the
*     FrameSet's base Frame and its current Frame, while the inverse
*     transformation converts in the opposite direction.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     in
*        Pointer to the PointSet holding the input coordinate data.
*     forward
*        A non-zero value indicates that the forward coordinate transformation
*        should be applied, while a zero value requests the inverse
*        transformation.
*     out
*        Pointer to a PointSet which will hold the transformed (output)
*        coordinate values. A NULL value may also be given, in which case a
*        new PointSet will be created by this function.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     Pointer to the output (possibly new) PointSet.

*  Notes:
*     - An error will result if the FrameSet supplied does not define
*     the requested coordinate transformation (either forward or
*     inverse).
*     - The number of coordinate values per point in the input
*     PointSet must match the number of input coordinates for the
*     FrameSet being applied (or number of output coordinates if the
*     inverse transformation is requested).  This will be equal to the
*     number of axes in the FrameSet's base Frame (or the current
*     Frame for the inverse transformation).
*     - If an output PointSet is supplied, it must have space for
*     sufficient number of points and coordinate values per point to
*     accommodate the result (e.g. the number of FrameSet output
*     coordinates, or number of input coordinates if the inverse
*     transformation is requested). Any excess space will be ignored.
*     - A null pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   AstMapping *map;              /* Pointer to the base->current Mapping */
   AstPointSet *result;          /* Pointer value to return */

/* Check the global error status. */
   if ( !astOK ) return NULL;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_mapping;

/* Obtain the Mapping between the base and current Frames in the
   FrameSet (note this takes account of whether the FrameSet has been
   inverted). */
   map = astGetMapping( this, AST__BASE, AST__CURRENT );

/* Apply the Mapping to the input PointSet. */
   result = astTransform( map, in, forward, out );

/* Annul the Mapping pointer. */
   map = astAnnul( map );

/* If an error has occurred and a new PointSet may have been created, then
   clean up by annulling it. In any case, ensure that a NULL result is
   returned.*/
   if ( !astOK ) {
      if ( !out ) result = astAnnul( result );
      result = NULL;
   }

/* Return a pointer to the output PointSet. */
   return result;
}

static int Unformat( AstFrame *this_frame, int axis, const char *string,
                     double *value, int *status ) {
/*
*  Name:
*     Unformat

*  Purpose:
*     Read a formatted coordinate value for a FrameSet axis.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int Unformat( AstFrame *this, int axis, const char *string,
*                   double *value, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the public astUnformat
*     method inherited from the Frame class).

*  Description:
*     This function reads a formatted coordinate value for a FrameSet
*     axis (supplied as a string) and returns the equivalent numerical
*     value as a double. It also returns the number of characters read
*     from the string.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     axis
*        The number of the FrameSet axis for which the coordinate
*        value is to be read (axis numbering starts at zero for the
*        first axis).
*     string
*        Pointer to a constant null-terminated string containing the
*        formatted coordinate value.
*     value
*        Pointer to a double in which the coordinate value read will be
*        returned.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The number of characters read from the string to obtain the
*     coordinate value.

*  Notes:
*     - Any white space at the beginning of the string will be
*     skipped, as also will any trailing white space following the
*     coordinate value read. The function's return value will reflect
*     this.
*     - A function value of zero (and no coordinate value) will be
*     returned, without error, if the string supplied does not contain
*     a suitably formatted value.
*     - The string "<bad>" is recognised as a special case and will
*     generate the value AST__BAD, without error. The test for this
*     string is case-insensitive and permits embedded white space.
*     - A function result of zero will be returned and no coordinate
*     value will be returned via the "value" pointer if this function
*     is invoked with the global error status set, or if it should
*     fail for any reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   double coord;                 /* Coordinate value read */
   int nc;                       /* Number of characters read */

/* Initialise. */
   nc = 0;

/* Check the global error status. */
   if ( !astOK ) return nc;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Validate the axis index. */
   (void) astValidateAxis( this, axis, 1, "astUnformat" );

/* Obtain a pointer to the FrameSet's current Frame and invoke the
   astUnformat method for this Frame. Annul the Frame pointer
   afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   nc = astUnformat( fr, axis, string, &coord );
   fr = astAnnul( fr );

/* If an error occurred, clear the number of characters read. */
   if ( !astOK ) {
      nc = 0;

/* Otherwise, if characters were read, return the coordinate value. */
   } else if ( nc ) {
      *value = coord;
   }

/* Return the number of characters read. */
   return nc;
}

static int ValidateAxis( AstFrame *this_frame, int axis, int fwd,
                         const char *method, int *status ) {
/*
*  Name:
*     ValidateAxis

*  Purpose:
*     Validate and permute a FrameSet's axis index.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int ValidateAxis( AstFrame *this, int axis, int fwd, const char *method,
*                       int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected
*     astValidateAxis method inherited from the Frame class).

*  Description:
*     This function checks the validity of an index (zero-based) which
*     is to be used to address one of the coordinate axes of the
*     current Frame in a FrameSet. If the index is valid, it is
*     permuted using the axis permutation array associated with the
*     FrameSet's current Frame and the (zero-based) permuted axis
*     index is returned.  This gives the index the axis had when the
*     Frame was first created. If the axis index supplied is not
*     valid, an error is reported and the global error status is set.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     axis
*        The axis index (zero-based) to be checked. To be valid, it
*        must lie between zero and (naxes-1) inclusive, where "naxes"
*        is the number of coordinate axes associated with the
*        FrameSet's current Frame.
*     fwd
*        If non-zero, the suppplied axis index is assumed to be an
*        "external" axis index, and the corresponding "internal" axis index
*        is returned as the function value. Otherwise, the suppplied axis
*        index is assumed to be an "internal" axis index, and the
*        corresponding "external" axis index is returned as the function
*        value.
*     method
*        Pointer to a constant null-terminated character string
*        containing the name of the method that invoked this function
*        to validate an axis index. This method name is used solely
*        for constructing error messages.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The permuted axis index - either "internal" or "external" as
*     specified by "fwd".

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   int naxes;                    /* Number of FrameSet axes */
   int result;                   /* Permuted axis index */

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Determine the number of FrameSet axes. */
   naxes = astGetNaxes( this );
   if ( astOK ) {

/* If the FrameSet has no axes, report an error (convert to 1-based
   axis numbering for the benefit of the public interface). */
      if ( naxes == 0 ) {
         astError( AST__AXIIN, "%s(%s): Invalid attempt to use an axis index "
                   "(%d) for a %s which has no axes.", status, method,
                   astGetClass( this ), axis + 1, astGetClass( this ) );

/* Otherwise, check the axis index for validity and report an error if
   it is not valid (again, convert to 1-based axis numbering). */
      } else if ( ( axis < 0 ) || ( axis >= naxes ) ) {
         astError( AST__AXIIN, "%s(%s): Axis index (%d) invalid - it should "
                   "be in the range 1 to %d.", status, method, astGetClass( this ),
                   axis + 1, naxes );

/* If the axis index was valid, obtain a pointer to the FrameSet's
   current Frame and invoke this Frame's astValidateAxis method to
   obtain the permuted axis index. Annul the Frame pointer
   afterwards. */
      } else {
         fr = astGetFrame( this, AST__CURRENT );
         result = astValidateAxis( fr, axis, fwd, "astValidateAxis" );
         fr = astAnnul( fr );
      }
   }

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = 0;

/* Return the result. */
   return result;
}

static void ValidateAxisSelection( AstFrame *this_frame, int naxes,
                                   const int *axes, const char *method, int *status ) {
/*
*  Name:
*     ValidateAxisSelection

*  Purpose:
*     Check that a set of axes selected from a Frame is valid.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void ValidateAxisSelection( AstFrame *this, int naxes,
*                                 const int *axes, const char *method, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astValidateAxisSelection
*     method inherited from the Frame class).

*  Description:
*     This function checks the validity of an array of (zero-based)
*     axis indices that specify a set of axes to be selected from a
*     Frame. To be valid, no axis should be selected more than
*     once. In assessing this, any axis indices that do not refer to
*     valid Frame axes (e.g. are set to -1) are ignored.
*
*     If the axis selection is valid, this function returns without further
*     action. Otherwise, an error is reported and the global error status is
*     set.

*  Parameters:
*     this
*        Pointer to the Frame.
*     naxes
*        The number of axes to be selected (may be zero).
*     axes
*        Pointer to an array of int with naxes elements that contains the
*        (zero based) axis indices to be checked.
*     method
*        Pointer to a constant null-terminated character string
*        containing the name of the method that invoked this function
*        to validate an axis selection. This method name is used
*        solely for constructing error messages.
*     status
*        Pointer to the inherited status variable.
*/

/* Local Variables: */
   AstFrame *fr;                 /* Pointer to current Frame */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke this
   Frame's astValidateAxisSelection method. Annul the Frame pointer
   afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   astValidateAxisSelection( fr, naxes, axes, method );
   fr = astAnnul( fr );

}

static int ValidateFrameIndex( AstFrameSet *this, int iframe,
                               const char *method, int *status ) {
/*
*+
*  Name:
*     astValidateFrameIndex

*  Purpose:
*     Validate a FrameSet Frame index number.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "frameset.h"
*     int astValidateFrameIndex( AstFrameSet *this, int iframe,
*                                const char *method )

*  Class Membership:
*     FrameSet method.

*  Description:
*     This function checks a (one-based) FrameSet Frame index for
*     validity. If it is not valid, an error is reported. Otherwise,
*     the function returns the Frame index value, having translated
*     the special values AST__CURRENT and AST__BASE into valid Frame
*     indices if necessary.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     iframe
*        The Frame index. To be valid this should lie in the range 1
*        to the number of Frames in the FrameSet.  In addition, the
*        values AST__CURRENT and AST__BASE may be given to indicate
*        the "current" and "base" Frames. These values will be
*        translated into the acceptable range.
*     method
*        Pointer to a constant null-terminated character string
*        containing the name of the method that invoked this function
*        to validate a Frame index. This method name is used solely
*        for constructing error messages.

*  Returned Value:
*     The validated (one-based) Frame index.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason
*     (e.g. if the Frame index is invalid).
*-
*/

/* Local Variables: */
   int nframe;                   /* Number of Frames */
   int result;                   /* Returned index value */

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Check if the base or current Frame was specified and retrieve the
   required Frame index from the FrameSet. */
   if ( iframe == AST__BASE ) {
      result = astGetBase( this );
   } else if ( iframe == AST__CURRENT ) {
      result = astGetCurrent( this );

/* Otherwise, determine how many Frames there are in the FrameSet. */
   } else {
      nframe = astGetNframe( this );
      if ( astOK ) {

/* Check that the supplied index is within range and report an error
   if it is not. */
         if ( ( iframe < 1 ) || ( iframe > nframe ) ) {
            astError( AST__FRMIN, "%s(%s): Invalid Frame index (%d) given.", status,
                      method, astGetClass( this ), iframe );
            astError( AST__FRMIN, "This value should be in the range 1 to "
                      "%d (or AST__CURRENT or AST__BASE).", status, nframe );

/* If OK, return the validated index value. */
         } else {
            result = iframe;
         }
      }
   }

/* Return the result. */
   return result;
}

static int ValidateSystem( AstFrame *this_frame, AstSystemType system, const char *method, int *status ) {
/*
*  Name:
*     ValidateSystem

*  Purpose:
*     Validate a value for a Frame's System attribute.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     int ValidateSystem( AstFrame *this, AstSystemType system,
*                         const char *method, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astValidateSystem
*     method inherited from the Frame class).

*  Description:
*     This function checks the validity of the supplied system value.
*     If the value is valid, it is returned unchanged. Otherwise, an
*     error is reported and a value of AST__BADSYSTEM is returned.

*  Parameters:
*     this
*        Pointer to the Frame.
*     system
*        The system value to be checked.
*     method
*        Pointer to a constant null-terminated character string
*        containing the name of the method that invoked this function
*        to validate an axis index. This method name is used solely
*        for constructing error messages.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The validated system value.

*  Notes:
*     - A value of AST_BADSYSTEM will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*-
*/

/* Local Variables: */
   AstSystemType result;      /* Validated system value */
   AstFrame *fr;              /* Pointer to FrameSet's current Frame */
   AstFrameSet *this;         /* Pointer to the FrameSet structure */

/* Initialise. */
   result = AST__BADSYSTEM;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_frame;

/* Obtain a pointer to the FrameSet's current Frame and invoke the
   astValidateSystem method for this Frame. Annul the Frame pointer
   afterwards. */
   fr = astGetFrame( this, AST__CURRENT );
   result = astValidateSystem( this, system, method );
   fr = astAnnul( fr );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = AST__BADSYSTEM;

/* Return the result. */
   return result;
}

static void VSet( AstObject *this_object, const char *settings,
                  char **text, va_list args, int *status ) {
/*
*  Name:
*     VSet

*  Purpose:
*     Set values for a FrameSet's attributes.

*  Type:
*     Private function.

*  Synopsis:
*     #include "frameset.h"
*     void VSet( AstObject *this, const char *settings, char **text,
*                va_list args, int *status )

*  Class Membership:
*     FrameSet member function (over-rides the protected astVSet
*     method inherited from the Object class).

*  Description:
*     This function assigns a set of attribute values for a FrameSet,
*     the attributes and their values being specified by means of a
*     string containing a comma-separated list of the form:
*
*        "attribute1 = value1, attribute2 = value2, ... "
*
*     Here, "attribute" specifies an attribute name and the value to
*     the right of each "=" sign should be a suitable textual
*     representation of the value to be assigned to that
*     attribute. This will be interpreted according to the attribute's
*     data type.
*
*     The string supplied may also contain "printf"-style format
*     specifiers identified by a "%" sign in the usual way. If
*     present, these will be substituted by values supplied as
*     optional arguments (as a va_list variable argument list), using
*     the normal "printf" rules, before the string is used.

*  Parameters:
*     this
*        Pointer to the FrameSet.
*     settings
*        Pointer to a null-terminated string containing a
*        comma-separated list of attribute settings.
*     text
*        Pointer to a location at which to return a pointer to dynamic
*        memory holding a copy of the expanded setting string. This memory
*        should be freed using astFree when no longer needed. If a NULL
*        pointer is supplied, no string is created.
*     args
*        The variable argument list which contains values to be
*        substituted for any "printf"-style format specifiers that
*        appear in the "settings" string.
*     status
*        Pointer to the inherited status variable.

*  Notes:
*     - This function preserves the integrity of the FrameSet (if
*     possible) by appropriately remapping its current Frame to take
*     account of its changed attribute values.
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Declare the thread specific global data */
   AstFrame *save_frame;         /* Saved pointer to integrity Frame */
   AstFrameSet *this;            /* Pointer to FrameSet structure */
   char *fulltext;               /* Pointer to expanded text string */
   const char *save_method;      /* Saved pointer to method name */
   int len;                      /* Length of settings string */
   int ok;                       /* Status OK? */
   int save_lost;                /* Saved integrity modified flag */

/* Initialise */
   if( text ) *text = NULL;

/* Check the global error status. */
   if ( !astOK ) return;

/* Get a pointer to the structure holding thread-specific global data. */
   astGET_GLOBALS(this_object);

/* Obtain the length of the "settings" string and test it is not
   zero. If it is, there is nothing more to do. */
   len = (int) strlen( settings );
   if ( len != 0 ) {

/* Obtain a pointer to the FrameSet structure. */
      this = (AstFrameSet *) this_object;

/* This function may be invoked recursively (because astConvert,
   below, constructs a FrameSet and may require that its attributes be
   set). To allow this, we first save any existing FrameSet integrity
   information in local variables. */
      save_frame = integrity_frame;
      save_lost = integrity_lost;
      save_method = integrity_method;

/* Set the name of the method being used (for use in error
   messages). */
      integrity_method = "astSet";

/* Record the initial integrity state of the FrameSet. */
      RecordIntegrity( this, status );

/* Invoke the parent astVSet method to set the FrameSet's attribute
   values and note if this succeeds. */
      (*parent_vset)( this_object, settings, &fulltext, args, status );
      ok = astOK;

/* Restore the FrameSet's integrity. */
      RestoreIntegrity( this, status );

/* If integrity could not be restored, then add contextual error
   information. */
      if ( !astOK && ok ) {

/* Display the message. */
         astError( astStatus, "Unable to accommodate the attribute setting "
                               "\"%s\".", status, fulltext );
      }

/* Restore any saved FrameSet integrity information. */
      integrity_frame = save_frame;
      integrity_lost = save_lost;
      integrity_method = save_method;

/* If the full text of the setting string is not needed, free it.
   Otherwise return it. */
      if( text ) {
         *text = fulltext;
      } else {
         fulltext = astFree( fulltext );
      }
   }
}

/* FrameSet Attributes. */
/* -------------------- */
/*
*att++
*  Name:
*     AllVariants

*  Purpose:
*     A list of the variant Mappings associated with the current Frame.

*  Type:
*     Public attribute.

*  Synopsis:
*     String, read-only.

*  Description:
*     This attrbute gives a space separated list of the names of all the
*     variant Mappings associated with the current Frame (see attribute
*     "Variant"). If the current Frame has no variant Mappings, then the
*     list will hold a single entry equal to the Domain name of the
*     current Frame.

*  Applicability:
*     FrameSet
*        All FrameSets have this attribute.
*att--
*/

/*
*att++
*  Name:
*     Base

*  Purpose:
*     FrameSet base Frame index.

*  Type:
*     Public attribute.

*  Synopsis:
*     Integer.

*  Description:
*     This attribute gives the index of the Frame which is to be
*     regarded as the "base" Frame within a FrameSet. The default is
*     the first Frame added to the FrameSet when it is created (this
*     Frame always has an index of 1).
*
*     When setting a new value for this attribute, a string may be
*     supplied instead of an integer index. In this case a search
*     is made within the FrameSet for a Frame that has its Domain
*     attribute value equal to the supplied string (the comparison is
*     case-insensitive). If found, the Frame is made the base Frame.
*     Otherwise an error is reported.

*  Applicability:
*     FrameSet
*        All FrameSets have this attribute.

*  Notes:
*     - Inverting a FrameSet (inverting the boolean sense of its
c     Invert attribute, with the astInvert function for example) will
f     Invert attribute, with the AST_INVERT routine for example) will
*     interchange the values of its Base and Current attributes.
*att--
*/

/*
*att++
*  Name:
*     Current

*  Purpose:
*     FrameSet current Frame index.

*  Type:
*     Public attribute.

*  Synopsis:
*     Integer.

*  Description:
*     This attribute gives the index of the Frame which is to be
*     regarded as the "current" Frame within a FrameSet. The default
*     is the most recent Frame added to the FrameSet (this Frame
*     always has an index equal to the FrameSet's Nframe attribute).
*
*     When setting a new value for this attribute, a string may be
*     supplied instead of an integer index. In this case a search
*     is made within the FrameSet for a Frame that has its Domain
*     attribute value equal to the supplied string (the comparison is
*     case-insensitive). If found, the Frame is made the current Frame.
*     Otherwise an error is reported.

*  Applicability:
*     FrameSet
*        All FrameSets have this attribute.

*  Notes:
*     - Inverting a FrameSet (inverting the boolean sense of its
c     Invert attribute, with the astInvert function for example) will
f     Invert attribute, with the AST_INVERT routine for example) will
*     interchange the values of its Base and Current attributes.
*att--
*/

/*
*att++
*  Name:
*     Nframe

*  Purpose:
*     Number of Frames in a FrameSet.

*  Type:
*     Public attribute.

*  Synopsis:
*     Integer, read-only.

*  Description:
*     This attrbute gives the number of Frames in a FrameSet. This
*     value will change as Frames are added or removed, but will
*     always be at least one.

*  Applicability:
*     FrameSet
*        All FrameSets have this attribute.
*att--
*/

/*
*att++
*  Name:
*     Variant

*  Purpose:
*     Indicates which variant of the current Frame is to be used.

*  Type:
*     Public attribute.

*  Synopsis:
*     String.

*  Description:
*     This attribute can be used to change the Mapping that connects the
*     current Frame to the other Frames in the FrameSet. By default, each
*     Frame in a FrameSet is connected to the other Frames by a single
*     Mapping that can only be changed by using the
c     astRemapFrame
f     AST_REMAPFRAME
*     method. However, it is also possible to associate multiple Mappings
*     with a Frame, each Mapping having an identifying name. If this is
*     done, the "Variant" attribute can be set to indicate the name of
*     the Mapping that is to be used with the current Frame.
*
*     A possible (if unlikely) use-case is to create a FrameSet that can
*     be used to describe the WCS of an image formed by co-adding images
*     of two different parts of the sky. In such an image, each pixel contains
*     flux from two points on the sky.and so the WCS for the image should
*     ideally contain one pixel Frame and two SkyFrames - one describing
*     each of the two co-added images. There is nothing to prevent a
*     FrameSet containing two explicit SkyFrames, but the problem then arises
*     of how to distinguish between them. The two primary characteristics of
*     a Frame that distinguishes it from other Frames are its class and its
*     Domain attribute value. The class of a Frame cannot be changed, but we
*     could in principle use two different Domain values to distinguish the
*     two SkyFrames. However, in practice it is not uncommon for application
*     software to assume that SkyFrames will have the default Domain value
*     of "SKY". That is, instead of searching for Frames that have a class
*     of "SkyFrame", such software searches for Frames that have a Domain
*     of "SKY". To alleviate this problem, it is possible to add a single
*     SkyFrame to the FrameSet, but specifying two alternate Mappings to
*     use with the SkyFrame. Setting the "Variant" attribute to the name
*     of one or the other of these alternate Mappings will cause the
*     SkyFrame to be remapped within the FrameSet so that it uses the
*     specified Mapping. The same facility can be used with any class of
*     Frame, not just SkyFrames.
*
*     To use this facility, the Frame should first be added to the
*     FrameSet in the usual manner using the
c     astAddFrame method. By default, the Mapping supplied to astAddFrame
f     AST_ADDFRAME method. By default, the Mapping supplied to AST_ADDVARIANT
*     is assigned a name equal to the Domain name of the Frame. To assign a
*     different name to it, the
c     astAddVariant
f     AST_ADDVARIANT
*     method should then be called specifying the required name and a NULL
*     Mapping. The
c     astAddVariant
f     AST_ADDVARIANT
*     method should then be called repeatedly to add each required extra
*     Mapping to the current Frame, supplying a unique name for each one.
*
*     Each Frame in a FrameSet can have its own set of variant Mappings.
*     To control the Mappings in use with a specific Frame, you need first
*     to make it the current Frame in the FrameSet.
*
*     The
c     astMirrorVariants function
f     AST_MIRRORVARIANTS routine
*     allows the effects of variant Mappings associated with a nominated
*     Frame to be propagated to other Frames in the FrameSet.
*
*     Once this has been done, setting a new value for the "Variant"
*     attribute of a FrameSet will cause the current Frame in the
*     FrameSet to be remapped to use the specified variant Mapping. An
*     error will be reported if the current Frame has no variant Mapping
*     with the supplied name.
*
*     Getting the value of the "Variant" attribute will return the name
*     of the variant Mapping currently in use with the current Frame. If
*     the Frame has no variant Mappings, the value will default to the
*     Domain name of the current Frame.
*
*     Clearing the "Variant" attribute will have the effect of removing
*     all variant Mappings (except for the currently selected Mapping) from
*     the current Frame.
*
*     Testing the "Variant" attribute will return
c     a non-zero value
f     .TRUE.
*     if the current Frame contains any variant Mappings, and
c     zero
f     .FALSE.
*     otherwise.
*
*     A complete list of the names associated with all the available
*     variant Mappings in the current Frame can be obtained from the
*     AllVariants attribute.
*
*     If a Frame with variant Mappings is remapped using the
c     astRemapFrame
f     AST_REMAPFRAME
*     method, the currently selected variant Mapping is used by
c     astRemapFrame
f     AST_REMAPFRAME
*     and the other variant Mappings are removed from the Frame.

*  Applicability:
*     FrameSet
*        All FrameSets have this attribute.
*att--
*/

/* Access to attributes of the current Frame. */
/* ------------------------------------------ */
/* Use the macros defined at the start of this file to implement
   private member functions that give access to the attributes of the
   current Frame of a FrameSet and its axes. These functions over-ride
   the attribute access methods inherited from the Frame class. */

/* Clear, Get, Set and Test axis-independent Frame attributes. */
MAKE_CLEAR(Digits)
MAKE_CLEAR(Domain)
MAKE_CLEAR(MatchEnd)
MAKE_CLEAR(MaxAxes)
MAKE_CLEAR(MinAxes)
MAKE_CLEAR(Permute)
MAKE_CLEAR(PreserveAxes)
MAKE_CLEAR(Title)

MAKE_GET(Digits,int)
MAKE_GET(Domain,const char *)
MAKE_GET(MatchEnd,int)
MAKE_GET(MaxAxes,int)
MAKE_GET(MinAxes,int)
MAKE_GET(Permute,int)
MAKE_GET(PreserveAxes,int)
MAKE_GET(Title,const char *)
MAKE_SET(Digits,int)
MAKE_SET(Domain,const char *)
MAKE_SET(MatchEnd,int)
MAKE_SET(MaxAxes,int)
MAKE_SET(MinAxes,int)
MAKE_SET(Permute,int)
MAKE_SET(PreserveAxes,int)
MAKE_SET(Title,const char *)
MAKE_TEST(Digits)
MAKE_TEST(Domain)
MAKE_TEST(MatchEnd)
MAKE_TEST(MaxAxes)
MAKE_TEST(MinAxes)
MAKE_TEST(Permute)
MAKE_TEST(PreserveAxes)
MAKE_TEST(Title)

MAKE_GET(ActiveUnit,int)
MAKE_SET(ActiveUnit,int)
MAKE_TEST(ActiveUnit)

MAKE_GET(System,AstSystemType)
MAKE_SET(System,AstSystemType)
MAKE_TEST(System)
MAKE_CLEAR(System)

MAKE_GET(AlignSystem,AstSystemType)
MAKE_SET(AlignSystem,AstSystemType)
MAKE_TEST(AlignSystem)
MAKE_CLEAR(AlignSystem)

MAKE_GET(Epoch,double)
MAKE_SET(Epoch,double)
MAKE_TEST(Epoch)
MAKE_CLEAR(Epoch)

MAKE_GET(ObsLon,double)
MAKE_SET(ObsLon,double)
MAKE_TEST(ObsLon)
MAKE_CLEAR(ObsLon)

MAKE_GET(ObsLat,double)
MAKE_SET(ObsLat,double)
MAKE_TEST(ObsLat)
MAKE_CLEAR(ObsLat)

MAKE_GET(ObsAlt,double)
MAKE_SET(ObsAlt,double)
MAKE_TEST(ObsAlt)
MAKE_CLEAR(ObsAlt)

/* Clear, Get, Set and Test axis-dependent Frame attributes. */
MAKE_CLEAR_AXIS(Direction)
MAKE_CLEAR_AXIS(Format)
MAKE_CLEAR_AXIS(Label)
MAKE_CLEAR_AXIS(Symbol)
MAKE_CLEAR_AXIS(Unit)
MAKE_GET_AXIS(Direction,int)
MAKE_GET_AXIS(Format,const char *)
MAKE_GET_AXIS(Label,const char *)
MAKE_GET_AXIS(Symbol,const char *)
MAKE_GET_AXIS(Unit,const char *)
MAKE_SET_AXIS(Direction,int)
MAKE_SET_AXIS(Format,const char *)
MAKE_SET_AXIS(Label,const char *)
MAKE_SET_AXIS(Symbol,const char *)
MAKE_SET_AXIS(Unit,const char *)
MAKE_TEST_AXIS(Direction)
MAKE_TEST_AXIS(Format)
MAKE_TEST_AXIS(Label)
MAKE_TEST_AXIS(Symbol)
MAKE_TEST_AXIS(Unit)

MAKE_GET_AXIS(Bottom,double)
MAKE_SET_AXIS(Bottom,double)
MAKE_TEST_AXIS(Bottom)
MAKE_CLEAR_AXIS(Bottom)

MAKE_GET_AXIS(Top,double)
MAKE_SET_AXIS(Top,double)
MAKE_TEST_AXIS(Top)
MAKE_CLEAR_AXIS(Top)

/* Copy constructor. */
/* ----------------- */
static void Copy( const AstObject *objin, AstObject *objout, int *status ) {
/*
*  Name:
*     Copy

*  Purpose:
*     Copy constructor for FrameSet objects.

*  Type:
*     Private function.

*  Synopsis:
*     void Copy( const AstObject *objin, AstObject *objout, int *status )

*  Description:
*     This function implements the copy constructor for FrameSet objects.

*  Parameters:
*     objin
*        Pointer to the object to be copied.
*     objout
*        Pointer to the object being constructed.
*     status
*        Pointer to the inherited status variable.

*  Notes:
*     -  This constructor makes a deep copy.
*/

/* Local Variables: */
   AstFrameSet *in;              /* Pointer to input FrameSet */
   AstFrameSet *out;             /* Pointer to output FrameSet */
   int iframe;                   /* Loop counter for Frames */
   int inode;                    /* Loop counter for nodes */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain pointers to the input and output FrameSets. */
   in = (AstFrameSet *) objin;
   out = (AstFrameSet *) objout;

/* For safety, first clear any references to the input memory from
   the output FrameSet. */
   out->frame = NULL;
   out->varfrm = NULL;
   out->node = NULL;
   out->map = NULL;
   out->link = NULL;
   out->invert = NULL;

/* Allocate memory in the output FrameSet to store the Frame and node
   information and copy scalar information across. */
   out->frame = astMalloc( sizeof( AstFrame * ) * (size_t) in->nframe );
   out->varfrm = astStore( NULL, in->varfrm, sizeof( int ) *
                                         (size_t) in->nframe );
   out->node = astStore( NULL, in->node, sizeof( int ) *
                                         (size_t) in->nframe );
   out->map = astMalloc( sizeof( AstMapping * ) * (size_t) ( in->nnode - 1 ) );
   out->link = astStore( NULL, in->link, sizeof( int ) *
                                         (size_t) ( in->nnode - 1 ) );
   out->invert = astStore( NULL, in->invert, sizeof( int ) *
                                         (size_t) ( in->nnode - 1 ) );

/* If OK, make copies of each input Frame and Mapping and store the
   resulting pointers in the output FrameSet. */
   if ( astOK ) {
      for ( iframe = 0; iframe < in->nframe; iframe++ ) {
         out->frame[ iframe ] = astCopy( in->frame[ iframe ] );
      }
      for ( inode = 0; inode < in->nnode - 1; inode++ ) {
         out->map[ inode ] = astCopy( in->map[ inode ] );
      }

/* If an error occurred while copying any of these objects, clean up
   by looping through the arrays of pointers again and annulling them
   all. */
      if ( !astOK ) {
         for ( iframe = 0; iframe < in->nframe; iframe++ ) {
            out->frame[ iframe ] = astAnnul( out->frame[ iframe ] );
         }
         for ( inode = 0; inode < in->nnode - 1; inode++ ) {
            out->map[ inode ] = astAnnul( out->map[ inode ] );
         }
      }
   }

/* If an error occurred, clean up by freeing all memory allocated above. */
   if ( !astOK ) {
      out->frame = astFree( out->frame );
      out->varfrm = astFree( out->varfrm );
      out->node = astFree( out->node );
      out->map = astFree( out->map );
      out->link = astFree( out->link );
      out->invert = astFree( out->invert );
   }
}

/* Destructor. */
/* ----------- */
static void Delete( AstObject *obj, int *status ) {
/*
*  Name:
*     Delete

*  Purpose:
*     Destructor for FrameSet objects.

*  Type:
*     Private function.

*  Synopsis:
*     void Delete( AstObject *obj, int *status )

*  Description:
*     This function implements the destructor for FrameSet objects.

*  Parameters:
*     obj
*        Pointer to the object to be deleted.
*     status
*        Pointer to the inherited status variable.

*  Notes:
*     This function attempts to execute even if the global error status is
*     set.
*/

/* Local Variables: */
   AstFrameSet *this;               /* Pointer to FrameSet */
   int iframe;                      /* Loop counter for Frames */
   int inode;                       /* Loop counter for nodes */

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) obj;

/* Annul all Frame pointers and clear the node numbers associated with
   them. */
   for ( iframe = 0; iframe < this->nframe; iframe++ ) {
      this->frame[ iframe ] = astAnnul( this->frame[ iframe ] );
      this->node[ iframe ] = 0;
   }

/* Annul all Mapping pointers and clear the links between pairs of
   nodes and the associated Mapping Invert information. */
   for ( inode = 0; inode < this->nnode - 1; inode++ ) {
      this->map[ inode ] = astAnnul( this->map[ inode ] );
      this->link[ inode ] = 0;
      this->invert[ inode ] = 0;
   }

/* Free all allocated memory. */
   this->frame = astFree( this->frame );
   this->varfrm = astFree( this->varfrm );
   this->node = astFree( this->node );
   this->map = astFree( this->map );
   this->link = astFree( this->link );
   this->invert = astFree( this->invert );
}

/* Dump function. */
/* -------------- */
static void Dump( AstObject *this_object, AstChannel *channel, int *status ) {
/*
*  Name:
*     Dump

*  Purpose:
*     Dump function for FrameSet objects.

*  Type:
*     Private function.

*  Synopsis:
*     void Dump( AstObject *this, AstChannel *channel, int *status )

*  Description:
*     This function implements the Dump function which writes out data
*     for the FrameSet class to an output Channel.

*  Parameters:
*     this
*        Pointer to the FrameSet whose data are being written.
*     channel
*        Pointer to the Channel to which the data are being written.
*     status
*        Pointer to the inherited status variable.
*/

/* Local Constants: */
#define COMMENT_LEN 150          /* Maximum length of a comment string */
#define KEY_LEN 50               /* Maximum length of a keyword */

/* Local Variables: */
   AstFrameSet *this;            /* Pointer to the FrameSet structure */
   char comment[ COMMENT_LEN + 1 ]; /* Buffer for comment string */
   char key[ KEY_LEN + 1 ];      /* Buffer for keyword string */
   int ifr;                      /* Loop counter for Frames */
   int inode;                    /* Loop counter for nodes */
   int invert;                   /* Invert attribute value */
   int ival;                     /* Integer value */
   int set;                      /* Attribute value set? */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the FrameSet structure. */
   this = (AstFrameSet *) this_object;

/* Determine if the FrameSet is inverted. This is required so that the
   effects of inversion can be un-done to obtain information about the
   "true" Base and Current Frames. (The values written are "internal"
   values that are not affected by the Invert setting.) */
   invert = astGetInvert( this );

/* Write out values representing the instance variables for the
   FrameSet class.  Accompany these with appropriate comment strings,
   possibly depending on the values being written.*/

/* In the case of attributes, we first use the appropriate (private)
   Test...  member function to see if they are set. If so, we then use
   the (private) Get... function to obtain the value to be written
   out.

   For attributes which are not set, we use the astGet... method to
   obtain the value instead. This will supply a default value
   (possibly provided by a derived class which over-rides this method)
   which is more useful to a human reader as it corresponds to the
   actual default attribute value.  Since "set" will be zero, these
   values are for information only and will not be read back. */

/* Nframe. */
/* ------- */
   set = ( this->nframe != 0 );
   ival = set ? this->nframe : astGetNframe( this );
   astWriteInt( channel, "Nframe", set, 1, ival,
                "Number of Frames in FrameSet" );

/* Base. */
/* ----- */
   set = ( this->base != -INT_MAX );
   ival = set ? this->base : ( !invert ? astGetBase( this ) :
                                         astGetCurrent( this ) );
   astWriteInt( channel, "Base", set, 1, ival, "Index of base Frame" );

/* Current. */
/* -------- */
   set = ( this->current != -INT_MAX );
   ival = set ? this->current : ( !invert ? astGetCurrent( this ) :
                                            astGetBase( this ) );
   astWriteInt( channel, "Currnt", set, 1, ival, "Index of current Frame" );

/* Number of nodes. */
/* ---------------- */
   ival = this->nnode;
   set = ( ival != this->nframe );
   astWriteInt( channel, "Nnode", set, 0, ival,
                "Number of nodes in FrameSet" );

/* Node index for each Frame. */
/* -------------------------- */
/* There is a node value for each Frame in the FrameSet. */
   for ( ifr = 1; ifr <= this->nframe; ifr++ ) {

/* Convert node numbering to start at 1 for the external
   representation. Regard a node number as "set" if it differs from
   the Frame number. */
      ival = this->node[ ifr - 1 ] + 1;
      set = ( ival != ifr );

/* Create a suitable keyword and comment. */
      (void) sprintf( key, "Nod%d", ifr );
      (void) sprintf( comment,
                      "Frame %d is associated with node %d", ifr, ival );

/* Write out the value. */
      astWriteInt( channel, key, set, 0, ival, comment );
   }

/* Index of variants Frame for each Frame. */
/* --------------------------------------- */
   for ( ifr = 1; ifr <= this->nframe; ifr++ ) {

/* Retain the one-based Frame index values in varfrm. Regard a number as
   "set" if it is greater than zero. */
      ival = this->varfrm[ ifr - 1 ];
      set = ( ival > 0 );

/* Create a suitable keyword and comment. */
      (void) sprintf( key, "VFr%d", ifr );
      (void) sprintf( comment,
                      "Frame %d inherits variants from Frame %d", ifr, ival );

/* Write out the value. */
      astWriteInt( channel, key, set, 0, ival, comment );
   }

/* Links between nodes. */
/* -------------------- */
/* Each node in the FrameSet (except the first) has a link to another
   node from which it is derived. */
   for ( inode = 1; inode < this->nnode; inode++ ) {

/* Convert node numbering to start at 1 (as above). */
      ival = this->link[ inode - 1 ] + 1;
      (void) sprintf( key, "Lnk%d", inode + 1 );
      (void) sprintf( comment,
                      "Node %d is derived from node %d", inode + 1, ival );
      astWriteInt( channel, key, 1, 0, ival, comment );

/* Inversion flags. */
/* ---------------- */
/* Each node with a link has a value which the Invert attribute of the
   associated Mapping should have when the transformation from the
   parent node to the node in question is required. */
      ival = this->invert[ inode - 1 ];

/* Regard the value as set only if the Mapping's inverse
   transformation is required. */
      set = ( ival != 0 );
      (void) sprintf( key, "Inv%d", inode + 1 );
      astWriteInt( channel, key, set, 0, ival,
                   ival ? "The inverse mapping is used" :
                          "The forward mapping is used" );
   }

/* Frame objects. */
/* -------------- */
/* Output an Object description for each Frame in the FrameSet. */
   for ( ifr = 1; ifr <= this->nframe; ifr++ ) {
      (void) sprintf( key, "Frm%d", ifr );
      (void) sprintf( comment, "Frame number %d", ifr );
      astWriteObject( channel, key, 1, 1, this->frame[ ifr - 1 ],
                      comment );
   }

/* Mapping objects. */
/* ---------------- */
/* Output an Object description for each Mapping in the FrameSet. */
   for ( inode = 1; inode < this->nnode; inode++ ) {
      (void) sprintf( key, "Map%d", inode + 1 );
      (void) sprintf( comment, "Mapping between nodes %d and %d",
                      this->link[ inode - 1 ] + 1, inode + 1 );
      astWriteObject( channel, key, 1, 1, this->map[ inode - 1 ], comment );
   }

/* Undefine macros local to this function. */
#undef COMMENT_LEN
#undef KEY_LEN
}

/* Standard class functions. */
/* ========================= */
/* Implement the astIsAFrameSet and astCheckFrameSet functions using
   the macros defined for this purpose in the "object.h" header
   file. */
astMAKE_ISA(FrameSet,Frame)
astMAKE_CHECK(FrameSet)

AstFrameSet *astFrameSet_( void *frame_void, const char *options, int *status, ...) {
/*
*+
*  Name:
*     astFrameSet

*  Purpose:
*     Create a FrameSet.

*  Type:
*     Protected function.

*  Synopsis:
*     #include "frameset.h"
*     AstFrameSet *astFrameSet( AstFrame *frame, const char *options, int *status, ... )

*  Class Membership:
*     FrameSet constructor.

*  Description:
*     This function creates a new FrameSet and optionally initialises
*     its attributes.

*  Parameters:
*     frame
*        Pointer to the initial Frame. If this is not a FrameSet, the
*        new FrameSet will be initialised to contain this Frame alone.
*
*        If it is a FrameSet, the new FrameSet will be initialised to
*        contain the same Frames (and Mappings) and to have the same
*        attribute values as the one supplied. This is similar to
*        making a copy, except that the Frames (and Mappings)
*        contained in the original FrameSet are not themselves copied,
*        but are shared by both FrameSets.
*     options
*        Pointer to a null terminated string containing an optional
*        comma-separated list of attribute assignments to be used for
*        initialising the new FrameSet. The syntax used is the same as
*        for the astSet method and may include "printf" format
*        specifiers identified by "%" symbols in the normal way.
*     status
*        Pointer to the inherited status variable.
*     ...
*        If the "options" string contains "%" format specifiers, then
*        an optional list of arguments may follow it in order to
*        supply values to be substituted for these specifiers. The
*        rules for supplying these are identical to those for the
*        astSet method (and for the C "printf" function).

*  Returned Value:
*     A pointer to the new FrameSet.

*  Notes:
*     - A NULL pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*-

*  Implementation Notes:
*     - This function implements the basic FrameSet constructor which
*     is available via the protected interface to the FrameSet class.
*     A public interface is provided by the astFrameSetId_ function.
*     - Because this function has a variable argument list, it is
*     invoked by a macro that evaluates to a function pointer (not a
*     function invocation) and no checking or casting of arguments is
*     performed before the function is invoked. Because of this, the
*     "frame" parameter is of type (void *) and is converted and
*     validated within the function itself.
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Pointer to thread-specific global data */
   AstFrame *frame;              /* Pointer to Frame structure */
   AstFrameSet *new;             /* Pointer to new FrameSet */
   va_list args;                 /* Variable argument list */

/* Initialise. */
   new = NULL;

/* Get a pointer to the thread specific global data structure. */
   astGET_GLOBALS(NULL);

/* Check the global status. */
   if ( !astOK ) return new;

/* Obtain and validate a pointer to the Frame structure provided. */
   frame = astCheckFrame( frame_void );
   if ( astOK ) {

/* Initialise the FrameSet, allocating memory and initialising the
   virtual function table as well if necessary. */
      new = astInitFrameSet( NULL, sizeof( AstFrameSet ), !class_init,
                             &class_vtab, "FrameSet", frame );

/* If successful, note that the virtual function table has been
   initialised. */
      if ( astOK ) {
         class_init = 1;

/* Obtain the variable argument list and pass it along with the
   options string to the astVSet method to initialise the new
   FrameSet's attributes. */
         va_start( args, status );
         astVSet( new, options, NULL, args );
         va_end( args );

/* If an error occurred, clean up by deleting the new object. */
         if ( !astOK ) new = astDelete( new );
      }
   }

/* Return a pointer to the new FrameSet. */
   return new;
}

AstFrameSet *astInitFrameSet_( void *mem, size_t size, int init,
                               AstFrameSetVtab *vtab, const char *name,
                               AstFrame *frame, int *status ) {
/*
*+
*  Name:
*     astInitFrameSet

*  Purpose:
*     Initialise a FrameSet.

*  Type:
*     Protected function.

*  Synopsis:
*     #include "frameset.h"
*     AstFrameSet *astInitFrameSet( void *mem, size_t size, int init,
*                                   AstFrameSetVtab *vtab, const char *name,
*                                   AstFrame *frame )

*  Class Membership:
*     FrameSet initialiser.

*  Description:
*     This function is provided for use by class implementations to
*     initialise a new FrameSet object. It allocates memory (if
*     necessary) to accommodate the FrameSet plus any additional data
*     associated with the derived class.  It then initialises a
*     FrameSet structure at the start of this memory. If the "init"
*     flag is set, it also initialises the contents of a virtual
*     function table for a FrameSet at the start of the memory passed
*     via the "vtab" parameter.

*  Parameters:
*     mem
*        A pointer to the memory in which the FrameSet is to be
*        created. This must be of sufficient size to accommodate the
*        FrameSet data (sizeof(FrameSet)) plus any data used by the
*        derived class. If a value of NULL is given, this function
*        will allocate the memory itself using the "size" parameter to
*        determine its size.
*     size
*        The amount of memory used by the FrameSet (plus derived class
*        data).  This will be used to allocate memory if a value of
*        NULL is given for the "mem" parameter. This value is also
*        stored in the FrameSet structure, so a valid value must be
*        supplied even if not required for allocating memory.
*     init
*        A logical flag indicating if the FrameSet's virtual function
*        table is to be initialised. If this value is non-zero, the
*        virtual function table will be initialised by this function.
*     vtab
*        Pointer to the start of the virtual function table to be
*        associated with the new FrameSet.
*     name
*        Pointer to a constant null-terminated character string which
*        contains the name of the class to which the new object
*        belongs (it is this pointer value that will subsequently be
*        returned by the astGetClass method).
*     frame
*        Pointer to the initial Frame (or FrameSet).

*  Returned Value:
*     A pointer to the new FrameSet.

*  Notes:
*     - A null pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*-
*/

/* Local Variables: */
   AstFrameSet *new;                /* Pointer to new FrameSet */
   AstFrameSet *old;                /* Pointer to original FrameSet */
   int iframe;                      /* Loop counter for Frames */
   int inode;                       /* Loop counter for nodes */

/* Check the global status. */
   if ( !astOK ) return NULL;

/* If necessary, initialise the virtual function table. */
   if ( init ) astInitFrameSetVtab( vtab, name );

/* Initialise a Frame structure (the parent class) as the first
   component within the FrameSet structure, allocating memory if
   necessary. Give this Frame zero axes, as all axis information for
   the FrameSet will be derived from the Frames it contains. */
   new = (AstFrameSet *) astInitFrame( mem, size, 0,
                                       (AstFrameVtab *) vtab, name, 0 );

   if ( astOK ) {

/* Initialise the FrameSet data. */
/* ----------------------------- */

/* Normal Frame supplied. */
/* ---------------------- */
/* Check that the Frame supplied is not a FrameSet (initialising using
   a FrameSet is a special case which is handled below).  If not, we
   initialise the new FrameSet to refer to a single Frame. */
      if ( !astIsAFrameSet( frame ) && astOK ) {

/* Allocate memory for the arrays of Frame information. */
         new->frame = astMalloc( sizeof( AstFrame * ) );
         new->node = astMalloc( sizeof( int ) );
         new->varfrm = astMalloc( sizeof( int ) );

/* The node arrays are not required until at least two Frames are
   present. */
         new->map = NULL;
         new->link = NULL;
         new->invert = NULL;

/* If OK, initialise these arrays, thus adding the Frame to the
   FrameSet. */
         if ( astOK ) {
            new->frame[ 0 ] = astClone( frame );
            new->node[ 0 ] = 0;
            new->varfrm[ 0 ] = 0;
            new->nframe = 1;
            new->nnode = 1;

/* Initialise the FrameSet attributes to their undefined states. */
            new->base = -INT_MAX;
            new->current = -INT_MAX;
         }

/* FrameSet supplied. */
/* ------------------ */
/* If a FrameSet was supplied, we will initialise the new FrameSet to
   refer to the same Frame and Mapping information (this is similar to
   making a copy, except that we clone all the pointers, instead of
   copying the Objects they refer to). */
      } else if ( astOK ) {

/* Obtain a pointer to the original FrameSet structure. */
         old = (AstFrameSet *) frame;

/* Allocate memory in the new FrameSet to store the Frame and node
   information and copy any scalar information across. */
         new->frame = astMalloc( sizeof( AstFrame * ) * (size_t) old->nframe );
         new->node = astStore( NULL, old->node,
                               sizeof( int ) * (size_t) old->nframe );
         new->varfrm = astStore( NULL, old->varfrm,
                               sizeof( int ) * (size_t) old->nframe );
         new->map = astMalloc( sizeof( AstMapping * ) *
                               (size_t) ( old->nnode - 1 ) );
         new->link = astStore( NULL, old->link,
                               sizeof( int ) * (size_t) ( old->nnode - 1 ) );
         new->invert = astStore( NULL, old->invert,
                                 sizeof( int ) * (size_t) ( old->nnode - 1 ) );

/* If OK, clone the pointer to each Frame and Mapping referenced by
   the original FrameSet and store the resulting pointers in the new
   FrameSet. */
         if ( astOK ) {
            for ( iframe = 0; iframe < old->nframe; iframe++ ) {
               new->frame[ iframe ] = astClone( old->frame[ iframe ] );
            }
            for ( inode = 0; inode < old->nnode - 1; inode++ ) {
               new->map[ inode ] = astClone( old->map[ inode ] );
            }

/* If an error occurred while cloning any of these pointers, clean up
   by looping through the arrays of cloned pointers again and
   annulling them all. */
            if ( !astOK ) {
               for ( iframe = 0; iframe < old->nframe; iframe++ ) {
                  new->frame[ iframe ] = astAnnul( new->frame[ iframe ] );
               }
               for ( inode = 0; inode < old->nnode - 1; inode++ ) {
                  new->map[ inode ] = astAnnul( new->map[ inode ] );
               }
            }
         }

/* If an error occurred, clean up by freeing all memory allocated
   above. */
         if ( !astOK ) {
            new->frame = astFree( new->frame );
            new->node = astFree( new->node );
            new->varfrm = astFree( new->varfrm );
            new->map = astFree( new->map );
            new->link = astFree( new->link );
            new->invert = astFree( new->invert );
         }

/* Copy the Frame and node counts across. */
         new->nframe = old->nframe;
         new->nnode = old->nnode;

/* Copy the other FrameSet attributes across. */
         new->base = old->base;
         new->current = old->current;

/* Transfer any other inherited attribute values that relate to the
   FrameSet itself (rather than the enclosed Frames). */
        if ( astTestInvert( old ) ) astSetInvert( new, astGetInvert( old ) );
      }

/* If an error occurred, clean up by deleting the new object. */
      if ( !astOK ) new = astDelete( new );
   }

/* Return a pointer to the new object. */
   return new;

/* Undefine macros local to this function. */
#undef TRANSFER
}

AstFrameSet *astLoadFrameSet_( void *mem, size_t size,
                               AstFrameSetVtab *vtab, const char *name,
                               AstChannel *channel, int *status ) {
/*
*+
*  Name:
*     astLoadFrameSet

*  Purpose:
*     Load a FrameSet.

*  Type:
*     Protected function.

*  Synopsis:
*     #include "frameset.h"
*     AstFrameSet *astLoadFrameSet( void *mem, size_t size,
*                                   AstFrameSetVtab *vtab, const char *name,
*                                   AstChannel *channel )

*  Class Membership:
*     FrameSet loader.

*  Description:
*     This function is provided to load a new FrameSet using data read
*     from a Channel. It first loads the data used by the parent class
*     (which allocates memory if necessary) and then initialises a
*     FrameSet structure in this memory, using data read from the
*     input Channel.

*  Parameters:
*     mem
*        A pointer to the memory into which the FrameSet is to be
*        loaded.  This must be of sufficient size to accommodate the
*        FrameSet data (sizeof(FrameSet)) plus any data used by
*        derived classes. If a value of NULL is given, this function
*        will allocate the memory itself using the "size" parameter to
*        determine its size.
*     size
*        The amount of memory used by the FrameSet (plus derived class
*        data).  This will be used to allocate memory if a value of
*        NULL is given for the "mem" parameter. This value is also
*        stored in the FrameSet structure, so a valid value must be
*        supplied even if not required for allocating memory.
*
*        If the "vtab" parameter is NULL, the "size" value is ignored
*        and sizeof(AstFrameSet) is used instead.
*     vtab
*        Pointer to the start of the virtual function table to be
*        associated with the new FrameSet. If this is NULL, a pointer
*        to the (static) virtual function table for the FrameSet class
*        is used instead.
*     name
*        Pointer to a constant null-terminated character string which
*        contains the name of the class to which the new object
*        belongs (it is this pointer value that will subsequently be
*        returned by the astGetClass method).
*
*        If the "vtab" parameter is NULL, the "name" value is ignored
*        and a pointer to the string "FrameSet" is used instead.

*  Returned Value:
*     A pointer to the new FrameSet.

*  Notes:
*     - A null pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*-
*/

/* Local Constants: */
   astDECLARE_GLOBALS            /* Pointer to thread-specific global data */
#define KEY_LEN 50               /* Maximum length of a keyword */

/* Local Variables: */
   AstFrameSet *new;             /* Pointer to the new FrameSet */
   char key[ KEY_LEN + 1 ];      /* Buffer for keyword string */
   int ifr;                      /* Get a pointer to the thread specific global data structure. */

/* Loop counter for Frames */
   int inode;                    /* Loop counter for nodes */

   astGET_GLOBALS(channel);

/* Initialise. */
   new = NULL;

/* Check the global error status. */
   if ( !astOK ) return new;

/* If a NULL virtual function table has been supplied, then this is
   the first loader to be invoked for this FrameSet. In this case the
   FrameSet belongs to this class, so supply appropriate values to be
   passed to the parent class loader (and its parent, etc.). */
   if ( !vtab ) {
      size = sizeof( AstFrameSet );
      vtab = &class_vtab;
      name = "FrameSet";

/* If required, initialise the virtual function table for this class. */
      if ( !class_init ) {
         astInitFrameSetVtab( vtab, name );
         class_init = 1;
      }
   }

/* Invoke the parent class loader to load data for all the ancestral
   classes of the current one, returning a pointer to the resulting
   partly-built FrameSet. */
   new = astLoadFrame( mem, size, (AstFrameVtab *) vtab, name,
                       channel );

   if ( astOK ) {

/* Read input data. */
/* ================ */
/* Request the input Channel to read all the input data appropriate to
   this class into the internal "values list". */
      astReadClassData( channel, "FrameSet" );

/* Now read each individual data item from this list and use it to
   initialise the appropriate instance variable(s) for this class. */

/* In the case of attributes, we first read the "raw" input value,
   supplying the "unset" value as the default. If a "set" value is
   obtained, we then use the appropriate (private) Set... member
   function to validate and set the value properly. */

/* Nframe. */
/* ------- */
      new->nframe = astReadInt( channel, "nframe", 1 );
      if ( new->nframe < 0 ) new->nframe = 1;

/* Number of nodes. */
/* ---------------- */
      new->nnode = astReadInt( channel, "nnode", new->nframe );
      if ( new->nnode < 1 ) new->nnode = 1;

/* Allocate memory to hold Frame and node information. */
      new->frame = astMalloc( sizeof( AstFrame *) * (size_t) new->nframe );
      new->node = astMalloc( sizeof( int ) * (size_t) new->nframe );
      new->varfrm = astMalloc( sizeof( int ) * (size_t) new->nframe );
      new->link = astMalloc( sizeof( int ) * (size_t) ( new->nnode - 1 ) );
      new->invert = astMalloc( sizeof( int ) * (size_t) ( new->nnode - 1 ) );
      new->map = astMalloc( sizeof( AstMapping * ) *
                            (size_t) ( new->nnode - 1 ) );

/* If an error occurs, ensure that all allocated memory is freed. */
      if ( !astOK ) {
         new->frame = astFree( new->frame );
         new->node = astFree( new->node );
         new->varfrm = astFree( new->varfrm );
         new->link = astFree( new->link );
         new->invert = astFree( new->invert );
         new->map = astFree( new->map );

/* Otherwise, initialise the arrays which will hold Object pointers. */
      } else {
         for ( ifr = 1; ifr <= new->nframe; ifr++ ) {
            new->frame[ ifr - 1 ] = NULL;
         }
         for ( inode = 1; inode < new->nnode; inode++ ) {
            new->map[ inode - 1 ] = NULL;
         }

/* Read Frame data... */
         for ( ifr = 1; ifr <= new->nframe; ifr++ ) {

/* Frame objects. */
/* -------------- */
/* Create the required keyword and then read the Frame. */
            (void) sprintf( key, "frm%d", ifr );
            new->frame[ ifr - 1 ] = astReadObject( channel, key, NULL );

/* Node index for each Frame. */
/* -------------------------- */
            (void) sprintf( key, "nod%d", ifr );
            new->node[ ifr - 1 ] = astReadInt( channel, key, ifr ) - 1;

/* Index of variants Frame. */
/* ------------------------ */
            (void) sprintf( key, "vfr%d", ifr );
            new->varfrm[ ifr - 1 ] = astReadInt( channel, key, 0 );
         }

/* Read node data... */
         for ( inode = 1; inode < new->nnode; inode++ ) {

/* Links between nodes. */
/* -------------------- */
            (void) sprintf( key, "lnk%d", inode + 1 );
            new->link[ inode - 1 ] = astReadInt( channel, key, 0 ) - 1;

/* Inversion flags. */
/* ---------------- */
            (void) sprintf( key, "inv%d", inode + 1 );
            new->invert[ inode - 1 ] = astReadInt( channel, key, 0 );

/* Mapping objects. */
/* ---------------- */
            (void) sprintf( key, "map%d", inode + 1 );
            new->map[ inode - 1 ] = astReadObject( channel, key, NULL );
         }

/* Read remaining data... */

/* Base. */
/* ----- */
         new->base = astReadInt( channel, "base", -INT_MAX );
         if ( new->base < 1 ) new->base = -INT_MAX;

/* Current. */
/* -------- */
         new->current = astReadInt( channel, "currnt", -INT_MAX );
         if ( new->base < 1 ) new->base = -INT_MAX;
      }

/* If an error occurred, clean up by deleting the new FrameSet. */
      if ( !astOK ) new = astDelete( new );
   }

/* Return the new FrameSet pointer. */
   return new;

/* Undefine macros local to this function. */
#undef KEY_LEN
}

/* Virtual function interfaces. */
/* ============================ */
/* These provide the external interface to the virtual functions defined by
   this class. Each simply checks the global error status and then locates and
   executes the appropriate member function, using the function pointer stored
   in the object's virtual function table (this pointer is located using the
   astMEMBER macro defined in "object.h").

   Note that the member function may not be the one defined here, as it may
   have been over-ridden by a derived class. However, it should still have the
   same interface. */
void astAddFrame_( AstFrameSet *this, int iframe, AstMapping *map,
                   AstFrame *frame, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,FrameSet,AddFrame))( this, iframe, map, frame, status );
}
void astClearBase_( AstFrameSet *this, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,FrameSet,ClearBase))( this, status );
}
void astClearCurrent_( AstFrameSet *this, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,FrameSet,ClearCurrent))( this, status );
}
void astClearVariant_( AstFrameSet *this, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,FrameSet,ClearVariant))( this, status );
}
int astGetBase_( AstFrameSet *this, int *status ) {
   if ( !astOK ) return 0;
   return (**astMEMBER(this,FrameSet,GetBase))( this, status );
}
int astGetCurrent_( AstFrameSet *this, int *status ) {
   if ( !astOK ) return 0;
   return (**astMEMBER(this,FrameSet,GetCurrent))( this, status );
}
const char *astGetVariant_( AstFrameSet *this, int *status ) {
   if ( !astOK ) return NULL;
   return (**astMEMBER(this,FrameSet,GetVariant))( this, status );
}
AstFrame *astGetFrame_( AstFrameSet *this, int iframe, int *status ) {
   if ( !astOK ) return NULL;
   return (**astMEMBER(this,FrameSet,GetFrame))( this, iframe, status );
}
AstMapping *astGetMapping_( AstFrameSet *this, int iframe1, int iframe2, int *status ) {
   if ( !astOK ) return NULL;
   return (**astMEMBER(this,FrameSet,GetMapping))( this, iframe1, iframe2, status );
}
int astGetNframe_( AstFrameSet *this, int *status ) {
   if ( !astOK ) return 0;
   return (**astMEMBER(this,FrameSet,GetNframe))( this, status );
}
void astRemapFrame_( AstFrameSet *this, int iframe, AstMapping *map, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,FrameSet,RemapFrame))( this, iframe, map, status );
}
void astAddVariant_( AstFrameSet *this, AstMapping *map, const char *name, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,FrameSet,AddVariant))( this, map, name, status );
}
void astMirrorVariants_( AstFrameSet *this, int iframe, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,FrameSet,MirrorVariants))( this, iframe, status );
}
void astRemoveFrame_( AstFrameSet *this, int iframe, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,FrameSet,RemoveFrame))( this, iframe, status );
}
void astSetBase_( AstFrameSet *this, int ibase, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,FrameSet,SetBase))( this, ibase, status );
}
void astSetCurrent_( AstFrameSet *this, int icurrent, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,FrameSet,SetCurrent))( this, icurrent, status );
}
void astSetVariant_( AstFrameSet *this, const char *variant, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,FrameSet,SetVariant))( this, variant, status );
}
int astTestBase_( AstFrameSet *this, int *status ) {
   if ( !astOK ) return 0;
   return (**astMEMBER(this,FrameSet,TestBase))( this, status );
}
int astTestCurrent_( AstFrameSet *this, int *status ) {
   if ( !astOK ) return 0;
   return (**astMEMBER(this,FrameSet,TestCurrent))( this, status );
}
int astTestVariant_( AstFrameSet *this, int *status ) {
   if ( !astOK ) return 0;
   return (**astMEMBER(this,FrameSet,TestVariant))( this, status );
}
int astValidateFrameIndex_( AstFrameSet *this, int iframe,
                            const char *method, int *status ) {
   if ( !astOK ) return 0;
   return (**astMEMBER(this,FrameSet,ValidateFrameIndex))( this, iframe,
                                                           method, status );
}
const char *astGetAllVariants_( AstFrameSet *this, int *status ) {
   if ( !astOK ) return NULL;
   return (**astMEMBER(this,FrameSet,GetAllVariants))( this, status );
}

/* Special public interface functions. */
/* =================================== */
/* These provide the public interface to certain special functions
   whose public interface cannot be handled using macros (such as
   astINVOKE) alone. In general, they are named after the
   corresponding protected version of the function, but with "Id"
   appended to the name. */

/* Public Interface Function Prototypes. */
/* ------------------------------------- */
/* The following functions have public prototypes only (i.e. no
   protected prototypes), so we must provide local prototypes for use
   within this module. */
AstFrameSet *astFrameSetId_( void *, const char *, ... );

/* Special interface function implementations. */
/* ------------------------------------------- */
AstFrameSet *astFrameSetId_( void *frame_void, const char *options, ... ) {
/*
*++
*  Name:
c     astFrameSet
f     AST_FRAMESET

*  Purpose:
*     Create a FrameSet.

*  Type:
*     Public function.

*  Synopsis:
c     #include "frameset.h"
c     AstFrameSet *astFrameSet( AstFrame *frame, const char *options, ... )
f     RESULT = AST_FRAMESET( FRAME, OPTIONS, STATUS )

*  Class Membership:
*     FrameSet constructor.

*  Description:
*     This function creates a new FrameSet and optionally initialises
*     its attributes.
*
*     A FrameSet consists of a set of one or more Frames (which
*     describe coordinate systems), connected together by Mappings
*     (which describe how the coordinate systems are inter-related). A
*     FrameSet makes it possible to obtain a Mapping between any pair
*     of these Frames (i.e. to convert between any of the coordinate
*     systems which it describes).  The individual Frames are
*     identified within the FrameSet by an integer index, with Frames
*     being numbered consecutively from one as they are added to the
*     FrameSet.
*
*     Every FrameSet has a "base" Frame and a "current" Frame (which
*     are allowed to be the same). Any of the Frames may be nominated
*     to hold these positions, and the choice is determined by the
*     values of the FrameSet's Base and Current attributes, which hold
*     the indices of the relevant Frames.  By default, the first Frame
*     added to a FrameSet is its base Frame, and the last one added is
*     its current Frame.
*
*     The base Frame describes the "native" coordinate system of
*     whatever the FrameSet is used to calibrate (e.g. the pixel
*     coordinates of an image) and the current Frame describes the
*     "apparent" coordinate system in which it should be viewed
*     (e.g. displayed, etc.). Any further Frames represent a library
*     of alternative coordinate systems, which may be selected by
*     making them current.
*
*     When a FrameSet is used in a context that requires a Frame,
*     (e.g. obtaining its Title value, or number of axes), the current
*     Frame is used. A FrameSet may therefore be used in place of its
*     current Frame in most situations.
*
*     When a FrameSet is used in a context that requires a Mapping,
*     the Mapping used is the one between its base Frame and its
*     current Frame. Thus, a FrameSet may be used to convert "native"
*     coordinates into "apparent" ones, and vice versa. Like any
c     Mapping, a FrameSet may also be inverted (see astInvert), which
f     Mapping, a FrameSet may also be inverted (see AST_INVERT), which
*     has the effect of interchanging its base and current Frames and
*     hence of reversing the Mapping between them.
*
*     Regions may be added into a FrameSet (since a Region is a type of
*     Frame), either explicitly or as components within CmpFrames. In this
*     case the Mapping between a pair of Frames within a FrameSet will
*     include the effects of the clipping produced by any Regions included
*     in the path between the Frames.

*  Parameters:
c     frame
f     FRAME = INTEGER (Given)
*        Pointer to the first Frame to be inserted into the
*        FrameSet. This initially becomes both the base and the
*        current Frame. (Further Frames may be added using the
c        astAddFrame function.)
f        AST_ADDFRAME routine.)
c     options
f     OPTIONS = CHARACTER * ( * ) (Given)
c        Pointer to a null-terminated string containing an optional
c        comma-separated list of attribute assignments to be used for
c        initialising the new FrameSet. The syntax used is identical to
c        that for the astSet function and may include "printf" format
c        specifiers identified by "%" symbols in the normal way.
c        If no initialisation is required, a zero-length string may be
c        supplied.
f        A character string containing an optional comma-separated
f        list of attribute assignments to be used for initialising the
f        new FrameSet. The syntax used is identical to that for the
f        AST_SET routine. If no initialisation is required, a blank
f        value may be supplied.
c     ...
c        If the "options" string contains "%" format specifiers, then
c        an optional list of additional arguments may follow it in
c        order to supply values to be substituted for these
c        specifiers. The rules for supplying these are identical to
c        those for the astSet function (and for the C "printf"
c        function).
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Returned Value:
c     astFrameSet()
f     AST_FRAMESET
*        A pointer to the new FrameSet.

*  Notes:
c     - If a pointer to an existing FrameSet is given for the "frame"
c     parameter, then the new FrameSet will (as a special case) be
f     - If a pointer to an existing FrameSet is given for the FRAME
f     argument, then the new FrameSet will (as a special case) be
*     initialised to contain the same Frames and Mappings, and to have
*     the same attribute values, as the one supplied. This process is
c     similar to making a copy of a FrameSet (see astCopy), except
f     similar to making a copy of a FrameSet (see AST_COPY), except
*     that the Frames and Mappings contained in the original are not
*     themselves copied, but are shared by both FrameSets.
*     - A null Object pointer (AST__NULL) will be returned if this
c     function is invoked with the AST error status set, or if it
f     function is invoked with STATUS set to an error value, or if it
*     should fail for any reason.
*--

*  Implementation Notes:
*     - This function implements the external (public) interface to
*     the astFrameSet constructor function. It returns an ID value
*     (instead of a true C pointer) to external users, and must be
*     provided because astFrameSet_ has a variable argument list which
*     cannot be encapsulated in a macro (where this conversion would
*     otherwise occur).
*     - Because no checking or casting of arguments is performed
*     before the function is invoked, the "frame" parameter is of type
*     (void *) and is converted from an ID value to a pointer and
*     validated within the function itself.
*     - The variable argument list also prevents this function from
*     invoking astFrameSet_ directly, so it must be a
*     re-implementation of it in all respects, except for the
*     conversions between IDs and pointers on input/output of Objects.
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Pointer to thread-specific global data */
   AstFrame *frame;              /* Pointer to Frame structure */
   AstFrameSet *new;             /* Pointer to new FrameSet */
   va_list args;                 /* Variable argument list */

   int *status;

/* Initialise. */
   new = NULL;

/* Get a pointer to the thread specific global data structure. */
   astGET_GLOBALS(NULL);

/* Pointer to inherited status value */

/* Get a pointer to the inherited status value. */
   status = astGetStatusPtr;

/* Check the global status. */
   if ( !astOK ) return new;

/* Obtain the Frame pointer from the ID supplied and validate the
   pointer to ensure it identifies a valid Frame. */
   frame = astVerifyFrame( astMakePointer( frame_void ) );
   if ( astOK ) {

/* Initialise the FrameSet, allocating memory and initialising the
   virtual function table as well if necessary. */
      new = astInitFrameSet( NULL, sizeof( AstFrameSet ), !class_init,
                             &class_vtab, "FrameSet", frame );

/* If successful, note that the virtual function table has been
   initialised. */
      if ( astOK ) {
         class_init = 1;

/* Obtain the variable argument list and pass it along with the
   options string to the astVSet method to initialise the new
   FrameSet's attributes. */
         va_start( args, options );
         astVSet( new, options, NULL, args );
         va_end( args );

/* If an error occurred, clean up by deleting the new object. */
         if ( !astOK ) new = astDelete( new );
      }
   }

/* Return an ID value for the new FrameSet. */
   return astMakeId( new );
}