File: hdfimport.c

package info (click to toggle)
libhdf4 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 29,892 kB
  • sloc: ansic: 128,688; sh: 14,969; fortran: 12,444; java: 5,864; xml: 1,305; makefile: 900; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (3295 lines) | stat: -rw-r--r-- 112,000 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF copyright notice, including       *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Name:
 *      hdfimport (previously fp2hdf)
 *
 * Purpose:
 *      To convert floating point and/or integer data to HDF Scientific Data Set (SDS)
 *      and/or 8-bit Raster Image Set (RIS8) format, storing the results
 *      in an HDF file.  The image data can be scaled about the mean value.
 *
 *                                  -------------
 *      floating point data        |             | ----------> RIS8
 *      (SDS, ASCII text, or  ---> | hdfimport   |   and/or
 *      native floating point)     |             | ----------> SDS
 *                                  -------------
 *                       AND / OR
 *                               ---------------
 *      integer data               |              |
 *      (ASCII text, or       ---> |  hdfimport   |  ----------> SDS
 *      binary integer)            |              |
 *                                  --------------
 * Synopsis:
 *      hdfimport -h[elp], OR
 *      hdfimport <infile> [ [-t[ype] <output-type> | -n] [<infile> [-t[ype] <output-type> | -n ]]...]
 *                            -o[utfile] <outfile> [-r[aster] [ras_opts ...]] [-f[loat]]
 *
 *      -h[elp]:
 *              Print this summary of usage, and exit.
 *
 *      <infile(s)>:
 *              Name of the input file(s), containing a single
 *        two-dimensional or three-dimensional floating point array
 *        in either ASCII text, native floating point, native integer
 *        or HDF SDS format.  If an HDF file is used for input, it
 *        must contain an SDS. The SDS need only contain a dimension
 *        record and the data, but if it also contains maximum and
 *        minimum values and/or scales for each axis, these will
 *        be used.  If the input format is ASCII text or native
 *        floating point or native integer, see "Notes" below on
 *        how it must be organized.
 *
 *      -t[ype] <output_type>:
 *              Optionally used for every input ASCII file to specify the
 *            data type of the data-set to be written. If not specified
 *              default data type is 32-bit floating point. <output-type>
 *              can be any of the following: FP32 (default), FP64, INT32
 *             INT16, INT8. It can be used only with ASCII files.
 *
 *      -n:
 *        This option is to be used only if the binary input file
 *        contains 64-bit floating point data and the default
 *        behaviour (default behaviour is to write it to a 32-bit
 *        floating point data-set) should be overridden to write
 *        it to a 64-bit floating point data-set.
 *
 *      -o[utfile] <outfile>:
 *              Data from one or more input files are stored as one or
 *              more data sets and/or images in one HDF output file,
 *              "outfile".
 *
 *      -r[aster]:
 *              Store output as a raster image set in the output file.
 *
 *      -f[loat]:
 *              Store output as a scientific data set in the output file.
 *              This is the default if the "-r" option is not specified.
 *
 *      ras_opts ...
 *
 *      -e[xpand] <horiz> <vert> [<depth>]:
 *              Expand float data via pixel replication to produce the
 *              image(s).  "horiz" and "vert" give the horizontal and
 *              vertical resolution of the image(s) to be produced; and
 *              optionally, "depth" gives the number of images or depth
 *              planes (for 3D input data).
 *
 *      -i[nterp] <horiz> <vert> [<depth>]:
 *              Apply bilinear, or trilinear, interpolation to the float
 *              data to produce the image(s).  "horiz", "vert", and "depth"
 *              must be greater than or equal to the dimensions of the
 *              original dataset.
 *      If max and min are supplied in input file, this option clips
 *      values that are greater than max or less then min, setting
 *      them to the max and min, respectively.
 *
 *      -p[alfile] <palfile>:
 *              Store the palette with the image.  Get the palette from
 *              "palfile"; which may be an HDF file containing a palette,
 *              or a file containing a raw palette.
 *
 *      -m[ean] <mean>:
 *              If a floating point mean value is given, the image will be
 *              scaled about the mean.  The new extremes (newmax and newmin),
 *              as given by:
 *
 *                 newmax = mean + max(abs(max-mean), abs(mean-min))
 *                 newmin = mean - max(abs(max-mean), abs(mean-min))
 *
 *              will be equidistant from the mean value.  If no mean value
 *              is given, then the mean will be:  0.5 * (max + min)
 *
 * Notes:
 *      If the input file format is ASCII text or native floating point or native integer(32-bit,
 *      16-bit, 8-bit), it
 *      must have the following input fields:
 *
 *              format
 *              nplanes
 *              nrows
 *              ncols
 *              max_value
 *              min_value
 *              [plane1 plane2 plane3 ...]
 *              row1 row2 row3 ...
 *              col1 col2 col3 ...
 *              data1 data2 data3 ...
 *              ...
 *
 *      Where:
 *              format:
 *                      Format designator ("TEXT", "FP32", "FP64", "IN32", "IN16", "IN08").
 *                      nplanes, nrows, ncols:
 *                      Dimensions are specified in the order slowest changing dimension first.
 *            ncols is dimension of the fastest changing dimension. (horizontal axis
 *            or X-axis in a 3D scale)
 *            nrows corresponds to dimension of the vertical axis or Y-axis in a 3D
 *            scale.
 *            nplanes corresponds to the slowest changing dimension i.e. dimension of
 *            the depth axis or the Z-axis in a 3D scale ("1" for 2D input).
 *              max_value:
 *                      Maximum data value.
 *              min_value:
 *                      Minimum data value.
 *              plane1, plane2, plane3, ...:
 *                      Scales for depth axis.
 *              row1, row2, row3, ...:
 *                      Scales for the vertical axis.
 *              col1, col2, col3, ...:
 *                      Scales for the horizontal axis.
 *              data1, data2, data3, ...:
 *                      The data ordered by rows, left to right and top
 *                      to bottom; then optionally, ordered by planes,
 *                      front to back.
 *
 *      For FP32 and FP64 input format, "format", "nplanes", "nrows", "ncols",
 *      and "nplanes" are native integers; where "format" is the integer
 *      representation of the appropriate 4-character string (0x46503332 for
 *      "FP32" and 0x46503634 for "FP64").  The remaining input fields are
 *      composed of native 32-bit floating point values for FP32 input format,
 *      or native 64-bit floating point values for FP64 input format.
 *
 *      For IN32, IN16 and IN08 input format, "format", "nplanes", "nrows", "ncols",
 *      and "nplanes" are native integers; where "format" is the integer
 *      representation of the appropriate 4-character string. The remaining input
 *      fields are composed of native 32-bit integer values for IN32 input format,
 *      or native 16-bit integer values for IN16 input format or native 8-bit
 *      integer values for IN08 input format.
 *
 */

#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "hdf.h"
#include "hfile_priv.h"
#include "mfhdf.h"

#ifdef H4_HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#ifdef H4_HAVE_FCNTL_H
#include <fcntl.h>
#endif

/*
 * global macros
 */
#define EXPAND   1 /* -e: expand image with pixel replication */
#define INTERP   2 /* -i: expand image with interpolation */
#define NAME_LEN 255

/*
 * structure definition to associate input files with the output data types
 */
struct infilesformat {
    char  filename[NAME_LEN];
    int   outtype; /* if the value is "" output type will be FP32. Applicable only to TEXT Files*/
    int32 handle;  /* added to facilitate the use of SD interface -BMR 2006/08/18 */
};
/*
 * structure definition for command line options
 */
struct Options {
    struct infilesformat infiles[30]; /* structure to hold the list of input file names. Limited to 30*/
    char                 outfile[32]; /* output file name */
    char                 palfile[32]; /* palette file name, if any */
    int                  fcount;      /* number of input files */
    int                  to_float;    /* float output is desired */
    int                  to_image;    /* image output is desired */
    int                  to_int;
    int                  pal;     /* output palette with image */
    int                  ctm;     /* color transform method: EXPAND or INTERP */
    int                  exh;     /* horizontal expansion factor */
    int                  exv;     /* vertical expansion factor */
    int                  exd;     /* depth expansion factor */
    int                  hres;    /* horizontal resolution of output image */
    int                  vres;    /* vertical resolution of output image */
    int                  dres;    /* depth resolution of output image */
    int                  mean;    /* scale image around a mean */
    float32              meanval; /* mean value to scale the image around */
};

/* Additional Structures to handle different data types */
struct int16set /* variables for an INT 16 data set */
{
    int16  max;
    int16  min;
    int16 *hscale;
    int16 *vscale;
    int16 *dscale;
};

struct int32set /* variables for an INT 32 data set */
{
    int32  max;
    int32  min;
    int32 *hscale;
    int32 *vscale;
    int32 *dscale;
};

struct fp64set /* variables for a FLOAT 64 data set */
{
    float64  max;
    float64  min;
    float64 *hscale;
    float64 *vscale;
    float64 *dscale;
};

struct int8set /* variables for an INT 8 data set */
{
    int8  max;
    int8  min;
    int8 *hscale;
    int8 *vscale;
    int8 *dscale;
};

/*
 * structure definition for the input data
 */
struct Input {
    int             is_hdf;    /* HDF file format flag */
    int             is_text;   /* ASCII text format flag */
    int             is_fp32;   /* 32-bit native floating point format flag */
    int             is_fp64;   /* 64-bit native floating point format flag */
    int             is_int32;  /* 32-bit int */
    int             is_int16;  /* 16-bit int */
    int32           rank;      /* number of input data dimensions */
    int32           dims[3];   /* input dimensions - ncols, nrows, nplanes */
    int             is_vscale; /* vertical axis scales in the input */
    int             is_hscale; /* horizontal axis scales in the input */
    int             is_dscale; /* depth axis scales in the input */
    float32         max;       /* maximum value of the data */
    float32         min;       /* minimum value of the data */
    float32        *hscale;    /* horizontal scales for fp32*/
    float32        *vscale;    /* vertical scales for fp32*/
    float32        *dscale;    /* depth scales for fp32*/
    struct int32set in32s;
    struct int16set in16s;
    struct int8set  in8s;
    struct fp64set  fp64s;
    void           *data; /* input data */
    int             outtype;
};

/*
 * structure definition for the output raster images
 */
struct Raster {
    int            hres; /* horizontal resolution of the image */
    int            vres; /* vertical resolution of the image */
    int            dres; /* depth resolution of the image */
    unsigned char *image;
};

/*
 *  constants to represent data types
 */

#define FP_32  0
#define FP_64  1
#define INT_32 2
#define INT_16 3
#define INT_8  4
#define NO_NE  5

/*
 * state table tokens
 */
#define FILENAME 0  /* filename */
#define OPT_o    1  /* output filename */
#define OPT_r    2  /* convert to image */
#define OPT_e    3  /* expand image via pixel replication */
#define OPT_i    4  /* make interpolated image */
#define OPT_num  5  /* resolution of enlarged image */
#define OPT_p    6  /* palette filename */
#define OPT_f    7  /* convert to float (default) */
#define OPT_h    8  /* request for explanation */
#define OPT_m    9  /* mean to scale around */
#define OPT_t    10 /* datatype of the SDS to be written */
#define OPT_n                                                                                                \
    11 /* for  a FLOAT 64 binary input file to be accepted as FLOAT 64 SDS (default behaviour is writing it  \
          as FLOAT 32 SDS */
#define ERR 20 /* invalid token */

/*
 * state table for parsing the command line.
 */
static int state_table[19][12] = {

    /* token ordering:
       FILENAME     OPT_o   OPT_r   OPT_e   OPT_i   OPT_num   OPT_p   OPT_f
       OPT_h        OPT_m   OPT_z */

    /* state 0: start */
    {1, ERR, ERR, ERR, ERR, ERR, ERR, ERR, 14, ERR, ERR, ERR},

    /* state 1: input files */
    {1, 2, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, 17, 18},

    /* state 2: -o[utfile] */
    {3, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR},

    /* state 3: outfile */
    {ERR, ERR, 4, ERR, ERR, ERR, ERR, 13, ERR, ERR, ERR, ERR},

    /* state 4: -r[aster] */
    {ERR, ERR, ERR, 5, 9, ERR, 10, 12, ERR, 15, ERR, ERR},

    /* state 5: -e[xpand] */
    {ERR, ERR, ERR, ERR, ERR, 6, ERR, ERR, ERR, ERR, ERR, ERR},

    /* state 6: -e[xpand] or -i[nterp] option argument */
    {ERR, ERR, ERR, ERR, ERR, 7, ERR, ERR, ERR, ERR, ERR, ERR},

    /* state 7: -e[xpand] or -i[nterp] option argument */
    {ERR, ERR, ERR, ERR, ERR, 8, 10, 12, ERR, 15, ERR, ERR},

    /* state 8: -e[xpand] or -i[nterp] option argument */
    {ERR, ERR, ERR, ERR, ERR, ERR, 10, 12, ERR, 15, ERR, ERR},

    /* state 9: -i[nterp] */
    {ERR, ERR, ERR, ERR, ERR, 6, ERR, ERR, ERR, ERR, ERR, ERR},

    /* state 10: -p[alfile] */
    {11, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR},

    /* state 11: palfile */
    {ERR, ERR, ERR, 5, 9, ERR, ERR, 12, ERR, 15, ERR, ERR},

    /* state 12: -f[loat] (after -r[aster]) */
    {ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR},

    /* state 13: -f[loat] */
    {ERR, ERR, 4, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR},

    /* state 14: -h[elp] */
    {ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR},

    /* state 15: -m[ean] */
    {ERR, ERR, ERR, ERR, ERR, 16, ERR, ERR, ERR, ERR, ERR, ERR},

    /* state 16: mean */
    {ERR, ERR, ERR, 5, 9, ERR, 10, 12, ERR, ERR, ERR, ERR},

    /* state 17: output type for data set */
    {1, 2, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR},

    /* state 18: override default behaviour for FP 64 */
    {1, 2, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR}

};

