File: voxel_loop.c

package info (click to toggle)
minc 2.1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,160 kB
  • sloc: ansic: 82,507; sh: 10,666; yacc: 1,187; perl: 612; makefile: 586; lex: 319
file content (3466 lines) | stat: -rw-r--r-- 129,740 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
/* ----------------------------- MNI Header -----------------------------------
@NAME       : voxel_loop.c
@DESCRIPTION: Routines to loop through a file doing an operation on a single
              voxel.
@METHOD     : 
@GLOBALS    : 
@CREATED    : January 10, 1994 (Peter Neelin)
@MODIFIED   : 
 * $Log: voxel_loop.c,v $
 * Revision 6.14  2010-03-02 23:24:40  rotor
 *  * libsrc/hdf_convenience.c: removed spurious debug output
 *  * libsrc/minc.h: replaced MAX_NC_OPEN with 32
 *  * libsrc/voxel_loop.c: replaced MAX_NC_OPEN with MI_MAX_NUM_ICV
 *
 * Revision 6.13  2010-03-02 12:23:14  rotor
 *  * ported HDF calls to 1.8.x
 *  * Makefile.am: updated for minccmp
 *
 * Revision 6.12  2008/01/17 02:33:02  rotor
 *  * removed all rcsids
 *  * removed a bunch of ^L's that somehow crept in
 *  * removed old (and outdated) BUGS file
 *
 * Revision 6.11  2008/01/13 09:38:54  stever
 * Avoid compiler warnings about functions and variables that are defined
 * but not used.  Remove some such functions and variables,
 * conditionalize some, and move static declarations out of header files
 * into C files.
 *
 * Revision 6.10  2008/01/13 04:30:28  stever
 * Add braces around static initializers.
 *
 * Revision 6.9  2008/01/12 19:08:14  stever
 * Add __attribute__ ((unused)) to all rcsid variables.
 *
 * Revision 6.8  2006/04/09 15:40:25  bert
 * Minor change
 *
 * Revision 6.7  2005/08/26 21:04:58  bert
 * Use #if rather than #ifdef with MINC2 symbol
 *
 * Revision 6.6  2004/11/01 22:23:14  bert
 * Get rid of minc_def.h, use standard MALLOC() macro
 *
 * Revision 6.5  2004/10/15 13:46:52  bert
 * Minor changes for Windows compatibility
 *
 * Revision 6.4  2004/04/27 15:43:29  bert
 * Support MINC 2.0 format
 *
 * Revision 6.3  2003/11/14 16:52:24  stever
 * More last-minute fixes.
 *
 * Revision 6.2  2003/09/18 16:49:46  bert
 * Use fabs instead of ABS
 *
 * Revision 6.1  2002/01/14 21:28:26  neelin
 * Moved nd_loop, voxel_loop, ParseArgv and time_stamp from ../progs/Proglib
 * in order to include them in the main minc library.
 *
 * Revision 6.9  2002/01/14 20:02:39  neelin
 * Force the input buffers to have a minimum size so that large images do
 * not force excessive reading of the input file.
 *
 * Revision 6.8  2001/11/28 18:39:16  neelin
 * Added get_info_vxoel_index to allow users to get the full multi-dimensional
 * file index of the current voxel.
 * Modifications to allow arg_string to be NULL.
 *
 * Revision 6.7  2001/09/18 15:32:27  neelin
 * Create image variable last to allow big images and to fix compatibility
 * problems with 2.3 and 3.x.
 *
 * Revision 6.6  2001/08/16 16:41:32  neelin
 * Added library functions to handle reading of datatype, sign and valid range,
 * plus writing of valid range and setting of default ranges. These functions
 * properly handle differences between valid_range type and image type. Such
 * difference can cause valid data to appear as invalid when double to float
 * conversion causes rounding in the wrong direction (out of range).
 * Modified voxel_loop, volume_io and programs to use these functions.
 *
 * Revision 6.5  2001/08/16 13:32:27  neelin
 * Partial fix for valid_range of different type from image (problems
 * arising from double to float conversion/rounding). NOT COMPLETE.
 *
 * Revision 6.4  2001/04/24 13:38:40  neelin
 * Replaced NC_NAT with MI_ORIGINAL_TYPE.
 *
 * Revision 6.3  2001/04/17 18:40:15  neelin
 * Modifications to work with NetCDF 3.x
 * In particular, changed NC_LONG to NC_INT (and corresponding longs to ints).
 * Changed NC_UNSPECIFIED to NC_NAT.
 * A few fixes to the configure script.
 *
 * Revision 6.2  2000/09/19 14:36:05  neelin
 * Added ability for caller to specify functions for allocating and freeing
 * voxel buffers used in loop. This is particularly useful for embedding
 * the voxel_loop code in other programs, such as Python, which manage memory
 * in their own way.
 *
 * Revision 6.1  1999/10/19 14:45:15  neelin
 * Fixed Log subsitutions for CVS
 *
 * Revision 6.0  1997/09/12 13:23:41  neelin
 * Release of minc version 0.6
 *
 * Revision 5.0  1997/08/21  13:24:41  neelin
 * Release of minc version 0.5
 *
 * Revision 4.2  1997/06/20  13:58:35  neelin
 * Fixed bug: when doing accumulation with no output file and with
 * 4D input (or more), had problem setting input start vector. This broke
 * mincconcat for 4D input files.
 *
 * Revision 4.1  1997/05/22  12:41:40  neelin
 * Loosened up checking of start coordinates so that we look at error
 * relative to the total extent of the volume (nelements*step) instead of
 * relative to start value (which may be close to zero).
 *
 * Revision 4.0  1997/05/07  20:00:50  neelin
 * Release of minc version 0.4
 *
 * Revision 3.0  1995/05/15  19:31:35  neelin
 * Release of minc version 0.3
 *
 * Revision 1.6  1995/05/11  12:31:29  neelin
 * Removed error messages from ncattdel.
 *
 * Revision 1.5  1995/05/02  16:05:32  neelin
 * Fixed bug in handling more than 30 files (needed to detach from icv).
 * Added more checking of dimensions.
 * Fixed bug in allocation of space for global max/min.
 *
 * Revision 1.4  1995/05/01  20:04:50  neelin
 * Fixed memory leak - not freeing global_minimum/maximum.
 *
 * Revision 1.3  1995/03/21  15:33:07  neelin
 * Changed call to voxel_function to always use proper vector length and
 * set num_voxels to the number of voxels, not multiplying by vector length.
 *
 * Revision 1.2  1995/03/21  14:06:39  neelin
 * Improved interface and added lots of functionality (much for the benefit
 * of mincconcat).
 *
 * Revision 1.1  94/12/14  10:17:19  neelin
 * Initial revision
 * 
@COPYRIGHT  :
              Copyright 1993 Peter Neelin, McConnell Brain Imaging Centre, 
              Montreal Neurological Institute, McGill University.
              Permission to use, copy, modify, and distribute this
              software and its documentation for any purpose and without
              fee is hereby granted, provided that the above copyright
              notice appear in all copies.  The author and McGill University
              make no representations about the suitability of this
              software for any purpose.  It is provided "as is" without
              express or implied warranty.
---------------------------------------------------------------------------- */

#include "minc_private.h"
#include <float.h>
#include <math.h>
#include "voxel_loop.h"
#include "nd_loop.h"

/* Minimum number of voxels to put in a buffer. If this is too small,
   then for large images excessive reading can result. If it is
   too large, then for large images too much memory will be used. */
#define MIN_VOXELS_IN_BUFFER 1024

/* Default ncopts values for error handling */
#define NC_OPTS_VAL NC_VERBOSE | NC_FATAL

/* Epsilon for coordinate comparisons */
#define COORD_EPSILON (FLT_EPSILON * 10.0)

/* Typedefs */
typedef struct Loopfile_Info Loopfile_Info;

/* Structure definitions */
struct Loop_Info {
   int current_file;
   int current_index;
   long start[MAX_VAR_DIMS];
   long count[MAX_VAR_DIMS];
   long dimvoxels[MAX_VAR_DIMS];   /* Number of voxels skipped by a step
                                      of one in each dimension */
   Loopfile_Info *loopfile_info;
};

struct Loop_Options {
   int verbose;
   int clobber;
   nc_type datatype;
   int is_signed;
   double valid_range[2];
   int max_open_files;
   int check_all_input_dim_info;
   int convert_input_to_scalar;
   int output_vector_size;        /* 0 = same as input size */
   int input_mincid;
   long total_copy_space;
   char *loop_dimension;
   int num_all_inputs;
   VoxelInputFileFunction input_file_function;
   VoxelOutputFileFunction output_file_function;
   int copy_all_header_info;
   int do_accumulate;
   int num_extra_buffers;
   VoxelStartFunction start_function;
   VoxelFinishFunction finish_function;
   VoxelFunction voxel_function;
   void *caller_data;
   Loop_Info *loop_info;
   int is_floating_type;
   AllocateBufferFunction allocate_buffer_function;
#if MINC2
   int v2format;
#endif /* MINC2 */
};

struct Loopfile_Info {
   int cflags;			/* creation flags */
   int num_input_files;
   int num_output_files;
   char **input_files;
   char **output_files;
   int input_all_open;
   int output_all_open;
   int *input_mincid;
   int *output_mincid;
   int *input_icvid;
   int *output_icvid;
   int current_input_file_number;
   int current_output_file_number;
   int headers_only;
   int want_headers_only;
   int sequential_access;
   int can_open_all_input;
};

/* Function prototypes */
PRIVATE int get_loop_dim_size(int inmincid, Loop_Options *loop_options);
PRIVATE void translate_input_coords(int inmincid,
                                    long chunk_cur[], long input_cur[],
                                    long chunk_curcount[], 
                                    long input_curcount[],
                                    int *loop_dim_index,
                                    Loop_Options *loop_options);
PRIVATE void check_input_files(Loop_Options *loop_options,
                               Loopfile_Info *loopfile_info);
PRIVATE int input_image_varinq(int mincid, int imgid, char *name,
                               nc_type *datatype, int *ndims, int dim[],
                               int *natts, Loop_Options *loop_options);
PRIVATE void get_dim_info(int mincid, int *ndims, long size[], 
                          char dimname[][MAX_NC_NAME],
                          double start[], double step[],
                          double dircos[][3], int is_regular[],
                          Loop_Options *loop_options);
PRIVATE void setup_output_files(Loop_Options *loop_options, 
                                Loopfile_Info *loopfile_info,
                                char *arg_string);
PRIVATE long get_vector_length(int mincid, Loop_Options *loop_options);
PRIVATE void setup_variables(int inmincid, int outmincid,
                             int output_curfile,
                             char *arg_string, Loop_Options *loop_options);
PRIVATE void update_history(int mincid, char *arg_string);
PRIVATE void setup_icvs(Loop_Options *loop_options, 
                        Loopfile_Info *loopfile_info);
PRIVATE void do_voxel_loop(Loop_Options *loop_options,
                           Loopfile_Info *loopfile_info);
PRIVATE void setup_looping(Loop_Options *loop_options, 
                           Loopfile_Info *loopfile_info,
                           int *ndims,
                           long block_start[], long block_end[], 
                           long block_incr[], long *block_num_voxels,
                           long chunk_incr[], long *chunk_num_voxels);
PRIVATE void initialize_file_and_index(Loop_Options *loop_options, 
                                       Loopfile_Info *loopfile_info,
                                       int do_loop,
                                       int *ifile, int *dim_index,
                                       int *dummy_index);
PRIVATE int finish_file_and_index(Loop_Options *loop_options, 
                                  Loopfile_Info *loopfile_info,
                                  int do_loop,
                                  int ifile, int dim_index,
                                  int dummy_index);
PRIVATE void increment_file_and_index(Loop_Options *loop_options, 
                                      Loopfile_Info *loopfile_info,
                                      int do_loop,
                                      int *ifile, int *dim_index,
                                      int *dummy_index);
PRIVATE Loopfile_Info *initialize_loopfile_info(int num_input_files,
                                                char *input_files[],
                                                int num_output_files,
                                                char *output_files[],
                                                Loop_Options *loop_options);
PRIVATE void cleanup_loopfile_info(Loopfile_Info *loopfile_info);
PRIVATE int get_input_numfiles(Loopfile_Info *loopfile_info);
PRIVATE int get_output_numfiles(Loopfile_Info *loopfile_info);
PRIVATE char *get_input_filename(Loopfile_Info *loopfile_info, int file_num);
PRIVATE void set_input_headers_only(Loopfile_Info *loopfile_info,
                                    int headers_only);
PRIVATE void set_input_sequential(Loopfile_Info *loopfile_info,
                                  int sequential_access);
PRIVATE int get_input_mincid(Loopfile_Info *loopfile_info,
                             int file_num);
PRIVATE int get_output_mincid(Loopfile_Info *loopfile_info,
                              int file_num);
PRIVATE int create_output_file(Loopfile_Info *loopfile_info,
                               int file_num);
PRIVATE int get_input_icvid(Loopfile_Info *loopfile_info,
                            int file_num);
PRIVATE int get_output_icvid(Loopfile_Info *loopfile_info,
                             int file_num);
PRIVATE int create_input_icvid(Loopfile_Info *loopfile_info,
                               int file_num);
PRIVATE int create_output_icvid(Loopfile_Info *loopfile_info,
                                int file_num);
