File: cif2cbf.c

package info (click to toggle)
cbflib 0.9.7%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 65,272 kB
  • sloc: ansic: 131,361; python: 22,780; sh: 3,108; makefile: 2,088; yacc: 659; java: 223; f90: 214; xml: 210; cpp: 58
file content (3129 lines) | stat: -rw-r--r-- 177,530 bytes parent folder | download | duplicates (3)
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
/**********************************************************************
 *          cif2cbf -- convert a cif to a cbf file                    *
 *                                                                    *
 * Version 0.9.5.13  13 October 2015                                  *
 *                                                                    *
 *                          Paul Ellis and                            *
 *         Herbert J. Bernstein (yaya@bernstein-plus-sons.com)        *
 *                                                                    *
 * (C) Copyright 2006 -- 2015 Herbert J. Bernstein                    *
 *                                                                    *
 **********************************************************************/

/**********************************************************************
 *                                                                    *
 * YOU MAY REDISTRIBUTE THE CBFLIB PACKAGE UNDER THE TERMS OF THE GPL *
 * WHILE YOU MAY ALTERNATIVE DISTRIBUTE THE API UNDER THE LGPL        *
 * YOU MAY ***NOT*** DISTRBUTE THIS PROGRAM UNDER THE LGPL            *
 *                                                                    *
 **********************************************************************/

/*************************** GPL NOTICES ******************************
 *                                                                    *
 * This program is free software; you can redistribute it and/or      *
 * modify it under the terms of the GNU General Public License as     *
 * published by the Free Software Foundation; either version 2 of     *
 * (the License, or (at your option) any later version.               *
 *                                                                    *
 * This program is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *
 * GNU General Public License for more details.                       *
 *                                                                    *
 * You should have received a copy of the GNU General Public License  *
 * along with this program; if not, write to the Free Software        *
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA           *
 * 02111-1307  USA                                                    *
 *                                                                    *
 **********************************************************************/

/**********************************************************************
 *                                SYNOPSIS                            *
 *                                                                    *
 *  cif2cbf [-i input_cif] [-o output_cbf] \                          *
 *    [-u update_cif ] \                                              *
 *    [-b {f[orward]|b[ackwards]}] \                                  *
 *    [-B {read|liberal|noread}] [-B {write|nowrite}] \               *
 *    [-c {p[acked]|c[annonical]|{b[yte_offset]}|\                    *
 *        {v[2packed]}|{f[latpacked]}|{I|nIbble_offset}|{L|LZ4}|      *
 *        {2|LZ4**2}|{BS|BSLZ4}|n[n[one]}] \                           *
 *    [-C highclipvalue ] \                                           *
 *    [-D ] \                                                         *
 *    [-d {d[igest]|n[odigest]|w[warndigest]} \                       *
 *    [-e {b[ase64]|k|q[uoted-printable]| \                           *
 *                  d[ecimal]|h[exadecimal]|o[ctal]|n[one]}] \        *
 *    [-I {0|2|4|8}] \                                                *
 *    [-L lowclipvalue ] \                                            *
 *    [-m {h[eaders]|n[oheaders]}] \                                  *
 *    [-m {dim[ensions]|nod[imensions}] \                             *
 *    [-p {0|1|2|4}] \                                                *
 *    [-R {0|4|8} \                                                   *
 *    [-S {read|noread}] [-S {write|nowrite}] \                       *
 *    [-T {read|noread}] [-T {write|nowrite}] \                       *
 *    [-v dictionary]* [-w] [-D]\                                     *
 *    [-O] \                                                          *
 *    [-5 {r|w|rw|rn|wn|rwn|n[oH5]}] \                                *
 *    [--nxpdb]                                                       *
 *    [--register {manual|plugin}] \                                  *
 *    [--add-minicbf-header] \                                        *
 *    [--minicbf] \                                                   *
 *    [--data-last] \                                                 *
 *    [--{eoi|elements-of-interest} element[,elementhigh] \           *
 *    [--{foi|frames-of-interest}  frame[,framehigh] \                *
 *    [--{roi|region-of-interest} fastlow,fasthigh\                   *
 *                                [,midlow,midhigh[,slowlow,slowhigh]]*
 *    [--add-update-data]                                             *
 *    [--subtract-update-data]                                        *
 *    [--merge-datablocks-by-number]                                  *
 *    [--merge-datablocks-by-name]                                    *
 *    [-U n] \                                                        *
 *    [input_cif] [output_cbf]                                        *
 *                                                                    *
 *  the options are:                                                  *
 *                                                                    *
 *  -i input_cif (default: stdin)                                     *
 *    the input  file in CIF or CBF  format.  If input_cif is not     *
 *    specified or is given as "-", it is copied from stdin to a      *
 *    temporary file.                                                 *
 *                                                                    *
 *  -o output_cbf (default: stdout)                                   *
 *    the output cif (if base64 or quoted-printable encoding is used) *
 *    or cbf (if no encoding is used).  if no output_cif is specified *
 *    or is given as "-", the output is written to stdout             *
 *    if the output_cbf is /dev/null, no output is written.           *
 *                                                                    *
 *  -u update_cif (no default)                                        *
 *    and optional second input file in CIF or CBF format containing  *
 *    data blocks to be merged with data blocks from the primary      *
 *    input CIF or CBF                                                *
 *                                                                    *
 *  The remaining options specify the characteristics of the          *
 *  output cbf.  Most of the characteristics of the input cif are     *
 *  derived from context, except when modified by the -B, -S, -T, -v  *
 *  and -w flags.                                                     *
 *                                                                    *
 *  -b byte_order (forward or backwards, default forward (1234) on    *
 *    little-endian machines, backwards (4321) on big-endian machines *
 *                                                                    *
 *  -B [no]read or liberal (default noread)                           *
 *    read to enable reading of DDLm style brackets                   *
 *    liberal to accept whitespace for commas                         *
 *                                                                    *
 *  -B [no]write (default write)                                      *
 *    write to enable writing of DDLm style brackets                  *
 *                                                                    *
 *  -c compression_scheme (Packed, Canonical, Byte_offset,            *
 *    V2packed, Flatpacked, nIbble, zlib, LZ4, LZ4**2, BSLZ4 or None, *
 *    default packed)                                                 *
 *                                                                    *
 *  -C highclipvalue                                                  *
 *    specifies a double precision value to which to clip the data    *
 *                                                                    *
 *  -d [no]digest or warndigest  (default md5 digest [R. Rivest,      *
 *    RFC 1321, April 1992 using"RSA Data Security, Inc. MD5          *
 *    Message-Digest Algorithm"] when MIME headers are selected)      *
 *                                                                    *
 *  -D test cbf_construct_detector                                    *
 *                                                                    *
 *  -e encoding (base64, k, quoted-printable or none, default base64) *
 *    specifies one of the standard MIME encodings for an ascii cif   *
 *    or "none" for a binary cbf                                      *
 *                                                                    *
 *  -I 0 or integer element size                                      *
 *    specifies integer conversion of the data, 0 to use the input    *
 *    number of bytes, 2, 4 or 8 for short, long or long long         *
 *    output integers                                                 *
 *                                                                    *
 *  -L lowclipvalue                                                   *
 *    specifies a double precision value to cut off the data from     *
 *    below                                                           *
 *                                                                    *
 *  -m [no]headers (default headers)                                  *
 *    selects MIME (N. Freed, N. Borenstein, RFC 2045, November 1996) *
 *    headers within binary data value text fields.                   *
 *                                                                    *
 *  -m [nod]imensions (default dimensions)                            *
 *    selects detailed recovery of dimensions from the input CIF      *
 *    for use in the MIME header of the output CIF                    *
 *                                                                    *
 *  -p K_of_padding (0, 1, 2, 4) for no padding after binary data     *
 *    1023, 2047 or 4095 bytes of padding after binary data           *
 *                                                                    *
 *  -R 0 or integer element size                                      *
 *    specifies real conversion of the data, 0 to use the input       *
 *    number of bytes,  4 or 8 for float or double output reals       *
 *                                                                    *
 *  -S [no]read or (default noread)                                   *
 *    read to enable reading of whitespace and comments               *
 *                                                                    *
 *  -S [no]write (default write)                                      *
 *    write to enable writing of whitespace and comments              *
 *                                                                    *
 *  -T [no]read or (default noread)                                   *
 *    read to enable reading of DDLm style triple quotes              *
 *                                                                    *
 *  -T [no]write (default write)                                      *
 *    write to enable writing of DDLm style triple quotes             *
 *                                                                    *
 *  -v dictionary specifies a dictionary to be used to validate       *
 *    the input cif and to apply aliases to the output cif.           *
 *    This option may be specified multiple times, with dictionaries  *
 *    layered in the order given.                                     *
 *                                                                    *
 *  -w process wide (2048 character) lines                            *
 *                                                                    *
 *  -W write wide (2048 character) lines                              *
 *                                                                    *
 *  -5 hdf5mode specifies whether to read and/or write in hdf5 mode   *
 *     the n parameter will cause the CIF H5 datablock to be deleted  *
 *     on both read and write, for both CIF, CBF and HDF5 files       *
 *                                                                    *
 *  --nxpdb                                                           *
 *     when in -5 w (hdf5 write) mode, --nxpdb forced the use of      *
 *     NXpdb conventions                                              *
 *                                                                    *
 *  -O when in -5 w (hdf5 write) mode, -O forces the use of opaque    *
 *     objects for CBF binaries                                       *
 *                                                                    *
 *                                                                    *
 *  --register manual or plugin (default plugin)                      *
 *     controls whether to rely on the HDF5 filter plugin mechanism   *
 *     or to manually register the CBFlib compression for HDF5        *
 *                                                                    *
 *  --add-minicbf-header                                              *
 *     add a minicbf header or replaces an exiting minicbf header     *
 *     in _array_data.header_contents                                 *
 *                                                                    *
 *  --minicbf                                                         *
 *     convert to a minicbf, implies --add-minicbf_header and         *
 *     --data-last                                                    *
 *                                                                    *
 *  --data-last                                                       *
 *     move array_data to be the last category and _array_data.data   *
 *     to be the last tag in that category                            *
 *                                                                    *
 *  --{eoi|elements-of-interest} element                              *
 *  --{eoi|elements-of-interest} element,elementhigh                  *
 *     delector elements of interest.  If only a single element       *
 *     is specified, only that one element is used                    *
 *                                                                    *
 *  --{foi|frames-of-interest} frame                                  *
 *  --{foi|frames-of-interest} frame,framehigh                        *
 *     frames of interest.  If only a single frame                    *
 *     is specified, only that one frame is used                      *
 *                                                                    *
 *  --roi fastlow,fasthigh                                            *
 *  --roi fastlow,fasthigh,slowlow,slowhigh                           *
 *  --roi fastlow,fasthigh,midlow,midhigh,slowlow,slowhigh            *
 *     restrict data transfers to the indicated range                 *
 *                                                                    *
 *  --region-of-interest  ...                                         *
 *     synonym for roi                                                *
 *                                                                    *
 *  --add-update-data                                                 *
 *  --subtract-update-data                                            *
 *     if an update cbf with data is provided via the -u option       *
 *     the add-update-data option causes the updated data to be added *
 *     pixel-by-pixel to the input cbf data, and                      *
 *     the subtract-update-data option causes the updated date to be  *
 *     subtracted pixe;-by-pixel from the input cbf data              *
 *     In both operations, ~0 in either file overrides the operation  *
 *                                                                    *
 *  --merge-datablocks-by-number                                      *
 *     when merging an update cif, align datablocks by their ordinal  *
 *     rather than by their names                                     *
 *  --merge-datablocks-by-name                                        *
 *     when merging an update cif, align datablocks by their names    *
 *     rather than by their ordinals                                  *
 *                                                                    *
 *  --U n                                                             *
 *     test cbf_construct_detector in element_id n                    *
 *                                                                    *
 *                                                                    *
 **********************************************************************/

/**********************************************************************
 *                                CREDITS                             *
 *                                                                    *
 *  This program is a Crystallographic Information File (CIF)         *
 *  application.  Please see the IUCR Policy below.   See the IUCR    *
 *  web page (http://www.iucr.org) or its mirrors for background      *
 *  and references on CIF.                                            *
 *                                                                    *
 *  This program is a Crystallographic Binary File (CBF) application. *
 *  Please see the ImgCIF/CBF web page at                             *
 *                                                                    *
 *          http://www.bernstein-plus-sons.com/software/CBF           *
 *                                                                    *
 *                                                                    *
 *  This program uses routines derived from mpack/munpack version     *
 *  1.5, ftp://ftp.andrew.cmu.edu/pub/mpack by John G. Myers,         *
 *  jgm+@cmu.edu.  "Mpack and munpack are utilties for encoding and   *
 *  decoding ... binary files in MIME ... format."  Please see the    *
 *  copyright notices and disclaimers in the mpack/munpack routines   *
 *                                                                    *
 *  This program uses routines derived from the "RSA Data Security,   *
 *  Inc. MD5 Message-Digest Algorithm."  Please see the copyright     *
 *  notice and disclaimer in md5c.c                                   *
 **********************************************************************/


/**********************************************************************
 *                                                                    *
 *                    Stanford University Notices                     *
 *  for the CBFlib software package that incorporates SLAC software   *
 *                 on which copyright is disclaimed                   *
 *                                                                    *
 * This software                                                      *
 * -------------                                                      *
 * The term 'this software', as used in these Notices, refers to      *
 * those portions of the software package CBFlib that were created by *
 * employees of the Stanford Linear Accelerator Center, Stanford      *
 * University.                                                        *
 *                                                                    *
 * Stanford disclaimer of copyright                                   *
 * --------------------------------                                   *
 * Stanford University, owner of the copyright, hereby disclaims its  *
 * copyright and all other rights in this software.  Hence, anyone    *
 * may freely use it for any purpose without restriction.             *
 *                                                                    *
 * Acknowledgement of sponsorship                                     *
 * ------------------------------                                     *
 * This software was produced by the Stanford Linear Accelerator      *
 * Center, Stanford University, under Contract DE-AC03-76SFO0515 with *
 * the Department of Energy.                                          *
 *                                                                    *
 * Government disclaimer of liability                                 *
 * ----------------------------------                                 *
 * Neither the United States nor the United States Department of      *
 * Energy, nor any of their employees, makes any warranty, express or *
 * implied, or assumes any legal liability or responsibility for the  *
 * accuracy, completeness, or usefulness of any data, apparatus,      *
 * product, or process disclosed, or represents that its use would    *
 * not infringe privately owned rights.                               *
 *                                                                    *
 * Stanford disclaimer of liability                                   *
 * --------------------------------                                   *
 * Stanford University makes no representations or warranties,        *
 * express or implied, nor assumes any liability for the use of this  *
 * software.                                                          *
 *                                                                    *
 * Maintenance of notices                                             *
 * ----------------------                                             *
 * In the interest of clarity regarding the origin and status of this *
 * software, this and all the preceding Stanford University notices   *
 * are to remain affixed to any copy or derivative of this software   *
 * made or distributed by the recipient and are to be affixed to any  *
 * copy of software made or distributed by the recipient that         *
 * contains a copy or derivative of this software.                    *
 *                                                                    *
 * Based on SLAC Software Notices, Set 4                              *
 * OTT.002a, 2004 FEB 03                                              *
 **********************************************************************/