/* static local functions */
static int gtoken(char *s);
static int process(struct Options *opt);
static int gfloat(char *infile, FILE *strm, float32 *fp32, struct Input *in);
static int gint(char *infile, FILE *strm, int32 *ival, struct Input *in);
static int isnum(char *s);
static int gdata(struct infilesformat infile_info, struct Input *in, FILE *strm, int *is_maxmin);
static int gdimen(struct infilesformat infile_info, struct Input *in, FILE *strm);
static int gmaxmin(struct infilesformat infile_info, struct Input *in, FILE *strm, int *is_maxmin);
static int gscale(struct infilesformat infile_info, struct Input *in, FILE *strm, int *is_scale);
static int gtype(char *infile, struct Input *in, FILE **strm);
static int indexes(float32 *scale, int dim, int *idx, int res);
static int interp(struct Input *in, struct Raster *im);
static int palette(char *palfile);
static int pixrep(struct Input *in, struct Raster *im);

/*
 * functions with non-integer return types
 */
void help(char *);
void mean(struct Input *, struct Options *);
void usage(char *);

/*
 * Additional functions defined to incorporate the revisions (pkamat)
 */
static int gfloat64(char *infile, FILE *strm, float64 *fp64, struct Input *in);
static int gint32(char *infile, FILE *strm, int32 *ival, struct Input *in);
static int gint16(char *infile, FILE *strm, int16 *ival, struct Input *in);
static int gint8(char *infile, FILE *strm, int8 *ival, struct Input *in);
static int init_scales(struct Input *in);
void       fpdeallocate(struct Input *in, struct Raster *im, struct Options *opt);

/*
 * Name:
 *      main
 *
 * Purpose:
 *      The driver for "hdfimport".
 *
 *  Revision (pkamat):
 *        Changes to the state table to handle -t option and the -n option.
 *      Also, a different structure used for holding input files.
 */
int
main(int argc, char *argv[])
{
    struct Options *opt = NULL;
    int             i, k;
    int             outfile_named = FALSE;
    int             token;
    int             state       = 0;
    int             flag        = 0;
    char            types[5][6] = {"FP32", "FP64", "INT32", "INT16", "INT8"};

    const char *err1 = "Invalid number of arguments:  %d.\n";
    const char *err2 = "Error in state table.\n";
    const char *err3 = "No output file given.\n";
    const char *err4 = "Program aborted.\n";
    const char *err5 = "Cannot allooacte memory.\n";

    if (NULL == (opt = (struct Options *)calloc(1, sizeof(struct Options)))) {
        fprintf(stderr, "%s", err5);
        goto err;
    }

    /* set 'stdout' and 'stderr' to line-buffering mode */
    (void)HDsetvbuf(stderr, (char *)NULL, _IOLBF, 0);
    (void)HDsetvbuf(stdout, (char *)NULL, _IOLBF, 0);

    /*
     * validate the number of command line arguments
     */
    if (argc < 2) {
        fprintf(stderr, err1, argc);
        usage(argv[0]);
        goto err;
    }

    opt->to_image = FALSE; /* default: no image */
    opt->to_float = FALSE; /* default: make float if no image */
                           /* Set FALSE here.  Will be set TRUE */
                           /* after confirming image option is not set.  */
    opt->ctm    = EXPAND;  /* default: pixel replication */
    opt->hres   = 0;       /* default: no expansion values */
    opt->vres   = 0;
    opt->dres   = 0;
    opt->pal    = FALSE; /* default: no palette */
    opt->mean   = FALSE; /* default: no mean given */
    opt->fcount = 0;     /* to count number of input files */

    /*
     * parse the command line
     */
    for (i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-V") == 0) {
            printf("%s, %s\n\n", argv[0], LIBVER_STRING);
            exit(0);
        }

        if ((token = gtoken(argv[i])) == ERR) {
            usage(argv[0]);
            goto err;
        }

        state = state_table[state][token];

        switch (state) {
            case 1: /* counting input files */
                (void)strcpy(opt->infiles[opt->fcount].filename, argv[i]);
                opt->infiles[opt->fcount].outtype = NO_NE;
                opt->fcount++;
                break;
            case 2: /* -o found; look for outfile */
                break;
            case 3: /* get outfile name */
                (void)strcpy(opt->outfile, argv[i]);
                outfile_named = TRUE;
                break;
            case 4: /* -r found */
                opt->to_image = TRUE;
                break;
            case 5: /* -e found */
                opt->ctm = EXPAND;
                break;
            case 6: /* horizontal resolution */
                opt->hres = atoi(argv[i]);
                break;
            case 7: /* vertical resolution */
                opt->vres = atoi(argv[i]);
                break;
            case 8: /* depth resolution */
                opt->dres = atoi(argv[i]);
                break;
            case 9: /* -i found */
                opt->ctm = INTERP;
                break;
            case 10: /* -p found */
                opt->pal = TRUE;
                break;
            case 11: /* get pal filename */
                (void)strcpy(opt->palfile, argv[i]);
                break;
            case 12: /* -f found (after a -r) */
            case 13: /* -f found (no -r yet) */
                opt->to_float = TRUE;
                break;
            case 14: /* -h found; help, then exit */
                help(argv[0]);
                exit(0);
            case 15: /* -m found */
                opt->mean = TRUE;
                break;
            case 16: /* mean value */
                opt->meanval = (float32)atof(argv[i]);
                break;
            case 17: /* -t found */
                i++;
                flag = 0;
                for (k = 0; ((k <= 4) && (!flag)); k++)
                    if (!strcmp(argv[i], types[k]))
                        flag = 1;
                if (flag)
                    opt->infiles[opt->fcount - 1].outtype = k - 1;
                else {
                    usage(argv[0]);
                    goto err;
                }
                break;
            case 18: /* -n found */
                opt->infiles[opt->fcount - 1].outtype = FP_64;
                break;
            case ERR: /* command syntax error */
            default:
                fprintf(stderr, "%s", err2);
                usage(argv[0]);
                goto err;
        }
    }

    /*
     * make sure an output file was specified
     */
    if (!outfile_named) {
        fprintf(stderr, "%s", err3);
        usage(argv[0]);
        goto err;
    }

    if (!opt->to_image)
        opt->to_float = TRUE;

    /*
     * process the input files
     */
    if (process(opt))
        goto err;

    free(opt);

    return EXIT_SUCCESS;

err:
    free(opt);
    fprintf(stderr, "%s", err4);
    return EXIT_FAILURE;
}

/*
 * Name:
 *      gdata
 *
 * Purpose:
 *      Get the input data.
 *
 * Revision(pkamat):
 *      Modified to read in data of type INT 32, INT 16, INT 8
 *      in addition to FP 32 and FP 64.
 * Revision: (bmribler - 2006/8/18)
 *    Replaced first parameter with 'struct infilesformat' to use both
 *    the file name and the SD identifier (handle.)
 */
