File: ADF_interface.c

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

***/

/***********************************************************************
    Library and Database "what" strings.
***********************************************************************/
/** Change the major revision letter in the Library Version for changes
    to the API (new public functions, changes to public header files,
    changes to existings functions or their defined behavior)
    and/or changes to the internal file format resulting in
    incompatibilites with previous library versions.  Change the
    internal revision number for internal changes and bug fixes;
    reset to zero for major revision letter changes. **/

/** change suggested by Kevin Mack of Adapco
    With the original ADF library, there is no binary data for at least
    the first 560 bytes, which causes a lot of programs
    (mailers, WinZip) to think that the file is text and try to do
    a \n -> \n\r conversion.  Since this string is only used for the
    'what' command, I am deciding that we don't need this functionality
    and am putting binary characters here. Specifically, I am putting
    control characters, because while some programs (Evolution/gnome-vfs)
    look for unprintable characters, some look for a ratio (Mozilla). **/

/** modification by Bruce Wedan
    I'm modifying the 1st 4 bytes of the header, @(#),  by turning on the
    high bit. This makes these bytes non-ASCII and should not effect the
    check/reporting of version number **/

static char ADF_L_identification[] = "\300\250\243\251ADF Library  Version E01>" ;
                                 /*   0   1   2   3   4567890123456789012345678901 = 32 */

/** Change version database version number every time the library
    version changes according to the following philosophy.

The format:

      AXXxxx

where:

      A      Major revision number.  Major internal structure changes.
             This number is not expected to change very often if at all
             because backward compatibility is only available by explicit
             policy decision.

             One alphabetic character.
             Range of values:  A-Za-z
             In unlikely event of reaching z, then can use any other
             unused printable ASCII character except blank or symbols
             used by "what" command: @, (, #, ), ~, >, \.

      XX     Minor revision number.  New features and minor changes and
             bug fixes.  Files are backward but NOT forward compatible.

             Two digit hexadecimal number (uppercase letters).
             Range of values:  00 - FF
             Reset to 00 with changes in major revision number.

      xxx    Incremental number.  Incremented with every new version of
             library (even if no changes are made to file format).
             Files are forward AND backward compatible.

             Three digit hexadecimal number (lowercase letters)
             Range of values:  000 to fff
             Does not reset.

Definitions:

   forward compatible     Older versions of libraries can read and write
                          to files created by newer versions of libraries.

   backward compatible    Newer versions of libraries can read and write
                          to files created by older versions of libraries.
**/

                                 /*                            AXXxxx       */
static char ADF_D_identification[] = "\300\250\243\251ADF Database Version A02011>" ;
                                 /*   0   1   2   3   4567890123456789012345678901 = 32 */

/***********************************************************************
    Includes
***********************************************************************/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#if defined(_WIN32) && !defined(__NUTC__)
#include <io.h>
#else
#include <unistd.h>
#endif
#include <stdlib.h>
#include <ctype.h>
#include "ADF.h"
#include "ADF_internals.h"
#if defined(_WIN32) && !defined(__NUTC__)
#include <ctype.h>
#ifndef F_OK
#define R_OK    004     /* Test for Read permission */
#define W_OK    002     /* Test for Write permission */
#define X_OK    001     /* Test for eXecute permission */
#define F_OK    000     /* Test for existence of File */
#endif
#endif
#ifdef MEM_DEBUG
#include "cg_malloc.h"
#endif


/***********************************************************************
    Error strings
    These strings must be kept in sync with the error defines in ADF.h.
***********************************************************************/
const char  *ADF_error_string[] = {
   "ADF -1: No Error.",
   "ADF  1: Integer number is less than given minimum value.",
   "ADF  2: Integer number is greater than given maximum value.",
   "ADF  3: String length of zero or blank string detected.",
   "ADF  4: String length longer than maximum allowable length.",
   "ADF  5: String is not an ASCII-HEX string.",
   "ADF  6: Too many ADF files opened.",
   "ADF  7: ADF file status was not recognized.",
   "ADF  8: ADF file-open error.",
   "ADF  9: ADF file not currently opened.",
   "ADF 10: ADF file index out of legal range.",
   "ADF 11: Block/offset out of legal range.",
   "ADF 12: A string pointer is NULL.",
   "ADF 13: FSEEK error.",
   "ADF 14: FWRITE error.",
   "ADF 15: FREAD error.",
   "ADF 16: Internal error: Memory boundary tag bad.",
   "ADF 17: Internal error: Disk boundary tag bad.",
   "ADF 18: File Open Error: NEW - File already exists.",
   "ADF 19: ADF file format was not recognized.",
   "ADF 20: Attempt to free the RootNode disk information.",
   "ADF 21: Attempt to free the FreeChunkTable disk information.",
   "ADF 22: File Open Error: OLD - File does not exist.",
   "ADF 23: Entered area of Unimplemented Code...",
   "ADF 24: Sub-Node.entries is bad.",
   "ADF 25: Memory allocation failed.",
   "ADF 26: Duplicate child name under a parent node.",
   "ADF 27: Node has no dimensions.",
   "ADF 28: Node's number-of-dimensions is not in legal range.",
   "ADF 29: Specified child is NOT a child of the specified parent.",
   "ADF 30: Data-Type is too long.",
   "ADF 31: Invalid Data-Type.",
   "ADF 32: A pointer is NULL.",
   "ADF 33: Node has no data associated with it.",
   "ADF 34: Error zeroing out memory.",
   "ADF 35: Requested data exceeds actual data available.",
   "ADF 36: Bad end value.",
   "ADF 37: Bad stride value.",
   "ADF 38: Minimum values is greater than the maximum value.",
   "ADF 39: The format of this machine does not match a known signature.",
   "ADF 40: Cannot convert to or from an unknown Native format.",
   "ADF 41: The two conversion formats are equal, no conversion done.",
   "ADF 42: The data format is not support on a particular machine.",
   "ADF 43: File Close error.",
   "ADF 44: Numeric overflow/underflow in data conversion.",
   "ADF 45: Bad start value.",
   "ADF 46: A value of zero is not allowable.",
   "ADF 47: Bad dimension value.",
   "ADF 48: Error state must be either a 0 (zero) or a 1 (one).",
   "ADF 49: Dimensional specifications for disk and memory are unequal.",
   "ADF 50: Too many link level used.  May be caused by a recursive link.",
   "ADF 51: The node is not a link.  It was expected to be a link.",
   "ADF 52: The linked-to node does not exist.",
   "ADF 53: The ADF file of a linked-node is not accessable.",
   "ADF 54: A node-id of 0.0 is not valid.",
   "ADF 55: Incomplete Data when reading multiple data blocks.",
   "ADF 56: Node name contains invalid characters.",
   "ADF 57: ADF file version incompatible with this library version.",
   "ADF 58: Nodes are not from the same file.",
   "ADF 59: Priority Stack Error.",
   "ADF 60: Machine format and file format are incompatable.",
   "ADF 61: FFLUSH error",
   "ADF 62: The node ID pointer is NULL.",
   "ADF 63: The maximum size for a file exceeded.",
   "ADF  x: Last error mesage"
   } ;

/***********************************************************************
    Global Variables:
***********************************************************************/
int ADF_sys_err = 0;
static int  ADF_abort_on_error = FALSE ;

int ADF_n_paths = 0;
char **ADF_paths = 0;

extern char data_chunk_start_tag[];

#define CHECK_ADF_ABORT( error_flag ) if( error_flag != NO_ERROR ) { \
                    if( ADF_abort_on_error == TRUE ) {    \
                      ADF_Error_Message( error_flag, 0L );\
                      ADFI_Abort( error_flag) ; }         \
                    else { return ; } }
/* Added to remove memory leaks in ADF_Get_Node_ID */
#define CHECK_ADF_ABORT1( error_flag ) if( error_flag != NO_ERROR ) { \
                    free (name_tmp);                      \
                    if( ADF_abort_on_error == TRUE ) {    \
                      ADF_Error_Message( error_flag, 0L );\
                      ADFI_Abort( error_flag) ; }         \
                    else { return ; } }

/***********************************************************************
ADF_Search_Add - add a path to link file search list
************************************************************************/
void ADF_Search_Add(const char *path, int *error_return)
{
    if (path == NULL || !*path) {
        *error_return = NULL_STRING_POINTER;
        return;
    }
    if (ADF_n_paths)
        ADF_paths = (char **)realloc(ADF_paths,(ADF_n_paths+1)*sizeof(char *));
    else
        ADF_paths = (char **)malloc(sizeof(char *));
    if (ADF_paths == NULL) {
        *error_return = MEMORY_ALLOCATION_FAILED;
        return;
    }
    ADF_paths[ADF_n_paths] = (char *)malloc(strlen(path)+1);
    if (ADF_paths[ADF_n_paths] == NULL) {
        *error_return = MEMORY_ALLOCATION_FAILED;
        return;
    }
    strcpy(ADF_paths[ADF_n_paths], path);
    ADF_n_paths++;
    *error_return = NO_ERROR;
}
/***********************************************************************
ADF_Search_Delete - delete the link path search list
************************************************************************/
void ADF_Search_Delete(int *error_return)
{
    if (ADF_n_paths) {
        int n;
        for (n = 0; n < ADF_n_paths; n++) {
            if (ADF_paths[n] != NULL) free(ADF_paths[n]);
        }
        free(ADF_paths);
        ADF_n_paths = 0;
        ADF_paths = 0;
        *error_return = NO_ERROR;
    }
    else
        *error_return = NO_DATA;
}