/**********************************************************************
 *                                 NOTICE                             *
 * Creative endeavors depend on the lively exchange of ideas. There   *
 * are laws and customs which establish rights and responsibilities   *
 * for authors and the users of what authors create.  This notice     *
 * is not intended to prevent you from using the software and         *
 * documents in this package, but to ensure that there are no         *
 * misunderstandings about terms and conditions of such use.          *
 *                                                                    *
 * Please read the following notice carefully.  If you do not         *
 * understand any portion of this notice, please seek appropriate     *
 * professional legal advice before making use of the software and    *
 * documents included in this software package.  In addition to       *
 * whatever other steps you may be obliged to take to respect the     *
 * intellectual property rights of the various parties involved, if   *
 * you do make use of the software and documents in this package,     *
 * please give credit where credit is due by citing this package,     *
 * its authors and the URL or other source from which you obtained    *
 * it, or equivalent primary references in the literature with the    *
 * same authors.                                                      *
 *                                                                    *
 * Some of the software and documents included within this software   *
 * package are the intellectual property of various parties, and      *
 * placement in this package does not in any way imply that any       *
 * such rights have in any way been waived or diminished.             *
 *                                                                    *
 * With respect to any software or documents for which a copyright    *
 * exists, ALL RIGHTS ARE RESERVED TO THE OWNERS OF SUCH COPYRIGHT.   *
 *                                                                    *
 * Even though the authors of the various documents and software      *
 * found here have made a good faith effort to ensure that the        *
 * documents are correct and that the software performs according     *
 * to its documentation, and we would greatly appreciate hearing of   *
 * any problems you may encounter, the programs and documents any     *
 * files created by the programs are provided **AS IS** without any   *
 * warranty as to correctness, merchantability or fitness for any     *
 * particular or general use.                                         *
 *                                                                    *
 * THE RESPONSIBILITY FOR ANY ADVERSE CONSEQUENCES FROM THE USE OF    *
 * PROGRAMS OR DOCUMENTS OR ANY FILE OR FILES CREATED BY USE OF THE   *
 * PROGRAMS OR DOCUMENTS LIES SOLELY WITH THE USERS OF THE PROGRAMS   *
 * OR DOCUMENTS OR FILE OR FILES AND NOT WITH AUTHORS OF THE          *
 * PROGRAMS OR DOCUMENTS.                                             *
 **********************************************************************/

/**********************************************************************
 *                                                                    *
 *                           The IUCr Policy                          *
 *      for the Protection and the Promotion of the STAR File and     *
 *     CIF Standards for Exchanging and Archiving Electronic Data     *
 *                                                                    *
 * Overview                                                           *
 *                                                                    *
 * The Crystallographic Information File (CIF)[1] is a standard for   *
 * information interchange promulgated by the International Union of  *
 * Crystallography (IUCr). CIF (Hall, Allen & Brown, 1991) is the     *
 * recommended method for submitting publications to Acta             *
 * Crystallographica Section C and reports of crystal structure       *
 * determinations to other sections of Acta Crystallographica         *
 * and many other journals. The syntax of a CIF is a subset of the    *
 * more general STAR File[2] format. The CIF and STAR File approaches *
 * are used increasingly in the structural sciences for data exchange *
 * and archiving, and are having a significant influence on these     *
 * activities in other fields.                                        *
 *                                                                    *
 * Statement of intent                                                *
 *                                                                    *
 * The IUCr's interest in the STAR File is as a general data          *
 * interchange standard for science, and its interest in the CIF,     *
 * a conformant derivative of the STAR File, is as a concise data     *
 * exchange and archival standard for crystallography and structural  *
 * science.                                                           *
 *                                                                    *
 * Protection of the standards                                        *
 *                                                                    *
 * To protect the STAR File and the CIF as standards for              *
 * interchanging and archiving electronic data, the IUCr, on behalf   *
 * of the scientific community,                                       *
 *                                                                    *
 * * holds the copyrights on the standards themselves,                *
 *                                                                    *
 * * owns the associated trademarks and service marks, and            *
 *                                                                    *
 * * holds a patent on the STAR File.                                 *
 *                                                                    *
 * These intellectual property rights relate solely to the            *
 * interchange formats, not to the data contained therein, nor to     *
 * the software used in the generation, access or manipulation of     *
 * the data.                                                          *
 *                                                                    *
 * Promotion of the standards                                         *
 *                                                                    *
 * The sole requirement that the IUCr, in its protective role,        *
 * imposes on software purporting to process STAR File or CIF data    *
 * is that the following conditions be met prior to sale or           *
 * distribution.                                                      *
 *                                                                    *
 * * Software claiming to read files written to either the STAR       *
 * File or the CIF standard must be able to extract the pertinent     *
 * data from a file conformant to the STAR File syntax, or the CIF    *
 * syntax, respectively.                                              *
 *                                                                    *
 * * Software claiming to write files in either the STAR File, or     *
 * the CIF, standard must produce files that are conformant to the    *
 * STAR File syntax, or the CIF syntax, respectively.                 *
 *                                                                    *
 * * Software claiming to read definitions from a specific data       *
 * dictionary approved by the IUCr must be able to extract any        *
 * pertinent definition which is conformant to the dictionary         *
 * definition language (DDL)[3] associated with that dictionary.      *
 *                                                                    *
 * The IUCr, through its Committee on CIF Standards, will assist      *
 * any developer to verify that software meets these conformance      *
 * conditions.                                                        *
 *                                                                    *
 * Glossary of terms                                                  *
 *                                                                    *
 * [1] CIF:  is a data file conformant to the file syntax defined     *
 * at http://www.iucr.org/iucr-top/cif/spec/index.html                *
 *                                                                    *
 * [2] STAR File:  is a data file conformant to the file syntax       *
 * defined at http://www.iucr.org/iucr-top/cif/spec/star/index.html   *
 *                                                                    *
 * [3] DDL:  is a language used in a data dictionary to define data   *
 * items in terms of "attributes". Dictionaries currently approved    *
 * by the IUCr, and the DDL versions used to construct these          *
 * dictionaries, are listed at                                        *
 * http://www.iucr.org/iucr-top/cif/spec/ddl/index.html               *
 *                                                                    *
 * Last modified: 30 September 2000                                   *
 *                                                                    *
 * IUCr Policy Copyright (C) 2000 International Union of              *
 * Crystallography                                                    *
 **********************************************************************/

#include "cbf.h"
#include "cbf_simple.h"
#include "img.h"
#include "cbf_string.h"
#include "cbf_copy.h"
#include "cbf_hdf5.h"
#include "cbf_alloc.h"
#include "cbf_minicbf_header.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include "cbf_getopt.h"
#include <unistd.h>

#define C2CBUFSIZ 8192
#define NUMDICTS 50

#ifdef __MINGW32__
#define NOMKSTEMP
#define NOTMPDIR
#endif

#define HDR_FINDDIMS    0x0040  /* On read, find header dims          */
#define HDR_NOFINDDIMS  0x0080  /* On read, don't find header dims    */

#define HDF5_READ_MODE  0x0001  /* Read the input file as HDF5        */
#define HDF5_WRITE_MODE 0x0002  /* Write the output file as HDF5      */
#define HDF5_NONE       0x0004  /* Flag for neither                   */



int local_exit (int status);
int outerror(int err);

int outerror(int err)
{

    if ((err&CBF_FORMAT)==CBF_FORMAT)
        fprintf(stderr, " cif2cbf: The file format is invalid.\n");
    if ((err&CBF_ALLOC)==CBF_ALLOC)
        fprintf(stderr, " cif2cbf Memory allocation failed.\n");
    if ((err&CBF_ARGUMENT)==CBF_ARGUMENT)
        fprintf(stderr, " cif2cbf: Invalid function argument.\n");
    if ((err&CBF_ASCII)==CBF_ASCII)
        fprintf(stderr, " cif2cbf: The value is ASCII (not binary).\n");
    if ((err&CBF_BINARY)==CBF_BINARY)
        fprintf(stderr, " cif2cbf: The value is binary (not ASCII).\n");
    if ((err&CBF_BITCOUNT)==CBF_BITCOUNT)
        fprintf(stderr, " cif2cbf: The expected number of bits does"
                " not match the actual number written.\n");
    if ((err&CBF_ENDOFDATA)==CBF_ENDOFDATA)
        fprintf(stderr, " cif2cbf: The end of the data was reached"
                " before the end of the array.\n");
    if ((err&CBF_FILECLOSE)==CBF_FILECLOSE)
        fprintf(stderr, " cif2cbf: File close error.\n");
    if ((err&CBF_FILEOPEN)==CBF_FILEOPEN)
        fprintf(stderr, " cif2cbf: File open error.\n");
    if ((err&CBF_FILEREAD)==CBF_FILEREAD)
        fprintf(stderr, " cif2cbf: File read error.\n");
    if ((err&CBF_FILESEEK)==CBF_FILESEEK)
        fprintf(stderr, " cif2cbf: File seek error.\n");
    if ((err&CBF_FILETELL)==CBF_FILETELL)
        fprintf(stderr, " cif2cbf: File tell error.\n");
    if ((err&CBF_FILEWRITE)==CBF_FILEWRITE)
        fprintf(stderr, " cif2cbf: File write error.\n");
    if ((err&CBF_IDENTICAL)==CBF_IDENTICAL)
        fprintf(stderr, " cif2cbf: A data block with the new name already exists.\n");
    if ((err&CBF_NOTFOUND)==CBF_NOTFOUND)
        fprintf(stderr, " cif2cbf: The data block, category, column or"
                " row does not exist.\n");
    if ((err&CBF_OVERFLOW)==CBF_OVERFLOW)
        fprintf(stderr, " cif2cbf: The number read cannot fit into the "
                "destination argument.\n        The destination has been set to the nearest value.\n");
    if ((err& CBF_UNDEFINED)==CBF_UNDEFINED)
        fprintf(stderr, " cif2cbf: The requested number is not defined (e.g. 0/0).\n");
    if ((err&CBF_NOTIMPLEMENTED)==CBF_NOTIMPLEMENTED)
        fprintf(stderr, " cif2cbf: The requested functionality is not yet implemented.\n");
    if ((err&CBF_H5ERROR)==CBF_H5ERROR)
        fprintf(stderr, " cif2cbf: HDF5 API error.\n");
    return 0;

}

#undef cbf_failnez
#undef cbf_onfailnez

#ifndef __FILE__

#define cbf_failnez(x) {int err; err = (x); if (err) { fprintf (stderr, \
"\nCBFlib error %d \n", err); outerror(err); local_exit (-1); }}

#define cbf_onfailnez(x,c) {int err; err = (x); if (err) { fprintf (stderr, \
"\nCBFlib error %d \n", err); \
{ c; } outerror(err); local_exit (-1); }}
#else
#ifndef __func__
#define cbf_failnez(x) {int err; err = (x); if (err) { fprintf (stderr, \
"\nCBFlib error %d at %s:%d\n", err,__FILE__,__LINE__); outerror(err); local_exit (-1); }}

#define cbf_onfailnez(x,c) {int err; err = (x); if (err) { fprintf (stderr, \
"\nCBFlib error %d at %s:%d\n", err,__FILE__,__LINE__); \
{ c; } outerror(err); local_exit (-1); }}
#else
#define cbf_failnez(x) {int err; err = (x); if (err) { fprintf (stderr, \
"\nCBFlib error %d at %s:%d(%s)\n", err,__FILE__,__LINE__,__func__); outerror(err); local_exit (-1); }}

#define cbf_onfailnez(x,c) {int err; err = (x); if (err) { fprintf (stderr, \
"\nCBFlib error %d at %s:%d(%s)\n", err,__FILE__,__LINE__,__func__); \
{ c; } outerror(err); local_exit (-1); }}

#endif
#endif



/* Find row by multiple columns
 n is the number of columns
 column is an array of n column names
 value is an array of values to match
 
 */

static int cbf_find_row_by_columns(cbf_handle handle, int n,
                                   const char * column[],
                                   const char * value[]){
    
    int icol;
    
    int match;
    
    const char *colvalue;
    
    if (!handle || n < 1
        || !column || !value
        || !column[0] || !value[0]) return CBF_ARGUMENT;
    
    cbf_failnez(cbf_rewind_row(handle));
    
    while (!cbf_find_column(handle,column[0])
           &&!cbf_find_nextrow(handle,value[0])) {
        
        match = 1;
        
        for (icol = 1; icol < n; icol++) {
            
            if ( !column[icol] || !value[icol] ) return CBF_ARGUMENT;
            
            cbf_failnez(cbf_find_column(handle,column[icol]));
            
            if (cbf_get_value(handle,&colvalue)
                || !colvalue
                || cbf_cistrcmp(colvalue,value[icol])) {
                
                match = 0;
                
                break;
                
            }
            
        }
        
        if (match) return CBF_SUCCESS;
        
    }
    
    return CBF_NOTFOUND;
    
}


/* Get the frame_id and frame_number for a given array_id and binary_id */

static int cbf_get_frame(cbf_handle handle,
                  const char * array_id,
                  const char * binary_id,
                  const char * * frame_id,
                  unsigned int * frame_number){
    
    const char * xframe_id;
    
    const char * columns[2] = {"array_id", "binary_id"};
    
    const char * values[2] = {array_id, binary_id};
    
    int xframe_number;
    
    if (!handle || !array_id || !binary_id ) return CBF_ARGUMENT;
    
    if ((!cbf_find_category(handle,"diffrn_data_frame")
         ||!cbf_find_category(handle,"diffrn_frame_data"))
        && !cbf_rewind_row(handle)
        && !cbf_find_row_by_columns(handle,2,columns,values)
        && !cbf_find_column(handle,"id")
        && !cbf_get_value(handle,&xframe_id)){
        
        if (frame_id) *frame_id = xframe_id;
        
        if (!cbf_find_category(handle,"diffrn_scan_frame")
            &&!cbf_rewind_row(handle)
            &&!cbf_find_column(handle,"frame_id")
            &&!cbf_find_row(handle,xframe_id)
            &&!cbf_find_column(handle,"frame_number")
            &&!cbf_get_integervalue(handle,&xframe_number)
            &&xframe_number >= 0) {
            
            if (frame_number) *frame_number = (unsigned int)xframe_number;
            return CBF_SUCCESS;
            
        }
        
        
    }
    
    return CBF_NOTFOUND;
    
}

/* Convert items of interest from a string to a range of elements or frames */

static int cbf_convertioi(const char *ioi, const size_t maxitems,
                   size_t * itemlow, size_t * itemhigh) {
    char * endptr;
    const char * str;
    if (!itemlow ) return CBF_ARGUMENT;
    *itemlow = 0;
    if (itemhigh) *itemhigh = maxitems-1;
    if (!ioi) {
        return CBF_SUCCESS;
    }
    str = ioi;
    *itemlow = (int)strtol(str,&endptr,0);
    if (*itemlow > maxitems-1) *itemlow = maxitems-1;
    if (*endptr == '\0') {
        if (itemhigh) *itemhigh = *itemlow;
        return CBF_SUCCESS;
    }
    if (*endptr != ',' && *endptr != ' ') return CBF_FORMAT;
    if (!itemhigh) return CBF_SUCCESS;
    
    str = endptr+1;
    *itemhigh = (int)strtol(str,&endptr,0);
    if (*itemhigh < *itemlow) *itemhigh = *itemlow;
    if (*itemhigh > maxitems-1) *itemhigh = maxitems-1;
    if (*endptr == '\0') return CBF_SUCCESS;
    if (*endptr != ',' && *endptr != ' ') return CBF_FORMAT;
    return CBF_SUCCESS;
}


void set_MP_terms(int crterm, int nlterm);