static int
gdata(struct infilesformat infile_info, struct Input *in, FILE *strm, int *is_maxmin)
{
    int32       i, j, k;
    float32    *fp32;
    int32      *in32;
    int16      *in16;
    float64    *fp64;
    int8       *in8;
    int32       hdfdims[3], start[3]; /* order: ZYX or YX */
    int32       sd_id, sds_id, sd_index;
    int32       len = in->dims[0] * in->dims[1] * in->dims[2];
    char        infile[NAME_LEN];
    intn        status;
    const char *err1 = "Unable to get input data from file: %s.\n";

    /*
     * extract the input data from the input file
     */
    if (in->is_hdf == TRUE) {
        sd_id = infile_info.handle;
        strcpy(infile, infile_info.filename);
        sd_index = 0;
        sds_id   = SDselect(sd_id, sd_index);

        /*
         * hdfdims is ordered: ZYX or YX
         * in->dims is ordered: XYZ
         */
        if (in->rank == 2) {
            hdfdims[0] = in->dims[1];
            hdfdims[1] = in->dims[0];
            start[0] = start[1] = 0;
        }
        else {
            hdfdims[0] = in->dims[2];
            hdfdims[1] = in->dims[1];
            hdfdims[2] = in->dims[0];
            start[0] = start[1] = start[2] = 0;
        }

        status = SDreaddata(sds_id, start, NULL, hdfdims, in->data);
        if (status == FAIL) {
            fprintf(stderr, err1, infile);
            goto err;
        }
    }
    else {
        if (in->outtype == FP_32) {
            for (k = 0, fp32 = (float32 *)in->data; k < in->dims[2]; k++) {
                for (j = 0; j < in->dims[1]; j++) {
                    for (i = 0; i < in->dims[0]; i++, fp32++) {
                        if (gfloat(infile, strm, fp32, in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                }
            }
            if (*is_maxmin == FALSE) {
                in->min = in->max = *(float32 *)in->data;
                for (i = 1; i < len; i++) {
                    if (((float32 *)in->data)[i] > in->max)
                        in->max = ((float32 *)in->data)[i];
                    if (((float32 *)in->data)[i] < in->min)
                        in->min = ((float32 *)in->data)[i];
                }
                *is_maxmin = TRUE;
            }
        }
        if (in->outtype == INT_32) {
            for (k = 0, in32 = (int32 *)in->data; k < in->dims[2]; k++) {
                for (j = 0; j < in->dims[1]; j++) {
                    for (i = 0; i < in->dims[0]; i++, in32++) {
                        if (gint32(infile, strm, in32, in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                }
            }
            if (*is_maxmin == FALSE) {
                in->in32s.min = in->in32s.max = *(int32 *)in->data;
                for (i = 1; i < len; i++) {
                    if (((int32 *)in->data)[i] > in->in32s.max)
                        in->in32s.max = ((int32 *)in->data)[i];
                    if (((int32 *)in->data)[i] < in->in32s.min)
                        in->in32s.min = ((int32 *)in->data)[i];
                }
                *is_maxmin = TRUE;
            }
        }
        if (in->outtype == INT_16) {
            for (k = 0, in16 = (int16 *)in->data; k < in->dims[2]; k++) {
                for (j = 0; j < in->dims[1]; j++) {
                    for (i = 0; i < in->dims[0]; i++, in16++) {
                        if (gint16(infile, strm, in16, in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                }
            }
            if (*is_maxmin == FALSE) {
                in->in16s.min = in->in16s.max = *(int16 *)in->data;
                for (i = 1; i < len; i++) {
                    if (((int16 *)in->data)[i] > in->in16s.max)
                        in->in16s.max = ((int16 *)in->data)[i];
                    if (((int16 *)in->data)[i] < in->in16s.min)
                        in->in16s.min = ((int16 *)in->data)[i];
                }
                *is_maxmin = TRUE;
            }
        }

        if (in->outtype == INT_8) {
            for (k = 0, in8 = (int8 *)in->data; k < in->dims[2]; k++) {
                for (j = 0; j < in->dims[1]; j++) {
                    for (i = 0; i < in->dims[0]; i++, in8++) {
                        if (gint8(infile, strm, in8, in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                }
            }
            if (*is_maxmin == FALSE) {
                in->in8s.min = in->in8s.max = *(int8 *)in->data;
                for (i = 1; i < len; i++) {
                    if (((int8 *)in->data)[i] > in->in8s.max)
                        in->in8s.max = ((int8 *)in->data)[i];
                    if (((int8 *)in->data)[i] < in->in8s.min)
                        in->in8s.min = ((int8 *)in->data)[i];
                }
                *is_maxmin = TRUE;
            }
        }

        if (in->outtype == FP_64) {
            for (k = 0, fp64 = (float64 *)in->data; k < in->dims[2]; k++) {
                for (j = 0; j < in->dims[1]; j++) {
                    for (i = 0; i < in->dims[0]; i++, fp64++) {
                        if (gfloat64(infile, strm, fp64, in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                }
            }
            if (*is_maxmin == FALSE) {
                in->fp64s.min = in->fp64s.max = *(float64 *)in->data;
                for (i = 1; i < len; i++) {
                    if (((float64 *)in->data)[i] > in->fp64s.max)
                        in->fp64s.max = ((float64 *)in->data)[i];
                    if (((float64 *)in->data)[i] < in->fp64s.min)
                        in->fp64s.min = ((float64 *)in->data)[i];
                }
                *is_maxmin = TRUE;
            }
        }

        /* } */
        (void)fclose(strm);
    }

#ifdef DEBUG
    printf("\tdata:");
    for (k = 0, fp32 = in->data; k < in->dims[2]; k++) {
        printf("\n");
        for (j = 0; j < in->dims[1]; j++) {
            printf("\n\t");
            for (i = 0; i < in->dims[0]; i++, fp32++)
                printf("%E ", *fp32);
        }
    }
    printf("\n\n\n");
#endif /* DEBUG */

    return (0);

err:
    return (1);
}

/*
 * Name:
 *      gdimen
 *
 * Purpose:
 *      Determine the input data dimensions.
 * Revision: (bmribler - 2006/8/18)
 *    Used the SD interface instead of DFSD.
 *    Replaced first parameter with 'struct infilesformat' to use both
 *    the file name and the SD identifier (handle.)
 */

static int
gdimen(struct infilesformat infile_info, struct Input *in, FILE *strm)
{
    int32 hdfdims[3];                      /* order: ZYX or YX */
    char *infile   = infile_info.filename; /* shortcut for input filename */
    char *sds_name = NULL;
    int32 rank, nattrs, dtype; /* rank, num of attrs, data type */

    const char *err1 = "Unable to get data dimensions from file: %s.\n";
    const char *err2 = "Invalid data rank of %d in file: %s.\n";
    const char *err3 = "Dimension(s) is less than '2' in file: %s.\n";
    const char *err4 = "Unexpected number type from file: %s.\n";
    const char *err5 = "Unable to get the length of the SDS' name: index %d.\n";
    const char *err6 = "Unable to allocate dynamic memory.\n";
    const char *err7 = "Failed to open the SDS.\n";

    /*
     * extract the rank and dimensions of the HDF input file
     */
    if (in->is_hdf == TRUE) {
        int32  sds_id, sd_index;
        int32  sd_id    = infile_info.handle; /* shortcut for handle from SDstart */
        uint16 name_len = 0;
        intn   status   = FAIL;

        /* get the dimension information of the only SDS in the file */
        sd_index = 0;
        sds_id   = SDselect(sd_id, sd_index);
        if (sds_id == FAIL) {
            fprintf(stderr, "%s", err7);
            goto err;
        }

        /* get the SDS name's length and allocate sufficient space for
        the name's buffer */
        status = SDgetnamelen(sds_id, &name_len);
        if (status == FAIL) {
            fprintf(stderr, err5, sd_index);
            goto err;
        }
        sds_name = (char *)malloc(name_len + 1);
        if (sds_name == NULL) {
            fprintf(stderr, "%s", err6);
            goto err;
        }

        /* obtain the SDS' information */
        status = SDgetinfo(sds_id, sds_name, &rank, hdfdims, &dtype, &nattrs);
        if (status == FAIL) {
            fprintf(stderr, err1, infile);
            goto err;
        }
        in->rank = (int)rank;

        /* don't know how to deal with other numbers yet */
        if (dtype != DFNT_FLOAT32) {
            fprintf(stderr, err4, infile);
            goto err;
        }

        /*
         * hdfdims is ordered: ZYX or YX
         * in->dims is ordered: XYZ
         */
        if (in->rank == 2) {
            in->dims[0] = hdfdims[1];
            in->dims[1] = hdfdims[0];
            in->dims[2] = 1;
        }
        else if (in->rank == 3) {
            in->dims[0] = hdfdims[2];
            in->dims[1] = hdfdims[1];
            in->dims[2] = hdfdims[0];
        }
        else {
            fprintf(stderr, err2, in->rank, infile);
            goto err;
        }

        /*
         * get the rank and dimensions from files of other input formats
         *
         */
    }
    else {
        if (gint(infile, strm, &in->dims[2], in)) {
            fprintf(stderr, err1, infile);
            goto err;
        }
        if (in->dims[2] > 1)
            in->rank = 3;
        else
            in->rank = 2;
        if (gint(infile, strm, &in->dims[1], in)) {
            fprintf(stderr, err1, infile);
            goto err;
        }
        if (gint(infile, strm, &in->dims[0], in)) {
            fprintf(stderr, err1, infile);
            goto err;
        }
    }

    /*
     * validate dimension sizes
     */
    if ((in->dims[0] < 2) || (in->dims[1] < 2)) {
        fprintf(stderr, err3, infile);
        goto err;
    }

#ifdef DEBUG
    printf("\nInput Information ...\n\n");
    printf("\trank:\n\n\t%d\n\n", in->rank);
    printf("\tdimensions (nplanes,nrows,ncols):\n\n");
    printf("\t%d %d %d\n\n", in->dims[2], in->dims[1], in->dims[0]);
#endif /* DEBUG */

    free(sds_name);
    return (0);

err:
    free(sds_name);
    return (1);
}

/*
 * Name:
 *      gfloat
 *
 * Purpose:
 *      Read in a single floating point value from the input stream.  The
 *      input format may either be ASCII text , 32-bit native floating point,
 *      or 64-bit native floating point.
 */
static int
gfloat(char *infile, FILE *strm, float32 *fp32, struct Input *in)
{
    float64 fp64 = 0.0;

    const char *err1 = "Unable to get 'float' value from file: %s.\n";

    if (in->is_text == TRUE) {
        if (fscanf(strm, "%e", fp32) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }
    }
    else if (in->is_fp32 == TRUE) {
        if (fread((char *)fp32, sizeof(float32), 1, strm) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }
    }
    else {
        if (fread((char *)&fp64, sizeof(float64), 1, strm) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }
        *fp32 = (float32)fp64;
    }

    return (0);

err:
    return (1);
}

/*
 * Name: (pkamat - New function)
 *      gfloat64
 *
 * Purpose:
 *      Read in a double floating point value from the input stream.  The
 *      input format may either be ASCII text ,
 *      or 64-bit native floating point.
 */

static int
gfloat64(char *infile, FILE *strm, float64 *fp64, struct Input *in)
{
    const char *err1 = "Unable to get 'float' value from file: %s.\n";

    if (in->is_text == TRUE) {
        if (fscanf(strm, "%le", fp64) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }
    }
    else {
        if (fread((char *)fp64, sizeof(float64), 1, strm) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }
    }

    return (0);

err:
    return (1);
}

/*
 * Name:
 *      gint
 *
 * Purpose:
 *      Read in a single integer value from the input stream.  The input
 *      format may either be ASCII text or a native BCD of type integer.
 */
static int
gint(char *infile, FILE *strm, int32 *ival, struct Input *in)
{
    const char *err1 = "Unable to get 'int' value from file: %s.\n";
    /*
     * process TEXT-formatted input
     */
    if (in->is_text == TRUE) {
        if (fscanf(strm, "%d", ival) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }

        /*
         * process BCD-formatted input
         */
    }
    else {
        if (fread((char *)ival, sizeof(int), 1, strm) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }
    }

    return (0);

err:
    return (1);
}

/*
 * Name: (pkamat - New function)
 *      gint32
 *
 * Purpose:
 *      Read in a single 32-bit integer value from the input stream.  The input
 *      format may either be ASCII text or a native BCD of type integer.
 */

static int
gint32(char *infile, FILE *strm, int32 *ival, struct Input *in)
{
    const char *err1 = "Unable to get 'int32' value from file: %s.\n";
    /*
     * process TEXT-formatted input
     */
    if (in->is_text == TRUE) {
        if (fscanf(strm, "%d", ival) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }

        /*
         * process BCD-formatted input
         */
    }
    else {
        if (fread((char *)ival, sizeof(int32), 1, strm) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }
    }

    return (0);

err:
    return (1);
}

/*
 * Name: (pkamat - New function)
 *      gint16
 *
 * Purpose:
 *      Read in a single 16-bit integer value from the input stream.  The input
 *      format may either be ASCII text or a native BCD of type 16-bit integer.
 */

static int
gint16(char *infile, FILE *strm, int16 *ival, struct Input *in)
{
    const char *err1 = "Unable to get 'int16' value from file: %s.\n";

    if (in->is_text == TRUE) {
        if (fscanf(strm, "%hd", ival) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }
    }

    else {
        if (fread((char *)ival, sizeof(int16), 1, strm) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }
    }

    return (0);

err:
    return (1);
}

/*
 * Name: (pkamat - New function)
 *      gint8
 *
 * Purpose:
 *      Read in a single 8-bit integer value from the input stream.  The input
 *      format may either be ASCII text or a native BCD of type 8-bit integer.
 */

static int
gint8(char *infile, FILE *strm, int8 *ival, struct Input *in)
{
    const char *err1 = "Unable to get 'int8' value from file: %s.\n";
    int16       temp;

    if (in->is_text == TRUE) {
        if (fscanf(strm, "%hd", &temp) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }
        *ival = (int8)temp;
    }
    else {
        if (fread((char *)ival, sizeof(int8), 1, strm) != 1) {
            fprintf(stderr, err1, infile);
            goto err;
        }
    }
    return (0);

err:
    return (1);
}
/*
 * Name:
 *      gmaxmin
 *
 * Purpose:
 *      Extract the maximum and minimum data values from the input file.
 *      Supports 32-bit integer, 16-bit integer, 8-bit integer, 32-bit float, 64-bit float
 * Revision: (pvn) March 14, 2006
 *      Used the SD interface instead of DFSD
 * Revision: (bmribler - 2006/8/18)
 *    Removed SDstart call here, used passed-in SD id from process() instead.
 *    Replaced first parameter with 'struct infilesformat' to use both
 *    the file name and the SD identifier (handle.)
 */
static int
gmaxmin(struct infilesformat infile_info, struct Input *in, FILE *strm, int *is_maxmin)
{
    const char *err1 = "Unable to get max/min values from file: %s.\n";

    /*
     * extract the max/min values from the input file
     */
    if (in->is_hdf == TRUE) {
        int32 sds_id, sd_index = 0;
        intn  status;

        sds_id = SDselect(infile_info.handle, sd_index);
        status = SDgetrange(sds_id, &in->max, &in->min);
        if (status != FAIL) {
            if (in->max > in->min)
                *is_maxmin = TRUE;
        }

        /* terminate access to the array. */
        if (SDendaccess(sds_id) == FAIL)
            goto err;
    }
    else /* input file is not an HDF file */
    {
        char *infile = infile_info.filename;
        if (in->outtype == FP_32) {
            if (gfloat(infile, strm, &in->max, in)) {
                fprintf(stderr, err1, infile);
                goto err;
            }
            if (gfloat(infile, strm, &in->min, in)) {
                fprintf(stderr, err1, infile);
                goto err;
            }
            if (in->max > in->min)
                *is_maxmin = TRUE;
        }
        if (in->outtype == FP_64) {
            if (gfloat64(infile, strm, &in->fp64s.max, in)) {
                fprintf(stderr, err1, infile);
                goto err;
            }
            if (gfloat64(infile, strm, &in->fp64s.min, in)) {
                fprintf(stderr, err1, infile);
                goto err;
            }
            if (in->fp64s.max > in->fp64s.min)
                *is_maxmin = TRUE;
        }
        if (in->outtype == INT_32) {
            if (gint32(infile, strm, &in->in32s.max, in)) {
                fprintf(stderr, err1, infile);
                goto err;
            }
            if (gint32(infile, strm, &in->in32s.min, in)) {
                fprintf(stderr, err1, infile);
                goto err;
            }
            if (in->in32s.max > in->in32s.min)
                *is_maxmin = TRUE;
        }

        if (in->outtype == INT_16) {
            if (gint16(infile, strm, &in->in16s.max, in)) {
                fprintf(stderr, err1, infile);
                goto err;
            }
            if (gint16(infile, strm, &in->in16s.min, in)) {
                fprintf(stderr, err1, infile);
                goto err;
            }
            if (in->in16s.max > in->in16s.min)
                *is_maxmin = TRUE;
        }

        if (in->outtype == INT_8) {
            if (gint8(infile, strm, &in->in8s.max, in)) {
                fprintf(stderr, err1, infile);
                goto err;
            }
            if (gint8(infile, strm, &in->in8s.min, in)) {
                fprintf(stderr, err1, infile);
                goto err;
            }
            if (in->in8s.max > in->in8s.min)
                *is_maxmin = TRUE;
        }
    }

#ifdef DEBUG
    printf("\tinput maximum/minimum values:\n\n");
    printf("\t%E %E\n\n", in->max, in->min);
#endif /* DEBUG */

    return (0);

err:
    return (1);
}

/*
 * Name:
 *      gscale
 *
 * Purpose:
 *      Determine the scale for each axis.
 *
 * Revision: (pkamat)
 *      Modified to support 32-bit integer, 16-bit integer, 8-bit integer in
 *        addition to 32-bit float and 64-bit float
 * Revision: (pvn) March 14, 2006
 *      Used the SD interface instead of DFSD
 * Revision: (bmribler - 2006/8/18)
 *    Removed SDstart call here, used passed-in SD id from process() instead.
 *    Replaced first parameter with 'struct infilesformat' to use both
 *    the file name and the SD identifier (handle.)
 */
static int
gscale(struct infilesformat infile_info, struct Input *in, FILE *strm, int *is_scale)
{
    int   i;
    int32 hdfdims[3]; /* order: ZYX or YX */

    const char *err1 = "Unable to get axis scale from file: %s.\n";

    *is_scale = TRUE;

    /*
     * hdfdims is ordered: ZYX or YX
     * in->dims is ordered: XYZ
     */
    if (in->rank == 2) {
        hdfdims[0] = in->dims[1];
        hdfdims[1] = in->dims[0];
    }
    else {
        hdfdims[0] = in->dims[2];
        hdfdims[1] = in->dims[1];
        hdfdims[2] = in->dims[0];
    }

    /*
     * extract the scale values from the input file
     */
    if (in->is_hdf == TRUE) {
        int32 sds_id, dim_id, sd_index = 0;
        int32 sd_id = infile_info.handle; /* shortcut for handle from SDstart */

        /* select the SDS */
        sds_id = SDselect(sd_id, sd_index);

        /* if the SDS is two-dimensional... */
        if (in->rank == 2) {
            /* select the dimension */
            dim_id = SDgetdimid(sds_id, 0);
            if (SDgetdimscale(dim_id, in->vscale) == FAIL)
                goto err;

            dim_id = SDgetdimid(sds_id, 1);
            if (SDgetdimscale(dim_id, in->hscale) == FAIL)
                goto err;
        }
        else /* ...three-dimensional... */
        {
            dim_id = SDgetdimid(sds_id, 0);
            if (SDgetdimscale(dim_id, in->dscale) == FAIL)
                goto err;

            dim_id = SDgetdimid(sds_id, 1);
            if (SDgetdimscale(dim_id, in->vscale) == FAIL)
                goto err;

            dim_id = SDgetdimid(sds_id, 2);
            if (SDgetdimscale(dim_id, in->hscale) == FAIL)
                goto err;
        }

        /* terminate access to the array. */
        if (SDendaccess(sds_id) == FAIL)
            goto err;
    }
    else /* input file is not an HDF file */
    {
        char infile[NAME_LEN];
        strcpy(infile, infile_info.filename);
        switch (in->outtype) {
            case 0: /* 32-bit float */
                if (in->rank == 2) {
                    for (i = 0; i < hdfdims[0]; i++) {
                        if (gfloat(infile, strm, &in->vscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->vscale[i] = in->vscale[i - 1];
                    for (i = 0; i < hdfdims[1]; i++) {
                        if (gfloat(infile, strm, &in->hscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }

                    in->hscale[i] = in->hscale[i - 1];
                }
                else {
                    for (i = 0; i < hdfdims[0]; i++) {
                        if (gfloat(infile, strm, &in->dscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->dscale[i] = in->dscale[i - 1];
                    for (i = 0; i < hdfdims[1]; i++) {
                        if (gfloat(infile, strm, &in->vscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->vscale[i] = in->vscale[i - 1];
                    for (i = 0; i < hdfdims[2]; i++) {
                        if (gfloat(infile, strm, &in->hscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->hscale[i] = in->hscale[i - 1];
                }
                break;

            case 1: /* 64-bit float */
                if (in->rank == 2) {
                    for (i = 0; i < hdfdims[0]; i++) {
                        if (gfloat64(infile, strm, &in->fp64s.vscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->fp64s.vscale[i] = in->fp64s.vscale[i - 1];
                    for (i = 0; i < hdfdims[1]; i++) {
                        if (gfloat64(infile, strm, &in->fp64s.hscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->fp64s.hscale[i] = in->fp64s.hscale[i - 1];
                }
                else {
                    for (i = 0; i < hdfdims[0]; i++) {
                        if (gfloat64(infile, strm, &in->fp64s.dscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->fp64s.dscale[i] = in->fp64s.dscale[i - 1];

                    for (i = 0; i < hdfdims[1]; i++) {
                        if (gfloat64(infile, strm, &in->fp64s.vscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->fp64s.vscale[i] = in->fp64s.vscale[i - 1];

                    for (i = 0; i < hdfdims[2]; i++) {
                        if (gfloat64(infile, strm, &in->fp64s.hscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->fp64s.hscale[i] = in->fp64s.hscale[i - 1];
                }
                break;

            case 2: /* 32-bit integer */
                if (in->rank == 2) {
                    for (i = 0; i < hdfdims[0]; i++) {
                        if (gint32(infile, strm, &in->in32s.vscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in32s.vscale[i] = in->in32s.vscale[i - 1];
                    for (i = 0; i < hdfdims[1]; i++) {
                        if (gint32(infile, strm, &in->in32s.hscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in32s.hscale[i] = in->in32s.hscale[i - 1];
                }

                else {
                    for (i = 0; i < hdfdims[0]; i++) {
                        if (gint32(infile, strm, &in->in32s.dscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in32s.dscale[i] = in->in32s.dscale[i - 1];
                    for (i = 0; i < hdfdims[1]; i++) {
                        if (gint32(infile, strm, &in->in32s.vscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in32s.vscale[i] = in->in32s.vscale[i - 1];
                    for (i = 0; i < hdfdims[2]; i++) {
                        if (gint32(infile, strm, &in->in32s.hscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in32s.hscale[i] = in->in32s.hscale[i - 1];
                }
                break;

            case 3: /* 16-bit integer */
                if (in->rank == 2) {
                    for (i = 0; i < hdfdims[0]; i++) {
                        if (gint16(infile, strm, &in->in16s.vscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in16s.vscale[i] = in->in16s.vscale[i - 1];
                    for (i = 0; i < hdfdims[1]; i++) {
                        if (gint16(infile, strm, &in->in16s.hscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in16s.hscale[i] = in->in16s.hscale[i - 1];
                }

                else {
                    for (i = 0; i < hdfdims[0]; i++) {
                        if (gint16(infile, strm, &in->in16s.dscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in16s.dscale[i] = in->in16s.dscale[i - 1];
                    for (i = 0; i < hdfdims[1]; i++) {
                        if (gint16(infile, strm, &in->in16s.vscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in16s.vscale[i] = in->in16s.vscale[i - 1];
                    for (i = 0; i < hdfdims[2]; i++) {
                        if (gint16(infile, strm, &in->in16s.hscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in16s.hscale[i] = in->in16s.hscale[i - 1];
                }
                break;

            case 4: /* 8-bit integer */
                if (in->rank == 2) {
                    for (i = 0; i < hdfdims[0]; i++) {
                        if (gint8(infile, strm, &in->in8s.vscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in8s.vscale[i] = in->in8s.vscale[i - 1];
                    for (i = 0; i < hdfdims[1]; i++) {
                        if (gint8(infile, strm, &in->in8s.hscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in8s.hscale[i] = in->in8s.hscale[i - 1];
                }

                else {
                    for (i = 0; i < hdfdims[0]; i++) {
                        if (gint8(infile, strm, &in->in8s.dscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in8s.dscale[i] = in->in8s.dscale[i - 1];
                    for (i = 0; i < hdfdims[1]; i++) {
                        if (gint8(infile, strm, &in->in8s.vscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in8s.vscale[i] = in->in8s.vscale[i - 1];
                    for (i = 0; i < hdfdims[2]; i++) {
                        if (gint8(infile, strm, &in->in8s.hscale[i], in)) {
                            fprintf(stderr, err1, infile);
                            goto err;
                        }
                    }
                    in->in8s.hscale[i] = in->in8s.hscale[i - 1];
                }
                break;
        }
    }

#ifdef DEBUG
    if (in->rank == 2) {
        printf("\tscales of the axes (vert,horiz):\n\n\t");
        for (i = 0; i < hdfdims[0]; i++)
            printf("%E ", in->vscale[i]);
        printf("\n\t");
        for (i = 0; i < hdfdims[1]; i++)
            printf("%E ", in->hscale[i]);
    }
    else {
        printf("\tscales of the axes (depth,vert,horiz):\n\n\t");
        for (i = 0; i < hdfdims[0]; i++)
            printf("%E ", in->dscale[i]);
        printf("\n\t");
        for (i = 0; i < hdfdims[1]; i++)
            printf("%E ", in->vscale[i]);
        printf("\n\t");
        for (i = 0; i < hdfdims[2]; i++)
            printf("%E ", in->hscale[i]);
    }
    printf("\n\n\n");
#endif /* DEBUG */

    return (0);

err:
    return (1);
}

/*
 * Name:
 *      gtoken
 *
 * Purpose:
 *      Return the token identifier associated with the command line
 *      argument.
 */
static int
gtoken(char *s)
{
    size_t len;
    int    token;

    const char *err1 = "Illegal argument: %s.\n";

    /*
     * identify the token type
     */
    if (s[0] == '-') { /* option name (or negative number) */
        token = ERR;
        len   = strlen(&s[1]);
        switch (s[1]) {
            case 'o':
                if (!strncmp("outfile", &s[1], len))
                    token = OPT_o;
                break;
            case 'r':
                if (!strncmp("raster", &s[1], len))
                    token = OPT_r;
                break;
            case 'e':
                if (!strncmp("expand", &s[1], len))
                    token = OPT_e;
                break;
            case 'i':
                if (!strncmp("interp", &s[1], len))
                    token = OPT_i;
                break;
            case 'p':
                if (!strncmp("palfile", &s[1], len))
                    token = OPT_p;
                break;
            case 'f':
                if (!strncmp("float", &s[1], len))
                    token = OPT_f;
                break;
            case 'h':
                if (!strncmp("help", &s[1], len))
                    token = OPT_h;
                break;
            case 'm':
                if (!strncmp("mean", &s[1], len))
                    token = OPT_m;
                break;
            case 'n':
                token = OPT_n;
                break;
            case 't':
                token = OPT_t;
                break;
            default:
                if (isnum(s)) /* negative number? */
                    token = OPT_num;
        }
        if (token == ERR)
            fprintf(stderr, err1, s);
    }
    else if (isnum(s)) /* positive number */
        token = OPT_num;
    else /* filename */
        token = FILENAME;

    return (token);
}

/*
 * Name:
 *      gtype
 *
 * Purpose:
 *      Determine the type of the input file (HDF, TEXT, FP32, FP64)
 *
 * Revision: (pkamat)
 *        Modified to support INT32, INT16, INT8 formats.
 *      Also determines and validates the outtype type of the data-set
 */
static int
gtype(char *infile, struct Input *in, FILE **strm)
{
    char buf[8];

    const char *err1 = "Unable to open file: %s.\n";
    const char *err2 = "Unable to get format tag from file: %s.\n";
    const char *err3 = "Invalid file format in file: %s.\n";
    const char *err4 =
        "Invalid use of -t or -n options. Can be used only for TEXT files or for FP64 binary files\n";

    /*
     * determine the input file format
     */
    if (Hishdf(infile))
        in->is_hdf = TRUE;
    else {
        if ((*strm = fopen(infile, "r")) == NULL) {
            fprintf(stderr, err1, infile);
            goto err;
        }
        if (fread(buf, 4, 1, *strm) != 1) {
            fprintf(stderr, err2, infile);
            goto err;
        }
        if (!memcmp("TEXT", buf, 4) || !memcmp("text", buf, 4)) {
            in->is_text = TRUE;
            if (in->outtype == NO_NE)
                in->outtype = FP_32;
        }
        else {
            if (!memcmp("FP64", buf, 4) || !memcmp("fp64", buf, 4)) {
                in->is_fp64 = TRUE;
                if (in->outtype != FP_64) {
                    if (in->outtype != NO_NE) {
                        fprintf(stderr, err4, infile);
                        goto err;
                    }
                    else {
                        in->outtype = FP_32;
                    }
                }
            }
            else {
                if (in->outtype != NO_NE) {
                    fprintf(stderr, err4, infile);
                    goto err;
                }
                if (!memcmp("FP32", buf, 4) || !memcmp("fp32", buf, 4)) {
                    in->is_fp32 = TRUE;
                    in->outtype = FP_32;
                }

                else if (!memcmp("IN32", buf, 4) || !memcmp("in32", buf, 4))
                    in->outtype = INT_32;
                else if (!memcmp("IN16", buf, 4) || !memcmp("in16", buf, 4))
                    in->outtype = INT_16;
                else if (!memcmp("IN08", buf, 4) || !memcmp("in08", buf, 4))
                    in->outtype = INT_8;
                else {
                    fprintf(stderr, err3, infile);
                    goto err;
                }
                if (in->outtype == NO_NE) {
                    fprintf(stderr, err4, infile);
                    goto err;
                }
            }
        }
    }

    return (0);

err:
    return (1);
}

/*
 * Name:
 *      help
 *
 * Purpose:
 *      Print a helpful summary of command usage and features.
 */
void
help(char *name)
{
    printf("Name:\n");
    printf("\t%s (previously fp2hdf)\n\n", name);
    printf("Purpose:\n");
    printf("\tTo convert floating point data to HDF Scientific ");
    printf("Data Set (SDS)\n");
    printf("\tand/or 8-bit Raster Image Set (RIS8) format, ");
    printf("storing the results\n");
    printf("\tin an HDF file.  The image data can be scaled ");
    printf("about a mean value.\n\n");

    fprintf(stderr, "Synopsis:");
    fprintf(stderr, "\n\t%s  -h[elp]", name);
    fprintf(stderr, "\n\t\t Print this summary of usage and exit.");
    fprintf(stderr, "\n\t\t ");
    fprintf(stderr, "\n\t%s  -V", name);
    fprintf(stderr, "\n\t\t Print version of the HDF4 library and exit.");
    fprintf(stderr, "\n\t\t ");
    fprintf(stderr,
            "\n\t%s  <infile> [ [-t[ype] <output-type> | -n] [<infile> [-t[ype] <output-type> | -n]...]",
            name);
    fprintf(stderr, "\n\t\t\t\t\t-o[utfile] <outfile> [-r[aster] [ras_opts ...]] [-f[loat]]");

    fprintf(stderr, "\n\n\t <infile(s)>:");
    fprintf(stderr, "\n\t\t Name of the input file(s), containing a single ");
    fprintf(stderr, "\n\t\t two-dimensional or three-dimensional floating point array ");
    fprintf(stderr, "\n\t\t in either ASCII text, native floating point, native integer ");
    fprintf(stderr, "\n\t\t or HDF SDS format.  If an HDF file is used for input, it ");
    fprintf(stderr, "\n\t\t must contain an SDS. The SDS need only contain a dimension ");
    fprintf(stderr, "\n\t\t record and the data, but if it also contains maximum and ");
    fprintf(stderr, "\n\t\t minimum values and/or scales for each axis, these will ");
    fprintf(stderr, "\n\t\t be used.  If the input format is ASCII text or native ");
    fprintf(stderr, "\n\t\t floating point or native integer, see \"Notes\" below on ");
    fprintf(stderr, "\n\t\t how it must be organized.");

    fprintf(stderr, "\n\n\t -t[ype] <output_type>: ");
    fprintf(stderr, "\n\t\t Optionally used for every input ASCII file to specify the ");
    fprintf(stderr, "\n\t\t data type of the data-set to be written. If not specified               ");
    fprintf(stderr, "\n\t\t default data type is 32-bit floating point. <output-type>");
    fprintf(stderr, "\n\t\t can be any of the following: FP32 (default), FP64, INT32");
    fprintf(stderr, "\n\t\t INT16, INT8. It can be used only with ASCII files.");

    fprintf(stderr, "\n\n\t -n:  ");
    fprintf(stderr, "\n\t\t This option is to be used only if the binary input file ");
    fprintf(stderr, "\n\t\t contains 64-bit floating point data and the default");
    fprintf(stderr, "\n\t\t behaviour (default behaviour is to write it to a 32-bit");
    fprintf(stderr, "\n\t\t floating point data-set) should be overridden to write ");
    fprintf(stderr, "\n\t\t it to a 64-bit floating point data-set.");

    fprintf(stderr, "\n\n\t -o[utfile] <outfile>:");
    fprintf(stderr, "\n\t\t Data from one or more input files are stored as one or");
    fprintf(stderr, "\n\t\t more data sets and/or images in one HDF output file,");
    fprintf(stderr, "\n\t\t \"outfile\".");

    fprintf(stderr, "\n\n\t -r[aster]:");
    fprintf(stderr, "\n\t\t Store output as a raster image set in the output file.");

    fprintf(stderr, "\n\n\t -f[loat]:");
    fprintf(stderr, "\n\t Store output as a scientific data set in the output file.");
    fprintf(stderr, "\n\t This is the default if the \"-r\" option is not specified.");

    fprintf(stderr, "\n\n\t ras_opts ...");
    fprintf(stderr, "\n\n\t -e[xpand] <horiz> <vert> [<depth>]:");
    fprintf(stderr, "\n\t Expand float data via pixel replication to produce the");
    fprintf(stderr, "\n\t image(s).  \"horiz\" and \"vert\" give the horizontal and");
    fprintf(stderr, "\n\t vertical resolution of the image(s) to be produced; and");
    fprintf(stderr, "\n\t optionally, \"depth\" gives the number of images or depth");
    fprintf(stderr, "\n\t planes (for 3D input data).");

    fprintf(stderr, "\n\n\t -i[nterp] <horiz> <vert> [<depth>]:");
    fprintf(stderr, "\n\t\t Apply bilinear, or trilinear, interpolation to the float");
    fprintf(stderr, "\n\t\t data to produce the image(s).  \"horiz\", \"vert\", and \"depth\"");
    fprintf(stderr, "\n\t\t must be greater than or equal to the dimensions of the");
    fprintf(stderr, "\n\t\t original dataset.");
    fprintf(stderr, "\n\t\t If max and min are supplied in input file, this option clips");
    fprintf(stderr, "\n\t\t values that are greater than max or less then min, setting");
    fprintf(stderr, "\n\t\t them to the max and min, respectively.");

    fprintf(stderr, "\n\n\t -p[alfile] <palfile>:");
    fprintf(stderr, "\n\t\t Store the palette with the image.  Get the palette from");
    fprintf(stderr, "\n\t\t \"palfile\"; which may be an HDF file containing a palette,");
    fprintf(stderr, "\n\t\t or a file containing a raw palette.");

    fprintf(stderr, "\n\n\t -m[ean] <mean>:");
    fprintf(stderr, "\n\t\t If a floating point mean value is given, the image will be");
    fprintf(stderr, "\n\t\t scaled about the mean.  The new extremes (newmax and newmin),");
    fprintf(stderr, "\n\t\t as given by:");

    fprintf(stderr, "\n\n\t\t\t newmax = mean + max(abs(max-mean), abs(mean-min))");
    fprintf(stderr, "\n\t\t\t newmin = mean - max(abs(max-mean), abs(mean-min))");

    fprintf(stderr, "\n\n\t\t will be equidistant from the mean value.  If no mean value");
    fprintf(stderr, "\n\t\t is given, then the mean will be:  0.5   (max + min)");

    fprintf(stderr, "\n\n\t Notes:");
    fprintf(
        stderr,
        "\n\t\t If the input file format is ASCII text or native floating point or native integer(32-bit,");
    fprintf(stderr, "\n\t\t 16-bit, 8-bit), it");
    fprintf(stderr, "\n\t\t must have the following input fields:");

    fprintf(stderr, "\n\t\t format");
    fprintf(stderr, "\n\t\t nplanes");
    fprintf(stderr, "\n\t\t nrows");
    fprintf(stderr, "\n\t\t cols");
    fprintf(stderr, "\n\t\t max_value");
    fprintf(stderr, "\n\t\t min_value");
    fprintf(stderr, "\n\t\t [plane1 plane2 plane3 ...]");
    fprintf(stderr, "\n\t\t row1 row2 row3 ...");
    fprintf(stderr, "\n\t\t col1 col2 col3 ...");
    fprintf(stderr, "\n\t\t data1 data2 data3 ...");

    fprintf(stderr, "\n\n\t\t Where:");
    fprintf(stderr, "\n\n\t\t format:");
    fprintf(stderr,
            "\n\t\t\t Format designator (\"TEXT\", \"FP32\", \"FP64\", \"IN32\", \"IN16\", \"IN08\").");
    fprintf(stderr, "\n\t\t\t nplanes, nrows, ncols:");
    fprintf(stderr, "\n\t\t\t Dimensions are specified in the order slowest changing dimension first.");
    fprintf(stderr, "\n\t\t\t ncols is dimension of the fastest changing dimension. (horizontal axis");
    fprintf(stderr, "\n\t\t\t or X-axis in a 3D scale)");
    fprintf(stderr, "\n\t\t\t nrows corresponds to dimension of the vertical axis or Y-axis in a 3D ");
    fprintf(stderr, "\n\t\t\t scale.");
    fprintf(stderr, "\n\t\t\t nplanes corresponds to the slowest changing dimension i.e. dimension of ");
    fprintf(stderr, "\n\t\t\t the depth axis or the Z-axis in a 3D scale (\"1\" for 2D input).");
    fprintf(stderr, "\n\t\t max_value:");
    fprintf(stderr, "\n\t\t\t Maximum data value.");
    fprintf(stderr, "\n\t\t min_value:");
    fprintf(stderr, "\n\t\t\t Minimum data value.");
    fprintf(stderr, "\n\t\t plane1, plane2, plane3, ...:");
    fprintf(stderr, "\n\t\t\t Scales for depth axis.");
    fprintf(stderr, "\n\t\t row1, row2, row3, ...:");
    fprintf(stderr, "\n\t\t\t Scales for the vertical axis.");
    fprintf(stderr, "\n\t\t col1, col2, col3, ...:");
    fprintf(stderr, "\n\t\t\t Scales for the horizontal axis.");
    fprintf(stderr, "\n\t\t data1, data2, data3, ...:");
    fprintf(stderr, "\n\t\t\t The data ordered by rows, left to right and top");
    fprintf(stderr, "\n\t\t\t to bottom; then optionally, ordered by planes,");
    fprintf(stderr, "\n\t\t\t front to back.");

    fprintf(stderr,
            "\n\n\t\t For FP32 and FP64 input format, \"format\", \"nplanes\", \"nrows\", \"ncols\",");
    fprintf(stderr, "\n\t\t and \"nplanes\" are native integers; where \"format\" is the integer");
    fprintf(stderr, "\n\t\t representation of the appropriate 4-character string (0x46503332 for");
    fprintf(stderr, "\n\t\t \"FP32\" and 0x46503634 for \"FP64\").  The remaining input fields are");
    fprintf(stderr, "\n\t\t composed of native 32-bit floating point values for FP32 input format,");
    fprintf(stderr, "\n\t\t or native 64-bit floating point values for FP64 input format.");

    fprintf(stderr,
            "\n\n\t For IN32, IN16 and IN08 input format, \"format\", \"nplanes\", \"nrows\", \"ncols\",");
    fprintf(stderr, "\n\t\t and \"nplanes\" are native integers; where \"format\" is the integer");
    fprintf(stderr, "\n\t\t representation of the appropriate 4-character string. The remaining input ");
    fprintf(stderr, "\n\t\t fields are composed of native 32-bit integer values for IN32 input format,");
    fprintf(stderr, "\n\t\t or native 16-bit integer values for IN16 input format or native 8-bit ");
    fprintf(stderr, "\n\t\t integer values for IN08 input format.");

    printf("\nExamples:\n");
    printf("\tConvert floating point data in \"f1.txt\" to SDS ");
    printf("format, and store it\n");
    printf("\tas an SDS in HDF file \"o1\":\n\n");
    printf("\t\t%s f1.txt -o o1\n\n", name);
    printf("\tConvert floating point data in \"f2.hdf\" to ");
    printf("8-bit raster format, and\n");
    printf("\tstore it as an RIS8 in HDF file \"o2\":\n\n");
    printf("\t\t%s f2.hdf -o o2 -r\n\n", name);
    printf("\tConvert floating point data in \"f3.bin\" to ");
    printf("8-bit raster format and\n");
    printf("\tSDS format, and store both the RIS8 and the SDS ");
    printf("in HDF file \"o3\":\n\n");
    printf("\t\t%s f3.bin -o o3 -r -f\n\n", name);
    printf("\tConvert floating point data in \"f4\" to a ");
    printf("500x600 raster image, and\n");
    printf("\tstore the RIS8 in HDF file \"o4\".  Also store a ");
    printf("palette from \"palfile\"\n");
    printf("\twith the image:\n\n");
    printf("\t\t%s f4 -o o4 -r -e 500 600 -p palfile\n\n", name);
    printf("\tConvert floating point data in \"f5\" to 200 ");
    printf("planes of 500x600 raster\n");
    printf("\timages, and store the RIS8 in HDF file \"o5\".  ");
    printf("Also scale the image\n");
    printf("\tdata so that it is centered about a mean value ");
    printf("of 10.0:\n\n");
    printf("\t\t%s f5 -o o5 -r -i 500 600 200 -m 10.0\n", name);

    return;
}

/*
 * Name:
 *      indexes
 *
 * Purpose:
 *      For each pixel location along an axis, determine the nearest
 *      scale value neighbor.  Return a list of indexes into the scale
 *      array.
 */
static int
indexes(float32 *scale, int dim, int *idx, int res)
{
    int      i, j;
    float32 *midpt;
    float32  loc;
    float32  delta;

    const char *err1 = "Unable to allocate dynamic memory.\n";
    /*
     * determine the midpoints between scale values
     */
    if ((midpt = (float32 *)malloc((size_t)dim * sizeof(float32))) == NULL) {
        fprintf(stderr, "%s", err1);
        goto err;
    }
    for (i = 0; i < dim - 1; i++)
        midpt[i] = (scale[i] + scale[i + 1]) * (float32)0.5;
    midpt[dim - 1] = scale[dim - 1] + (scale[dim - 1] - midpt[dim - 2]);

    /*
     * determine the distance between pixel locations
     */
    delta = (scale[dim - 1] - scale[0]) / (float32)(res - 1);

    /*
     * compute indexes, keeping the index the same until the location
     * extends beyond the midpoint
     */
    for (i = 1, j = 0, idx[0] = 0, loc = scale[0]; i < res; i++) {
        loc += delta;
        idx[i] = idx[i - 1];
        while (loc >= midpt[j]) {
            idx[i] += 1;
            j += 1;
        }
    }

    /*
     * free dynamically allocated memory
     */
    free(midpt);

    return (0);

err:
    return (1);
}

/*
 * Name:
 *      interp
 *
 * Purpose:
 *      Use a bilinear, or trilinear, interpolation scheme to construct
 *      the raster image(s).
 *
 *  Bug revision:  the line that previously read:
 *
 *      hratio[i] = ((hrange > 0) ? 1.0 : -1.0) * (in->hscale[j+1] -
 *                    loc) / (in->hscale[j+1] - in->hscale[j]);
 *    has been changed to read:
 *      hratio[i] = (in->hscale[j+1] - loc) / (in->hscale[j+1] - in->hscale[j]);
 *
 *    Similar changes were made to the corresponding lines for
 *    computing vratio and dratio.
 *
 *  Bug revision: If values occur that are outside the ranges of the
 *    max and min values provided, these values are now "clipped" to
 *    be the same as the max and min, respectively.
 */

static int
interp(struct Input *in, struct Raster *im)
{
    int            i, j, k, m;
    int           *hinc, *voff, *doff = NULL;
    float32        pix;
    float32        loc;
    float32        range;
    float32        ratio;
    float32        hrange, vrange, drange = (float32)0.0;
    float32        hdelta, vdelta, ddelta = (float32)0.0;
    float32        t1, t2, t3, t4, t5, t6;
    float32       *hratio, *vratio, *dratio = NULL;
    float32       *pt[8];
    unsigned char *ip = im->image;

    const char *err1 = "Unable to allocate dynamic memory.\n";

    /*
     * determine the range of pixel locations
     */
    range  = in->max - in->min;
    ratio  = (float32)237.9 / range;
    hrange = in->hscale[in->dims[0] - 1] - in->hscale[0];
    vrange = in->vscale[in->dims[1] - 1] - in->vscale[0];
    if (in->rank == 3)
        drange = in->dscale[in->dims[2] - 1] - in->dscale[0];

    /*
     * determine the distance between pixel locations
     */
    hdelta = hrange / (float32)(im->hres - 1);
    vdelta = vrange / (float32)(im->vres - 1);
    if (in->rank == 3)
        ddelta = drange / (float32)(im->dres - 1);

    /*
     * allocate dynamic memory for the interpolation ratio buffers
     */
    if ((hratio = (float32 *)malloc((size_t)im->hres * sizeof(float32))) == NULL) {
        fprintf(stderr, "%s", err1);
        goto err;
    }
    if ((vratio = (float32 *)malloc((unsigned int)im->vres * sizeof(float32))) == NULL) {
        fprintf(stderr, "%s", err1);
        goto err;
    }
    if (in->rank == 3) {
        if ((dratio = (float32 *)malloc((unsigned int)im->dres * sizeof(float32))) == NULL) {
            fprintf(stderr, "%s", err1);
            goto err;
        }
    }

    /*
     * allocate dynamic memory for the pixel location offset/increment
     * buffers
     */
    if ((hinc = (int *)malloc((unsigned int)im->hres * sizeof(int))) == NULL) {
        fprintf(stderr, "%s", err1);
        goto err;
    }
    if ((voff = (int *)malloc((unsigned int)(im->vres + 1) * sizeof(int))) == NULL) {
        fprintf(stderr, "%s", err1);
        goto err;
    }
    if (in->rank == 3) {
        if ((doff = (int *)malloc((unsigned int)(im->dres + 1) * sizeof(int))) == NULL) {
            fprintf(stderr, "%s", err1);
            goto err;
        }
    }

    /*
     * compute the interpolation ratios and pixel location
     * offsets/increments for each axis
     */
    for (i = 0, j = 0; i < im->hres; i++) {
        loc     = hdelta * (float)i + in->hscale[0];
        hinc[i] = 0;
        while ((j < (in->dims[0] - 2)) &&
               ((hrange > (float32)0.0) ? (in->hscale[j + 1] < loc) : (in->hscale[j + 1] > loc))) {
            hinc[i] += 1;
            j += 1;
        }
        hratio[i] = (in->hscale[j + 1] - loc) / (in->hscale[j + 1] - in->hscale[j]);
    }
    for (i = 0, j = 0, voff[0] = 0; i < im->vres; i++) {
        loc = vdelta * (float)i + in->vscale[0];
        while ((j < (in->dims[1] - 2)) &&
               ((vrange > (float32)0.0) ? (in->vscale[j + 1] < loc) : (in->vscale[j + 1] > loc))) {
            voff[i] += 1;
            j += 1;
        }
        vratio[i]   = (in->vscale[j + 1] - loc) / (in->vscale[j + 1] - in->vscale[j]);
        voff[i + 1] = voff[i];
    }
    if (in->rank == 3) {
        for (i = 0, j = 0, doff[0] = 0; i < im->dres; i++) {
            loc = ddelta * (float)i + in->dscale[0];
            while ((j < (in->dims[2] - 2)) &&
                   ((drange > (float32)0.0) ? (in->dscale[j + 1] < loc) : (in->dscale[j + 1] > loc))) {
                doff[i] += 1;
                j += 1;
            }
            dratio[i]   = (in->dscale[j + 1] - loc) / (in->dscale[j + 1] - in->dscale[j]);
            doff[i + 1] = doff[i];
        }
    }

    /*
     * do the interpolation for each point in the target image, taking
     * advantage of the fact that the target is evenly spaced along each
     * axis
     */
    if (in->rank == 2) {
        for (i = 0; i < im->vres; i++) {
            pt[0] = (float32 *)in->data + (in->dims[0] * voff[i]);
            pt[1] = pt[0] + 1;
            pt[2] = pt[0] + in->dims[0];
            pt[3] = pt[2] + 1;
            for (j = 0; j < im->hres; j++) {
                for (m = 0; m < 4; m++)
                    pt[m] += hinc[j];
                t1  = *pt[2] - ((*pt[2] - *pt[0]) * vratio[i]);
                t2  = *pt[3] - ((*pt[3] - *pt[1]) * vratio[i]);
                pix = t2 - ((t2 - t1) * hratio[j]);
                if (pix > in->max)
                    pix = in->max; /* clip (bug fix) */
                if (pix < in->min)
                    pix = in->min; /* ditto */
                *ip++ = (unsigned char)((ratio * (pix - in->min)) + (float32)1.5);
            }
        }
    }
    else { /* rank == 3 */
        for (i = 0; i < im->dres; i++) {
            for (j = 0; j < im->vres; j++) {
                pt[0] = (float32 *)in->data + (in->dims[0] * voff[j]) + (in->dims[0] * in->dims[1] * doff[i]);
                pt[1] = pt[0] + 1;
                pt[2] = pt[0] + in->dims[0];
                pt[3] = pt[2] + 1;
                pt[4] = pt[0] + (in->dims[0] * in->dims[1]);
                pt[5] = pt[4] + 1;
                pt[6] = pt[4] + in->dims[0];
                pt[7] = pt[6] + 1;
                for (k = 0; k < im->hres; k++) {
                    for (m = 0; m < 8; m++)
                        pt[m] += hinc[k];
                    t1  = *pt[4] - ((*pt[4] - *pt[0]) * dratio[i]);
                    t2  = *pt[6] - ((*pt[6] - *pt[2]) * dratio[i]);
                    t3  = *pt[5] - ((*pt[5] - *pt[1]) * dratio[i]);
                    t4  = *pt[7] - ((*pt[7] - *pt[3]) * dratio[i]);
                    t5  = t2 - ((t2 - t1) * vratio[j]);
                    t6  = t4 - ((t4 - t3) * vratio[j]);
                    pix = t6 - ((t6 - t5) * hratio[k]);
                    if (pix > in->max)
                        pix = in->max; /* clip (bug fix) */
                    if (pix < in->min)
                        pix = in->min; /* ditto */
                    *ip++ = (unsigned char)((ratio * (pix - in->min)) + (float32)1.5);
                }
            }
        }
    }

    /*
     * free dynamically allocated memory
     */
    free(hratio);
    free(vratio);
    if (in->rank == 3)
        free(dratio);
    free(hinc);
    free(voff);
    if (in->rank == 3)
        free(doff);

    return (0);

err:
    return (1);
}

/*
 * Name:
 *      isnum
 *
 * Purpose:
 *      Determine whether or not the string is representative of an
 *      integer or floating point number.  If it is, a non-zero value
 *      is returned.  A leading (-) to denote sign is acceptable.
 */
static int
isnum(char *s)
{
    char *cp;
    int   rval = FALSE;

    /*
     * check to see if its a floating point number
     */
    cp = s;
    (void)strtod(s, &cp);
    if ((*cp == '\0') && (cp != s))
        rval = TRUE;

    /*
     * check to see if its an integer number (radix 8, 10, or 16)
     */
    else {
        cp = s;
        (void)strtol(s, &cp, 0);
        if ((*cp == '\0') && (cp != s))
            rval = TRUE;
    }

    return (rval);
}

/*
 * Name:
 *      mean
 *
 * Purpose:
 *      Reset the maximum and minimum data values to be symmetric about
 *      the user-specified mean value.
 */
void
mean(struct Input *in, struct Options *opt)
{
    float32 delta, delta_max, delta_min;

    delta_max = (float32)fabs((double)(in->max - opt->meanval));
    delta_min = (float32)fabs((double)(opt->meanval - in->min));
    delta     = (delta_max > delta_min) ? delta_max : delta_min;

    in->max = opt->meanval + delta;
    in->min = opt->meanval - delta;

    return;
}

/*
 * Name:
 *      palette
 *
 * Purpose:
 *      Process the (user specified) palette input file.
 */
static int
palette(char *palfile)
{
    unsigned char *color;
    unsigned char  pal[1024], red[256], green[256], blue[256];
    FILE          *strm;
    int            i;

    const char *err1 = "Unable to get palette from file: %s.\n";
    const char *err2 = "Unable to open palette file: %s.\n";
    const char *err3 = "Unable to set default palette.\n";

    /*
     * extract a palette from an HDF file
     */
    if (Hishdf(palfile)) {
        if (DFPgetpal(palfile, pal)) {
            fprintf(stderr, err1, palfile);
            goto err;
        }

        /*
         * read in a raw palette file
         */
    }
    else {
        if ((strm = fopen(palfile, "r")) == NULL) {
            fprintf(stderr, err2, palfile);
            goto err;
        }
        if (fread((char *)red, 1, 256, strm) != 256) {
            fprintf(stderr, err1, palfile);
            goto err;
        }
        else if (fread((char *)green, 1, 256, strm) != 256) {
            fprintf(stderr, err1, palfile);
            goto err;
        }
        else if (fread((char *)blue, 1, 256, strm) != 256) {
            fprintf(stderr, err1, palfile);
            goto err;
        }
        (void)fclose(strm);

        /*
         * interleave the R,G,B values
         */
        color = pal;
        for (i = 0; i < 256; i++) {
            *color++ = red[i];
            *color++ = green[i];
            *color++ = blue[i];
        }
    }

    /*
     * set up the palette as the default for subsequent images
     */
    if (DFR8setpalette(pal)) {
        fprintf(stderr, "%s", err3);
        goto err;
    }

    return (0);

err:
    return (1);
}

/*
 * Name:
 *      pixrep
 *
 * Purpose:
 *      Expand the image(s) to the desired resolution using pixel
 *      replication.
 */
static int
pixrep(struct Input *in, struct Raster *im)
{
    int           *hidx, *vidx, *didx;
    int            ovidx, odidx;
    int            dummy;
    int32          i, j, k;
    float32       *dp;
    float32        range;
    float32        ratio;
    unsigned char *ip, *plane, *row, *pix;

    const char *err1 = "Unable to dynamically allocate memory.\n";
    dp               = (float32 *)in->data;
    ip               = im->image;
    range            = in->max - in->min;
    ratio            = (float32)237.9 / range;

    /*
     * determine the scale indexes of the horizontal pixel locations
     */
    if ((hidx = (int *)malloc((unsigned int)(im->hres + 1) * sizeof(int))) == NULL) {
        fprintf(stderr, "%s", err1);
        goto err;
    }

    if (indexes(in->hscale, in->dims[0], hidx, im->hres))
        goto err;

    /*
     * determine the scale indexes of the vertical pixel locations
     */
    if ((vidx = (int *)malloc((unsigned int)(im->vres + 1) * sizeof(int))) == NULL) {
        fprintf(stderr, "%s", err1);
        goto err;
    }

    if (indexes(in->vscale, in->dims[1], vidx, im->vres))
        goto err;

    /*
     * determine the scale indexes of the depth plane locations
     */
    dummy = 0;
    didx  = &dummy;
    if (in->rank == 3) {
        if ((didx = (int *)malloc((unsigned int)(im->dres + 1) * sizeof(int))) == NULL) {
            fprintf(stderr, "%s", err1);
            goto err;
        }

        if (indexes(in->dscale, in->dims[2], didx, im->dres))
            goto err;
    }

    /*
     * compute the expanded image
     */
    if ((pix = (unsigned char *)malloc((unsigned int)(in->dims[0] + 1))) == NULL) {
        fprintf(stderr, "%s", err1);
        goto err;
    }
    for (k = 0, odidx = didx[0] - 1; k < im->dres; k++) {
        /*
         * construct a new depth plane
         */
        if (didx[k] > odidx) {
            for (j = 0, ovidx = vidx[0] - 1; j < im->vres; j++) {
                /*
                 * construct a new row
                 */
                if (vidx[j] > ovidx) {
                    for (i = 0; i < in->dims[0]; i++)
                        pix[i] = (unsigned char)((ratio * (*dp++ - in->min)) + (float32)1.5);
                    for (i = 0; i < im->hres; i++)
                        *ip++ = pix[hidx[i]];
                    /*
                     * repeat the previous row
                     */
                }
                else {
                    row = ip - im->hres;
                    for (i = 0; i < im->hres; i++)
                        *ip++ = *row++;
                }
                ovidx = vidx[j];
            }
            /*
             * repeat the previous depth plane
             */
        }
        else {
            plane = ip - (im->hres * im->vres);
            for (j = 0; j < im->vres; j++)
                for (i = 0; i < im->hres; i++)
                    *ip++ = plane[(j * im->hres) + i];
        }
        odidx = didx[k];
    }

    /*
     * free dynamically allocated space
     */
    free(hidx);
    free(vidx);
    if (in->rank == 3)
        free(didx);
    free(pix);

    return (0);

err:
    return (1);
}

/*
 * Name:
 *      create_SDS
 *
 * Purpose:
 *      This function contains common code that creates a two- or
 *    three-dimensional dataset, used in function 'process.'  It
 *    was factored out to reduce the length of 'process.'
 *    Returns the new SDS identifier, if success, and FAIL,
 *    otherwise. (bmribler - 2006/8/18)
 */
static int32
create_SDS(int32 sd_id, int32 nt, struct Input *in)
{
    int32 sds_id = FAIL;

    if (in->rank == 2) {
        int32 edges[2];
        edges[0] = in->dims[1];
        edges[1] = in->dims[0];
        sds_id   = SDcreate(sd_id, NULL, nt, in->rank, edges);
    }
    else {
        int32 edges[3];
        edges[0] = in->dims[2];
        edges[1] = in->dims[1];
        edges[2] = in->dims[0];
        sds_id   = SDcreate(sd_id, NULL, nt, in->rank, edges);
    }
    return (sds_id);
}

/*
 * Name:
 *      alloc_data
 *
 * Purpose:
 *      This function contains common code that allocates memory for
 *    the data buffer to hold different types of data.  It
 *    was factored out to reduce the length of 'process.'
 *    Returns SUCCEED or FAIL. (bmribler - 2006/8/18)
 */
static intn
alloc_data(void **data, int32 len, int outtype)
{
    const char *alloc_err = "Unable to dynamically allocate memory.\n";

    switch (outtype) {
        case 0: /* 32-bit float */
        case 5: /* NO_NE */
            if ((*data = (void *)malloc((size_t)len * sizeof(float32))) == NULL) {
                fprintf(stderr, "%s", alloc_err);
                return FAIL;
            }
            break;
        case 1: /* 64-bit float */
            if ((*data = (void *)malloc((size_t)len * sizeof(float64))) == NULL) {
                fprintf(stderr, "%s", alloc_err);
                return FAIL;
            }
            break;
        case 2: /* 32-bit integer */
            if ((*data = (void *)malloc((size_t)len * sizeof(int32))) == NULL) {
                fprintf(stderr, "%s", alloc_err);
                return FAIL;
            }
            break;
        case 3: /* 16-bit integer */
            if ((*data = (void *)malloc((size_t)len * sizeof(int16))) == NULL) {
                fprintf(stderr, "%s", alloc_err);
                return FAIL;
            }
            break;
        case 4: /* 8-bit integer */
            if ((*data = (void *)malloc((size_t)len * sizeof(int8))) == NULL) {
                fprintf(stderr, "%s", alloc_err);
                return FAIL;
            }
            break;
    } /* end switch */
    return SUCCEED;
} /* alloc_data */

/*
 * Name:
 *      write_SDS
 *
 * Purpose:
 *      This function contains common code, that writes a two- or
 *    three-dimensional dataset, used in function 'process.'  It
 *    was factored out to reduce the length of 'process.'
 *    Returns SUCCEED or FAIL. (bmribler - 2006/8/18)
 */
static intn
write_SDS(int32 sds_id, struct Input *in)
{
    const char *write_err = "Unable to write an SDS to the HDF output file\n";
    if (in->rank == 2) {
        int32 edges[2], start[2];
        edges[0] = in->dims[1];
        edges[1] = in->dims[0];
        start[0] = 0;
        start[1] = 0;
        if (SDwritedata(sds_id, start, NULL, edges, (void *)in->data) != 0) {
            fprintf(stderr, "%s", write_err);
            return FAIL;
        }
    }
    else {
        int32 edges[3], start[3];
        edges[0] = in->dims[2];
        edges[1] = in->dims[1];
        edges[2] = in->dims[0];
        start[0] = 0;
        start[1] = 0;
        start[2] = 0;
        if (SDwritedata(sds_id, start, NULL, edges, (void *)in->data) != 0) {
            fprintf(stderr, "%s", write_err);
            return FAIL;
        }
    }
    return SUCCEED;
} /* write_SDS */

/*
 * Name:
 *      set_dimensions
 *
 * Purpose:
 *      This function contains the common code, that sets dimension scale
 *    for a two- or three-dimensional dataset, used in function 'process.'
 *    It was factored out to reduce the length of 'process.'
 *    Returns SUCCEED or FAIL. (bmribler - 2006/8/18)
 */
static intn
set_dimensions(int32 sds_id, struct Input *in, int32 nt, void *dscale, void *vscale, void *hscale)
{
    int32       dim_id, dim_index;
    const char *dim_err = "Unable to set dimension scales\n";

    if (in->rank == 2) {
        int32 edges[2];

        edges[0] = in->dims[1];
        edges[1] = in->dims[0];

        dim_index = 0;
        dim_id    = SDgetdimid(sds_id, dim_index);

        if (SDsetdimscale(dim_id, edges[0], nt, (void *)vscale) == FAIL) {
            fprintf(stderr, "%s, dim index %d\n", dim_err, dim_index);
            return FAIL;
        }

        dim_index = 1;
        dim_id    = SDgetdimid(sds_id, dim_index);

        if (SDsetdimscale(dim_id, edges[1], nt, hscale) != 0) {
            fprintf(stderr, "%s, dim index %d\n", dim_err, dim_index);
            return FAIL;
        }
    }
    else {
        int32 edges[3];
        edges[0] = in->dims[2];
        edges[1] = in->dims[1];
        edges[2] = in->dims[0];

        dim_index = 0;
        dim_id    = SDgetdimid(sds_id, dim_index);

        if (SDsetdimscale(dim_id, edges[0], nt, dscale) != 0) {
            fprintf(stderr, "%s, dim index %d\n", dim_err, dim_index);
            return FAIL;
        }
        dim_index = 1;
        dim_id    = SDgetdimid(sds_id, dim_index);

        if (SDsetdimscale(dim_id, edges[1], nt, vscale) != 0) {
            fprintf(stderr, "%s, dim index %d\n", dim_err, dim_index);
            return FAIL;
        }
        dim_index = 2;
        dim_id    = SDgetdimid(sds_id, dim_index);

        if (SDsetdimscale(dim_id, edges[2], nt, hscale) != 0) {
            fprintf(stderr, "%s, dim index %d\n", dim_err, dim_index);
            return FAIL;
        }
    }
    return SUCCEED;
} /* set_dimensions */

/*
 * Name:
 *      process
 *
 * Purpose:
 *      Process each input file.
 *
 * Revision: (pkamat)
 *    Modified to support the writing of the data set in any of the
 *    following types: INT32, INT16, INT8 and FP64
 * Modification: pvn: March, 3, 2006
 *    handled the case of in->outtype == 5 (NO_NE), for hdf input type
 *    current version assumes that datum is DFNT_FLOAT32 for this case
 * Revision: (bmribler - 2006/8/18)
 *    - Modified to store the input SD identifier in 'struct infilesformat'
 *      so the id can be passed into various functions, instead of
 *      repeatedly calling SDstart in these functions.
 *    - Factored out common codes to make this ~900-line function become
 *      more readable and maintainable.
 */
static int
process(struct Options *opt)
{
    struct Input   in;
    struct Raster  im;
    unsigned char *ip;
    int            i, j;
    int            is_maxmin;
    int            is_scale;
    int32          len;
    FILE          *strm = NULL;
    int32          hdf;
    int32          sd_id  = FAIL;
    int32          sds_id = FAIL;

#ifdef DEBUG
    int h, v, d;
#endif /* DEBUG */

    const char *err1  = "Error creating HDF output file: %s.\n";
    const char *err1a = "Error opening the created HDF output file for writing, file %s.\n";
    const char *err2  = "Unable to dynamically allocate memory.\n";
    const char *err3a = "Warning: cannot make image smaller using -e ";
    const char *err3b = "option.\n\t %s resolution will be made the ";
    const char *err3c = "same as %s dimension of the\n\t dataset, ";
    const char *err3d = "which is: %d.\n\n";
    const char *err4  = "Unable to write an RIS8 to the HDF output file\n";
    const char *err5a = "Unable to set range to an SDS\n";
    const char *err6a = "Unable to close the SDS\n";
    const char *err6  = "Unable to close the HDF output file\n";
    /*
     * process the palette file (if one was specified)
     */
    if (opt->pal == TRUE)
        if (palette(opt->palfile))
            goto err;

    /*
     * create the HDF output file
     */
    if ((hdf = Hopen(opt->outfile, DFACC_CREATE, 0)) == FAIL) {
        fprintf(stderr, err1, opt->outfile);
        goto err;
    }
    (void)Hclose(hdf);

    /* new interface */
    if ((sd_id = SDstart(opt->outfile, DFACC_WRITE)) == FAIL) {
        fprintf(stderr, err1a, opt->outfile);
        goto err;
    }

    /*
     * main loop: process input files, one per pass
     */

    for (i = 0; i < opt->fcount; i++) {
        /*
         * initialize key parameters
         */
        in.is_hdf  = FALSE;
        in.is_text = FALSE;
        in.is_fp32 = FALSE;
        in.is_fp64 = FALSE;
        is_maxmin  = FALSE;
        is_scale   = FALSE;
        in.outtype = opt->infiles[i].outtype;

        if (Hishdf(opt->infiles[i].filename)) {
            in.is_hdf              = TRUE;
            opt->infiles[i].handle = SDstart(opt->infiles[i].filename, DFACC_RDONLY);
            if (opt->infiles[i].handle == FAIL) {
                fprintf(stderr, err1a, opt->infiles[i].filename);
                goto err;
            }
        }
        /*
         * get the file type, input data dimensions, and input data
         * max/min values
         */

        if (gtype(opt->infiles[i].filename, &in, &strm))
            goto err;

        if (gdimen(opt->infiles[i], &in, strm))
            goto err;

        if (gmaxmin(opt->infiles[i], &in, strm, &is_maxmin))
            goto err;

        /*
         * Initialize the scale variables according to the output type
         * of data set
         */
        if (init_scales(&in))
            goto err;

        /*
         * get the scale for each axis
         */
        if (gscale(opt->infiles[i], &in, strm, &is_scale))
            goto err;

        /*
         * get the input data
         */
        len = in.dims[0] * in.dims[1] * in.dims[2];

        /* allocate memory for in.data depending on in.outtype value */
        if (alloc_data(&(in.data), len, in.outtype) == FAIL)
            goto err;

        if (gdata(opt->infiles[i], &in, strm, &is_maxmin))
            goto err;

        /*
         * put the input data in the HDF output file, in SDS format
         */
        if (opt->to_float == TRUE) {
            intn status;
            switch (in.outtype) {

                case 0: /* 32-bit float */
                case 5: /* NO_NE */
                    /* create data-set */
                    sds_id = create_SDS(sd_id, DFNT_FLOAT32, &in);
                    if (sds_id == FAIL)
                        goto err;

                    if (is_scale == TRUE) {
                        /* set range */
                        if (SDsetrange(sds_id, &in.max, &in.min) != 0) {
                            fprintf(stderr, "%s", err5a);
                            goto err;
                        }

                        /* set dimension scale */
                        status = set_dimensions(sds_id, &in, DFNT_FLOAT32, (void *)in.dscale,
                                                (void *)in.vscale, (void *)in.hscale);
                        if (status == FAIL)
                            goto err;
                    }

                    /* write data to the data set */
                    if (write_SDS(sds_id, &in) == FAIL)
                        goto err;
                    break;

                case 1: /* 64-bit float */

                    /* create data-set */
                    sds_id = create_SDS(sd_id, DFNT_FLOAT64, &in);
                    if (sds_id == FAIL)
                        goto err;

                    if (is_scale == TRUE) {
                        /* set range */
                        if (SDsetrange(sds_id, &in.fp64s.max, &in.fp64s.min) != 0) {
                            fprintf(stderr, "%s", err5a);
                            goto err;
                        }

                        /* set dimension scale */
                        status = set_dimensions(sds_id, &in, DFNT_FLOAT64, (void *)in.fp64s.dscale,
                                                (void *)in.fp64s.vscale, (void *)in.fp64s.hscale);
                        if (status == FAIL)
                            goto err;
                    }

                    /* write data to the data set */
                    if (write_SDS(sds_id, &in) == FAIL)
                        goto err;
                    break;

                case 2: /* 32-bit integer */

                    /* create data-set */
                    sds_id = create_SDS(sd_id, DFNT_INT32, &in);
                    if (sds_id == FAIL)
                        goto err;

                    if (is_scale == TRUE) {
                        /* set range */
                        if (SDsetrange(sds_id, &in.in32s.max, &in.in32s.min) != 0) {
                            fprintf(stderr, "%s", err5a);
                            goto err;
                        }

                        /* set dimension scale */
                        status = set_dimensions(sds_id, &in, DFNT_INT32, (void *)in.in32s.dscale,
                                                (void *)in.in32s.vscale, (void *)in.in32s.hscale);
                        if (status == FAIL)
                            goto err;
                    }

                    /* write data to the data set */
                    if (write_SDS(sds_id, &in) == FAIL)
                        goto err;
                    break;

                case 3: /* 16-bit integer */
                    /* create data-set */
                    sds_id = create_SDS(sd_id, DFNT_INT16, &in);
                    if (sds_id == FAIL)
                        goto err;

                    if (is_scale == TRUE) {
                        /* set range */
                        if (SDsetrange(sds_id, &in.in16s.max, &in.in16s.min) != 0) {
                            fprintf(stderr, "%s", err5a);
                            goto err;
                        }

                        /* set dimension scale */
                        status = set_dimensions(sds_id, &in, DFNT_INT16, (void *)in.in16s.dscale,
                                                (void *)in.in16s.vscale, (void *)in.in16s.hscale);
                        if (status == FAIL)
                            goto err;
                    }

                    /* write data to the data set */
                    if (write_SDS(sds_id, &in) == FAIL)
                        goto err;
                    break;

                case 4: /* 8-bit integer */
                    /* create data-set */
                    sds_id = create_SDS(sd_id, DFNT_INT8, &in);
                    if (sds_id == FAIL)
                        goto err;

                    if (is_scale == TRUE) {
                        /* set range */
                        if (SDsetrange(sds_id, &in.in8s.max, &in.in8s.min) != 0) {
                            fprintf(stderr, "%s", err5a);
                            goto err;
                        }

                        /* set dimension scale */
                        status = set_dimensions(sds_id, &in, DFNT_INT8, (void *)in.in8s.dscale,
                                                (void *)in.in8s.vscale, (void *)in.in8s.hscale);
                        if (status == FAIL)
                            goto err;
                    }

                    /* write data to the data set */
                    if (write_SDS(sds_id, &in) == FAIL)
                        goto err;
                    break;
            }
            /* close data set */
            if (SDendaccess(sds_id) == FAIL) {
                fprintf(stderr, "%s", err6a);
                goto err;
            }

            /* close the input file */
            if (in.is_hdf == TRUE) {
                if (SDend(opt->infiles[i].handle) == FAIL) {
                    fprintf(stderr, "SDend failed");
                    goto err;
                }
            }
        } /* if opt->to_float == TRUE */

        /*
         * put the input data in the HDF output file, in RIS8 format
         */
        if (opt->to_image == TRUE) {
            /*
             * allocate a buffer for the output image
             */
            im.hres = (opt->hres == 0) ? in.dims[0] : opt->hres;
            if ((im.hres < in.dims[0]) && (opt->ctm == EXPAND)) {
                fprintf(stderr, "%s", err3a);
                fprintf(stderr, err3b, "Horiz.");
                fprintf(stderr, err3c, "horiz.");
                fprintf(stderr, err3d, in.dims[0]);
                im.hres   = in.dims[0];
                opt->hres = in.dims[0];
            }
            im.vres = (opt->vres == 0) ? in.dims[1] : opt->vres;
            if ((im.vres < in.dims[1]) && (opt->ctm == EXPAND)) {
                fprintf(stderr, "%s", err3a);
                fprintf(stderr, err3b, "Vert.");
                fprintf(stderr, err3c, "vert.");
                fprintf(stderr, err3d, in.dims[1]);
                im.vres   = in.dims[1];
                opt->vres = in.dims[1];
            }
            im.dres = 1;
            if (in.rank == 3) {
                im.dres = (opt->dres == 0) ? in.dims[2] : opt->dres;
                if ((im.dres < in.dims[2]) && (opt->ctm == EXPAND)) {
                    fprintf(stderr, "%s", err3a);
                    fprintf(stderr, err3b, "Depth");
                    fprintf(stderr, err3c, "depth");
                    fprintf(stderr, err3d, in.dims[2]);
                    im.dres   = in.dims[2];
                    opt->dres = in.dims[2];
                }
            }
            len = im.hres * im.vres * im.dres;
            if ((im.image = (unsigned char *)malloc((unsigned int)len)) == NULL) {
                fprintf(stderr, "%s", err2);
                goto err;
            }

            /*
             * reset max/min symmetrically about the mean value
             */
            if (opt->mean == TRUE)
                mean(&in, opt);

            /*
             * perform pixel replication or interpolation
             */
            if (opt->ctm == EXPAND) {
                if (pixrep(&in, &im))
                    goto err;
            }
            else { /* INTERP */
                if (interp(&in, &im))
                    goto err;
            }

            len = im.hres * im.vres;
            for (j = 0, ip = im.image; j < im.dres; j++, ip += len) {
                if (DFR8addimage(opt->outfile, ip, im.hres, im.vres, DFTAG_RLE)) {
                    fprintf(stderr, "%s", err4);
                    goto err;
                }
            }

#ifdef DEBUG
            printf("Output Raster Information ...\n\n");
            printf("\tresolution (horiz,vert,[depth]):\n\n");
            if (in.rank == 2)
                printf("\t%d %d\n\n", im.hres, im.vres);
            else
                printf("\t%d %d %d\n\n", im.hres, im.vres, im.dres);
            if (opt->mean == TRUE) {
                printf("\tadjusted max/min values:\n\n");
                printf("\t%f %f\n\n", in.max, in.min);
            }
            printf("\tcolor index values:");
            for (d = 0, ip = im.image; d < im.dres; d++) {
                printf("\n");
                for (v = 0; v < im.vres; v++) {
                    printf("\n");
                    for (h = 0; h < im.hres; h++, ip++)
                        printf("\t%d", *ip);
                }
            }
            printf("\n");
#endif /* DEBUG */
        }
        /*
         * free dynamically allocated space
         */
        fpdeallocate(&in, &im, opt);
    } /* end of for loop */

    /* close the output file */
    if (SDend(sd_id) != 0) {
        fprintf(stderr, "%s", err6);
        goto err;
    }

    return (0);

err:
    return (1);
}

/*
 * Name: (pkamat - New function)
 *      fpdeallocate
 *
 * Purpose:
 *      Deallocate memory of all data structures
 */

void
fpdeallocate(struct Input *in, struct Raster *im, struct Options *opt)
{
    switch (in->outtype) {
        case 0: /* 32-bit float */
        case 5: /* NO_NE */
            free(in->hscale);
            free(in->vscale);
            if (in->rank == 3)
                free(in->dscale);

            if (opt->to_image == TRUE)
                free(im->image);
            break;

        case 1: /* 64-bit float */
            free(in->fp64s.hscale);
            free(in->fp64s.vscale);
            if (in->rank == 3)
                free(in->fp64s.dscale);

            if (opt->to_image == TRUE)
                free(im->image);
            break;

        case 2: /* 32-bit integer */
            free(in->in32s.hscale);
            free(in->in32s.vscale);
            if (in->rank == 3)
                free(in->in32s.dscale);
            break;

        case 3: /* 16-bit integer */
            free(in->in16s.hscale);
            free(in->in16s.vscale);
            if (in->rank == 3)
                free(in->in16s.dscale);
            break;

        case 4: /* 8-bit integer */
            free(in->in8s.hscale);
            free(in->in8s.vscale);
            if (in->rank == 3)
                free(in->in8s.dscale);
            break;
    }
    free(in->data);
}

/*
 * Name: (pkamat - New function)
 *      init_scales
 *
 * Purpose:
 *      Initialise the data-structures to hold scale information
 * Modification: pvn: March, 3, 2006
 *  handled the case of in->outtype == 5 (NO_NE), for hdf input type
 *  current version assumes that datum is DFNT_FLOAT32 for this case
 */
static int
init_scales(struct Input *in)
{
    const char *err1 = "Unable to dynamically allocate memory.\n";
    switch (in->outtype) {
        case 0: /* 32-bit float */
        case 5: /* NO_NE */
            if ((in->hscale = (float32 *)malloc((size_t)(in->dims[0] + 1) * sizeof(float32))) == NULL) {
                fprintf(stderr, "%s", err1);
                goto err;
            }
            if ((in->vscale = (float32 *)malloc((size_t)(in->dims[1] + 1) * sizeof(float32))) == NULL) {
                fprintf(stderr, "%s", err1);
                goto err;
            }
            if (in->rank == 3) {
                if ((in->dscale = (float32 *)malloc((size_t)(in->dims[2] + 1) * sizeof(float32))) == NULL) {
                    fprintf(stderr, "%s", err1);
                    goto err;
                }
            }
            break;

        case 1: /* 64-bit float */

            if ((in->fp64s.hscale = (float64 *)malloc((size_t)(in->dims[0] + 1) * sizeof(float64))) == NULL) {
                fprintf(stderr, "%s", err1);
                goto err;
            }
            if ((in->fp64s.vscale = (float64 *)malloc((size_t)(in->dims[1] + 1) * sizeof(float64))) == NULL) {
                fprintf(stderr, "%s", err1);
                goto err;
            }
            if (in->rank == 3) {
                if ((in->fp64s.dscale = (float64 *)malloc((size_t)(in->dims[2] + 1) * sizeof(float64))) ==
                    NULL) {
                    fprintf(stderr, "%s", err1);
                    goto err;
                }
            }
            break;
        case 2: /* 32-bit integer */
            if ((in->in32s.hscale = (int32 *)malloc((size_t)(in->dims[0] + 1) * sizeof(int32))) == NULL) {
                fprintf(stderr, "%s", err1);
                goto err;
            }
            if ((in->in32s.vscale = (int32 *)malloc((size_t)(in->dims[1] + 1) * sizeof(int32))) == NULL) {
                fprintf(stderr, "%s", err1);
                goto err;
            }
            if (in->rank == 3) {
                if ((in->in32s.dscale = (int32 *)malloc((size_t)(in->dims[2] + 1) * sizeof(int32))) == NULL) {
                    fprintf(stderr, "%s", err1);
                    goto err;
                }
            }
            break;

        case 3: /* 16-bit integer */
            if ((in->in16s.hscale = (int16 *)malloc((size_t)(in->dims[0] + 1) * sizeof(int16))) == NULL) {
                fprintf(stderr, "%s", err1);
                goto err;
            }
            if ((in->in16s.vscale = (int16 *)malloc((size_t)(in->dims[1] + 1) * sizeof(int16))) == NULL) {
                fprintf(stderr, "%s", err1);
                goto err;
            }
            if (in->rank == 3) {
                if ((in->in16s.dscale = (int16 *)malloc((size_t)(in->dims[2] + 1) * sizeof(int16))) == NULL) {
                    fprintf(stderr, "%s", err1);
                    goto err;
                }
            }
            break;

        case 4: /* 8-bit integer */
            if ((in->in8s.hscale = (int8 *)malloc((size_t)(in->dims[0] + 1) * sizeof(int8))) == NULL) {
                fprintf(stderr, "%s", err1);
                goto err;
            }
            if ((in->in8s.vscale = (int8 *)malloc((size_t)(in->dims[1] + 1) * sizeof(int8))) == NULL) {
                fprintf(stderr, "%s", err1);
                goto err;
            }
            if (in->rank == 3) {
                if ((in->in8s.dscale = (int8 *)malloc((size_t)(in->dims[2] + 1) * sizeof(int8))) == NULL) {
                    fprintf(stderr, "%s", err1);
                    goto err;
                }
            }
            break;
    }
    return (0);
err:
    return (1);
}

/*
 * Name:
 *      usage
 *
 * Purpose:
 *      Print a summary of command usage.
 */
void
usage(char *name)
{
    fprintf(stderr, "\nUsage:\t%s -h[elp], OR\n", name);
    fprintf(stderr, "\t%s -V, OR\n", name);
    fprintf(stderr, "\t%s <infile> [ [-t[ype] <output-type> | -n] ", name);
    fprintf(stderr, "[<infile> [-t[ype] <output-type> | -n ]]...]\n");
    fprintf(stderr, "\t\t\t\t\t-o[utfile] <outfile> [options..]\n");
    fprintf(stderr, "\n\t-t[ype] <output_type>");

    fprintf(stderr, "\n\t\tOptionally used for every input ASCII file to specify the");
    fprintf(stderr, "\n\t\tdata type of the data-set to be written. If not specified");
    fprintf(stderr, "\n\t\tdefault data type is 32-bit floating point. <output-type>");
    fprintf(stderr, "\n\t\tcan be any of the following: FP32 (default), FP64, INT32");
    fprintf(stderr, "\n\t\tINT16, INT8. It can be used only with ASCII files.");

    fprintf(stderr, "\n\t-n");
    fprintf(stderr, "\n\t\tThis option is to be used only if the binary input file ");
    fprintf(stderr, "\n\t\tcontains 64-bit floating point data and the default");
    fprintf(stderr, "\n\t\tbehaviour (default behaviour is to write it to a 32-bit");
    fprintf(stderr, "\n\t\tfloating point data-set) should be overridden to write ");
    fprintf(stderr, "\n\t\tit to a 64-bit floating point data-set.");
    fprintf(stderr, "\n\n\toptions...\n");
    fprintf(stderr, "\n\t-r[aster]:\n");
    fprintf(stderr, "\t\tproduce an image.  Could be ");
    fprintf(stderr, "followed by:\n");
    fprintf(stderr, "\t\t-e[xpand] <horiz> <vert> ");
    fprintf(stderr, "[<depth>]:\n");
    fprintf(stderr, "\t\t\t resolution with pixel ");
    fprintf(stderr, "replication\n");
    fprintf(stderr, "\t\t-i[nterp] <horiz> <vert> ");
    fprintf(stderr, "[<depth>]:\n");
    fprintf(stderr, "\t\t\tresolution with interpolation\n");
    fprintf(stderr, "\t\t-p[alfile] <palfile>:\n");
    fprintf(stderr, "\t\t\tinclude palette from palfile\n");
    fprintf(stderr, "\t\t-m[ean] <meanval>:\n");
    fprintf(stderr, "\t\t\tmean value to scale image ");
    fprintf(stderr, "around\n");
    fprintf(stderr, "\t-f[loat]:\n");
    fprintf(stderr, "\t\tproduce floating point data\n\n");
}