/***********************************************************************
Data Query:
Note:  If the node is a link, the data query will occur on the linked-to
node, not the node which is the link.
Internal Implementation:  A linked node will have a data-type of "LK",
dimension of 1 and a dimension value of the length of a data string
containing the file-path and the node-path within the file.  The
routines ADF_Is_Link and ADF_Get_Link_Path allow viewing of a link's
data-type and data.
***********************************************************************/
/***********************************************************************
Data I/O:
A 1-based system is used with all index values  (the first element has an
index of 1, not 0).
***********************************************************************/
/* end of file ADF_AAA_var.c */
/* end of file ADF_AAA_var.c */
/* file ADF_Children_Names.c */
/***********************************************************************
ADF Children names:

Get Children names of a Node.  Return the name of children nodes
directly associated with a parent node.  The names of the children
are NOT guaranteed to be returned in any particular order.  If a new
child is added, it is NOT guaranteed to be returned as the last child.

Null-terminated names will be written into the names array and thus
there needs to be room for the null character.  As an example,
the array can be defined as:

   char  names[IMAX_NUM][IMAX_NAME_LENGTH+1];

where IMAX_NUM and IMAX_NAME_LENGTH are defined by the using application
and correspond to this function's "imax_num" and "imax_name_len" parameters
respectively.  "imax_name_len" is the maximum length of a name to be copied
into the names array.  This value can be equal to ADF_NAME_LENGTH but does
not have to be.  However, the name dimension of the array MUST be declared
to be "imax_name_len" + 1.  The name will be returned truncated (but still
null-terminated) if the actual name is longer than "imax_name_len" and
if "imax_name_len" is less than ADF_NAME_LENGTH.

Note that the names array parameter is declared as a single dimension
character array inside this function.  Therefore, use a (char *) cast to
cast a two dimensional array argument.

ADF_Children_Names( PID, istart, imax_num, imax_name_len, inum_ret,
                    names, error_return )
input:  const double PID          The ID of the Node to use.
input:  const int istart          The Nth child's name to start with (first is 1).
input:  const int imax_num        Maximum number of names to return.
input:  const int imax_name_len   Maximum Length of a name to return.
output: int *inum_ret             The number of names returned.
output: char *names               The returned names (cast with (char *)).
output: int *error_return         Error return.

   Possible errors:
NO_ERROR
NULL_STRING_POINTER
NULL_POINTER
NUMBER_LESS_THAN_MINIMUM
***********************************************************************/
void    ADF_Children_Names(
        const double PID,
        const int istart,
        const int imax_num,
        const int imax_name_len,
        int *inum_ret,
        char *names,
        int *error_return )
{
int                         i ;
unsigned int                file_index ;
struct DISK_POINTER         block_offset ;
struct NODE_HEADER          node ;
struct SUB_NODE_TABLE_ENTRY sub_node_table_entry ;
double                      LID ;

*error_return = NO_ERROR ;

if( inum_ret == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */
*inum_ret = 0 ;

if( names == NULL ) {
   *error_return = NULL_STRING_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

if( (istart <=0) || (imax_num <= 0) || (imax_name_len <= 0) ) {
   *error_return = NUMBER_LESS_THAN_MINIMUM ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

ADFI_chase_link( PID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

   /** Check for zero children, return if 0 **/
if( node.num_sub_nodes == 0 ) {
   return ;
   } /* end if */

   /** point to the first child wanted **/
block_offset.block = node.sub_node_table.block ;
block_offset.offset = node.sub_node_table.offset +
                      (TAG_SIZE + DISK_POINTER_SIZE +
                      (ADF_NAME_LENGTH + DISK_POINTER_SIZE) * (istart-1)) ;

   /** Return the data for the requested children **/
for( i=(istart-1); i< MIN(istart-1+imax_num, (int) node.num_sub_nodes); i++ ) {
   ADFI_adjust_disk_pointer( &block_offset, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

   /** Read the sun-node entry table **/
   ADFI_read_sub_node_table_entry(  file_index, &block_offset,
                &sub_node_table_entry, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

   /** Convert the child's name from blank-filled into a C string **/
   ADFI_string_2_C_string( sub_node_table_entry.child_name,
                           MIN(imax_name_len,ADF_NAME_LENGTH),
                           &names[(i-(istart-1))*(imax_name_len+1)],
                           error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

   /** Increment the disk-pointer and the number of names returned **/
   block_offset.offset += (ADF_NAME_LENGTH + DISK_POINTER_SIZE) ;
   *inum_ret = *inum_ret + 1 ;
   } /* end for */
} /* end of ADF_Children_Names */
/* end of file ADF_Children_Names.c */
/* file ADF_Children_IDs.c */
/***********************************************************************
ADF Children IDs:

Get Children node IDs of a Node.  Return the node IDs of children nodes
directly associated with a parent node.

ADF_Children_IDs( PID, istart, imax_num, inum_ret, IDs, error_return)
input:  const double PID          The ID of the Node to use.
input:  const int istart          The Nth child's name to start with (first is 1).
input:  const int imax_num        Maximum number of names to return.
output: int *inum_ret             The number of names returned.
output: double *IDs               The returned node IDs
output: int *error_return         Error return.

   Possible errors:
NO_ERROR
NULL_STRING_POINTER
NULL_POINTER
NUMBER_LESS_THAN_MINIMUM
***********************************************************************/
void    ADF_Children_IDs (
        const double PID,
        const int istart,
        const int imax_num,
        int *inum_ret,
        double *IDs,
        int *error_return )
{
int                         i ;
unsigned int                file_index ;
struct DISK_POINTER         block_offset ;
struct NODE_HEADER          node ;
struct SUB_NODE_TABLE_ENTRY sub_node_table_entry ;
double                      LID ;

*error_return = NO_ERROR ;

if( inum_ret == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */
*inum_ret = 0 ;

if( IDs == NULL ) {
   *error_return = NULL_NODEID_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

if( (istart <=0) || (imax_num <= 0) ) {
   *error_return = NUMBER_LESS_THAN_MINIMUM ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

ADFI_chase_link( PID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

   /** Check for zero children, return if 0 **/
if( node.num_sub_nodes == 0 ) {
   return ;
   } /* end if */

   /** point to the first child wanted **/
block_offset.block = node.sub_node_table.block ;
block_offset.offset = node.sub_node_table.offset +
                      (TAG_SIZE + DISK_POINTER_SIZE +
                      (ADF_NAME_LENGTH + DISK_POINTER_SIZE) * (istart-1)) ;

   /** Return the data for the requested children **/
for( i=(istart-1); i< MIN(istart-1+imax_num, (int) node.num_sub_nodes); i++ ) {
   ADFI_adjust_disk_pointer( &block_offset, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

   /** Read the sub-node entry table **/
   ADFI_read_sub_node_table_entry(  file_index, &block_offset,
                &sub_node_table_entry, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

    /** Get the ID from the sub-node table **/
   ADFI_file_block_offset_2_ID( file_index,
                sub_node_table_entry.child_location.block,
                sub_node_table_entry.child_location.offset,
                &IDs[i-(istart-1)], error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

   /** Increment the disk-pointer and the number of IDs returned **/
   block_offset.offset += (ADF_NAME_LENGTH + DISK_POINTER_SIZE) ;
   *inum_ret = *inum_ret + 1 ;
   } /* end for */
} /* end of ADF_Children_IDs */
/* end of file ADF_Children_IDs.c */
/* file ADF_Create.c */
/***********************************************************************
ADF Create:

Create a Node.  Create a new node (not a link-node) as a child of a
given parent.  Default values in this new node are:
	label=blank,
	number of sub-nodes = 0,
	data-type = "MT",
	number of dimensions = 0,
	data = NULL.

ADF_Create( PID, name, ID, error_return )
input:  const double PID	The ID of the parent node, to whom we
				are creating a new child node.
input:  const char *name	The name of the new child.
output: double *ID		The ID of the newly created node.
output: int *error_return	Error return.

   Possible errors:
NO_ERROR
NULL_STRING_POINTER
NULL_POINTER
***********************************************************************/
void	ADF_Create(
		const double PID,
		const char *name,
		double *ID,
		int *error_return )
{
unsigned int 			file_index ;
struct DISK_POINTER		parent_block_offset, child_block_offset ;
struct DISK_POINTER		sub_node_entry_location ;
struct NODE_HEADER		parent_node, child_node ;
struct SUB_NODE_TABLE_ENTRY	sub_node_entry ;
int				i, name_length, name_start, found ;
double				LID ;

ADFI_check_string_length( name, ADF_NAME_LENGTH, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( ID == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

ADFI_chase_link( PID, &LID, &file_index,  &parent_block_offset,
		&parent_node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;


	/** Initialize node header **/
ADFI_fill_initial_node_header( &child_node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Skip any leading blanks in the name **/
name_start = 0 ;
while( name[ name_start ] == ' ' ) {
   name_start++ ;
   } /* end while */
name_length = strlen( &name[ name_start ] ) ;
if( name_length > ADF_NAME_LENGTH ) {
   *error_return = STRING_LENGTH_TOO_BIG ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** Check for uniqueness and legality of the name **/
ADFI_check_4_child_name( file_index, &parent_block_offset,
	&name[ name_start ], &found, &sub_node_entry_location,
	&sub_node_entry, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;
if( found == 1 ) {
   *error_return = DUPLICATE_CHILD_NAME ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */
for ( i=0; i < name_length; i++ ) {
   if (  ! isprint ( name[ name_start + i ] ) ||
           name[ name_start + i ] == '/' ) {
      *error_return = INVALID_NODE_NAME;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */
   } /* end for */

	/** Assign the name to the new node **/
strncpy( child_node.name, &name[ name_start ], name_length ) ;

	/** Allocate disk space for the new node **/
ADFI_file_malloc( file_index, NODE_HEADER_SIZE, &child_block_offset,
	error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Write out the new node header **/
ADFI_write_node_header( file_index, &child_block_offset, &child_node,
		error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** OK, new node is on disk.  Now, update the list of
	    children for the parent...
	**/
ADFI_add_2_sub_node_table( file_index, &parent_block_offset,
	&child_block_offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Return the ID of the new child **/
ADFI_file_block_offset_2_ID( file_index, child_block_offset.block,
	child_block_offset.offset, ID, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Finally, update modification date **/
ADFI_write_modification_date( file_index, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Create */
/* end of file ADF_Create.c */
/* file ADF_Database_Close.c */
/***********************************************************************
ADF Database Close:

Close an opened database.  If the ADF database spans multiple files,
then all files used will also be closed.  If an ADF file which is
linked to by this database is also opened through another
database, only the opened file stream associated with this database
will be closed.

ADF_Database_Close( Root_ID, error_return )
input:  const double Root_ID	Root-ID of the ADF database.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Database_Close(
		const double Root_ID,
		int *error_return )
{
unsigned int 		file_index ;
struct DISK_POINTER	block_offset ;

*error_return = NO_ERROR ;

	/** Get the file, block, and offset numbers from the ID **/
ADFI_ID_2_file_block_offset( Root_ID, &file_index, &block_offset.block,
		&block_offset.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Close the ADF file (which may close other sub-files) **/
ADFI_close_file( file_index, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;
} /* end of ADF_Database_Close */
/* end of file ADF_Database_Close.c */
/* file ADF_Database_Delete.c */
/***********************************************************************
ADF Database Delete:

Delete an existing database.  This will delete one or more ADF files
which are linked together under file top ADF file named "filename".

ADF_Database_Delete( filename, error_return )
input:  char *filename		Filename of the ADF database to delete.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Database_Delete(
		const char *filename,
		int *error_return )
{
ADFI_check_string_length( filename, ADF_FILENAME_LENGTH, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

fprintf(stderr,"Subroutine ADF_Database_Delete is not yet implemented...\n" ) ;
*error_return = UNIMPLEMENTED_CODE ;
CHECK_ADF_ABORT( *error_return ) ;
} /* end of ADF_Database_Delete */
/* end of file ADF_Database_Delete.c */
/* file ADF_Database_Garbage_Collection.c */
/***********************************************************************
ADF Database Garbage Collection:

Garbage Collection.  This capability will most likely be implemented
internally and will not be user-callable.

ADF_Database_Garbage_Collection( ID, error_return )
input:  const double ID		The ID of a node in the ADF file in which
				to do garbage collection.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Database_Garbage_Collection(
		const double ID,
		int *error_return )
{
fprintf(stderr,
"Subroutine ADF_Database_Garbage_Collection is not yet implemented...\n" ) ;
*error_return = UNIMPLEMENTED_CODE ;
CHECK_ADF_ABORT( *error_return ) ;
} /* end of ADF_Database_Garbage_Collection */

/* end of file ADF_Database_Garbage_Collection.c */
/* file ADF_Database_Get_Format.c */
/***********************************************************************
ADF Database Get Format:

Get the data format used in an existing database.

ADF_Database_Get_Format( Root_ID, format, error_return )
input:  const double Root_ID	The root_ID of the ADF file.
output: char *format		See format for ADFDOPN.  Maximum of 20
				characters returned.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Database_Get_Format(
		const double Root_ID,
		char *format,
		int *error_return )
{
unsigned int 		file_index ;
struct DISK_POINTER	block_offset ;
struct	FILE_HEADER	file_header ;

if( format == NULL ) {
   *error_return = NULL_STRING_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** Get the file, block, and offset numbers from the ID **/
ADFI_ID_2_file_block_offset( Root_ID, &file_index, &block_offset.block,
		&block_offset.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get node_header for the node **/
ADFI_read_file_header( file_index, &file_header, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

#define	EVAL_2_BYTES( C0, C1 )  (((C0)<<8)+((C1)))

switch( EVAL_2_BYTES( file_header.numeric_format, file_header.os_size ) ) {
   case EVAL_2_BYTES( 'B', 'L' ) :
      strcpy( format, IEEE_BIG_32_FORMAT_STRING ) ;

      break ;

   case EVAL_2_BYTES( 'L', 'L' ) :
      strcpy( format, IEEE_LITTLE_32_FORMAT_STRING ) ;
      break ;

   case EVAL_2_BYTES( 'B', 'B' ) :
      strcpy( format, IEEE_BIG_64_FORMAT_STRING ) ;

      break ;

   case EVAL_2_BYTES( 'L', 'B' ) :
      strcpy( format, IEEE_LITTLE_64_FORMAT_STRING ) ;
      break ;

   case EVAL_2_BYTES( 'C', 'B' ) :
      strcpy( format, CRAY_FORMAT_STRING ) ;
      break ;

   case EVAL_2_BYTES( 'N', 'L' ) :
   case EVAL_2_BYTES( 'N', 'B' ) :
      strcpy( format, NATIVE_FORMAT_STRING ) ;
      break ;

   default:
      *error_return = ADF_FILE_FORMAT_NOT_RECOGNIZED ;
      return ;

   } /* end switch */

} /* end of ADF_Database_Get_Format */
/* end of file ADF_Database_Get_Format.c */
/* file ADF_Database_Open.c */
/***********************************************************************
ADF Database Open:

Open a database.  Open either a new or an existing ADF file.  If links to
other ADF files are used, these additional file will be opened
automatically as required.

ADF_Database_Open( filename, status, format, root_ID, error_return)
input:  const char *filename	Not used if status SCRATCH is used.
	Filename must be a legal name and may include a relative or
	absolute path.  It must be directly usable by the C fopen()
	system routine.

input:  const char *status_in	Like FORTRAN OPEN() status.
	Allowable values are:
		READ_ONLY - File must exist.  Writing NOT allowed.
		OLD - File must exist.  Reading and writing allowed.
		NEW - File must not exist.
		SCRATCH - New file.  Filename is ignored.
		UNKNOWN - OLD if file exists, else NEW is used.

input:  const char *format	Specifies the numeric format for the
		file.  If blank or NULL, the machine's native format is
		used.  This field is only used when a file is created.
	NATIVE - Determine the format on the machine.  If the
		native format is not one of the formats
		supported, the created file cannot be used on
		other machines.
	IEEE_BIG - Use the IEEE big ENDIAN format.
	IEEE_LITTLE - Use the IEEE little ENDIAN format.
	CRAY - Use the native Cray format.

output:  double *root_ID	Root-ID of the opened ADF database.
output:  int *error_return	Error return.

   Possible errors:
NO_ERROR
NULL_STRING_POINTER
ADF_FILE_STATUS_NOT_RECOGNIZED
REQUESTED_NEW_FILE_EXISTS
FILE_OPEN_ERROR
***********************************************************************/
void	ADF_Database_Open(
		const char *filename,
		const char *status_in,
		const char *format,
		double *Root_ID,
		int *error_return )
{
int                 iret ;
int                 error_dummy ;
char                machine_format, format_to_use, os_to_use ;
char                *status ;
int                 formats_compare ;
unsigned int        file_index ;
unsigned int        file_minor_version, lib_minor_version ;
struct  FILE_HEADER       file_header ;
struct  NODE_HEADER       node_header ;
struct  FREE_CHUNK_TABLE  free_chunk_table ;

file_header.tag0[0] = '\0' ;

status = (char *)status_in ;
if( status == NULL ) {
   *error_return = NULL_STRING_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
} /* end if */

if( Root_ID == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
} /* end if */

	/** DO NOT Check filename for NULL here, it may NOT be used... **/

*error_return = NO_ERROR ;

		/** Get this machine's numeric format **/
   ADFI_figure_machine_format( format, &machine_format, &format_to_use,
			       &os_to_use, error_return ) ;

if( ADFI_stridx_c( status, "SCRATCH" ) != 0 ) {
   ADFI_check_string_length( filename, ADF_FILENAME_LENGTH, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
} /* end if */
ADFI_check_string_length( status, ADF_STATUS_LENGTH, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Determine the requested STATUS **/
if( ADFI_stridx_c( status, "UNKNOWN" ) == 0 ) {
	/** Determine the assessability of the filename **/
   iret = access( filename, F_OK ) ;
   if( iret != 0 ) /* File does not exist, set status to NEW */
      status = "NEW" ;
   else
      status = "OLD" ;
} /* end else if */

if( (ADFI_stridx_c( status, "READ_ONLY" ) == 0) ||
    (ADFI_stridx_c( status, "OLD" ) == 0) ) {
	/** Determine the assessability of the filename **/
   iret = access( filename, F_OK ) ;
   if( iret != 0 ) { /* File does not exist, this is BAD for OLD */
      *error_return = REQUESTED_OLD_FILE_NOT_FOUND ;
      CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** open the file **/
   ADFI_open_file( filename, status, &file_index, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
} /* end else if */

else if( (ADFI_stridx_c( status, "NEW" ) == 0) ||
         (ADFI_stridx_c( status, "SCRATCH" ) == 0) ) {
	/** Determine the assessability of the filename **/
   if( ADFI_stridx_c( status, "NEW" ) == 0 ) {
      iret = access( filename, F_OK ) ;
      if( iret == 0 ) { /* File exists, this is BAD for NEW */
         *error_return = REQUESTED_NEW_FILE_EXISTS ;
         CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */
      if( errno != ENOENT ) {
         *error_return = FILE_OPEN_ERROR ;
         CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */
   } /* end if */

		/** Compose the file header **/
   ADFI_fill_initial_file_header( format_to_use, os_to_use,
				  ADF_D_identification,
				  &file_header, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

		/** Open the new file **/
   ADFI_open_file( filename, status, &file_index, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

		/** write out the file header **/
   ADFI_write_file_header( file_index, &file_header, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

		/** Compose Initial root-node header **/
   ADFI_fill_initial_node_header( &node_header, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

   strncpy( node_header.name,  ROOT_NODE_NAME, strlen( ROOT_NODE_NAME )) ;
   strncpy( node_header.label, ROOT_NODE_LABEL, strlen( ROOT_NODE_LABEL ) ) ;

		/** Write out the root-node header **/
   ADFI_write_node_header( file_index, &file_header.root_node,
			&node_header, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

		/** Compose Initial Free-Chunk Table **/
   ADFI_fill_initial_free_chunk_table( &free_chunk_table, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

		/** Write out Free-Chunk Table **/
   ADFI_write_free_chunk_table( file_index, &free_chunk_table, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
} /* end else if */

else {
   *error_return = ADF_FILE_STATUS_NOT_RECOGNIZED ;
   CHECK_ADF_ABORT( *error_return ) ;
} /* end else */

	/** Read the header of the ADF file **/
if( file_header.tag0[0] == '\0' ) {
   ADFI_read_file_header( file_index, &file_header, error_return ) ;
   if ( *error_return != NO_ERROR ) goto Open_Error ;

    /** Check Database version numbers for compatibility **/
   if( file_header.what[25] != ADF_D_identification[25] )  {
/* Look at major revision letter: version in file must equal what
   this library would write unless there is a policy decision to
   support both versions. */

      *error_return = INVALID_VERSION ;
      if ( *error_return != NO_ERROR ) goto Open_Error ;
   } /* end if */

   if( file_header.what[28] == '>' )
   {
/* we have an old file created before this version numbering scheme
   was instituted - probably will not work */
     *error_return = INVALID_VERSION ;
     if ( *error_return != NO_ERROR ) goto Open_Error ;
   }
   else  /* check version number for file format compatibility */
   {
/* Look at minor revision number: version in file must be less than
   or equal to what this library would write. */

      ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &file_header.what[26],
            &file_minor_version, error_return) ;
      if ( *error_return != NO_ERROR ) goto Open_Error ;

      ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &ADF_D_identification[26],
            &lib_minor_version,  error_return) ;
      if ( *error_return != NO_ERROR ) goto Open_Error ;
      if( file_minor_version > lib_minor_version ) {
         *error_return = INVALID_VERSION ;
         if ( *error_return != NO_ERROR ) goto Open_Error ;
      } /* end if */

      if( file_minor_version < lib_minor_version ) {
           /** If a new feature is added which requires that the file version
               be changed then it is done here. Care must be take not to
               break forward compatibility by changing the file version. Thus
               new features may not be available for older file versions.
               For instance version A1 files cannot be upgraded to version
               A2 and above since a change was made to how links were store
               and the file version is used to decide how to treat links. **/
	 if (  ADF_D_identification[25] == 'A' && file_minor_version > 1 ) {
            ADFI_remember_version_update( file_index, ADF_D_identification,
                                          error_return ) ;

            if ( *error_return != NO_ERROR ) goto Open_Error ;
	 } /* end if */

	    /** The link separator was changed from " " to ">" in order
		to support blanks in filenames under Windows. This change
		is for version A02 and higher **/
	 if (  ADF_D_identification[25] == 'A' && file_minor_version < 2 ) {
	    ADF_file[file_index].link_separator = ' ' ;
	 } /* end if */
      } /* end if */
   } /* end if */
} /* end if */

	/** get the root ID for the user **/
ADFI_file_block_offset_2_ID( file_index, file_header.root_node.block,
		file_header.root_node.offset, Root_ID, error_return ) ;
if ( *error_return != NO_ERROR ) goto Open_Error ;

	/** Remember the file's data format **/
ADFI_remember_file_format( file_index, file_header.numeric_format,
			   file_header.os_size, error_return ) ;
if ( *error_return != NO_ERROR ) goto Open_Error ;

        /** check machine modes, if machine is native the file must be !! **/
ADFI_file_and_machine_compare( file_index, NULL, &formats_compare,
			       error_return ) ;
if ( *error_return != NO_ERROR ) goto Open_Error ;

return ;

Open_Error:
	/** Close the ADF file and free its index **/
ADFI_close_file( file_index, &error_dummy ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Database_Open */
/* end of file ADF_Database_Open.c */
/***********************************************************************
ADF Database Valid:

Checks if a file is a valid ADF file. If status if given, then
check if the file can be opened in that mode.

ADF_Database_Valid( filename, status, error_return)
input:  const char *filename
	Filename must be a legal name and may include a relative or
	absolute path.  It must be directly usable by the C fopen()
	system routine.

output:  int *error_return	Error return.

   Possible errors:
NO_ERROR
NULL_STRING_POINTER
FILE_OPEN_ERROR
ADF_FILE_FORMAT_NOT_RECOGNIZED
***********************************************************************/
void	ADF_Database_Valid(
		const char *filename,
		int *error_return )
{
    FILE *fp;
    char header[33];

    if (NULL == filename || 0 == *filename) {
        *error_return = NULL_STRING_POINTER;
        return;
    }

    if (access(filename, F_OK)) {
        *error_return = REQUESTED_OLD_FILE_NOT_FOUND;
        return;
    }
    if ((fp = fopen(filename, "r+b")) == NULL) {
        if (errno == EMFILE)
            *error_return = TOO_MANY_ADF_FILES_OPENED;
        else
            *error_return = FILE_OPEN_ERROR;
        return;
    }
    fread (header, sizeof(char), 32, fp);
    fclose (fp);
    header[32] = 0;
    if (strncmp (&header[4], "ADF Database Version", 20))
        *error_return = ADF_FILE_FORMAT_NOT_RECOGNIZED;
    else
        *error_return = NO_ERROR;
}
/* file ADF_Database_Set_Format.c */
/***********************************************************************
ADF Database Set Format:

Set the data format used in an existing database.
	Note:  Use with extreme caution.  Needed only
	for data conversion utilities and NOT intended
	for the general user!!!

ADF_Database_Set_Format( Root_ID, format, error_return )
input:  const double Root_ID	The root_ID if the ADF file.
input:  const char *format	See format for ADFDOPN.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Database_Set_Format(
		const double Root_ID,
		const char *format,
		int *error_return )
{
unsigned int 		file_index ;
struct DISK_POINTER	block_offset ;
struct	FILE_HEADER	file_header ;
char			machine_format, format_to_use, os_to_use ;

ADFI_check_string_length( format, ADF_FORMAT_LENGTH, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get the file, block, and offset numbers from the ID **/
ADFI_ID_2_file_block_offset( Root_ID, &file_index, &block_offset.block,
		&block_offset.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get node_header for the node **/
ADFI_read_file_header( file_index, &file_header, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

ADFI_figure_machine_format( format, &machine_format, &format_to_use,
			    &os_to_use, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

file_header.numeric_format = format_to_use ;
file_header.os_size = os_to_use ;

   /** Get modification date to be updated with the header **/
ADFI_get_current_date ( file_header.modification_date );

   /** Now write the disk header out... **/
ADFI_write_file_header( file_index, &file_header, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

ADFI_remember_file_format( file_index, format_to_use, os_to_use,
			   error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Database_Set_Format */
/* end of file ADF_Database_Set_Format.c */
/* file ADF_Database_Version.c */
/***********************************************************************
ADF Database Version:

Get ADF File Version ID.  This is the version number of the ADF library
routines which created an ADF database.  Modified ADF databases
will take on the version ID of the current ADF library version if
it is higher than the version indicated in the file.
	The format of the version ID is:  "ADF Database Version 000.01"

ADF_Database_Version( Root_ID, version, creation_date, modification_date,
	error_return )
input:  const double Root_ID	The ID of the root node in the ADF file.
output: char *version		A 32-byte character string containing the
				version ID.
output: char *creation_date	A 32-byte character string containing the
				creation date of the file.
output: char *modification_date	A 32-byte character string containing the
				last modification date of the file.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Database_Version(
		const double Root_ID,
		char *version,
		char *creation_date,
		char *modification_date,
		int *error_return )
{
unsigned int 		file_index ;
struct DISK_POINTER	block_offset ;
struct	FILE_HEADER	file_header ;

if( (version == NULL) || (creation_date == NULL) ||
    (modification_date == NULL) ) {
   *error_return = NULL_STRING_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** Get the file, block, and offset numbers from the ID **/
ADFI_ID_2_file_block_offset( Root_ID, &file_index, &block_offset.block,
		&block_offset.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get node_header for the node **/
ADFI_read_file_header( file_index, &file_header, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

*error_return = NO_ERROR ;
	/** Convert the "what" string into a C string **/
ADFI_string_2_C_string( &file_header.what[4], strcspn ( file_header.what, ">" ) - 4,
                        version, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Convert the creation date string into a C string **/
ADFI_string_2_C_string( file_header.creation_date, 28,
	creation_date, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Convert the modification date string into a C string **/
ADFI_string_2_C_string( file_header.modification_date, 28,
	modification_date, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Database_Version */
/* end of file ADF_Database_Version.c */
/* file ADF_Delete.c */
/***********************************************************************
ADF Delete:

Delete a Node.   If the node is NOT a link, then the specified node and all
sub-nodes anywhere under it are also deleted.  For a link, and also
for links farther down in the tree, the link-node will be deleted,
but the node which the link is linked to is not affected.  When a
node is deleted, other link-nodes which point to it are left
dangling.  For example, if N13 is deleted, then L1 and L2 point to a
non-existing node.  This is OK until L1 and L2 are used.

ADF_Delete( PID, ID, error_return )
input:  const double PID	The ID of the node's parent.
input:  const double ID		The ID of the node to use.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Delete(
		const double PID,
		const double ID,
		int *error_return )
{
int                     num_ids , i, link_path_length ;
double                  *ids ;
unsigned int            file_index ;
struct DISK_POINTER     parent ;
struct DISK_POINTER     child ;
struct NODE_HEADER      node_header ;

    /** Don't use ADFI_chase_link() - delete link nodes but NOT the
        nodes they are linked too **/

ADFI_ID_2_file_block_offset( ID, &file_index, &child.block, &child.offset,
                             error_return ) ;

CHECK_ADF_ABORT( *error_return ) ;

ADF_Is_Link( ID, &link_path_length, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

ADFI_read_node_header( file_index, &child, &node_header, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

    /** Delete node data **/

if( link_path_length > 0 ) { /** this node IS a link **/
    /** Delete the link path data for this node **/
   ADFI_delete_data( file_index, &node_header, error_return ) ;
   }
else {  /** this node is NOT a link **/

    /** Recursively delete all sub-nodes (children) of this node **/
   ADFI_get_direct_children_ids( file_index, &child, &num_ids, &ids,
                                 error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

   for( i=0; i<num_ids; i++ ) {
      ADF_Delete( ID, ids[i], error_return ) ;  /* resursion */
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end for */

   if( num_ids > 0 ) {
      free( ids ) ;
      } /* end if */

    /** Delete all data for this node **/

   ADF_Put_Dimension_Information( ID, "MT", 0, (int *)0, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if-else */


    /** Disassociate node from parent **/
ADFI_ID_2_file_block_offset( PID, &file_index,
                             &parent.block, &parent.offset, error_return ) ;
    /* file_index should be same as before since parent and child
       should be in the same file */
CHECK_ADF_ABORT( *error_return ) ;

ADFI_delete_from_sub_node_table( file_index, &parent, &child, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

    /** Delete this node's sub node table **/
if( node_header.entries_for_sub_nodes > 0 ) {
   ADFI_delete_sub_node_table( file_index, &node_header.sub_node_table,
                               node_header.entries_for_sub_nodes, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

    /** Delete node header from disk **/
ADFI_file_free( file_index, &child, 0, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Finally, update modification date **/
ADFI_write_modification_date( file_index, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Delete */
/* end of file ADF_Delete.c */
/* file ADF_Error_Message.c */
/***********************************************************************
ADF Error message:

Return Error Message.  Given an error_return from an ADF routine,
get a textual description of the error.

ADF_Error_Message( error_return, error_string )
input:  const int error_return	An ADF-generated error code.
output: char *error_string	An 80-byte description of the specified error.
			If the number is NULL, then print out error message.
***********************************************************************/
void	ADF_Error_Message(
		const int error_return_input,
		char *error_string )
{
char  err_msg_str[ADF_MAX_ERROR_STR_LENGTH+1] ;

	/** If return pointer is NULL, print message to stdout **/
if( error_string == NULL ) {
   ADF_Error_Message( error_return_input, err_msg_str ) ;
   fprintf(stderr,"%s\n", err_msg_str ) ;
   return ;
   } /* end if */

	/** NO_ERROR is NOT zero for pointer-assignment checking **/
if( error_return_input == NO_ERROR ) {
   strcpy( error_string, ADF_error_string[ 0 ] ) ;
   } /* end if */
	/** Check range of error code **/
else if( (error_return_input <= 0) ||
	 (error_return_input >= sizeof( ADF_error_string )/sizeof(char *) - 1 ) ) {
   sprintf( error_string, "ADF: Unrecognized error number %d.",
                error_return_input ) ;
   } /* end else if */
	/** Error-code good, copy it for the user **/
else if (ADF_sys_err &&
        (FILE_OPEN_ERROR == error_return_input ||
         FILE_CLOSE_ERROR == error_return_input ||
         FSEEK_ERROR == error_return_input ||
         FREAD_ERROR == error_return_input ||
         FWRITE_ERROR == error_return_input ||
         FFLUSH_ERROR == error_return_input)) {
   char *p;
   strncpy (err_msg_str, strerror(ADF_sys_err), ADF_MAX_ERROR_STR_LENGTH-8);
   err_msg_str[ADF_MAX_ERROR_STR_LENGTH-8] = 0;
   p = err_msg_str + strlen(err_msg_str) - 1;
   if (*p == '\n') *p = 0;
   sprintf (error_string, "ADF %d: %s", error_return_input, err_msg_str);
}
else {
   strcpy( error_string, ADF_error_string[error_return_input] ) ;
   } /* end else */
} /* end of ADF_Error_Message */
/* end of file ADF_Error_Message.c */
/* file ADF_Flush_to_Disk.c */
/***********************************************************************
ADF Flush to Disk:

Flush data to disk.  This routine is used force any modified information
to be flushed to the physical disk.  This ensures that data will not
be lost if a program aborts.  This control of when to flush all data
to disk is provided to the user rather than to flush the data every
time it is modified, which would result in reduced performance.

ADF_Flush_to_Disk( ID, error_return )
input:  const double ID		The ID of a node in the ADF file to flush.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Flush_to_Disk(
		const double ID,
		int *error_return )
{
double               LID ;
unsigned int         file_index ;
struct DISK_POINTER  block_offset ;
struct NODE_HEADER   node ;

   ADFI_chase_link( ID, &LID, &file_index, &block_offset, &node,
                    error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

   ADFI_fflush_file( file_index, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
} /* end of ADF_Flush_to_Disk */
/* end of file ADF_Flush_to_Disk.c */
/* file ADF_Get_Data_Type.c */
/***********************************************************************
ADF Get Data Type:

Get Data Type.  Return the 32 character string in a node's data-type field.
In C, the name will be null terminated after the last non-blank character.
A maximum of 33 characters may be used (32 for the name plus 1 for the null).

ADF_Get_Data_Type( ID, data_type, error_return )
input:  const double ID		The ID of the node to use.
output: char *data_type		The 32-character data-type of the node.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Get_Data_Type(
		const double ID,
		char *data_type,
		int *error_return )
{
unsigned int 			file_index ;
struct DISK_POINTER		block_offset ;
struct NODE_HEADER		node ;
double				LID ;

if( data_type == NULL ) {
   *error_return = NULL_STRING_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Copy the blank-filled data-type into a C string **/
ADFI_string_2_C_string( node.data_type, ADF_DATA_TYPE_LENGTH, data_type,
		error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Get_Data_Type */
/* end of file ADF_Get_Data_Type.c */
/* file ADF_Get_Dimension_Values.c */
/***********************************************************************
ADF Get Dimension Values:

Get Dimension Values.  Return the dimension values for a node.  Values
will be in the range of 1 to 100,000.  Values will only be returned
for the number of dimensions defined in the node.  If the number
of dimensions for the node is zero, an error is returned.

ADF_Get_Dimension_Values( ID, dim_vals, error_return )
input:  const double ID		The ID of the node to use.
output: int dim_vals[]		Array for returned dimension values.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Get_Dimension_Values(
		const double ID,
		int dim_vals[],
		int *error_return )
{
unsigned int 			file_index ;
struct DISK_POINTER		block_offset ;
struct NODE_HEADER		node ;
int				i ;
double				LID ;

if( dim_vals == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Check for zero dimensions **/
if( node.number_of_dimensions == 0 ) {
   *error_return = ZERO_DIMENSIONS ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** Check for too-large-of dimensions **/
if( node.number_of_dimensions > ADF_MAX_DIMENSIONS ) {
   *error_return = BAD_NUMBER_OF_DIMENSIONS ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** Copy the dimension information **/
for( i=0; i<(int)node.number_of_dimensions; i++ )
   dim_vals[i] = node.dimension_values[i] ;

} /* end of ADF_Get_Dimension_Values */
/* end of file ADF_Get_Dimension_Values.c */
/* file ADF_Get_Error_State.c */
/***********************************************************************
ADF Get Error State:

Get Error State.  Return the current error state.

ADF_Get_Error_State( error_state, error_return )
output: int *error_state	Flag for ABORT on error (1) or return error
				status (0).
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Get_Error_State(
		int *error_state,
		int *error_return )
{
if( error_state == 0L ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

if( ADF_abort_on_error == TRUE )
   *error_state = 1 ;
else
   *error_state = 0 ;

} /* end of ADF_Get_Error_State */
/* end of file ADF_Get_Error_State.c */
/* file ADF_Get_Label.c */
/***********************************************************************
ADF Get Label:

Return the 32 character string in a node's label field.

ADF_Get_Label( ID, label, error_return )
input:  const double ID		The ID of the node to use.
output: char *label		The 32-character label of the node.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Get_Label(
		const double ID,
		char *label,
		int *error_return )
{
unsigned int 			file_index ;
struct DISK_POINTER		block_offset ;
struct NODE_HEADER		node ;
double				LID ;

if( label == NULL ) {
   *error_return = NULL_STRING_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Copy the blank-filled label type into a C string **/
ADFI_string_2_C_string( node.label, ADF_LABEL_LENGTH, label,
		error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Get_Label */
/* end of file ADF_Get_Label.c */
/* file ADF_Get_Link_Path.c */
/***********************************************************************
ADF Get Link path:

Get path information from a link.  If the node is a link-node, return
the path information.  Else, return an error.  If the link is in the same
file, then the filename returned is zero length.

ADF_Get_Link_Path( ID, file, name_in_file, error_return )
input:  const double ID		The ID of the node to use.
output: char *file	        The returned filename
output: char *name_in_file	The returned name of node.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Get_Link_Path(
		const double ID,
		char *file,
		char *name_in_file,
		int *error_return )
{
unsigned int		file_index ;
int                     file_bytes, machine_bytes, total_bytes ;
char                    file_format, machine_format ;
struct DISK_POINTER     block_offset ;
struct NODE_HEADER      node_header ;
struct TOKENIZED_DATA_TYPE tokenized_data_type[ 2 ] ;
char   link_data[ADF_FILENAME_LENGTH + ADF_MAX_LINK_DATA_SIZE + 1 + 1] ;
size_t                     lenfilename ;
char *separator;

if( file == NULL ) {
   *error_return = NULL_STRING_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

if( name_in_file == NULL ) {
   *error_return = NULL_STRING_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */
        /** Get the file, block, and offset numbers from the ID **/
ADFI_ID_2_file_block_offset( ID, &file_index, &block_offset.block,
                &block_offset.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

        /** Get node_header for the node **/
ADFI_read_node_header( file_index, &block_offset, &node_header, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( (node_header.data_type[0] != 'L') || (node_header.data_type[1] != 'K')) {
   *error_return = NODE_IS_NOT_A_LINK ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

        /** Get tokenized datatype **/
ADFI_evaluate_datatype( file_index, node_header.data_type,
        &file_bytes, &machine_bytes, tokenized_data_type,
        &file_format, &machine_format, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

total_bytes = file_bytes * node_header.dimension_values[0] ;
ADFI_read_data_chunk( file_index, &node_header.data_chunks,
                      tokenized_data_type, file_bytes, total_bytes,
	              0, total_bytes, link_data, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

                /* NULL terminate the string */
link_data[ node_header.dimension_values[0] ] = '\0' ;

file[0] = '\0' ;
name_in_file[0] = '\0' ;

     /** look for file/link delimiter **/
separator = strchr (link_data, ADF_file[file_index].link_separator);
if (separator == NULL) {
   lenfilename = 0;
} else {
   lenfilename = (size_t)(separator - link_data);
}

if ( lenfilename == 0 )  /** no filename **/
{
   strcpy( name_in_file, &link_data[1] );
}
else if ( lenfilename > 0 && lenfilename == strlen( link_data ) )
{
   strcpy( file, link_data) ;  /** no link ? **/
}
else
{
   strncpy( file, link_data, lenfilename) ;
   file[lenfilename] = '\0';
   strcpy( name_in_file, &link_data[lenfilename+1] );
} /* end if */

} /* end of ADF_Get_Link_Path */
/* end of file ADF_Get_Link_Path.c */
/* file ADF_Get_Name.c */
/***********************************************************************
ADF get name:

Get Name of a Node.  Given a node's ID, return the 32 character name of
that node.

ADF_Get_Name( ID, name, error_return )
input:  const double ID		The ID of the node to use.
output: char *name		The simple name of the node (no path info).
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Get_Name(
		const double ID,
		char *name,
		int *error_return )
{
unsigned int 		file_index ;
struct DISK_POINTER	block_offset ;
struct NODE_HEADER	node ;

if( name == NULL ) {
   *error_return = NULL_STRING_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

	/** Get the file, block, and offset numbers from the ID **/
ADFI_ID_2_file_block_offset( ID, &file_index, &block_offset.block,
		&block_offset.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get node_header for the node **/
ADFI_read_node_header( file_index, &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Copy the blank-filled name into a C string **/
ADFI_string_2_C_string( node.name, ADF_NAME_LENGTH, name,
		error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Get_Name */
/* end of file ADF_Get_Name.c */
/* file ADF_Get_Node_ID.c */
/***********************************************************************
ADF get Node ID:

Get Unique-Identifier of a Node.  Given a parent node ID and a name of
a child node, this routine returns the ID of the child.  If the child
node is a link, the ID of the link node is returned (not the ID of the
linked-to node) - otherwise there would be no way to obtain the ID
of a link node.

The child name may be a simple name or a compound path name.
If the name is a compound path name and it begins with a '/',
then the parent node ID may be any valid ID in the same database
as the first node in the path.  If the name is only "/" and the
parent ID is any valid ID in the database, the root ID is returned.
If the name is a compound path name and does not begin with a '/',
then the parent node ID is the ID of the parent of the first node
in the path.  If the path name contains a link node (except for
the ending leaf node), then the link is followed.


ADF_Get_Node_ID( PID, name, ID, error_return )
input:  const double PID    The ID of name's parent.
input:  const char *name    The name of the node.  Compound
    names including path information use a slash "/" notation between
    node names.  If a leading slash is used, then PID can be any
    valid node ID in the ADF database of the first name in the path.

output: double *ID          The ID of the named node.
output: int *error_return   Error return.

   Possible errors:
NO_ERROR
NULL_STRING_POINTER
NULL_POINTER
***********************************************************************/
void    ADF_Get_Node_ID(
        const double PID,
        const char *name,
        double *ID,
        int *error_return )
{
double                  LID ;
int                     found ;
int                     name_length ;
unsigned int            file_index ;
struct DISK_POINTER     parent_block_offset, sub_node_entry_location ;
struct SUB_NODE_TABLE_ENTRY sub_node_entry ;
struct NODE_HEADER      node_header ;
char                    *name_tmp, *name_ptr, *name_pos ;

if( name == NULL ) {
   *error_return = NULL_STRING_POINTER ;
   return ;
   } /* end if */

name_length = strlen( name ) ;
if( name_length == 0 ) {
   *error_return = STRING_LENGTH_ZERO ;
   return ;
   } /* end if */

if( ID == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

*ID = PID ;  /** initialize the ID variable to use in intermediate steps **/

if( name[0] == '/' ) { /** start at the root node **/
    /** according to user documentation, PID can be any valid node
        in the database, but we need to use it to get the root ID
        in order to start at the top **/

   ADF_Get_Root_ID( PID, ID, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

    /** This is the root-node, return the Root-ID **/
   if( name[ 1 ] == '\0' ) {
      return ;    /** NOT an error, just done and need to get out **/
      } /* end if */
   } /* end if */

name_tmp = (char *) malloc( (name_length + 1) * sizeof( char ) ) ;
if( name_tmp == NULL ) {
   *error_return = MEMORY_ALLOCATION_FAILED ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

strcpy( name_tmp, name ) ;

    /** start search for tokens (names separated by '/') **/
name_pos = name_tmp ;
name_ptr = ADFI_strtok( name_tmp, &name_pos, "/" ) ;
if( name_ptr == NULL ) {   /** this should never happen but check anyway **/
   *error_return = INVALID_NODE_NAME ;
   CHECK_ADF_ABORT1( *error_return ) ;
   } /* end if */

    /** Get file-index, etc. to start.  Note: Parent ID may be a link **/
ADFI_chase_link( *ID, &LID, &file_index,
                 &parent_block_offset, &node_header, error_return ) ;
CHECK_ADF_ABORT1( *error_return ) ;
*ID = LID ;

    /** Track through the possible compound name string **/
while( name_ptr ) {

    /** Find this child under the current parent **/
   ADFI_check_4_child_name( file_index, &parent_block_offset, name_ptr, &found,
        &sub_node_entry_location, &sub_node_entry, error_return ) ;
   CHECK_ADF_ABORT1( *error_return ) ;

   if( found == 0 ) { /** Child NOT found **/
      *error_return = CHILD_NOT_OF_GIVEN_PARENT ;
      CHECK_ADF_ABORT1( *error_return ) ;
      } /* end if */

    /** create the child ID **/
   ADFI_file_block_offset_2_ID( file_index,
          sub_node_entry.child_location.block,
          sub_node_entry.child_location.offset, ID, error_return ) ;

    /** Get the next node-name token (NULL if no more). This is needed
        for the while-loop check and normally would be done at the
        end of the loop, but it is useful in the next step to see if
        there are any more branches in the path.  **/
   name_ptr = ADFI_strtok( name_tmp, &name_pos, "/" ) ;

    /** If this node is the last in the path it may be a link, but
        there needs to be a mechanism by which a link's ID can
        be determined and so we cannot follow the link at this time. **/
   if( name_ptr != NULL ) {
      /* Make sure we have a real ID so we can continue the search */
      ADFI_chase_link( *ID, &LID, &file_index, &parent_block_offset,
                       &node_header, error_return ) ;
      CHECK_ADF_ABORT1( *error_return ) ;
      *ID = LID ;

    /** This child now becomes the parent.  Do it again... **/
      ADFI_ID_2_file_block_offset( *ID, &file_index,
                                    &parent_block_offset.block,
                                    &parent_block_offset.offset,
                                    error_return ) ;
      CHECK_ADF_ABORT1( *error_return ) ;
      } /* end if */

   } /* end while */

free( name_tmp ) ;

} /* end of ADF_Get_Node_ID */
/* end of file ADF_Get_Node_ID.c */
/* file ADF_Get_Number_of_Dimensions.c */
/***********************************************************************
ADF Get Number of Dimensions:

Get Number of Dimensions.  Return the number of data dimensions
used in a node.  Valid values are from 0 to 12.

ADF_Get_Number_of_Dimensions( ID, num_dims, error_return)
input:  const double ID		The ID of the node to use.
output: int *num_dims		The returned number of dimensions.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Get_Number_of_Dimensions(
		const double ID,
		int *num_dims,
		int *error_return )
{
unsigned int 			file_index ;
struct DISK_POINTER		block_offset ;
struct NODE_HEADER		node ;
double				LID ;

if( num_dims == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset,
		&node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Return the number of dimensions **/
*num_dims = node.number_of_dimensions ;

} /* end of ADF_Get_Number_of_Dimensions */
/* end of file ADF_Get_Number_of_Dimensions.c */
/* file ADF_Get_Root_ID.c */
/***********************************************************************
ADF_Get_Root_ID:
	Get root-ID for an ADF system from any ID in the system.

input:  const double ID		The ID of the node to use.
output: *double Root_ID		The returned ID of the root node.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Get_Root_ID(
		const double ID,
		double *Root_ID,
		int *error_return )
{
unsigned int		file_index ;
struct DISK_POINTER	block_offset ;
struct	FILE_HEADER	file_header ;

if( Root_ID == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

	/** Get the file ID **/
ADFI_ID_2_file_block_offset( ID, &file_index, &block_offset.block,
		&block_offset.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Use the file header to find the root ID **/
ADFI_read_file_header( file_index, &file_header, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Format the root ID **/
ADFI_file_block_offset_2_ID( file_index, file_header.root_node.block,
	file_header.root_node.offset, Root_ID, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Get_Root_ID */
/* end of file ADF_Get_Root_ID.c */
/* file ADF_Is_Link.c */
/***********************************************************************
ADF Is Link:

Test if a Node is a link.  If the actual data-type of the node is "LK"
(created with ADF_Link), return the link path length.  Otherwise,
return 0.

ADF_Is_Link( ID, link_path_length, error_return )
input:  const double ID		The ID of the node to use.
output: int *link_path_length	0 if the node is NOT a link.  If the
	node is a link, the length of the path string is returned.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Is_Link(
		const double ID,
		int *link_path_length,
		int *error_return )
{
unsigned int		file_index ;
struct DISK_POINTER	block_offset ;
struct NODE_HEADER	node_header ;

if( link_path_length == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

        /** Get the file, block, and offset numbers from the ID **/
ADFI_ID_2_file_block_offset( ID, &file_index, &block_offset.block,
                &block_offset.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

        /** Get node_header for the node **/
ADFI_read_node_header( file_index, &block_offset, &node_header, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( (node_header.data_type[0] == 'L') && (node_header.data_type[1] == 'K'))
   *link_path_length = node_header.dimension_values[0] ;
else
   *link_path_length =  0 ;

} /* end of ADF_Is_Link */
/* end of file ADF_Is_Link.c */
/* file ADF_Library_Version.c */
/***********************************************************************
ADF Library Version:

Get ADF Library Version ID.  This is the version number of the ADF
library routines which your program is currently using.
	The format of the version ID is:  "ADF Library  Version 000.01"

ADF_Library_Version( version, error_return )
output:  char *version		A 32-byte character string containing
	the ADF Library version ID information.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Library_Version(
		char *version,
		int *error_return )
{

int   lversion;

if( version == NULL ) {
   *error_return = NULL_STRING_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

	/** Copy the proper portion of the "what" string **/
strcpy ( version, &ADF_L_identification[4] ) ;
lversion = strlen ( version ) ;
version[lversion-1] = '\0' ; /** remove trailing "what" delimiter ('>') **/
} /* end of ADF_Library_Version */
/* end of file ADF_Library_Version.c */
/* file ADF_Link.c */
/***********************************************************************
ADF Link:

Create a link.  Note:  The Node linked to does not have to exist when the
link is created (but it may exist and that is OK).  However, when
the link is used, an error will occur if the linked to node does not
exist.

ADF_Link( PID, name, file, name_in_file, ID, error_return )
input:  const double PID	The ID of the Node's parent.
input:  const char *name	The name of the link node.
input:  const char *file	The filename to use for the link (directly
	usable by a C open() routine).  If blank (null),
	the link will be within the same file.

input:  const char *name_in_file The name of the node which
	the link will point to.  This can be a simple or compound name.

output: double ID		The returned ID of the link-node.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Link(
		const double PID,
		const char *name,
		const char *file_name,
		const char *name_in_file,
		double *ID,
		int *error_return )
{
char	                link_data[ADF_FILENAME_LENGTH +
				  ADF_MAX_LINK_DATA_SIZE + 2] ;
int			null_filename = FALSE ;
int			filename_length, linked_to_length, data_length ;
int			dim_vals[1] ;
unsigned int            file_index ;
struct DISK_POINTER     block_offset ;
struct NODE_HEADER      node_header ;

	/** Don't check file since it can be a NULL pointer **/

ADFI_check_string_length( name, ADF_NAME_LENGTH, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

ADFI_check_string_length( name_in_file, ADF_MAX_LINK_DATA_SIZE, error_return );
CHECK_ADF_ABORT( *error_return ) ;

ADF_Is_Link( PID, &linked_to_length, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;
if (  linked_to_length > 0 ) {
   *error_return = LINKS_TOO_DEEP ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** Create the node in the normal way **/
ADF_Create( PID, name, ID, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

        /** Get the file, block, and offset numbers from the ID **/
ADFI_ID_2_file_block_offset( *ID, &file_index, &block_offset.block,
                &block_offset.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Add the file and linked-to name as data in the child **/
ADFI_check_string_length( file_name, ADF_FILENAME_LENGTH, error_return ) ;
if( *error_return != NO_ERROR ) {
   null_filename = TRUE ;
   filename_length = 0 ;
   } /* end if */
else {
   filename_length = strlen( file_name) ;
   } /* end else */
linked_to_length = strlen( name_in_file ) ;

data_length = filename_length + linked_to_length + 1 ;
if( data_length > ADF_FILENAME_LENGTH + ADF_MAX_LINK_DATA_SIZE + 1 ) {
   *error_return = STRING_LENGTH_TOO_BIG ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

if( null_filename == TRUE ) {
   sprintf( link_data, "%c%s", ADF_file[file_index].link_separator,
            name_in_file ) ;
   } /* end if */
else {
   sprintf( link_data, "%s%c%s", file_name,
            ADF_file[file_index].link_separator, name_in_file ) ;
   } /* end else */

	/** We must use a datatype of "C1" to put the data into this node.
	    With a datatype of "Lk" (a link), the written data will go
	    into the linked-to node (that's the whole point).  To set
	    this up we must be careful...
	**/
dim_vals[0] = data_length ;
ADF_Put_Dimension_Information( *ID, "C1", 1, dim_vals, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

ADF_Write_All_Data( *ID, link_data, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Change the datatype to be LK, without deleting the data.
	    We can't use ADF_Put_Dimension_Information since the change
	    of datatype will delete the data.  We must do this manually.
	**/

ADFI_read_node_header( file_index, &block_offset, &node_header, error_return );
CHECK_ADF_ABORT( *error_return ) ;

if( (node_header.data_type[0] != 'C') || (node_header.data_type[1] != '1') ||
    (node_header.data_type[2] != ' ') ) {
   *error_return = INVALID_DATA_TYPE ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

node_header.data_type[0] = 'L' ;
node_header.data_type[1] = 'K' ;
ADFI_write_node_header( file_index, &block_offset, &node_header, error_return );
CHECK_ADF_ABORT( *error_return ) ;

	/** Finally, update modification date **/
ADFI_write_modification_date( file_index, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Link */
/* end of file ADF_Link.c */
/* file ADF_Move_Child.c */
/***********************************************************************
ADF Move Child:

Change Parent (move a Child Node).  The node and the 2 parents must
all exist within a single ADF file.  If the node is pointed to by a
link-node, changing the node's parent will break the link.

ADF_Move_Child( PID, ID, NPID, error_return )
input:  double PID		The ID of the Node's parent.
input:  double ID		The ID of the node to use.
input:  double NPID		The ID of the Node's New Parent
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Move_Child(
		const double PID,
		const double ID,
		const double NPID,
		int *error_return )
{

unsigned int        parent_file_index, child_file_index,
                    new_parent_file_index, file_index ;
char     child_name[ ADF_NAME_LENGTH ] ;
int      found ;
struct DISK_POINTER parent, child, new_parent, sub_node_entry_location ;
struct SUB_NODE_TABLE_ENTRY  sub_node_entry ;

*error_return = NO_ERROR ;

ADFI_ID_2_file_block_offset( PID, &parent_file_index, &parent.block,
                             &parent.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

ADFI_ID_2_file_block_offset( ID, &child_file_index, &child.block,
                             &child.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( child_file_index != parent_file_index ) {
   *error_return = NODES_NOT_IN_SAME_FILE ;
   CHECK_ADF_ABORT( *error_return ) ;
   }

ADFI_ID_2_file_block_offset( NPID, &new_parent_file_index, &new_parent.block,
                             &new_parent.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( new_parent_file_index != parent_file_index ) {
   *error_return = NODES_NOT_IN_SAME_FILE ;
   CHECK_ADF_ABORT( *error_return ) ;
   }

file_index = parent_file_index ;  /* use a shorter, more generic  name -
                                     file indices should now be the same
                                     for all 3 nodes */

    /** check that child is really a child of parent **/
ADF_Get_Name( ID, child_name, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

ADFI_check_4_child_name( file_index, &parent, child_name, &found,
                         &sub_node_entry_location, &sub_node_entry, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( found == 0 ) { /* child not found */
   *error_return = CHILD_NOT_OF_GIVEN_PARENT ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

    /** add child to its new parent's sub node table  **/
ADFI_add_2_sub_node_table( file_index, &new_parent, &child, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

    /** remove child from its old parent's sub node table  **/
ADFI_delete_from_sub_node_table( file_index, &parent, &child, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Move_Child */
/* end of file ADF_Move_Child.c */
/* file ADF_Number_of_Children.c */
/***********************************************************************
ADF Number of Children;

Get Number of Children of a Node.  Return the number of children
nodes directly associated with a parent node.

ADF_Number_of_Children( ID, num_children, error_return )
input:  const double ID		The ID of the node to use.
output: int *num_children	The number of children directly
				associated with this node.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Number_of_Children(
		const double ID,
		int *num_children,
		int *error_return )
{
unsigned int 			file_index ;
struct DISK_POINTER		block_offset ;
struct NODE_HEADER		node ;
double				LID ;

if( num_children == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Return the number of children **/
*num_children = node.num_sub_nodes ;
} /* end of ADF_Number_of_Children */
/* end of file ADF_Number_of_Children.c */
/* file ADF_Put_Dimension_Information.c */
/***********************************************************************
ADF Put Dimension Information:

Set/change the data-type and Dimension Information of a Node.  Valid
user-definable data-types are:

No data				MT
Integer 32			I4
Integer 64			I8
Unsigned Int 32			U4
Unsigned Int 64			U8
Real 32				R4
Real 64				R8
Complex 64			X4
Complex 128			X8
Character (unsigned byte)	C1
Byte (unsigned byte)		B1
Compound data-types can be used which combine types
("I4,I4,R8"), define an array ("I4[25]"), or a combination of these
("I4,C1[20],R8[3]").
dims can be a number from 0 to 12.

dim_vals is an array of integers.  The number of integers used is
determined by the dims argument.  If dims is zero, the dim_values
are not used.  Valid range for dim_values are from 1 to 2,147,483,648.
The total data size, calculated by the data-type-size times the
dimension value(s), cannot exceed 2,147,483,648.

Note:  When this routine is called and the data-type or the
number of dimensions changes, any data currently associated
with the node is lost!!   The dimension values can be changed and
the data space will be extended as needed.

ADF_Put_Dimension_Information( ID, data_type, dims, dim_vals, error_return )
input:  const double ID         The ID of the node.
input:  const char *data-type   The data-type to use.
input:  const int dims          The number of dimensions this node has.
input:  const int dim_vals[]    The dimension values for this node.
output: int *error_return       Error return.
***********************************************************************/
void    ADF_Put_Dimension_Information(
        const double ID,
        const char *data_type,
        const int dims,
        const int dim_vals[],
        int *error_return )
{
unsigned int        file_index ;
struct DISK_POINTER	block_offset ;
struct NODE_HEADER node ;
struct TOKENIZED_DATA_TYPE
       tokenized_data_type[ 1 + (ADF_DATA_TYPE_LENGTH + 1)/3 ] ;
char        file_format, machine_format ;
int         file_bytes[2], machine_bytes[2] ;
int         data_bytes, old_data_bytes ;
int         i, datatype_length ;
int         preserve_data = FALSE ;
double      LID ;

ADFI_check_string_length( data_type, ADF_DATA_TYPE_LENGTH, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( dim_vals == NULL && dims > 0 ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Check new datatype **/
ADFI_evaluate_datatype( file_index, data_type,
	&file_bytes[0], &machine_bytes[0],
	tokenized_data_type, &file_format, &machine_format, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Look at old datatype **/
ADFI_evaluate_datatype( file_index, node.data_type,
	&file_bytes[1], &machine_bytes[1],
	tokenized_data_type, &file_format, &machine_format, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Calculate new data-size **/
if( dims < 0 ) {
   *error_return = NUMBER_LESS_THAN_MINIMUM ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */
if( dims > ADF_MAX_DIMENSIONS) {
   *error_return = BAD_NUMBER_OF_DIMENSIONS ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** If the number of dimensions is zero, set data-bytes to zero **/
if( dims == 0 )
   data_bytes = 0 ;
else {	/** Calculate the total number of bytes in the data **/
   for( data_bytes=file_bytes[0], i=0; i<dims; i++ ) {
      if( dim_vals[i] <=0 ) {
	 *error_return = BAD_DIMENSION_VALUE ;
         CHECK_ADF_ABORT( *error_return ) ;
	 } /* end if */
      data_bytes *= dim_vals[i] ;
      } /* end for */
   } /* end else */

	/** Calculate old data-size **/
if( node.number_of_dimensions == 0 )
   old_data_bytes = 0 ;
else {
   for( old_data_bytes=file_bytes[1], i=0;
		i<(int)node.number_of_dimensions; i++ )
      old_data_bytes *= node.dimension_values[i] ;
   } /* end else */


	/** If the data-types are the same... **/
if( ADFI_stridx_c( node.data_type, data_type ) == 0 ) { /* datatypes the same */
   if( dims == (int) node.number_of_dimensions )
      preserve_data = TRUE ;
   } /* end if */
     /** If a different datatype, throw-away the data, record new datatype **/
else {
   datatype_length = strlen( data_type ) ;
	/** Copy the datatype **/
   for( i=0; i<MIN(datatype_length, ADF_DATA_TYPE_LENGTH); i++ ) {
      node.data_type[i] = data_type[i] ;
      } /* end for */
	/** Blank fill the remaining space **/
   for( ; i<ADF_DATA_TYPE_LENGTH; i++ ) {
      node.data_type[i] = ' ' ;
      } /* end for */
   } /* end else */

	/** Record the number of dimensions and the dimension values **/
node.number_of_dimensions = dims ;
for( i=0; i<dims; i++ )
   node.dimension_values[i] = dim_vals[i] ;
for( ; i<ADF_MAX_DIMENSIONS; i++ )  /** Zero out remaining dimension values **/
   node.dimension_values[i] = 0 ;

if( preserve_data != TRUE ) {	/** Free the old data **/
   ADFI_delete_data( file_index, &node, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

   node.number_of_data_chunks = 0 ;
   ADFI_set_blank_disk_pointer( &node.data_chunks ) ;
   } /* end if */

	/** Write modified node_header for the node **/
ADFI_write_node_header( file_index, &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Finally, update modification date **/
ADFI_write_modification_date( file_index, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Put_Dimension_Information */
/* end of file ADF_Put_Dimension_Information.c */
/* file ADF_Put_Name.c */
/***********************************************************************
ADF Put name:

Put (change) Name of a Node.  Warning:  If the node is pointed to by a
link-node, changing the node's name will break the link.

ADF_Put_Name( PID, ID, name, error_return )
input:  const double PID	The ID of the Node's parent.
input:  const double ID		The ID of the node to use.
input:  const char *name	The new name of the node.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Put_Name(
		const double PID,
		const double ID,
		const char *name,
		int *error_return )
{
unsigned int 			file_index ;
struct DISK_POINTER		parent_block_offset, child_block_offset ;
struct DISK_POINTER		sub_node_entry_location ;
struct NODE_HEADER		parent_node, child_node ;
struct SUB_NODE_TABLE_ENTRY	sub_node_entry ;
int				i, name_start, name_length, found ;

ADFI_check_string_length( name, ADF_NAME_LENGTH, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

*error_return = NO_ERROR ;

	/** Get the file, block, and offset numbers from the PID **/
ADFI_ID_2_file_block_offset( PID, &file_index, &parent_block_offset.block,
		&parent_block_offset.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get the file, block, and offset numbers from the ID **/
ADFI_ID_2_file_block_offset( ID, &file_index, &child_block_offset.block,
		&child_block_offset.offset, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get node_header for the node (parent) **/
ADFI_read_node_header( file_index, &parent_block_offset,
		&parent_node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get node_header for the node (child) **/
ADFI_read_node_header( file_index, &child_block_offset,
		&child_node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Skip any leading blanks in the name **/
name_start = 0 ;
while( name[ name_start ] == ' ' )
   name_start++ ;
name_length = strlen( &name[ name_start ] ) ;
if( name_length > ADF_NAME_LENGTH ) {
   *error_return = STRING_LENGTH_TOO_BIG ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

if( name_length == 0 ) {
   *error_return = STRING_LENGTH_ZERO ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** Need to check for uniqueness and legality of the name **/
ADFI_check_4_child_name( file_index, &parent_block_offset,
	&name[ name_start ], &found, &sub_node_entry_location,
	&sub_node_entry, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( found == 1 ) {
   *error_return = DUPLICATE_CHILD_NAME ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

for ( i=0; i < name_length; i++ ) {
   if (  ! isprint ( name[ name_start + i ] ) ||
           name[ name_start + i ] == '/' ) {
      *error_return = INVALID_NODE_NAME;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */
   } /* end for */

	/** Confirm that child is from the parent **/
ADFI_check_4_child_name( file_index, &parent_block_offset,
	child_node.name, &found, &sub_node_entry_location,
	&sub_node_entry, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( found == 0 ) {
   *error_return = CHILD_NOT_OF_GIVEN_PARENT ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

if( (child_block_offset.block != sub_node_entry.child_location.block) ||
   (child_block_offset.offset != sub_node_entry.child_location.offset) ) {
   *error_return = CHILD_NOT_OF_GIVEN_PARENT ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** Copy the name **/
name_length = strlen( name ) ;
for( i=0; i<MIN(name_length, ADF_NAME_LENGTH); i++ ) {
   child_node.name[i] = name[i] ;
   sub_node_entry.child_name[i] = name[i] ;
   } /* end for */
	/** Blank fill the remaining space **/
for( ; i<ADF_NAME_LENGTH; i++ ) {
   child_node.name[i] = ' ' ;
   sub_node_entry.child_name[i] = ' ' ;
   } /* end for */

	/** Write modified node_header **/
ADFI_write_node_header( file_index, &child_block_offset,
		&child_node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** replace the child's name in the parent's sub-node_table **/
ADFI_write_sub_node_table_entry( file_index, &sub_node_entry_location,
	&sub_node_entry, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Finally, update modification date **/
ADFI_write_modification_date( file_index, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Put_Name */
/* end of file ADF_Put_Name.c */
/* file ADF_Read_All_Data.c */
/***********************************************************************
ADF Read All Data:

Read all data from a Node.  Reads all the node's data and returns it into
a contiguous memory space.

ADF_Read_All_Data( ID, data, error_return )
input:  const double ID		The ID of the node to use.
output: char *data		The start of the data in memory.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Read_All_Data(
		const double ID,
		char *data,
		int *error_return )
{
unsigned int            file_index ;
struct DISK_POINTER	block_offset ;
struct NODE_HEADER	node ;
struct TOKENIZED_DATA_TYPE
		tokenized_data_type[ 1 + (ADF_DATA_TYPE_LENGTH + 1)/3 ] ;
struct	DATA_CHUNK_TABLE_ENTRY	*data_chunk_table ;
char	*data_pointer ;

char			file_format, machine_format ;
int			file_bytes, memory_bytes, bytes_to_read ;
long			total_bytes, bytes_read ;
int			i, j ;
double			LID ;

if( data == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get datatype size **/
ADFI_evaluate_datatype( file_index, node.data_type, &file_bytes, &memory_bytes,
	tokenized_data_type, &file_format, &machine_format, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( (file_bytes == 0) || (node.number_of_dimensions == 0) ) {
   *error_return = NO_DATA ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** Calculate total number of bytes in the data **/
total_bytes = file_bytes ;
for( j=0; j<(int)node.number_of_dimensions; j++ )
   total_bytes *= node.dimension_values[j] ;

	/** If there is NO DATA, fill data space with zeros, return error **/
if( node.number_of_data_chunks == 0  ) {
   memset( data, 0, total_bytes*memory_bytes/file_bytes ) ;
   *error_return = NO_DATA ;
   return ;	/** NO_DATA is really a warning, so don't check & abort... **/
   } /* end if */

	/** Read the data from disk **/
else if( node.number_of_data_chunks == 1 ) {
   ADFI_read_data_chunk( file_index, &node.data_chunks, tokenized_data_type,
		file_bytes, total_bytes, 0, total_bytes, data,
		error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end else if */
else {
	/** Allocate memory for the required table space in memory **/
   data_chunk_table = (struct  DATA_CHUNK_TABLE_ENTRY *)
   malloc( node.number_of_data_chunks * sizeof( *data_chunk_table ) ) ;
   if( data_chunk_table == NULL ) {
      *error_return = MEMORY_ALLOCATION_FAILED ;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */

	/** Read in the table **/
   ADFI_read_data_chunk_table( file_index, &node.data_chunks,
   				data_chunk_table, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

	/** Read data from each entry in the table **/
   bytes_read = 0 ;
   data_pointer = data ;
   for( i=0; i<(int)node.number_of_data_chunks; i++ ) {
      bytes_to_read = 
      (data_chunk_table[i].end.block - data_chunk_table[i].start.block) *
		DISK_BLOCK_SIZE +
      (data_chunk_table[i].end.offset - data_chunk_table[i].start.offset) -
		(TAG_SIZE + DISK_POINTER_SIZE) ;

	/** Check to be sure we aren't reading too much data
		(shrinking a data block can cause this)
	**/
      if( bytes_read + bytes_to_read > total_bytes ) {
	 bytes_to_read = total_bytes - bytes_read ;
	 } /* end if */
      if( bytes_to_read == 0 )
	 break ;
      ADFI_read_data_chunk( file_index, &data_chunk_table[i].start,
		tokenized_data_type, file_bytes, bytes_to_read, 0,
		bytes_to_read, data_pointer, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

/** note: memory_bytes and file_bytes might be different (e.g., if machine
          is "IEEE_BIG" and file is "CRAY") in which case data pointer advances
          at a different rate from file pointer. **/
      data_pointer += (bytes_to_read * memory_bytes) / file_bytes ;
      bytes_read += bytes_to_read ;
      } /* end for */
   free( data_chunk_table ) ;
   if( bytes_read < total_bytes ) {
      *error_return = INCOMPLETE_DATA ;
      memset( data_pointer, 0, total_bytes - bytes_read ) ;
      } /* end if */
   } /* end else */

} /* end of ADF_Read_All_Data */
/* end of file ADF_Read_All_Data.c */
/* file ADF_Read_Block_Data.c */
/***********************************************************************
ADF Read Block Data:

Read a continous block of data from a Node.  Reads a block the node's data
and returns it into a contiguous memory space.

ADF_Read_Block_Data( ID, data, error_return )
input:  const double ID		The ID of the node to use.
input:  const long b_start	The starting point in block in token space
input:  const long b_end 	The ending point in block in token space
output: char *data		The start of the data in memory.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Read_Block_Data(
		const double ID,
		const long b_start,
		const long b_end,
		char *data,
		int *error_return )
{
unsigned int            file_index ;
struct DISK_POINTER	block_offset ;
struct NODE_HEADER	node ;
struct TOKENIZED_DATA_TYPE
		tokenized_data_type[ 1 + (ADF_DATA_TYPE_LENGTH + 1)/3 ] ;
struct	DATA_CHUNK_TABLE_ENTRY	*data_chunk_table ;
char	*data_pointer ;

char			file_format, machine_format ;
int			file_bytes, memory_bytes, bytes_to_read ;
long			total_bytes, bytes_read, start_offset ;
long                    chunk_size, chunk_end_byte ;
long                    start_byte, end_byte, block_bytes ;
int			i, j ;
double			LID ;

if( data == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get datatype size **/
ADFI_evaluate_datatype( file_index, node.data_type, &file_bytes, &memory_bytes,
	tokenized_data_type, &file_format, &machine_format, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( (file_bytes == 0) || (node.number_of_dimensions == 0) ) {
   *error_return = NO_DATA ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** Calculate total number of bytes in the data **/
total_bytes = file_bytes ;
for( j=0; j<(int)node.number_of_dimensions; j++ )
   total_bytes *= node.dimension_values[j] ;
if( total_bytes == 0 ) {
   *error_return = ZERO_DIMENSIONS ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

        /** Calculate the starting and ending range in the file **/
start_byte = file_bytes * (b_start-1) ;
end_byte   = file_bytes * b_end ;
if ( start_byte < 0 || start_byte > end_byte || end_byte > total_bytes ) {
   *error_return = START_OUT_OF_DEFINED_RANGE ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */
block_bytes = end_byte - start_byte ;

	/** If there is NO DATA, fill data space with zeros, return error **/
if( node.number_of_data_chunks == 0  ) {
   memset( data, 0, block_bytes*memory_bytes/file_bytes ) ;
   *error_return = NO_DATA ;
   return ;	/** NO_DATA is really a warning, so don't check & abort... **/
   } /* end if */

	/** Read the data from disk **/
else if( node.number_of_data_chunks == 1 ) {
   ADFI_read_data_chunk( file_index, &node.data_chunks, tokenized_data_type,
		         file_bytes, total_bytes, start_byte, block_bytes,
			 data, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end else if */
else {
	/** Allocate memory for the required table space in memory **/
   data_chunk_table = (struct  DATA_CHUNK_TABLE_ENTRY *)
   malloc( node.number_of_data_chunks * sizeof( *data_chunk_table ) ) ;
   if( data_chunk_table == NULL ) {
      *error_return = MEMORY_ALLOCATION_FAILED ;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */

	/** Read in the table **/
   ADFI_read_data_chunk_table( file_index, &node.data_chunks,
   				data_chunk_table, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

	/** Read data from each entry in the table **/
   bytes_read = 0 ;
   chunk_end_byte = 0 ;
   data_pointer = data ;
   for( i=0; i<(int)node.number_of_data_chunks; i++ ) {
      chunk_size = 
      (data_chunk_table[i].end.block - data_chunk_table[i].start.block) *
		DISK_BLOCK_SIZE +
      (data_chunk_table[i].end.offset - data_chunk_table[i].start.offset) -
		(TAG_SIZE + DISK_POINTER_SIZE) ;

        /** Check to be sure we don't think the chunk is bigger than it is
                (shrinking a data block can cause this)
        **/
      if( chunk_end_byte + chunk_size > total_bytes ) {
         chunk_size = total_bytes - chunk_end_byte ;
         } /* end if */
      if( chunk_size == 0 )
         break ;

      chunk_end_byte += chunk_size ;

        /** If start of block not in this chunk then continue **/
      if ( start_byte >= chunk_end_byte )
	 continue ;

         /** Set offset into the current chunk **/
      if ( start_byte > (chunk_end_byte - chunk_size) )
	   /** The start of the block is inside the current chunk so
	     adjust the offset to the beginning of the block **/
         start_offset = ( start_byte - (chunk_end_byte-chunk_size) ) ;
      else
	 start_offset = 0 ;

         /** Calculate the number of bytes needed in this chunk **/
      bytes_to_read = chunk_size - start_offset ;
      if( bytes_read + bytes_to_read > block_bytes ) {
	 bytes_to_read = block_bytes - bytes_read ;
	 } /* end if */
      if( bytes_to_read == 0 || (chunk_end_byte-chunk_size) > end_byte )
	 break ;
      
      ADFI_read_data_chunk( file_index, &data_chunk_table[i].start,
		tokenized_data_type, file_bytes, chunk_size, start_offset,
		bytes_to_read, data_pointer, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

/** note: memory_bytes and file_bytes might be different (e.g., if machine
          is "IEEE_BIG" and file is "CRAY") in which case data pointer advances
          at a different rate from file pointer. **/
      data_pointer += (bytes_to_read * memory_bytes) / file_bytes ;
      bytes_read += bytes_to_read ;
      } /* end for */
   free( data_chunk_table ) ;
   if( bytes_read < block_bytes ) {
      *error_return = INCOMPLETE_DATA ;
      memset( data_pointer, 0, total_bytes - bytes_read ) ;
      } /* end if */
   } /* end else */

} /* end of ADF_Read_Block_Data */
/* end of file ADF_Read_Block_Data.c */
/* file ADF_Read_Data.c */
/***********************************************************************
ADF Read Data:

Read data from a node, with partial capabilities.  The partial 
capabilities are both in the node's data and also in memory.  
Vectors of integers are used to indicate the data to be accessed
from the node, and another set of integer vectors is used to 
describe the memory location for the data.  
	Note:  If the data-type of the node is a compound data-type ("I4[3],R8") 
for example, the partial capabilities will access one or more of
these 20-byte data entities.  You cannot access a subset of an 
occurrence of the data-type.

ADF_Read_Data( ID, s_start[], s_end[], s_stride[], m_num_dims, 
	m_dims[], m_start[], m_end[], m_stride[], data, error_return )
input:  const double ID		The ID of the node to use.
input:  const int s_start[]	The starting dimension values to use in 
				the database (node).
input:  const int s_end[]	The ending dimension values to use in 
				the database (node).
input:  const int s_stride[]	The stride values to use in the database (node).
input:  const int m_num_dims	The number of dimensions to use in memory.
input:  const int m_dims[]	The dimensionality to use in memory.
input:  const int m_start[]	The starting dimension values to use in memory.
input:  const int m_end[]	The ending dimension values to use in memory.
input:  const int m_stride[]	The stride values to use in memory.
output: char *data		The start of the data in memory.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Read_Data(
		const double ID,
		const int s_start[],
		const int s_end[],
		const int s_stride[],
		const int m_num_dims,
		const int m_dims[],
		const int m_start[],
		const int m_end[],
		const int m_stride[],
		char *data,
		int *error_return )
{
unsigned int   file_index ;
struct DISK_POINTER	block_offset, relative_block ;
struct NODE_HEADER  node ;
struct TOKENIZED_DATA_TYPE
              tokenized_data_type[ 1 + (ADF_DATA_TYPE_LENGTH + 1)/3 ] ;
int           current_disk[ADF_MAX_DIMENSIONS] ;
int           current_memory[ADF_MAX_DIMENSIONS] ;
unsigned long total_disk_elements, total_memory_elements ;
unsigned long disk_offset, memory_offset ;
char          disk_format, machine_format ;
int           formats_compare ;
int           i ;
int	      file_bytes = 0 ;
int	      memory_bytes = 0 ;
int	      no_data = FALSE ;
double        LID ;
unsigned long relative_offset = 0, current_chunk_size = 0,
              past_chunk_sizes, current_chunk ;
struct DATA_CHUNK_TABLE_ENTRY   *data_chunk_table = NULL ;

if( (s_start == NULL) || (s_end == NULL) || (s_stride == NULL) ||
    (m_dims == NULL) || (m_start == NULL) || (m_end == NULL) ||
    (m_stride == NULL) || (data == NULL) ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get datatype length **/
ADFI_evaluate_datatype( file_index, node.data_type, &file_bytes, &memory_bytes,
	tokenized_data_type, &disk_format, &machine_format, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( (file_bytes == 0) || (node.number_of_dimensions == 0) ) {
   *error_return = NO_DATA ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

ADFI_count_total_array_points( node.number_of_dimensions,
			       node.dimension_values,
			       s_start, s_end, s_stride,
			       &total_disk_elements, &disk_offset,
			       error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

ADFI_count_total_array_points( (unsigned int)m_num_dims,
			       (unsigned int *)m_dims,
			       m_start, m_end, m_stride,
			       &total_memory_elements, &memory_offset,
			       error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( total_disk_elements != total_memory_elements ) {
   *error_return = UNEQUAL_MEMORY_AND_DISK_DIMS ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

ADFI_file_and_machine_compare( file_index, tokenized_data_type,
			       &formats_compare, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Check to see if there is actual data to use **/
if( node.number_of_data_chunks == 0  ) {
   no_data = TRUE ;
   } /* end if */
	/** Check for multiple data-chunks **/
else if( node.number_of_data_chunks == 1 ) { /** A single data chunk **/
	/** Point to the start of the data **/
   block_offset.block = node.data_chunks.block ;
   block_offset.offset = node.data_chunks.offset + TAG_SIZE +
		         DISK_POINTER_SIZE + disk_offset * file_bytes ;
   ADFI_adjust_disk_pointer( &block_offset, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end else if */
else if( node.number_of_data_chunks > 1 ) {	/** Multiple data chunks **/
   current_chunk = 0 ;
   past_chunk_sizes = 0 ;
   relative_offset = disk_offset * file_bytes ;
        /** Allocate memory for the required table space in memory **/
   data_chunk_table = (struct  DATA_CHUNK_TABLE_ENTRY *)
	malloc( node.number_of_data_chunks * sizeof( *data_chunk_table ) ) ;
   if( data_chunk_table == NULL ) {
      *error_return = MEMORY_ALLOCATION_FAILED ;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */

	/** Read in the table **/
   ADFI_read_data_chunk_table( file_index, &node.data_chunks,
                data_chunk_table, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

   current_chunk_size = (data_chunk_table[ current_chunk ].end.block -
	data_chunk_table[ current_chunk ].start.block) * DISK_BLOCK_SIZE +
	(data_chunk_table[ current_chunk ].end.offset -
		data_chunk_table[ current_chunk ].start.offset) -
	(TAG_SIZE + DISK_POINTER_SIZE) ;

   } /* end else if */

	/** Setup initial indexing **/
for( i=0; i<(int)node.number_of_dimensions; i++ )
   current_disk[i] = s_start[i] ;
for( i=0; i<m_num_dims; i++ )
   current_memory[i] = m_start[i] ;
   
	/** Adjust data pointer **/
if( memory_offset != 0 )
   data += memory_offset * memory_bytes ;
for( i=0; i<total_disk_elements; i++ ) {
	/** If there is no data on disk, return zeros **/
   if( no_data == TRUE ) {
      memset( data, 0, memory_bytes ) ;
      } /* end if */
   else if( node.number_of_data_chunks == 1 ) {	/** A single data chunk **/
	/** Get the data off of disk **/
      if ( block_offset.offset > DISK_BLOCK_SIZE ) {
        ADFI_adjust_disk_pointer( &block_offset, error_return ) ;
        CHECK_ADF_ABORT( *error_return ) ;
        } /* end if */

      if( formats_compare ) {
      /** Read the data off of disk directly **/
         ADFI_read_file( file_index, block_offset.block, block_offset.offset,
            file_bytes, (char *)data, error_return ) ;
         CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */
      else {   /** Read and translate data **/
         ADFI_read_data_translated( file_index, block_offset.block,
            block_offset.offset, tokenized_data_type, file_bytes,
            file_bytes, data, error_return ) ;
         CHECK_ADF_ABORT( *error_return ) ;
      } /* end else */

   /** Increment disk pointers, for the special case of one dimensional
       data we will a simple increment to maximize the throught. Thus for
       block reads you can temporarily change to 1D for the read to
       improve efficiency. Note total size shouldn't change!! **/
      if( i < total_disk_elements - 1 ) {
        if ( node.number_of_dimensions == 1 ) {
	  disk_offset = s_stride[0];
	  current_disk[0] += disk_offset;
	  if ( current_disk[0] > s_end[0] ) current_disk[0] = s_end[0] ;
	} /* end if */
	else {
  	  ADFI_increment_array(
		    node.number_of_dimensions, node.dimension_values,
                    s_start, s_end, s_stride, current_disk, &disk_offset,
                    error_return ) ;
           CHECK_ADF_ABORT( *error_return ) ;
	}  /* end else */

         block_offset.offset += disk_offset * file_bytes ;
         if ( block_offset.offset > DISK_BLOCK_SIZE ) {
           ADFI_adjust_disk_pointer( &block_offset, error_return ) ;
           CHECK_ADF_ABORT( *error_return ) ;
           } /* end if */
         } /* end if */
      } /* end else if */
   else if( node.number_of_data_chunks > 1 ) {	/** Multiple data chunks **/
      while( relative_offset >= past_chunk_sizes + current_chunk_size ) {
	 if( ++current_chunk >= node.number_of_data_chunks ) {
	    *error_return = INCOMPLETE_DATA ;
            CHECK_ADF_ABORT( *error_return ) ;
	    } /* end if */
	 else {
	    past_chunk_sizes += current_chunk_size ;
   	    current_chunk_size = (data_chunk_table[ current_chunk ].end.block -
	      data_chunk_table[ current_chunk ].start.block) * DISK_BLOCK_SIZE +
		(data_chunk_table[ current_chunk ].end.offset -
		data_chunk_table[ current_chunk ].start.offset) -
		(TAG_SIZE + DISK_POINTER_SIZE) ;
	    } /* end else */
	 } /* end while */

	/** Get the data off of disk **/
      relative_block.block = data_chunk_table[ current_chunk ].start.block ;
      relative_block.offset = data_chunk_table[ current_chunk ].start.offset +
		(TAG_SIZE + DISK_POINTER_SIZE) +
		(relative_offset - past_chunk_sizes) ;
      if ( relative_block.offset > DISK_BLOCK_SIZE ) {
        ADFI_adjust_disk_pointer( &relative_block, error_return ) ;
        CHECK_ADF_ABORT( *error_return ) ;
      }

      if( formats_compare ) {
      /** Read the data off of disk directly **/
      ADFI_read_file( file_index, relative_block.block, relative_block.offset,
            file_bytes, (char *)data, error_return ) ;
         CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */
      else {   /** Read and translate data **/
         ADFI_read_data_translated( file_index, relative_block.block,
            relative_block.offset, tokenized_data_type, file_bytes,
            file_bytes, data, error_return ) ;
         CHECK_ADF_ABORT( *error_return ) ;
      } /* end else */

	/** Increment disk pointers **/
      if( i < total_disk_elements - 1 ) {
        if ( node.number_of_dimensions == 1 ) {
	  disk_offset = s_stride[0];
	  current_disk[0] += disk_offset;
	  if ( current_disk[0] > s_end[0] ) current_disk[0] = s_end[0] ;
	} /* end if */
	else {
           ADFI_increment_array(
		node.number_of_dimensions, node.dimension_values,
                s_start, s_end, s_stride, current_disk, &disk_offset,
                error_return ) ;
           CHECK_ADF_ABORT( *error_return ) ;
	}  /* end else */

         relative_offset += disk_offset * file_bytes ;
         } /* end if */
      } /* end else if */

   if( i < total_disk_elements - 1 ) {
	/** Increment memory pointers **/
     if ( m_num_dims == 1 ) {
       memory_offset = m_stride[0];
       current_memory[0] += disk_offset;
       if ( current_memory[0] > m_end[0] ) current_memory[0] = m_end[0] ;
     } /* end if */ 
     else {
       ADFI_increment_array(
		(unsigned int)m_num_dims, (unsigned int* )m_dims,
		m_start, m_end, m_stride,
                current_memory, &memory_offset, error_return ) ;
       CHECK_ADF_ABORT( *error_return ) ;
     } /* end else */

	/** Adjust data pointer **/
      data += memory_offset * memory_bytes ;
      } /* end if */
   } /* end for */

if( node.number_of_data_chunks > 1 ) /** Multiple data chunks **/
   free( data_chunk_table ) ;

} /* end of ADF_Read_Data */
/* end of file ADF_Read_Data.c */
/* file ADF_Set_Error_State.c */
/***********************************************************************
ADF Set Error State:

Set Error State.  For all ADF calls, set the error handling convention;
either return error codes, or abort the program on an error.  The
default state for the ADF interface is to return error codes and NOT abort.

ADF_Set_Error_State( error_state, error_return )
input:  const int error_state	Flag for ABORT on error (1) or return error
				status (0).
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Set_Error_State(
		const int error_state,
		int *error_return )
{
*error_return = NO_ERROR ;
if( error_state == 0 )
   ADF_abort_on_error = FALSE ;
else if( error_state == 1 )
   ADF_abort_on_error = TRUE ;
else {
   *error_return = BAD_ERROR_STATE ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end else */

} /* end of ADF_Set_Error_State */
/* end of file ADF_Set_Error_State.c */
/* file ADF_Set_Label.c */
/***********************************************************************
ADF Set Label:

Set Label.  Set the 32 character string in a node's label field.

ADF_Set_Label( ID, label, error_return )
input:  const double ID		The ID of the node to use.
input:  const char *label	The 32-character label of the node.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Set_Label(
		const double ID,
		const char *label,
		int *error_return )
{
unsigned int 		file_index ;
struct DISK_POINTER	block_offset ;
struct NODE_HEADER	node ;
int			i, label_length ;
double			LID ;

	/** Don't check for NULL or BLANK label, these are OK **/
*error_return = NO_ERROR ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Copy the label **/
if( label == NULL )
   label_length = 0 ; /* copy none, then blank fill */
else
   label_length = strlen( label ) ;
if( label_length > ADF_LABEL_LENGTH ) {
   *error_return = STRING_LENGTH_TOO_BIG ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */
for( i=0; i<MIN(label_length, ADF_LABEL_LENGTH); i++ )
   node.label[i] = label[i] ;
	/** Blank fill the remaining space **/
for( ; i<ADF_LABEL_LENGTH; i++ )
   node.label[i] = ' ' ;

	/** Write modified node_header **/
ADFI_write_node_header( file_index, &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Finally, update modification date **/
ADFI_write_modification_date( file_index, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Set_Label */
/* end of file ADF_Set_Label.c */
/* file ADF_Write_All_Data.c */
/* file ADF_Write_All_Data.c */
/***********************************************************************
ADF Write All Data:

Write all data to a Node.  Writes all the node's data from a contiguous 
memory space.

ADF_Write_All_Data( ID, data, error_return )
input:  const double ID		The ID of the node to use.
input:  const char *data	The start of the data in memory.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Write_All_Data(
		const double ID,
		const char *data,
		int *error_return )
{
unsigned int            file_index ;
struct DISK_POINTER	block_offset, new_block_offset, dct_block_offset ;
struct NODE_HEADER	node ;
struct TOKENIZED_DATA_TYPE
		tokenized_data_type[ 1 + (ADF_DATA_TYPE_LENGTH + 1)/3 ] ;
struct DATA_CHUNK_TABLE_ENTRY	data_chunk_entry_table[2], *data_chunk_table ;
int			file_bytes, memory_bytes ;
long			total_bytes, current_bytes ;
int			i, j ;
char    		tag[TAG_SIZE+1] ;
struct DISK_POINTER     data_start, chunk_start, end_of_chunk_tag ;
long                    chunk_total_bytes ;
char			file_format, machine_format ;
double			LID ;

if( data == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get the datatype length **/
ADFI_evaluate_datatype( file_index, node.data_type, &file_bytes, &memory_bytes,
	tokenized_data_type, &file_format, &machine_format, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Calculate the total number of data bytes **/
total_bytes = file_bytes ;
for( j=0; j<(int)node.number_of_dimensions; j++ )
   total_bytes *= node.dimension_values[j] ;
if( total_bytes == 0 ) {
   *error_return = ZERO_DIMENSIONS ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** If there currently is NO data, allocate disk space for it **/
if( node.number_of_data_chunks == 0  ) {
   ADFI_file_malloc( file_index,
		total_bytes + TAG_SIZE + TAG_SIZE + DISK_POINTER_SIZE,
		&node.data_chunks, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

	/** Write the new data **/
   ADFI_write_data_chunk( file_index, &node.data_chunks, tokenized_data_type,
		file_bytes, total_bytes, 0, total_bytes, data,
		error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

	/** Record the modified the node-header **/
   node.number_of_data_chunks = 1 ;
   ADFI_write_node_header( file_index, &block_offset, &node, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */
else if( node.number_of_data_chunks == 1 ) {
	/** Get the data length **/
   ADFI_read_chunk_length( file_index, &node.data_chunks, tag,
		&end_of_chunk_tag, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
   tag[TAG_SIZE] = '\0' ;

        /** Check start-of-chunk tag **/
   if( ADFI_stridx_c( tag, data_chunk_start_tag ) != 0 ) {
      *error_return = ADF_DISK_TAG_ERROR ;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */

	/** Point to the start of the data **/
   data_start.block = node.data_chunks.block ;
   data_start.offset = node.data_chunks.offset + TAG_SIZE + DISK_POINTER_SIZE ;
   ADFI_adjust_disk_pointer( &data_start, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

	/** See if the new data exceedes the existing data space **/
   chunk_total_bytes = end_of_chunk_tag.offset - data_start.offset +
	(end_of_chunk_tag.block - data_start.block) * DISK_BLOCK_SIZE ;


	/** If Data grew:  Write old size, then allocate more
		data-space and write the rest **/
   if( total_bytes > chunk_total_bytes ) {
	/** Write the part of the new data to existing data-chunk **/
      ADFI_write_data_chunk( file_index, &node.data_chunks,
		tokenized_data_type, file_bytes, chunk_total_bytes, 0,
		chunk_total_bytes, data, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

	/** Allocate a second data chunk **/
      total_bytes -= chunk_total_bytes ;
      ADFI_file_malloc( file_index,
		total_bytes + TAG_SIZE + TAG_SIZE + DISK_POINTER_SIZE,
		&new_block_offset, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

	/** Write the rest of the data **/
/** note: memory_bytes and file_bytes might be different (e.g., if machine
          is "IEEE_BIG" and file is "CRAY") in which case data pointer advances
          at a different rate from file pointer. **/
      data += (chunk_total_bytes * memory_bytes ) / file_bytes ;

      ADFI_write_data_chunk( file_index, &new_block_offset,
		tokenized_data_type, file_bytes, total_bytes, 0,
		total_bytes, data, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

	/** Allocate a data-chunk-table for two entries **/
      ADFI_file_malloc( file_index, 2 * TAG_SIZE + 5 * DISK_POINTER_SIZE,
		&dct_block_offset, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

	/** Write data-chunk-table to disk **/
      data_chunk_entry_table[0].start.block = node.data_chunks.block ;
      data_chunk_entry_table[0].start.offset = node.data_chunks.offset ;
      chunk_start.block = node.data_chunks.block ;
      chunk_start.offset = node.data_chunks.offset + TAG_SIZE ;
      ADFI_adjust_disk_pointer( &chunk_start, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
	   /** get the size of the data_chunk for the table end pointer **/
      ADFI_read_disk_pointer_from_disk( file_index,
                chunk_start.block, chunk_start.offset,
		&data_chunk_entry_table[0].end, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
      data_chunk_entry_table[1].start.block = new_block_offset.block ;
      data_chunk_entry_table[1].start.offset = new_block_offset.offset ;
      chunk_start.block = new_block_offset.block ;
      chunk_start.offset = new_block_offset.offset + TAG_SIZE ;
      ADFI_adjust_disk_pointer( &chunk_start, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
	   /** get the size of the data_chunk for the table end pointer **/
      ADFI_read_disk_pointer_from_disk( file_index,
                chunk_start.block, chunk_start.offset,
		&data_chunk_entry_table[1].end, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
      ADFI_write_data_chunk_table( file_index, &dct_block_offset,
		2, data_chunk_entry_table, error_return ) ;

	/** Update node header with number of data-chunks = 2 and the
		pointer to the data-chunk-table **/
      node.data_chunks.block = dct_block_offset.block ;
      node.data_chunks.offset = dct_block_offset.offset ;
      node.number_of_data_chunks = 2 ;
      ADFI_write_node_header( file_index, &block_offset, &node, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */
   else {
	/** Write the new data to existing data-chunk **/
      ADFI_write_data_chunk( file_index, &node.data_chunks,
		tokenized_data_type, file_bytes, total_bytes, 0,
		total_bytes, data, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end else */
   } /* end else if */
else { /** Multiple data chunks **/
	/** Allocate memory for the data-chunk-table, with an additional
	    entry in case we need to grow it
	**/
         data_chunk_table = (struct  DATA_CHUNK_TABLE_ENTRY *)
	    malloc( (node.number_of_data_chunks + 1 ) *
					sizeof( *data_chunk_table ) ) ;
         if( data_chunk_table == NULL ) {
            *error_return = MEMORY_ALLOCATION_FAILED ;
            CHECK_ADF_ABORT( *error_return ) ;
            } /* end if */

	/** Read in the table **/
	 ADFI_read_data_chunk_table( file_index, &node.data_chunks,
                data_chunk_table, error_return ) ;
         CHECK_ADF_ABORT( *error_return ) ;

	/** looping on the data-chunks, write the size of the current chunk **/
	 for( i=0; i<(int)node.number_of_data_chunks; i++ ) {
	    current_bytes = (data_chunk_table[i].end.block -
	         data_chunk_table[i].start.block) * DISK_BLOCK_SIZE +
	         (data_chunk_table[i].end.offset -
			data_chunk_table[i].start.offset) -
		 (TAG_SIZE + DISK_POINTER_SIZE) ;
        /** Limit the number of bytes written by whats left to write. **/
            current_bytes = MIN( current_bytes, total_bytes ) ;
            ADFI_write_data_chunk( file_index, &data_chunk_table[i].start,
		 tokenized_data_type, file_bytes, current_bytes, 0,
		 current_bytes, data, error_return ) ;
            CHECK_ADF_ABORT( *error_return ) ;

/** note: memory_bytes and file_bytes might be different (e.g., if machine
          is "IEEE_BIG" and file is "CRAY") in which case data pointer advances
          at a different rate from file pointer. **/
	    data += (current_bytes * memory_bytes ) / file_bytes ;

	    total_bytes -= current_bytes ;
	    if( total_bytes <= 0 )
	       break ;
	    } /* end for */

	/** If we are out of data-chunks and have data left, allocate a
		new data-chunk in the file. **/

	 if( total_bytes > 0 ) {
		/** Write data-chunk-table to disk **/

		/** allocate data space in the file **/
            ADFI_file_malloc( file_index, 2 * TAG_SIZE + DISK_POINTER_SIZE +
			total_bytes,
		      &data_chunk_table[ node.number_of_data_chunks ].start,
			error_return ) ;
            CHECK_ADF_ABORT( *error_return ) ;

	    data_chunk_table[ node.number_of_data_chunks ].end.block =
	    	data_chunk_table[ node.number_of_data_chunks ].start.block ;
	    data_chunk_table[ node.number_of_data_chunks ].end.offset =
	    	data_chunk_table[ node.number_of_data_chunks ].start.offset +
		TAG_SIZE + DISK_POINTER_SIZE + total_bytes ;
   	    ADFI_adjust_disk_pointer(
		&data_chunk_table[ node.number_of_data_chunks ].end,
			error_return ) ;
   	    CHECK_ADF_ABORT( *error_return ) ;

		/** allocate space for the new data-chunk-entry-table **/
      	    ADFI_file_malloc( file_index, 2 * TAG_SIZE + 
		(2 * (node.number_of_data_chunks + 1) + 1) * DISK_POINTER_SIZE,
		&dct_block_offset, error_return ) ;
      	    CHECK_ADF_ABORT( *error_return ) ;

            ADFI_write_data_chunk_table( file_index, &dct_block_offset,
		node.number_of_data_chunks+1, data_chunk_table, error_return ) ;
      	    CHECK_ADF_ABORT( *error_return ) ;

            ADFI_write_data_chunk( file_index,
		&data_chunk_table[node.number_of_data_chunks ].start,
		tokenized_data_type, file_bytes, total_bytes, 0,
		total_bytes, data, error_return ) ;
            CHECK_ADF_ABORT( *error_return ) ;

		/** Free the old data-chunk-table **/
	    ADFI_file_free( file_index, &node.data_chunks, 0, error_return ) ;
            CHECK_ADF_ABORT( *error_return ) ;
	    
		/** Update node header with number of data-chunks++ and the
			pointer to the data-chunk-table **/
	    node.number_of_data_chunks++ ; 
      	    node.data_chunks.block = dct_block_offset.block ;
      	    node.data_chunks.offset = dct_block_offset.offset ;
      	    ADFI_write_node_header( file_index, &block_offset, &node,
				error_return ) ;
      	    CHECK_ADF_ABORT( *error_return ) ;
	    } /* end if */
	 free( data_chunk_table ) ;
   } /* end else */

	/** Finally, update modification date **/
ADFI_write_modification_date( file_index, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Write_All_Data */
/* end of file ADF_Write_All_Data.c */
/* end of file ADF_Write_All_Data.c */
/* file ADF_Write_Block_Data.c */
/***********************************************************************
ADF Write Block Data:

Write all data to a Node.  Writes all the node's data from a contiguous 
memory space.

ADF_Write_All_Data( ID, data, error_return )
input:  const double ID     The ID of the node to use.
input:  const long b_start  The starting point in block in token space
input:  const long b_end    The ending point in block in token space
input:  const char *data    The start of the data in memory.
output: int *error_return   Error return.
***********************************************************************/
void    ADF_Write_Block_Data(
        const double ID,
        const long b_start,
        const long b_end,
        char *data,
        int *error_return )
{
unsigned int        file_index ;
struct DISK_POINTER	block_offset, new_block_offset, dct_block_offset ;
struct NODE_HEADER	node ;
struct TOKENIZED_DATA_TYPE
        tokenized_data_type[ 1 + (ADF_DATA_TYPE_LENGTH + 1)/3 ] ;
struct DATA_CHUNK_TABLE_ENTRY  data_chunk_entry_table[2], *data_chunk_table ;

char        file_format, machine_format ;
int         file_bytes, memory_bytes ;
long        total_bytes, bytes_written, bytes_to_write = 0;
int         i, j ;
char        tag[TAG_SIZE+1] ;
struct DISK_POINTER     data_start, chunk_start, end_of_chunk_tag ;
long        start_offset ;
long        chunk_size, chunk_end_byte ;
long        start_byte, end_byte, block_bytes ;
double      LID ;

if( data == NULL ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

    /** Get the datatype length **/
ADFI_evaluate_datatype( file_index, node.data_type, &file_bytes, &memory_bytes,
	tokenized_data_type, &file_format, &machine_format, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

    /** Calculate the total number of data bytes **/
total_bytes = file_bytes ;
for( j=0; j<(int)node.number_of_dimensions; j++ )
   total_bytes *= node.dimension_values[j] ;
if( total_bytes == 0 ) {
   *error_return = ZERO_DIMENSIONS ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

        /** Calculate the starting and ending range in the file **/
start_byte = file_bytes * (b_start-1) ;
end_byte   = file_bytes * b_end ;
if ( start_byte < 0 || start_byte > end_byte || end_byte > total_bytes ) {
   *error_return = START_OUT_OF_DEFINED_RANGE ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */
block_bytes = end_byte - start_byte ;

    /** If there currently is NO data, allocate disk space for it **/
if( node.number_of_data_chunks == 0  ) {
   ADFI_file_malloc( file_index,
		total_bytes + TAG_SIZE + TAG_SIZE + DISK_POINTER_SIZE,
		&node.data_chunks, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

    /** Write the new data **/
   ADFI_write_data_chunk( file_index, &node.data_chunks, tokenized_data_type,
		file_bytes, total_bytes, start_byte, block_bytes, data,
		error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

    /** Record the modified the node-header **/
   node.number_of_data_chunks = 1 ;
   ADFI_write_node_header( file_index, &block_offset, &node, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */
else if( node.number_of_data_chunks == 1 ) {
    /** Get the data length **/
   ADFI_read_chunk_length( file_index, &node.data_chunks, tag,
		&end_of_chunk_tag, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
   tag[TAG_SIZE] = '\0' ;

        /** Check start-of-chunk tag **/
   if( ADFI_stridx_c( tag, data_chunk_start_tag ) != 0 ) {
      *error_return = ADF_DISK_TAG_ERROR ;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */

    /** Point to the start of the data **/
   data_start.block = node.data_chunks.block ;
   data_start.offset = node.data_chunks.offset + TAG_SIZE + DISK_POINTER_SIZE ;
   ADFI_adjust_disk_pointer( &data_start, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

	/** See if the new data exceedes the existing data space **/
   chunk_size = end_of_chunk_tag.offset - data_start.offset +
	(end_of_chunk_tag.block - data_start.block) * DISK_BLOCK_SIZE ;


    /** If Data grew:  Write old size, then allocate more
        data-space and write the rest **/
   if( total_bytes > chunk_size ) {
    /** Write the part of the new data to existing data-chunk **/
     bytes_written = 0 ;
     if ( start_byte <= chunk_size ) {
        bytes_to_write = MIN ( block_bytes, (chunk_size-start_byte) ) ;
        ADFI_write_data_chunk( file_index, &node.data_chunks,
	        tokenized_data_type, file_bytes, chunk_size, start_byte,
		bytes_to_write, data, error_return ) ;
        CHECK_ADF_ABORT( *error_return ) ;
	bytes_written += bytes_to_write ;
        } /* end if */

    /** Allocate a second data chunk **/
      total_bytes -= chunk_size ;
      ADFI_file_malloc( file_index,
		total_bytes + TAG_SIZE + TAG_SIZE + DISK_POINTER_SIZE,
		&new_block_offset, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

    /** Write the rest of the data **/
/** note: memory_bytes and file_bytes might be different (e.g., if machine
          is "IEEE_BIG" and file is "CRAY") in which case data pointer advances
          at a different rate from file pointer. **/
      data += (bytes_to_write * memory_bytes ) / file_bytes ;

      if ( bytes_written < block_bytes ) {
	 bytes_to_write = block_bytes - bytes_written ;
	 start_offset  = MAX ( 0L, (start_byte - chunk_size) ) ;
         ADFI_write_data_chunk( file_index, &new_block_offset,
		tokenized_data_type, file_bytes, total_bytes, start_offset,
		bytes_to_write, data, error_return ) ;
         CHECK_ADF_ABORT( *error_return ) ;
         } /* end if */
      else {
         ADFI_write_data_chunk( file_index, &new_block_offset,
		tokenized_data_type, file_bytes, total_bytes, 0,
		total_bytes, NULL, error_return ) ;
         CHECK_ADF_ABORT( *error_return ) ;
         } /* end else */

    /** Allocate a data-chunk-table for two entries **/
      ADFI_file_malloc( file_index, 2 * TAG_SIZE + 5 * DISK_POINTER_SIZE,
		&dct_block_offset, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

    /** Write data-chunk-table to disk **/
      data_chunk_entry_table[0].start.block = node.data_chunks.block ;
      data_chunk_entry_table[0].start.offset = node.data_chunks.offset ;
       /** get the size of the data_chunk for the table end pointer **/
      chunk_start.block = node.data_chunks.block ;
      chunk_start.offset = node.data_chunks.offset + TAG_SIZE ;
      ADFI_adjust_disk_pointer( &chunk_start, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
      ADFI_read_disk_pointer_from_disk( file_index,
		chunk_start.block, chunk_start.offset,
		&data_chunk_entry_table[0].end, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
      data_chunk_entry_table[1].start.block = new_block_offset.block ;
      data_chunk_entry_table[1].start.offset = new_block_offset.offset ;
      chunk_start.block = new_block_offset.block ;
      chunk_start.offset = new_block_offset.offset + TAG_SIZE ;
      ADFI_adjust_disk_pointer( &chunk_start, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
       /** get the size of the data_chunk for the table end pointer **/
      ADFI_read_disk_pointer_from_disk( file_index,
		chunk_start.block, chunk_start.offset,
		&data_chunk_entry_table[1].end, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
      ADFI_write_data_chunk_table( file_index, &dct_block_offset,
		2, data_chunk_entry_table, error_return ) ;

    /** Update node header with number of data-chunks = 2 and the
		pointer to the data-chunk-table **/
      node.data_chunks.block = dct_block_offset.block ;
      node.data_chunks.offset = dct_block_offset.offset ;
      node.number_of_data_chunks = 2 ;
      ADFI_write_node_header( file_index, &block_offset, &node, error_return );
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */
   else {
    /** Write the new data to existing data-chunk **/
      ADFI_write_data_chunk( file_index, &node.data_chunks,
		tokenized_data_type, file_bytes, chunk_size, start_byte,
		block_bytes, data, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end else */
   } /* end else if */
else { /** Multiple data chunks **/
    /** Allocate memory for the data-chunk-table, with an additional
	    entry in case we need to grow it
    **/
         data_chunk_table = (struct  DATA_CHUNK_TABLE_ENTRY *)
	    malloc( (node.number_of_data_chunks + 1 ) *
					sizeof( *data_chunk_table ) ) ;
         if( data_chunk_table == NULL ) {
            *error_return = MEMORY_ALLOCATION_FAILED ;
            CHECK_ADF_ABORT( *error_return ) ;
            } /* end if */

    /** Read in the table **/
	 ADFI_read_data_chunk_table( file_index, &node.data_chunks,
                data_chunk_table, error_return ) ;
         CHECK_ADF_ABORT( *error_return ) ;

    /** looping on the data-chunks, write the size of the current chunk **/
        chunk_end_byte = 0 ;
	 bytes_written = 0 ;
	 for( i=0; i<(int)node.number_of_data_chunks; i++ ) {
	    chunk_size = (data_chunk_table[i].end.block -
	         data_chunk_table[i].start.block) * DISK_BLOCK_SIZE +
	         (data_chunk_table[i].end.offset -
			data_chunk_table[i].start.offset) -
		 (TAG_SIZE + DISK_POINTER_SIZE) ;
            chunk_end_byte += chunk_size ;

              /** If start of block not in this chunk then continue **/
            if ( start_byte > chunk_end_byte )
	       continue ;

            /** Set offset into the current chunk **/
            if ( start_byte > (chunk_end_byte - chunk_size) )
	         /** The start of the block is inside the current chunk so
	           adjust the offset to the beginning of the block **/
               start_offset = ( start_byte - (chunk_end_byte-chunk_size) ) ;
            else
	       start_offset = 0 ;

              /** Check to be sure we aren't writing too much data **/
            bytes_to_write = chunk_size - start_offset ;
            if( bytes_written + bytes_to_write > block_bytes ) {
	       bytes_to_write = block_bytes - bytes_written ;
	       } /* end if */
            if( bytes_to_write == 0 || (chunk_end_byte-chunk_size) > end_byte )
	       continue ;

           /** Write the chunk **/
            ADFI_write_data_chunk( file_index, &data_chunk_table[i].start,
		 tokenized_data_type, file_bytes, chunk_size, start_offset,
		 bytes_to_write, data, error_return ) ;
            CHECK_ADF_ABORT( *error_return ) ;

/** note: memory_bytes and file_bytes might be different (e.g., if machine
          is "IEEE_BIG" and file is "CRAY") in which case data pointer advances
          at a different rate from file pointer. **/
	    data += (bytes_to_write * memory_bytes ) / file_bytes ;

	    bytes_written += bytes_to_write ;
	    } /* end for */

    /** If we are out of data-chunks and have data left, allocate a
		new data-chunk in the file. **/
         total_bytes -= chunk_end_byte ;
	 if( total_bytes > 0 ) {
        /** Write data-chunk-table to disk **/

        /** allocate data space in the file **/
            ADFI_file_malloc( file_index, 2 * TAG_SIZE + DISK_POINTER_SIZE +
			total_bytes,
		      &data_chunk_table[ node.number_of_data_chunks ].start,
			error_return ) ;
            CHECK_ADF_ABORT( *error_return ) ;

	    data_chunk_table[ node.number_of_data_chunks ].end.block =
	    	data_chunk_table[ node.number_of_data_chunks ].start.block ;
	    data_chunk_table[ node.number_of_data_chunks ].end.offset =
	    	data_chunk_table[ node.number_of_data_chunks ].start.offset +
		TAG_SIZE + DISK_POINTER_SIZE + total_bytes ;
   	    ADFI_adjust_disk_pointer(
		&data_chunk_table[ node.number_of_data_chunks ].end,
			error_return ) ;
   	    CHECK_ADF_ABORT( *error_return ) ;

        /** allocate space for the new data-chunk-entry-table **/
      	    ADFI_file_malloc( file_index, 2 * TAG_SIZE + 
		(2 * (node.number_of_data_chunks + 1) + 1) * DISK_POINTER_SIZE,
		&dct_block_offset, error_return ) ;
      	    CHECK_ADF_ABORT( *error_return ) ;

            ADFI_write_data_chunk_table( file_index, &dct_block_offset,
		node.number_of_data_chunks+1, data_chunk_table, error_return ) ;
      	    CHECK_ADF_ABORT( *error_return ) ;

            if ( bytes_written < block_bytes ) {
	       bytes_to_write = block_bytes - bytes_written ;
	       start_offset  = MAX ( 0L, (start_byte - total_bytes) ) ;
               ADFI_write_data_chunk( file_index,
		   &data_chunk_table[node.number_of_data_chunks ].start,
		   tokenized_data_type, file_bytes,
		   total_bytes, start_offset, bytes_to_write,
		   data, error_return ) ;
               CHECK_ADF_ABORT( *error_return ) ;
	       } /* end if */
            else {
               ADFI_write_data_chunk( file_index,
           &data_chunk_table[node.number_of_data_chunks ].start,
           tokenized_data_type, file_bytes, total_bytes, 0,
           total_bytes, NULL, error_return ) ;
               CHECK_ADF_ABORT( *error_return ) ;
            } /* end else */

        /** Free the old data-chunk-table **/
	    ADFI_file_free( file_index, &node.data_chunks, 0, error_return ) ;
            CHECK_ADF_ABORT( *error_return ) ;
	    
        /** Update node header with number of data-chunks++ and the
			pointer to the data-chunk-table **/
	    node.number_of_data_chunks++ ; 
      	    node.data_chunks.block = dct_block_offset.block ;
      	    node.data_chunks.offset = dct_block_offset.offset ;
      	    ADFI_write_node_header( file_index, &block_offset, &node,
				error_return ) ;
      	    CHECK_ADF_ABORT( *error_return ) ;
	    } /* end if */
	 free( data_chunk_table ) ;
   } /* end else */

    /** Finally, update modification date **/
ADFI_write_modification_date( file_index, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Write_Block_Data */
/* end of file ADF_Write_Block_Data.c */
/* file ADF_Write_Data.c */
/***********************************************************************
ADF Write Data:

Write data to a Node, with partial capabilities.  See ADF_Read_Data for 
description.

ADF_Write_Data( ID, s_start[], s_end[], s_stride[], m_num_dims, 
	m_dims[], m_start[], m_end[], m_stride[], data, error_return )
input:  const double ID		The ID of the node to use.
input:  const int s_start[]	The starting dimension values to use in 
				the database (node).
input:  const int s_end[]	The ending dimension values to use in 
				the database (node).
input:  const int s_stride[]	The stride values to use in the database (node).
input:  const int m_num_dims	The number of dimensions to use in memory.
input:  const int m_dims[]	The dimensionality to use in memory.
input:  const int m_start[]	The starting dimension values to use in memory.
input:  const int m_end[]	The ending dimension values to use in memory.
input:  const int m_stride[]	The stride values to use in memory.
input:  const char *data	The start of the data in memory.
output: int *error_return	Error return.
***********************************************************************/
void	ADF_Write_Data(
		const double ID,
		const int s_start[],
		const int s_end[],
		const int s_stride[],
		const int m_num_dims,
		const int m_dims[],
		const int m_start[],
		const int m_end[],
		const int m_stride[],
		const char *data,
		int *error_return )
{
unsigned int        file_index ;
struct DISK_POINTER block_offset, dct_block_offset, relative_block ;
struct DISK_POINTER data_start, new_block_offset ;
struct DISK_POINTER chunk_start, end_of_chunk_tag ;
struct NODE_HEADER  node ;
struct DATA_CHUNK_TABLE_ENTRY   *data_chunk_table ;
struct TOKENIZED_DATA_TYPE
              tokenized_data_type[ 1 + (ADF_DATA_TYPE_LENGTH + 1)/3 ] ;
int           current_disk[ADF_MAX_DIMENSIONS] ;
int           current_memory[ADF_MAX_DIMENSIONS] ;
unsigned long total_disk_elements, total_memory_elements ;
unsigned long disk_offset, memory_offset ;
int           formats_compare ;
char          disk_format, machine_format ;
int           i ;
int           file_bytes = 0 ;
int           memory_bytes = 0 ;
char          tag[TAG_SIZE+1] ;
unsigned long total_bytes ;
long          current_bytes, chunk_total_bytes ;
double        LID ;
unsigned long relative_offset, current_chunk, current_chunk_size,
              past_chunk_sizes ;

if( (s_start == NULL) || (s_end == NULL) || (s_stride == NULL) ||
    (m_dims == NULL) || (m_start == NULL) || (m_end == NULL) ||
    (m_stride == NULL) || (data == NULL) ) {
   *error_return = NULL_POINTER ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

*error_return = NO_ERROR ;
data_chunk_table = 0L ;

ADFI_chase_link( ID, &LID, &file_index,  &block_offset, &node, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** Get datatype length **/
ADFI_evaluate_datatype( file_index, node.data_type, &file_bytes, &memory_bytes,
	tokenized_data_type, &disk_format, &machine_format, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( (file_bytes == 0) || (node.number_of_dimensions == 0) ) {
   *error_return = NO_DATA ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

ADFI_count_total_array_points( node.number_of_dimensions,
			       node.dimension_values,
			       s_start, s_end, s_stride,
			       &total_disk_elements, &disk_offset,
			       error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

ADFI_count_total_array_points( (unsigned int)m_num_dims,
			       (unsigned int *)m_dims,
			       m_start, m_end, m_stride,
			       &total_memory_elements, &memory_offset,
			       error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

if( total_disk_elements != total_memory_elements ) {
   *error_return = UNEQUAL_MEMORY_AND_DISK_DIMS ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

	/** Calculate the total number of data bytes **/
total_bytes = file_bytes ;
for( i=0; i<(int)node.number_of_dimensions; i++ )
   total_bytes *= node.dimension_values[i] ;
if( total_bytes == 0 ) {
   *error_return = ZERO_DIMENSIONS ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */

   /** check for need of data translation **/
ADFI_file_and_machine_compare( file_index, tokenized_data_type,
			       &formats_compare, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

	/** If there currently is NO data, allocate disk space for it **/
if( node.number_of_data_chunks == 0  ) {
   ADFI_file_malloc( file_index,
		total_bytes + TAG_SIZE + TAG_SIZE + DISK_POINTER_SIZE,
		&node.data_chunks, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

	/** initialize the new disk_space with zero's, then we'll
		write the partial data **/
   ADFI_write_data_chunk( file_index, &node.data_chunks, tokenized_data_type,
		file_bytes, total_bytes, 0, total_bytes, 0L,
		error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

	/** Record the modified the node-header **/
   node.number_of_data_chunks = 1 ;
   ADFI_write_node_header( file_index, &block_offset, &node, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
   } /* end if */
	/** If one data chunk, check to see if we need to add a second **/
else if( node.number_of_data_chunks == 1 ) {
	/** Get the data length **/
   ADFI_read_chunk_length( file_index, &node.data_chunks, tag,
		&end_of_chunk_tag, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;
   tag[TAG_SIZE] = '\0' ;

        /** Check start-of-chunk tag **/
   if( ADFI_stridx_c( tag, data_chunk_start_tag ) != 0 ) {
      *error_return = ADF_DISK_TAG_ERROR ;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */

	/** Point to the start of the data **/
   data_start.block = node.data_chunks.block ;
   data_start.offset = node.data_chunks.offset + TAG_SIZE + DISK_POINTER_SIZE ;
   ADFI_adjust_disk_pointer( &data_start, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

	/** See if the new data exceedes the existing data space **/
   chunk_total_bytes = end_of_chunk_tag.offset - data_start.offset +
	(end_of_chunk_tag.block - data_start.block) * DISK_BLOCK_SIZE ;

	/** If Data grew: Allocate more data-space and initialize to zero**/
   if( (long int) total_bytes > chunk_total_bytes ) {
	/** Allocate memory for the data-chunk-table, with an additional
	    entry in case we need to grow it **/
      data_chunk_table = (struct  DATA_CHUNK_TABLE_ENTRY *)
	    malloc( (node.number_of_data_chunks + 1 ) *
					sizeof( *data_chunk_table ) ) ;
      if( data_chunk_table == NULL ) {
         *error_return = MEMORY_ALLOCATION_FAILED ;
         CHECK_ADF_ABORT( *error_return ) ;
         } /* end if */

	/** Allocate a second data chunk **/
      total_bytes -= chunk_total_bytes ;
      ADFI_file_malloc( file_index,
		total_bytes + TAG_SIZE + TAG_SIZE + DISK_POINTER_SIZE,
		&new_block_offset, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

	/** Initialize the new data with zeros **/
      ADFI_write_data_chunk( file_index, &new_block_offset,
		tokenized_data_type, file_bytes, total_bytes, 0,
		total_bytes, 0L, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

	/** Allocate a data-chunk-table for two entries **/
      ADFI_file_malloc( file_index, 2 * TAG_SIZE + 5 * DISK_POINTER_SIZE,
		&dct_block_offset, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

	/** Write data-chunk-table to disk **/
      data_chunk_table[0].start.block = node.data_chunks.block ;
      data_chunk_table[0].start.offset = node.data_chunks.offset ;
      chunk_start.block = node.data_chunks.block ;
      chunk_start.offset = node.data_chunks.offset + TAG_SIZE ;
      ADFI_adjust_disk_pointer( &chunk_start, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
	   /** get the size of the data_chunk for the table end pointer **/
      ADFI_read_disk_pointer_from_disk( file_index,
		chunk_start.block, chunk_start.offset,
		&data_chunk_table[0].end, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
      data_chunk_table[1].start.block = new_block_offset.block ;
      data_chunk_table[1].start.offset = new_block_offset.offset ;
      chunk_start.block = new_block_offset.block ;
      chunk_start.offset = new_block_offset.offset + TAG_SIZE ;
      ADFI_adjust_disk_pointer( &chunk_start, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
	   /** get the size of the data_chunk for the table end pointer **/
      ADFI_read_disk_pointer_from_disk( file_index,
		chunk_start.block, chunk_start.offset,
		&data_chunk_table[1].end, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
      ADFI_write_data_chunk_table( file_index, &dct_block_offset,
		2, data_chunk_table, error_return ) ;

	/** Update node header with number of data-chunks = 2 and the
		pointer to the data-chunk-table **/
      node.data_chunks.block = dct_block_offset.block ;
      node.data_chunks.offset = dct_block_offset.offset ;
      node.number_of_data_chunks = 2 ;
      ADFI_write_node_header( file_index, &block_offset, &node, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */
   } /* end else if */
else { /** Multiple data chunks, check to see if we need to add one mode **/
	/** Allocate memory for the data-chunk-table, with an additional
	    entry in case we need to grow it **/
   data_chunk_table = (struct  DATA_CHUNK_TABLE_ENTRY *)
	    malloc( (node.number_of_data_chunks + 1 ) *
					sizeof( *data_chunk_table ) ) ;
   if( data_chunk_table == NULL ) {
      *error_return = MEMORY_ALLOCATION_FAILED ;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */

	/** Read in the table **/
   ADFI_read_data_chunk_table( file_index, &node.data_chunks,
                data_chunk_table, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

	/** looping on the data-chunks, look at the size of the chunks **/
   for( i=0; i<(int)node.number_of_data_chunks; i++ ) {
      current_bytes = (data_chunk_table[i].end.block -
	         data_chunk_table[i].start.block) * DISK_BLOCK_SIZE +
	         (data_chunk_table[i].end.offset -
		 data_chunk_table[i].start.offset) -
		 (TAG_SIZE + DISK_POINTER_SIZE) ;
      total_bytes -= current_bytes ;
      if( total_bytes <= 0 )
         break ;
      } /* end for */

	/** If we are out of data-chunks and have data left, allocate a
		new data-chunk in the file. **/
   if( total_bytes > 0 ) {
		/** Write data-chunk-table to disk **/

		/** allocate data space in the file **/
      ADFI_file_malloc( file_index, 2 * TAG_SIZE + DISK_POINTER_SIZE +
	total_bytes, &data_chunk_table[ node.number_of_data_chunks ].start,
			error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

      data_chunk_table[ node.number_of_data_chunks ].end.block =
	    	data_chunk_table[ node.number_of_data_chunks ].start.block ;
      data_chunk_table[ node.number_of_data_chunks ].end.offset =
	    	data_chunk_table[ node.number_of_data_chunks ].start.offset +
		TAG_SIZE + DISK_POINTER_SIZE + total_bytes ;
      ADFI_adjust_disk_pointer(
	   &data_chunk_table[ node.number_of_data_chunks ].end, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

		/** allocate space for the new data-chunk-entry-table **/
      ADFI_file_malloc( file_index, 2 * TAG_SIZE + 
		(2 * (node.number_of_data_chunks + 1) + 1) * DISK_POINTER_SIZE,
		&dct_block_offset, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

      ADFI_write_data_chunk_table( file_index, &dct_block_offset,
		node.number_of_data_chunks+1, data_chunk_table, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;

	/** Initialize the new data chunk to zeros **/
      ADFI_write_data_chunk( file_index,
  	        &data_chunk_table[node.number_of_data_chunks ].start,
	        tokenized_data_type, file_bytes, total_bytes, 0,
	        total_bytes, 0L, error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
		/** Free the old data-chunk-table **/
      ADFI_file_free( file_index, &node.data_chunks, 0, error_return ) ;
            CHECK_ADF_ABORT( *error_return ) ;
	    
		/** Update node header with number of data-chunks++ and the
			pointer to the data-chunk-table **/
      node.number_of_data_chunks++ ; 
      node.data_chunks.block = dct_block_offset.block ;
      node.data_chunks.offset = dct_block_offset.offset ;
      ADFI_write_node_header( file_index, &block_offset, &node,
				error_return ) ;
      CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */
   } /* end else */

	/** Do single data-chunks here... **/
if( node.number_of_data_chunks == 1 ) {
	/** Point to the start of the data **/
   block_offset.block = node.data_chunks.block ;
   block_offset.offset = node.data_chunks.offset + TAG_SIZE +
	DISK_POINTER_SIZE + disk_offset * file_bytes ;
   ADFI_adjust_disk_pointer( &block_offset, error_return ) ;
   CHECK_ADF_ABORT( *error_return ) ;

	/** Setup initial indexing **/
   for( i=0; i<(int)node.number_of_dimensions; i++ )
      current_disk[i] = s_start[i] ;
   for( i=0; i<m_num_dims; i++ )
      current_memory[i] = m_start[i] ;
   
	/** Adjust data pointer **/
   if( memory_offset != 0 )
      data += memory_offset * memory_bytes ;

   for( i=0; i<total_disk_elements; i++ ) {
	/** Put the data to disk **/
      if ( block_offset.offset > DISK_BLOCK_SIZE ) {
        ADFI_adjust_disk_pointer( &block_offset, error_return ) ;
        CHECK_ADF_ABORT( *error_return ) ;
      }

   /** Here is where we need to check for spanning multiple data-chunks **/

			/** Put the data out to disk **/
      if( formats_compare ) {  /* directly */
         ADFI_write_file( file_index, block_offset.block, block_offset.offset,
               file_bytes, (char *)data, error_return ) ;
         CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */
      else {                   /* translated */
         ADFI_write_data_translated( file_index, block_offset.block,
               block_offset.offset, tokenized_data_type, file_bytes,
               file_bytes, (char *)data, error_return ) ;
         CHECK_ADF_ABORT( *error_return ) ;
      } /* end else */

   /** Increment disk/memory pointers, for the special case of one dimensional
       data we will a simple increment to maximize the throught. Thus for
       block writes you can temporarily change to 1D for the read to
       improve efficiency. Note total size shouldn't change!! **/
      if( i < total_disk_elements - 1 ) {
        if ( node.number_of_dimensions == 1 ) {
	  disk_offset = s_stride[0];
	  current_disk[0] += disk_offset;
	  if ( current_disk[0] > s_end[0] ) current_disk[0] = s_end[0] ;
	} /* end if */ 
	else {
  	  ADFI_increment_array(
		    node.number_of_dimensions, node.dimension_values,
                    s_start, s_end, s_stride, current_disk, &disk_offset,
                    error_return ) ;
           CHECK_ADF_ABORT( *error_return ) ;
	} /* end else */ 

        if ( m_num_dims == 1 ) {
	  memory_offset = m_stride[0];
	  current_memory[0] += disk_offset;
	  if ( current_memory[0] > m_end[0] ) current_memory[0] = m_end[0] ;
	} /* end if */ 
	else {
           ADFI_increment_array(
	        (unsigned int)m_num_dims, (unsigned int* )m_dims,
		m_start, m_end, m_stride,
                current_memory, &memory_offset, error_return ) ;
           CHECK_ADF_ABORT( *error_return ) ;
	} /* end else */ 

         block_offset.offset += disk_offset * file_bytes ;
         if ( block_offset.offset > DISK_BLOCK_SIZE ) {
           ADFI_adjust_disk_pointer( &block_offset, error_return ) ;
           CHECK_ADF_ABORT( *error_return ) ;
           } /* end if */
	 
		/** Adjust data pointer **/
         data += memory_offset * memory_bytes ;
         } /* end if */
      } /* end for */
   } /* end if */
else {
	/** Point to the start of the data **/
   current_chunk = 0 ;
   past_chunk_sizes = 0 ;
   relative_offset = disk_offset * file_bytes ;
   current_chunk_size = (data_chunk_table[ current_chunk ].end.block -
	data_chunk_table[ current_chunk ].start.block) * DISK_BLOCK_SIZE +
	(data_chunk_table[ current_chunk ].end.offset -
	data_chunk_table[ current_chunk ].start.offset) -
	(TAG_SIZE + DISK_POINTER_SIZE) ;

	/** Setup initial indexing **/
   for( i=0; i<(int)node.number_of_dimensions; i++ )
      current_disk[i] = s_start[i] ;
   for( i=0; i<m_num_dims; i++ )
      current_memory[i] = m_start[i] ;
   
	/** Adjust data pointer **/
   if( memory_offset != 0 )
      data += memory_offset * memory_bytes ;

   for( i=0; i<total_disk_elements; i++ ) {
      while( relative_offset >= past_chunk_sizes + current_chunk_size ) {
	 if( ++current_chunk >= node.number_of_data_chunks ) {
	    *error_return = INCOMPLETE_DATA ;
            CHECK_ADF_ABORT( *error_return ) ;
	    } /* end if */
	 else {
	    past_chunk_sizes += current_chunk_size ;
   	    current_chunk_size = (data_chunk_table[ current_chunk ].end.block -
	      data_chunk_table[ current_chunk ].start.block) * DISK_BLOCK_SIZE +
		(data_chunk_table[ current_chunk ].end.offset -
		data_chunk_table[ current_chunk ].start.offset) -
		(TAG_SIZE + DISK_POINTER_SIZE) ;
	    } /* end else */
	 } /* end while */

	/** Put the data to disk **/
      relative_block.block = data_chunk_table[ current_chunk ].start.block ;
      relative_block.offset = data_chunk_table[ current_chunk ].start.offset +
		(TAG_SIZE + DISK_POINTER_SIZE) +
		(relative_offset - past_chunk_sizes) ;
      if ( relative_block.offset > DISK_BLOCK_SIZE ) {
        ADFI_adjust_disk_pointer( &relative_block, error_return ) ;
        CHECK_ADF_ABORT( *error_return ) ;
        } /* end if */

			/** Put the data out to disk **/
      if( formats_compare ) {  /* directly */
         ADFI_write_file( file_index,
               relative_block.block, relative_block.offset,
               file_bytes, (char *)data, error_return ) ;
         CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */
      else {                   /* translated */
         ADFI_write_data_translated( file_index, relative_block.block,
               relative_block.offset, tokenized_data_type, file_bytes,
               file_bytes, (char *)data, error_return ) ;
         CHECK_ADF_ABORT( *error_return ) ;
      } /* end if */

	/** Increment disk and memory pointers **/
      if( i < total_disk_elements - 1 ) {
        if ( node.number_of_dimensions == 1 ) {
	  disk_offset = s_stride[0];
	  current_disk[0] += disk_offset;
	  if ( current_disk[0] > s_end[0] ) current_disk[0] = s_end[0] ;
	} /* end if */
	else {
          ADFI_increment_array(
		node.number_of_dimensions, node.dimension_values,
                s_start, s_end, s_stride, current_disk, &disk_offset,
                error_return ) ;
          CHECK_ADF_ABORT( *error_return ) ;
	}  /* end else */

        relative_offset += disk_offset * file_bytes ;

        if ( m_num_dims == 1 ) {
          memory_offset = m_stride[0];
          current_memory[0] += disk_offset;
          if ( current_memory[0] > m_end[0] ) current_memory[0] = m_end[0] ;
        } /* end if */ 
        else {
          ADFI_increment_array(
		(unsigned int)m_num_dims, (unsigned int* )m_dims,
		m_start, m_end, m_stride,
                current_memory, &memory_offset, error_return ) ;
          CHECK_ADF_ABORT( *error_return ) ;
	}  /* end else */

		/** Adjust data pointer **/
         data += memory_offset * memory_bytes ;
         } /* end if */
      } /* end for */
   } /* end else */

if( data_chunk_table != 0L )
   free( data_chunk_table ) ;

	/** Finally, update modification date **/
ADFI_write_modification_date( file_index, error_return ) ;
CHECK_ADF_ABORT( *error_return ) ;

} /* end of ADF_Write_Data */
/* end of file ADF_Write_Data.c */
/* end of combine 2.0 */