int main (int argc, char *argv [])
{
    FILE *in, *out=NULL, *update=NULL, *file, *dict;
    cbf_h5handle h5in, h5out;
    clock_t a,b;
    cbf_handle cif = NULL;
    cbf_handle ucif = NULL;
    cbf_handle cbf = NULL;
    cbf_handle altcbf = NULL;
    cbf_handle cbfsave = NULL;
    cbf_handle dic = NULL;
    cbf_handle odic;
    cbf_getopt_handle opts;
    unsigned long int hdf5mode = 0L;
    unsigned long int hdf5noH5 = 0L;
    unsigned long int hdf5register = 0;
    int devnull = 0;
    int c;
    int errflg = 0;
    int opaquemode = 0;
    int nxpdbmode = 0;
    int add_minicbf_header = 0;
    int minicbf = 0;
    int data_last = 0;
    const char *cifin, *cbfout, *updatecif;
    const char *dictionary[NUMDICTS];
    int dqrflags[NUMDICTS];
    char *ciftmp=NULL;
#ifndef NOMKSTEMP
    int ciftmpfd;
#endif
    int ciftmpused;
    int padflag;
    int dimflag;
    size_t nbytes;
    int ndict = 0;
    int kd;
    int wide = 0;
    int Wide = 0;
    int IorR = 0;
    int i5;
    int nelsize;
    int testconstruct;
    char buf[C2CBUFSIZ];
    unsigned int blocks, categories, blocknum, catnum, blockitems, itemnum;
    unsigned int elements;
    size_t elementlow, elementhigh;
    size_t framelow, framehigh;
    CBF_NODETYPE itemtype;
    const char *datablock_name;
    const char *saveframe_name;
    const char *category_name;
    const char *column_name;
    const char *value;
    const char *eoi;  /* elements of interest */
    const char *foi;  /* frames of interest */
    const char *roi;  /* region of interest */
    unsigned int colnum, rownum;
    unsigned int columns;
    unsigned int rows;
    int add_update_data=0;
    int subtract_update_data=0;
    int merge_datablocks_by_number=-1;
    double cliphigh, cliplow;

    int mime, digest, encoding, compression, h5compression, bytedir, cbforcif, term;
    int qrflags, qwflags;

    const char * optarg;


    /* Extract options */

    /**********************************************************************
     *  cif2cbf [-i input_cif] [-o output_cbf] \                          *
     *    [-u update_cif] \                                               *
     *    [-b {b[ackwards]|f[orwards]}] \                                 *
     *    [-B {read|liberal|noread}] [-B {write|nowrite}] \               *
     *    [-c {p[acked]|c[annonical]|{b[yte_offset]}|\                    *
     *        {v[2packed]}|{f[latpacked]}|{I|nIbble_offset}| \            *
     *        {z[lib]}|{L[Z4]}|{2|[LZ4**2}|{BS|BSLZ4}|{|n[n[one]}] \      *
     *    [-C highclipvalue] \                                            *
     *    [-d {d[igest]|n[odigest]|w[arndigest]} \                        *
     *    [-D ] \                                                         *
     *    [-e {b[ase64]|k|q[uoted-printable]| \                           *
     *                  d[ecimal]|h[exadecimal]|o[ctal]|n[one]}] \        *
     *    [-I {0|2|4|8}]  \                                               *
     *    [-L lowclipvalue ] \                                            *
     *    [-m {h[eaders]|noh[eaders]}] \                                  *
     *    [-m {d[imensions]|nod[imensions]] \                             *
     *    [-R {0|4|8}] \                                                  *
     *    [-S {read|noread}] [-S {write|nowrite}] \                       *
     *    [-T {read|noread}] [-T {write|nowrite}] \                       *
     *    [-p {0|1|2|4}] \                                                *
     *    [-v dictionary]* [-w] [-W] \                                    *
     *    [-5 {r|w|rw|rn|wn|rwn|n[oH5]} \                                 *
     *    [--nxpdb] \                                                     *
     *    [-O] \                                                          *
     *    [--register {manual|plugin}] \                                  *
     *    [--add-minicbf-header] \                                        *
     *    [--minicbf] \                                                   *
     *    [--data-last] \                                                 *
     *    [--{eoi|elements-of-interest} element[,elementhigh] \           *
     *    [--{foi|frames-of-interest}  frame[,framehigh] \                *
     *    [--{roi|region-of-interest}  fastlow,fasthigh,.... \            *
     *    [--add-update-data] \                                           *
     *    [--subtract-update-data] \                                      *
     *    [--merge-datablocks-by-number \                                 *
     *    [--merge-datablocks-by-name \                                  *
     *    [-U n] \                                                        *
     *    [input_cif] [output_cbf]                                        *
     *                                                                    *
     **********************************************************************/

    mime = 0;
    digest = 0;
    encoding = 0;
    compression = 0;
    h5compression = 0;
    bytedir = 0;
    ndict = 0;
    padflag = 0;
    qrflags = qwflags = 0;
    dimflag = 0;
    nelsize = 0;
    hdf5mode = 0;
    hdf5noH5 = 0;
    hdf5register = 0;
    unsigned int elno = 0;


    cifin = NULL;
    cbfout = NULL;
    cbfsave = NULL;
    updatecif = NULL;
    eoi = NULL;
    foi = NULL;
    roi = NULL;
    ciftmpused = 0;
    testconstruct = 0;
    cliphigh = cliplow = 0.;
    add_update_data = 0;
    subtract_update_data = 0;

    cbf_failnez(cbf_make_getopt_handle(&opts))

    cbf_failnez(cbf_getopt_parse(opts, argc, argv, "-i(input):" \
                                 "o(output):u(update):" \
                                 "b(byte-direction):B(parse-brackets):" \
                                 "c(compression):C(cliphigh):" \
                                 "D(test-construct-detector)d(digest):" \
                                 "e(encoding):I(integer):"  \
                                 "L(cliplow):m(mime-header):" \
                                 "p(pad):P(parse-level):R(real):" \
                                 "S(white-space):T(treble-quotes):" \
                                 "v(validation-dictionary):5(hdf5):" \
                                 "Z(register):U(construct-detector):" \
                                 "O(opaque)" \
                                 "w(read-wide)W(write-wide)" \
                                 "\001(add-minicbf-header)" \
                                 "\002(minicbf)" \
                                 "\003(data-last)" \
                                 "\004(elements-of-interest):\004(eoi):" \
                                 "\005(frames-of-interest):\005(foi):" \
                                 "\006(region-of-interest):\006(roi):" \
                                 "\007(help)" \
                                 "\010(add-update-data)" \
                                 "\011(subtract-update-data)" \
                                 "\012(merge-datablocks-by-number)" \
                                 "\013(merge-datablocks-by-name)" \
                                 "\014(nxpdb)" \
                                 "\015(add-minicbf-header)" \
                                 "\016(minicbf)" \
                                 "\017(data-last)" \
                                 "\020(help)" \
                                 "\021(add-update-data)" \
                                 "\022(subtract-update-data)" \
                                 "\023(merge-datablocks-by-number)" \
                                 "\024(merge-datablocks-by-name)"
                                 ));

    if (!cbf_rewind_getopt_option(opts))
        for(;!cbf_get_getopt_data(opts,&c,NULL,NULL,&optarg);cbf_next_getopt_option(opts)) {
            if (!c) break;
            switch (c) {
                case 'i':     /* input file */
                    if (cifin) errflg++;
                    else cifin = optarg;
                    break;
                    
                case 'o':     /* output file */
                    if (cbfout) errflg++;
                    else {
                        cbfout = optarg;
                    }
                    break;
                    
                case 'u':     /* update file */
                    if (updatecif) errflg++;
                    else updatecif = optarg;
                    break;
                    
                case 'b':     /* byte order */
                    if (bytedir) errflg++;
                    if (optarg[0] == 'f' || optarg[0] == 'F') {
                        bytedir = ENC_FORWARD;
                    } else {
                        if (optarg[0] == 'b' || optarg[0] == 'B' ) {
                            bytedir = ENC_BACKWARD;
                        } else {
                            errflg++;
                        }
                    }
                    break;
                    
                case 'B':
                    if (!strcmp(optarg,"cif20read")) {
                        qrflags &= ~CBF_PARSE_BRACKETS;
                        qrflags |= CBF_PARSE_BRC;
                    } else if (!strcmp(optarg,"nocif20read")) {
                        qrflags &= ~CBF_PARSE_BRACKETS;
                    } else if (!strcmp(optarg,"cif20write")) {
                        qwflags &= ~CBF_PARSE_BRACKETS;
                        qwflags |= CBF_PARSE_BRC;
                    } else if (!strcmp(optarg,"nocif20write")) {
                        qwflags &= ~CBF_PARSE_BRACKETS;
                    } else if (!strcmp(optarg,"read")) {
                        qrflags |= CBF_PARSE_BRACKETS;
                    } else if (!strcmp(optarg,"noread")) {
                        qrflags &= ~CBF_PARSE_BRACKETS;
                    } else if (!strcmp(optarg,"write")) {
                        qwflags |= CBF_PARSE_BRACKETS;
                    } else if (!strcmp(optarg,"nowrite")) {
                        qwflags  &= ~CBF_PARSE_BRACKETS;
                    } else errflg++;
                    break;
                    
                    
                case 'c':
                    if (compression) errflg++;
                    if (optarg[0] == 'p' || optarg[0] == 'P') {
                        h5compression = compression = CBF_PACKED;
                        h5compression |= CBF_H5COMPRESSION_CBF;
                    } else {
                        if (optarg[0] == 'c' || optarg[0] == 'C') {
                            h5compression = compression = CBF_CANONICAL;
                            h5compression |= CBF_H5COMPRESSION_CBF;
                        } else {
                            if (optarg[0] == 'b' || optarg[0] == 'B') {
                                h5compression = compression = CBF_BYTE_OFFSET;
                                h5compression |= CBF_H5COMPRESSION_CBF;
                            } else {
                                if (optarg[0] == 'n' || optarg[0] == 'N') {
                                    h5compression = compression = CBF_NONE;
                                } else {
                                    if (optarg[0] == 'v' || optarg[0] == 'V') {
                                        h5compression = compression = CBF_PACKED_V2;
                                        h5compression |= CBF_H5COMPRESSION_CBF;
                                    } else {
                                        if (optarg[0] == 'f' || optarg[0] == 'F') {
                                            h5compression = compression = CBF_PACKED|CBF_FLAT_IMAGE;
                                            h5compression |= CBF_H5COMPRESSION_CBF;
                                        } else {
                                            if (optarg[0] == 'i' || optarg[0] == 'I' || !cbf_cistrcmp(optarg,"nibble_offset")) {
                                                h5compression = compression = CBF_NIBBLE_OFFSET;
                                                h5compression |= CBF_H5COMPRESSION_CBF;
                                            } else {
                                                if (optarg[0] == 'z' || optarg[0] == 'Z') {
                                                    h5compression = CBF_H5COMPRESSION_ZLIB;
                                                    compression = CBF_NIBBLE_OFFSET;
                                                } else {
                                                    if (optarg[0] == 'l' || optarg[0] == 'L'){
                                                        h5compression = CBF_H5COMPRESSION_LZ4;
                                                        compression = CBF_NIBBLE_OFFSET;
                                                    } else {
                                                        if (optarg[0] == '2' || !cbf_cistrcmp(optarg,"LZ4**2")){
                                                            h5compression = CBF_H5COMPRESSION_LZ4_2;
                                                            compression = CBF_NIBBLE_OFFSET;
                                                        } else {
                                                            if ((optarg[0] == 'B' && optarg[1] == 'S') 
                                                                || !cbf_cistrcmp(optarg,"BSLZ4")){
                                                                h5compression = CBF_H5COMPRESSION_BSLZ4;
                                                                compression = CBF_NIBBLE_OFFSET;
                                                            } else {
                                                                errflg++;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                    
                case 'C':
                    cliphigh = atof(optarg);
                    break;
                    
                case 'd':
                    if (digest) errflg++;
                    if (optarg[0] == 'd' || optarg[0] == 'H' ) {
                        digest = MSG_DIGEST;
                    } else {
                        if (optarg[0] == 'n' || optarg[0] == 'N' ) {
                            digest = MSG_NODIGEST;
                        } else {
                            if (optarg[0] == 'w' || optarg[0] == 'W' ) {
                                digest = MSG_DIGESTWARN;
                            } else {
                                errflg++;
                            }
                        }
                    }
                    break;
                    
                case 'D':  /* test construct_detector */
                    if (testconstruct) errflg++;
                    else testconstruct = 1;
                    elno = 0;
                    break;
                    
                case 'U': /* test construct detector on element number n */
                    if (testconstruct) errflg++;
                    else testconstruct = 1;
                    elno = atoi(optarg);
                    break;
                    
                    
                case '5': /* set hdf5 flags */
                    if (hdf5mode || hdf5noH5){
                        errflg++;
                    } else {
                        i5 = 0;
                        if (optarg[i5] == 'r' || optarg[i5] == 'R') {
                            hdf5mode |= HDF5_READ_MODE;
                            i5++;
                        }
                        if (optarg[i5] == 'w' || optarg[i5] == 'W') {
                            hdf5mode |= HDF5_WRITE_MODE;
                            i5++;
                        }
                        if (optarg[i5] == 'n' || optarg[i5] == 'N') {
                            hdf5noH5 = CBF_H5_NOH5;
                        }
                        if (!hdf5mode && !hdf5noH5){
                            errflg++;
                        }
                    }
                    break;
                    
                case 'Z': /* register compressions */
                    if (hdf5register) errflg++;
                    if (cbf_cistrcmp(optarg,"manual")== 0) {
                        hdf5register = CBF_H5_REGISTER_COMPRESSIONS;
                    } else if (cbf_cistrcmp(optarg,"plugin") == 0) {
                        hdf5register = 0;
                    } else {
                        errflg++;
                    }
                    break;
                    
                case '\001': /* add minicbf header */
                    if (add_minicbf_header) errflg++;
                    add_minicbf_header = 1;
                    break;
                    
                case '\002': /* minicbf output only */
                    if (minicbf) errflg++;
                    minicbf = 1;
                    add_minicbf_header = 1;
                    data_last = 1;
                    break;
                    
                case '\003': /* place data last */
                    if (data_last) errflg++;
                    data_last = 1;
                    break;
                    
                case '\004': /* elements of interest */
                    if (eoi) errflg++;
                    eoi = optarg;
                    break;
                    
                case '\005': /* frames of interest */
                    if (foi) errflg++;
                    foi = optarg;
                    break;
                    
                case '\006': /* region of interest */
                    if (roi) errflg++;
                    roi = optarg;
                    break;
                    
                case '\007': /* help */
                    errflg++;
                    break;
                    
                case '\010': /* add-update-data */
                    if (add_update_data || subtract_update_data) errflg++;
                    add_update_data = 1;
                    break;
                    
                case '\011': /* subtract-update-data */
                    if (add_update_data || subtract_update_data) errflg++;
                    subtract_update_data = 1;
                    break;
                    
                case '\012': /* merge-datablocks-by-number */
                    if (merge_datablocks_by_number >= 0) errflg++;
                    merge_datablocks_by_number = 1;
                    break;
                    
                case '\013': /* merge-datablocks-by-name */
                    if (merge_datablocks_by_number >= 0) errflg++;
                    merge_datablocks_by_number = 0;
                    break;

                case '\014': /* nxpdb */
                    if (nxpdbmode) errflg++;
                    nxpdbmode = 1;
                    break;

                case '\015': /* add minicbf header */
                    if (add_minicbf_header) errflg++;
                    if (!hdf5mode)add_minicbf_header = 1;
                    break;
                    
                case '\016': /* minicbf output only */
                    if (minicbf) errflg++;
                    minicbf = 1;
                    add_minicbf_header = 1;
                    data_last = 1;
                    break;
                     
                case '\017':  /* place data last */
                    if (data_last) errflg++;
                    data_last = 1;
                    break;

                    
                case '\020': /* help */
                    errflg++;
                    break;
                    
                case '\021': /* add update data */
                    if (add_update_data || subtract_update_data) errflg++;
                    add_update_data = 1;
                    break;

                    
                case '\022': /* subtract update data */
                    if (add_update_data || subtract_update_data) errflg++;
                    subtract_update_data = 1;
                    break;

                    
                case '\023': /* merge-datablocks-by-number */
                    if (merge_datablocks_by_number >= 0) errflg++;
                    merge_datablocks_by_number = 1;
                    break;

                case '\024': /* merge-datablocks-by-name */ 
                    if (merge_datablocks_by_number >= 0) errflg++;
                    merge_datablocks_by_number = 0;
                    break;

                    
                case 'O': /* set Opaque mode */
                    if (opaquemode) errflg++;
                    opaquemode = 1;
                    break;
                    
                case 'e':
                    if (encoding) errflg++;
                    if (optarg[0] == 'b' || optarg[0] == 'B' ) {
                        encoding = ENC_BASE64;
                    } else {
                        if (optarg[0] == 'k' || optarg[0] == 'K' ) {
                            encoding = ENC_BASE32K;
                        } else {
                            if (optarg[0] == 'q' || optarg[0] == 'Q' ) {
                                encoding = ENC_QP;
                            } else {
                                if (optarg[0] == 'd' || optarg[0] == 'D' ) {
                                    encoding = ENC_BASE10;
                                } else {
                                    if (optarg[0] == 'h' || optarg[0] == 'H' ) {
                                        encoding = ENC_BASE16;
                                    } else {
                                        if (optarg[0] == 'o' || optarg[0] == 'O' ) {
                                            encoding = ENC_BASE8;
                                        } else {
                                            if (optarg[0] == 'n' || optarg[0] == 'N' ) {
                                                encoding = ENC_NONE;
                                            } else {
                                                errflg++;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                    
                case 'I':
                    if (IorR) errflg++;
                    IorR = CBF_CPY_SETINTEGER;
                    nelsize = atoi(optarg);
                    if (nelsize != 0 && nelsize != 1 && nelsize !=2 && nelsize !=4 && nelsize != 8) errflg++;
                    break;
                    
                case 'L':
                    cliplow = atof(optarg);
                    break;
                    
                    
                case 'm':
                    if (optarg[0] == 'h' || optarg[0] == 'H' ) {
                        if (mime) errflg++;
                        mime = MIME_HEADERS;
                    } else if (optarg[0] == 'd' || optarg[0] == 'D' ) {
                        if (dimflag) errflg++;
                        dimflag = HDR_FINDDIMS;
                    } else if (optarg[0] == 'n' || optarg[0] == 'N' ) {
                        if (!strncasecmp(optarg,"noh",3) ){
                            if (mime) errflg++;
                            mime = PLAIN_HEADERS;
                        } else if (!strncasecmp(optarg,"nod",3)) {
                            if (dimflag) errflg++;
                            dimflag = HDR_NOFINDDIMS;
                        } else {
                            errflg++;
                        }
                    } else {
                        errflg++;
                    }
                    break;
                    
                    
                case 'p':
                    if (padflag) errflg++;
                    if (optarg[0] == '1') {
                        padflag = PAD_1K;
                    } else if (optarg[0] == '2'){
                        padflag = PAD_2K;
                    } else if (optarg[0] == '4'){
                        padflag = PAD_4K;
                    } else errflg++;
                    break;
                    
                    
                case 'P':    /* Parse level */
                    if (!strcmp(optarg,"cif20read")) {
                        qrflags &= ~(CBF_PARSE_BRACKETS|CBF_PARSE_TQ|CBF_PARSE_CIF2_DELIMS|CBF_PARSE_WIDE|CBF_PARSE_UTF8);
                        qrflags |= CBF_PARSE_BRC|CBF_PARSE_TQ|CBF_PARSE_CIF2_DELIMS|CBF_PARSE_WIDE|CBF_PARSE_UTF8;
                    } else if (!strcmp(optarg,"cif20write")) {
                        qwflags &= ~(CBF_PARSE_BRACKETS|CBF_PARSE_TQ|CBF_PARSE_CIF2_DELIMS|CBF_PARSE_WIDE|CBF_PARSE_UTF8);
                        qwflags |= CBF_PARSE_BRC|CBF_PARSE_TQ|CBF_PARSE_CIF2_DELIMS|CBF_PARSE_WIDE|CBF_PARSE_UTF8;
                    } else if (!strcmp(optarg,"oldddlmread")) {
                        qrflags &= ~(CBF_PARSE_BRACKETS|CBF_PARSE_TQ|CBF_PARSE_CIF2_DELIMS|CBF_PARSE_WIDE|CBF_PARSE_UTF8);
                        qrflags |= CBF_PARSE_BRACKETS|CBF_PARSE_WIDE;
                    } else if (!strcmp(optarg,"oldddlmwrite")) {
                        qwflags &= ~(CBF_PARSE_BRACKETS|CBF_PARSE_TQ|CBF_PARSE_CIF2_DELIMS|CBF_PARSE_WIDE|CBF_PARSE_UTF8);
                        qwflags |= CBF_PARSE_BRACKETS|CBF_PARSE_WIDE;
                    } if (!strcmp(optarg,"cif11read")) {
                        qrflags &= ~(CBF_PARSE_BRACKETS|CBF_PARSE_TQ|CBF_PARSE_CIF2_DELIMS|CBF_PARSE_WIDE|CBF_PARSE_UTF8);
                        qrflags |= CBF_PARSE_WIDE;
                    } else if (!strcmp(optarg,"cif11write")) {
                        qwflags &= ~(CBF_PARSE_BRACKETS|CBF_PARSE_TQ|CBF_PARSE_CIF2_DELIMS|CBF_PARSE_WIDE|CBF_PARSE_UTF8);
                        qwflags |= CBF_PARSE_WIDE;
                    } else if (!strcmp(optarg,"cif10read")) {
                        qrflags &= ~(CBF_PARSE_BRACKETS|CBF_PARSE_TQ|CBF_PARSE_CIF2_DELIMS|CBF_PARSE_WIDE|CBF_PARSE_UTF8);
                    } else if (!strcmp(optarg,"cif10write")) {
                        qwflags &= ~(CBF_PARSE_BRACKETS|CBF_PARSE_TQ|CBF_PARSE_CIF2_DELIMS|CBF_PARSE_WIDE|CBF_PARSE_UTF8);
                    } else errflg++;
                    break;
                    
                case 'R':
                    if (IorR) errflg++;
                    IorR = CBF_CPY_SETREAL;
                    nelsize = atoi(optarg);
                    if (nelsize != 0 && nelsize !=4 && nelsize != 8) errflg++;
                    break;
                    
                case 'S':  /* Parse whitespace */
                    if (!strcmp(optarg,"read")) {
                        qrflags |= CBF_PARSE_WS;
                    } else if (!strcmp(optarg,"noread")) {
                        qrflags &= CBF_PARSE_WS;
                    } else if (!strcmp(optarg,"write")) {
                        qwflags |= CBF_PARSE_WS;
                    } else if (!strcmp(optarg,"nowrite")) {
                        qwflags &= CBF_PARSE_WS;
                    } else errflg++;
                    break;
                    
                case 'T':  /* Parse treble quotes */
                    if (!strcmp(optarg,"read")) {
                        qrflags |= CBF_PARSE_TQ;
                    } else if (!strcmp(optarg,"noread")) {
                        qrflags &= ~CBF_PARSE_TQ;
                    } else if (!strcmp(optarg,"write")) {
                        qwflags |= CBF_PARSE_TQ;
                    } else if (!strcmp(optarg,"nowrite")) {
                        qwflags &= ~CBF_PARSE_TQ;
                    } else errflg++;
                    break;
                    
                case 'v':  /* validate against dictionary */
                    if (ndict < NUMDICTS) {
                        dqrflags[ndict] = qrflags;
                        dictionary[ndict++] = optarg;
                    } else if (ndict == NUMDICTS) {
                        errflg++;
                        ndict++;
                        fprintf(stderr, " Too many dictionaries, increase NUMDICTS");
                    }
                    break;
                    
                case 'w': /* read wide files */
                    if (wide) errflg++;
                    else wide = 1;
                    break;
                    
                case 'W': /* write wide files */
                    if (Wide) errflg++;
                    else Wide = 1;
                    break;
                    
                default:
                    errflg++;
                    break;
            }
        }
    for(;!cbf_get_getopt_data(opts,&c,NULL,NULL,&optarg);cbf_next_getopt_option(opts)) {
        if (!cifin) {
            cifin = optarg;
        } else {
            if (!cbfout) {
                cbfout = optarg;
            } else {
                errflg++;
            }
        }
    }
    if ((roi||!updatecif) && (add_update_data || subtract_update_data)) errflg++;
    if (errflg) {
        fprintf(stderr,"cif2cbf:  Usage: \n");
        fprintf(stderr,
                "  cif2cbf [-i input_cif] [-o output_cbf] \\\n");
        fprintf(stderr,
                "    [-u update_cif] \\\n");
        fprintf(stderr,
                "    [-c {p[acked]|c[annonical]|{b[yte_offset]}|\\\n");
        fprintf(stderr,
                "        {v[2packed]}|{f[latpacked]}|{I|nIbble_offset}|{L|LZ4}| \\\n");
        fprintf(stderr,
                "        {2|LZ4**2}|{BS|BSLZ4}|n[n[one]}] \\\n");
        fprintf(stderr,
                "    [-C highclipvalue] \\\n");
        fprintf(stderr,
                "    [-D ] \\\n");
        fprintf(stderr,
                "    [-I {0|2|4|8}] \\\n");
        fprintf(stderr,
                "    [-R {0|4|8}] \\\n");
        fprintf(stderr,
                "    [-L {0|4|8}] \\\n");
        fprintf(stderr,
                "    [-m {h[eaders]|noh[eaders]}] \\\n");
        fprintf(stderr,
                "    [-m {d[imensions]|nod[imensions}] \\\n");
        fprintf(stderr,
                "    [-d {d[igest]|n[odigest]|w[arndigest]}] \\\n");
        fprintf(stderr,
                "    [-B {read|liberal|noread}] [-B {write|nowrite}] \\\n");
        fprintf(stderr,
                "    [-S {read|noread}] [-S {write|nowrite}] \\\n");
        fprintf(stderr,
                "    [-T {read|noread}] [-T {write|nowrite}] \\\n");
        fprintf(stderr,
                "    [-e {b[ase64]|q[uoted-printable]|\\\n");
        fprintf(stderr,
                "                  d[ecimal]|h[examdecimal|o[ctal]|n[one]}] \\\n");
        fprintf(stderr,
                "    [-b {f[orward]|b[ackwards]}\\\n");
        fprintf(stderr,
                "    [-p {1|2|4}\\\n");
        fprintf(stderr,
                "    [-v dictionary]* [-w] [-W]\\\n");
        fprintf(stderr,
                "    [-5 {r|w|rw|rn|wn|rwn|n[oH5]}]\\\n");
        fprintf(stderr,
                "    [--nxpdb]\\\n");
        fprintf(stderr,
                "    [-O] \\\n");
        fprintf(stderr,
                "    [--register {manual|plugin}] \\\n");
        fprintf(stderr,
                "    [--add-minicbf-header] \\\n");
        fprintf(stderr,
                "    [--minicbf] \\\n");
        fprintf(stderr,
                "    [--data-last] \\\n");
        fprintf(stderr,
                "    [--{eoi|elements-of-interest} element[,elementhigh] \\\n");
        fprintf(stderr,
                "    [--{foi|frames-of-interest}  frame[,framehigh] \\\n");
        fprintf(stderr,
                "    [--{roi|region-of-interest}\n"
                "    fastlow,fasthigh[[,midlow,midhigh[,slowlow,slowhigh]]] \\\n");
        fprintf(stderr,
                "    [--{add-data-array} \\\n");
        fprintf(stderr,
                "    [--{subtract-data-array} \\\n");
        fprintf(stderr,
                "    [-U n] \\\n");
        fprintf(stderr,
                "    [input_cif] [output_cbf] \n\n");
        exit(2);
    }


    /* Set up for CIF of CBF output */

    if (!encoding) {
        encoding = ENC_BASE64;
    }
    cbforcif = CBF;
    term = ENC_CRTERM | ENC_LFTERM;
    if (encoding == ENC_BASE64 || \
        encoding == ENC_BASE32K || \
        encoding == ENC_QP || \
        encoding == ENC_BASE10 || \
        encoding == ENC_BASE16 || \
        encoding == ENC_BASE8) {
        cbforcif = CIF;
        term = ENC_LFTERM;
    }

    /* Set up for headers */

    if (!mime) {
        mime = MIME_HEADERS;
    }
    if (!digest) {
        if (mime == MIME_HEADERS) {
            digest = MSG_DIGEST;
        } else {
            digest = MSG_NODIGEST;
        }
    }

    if (!dimflag) dimflag = HDR_FINDDIMS;


    /* Set up for decimal, hexadecimal or octal output */
    if (!bytedir)
        bytedir = ENC_BACKWARD;

    /* Set up for Compression */

    if (!compression)
        compression = CBF_PACKED;
    if (!h5compression)
        h5compression = CBF_NIBBLE_OFFSET|CBF_H5COMPRESSION_CBF;


    /* Read the cif */

    if (!cifin || strcmp(cifin?cifin:"","-") == 0) {
        ciftmp = (char *)malloc(strlen("/tmp/cif2cbfXXXXXX")+1);
#ifdef NOTMPDIR
        strcpy(ciftmp, "cif2cbfXXXXXX");
#else
        strcpy(ciftmp, "/tmp/cif2cbfXXXXXX");
#endif
#ifdef NOMKSTEMP
        if ((ciftmp = mktemp(ciftmp)) == NULL ) {
            fprintf(stderr,"\n cif2cbf: Can't create temporary file name %s.\n", ciftmp);
            fprintf(stderr,"%s\n",strerror(errno));
            exit(1);
        }
        if ( (file = fopen(ciftmp,"wb+")) == NULL) {
            fprintf(stderr,"Can't open temporary file %s.\n", ciftmp);
            fprintf(stderr,"%s\n",strerror(errno));
            exit(1);
        }
#else
        if ((ciftmpfd = mkstemp(ciftmp)) == -1 ) {
            fprintf(stderr,"\n cif2cbf: Can't create temporary file %s.\n", ciftmp);
            fprintf(stderr,"%s\n",strerror(errno));
            exit(1);
        }
        if ( (file = fdopen(ciftmpfd, "w+")) == NULL) {
            fprintf(stderr,"Can't open temporary file %s.\n", ciftmp);
            fprintf(stderr,"%s\n",strerror(errno));
            exit(1);
        }
#endif
        while ((nbytes = fread(buf, 1, C2CBUFSIZ, stdin))) {
            if(nbytes != fwrite(buf, 1, nbytes, file)) {
                fprintf(stderr,"Failed to write %s.\n", ciftmp);
                exit(1);
            }
        }
        fclose(file);
        cifin = ciftmp;
        ciftmpused = 1;
    }


    if ( cbf_make_handle (&cif) ) {
        fprintf(stderr,"Failed to create handle for input_cif\n");
        exit(1);
    }

    if ( cbf_make_handle (&dic) ) {
        fprintf(stderr,"Failed to create handle for dictionary\n");
        exit(1);
    }


    if ( cbf_make_handle (&cbf) ) {
        fprintf(stderr,"Failed to create handle for output_cbf\n");
        exit(1);
    }

    altcbf = NULL;
    if ( (data_last || minicbf) && cbf_make_handle (&altcbf) ) {
        fprintf(stderr,"Failed to create handle for altcbf\n");
        exit(1);
    }

    if ( cbf_make_handle (&ucif) ) {
        fprintf(stderr,"Failed to create handle for update_cif\n");
        exit(1);
    }


    for (kd=0; kd< ndict; kd++) {

        if (!(dict = fopen (dictionary[kd], "rb")))  {
            fprintf (stderr,"Couldn't open the dictionary %s\n", dictionary[kd]);
            exit (1);
        }
        cbf_failnez(cbf_read_widefile(dic, dict, MSG_DIGEST|dqrflags[kd]))
        cbf_failnez(cbf_convert_dictionary(cif,dic))
        cbf_failnez(cbf_get_dictionary(cif,&odic))
        cbf_failnez(cbf_set_dictionary(cbf,odic))

    }

    a = clock ();

    /* Read the file */
    if (hdf5mode&HDF5_READ_MODE) {
        if (cbf_open_h5handle(&h5in,cifin)) {
            fprintf (stderr,"Couldn't open the input HDF5 file %s\n", cifin);
            exit (1);
        }
    } else {
        if (!(in = fopen (cifin, "rb"))) {
            fprintf (stderr,"Couldn't open the input CIF file %s\n", cifin);
            exit (1);
        }
    }

    if (ciftmpused) {
        if (unlink(ciftmp) != 0 ) {
            fprintf(stderr,"cif2cif:  Can't unlink temporary file %s.\n", ciftmp);
            fprintf(stderr,"%s\n",strerror(errno));
            exit(1);
        }
    }

    if (hdf5mode&HDF5_READ_MODE) {
        cbf_failnez (cbf_read_h5file(cif, h5in, hdf5register|MSG_DIGEST|qrflags|(digest&MSG_DIGESTWARN)))
    } else {
        if (!wide) {
            cbf_failnez (cbf_read_file (cif, in, MSG_DIGEST|qrflags|(digest&MSG_DIGESTWARN)))
        } else {
            cbf_failnez (cbf_read_widefile (cif, in, MSG_DIGEST|qrflags|(digest&MSG_DIGESTWARN)))
        }
    }

    if (hdf5noH5) {

        if (!cbf_find_datablock(cif,"H5")) {

            cbf_failnez(cbf_remove_datablock(cif));

        }

    }

    cbf_failnez (cbf_rewind_datablock(cif))

    cbf_failnez (cbf_count_datablocks(cif, &blocks))

    if (cbf_count_elements(cif,&elements)) {
        elements = elementlow = elementhigh = 0;
    } else {
        elementlow = 0; elementhigh = elements?elements-1:elements;
        if (eoi) {
            cbf_failnez(cbf_convertioi(eoi,elements,&elementlow,&elementhigh));
        }
    }
    
    framelow = 0; framehigh = (int)1.e8;
    if (foi) {
        cbf_failnez(cbf_convertioi(eoi,framehigh+1,&framelow,&framehigh));
    }
    
    cbfsave = cbf;

    for (blocknum = 0; blocknum < blocks;  blocknum++ )
    { /* start of copy loop */


        cbf_failnez (cbf_select_datablock(cif, blocknum))
        cbf_failnez (cbf_datablock_name(cif, &datablock_name))
        cbf_failnez (cbf_force_new_datablock(cbf, datablock_name))
        if (altcbf) {
            cbf_failnez (cbf_force_new_datablock(altcbf, datablock_name))
        }

        if ( !cbf_rewind_blockitem(cif, &itemtype) ) {
            cbf_failnez (cbf_count_blockitems(cif, &blockitems))

            for (itemnum = 0; itemnum < blockitems;  itemnum++) {
                const char* array_id;
                const char* binary_id;
                cbf_select_blockitem(cif, itemnum, &itemtype);
                if (itemtype == CBF_CATEGORY) {
                    cbf_category_name(cif,&category_name);
                    if (altcbf && !cbf_cistrcmp(category_name,"array_data")) {
                        cbf = altcbf;
                    } else {
                        cbf = cbfsave;
                    }
                    cbf_force_new_category(cbf, category_name);
                    cbf_count_rows(cif,&rows);
                    cbf_count_columns(cif,&columns);
                    if (altcbf && !cbf_cistrcmp(category_name,"array_data")) {
                        if (minicbf || add_minicbf_header) {
                            cbf_failnez(cbf_new_column(altcbf,"header_convention"))
                            cbf_failnez(cbf_new_column(altcbf,"header_contents"))
                        }
                        /*  Transfer the column names from cif to altcbf, header
                          first, data last */
                        if ( ! cbf_rewind_column(cif) ) {
                            do {
                                cbf_failnez(cbf_column_name(cif, &column_name));
                                if (cbf_cistrcmp(column_name,"data") ) {
                                    cbf_new_column(altcbf, column_name);
                                }
                            } while ( ! cbf_next_column(cif) );
                            cbf_new_column(altcbf,"data");
                            cbf_rewind_column(cif);
                            cbf_rewind_row(cif);
                        }
                    } else {

                    /*  Transfer the columns names from cif to cbf */
                    if ( ! cbf_rewind_column(cif) ) {
                        do {
                            cbf_failnez(cbf_column_name(cif, &column_name))
                            cbf_failnez(cbf_new_column(cbf, column_name))
                        } while ( ! cbf_next_column(cif) );
                        cbf_rewind_column(cif);
                        cbf_rewind_row(cif);
                    }
                    }
                    /* Transfer the rows from cif to cbf */
                    for (rownum = 0; rownum < rows; rownum++ ) {
                        cbf_failnez (cbf_select_row(cif, rownum))
                        /* See if this is an array_data category row, and if, so,
                         log the array_id, binary_id, frame number and detector
                         element number for this row, if any
                         
                         Skip the row if excluded by the eoi or foi
                         
                         */
                        if (foi || eoi){
                            unsigned int element_number;
                            unsigned int frame_number;

                            if (cbf_find_column(cif,"array_id")
                                || cbf_get_value(cif,&array_id)) array_id = NULL;
                            if (cbf_find_column(cif,"binary_id")
                                || cbf_get_value(cif,&binary_id)) binary_id = NULL;
                            
                            if (elements && foi && !cbf_get_element_number(cif,NULL,array_id,NULL,&element_number)) {
                                if ((element_number %elements) < elementlow
                                    || (element_number %elements) > elementhigh) continue;
                            }
                            
                            if (foi && !cbf_get_frame(cif,array_id,binary_id, NULL, &frame_number)) {
                                if (frame_number < framelow
                                    || frame_number > framehigh) continue;
                            }
                            
                            /* recover the starting position */
                            cbf_failnez (cbf_select_blockitem(cif, itemnum, &itemtype));
                            cbf_failnez (cbf_rewind_column(cif));
                            cbf_failnez (cbf_select_row(cif, rownum));
                        }
                        
                        cbf_failnez (cbf_new_row(cbf))
                        cbf_rewind_column(cif);
                        for (colnum = 0; colnum < columns; colnum++ ) {
                            const char *typeofvalue;

                            cbf_failnez (cbf_select_column(cif, colnum))
                            cbf_failnez (cbf_column_name(cif, &column_name))

                            if ( ! cbf_get_value(cif, &value) ) {
                                if (compression && value && column_name && !cbf_cistrcmp("compression_type",column_name)) {
                                    cbf_failnez (cbf_select_column(cbf, colnum))
                                    switch (compression&CBF_COMPRESSION_MASK) {
                                        case (CBF_NONE):
                                            cbf_failnez (cbf_set_value      (cbf,"none"))
                                            cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                            break;
                                        case (CBF_CANONICAL):
                                            cbf_failnez (cbf_set_value      (cbf,"canonical"))
                                            cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                            break;
                                        case (CBF_PACKED):
                                            cbf_failnez (cbf_set_value      (cbf,"packed"))
                                            cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                            break;
                                        case (CBF_PACKED_V2):
                                            cbf_failnez (cbf_set_value      (cbf,"packed_v2"))
                                            cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                            break;
                                        case (CBF_BYTE_OFFSET):
                                            cbf_failnez (cbf_set_value      (cbf,"byte_offsets"))
                                            cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                            break;
                                        case (CBF_NIBBLE_OFFSET):
                                            cbf_failnez (cbf_set_value      (cbf,"nibble_offset"))
                                            cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                            break;
                                        case (CBF_PREDICTOR):
                                            cbf_failnez (cbf_set_value      (cbf,"predictor"))
                                            cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                            break;
                                        default:
                                            cbf_failnez (cbf_set_value      (cbf,"."))
                                            cbf_failnez (cbf_set_typeofvalue(cbf,"null"))
                                            break;
                                    }
                                    if (compression&CBF_FLAG_MASK) {
                                        if (compression&CBF_UNCORRELATED_SECTIONS) {
                                            cbf_failnez (cbf_require_column   (cbf, "compression_type_flag"))
                                            cbf_failnez (cbf_set_value        (cbf, "uncorrelated_sections"))
                                            cbf_failnez (cbf_set_typeofvalue  (cbf, "word"))
                                        } else if (compression&CBF_FLAT_IMAGE)  {
                                            cbf_failnez (cbf_require_column   (cbf, "compression_type_flag"))
                                            cbf_failnez (cbf_set_value        (cbf, "flat"))
                                            cbf_failnez (cbf_set_typeofvalue  (cbf, "word"))
                                        }
                                    } else {
                                        if (!cbf_find_column(cbf, "compression_type_flag")) {
                                            cbf_failnez (cbf_set_value      (cbf,"."))
                                            cbf_failnez (cbf_set_typeofvalue(cbf,"null"))
                                        }
                                    }
                                } else  if (compression && value && column_name && !cbf_cistrcmp("compression_type_flag",column_name)) {
                                    if (compression&CBF_FLAG_MASK) {
                                        if (compression&CBF_UNCORRELATED_SECTIONS) {
                                            cbf_failnez (cbf_require_column   (cbf, "compression_type_flag"))
                                            cbf_failnez (cbf_set_value        (cbf, "uncorrelated_sections"))
                                            cbf_failnez (cbf_set_typeofvalue  (cbf, "word"))
                                        } else if (compression&CBF_FLAT_IMAGE)  {
                                            cbf_failnez (cbf_require_column   (cbf, "compression_type_flag"))
                                            cbf_failnez (cbf_set_value        (cbf, "flat"))
                                            cbf_failnez (cbf_set_typeofvalue  (cbf, "word"))
                                        }
                                    } else {
                                        if (!cbf_find_column(cbf, "compression_type_flag")) {
                                            cbf_failnez (cbf_set_value      (cbf,"."))
                                            cbf_failnez (cbf_set_typeofvalue(cbf,"null"))
                                        }
                                    }
                                } else {
                                    cbf_failnez (cbf_get_typeofvalue(cif, &typeofvalue))
                                    cbf_failnez (cbf_select_column(cbf, colnum))
                                    cbf_failnez (cbf_set_value(cbf, value))
                                    if (typeofvalue) {
                                        cbf_failnez (cbf_set_typeofvalue(cbf, typeofvalue))
                                    }
                                }
                            } else {

                                int binary_id, elsigned, elunsigned;
                                size_t elements, elsize;
                                int minelement, maxelement;
                                unsigned int cifcompression;
                                int realarray;
                                const char *byteorder;
                                size_t fastdim, middim, slowdim, padding;

                                cbf_failnez(cbf_get_arrayparameters_wdims_fs(cif,
                                                                             &cifcompression,
                                                                             &binary_id,
                                                                             &elsize,
                                                                             &elsigned,
                                                                             &elunsigned,
                                                                             &elements,
                                                                             &minelement,
                                                                             &maxelement,
                                                                             &realarray,
                                                                             &byteorder,
                                                                             &fastdim,
                                                                             &middim,
                                                                             &slowdim,
                                                                             &padding));
                                if (fastdim < 1) fastdim = 1;
                                if (middim < 1) middim = 1;
                                if (slowdim < 1) slowdim = 1;
                                {   cbf_failnez (cbf_select_column(cbf,colnum))
                                    cbf_failnez (
                                                 cbf_copy_value_with_roi(cbf,
                                                                         cif,
                                                                         category_name,
                                                                         column_name,
                                                                         rownum,
                                                                         compression,
                                                                         dimflag,
                                                                         IorR,
                                                                         nelsize?((size_t)nelsize):elsize,
                                                                         realarray?CBF_CPY_SETSIGNED:0,
                                                                         cliplow,
                                                                         cliphigh,
                                                                         roi))

                                    }
                                }
                            }
                        }
                } else {
                    cbf_saveframe_name(cif,&saveframe_name);
                    cbf_force_new_saveframe(cbf, saveframe_name);

                    if ( !cbf_rewind_category(cif) ) {
                        cbf_failnez (cbf_count_categories(cif, &categories))

                        for (catnum = 0; catnum < categories;  catnum++) {
                            cbf_select_category(cif, catnum);
                            cbf_category_name(cif,&category_name);
                            cbf_force_new_category(cbf, category_name);
                            cbf_count_rows(cif,&rows);
                            cbf_count_columns(cif,&columns);

                            /*  Transfer the columns names from cif to cbf */
                            if ( ! cbf_rewind_column(cif) ) {
                                do {
                                    cbf_failnez(cbf_column_name(cif, &column_name))
                                    cbf_failnez(cbf_new_column(cbf, column_name))
                                } while ( ! cbf_next_column(cif) );
                                cbf_rewind_column(cif);
                                cbf_rewind_row(cif);
                            }
                            /* Transfer the rows from cif to cbf */
                            for (rownum = 0; rownum < rows; rownum++ ) {
                                cbf_failnez (cbf_select_row(cif, rownum))
                                cbf_failnez (cbf_new_row(cbf))
                                cbf_rewind_column(cif);
                                for (colnum = 0; colnum < columns; colnum++ ) {
                                    const char *typeofvalue;

                                    cbf_failnez (cbf_select_column(cif, colnum))

                                    if ( ! cbf_get_value(cif, &value) ) {
                                        cbf_failnez (cbf_get_typeofvalue(cif, &typeofvalue))
                                        cbf_failnez (cbf_select_column(cbf, colnum))
                                        cbf_failnez (cbf_set_value(cbf, value))
                                        cbf_failnez (cbf_set_typeofvalue(cbf, typeofvalue))
                                    } else {

                                        void * array;
                                        int binary_id, elsigned, elunsigned;
                                        size_t elements,elements_read, elsize;
                                        int minelement, maxelement;
                                        unsigned int cifcompression;
                                        int realarray;
                                        const char * byteorder;
                                        size_t fastdim, middim, slowdim, padding;

                                        cbf_failnez(cbf_get_arrayparameters_wdims_fs(
                                                                                     cif, &cifcompression,
                                                                                     &binary_id, &elsize, &elsigned, &elunsigned,
                                                                                     &elements, &minelement, &maxelement, &realarray,
                                                                                     &byteorder, &fastdim, &middim, &slowdim, &padding))
                                        if ((array=malloc(elsize*elements))) {
                                            cbf_failnez (cbf_select_column(cbf,colnum))
                                            if (!realarray) {
                                                cbf_failnez (cbf_get_integerarray(
                                                                                  cif, &binary_id, array, elsize, elsigned,
                                                                                  elements, &elements_read))
                                                cbf_failnez(cbf_set_integerarray_wdims_fs(
                                                                                          cbf, compression,
                                                                                          binary_id, array, elsize, elsigned, elements,
                                                                                          byteorder, fastdim, middim, slowdim, padding))
                                            } else  {
                                                cbf_failnez (cbf_get_realarray(
                                                                               cif, &binary_id, array, elsize,
                                                                               elements, &elements_read))
                                                if (dimflag == HDR_FINDDIMS && fastdim==0) {
                                                    cbf_get_arraydimensions(cif,NULL,&fastdim,&middim,&slowdim);
                                                }
                                                cbf_failnez(cbf_set_realarray_wdims_fs(
                                                                                       cbf, compression,
                                                                                       binary_id, array, elsize, elements,
                                                                                       byteorder, fastdim, middim, slowdim, padding))
                                            }
                                            free(array);
                                        } else {
                                            fprintf(stderr,
                                                    "\nFailed to allocate memory %ld bytes",
                                                    (long) elsize*elements);
                                            exit(1);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }

    }

    cbf = cbfsave;
    b = clock ();
    fprintf (stderr,
             " Time to read input_cif: %.3fs\n",
             ((b - a) * 1.0) / CLOCKS_PER_SEC);


    /* Read the update file, if any */
    if (updatecif) {

        if (!(update = fopen (updatecif, "rb"))) {
            fprintf (stderr,"Couldn't open the update CIF file %s\n", updatecif);
            exit (1);
        }

        cbf_failnez (cbf_read_widefile (ucif, update, MSG_DIGEST|qrflags|(digest&MSG_DIGESTWARN)))

        cbf_failnez (cbf_rewind_datablock(ucif))

        cbf_failnez (cbf_count_datablocks(ucif, &blocks))

        for (blocknum = 0; blocknum < blocks;  blocknum++ ) {
            /* start of merge loop */


            cbf_failnez (cbf_select_datablock(ucif, blocknum))
            cbf_failnez (cbf_datablock_name(ucif, &datablock_name))

            /* either this is a new datablock, in which case we copy it
             or it has the same name as an existing datablock, in which
             case we merge it */
            
            if (merge_datablocks_by_number) {
                const char * odatablock_name;
                if (cbf_select_datablock(cbf,blocknum)){
                    cbf_failnez (cbf_force_new_datablock(cbf, datablock_name))
                }
                cbf_failnez (cbf_datablock_name(cbf, &odatablock_name));
                if (cbf_cistrcmp(datablock_name,odatablock_name)) {
                    char ndatablock_name[76];
                    const char * db = datablock_name;
                    const char * odb = odatablock_name;
                    size_t dbsz = strlen(datablock_name);
                    size_t odbsz = strlen(odatablock_name);
                    size_t ndbsz;
                    size_t idb;
                    for (idb = 0; idb < dbsz && idb < odbsz && idb < 69; idb++) {
                        if (toupper(db[idb]) == toupper(odb[idb])) continue;
                        break;
                    }
                    strncpy(ndatablock_name,odatablock_name,69);
                    ndatablock_name[69] = '\0';
                    strcat(ndatablock_name,"_");
                    
                    dbsz = strlen(datablock_name+idb);
                    ndbsz = strlen(ndatablock_name)+dbsz;
                    if (ndbsz <= 75) {
                        strncpy(ndatablock_name+ndbsz-dbsz,db+idb,dbsz);
                        ndatablock_name[ndbsz] = '\0';
                    } else {
                        /* The new length is too long truncate, keeping all of odatablock
                           if it is less then 49 characters long, take the last
                           25 from datablock_name*/
                        if (ndbsz - dbsz < 50) {
                            strncpy(ndatablock_name+ndbsz-dbsz,db+idb-(dbsz-25),25);
                            ndatablock_name[ndbsz-dbsz+25]='\0';
                        } else {
                            ndatablock_name[49] = '_';
                            strncpy(ndatablock_name+50,db+idb-(dbsz-25),25);
                            ndatablock_name[75] = '\0';
                        }
                    }
                    /* Don't report errors in changing this name */
                    cbf_set_datablockname(cbf,ndatablock_name);
                }
            } else {
                cbf_failnez (cbf_require_datablock(cbf, datablock_name))
                if (altcbf) {
                    cbf_failnez (cbf_require_datablock(altcbf, datablock_name))
                }
            }

            if ( !cbf_rewind_blockitem(ucif, &itemtype) ) {
                cbf_failnez (cbf_count_blockitems(ucif, &blockitems))

                for (itemnum = 0; itemnum < blockitems;  itemnum++) {
                    cbf_select_blockitem(ucif, itemnum, &itemtype);
                    if (itemtype == CBF_CATEGORY) {
                        cbf_category_name(ucif,&category_name);
                        if (altcbf && !cbf_cistrcmp(category_name,"array_data")) {
                            cbf = altcbf;
                        } else {
                            cbf = cbfsave;
                        }
                        cbf_require_category(cbf, category_name);
                        cbf_count_rows(ucif,&rows);
                        cbf_count_columns(ucif,&columns);
                        if (altcbf && !cbf_cistrcmp(category_name,"array_data")) {
                            if (minicbf || add_minicbf_header) {
                                cbf_failnez(cbf_require_column(altcbf,"header_convention"))
                                cbf_failnez(cbf_require_column(altcbf,"header_contents"))
                            }
                            /*  Transfer the column names from cif to altcbf, header
                             first, data last */
                            if ( ! cbf_rewind_column(ucif) ) {
                                do {
                                    cbf_failnez(cbf_column_name(ucif, &column_name));
                                    if (cbf_cistrcmp(column_name,"data") ) {
                                        cbf_require_column(altcbf, column_name);
                                    }
                                } while ( ! cbf_next_column(ucif) );
                                cbf_require_column(altcbf,"data");
                                cbf_rewind_column(ucif);
                                cbf_rewind_row(ucif);
                            }
                        } else {
                        /*  Transfer the columns names from ucif to cbf */
                        if ( ! cbf_rewind_column(ucif) ) {
                            do {
                                cbf_failnez(cbf_column_name(ucif, &column_name))
                                cbf_failnez(cbf_require_column(cbf, column_name))
                            } while ( ! cbf_next_column(ucif) );
                            cbf_rewind_column(ucif);
                            cbf_rewind_row(ucif);
                            cbf_rewind_column(cbf);
                            cbf_rewind_row(cbf);
                        }
                        }
                        /* Transfer the rows from ucif to cbf */
                        for (rownum = 0; rownum < rows; rownum++ ) {
                            cbf_failnez (cbf_select_row(ucif, rownum))
                            if (cbf_select_row(cbf,rownum)){
                                cbf_failnez (cbf_new_row(cbf))
                            }
                            cbf_rewind_column(ucif);
                            cbf_rewind_column(cbf);
                            for (colnum = 0; colnum < columns; colnum++ ) {
                                const char *typeofvalue;

                                cbf_failnez (cbf_select_column(ucif, colnum));
                                cbf_failnez (cbf_column_name(ucif, &column_name));
                                cbf_failnez (cbf_find_column(cbf,column_name));

                                if ( ! cbf_get_value(ucif, &value) ) {
                                    if (compression && value && column_name && !cbf_cistrcmp("compression_type",column_name)) {
                                        switch (compression&CBF_COMPRESSION_MASK) {
                                            case (CBF_NONE):
                                                cbf_failnez (cbf_set_value      (cbf,"none"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            case (CBF_CANONICAL):
                                                cbf_failnez (cbf_set_value      (cbf,"canonical"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            case (CBF_PACKED):
                                                cbf_failnez (cbf_set_value      (cbf,"packed"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            case (CBF_PACKED_V2):
                                                cbf_failnez (cbf_set_value      (cbf,"packed_v2"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            case (CBF_BYTE_OFFSET):
                                                cbf_failnez (cbf_set_value      (cbf,"byte_offsets"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            case (CBF_NIBBLE_OFFSET):
                                                cbf_failnez (cbf_set_value      (cbf,"nibble_offset"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            case (CBF_PREDICTOR):
                                                cbf_failnez (cbf_set_value      (cbf,"predictor"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            default:
                                                cbf_failnez (cbf_set_value      (cbf,"."))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"null"))
                                                break;
                                        }
                                        if (compression&CBF_FLAG_MASK) {
                                            if (compression&CBF_UNCORRELATED_SECTIONS) {
                                                cbf_failnez (cbf_require_column   (cbf, "compression_type_flag"))
                                                cbf_failnez (cbf_set_value        (cbf, "uncorrelated_sections"))
                                                cbf_failnez (cbf_set_typeofvalue  (cbf, "word"))
                                            } else if (compression&CBF_FLAT_IMAGE)  {
                                                cbf_failnez (cbf_require_column   (cbf, "compression_type_flag"))
                                                cbf_failnez (cbf_set_value        (cbf, "flat"))
                                                cbf_failnez (cbf_set_typeofvalue  (cbf, "word"))
                                            }
                                        } else {
                                            if (!cbf_find_column(cbf, "compression_type_flag")) {
                                                cbf_failnez (cbf_set_value      (cbf,"."))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"null"))
                                            }
                                        }
                                    } else  if (compression && value && column_name && !cbf_cistrcmp("compression_type_flag",column_name)) {
                                        if (compression&CBF_FLAG_MASK) {
                                            if (compression&CBF_UNCORRELATED_SECTIONS) {
                                                cbf_failnez (cbf_require_column   (cbf, "compression_type_flag"))
                                                cbf_failnez (cbf_set_value        (cbf, "uncorrelated_sections"))
                                                cbf_failnez (cbf_set_typeofvalue  (cbf, "word"))
                                            } else if (compression&CBF_FLAT_IMAGE)  {
                                                cbf_failnez (cbf_require_column   (cbf, "compression_type_flag"))
                                                cbf_failnez (cbf_set_value        (cbf, "flat"))
                                                cbf_failnez (cbf_set_typeofvalue  (cbf, "word"))
                                            }
                                        } else {
                                            if (!cbf_find_column(cbf, "compression_type_flag")) {
                                                cbf_failnez (cbf_set_value      (cbf,"."))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"null"))
                                            }
                                        }
                                    } else {
                                        cbf_failnez (cbf_get_typeofvalue(ucif, &typeofvalue))
                                        cbf_failnez (cbf_find_column(cbf, column_name))
                                        cbf_failnez (cbf_set_value(cbf, value))
                                        cbf_failnez (cbf_set_typeofvalue(cbf, typeofvalue))
                                    }
                                } else {
                                    
                                    void * array;
                                    void * oarray;
                                    int binary_id, elsigned, elunsigned;
                                    size_t elements,elements_read, elsize;
                                    int minelement, maxelement;
                                    unsigned int cifcompression;
                                    int realarray;
                                    const char *byteorder;
                                    size_t fastdim, middim, slowdim, padding;
                                    
                                    int obinary_id, oelsigned, oelunsigned;
                                    size_t oelements,oelements_read, oelsize;
                                    int ominelement, omaxelement;
                                    unsigned int ocifcompression;
                                    int orealarray;
                                    const char *obyteorder;
                                    size_t ofastdim, omiddim, oslowdim, opadding;
                                    
                                    double doval, odoval;
                                    
                                    oarray = NULL;
                                    
                                    cbf_failnez(cbf_get_arrayparameters_wdims_fs(ucif,
                                                                                 &cifcompression,
                                                                                 &binary_id,
                                                                                 &elsize,
                                                                                 &elsigned,
                                                                                 &elunsigned,
                                                                                 &elements,
                                                                                 &minelement,
                                                                                 &maxelement,
                                                                                 &realarray,
                                                                                 &byteorder,
                                                                                 &fastdim,
                                                                                 &middim,
                                                                                 &slowdim,
                                                                                 &padding));
                                    
                                    
                                    if ((add_update_data || subtract_update_data)
                                        && !cbf_require_column(cbf, column_name)
                                        && cbf_get_value(cbf, &value)
                                        && !cbf_get_arrayparameters_wdims_fs(cbf,
                                                                            &ocifcompression,
                                                                            &obinary_id,
                                                                            &oelsize,
                                                                            &oelsigned,
                                                                            &oelunsigned,
                                                                            &oelements,
                                                                            &ominelement,
                                                                            &omaxelement,
                                                                            &orealarray,
                                                                            &obyteorder,
                                                                            &ofastdim,
                                                                            &omiddim,
                                                                            &oslowdim,
                                                                            &opadding)
                                        && binary_id == obinary_id
                                        && elsize == oelsize
                                        && elsigned == oelsigned
                                        && elunsigned == oelunsigned
                                        && realarray == orealarray
                                        && elements == oelements) {
                                        oarray = malloc(oelsize*oelements);
                                        if (!orealarray) {
                                            cbf_failnez (cbf_get_integerarray(cbf,
                                                                              &obinary_id,
                                                                              oarray,
                                                                              oelsize,
                                                                              oelsigned,
                                                                              oelements,
                                                                              &oelements_read));
                                        } else {
                                            cbf_failnez (cbf_get_realarray(cbf,
                                                                           &obinary_id,
                                                                           oarray,
                                                                           oelsize,
                                                                           oelements,
                                                                           &oelements_read));
                                        }
                                        
                                        
                                        
                                    }
                                    
                                    
                                    if ((array=malloc(elsize*elements))) {
                                        cbf_failnez (cbf_find_column(cbf,column_name))
                                        if (!realarray)  {
                                            cbf_failnez (cbf_get_integerarray(ucif,
                                                                              &binary_id,
                                                                              array,
                                                                              elsize,
                                                                              elsigned,
                                                                              elements,
                                                                              &elements_read))
                                            if (dimflag == HDR_FINDDIMS && fastdim==0) {
                                                cbf_get_arraydimensions(ucif,NULL,&fastdim,&middim,&slowdim);
                                            }
                                            if (oarray) {
                                                size_t elno;
                                                if (elsize == sizeof(char)) {
                                                    if (add_update_data) {
                                                        for (elno= 0; elno < elements_read &&elno < oelements_read; elno++) {
                                                            doval = ((char *)array)[elno];
                                                            odoval = ((char *)oarray)[elno];
                                                            if (((char *)array)[elno] != (char)(~0) && ((char *)oarray)[elno] != (char)(~0)) {
                                                                doval += odoval;
                                                                if (cliplow < cliphigh) {
                                                                    if (doval < cliplow) doval = cliplow;
                                                                    if (doval > cliphigh) doval = cliphigh;
                                                                }
                                                                ((char *)array)[elno] = (char) doval;
                                                            }
                                                        }
                                                    } else {
                                                        for (elno= 0; elno < elements_read &&elno < oelements_read; elno++) {
                                                            doval = ((char *)array)[elno];
                                                            odoval = ((char *)oarray)[elno];
                                                            if (((char *)array)[elno] != (char)(~0) && ((char *)oarray)[elno] != (char)(~0)) {
                                                                doval -= odoval;
                                                                if (cliplow < cliphigh) {
                                                                    if (doval < cliplow) doval = cliplow;
                                                                    if (doval > cliphigh) doval = cliphigh;
                                                                }
                                                                ((char *)array)[elno] = (char) doval;
                                                            }
                                                        }
                                                    }
                                                } else if (elsize == sizeof(short int)) {
                                                    if (add_update_data) {
                                                        for (elno= 0; elno < elements_read &&elno < oelements_read; elno++) {
                                                            doval = ((short int *)array)[elno];
                                                            odoval = ((short int *)oarray)[elno];
                                                            if ( ( (((short int *)array)[elno] < -7) ||(((short int *)array)[elno] > -1) )
                                                                &&  ( (((short int *)oarray)[elno] < -7)||(((short int *)oarray)[elno] > -1))) {
                                                                doval += odoval;
                                                                if (cliplow < cliphigh) {
                                                                    if (doval < cliplow) doval = cliplow;
                                                                    if (doval > cliphigh) doval = cliphigh;
                                                                }
                                                                ((short int *)array)[elno] = (short int) doval;
                                                            }
                                                        }
                                                    } else {
                                                        for (elno= 0; elno < elements_read &&elno < oelements_read; elno++) {
                                                            doval = ((short int *)array)[elno];
                                                            odoval = ((short int *)oarray)[elno];
                                                            if ( ( (((short int *)array)[elno] < -7) ||(((short int *)array)[elno] > -1) )
                                                                &&  ( (((short int *)oarray)[elno] < -7)||(((short int *)oarray)[elno] > -1))) {
                                                                doval -= odoval;
                                                                if (cliplow < cliphigh) {
                                                                    if (doval < cliplow) doval = cliplow;
                                                                    if (doval > cliphigh) doval = cliphigh;
                                                                }
                                                                ((short int *)array)[elno] = (short int) doval;
                                                            }
                                                        }
                                                    }
                                                } else if (elsize == sizeof(int)) {
                                                    if (add_update_data) {
                                                        for (elno= 0; elno < elements_read &&elno < oelements_read; elno++) {
                                                            doval = ((int *)array)[elno];
                                                            odoval = ((int *)oarray)[elno];
                                                            if ( ( (((int *)array)[elno] < -7) ||(((int *)array)[elno] > -1) )
                                                                &&  ( (((int *)oarray)[elno] < -7)||(((int *)oarray)[elno] > -1))) {
                                                                doval += odoval;
                                                                if (cliplow < cliphigh) {
                                                                    if (doval < cliplow) doval = cliplow;
                                                                    if (doval > cliphigh) doval = cliphigh;
                                                                }
                                                                ((int *)array)[elno] = (int) doval;
                                                            }
                                                        }
                                                    } else {
                                                        for (elno= 0; elno < elements_read &&elno < oelements_read; elno++) {
                                                            doval = ((int *)array)[elno];
                                                            odoval = ((int *)oarray)[elno];
                                                            if ( ( (((int *)array)[elno] < -7) ||(((int *)array)[elno] > -1) )
                                                                &&  ( (((int *)oarray)[elno] < -7)||(((int *)oarray)[elno] > -1))) {
                                                                doval -= odoval;
                                                                if (cliplow < cliphigh) {
                                                                    if (doval < cliplow) doval = cliplow;
                                                                    if (doval > cliphigh) doval = cliphigh;
                                                                }
                                                                ((int *)array)[elno] = (int) doval;
                                                            }
                                                        }
                                                    }
                                                    
                                                } else if (elsize == sizeof(long int)) {
                                                    if (add_update_data) {
                                                        for (elno= 0; elno < elements_read &&elno < oelements_read; elno++) {
                                                            doval = ((long int *)array)[elno];
                                                            odoval = ((long int *)oarray)[elno];
                                                            if ( ( (((long int *)array)[elno] < -7) ||(((long int *)array)[elno] > -1) )
                                                                &&  ( (((long int *)oarray)[elno] < -7)||(((long int *)oarray)[elno] > -1))) {
                                                                doval += odoval;
                                                                if (cliplow < cliphigh) {
                                                                    if (doval < cliplow) doval = cliplow;
                                                                    if (doval > cliphigh) doval = cliphigh;
                                                                }
                                                                ((int *)array)[elno] = (long int) doval;
                                                            }
                                                        }
                                                    } else {
                                                        for (elno= 0; elno < elements_read &&elno < oelements_read; elno++) {
                                                            doval = ((long int *)array)[elno];
                                                            odoval = ((long int *)oarray)[elno];
                                                            if ( ( (((long int *)array)[elno] < -7) ||(((long int *)array)[elno] > -1) )
                                                                &&  ( (((long int *)oarray)[elno] < -7)||(((long int *)oarray)[elno] > -1))) {
                                                                doval -= odoval;
                                                                if (cliplow < cliphigh) {
                                                                    if (doval < cliplow) doval = cliplow;
                                                                    if (doval > cliphigh) doval = cliphigh;
                                                                }
                                                                ((int *)array)[elno] = (long int) doval;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            cbf_failnez(cbf_set_integerarray_wdims_fs(
                                                                                      cbf, compression,
                                                                                      binary_id, array, elsize, elsigned, elements,
                                                                                      "little_endian", fastdim, middim, slowdim, 0))
                                        } else {
                                            cbf_failnez (cbf_get_realarray(ucif,
                                                                           &binary_id,
                                                                           array,
                                                                           elsize,
                                                                           elements,
                                                                           &elements_read))
                                            if (dimflag == HDR_FINDDIMS && fastdim==0) {
                                                cbf_get_arraydimensions(ucif,NULL,&fastdim,&middim,&slowdim);
                                            }
                                            if (oarray) {
                                                size_t elno;
                                                if (elsize == sizeof(float)) {
                                                    if (add_update_data) {
                                                        for (elno= 0; elno < elements_read &&elno < oelements_read; elno++) {
                                                            doval = ((float *)array)[elno];
                                                            odoval = ((float *)oarray)[elno];
                                                            if (((float *)array)[elno] != (float)(~0) && ((float *)oarray)[elno] != (float)(~0)) {
                                                                doval += odoval;
                                                                if (cliplow < cliphigh) {
                                                                    if (doval < cliplow) doval = cliplow;
                                                                    if (doval > cliphigh) doval = cliphigh;
                                                                }
                                                                ((float *)array)[elno] = (float)doval;
                                                            }
                                                        }
                                                    } else {
                                                        for (elno= 0; elno < elements_read &&elno < oelements_read; elno++) {
                                                            doval = ((float *)array)[elno];
                                                            odoval = ((float *)oarray)[elno];
                                                            if (((float *)array)[elno] != (float)(~0) && ((char *)oarray)[elno] != (char)(~0)) {
                                                                doval -= odoval;
                                                                if (cliplow < cliphigh) {
                                                                    if (doval < cliplow) doval = cliplow;
                                                                    if (doval > cliphigh) doval = cliphigh;
                                                                }
                                                                ((float *)array)[elno] = (float)doval;
                                                            }
                                                        }
                                                    }
                                                } else if (elsize == sizeof(double)) {
                                                    if (add_update_data) {
                                                        for (elno= 0; elno < elements_read &&elno < oelements_read; elno++) {
                                                            doval = ((double *)array)[elno];
                                                            odoval = ((double *)oarray)[elno];
                                                            if (((double *)array)[elno] != (double)(~0) && ((double *)oarray)[elno] != (double)(~0)) {
                                                                doval += odoval;
                                                                if (cliplow < cliphigh) {
                                                                    if (doval < cliplow) doval = cliplow;
                                                                    if (doval > cliphigh) doval = cliphigh;
                                                                }
                                                                ((double *)array)[elno] = doval;
                                                            }
                                                        }
                                                    } else {
                                                        for (elno= 0; elno < elements_read &&elno < oelements_read; elno++) {
                                                            doval = ((double *)array)[elno];
                                                            odoval = ((double *)oarray)[elno];
                                                            if (((double *)array)[elno] != (double)(~0) && ((double *)oarray)[elno] != (double)(~0)) {
                                                                doval -= odoval;
                                                                if (cliplow < cliphigh) {
                                                                    if (doval < cliplow) doval = cliplow;
                                                                    if (doval > cliphigh) doval = cliphigh;
                                                                }
                                                                ((double *)array)[elno] = doval;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            cbf_failnez(cbf_set_realarray_wdims_fs(cbf,
                                                                                   compression,
                                                                                   binary_id,
                                                                                   array,
                                                                                   elsize,
                                                                                   elements,
                                                                                   "little_endian",
                                                                                   fastdim,
                                                                                   middim,
                                                                                   slowdim,
                                                                                   0))
                                        }
                                        free(array);
                                    } else {
                                        fprintf(stderr,
                                                "\nFailed to allocate memory %ld bytes",
                                                (long) elsize*elements);
                                        exit(1);
                                    }
                                }
                            }
                        }
                    } else {
                        cbf_saveframe_name(ucif,&saveframe_name);
                        if (!cbf_find_saveframe(cbf,saveframe_name) ) {
                            cbf_failnez(cbf_force_new_saveframe(cbf, saveframe_name))
                        }

                        if ( !cbf_rewind_category(ucif) ) {
                            cbf_failnez (cbf_count_categories(ucif, &categories))

                            for (catnum = 0; catnum < categories;  catnum++) {
                                cbf_select_category(ucif, catnum);
                                cbf_category_name(ucif,&category_name);
                                cbf_require_category(cbf, category_name);
                                cbf_count_rows(ucif,&rows);
                                cbf_count_columns(ucif,&columns);

                                /*  Transfer the columns names from ucif to cbf */
                                if ( ! cbf_rewind_column(ucif) ) {
                                    do {
                                        cbf_failnez(cbf_column_name(ucif, &column_name))
                                        cbf_failnez(cbf_require_column(cbf, column_name))
                                    } while ( ! cbf_next_column(ucif) );
                                    cbf_rewind_column(ucif);
                                    cbf_rewind_row(ucif);
                                }
                                /* Transfer the rows from ucif to cbf */
                                for (rownum = 0; rownum < rows; rownum++ ) {
                                    cbf_failnez (cbf_select_row(ucif, rownum))
                                    if (cbf_select_row(cbf, rownum)) {
                                        cbf_failnez (cbf_new_row(cbf))
                                    }
                                    cbf_rewind_column(ucif);
                                    for (colnum = 0; colnum < columns; colnum++ ) {
                                        const char *typeofvalue;

                                        cbf_failnez (cbf_select_column(ucif, colnum))
                                        cbf_failnez (cbf_column_name(ucif, &column_name))

                                        if ( ! cbf_get_value(ucif, &value) ) {
                                            cbf_failnez (cbf_get_typeofvalue(ucif, &typeofvalue))
                                            cbf_failnez (cbf_find_column(cbf, column_name))
                                            cbf_failnez (cbf_set_value(cbf, value))
                                            cbf_failnez (cbf_set_typeofvalue(cbf, typeofvalue))
                                        } else {

                                            void * array;
                                            int binary_id, elsigned, elunsigned;
                                            size_t elements,elements_read, elsize;
                                            int minelement, maxelement;
                                            unsigned int cifcompression;
                                            int realarray;
                                            const char * byteorder;
                                            size_t fastdim, middim, slowdim, padding;

                                            cbf_failnez(cbf_get_arrayparameters_wdims_fs(ucif,
                                                                                         &cifcompression,
                                                                                         &binary_id,
                                                                                         &elsize,
                                                                                         &elsigned,
                                                                                         &elunsigned,
                                                                                         &elements,
                                                                                         &minelement,
                                                                                         &maxelement,
                                                                                         &realarray,
                                                                                         &byteorder,
                                                                                         &fastdim,
                                                                                         &middim,
                                                                                         &slowdim,
                                                                                         &padding))
                                            if ((array=malloc(elsize*elements))) {
                                                cbf_failnez (cbf_find_column(cbf,column_name))
                                                if (!realarray) {
                                                    cbf_failnez (cbf_get_integerarray(ucif,
                                                                                      &binary_id,
                                                                                      array,
                                                                                      elsize,
                                                                                      elsigned,
                                                                                      elements,
                                                                                      &elements_read))
                                                    cbf_failnez(cbf_set_integerarray_wdims_fs(cbf,
                                                                                              compression,
                                                                                              binary_id,
                                                                                              array,
                                                                                              elsize,
                                                                                              elsigned,
                                                                                              elements,
                                                                                              byteorder,
                                                                                              fastdim,
                                                                                              middim,
                                                                                              slowdim,
                                                                                              padding))
                                                } else  {
                                                    cbf_failnez (cbf_get_realarray(ucif,
                                                                                   &binary_id,
                                                                                   array,
                                                                                   elsize,
                                                                                   elements,
                                                                                   &elements_read))
                                                    if (dimflag == HDR_FINDDIMS && fastdim==0) {
                                                        cbf_get_arraydimensions(ucif,
                                                                                NULL,
                                                                                &fastdim,
                                                                                &middim,
                                                                                &slowdim);
                                                    }
                                                    cbf_failnez(cbf_set_realarray_wdims_fs(cbf,
                                                                                           compression,
                                                                                           binary_id,
                                                                                           array,
                                                                                           elsize,
                                                                                           elements,
                                                                                           byteorder,
                                                                                           fastdim,
                                                                                           middim,
                                                                                           slowdim,
                                                                                           padding))
                                                }
                                                free(array);
                                            } else {
                                                fprintf(stderr,
                                                        "\nFailed to allocate memory %ld bytes",
                                                        (long) elsize*elements);
                                                exit(1);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }

        if (ucif) {
            cbf_failnez (cbf_free_handle (ucif));
    }

    }
    
    cbf = cbfsave;
    
    if (add_minicbf_header) {
        
        if (minicbf || data_last) {
        
            cbf_failnez(cbf_set_minicbf_header(cbf, altcbf, NULL));
            
        } else {
            
            cbf_failnez(cbf_set_minicbf_header(cbf, cbf, NULL));

    }

    }
    
    if (data_last && !minicbf && altcbf) {
        
        ucif = altcbf;
        
        cbf_failnez (cbf_count_datablocks(ucif, &blocks));
        
        for (blocknum = 0; blocknum < blocks;  blocknum++ ) {
            /* start of merge loop */
            
            
            cbf_failnez (cbf_select_datablock(ucif, blocknum));
            cbf_failnez (cbf_datablock_name(ucif, &datablock_name));
            
            /* either this is a new datablock, in which case we copy it
             or it has the same name as an existing datablock, in which
             case we merge it */
            
            cbf_failnez (cbf_require_datablock(cbf, datablock_name))
            
            if ( !cbf_rewind_blockitem(ucif, &itemtype) ) {
                cbf_failnez (cbf_count_blockitems(ucif, &blockitems))
                
                for (itemnum = 0; itemnum < blockitems;  itemnum++) {
                    cbf_select_blockitem(ucif, itemnum, &itemtype);
                    if (itemtype == CBF_CATEGORY) {
                        cbf_category_name(ucif,&category_name);
                        cbf_require_category(cbf, category_name);
                        cbf_count_rows(ucif,&rows);
                        cbf_count_columns(ucif,&columns);
                        {
                            if (minicbf || add_minicbf_header) {
                                cbf_failnez(cbf_require_column(cbf,"header_convention"))
                                cbf_failnez(cbf_require_column(cbf,"header_contents"))
                            }
                            /*  Transfer the column names from cif to altcbf, header
                             first, data last */
                            if ( ! cbf_rewind_column(ucif) ) {
                                do {
                                    cbf_failnez(cbf_column_name(ucif, &column_name));
                                    if (cbf_cistrcmp(column_name,"data") ) {
                                        cbf_require_column(cbf, column_name);
                                    }
                                } while ( ! cbf_next_column(ucif) );
                                cbf_require_column(cbf,"data");
                                cbf_rewind_column(ucif);
                                cbf_rewind_row(ucif);
                            }
                        }
                        /* Transfer the rows from ucif to cbf */
                        for (rownum = 0; rownum < rows; rownum++ ) {
                            cbf_failnez (cbf_select_row(ucif, rownum))
                            if (cbf_select_row(cbf,rownum)){
                                cbf_failnez (cbf_new_row(cbf))
                            }
                            cbf_rewind_column(ucif);
                            for (colnum = 0; colnum < columns; colnum++ ) {
                                const char *typeofvalue;
                                
                                cbf_failnez (cbf_select_column(ucif, colnum))
                                cbf_failnez (cbf_column_name(ucif, &column_name))
                                
                                if ( ! cbf_get_value(ucif, &value) ) {
                                    if (compression && value && column_name && !cbf_cistrcmp("compression_type",column_name)) {
                                        cbf_failnez (cbf_find_column(cbf, column_name))
                                        switch (compression&CBF_COMPRESSION_MASK) {
                                            case (CBF_NONE):
                                                cbf_failnez (cbf_set_value      (cbf,"none"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            case (CBF_CANONICAL):
                                                cbf_failnez (cbf_set_value      (cbf,"canonical"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            case (CBF_PACKED):
                                                cbf_failnez (cbf_set_value      (cbf,"packed"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            case (CBF_PACKED_V2):
                                                cbf_failnez (cbf_set_value      (cbf,"packed_v2"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            case (CBF_BYTE_OFFSET):
                                                cbf_failnez (cbf_set_value      (cbf,"byte_offsets"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            case (CBF_NIBBLE_OFFSET):
                                                cbf_failnez (cbf_set_value      (cbf,"nibble_offset"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            case (CBF_PREDICTOR):
                                                cbf_failnez (cbf_set_value      (cbf,"predictor"))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"word"))
                                                break;
                                            default:
                                                cbf_failnez (cbf_set_value      (cbf,"."))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"null"))
                                                break;
                                        }
                                        if (compression&CBF_FLAG_MASK) {
                                            if (compression&CBF_UNCORRELATED_SECTIONS) {
                                                cbf_failnez (cbf_require_column   (cbf, "compression_type_flag"))
                                                cbf_failnez (cbf_set_value        (cbf, "uncorrelated_sections"))
                                                cbf_failnez (cbf_set_typeofvalue  (cbf, "word"))
                                            } else if (compression&CBF_FLAT_IMAGE)  {
                                                cbf_failnez (cbf_require_column   (cbf, "compression_type_flag"))
                                                cbf_failnez (cbf_set_value        (cbf, "flat"))
                                                cbf_failnez (cbf_set_typeofvalue  (cbf, "word"))
                                            }
                                        } else {
                                            if (!cbf_find_column(cbf, "compression_type_flag")) {
                                                cbf_failnez (cbf_set_value      (cbf,"."))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"null"))
                                            }
                                        }
                                    } else  if (compression && value && column_name && !cbf_cistrcmp("compression_type_flag",column_name)) {
                                        if (compression&CBF_FLAG_MASK) {
                                            if (compression&CBF_UNCORRELATED_SECTIONS) {
                                                cbf_failnez (cbf_require_column   (cbf, "compression_type_flag"))
                                                cbf_failnez (cbf_set_value        (cbf, "uncorrelated_sections"))
                                                cbf_failnez (cbf_set_typeofvalue  (cbf, "word"))
                                            } else if (compression&CBF_FLAT_IMAGE)  {
                                                cbf_failnez (cbf_require_column   (cbf, "compression_type_flag"))
                                                cbf_failnez (cbf_set_value        (cbf, "flat"))
                                                cbf_failnez (cbf_set_typeofvalue  (cbf, "word"))
                                            }
                                        } else {
                                            if (!cbf_find_column(cbf, "compression_type_flag")) {
                                                cbf_failnez (cbf_set_value      (cbf,"."))
                                                cbf_failnez (cbf_set_typeofvalue(cbf,"null"))
                                            }
                                        }
                                    } else {
                                        cbf_failnez (cbf_get_typeofvalue(ucif, &typeofvalue))
                                        cbf_failnez (cbf_find_column(cbf, column_name))
                                        cbf_failnez (cbf_set_value(cbf, value))
                                        cbf_failnez (cbf_set_typeofvalue(cbf, typeofvalue))
                                    }
                                } else {
                                    
                                    void * array;
                                    int binary_id, elsigned, elunsigned;
                                    size_t elements,elements_read, elsize;
                                    int minelement, maxelement;
                                    unsigned int cifcompression;
                                    int realarray;
                                    const char *byteorder;
                                    size_t fastdim, middim, slowdim, padding;
                                    
                                    cbf_failnez(cbf_get_arrayparameters_wdims_fs(
                                                                                 ucif, &cifcompression,
                                                                                 &binary_id, &elsize, &elsigned, &elunsigned,
                                                                                 &elements, &minelement, &maxelement, &realarray,
                                                                                 &byteorder, &fastdim, &middim, &slowdim, &padding))
                                    if ((array=malloc(elsize*elements))) {
                                        cbf_failnez (cbf_find_column(cbf,column_name))
                                        if (!realarray)  {
                                            cbf_failnez (cbf_get_integerarray(
                                                                              ucif, &binary_id, array, elsize, elsigned,
                                                                              elements, &elements_read))
                                            if (dimflag == HDR_FINDDIMS && fastdim==0) {
                                                cbf_get_arraydimensions(ucif,NULL,&fastdim,&middim,&slowdim);
                                            }
                                            cbf_failnez(cbf_set_integerarray_wdims_fs(
                                                                                      cbf, compression,
                                                                                      binary_id, array, elsize, elsigned, elements,
                                                                                      "little_endian", fastdim, middim, slowdim, 0))
                                        } else {
                                            cbf_failnez (cbf_get_realarray(
                                                                           ucif, &binary_id, array, elsize,
                                                                           elements, &elements_read))
                                            if (dimflag == HDR_FINDDIMS && fastdim==0) {
                                                cbf_get_arraydimensions(ucif,NULL,&fastdim,&middim,&slowdim);
                                            }
                                            cbf_failnez(cbf_set_realarray_wdims_fs(
                                                                                   cbf, compression,
                                                                                   binary_id, array, elsize, elements,
                                                                                   "little_endian", fastdim, middim, slowdim, 0))
                                        }
                                        free(array);
                                    } else {
                                        fprintf(stderr,
                                                "\nFailed to allocate memory %ld bytes",
                                                (long) elsize*elements);
                                        exit(1);
                                    }
                                }
                            }
                        }
                    } else {
                        cbf_saveframe_name(ucif,&saveframe_name);
                        if (!cbf_find_saveframe(cbf,saveframe_name) ) {
                            cbf_failnez(cbf_force_new_saveframe(cbf, saveframe_name))
                        }
                        
                        if ( !cbf_rewind_category(ucif) ) {
                            cbf_failnez (cbf_count_categories(ucif, &categories))
                            
                            for (catnum = 0; catnum < categories;  catnum++) {
                                cbf_select_category(ucif, catnum);
                                cbf_category_name(ucif,&category_name);
                                cbf_require_category(cbf, category_name);
                                cbf_count_rows(ucif,&rows);
                                cbf_count_columns(ucif,&columns);
                                
                                /*  Transfer the columns names from ucif to cbf */
                                if ( ! cbf_rewind_column(ucif) ) {
                                    do {
                                        cbf_failnez(cbf_column_name(ucif, &column_name))
                                        cbf_failnez(cbf_require_column(cbf, column_name))
                                    } while ( ! cbf_next_column(ucif) );
                                    cbf_rewind_column(ucif);
                                    cbf_rewind_row(ucif);
                                }
                                /* Transfer the rows from ucif to cbf */
                                for (rownum = 0; rownum < rows; rownum++ ) {
                                    cbf_failnez (cbf_select_row(ucif, rownum))
                                    if (cbf_select_row(cbf, rownum)) {
                                        cbf_failnez (cbf_new_row(cbf))
                                    }
                                    cbf_rewind_column(ucif);
                                    for (colnum = 0; colnum < columns; colnum++ ) {
                                        const char *typeofvalue;
                                        
                                        cbf_failnez (cbf_select_column(ucif, colnum))
                                        cbf_failnez (cbf_column_name(ucif, &column_name))
                                        
                                        if ( ! cbf_get_value(ucif, &value) ) {
                                            cbf_failnez (cbf_get_typeofvalue(ucif, &typeofvalue))
                                            cbf_failnez (cbf_find_column(cbf, column_name))
                                            cbf_failnez (cbf_set_value(cbf, value))
                                            cbf_failnez (cbf_set_typeofvalue(cbf, typeofvalue))
                                        } else {
                                            
                                            void * array;
                                            int binary_id, elsigned, elunsigned;
                                            size_t elements,elements_read, elsize;
                                            int minelement, maxelement;
                                            unsigned int cifcompression;
                                            int realarray;
                                            const char * byteorder;
                                            size_t fastdim, middim, slowdim, padding;
                                            
                                            cbf_failnez(cbf_get_arrayparameters_wdims_fs(ucif,
                                                                                         &cifcompression,
                                                                                         &binary_id,
                                                                                         &elsize,
                                                                                         &elsigned,
                                                                                         &elunsigned,
                                                                                         &elements,
                                                                                         &minelement,
                                                                                         &maxelement,
                                                                                         &realarray,
                                                                                         &byteorder,
                                                                                         &fastdim,
                                                                                         &middim,
                                                                                         &slowdim,
                                                                                         &padding))
                                            if ((array=malloc(elsize*elements))) {
                                                cbf_failnez (cbf_find_column(cbf,column_name))
                                                if (!realarray) {
                                                    cbf_failnez (cbf_get_integerarray(ucif,
                                                                                      &binary_id,
                                                                                      array,
                                                                                      elsize,
                                                                                      elsigned,
                                                                                      elements,
                                                                                      &elements_read))
                                                    cbf_failnez(cbf_set_integerarray_wdims_fs(cbf,
                                                                                              compression,
                                                                                              binary_id,
                                                                                              array,
                                                                                              elsize,
                                                                                              elsigned,
                                                                                              elements,
                                                                                              byteorder,
                                                                                              fastdim,
                                                                                              middim,
                                                                                              slowdim,
                                                                                              padding))
                                                } else  {
                                                    cbf_failnez (cbf_get_realarray(ucif,
                                                                                   &binary_id,
                                                                                   array,
                                                                                   elsize,
                                                                                   elements,
                                                                                   &elements_read))
                                                    if (dimflag == HDR_FINDDIMS && fastdim==0) {
                                                        cbf_get_arraydimensions(ucif,
                                                                                NULL,
                                                                                &fastdim,
                                                                                &middim,
                                                                                &slowdim);
                                                    }
                                                    cbf_failnez(cbf_set_realarray_wdims_fs(cbf,
                                                                                           compression,
                                                                                           binary_id,
                                                                                           array,
                                                                                           elsize,
                                                                                           elements,
                                                                                           byteorder,
                                                                                           fastdim,
                                                                                           middim,
                                                                                           slowdim,
                                                                                           padding))
                                                }
                                                free(array);
                                            } else {
                                                fprintf(stderr,
                                                        "\nFailed to allocate memory %ld bytes",
                                                        (long) elsize*elements);
                                                exit(1);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                
            }
        }
        
    }
    
    a = clock ();

    if (hdf5mode&HDF5_WRITE_MODE) {

        if(nxpdbmode) { 
            if (cbf_create_h5handle_nxpdb(&h5out,cbfout)) {
                printf (" Couldn't open the HDF5 file %s for writing\n", cbfout);
            }
        } else if (cbf_create_h5handle(&h5out,cbfout)) {
            printf (" Couldn't open the HDF5 file %s for writing\n", cbfout);

        }

    } else {

        out = NULL;
        if ( ! cbfout || strcmp(cbfout?cbfout:"","-") == 0 ) {
            out = stdout;
        } else if ( strcmp(cbfout?cbfout:"","/dev/null") ==0 ){
            devnull=1;
        } else {
            out = fopen (cbfout, "w+b");
        }
        if ( ( devnull == 0 ) &&  ! out ) {
            if (encoding == ENC_NONE) {
                printf (" Couldn't open the CBF file %s\n", cbfout);
            } else {
                printf (" Couldn't open the CIF file %s\n", cbfout);
            }
            exit (1);
        }

    }

    if (testconstruct) {
        void * array;
        const char * array_id;
        const char * array_section_id;
        unsigned int compression;
        int          id;
        size_t       elsize;
        int          elsigned;
        int          elunsigned;
        size_t       nelem;
        int          minelem;
        int          maxelem;
        int          realarray;
        size_t dimslow, dimmid, dimfast;
        cbf_detector detector;
        cbf_failnez(cbf_construct_detector (cbf, &detector, elno))
        cbf_failnez(cbf_get_3d_image_size(cbf,0,elno,&dimslow,&dimmid,&dimfast));
        fprintf(stderr,"dimslow = %ld, dimmid = %ld, dimfast = %ld\n",(long)dimslow,(long)dimmid,(long)dimfast);
        cbf_failnez(cbf_get_array_id(cbf,elno,&array_id));
        cbf_failnez(cbf_get_array_section_id(cbf,elno,&array_section_id));
        cbf_failnez(cbf_get_array_arrayparameters (cbf,
                                                   array_id,
                                                   0,
                                                   &compression,
                                                   &id,
                                                   &elsize,
                                                   &elsigned,
                                                   &elunsigned,
                                                   &nelem,
                                                   &minelem,
                                                   &maxelem,
                                                   &realarray));
        cbf_failnez(cbf_alloc(&array,NULL,1,dimslow*dimmid*dimfast*elsize));
        cbf_failnez(cbf_get_image(cbf,0,elno,array,elsize,elsigned,dimmid,dimfast));
        
        cbf_free_detector (detector);
        
    }


    if (hdf5noH5) {

        if (!cbf_find_datablock(cbf,"H5")) {

            cbf_failnez(cbf_remove_datablock(cbf));

        }
    }


    if ( ! devnull ){
        if (hdf5mode&HDF5_WRITE_MODE) {

            cbf_failnez(cbf_write_h5file (cbf, h5out, hdf5register|h5compression|
                                          (opaquemode?CBF_H5_OPAQUE:0)|
                                          (nxpdbmode?CBF_H5_NXPDB:0)  |
                                          (hdf5noH5?CBF_H5_NOH5:0)      ));

        } else {

            if (minicbf) {
                cbf = altcbf;
            } else {
                cbf = cbfsave;
            }

            if (Wide) {
                cbf_failnez (cbf_write_widefile (cbf, out, 1, cbforcif,
                                                 mime | (digest&(MSG_DIGEST|MSG_DIGESTNOW)) | padflag | qwflags,
                                                 encoding | bytedir | term ));
            } else {
                cbf_failnez (cbf_write_file (cbf, out, 1, cbforcif,
                                             mime | (digest&(MSG_DIGEST|MSG_DIGESTNOW)) | padflag | qwflags,
                                             encoding | bytedir | term ));
            }
        }

    }
    
    cbf = cbfsave;
    
	if (cbf) { 
        cbf_failnez (cbf_free_handle (cbf));
    }
    
	if (dic) {
        cbf_failnez (cbf_free_handle (dic));
    }
    
	if (cif) {
        cbf_failnez (cbf_free_handle (cif));
    }
    
    if (altcbf) {
        cbf_failnez (cbf_free_handle (altcbf));
    }

    b = clock ();
    if (encoding == ENC_NONE) {
        fprintf (stderr, " Time to write the CBF image: %.3fs\n",
                 ((b - a) * 1.0) / CLOCKS_PER_SEC);
    } else {
        if ( ! devnull )
            fprintf (stderr, " Time to write the CIF image: %.3fs\n",
                     ((b - a) * 1.0) / CLOCKS_PER_SEC);
    }

    cbf_failnez (cbf_free_getopt_handle(opts))

    exit(0);

}

int local_exit (int status)
{
    exit(status);
    return 1; /* avoid warnings */
}