PRIVATE Loop_Info *create_loop_info(void);
PRIVATE void initialize_loop_info(Loop_Info *loop_info);
PRIVATE void free_loop_info(Loop_Info *loop_info);
PRIVATE void set_info_shape(Loop_Info *loop_info, long start[], long count[]);
PRIVATE void set_info_current_file(Loop_Info *loop_info, int current_file);
PRIVATE void set_info_current_index(Loop_Info *loop_info, int current_index);
PRIVATE void set_info_loopfile_info(Loop_Info *loop_info, 
                                    Loopfile_Info *loopfile_info);

/* ----------------------------- MNI Header -----------------------------------
@NAME       : voxel_loop
@INPUT      : num_input_files - number of input files.
              input_files - array of names of input files.
              num_output_files - number of output files.
              output_files - array of names of output files.
              arg_string - string for history.
              loop_options - pointer to structure containing loop options.
              voxel_function - user function to process a group of voxels.
                 See description in header file.
              caller_data - data that will be passed to voxel_function
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to loop through the voxels of a file and call a function
              to operate on each voxel.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 10, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void voxel_loop(int num_input_files, char *input_files[], 
                       int num_output_files, char *output_files[], 
                       char *arg_string, 
                       Loop_Options *loop_options,
                       VoxelFunction voxel_function, void *caller_data)
{
   Loopfile_Info *loopfile_info;
   int need_to_free_loop_options;
   int old_ncopts;

   (void)fprintf(stderr, "About to loop, max_buffer is %d\n", loop_options->total_copy_space);
   
   /* Save ncopts and set it to default value */
   old_ncopts = ncopts;
   ncopts = NC_OPTS_VAL;

   /* Check that there is at least one input file */
   if (num_input_files < 1) {
      (void) fprintf(stderr, "There must be at least one input file.\n");
      exit(EXIT_FAILURE);
   }
   if (num_output_files < 0) {
      (void) fprintf(stderr, "Negative number of output files!\n");
      exit(EXIT_FAILURE);
   }

   /* Initialize loop options if needed */
   need_to_free_loop_options = FALSE;
   if (loop_options == NULL) {
      loop_options = create_loop_options();
      need_to_free_loop_options = TRUE;
   }
   loop_options->voxel_function = voxel_function;
   loop_options->caller_data = caller_data;

   /* Make sure that Loop_Info structure is initialized */
   initialize_loop_info(loop_options->loop_info);

   /* Initialize looping info */
   loopfile_info = initialize_loopfile_info(num_input_files, input_files,
                                            num_output_files, output_files,
                                            loop_options);

   /* Check that input files match */
   set_input_headers_only(loopfile_info, TRUE);
   check_input_files(loop_options, loopfile_info);
   set_input_headers_only(loopfile_info, FALSE);

   /* Set up variables in output file */
   setup_output_files(loop_options, loopfile_info, arg_string);

   /* Setup icv's */
   setup_icvs(loop_options, loopfile_info);

   /* Loop through the voxels */
   do_voxel_loop(loop_options, loopfile_info);

   /* Clean up looping info */
   cleanup_loopfile_info(loopfile_info);

   /* Free loop options if needed */
   if (need_to_free_loop_options) {
      free_loop_options(loop_options);
   }

   /* Restore ncopts */
   ncopts = old_ncopts;
                 
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_loop_dim_size
@INPUT      : inmincid - input minc id
              loop_options - Options for loops
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to get the size of the looping dimension for the given
              input file
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 24, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE int get_loop_dim_size(int inmincid, Loop_Options *loop_options)
{
   int dimid;
   long dim_length;
   int ndims, dim[MAX_VAR_DIMS];
   int found;
   int idim;

   /* Look for dimension */
   dimid = MI_ERROR;
   if (loop_options->loop_dimension != NULL) {
      ncopts = 0;
      dimid = ncdimid(inmincid, loop_options->loop_dimension);
      ncopts = NC_OPTS_VAL;
   }
   if (dimid == MI_ERROR) return 1;
   (void) ncdiminq(inmincid, dimid, NULL, &dim_length);

   /* Get image variable info */
   (void) ncvarinq(inmincid, ncvarid(inmincid, MIimage), NULL, NULL, 
                   &ndims, dim, NULL);

   /* Check to see if the dimension subscripts the image */
   found = FALSE;
   for (idim=0; idim < ndims; idim++) {
      if (dimid == dim[idim]) found = TRUE;
   }

   /* Return appropriate value */
   if (found)
      return dim_length;
   else
      return 1;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : translate_input_coords
@INPUT      : inmincid - input minc id
              chunk_cur - start for current chunk
              chunk_curcount - count for current chunk
              loop_options - Options for loops
@OUTPUT     : input_cur - start for input file
              input_curcount - count for input file
              loop_dim_index - index in input_cur for looping dimension
@RETURNS    : (nothing)
@DESCRIPTION: Routine to translate the input hyperslab coords from those 
              excluding the looping dimension to ones appropriate for the 
              input file. Also returns the index (offset in input_cur) for 
              the looping dimension (so that the caller can modify this 
              index).
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 24, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void translate_input_coords(int inmincid,
                                    long chunk_cur[], long input_cur[],
                                    long chunk_curcount[], 
                                    long input_curcount[],
                                    int *loop_dim_index,
                                    Loop_Options *loop_options)
{
   int dimid;
   int ndims, dim[MAX_VAR_DIMS];
   int idim, jdim;

   /* Look for dimension */
   dimid = MI_ERROR;
   if (loop_options->loop_dimension != NULL) {
      ncopts = 0;
      dimid = ncdimid(inmincid, loop_options->loop_dimension);
      ncopts = NC_OPTS_VAL;
   }

   /* Get image variable info */
   (void) ncvarinq(inmincid, ncvarid(inmincid, MIimage), NULL, NULL, 
                   &ndims, dim, NULL);

   /* Copy the hyperslab coordinates and get the index */
   *loop_dim_index = ndims;
   jdim = 0;
   for (idim=0; idim < ndims; idim++) {
      if (dimid != dim[idim]) {
         input_cur[idim] = chunk_cur[jdim];
         input_curcount[idim] = chunk_curcount[jdim];
         jdim++;
      }
      else {
         input_cur[idim] = 0;
         input_curcount[idim] = 1;
         *loop_dim_index = idim;
      }
   }

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : check_input_files
@INPUT      : loop_options - Options for loops
              loopfile_info - Information describing looping stuff and files
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to check input files for consistency.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void check_input_files(Loop_Options *loop_options,
                               Loopfile_Info *loopfile_info)
{
   int ifile, idim, jdim;
   int first_ndims, ndims;
   int input_mincid;
   long first_size[MAX_VAR_DIMS], size[MAX_VAR_DIMS];
   char first_dimname[MAX_VAR_DIMS][MAX_NC_NAME];
   char dimname[MAX_VAR_DIMS][MAX_NC_NAME];
   double extent;
   double first_start[MAX_VAR_DIMS], start[MAX_VAR_DIMS];
   double first_step[MAX_VAR_DIMS], step[MAX_VAR_DIMS];
   double start_diff, step_diff;
   double first_dircos[MAX_VAR_DIMS][3], dircos[MAX_VAR_DIMS][3];
   double dircos_diff, dircos_cumdiff;
   int first_is_regular[MAX_VAR_DIMS], is_regular[MAX_VAR_DIMS];

   /* Keep track of number of inputs (files and looping dimension) */
   loop_options->num_all_inputs = 0;

   /* Loop over files. For the first file, we only get the dimension info, 
      we don't check it. */
   for (ifile = 0; ifile < get_input_numfiles(loopfile_info); ifile++) {

      /* Get mincid */
      input_mincid = get_input_mincid(loopfile_info, ifile);

      /* Add up number of inputs */
      loop_options->num_all_inputs += 
         get_loop_dim_size(input_mincid, loop_options);

      /* Get dimension information for this file */
      if (ifile == 0) {
         get_dim_info(input_mincid, &first_ndims, first_size, first_dimname,
                      first_start, first_step, first_dircos,
                      first_is_regular, loop_options);
      }
      else {
         get_dim_info(input_mincid, &ndims, size, dimname, start, step,
                      dircos, is_regular, loop_options);
         
         /* Check number of dimensions */
         if (ndims != first_ndims) {
            (void) fprintf(stderr, 
               "Files %s and %s have different numbers of dimensions\n",
                           get_input_filename(loopfile_info,ifile),
                           get_input_filename(loopfile_info,0));
            exit(EXIT_FAILURE);
         }

         /* Loop over dimensions */
         for (idim = 0; idim < first_ndims; idim++) {

            /* Check dimension sizes */
            if (size[idim] != first_size[idim]) {
               (void) fprintf(stderr, 
                  "Files %s and %s have different sizes of dimensions\n",
                              get_input_filename(loopfile_info,ifile),
                              get_input_filename(loopfile_info,0));
               exit(EXIT_FAILURE);
            }

            /* Check optional dimension stuff */
            if (loop_options->check_all_input_dim_info) {

               /* Check names */
               if (strcmp(dimname[idim], first_dimname[idim]) != 0) {
                  (void) fprintf(stderr, 
                     "Files %s and %s have different dimension names\n",
                                 get_input_filename(loopfile_info,ifile),
                                 get_input_filename(loopfile_info,0));
                  exit(EXIT_FAILURE);
               }

               /* Check coordinates */
               start_diff = start[idim] - first_start[idim];
               extent = ((double) first_size[idim]) * first_step[idim];
               if (extent != 0.0) start_diff /= extent;
               step_diff = step[idim] - first_step[idim];
               if (first_step[idim] != 0.0) step_diff /= first_step[idim];
               dircos_cumdiff = 0.0;
               for (jdim=0; jdim < 3; jdim++) {
                  dircos_diff = first_dircos[idim][jdim] - dircos[idim][jdim];
                  if (first_dircos[idim][jdim] != 0.0)
                     dircos_diff /= first_dircos[idim][jdim];
                  dircos_cumdiff += fabs(dircos_diff);
               }
               if (fabs(start_diff) > COORD_EPSILON) {
                  (void) fprintf(stderr, 
                     "Files %s and %s have different start coordinates (%s)\n",
                                 get_input_filename(loopfile_info,ifile),
                                 get_input_filename(loopfile_info,0),
                                 first_dimname[idim]);
                  exit(EXIT_FAILURE);
               }
               if (fabs(step_diff) > COORD_EPSILON) {
                  (void) fprintf(stderr, 
                     "Files %s and %s have different step coordinates (%s)\n",
                                 get_input_filename(loopfile_info,ifile),
                                 get_input_filename(loopfile_info,0),
                                 first_dimname[idim]);
                  exit(EXIT_FAILURE);
               }
               if (dircos_cumdiff > COORD_EPSILON) {
                  (void) fprintf(stderr, 
                     "Files %s and %s have different direction cosines (%s)\n",
                                 get_input_filename(loopfile_info,ifile),
                                 get_input_filename(loopfile_info,0),
                                 first_dimname[idim]);
                  exit(EXIT_FAILURE);
               }
               if (first_is_regular[idim] != is_regular[idim]) {
                  (void) fprintf(stderr, 
                     "Files %s and %s have different coordinate spacings (%s)\n",
                                 get_input_filename(loopfile_info,ifile),
                                 get_input_filename(loopfile_info,0),
                                 first_dimname[idim]);
                  exit(EXIT_FAILURE);
               }

            }      /* If check all dimension info */

         }      /* End of loop over dimensions */

      }      /* End of if ifile == 0 else */

      /* Call the user's function if needed */
      if (loop_options->input_file_function != NULL) {
         set_info_current_file(loop_options->loop_info, ifile);
         set_info_loopfile_info(loop_options->loop_info, loopfile_info);
         loop_options->input_file_function(loop_options->caller_data,
                                           input_mincid, ifile,
                                           loop_options->loop_info);
         set_info_loopfile_info(loop_options->loop_info, NULL);
      }

   }      /* End of loop over files */   
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : input_image_varinq
@INPUT      : mincid - Minc id of file
              imgid - id of image variable
              loop_options - structure containing looping options
@OUTPUT     : name - name of variable
              datatype - type of variable
              ndims - number of dimensions
              dim - array of dimension ids
              natts - number of attributes
@RETURNS    : MI_ERROR if an error occurs
@DESCRIPTION: Routine to call ncvarinq for the image variable, suppressing
              the dimension specified in loop_options.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 20, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE int input_image_varinq(int mincid, int imgid, char *name,
                               nc_type *datatype, int *ndims, int dim[],
                               int *natts, Loop_Options *loop_options)
{
   int dimid;
   int old_ncopts;
   int idim, jdim;
   int status;
   int nimgdims;
   char dimname[MAX_NC_NAME];

   /* Get loop dimension id */
   dimid = MI_ERROR;
   if (loop_options->loop_dimension != NULL) {
      old_ncopts = ncopts; ncopts = 0;
      dimid = ncdimid(mincid, loop_options->loop_dimension);
      ncopts = old_ncopts;
   }

   /* Call ncvarinq. If there is no loop dim, or an error occurred, then
      return. */
   status = ncvarinq(mincid, imgid, name, datatype, ndims, dim, natts);
   if ((dimid == MI_ERROR) || (status == MI_ERROR)) 
      return status;

   /* Get number of image dimensions */
   nimgdims = 2;
   if (*ndims > 0) {
      (void) ncdiminq(mincid, dim[*ndims-1], dimname, NULL);
      if (strcmp(dimname, MIvector_dimension) == 0)
         nimgdims++;
   }

   /* Look for the loop dimension */
   jdim = 0;
   for (idim=0; idim < *ndims; idim++) {
      if (dim[idim] != dimid) {
         dim[jdim] = dim[idim];
         jdim++;
      }
      else if (idim >= *ndims-nimgdims) {
         (void) fprintf(stderr, 
                        "Don't use an image dimension as a loop dimension.\n");
         exit(EXIT_FAILURE);
      }
   }

   /* Save number of dimensions */
   *ndims = jdim;

   return status;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_dim_info
@INPUT      : mincid - Minc id of file
@OUTPUT     : ndims - number of dimensions
              size  - array of sizes of dimensions
              dimname - array of dimension names
              start - array of starts for dimensions
              step  - array of steps for dimensions
              dircos - array of direction cosines
              is_regular - array of flags indicating whether dimension is
                 regularly spaced or not
              loop_options - looping options
@RETURNS    : (nothing)
@DESCRIPTION: Routine to get dimension information for a file.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void get_dim_info(int mincid, int *ndims, long size[], 
                          char dimname[][MAX_NC_NAME],
                          double start[], double step[],
                          double dircos[][3], int is_regular[],
                          Loop_Options *loop_options)
{
   int imgid, varid;
   int idim, jdim;
   int dim[MAX_VAR_DIMS];
   int att_length;
   char string[MAX_NC_NAME];
   char *thename;
   int old_ncopts;
   enum {XAXIS, YAXIS, ZAXIS, OAXIS} dimtype;
   static double default_dircos[][3] = {
      { 1.0, 0.0, 0.0 },
      { 0.0, 1.0, 0.0 },
      { 0.0, 0.0, 1.0 },
      { 0.0, 0.0, 0.0 }
   };
   char regstring[MI_MAX_ATTSTR_LEN];

   /* Get image variable info */
   imgid = ncvarid(mincid, MIimage);
   (void) input_image_varinq(mincid, imgid, NULL, NULL, ndims, dim, NULL,
                             loop_options);

   /* Loop through dimensions */
   for (idim=0; idim < *ndims; idim++) {
      if (dimname == NULL) thename = string;
      else thename = dimname[idim];
      (void) ncdiminq(mincid, dim[idim], thename, &size[idim]);

      /* Set default coordinate info */
      if (start != NULL) start[idim] = 0.0;
      if (step != NULL) step[idim] = 1.0;
      if (dircos != NULL) {
         if ((strcmp(thename, MIxspace) == 0) ||
             (strcmp(thename, MIxfrequency) == 0))
            dimtype = XAXIS;
         else if ((strcmp(thename, MIyspace) == 0) ||
                  (strcmp(thename, MIyfrequency) == 0))
            dimtype = YAXIS;
         else if ((strcmp(thename, MIzspace) == 0) ||
                  (strcmp(thename, MIzfrequency) == 0))
            dimtype = ZAXIS;
         else
            dimtype = OAXIS;
         for (jdim=0; jdim < 3; jdim++)
            dircos[idim][jdim] = default_dircos[dimtype][jdim];
      }
      if (is_regular != NULL)
         is_regular[idim] = TRUE;

      /* Get the coordinate info */
      old_ncopts = ncopts; ncopts = 0;
      varid = ncvarid(mincid, thename);
      if (varid != MI_ERROR) {
         if (start != NULL)
            (void) miattget1(mincid, varid, MIstart, NC_DOUBLE, &start[idim]);
         if (step != NULL)
            (void) miattget1(mincid, varid, MIstep, NC_DOUBLE, &step[idim]);
         if (dircos != NULL)
            (void) miattget(mincid, varid, MIdirection_cosines, NC_DOUBLE,
                            3, dircos[idim], &att_length);
         if (is_regular != NULL) {
            if (miattgetstr(mincid, varid, MIspacing, 
                            sizeof(regstring), regstring) != NULL) {
               if (strcmp(regstring, MI_REGULAR) == 0)
                  is_regular[idim] = TRUE;
               else if (strcmp(regstring, MI_IRREGULAR) == 0)
                  is_regular[idim] = FALSE;
            }
         }
      }
      ncopts = old_ncopts;
   }
   
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : setup_output_files
@INPUT      : loop_options - Options controlling looping
              loopfile_info - Looping information
              arg_string - string for history
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to setup the the output files
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void setup_output_files(Loop_Options *loop_options, 
                                Loopfile_Info *loopfile_info,
                                char *arg_string)
{
   int inmincid, outmincid;
   int ifile;

   /* Get mincid for first input file */
   inmincid = get_input_mincid(loopfile_info, 0);

   /* Create output files */
   for (ifile=0; ifile < get_output_numfiles(loopfile_info); ifile++) {
      outmincid = create_output_file(loopfile_info, ifile);
      setup_variables(inmincid, outmincid, ifile, arg_string, loop_options);
   }
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_vector_length
@INPUT      : mincid - minc file id
              loop_options - looping options (if NULL, uses ncvarinq)
@OUTPUT     : (none)
@RETURNS    : Length of vector dimension or zero if no such dimension.
@DESCRIPTION: Routine to get the length of the vector dimension in a minc file.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE long get_vector_length(int mincid, Loop_Options *loop_options)
{
   int imgid;
   int ndims;
   int dim[MAX_VAR_DIMS];
   char dimname[MAX_NC_NAME];
   long vector_length;

   /* Get image variable id */
   imgid = ncvarid(mincid, MIimage);

   /* Get the image dimension info */
   if (loop_options != NULL) {
      (void) input_image_varinq(mincid, imgid, NULL, NULL, &ndims, dim, NULL, 
                                loop_options);
   }
   else {
      (void) ncvarinq(mincid, imgid, NULL, NULL, &ndims, dim, NULL);
   }

   /* Check for vector dimension */
   (void) ncdiminq(mincid, dim[ndims-1], dimname, &vector_length);
   if ((strcmp(dimname, MIvector_dimension) != 0) || (ndims <= 2)) {
      vector_length = 0;
   }

   return vector_length;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : setup_variables
@INPUT      : inmincid - input minc file id
              outmincid - output minc file id
              output_curfile - current output file number (counting from zero)
              arg_string - string for history
              loop_options - options controlling loop behaviour
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to setup the variables in the output file.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 10, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void setup_variables(int inmincid, int outmincid,
                             int output_curfile,
                             char *arg_string, Loop_Options *loop_options)
{
   int inimgid, outimgid, maxid, minid, varid;
   int indim[MAX_VAR_DIMS], outdim[MAX_VAR_DIMS];
   nc_type datatype;
   int idim, odim, ivar, in_ndims, out_ndims, in_nimgdims, out_nimgdims;
   char dimname[MAX_NC_NAME];
   int nvars, varlist[MAX_VAR_DIMS*2];
   long dimlength;
   int input_vector_length;
   int changing_vector_dim;
   double valid_range[2];

   /* Get image variable id for input file */
   inimgid = ncvarid(inmincid, MIimage);

   /* Get the list of dimensions subscripting the image variable */
   (void) input_image_varinq(inmincid, inimgid, NULL, &datatype, 
                             &in_ndims, indim, NULL, loop_options);

   /* Get the length of the input vector dimension */
   input_vector_length = get_vector_length(inmincid, loop_options);
   if (loop_options->convert_input_to_scalar && (input_vector_length > 0)) {
      input_vector_length = 0;
      in_ndims--;
   }

   /* Get the number of image dimensions in the input file */
   in_nimgdims = (input_vector_length == 0 ? 2 : 3);

   /* Are we changing the length of the vector dimension (or removing it?).
      For an output vector size of 1, we don't want an output vector
      dimension. */
   changing_vector_dim = 
      ((loop_options->output_vector_size != 0) &&
       (loop_options->output_vector_size != input_vector_length));
   if (loop_options->output_vector_size == 1)
      changing_vector_dim = (input_vector_length != 0);

   /* Work out number of output dimensions and image dimensions */
   out_ndims = in_ndims;
   out_nimgdims = in_nimgdims;
   if (changing_vector_dim && (input_vector_length == 0)) {
      out_ndims++;
      out_nimgdims++;
   }
   else if (changing_vector_dim && (loop_options->output_vector_size <= 1)) {
      out_ndims--;
      out_nimgdims--;
   }
      
   /* Set up the output minc file */

   /* Loop, creating output dimensions */
   odim = 0;
   nvars = 0;
   for (idim=0; idim < in_ndims; idim++) {

      /* Check for a change in vector dimension length */
      if ((idim != in_ndims-1) || (input_vector_length == 0) || 
          !changing_vector_dim) {

         /* Copy the dimension */
         (void) ncdiminq(inmincid, indim[idim], dimname, &dimlength);
         outdim[odim] = ncdimdef(outmincid, dimname, dimlength);

         /* Copy the dimension variables if we are not copying the 
            whole header */
         if (!loop_options->copy_all_header_info) {
            ncopts = 0;
            for (ivar=0; ivar < 2; ivar++) {
               if (ivar == 1) 
                  (void) strcat(dimname, "_width");
               varlist[nvars] = ncvarid(inmincid, dimname);
               if (varlist[nvars] != MI_ERROR) {
                  (void) micopy_var_def(inmincid, varlist[nvars], outmincid);
                  nvars++;
               }
            }
            ncopts = NC_OPTS_VAL;
         }
         
         odim++;
      }
   }

   /* Create the output vector dimension if needed */
   if (changing_vector_dim && (loop_options->output_vector_size > 1)) {
      outdim[odim] = ncdimdef(outmincid, MIvector_dimension, 
                              (long) loop_options->output_vector_size);
   }

   /* Copy other variables in file, if appropriate */
   if (loop_options->copy_all_header_info) {
      nvars = 0;
      ncopts = 0;
      varlist[nvars] = inimgid;
      if (varlist[nvars] != MI_ERROR) nvars++;
      varlist[nvars] = ncvarid(inmincid, MIimagemax);
      if (varlist[nvars] != MI_ERROR) nvars++;
      varlist[nvars] = ncvarid(inmincid, MIimagemin);
      if (varlist[nvars] != MI_ERROR) nvars++;
      if (loop_options->loop_dimension != NULL) {
         (void) strcpy(dimname, loop_options->loop_dimension);
         varlist[nvars] = ncvarid(inmincid, dimname);
         if (varlist[nvars] != MI_ERROR) nvars++;
         (void) strcat(dimname, "_width");
         varlist[nvars] = ncvarid(inmincid, dimname);
         if (varlist[nvars] != MI_ERROR) nvars++;
      }
      (void) micopy_all_var_defs(inmincid, outmincid, 
                                 nvars, varlist);
      ncopts = NC_OPTS_VAL;
   }

   /* Add the time stamp to the history */
   update_history(outmincid, arg_string);
 
   /* Call the user's function if needed */
   if (loop_options->output_file_function != NULL) {
      set_info_current_file(loop_options->loop_info, 0);
      loop_options->output_file_function(loop_options->caller_data,
                                         outmincid, output_curfile,
                                         loop_options->loop_info);
   }

   /* Create the image-min/max variables */
   maxid = micreate_std_variable(outmincid, MIimagemax, NC_DOUBLE, 
                                 out_ndims-out_nimgdims, outdim);
   minid = micreate_std_variable(outmincid, MIimagemin, NC_DOUBLE, 
                                 out_ndims-out_nimgdims, outdim);
   ncopts = 0;
   (void) micopy_all_atts(inmincid, ncvarid(inmincid, MIimagemax),
                          outmincid, maxid);
   (void) micopy_all_atts(inmincid, ncvarid(inmincid, MIimagemin),
                          outmincid, minid);
   ncopts = NC_OPTS_VAL;

   /* Create the image variable */
   if (loop_options->datatype != MI_ORIGINAL_TYPE) {
      datatype = loop_options->datatype;
   }
   loop_options->is_floating_type = 
      ((datatype == NC_FLOAT) || (datatype == NC_DOUBLE));
   outimgid = micreate_std_variable(outmincid, MIimage, datatype, 
                                    out_ndims, outdim);
   (void) micopy_all_atts(inmincid, inimgid, outmincid, outimgid);
   if (loop_options->is_floating_type) {
      ncopts = 0;
      (void) ncattdel(outmincid, outimgid, MIsigntype);
      ncopts = NC_OPTS_VAL;
      valid_range[0] = 0;
      valid_range[1] = 1;
      (void) miset_valid_range(outmincid, outimgid, valid_range);
   }
   else if (loop_options->datatype != MI_ORIGINAL_TYPE) {
      if (loop_options->is_signed)
         (void) miattputstr(outmincid, outimgid, MIsigntype, MI_SIGNED);
      else
         (void) miattputstr(outmincid, outimgid, MIsigntype, MI_UNSIGNED);
      if ((loop_options->valid_range[1] > loop_options->valid_range[0])) {
         (void) miset_valid_range(outmincid, outimgid, 
                                  loop_options->valid_range);
      }
      else {
         ncopts = 0;
         (void) ncattdel(outmincid, outimgid, MIvalid_range);
         ncopts = NC_OPTS_VAL;
      }
   }
   (void) miattputstr(outmincid, outimgid, MIcomplete, MI_FALSE);

   /* Put the file in data mode */
   (void) ncsetfill(outmincid, NC_NOFILL);
   (void) ncendef(outmincid);

   /* Copy over variable values */
   ncopts = 0;
   if (loop_options->copy_all_header_info) {
      (void) micopy_all_var_values(inmincid, outmincid, nvars, varlist);
   }
   else {
      for (ivar=0; ivar < nvars; ivar++) {
         (void) ncvarinq(inmincid, varlist[ivar], dimname, NULL, NULL,
                         NULL, NULL);
         varid = ncvarid(outmincid, dimname);
         if (varid != MI_ERROR) {
            (void) micopy_var_values(inmincid, varlist[ivar], 
                                     outmincid, varid);
         }
      }
   }
   ncopts = NC_OPTS_VAL;
         

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : update_history
@INPUT      : mincid - id of output minc file
              arg_string - string giving list of arguments
@OUTPUT     : (nothing)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to update the history global variable in the output 
              minc file
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : August 26, 1993 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void update_history(int mincid, char *arg_string)
{
   nc_type datatype;
   int att_length;
   char *string;

   if (arg_string == NULL) return;

   /* Get the history attribute length */
   ncopts=0;
   if ((ncattinq(mincid, NC_GLOBAL, MIhistory, &datatype,
                 &att_length) == MI_ERROR) ||
       (datatype != NC_CHAR))
      att_length = 0;
   att_length += strlen(arg_string) + 1;

   /* Allocate a string and get the old history */
   string = MALLOC(att_length, char);
   string[0] = '\0';
   (void) miattgetstr(mincid, NC_GLOBAL, MIhistory, att_length, 
                      string);
   ncopts = NC_OPTS_VAL;

   /* Add the new command and put the new history. */
   (void) strcat(string, arg_string);
   (void) miattputstr(mincid, NC_GLOBAL, MIhistory, string);
   FREE(string);

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : setup_icvs
@INPUT      : loop_options - loop option info
              loopfile_info - looping information
@OUTPUT     : (nothing)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to set up the input and output icv's.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void setup_icvs(Loop_Options *loop_options, 
                        Loopfile_Info *loopfile_info)
{
   int ifile;
   int icvid;

   /* Loop through input icv's, setting their values. Attaching is
      done by get_input_icvid. */
   for (ifile=0; ifile < get_input_numfiles(loopfile_info); ifile++) {
      icvid = create_input_icvid(loopfile_info, ifile);
      (void) miicv_setint(icvid, MI_ICV_TYPE, NC_DOUBLE);
      (void) miicv_setint(icvid, MI_ICV_DO_NORM, TRUE);
      (void) miicv_setint(icvid, MI_ICV_USER_NORM, TRUE);
      (void) miicv_setint(icvid, MI_ICV_DO_FILLVALUE, TRUE);
      if (loop_options->convert_input_to_scalar) {
         (void) miicv_setint(icvid, MI_ICV_DO_DIM_CONV, TRUE);
         (void) miicv_setint(icvid, MI_ICV_DO_SCALAR, TRUE);
         (void) miicv_setint(icvid, MI_ICV_XDIM_DIR, MI_ICV_ANYDIR);
         (void) miicv_setint(icvid, MI_ICV_YDIM_DIR, MI_ICV_ANYDIR);
         (void) miicv_setint(icvid, MI_ICV_ZDIM_DIR, MI_ICV_ANYDIR);
         (void) miicv_setint(icvid, MI_ICV_KEEP_ASPECT, FALSE);
      }
   }

   /* Loop through output icv's, setting their values. Attaching is
      done by get_input_icvid. */
   for (ifile=0; ifile < get_output_numfiles(loopfile_info); ifile++) {
      icvid = create_output_icvid(loopfile_info, ifile);
      (void) miicv_setint(icvid, MI_ICV_TYPE, NC_DOUBLE);
      (void) miicv_setint(icvid, MI_ICV_DO_NORM, TRUE);
      (void) miicv_setint(icvid, MI_ICV_USER_NORM, TRUE);
   }
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : do_voxel_loop
@INPUT      : loop_options - user options for looping
              loopfile_info - information on files used in loop
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to loop through the voxels and do something to each one
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 10, 1994 (Peter Neelin)
@MODIFIED   : November 30, 1994 (P.N.)
---------------------------------------------------------------------------- */
PRIVATE void do_voxel_loop(Loop_Options *loop_options,
                           Loopfile_Info *loopfile_info)
{
   long block_start[MAX_VAR_DIMS], block_end[MAX_VAR_DIMS];
   long block_incr[MAX_VAR_DIMS];
   long block_cur[MAX_VAR_DIMS], block_curcount[MAX_VAR_DIMS];
   long chunk_start[MAX_VAR_DIMS], chunk_end[MAX_VAR_DIMS];
   long chunk_incr[MAX_VAR_DIMS];
   long chunk_cur[MAX_VAR_DIMS], chunk_curcount[MAX_VAR_DIMS];
   long input_cur[MAX_VAR_DIMS], input_curcount[MAX_VAR_DIMS];
   long firstfile_cur[MAX_VAR_DIMS], firstfile_curcount[MAX_VAR_DIMS];
   double **input_buffers, **output_buffers, **extra_buffers;
   double **results_buffers;
   long chunk_num_voxels, block_num_voxels, ivox;
   int outmincid, imgid, maxid, minid;
   double *data, minimum, maximum, valid_range[2];
   double *global_minimum, *global_maximum;
   int ifile, ofile, ibuff, ndims, idim;
   int num_output_files;
   int num_input_buffers, num_output_buffers, num_extra_buffers;
   int input_vector_length, output_vector_length;
   int modify_vector_count;
   int current_input;
   int input_icvid, input_mincid;
   int loop_dim_index;
   int dim_index;
   int outer_file_loop;
   int dummy_index;
   int input_curfile;
   nc_type file_datatype;

   /* Get number of files, buffers, etc. */
   num_output_files = get_output_numfiles(loopfile_info);
   num_input_buffers = (loop_options->do_accumulate ? 1 : 
                        loop_options->num_all_inputs);
   num_extra_buffers = loop_options->num_extra_buffers;
   num_output_buffers = num_output_files + num_extra_buffers;
   input_vector_length = 
      get_vector_length(get_input_mincid(loopfile_info, 0), loop_options);
   if ((input_vector_length == 0) || (loop_options->convert_input_to_scalar))
      input_vector_length = 1;
   if (num_output_files > 0) {
      output_vector_length = 
         get_vector_length(get_output_mincid(loopfile_info, 0), NULL);
      if (output_vector_length == 0) output_vector_length = 1;
   }
   else
      output_vector_length = 1;
   modify_vector_count = (input_vector_length != output_vector_length);

   /* Initialize all of the counters to reasonable values */
   (void) miset_coords(MAX_VAR_DIMS, 0, block_start);
   (void) miset_coords(MAX_VAR_DIMS, 0, block_end);
   (void) miset_coords(MAX_VAR_DIMS, 0, block_incr);
   (void) miset_coords(MAX_VAR_DIMS, 0, block_cur);
   (void) miset_coords(MAX_VAR_DIMS, 0, block_curcount);
   (void) miset_coords(MAX_VAR_DIMS, 0, chunk_start);
   (void) miset_coords(MAX_VAR_DIMS, 0, chunk_end);
   (void) miset_coords(MAX_VAR_DIMS, 0, chunk_incr);
   (void) miset_coords(MAX_VAR_DIMS, 0, chunk_cur);
   (void) miset_coords(MAX_VAR_DIMS, 0, chunk_curcount);
   (void) miset_coords(MAX_VAR_DIMS, 0, input_cur);
   (void) miset_coords(MAX_VAR_DIMS, 0, input_curcount);
   (void) miset_coords(MAX_VAR_DIMS, 0, firstfile_cur);
   (void) miset_coords(MAX_VAR_DIMS, 0, firstfile_curcount);

   /* Get block and chunk looping information */
   setup_looping(loop_options, loopfile_info, &ndims,
                 block_start, block_end, 
                 block_incr, &block_num_voxels,
                 chunk_incr, &chunk_num_voxels);

   /* Allocate space for buffers */

   if (loop_options->allocate_buffer_function != NULL) {
      loop_options->allocate_buffer_function
         (loop_options->caller_data, TRUE, 
          num_input_buffers, chunk_num_voxels, input_vector_length, 
          &input_buffers, 
          num_output_files, block_num_voxels, output_vector_length, 
          &output_buffers, 
          num_extra_buffers, chunk_num_voxels, output_vector_length, 
          &extra_buffers, 
          loop_options->loop_info);
      
   }
   else {

      /* Allocate input buffers */
      input_buffers = MALLOC(num_input_buffers, double *);
      for (ibuff=0; ibuff < num_input_buffers; ibuff++) {
         input_buffers[ibuff] = MALLOC(chunk_num_voxels * input_vector_length, 
                                       double);
      }

      /* Allocate output buffers */
      if (num_output_files > 0) {
         output_buffers = MALLOC(num_output_files, double *);
         for (ibuff=0; ibuff < num_output_files; ibuff++) {
            output_buffers[ibuff] = MALLOC(block_num_voxels * 
                                           output_vector_length, double);
         }
      }

      /* Allocate extra buffers */
      if (num_extra_buffers > 0) {
         extra_buffers = MALLOC(num_extra_buffers, double *);
         for (ibuff=0; ibuff < num_extra_buffers; ibuff++) {
            extra_buffers[ibuff] = MALLOC(chunk_num_voxels *
                                          output_vector_length, double);
         }
      }

   }

   /* Set up the results pointers */
   if (num_output_buffers > 0) {
      results_buffers = MALLOC(num_output_buffers, double *);
      for (ibuff=0; ibuff < num_output_buffers; ibuff++) {
         if (ibuff < num_output_files) {
            results_buffers[ibuff] = output_buffers[ibuff];
         }
         else {
            results_buffers[ibuff] = extra_buffers[ibuff-num_output_files];
         }
      }
   }

   /* Initialize global min and max */
   if (num_output_files > 0) {
      global_minimum = MALLOC(num_output_files, double);
      global_maximum = MALLOC(num_output_files, double);
      for (ofile=0; ofile < num_output_files; ofile++) {
         global_minimum[ofile] = DBL_MAX;
         global_maximum[ofile] = -DBL_MAX;
      }
   }
   else {
      global_minimum = NULL;
      global_maximum = NULL;
   }

   /* Initialize loop info - just to be safe */
   initialize_loop_info(loop_options->loop_info);

   /* Print log message */
   if (loop_options->verbose) {
      (void) fprintf(stderr, "Processing:");
      (void) fflush(stderr);
   }

   /* Outer loop over files, if appropriate */
   outer_file_loop = (loop_options->do_accumulate && 
                      (num_output_buffers <= 0));
   for (initialize_file_and_index(loop_options, loopfile_info,
                                  outer_file_loop, &ifile, &dim_index,
                                  &dummy_index);
        finish_file_and_index(loop_options, loopfile_info,
                              outer_file_loop, ifile, dim_index,
                              dummy_index);
        increment_file_and_index(loop_options, loopfile_info,
                                 outer_file_loop, &ifile, &dim_index,
                                 &dummy_index)) {

      /* Loop through blocks (image-max/min do not vary over blocks) */

      nd_begin_looping(block_start, block_cur, ndims);

      while (!nd_end_of_loop(block_cur, block_end, ndims)) {

         nd_update_current_count(block_cur, block_incr, block_end,
                                 block_curcount, ndims);

         /* Set results_buffers to beginning of output buffers */
         for (ofile=0; ofile < num_output_files; ofile++) {
            results_buffers[ofile] = output_buffers[ofile];
         }

         /* Loop through chunks (space for input buffers) */
         for (idim=0; idim < ndims; idim++) {
            chunk_start[idim] = block_cur[idim];
            chunk_end[idim] = block_cur[idim] + block_curcount[idim];
         }

         nd_begin_looping(chunk_start, chunk_cur, ndims);

         while (!nd_end_of_loop(chunk_cur, chunk_end, ndims)) {

            nd_update_current_count(chunk_cur, chunk_incr, chunk_end,
                                    chunk_curcount, ndims);

            /* Print log message */
            if (loop_options->verbose) {
               (void) fprintf(stderr, ".");
               (void) fflush(stderr);
            }

            /* Calculate number of voxels in a chunk */
            chunk_num_voxels = 1;
            for (idim=0; idim < ndims; idim++)
               chunk_num_voxels *= chunk_curcount[idim];
            chunk_num_voxels /= input_vector_length;

            /* Translate start and count for file and save in loop_info */
            if (outer_file_loop)
               input_curfile = ifile;
            else
               input_curfile = 0;
            input_mincid = get_input_mincid(loopfile_info, input_curfile);
            translate_input_coords(input_mincid, chunk_cur, firstfile_cur,
                                   chunk_curcount, firstfile_curcount,
                                   &loop_dim_index, loop_options);

            /* Save start and count and file and index in loop_info */
            set_info_shape(loop_options->loop_info, 
                           firstfile_cur, firstfile_curcount);
            set_info_current_file(loop_options->loop_info, 0);
            set_info_current_index(loop_options->loop_info, 0);

            /* Initialize results buffers if necessary */
            if (loop_options->do_accumulate) {
               if (loop_options->start_function != NULL) {
                  loop_options->start_function
                     (loop_options->caller_data,
                      chunk_num_voxels,
                      num_output_buffers,
                      output_vector_length,
                      results_buffers,
                      loop_options->loop_info);
               }
            }

            /* Get the input buffers and accumulate them if needed */
            current_input = 0;
            for (initialize_file_and_index(loop_options, loopfile_info,
                                           !outer_file_loop, &ifile, 
                                           &dim_index, &dummy_index);
                 finish_file_and_index(loop_options, loopfile_info,
                                       !outer_file_loop, ifile, 
                                       dim_index, dummy_index);
                 increment_file_and_index(loop_options, loopfile_info,
                                          !outer_file_loop, &ifile, 
                                          &dim_index, &dummy_index)) {

               /* Get input icvid and mincid and translate coords for file.
                  We need to do this each time in case we have an outer
                  file loop. */
               input_icvid = get_input_icvid(loopfile_info, ifile);
               (void) miicv_inqint(input_icvid, MI_ICV_CDFID, 
                                   &input_mincid);
               translate_input_coords(input_mincid, chunk_cur, input_cur,
                                      chunk_curcount, input_curcount,
                                      &loop_dim_index, loop_options);


               /* Read buffer */
               ibuff = (loop_options->do_accumulate ? 0 : current_input);
               input_cur[loop_dim_index] = dim_index;
               (void) miicv_get(input_icvid,
                                input_cur, input_curcount, 
                                input_buffers[ibuff]);
               if (loop_options->do_accumulate) {
                  set_info_shape(loop_options->loop_info, 
                                 input_cur, input_curcount);
                  set_info_current_file(loop_options->loop_info, ifile);
                  set_info_current_index(loop_options->loop_info, dim_index);
                  set_info_loopfile_info(loop_options->loop_info, 
                                         loopfile_info);
                  loop_options->voxel_function(loop_options->caller_data,
                                               chunk_num_voxels, 
                                               num_input_buffers, 
                                               input_vector_length,
                                               input_buffers,
                                               num_output_buffers, 
                                               output_vector_length,
                                               results_buffers,
                                               loop_options->loop_info);
                  set_info_loopfile_info(loop_options->loop_info, NULL);
               }

               current_input++;

            }            /* Inner loop over files and dimension index */

            /* Do something with the buffers or finish accumulation */
            set_info_shape(loop_options->loop_info, 
                           firstfile_cur, firstfile_curcount);
            set_info_current_file(loop_options->loop_info, 0);
            set_info_current_index(loop_options->loop_info, 0);
            if (loop_options->do_accumulate) {
               if (loop_options->finish_function != NULL) {
                  loop_options->finish_function(loop_options->caller_data,
                                                chunk_num_voxels, 
                                                num_output_buffers,
                                                output_vector_length,
                                                results_buffers,
                                                loop_options->loop_info);
               }
            }
            else {
               loop_options->voxel_function(loop_options->caller_data,
                                            chunk_num_voxels, 
                                            num_input_buffers, 
                                            input_vector_length,
                                            input_buffers,
                                            num_output_buffers, 
                                            output_vector_length,
                                            results_buffers,
                                            loop_options->loop_info);
            }

            /* Increment results_buffers through output buffers */
            for (ofile=0; ofile < num_output_files; ofile++) {
               results_buffers[ofile] += 
                  chunk_num_voxels * output_vector_length;
            }

            nd_increment_loop(chunk_cur, chunk_start, chunk_incr, 
                              chunk_end, ndims);

         }     /* End of loop through chunks */

         /* Write out output buffers */

         for (ofile=0; ofile < num_output_files; ofile++) {
            outmincid = get_output_mincid(loopfile_info, ofile);
            maxid = ncvarid(outmincid, MIimagemax);
            minid = ncvarid(outmincid, MIimagemin);
            data = output_buffers[ofile];

            /* Find the max and min */
            minimum = DBL_MAX;
            maximum = -DBL_MAX;
            for (ivox=0; ivox < block_num_voxels*output_vector_length; 
                 ivox++) {
               if (data[ivox] != -DBL_MAX) {
                  if (data[ivox] < minimum) minimum = data[ivox];
                  if (data[ivox] > maximum) maximum = data[ivox];
               }
            }
            if ((minimum == DBL_MAX) && (maximum == -DBL_MAX)) {
               minimum = 0.0;
               maximum = 0.0;
            }

            /* Save global min and max */
            if (minimum < global_minimum[ofile]) 
               global_minimum[ofile] = minimum;
            if (maximum > global_maximum[ofile]) 
               global_maximum[ofile] = maximum;

            /* Write out the max and min */
            (void) mivarput1(outmincid, maxid, block_cur, 
                             NC_DOUBLE, NULL, &maximum);
            (void) mivarput1(outmincid, minid, block_cur, 
                             NC_DOUBLE, NULL, &minimum);

            /* Write out the values */
            if (modify_vector_count)
               block_curcount[ndims-1] = output_vector_length;
            (void) miicv_put(get_output_icvid(loopfile_info, ofile), 
                             block_cur, block_curcount, data);
         }          /* End of loop through output files */

         nd_increment_loop(block_cur, block_start, block_incr, 
                           block_end, ndims);

      }     /* End of loop through chunks */

   }     /* End of outer loop through files and dimension indices */

   /* Data has been completely written */
   for (ofile=0; ofile < num_output_files; ofile++) {
      outmincid = get_output_mincid(loopfile_info, ofile);
      imgid = ncvarid(outmincid, MIimage);
      (void) miattputstr(outmincid, imgid, MIcomplete, MI_TRUE);
      if (loop_options->is_floating_type) {
         if ((global_minimum[ofile] == DBL_MAX) && 
             (global_maximum[ofile] == -DBL_MAX)) {
            global_minimum[ofile] = 0.0;
            global_maximum[ofile] = 0.0;
         }
         valid_range[0] = global_minimum[ofile];
         valid_range[1] = global_maximum[ofile];

         /* Force truncation of valid_range to match float image */
         if ((ncvarinq(outmincid, imgid, NULL, &file_datatype, NULL, NULL,
                       NULL) != MI_ERROR) && (file_datatype == NC_FLOAT)) {
            valid_range[0] = (float) valid_range[0];
            valid_range[1] = (float) valid_range[1];
         }

         (void) miset_valid_range(outmincid, imgid, valid_range);

      }
   }

   /* Print log message */
   if (loop_options->verbose) {
      (void) fprintf(stderr, "Done\n");
      (void) fflush(stderr);
   }

   /* Free results pointer array, but not its buffers, since these
      were allocate as output_buffers and extra_buffers */
   if (num_output_buffers > 0) {
      FREE(results_buffers);
   }

   /* Free the buffers */
   if (loop_options->allocate_buffer_function != NULL) {
      loop_options->allocate_buffer_function
         (loop_options->caller_data, FALSE, 
          num_input_buffers, chunk_num_voxels, input_vector_length, 
          &input_buffers, 
          num_output_files, block_num_voxels, output_vector_length, 
          &output_buffers, 
          num_extra_buffers, chunk_num_voxels, output_vector_length, 
          &extra_buffers, 
          loop_options->loop_info);
   }
   else {

      /* Free input buffers */
      for (ibuff=0; ibuff < num_input_buffers; ibuff++) {
         FREE(input_buffers[ibuff]);
      }
      FREE(input_buffers);

      /* Free output buffers */
      if (num_output_files > 0) {
         for (ibuff=0; ibuff < num_output_files; ibuff++) {
            FREE(output_buffers[ibuff]);
         }
         FREE(output_buffers);
      }

      /* Free extra buffers */
      if (num_extra_buffers > 0) {
         for (ibuff=0; ibuff < num_extra_buffers; ibuff++) {
            FREE(extra_buffers[ibuff]);
         }
         FREE(extra_buffers);
      }

   }

   /* Free max and min arrays */
   if (num_output_files > 0) {
      FREE(global_minimum);
      FREE(global_maximum);
   }

   return;

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : setup_looping
@INPUT      : loop_options - users options controlling looping
              loopfile_info - information on files
@OUTPUT     : ndims - number of dimensions
              block_start - vector specifying start of block
              block_end - end of block
              block_incr - increment for stepping through blocks
              block_num_voxels - number of voxels in block
              chunk_incr - increment for stepping through chunks
              chunk_num_voxels - number of voxels in chunk
@RETURNS    : (nothing)
@DESCRIPTION: Routine to set up vectors giving blocks and chunks through
              which we will loop.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : December 2, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void setup_looping(Loop_Options *loop_options, 
                           Loopfile_Info *loopfile_info,
                           int *ndims,
                           long block_start[], long block_end[], 
                           long block_incr[], long *block_num_voxels,
                           long chunk_incr[], long *chunk_num_voxels)
{
   int inmincid;
   int total_ndims, scalar_ndims, idim;
   int input_vector_length, output_vector_length;
   int num_input_buffers;
   int vector_data;
   int nimgdims;
   long size[MAX_VAR_DIMS];
   long max_voxels_in_buffer;

   /* Get input mincid */
   inmincid = get_input_mincid(loopfile_info, 0);

   /* Get number of dimensions and their sizes */
   get_dim_info(inmincid, &total_ndims, size, NULL, NULL, NULL, NULL, NULL,
                loop_options);

   /* Get vector lengths */
   input_vector_length = get_vector_length(inmincid, loop_options);
   if (get_output_numfiles(loopfile_info) > 0)
      output_vector_length = 
         get_vector_length(get_output_mincid(loopfile_info, 0), NULL);
   else
      output_vector_length = 0;

   /* Check for vector data and whether we are adding a dimension */
   vector_data = ((input_vector_length > 0) || (output_vector_length > 0));
   if ((input_vector_length == 0) && (output_vector_length > 0)) {
      total_ndims++;
      size[total_ndims-1] = 1;
   }
   scalar_ndims = (vector_data ? total_ndims - 1 : total_ndims);

   /* Get number of image dimensions */
   nimgdims = (vector_data ? 3 : 2);

   /* Set vector lengths properly */
   if (input_vector_length <= 0) input_vector_length = 1;
   if (output_vector_length <= 0) output_vector_length = 1;

   /* Set vectors */
   *block_num_voxels = 1;
   for (idim=0; idim < total_ndims; idim++) {
      block_start[idim] = 0;
      block_end[idim] = size[idim];
      if (idim < total_ndims - nimgdims)
         block_incr[idim] = 1;
      else 
         block_incr[idim] = size[idim];
      *block_num_voxels *= block_incr[idim];
      chunk_incr[idim] = 1;
   }
   if (vector_data) {
      *block_num_voxels /= input_vector_length;
      idim = total_ndims-1;
      chunk_incr[idim] = block_incr[idim];
   }

   /* Figure out chunk size. Enforce a minimum chunk size. */
   *chunk_num_voxels = 1;
   num_input_buffers = (loop_options->do_accumulate ? 1 : 
                        loop_options->num_all_inputs);
   max_voxels_in_buffer = 
      (loop_options->total_copy_space/((long) sizeof(double)) - 
       get_output_numfiles(loopfile_info) * *block_num_voxels *
       output_vector_length) / 
          (num_input_buffers * input_vector_length + 
           loop_options->num_extra_buffers * output_vector_length);
   if (max_voxels_in_buffer < MIN_VOXELS_IN_BUFFER) {
      max_voxels_in_buffer = MIN_VOXELS_IN_BUFFER;
   }
   if (max_voxels_in_buffer > 0) {
      for (idim=scalar_ndims-1; idim >= 0; idim--) {
         chunk_incr[idim] = max_voxels_in_buffer / *chunk_num_voxels;
         if (chunk_incr[idim] > block_incr[idim])
            chunk_incr[idim] = block_incr[idim];
         *chunk_num_voxels *= chunk_incr[idim];
      }
   }

   /* Set ndims */
   *ndims = total_ndims;
                
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : initialize_file_and_index
@INPUT      : loop_options - users options controlling looping
              loopfile_info - information on files
              do_loop - TRUE if looping stuff should really be done
@OUTPUT     : ifile - file counter
              dim_index - dimension index counter
              dummy_index - counter used when do_loop is FALSE
@RETURNS    : (nothing)
@DESCRIPTION: Routine to initialize the file and index loop. These 
              three routines allow looping through files and dimension 
              indices at two levels. If do_loop is TRUE, then normal looping 
              is done (increment dim_index fastest, then ifile). If do_loop is
              FALSE, then only one loop is performed (using dummy_index).
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 1, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void initialize_file_and_index(Loop_Options *loop_options, 
                                       Loopfile_Info *loopfile_info,
                                       int do_loop,
                                       int *ifile, int *dim_index,
                                       int *dummy_index)
     /* ARGSUSED */
{
   if (do_loop) {
      *ifile = 0;
      *dim_index = 0;
   }
   else {
      *dummy_index = 0;
   }
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : finish_file_and_index
@INPUT      : loop_options - users options controlling looping
              loopfile_info - information on files
              do_loop - TRUE if looping stuff should really be done
              ifile - file counter
              dim_index - dimension index counter
              dummy_index - counter used when do_loop is FALSE
@RETURNS    : TRUE while there are more buffers to process.
@DESCRIPTION: Routine to test for the end of the file and index loop. These 
              three routines allow looping through files and dimension 
              indices at two levels. If do_loop is TRUE, then normal looping 
              is done (increment dim_index fastest, then ifile). If do_loop is
              FALSE, then only one loop is performed (using dummy_index).
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 1, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE int finish_file_and_index(Loop_Options *loop_options, 
                                  Loopfile_Info *loopfile_info,
                                  int do_loop,
                                  int ifile, int dim_index,
                                  int dummy_index)
     /* ARGSUSED */
{
   if (do_loop) {
      return (ifile < get_input_numfiles(loopfile_info));
   }
   else {
      return (dummy_index < 1);
   }
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : increment_file_and_index
@INPUT      : loop_options - users options controlling looping
              loopfile_info - information on files
              do_loop - TRUE if looping stuff should really be done
@OUTPUT     : ifile - file counter
              dim_index - dimension index counter
              dummy_index - counter used when do_loop is FALSE
@RETURNS    : (nothing)
@DESCRIPTION: Routine to increment the file and index loop. These 
              three routines allow looping through files and dimension 
              indices at two levels. If do_loop is TRUE, then normal looping 
              is done (increment dim_index fastest, then ifile). If do_loop is
              FALSE, then only one loop is performed (using dummy_index).
              Note that dummy_index is not touched if do_loop is TRUE.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 1, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void increment_file_and_index(Loop_Options *loop_options, 
                                      Loopfile_Info *loopfile_info,
                                      int do_loop,
                                      int *ifile, int *dim_index,
                                      int *dummy_index)
{
   int input_mincid;

   if (do_loop) {
      (*dim_index)++;
      input_mincid = get_input_mincid(loopfile_info, *ifile);
      if (*dim_index >= get_loop_dim_size(input_mincid, loop_options)) {
         *dim_index = 0;
         (*ifile)++;
      }
   }
   else {
      (*dummy_index)++;
   }
}


/* ------------ Routines controlling looping over files ------------ */

/* ----------------------------- MNI Header -----------------------------------
@NAME       : initialize_loopfile_info
@INPUT      : num_input_files - number of input files
              input_files - list of input file names
              num_output_files - list of output file names
              output_files - list of output file names
              loop_options - user options for looping
@OUTPUT     : (none)
@RETURNS    : pointer to Loopfile_Info structure
@DESCRIPTION: Routine to set up looping information for these files.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE Loopfile_Info *initialize_loopfile_info(int num_input_files,
                                                char *input_files[],
                                                int num_output_files,
                                                char *output_files[],
                                                Loop_Options *loop_options)
{
   int num_free_files, num_files, ifile;
   Loopfile_Info *loopfile_info;

   /* Allocate structure */
   loopfile_info = MALLOC(1, Loopfile_Info);

   /* Save clobber info */
   if (loop_options->clobber) {
       loopfile_info->cflags = NC_CLOBBER;
   }
   else {
       loopfile_info->cflags = NC_NOCLOBBER;
   }

#if MINC2
   if (loop_options->v2format) {
       loopfile_info->cflags |= MI2_CREATE_V2;
   }
#endif /* MINC2 */

   /* Save number of input and output files */
   loopfile_info->num_input_files = num_input_files;
   loopfile_info->num_output_files = num_output_files;

   /* Save input file names (just copy pointers, not strings) */
   if (num_input_files > 0) {
      loopfile_info->input_files = MALLOC(num_input_files, char *);
      for (ifile=0; ifile < num_input_files; ifile++)
         loopfile_info->input_files[ifile] = input_files[ifile];
   }
   else
      loopfile_info->input_files = NULL;

   /* Save output file names (just copy pointers, not strings) */
   if (num_output_files > 0) {
      loopfile_info->output_files = MALLOC(num_output_files, char *);
      for (ifile=0; ifile < num_output_files; ifile++)
         loopfile_info->output_files[ifile] = output_files[ifile];
   }
   else
      loopfile_info->output_files = NULL;

   /* Keep track of number of files that we can open */
   num_free_files = loop_options->max_open_files;
   if (num_free_files > MI_MAX_NUM_ICV) num_free_files = MI_MAX_NUM_ICV;

   /* Check to see if we can open output files (we must leave room for one
      input file) */
   if (num_output_files < num_free_files-1) {
      loopfile_info->output_all_open = TRUE;
      num_files = num_output_files;
   }
   else {
      loopfile_info->output_all_open = FALSE;
      num_files = 1;
   }
   num_free_files -= num_files;
   loopfile_info->output_mincid = MALLOC(num_files, int);
   loopfile_info->output_icvid = MALLOC(num_files, int);
   for (ifile=0; ifile < num_files; ifile++) {
      loopfile_info->output_mincid[ifile] = MI_ERROR;
      loopfile_info->output_icvid[ifile] = MI_ERROR;
   }
   loopfile_info->current_input_file_number = -1;

   /* Check whether sequential access would be better */
   loopfile_info->sequential_access = 
      (loop_options->do_accumulate &&
       ((num_output_files + loop_options->num_extra_buffers) <= 0));

   /* Check to see if we can open input files */
   if (num_input_files < num_free_files) { 
      loopfile_info->can_open_all_input = TRUE;
      num_files = num_input_files;
   }
   else {
      loopfile_info->can_open_all_input = FALSE;
      num_files = 1;
   }
   num_free_files -= num_files;
   loopfile_info->input_mincid = MALLOC(num_files, int);
   loopfile_info->input_icvid = MALLOC(num_files, int);
   for (ifile=0; ifile < num_files; ifile++) {
      loopfile_info->input_mincid[ifile] = MI_ERROR;
      loopfile_info->input_icvid[ifile] = MI_ERROR;
   }
   loopfile_info->current_output_file_number = -1;

   /* Check for an already open input file */
   if (loop_options->input_mincid != MI_ERROR) {
      loopfile_info->input_mincid[0] = loop_options->input_mincid;
      loopfile_info->current_input_file_number = 0;
   }

   /* Check whether we want to open all input files */
   loopfile_info->input_all_open = (! loopfile_info->sequential_access) &&
      loopfile_info->can_open_all_input;

   /* Set default for expanding compressed files */
   loopfile_info->headers_only = FALSE;
   loopfile_info->want_headers_only = FALSE;

   /* Return the loopfile_info structure */
   return loopfile_info;

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : cleanup_loopfile_info
@INPUT      : loopfile_info - looping information
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to clean up looping information.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void cleanup_loopfile_info(Loopfile_Info *loopfile_info)
{
   int num_files, ifile;

   /* Close input files and free icv's */
   if (loopfile_info->can_open_all_input)
      num_files = loopfile_info->num_input_files;
   else
      num_files = 1;
   for (ifile=0; ifile < num_files; ifile++) {
      if (loopfile_info->input_icvid[ifile] != MI_ERROR)
         (void) miicv_free(loopfile_info->input_icvid[ifile]);
      if (loopfile_info->input_mincid[ifile] != MI_ERROR)
         (void) miclose(loopfile_info->input_mincid[ifile]);
   }

   /* Close output files and free icv's */
   if (loopfile_info->output_all_open)
      num_files = loopfile_info->num_output_files;
   else
      num_files = 1;
   for (ifile=0; ifile < num_files; ifile++) {
      (void) miicv_free(loopfile_info->output_icvid[ifile]);
      (void) miclose(loopfile_info->output_mincid[ifile]);
   }

   /* Free input arrays */
   if (loopfile_info->input_files != NULL)
      FREE(loopfile_info->input_files);
   if (loopfile_info->input_mincid != NULL)
      FREE(loopfile_info->input_mincid);
   if (loopfile_info->input_icvid != NULL)
      FREE(loopfile_info->input_icvid);

   /* Free output arrays */
   if (loopfile_info->output_files != NULL)
      FREE(loopfile_info->output_files);
   if (loopfile_info->output_mincid != NULL)
      FREE(loopfile_info->output_mincid);
   if (loopfile_info->output_icvid != NULL)
      FREE(loopfile_info->output_icvid);

   /* Free the structure */
   FREE(loopfile_info);
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_input_numfiles
@INPUT      : loopfile_info - looping information
@OUTPUT     : (none)
@RETURNS    : Number of input files
@DESCRIPTION: Routine to get the number of input files.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 1, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE int get_input_numfiles(Loopfile_Info *loopfile_info)
{
   return loopfile_info->num_input_files;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_output_numfiles
@INPUT      : loopfile_info - looping information
@OUTPUT     : (none)
@RETURNS    : Number of output files
@DESCRIPTION: Routine to get the number of output files.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 1, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE int get_output_numfiles(Loopfile_Info *loopfile_info)
{
   return loopfile_info->num_output_files;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_input_filename
@INPUT      : loopfile_info - looping information
              file_num - input file number
@OUTPUT     : (none)
@RETURNS    : Pointer to string containing input file name for file ifile.
@DESCRIPTION: Routine to get name of an input file.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 1, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE char *get_input_filename(Loopfile_Info *loopfile_info, int file_num)
{
   /* Check for bad file_num */
   if ((file_num < 0) || (file_num >= loopfile_info->num_input_files)) {
      (void) fprintf(stderr, "Bad input file number %d\n", file_num);
      exit(EXIT_FAILURE);
   }

   return loopfile_info->input_files[file_num];
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_input_headers_only
@INPUT      : loopfile_info - looping information
              headers_only - TRUE if we only need input headers, FALSE
                 if we need the whole file.
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to modify the Loopfile_Info structure so that
              in future we get either whole minc files or only the headers.
              The change is only made if it makes sense (ie. we are processing
              files sequentially).
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 1, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void set_input_headers_only(Loopfile_Info *loopfile_info,
                                    int headers_only)
{
   int num_files, ifile;
   int icvid, mincid;

   /* Change the indication that we want to have headers only */
   loopfile_info->want_headers_only = headers_only;

   /* If headers_only is not changing, don't do anything */
   if ((headers_only && loopfile_info->headers_only) ||
       (!headers_only && !loopfile_info->headers_only)) {
      return;
   }

   /* Check to see if it makes sense to change to headers only */
   if (headers_only && loopfile_info->input_all_open) return;

   /* Change the value */
   loopfile_info->headers_only = headers_only;

   /* If we are going to headers_only == FALSE, then loop through icv's and 
      files, making sure that they are detached and closed (we will need to 
      re-open them */
   if (!loopfile_info->headers_only) {
      num_files = (loopfile_info->can_open_all_input ?
                   loopfile_info->num_input_files : 1);
      for (ifile=0; ifile < num_files; ifile++) {
         icvid = loopfile_info->input_icvid[ifile];
         mincid = MI_ERROR;
         if (icvid != MI_ERROR) {
            (void) miicv_inqint(icvid, MI_ICV_CDFID, &mincid);
            if (mincid != MI_ERROR) {
               (void) miicv_detach(icvid);
               (void) miclose(mincid);
            }
         }
         if ((loopfile_info->input_mincid[ifile] != MI_ERROR) &&
             (loopfile_info->input_mincid[ifile] != mincid)) {
            (void) miclose(loopfile_info->input_mincid[ifile]);
         }
         loopfile_info->input_mincid[ifile] = MI_ERROR;
      }
         
   }

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_input_sequential
@INPUT      : loopfile_info - looping information
              sequential_access - TRUE if we want to open only one file at a
                 time, FALSE otherwise.
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to modify the Loopfile_Info structure so that
              files are opened one at a time.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 1, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void set_input_sequential(Loopfile_Info *loopfile_info,
                                  int sequential_access)
{
   int old_input_all_open;
   int ifile, num_files;
   int mincid, icvid;
   int current_input_file_number;

   /* Set flag for sequential access */
   loopfile_info->sequential_access = sequential_access;

   /* Change status of input_all_open */
   old_input_all_open = loopfile_info->input_all_open;
   loopfile_info->input_all_open = (! loopfile_info->sequential_access) &&
      loopfile_info->can_open_all_input;

   /* Check if input_all_open has changed */
   if (!old_input_all_open && loopfile_info->input_all_open) {
      current_input_file_number = loopfile_info->current_input_file_number;
      if (current_input_file_number >= 0) {
         mincid = loopfile_info->input_mincid[0];
         loopfile_info->input_mincid[0] = MI_ERROR;
         loopfile_info->input_mincid[current_input_file_number] = mincid;
      }
   }
   else if (old_input_all_open && !loopfile_info->input_all_open) {
      if (loopfile_info->can_open_all_input)
         num_files = loopfile_info->num_input_files;
      else
         num_files = 1;
      for (ifile=0; ifile < num_files; ifile++) {
         icvid = loopfile_info->input_icvid[ifile];
         if (icvid != MI_ERROR) {
            (void) miicv_inqint(icvid, MI_ICV_CDFID, &mincid);
            (void) miicv_detach(icvid);
            if (mincid != MI_ERROR) {
               (void) miclose(mincid);
            }
         }
         if ((loopfile_info->input_mincid[ifile] != MI_ERROR) &&
             (loopfile_info->input_mincid[ifile] != mincid))
            (void) miclose(loopfile_info->input_mincid[ifile]);
         loopfile_info->input_mincid[ifile] = MI_ERROR;
      }
   }

   /* Call set_input_headers_only in case want_headers_only is different
      from headers_only */
   set_input_headers_only(loopfile_info, loopfile_info->want_headers_only);

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_input_mincid
@INPUT      : loopfile_info - looping information
              file_num - input file number
@OUTPUT     : (none)
@RETURNS    : Id of minc file
@DESCRIPTION: Routine to get the minc id for an input file. The file number
              corresponds to the file's position in the input_files list
              (counting from zero).
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE int get_input_mincid(Loopfile_Info *loopfile_info,
                             int file_num)
{
   int index;
   int created_tempfile;
   char *filename;

   /* Check for bad file_num */
   if ((file_num < 0) || (file_num >= loopfile_info->num_input_files)) {
      (void) fprintf(stderr, "Bad input file number %d\n", file_num);
      exit(EXIT_FAILURE);
   }

   /* Check to see if all files are open or not */
   if (loopfile_info->input_all_open) {
      index = file_num;
   }
   else {
      index = 0;
      if ((loopfile_info->input_mincid[index] != MI_ERROR) &&
          (loopfile_info->current_input_file_number != file_num)) {
         if (loopfile_info->input_icvid[index] != MI_ERROR)
            (void) miicv_detach(loopfile_info->input_icvid[index]);
         (void) miclose(loopfile_info->input_mincid[index]);
         loopfile_info->input_mincid[index] = MI_ERROR;
      }
      loopfile_info->current_input_file_number = file_num;
   }

   /* Open the file if it hasn't been already */
   if (loopfile_info->input_mincid[index] == MI_ERROR) {
      filename = miexpand_file(loopfile_info->input_files[file_num], NULL,
                               loopfile_info->headers_only,
                               &created_tempfile);
      loopfile_info->input_mincid[index] = miopen(filename, NC_NOWRITE);
      if (created_tempfile) {
         (void) remove(filename);
      }
      FREE(filename);
   }

   return loopfile_info->input_mincid[index];
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_output_mincid
@INPUT      : loopfile_info - looping information
              file_num - output file number
@OUTPUT     : (none)
@RETURNS    : Id of minc file
@DESCRIPTION: Routine to get the minc id for an output file. The file number
              corresponds to the file's position in the output_files list
              (counting from zero).
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE int get_output_mincid(Loopfile_Info *loopfile_info,
                              int file_num)
{
   int index;

   /* Check for bad file_num */
   if ((file_num < 0) || (file_num >= loopfile_info->num_output_files)) {
      (void) fprintf(stderr, "Bad output file number %d\n", file_num);
      exit(EXIT_FAILURE);
   }

   /* Check to see if all files are open or not */
   if (loopfile_info->output_all_open) {
      index = file_num;
   }
   else {
      index = 0;
      if ((loopfile_info->output_mincid[index] != MI_ERROR) &&
          (loopfile_info->current_output_file_number != file_num)) {
         if (loopfile_info->output_icvid[index] != MI_ERROR)
            (void) miicv_detach(loopfile_info->output_icvid[index]);
         (void) miclose(loopfile_info->output_mincid[index]);
         loopfile_info->output_mincid[index] = MI_ERROR;
      }
      loopfile_info->current_output_file_number = file_num;
   }

   /* Open the file if it hasn't been already */
   if (loopfile_info->output_mincid[index] == MI_ERROR) {
      loopfile_info->output_mincid[index] =
         miopen(loopfile_info->output_files[file_num], NC_WRITE);
   }

   return loopfile_info->output_mincid[index];
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : create_output_file
@INPUT      : loopfile_info - looping information
              file_num - output file number
@OUTPUT     : (none)
@RETURNS    : Id of minc file
@DESCRIPTION: Routine to create an output file. The file number
              corresponds to the file's position in the output_files list
              (counting from zero).
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE int create_output_file(Loopfile_Info *loopfile_info,
                               int file_num)
{
   int index;

   /* Check for bad file_num */
   if ((file_num < 0) || (file_num >= loopfile_info->num_output_files)) {
      (void) fprintf(stderr, "Bad output file number %d for create.\n", 
                     file_num);
      exit(EXIT_FAILURE);
   }

   /* Check to see if all files are open or not */
   if (loopfile_info->output_all_open) {
      index = file_num;
   }
   else {
      index = 0;
      if ((loopfile_info->output_mincid[index] != MI_ERROR) &&
          (loopfile_info->current_output_file_number != file_num)) {
         (void) miclose(loopfile_info->output_mincid[index]);
         loopfile_info->output_mincid[index] = MI_ERROR;
      }
      loopfile_info->current_output_file_number = file_num;
   }

   /* Create the file */
   if (loopfile_info->output_mincid[index] != MI_ERROR) {
      (void) fprintf(stderr, "File %s has already been created\n",
                     loopfile_info->output_files[file_num]);
      exit(EXIT_FAILURE);
   }
   loopfile_info->output_mincid[index] =
      micreate(loopfile_info->output_files[file_num], 
               loopfile_info->cflags);

   return loopfile_info->output_mincid[index];
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_input_icvid
@INPUT      : loopfile_info - looping information
              file_num - input file number
@OUTPUT     : (none)
@RETURNS    : Id of icv for the specified file
@DESCRIPTION: Routine to get the icv id for an input file. The file number
              corresponds to the file's position in the input_files list
              (counting from zero).
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE int get_input_icvid(Loopfile_Info *loopfile_info,
                            int file_num)
{
   int mincid, icv_mincid, icvid;
   int index;

   /* Check for bad file_num */
   if ((file_num < 0) || (file_num >= loopfile_info->num_input_files)) {
      (void) fprintf(stderr, "Bad input file number %d\n", file_num);
      exit(EXIT_FAILURE);
   }

   /* Check to see if all files are open or not - get the correct index */
   if (loopfile_info->can_open_all_input) {
      index = file_num;
   }
   else {
      index = 0;
   }

   /* Check to see if the icv is attached to the correct minc file. If
      not, re-attach it. */
   icvid = loopfile_info->input_icvid[index];
   mincid = get_input_mincid(loopfile_info, file_num);
   if (icvid != MI_ERROR)
      (void) miicv_inqint(icvid, MI_ICV_CDFID, &icv_mincid);
   else
      icv_mincid = MI_ERROR;
   if (mincid != icv_mincid) {
      (void) miicv_attach(icvid, mincid, ncvarid(mincid, MIimage));
   }
   return icvid;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_output_icvid
@INPUT      : loopfile_info - looping information
              file_num - output file number
@OUTPUT     : (none)
@RETURNS    : Id of icv for the specified file
@DESCRIPTION: Routine to get the icv id for an output file. The file number
              corresponds to the file's position in the output_files list
              (counting from zero).
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE int get_output_icvid(Loopfile_Info *loopfile_info,
                             int file_num)
{
   int mincid, icv_mincid, icvid;
   int index;

   /* Check for bad file_num */
   if ((file_num < 0) || (file_num >= loopfile_info->num_output_files)) {
      (void) fprintf(stderr, "Bad output file number %d\n", file_num);
      exit(EXIT_FAILURE);
   }

   /* Check to see if all files are open or not - get the correct index */
   if (loopfile_info->output_all_open) {
      index = file_num;
   }
   else {
      index = 0;
   }

   /* Check to see if the icv is attached to the correct minc file. If
      not, re-attach it. */
   icvid = loopfile_info->output_icvid[index];
   mincid = get_output_mincid(loopfile_info, file_num);
   if (icvid != MI_ERROR)
      (void) miicv_inqint(icvid, MI_ICV_CDFID, &icv_mincid);
   else
      icv_mincid = MI_ERROR;
   if (mincid != icv_mincid) {
      (void) miicv_attach(icvid, mincid, ncvarid(mincid, MIimage));
   }
   return icvid;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : create_input_icvid
@INPUT      : loopfile_info - looping information
              file_num - input file number
@OUTPUT     : (none)
@RETURNS    : Id of icv for the specified file
@DESCRIPTION: Routine to create the icv id for an input file. The file number
              corresponds to the file's position in the input_files list
              (counting from zero). If the icv already exists, just 
              return it. If there are too many files, then only one
              icv will be used.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE int create_input_icvid(Loopfile_Info *loopfile_info,
                               int file_num)
{
   int index;

   /* Check for bad file_num */
   if ((file_num < 0) || (file_num >= loopfile_info->num_input_files)) {
      (void) fprintf(stderr, "Bad input file number %d\n", file_num);
      exit(EXIT_FAILURE);
   }

   /* Check to see if all files are open or not - get the correct index */
   if (loopfile_info->can_open_all_input) {
      index = file_num;
   }
   else {
      index = 0;
   }

   /* Check to see if icv exists - if not create it */
   if (loopfile_info->input_icvid[index] == MI_ERROR) {
      loopfile_info->input_icvid[index] = miicv_create();
   }

   return loopfile_info->input_icvid[index];
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : create_output_icvid
@INPUT      : loopfile_info - looping information
              file_num - output file number
@OUTPUT     : (none)
@RETURNS    : Id of icv for the specified file
@DESCRIPTION: Routine to create the icv id for an output file. The file number
              corresponds to the file's position in the output_files list
              (counting from zero). If the icv already exists, just 
              return it. If there are too many files, then only one
              icv will be used.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 30, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE int create_output_icvid(Loopfile_Info *loopfile_info,
                                int file_num)
{
   int index;

   /* Check for bad file_num */
   if ((file_num < 0) || (file_num >= loopfile_info->num_output_files)) {
      (void) fprintf(stderr, "Bad output file number %d\n", file_num);
      exit(EXIT_FAILURE);
   }

   /* Check to see if all files are open or not - get the correct index */
   if (loopfile_info->output_all_open) {
      index = file_num;
   }
   else {
      index = 0;
   }

   /* Check to see if icv exists - if not create it */
   if (loopfile_info->output_icvid[index] == MI_ERROR) {
      loopfile_info->output_icvid[index] = miicv_create();
   }

   return loopfile_info->output_icvid[index];
}


/* ------------ Routines to set loop options ------------ */

/* ----------------------------- MNI Header -----------------------------------
@NAME       : create_loop_options
@INPUT      : (none)
@OUTPUT     : (none)
@RETURNS    : Pointer to Loop_Options structure
@DESCRIPTION: Routine to create and initialize the loop options structure.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : December 6, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI Loop_Options *create_loop_options(void)
{
   Loop_Options *loop_options;

   /* Allocate structure */
   loop_options = MALLOC(1, Loop_Options);

   /* Fill in the defaults */
   loop_options->clobber = FALSE;
   loop_options->verbose = TRUE;
   loop_options->datatype = MI_ORIGINAL_TYPE;
   loop_options->is_signed = TRUE;
   loop_options->valid_range[0] = 0.0;
   loop_options->valid_range[1] = 0.0;
   loop_options->max_open_files = MI_MAX_NUM_ICV - 2;
   loop_options->check_all_input_dim_info = TRUE;
   loop_options->convert_input_to_scalar = FALSE;
   loop_options->output_vector_size = 0;
   loop_options->input_mincid = MI_ERROR;
   
   loop_options->total_copy_space = miget_cfg_int(MICFG_MAXBUF) * 1024;
   if(loop_options->total_copy_space == 0){
      loop_options->total_copy_space = MI2_DEF_BUFF_SIZE * 1024;
      }
   
   loop_options->loop_dimension = NULL;
   loop_options->num_all_inputs = 0;
   loop_options->input_file_function = NULL;
   loop_options->output_file_function = NULL;
   loop_options->copy_all_header_info = TRUE;
   loop_options->do_accumulate = FALSE;
   loop_options->num_extra_buffers = 0;
   loop_options->start_function = NULL;
   loop_options->finish_function = NULL;
   loop_options->voxel_function = NULL;
   loop_options->caller_data = NULL;
   loop_options->loop_info = create_loop_info();

   loop_options->allocate_buffer_function = NULL;

#if MINC2
   loop_options->v2format = FALSE; /* Use MINC 2.0 file format (HDF5)? */
#endif /* MINC2 */

   /* Return the structure pointer */
   return loop_options;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : free_loop_options
@INPUT      : loop_options - pointer to structure to cleanup
@OUTPUT     : (none)
@RETURNS    : (none)
@DESCRIPTION: Routine to cleanup and free the Loop_Options structure
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : December 6, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void free_loop_options(Loop_Options *loop_options)
{
   free_loop_info(loop_options->loop_info);
   if (loop_options->loop_dimension != NULL)
      FREE(loop_options->loop_dimension);
   FREE(loop_options);
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_clobber
@INPUT      : loop_options - user options for looping
              verbose - TRUE if output files should be clobbered
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to turn output clobber on or off
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : December 6, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_clobber(Loop_Options *loop_options, 
                             int clobber)
{
   loop_options->clobber = clobber;
}

#if MINC2
/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_v2format
@INPUT      : loop_options - user options for looping
              v2format - TRUE if output files should use MINC 2.0 format
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to turn output clobber on or off
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : October 8, 2003 (Bert Vincent)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_v2format(Loop_Options *loop_options, int v2format)
{
   loop_options->v2format = v2format;
}
#endif /* MINC2 */

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_verbose
@INPUT      : loop_options - user options for looping
              verbose - TRUE if logging should be done.
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to turn logging on or off.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : December 6, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_verbose(Loop_Options *loop_options, 
                             int verbose)
{
   loop_options->verbose = verbose;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_datatype
@INPUT      : loop_options - user options for looping
              datatype - NetCDF datatype for output (MI_ORIGINAL_TYPE means
                 use input type)
              is_signed - TRUE if type is signed
              valid_min - valid minimum for type (if valid_min >= valid_max,
                 then defaults are used)
              valid_max - valid maximum for type
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to set the output file datatype, sign and range.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 20, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_datatype(Loop_Options *loop_options, 
                              nc_type datatype, int is_signed,
                              double valid_min, double valid_max)
{
   loop_options->datatype = datatype;
   loop_options->is_signed = is_signed;
   loop_options->valid_range[0] = valid_min;
   loop_options->valid_range[1] = valid_max;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_max_open_files
@INPUT      : loop_options - user options for looping
              max_open_files - maximum number of open files allowed (between
                 1 and MI_MAX_NUM_ICV)
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to set the maximum number of open minc files.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : December 6, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_max_open_files(Loop_Options *loop_options, 
                                    int max_open_files)
{
   if ((max_open_files <= 0) || (max_open_files > MI_MAX_NUM_ICV)) {
      (void) fprintf(stderr, 
                     "Bad number of files %d in set_loop_max_open_files\n",
                     max_open_files);
      exit(EXIT_FAILURE);
   }

   loop_options->max_open_files = max_open_files;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_check_dim_info
@INPUT      : loop_options - user options for looping
              check_dim_info - TRUE if all dimension information in input
                 files should be check, FALSE otherwise
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to turn dimension info checking on or off.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 16, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_check_dim_info(Loop_Options *loop_options, 
                                    int check_dim_info)
{
   loop_options->check_all_input_dim_info = check_dim_info;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_convert_input_to_scalar
@INPUT      : loop_options - user options for looping
              convert_input_to_scalar - TRUE if input should be converted
                 to scalar values
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to allow input to be converted to scalar values
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : December 6, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_convert_input_to_scalar(Loop_Options *loop_options, 
                                             int convert_input_to_scalar)
{
   loop_options->convert_input_to_scalar = convert_input_to_scalar;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_output_vector_size
@INPUT      : loop_options - user options for looping
              output_vector_size - length of vector dimension for output.
                 0 means keep the size the same as the input.
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to turn set the length of the vector dimension for 
              output.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : December 6, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_output_vector_size(Loop_Options *loop_options, 
                                        int output_vector_size)
{
   if (output_vector_size < 0) {
      (void) fprintf(stderr, 
                     "Bad vector size %d in set_loop_output_vector_size\n",
                     output_vector_size);
      exit(EXIT_FAILURE);
   }
   loop_options->output_vector_size = output_vector_size;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_first_input_mincid
@INPUT      : loop_options - user options for looping
              input_mincid - id of first minc file (already opened).
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to turn allow the user to pass in an already opened minc
              file.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : December 6, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_first_input_mincid(Loop_Options *loop_options, 
                                        int input_mincid)
{
   loop_options->input_mincid = input_mincid;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_buffer_size
@INPUT      : loop_options - user options for looping
              buffer_size - maximum amount of buffer space to use (in bytes).
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to turn set a limit on the amount of buffer space used.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : December 6, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_buffer_size(Loop_Options *loop_options,
                                 long buffer_size)
{
   if (buffer_size <= 0) {
      (void) fprintf(stderr, "Bad buffer size %d in set_loop_buffer_size\n",
                     (int) buffer_size);
   }

   loop_options->total_copy_space = buffer_size;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_dimension
@INPUT      : loop_options - user options for looping
              dimension_name - name of dimension that is treated like a series
                 of input files.
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to allow a dimension to be treated like a series of
              input files (one buffer per dimension element per file).
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 24, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_dimension(Loop_Options *loop_options,
                               char *dimension_name)
{
   if (loop_options->loop_dimension != NULL)
      FREE(loop_options->loop_dimension);

   if ((dimension_name == NULL) || ((int)strlen(dimension_name) <= 0)) {
      loop_options->loop_dimension = NULL;
   }
   else {
      loop_options->loop_dimension = strdup(dimension_name);
   }
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_input_file_function
@INPUT      : loop_options - user options for looping
              input_file_function - function to be called for each
                 input file so that the user can extract any extract
                 information.
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to allow the user to define a function to be called
              for each input file before processing is done.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 20, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_input_file_function
   (Loop_Options *loop_options,
    VoxelInputFileFunction input_file_function)
{
   loop_options->input_file_function = input_file_function;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_output_file_function
@INPUT      : loop_options - user options for looping
              output_file_function - function to be called for each
                 output file so that the user can modify the header (file
                 is in define mode).
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to allow the user to define a function to be called
              for each output file so that things can be added to the header. 
              The file will be in define mode and should remain that way.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : December 6, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_output_file_function
   (Loop_Options *loop_options,
    VoxelOutputFileFunction output_file_function)
{
   loop_options->output_file_function = output_file_function;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_copy_all_header
@INPUT      : loop_options - user options for looping
              copy_all_header - TRUE if all header info should be copied
                 to output file
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to turn copying of all header info off.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 13, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_copy_all_header(Loop_Options *loop_options, 
                                     int copy_all_header)
{
   loop_options->copy_all_header_info = copy_all_header;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_accumulate
@INPUT      : loop_options - user options for looping
              do_accumulation - TRUE if accumulating should be done,
                 FALSE otherwise.
              num_extra_buffers - number of extra buffers to allocate.
              start_function - function to be called before looping with 
                 all output and extra buffers as arguments. NULL means
                 don't call any function.
              finish_function - function to be called after looping with
                 all output and extra buffers as arguments. NULL means
                 don't call any function.
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to turn allow an accumulation mode of looping. Instead
              of loading all input files and then calling the voxel routine,
              the voxel routine is called after for each input file. The
              user can provide a start_function that is called to initialize
              the output buffers and a finish_function that is called to
              finish calculations. The user can also ask for extra buffers.
              These are treated like output buffers (at the end of the list)
              but are not written out.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : December 6, 1994 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void set_loop_accumulate(Loop_Options *loop_options, 
                                int do_accumulation,
                                int num_extra_buffers,
                                VoxelStartFunction start_function,
                                VoxelFinishFunction finish_function)
{
   loop_options->do_accumulate = do_accumulation;

   /* Turning off accumulation */
   if (!do_accumulation) {
      loop_options->num_extra_buffers = 0;
      loop_options->start_function = NULL;
      loop_options->finish_function = NULL;
   }

   /* Turning on accumulation */
   else {
      if (num_extra_buffers < 0) {
         (void) fprintf(stderr, 
                        "Bad num_extra_buffers %d in set_loop_accumulate\n",
                        num_extra_buffers);
         exit(EXIT_FAILURE);
      }
      loop_options->do_accumulate = TRUE;
      loop_options->num_extra_buffers = num_extra_buffers;
      loop_options->start_function = start_function;
      loop_options->finish_function = finish_function;
   }
   
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_loop_allocate_buffer_function
@INPUT      : loop_options - user options for looping
              allocate_buffer_function - user function that allocates and frees
                 input_data
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to set the function that allocates and frees input_data
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : Aug 29, 2000 (J. Taylor)
@MODIFIED   : Sept. 19, 2000 (P. Neelin)
---------------------------------------------------------------------------- */
MNCAPI void set_loop_allocate_buffer_function(Loop_Options *loop_options, 
                         AllocateBufferFunction allocate_buffer_function)

{
   loop_options->allocate_buffer_function = allocate_buffer_function;
}

/* ------------ Routines to set and get loop info ------------ */

/* ----------------------------- MNI Header -----------------------------------
@NAME       : create_loop_info
@INPUT      : (none)
@OUTPUT     : (none)
@RETURNS    : Pointer to Loop_Info structure
@DESCRIPTION: Routine to create and initialize the loop info structure.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 20, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE Loop_Info *create_loop_info(void)
{
   Loop_Info *loop_info;

   /* Allocate structure */
   loop_info = MALLOC(1, Loop_Info);

   /* Fill in the defaults */
   initialize_loop_info(loop_info);

   /* Return the structure pointer */
   return loop_info;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : initialize_loop_info
@INPUT      : loop_info - pointer to Loop_Info structure
@OUTPUT     : (none)
@RETURNS    : (nothing)
@DESCRIPTION: Routine to initialize the loop info structure.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : February 28, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void initialize_loop_info(Loop_Info *loop_info)
{
   int idim;

   /* Fill in the defaults */
   loop_info->current_file = 0;
   loop_info->current_index = 0;
   for (idim=0; idim < MAX_VAR_DIMS; idim++) {
      loop_info->start[idim] = 0;
      loop_info->count[idim] = 0;
   }
   loop_info->loopfile_info = NULL;

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : free_loop_info
@INPUT      : loop_info - pointer to structure to cleanup
@OUTPUT     : (none)
@RETURNS    : (none)
@DESCRIPTION: Routine to cleanup and free the Loop_Info structure
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 20, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void free_loop_info(Loop_Info *loop_info)
{
   FREE(loop_info);
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_info_shape
@INPUT      : loop_info - info structure pointer
              start - start of hyperslab
              count - count of hyperslab
@OUTPUT     : (none)
@RETURNS    : (none)
@DESCRIPTION: Routine to set the hyperslab shape (start and count)
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 20, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void set_info_shape(Loop_Info *loop_info, long start[], long count[])
{
   int idim;
   long size;

   /* Save the shape of the current chunk */
   for (idim=0; idim < MAX_VAR_DIMS; idim++) {
      loop_info->start[idim] = start[idim];
      loop_info->count[idim] = count[idim];
   }

   /* Figure out how many voxels will be skipped by a step of one 
      in each dimension */
   loop_info->dimvoxels[MAX_VAR_DIMS-1] = 1;
   for (idim=MAX_VAR_DIMS-2; idim >= 0; idim--) {
      size = loop_info->count[idim+1];
      if (size <= 0) size = 1;
      loop_info->dimvoxels[idim] = size * loop_info->dimvoxels[idim+1];
   }

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_info_shape
@INPUT      : loop_info - info structure pointer
              ndims - number of dimensions to copy (uses MAX_VAR_DIMS
                 if ndims == 0)
@OUTPUT     : start - start of hyperslab
              count - count of hyperslab
@RETURNS    : (none)
@DESCRIPTION: Routine to get the current hyperslab shape (start and count)
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : January 20, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void get_info_shape(Loop_Info *loop_info, int ndims,
                           long start[], long count[])
{
   int idim;

   if ((ndims <= 0) || (ndims > MAX_VAR_DIMS))
      ndims = MAX_VAR_DIMS;
   for (idim=0; idim < ndims; idim++) {
      start[idim] = loop_info->start[idim];
      count[idim] = loop_info->count[idim];
   }
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_info_voxel_index
@INPUT      : loop_info - info structure pointer
              subscript - 1-D voxel index into data buffers of voxel function
              ndims - number of dimensions in index array (uses MAX_VAR_DIMS
                 if ndims == 0)
@OUTPUT     : index - multi-dimensional voxel index into file
@RETURNS    : (none)
@DESCRIPTION: Routine to get the current voxel index.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : November 28, 2001 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI void get_info_voxel_index(Loop_Info *loop_info, long subscript, 
                                 int ndims, long index[])
{
   int idim;
   long this_index;

   /* Check the array size */
   if ((ndims <= 0) || (ndims > MAX_VAR_DIMS))
      ndims = MAX_VAR_DIMS;

   /* Convert the 1-D subscript into a multi-dim index and add it to
      the start index of the chunk */
   for (idim=0; idim < ndims; idim++) {
      this_index = subscript / loop_info->dimvoxels[idim];
      index[idim] = loop_info->start[idim] + this_index;
      subscript -= this_index * loop_info->dimvoxels[idim];
   }
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_info_current_file
@INPUT      : loop_info - info structure pointer
              current_file - number of current input file (for 
                 accumulation)
@OUTPUT     : (none)
@RETURNS    : (none)
@DESCRIPTION: Routine to set the current input file number.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : February 28, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void set_info_current_file(Loop_Info *loop_info, int current_file)
{

   loop_info->current_file = current_file;

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_info_current_file
@INPUT      : loop_info - info structure pointer
@OUTPUT     : (none)
@RETURNS    : Number of current input file (for accumulating over files)
@DESCRIPTION: Routine to get the current input file number.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : February 28, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI int get_info_current_file(Loop_Info *loop_info)
{

   return loop_info->current_file;

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_info_current_mincid
@INPUT      : loop_info - info structure pointer
@OUTPUT     : (none)
@RETURNS    : Minc id for current input file (for accumulating over files)
@DESCRIPTION: Routine to get the current input file mincid.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 8, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI int get_info_current_mincid(Loop_Info *loop_info)
{

   if (loop_info->loopfile_info == NULL) return MI_ERROR;
   return get_input_mincid(loop_info->loopfile_info, loop_info->current_file);

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_info_current_index
@INPUT      : loop_info - info structure pointer
              current_index - number of current dimension index (for 
                 accumulation over files and dimension)
@OUTPUT     : (none)
@RETURNS    : (none)
@DESCRIPTION: Routine to set the current dimension index.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : February 28, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void set_info_current_index(Loop_Info *loop_info, int current_index)
{

   loop_info->current_index = current_index;

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_info_current_index
@INPUT      : loop_info - info structure pointer
@OUTPUT     : (none)
@RETURNS    : Number of current dimension index (for accumulating over files)
@DESCRIPTION: Routine to get the current dimension index.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : February 28, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI int get_info_current_index(Loop_Info *loop_info)
{

   return loop_info->current_index;

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : set_info_loopfile_info
@INPUT      : loop_info - info structure pointer
              loopfile_info - loopfile info structure pointer
@OUTPUT     : (none)
@RETURNS    : (none)
@DESCRIPTION: Routine to set the loopfile info structure for future queries
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 7, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
PRIVATE void set_info_loopfile_info(Loop_Info *loop_info, 
                                    Loopfile_Info *loopfile_info)
{

   loop_info->loopfile_info = loopfile_info;

}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_info_whole_file
@INPUT      : loop_info - info structure pointer
@OUTPUT     : (none)
@RETURNS    : Id of current minc file
@DESCRIPTION: Routine to change minc file handling to get the whole input 
              file, not just the header (should be called from within the 
              input_file_function).
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : March 7, 1995 (Peter Neelin)
@MODIFIED   : 
---------------------------------------------------------------------------- */
MNCAPI int get_info_whole_file(Loop_Info *loop_info)
{
   Loopfile_Info *loopfile_info;

   /* Check for NULL pointer */
   if (loop_info->loopfile_info == NULL) return MI_ERROR;
   loopfile_info = loop_info->loopfile_info;

   /* Make the input non-sequential (hold the files open if possible), and
      ask for whole files */
   set_input_sequential(loopfile_info, FALSE);
   set_input_headers_only(loopfile_info, FALSE);

   /* Return the current minc file id */
   if (loop_info->current_file >= 0)
      return get_input_mincid(loopfile_info, loop_info->current_file);
   else
      return MI_ERROR;

}