File: cfitsio.tex

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

\begin{titlepage}
\normalsize
\vspace*{4.0cm}
\begin{center}
{\Huge \bf CFITSIO User's Reference Guide}\\
\end{center}
\medskip 
\medskip 
\begin{center}
{\LARGE \bf An Interface to FITS Format Files}\\
\end{center}
\begin{center}
{\LARGE \bf for C Programmers}\\
\end{center}
\medskip
\medskip
\begin{center}
{\Large Version 3.4 \\}
\end{center}
\bigskip
\vskip 2.5cm
\begin{center}
{HEASARC\\
Code 662\\
Goddard Space Flight Center\\
Greenbelt, MD 20771\\
USA}
\end{center}

\vfill
\bigskip
\begin{center}
{\Large May 2019\\}
\end{center}
\vfill
\end{titlepage}

\clearpage

\tableofcontents
\chapter{Introduction }
\pagenumbering{arabic}


\section{ A Brief Overview}

CFITSIO is a machine-independent library of routines for reading and
writing data files in the FITS (Flexible Image Transport System) data
format.  It can also read IRAF format image files and raw binary data
arrays by converting them on the fly into a virtual FITS format file.
This library is written in ANSI C and provides a powerful yet simple
interface for accessing FITS files which will run on most commonly used
computers and workstations.  CFITSIO supports all the features
described in the official definition of the FITS format and can
read and write all the currently defined types of extensions, including
ASCII tables (TABLE), Binary tables (BINTABLE) and IMAGE extensions.
The CFITSIO routines insulate the programmer from having to deal with
the complicated formatting details in the FITS file, however, it is
assumed that users have a general knowledge about the structure and
usage of FITS files.

CFITSIO also contains a set of Fortran callable wrapper routines which
allow Fortran programs to call the CFITSIO routines.  See the companion
``FITSIO User's Guide'' for the definition of the Fortran subroutine
calling sequences.  These wrappers replace the older Fortran FITSIO
library which is no longer supported.

The CFITSIO package was initially developed by the HEASARC (High Energy
Astrophysics Science Archive Research Center) at the NASA Goddard Space
Flight Center to convert various existing and newly acquired
astronomical data sets into FITS format and to further analyze data
already in FITS format.  New features continue to be added to CFITSIO
in large part due to contributions of ideas or actual code from
users of the package.  The Integral Science Data Center in Switzerland,
and the XMM/ESTEC project in The Netherlands made especially significant
contributions that resulted in many of the new features that appeared
in v2.0 of CFITSIO.


\section{Sources of FITS Software and Information}

The latest version of the CFITSIO source code,
documentation, and example programs are available on the
Web or via anonymous ftp from:

\begin{verbatim}
        http://heasarc.gsfc.nasa.gov/fitsio
        ftp://legacy.gsfc.nasa.gov/software/fitsio/c
\end{verbatim}

Any questions, bug reports, or suggested enhancements related to the CFITSIO
package should be sent to the FTOOLS Help Desk at the HEASARC:

\begin{verbatim}
        http://heasarc.gsfc.nasa.gov/cgi-bin/ftoolshelp
\end{verbatim}

This User's Guide assumes that readers already have a general
understanding of the definition and structure of FITS format files.
Further information about FITS formats is available from the FITS Support
Office at {\tt http://fits.gsfc.nasa.gov}.  In particular, the
'FITS Standard' gives the authoritative definition of the FITS data
format.  Other documents available at that Web site
provide additional historical background
and practical advice on using FITS files.

The HEASARC also provides a very sophisticated FITS file analysis
program called `Fv' which can be used to display and edit the contents
of any FITS file as well as construct new FITS files from scratch.
Fv is freely available for
most Unix platforms, Mac PCs, and Windows PCs.
CFITSIO users may also be interested in the FTOOLS package of programs
that can be used to manipulate and analyze FITS format files.
Fv and FTOOLS are available from their respective Web sites at:

\begin{verbatim}
        http://fv.gsfc.nasa.gov
        http://heasarc.gsfc.nasa.gov/ftools
\end{verbatim}


\section{Acknowledgments}

The development of the many powerful features in CFITSIO was made
possible through collaborations with many people or organizations from
around the world.  The following in particular have made especially
significant contributions:

Programmers from the Integral Science Data Center, Switzerland (namely,
Jurek Borkowski, Bruce O'Neel, and Don Jennings), designed the concept
for the plug-in I/O drivers that was introduced with CFITSIO 2.0.  The
use of `drivers' greatly simplified  the low-level I/O, which in turn
made other new features in CFITSIO (e.g., support for compressed FITS
files and support for IRAF format image files) much easier to
implement.  Jurek Borkowski wrote the Shared Memory driver, and Bruce
O'Neel wrote the drivers for accessing FITS files over the network
using the FTP, HTTP, and ROOT protocols.  Also, in 2009, Bruce O'Neel
was the key developer of the thread-safe version of CFITSIO.

The ISDC also provided the template parsing routines (written by Jurek
Borkowski) and the hierarchical grouping routines (written by Don
Jennings).  The ISDC DAL (Data Access Layer) routines are layered on
top of CFITSIO and make extensive use of these features.

Giuliano Taffoni and Andrea Barisani, at INAF, University of Trieste,
Italy, implemented the I/O driver routines for accessing FITS files
on the computational grids using the gridftp protocol.

Uwe Lammers (XMM/ESA/ESTEC, The Netherlands) designed the
high-performance lexical parsing algorithm that is used to do
on-the-fly filtering of FITS tables.  This algorithm essentially
pre-compiles the user-supplied selection expression into a form that
can be rapidly evaluated for each row.  Peter Wilson (RSTX, NASA/GSFC)
then wrote the parsing routines used by CFITSIO based on Lammers'
design, combined with other techniques such as the CFITSIO iterator
routine to further enhance the data processing throughput.  This effort
also benefited from a much earlier lexical parsing routine that was
developed by Kent Blackburn (NASA/GSFC). More recently, Craig Markwardt
(NASA/GSFC) implemented additional functions (median, average, stddev)
and other enhancements to the lexical parser.

The CFITSIO iterator function is loosely based on similar ideas
developed for the XMM Data Access Layer.

Peter Wilson (RSTX, NASA/GSFC) wrote the complete set of
Fortran-callable wrappers for all the CFITSIO routines, which in turn
rely on the CFORTRAN macro developed by Burkhard Burow.

The syntax used by CFITSIO for filtering or binning input FITS files is
based on ideas developed for the AXAF Science Center Data Model by
Jonathan McDowell, Antonella Fruscione, Aneta Siemiginowska and Bill
Joye. See http://heasarc.gsfc.nasa.gov/docs/journal/axaf7.html for
further description of the AXAF Data Model.

The file decompression code were taken directly from the gzip (GNU zip)
program developed by Jean-loup Gailly and others.

The new compressed image data format (where the image is tiled and
the compressed byte stream from each tile is stored in a binary table)
was implemented in collaboration with Richard White (STScI), Perry
Greenfield (STScI) and Doug Tody (NOAO).

Doug Mink (SAO) provided the routines for converting IRAF format
images into FITS format.

Martin Reinecke (Max Planck Institute, Garching)) provided the modifications to
cfortran.h that are necessary to support 64-bit integer values when calling
C routines from fortran programs.  The cfortran.h macros were originally developed
by Burkhard Burow (CERN).

Julian Taylor (ESO, Garching) provided the fast byte-swapping algorithms
that use the SSE2 and SSSE3 machine instructions available on x86\_64 CPUs.

In addition, many other people have made valuable contributions to the
development of CFITSIO.  These include (with apologies to others that may
have inadvertently been omitted):

Steve Allen, Carl Akerlof, Keith Arnaud, Morten Krabbe Barfoed, Kent
Blackburn, G Bodammer, Romke Bontekoe, Lucio Chiappetti, Keith Costorf,
Robin Corbet, John Davis,  Richard Fink, Ning Gan, Emily Greene, Gretchen
Green, Joe Harrington, Cheng Ho, Phil Hodge, Jim Ingham, Yoshitaka
Ishisaki, Diab Jerius, Mark Levine, Todd Karakaskian, Edward King,
Scott Koch,  Claire Larkin, Rob Managan, Eric Mandel, Richard Mathar,
John Mattox, Carsten Meyer, Emi Miyata, Stefan Mochnacki, Mike Noble,
Oliver Oberdorf, Clive Page, Arvind Parmar, Jeff Pedelty, Tim Pearson,
Philippe Prugniel, Maren Purves, Scott Randall, Chris Rogers, Arnold Rots,
Rob Seaman, Barry Schlesinger, Robin Stebbins, Andrew Szymkowiak, Allyn Tennant,
Peter Teuben, James Theiler, Doug Tody, Shiro Ueno, Steve Walton, Archie
Warnock, Alan Watson, Dan Whipple, Wim Wimmers, Peter Young, Jianjun Xu,
and Nelson Zarate.


\section{Legal Stuff}

Copyright (Unpublished--all rights reserved under the copyright laws of
the United States), U.S. Government as represented by the Administrator
of the National Aeronautics and Space Administration.  No copyright is
claimed in the United States under Title 17, U.S. Code.

Permission to freely use, copy, modify, and distribute this software
and its documentation without fee is hereby granted, provided that this
copyright notice and disclaimer of warranty appears in all copies.

DISCLAIMER:

THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND,
EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO,
ANY WARRANTY THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY
IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, AND FREEDOM FROM INFRINGEMENT, AND ANY WARRANTY THAT THE
DOCUMENTATION WILL CONFORM TO THE SOFTWARE, OR ANY WARRANTY THAT THE
SOFTWARE WILL BE ERROR FREE.  IN NO EVENT SHALL NASA BE LIABLE FOR ANY
DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, SPECIAL OR
CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, OR IN ANY WAY
CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
CONTRACT, TORT , OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY
PERSONS OR PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED
FROM, OR AROSE OUT OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR
SERVICES PROVIDED HEREUNDER."

\chapter{ Creating the CFITSIO Library }


\section{Building the Library}

The CFITSIO code is contained in about 40 C source files (*.c) and header
files (*.h). On VAX/VMS systems 2 assembly-code files (vmsieeed.mar and
vmsieeer.mar) are also needed.

CFITSIO is written in ANCI C and should be compatible with most existing C and C++
compilers. Cray supercomputers are currently not supported.


\subsection{Unix Systems}

The CFITSIO library is built on Unix systems by typing:

\begin{verbatim}
 >  ./configure [--prefix=/target/installation/path] [--enable-reentrant]
                [--enable-sse2] [--enable-ssse3]
 >  make          (or  'make shared')
 >  make install  (this step is optional)
\end{verbatim}
at the operating system prompt.  The configure command customizes the
Makefile for the particular system, then the `make' command compiles the
source files and builds the library.  Type `./configure' and not simply
`configure' to ensure that the configure script in the current directory
is run and not some other system-wide configure script.  The optional
'prefix' argument to configure gives the path to the directory where
the CFITSIO library and include files should be installed via the later
'make install' command. For example,

\begin{verbatim}
   > ./configure --prefix=/usr1/local
\end{verbatim}
will cause the 'make install' command to copy the CFITSIO libcfitsio file
to /usr1/local/lib and the necessary include files to /usr1/local/include
(assuming of course that the  process has permission to write to these
directories).

All the available configure options can be seen by entering the command

\begin{verbatim}
   > ./configure --help
\end{verbatim}
Some of the more useful options are described below:

The --enable-reentrant option will attempt to configure CFITSIO
so that it can be used in multi-threaded programs.  See the
"Using CFITSIO in Multi-threaded Environments" section, below, for more details.

The --enable-sse2 and --enable-ssse3 options will cause configure to
attempt to build CFITSIO using faster byte-swapping algorithms.
See the "Optimizing Programs" chapter of this manual for
more information about these options.

The --with-gsiftp-flavour and --with-gsiftp options enable support for the Globus
Toolkit gsiftp protocal.  See the "Extended File Name Syntax" chapter for more
information.

The --with-bzip2 option enables support for reading FITS files that have been
externally compressed by the bzip2 algorithm.  This requires that the CFITSIO
library, and all applications program that use CFITSIO, to be linked to include
the libbz2 library.

The 'make shared' option builds a shared or dynamic version of the
CFITSIO library.  When using the shared library the executable code is
not copied into your program at link time and instead the program
locates the necessary library code at run time, normally through
LD\_LIBRARY\_PATH or some other method. The advantages of using a shared
library are:

\begin{verbatim}
   1.  Less disk space if you build more than 1 program
   2.  Less memory if more than one copy of a program using the shared
       library is running at the same time since the system is smart
       enough to share copies of the shared library at run time.
   3.  Possibly easier maintenance since a new version of the shared
       library can be installed without relinking all the software
       that uses it (as long as the subroutine names and calling
       sequences remain unchanged).
   4.  No run-time penalty.
\end{verbatim}
The disadvantages are:

\begin{verbatim}
   1. More hassle at runtime.  You have to either build the programs
      specially or have LD_LIBRARY_PATH set right.
   2. There may be a slight start up penalty, depending on where you are
      reading the shared library and the program from and if your CPU is
      either really slow or really heavily loaded.
\end{verbatim}

On Mac OS X platforms the 'make shared' command works like on other
UNIX platforms, but a .dylib file will be created instead of .so.  If
installed in a nonstandard location, add its location to the
DYLD\_LIBRARY\_PATH environment variable so that the library can be found
at run time.

On HP/UX systems, the environment variable CFLAGS should be set
to -Ae before running configure to enable "extended ANSI" features.

By default, a set of Fortran-callable wrapper routines are
also built and included in the CFITSIO library.  If these wrapper
routines are not needed (i.e., the CFITSIO library will not
be linked to any Fortran applications which call FITSIO subroutines)
then they may be omitted from the build by typing 'make all-nofitsio'
instead of simply typing 'make'.  This will reduce the size
of the CFITSIO library slightly.

It may not be possible to statically link programs that use CFITSIO on
some platforms (namely, on Solaris 2.6) due to the network drivers
(which provide FTP and HTTP access to FITS files).  It is possible to
make both a dynamic and a static version of the CFITSIO library, but
network file access will not be possible using the static version.


\subsection{VMS}

On VAX/VMS and ALPHA/VMS systems the make\_gfloat.com command file may
be executed to build the cfitsio.olb object library using the default
G-floating point option for double variables.  The make\_dfloat.com and
make\_ieee.com files may be used instead to build the library with the
other floating point options. Note that the getcwd function that is
used in the group.c module may require that programs using CFITSIO be
linked with the ALPHA\$LIBRARY:VAXCRTL.OLB library.  See the example
link line in the next section of this document.


\subsection{Windows PCs}

A precompiled DLL version of CFITSIO (not necessarily the latest version)
is available on the CFITSIO web site.
The CFITSIO library may also be built from the source code using the
CMake build system.  See the "README.win" file in the CFITSIO source distribution
for more information.


\subsection{Macintosh PCs}

When building on Mac OS-X, users should follow the Unix instructions,
above.  See the README.MacOS file for instructions on building a Universal
Binary that supports both Intel and PowerPC CPUs.


\section{Testing the Library}

The CFITSIO library should be tested by building and running
the testprog.c program that is included with the release.
On Unix systems, type:

\begin{verbatim}
    % make testprog
    % testprog > testprog.lis
    % diff testprog.lis testprog.out
    % cmp testprog.fit testprog.std
\end{verbatim}
 On VMS systems,
(assuming cc is the name of the C compiler command), type:

\begin{verbatim}
    $ cc testprog.c
    $ link testprog, cfitsio/lib, alpha$library:vaxcrtl/lib
    $ run testprog
\end{verbatim}
The test program should produce a FITS file called `testprog.fit'
that is identical to the `testprog.std' FITS file included with this
release.  The diagnostic messages (which were piped to the file
testprog.lis in the Unix example) should be identical to the listing
contained in the file testprog.out.  The 'diff' and 'cmp' commands
shown above should not report any differences in the files.  (There
may be some minor format differences, such as the presence or
absence of leading zeros, or 3 digit exponents in numbers,
which can be ignored).

The Fortran wrappers in CFITSIO may be tested with the testf77
program on Unix systems with:

\begin{verbatim}
    % f77 -o testf77 testf77.f -L. -lcfitsio -lnsl -lsocket
  or
    % f77 -f -o testf77 testf77.f -L. -lcfitsio    (under SUN O/S)
  or
    % f77 -o testf77 testf77.f -Wl,-L. -lcfitsio -lm -lnsl -lsocket (HP/UX)

    % testf77 > testf77.lis
    % diff testf77.lis testf77.out
    % cmp testf77.fit testf77.std
\end{verbatim}
On machines running SUN O/S, Fortran programs must be compiled with the
'-f' option to force double precision variables to be aligned on 8-byte
boundarys to make the fortran-declared variables compatible with C.  A
similar compiler option may be required on other platforms.  Failing to
use this option may cause the program to crash on FITSIO routines that
read or write double precision variables.

Also note that on some systems, the output listing of the testf77
program may differ slightly from the testf77.std template, if leading
zeros are not printed by default before the decimal point when using F
format.

A few other utility programs are included with CFITSIO; the first four
of this programs can be compiled an linked by typing `make
program\_name' where `program\_name' is the actual name of the program:

\begin{verbatim}
    speed - measures the maximum throughput (in MB per second)
              for writing and reading FITS files with CFITSIO.

    listhead - lists all the header keywords in any FITS file

    fitscopy - copies any FITS file (especially useful in conjunction
                 with the CFITSIO's extended input filename syntax).

    cookbook - a sample program that performs common read and
                 write operations on a FITS file.

    iter_a, iter_b, iter_c - examples of the CFITSIO iterator routine
\end{verbatim}


\section{Linking Programs with CFITSIO}

When linking applications software with the CFITSIO library, several
system libraries usually need to be specified on the link command
line.  On Unix systems, the most reliable way to determine what
libraries are required is to type 'make testprog' and see what
libraries the configure script has added.  The typical libraries that
need to be added are -lm (the math library) and -lnsl and -lsocket
(needed only for FTP and HTTP file access).  These latter 2 libraries
are not needed on VMS and Windows platforms, because FTP file access is
not currently supported on those platforms.

Note that when upgrading to a newer version of CFITSIO it is usually
necessary to recompile, as well as relink, the programs that use CFITSIO,
because the definitions in fitsio.h often change.


\section{Using CFITSIO in Multi-threaded Environments}

CFITSIO can be used either with the
POSIX pthreads interface or the OpenMP interface for multi-threaded
parallel programs.  When used in a multi-threaded environment,
the CFITSIO library *must* be built using
the -D\_REENTRANT compiler directive.  This can be done using the following
build commands:

\begin{verbatim}
  >./configure --enable-reentrant
  > make
\end{verbatim}
A function called fits\_is\_reentrant is available to test
whether or not CFITSIO was compiled with the -D\_REENTRANT
directive.  When this feature is enabled, multiple threads can
call any of the CFITSIO routines
to simultaneously read or write separate
FITS files.  Multiple threads can also read data from
the same FITS file simultaneously, as long as the file
was opened independently by each thread.  This relies on
the operating system to correctly deal with reading the
same file by multiple processes.  Different threads should
not share the same 'fitsfile' pointer to read an opened
FITS file, unless locks are placed around the calls to
the CFITSIO reading routines.
Different threads should never try to write to the same
FITS file.


\section{Getting Started with CFITSIO}

In order to effectively use the CFITSIO library it is recommended that
new users begin by reading the ``CFITSIO Quick Start Guide''.  It
contains all the basic information needed to write programs that
perform most types of operations on FITS files.  The set of example
FITS utility programs that are available from the CFITSIO web site are
also very useful for learning how to use CFITSIO.  To learn even more
about the capabilities of the CFITSIO library the following steps are
recommended:

1.  Read the following short `FITS Primer' chapter for an overview of
the structure of FITS files.

2. Review the Programming Guidelines in Chapter 4 to become familiar
with the conventions used by the CFITSIO interface.

3.  Refer to the cookbook.c, listhead.c, and fitscopy.c programs that
are included with this release for examples of routines that perform
various common FITS file operations.  Type 'make program\_name' to
compile and link these programs on Unix systems.

4.  Write a simple program to read or write a FITS file using the Basic
Interface routines described in Chapter 5.

5.  Scan through the more specialized routines that are described in
the following chapters to become familiar with the functionality that
they provide.


\section{Example Program}

The following listing shows an example of how to use the CFITSIO
routines in a C program.    Refer to the cookbook.c program that is
included with the CFITSIO distribution for other example routines.

This program creates a new FITS file, containing a FITS image.  An
`EXPOSURE' keyword is written to the header, then the image data are
written to the FITS file before closing the FITS file.

\begin{verbatim}
#include "fitsio.h"  /* required by every program that uses CFITSIO  */
main()
{
    fitsfile *fptr;       /* pointer to the FITS file; defined in fitsio.h */
    int status, ii, jj;
    long  fpixel = 1, naxis = 2, nelements, exposure;
    long naxes[2] = { 300, 200 };   /* image is 300 pixels wide by 200 rows */
    short array[200][300];

    status = 0;         /* initialize status before calling fitsio routines */
    fits_create_file(&fptr, "testfile.fits", &status);   /* create new file */

    /* Create the primary array image (16-bit short integer pixels */
    fits_create_img(fptr, SHORT_IMG, naxis, naxes, &status);

    /* Write a keyword; must pass the ADDRESS of the value */
    exposure = 1500.;
    fits_update_key(fptr, TLONG, "EXPOSURE", &exposure,
         "Total Exposure Time", &status);

    /* Initialize the values in the image with a linear ramp function */
    for (jj = 0; jj < naxes[1]; jj++)
        for (ii = 0; ii < naxes[0]; ii++)
            array[jj][ii] = ii + jj;

    nelements = naxes[0] * naxes[1];          /* number of pixels to write */

    /* Write the array of integers to the image */
    fits_write_img(fptr, TSHORT, fpixel, nelements, array[0], &status);

    fits_close_file(fptr, &status);            /* close the file */

    fits_report_error(stderr, status);  /* print out any error messages */
    return( status );
}
\end{verbatim}

\chapter{  A FITS Primer }

This section gives a brief overview of the structure of FITS files.
Users should refer to the documentation available from the FITS Support OFfice,
as described in the introduction, for more detailed information on FITS
formats.

FITS was first developed in the late 1970's as a standard data
interchange format between various astronomical observatories.  Since
then FITS has become the standard data format supported by most
astronomical data analysis software packages.

A FITS file consists of one or more Header + Data Units (HDUs), where
the first HDU is called the `Primary HDU', or `Primary Array'.  The
primary array contains an N-dimensional array of pixels, such as a 1-D
spectrum, a 2-D image, or a 3-D data cube.  Six different primary
data types are supported: Unsigned 8-bit bytes, 16-bit, 32-bit, and 64-bit signed
integers, and 32 and 64-bit floating point reals.  FITS also has a
convention for storing 16, 32-bit, and 64-bit unsigned integers (see the later
section entitled `Unsigned Integers' for more details). The primary HDU
may also consist of only a header with a null array containing no
data pixels.

Any number of additional HDUs may follow the primary array; these
additional HDUs are called FITS `extensions'.  There are currently 3
types of extensions defined by the FITS standard:

\begin{itemize}
\item
  Image Extension - a N-dimensional array of pixels, like in a primary array
\item
  ASCII Table Extension - rows and columns of data in ASCII character format
\item
  Binary Table Extension - rows and columns of data in binary representation
\end{itemize}

In each case the HDU consists of an ASCII Header Unit followed by an optional
Data Unit.  For historical reasons, each Header or Data unit must be an
exact multiple of 2880 8-bit bytes long.  Any unused space is padded
with fill characters (ASCII blanks or zeros).

Each Header Unit consists of any number of 80-character keyword records
or `card images' which have the
general form:

\begin{verbatim}
  KEYNAME = value / comment string
  NULLKEY =       / comment: This keyword has no value
\end{verbatim}
The keyword names may be up to 8 characters long and can only contain
uppercase letters, the digits 0-9, the hyphen, and the underscore
character. The keyword name is (usually) followed by an equals sign and
a space character (= ) in columns 9 - 10 of the record, followed by the
value of the keyword which may be either an integer, a floating point
number, a character string (enclosed in single quotes), or a boolean
value (the letter T or F).   A keyword may also have a null or undefined
value if there is no specified value string, as in the second example, above

The last keyword in the header is always the `END' keyword which has no
value or comment fields. There are many rules governing the exact
format of a keyword record (see the FITS Standard) so it is better
to rely on standard interface software like CFITSIO to correctly
construct or to parse the keyword records rather than try to deal
directly with the raw FITS formats.

Each Header Unit begins with a series of required keywords which depend
on the type of HDU.  These required keywords specify the size and
format of the following Data Unit.  The header may contain other
optional keywords to describe other aspects of the data, such as the
units or scaling values.  Other COMMENT or HISTORY keywords are also
frequently added to further document the data file.

The optional Data Unit immediately follows the last 2880-byte block in
the Header Unit.  Some HDUs do not have a Data Unit and only consist of
the Header Unit.

If there is more than one HDU in the FITS file, then the Header Unit of
the next HDU immediately follows the last 2880-byte block of the
previous Data Unit (or Header Unit if there is no Data Unit).

The main required keywords in FITS primary arrays or image extensions are:
\begin{itemize}
\item
BITPIX -- defines the data type of the array: 8, 16, 32, 64, -32, -64 for
unsigned 8--bit byte, 16--bit signed integer, 32--bit signed integer,
32--bit IEEE floating point, and 64--bit IEEE double precision floating
point, respectively.
\item
NAXIS --  the number of dimensions in the array, usually 0, 1, 2, 3, or 4.
\item
NAXISn -- (n ranges from 1 to NAXIS) defines the size of each dimension.
\end{itemize}

FITS tables start with the keyword XTENSION = `TABLE' (for ASCII
tables) or XTENSION = `BINTABLE' (for binary tables) and have the
following main keywords:
\begin{itemize}
\item
TFIELDS -- number of fields or columns in the table
\item
NAXIS2 -- number of rows in the table
\item
TTYPEn -- for each column (n ranges from 1 to TFIELDS) gives the
name of the column
\item
TFORMn -- the data type of the column
\item
TUNITn -- the physical units of the column (optional)
\end{itemize}

Users should refer to the FITS Support Office at {\tt http://fits.gsfc.nasa.gov}
for further information about the FITS format and related software
packages.


\chapter{ Programming Guidelines }


\section{CFITSIO Definitions}

Any program that uses the CFITSIO interface must include the fitsio.h
header file with the statement

\begin{verbatim}
  #include "fitsio.h"
\end{verbatim}
This header file contains the prototypes for all the CFITSIO user
interface routines as well as the definitions of various constants used
in the interface.  It also defines a C structure of type `fitsfile'
that is used by CFITSIO to store the relevant parameters that define
the format of a particular FITS file.  Application programs must define
a pointer to this structure for each FITS file that is to be opened.
This structure is initialized (i.e., memory is allocated for the
structure) when the FITS file is first opened or created with the
fits\_open\_file or fits\_create\_file routines.  This fitsfile pointer
is then passed as the first argument to every other CFITSIO routine
that operates on the FITS file.  Application programs must not directly
read or write elements in this fitsfile structure because the
definition of the structure may change in future versions of CFITSIO.

A number of symbolic constants are also defined in fitsio.h for the
convenience of application programmers.  Use of these symbolic
constants rather than the actual numeric value will help to make the
source code more readable and easier for others to understand.

\begin{verbatim}
String Lengths, for use when allocating character arrays:

  #define FLEN_FILENAME 1025 /* max length of a filename                  */
  #define FLEN_KEYWORD   72  /* max length of a keyword                   */
  #define FLEN_CARD      81  /* max length of a FITS header card          */
  #define FLEN_VALUE     71  /* max length of a keyword value string      */
  #define FLEN_COMMENT   73  /* max length of a keyword comment string    */
  #define FLEN_ERRMSG    81  /* max length of a CFITSIO error message     */
  #define FLEN_STATUS    31  /* max length of a CFITSIO status text string */

  Note that FLEN_KEYWORD is longer than the nominal 8-character keyword
  name length because the HIERARCH convention supports longer keyword names.

Access modes when opening a FITS file:

  #define READONLY  0
  #define READWRITE 1

BITPIX data type code values for FITS images:

  #define BYTE_IMG      8  /*  8-bit unsigned integers */
  #define SHORT_IMG    16  /* 16-bit   signed integers */
  #define LONG_IMG     32  /* 32-bit   signed integers */
  #define LONGLONG_IMG 64  /* 64-bit   signed integers */
  #define FLOAT_IMG   -32  /* 32-bit single precision floating point */
  #define DOUBLE_IMG  -64  /* 64-bit double precision floating point */

  The following 4 data type codes are also supported by CFITSIO:
  #define SBYTE_IMG  10      /*  8-bit signed integers, equivalent to */
                             /*  BITPIX = 8, BSCALE = 1, BZERO = -128 */
  #define USHORT_IMG  20     /* 16-bit unsigned integers, equivalent to */
                             /*  BITPIX = 16, BSCALE = 1, BZERO = 32768 */
  #define ULONG_IMG   40     /* 32-bit unsigned integers, equivalent to */
                             /*  BITPIX = 32, BSCALE = 1, BZERO = 2147483648 */
  #define ULONGLONG_IMG  80  /* 64-bit unsigned integers, equivalent to */
                             /*  BITPIX = 64, BSCALE = 1, BZERO = 9223372036854775808*/

Codes for the data type of binary table columns and/or for the
data type of variables when reading or writing keywords or data:

                              DATATYPE               TFORM CODE
  #define TBIT          1  /*                            'X' */
  #define TBYTE        11  /* 8-bit unsigned byte,       'B' */
  #define TLOGICAL     14  /* logicals (int for keywords     */
                           /*  and char for table cols   'L' */
  #define TSTRING      16  /* ASCII string,              'A' */
  #define TSHORT       21  /* signed short,              'I' */
  #define TLONG        41  /* signed long,                   */
  #define TLONGLONG    81  /* 64-bit long signed integer 'K' */
  #define TFLOAT       42  /* single precision float,    'E' */
  #define TDOUBLE      82  /* double precision float,    'D' */
  #define TCOMPLEX     83  /* complex (pair of floats)   'C' */
  #define TDBLCOMPLEX 163  /* double complex (2 doubles) 'M' */

  The following data type codes are also supported by CFITSIO:
  #define TINT         31  /* int                            */
  #define TSBYTE       12  /* 8-bit signed byte,         'S' */
  #define TUINT        30  /* unsigned int               'V' */
  #define TUSHORT      20  /* unsigned short             'U' */
  #define TULONG       40  /* unsigned long                  */
  #define TULONGLONG   80  /* unsigned long long         'W' */

  The following data type code is only for use with fits\_get\_coltype
  #define TINT32BIT    41  /* signed 32-bit int,         'J' */


HDU type code values (value returned when moving to new HDU):

  #define IMAGE_HDU  0  /* Primary Array or IMAGE HDU */
  #define ASCII_TBL  1  /* ASCII  table HDU */
  #define BINARY_TBL 2  /* Binary table HDU */
  #define ANY_HDU   -1  /* matches any type of HDU */

Column name and string matching case-sensitivity:

  #define CASESEN   1   /* do case-sensitive string match */
  #define CASEINSEN 0   /* do case-insensitive string match */

Logical states (if TRUE and FALSE are not already defined):

  #define TRUE 1
  #define FALSE 0

Values to represent undefined floating point numbers:

  #define FLOATNULLVALUE  -9.11912E-36F
  #define DOUBLENULLVALUE -9.1191291391491E-36

Image compression algorithm definitions

  #define RICE_1       11
  #define GZIP_1       21
  #define GZIP_2       22
  #define PLIO_1       31
  #define HCOMPRESS_1  41
  #define NOCOMPRESS  -1

  #define NO_DITHER -1
  #define SUBTRACTIVE_DITHER_1 1
  #define SUBTRACTIVE_DITHER_2 2
\end{verbatim}


\section{Current Header Data Unit (CHDU)}

The concept of the Current Header and Data Unit, or CHDU, is
fundamental to the use of the CFITSIO library.  A simple FITS image may
only contain a single Header and Data unit (HDU), but in general FITS
files can contain multiple Header Data Units (also known as
`extensions'), concatenated one after the other in the file.  The user
can specify which HDU should be initially opened at run time by giving
the HDU name or number after the root file name.  For example,
'myfile.fits[4]' opens the 5th HDU in the file (note that the numbering
starts with 0), and 'myfile.fits[EVENTS] opens the HDU with the name
'EVENTS' (as defined by the EXTNAME or HDUNAME keywords).  If no HDU is
specified then CFITSIO opens the first HDU (the primary array) by
default.  The CFITSIO routines which read and write data  only operate
within the opened HDU,  Other CFITSIO routines are provided to move to
and open any other existing HDU within the FITS file or to append or
insert new HDUs in the FITS file.


\section{Function Names and Variable Datatypes}

Most of the CFITSIO routines have both a short name as well as a
longer descriptive name.  The short name is only 5 or 6 characters long
and is similar to the subroutine name in the Fortran-77 version of
FITSIO.  The longer name is more descriptive and it is recommended that
it be used instead of the short name to more clearly document the
source code.

Many of the CFITSIO routines come in families which differ only in the
data type of the associated parameter(s).  The data type of these
routines is indicated by the suffix of the routine name.  The short
routine names have a 1 or 2 character suffix (e.g., 'j' in 'ffpkyj')
while the long routine names have a 4 character or longer suffix
as shown in the following table:

\begin{verbatim}
    Long      Short  Data
    Names     Names  Type
    -----     -----  ----
    _bit        x    bit
    _byt        b    unsigned byte
    _sbyt       sb   signed byte
    _sht        i    short integer
    _lng        j    long integer
    _lnglng     jj   8-byte LONGLONG integer (see note below)
    _usht       ui   unsigned short integer
    _ulng       uj   unsigned long integer
    _ulnglng    ujj  unsigned long long integer
    _uint       uk   unsigned int integer
    _int        k    int integer
    _flt        e    real exponential floating point (float)
    _fixflt     f    real fixed-decimal format floating point (float)
    _dbl        d    double precision real floating-point (double)
    _fixdbl     g    double precision fixed-format floating point (double)
    _cmp        c    complex reals (pairs of float values)
    _fixcmp     fc   complex reals, fixed-format floating point
    _dblcmp     m    double precision complex (pairs of double values)
    _fixdblcmp  fm   double precision complex, fixed-format floating point
    _log        l    logical (int)
    _str        s    character string
\end{verbatim}

The logical data type corresponds to `int' for logical keyword values,
and `byte' for logical binary table columns.  In other words, the value
when writing a logical keyword must be stored in an `int' variable, and
must be stored in a `char' array when reading or writing to `L' columns
in a binary table.  Implicit data type conversion is not supported for
logical table columns, but is for keywords, so a logical keyword may be
read and cast to any numerical data type; a returned value = 0
indicates false, and any other value = true.

The `int' data type may be 2 bytes long on some old PC compilers,
but otherwise it is nearly always 4 bytes long.   Some 64-bit
machines, like the Alpha/OSF, define the `short', `int',
and `long' integer data types to be 2, 4, and 8 bytes long,
respectively.

Because there is no universal C compiler standard for the name of the
8-byte integer datatype, the fitsio.h include file typedef's
'LONGLONG'  to be equivalent to  an
appropriate 8-byte integer data type on each supported platform.
For maximum software portability it is recommended that
this LONGLONG datatype be used to define 8-byte integer variables
rather than using the native data type name on a particular
platform. On most
32-bit Unix and Mac OS-X operating systems LONGLONG is equivalent to the
intrinsic 'long long' 8-byte integer datatype.  On 64-bit systems (which currently
includes Alpha OSF/1, 64-bit Sun Solaris, 64-bit SGI MIPS, and  64-bit
Itanium and Opteron PC systems), LONGLONG is  simply typedef'ed to be
equivalent to 'long'.   Microsoft Visual C++ Version 6.0 does not define
a 'long long'  data type, so LONGLONG is typedef'ed to be equivalent to
the '\_\_int64' data type on 32-bit windows systems when using Visual C++.

A related issue that affects the portability of software is how to print
out the value of a 'LONGLONG' variable with printf.  Developers may
find it convenient to use the following preprocessing statements
in their C programs to handle this in a machine-portable manner:


\begin{verbatim}
#if defined(_MSC_VER) /* Microsoft Visual C++ */
          printf("%I64d", longlongvalue);
	
#elif (USE_LL_SUFFIX == 1)
          printf("%lld", longlongvalue);
	
#else
          printf("%ld", longlongvalue);
#endif
\end{verbatim}

Similarly, the name of the C utility routine that converts a character
string of digits into a 8-byte integer value is platform dependent:


\begin{verbatim}
#if defined(_MSC_VER) /* Microsoft Visual C++ */
      /* VC++ 6.0 does not seem to have an 8-byte conversion routine */
	
#elif (USE_LL_SUFFIX == 1)
          longlongvalue = atoll(*string);
	
#else
          longlongvalue = atol(*string);
#endif
\end{verbatim}

When dealing with the FITS byte data type it is important to remember
that the raw values (before any scaling by the BSCALE and BZERO, or
TSCALn and TZEROn keyword values) in byte arrays (BITPIX = 8) or byte
columns (TFORMn = 'B') are interpreted as unsigned bytes with values
ranging from 0 to 255.  Some C compilers define a 'char' variable as
signed, so it is important to explicitly declare a numeric char
variable as 'unsigned char' to avoid any ambiguity

One feature of the CFITSIO routines is that they can operate on a `X'
(bit) column in a binary table as though it were a `B' (byte) column.
For example a `11X' data type column can be interpreted the same as a
`2B' column (i.e., 2 unsigned 8-bit bytes).  In some instances, it can
be more efficient to read and write whole bytes at a time, rather than
reading or writing each individual bit.

The complex and double precision complex data types are not directly
supported in ANSI C so these data types should be interpreted as pairs
of float or double values, respectively, where the first  value in each
pair is the real part, and the second is the imaginary part.


\section{Support for Unsigned Integers and Signed Bytes}

Although FITS does not directly support unsigned integers as one of its
fundamental data types, FITS can still be used to efficiently store
unsigned integer data values in images and binary tables.  The
convention used in FITS files is to store the unsigned integers as
signed integers with an associated offset (specified by the BZERO or
TZEROn keyword).  For example, to store unsigned 16-bit integer values
in a FITS image the image would be defined as a signed 16-bit integer
(with BITPIX keyword = SHORT\_IMG = 16) with the keywords BSCALE = 1.0
and BZERO = 32768.  Thus the unsigned values of 0, 32768, and 65535,
for example, are physically stored in the FITS image as -32768, 0, and
32767, respectively;  CFITSIO automatically adds the BZERO offset to
these values when they are read.  Similarly, in the case of unsigned
32-bit integers the BITPIX keyword would be equal to LONG\_IMG = 32 and
BZERO would be equal to 2147483648 (i.e. 2 raised to the 31st power).

The CFITSIO interface routines will efficiently and transparently apply
the appropriate offset in these cases so in general application
programs do not need to be concerned with how the unsigned values are
actually stored in the FITS file.  As a convenience for users, CFITSIO
has several predefined constants for the value of BITPIX  (USHORT\_IMG,
ULONG\_IMG, ULONGLONG\_IMG) and for the TFORMn value in the case of binary tables (`U',
`V', and `W') which programmers can use when creating FITS files containing
unsigned integer values.  The following code fragment illustrates how
to write a FITS 1-D primary array of unsigned 16-bit integers:

\begin{verbatim}
      unsigned short uarray[100];
      int naxis, status;
      long naxes[10], group, firstelem, nelements;
       ...
      status = 0;
      naxis = 1;
      naxes[0] = 100;
      fits_create_img(fptr, USHORT_IMG, naxis, naxes, &status);

      firstelem = 1;
      nelements = 100;
      fits_write_img(fptr, TUSHORT, firstelem, nelements,
                          uarray, &status);
       ...
\end{verbatim}
In the above example, the 2nd parameter in fits\_create\_img tells
CFITSIO to write the header keywords appropriate for an array of 16-bit
unsigned integers (i.e., BITPIX = 16 and BZERO = 32768).  Then the
fits\_write\_img routine writes the array of unsigned short integers
(uarray) into the primary array of the FITS file.  Similarly, a 32-bit
unsigned integer image may be created by setting the second parameter
in fits\_create\_img equal to `ULONG\_IMG' and by calling the
fits\_write\_img routine with the second parameter = TULONG to write
the array of unsigned long image pixel values.

An analogous set of routines are available for reading or writing unsigned
integer values and signed byte values in a FITS binary table extension.
When specifying the TFORMn keyword value which defines the format of a
column, CFITSIO recognizes 4 additional data type codes besides those
already defined in the FITS standard: `U' meaning a 16-bit unsigned
integer column, `V' for a 32-bit unsigned integer column, `W' for a 64-bit
unsigned integer column, and 'S'
for a signed byte column.  These non-standard data type codes are not
actually written into the FITS file but instead are just used internally
within CFITSIO.  The following code fragment illustrates how to use
these features:

\begin{verbatim}
      unsigned short uarray[100];
      unsigned int  varray[100];

      int colnum, tfields, status;
      long nrows, firstrow, firstelem, nelements, pcount;

      char extname[] = "Test_table";           /* extension name */

      /* define the name, data type, and physical units for 4 columns */
      char *ttype[] = { "Col_1", "Col_2", "Col_3", "Col_4" };
      char *tform[] = { "1U",      "1V",    "1W",    "1S"};  /* special CFITSIO codes */
      char *tunit[] = { " ",        " ",    " ",    " " };
       ...

           /* write the header keywords */
      status  = 0;
      nrows   = 1;
      tfields = 3
      pcount  = 0;
      fits_create_tbl(fptr, BINARY_TBL, nrows, tfields, ttype, tform,
                tunit, extname, &status);

           /* write the unsigned shorts to the 1st column */
      colnum    = 1;
      firstrow  = 1;
      firstelem = 1;
      nelements = 100;
      fits_write_col(fptr, TUSHORT, colnum, firstrow, firstelem,
              nelements, uarray, &status);

           /* now write the unsigned longs to the 2nd column */
      colnum    = 2;
      fits_write_col(fptr, TUINT, colnum, firstrow, firstelem,
              nelements, varray, &status);
       ...
\end{verbatim}
Note that the non-standard TFORM values for the 3 columns, `U', `V', and `W'
tell CFITSIO to write the keywords appropriate for unsigned 16-bit, unsigned 32-bit and
unsigned 64-bit integers, respectively (i.e., TFORMn = '1I' and TZEROn
= 32768 for unsigned 16-bit integers, TFORMn = '1J' and TZEROn =
2147483648 for unsigned 32-bit integers, and TFORMn = '1K' and TZEROn =
9223372036854775808 for unsigned 64-bit integers).  The 'S' TFORMn value tells
CFITSIO to write the keywords appropriate for a signed 8-bit byte column
with TFORMn = '1B' and TZEROn = -128.  The calls to fits\_write\_col
then write the arrays of unsigned integer values to the columns.


\section{Dealing with Character Strings}

The character string values in a FITS header or in an ASCII column in a
FITS table extension are generally padded out with non-significant
space characters (ASCII 32) to fill up the header record or the column
width.  When reading a FITS string value, the CFITSIO routines will
strip off these non-significant trailing spaces and will return a
null-terminated string value containing only the significant
characters.  Leading spaces in a FITS string are considered
significant.  If the string contains all blanks, then CFITSIO will
return a single blank character, i.e, the first blank is considered to
be significant, since it distinguishes the string from a null or
undefined string, but the remaining trailing spaces are not
significant.

Similarly, when writing string values to a FITS file the
CFITSIO routines expect to get a null-terminated string as input;
CFITSIO will pad the string with blanks if necessary when writing it
to the FITS file.

When calling CFITSIO routines that return a character string it is
vital that the size of the char array be large enough to hold the
entire string of characters, otherwise CFITSIO will overwrite whatever
memory locations follow the char array, possibly causing the program to
execute incorrectly.  This type of error can be difficult to debug, so
programmers should always ensure that the char arrays are allocated
enough space to hold the longest possible string, {\bf including} the
terminating NULL character.  The fitsio.h file contains the following
defined constants which programmers are strongly encouraged to use
whenever they are allocating space for char arrays:

\begin{verbatim}
#define FLEN_FILENAME 1025 /* max length of a filename */
#define FLEN_KEYWORD   72  /* max length of a keyword  */
#define FLEN_CARD      81  /* length of a FITS header card */
#define FLEN_VALUE     71  /* max length of a keyword value string */
#define FLEN_COMMENT   73  /* max length of a keyword comment string */
#define FLEN_ERRMSG    81  /* max length of a CFITSIO error message */
#define FLEN_STATUS    31  /* max length of a CFITSIO status text string */
\end{verbatim}
For example, when declaring a char array to hold the value string
of FITS keyword, use the following statement:

\begin{verbatim}
    char value[FLEN_VALUE];
\end{verbatim}
Note that FLEN\_KEYWORD is longer than needed for the nominal 8-character
keyword name because the HIERARCH convention supports longer keyword names.


\section{Implicit Data Type Conversion}

The CFITSIO routines that read and write numerical data can perform
implicit data type conversion.  This means that the data type of the
variable or array in the program does not need to be the same as the
data type of the value in the FITS file.  Data type conversion is
supported for numerical and string data types (if the string contains a
valid number enclosed in quotes) when reading a FITS header keyword
value and for numeric values when reading or writing values in the
primary array or a table column.  CFITSIO returns status =
NUM\_OVERFLOW  if the converted data value exceeds the range of the
output data type.  Implicit data type conversion is not supported
within binary tables for string, logical, complex, or double complex
data types.

In addition, any table column may be read as if it contained string values.
In the case of numeric columns the returned string will be formatted
using the TDISPn display format if it exists.


\section{Data Scaling}

When reading numerical data values in the primary array or a
table column, the values will be scaled automatically by the BSCALE and
BZERO (or TSCALn and TZEROn) header values if they are
present in the header.  The scaled data that is returned to the reading
program will have

\begin{verbatim}
        output value = (FITS value) * BSCALE + BZERO
\end{verbatim}
(a corresponding formula using TSCALn and TZEROn is used when reading
from table columns).  In the case of integer output values the floating
point scaled value is truncated to an integer (not rounded to the
nearest integer).  The fits\_set\_bscale and fits\_set\_tscale routines
(described in the `Advanced' chapter) may be used to override the
scaling parameters defined in the header (e.g., to turn off the scaling
so that the program can read the raw unscaled values from the FITS
file).

When writing numerical data to the primary array or to a table column
the data values will generally be automatically inversely scaled by the
value of the BSCALE and BZERO (or TSCALn and TZEROn) keyword values if
they they exist in the header.  These keywords must have been written
to the header before any data is written for them to have any immediate
effect.  One may also use the fits\_set\_bscale and fits\_set\_tscale
routines to define or override the scaling keywords in the header
(e.g., to turn off the scaling so that the program can write the raw
unscaled values into the FITS file). If scaling is performed, the
inverse scaled output value that is written into the FITS file will
have

\begin{verbatim}
         FITS value = ((input value) - BZERO) / BSCALE
\end{verbatim}
(a corresponding formula using TSCALn and TZEROn is used when
writing to table columns).  Rounding to the nearest integer, rather
than truncation, is performed when writing integer data types to the
FITS file.


\section{Support for IEEE Special Values}

The ANSI/IEEE-754 floating-point number standard defines certain
special values that are used to represent such quantities as
Not-a-Number (NaN), denormalized, underflow, overflow, and infinity.
(See the Appendix in the FITS standard or the FITS User's
Guide for a list of these values).  The CFITSIO routines that read
floating point data in FITS files recognize these IEEE special values
and by default interpret the overflow and infinity values as being
equivalent to a NaN, and convert the underflow and denormalized values
into zeros.  In some cases programmers may want access to the raw IEEE
values, without any modification by CFITSIO.  This can be done by
calling the fits\_read\_img or fits\_read\_col routines while
specifying 0.0 as the value of the NULLVAL parameter.  This will force
CFITSIO to simply pass the IEEE values through to the application
program without any modification.  This is not fully supported on
VAX/VMS machines, however, where there is no easy way to bypass the
default interpretation of the IEEE special values.  This is also not
supported when reading floating-point images that have been compressed
with the FITS tiled image compression convention that is discussed in
section 5.6;  the pixels values in tile compressed images are
represented by scaled integers, and a reserved integer value
(not a NaN) is used to represent undefined pixels.


\section{Error Status Values and the Error Message Stack}

Nearly all the CFITSIO routines return an error status value
in 2 ways: as the value of the last parameter in the function call,
and as the returned value of the function itself.  This provides
some flexibility in the way programmers can test if an error
occurred, as illustrated in the following 2 code fragments:

\begin{verbatim}
    if ( fits_write_record(fptr, card, &status) )
         printf(" Error occurred while writing keyword.");

or,

    fits_write_record(fptr, card, &status);
    if ( status )
         printf(" Error occurred while writing keyword.");
\end{verbatim}
A listing of all the CFITSIO status code values is given at the end of
this document.  Programmers are encouraged to use the symbolic
mnemonics (defined in fitsio.h) rather than the actual integer status
values to improve the readability of their code.

The CFITSIO library uses an `inherited status' convention for the
status parameter which means that if a routine is called with a
positive input value of the status parameter as input, then the routine
will exit immediately without changing the value of the status
parameter.  Thus, if one passes the status value returned from each
CFITSIO routine as input to the next CFITSIO routine, then whenever an
error is detected all further CFITSIO processing will cease.  This
convention can simplify the error checking in application programs
because it is not necessary to check the value of the status parameter
after every single CFITSIO routine call.  If a program contains a
sequence of several CFITSIO calls, one can just check the status value
after the last call.  Since the returned status values are generally
distinctive, it should be possible to determine which routine
originally returned the error status.

CFITSIO also maintains an internal stack of error messages
(80-character maximum length)  which in many cases provide a more
detailed explanation of the cause of the error than is provided by the
error status number alone.  It is recommended that the error message
stack be printed out whenever a program detects a CFITSIO error.  The
function fits\_report\_error will print out the entire error message
stack, or alternatively one may call fits\_read\_errmsg to get the
error messages one at a time.


\section{Variable-Length Arrays in Binary Tables}

CFITSIO provides easy-to-use support for reading and writing data in
variable length fields of a binary table. The variable length columns
have TFORMn keyword values of the form `1Pt(len)' or `1Qt(len)' where `t'
is the data type code (e.g., I, J, E, D, etc.) and `len' is an integer
specifying the maximum length of the vector in the table.  The 'P' type
variable length columns use 32-bit array length and byte offset values, whereas
the 'Q' type columns use 64-bit values, which may be required when dealing
with large arrays.  CFITSIO supports a local convention that interprets
the 'P' type descriptors as unsigned 32-bit integers, which provides
a factor of 2 greater range for the array length or heap address than
is possible with 32-bit 'signed' integers.  Note, however, that other software
packages may not support this convention, and may be unable to read thees
extended range variable length records.

If the value
of `len' is not specified when the table is created (e.g., if the TFORM
keyword value is simply specified as '1PE' instead of '1PE(400) ), then
CFITSIO will automatically scan the table when it is closed to
determine the maximum length of the vector and will append this value
to the TFORMn value.

The same routines that read and write data in an ordinary fixed length
binary table extension are also used for variable length fields,
however, the routine parameters take on a slightly different
interpretation as described below.

All the data in a variable length field is written into an area called
the `heap' which follows the main fixed-length FITS binary table. The
size of the heap, in bytes, is specified by the PCOUNT keyword in the
FITS header. When creating a new binary table, the initial value of
PCOUNT should usually be set to zero. CFITSIO will recompute the size
of the heap as the data is written and will automatically update the
PCOUNT keyword value when the table is closed.  When writing variable
length data to a table, CFITSIO will automatically extend the size
of the heap area if necessary, so that any following HDUs do not
get overwritten.

By default the heap data area starts immediately after the last row of
the fixed-length table.  This default starting location may be
overridden by the THEAP keyword, but this is not recommended.
If additional rows of data are added to the table, CFITSIO will
automatically shift the the heap down to make room for the new
rows, but it is obviously be more efficient to initially
create the table with the necessary number of blank rows, so that
the heap does not needed to be constantly moved.

When writing row of data to a variable length field the entire array of values for
a given row of the table must be written with a single call to
fits\_write\_col.
The total length of the array is given by nelements
+ firstelem - 1.  Additional elements cannot be appended to an existing
vector at a later time since any attempt to do so will simply overwrite
all the previously written data and the new data will be
written to a new area of the heap.  The fits\_compress\_heap routine
is provided to compress the heap and recover any unused space.
To avoid having to deal with this issue, it is recommended
that rows in a variable length field should only be written once.
An exception to
this general rule occurs when setting elements of an array as
undefined.  It is allowed to first write a dummy value into the array with
fits\_write\_col, and then call fits\_write\_col\_nul to flag the
desired elements as undefined. Note that the rows of a table,
whether fixed or variable length, do not have to be written
consecutively and may be written in any order.

When writing to a variable length ASCII character field (e.g., TFORM =
'1PA') only a single character string can be written.  The `firstelem'
and `nelements' parameter values in the fits\_write\_col routine are
ignored and the number of characters to write is simply determined by
the length of the input null-terminated character string.

The fits\_write\_descript routine is useful in situations where
multiple rows of a variable length column have the identical array of
values.  One can simply write the array once for the first row, and
then use fits\_write\_descript to write the same descriptor values into
the other rows;  all the rows will then point to the same storage
location thus saving disk space.

When reading from a variable length array field one can only read as
many elements as actually exist in that row of the table; reading does
not automatically continue with the next row of the table as occurs
when reading an ordinary fixed length table field.  Attempts to read
more than this will cause an error status to be returned.  One can
determine the number of elements in each row of a variable column with
the fits\_read\_descript routine.


\section{Multiple Access to the Same FITS File}

CFITSIO supports simultaneous read and write access to different
HDUs in the same FITS file in some circumstances, as described
below:

\begin{itemize}

\item
Multi-threaded programs

When CFITSIO is compiled with the -D\_REENTRANT directive (as can be
tested with the fits\_is\_reentrant function) different threads can
call any of the CFITSIO routines  to simultaneously read or write
separate  FITS files.  Multiple threads can also read data from the
same FITS file simultaneously, as long as the file was opened
independently by each thread.  This relies on the operating system
to correctly deal with reading the same file by multiple processes.
Different threads should not share the same 'fitsfile' pointer to
read an opened FITS file, unless locks are placed around the calls
to  the CFITSIO reading routines.  Different threads should  never
try to write to the same FITS file.

\item
Multiple read access to the same FITS file within a single
program/thread

A single process may open the same FITS file with READONLY access
multiple times, and thus create multiple 'fitsfile*' pointers to
that same file within CFITSIO. This relies on the operating system's
ability to open a single file multiple times and correctly manage
the subsequent read requests directed to the different C 'file*'
pointers, which actually all point to the same file.  CFITSIO simply
executes the read requests to the differnet 'fitsfile*' pointers the
same as if they were physically different files.

\item
Multiple write access to the same FITS file within a single
program/thread

CFITSIO supports opening the same FITS file multiple times with
WRITE access, but it only physically opens the file (at the
operating system level) once, on the first call to fits\_open\_file.
If fits\_open\_file is subsequently called to open the same file
again, CFITSIO will recognize that the file is already open, and
will return a new 'fitsfile*' pointer that logically points to the
first 'fitsfile*' pointer, without actually opening the file a
second time. The application program can then treat the 2
'fitsfile*' pointers as if they point to different files, and can
seemingly move to and write data to 2 different  HDUs within the
same file.  However, each time the application program switches
which 'fitsfile*' pointer it is writing to, CFITSIO will flush  any
internal buffers that contain data written to the first 'fitsfile*'
pointer,  then move to the HDU that the other 'fitsfile*' pointer
is writing to. Obviously, this may add a significant amount of
computational overhead if the  application program uses this feature
to frequently switch back and forth between writing to 2 (or more)
HDUs in the same file, so this capability should be used
judiciously.

Note that CFITSIO will not allow a FITS file to be opened a second
time with READWRITE access if it was opened previously with READONLY
access.

\end{itemize}


\section{When the Final Size of the FITS HDU is Unknown}

It is not required to know the total size of a FITS data array or table
before beginning to write the data to the FITS file.  In the case of
the primary array or an image extension, one should initially create
the array with the size of the highest dimension (largest NAXISn
keyword) set to a dummy value, such as 1.  Then after all the data have
been written and the true dimensions are known, then the NAXISn value
should be updated using the fits\_update\_key routine before moving to
another extension or closing the FITS file.

When writing to FITS tables, CFITSIO automatically keeps track of the
highest row number that is written to, and will increase the size of
the table if necessary.  CFITSIO will also automatically insert space
in the FITS file if necessary, to ensure that the data 'heap', if it
exists, and/or any additional HDUs that follow the table do not get
overwritten as new rows are written to the table.

As a general rule it is best to specify the initial number of rows = 0
when the table is created, then let CFITSIO keep track of the number of
rows that are actually written.  The application program should not
manually update the number of rows in the table (as given by the NAXIS2
keyword) since CFITSIO does this automatically.  If a table is
initially created with more than zero rows, then this will usually be
considered as the minimum size of the table, even if fewer rows are
actually written to the table.  Thus, if a table is initially created
with NAXIS2 = 20, and CFITSIO only writes 10 rows of data before
closing the table, then NAXIS2 will remain equal to 20.  If however, 30
rows of data are written to this table, then NAXIS2 will be increased
from 20 to 30.  The one exception to this automatic updating of the
NAXIS2 keyword is if the application program directly modifies the
value of NAXIS2 (up or down) itself just before closing the table.  In this
case, CFITSIO does not update NAXIS2 again, since it assumes that the
application program must have had a good reason for changing the value
directly.  This is not recommended, however, and is only provided for
backward compatibility with software that initially creates a table
with a large number of rows, than decreases the NAXIS2 value to the
actual smaller value just before closing the table.


\section{CFITSIO Size Limitations}

CFITSIO places very few restrictions on the size of FITS files that it
reads or writes.  There are a few limits, however, that may affect
some extreme cases:

1.  The maximum number of FITS files that may be simultaneously opened by
CFITSIO is set by NMAXFILES, as defined in fitsio2.h.  The current default
value is 1000, but this may be increased if necessary. Note that CFITSIO
allocates NIOBUF * 2880 bytes of I/O buffer space for each file that is
opened.  The default value of NIOBUF is 40 (defined in fitsio.h), so this
amounts to  more than 115K of memory  for each opened file (or 115 MB for
1000 opened files).  Note that the underlying  operating system, may have a
lower limit on the number of files that can be opened simultaneously.

2.  It used to be common for computer systems to only support disk files up
to 2**31 bytes = 2.1 GB in size, but most systems now support larger files.
CFITSIO can optionally read and write these so-called 'large files' that
are greater than 2.1 GB on
platforms where they are supported, but this
usually requires that special compiler option flags be specified to turn
on this  option.  On linux and solaris systems the compiler flags are
'-D\_LARGEFILE\_SOURCE' and  `-D\_FILE\_OFFSET\_BITS=64'. These flags
may also work on other platforms but this has not been tested.  Starting
with version 3.0 of CFITSIO, the default Makefile that is distributed
with CFITSIO will include these 2 compiler flags when building on Solaris
and Linux PC systems.   Users on other platforms will need to add these
compiler flags manually if they want to support large files.  In most
cases it appears that it is not necessary to include these compiler
flags when compiling application code that call the CFITSIO library
routines.

When CFITSIO is built with large file support (e.g., on Solaris and
Linux PC system by default) then it can read and write FITS data files
on disk that have any of these conditions:

\begin{itemize}
\item
FITS files larger than 2.1 GB in size
\item
FITS images containing greater than 2.1 G pixels
\item
FITS images that have one dimension with more than 2.1 G pixels
(as given by one of the NAXISn keyword)
\item
FITS tables containing more than 2.1E09 rows (given by the NAXIS2 keyword),
or with rows that are more than 2.1 GB wide (given by the NAXIS1 keyword)
\item
FITS binary tables with a variable-length array heap that is larger
than 2.1 GB (given by the PCOUNT keyword)
\end{itemize}

The current maximum FITS file size supported by  CFITSIO
is about 6 terabytes (containing
2**31 FITS blocks, each 2880 bytes in size). Currently, support for large
files in CFITSIO has been tested on the Linux, Solaris, and IBM AIX
operating systems.

Note that when writing application programs that are intended to support
large files it is important to use 64-bit integer variables
to store quantities such as the dimensions of images, or the number of
rows in a table.  These programs must also call the special versions
of some of the CFITSIO routines that have been adapted to
support 64-bit integers.  The names of these routines end in
'll' ('el' 'el') to distinguish them from the 32-bit integer
version (e.g.,  fits\_get\_num\_rowsll).


\chapter{Basic CFITSIO Interface Routines }

This chapter describes the basic routines in the CFITSIO user interface
that provide all the functions normally needed to read and write most
FITS files.  It is recommended that these routines be used for most
applications and that the more advanced routines described in the
next chapter only be used in special circumstances when necessary.

The following conventions are used in this chapter in the description
of each function:

1. Most functions have 2 names: a long descriptive name and a short
concise name.  Both names are listed on the first line of the following
descriptions, separated by a slash (/) character.  Programmers may use
either name in their programs but the long names are recommended to
help document the code and make it easier to read.

2. A right arrow symbol ($>$) is used in the function descriptions to
separate the input parameters from the output parameters in the
definition of each routine.  This symbol is not actually part of the C
calling sequence.

3. The function parameters are defined in more detail in the
alphabetical listing in Appendix B.

4.  The first argument in almost all the functions is a pointer to a
structure of type `fitsfile'.  Memory for this structure is allocated
by CFITSIO when the FITS file is first opened or created and is freed
when the FITS file is closed.

5.  The last argument in almost all the functions is the error status
parameter.  It must be equal to 0 on input, otherwise the function will
immediately exit without doing anything.  A non-zero output value
indicates that an error occurred in the function.  In most cases the
status value is also returned as the value of the function itself.


\section{CFITSIO Error Status Routines}


\begin{description}
\item[1 ] Return a descriptive text string (30 char max.) corresponding to
   a CFITSIO error status code.\label{ffgerr}
\end{description}

\begin{verbatim}
  void fits_get_errstatus / ffgerr (int status, > char *err_text)
\end{verbatim}

\begin{description}
\item[2 ] Return the top (oldest) 80-character error message from the
    internal CFITSIO stack of error messages and shift any remaining
    messages on the stack up one level.  Call this routine
    repeatedly to get each message in sequence.  The function returns
   a value = 0 and a null error message when the error stack is empty.
\label{ffgmsg}
\end{description}

\begin{verbatim}
  int fits_read_errmsg / ffgmsg (char *err_msg)
\end{verbatim}

\begin{description}
\item[3 ] Print out the error message corresponding to the input status
    value and all the error messages on the CFITSIO stack to the specified
    file stream  (normally to stdout or stderr).  If the input
    status value = 0 then this routine does nothing.
\label{ffrprt}
\end{description}

\begin{verbatim}
  void fits_report_error / ffrprt (FILE *stream, status)
\end{verbatim}

\begin{description}
\item[4 ]The fits\_write\_errmark routine puts an invisible marker on the
   CFITSIO error stack.  The fits\_clear\_errmark routine can then be
   used to delete any more recent error messages on the stack, back to
   the position of the marker.  This preserves any older error messages
   on the stack.  The fits\_clear\_errmsg routine simply clears all the
   messages (and marks) from the stack.  These routines are called
   without any arguments.
\label{ffpmrk}  \label{ffcmsg}
\end{description}

\begin{verbatim}
  void fits_write_errmark / ffpmrk (void)
  void fits_clear_errmark / ffcmrk (void)
  void fits_clear_errmsg / ffcmsg (void)
\end{verbatim}


\section{FITS File Access Routines}


\begin{description}
\item[1 ] Open an existing data file. \label{ffopen}


\begin{verbatim}
int fits_open_file / ffopen
    (fitsfile **fptr, char *filename, int iomode, > int *status)

int fits_open_diskfile / ffdkopen
    (fitsfile **fptr, char *filename, int iomode, > int *status)

int fits_open_data / ffdopn
    (fitsfile **fptr, char *filename, int iomode, > int *status)

int fits_open_table / fftopn
    (fitsfile **fptr, char *filename, int iomode, > int *status)

int fits_open_image / ffiopn
    (fitsfile **fptr, char *filename, int iomode, > int *status)

int fits_open_extlist / ffeopn
    (fitsfile **fptr, char *filename, int iomode, char *extlist,
    >  int *hdutype, int *status)
\end{verbatim}

The iomode parameter determines the read/write access allowed in the
file and can have values of READONLY (0) or READWRITE (1). The filename
parameter gives the name of the file to be opened, followed by an
optional argument giving the name or index number of the extension
within the FITS file that should be moved to and opened (e.g.,
\verb-myfile.fits+3- or \verb-myfile.fits[3]- moves to the 3rd extension within
the file, and \verb-myfile.fits[events]- moves to the extension with the
keyword EXTNAME = 'EVENTS').

The fits\_open\_diskfile routine is similar to the fits\_open\_file routine
except that it does not support the extended filename syntax in the input
file name.  This routine simply tries to open the specified input file
on magnetic disk.  This routine is mainly for use in cases where the
filename (or directory path) contains square or curly bracket characters
that would confuse the extended filename parser.

The fits\_open\_data routine is similar to the fits\_open\_file routine
except that it will move to the first HDU containing significant data,
if a HDU name or number to open was not explicitly specified as
part of the filename.  In this case, it will look for the first
IMAGE HDU with NAXIS greater than 0, or the first table that does not contain the
strings `GTI' (Good Time Interval extension) or `OBSTABLE' in the
EXTNAME keyword value.

The fits\_open\_table and fits\_open\_image routines are similar to
fits\_open\_data except they will move to the first significant table
HDU or image HDU in the file, respectively, if a HDU name or
number is not specified as part of the filename.

The fits\_open\_extname routine opens the file and attempts to move to a 'useful'
HDU. If after opening the file CFITSIO is pointing to null primary array,
then CFITSIO will attempt to move to the first extension that has an EXTNAME or
HDUNAME keyword value that matches one of the names in the input extlist
space-delimited list of names.  If that fails, then CFITSIO simply moves to the
2nd HDU in the file.

IRAF images (.imh format files) and raw binary data arrays may also be
opened with READONLY access.  CFITSIO will automatically test if the
input file is an IRAF image, and if, so will convert it on the fly into
a virtual FITS image before it is opened by the application program.
If the input file is a raw binary data array of numbers, then the data type
and dimensions of the array must be specified in square brackets
following the name of the file (e.g.  'rawfile.dat[i512,512]' opens a
512 x 512 short integer image).  See the `Extended File Name Syntax'
chapter for more details on how to specify the raw file name.  The raw
file is converted on the fly into a virtual FITS image in memory that
is then opened by the application program with READONLY access.

Programs can read the input file from the 'stdin' file stream if a dash
character ('-') is given as the filename. Files can also be opened over
the network using FTP or HTTP protocols by supplying the appropriate URL
as the filename.  The HTTPS and FTPS protocols are also supported if the CFITSIO
build includes the libcurl library.  (If the CFITSIO 'configure' script
finds a usable libcurl library on your system, it will automatically be 
included in the build.)

The input file can be modified in various ways to create a virtual file
(usually stored in memory) that is then opened by the application
program by supplying a filtering or binning specifier in square brackets
following the filename. Some of the more common filtering methods are
illustrated in the following paragraphs, but users should refer to the
'Extended File Name Syntax' chapter for a complete description of
the full file filtering syntax.

When opening an image, a rectangular subset of the physical image may be
opened by listing the first and last pixel in each dimension (and
optional pixel skipping factor):

\begin{verbatim}
myimage.fits[101:200,301:400]
\end{verbatim}
will create and open a 100x100 pixel virtual image of that section of
the physical image, and \verb+myimage.fits[*,-*]+ opens a virtual image
that is the same size as the physical image but has been flipped in
the vertical direction.

When opening a table, the filtering syntax can be used to add or delete
columns or keywords in the virtual table:
\verb-myfile.fits[events][col !time; PI = PHA*1.2]- opens a virtual table in which the TIME column
has been deleted and a new PI column has been added with a value 1.2
times that of the PHA column. Similarly, one can filter a table to keep
only those rows that satisfy a selection criterion:
\verb-myfile.fits[events][pha > 50]- creates and opens a virtual table
containing only those rows with a PHA value greater than 50. A large
number of boolean and mathematical operators can be used in the
selection expression. One can also filter table rows using 'Good Time
Interval' extensions, and spatial region filters as in
\verb-myfile.fits[events][gtifilter()]- and
\verb-myfile.fits[events][regfilter( "stars.rng")]-.

Finally, table columns may be binned or histogrammed to generate a
virtual image. For example, \verb-myfile.fits[events][bin (X,Y)=4]- will
result in a 2-dimensional image calculated by binning the X and Y
columns in the event table with a bin size of 4 in each dimension. The
TLMINn and TLMAXn keywords will be used by default to determine the
range of the image.

A single program can open the same FITS file more than once and then
treat the resulting fitsfile pointers as though they were completely
independent FITS files. Using this facility, a program can open a FITS
file twice, move to 2 different extensions within the file, and then
 read and write data in those extensions in any order.
\end{description}


\begin{description}
\item[2 ]  Create and open a new empty output FITS file. \label{ffinit}


\begin{verbatim}
int fits_create_file / ffinit
    (fitsfile **fptr, char *filename, > int *status)

int fits_create_diskfile / ffdkinit
    (fitsfile **fptr, char *filename, > int *status)
\end{verbatim}

An error will be returned if the specified file already exists, unless
the filename is prefixed with an exclamation point (!). In that case
CFITSIO will overwrite (delete) any existing file with the same name.
Note that the exclamation point is a special UNIX character so if
it is used on the command line it must be preceded by a backslash to
force the UNIX shell to accept the character as part of the filename.

The output file will be written to the 'stdout' file stream if a dash
character ('-') or the string 'stdout' is given as the filename. Similarly,
'-.gz' or 'stdout.gz' will cause the file to be gzip compressed before
it is written out to the stdout stream.

Optionally, the name of a template file that is used to define the
structure of the new file may be specified in parentheses following the
output file name. The template file may be another FITS file, in which
case the new file, at the time it is opened, will be an exact copy of
the template file except that the data structures (images and tables)
will be filled with zeros. Alternatively, the template file may be an
ASCII format text file containing directives that define the keywords to be
created in each HDU of the file. See the 'Extended File Name Syntax'
 section for a complete description of the template file syntax.

The fits\_create\_diskfile routine is similar to the fits\_create\_file routine
except that it does not support the extended filename syntax in the input
file name.  This routine simply tries to create the specified file
on magnetic disk.  This routine is mainly for use in cases where the
filename (or directory path) contains square or curly bracket characters
 that would confuse the extended filename parser.
\end{description}



\begin{description}
\item[3 ] Close a previously opened FITS file.  The first routine simply
closes the file, whereas the second one also DELETES the file, which
can be useful in cases where a FITS file has been partially created,
but then an error occurs which prevents it from being completed.
Note that these routines behave differently than most other CFITSIO routines
if the input value of the `status' parameter is not zero:  Instead of
simply returning to the calling program without doing anything, these
routines effectively ignore the input status value and still attempt
to close or delete the file.
 \label{ffclos} \label{ffdelt}
\end{description}

\begin{verbatim}
  int fits_close_file / ffclos (fitsfile *fptr, > int *status)

  int fits_delete_file / ffdelt (fitsfile *fptr, > int *status)
\end{verbatim}

\begin{description}
\item[4 ]Return the name, I/O mode (READONLY or READWRITE), and/or the file
type (e.g. 'file://', 'ftp://') of the opened FITS file. \label{ffflnm}
 \label{ffflmd} \label{ffurlt}
\end{description}

\begin{verbatim}
  int fits_file_name / ffflnm (fitsfile *fptr, > char *filename, int *status)

  int fits_file_mode / ffflmd (fitsfile *fptr, > int *iomode, int *status)

  int fits_url_type / ffurlt (fitsfile *fptr, > char *urltype, int *status)
\end{verbatim}

\section{HDU Access Routines}

The following functions perform operations on Header-Data Units (HDUs)
as a whole.


\begin{description}
\item[1 ] Move to a different HDU in the file.  The first routine moves to a
    specified absolute HDU number (starting with 1 for the primary
    array) in the FITS file, and the second routine moves a relative
    number HDUs forward or backward from the current HDU.  A null
    pointer may be given for the hdutype parameter if it's value is not
    needed.  The third routine moves to the (first) HDU which has the
    specified extension type and EXTNAME and EXTVER keyword values (or
    HDUNAME and HDUVER keywords).  The hdutype parameter may have a
    value of IMAGE\_HDU, ASCII\_TBL, BINARY\_TBL, or ANY\_HDU where
    ANY\_HDU means that only the extname and extver values will be used
    to locate the correct extension.  If the input value of extver is 0
    then the EXTVER keyword is ignored and the first HDU with a
    matching EXTNAME (or HDUNAME) keyword will be found.  If no
    matching HDU is found in the file then the current HDU will remain
    unchanged and a status = BAD\_HDU\_NUM will be returned.
  \label{ffmahd} \label{ffmrhd} \label{ffmnhd}
\end{description}

\begin{verbatim}
  int fits_movabs_hdu / ffmahd
      (fitsfile *fptr, int hdunum, > int *hdutype, int *status)

  int fits_movrel_hdu / ffmrhd
      (fitsfile *fptr, int nmove, > int *hdutype, int *status)

  int fits_movnam_hdu / ffmnhd
      (fitsfile *fptr, int hdutype, char *extname, int extver, > int *status)
\end{verbatim}

\begin{description}
\item[2 ] Return the total number of HDUs in the FITS file.  This returns the
number of completely defined HDUs in the file.  If a new HDU has just been added to
the FITS file, then that last HDU will only be counted if it has been closed,
or if data has been written to the HDU.
   The current HDU remains unchanged by this routine. \label{ffthdu}
\end{description}

\begin{verbatim}
  int fits_get_num_hdus / ffthdu
      (fitsfile *fptr, > int *hdunum, int *status)
\end{verbatim}

\begin{description}
\item[3 ] Return the number of the current HDU (CHDU) in the FITS file (where
    the primary array = 1).  This function returns the HDU number
   rather than a status value.  \label{ffghdn}
\end{description}

\begin{verbatim}
  int fits_get_hdu_num / ffghdn
      (fitsfile *fptr, > int *hdunum)
\end{verbatim}

\begin{description}
\item[4 ] Return the type of the current HDU in the FITS file.  The possible
   values for hdutype are: IMAGE\_HDU, ASCII\_TBL, or BINARY\_TBL.  \label{ffghdt}
\end{description}

\begin{verbatim}
  int fits_get_hdu_type / ffghdt
      (fitsfile *fptr, > int *hdutype, int *status)
\end{verbatim}

\begin{description}
\item[5 ] Copy all or part of the HDUs in the FITS file associated with infptr
    and append them to the end of the FITS file associated with
    outfptr.  If 'previous' is true (not 0), then any HDUs preceding
    the current HDU in the input file will be copied to the output
    file.  Similarly, 'current' and 'following' determine whether the
    current HDU, and/or any following HDUs in the input file will be
    copied to the output file. Thus, if all 3 parameters are true, then the
    entire input file will be copied.  On exit, the current HDU in
    the input file will be unchanged, and the last HDU in the output
   file will be the current HDU.  \label{ffcpfl}
\end{description}

\begin{verbatim}
  int fits_copy_file / ffcpfl
      (fitsfile *infptr, fitsfile *outfptr, int previous, int current,
          int following, > int *status)
\end{verbatim}

\begin{description}
\item[6 ] Copy the current HDU from the FITS file associated with infptr and append it
    to the end of the FITS file associated with outfptr.  Space may be
   reserved for MOREKEYS additional keywords in the output header. \label{ffcopy}
\end{description}

\begin{verbatim}
  int fits_copy_hdu / ffcopy
      (fitsfile *infptr, fitsfile *outfptr, int morekeys, > int *status)
\end{verbatim}

\begin{description}
\item[7 ] Write the current HDU in the input FITS file to the
   output FILE stream (e.g., to stdout). \label{ffwrhdu}
\end{description}

\begin{verbatim}
  int fits_write_hdu / ffwrhdu
      (fitsfile *infptr, FILE *stream, > int *status)
\end{verbatim}

\begin{description}
\item[8 ]  Copy the header (and not the data) from the CHDU associated with infptr
    to the CHDU associated with outfptr.  If the current output HDU
    is not completely empty, then the CHDU will be closed and a new
    HDU will be appended to the output file.   An empty output data unit
   will be created with all values initially = 0). \label{ffcphd}
\end{description}

\begin{verbatim}
  int fits_copy_header / ffcphd
      (fitsfile *infptr, fitsfile *outfptr, > int *status)
\end{verbatim}

\begin{description}
\item[9 ]  Delete the CHDU in the FITS file.  Any following HDUs will be shifted
    forward in the file, to fill in the gap created by the deleted
    HDU.  In the case of deleting the primary array (the first HDU in
    the file) then the current primary array will be replace by a null
    primary array containing the minimum set of required keywords and
    no data.  If there are more extensions in the file following the
    one that is deleted, then the the CHDU will be redefined to point
    to the following extension.  If there are no following extensions
    then the CHDU will be redefined to point to the previous HDU.  The
    output hdutype parameter returns the type of the new CHDU.  A null
    pointer may be given for
   hdutype if the returned value is not needed. \label{ffdhdu}
\end{description}

\begin{verbatim}
  int fits_delete_hdu / ffdhdu
      (fitsfile *fptr, > int *hdutype, int *status)
\end{verbatim}

\section{Header Keyword Read/Write Routines}

These routines read or write keywords in the Current Header Unit
(CHU).  Wild card characters (*, ?, or \#) may be used when specifying
the name of the keyword to be read: a '?' will match any single
character at that position in the keyword name and a '*' will match any
length (including zero) string of characters.  The '\#' character will
match any consecutive string of decimal digits (0 - 9).  When a wild
card is used the routine will only search for a match from the current
header position to the end of the header and will not resume the search
from the top of the header back to the original header position as is
done when no wildcards are included in the keyword name.  The
fits\_read\_record routine may be used to set the starting position
when doing wild card searches.  A status value of KEY\_NO\_EXIST is
returned if the specified keyword to be read is not found in the
header.


\subsection{Keyword Reading Routines}


\begin{description}
\item[1 ] Return the number of existing keywords (not counting the
    END keyword) and the amount of space currently available for more
    keywords.  It returns morekeys = -1 if the header has not yet been
    closed.  Note that CFITSIO will dynamically add space if required
    when writing new keywords to a header so in practice there is no
    limit to the number of keywords that can be added to a header.  A
    null pointer may be entered for the morekeys parameter if it's
   value is not needed. \label{ffghsp}
\end{description}

\begin{verbatim}
  int fits_get_hdrspace / ffghsp
      (fitsfile *fptr, > int *keysexist, int *morekeys, int *status)
\end{verbatim}

\begin{description}
\item[2 ] Return the specified keyword.  In the first routine,
    the datatype parameter specifies the desired returned data type of the
    keyword value and can have one of the following symbolic constant
    values:  TSTRING, TLOGICAL (== int), TBYTE, TSHORT, TUSHORT, TINT,
    TUINT, TLONG, TULONG, TLONGLONG, TFLOAT, TDOUBLE, TCOMPLEX, and TDBLCOMPLEX.
    Within the context of this routine, TSTRING corresponds to a
    'char*' data type, i.e., a pointer to a character array.  Data type
    conversion will be performed for numeric values if the keyword
    value does not have the same data type.  If the value of the keyword
    is undefined (i.e., the value field is blank) then an error status
    = VALUE\_UNDEFINED will be returned.

    The second routine returns the keyword value as a character string
    (a literal copy of what is in the value field) regardless of the
    intrinsic data type of the keyword.  The third routine returns
    the entire 80-character header record of the keyword, with any
    trailing blank characters stripped off. The fourth routine returns
    the (next) header record that contains the literal string of characters
    specified by the 'string' argument.

    If a NULL comment pointer is supplied then the comment string
   will not be returned. \label{ffgky} \label{ffgkey} \label{ffgcrd}
\end{description}

\begin{verbatim}
  int fits_read_key / ffgky
      (fitsfile *fptr, int datatype, char *keyname, > DTYPE *value,
       char *comment, int *status)

  int fits_read_keyword / ffgkey
      (fitsfile *fptr, char *keyname, > char *value, char *comment,
       int *status)

  int fits_read_card / ffgcrd
      (fitsfile *fptr, char *keyname, > char *card, int *status)

  int fits_read_str / ffgstr
      (fitsfile *fptr, char *string, > char *card, int *status)
\end{verbatim}

\begin{description}
\item[3 ] Read a string-valued keyword and return the string length, the value string,
    and/or the comment field.   The first routine, ffgksl, simply
    returns the length of the character string value of the specified keyword.
    The second routine, ffgsky, also returns up to maxchar characters
    of the keyword value string, starting with the firstchar character,
    and the keyword comment string (unless the input value of comm = NULL).
    The valuelen argument returns the total length of the keyword value string
    regardless of how much of the string is actually returned (which
    depends on the value of the firstchar and maxchar arguments).
    Note that the value character string argument must be allocated large enough to
    also hold the null terminator at the end of the returned string.  These
    routines support string keywords that use the CONTINUE convention to
    continue long string values over multiple FITS header records.  Normally,
    string-valued keywords have a maximum length of 68 characters, however,
   CONTINUE'd string keywords may be arbitrarily long. \label{ffgksl} \label{ffgsky}
\end{description}

\begin{verbatim}
  int fits_get_key_strlen / ffgksl
      (fitsfile *fptr, const char *keyname, int *length, int *status);

  int fits_read_string_key / ffgsky
       (fitsfile *fptr, const char *keyname, int firstchar, int maxchar,
       char *value, int *valuelen, char *comm, int *status);
\end{verbatim}

\begin{description}
\item[4 ] Return the nth header record in the CHU.  The first keyword
   in the header is at keynum = 1;  if keynum = 0 then these routines
   simply reset the internal CFITSIO pointer to the beginning of the header
   so that subsequent keyword operations will start at the top of the
   header (e.g., prior to searching for keywords using wild cards in
   the keyword name).   The first routine returns the entire
   80-character header record (with trailing blanks truncated),
   while the second routine parses the record and returns the name,
   value, and comment fields as separate (blank truncated)
   character strings.  If a NULL comment pointer is given on input,
   then the comment string will not be
  returned. \label{ffgrec} \label{ffgkyn}
\end{description}

\begin{verbatim}
  int fits_read_record / ffgrec
      (fitsfile *fptr, int keynum, > char *card, int *status)

  int fits_read_keyn / ffgkyn
      (fitsfile *fptr, int keynum, > char *keyname, char *value,
       char *comment, int *status)
\end{verbatim}

\begin{description}
\item[5 ] Return the next keyword whose name matches one of the strings in
    'inclist' but does not match any of the strings in 'exclist'.
    The strings in inclist and exclist may contain wild card characters
    (*, ?, and \#) as described at the beginning of this section.
    This routine searches from the current header position to the
    end of the header, only, and does not continue the search from
    the top of the header back to the original position.  The current
    header position may be reset with the ffgrec routine.  Note
    that nexc may be set = 0 if there are no keywords to be excluded.
    This routine returns status = KEY\_NO\_EXIST if a matching
   keyword is not found. \label{ffgnxk}
\end{description}

\begin{verbatim}
  int fits_find_nextkey / ffgnxk
      (fitsfile *fptr, char **inclist, int ninc, char **exclist,
       int nexc, > char *card, int  *status)
\end{verbatim}

\begin{description}
\item[6 ] Return the physical units string from an existing keyword.  This
    routine uses a local convention, shown in the following example,
    in which the keyword units are enclosed in square brackets in the
    beginning of the keyword comment field.  A null string is returned
   if no units are defined for the keyword.  \label{ffgunt}
\end{description}

\begin{verbatim}
     VELOCITY=                 12.3 / [km/s] orbital speed

  int fits_read_key_unit / ffgunt
      (fitsfile *fptr, char *keyname, > char *unit, int *status)
\end{verbatim}

\begin{description}
\item[7 ] Concatenate the header keywords in the CHDU into a single long
    string of characters.  This provides a convenient way of passing
    all or part of the header information in a FITS HDU to other subroutines.
    Each 80-character fixed-length keyword record is appended to the
    output character string, in order, with no intervening separator or
    terminating characters. The last header record is terminated with
    a NULL character.  These routine allocates memory for the returned
    character array, so the calling program must free the memory when
    finished.  The cleanest way to do this is to
    call the fits\_free\_memory routine.

    There are 2 related routines: fits\_hdr2str simply concatenates all
    the existing keywords in the header; fits\_convert\_hdr2str is similar,
    except that if the CHDU is a tile compressed image (stored in a binary
    table) then it will first convert that header back to that of the corresponding
    normal FITS image before concatenating the keywords.

    Selected keywords may be excluded from the returned character string.
    If the second parameter (nocomments) is TRUE (nonzero) then any
    COMMENT, HISTORY, or blank keywords in the header will not be copied
    to the output string.

    The 'exclist' parameter may be used to supply a list of keywords
    that are to be excluded from the output character string. Wild card
    characters (*, ?, and \#) may be used in the excluded keyword names.
    If no additional keywords are to be excluded, then set nexc = 0 and
   specify NULL for the the **exclist  parameter.  \label{ffhdr2str}
\end{description}

\begin{verbatim}
  int fits_hdr2str / ffhdr2str
      (fitsfile *fptr, int nocomments, char **exclist, int nexc,
      > char **header, int *nkeys, int *status)

  int fits_convert_hdr2str / ffcnvthdr2str
      (fitsfile *fptr, int nocomments, char **exclist, int nexc,
      > char **header, int *nkeys, int *status)

  int fits_free_memory / fffree
      (char *header, > int *status);

\end{verbatim}


\subsection{Keyword Writing Routines}


\begin{description}
\item[1 ] Write a keyword of the appropriate data type into the
    CHU.  The first routine simply appends a new keyword whereas the
    second routine will update the value and comment fields of the
    keyword if it already exists, otherwise it appends a new
    keyword.  Note that the address to the value, and not the value
    itself, must be entered.    The datatype parameter specifies the
    data type of the keyword value with one of the following values:
    TSTRING, TLOGICAL (== int), TBYTE, TSHORT, TUSHORT, TINT, TUINT,
    TLONG, TLONGLONG, TULONG, TFLOAT, TDOUBLE.  Within the context of this
    routine, TSTRING corresponds to a 'char*' data type, i.e., a pointer
    to a character array.  A null pointer may be entered for the
    comment parameter in which case the  keyword comment
   field will be unmodified or left blank.  \label{ffpky} \label{ffuky}
\end{description}

\begin{verbatim}
  int fits_write_key / ffpky
      (fitsfile *fptr, int datatype, char *keyname, DTYPE *value,
          char *comment, > int *status)

  int fits_update_key / ffuky
      (fitsfile *fptr, int datatype, char *keyname, DTYPE *value,
          char *comment, > int *status)
\end{verbatim}

\begin{description}
\item[2 ] Write a keyword with a null or undefined value (i.e., the
    value field in the keyword is left blank).  The first routine
    simply appends a new keyword whereas the second routine will update
    the value and comment fields of the keyword if it already exists,
    otherwise it appends a new keyword.  A null pointer may be
    entered for the comment parameter in which case the  keyword
    comment
   field will be unmodified or left blank. \label{ffpkyu} \label{ffukyu}
\end{description}

\begin{verbatim}
  int fits_write_key_null / ffpkyu
      (fitsfile *fptr, char *keyname, char *comment, > int *status)

  int fits_update_key_null / ffukyu
      (fitsfile *fptr, char *keyname, char *comment, > int *status)
\end{verbatim}

\begin{description}
\item[3 ] Write (append) a COMMENT or HISTORY keyword to the CHU.  The comment or
    history string will be continued over multiple keywords if it is longer
   than 70 characters. \label{ffpcom} \label{ffphis}
\end{description}

\begin{verbatim}
  int fits_write_comment / ffpcom
      (fitsfile *fptr, char *comment, > int *status)

  int fits_write_history / ffphis
      (fitsfile *fptr, char *history, > int *status)
\end{verbatim}

\begin{description}
\item[4 ] Write the DATE keyword to the CHU. The keyword value will contain
    the current system date as a character string in 'yyyy-mm-ddThh:mm:ss'
    format. If a DATE keyword already exists in the header, then this
    routine will simply update the keyword value with the current date.
   \label{ffpdat}
\end{description}

\begin{verbatim}
  int fits_write_date / ffpdat
      (fitsfile *fptr, > int *status)
\end{verbatim}

\begin{description}
\item[5 ]Write a user specified keyword record into the CHU.  This is
   a low--level routine which can be used to write any arbitrary
   record into the header.  The record must conform to the all
  the FITS format requirements. \label{ffprec}
\end{description}

\begin{verbatim}
  int fits_write_record / ffprec
      (fitsfile *fptr, char *card, > int *status)
\end{verbatim}

\begin{description}
\item[6 ]Update an 80-character record in the CHU.  If a keyword with the input
   name already exists, then it is overwritten by the value of card.  This
   could modify the keyword name as well as the value and comment fields.
   If the keyword doesn't already exist then a new keyword card is appended
  to the header. \label{ffucrd}
\end{description}

\begin{verbatim}
  int fits_update_card / ffucrd
      (fitsfile *fptr, char *keyname, char *card, > int *status)
\end{verbatim}


\begin{description}
\item[7 ] Modify (overwrite) the comment field of an existing keyword. \label{ffmcom}
\end{description}

\begin{verbatim}
  int fits_modify_comment / ffmcom
      (fitsfile *fptr, char *keyname, char *comment, > int *status)
\end{verbatim}


\begin{description}
\item[8 ] Write the physical units string into an existing keyword.  This
    routine uses a local convention, shown in the following example,
    in which the keyword units are enclosed in square brackets in the
   beginning of the keyword comment field.  \label{ffpunt}
\end{description}

\begin{verbatim}
     VELOCITY=                 12.3 / [km/s] orbital speed

  int fits_write_key_unit / ffpunt
      (fitsfile *fptr, char *keyname, char *unit, > int *status)
\end{verbatim}

\begin{description}
\item[9 ] Rename an existing keyword, preserving the current value
   and comment fields. \label{ffmnam}
\end{description}

\begin{verbatim}
  int fits_modify_name / ffmnam
      (fitsfile *fptr, char *oldname, char *newname, > int *status)
\end{verbatim}

\begin{description}
\item[10]  Delete a keyword record.  The space occupied by
    the keyword is reclaimed by moving all the following header records up
    one row in the header.  The first routine deletes a keyword at a
    specified position in the header (the first keyword is at position 1),
    whereas the second routine deletes a specifically named keyword.
    Wild card characters may be used when specifying the name of the keyword
    to be deleted. The third routine deletes the (next) keyword that contains
    the literal character string specified by the 'string'
   argument.\label{ffdrec} \label{ffdkey}
\end{description}

\begin{verbatim}
  int fits_delete_record / ffdrec
      (fitsfile *fptr, int   keynum,  > int *status)

  int fits_delete_key / ffdkey
      (fitsfile *fptr, char *keyname, > int *status)

  int fits_delete_str / ffdstr
      (fitsfile *fptr, char *string, > int *status)
\end{verbatim}

\section{Primary Array or IMAGE Extension I/O Routines}

These routines read or write data values in the primary data array (i.e.,
the first HDU in a FITS file) or an IMAGE extension.   There are also
routines to get information about the data type and size of the image.
Users should also read the following chapter on the CFITSIO iterator
function which provides a more `object oriented' method of reading and
writing images.  The iterator function is a little more complicated to
use, but the advantages are that it usually takes less code to perform
the same operation, and the resulting program often runs faster because
the FITS files are read and written using the most efficient block size.

C programmers should note that the ordering of arrays in FITS files, and
hence in all the CFITSIO calls, is more similar to the dimensionality
of arrays in Fortran rather than C.  For instance if a FITS image has
NAXIS1 = 100 and NAXIS2 = 50, then a 2-D array just large enough to hold
the image should be declared as array[50][100] and not as array[100][50].

The `datatype'  parameter specifies the data type of the `nulval'  and
`array' pointers and can have one of the following  values:  TBYTE,
TSBYTE, TSHORT, TUSHORT, TINT, TUINT, TLONG, TLONGLONG, TULONG, TULONGLONG, TFLOAT,
TDOUBLE.  Automatic data type conversion is performed if the data type
of the FITS array (as defined by the BITPIX keyword) differs from that
specified by 'datatype'.  The data values are also automatically scaled
by the BSCALE and BZERO keyword values as they are being read or written
in the FITS array.


\begin{description}
\item[1 ] Get the data type or equivalent data type of the image.  The
    first routine returns the physical data type of the FITS image, as
    given by the BITPIX keyword, with allowed values of BYTE\_IMG (8),
    SHORT\_IMG (16), LONG\_IMG (32), LONGLONG\_IMG (64),
    FLOAT\_IMG (-32), and DOUBLE\_IMG
    (-64).
    The second routine is similar, except that if the image pixel
    values are scaled, with non-default values for the BZERO and BSCALE
    keywords, then the routine will return the 'equivalent' data type
    that is needed to store the scaled values.  For example, if BITPIX
    = 16 and BSCALE = 0.1 then the equivalent data type is FLOAT\_IMG.
    Similarly if BITPIX = 16, BSCALE = 1, and BZERO = 32768, then the
    the pixel values span the range of an unsigned short integer and
   the returned data type will be USHORT\_IMG. \label{ffgidt}
\end{description}

\begin{verbatim}
  int fits_get_img_type / ffgidt
      (fitsfile *fptr, > int *bitpix, int *status)

  int fits_get_img_equivtype / ffgiet
      (fitsfile *fptr, > int *bitpix, int *status)
\end{verbatim}

\begin{description}
\item[2 ] Get the number of dimensions, and/or the size of
    each dimension in the image .  The number of axes in the image is
    given by naxis, and the size of each dimension is given by the
    naxes array (a maximum of maxdim dimensions will be returned).
   \label{ffgidm} \label{ffgisz} \label{ffgipr}
\end{description}

\begin{verbatim}
  int fits_get_img_dim / ffgidm
      (fitsfile *fptr, > int *naxis, int *status)

  int fits_get_img_size / ffgisz
      (fitsfile *fptr, int maxdim, > long *naxes, int *status)

  int fits_get_img_sizell / ffgiszll
      (fitsfile *fptr, int maxdim, > LONGLONG *naxes, int *status)

  int fits_get_img_param / ffgipr
      (fitsfile *fptr, int maxdim, > int *bitpix, int *naxis, long *naxes,
       int *status)

  int fits_get_img_paramll / ffgiprll
      (fitsfile *fptr, int maxdim, > int *bitpix, int *naxis, LONGLONG *naxes,
       int *status)
\end{verbatim}

\begin{description}
\item[3 ]Create a new primary array or IMAGE extension with a specified
   data type and size.  If the FITS file is currently empty then a
   primary array is created, otherwise a new IMAGE extension is
  appended to the file. \label{ffcrim}
\end{description}

\begin{verbatim}
  int fits_create_img / ffcrim
      ( fitsfile *fptr, int bitpix, int naxis, long *naxes, > int *status)

  int fits_create_imgll / ffcrimll
      ( fitsfile *fptr, int bitpix, int naxis, LONGLONG *naxes, > int *status)
\end{verbatim}

\begin{description}
\item[4 ] Copy an n-dimensional image in a particular row and column of a
    binary table (in a vector column)
    to or from a primary array or image extension.

    The 'cell2image' routine
    will append a new image extension (or primary array) to the output file.
    Any WCS keywords associated with the input column image will be translated
    into the appropriate form for an image extension.  Any other keywords
    in the table header that are not specifically related to defining the
    binary table structure or to other columns in the table
    will also be copied to the header of the output image.

    The 'image2cell' routine will copy the input image into the specified row
    and column of the current binary table in the output file.  The binary table
    HDU must exist before calling this routine, but it
    may be empty, with no rows or columns of data.  The specified column
    (and row) will be created if it does not already exist.  The 'copykeyflag'
    parameter controls which keywords are copied from the input
    image to the header of the output table: 0 = no keywords will be copied,
    1 = all keywords will be copied (except those keywords that would be invalid in
   the table header), and 2 = copy only the WCS keywords. \label{copycell}
\end{description}

\begin{verbatim}
  int fits_copy_cell2image
      (fitsfile *infptr, fitsfile *outfptr, char *colname, long rownum,
       > int *status)

  int fits_copy_image2cell
      (fitsfile *infptr, fitsfile *outfptr, char *colname, long rownum,
       int copykeyflag > int *status)
\end{verbatim}


\begin{description}
\item[5 ] Write a rectangular subimage (or the whole image) to the FITS data
    array.  The fpixel and lpixel arrays give the coordinates of the
    first (lower left corner) and last (upper right corner) pixels in
   FITS image to be written to.  \label{ffpss}
\end{description}

\begin{verbatim}
  int fits_write_subset / ffpss
      (fitsfile *fptr, int datatype, long *fpixel, long *lpixel,
       DTYPE *array, > int *status)
\end{verbatim}

\begin{description}
\item[6 ] Write pixels into the FITS data array.  'fpixel' is an array of
   length NAXIS which gives the coordinate of the starting pixel to be
   written to, such that fpixel[0] is in the range 1 to NAXIS1,
   fpixel[1] is in the range 1 to NAXIS2, etc.  The first pair of routines
   simply writes the array of pixels to the FITS file (doing data type
   conversion if necessary) whereas the second routines will substitute
   the  appropriate FITS null value for any elements which are equal to
   the input value of nulval (note that this parameter gives the
   address of the null value, not the null value itself).  For integer
   FITS arrays, the FITS null value is defined by the BLANK keyword (an
   error is returned if the BLANK keyword doesn't exist).  For floating
   point FITS arrays  the special IEEE NaN (Not-a-Number) value will be
   written into the FITS file.  If a null pointer is entered for
   nulval, then the null value is ignored and this routine behaves
  the same as fits\_write\_pix.   \label{ffppx} \label{ffppxn}
\end{description}

\begin{verbatim}
  int fits_write_pix / ffppx
      (fitsfile *fptr, int datatype, long *fpixel, LONGLONG nelements,
       DTYPE *array, int *status);

  int fits_write_pixll / ffppxll
      (fitsfile *fptr, int datatype, LONGLONG *fpixel, LONGLONG nelements,
       DTYPE *array, int *status);

  int fits_write_pixnull / ffppxn
      (fitsfile *fptr, int datatype, long *fpixel, LONGLONG nelements,
       DTYPE *array, DTYPE *nulval, > int *status);

  int fits_write_pixnullll / ffppxnll
      (fitsfile *fptr, int datatype, LONGLONG *fpixel, LONGLONG nelements,
       DTYPE *array, DTYPE *nulval, > int *status);
\end{verbatim}

\begin{description}
\item[7 ] Set FITS data array elements equal to the appropriate null pixel
    value. For integer FITS arrays, the FITS null value is defined by
    the BLANK keyword  (an error is returned if the BLANK keyword
    doesn't exist). For floating point FITS arrays the special IEEE NaN
    (Not-a-Number) value will be written into the FITS file.  Note that
    'firstelem' is a scalar giving the  offset to the first pixel to be
    written in the equivalent 1-dimensional array of image pixels. \label{ffpprn}
\end{description}

\begin{verbatim}
  int fits_write_null_img / ffpprn
      (fitsfile *fptr, LONGLONG firstelem, LONGLONG nelements, > int *status)
\end{verbatim}

\begin{description}
\item[8 ] Read a rectangular subimage (or the whole image) from the FITS
    data array.  The fpixel and lpixel arrays give the coordinates of
    the first (lower left corner) and last (upper right corner) pixels
    to be read from the FITS image. Undefined FITS array elements will
    be returned with a value = *nullval, (note that this parameter
    gives the address of the null value, not the null value itself)
    unless nulval = 0 or *nulval = 0, in which case no checks for
   undefined pixels will be performed.  \label{ffgsv}
\end{description}

\begin{verbatim}
  int fits_read_subset / ffgsv
      (fitsfile *fptr, int  datatype, long *fpixel, long *lpixel, long *inc,
       DTYPE *nulval, > DTYPE *array, int *anynul, int *status)
\end{verbatim}

\begin{description}
\item[9 ] Read pixels from the FITS data array.  'fpixel' is the starting
    pixel location and is an array of length NAXIS such that fpixel[0]
    is in the range 1 to NAXIS1, fpixel[1] is in the range 1 to NAXIS2,
    etc. The nelements parameter specifies the number of pixels to
    read.  If fpixel is set to the first pixel, and nelements is set
    equal to the NAXIS1 value, then this routine would read the first
    row of the image.  Alternatively, if nelements is set equal to
    NAXIS1 * NAXIS2 then it would read an entire 2D image, or the first
    plane of a 3-D datacube.

    The first 2 routines will return any undefined pixels in the FITS array
    equal to the value of *nullval (note that this parameter gives the
    address of the null value, not the null value itself) unless nulval
    = 0 or *nulval = 0, in which case no checks for undefined pixels
    will be performed.  The second 2 routines are similar except that any
    undefined pixels will have the corresponding nullarray element set
   equal to TRUE (= 1).  \label{ffgpxv}  \label{ffgpxf}
\end{description}

\begin{verbatim}
  int fits_read_pix / ffgpxv
      (fitsfile *fptr, int  datatype, long *fpixel, LONGLONG nelements,
       DTYPE *nulval, > DTYPE *array, int *anynul, int *status)

  int fits_read_pixll / ffgpxvll
      (fitsfile *fptr, int  datatype, LONGLONG *fpixel, LONGLONG nelements,
       DTYPE *nulval, > DTYPE *array, int *anynul, int *status)

  int fits_read_pixnull / ffgpxf
      (fitsfile *fptr, int  datatype, long *fpixel, LONGLONG nelements,
       > DTYPE *array, char *nullarray, int *anynul, int *status)

  int fits_read_pixnullll / ffgpxfll
      (fitsfile *fptr, int  datatype, LONGLONG *fpixel, LONGLONG nelements,
       > DTYPE *array, char *nullarray, int *anynul, int *status)
\end{verbatim}

\begin{description}
\item[10]  Copy a rectangular section of an image and write it to a new
     FITS primary image or image extension.  The new image HDU is appended
     to the end of the output file; all the keywords in the input image
     will be copied to the output image.  The common WCS keywords will
     be updated if necessary to correspond to the coordinates of the section.
     The format of the section expression is
     same as specifying an image section using the extended file name
     syntax (see "Image Section" in Chapter 10).
     (Examples:  "1:100,1:200", "1:100:2, 1:*:2", "*, -*").
    \label{ffcpimg}
\end{description}

\begin{verbatim}
  int fits_copy_image_section / ffcpimg
      (fitsfile *infptr, fitsfile *outfptr, char *section, int *status)
\end{verbatim}


\section{Image Compression}

CFITSIO transparently supports the 2 methods of image compression described
below.

1)  The entire FITS file may be externally compressed with the gzip or Unix
compress utility programs, producing a *.gz or *.Z file, respectively. When reading
compressed files of this type, CFITSIO first uncompresses the entire file
into memory before performing the requested read operations.  Output files
can be directly written in the gzip compressed format if the user-specified
filename ends with `.gz'.  In this case, CFITSIO initially writes the
uncompressed file in memory and then compresses it and writes it to disk
when the FITS file is closed, thus saving user disk space. Read and write
access to these compressed FITS files is generally quite fast since all the
I/O is performed in memory; the main limitation with this technique is that
there must be enough available memory (or swap space) to hold the entire
uncompressed FITS file.

2) CFITSIO also supports the FITS tiled image compression convention in
which the image is subdivided into a grid of rectangular tiles, and each
tile of pixels is individually compressed.   The details of this FITS
compression  convention are described at the FITS Support Office web site at
http://fits.gsfc.nasa.gov/fits\_registry.html, and in the fpackguide pdf file
that is included with the CFITSIO source file distributions Basically, the compressed
image tiles are stored in rows of a variable length array column in a FITS
binary table, however CFITSIO recognizes that this binary table extension
contains an image and treats it as if it were an IMAGE extension.  This
tile-compressed format is especially well suited for compressing very large
images because a) the FITS header keywords remain uncompressed for rapid
read access, and because b) it is possible to extract and uncompress
sections of the image without having to uncompress the entire image. This
format is also much more effective in compressing floating point images
than simply compressing the image using gzip or compress because it
approximates the floating point values with scaled integers which can then
be compressed more efficiently.

Currently CFITSIO supports 3 general purpose compression algorithms  plus
one other special-purpose compression technique that is designed for data
masks with positive integer pixel values. The 3 general purpose algorithms
are GZIP, Rice, and HCOMPRESS, and the special purpose  algorithm is the
IRAF pixel list compression technique (PLIO).  There are 2 variants of the GZIP
algorithm: GZIP\_1 compresses the array of image pixel value normally with the
GZIP algorithm, while GZIP\_2 first shuffles the bytes in all the pixel
values so that the most-significant byte of every pixel appears first, followed
by the less significant bytes in sequence.  GZIP\_2 may be more effective
in cases where the most significant byte in most of the image pixel values
contains the same bit pattern. In principle, any number of
other compression algorithms could also be supported  by the FITS tiled
image compression convention.

The FITS image can be subdivided into any desired rectangular grid of
compression tiles.  With the GZIP, Rice,  and PLIO algorithms, the default
is to take each row of the image as a tile.  The HCOMPRESS algorithm is
inherently 2-dimensional in nature, so the default in this case is to take
16 rows of the image per tile. In most cases it makes little difference what
tiling pattern is used, so the default tiles are usually adequate.  In the
case of very small images, it could be more efficient to compress the whole
image as a single tile. Note that the image dimensions are not required to
be an integer multiple of the tile dimensions; if not, then the tiles at the
edges of the image will be smaller than the other tiles.

The 4 supported image compression algorithms are all 'loss-less' when
applied to integer FITS images;  the pixel values are preserved exactly with
no loss of information during the compression and uncompression process.  In
addition, the HCOMPRESS algorithm supports a 'lossy' compression mode that
will produce
larger amount of image compression.  This is achieved by specifying a non-zero
value for the HCOMPRESS ``scale''
parameter.  Since the amount of compression that is achieved depends directly
on the RMS noise in the image, it is usually more convention
to specify the HCOMPRESS scale factor relative to the RMS noise.
Setting s = 2.5 means use a scale factor that is 2.5 times the calculated RMS noise
in the image tile.   In some cases
it may be desirable to specify the exact scaling to be used,
instead of specifying it relative to the calculated noise value.  This may
be done by specifying the negative of desired scale value (typically
in the range -2 to -100).

Very high compression factors (of 100 or more) can be
achieved by using large HCOMPRESS scale values, however, this can produce undesirable
``blocky'' artifacts in the compressed image.  A variation of the HCOMPRESS
algorithm (called HSCOMPRESS) can be used in this case to apply a small
amount of smoothing of the image when it is uncompressed to help cover up
these artifacts.  This smoothing is purely cosmetic and does not cause any
significant change to the image pixel values.

Floating point FITS images (which have BITPIX = -32 or -64) usually contain
too much ``noise'' in the least significant bits of the mantissa of the
pixel values  to be effectively compressed with any lossless algorithm.
Consequently, floating point images are first quantized into scaled integer
pixel values (and thus throwing away much of the noise) before being
compressed with the specified algorithm (either GZIP, Rice, or HCOMPRESS).
This technique produces much higher compression factors than
simply using the GZIP utility to externally compress the whole FITS file,  but it also
means that the original floating value pixel values are not exactly
preserved. When done properly, this  integer scaling technique will only
discard the insignificant noise while still preserving all the real
information in the image.  The amount of precision that is retained in the
pixel values is controlled by the "quantization level" parameter, q.  Larger
values of q will result in compressed images whose pixels more closely match
the floating point pixel values, but at the same time the amount of
compression that is achieved will be reduced.  Users should experiment with
different values for  this parameter to determine the optimal value that
preserves all the useful information in the image, without needlessly
preserving all the ``noise'' which will hurt the compression efficiency.

The default value for the quantization scale factor is 4.0, which means that
scaled integer pixel values will be quantized such that the difference
between adjacent integer values will be 1/4th of the noise level in the
image background. CFITSIO uses an optimized algorithm to accurately estimate
the noise in the image.  As an example, if the RMS noise in the background
pixels of an  image = 32.0,  then the spacing between adjacent scaled
integer pixel values  will equal 8.0 by default.  Note that the RMS noise is
independently calculated for each tile of the image, so the resulting
integer scaling factor may fluctuate slightly for each tile.   In some cases
it may be desirable to specify the exact quantization level to be used,
instead of specifying it relative to the calculated noise value.  This may
be done by specifying the negative of desired quantization level for the
value of q.  In the previous example, one could specify q = -8.0 so that the
quantized integer levels differ by exactly 8.0.  Larger negative values for q means
that the levels are more coarsely spaced, and will produce higher
compression factors.

When floating point images are being quantized, one must also specify what
quantization method is to be used.  The default algorithm is called
``SUBTRACTIVE\_DITHER\_1''.  A second variation called ``SUBTRACTIVE\_DITHER\_2''
is also available, which does the same thing except that any pixels with a
value of 0.0 are not dithered and instead the zero values are exactly
preserved in the compressed image.  One may also turn off dithering
completely with the ``NO\_DITHER'' option, but this is not recommended
because it can cause larger systematic errors in measurements of the
position or brightness of objects in the compressed image.

There are 3 methods for specifying all the parameters needed to write a FITS
image in the tile compressed format.  The parameters may either be specified
at run time as part of the file name of the output compressed FITS file, or
the writing program may call a set of helper CFITSIO subroutines that are provided
for specifying the parameter values, or ``compression directive'' keywords
may be added to the header of each image HDU to specify the compression parameters.
These 3 methods are described below.

1)  At run time, when specifying the name of the output FITS file to be
created, the user can indicate that images should be
written in tile-compressed format by enclosing the compression
parameters in square brackets following the root disk file name
in the following format:

\begin{verbatim}
    [compress NAME T1,T2; q[z] QLEVEL, s HSCALE]
\end{verbatim}
where

\begin{verbatim}
    NAME   = algorithm name:  GZIP, Rice, HCOMPRESS, HSCOMPRSS or PLIO
             may be abbreviated to the first letter (or HS for HSCOMPRESS)
    T1,T2  = tile dimension (e.g. 100,100 for square tiles 100 pixels wide)
    QLEVEL = quantization level for floating point FITS images
    HSCALE = HCOMPRESS scale factor; default = 0 which is lossless.
\end{verbatim}

Here are a few examples of this extended syntax:


\begin{verbatim}
    myfile.fit[compress]    - use the default compression algorithm (Rice)
                              and the default tile size (row by row)

    myfile.fit[compress G] - use the specified compression algorithm;
    myfile.fit[compress R]     only the first letter of the algorithm
    myfile.fit[compress P]     should be given.
    myfile.fit[compress H]

    myfile.fit[compress R 100,100]   - use Rice and 100 x 100 pixel tiles

    myfile.fit[compress R; q 10.0] - quantization level = (RMS-noise) / 10.
    myfile.fit[compress R; qz 10.0] - quantization level = (RMS-noise) / 10.
                      also use the SUBTRACTIVE_DITHER_2 quantization method
    myfile.fit[compress HS; s 2.0]  -  HSCOMPRESS (with smoothing)
                                          and scale = 2.0 * RMS-noise
\end{verbatim}

2)  Before calling the CFITSIO routine to write the image header
keywords (e.g., fits\_create\_image) the programmer can call the
routines described below to specify the compression algorithm and the
tiling pattern that is to be used.  There are routines for specifying
the various compression parameters and similar routines to
return the current values of the parameters:
\label{ffsetcomp}  \label{ffgetcomp}

\begin{verbatim}
  int fits_set_compression_type(fitsfile *fptr, int comptype, int *status)
  int fits_set_tile_dim(fitsfile *fptr, int ndim, long *tilesize, int *status)
  int fits_set_quantize_level(fitsfile *fptr, float qlevel, int *status)
  int fits_set_quantize_method(fitsfile *fptr, int method, int *status)
  int fits_set_quantize_dither(fitsfile *fptr, int dither, int *status)
  int fits_set_dither_seed(fitsfile *fptr, int seed, int *status)
  int fits_set_dither_offset(fitsfile *fptr, int offset, int *status)
  int fits_set_lossy_int(fitsfile *fptr, int lossy_int, int *status)
      this forces integer image to be converted to floats, then quantized
  int fits_set_huge_hdu(fitsfile *fptr, int huge, int *status);
      this should be called when the compressed image size is more than 4 GB.
  int fits_set_hcomp_scale(fitsfile *fptr, float scale, int *status)
  int fits_set_hcomp_smooth(fitsfile *fptr, int smooth, int *status)
              Set smooth = 1 to apply smoothing when uncompressing the image

  int fits_get_compression_type(fitsfile *fptr, int *comptype, int *status)
  int fits_get_tile_dim(fitsfile *fptr, int ndim, long *tilesize, int *status)
  int fits_get_quantize_level(fitsfile *fptr, float *level, int *status)
  int fits_get_hcomp_scale(fitsfile *fptr, float *scale, int *status)
\end{verbatim}
Several symbolic constants are defined for use as the value of the
`comptype' parameter:  GZIP\_1, GZIP\_2, RICE\_1, HCOMPRESS\_1 or PLIO\_1.
Entering NULL for comptype will turn off the tile-compression and cause normal FITS
images to be written.

There are also defined symbolic constants for the quantization method:
``SUBTRACTIVE\_DITHER\_1'', ``SUBTRACTIVE\_DITHER\_2'', and ``NO\_DITHER''.

3) CFITSIO will uses the values of the following keywords, if they are present
in the header of the image HDU,
to determine how to compress that HDU.  These keywords override any compression parameters
that were specified with the previous 2 methods.

\begin{verbatim}
  FZALGOR  - 'RICE_1' , 'GZIP_1', 'GZIP_2', 'HCOMPRESS_1', 'PLIO_1', 'NONE'
  FZTILE   - 'ROW', 'WHOLE', or '(n,m)'
  FZQVALUE - float value (default = 4.0)
  FZQMETHD - 'SUBTRACTIVE_DITHER_1', 'SUBTRACTIVE_DITHER_2', 'NO_DITHER'
  FZDTHRSD - 'CLOCK', 'CHECKSUM', 1 - 10000
  FZINT2F  -  T, or F:  Convert integers to floats, then quantize?
  FZHSCALE - float value (default = 0).  Hcompress scale value.
\end{verbatim}

No special action is required by software when read tile-compressed images because
all the CFITSIO routines that read normal uncompressed FITS images also
transparently read images in the tile-compressed format;  CFITSIO essentially
treats the binary table that contains the compressed tiles as if
it were an IMAGE extension.


The following 2 routines are available for compressing or
or decompressing an image:

\begin{verbatim}
  int fits_img_compress(fitsfile *infptr, fitsfile *outfptr, int *status);
  int fits_img_decompress (fitsfile *infptr, fitsfile *outfptr, int *status);
\end{verbatim}
Before calling the compression routine, the compression parameters must
first be defined in one of the 3 way described in the previous paragraphs.
There is also a routine to determine if the current HDU contains
a tile compressed image (it returns 1 or 0):

\begin{verbatim}
  int fits_is_compressed_image(fitsfile *fptr, int *status);
\end{verbatim}
A small example program called 'imcopy' is included with CFITSIO that
can be used to compress (or uncompress) any FITS image.  This
program can be used to experiment with the various compression options
on existing FITS images as shown in these examples:

\begin{verbatim}
1)  imcopy infile.fit 'outfile.fit[compress]'

       This will use the default compression algorithm (Rice) and the
       default tile size (row by row)

2)  imcopy infile.fit 'outfile.fit[compress GZIP]'

       This will use the GZIP compression algorithm and the default
       tile size (row by row).  The allowed compression algorithms are
       Rice, GZIP, and PLIO.  Only the first letter of the algorithm
       name needs to be specified.

3)  imcopy infile.fit 'outfile.fit[compress G 100,100]'

       This will use the GZIP compression algorithm and 100 X 100 pixel
       tiles.

4)  imcopy infile.fit 'outfile.fit[compress R 100,100; qz 10.0]'

       This will use the Rice compression algorithm, 100 X 100 pixel
       tiles, and quantization level = RMSnoise / 10.0 (assuming the
       input image has a floating point data type). By specifying
       qz instead of q, this means use the subtractive dither2
       quantization method.

5)  imcopy infile.fit outfile.fit

       If the input file is in tile-compressed format, then it will be
       uncompressed to the output file.  Otherwise, it simply copies
       the input image to the output image.

6)  imcopy 'infile.fit[1001:1500,2001:2500]'  outfile.fit

       This extracts a 500 X 500 pixel section of the much larger
       input image (which may be in tile-compressed format).  The
       output is a normal uncompressed FITS image.

7)  imcopy 'infile.fit[1001:1500,2001:2500]'  outfile.fit.gz

       Same as above, except the output file is externally compressed
       using the gzip algorithm.

\end{verbatim}

\section{ASCII and Binary Table Routines}

These routines perform read and write operations on columns of data in
FITS ASCII or Binary tables.  Note that in the following discussions,
the first row and column in a table is at position 1 not 0.

Users should also read the following chapter on the CFITSIO iterator
function which provides a more `object oriented' method of reading and
writing table columns.  The iterator function is a little more
complicated to use, but the advantages are that it usually takes less
code to perform the same operation, and the resulting program often
runs faster because the FITS files are read and written using the most
efficient block size.


\subsection{Create New Table}


\begin{description}
\item[1 ]Create a new ASCII or bintable table extension. If
   the FITS file is currently empty then a dummy primary array will be
   created before appending the table extension to it.  The tbltype
   parameter defines the type of table and can have values of
   ASCII\_TBL or BINARY\_TBL.  The naxis2 parameter gives the initial
   number of rows to be created in the table, and should normally be
   set = 0.  CFITSIO will automatically increase the size of the table
   as additional rows are written.  A non-zero number of rows may be
   specified to reserve space for that many rows, even if a fewer
   number of rows will be written.  The tunit and extname parameters
   are optional and a null pointer may be given if they are not
   defined.  The FITS Standard recommends that only letters, digits,
   and the underscore character be used in column names (the ttype
   parameter) with no embedded spaces.  Trailing blank characters are
   not significant.   \label{ffcrtb}
\end{description}

\begin{verbatim}
  int fits_create_tbl / ffcrtb
      (fitsfile *fptr, int tbltype, LONGLONG naxis2, int tfields, char *ttype[],
       char *tform[], char *tunit[], char *extname, int *status)
\end{verbatim}

\subsection{Column Information Routines}


\begin{description}
\item[1 ]  Get the number of rows or columns in the current FITS table.
     The number of rows is given by the NAXIS2 keyword and the
     number of columns is given by the TFIELDS keyword in the header
    of the table. \label{ffgnrw}
\end{description}

\begin{verbatim}
  int fits_get_num_rows / ffgnrw
      (fitsfile *fptr, > long *nrows, int *status);

  int fits_get_num_rowsll / ffgnrwll
      (fitsfile *fptr, > LONGLONG *nrows, int *status);

  int fits_get_num_cols / ffgncl
      (fitsfile *fptr, > int *ncols, int *status);
\end{verbatim}


\begin{description}
\item[2 ] Get the table column number (and name) of the column whose name
matches an input template name.   If casesen  = CASESEN then the column
name match will be case-sensitive, whereas if casesen = CASEINSEN then
the case will be ignored.  As a general rule, the column names should
be treated as case INsensitive.

The input column name template may be either the exact name of the
column to be searched for, or it may contain wild card characters (*,
?, or \#), or it may contain the integer number of the desired column
(with the first column = 1).  The `*' wild card character matches any
sequence of characters (including zero characters) and the `?'
character matches any single character.  The \# wildcard will match any
consecutive string of decimal digits (0-9).  If more than one column
name in the table matches the template string, then the first match is
returned and the status value will be set to COL\_NOT\_UNIQUE  as a
warning that a unique match was not found.  To find the other cases
that match the template, call the routine again leaving the input
status value equal to COL\_NOT\_UNIQUE and the next matching name will
then be returned.  Repeat this process until a status =
COL\_NOT\_FOUND  is returned.

The FITS Standard recommends that only letters, digits, and the
underscore character be used in column names (with no embedded
spaces).  Trailing blank characters are not significant.
  \label{ffgcno} \label{ffgcnn}
\end{description}

\begin{verbatim}
  int fits_get_colnum / ffgcno
      (fitsfile *fptr, int casesen, char *templt, > int *colnum,
       int *status)

  int fits_get_colname / ffgcnn
      (fitsfile *fptr, int casesen, char *templt, > char *colname,
       int *colnum, int *status)
\end{verbatim}

\begin{description}
\item[3 ] Return the data type, vector repeat value, and the width in bytes
    of a column in an ASCII or binary table.  Allowed values for the
    data type in ASCII tables are:  TSTRING, TSHORT, TLONG, TFLOAT, and
    TDOUBLE.  Binary tables also support these types: TLOGICAL, TBIT,
    TBYTE, TLONGLONG, TCOMPLEX and TDBLCOMPLEX.  The negative of the data type code
    value is returned if it is a variable length array column.  Note
    that in the case of a 'J' 32-bit integer binary table column, this
    routine will return data type = TINT32BIT (which in fact is
    equivalent to TLONG).  With most current C compilers, a value in a
    'J' column has the same size as an 'int' variable, and may not be
    equivalent to a 'long' variable, which is 64-bits long on an
    increasing number of compilers.

    The 'repeat' parameter returns the vector repeat count on the binary
    table TFORMn keyword value. (ASCII table columns always have repeat
    = 1).  The 'width' parameter returns the width in bytes of a single
    column element (e.g., a '10D' binary table column will have width =
    8, an ASCII table 'F12.2' column will have width = 12, and a binary
    table'60A' character string  column will have width = 60);  Note that
    CFITSIO supports the local convention for specifying arrays of
    fixed length strings within a binary table character column using
    the syntax TFORM = 'rAw' where 'r' is the total number of characters
    (= the width of the column) and 'w' is the width of a unit string
    within the column.  Thus if the column has TFORM = '60A12' then this
    means that each row of the table contains 5 12-character substrings
    within the 60-character field, and thus in this case this routine will
    return typecode = TSTRING, repeat = 60, and width = 12.  (The TDIMn
    keyword may also be used to specify the unit string length; The pair
    of keywords TFORMn = '60A' and TDIMn = '(12,5)'  would have the
    same effect as TFORMn = '60A12').  The number
    of substrings in any binary table character string field can be
    calculated by (repeat/width).  A null pointer may be given for any of
    the output parameters that are not needed.

   The second routine, fit\_get\_eqcoltype is similar except that in
   the case of scaled integer columns it returns the 'equivalent' data
   type that is needed to store the scaled values, and not necessarily
   the physical data type of the unscaled values as stored in the FITS
   table.  For example if a '1I' column in a binary table has TSCALn =
   1 and TZEROn = 32768, then this column effectively contains unsigned
   short integer values, and thus the returned value of typecode will
   be TUSHORT, not TSHORT.  Similarly, if a column has TTYPEn = '1I'
   and TSCALn = 0.12, then the returned typecode
  will be TFLOAT. \label{ffgtcl}
\end{description}

\begin{verbatim}
  int fits_get_coltype / ffgtcl
      (fitsfile *fptr, int colnum, > int *typecode, long *repeat,
       long *width, int *status)

  int fits_get_coltypell / ffgtclll
      (fitsfile *fptr, int colnum, > int *typecode, LONGLONG *repeat,
       LONGLONG *width, int *status)

  int fits_get_eqcoltype / ffeqty
      (fitsfile *fptr, int colnum, > int *typecode, long *repeat,
       long *width, int *status)

  int fits_get_eqcoltypell / ffeqtyll
      (fitsfile *fptr, int colnum, > int *typecode, LONGLONG *repeat,
       LONGLONG *width, int *status)
\end{verbatim}

\begin{description}
\item[4 ] Return the display width of a column.  This is the length
    of the string that will be returned by the fits\_read\_col routine
    when reading the column as a formatted string.  The display width is
    determined by the TDISPn keyword, if present, otherwise by the data
   type of the column. \label{ffgcdw}
\end{description}

\begin{verbatim}
  int fits_get_col_display_width / ffgcdw
      (fitsfile *fptr, int colnum, > int *dispwidth, int *status)
\end{verbatim}


\begin{description}
\item[5 ] Return the number of and size of the dimensions of a table column in
    a binary table. Normally this information is given by the TDIMn keyword,
    but if this keyword is not present then this routine returns naxis = 1
   and naxes[0] equal to the repeat count in the TFORM keyword. \label{ffgtdm}
\end{description}

\begin{verbatim}
  int fits_read_tdim / ffgtdm
      (fitsfile *fptr, int colnum, int maxdim, > int *naxis,
       long *naxes, int *status)

  int fits_read_tdimll / ffgtdmll
      (fitsfile *fptr, int colnum, int maxdim, > int *naxis,
       LONGLONG *naxes, int *status)
\end{verbatim}

\begin{description}
\item[6 ] Decode the input TDIMn keyword string (e.g. '(100,200)') and return the
    number of and size of the dimensions of a binary table column. If the input
    tdimstr character string is null, then this routine returns naxis = 1
    and naxes[0] equal to the repeat count in the TFORM keyword. This routine
   is called by fits\_read\_tdim.  \label{ffdtdm}
\end{description}

\begin{verbatim}
  int fits_decode_tdim / ffdtdm
      (fitsfile *fptr, char *tdimstr, int colnum, int maxdim, > int *naxis,
       long *naxes, int *status)

  int fits_decode_tdimll / ffdtdmll
      (fitsfile *fptr, char *tdimstr, int colnum, int maxdim, > int *naxis,
       LONGLONG *naxes, int *status)
\end{verbatim}

\begin{description}
\item[7 ] Write a TDIMn keyword whose value has the form '(l,m,n...)'
    where l, m, n... are the dimensions of a multidimensional array
   column in a binary table. \label{ffptdm}
\end{description}

\begin{verbatim}
  int fits_write_tdim / ffptdm
      (fitsfile *fptr, int colnum, int naxis, long *naxes, > int *status)

  int fits_write_tdimll / ffptdmll
      (fitsfile *fptr, int colnum, int naxis, LONGLONG *naxes, > int *status)
\end{verbatim}


\subsection{Routines to Edit Rows or Columns}


\begin{description}
\item[1 ] Insert or delete rows in an ASCII or binary table. When inserting rows
    all the rows following row FROW are shifted down by NROWS rows;  if
    FROW = 0 then the blank rows are inserted at the beginning of the
    table.  Note that it is *not* necessary to insert rows in a table before
    writing data to those rows (indeed, it would be inefficient to do so).
    Instead one may simply write data to any row of the table, whether that
    row of data already exists or not.

    The first delete routine deletes NROWS consecutive rows
    starting with row FIRSTROW.  The second delete routine takes an
    input string that lists the rows or row ranges (e.g.,
    '5-10,12,20-30'), whereas the third delete routine takes an input
    integer array that specifies each individual row to be deleted. In
    both latter cases, the input list of rows to delete must be sorted
    in ascending order.  These routines update the NAXIS2 keyword to
    reflect the new number of rows in the
   table. \label{ffirow} \label{ffdrow} \label{ffdrws} \label{ffdrrg}
\end{description}

\begin{verbatim}
  int fits_insert_rows / ffirow
      (fitsfile *fptr, LONGLONG firstrow, LONGLONG nrows, > int *status)

  int fits_delete_rows / ffdrow
      (fitsfile *fptr, LONGLONG firstrow, LONGLONG nrows, > int *status)

  int fits_delete_rowrange / ffdrrg
      (fitsfile *fptr, char *rangelist, > int *status)

  int fits_delete_rowlist / ffdrws
      (fitsfile *fptr, long *rowlist, long nrows, > int *status)

  int fits_delete_rowlistll / ffdrwsll
      (fitsfile *fptr, LONGLONG *rowlist, LONGLONG nrows, > int *status)
\end{verbatim}

\begin{description}
\item[2 ] Insert or delete column(s) in an ASCII or binary
    table.  When inserting, COLNUM specifies the column number that the
    (first) new column should occupy in the table.  NCOLS specifies how
    many columns are to be inserted. Any existing columns from this
    position and higher are shifted over to allow room for the new
    column(s).  The index number on all the following keywords will be
    incremented or decremented if necessary to reflect the new position
    of the column(s) in the table:  TBCOLn, TFORMn, TTYPEn, TUNITn,
    TNULLn, TSCALn, TZEROn, TDISPn, TDIMn, TLMINn, TLMAXn, TDMINn,
    TDMAXn, TCTYPn, TCRPXn, TCRVLn, TCDLTn, TCROTn,
   and TCUNIn. \label{fficol} \label{fficls} \label{ffdcol}
\end{description}

\begin{verbatim}
  int fits_insert_col / fficol
      (fitsfile *fptr, int colnum, char *ttype, char *tform,
       > int *status)

  int fits_insert_cols / fficls
      (fitsfile *fptr, int colnum, int ncols, char **ttype,
       char **tform, > int *status)

  int fits_delete_col / ffdcol(fitsfile *fptr, int colnum, > int *status)
\end{verbatim}

\begin{description}
\item[3 ] Copy column(s) between HDUs.  If create\_col = TRUE, then
    new column(s) will be inserted in the output table, starting at
    position `outcolumn', otherwise the existing output column(s) will
    be overwritten (in which case they must have a compatible data
    type).

    The first form copies a single column incolnum to outcolnum.
    Copying within the same HDU is permitted.  The second form copies
    ncols columns from the input, starting at column incolnum to the
    output, starting at outcolnum.  For the second form, the input and
    output must be different HDUs.

    If outcolnum is greater than the number of column in the output table, then
    the new column(s) will be appended to the end of the table.
    Note that the first column in a table is at colnum = 1.
    The standard indexed keywords that related to the columns (e.g., TDISPn,
    TUNITn, TCRPXn, TCDLTn, etc.) will also be copied. \label{ffcpcl} \label{ffccls}
\end{description}

\begin{verbatim}
  int fits_copy_col / ffcpcl
      (fitsfile *infptr, fitsfile *outfptr, int incolnum, int outcolnum,
       int create_col, > int *status);

  int fits_copy_cols / ffccls
      (fitsfile *infptr, fitsfile *outfptr, int incolnum, int outcolnum, 
       int ncols, int create_col, > int *status);
\end{verbatim}

\begin{description}
\item[4 ] Copy 'nrows' consecutive rows from one table to another, beginning
    with row 'firstrow'.  These rows will be appended to any existing
    rows in the output table.
   Note that the first row in a table is at row = 1. 
   \label{ffcprw}
\end{description}

\begin{verbatim}
  int fits_copy_rows / ffcprw
      (fitsfile *infptr, fitsfile *outfptr, LONGLONG firstrow,
       LONGLONG nrows, > int *status);
\end{verbatim}

\begin{description}
\item[5 ] Modify the vector length of a binary table column (e.g.,
    change a column from TFORMn = '1E' to '20E').  The vector
   length may be increased or decreased from the current value. \label{ffmvec}
\end{description}

\begin{verbatim}
  int fits_modify_vector_len / ffmvec
      (fitsfile *fptr, int colnum, LONGLONG newveclen, > int *status)
\end{verbatim}

\subsection{Read and Write Column Data Routines}

The following routines write or read data values in the current ASCII
or binary table extension.  If a write operation extends beyond the
current size of the table, then the number of rows in the table will
automatically be increased and the NAXIS2 keyword value will be
updated.  Attempts to read beyond the end of the table will result in
an error.

Automatic data type conversion is performed for numerical data types
(only) if the data type of the column (defined by the TFORMn keyword)
differs from the data type of the array in the calling routine.  ASCII and binary
tables support the following data type values:  TSTRING, TBYTE, TSBYTE, TSHORT,
TUSHORT, TINT, TUINT, TLONG, TLONGLONG, TULONG, TULONGLONG, TFLOAT, or TDOUBLE.
Binary tables also support TLOGICAL (internally mapped to the `char'
data type), TCOMPLEX, and TDBLCOMPLEX.

Note that it is *not* necessary to insert rows in a table before
writing data to those rows (indeed, it would be inefficient to do so).
Instead, one may simply write data to any row of the table, whether that
row of data already exists or not.

Individual bits in a binary table 'X' or 'B' column may be read/written
to/from a *char array by specifying the TBIT datatype.  The *char
array will be interpreted as an array of logical TRUE (1) or FALSE (0)
values that correspond to the value of each bit in the FITS 'X' or 'B' column.
Alternatively, the values in a binary table 'X' column may be read/written
8 bits at a time to/from an array of 8-bit integers by specifying the
TBYTE datatype.

Note that within the context of these routines, the TSTRING data type
corresponds to a C 'char**' data type, i.e., a pointer to an array of
pointers to an array of characters.  This is different from the keyword
reading and writing routines where TSTRING corresponds to a C 'char*'
data type, i.e., a single pointer to an array of characters.  When
reading strings from a table, the char arrays obviously must have been
allocated long enough to hold the whole FITS table string.

Numerical data values are automatically scaled by the TSCALn and TZEROn
keyword values (if they exist).

In the case of binary tables with vector elements, the 'felem'
parameter defines the starting element (beginning with 1, not 0) within
the cell (a cell is defined as the intersection of a row and a column
and may contain a single value or a vector of values).  The felem
parameter is ignored when dealing with ASCII tables. Similarly, in the
case of binary tables the 'nelements' parameter specifies the total
number of vector values to be read or written (continuing on subsequent
rows if required) and not the number of table cells.


\begin{description}
\item[1 ] Write elements into an ASCII or binary table column.
\end{description}
   The first routine simply writes the array of values to the FITS file
   (doing data type conversion if necessary) whereas the second routine
   will substitute the  appropriate FITS null value for all elements
   which are equal to the input value of nulval (note that this
   parameter gives the address of nulval, not the null value
   itself).  For integer columns the FITS null value is defined by the
   TNULLn keyword (an error is returned if the keyword doesn't exist).
   For floating point columns  the special IEEE NaN (Not-a-Number)
   value will be written into the FITS file.  If a null pointer is
   entered for nulval, then the null value is ignored and this routine
   behaves the same as the first routine.  The third routine
   simply writes undefined pixel values to the column.  The fourth routine
   fills every column in the table with null values, in the specified
   rows (ignoring any columns that do not have a defined null value).
   \label{ffpcl} \label{ffpcn} \label{ffpclu}

\begin{verbatim}
  int fits_write_col / ffpcl
      (fitsfile *fptr, int datatype, int colnum, LONGLONG firstrow,
       LONGLONG firstelem, LONGLONG nelements, DTYPE *array, > int *status)

  int fits_write_colnull / ffpcn
      (fitsfile *fptr, int datatype, int colnum, LONGLONG firstrow,
      LONGLONG firstelem, LONGLONG nelements, DTYPE *array, DTYPE *nulval,
      > int *status)

   int fits_write_col_null / ffpclu
       (fitsfile *fptr, int colnum, LONGLONG firstrow, LONGLONG firstelem,
        LONGLONG nelements, > int *status)

   int fits_write_nullrows / ffprwu
       (fitsfile *fptr, LONGLONG firstrow, LONGLONG nelements, > int *status)
\end{verbatim}

\begin{description}
\item[2 ] Read elements from an ASCII or binary table column.  The data type
    parameter specifies the data type of the `nulval' and `array'  pointers;
    Undefined array elements will be returned with a value = *nullval,
    (note that this parameter gives the address of the null value, not the
    null value itself) unless nulval = 0 or *nulval = 0, in which case
    no checking for undefined pixels will be performed.  The second
    routine is similar except that any undefined pixels will have the
    corresponding nullarray element set equal to TRUE (= 1).

    Any column, regardless of it's intrinsic data type, may be read as a
    string.  It should be noted however that reading a numeric column
    as a string is 10 - 100 times slower than reading the same column
    as a number due to the large overhead in constructing the formatted
    strings.  The display format of the returned strings will be
    determined by the TDISPn keyword, if it exists, otherwise by the
    data type of the column.  The length of the returned strings (not
    including the null terminating character) can be determined with
    the fits\_get\_col\_display\_width routine.  The following TDISPn
    display formats are currently supported:

\begin{verbatim}
    Iw.m   Integer
    Ow.m   Octal integer
    Zw.m   Hexadecimal integer
    Fw.d   Fixed floating point
    Ew.d   Exponential floating point
    Dw.d   Exponential floating point
    Gw.d   General; uses Fw.d if significance not lost, else Ew.d
\end{verbatim}
    where w is the width in characters of the displayed values, m is the minimum
    number of digits displayed, and d is the number of digits to the right of the
    decimal.  The .m field is optional.
   \label{ffgcv} \label{ffgcf}
\end{description}

\begin{verbatim}
  int fits_read_col / ffgcv
      (fitsfile *fptr, int datatype, int colnum, LONGLONG firstrow, LONGLONG firstelem,
       LONGLONG nelements, DTYPE *nulval, DTYPE *array, int *anynul, int *status)

  int fits_read_colnull / ffgcf
      (fitsfile *fptr, int datatype, int colnum, LONGLONG firstrow, LONGLONG firstelem,
      LONGLONG nelements, DTYPE *array, char *nullarray, int *anynul, int *status)
\end{verbatim}


\subsection{Row Selection and Calculator Routines}

These routines all parse and evaluate an input string containing a user
defined arithmetic expression.  The first 3 routines select rows in a
FITS table, based on whether the expression evaluates to true (not
equal to zero) or false (zero).  The other routines evaluate the
expression and calculate a value for each row of the table.  The
allowed expression syntax is described in the row filter section in the
`Extended File Name Syntax' chapter of this document.  The expression
may also be written to a text file, and the name of the file, prepended
with a '@' character may be supplied for the 'expr' parameter (e.g.
'@filename.txt'). The  expression  in  the  file can be arbitrarily
complex and extend over multiple lines of the file.  Lines  that begin
with 2 slash characters ('//') will  be ignored and may be used to add
comments to the file.


\begin{description}
\item[1 ] Evaluate a boolean expression over the indicated rows, returning an
 array of flags indicating which rows evaluated to TRUE/FALSE.
 Upon return,
 *n\_good\_rows contains the number of rows that evaluate to TRUE. \label{fffrow}
\end{description}

\begin{verbatim}
  int fits_find_rows / fffrow
      (fitsfile *fptr,  char *expr, long firstrow, long nrows,
      > long *n_good_rows, char *row_status,  int *status)
\end{verbatim}

\begin{description}
\item[2 ] Find the first row which satisfies the input boolean expression \label{ffffrw}
\end{description}

\begin{verbatim}
  int fits_find_first_row / ffffrw
      (fitsfile *fptr,  char *expr, > long *rownum, int *status)
\end{verbatim}

\begin{description}
\item[3 ]Evaluate an expression on all rows of a table.  If the input and output
files are not the same, copy the TRUE rows to the output file; if the output
table is not empty, then this routine will append the new
selected rows after the existing rows.   If the
files are the same, delete the FALSE rows (preserve the TRUE rows). \label{ffsrow}
\end{description}

\begin{verbatim}
  int fits_select_rows / ffsrow
      (fitsfile *infptr, fitsfile *outfptr,  char *expr,  > int *status )
\end{verbatim}

\begin{description}
\item[4 ] Calculate an expression for the indicated rows of a table, returning
the results, cast as datatype (TSHORT, TDOUBLE, etc), in array.  If
nulval==NULL, UNDEFs will be zeroed out.  For vector results, the number
of elements returned may be less than nelements if nelements is not an
even multiple of the result dimension.  Call fits\_test\_expr to obtain
the dimensions of the results.  \label{ffcrow}
\end{description}

\begin{verbatim}
  int fits_calc_rows / ffcrow
      (fitsfile *fptr,  int datatype, char *expr, long firstrow,
       long nelements, void *nulval, > void *array,  int *anynul, int *status)
\end{verbatim}

\begin{description}
\item[5 ]Evaluate an expression and write the result either to a column (if
the expression is a function of other columns in the table) or to a
keyword (if the expression evaluates to a constant and is not a
function of other columns in the table).  In the former case, the
parName parameter is the name of the column (which may or may not already
exist) into which to write the results, and parInfo contains an
optional TFORM keyword value if a new column is being created.  If a
TFORM value is not specified then a default format will be used,
depending on the expression.  If the expression evaluates to a constant,
then the result will be written to the keyword name given by the
parName parameter, and the parInfo parameter may be used to supply an
optional comment for the keyword.  If the keyword does not already
exist, then the name of the keyword must be preceded with a '\#' character,
 otherwise the result will be written to a column with that name. \label{ffcalc}
\end{description}

\begin{verbatim}
  int fits_calculator / ffcalc
      (fitsfile *infptr, char *expr, fitsfile *outfptr, char *parName,
       char *parInfo, >  int *status)
\end{verbatim}

\begin{description}
\item[6 ] This calculator routine is similar to the previous routine, except
that the expression is only evaluated over the specified
row ranges.  nranges specifies the number of row ranges, and firstrow
and lastrow give the starting and ending row number of each range. \label{ffcalcrng}
\end{description}

\begin{verbatim}
  int fits_calculator_rng / ffcalc_rng
      (fitsfile *infptr, char *expr, fitsfile *outfptr, char *parName,
       char *parInfo, int nranges, long *firstrow, long *lastrow
       >  int *status)
\end{verbatim}

\begin{description}
\item[7 ]Evaluate the given expression and return dimension and type information
on the result.  The returned dimensions correspond to a single row entry
of the requested expression, and are equivalent to the result of fits\_read\_tdim().
Note that strings are considered to be one element regardless of string length.
If maxdim == 0, then naxes is optional. \label{fftexp}
\end{description}

\begin{verbatim}
  int fits_test_expr / fftexp
      (fitsfile *fptr, char *expr, int maxdim > int *datatype, long *nelem, int *naxis,
       long *naxes, int *status)
\end{verbatim}


\subsection{Column Binning or Histogramming Routines}

The following routines may be useful when performing histogramming operations on
column(s) of a table to generate an image in a primary array or image extension.


\begin{description}
\item[1 ]  Calculate the histogramming parameters (min, max, and bin size
for each axis of the histogram, based on a variety of possible input parameters.
If the input names of the columns to be binned are null, then the routine will first
look for the CPREF = "NAME1, NAME2, ..." keyword which lists the preferred
columns.  If not present, then the routine will assume the column names X, Y, Z, and T
for up to 4 axes (as specified by the NAXIS parameter).

MININ and MAXIN are input arrays that give the minimum and maximum value for
the histogram, along each axis.  Alternatively, the name of keywords that give
the min, max, and binsize may be give with the MINNAME, MAXNAME, and BINNAME
array parameters.  If the value = DOUBLENULLVALUE and no keyword names are
given,  then the routine will use the TLMINn and TLMAXn keywords, if present, or the
actual min and/or max values in the column.

The ``d'' version has double precision floating point outputs as noted
in the calling signature.  The version without ``d'' has single
precision floating point outputs.

BINSIZEIN is an array giving the binsize along each axis.
If the value =
DOUBLENULLVALUE, and a keyword name is not specified with BINNAME,
then this routine will first look for the TDBINn keyword, or else will
use a binsize = 1, or a binsize that produces 10 histogram bins, which ever
is smaller.
 \label{calcbinning}
\end{description}

\begin{verbatim}
  int fits_calc_binning[d]
   Input parameters:
     (fitsfile *fptr,  /* IO - pointer to table to be binned              */
      int naxis,       /* I - number of axes/columns in the binned image  */
      char colname[4][FLEN_VALUE],   /* I - optional column names         */
      double *minin,     /* I - optional lower bound value for each axis  */
      double *maxin,     /* I - optional upper bound value, for each axis */
      double *binsizein, /* I - optional bin size along each axis         */
      char minname[4][FLEN_VALUE], /* I - optional keywords for min       */
      char maxname[4][FLEN_VALUE], /* I - optional keywords for max       */
      char binname[4][FLEN_VALUE], /* I - optional keywords for binsize   */
   Output parameters:
      int *colnum,     /* O - column numbers, to be binned */
      long *naxes,     /* O - number of bins in each histogram axis */
      float[double] *amin,     /* O - lower bound of the histogram axes */
      float[double] *amax,     /* O - upper bound of the histogram axes */
      float[double] *binsize,  /* O - width of histogram bins/pixels on each axis */
      int *status)
\end{verbatim}


\begin{description}
\item[2 ] Copy the relevant keywords from the header of the table that is being
binned, to the the header of the output histogram image.  This will not
copy the table structure keywords (e.g., NAXIS, TFORMn, TTYPEn, etc.) nor
will it copy the keywords that apply to other columns of the table that are
not used to create the histogram.  This routine will translate the names of
the World Coordinate System (WCS) keywords for the binned columns into the
form that is need for a FITS image (e.g., the TCTYPn table keyword will
be translated to the CTYPEn image keyword).
 \label{copypixlist2image}
\end{description}

\begin{verbatim}
  int fits_copy_pixlist2image
      (fitsfile *infptr,   /* I - pointer to input HDU */
       fitsfile *outfptr,  /* I - pointer to output HDU */
       int firstkey,       /* I - first HDU keyword to start with */
       int naxis,          /* I - number of axes in the image */
       int *colnum,        /* I - numbers of the columns to be binned  */
       int *status)        /* IO - error status */
\end{verbatim}


\begin{description}
\item[3 ] Write a set of default WCS keywords to the histogram header, IF the
WCS keywords do not already exist.  This will create a linear WCS where
the coordinate types are equal to the original column names.
 \label{writekeyshisto}
\end{description}

\begin{verbatim}
  int fits_write_keys_histo
     (fitsfile *fptr,     /* I - pointer to table to be binned              */
      fitsfile *histptr,  /* I - pointer to output histogram image HDU      */
      int naxis,          /* I - number of axes in the histogram image      */
      int *colnum,        /* I - column numbers of the binned columns       */
      int *status)
\end{verbatim}


\begin{description}
\item[4 ] Update the WCS keywords in a histogram image header that give the location
of the reference pixel (CRPIXn), and the pixel size (CDELTn), in the binned
image.

The ``d'' version has double precision floating point inputs as noted
in the calling signature.  The version without ``d'' has single
precision floating point inputs.

 \label{rebinwcs}
\end{description}

\begin{verbatim}
  int fits_rebin_wcs[d]
     (fitsfile *fptr,     /* I - pointer to table to be binned           */
      int naxis,          /* I - number of axes in the histogram image   */
      float[double] *amin,        /* I - first pixel include in each axis        */
      float[double] *binsize,     /* I - binning factor for each axis            */
      int *status)
\end{verbatim}


\begin{description}
\item[5 ] Bin the values in the input table columns, and write the histogram
array to the output FITS image (histptr).

The ``d'' version has double precision floating point inputs as noted
in the calling signature.  The version without ``d'' has single
precision floating point inputs.
 \label{makehist}
\end{description}

\begin{verbatim}
  int fits_make_hist[d]
   (fitsfile *fptr,    /* I - pointer to table with X and Y cols;      */
    fitsfile *histptr, /* I - pointer to output FITS image             */
    int bitpix,        /* I - datatype for image: 16, 32, -32, etc     */
    int naxis,         /* I - number of axes in the histogram image    */
    long *naxes,       /* I - size of axes in the histogram image      */
    int *colnum,       /* I - column numbers (array length = naxis)    */
    float[double] *amin,       /* I - minimum histogram value, for each axis   */
    float[double] *amax,       /* I - maximum histogram value, for each axis   */
    float[double] *binsize,    /* I - bin size along each axis                 */
    float[double] weight,      /* I - binning weighting factor (FLOATNULLVALUE */
                       /*     for no weighting)                        */
    int wtcolnum,      /* I - keyword or col for weight      (or NULL) */
    int recip,         /* I - use reciprocal of the weight? 0 or 1     */
    char *selectrow,   /* I - optional array (length = no. of          */
                       /* rows in the table).  If the element is true  */
                       /* then the corresponding row of the table will */
                       /* be included in the histogram, otherwise the  */
                       /* row will be skipped.  Ingnored if *selectrow */
                       /* is equal to NULL.                            */
    int *status)
\end{verbatim}



\section{Utility Routines}


\subsection{File Checksum Routines}

The following routines either compute or validate the checksums for the
CHDU.  The DATASUM keyword is used to store the numerical value of the
32-bit, 1's complement checksum for the data unit alone.  If there is
no data unit then the value is set to zero. The numerical value is
stored as an ASCII string of digits, enclosed in quotes, because the
value may be too large to represent as a 32-bit signed integer.  The
CHECKSUM keyword is used to store the ASCII encoded COMPLEMENT of the
checksum for the entire HDU.  Storing the complement, rather than the
actual checksum, forces the checksum for the whole HDU to equal zero.
If the file has been modified since the checksums were computed, then
the HDU checksum will usually not equal zero.  These checksum keyword
conventions are based on a paper by Rob Seaman published in the
proceedings of the ADASS IV conference in Baltimore in November 1994
and a later revision in June 1995.  See Appendix B for the definition
of the parameters used in these routines.


\begin{description}
\item[1 ] Compute and write the DATASUM and CHECKSUM keyword values for the CHDU
    into the current header.  If the keywords already exist, their values
    will be updated only if necessary (i.e., if the file
    has been modified since the original keyword
   values were computed). \label{ffpcks}
\end{description}

\begin{verbatim}
  int fits_write_chksum / ffpcks
      (fitsfile *fptr, > int *status)
\end{verbatim}

\begin{description}
\item[2 ] Update the CHECKSUM keyword value in the CHDU, assuming that the
    DATASUM keyword exists and already has the correct value.  This routine
    calculates the new checksum for the current header unit, adds it to the
    data unit checksum, encodes the value into an ASCII string, and writes
   the string to the CHECKSUM keyword. \label{ffupck}
\end{description}

\begin{verbatim}
  int fits_update_chksum / ffupck
      (fitsfile *fptr, > int *status)
\end{verbatim}

\begin{description}
\item[3 ] Verify the CHDU by computing the checksums and comparing
    them with the keywords.  The data unit is verified correctly
    if the computed checksum equals the value of the DATASUM
    keyword.  The checksum for the entire HDU (header plus data unit) is
    correct if it equals zero.  The output DATAOK and HDUOK parameters
    in this routine are integers which will have a value = 1
    if the data or HDU is verified correctly, a value = 0
    if the DATASUM or CHECKSUM keyword is not present, or value = -1
   if the computed checksum is not correct. \label{ffvcks}
\end{description}

\begin{verbatim}
  int fits_verify_chksum / ffvcks
      (fitsfile *fptr, > int *dataok, int *hduok, int *status)
\end{verbatim}

\begin{description}
\item[4 ] Compute and return the checksum values for the CHDU
    without creating or modifying the
    CHECKSUM and DATASUM keywords.  This routine is used internally by
   ffvcks, but may be useful in other situations as well. \label{ffgcks}
\end{description}

\begin{verbatim}
  int fits_get_chksum/ /ffgcks
      (fitsfile *fptr, > unsigned long *datasum, unsigned long *hdusum,
       int *status)
\end{verbatim}

\begin{description}
\item[5 ] Encode a checksum value
    into a 16-character string.  If complm is non-zero (true) then the 32-bit
   sum value will be complemented before encoding. \label{ffesum}
\end{description}

\begin{verbatim}
  int fits_encode_chksum / ffesum
      (unsigned long sum, int complm, > char *ascii);
\end{verbatim}

\begin{description}
\item[6 ] Decode a 16-character checksum string into a unsigned long value.
    If is non-zero (true). then the 32-bit sum value will be complemented
    after decoding.  The checksum value is also returned as the
   value of the function. \label{ffdsum}
\end{description}

\begin{verbatim}
  unsigned long fits_decode_chksum / ffdsum
           (char *ascii, int complm, > unsigned long *sum);
\end{verbatim}


\subsection{Date and Time Utility Routines}

The following routines help to construct or parse the FITS date/time
strings.   Starting in the year 2000, the FITS DATE keyword values (and
the values of other `DATE-' keywords) must have the form 'YYYY-MM-DD'
(date only) or 'YYYY-MM-DDThh:mm:ss.ddd...' (date and time) where the
number of decimal places in the seconds value is optional.  These times
are in UTC.  The older 'dd/mm/yy' date format may not be used for dates
after 01 January 2000.  See Appendix B for the definition of the
parameters used in these routines.


\begin{description}
\item[1 ] Get the current system date.  C already provides standard
    library routines for getting the current date and time,
    but this routine is provided for compatibility with
    the Fortran FITSIO library.  The returned year has 4 digits
    (1999, 2000, etc.) \label{ffgsdt}
\end{description}

\begin{verbatim}
  int fits_get_system_date/ffgsdt
      ( > int *day, int *month, int *year, int *status )
\end{verbatim}


\begin{description}
\item[2 ] Get the current system date and time string ('YYYY-MM-DDThh:mm:ss').
The time will be in UTC/GMT if available, as indicated by a returned timeref
value = 0.  If the returned value of timeref = 1 then this indicates that
it was not possible to convert the local time to UTC, and thus the local
time was returned.
\end{description}

\begin{verbatim}
  int fits_get_system_time/ffgstm
      (> char *datestr, int  *timeref, int *status)
\end{verbatim}


\begin{description}
\item[3 ] Construct a date string from the input date values.  If the year
is between 1900 and 1998, inclusive, then the returned date string will
have the old FITS format ('dd/mm/yy'), otherwise the date string will
have the new FITS format ('YYYY-MM-DD').  Use fits\_time2str instead
 to always return a date string using the new FITS format. \label{ffdt2s}
\end{description}

\begin{verbatim}
  int fits_date2str/ffdt2s
      (int year, int month, int day, > char *datestr, int *status)
\end{verbatim}


\begin{description}
\item[4 ] Construct a new-format date + time string ('YYYY-MM-DDThh:mm:ss.ddd...').
  If the year, month, and day values all = 0 then only the time is encoded
  with format 'hh:mm:ss.ddd...'.  The decimals parameter specifies how many
  decimal places of fractional seconds to include in the string.  If `decimals'
 is negative, then only the date will be return ('YYYY-MM-DD').
\end{description}

\begin{verbatim}
  int fits_time2str/fftm2s
      (int year, int month, int day, int hour, int minute, double second,
      int decimals, > char *datestr, int *status)
\end{verbatim}


\begin{description}
\item[5 ] Return the date as read from the input string, where the string may be
in either the old ('dd/mm/yy')  or new ('YYYY-MM-DDThh:mm:ss' or
'YYYY-MM-DD') FITS format.  Null pointers may be supplied for any
  unwanted output date parameters.
\end{description}

\begin{verbatim}
  int fits_str2date/ffs2dt
      (char *datestr, > int *year, int *month, int *day, int *status)
\end{verbatim}


\begin{description}
\item[6 ] Return the date and time as read from the input string, where the
string may be in either the old  or new FITS format.  The returned hours,
minutes, and seconds values will be set to zero if the input string
does not include the time ('dd/mm/yy' or 'YYYY-MM-DD') .  Similarly,
the returned year, month, and date values will be set to zero if the
date is not included in the input string ('hh:mm:ss.ddd...').  Null
pointers may be supplied for any unwanted output date and time
parameters.
\end{description}

\begin{verbatim}
  int fits_str2time/ffs2tm
      (char *datestr, > int *year, int *month, int *day, int *hour,
      int *minute, double *second, int *status)
\end{verbatim}


\subsection{General Utility Routines}

The following utility routines may be useful for certain applications.


\begin{description}
\item[1 ] Return the revision number of the CFITSIO library.
    The revision number will be incremented with each new
   release of CFITSIO. \label{ffvers}
\end{description}

\begin{verbatim}
  float fits_get_version / ffvers ( > float *version)
\end{verbatim}

\begin{description}
\item[2 ] Write an 80-character message to the CFITSIO error stack.  Application
    programs should not normally write to the stack, but there may be
   some situations where this is desirable. \label{ffpmsg}
\end{description}

\begin{verbatim}
  void fits_write_errmsg / ffpmsg (char *err_msg)
\end{verbatim}

\begin{description}
\item[3 ] Convert a character string to uppercase (operates in place). \label{ffupch}
\end{description}

\begin{verbatim}
  void fits_uppercase / ffupch (char *string)
\end{verbatim}

\begin{description}
\item[4 ] Compare the input template string against the reference string
    to see if they match.  The template string may contain wildcard
    characters: '*' will match any sequence of characters (including
    zero characters) and '?' will match any single character in the
    reference string.  The '\#' character will match any consecutive string
    of decimal digits (0 - 9).  If casesen = CASESEN = TRUE then the match will
    be case sensitive, otherwise the case of the letters will be ignored
    if casesen = CASEINSEN = FALSE.  The returned MATCH parameter will be
    TRUE if the 2 strings match, and EXACT will be TRUE if the match is
    exact (i.e., if no wildcard characters were used in the match).
   Both strings must be 68 characters or less in length. \label{ffcmps}
\end{description}

\begin{verbatim}
  void fits_compare_str / ffcmps
       (char *templt, char *string, int casesen, > int *match, int *exact)
\end{verbatim}

\begin{description}
\item[5 ]Split a string containing a list of names (typically file names or column
   names) into individual name tokens by a sequence of calls to
   fits\_split\_names.  The names in the list must be delimited by a comma
   and/or spaces.  This routine ignores spaces and commas that occur
   within parentheses, brackets, or curly brackets.  It also strips any
   leading and trailing blanks from the returned name.

   This routine is similar to the ANSI C 'strtok' function:

   The first call to fits\_split\_names has a non-null input string.
   It finds the first name in the string and terminates it by overwriting
   the next character of the string with a null terminator and returns a
   pointer to the name.  Each subsequent call, indicated by a NULL value
   of the input string, returns the next name, searching from just past
   the end of the previous name.  It returns NULL when no further names
  are found.  \label{splitnames}
\end{description}

\begin{verbatim}
   char *fits_split_names(char *namelist)
\end{verbatim}
   The following example shows how a string would be split into 3 names:

\begin{verbatim}
    myfile[1][bin (x,y)=4], file2.fits  file3.fits
    ^^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^  ^^^^^^^^^^
        1st name             2nd name    3rd name
\end{verbatim}

\begin{description}
\item[6 ] Test that the keyword name contains only legal characters (A-Z,0-9,
    hyphen, and underscore) or that the keyword record contains only legal
   printable ASCII characters  \label{fftkey} \label{fftrec}
\end{description}

\begin{verbatim}
  int fits_test_keyword / fftkey (char *keyname, > int *status)

  int fits_test_record / fftrec (char *card, > int *status)
\end{verbatim}

\begin{description}
\item[7 ] Test whether the current header contains any NULL (ASCII 0) characters.
    These characters are illegal in the header, but they will go undetected
    by most of the CFITSIO keyword header routines, because the null is
    interpreted as the normal end-of-string terminator.  This routine returns
    the position of the first null character in the header, or zero if there
    are no nulls.  For example a returned value of 110 would indicate that
    the first NULL is located in the 30th character of the second keyword
    in the header (recall that each header record is 80 characters long).
    Note that this is one of the few CFITSIO routines in which the returned
    value is not necessarily equal to the status value).   \label{ffnchk}
\end{description}

\begin{verbatim}
  int fits_null_check / ffnchk (char *card, > int *status)
\end{verbatim}

\begin{description}
\item[8 ] Parse a header keyword record and return the name of the keyword,
    and the length of the name.
    The keyword name normally occupies the first 8 characters of the
    record, except under the HIERARCH convention where the name can
   be up to 70 characters in length. \label{ffgknm}
\end{description}

\begin{verbatim}
  int fits_get_keyname / ffgknm
      (char *card, > char *keyname, int *keylength, int *status)
\end{verbatim}

\begin{description}
\item[9 ] Parse a header keyword record, returning the value (as
    a literal character string) and comment strings.  If the keyword has no
    value (columns 9-10 not equal to '= '), then a null value string is
    returned and the comment string is set equal to column 9 - 80 of the
   input string. \label{ffpsvc}
\end{description}

\begin{verbatim}
  int fits_parse_value / ffpsvc
      (char *card, > char *value, char *comment, int *status)
\end{verbatim}

\begin{description}
\item[10] Construct a properly formated 80-character header keyword record from the
    input keyword name, keyword value, and keyword comment strings.
    Hierarchical keyword names (e.g., "ESO TELE CAM") are supported.
    The value string may contain an integer, floating point, logical, or
    quoted character string (e.g., "12", "15.7", "T",
    or "'NGC 1313'").  \label{ffmkky}
\end{description}

\begin{verbatim}
   int fits_make_key / ffmkky
       (const char *keyname, const char *value, const char *comment,
         > char *card, int *status)
\end{verbatim}

\begin{description}
\item[11] Construct an array indexed keyword name (ROOT + nnn).
    This routine appends the sequence number to the root string to create
   a keyword name (e.g., 'NAXIS' + 2 = 'NAXIS2') \label{ffkeyn}
\end{description}

\begin{verbatim}
  int fits_make_keyn / ffkeyn
      (char *keyroot, int value, > char *keyname, int *status)
\end{verbatim}

\begin{description}
\item[12]  Construct a sequence keyword name (n + ROOT).
    This routine concatenates the sequence number to the front of the
   root string to create a keyword name (e.g., 1 + 'CTYP' = '1CTYP') \label{ffnkey}
\end{description}

\begin{verbatim}
  int fits_make_nkey / ffnkey
      (int value, char *keyroot, > char *keyname, int *status)
\end{verbatim}

\begin{description}
\item[13] Determine the data type of a keyword value string. This routine
    parses the keyword value string  to determine its data type.
    Returns 'C', 'L', 'I', 'F' or 'X', for character string, logical,
   integer, floating point, or complex, respectively. \label{ffdtyp}
\end{description}

\begin{verbatim}
  int fits_get_keytype / ffdtyp
      (char *value, > char *dtype, int *status)
\end{verbatim}

\begin{description}
\item[14] Determine the integer data type of an integer keyword value string.
   The returned datatype value is the minimum integer datatype (starting
   from top of the following list and working down) required
  to store the integer value:
\end{description}

\begin{verbatim}
    Data Type      Range
     TSBYTE:     -128 to 127
     TBYTE:       128 to 255
     TSHORT:     -32768 to 32767
     TUSHORT:     32768 to 65535
     TINT        -2147483648 to 2147483647
     TUINT        2147483648 to 4294967295
     TLONGLONG   -9223372036854775808 to 9223372036854775807
\end{verbatim}

\begin{description}
\item[  ]  The *neg parameter returns 1 if the input value is
    negative and returns 0 if it is non-negative.\label{ffinttyp}
\end{description}

\begin{verbatim}
  int fits_get_inttype / ffinttyp
      (char *value, > int *datatype, int *neg, int *status)
\end{verbatim}

\begin{description}
\item[15] Return the class of an input header record.  The record is classified
    into one of the following categories (the class values are
    defined in fitsio.h).  Note that this is one of the few CFITSIO
   routines that does not return a status value. \label{ffgkcl}
\end{description}

\begin{verbatim}
       Class  Value             Keywords
  TYP_STRUC_KEY  10  SIMPLE, BITPIX, NAXIS, NAXISn, EXTEND, BLOCKED,
                     GROUPS, PCOUNT, GCOUNT, END
                     XTENSION, TFIELDS, TTYPEn, TBCOLn, TFORMn, THEAP,
                     and the first 4 COMMENT keywords in the primary array
                     that define the FITS format.
  TYP_CMPRS_KEY  20  The keywords used in the compressed image or table
                     format, including ZIMAGE, ZCMPTYPE, ZNAMEn, ZVALn,
                     ZTILEn, ZBITPIX, ZNAXISn, ZSCALE, ZZERO, ZBLANK
  TYP_SCAL_KEY   30  BSCALE, BZERO, TSCALn, TZEROn
  TYP_NULL_KEY   40  BLANK, TNULLn
  TYP_DIM_KEY    50  TDIMn
  TYP_RANG_KEY   60  TLMINn, TLMAXn, TDMINn, TDMAXn, DATAMIN, DATAMAX
  TYP_UNIT_KEY   70  BUNIT, TUNITn
  TYP_DISP_KEY   80  TDISPn
  TYP_HDUID_KEY  90  EXTNAME, EXTVER, EXTLEVEL, HDUNAME, HDUVER, HDULEVEL
  TYP_CKSUM_KEY 100  CHECKSUM, DATASUM
  TYP_WCS_KEY   110  WCS keywords defined in the the WCS papers, including:
                     CTYPEn, CUNITn, CRVALn, CRPIXn, CROTAn, CDELTn
                     CDj_is, PVj_ms, LONPOLEs, LATPOLEs
                     TCTYPn, TCTYns, TCUNIn, TCUNns, TCRVLn, TCRVns, TCRPXn,
                     TCRPks, TCDn_k, TCn_ks, TPVn_m, TPn_ms, TCDLTn, TCROTn
                     jCTYPn, jCTYns, jCUNIn, jCUNns, jCRVLn, jCRVns, iCRPXn,
                     iCRPns, jiCDn,  jiCDns, jPVn_m, jPn_ms, jCDLTn, jCROTn
                     (i,j,m,n are integers, s is any letter)
  TYP_REFSYS_KEY 120 EQUINOXs, EPOCH, MJD-OBSs, RADECSYS, RADESYSs, DATE-OBS
  TYP_COMM_KEY   130 COMMENT, HISTORY, (blank keyword)
  TYP_CONT_KEY   140 CONTINUE
  TYP_USER_KEY   150 all other keywords

  int fits_get_keyclass / ffgkcl (char *card)
\end{verbatim}

\begin{description}
\item[16] Parse the 'TFORM' binary table column format string.
    This routine parses the input TFORM character string and returns the
    integer data type code, the repeat count of the field, and, in the case
    of character string fields, the length of the unit string.  See Appendix
    B for the allowed values for the returned typecode parameter.  A
   null pointer may be given for any output parameters that are not needed. \label{ffbnfm}
\end{description}

\begin{verbatim}
   int fits_binary_tform / ffbnfm
       (char *tform, > int *typecode, long *repeat, long *width,
        int *status)

   int fits_binary_tformll / ffbnfmll
       (char *tform, > int *typecode, LONGLONG *repeat, long *width,
        int *status)
\end{verbatim}

\begin{description}
\item[17] Parse the 'TFORM' keyword value that defines the column format in
    an ASCII table.  This routine parses the input TFORM character
    string and returns the data type code, the width of the column,
    and (if it is a floating point column) the number of decimal places
    to the right of the decimal point.  The returned data type codes are
    the same as for the binary table, with the following
    additional rules:  integer columns that are between 1 and 4 characters
    wide are defined to be short integers (code = TSHORT).  Wider integer
    columns are defined to be regular integers (code = TLONG).  Similarly,
    Fixed decimal point columns (with TFORM = 'Fw.d') are defined to
    be single precision reals (code = TFLOAT) if w is between 1 and 7 characters
    wide, inclusive.  Wider 'F' columns will return a double precision
    data code (= TDOUBLE).  'Ew.d' format columns will have datacode = TFLOAT,
    and 'Dw.d' format columns will have datacode = TDOUBLE. A null
   pointer may be given for any output parameters that are not needed. \label{ffasfm}
\end{description}

\begin{verbatim}
  int fits_ascii_tform / ffasfm
      (char *tform, > int *typecode, long *width, int *decimals,
       int *status)
\end{verbatim}

\begin{description}
\item[18] Calculate the starting column positions and total ASCII table width
    based on the input array of ASCII table TFORM values.  The SPACE input
    parameter defines how many blank spaces to leave between each column
    (it is recommended to have one space between columns for better human
   readability). \label{ffgabc}
\end{description}

\begin{verbatim}
  int fits_get_tbcol / ffgabc
      (int tfields, char **tform, int space, > long *rowlen,
       long *tbcol, int *status)
\end{verbatim}

\begin{description}
\item[19] Parse a template header record and return a formatted 80-character string
    suitable for appending to (or deleting from) a FITS header file.
    This routine is useful for parsing lines from an ASCII template file
    and reformatting them into legal FITS header records.  The formatted
    string may then be passed to the fits\_write\_record, ffmcrd, or
    fits\_delete\_key routines
   to append or modify a FITS header record. \label{ffgthd}
\end{description}

\begin{verbatim}
  int fits_parse_template / ffgthd
      (char *templt, > char *card, int *keytype, int *status)
\end{verbatim}
    The input templt character string generally should contain 3 tokens:
    (1) the KEYNAME, (2) the VALUE, and (3) the COMMENT string.  The
    TEMPLATE string must adhere to the following format:


\begin{description}
\item[- ]     The KEYNAME token must begin in columns 1-8 and be a maximum  of 8
        characters long.  A legal FITS keyword name may only
        contain the characters A-Z, 0-9, and '-' (minus sign) and
        underscore.  This routine will automatically convert any lowercase
        characters to uppercase in the output string.  If the first 8 characters
        of the template line are
        blank then the remainder of the line is considered to be a FITS comment
       (with a blank keyword name).
\end{description}


\begin{description}
\item[- ]     The VALUE token must be separated from the KEYNAME token by one or more
        spaces and/or an '=' character.  The data type of the VALUE token
        (numeric, logical, or character string) is automatically determined
        and  the output CARD string is formatted accordingly.  The value
        token may be forced to be interpreted as a string (e.g. if it is a
        string of numeric digits) by enclosing it in single quotes.
        If the value token is a character string that contains 1 or more
        embedded blank space characters or slash ('/') characters then the
       entire character string must be enclosed in single quotes.
\end{description}


\begin{description}
\item[- ]     The COMMENT token is optional, but if present must be separated from
       the VALUE token by a blank space or a  '/' character.
\end{description}


\begin{description}
\item[- ]     One exception to the above rules is that if the first non-blank
        character in the first 8 characters of the template string is a
        minus sign ('-') followed
        by a single token, or a single token followed by an equal sign,
        then it is interpreted as the name of a keyword which is to be
       deleted from the FITS header.
\end{description}


\begin{description}
\item[- ]     The second exception is that if the template string starts with
        a minus sign and is followed by 2 tokens (without an equals sign between
        them) then the second token
        is interpreted as the new name for the keyword specified by
        first token.  In this case the old keyword name (first token)
        is returned in characters 1-8 of the returned CARD string, and
        the new keyword name (the second token) is returned in characters
        41-48 of the returned CARD string.  These old and new names
        may then be passed to the ffmnam routine which will change
       the keyword name.
\end{description}

    The keytype output parameter indicates how the returned CARD string
    should be interpreted:

\begin{verbatim}
        keytype                  interpretation
        -------          -------------------------------------------------
           -2            Rename the keyword with name = the first 8 characters of CARD
                         to the new name given in characters 41 - 48 of CARD.

           -1            delete the keyword with this name from the FITS header.

            0            append the CARD string to the FITS header if the
                         keyword does not already exist, otherwise update
                         the keyword value and/or comment field if is already exists.

            1            This is a HISTORY or COMMENT keyword; append it to the header

            2            END record; do not explicitly write it to the FITS file.
\end{verbatim}
     EXAMPLES:  The following lines illustrate valid input template strings:

\begin{verbatim}
      INTVAL 7 / This is an integer keyword
      RVAL           34.6   /     This is a floating point keyword
      EVAL=-12.45E-03  / This is a floating point keyword in exponential notation
      lval F / This is a boolean keyword
                  This is a comment keyword with a blank keyword name
      SVAL1 = 'Hello world'   /  this is a string keyword
      SVAL2  '123.5'  this is also a string keyword
      sval3  123+  /  this is also a string keyword with the value '123+    '
      # the following template line deletes the DATE keyword
      - DATE
      # the following template line modifies the NAME keyword to OBJECT
      - NAME OBJECT
\end{verbatim}

\begin{description}
\item[20]  Translate a keyword name into a new name, based on a set of patterns.
This routine is useful for translating keywords in cases such as
adding or deleting columns in
a table, or copying a column from one table to another, or extracting
an array from a cell in a binary table column into an image extension.  In
these cases, it is necessary to translate the names of the keywords associated
with the original table column(s) into the appropriate keyword name in the final
file.  For example, if column 2 is deleted from a table,
then the value of 'n' in all the
TFORMn and TTYPEn keywords for columns 3 and higher must be decremented
by 1.  Even more complex translations are sometimes needed to convert the
WCS keywords when extracting an image out of a table column cell into
a separate image extension.

The user passes an array of patterns to be matched.  Input pattern
number i is pattern[i][0], and output pattern number i is
pattern[i][1].  Keywords are matched against the input patterns.  If a
match is found then the keyword is re-written according to the output
pattern.

Order is important.  The first match is accepted.  The fastest match
will be made when templates with the same first character are grouped
together.

Several characters have special meanings:

\begin{verbatim}
     i,j - single digits, preserved in output template
     n - column number of one or more digits, preserved in output template
     m - generic number of one or more digits, preserved in output template
     a - coordinate designator, preserved in output template
     # - number of one or more digits
     ? - any character
     * - only allowed in first character position, to match all
         keywords; only useful as last pattern in the list
\end{verbatim}
i, j, n, and m are returned by the routine.

For example, the input pattern "iCTYPn" will match "1CTYP5" (if n\_value
is 5); the output pattern "CTYPEi" will be re-written as "CTYPE1".
Notice that "i" is preserved.

The following output patterns are special:

    "-" - do not copy a keyword that matches the corresponding input pattern

    "+" - copy the input unchanged

The inrec string could be just the 8-char keyword name, or the entire
80-char header record.  Characters 9 - 80 in the input string simply get
appended to the translated keyword name.

If n\_range = 0, then only keywords with 'n' equal to n\_value will be
considered as a pattern match.  If n\_range = +1, then all values of
'n' greater than or equal to n\_value will be a match, and if -1,
then values of 'n' less than or equal to n\_value will match.\label{translatekey}
\end{description}

\begin{verbatim}
int fits_translate_keyword(
      char *inrec,        /* I - input string */
      char *outrec,       /* O - output converted string, or */
                          /*     a null string if input does not  */
                          /*     match any of the patterns */
      char *patterns[][2],/* I - pointer to input / output string */
                          /*     templates */
      int npat,           /* I - number of templates passed */
      int n_value,        /* I - base 'n' template value of interest */
      int n_offset,       /* I - offset to be applied to the 'n' */
                          /*     value in the output string */
      int n_range,        /* I - controls range of 'n' template */
                          /*     values of interest (-1,0, or +1) */
      int *pat_num,       /* O - matched pattern number (0 based) or -1 */
      int *i,             /* O - value of i, if any, else 0 */
      int *j,             /* O - value of j, if any, else 0 */
      int *m,             /* O - value of m, if any, else 0 */
      int *n,             /* O - value of n, if any, else 0 */
      int *status)        /* IO - error status */
\end{verbatim}

\begin{description}
\item[  ]  Here is an example of some of the patterns used to convert the keywords associated
with an image in a cell of a table column into the keywords appropriate for
an IMAGE extension:
\end{description}

\begin{verbatim}
    char *patterns[][2] = {{"TSCALn",  "BSCALE"  },  /* Standard FITS keywords */
			   {"TZEROn",  "BZERO"   },
			   {"TUNITn",  "BUNIT"   },
			   {"TNULLn",  "BLANK"   },
			   {"TDMINn",  "DATAMIN" },
			   {"TDMAXn",  "DATAMAX" },
			   {"iCTYPn",  "CTYPEi"  },  /* Coordinate labels */
			   {"iCTYna",  "CTYPEia" },
			   {"iCUNIn",  "CUNITi"  },  /* Coordinate units */
			   {"iCUNna",  "CUNITia" },
			   {"iCRVLn",  "CRVALi"  },  /* WCS keywords */
			   {"iCRVna",  "CRVALia" },
			   {"iCDLTn",  "CDELTi"  },
			   {"iCDEna",  "CDELTia" },
			   {"iCRPXn",  "CRPIXi"  },
			   {"iCRPna",  "CRPIXia" },
			   {"ijPCna",  "PCi_ja"  },
			   {"ijCDna",  "CDi_ja"  },
			   {"iVn_ma",  "PVi_ma"  },
			   {"iSn_ma",  "PSi_ma"  },
			   {"iCRDna",  "CRDERia" },
			   {"iCSYna",  "CSYERia" },
			   {"iCROTn",  "CROTAi"  },
			   {"WCAXna",  "WCSAXESa"},
			   {"WCSNna",  "WCSNAMEa"}};
\end{verbatim}

\begin{description}
\item[21]  Translate the keywords in the input HDU into the keywords that are
appropriate for the output HDU.  This is a driver routine that calls
the previously described routine.
\end{description}

\begin{verbatim}
int fits_translate_keywords(
	   fitsfile *infptr,   /* I - pointer to input HDU */
	   fitsfile *outfptr,  /* I - pointer to output HDU */
	   int firstkey,       /* I - first HDU record number to start with */
	   char *patterns[][2],/* I - pointer to input / output keyword templates */
	   int npat,           /* I - number of templates passed */
	   int n_value,        /* I - base 'n' template value of interest */
	   int n_offset,       /* I - offset to be applied to the 'n' */
 	                       /*     value in the output string */
	   int n_range,        /* I - controls range of 'n' template */
	                       /*     values of interest (-1,0, or +1) */
	   int *status)        /* IO - error status */
\end{verbatim}


\begin{description}
\item[22]  Parse the input string containing a list of rows or row ranges, and
     return integer arrays containing the first and last row in each
     range.  For example, if rowlist = "3-5, 6, 8-9" then it will
     return numranges = 3, rangemin = 3, 6, 8 and rangemax = 5, 6, 9.
     At most, 'maxranges' number of ranges will be returned.  'maxrows'
     is the maximum number of rows in the table; any rows or ranges
     larger than this will be ignored.  The rows must be specified in
     increasing order, and the ranges must not overlap. A minus sign
     may be use to specify all the rows to the upper or lower bound, so
     "50-" means all the rows from 50 to the end of the table, and "-"
     means all the rows in the table, from 1 - maxrows.
   \label{ffrwrg}
\end{description}

\begin{verbatim}
    int fits_parse_range / ffrwrg(char *rowlist, LONGLONG maxrows, int maxranges, >
       int *numranges, long *rangemin, long *rangemax, int *status)

    int fits_parse_rangell / ffrwrgll(char *rowlist, LONGLONG maxrows, int maxranges, >
       int *numranges, LONGLONG *rangemin, LONGLONG *rangemax, int *status)
\end{verbatim}

\begin{description}
\item[23]  Check that the Header fill bytes (if any) are all blank.  These are the bytes
     that may follow END keyword and before the beginning of data unit,
     or the end of the HDU if there is no data unit.
   \label{ffchfl}
\end{description}

\begin{verbatim}
    int ffchfl(fitsfile *fptr, > int *status)
\end{verbatim}

\begin{description}
\item[24]  Check that the Data fill bytes (if any) are all zero (for IMAGE or
     BINARY Table HDU) or all blanks (for ASCII table HDU).  These file
     bytes may be located after the last valid data byte in the HDU and
     before the physical end of the HDU.
     \label{ffcdfl}
\end{description}

\begin{verbatim}
    int ffcdfl(fitsfile *fptr, > int *status)
\end{verbatim}

\begin{description}
\item[25]  Estimate the root-mean-squared (RMS) noise in an image.
These routines are mainly for use with the Hcompress image compression
algorithm.  They return an estimate of the RMS noise in the background
pixels of the image.  This robust algorithm (written by Richard
White, STScI) first attempts to estimate the RMS value
as 1.68 times the median of the absolute differences between successive
pixels in the image.  If the median = 0,  then the
algorithm falls back to computing the RMS of the difference between successive
pixels, after several N-sigma rejection cycles to remove
extreme values.  The input parameters are:  the array of image pixel values
(either float or short values), the number of values in the array,
the value that is used to represent null pixels (enter a very
large number if there are no null pixels). \label{imageRMS}
\end{description}

\begin{verbatim}
    int fits_rms_float (float fdata[], int npix, float in_null_value,
                   > double *rms, int *status)
    int fits_rms_short (short fdata[], int npix, short in_null_value,
                   > double *rms, int *status)
\end{verbatim}

\begin{description}
\item[26]  Was CFITSIO compiled with the -D\_REENTRANT directive
so that it may be safely used in multi-threaded environments?
The following function returns 1 if yes, 0 if no.  Note, however,
that even if the -D\_REENTRANT directive was specified, this does
not guarantee that the CFITSIO routines are thread-safe, because
some compilers may not support this feature.\label{reentrant}
\end{description}

\begin{verbatim}
int fits_is_reentrant(void)
\end{verbatim}

\chapter{  The CFITSIO Iterator Function }

The fits\_iterate\_data function in CFITSIO provides a unique method of
executing an arbitrary user-supplied `work' function that operates on
rows of data in  FITS tables or on pixels in FITS images.  Rather than
explicitly reading and writing the FITS images or columns of data, one
instead calls the CFITSIO iterator routine, passing to it the name of
the user's work function that is to be executed along with a list of
all the table columns or image arrays that are to be passed to the work
function.  The CFITSIO iterator function then does all the work of
allocating memory for the arrays, reading the input data from the FITS
file, passing them to the work function, and then writing any output
data back to the FITS file after the work function exits.  Because
it is often more efficient to process only a subset of the total table
rows at one time, the iterator function can determine the optimum
amount of data to pass in each iteration and repeatedly call the work
function until the entire table been processed.

For many applications this single CFITSIO iterator function can
effectively replace all the other CFITSIO routines for reading or
writing data in FITS images or tables.  Using the iterator has several
important advantages over the traditional method of reading and writing
FITS data files:

\begin{itemize}
\item
It cleanly separates the data I/O from the routine that operates on
the data.  This leads to a more modular and `object oriented'
programming style.

\item
It simplifies the application program by eliminating the need to allocate
memory for the data arrays and eliminates most of the calls to the CFITSIO
routines that explicitly read and write the data.

\item
It ensures that the data are processed as efficiently as possible.
This is especially important when processing tabular data since
the iterator function will calculate the most efficient number
of rows in the table to be passed at one time to the user's work
function on each iteration.

\item
Makes it possible for larger projects to develop a library of work
functions that all have a uniform calling sequence and are all
independent of the details of the FITS file format.

\end{itemize}

There are basically 2 steps in using the CFITSIO iterator function.
The first step is to design the work function itself which must have a
prescribed set of input parameters.  One of these parameters is a
structure containing pointers to the arrays of data; the work function
can perform any desired operations on these arrays and does not need to
worry about how the input data were read from the file or how the
output data get written back to the file.

The second step is to design the driver routine that opens all the
necessary FITS files and initializes  the input parameters to the
iterator function.  The driver program calls the CFITSIO iterator
function which then reads the data and passes it to the user's work
function.

The following 2 sections describe these steps in more detail.  There
are also several example programs included with the CFITSIO
distribution which illustrate how to use the iterator function.


\section{The Iterator Work Function}

The user-supplied iterator work function must have the following set of
input parameters (the function can be given any desired name):


\begin{verbatim}
  int user_fn( long totaln, long offset, long firstn, long nvalues,
               int narrays, iteratorCol *data,  void *userPointer )
\end{verbatim}

\begin{itemize}

\item
  totaln -- the total number of table rows or image pixels
            that will be passed to the work function
            during 1 or more iterations.

\item
  offset     -- the offset applied to the first table row or image
                pixel to be passed to the work function.  In other
                words, this is the number of rows or pixels that
                are skipped over before starting the iterations. If
                offset = 0, then all the table rows or image pixels
                will be passed to the work function.

\item
  firstn     -- the number of the first table row or image pixel
                (starting with 1)  that is being passed in this
                particular call to the work function.

\item
  nvalues    -- the number of table rows or image pixels that are
                being passed in this particular call to the work
                function.  nvalues will always be less than or
                equal to totaln and will have the same value on
                each iteration, except possibly on the last
                call which may have a smaller value.

\item
  narrays     -- the number of arrays of data that are being passed
                 to the work function.  There is one array for each
                 image or table column.

\item
  *data   -- array of structures, one for each
             column or image.  Each structure contains a pointer
             to the array of data as well as other descriptive
             parameters about that array.

\item
  *userPointer -- a user supplied pointer that can be used
                 to pass ancillary information from the driver function
                 to the work function.
                 This pointer is passed to the CFITSIO iterator function
                 which then passes it on to the
                 work function without any modification.
                 It may point to a single number, to an array of values,
                 to a structure containing an arbitrary set of parameters
                 of different types,
                 or it may be a null pointer if it is not needed.
                 The work function must cast this pointer to the
                 appropriate data type before using it it.
\end{itemize}

The totaln, offset, narrays, data, and userPointer parameters are
guaranteed to have the same value on each iteration.  Only firstn,
nvalues, and the arrays of data pointed to by the data structures may
change on each iterative call to the work function.

Note that the iterator treats an image as a long 1-D array of pixels
regardless of it's intrinsic dimensionality.  The total number of
pixels is just the product of the size of each dimension, and the order
of the pixels is the same as the order that they are stored in the FITS
file. If the work function needs to know the number and size of the
image dimensions then these parameters can be passed via the
userPointer structure.

The iteratorCol structure is currently defined as follows:

\begin{verbatim}
typedef struct  /* structure for the iterator function column information */
{
   /* structure elements required as input to fits_iterate_data: */

  fitsfile *fptr;       /* pointer to the HDU containing the column or image */
  int      colnum;      /* column number in the table; ignored for images    */
  char     colname[70]; /* name (TTYPEn) of the column; null for images      */
  int      datatype;    /* output data type (converted if necessary) */
  int      iotype;      /* type: InputCol, InputOutputCol, or OutputCol */

  /* output structure elements that may be useful for the work function: */

  void     *array;    /* pointer to the array (and the null value) */
  long     repeat;    /* binary table vector repeat value; set     */
                      /*     equal to 1 for images                 */
  long     tlmin;     /* legal minimum data value, if any          */
  long     tlmax;     /* legal maximum data value, if any          */
  char     unit[70];  /* physical unit string (BUNIT or TUNITn)    */
  char     tdisp[70]; /* suggested display format; null if none    */

} iteratorCol;
\end{verbatim}

Instead of directly reading or writing the elements in this structure,
it is recommended that programmers use the access functions that are
provided for this purpose.

The first five elements in this structure must be initially defined by
the driver routine before calling the iterator routine.  The CFITSIO
iterator routine uses this information to determine what column or
array to pass to the work function, and whether the array is to be
input to the work function, output from the work function, or both.
The CFITSIO iterator function fills in the values of the remaining
structure elements before passing it to the work function.

The array structure element is a pointer to the actual data array and
it must be cast to the correct data type before it is used.  The
`repeat' structure element give the number of data values in each row
of the table, so that the total number of data values in the array is
given by repeat * nvalues.  In the case of image arrays and ASCII
tables, repeat will always be equal to 1.  When the data type is a
character string, the array pointer is actually a pointer to an array
of string pointers (i.e., char **array).  The other output structure
elements are provided for convenience in case that information is
needed within the work function.  Any other information may be passed
from the driver routine to the work function via the userPointer
parameter.

Upon completion, the work routine must return an integer status value,
with 0 indicating success and any other value indicating an error which
will cause the iterator function to immediately exit at that point.  Return status
values in the range 1 -- 1000 should be avoided since these are
reserved for use by CFITSIO.  A return status value of -1 may be used to
force the CFITSIO iterator function to stop at that point and return
control to the driver routine after writing any output arrays to the
FITS file.  CFITSIO does not considered this to be an error condition,
so any further processing by the application program will continue normally.


\section{The Iterator Driver Function}

The iterator driver function must open the necessary FITS files and
position them to the correct HDU.  It must also initialize the following
parameters in the iteratorCol structure (defined above) for each
column or image before calling the CFITSIO iterator function.
Several `constructor' routines are provided in CFITSIO for this
purpose.

\begin{itemize}
\item
  *fptr --  The fitsfile pointer to the table or image.
\item
colnum -- the number of the column in the table.  This value is ignored
          in the case of images.  If colnum equals 0, then the column name
          will be used to identify the column to be passed to the
          work function.

\item
colname -- the name (TTYPEn keyword) of the column.  This is
           only required if colnum = 0 and is ignored for images.
\item
datatype -- The desired data type of the array to be passed to the
            work function.  For numerical data the data type does
            not need to be the same as the actual data type in the
            FITS file, in which case CFITSIO will do the conversion.
            Allowed values are: TSTRING, TLOGICAL, TBYTE, TSBYTE, TSHORT, TUSHORT,
            TINT, TLONG, TULONG, TFLOAT, TDOUBLE.  If the input
            value of data type equals 0, then the  existing
            data type of the column or image will be used without
            any conversion.

\item
iotype -- defines whether the data array is to be input to the
          work function (i.e, read from the FITS file), or output
          from the work function (i.e., written to the FITS file) or
          both.  Allowed values are InputCol, OutputCol, or InputOutputCol.
	  Variable-length array columns are supported as InputCol or
	  InputOutputCol types, but may not be used for an OutputCol type.
\end{itemize}

After the driver routine has initialized all these parameters, it
can then call the CFITSIO iterator function:


\begin{verbatim}
  int fits_iterate_data(int narrays, iteratorCol *data, long offset,
      long nPerLoop, int (*workFn)( ), void *userPointer, int *status);
\end{verbatim}

\begin{itemize}
\item

   narrays    -- the number of columns or images that are to be passed
                 to the work function.
\item
   *data --     pointer to array of structures containing information
                about each column or image.

\item
   offset      -- if positive, this number of rows at the
                      beginning of the table (or pixels in the image)
                      will be skipped and will not be passed to the work
                      function.

\item
   nPerLoop   - specifies the number of table rows (or number of
                    image pixels) that are to be passed to the work
                    function on each iteration.  If nPerLoop = 0
                    then CFITSIO will calculate the optimum number
                    for greatest efficiency.
                    If nPerLoop is negative, then all the rows
                    or pixels will be passed at one time, and the work
                    function will only be called once.  If any variable
		    length arrays are being processed, then the nPerLoop
		    value is ignored, and the iterator will always process
		    one row of the table at a time.

\item
   *workFn     - the name (actually the address) of the work function
                 that is to be called by fits\_iterate\_data.

\item
   *userPointer - this is a user supplied pointer that can be used
                  to pass ancillary information from the driver routine
                  to the work function.  It may point to a single number,
                  an array, or to a structure containing an arbitrary set
                  of parameters.

\item
   *status      - The CFITSIO error status.  Should = 0 on input;
                  a non-zero output value indicates an error.
\end{itemize}

When fits\_iterate\_data is called it first allocates memory to hold
all the requested columns of data or image pixel arrays.  It then reads
the input data from the FITS tables or images into the arrays then
passes the structure with pointers to these data arrays to the work
function.  After the work function returns, the iterator function
writes any output columns of data or images back to the FITS files.  It
then repeats this process for any remaining sets of rows or image
pixels until it has processed the entire table or image or until the
work function returns a non-zero status value.  The iterator then frees
the memory that it initially allocated and returns control to the
driver routine that called it.


\section{Guidelines for Using the Iterator Function}

The totaln, offset, firstn, and nvalues parameters that are passed to
the work function are useful for determining how much of the data has
been processed and how much remains left to do.  On the very first call
to the work function firstn will be equal to offset + 1;  the work
function may need to perform various initialization tasks before
starting to  process the data. Similarly, firstn + nvalues - 1 will be
equal to totaln on the last iteration, at which point the work function
may need to perform some clean up operations before exiting for the
last time.  The work function can also force an early termination of
the iterations by returning a status value = -1.

The narrays and iteratorCol.datatype arguments allow the work function
to double check that the number of input arrays and their data types
have the expected values.  The iteratorCol.fptr and iteratorCol.colnum
structure elements can be used if the work function needs to read or
write the values of other keywords in the FITS file associated with
the array.  This should generally only be done during the
initialization step or during the clean up step after the last set of
data has been processed.  Extra FITS file I/O during the main
processing loop of the work function can seriously degrade the speed of
the program.

If variable-length array columns are being processed, then the iterator
will operate on one row of the table at a time.  In this case the
the repeat element in the interatorCol structure will be set equal to
the number of elements in the current row that is being processed.

One important feature of the iterator is that the first element in each
array that is passed to the work function gives the value that is used
to represent null or undefined values in the array.  The real data then
begins with the second element of the array (i.e., array[1], not
array[0]).  If the first array element is equal to zero, then this
indicates that all the array elements have defined values and there are
no undefined values.  If array[0] is not equal to zero, then this
indicates that some of the data values are undefined and this value
(array[0]) is used to represent them.  In the case of output arrays
(i.e., those arrays that will be written back to the FITS file by the
iterator function after the work function exits) the work function must
set the first array element to the desired null value if necessary,
otherwise the first element should be set to zero to indicate that
there are no null values in the output array.  CFITSIO defines 2
values, FLOATNULLVALUE and DOUBLENULLVALUE, that can be used as default
null values for float and double data types, respectively.  In the case
of character string data types, a null string is always used to
represent undefined strings.

In some applications it may be necessary to recursively call the iterator
function.  An example of this is given by one of the example programs
that is distributed with CFITSIO: it first calls a work function that
writes out a 2D histogram image.  That work function in turn calls
another work function that reads the  `X' and `Y' columns in a table to
calculate the value of each 2D histogram image pixel. Graphically, the
program structure can be described as:

\begin{verbatim}
 driver --> iterator --> work1_fn --> iterator --> work2_fn
\end{verbatim}

Finally, it should be noted that the table columns or image arrays that
are passed to the work function do not all have to come from the same
FITS file and instead may come from any combination of sources as long
as they have the same length.   The length of the first table column or
image array is used by the iterator if they do not all have the same
length.


\section{Complete List of Iterator Routines}

All of the iterator routines are listed below.  Most of these routines
do not have a corresponding short function name.


\begin{description}
\item[1 ]  Iterator `constructor' functions that set
   the value of elements in the iteratorCol structure
   that define the columns or arrays. These set the fitsfile
    pointer, column name, column number, datatype, and iotype,
    respectively.  The last 2 routines allow all the parameters
    to be set with one function call (one supplies the column
   name, the other the column number). \label{ffiterset}
\end{description}


\begin{verbatim}
  int fits_iter_set_file(iteratorCol *col, fitsfile *fptr);

  int fits_iter_set_colname(iteratorCol *col, char *colname);

  int fits_iter_set_colnum(iteratorCol *col, int colnum);

  int fits_iter_set_datatype(iteratorCol *col, int datatype);

  int fits_iter_set_iotype(iteratorCol *col, int iotype);

  int fits_iter_set_by_name(iteratorCol *col, fitsfile *fptr,
          char *colname, int datatype,  int iotype);

  int fits_iter_set_by_num(iteratorCol *col, fitsfile *fptr,
          int colnum, int datatype,  int iotype);
\end{verbatim}

\begin{description}
\item[2 ]  Iterator `accessor' functions that return the value of the
     element in the iteratorCol structure
    that describes a particular data column or array \label{ffiterget}
\end{description}

\begin{verbatim}
  fitsfile * fits_iter_get_file(iteratorCol *col);

  char * fits_iter_get_colname(iteratorCol *col);

  int fits_iter_get_colnum(iteratorCol *col);

  int fits_iter_get_datatype(iteratorCol *col);

  int fits_iter_get_iotype(iteratorCol *col);

  void * fits_iter_get_array(iteratorCol *col);

  long fits_iter_get_tlmin(iteratorCol *col);

  long fits_iter_get_tlmax(iteratorCol *col);

  long fits_iter_get_repeat(iteratorCol *col);

  char * fits_iter_get_tunit(iteratorCol *col);

  char * fits_iter_get_tdisp(iteratorCol *col);
\end{verbatim}

\begin{description}
\item[3 ]  The CFITSIO iterator function \label{ffiter}
\end{description}

\begin{verbatim}
  int fits_iterate_data(int narrays,  iteratorCol *data, long offset,
            long nPerLoop,
            int (*workFn)( long totaln, long offset, long firstn,
                           long nvalues, int narrays, iteratorCol *data,
                           void *userPointer),
            void *userPointer,
            int *status);
\end{verbatim}

\chapter{ World Coordinate System Routines }

The FITS community has adopted a set of keyword conventions that define
the transformations needed to convert between pixel locations in an
image and the corresponding celestial coordinates on the sky, or more
generally, that define world coordinates that are to be associated with
any pixel location in an n-dimensional FITS array. CFITSIO is distributed
with a a few self-contained World Coordinate System (WCS) routines,
however, these routines DO NOT support all the latest WCS conventions,
so it is STRONGLY RECOMMENDED that software developers use a more robust
external WCS library.  Several recommended libraries are:


\begin{verbatim}
  WCSLIB -  supported by Mark Calabretta
  WCSTools - supported by Doug Mink
  AST library - developed by the U.K. Starlink project
\end{verbatim}

More information about the WCS keyword conventions and links to all of
these WCS libraries can be found on the FITS Support Office web site at
http://fits.gsfc.nasa.gov under the WCS link.

The functions provided in these external WCS libraries will need
access to the  WCS keywords contained in the FITS file headers.
One convenient way to pass this information to the external library is
to use the fits\_hdr2str routine in CFITSIO (defined below) to copy the
header keywords into one long string, and then pass this string to an
interface routine in the external library that will extract
the necessary WCS information (e.g., the 'wcspih' routine in the WCSLIB
library and the 'astFitsChan' and 'astPutCards' functions in the AST
library).


\begin{description}
\item[1 ] Concatenate the header keywords in the CHDU into a single long
    string of characters. Each 80-character fixed-length keyword
    record is appended to the output character string, in order, with
    no intervening separator or terminating characters. The last header
    record is terminated with a NULL character.  This routine allocates
    memory for the returned character array, so the calling program must
    free the memory when finished.

    There are 2 related routines: fits\_hdr2str simply concatenates all
    the existing keywords in the header; fits\_convert\_hdr2str is similar,
    except that if the CHDU is a tile compressed image (stored in a binary
    table) then it will first convert that header back to that of a
    normal FITS image before concatenating the keywords.

    Selected keywords may be excluded from the returned character string.
    If the second parameter (nocomments) is TRUE (nonzero) then any
    COMMENT, HISTORY, or blank keywords in the header will not be copied
    to the output string.

    The 'exclist' parameter may be used to supply a list of keywords
    that are to be excluded from the output character string. Wild card
    characters (*, ?, and \#) may be used in the excluded keyword names.
    If no additional keywords are to be excluded, then set nexc = 0 and
   specify NULL for the the **exclist  parameter.  \label{hdr2str}
\end{description}

\begin{verbatim}
  int fits_hdr2str
      (fitsfile *fptr, int nocomments, char **exclist, int nexc,
      > char **header, int *nkeys, int *status)

  int fits_convert_hdr2str / ffcnvthdr2str
      (fitsfile *fptr, int nocomments, char **exclist, int nexc,
      > char **header, int *nkeys, int *status)
\end{verbatim}


\begin{description}
\item[2 ]  The following CFITSIO routine is specifically designed for use
in conjunction with the WCSLIB library.  It is not expected that
applications programmers will call this routine directly, but it
is documented here for completeness.  This routine extracts arrays
from a binary table that contain WCS information using the -TAB table
lookup convention.  See the documentation provided with the WCSLIB
 library for more information.  \label{wcstab}
\end{description}

\begin{verbatim}
  int fits_read_wcstab
       (fitsfile *fptr, int nwtb, wtbarr *wtb, int *status);
\end{verbatim}

\section{ Self-contained WCS Routines}

The following routines DO NOT support the more recent WCS conventions
that have been approved as part of the FITS standard.  Consequently,
the following routines ARE NOW DEPRECATED.  It is STRONGLY RECOMMENDED
that software developers not use these routines, and instead use an
external WCS library, as described in the previous section.

These routines are included mainly for backward compatibility with
existing software.  They support the following standard map
projections: -SIN, -TAN, -ARC, -NCP, -GLS, -MER, and -AIT (these are the
legal values for the coordtype parameter).  These routines are based
on similar functions in Classic AIPS.  All the angular quantities are
given in units of degrees.


\begin{description}
\item[1 ] Get the values of the basic set of standard FITS celestial coordinate
    system keywords from the header of a FITS image (i.e., the primary
    array or an IMAGE extension).  These values may then be passed to
    the fits\_pix\_to\_world and fits\_world\_to\_pix routines that
    perform the coordinate transformations.  If any or all of the WCS
    keywords are not present, then default values will be returned. If
    the first coordinate axis is the declination-like coordinate, then
    this routine will swap them so that the longitudinal-like coordinate
    is returned as the first axis.

    The first routine (ffgics) returns
    the primary WCS, whereas the second routine returns the particular
    version of the WCS specified by the 'version' parameter, which much
    be a character ranging from 'A' to 'Z' (or a blank character, which is
    equivalent to calling ffgics).

    If the file uses the newer 'CDj\_i' WCS transformation matrix
    keywords instead of old style 'CDELTn' and 'CROTA2' keywords, then
    this routine will calculate and return the values of the equivalent
    old-style keywords.  Note that the conversion from the new-style
    keywords to the old-style values is sometimes only an
    approximation, so if the approximation is larger than an internally
    defined threshold level, then CFITSIO will still return the
    approximate WCS keyword values, but will also return with status =
    APPROX\_WCS\_KEY, to warn the calling program that approximations
    have been made.  It is then up to the calling program to decide
    whether the approximations are sufficiently accurate for the
    particular application, or whether more precise WCS transformations
   must be performed using new-style WCS keywords directly. \label{ffgics}
\end{description}

\begin{verbatim}
  int fits_read_img_coord / ffgics
      (fitsfile *fptr, > double *xrefval, double *yrefval,
       double *xrefpix, double *yrefpix, double *xinc, double *yinc,
       double *rot, char *coordtype, int *status)

  int fits_read_img_coord_version / ffgicsa
      (fitsfile *fptr, char version, > double *xrefval, double *yrefval,
       double *xrefpix, double *yrefpix, double *xinc, double *yinc,
       double *rot, char *coordtype, int *status)
\end{verbatim}

\begin{description}
\item[2 ] Get the values of the standard FITS celestial coordinate system
    keywords from the header of a FITS table where the X and Y (or RA
    and DEC) coordinates are stored in 2 separate columns of the table
    (as in the Event List table format that is often used by high energy
    astrophysics missions).  These values may then be passed to the
    fits\_pix\_to\_world and fits\_world\_to\_pix routines that perform
   the coordinate transformations. \label{ffgtcs}
\end{description}

\begin{verbatim}
  int fits_read_tbl_coord / ffgtcs
      (fitsfile *fptr, int xcol, int ycol, > double *xrefval,
       double *yrefval, double *xrefpix, double *yrefpix, double *xinc,
       double *yinc, double *rot, char *coordtype, int *status)
\end{verbatim}

\begin{description}
\item[3 ]  Calculate the celestial coordinate corresponding to the input
    X and Y pixel location in the image. \label{ffwldp}
\end{description}

\begin{verbatim}
  int fits_pix_to_world / ffwldp
      (double xpix, double ypix, double xrefval, double yrefval,
       double xrefpix, double yrefpix, double xinc, double yinc,
       double rot, char *coordtype, > double *xpos, double *ypos,
       int *status)
\end{verbatim}

\begin{description}
\item[4 ]  Calculate the X and Y pixel location corresponding to the input
    celestial coordinate in the image. \label{ffxypx}
\end{description}

\begin{verbatim}
  int fits_world_to_pix / ffxypx
      (double xpos, double ypos, double xrefval, double yrefval,
       double xrefpix, double yrefpix, double xinc, double yinc,
       double rot, char *coordtype, > double *xpix, double *ypix,
       int *status)
\end{verbatim}


\chapter{  Hierarchical Grouping Routines }

These functions allow for the creation and manipulation of FITS HDU
Groups, as defined in "A Hierarchical Grouping Convention for FITS" by
Jennings, Pence, Folk and Schlesinger:

https://fits.gsfc.nasa.gov/registry/grouping/grouping.pdf

A group is a
collection of HDUs whose association is defined by a {\it grouping
table}.  HDUs which are part of a group are referred to as {\it member
HDUs} or simply as {\it members}. Grouping table member HDUs may
themselves be grouping tables, thus allowing for the construction of
open-ended hierarchies of HDUs.

Grouping tables contain one row for each member HDU. The grouping table
columns provide identification information that allows applications to
reference or "point to" the member HDUs. Member HDUs are expected, but
not required, to contain a set of GRPIDn/GRPLCn keywords in their
headers for each grouping table that they are referenced by. In this
sense, the GRPIDn/GRPLCn keywords "link" the member HDU back to its
Grouping table. Note that a member HDU need not reside in the same FITS
file as its grouping table, and that a given HDU may be referenced by
up to 999 grouping tables simultaneously.

Grouping tables are implemented as FITS binary tables with up to six
pre-defined column TTYPEn values: 'MEMBER\_XTENSION', 'MEMBER\_NAME',
'MEMBER\_VERSION', 'MEMBER\_POSITION', 'MEMBER\_URI\_TYPE' and 'MEMBER\_LOCATION'.
The first three columns allow member HDUs to be identified by reference to
their XTENSION, EXTNAME and EXTVER keyword values. The fourth column allows
member HDUs to be identified by HDU position within their FITS file.
The last two columns identify the FITS file in which the member HDU resides,
if different from the grouping table FITS file.

Additional user defined "auxiliary" columns may also be included with any
grouping table. When a grouping table is copied or modified the presence of
auxiliary columns is always taken into account by the grouping support
functions; however, the grouping support functions cannot directly
make use of this data.

If a grouping table column is defined but the corresponding member HDU
information is unavailable then a null value of the appropriate data type
is inserted in the column field. Integer columns (MEMBER\_POSITION,
MEMBER\_VERSION) are defined with a TNULLn value of zero (0). Character field
columns (MEMBER\_XTENSION, MEMBER\_NAME, MEMBER\_URI\_TYPE, MEMBER\_LOCATION)
utilize an ASCII null character to denote a null field value.

The grouping support functions belong to two basic categories: those that
work with grouping table HDUs (ffgt**) and those that work with member HDUs
(ffgm**). Two functions, fits\_copy\_group() and fits\_remove\_group(), have the
option to recursively copy/delete entire groups. Care should be taken when
employing these functions in recursive mode as poorly defined groups could
cause unpredictable results. The problem of a grouping table directly or
indirectly referencing itself (thus creating an infinite loop) is protected
against; in fact, neither function will attempt to copy or delete an HDU
twice.


\section{Grouping Table Routines}


\begin{description}
\item[1 ]Create (append) a grouping table at the end of the current FITS file
   pointed to by fptr. The grpname parameter provides the grouping table
   name (GRPNAME keyword value) and may be set to NULL if no group name
   is to be specified. The grouptype parameter specifies the desired
   structure of the grouping table and may take on the values:
   GT\_ID\_ALL\_URI (all columns created), GT\_ID\_REF (ID by reference columns),
   GT\_ID\_POS (ID by position columns), GT\_ID\_ALL (ID by reference and
   position columns), GT\_ID\_REF\_URI (ID by reference and FITS file URI
  columns), and GT\_ID\_POS\_URI (ID by position and FITS file URI columns). \label{ffgtcr}
\end{description}

\begin{verbatim}
  int fits_create_group / ffgtcr
      (fitsfile *fptr, char *grpname, int grouptype, > int *status)
\end{verbatim}

\begin{description}
\item[2 ]Create (insert) a grouping table just after the CHDU of the current FITS
   file pointed to by fptr. All HDUs below the the insertion point will be
   shifted downwards to make room for the new HDU. The grpname parameter
   provides the grouping table name (GRPNAME keyword value) and may be set to
   NULL if no group name is to be specified. The grouptype parameter specifies
   the desired structure of the grouping table and may take on the values:
   GT\_ID\_ALL\_URI (all columns created), GT\_ID\_REF (ID by reference columns),
   GT\_ID\_POS (ID by position columns), GT\_ID\_ALL (ID by reference and
   position columns), GT\_ID\_REF\_URI (ID by reference and FITS file URI
  columns), and GT\_ID\_POS\_URI (ID by position and FITS file URI columns) \label{ffgtis}.
\end{description}

\begin{verbatim}
  int fits_insert_group / ffgtis
      (fitsfile *fptr, char *grpname, int grouptype, > int *status)
\end{verbatim}

\begin{description}
\item[3 ]Change the structure of an existing grouping table pointed to by
   gfptr. The grouptype parameter (see fits\_create\_group() for valid
   parameter values) specifies the new structure of the grouping table. This
   function only adds or removes grouping table columns, it does not add
   or delete group members (i.e., table rows). If the grouping table already
   has the desired structure then no operations are performed and function
   simply returns with a (0) success status code. If the requested structure
   change creates new grouping table columns, then the column values for all
   existing members will be filled with the null values appropriate to the
  column type. \label{ffgtch}
\end{description}

\begin{verbatim}
  int fits_change_group / ffgtch
      (fitsfile *gfptr, int grouptype, > int *status)
\end{verbatim}

\begin{description}
\item[4 ]Remove the group defined by the grouping table pointed to by gfptr, and
   optionally all the group member HDUs. The rmopt parameter specifies the
   action to be taken for
   all members of the group defined by the grouping table. Valid values are:
   OPT\_RM\_GPT (delete only the grouping table) and OPT\_RM\_ALL (recursively
   delete all HDUs that belong to the group). Any groups containing the
   grouping table gfptr as a member are updated, and if rmopt == OPT\_RM\_GPT
   all members have their GRPIDn and GRPLCn  keywords updated accordingly.
   If rmopt == OPT\_RM\_ALL, then other groups that contain the deleted members
  of gfptr are updated to reflect the deletion accordingly. \label{ffgtrm}
\end{description}

\begin{verbatim}
  int fits_remove_group / ffgtrm
      (fitsfile *gfptr, int rmopt, > int *status)
\end{verbatim}

\begin{description}
\item[5 ]Copy (append) the group defined by the grouping table pointed to by infptr,
   and optionally all group member HDUs, to the FITS file pointed to by
   outfptr. The cpopt parameter specifies the action to be taken for all
   members of the group infptr. Valid values are: OPT\_GCP\_GPT (copy only
   the grouping table) and OPT\_GCP\_ALL (recursively copy ALL the HDUs that
   belong to the group defined by infptr). If the cpopt == OPT\_GCP\_GPT then
   the members of infptr have their GRPIDn and GRPLCn keywords updated to
   reflect the existence of the new grouping table outfptr, since they now
   belong to the new group. If cpopt == OPT\_GCP\_ALL then the new
   grouping table outfptr only contains pointers to the copied member HDUs
   and not the original member HDUs of infptr. Note that, when
   cpopt == OPT\_GCP\_ALL, all members of the group defined by infptr will be
   copied to a single FITS file pointed to by outfptr regardless of their
  file distribution in the original group.  \label{ffgtcp}
\end{description}

\begin{verbatim}
  int fits_copy_group / ffgtcp
      (fitsfile *infptr, fitsfile *outfptr, int cpopt, > int *status)
\end{verbatim}

\begin{description}
\item[6 ] Merge the two groups defined by the grouping table HDUs infptr and outfptr
    by combining their members into a single grouping table. All member HDUs
    (rows) are copied from infptr to outfptr. If mgopt == OPT\_MRG\_COPY then
    infptr continues to exist unaltered after the merge. If the mgopt ==
    OPT\_MRG\_MOV then infptr is deleted after the merge. In both cases,
   the GRPIDn and GRPLCn keywords of the member HDUs are updated accordingly. \label{ffgtmg}
\end{description}

\begin{verbatim}
  int fits_merge_groups / ffgtmg
      (fitsfile *infptr, fitsfile *outfptr, int mgopt, > int *status)
\end{verbatim}

\begin{description}
\item[7 ]"Compact" the group defined by grouping table pointed to by gfptr. The
   compaction is achieved by merging (via fits\_merge\_groups()) all direct
   member HDUs of gfptr that are themselves grouping tables. The cmopt
   parameter defines whether the merged grouping table HDUs remain after
   merging (cmopt == OPT\_CMT\_MBR) or if they are deleted after merging
   (cmopt == OPT\_CMT\_MBR\_DEL). If the grouping table contains no direct
   member HDUs that are themselves grouping tables then this function
   does nothing. Note that this function is not recursive, i.e., only the
  direct member HDUs of gfptr are considered for merging. \label{ffgtcm}
\end{description}

\begin{verbatim}
  int fits_compact_group / ffgtcm
      (fitsfile *gfptr, int cmopt, > int *status)
\end{verbatim}

\begin{description}
\item[8 ]Verify the integrity of the grouping table pointed to by gfptr to make
   sure that all group members are accessible and that all links to other
   grouping tables are valid. The firstfailed parameter returns the member
   ID (row number) of the first member HDU to fail verification (if positive
   value) or the first group link to fail (if negative value). If gfptr is
  successfully verified then firstfailed contains a return value of 0. \label{ffgtvf}
\end{description}

\begin{verbatim}
  int fits_verify_group / ffgtvf
      (fitsfile *gfptr, > long *firstfailed, int *status)
\end{verbatim}

\begin{description}
\item[9 ] Open a grouping table that contains the member HDU pointed to by mfptr.
    The grouping table to open is defined by the grpid parameter, which
    contains the keyword index value of the GRPIDn/GRPLCn keyword(s) that
    link the member HDU mfptr to the grouping table. If the grouping table
    resides in a file other than the member HDUs file then an attempt is
    first made to open the file readwrite, and failing that readonly. A
    pointer to the opened grouping table HDU is returned in gfptr.

    Note that it is possible, although unlikely and undesirable, for the
    GRPIDn/GRPLCn keywords in a member HDU header to be non-continuous, e.g.,
    GRPID1, GRPID2, GRPID5, GRPID6. In such cases, the grpid index value
    specified in the function call shall identify the (grpid)th GRPID value.
    In the above example, if grpid == 3, then the group specified by GRPID5
   would be opened. \label{ffgtop}
\end{description}

\begin{verbatim}
  int fits_open_group / ffgtop
      (fitsfile *mfptr, int grpid, > fitsfile **gfptr, int *status)
\end{verbatim}

\begin{description}
\item[10]  Add a member HDU to an existing grouping table pointed to by gfptr.
   The member HDU may either be pointed to mfptr (which must be positioned
   to the member HDU) or, if mfptr == NULL, identified by the hdupos parameter
   (the HDU position number, Primary array == 1) if both the grouping table
   and the member HDU reside in the same FITS file. The new member HDU shall
   have the appropriate GRPIDn and GRPLCn keywords created in its header.
   Note that if the member HDU is already a member of the group then it will
  not be added a second time. \label{ffgtam}
\end{description}

\begin{verbatim}
  int fits_add_group_member / ffgtam
      (fitsfile *gfptr, fitsfile *mfptr, int hdupos, > int *status)
\end{verbatim}


\section{Group Member Routines}


\begin{description}
\item[1 ] Return the number of member HDUs in a grouping table gfptr. The number
    of member HDUs is just the NAXIS2 value (number of rows) of the grouping
   table. \label{ffgtnm}
\end{description}

\begin{verbatim}
  int fits_get_num_members / ffgtnm
      (fitsfile *gfptr, > long *nmembers, int *status)
\end{verbatim}

\begin{description}
\item[2 ]  Return the number of groups to which the HDU pointed to by mfptr is
     linked, as defined by the number of GRPIDn/GRPLCn keyword records that
     appear in its header. Note that each time this function is called, the
     indices of the GRPIDn/GRPLCn keywords are checked to make sure they
     are continuous (ie no gaps) and are re-enumerated to eliminate gaps if
    found.  \label{ffgmng}
\end{description}

\begin{verbatim}
  int fits_get_num_groups / ffgmng
      (fitsfile *mfptr, > long *nmembers, int *status)
\end{verbatim}

\begin{description}
\item[3 ] Open a member of the grouping table pointed to by gfptr. The member to
    open is identified by its row number within the grouping table as given
    by the parameter 'member' (first member == 1) . A fitsfile pointer to
    the opened member HDU is returned as mfptr. Note that if the member HDU
    resides in a FITS file different from the grouping table HDU then the
   member file is first opened readwrite and, failing this, opened readonly. \label{ffgmop}
\end{description}

\begin{verbatim}
  int fits_open_member / ffgmop
      (fitsfile *gfptr, long member, > fitsfile **mfptr, int *status)
\end{verbatim}

\begin{description}
\item[4 ]Copy (append) a member HDU of the grouping table pointed to by gfptr.
   The member HDU is identified by its row number within the grouping table
   as given by the parameter 'member' (first member == 1). The copy of the
   group member HDU will be appended to the FITS file pointed to by mfptr,
   and upon return mfptr shall point to the copied member HDU. The cpopt
   parameter may take on the following values: OPT\_MCP\_ADD which adds a new
   entry in gfptr for the copied member HDU, OPT\_MCP\_NADD which does not add
   an entry in gfptr for the copied member, and OPT\_MCP\_REPL which replaces
  the original member entry with the copied member entry. \label{ffgmcp}
\end{description}

\begin{verbatim}
  int fits_copy_member / ffgmcp
      (fitsfile *gfptr, fitsfile *mfptr, long member, int cpopt, > int *status)
\end{verbatim}

\begin{description}
\item[5 ]Transfer a group member HDU from the grouping table pointed to by
   infptr to the grouping table pointed to by outfptr. The member HDU to
   transfer is identified by its row number within infptr as specified by
   the parameter 'member' (first member == 1). If tfopt == OPT\_MCP\_ADD then
   the member HDU is made
   a member of outfptr and remains a member of infptr. If tfopt == OPT\_MCP\_MOV
  then the member HDU is deleted from infptr after the transfer to outfptr. \label{ffgmtf}
\end{description}

\begin{verbatim}
  int fits_transfer_member / ffgmtf
      (fitsfile *infptr, fitsfile *outfptr, long member, int tfopt,
       > int *status)
\end{verbatim}

\begin{description}
\item[6 ]Remove a member HDU from the grouping table pointed to by gfptr. The
   member HDU to be deleted is identified by its row number in the grouping
   table as specified by the parameter 'member' (first member == 1). The rmopt
   parameter may take on the following values: OPT\_RM\_ENTRY which
   removes the member HDU entry from the grouping table and updates the
   member's GRPIDn/GRPLCn keywords, and OPT\_RM\_MBR which removes the member
  HDU entry from the grouping table and deletes the member HDU itself. \label{ffgmrm}
\end{description}

\begin{verbatim}
  int fits_remove_member / ffgmrm
      (fitsfile *gfptr, long member, int rmopt, > int *status)
\end{verbatim}

\chapter{ Specialized CFITSIO Interface Routines }

The basic interface routines described previously are recommended
for most uses, but the routines described in this chapter
are also available if necessary.  Some of these routines perform more
specialized function that cannot easily be done with the basic
interface routines while others duplicate the functionality of the
basic routines but have a slightly different calling sequence.
See Appendix B for the definition of each function parameter.


\section{FITS File Access Routines}

\subsection{File Access}

\begin{description}
\item[1 ] Open an existing FITS file residing in core computer memory.  This
routine is analogous to fits\_open\_file.   The 'filename'  is
currently ignored by this routine and may be any arbitrary string. In
general, the application must have preallocated an initial block of
memory to hold the FITS file prior to calling this routine:  'memptr'
points to the starting address and 'memsize' gives the initial size of
the block of memory.  'mem\_realloc' is a pointer to an optional
function that CFITSIO can call to allocate additional memory, if needed
(only if mode = READWRITE), and is modeled after the standard C
'realloc' function;  a null pointer may be given if the initial
allocation of memory is all that will be required (e.g., if the file is
opened with mode = READONLY).  The 'deltasize' parameter may be used to
suggest a minimum amount of additional memory that should be allocated
during each call to the memory reallocation function.  By default,
CFITSIO will reallocate enough additional space to hold the entire
currently defined FITS file (as given by the NAXISn keywords) or 1 FITS
block (= 2880 bytes), which ever is larger.  Values of deltasize less
than 2880 will be ignored.  Since the memory reallocation operation can
be computationally expensive, allocating a larger initial block of
memory, and/or specifying a larger deltasize value may help to reduce
the number of reallocation calls and make the application program run
faster.  Note that values of the memptr and memsize pointers will be updated
by CFITSIO if the location or size of the FITS file in memory
should change as a result of allocating more memory. \label{ffomem}
\end{description}

\begin{verbatim}
  int fits_open_memfile / ffomem
      (fitsfile **fptr, const char *filename, int mode, void **memptr,
       size_t *memsize, size_t deltasize,
       void *(*mem_realloc)(void *p, size_t newsize), int *status)
\end{verbatim}

\begin{description}
\item[2 ] Create a new FITS file residing in core computer memory.  This
routine is analogous to fits\_create\_file.    In general, the
application must have preallocated an initial block of memory to hold
the FITS file prior to calling this routine:  'memptr' points to the
starting address and 'memsize' gives the initial size of the block of
memory.  'mem\_realloc' is a pointer to an optional function that
CFITSIO can call to allocate additional memory, if needed, and is
modeled after the standard C 'realloc' function;  a null pointer may be
given if the initial allocation of memory is all that will be
required.  The 'deltasize' parameter may be used to suggest a minimum
amount of additional memory that should be allocated during each call
to the memory reallocation function.  By default, CFITSIO will
reallocate enough additional space to hold 1 FITS block (= 2880 bytes)
and  values of deltasize less than 2880 will be ignored.  Since the
memory reallocation operation can be computationally expensive,
allocating a larger initial block of memory, and/or specifying a larger
deltasize value may help to reduce the number of reallocation calls
and make the application program run
faster. Note that values of the memptr and memsize pointers will be updated
by CFITSIO if the location or size of the FITS file in memory
should change as a result of allocating more memory. \label{ffimem}
\end{description}

\begin{verbatim}
  int fits_create_memfile / ffimem
      (fitsfile **fptr, void **memptr,
       size_t *memsize, size_t deltasize,
       void *(*mem_realloc)(void *p, size_t newsize), int *status)
\end{verbatim}

\begin{description}
\item[3 ] Reopen a FITS file that was previously opened with
    fits\_open\_file or fits\_create\_file.  The new fitsfile
    pointer may then be treated as a separate file, and one may
    simultaneously read or write to 2 (or more)  different extensions in
    the same file.   The fits\_open\_file routine (above) automatically
    detects cases where a previously opened file is being opened again,
    and then internally call fits\_reopen\_file, so programs should rarely
    need to explicitly call this routine.
\label{ffreopen}
\end{description}

\begin{verbatim}
  int fits_reopen_file / ffreopen
      (fitsfile *openfptr, fitsfile **newfptr, > int *status)
\end{verbatim}


\begin{description}
\item[4 ]  Create a new FITS file, using a template file to define its
  initial size and structure.  The template may be another FITS HDU
  or an ASCII template file.  If the input template file name pointer
  is null, then this routine behaves the same as fits\_create\_file.
  The currently supported format of the ASCII template file is described
  under the fits\_parse\_template routine (in the general Utilities
  section)
\label{fftplt}
\end{description}

\begin{verbatim}
  int fits_create_template / fftplt
      (fitsfile **fptr, char *filename, char *tpltfile > int *status)
\end{verbatim}


\begin{description}
\item[5 ] Parse the input filename or URL into its component parts, namely:
\begin{itemize}
\item
the file type (file://, ftp://, http://, etc),
\item
the base input file name,
\item
the name of the output file that the input file is to be copied to prior
to opening,
\item
the HDU or extension specification,
\item
the filtering specifier,
\item
the binning specifier,
\item
the column specifier,
\item
and the
image pixel filtering specifier.
\end{itemize}
A null pointer (0) may be be specified for any of the output string arguments
that are not needed.  Null strings will be returned for any components that are not
present in the input file name.  The calling routine must allocate sufficient
memory to hold the returned character strings.  Allocating the string lengths
equal to FLEN\_FILENAME is guaranteed to be safe.
These routines are mainly for internal use
by other CFITSIO routines. \label{ffiurl}
\end{description}

\begin{verbatim}
  int fits_parse_input_url / ffiurl
      (char *filename, > char *filetype, char *infile, char *outfile, char
       *extspec, char *filter, char *binspec, char *colspec, int *status)

  int fits_parse_input_filename / ffifile
      (char *filename, > char *filetype, char *infile, char *outfile, char
       *extspec, char *filter, char *binspec, char *colspec, char *pixspec,
       int *status)
\end{verbatim}

\begin{description}
\item[6 ] Parse the input filename and return the HDU number that would be
moved to if the file were opened with fits\_open\_file.  The returned
HDU number begins with 1 for the primary array, so for example, if the
input filename = `myfile.fits[2]' then hdunum = 3 will be returned.
CFITSIO does not open the file to check if the extension actually
exists if an extension number is specified. If an extension name is
included in the file name specification (e.g.  `myfile.fits[EVENTS]'
then this routine will have to open the FITS file and look for the
position of the named extension, then close file again.  This is not
possible if the file is being read from the stdin stream, and an error
will be returned in this case.  If the filename does not specify an
explicit extension (e.g. 'myfile.fits') then hdunum = -99 will be
returned, which is functionally equivalent to hdunum = 1. This routine
is mainly used for backward compatibility in the ftools software
package and is not recommended for general use.  It is generally better
and more efficient to first open the FITS file with fits\_open\_file,
then use fits\_get\_hdu\_num to determine which HDU in the file has
been opened, rather than calling fits\_parse\_input\_url followed by a
call to fits\_open\_file.
 \label{ffextn}
\end{description}

\begin{verbatim}
   int fits_parse_extnum / ffextn
       (char *filename, > int *hdunum, int *status)
\end{verbatim}

\begin{description}
\item[7 ]Parse the input file name and return the root file name.  The root
name includes the file type if specified, (e.g.  'ftp://' or 'http://')
and the full path name, to the extent that it is specified in the input
filename.  It does not include the HDU name or number, or any filtering
specifications. The calling routine must allocate sufficient
memory to hold the returned rootname character string.  Allocating the length
equal to FLEN\_FILENAME is guaranteed to be safe.
 \label{ffrtnm}
\end{description}

\begin{verbatim}
   int fits_parse_rootname / ffrtnm
       (char *filename, > char *rootname, int *status);
\end{verbatim}

\begin{description}
\item[8 ]Test if the input file or a compressed version of the file (with
a .gz, .Z, .z, or .zip extension) exists on disk.  The returned value of
the 'exists' parameter will have 1 of the 4 following values:

\begin{verbatim}
   2:  the file does not exist, but a compressed version does exist
   1:  the disk file does exist
   0:  neither the file nor a compressed version of the file exist
  -1:  the input file name is not a disk file (could be a ftp, http,
       smem, or mem file, or a file piped in on the STDIN stream)
\end{verbatim}

 \label{ffexist}
\end{description}

\begin{verbatim}
   int fits_file_exists / ffexist
       (char *filename, > int *exists, int *status);
\end{verbatim}

\begin{description}
\item[9 ]Flush any internal buffers of data to the output FITS file. These
   routines rarely need to be called, but can be useful in cases where
   other processes need to access the same FITS file in real time,
   either on disk or in memory.  These routines also help to ensure
   that if the application program subsequently aborts then the FITS
   file will have been closed properly.  The first routine,
   fits\_flush\_file is more rigorous and completely closes, then
   reopens, the current HDU, before flushing the internal buffers, thus
   ensuring that the output FITS file is identical to what would be
   produced if the FITS was closed at that point (i.e., with a call to
   fits\_close\_file).  The second routine, fits\_flush\_buffer simply
   flushes the internal CFITSIO buffers of data to the output FITS
   file, without updating and closing the current HDU.  This is much
   faster, but there may be circumstances where the flushed file does
   not completely reflect the final state of the file as it will exist
   when the file is actually closed.

   A typical use of these routines would be to flush the state of a
   FITS table to disk after each row of the table is written.  It is
   recommend that fits\_flush\_file be called after the first row is
   written, then fits\_flush\_buffer may be called after each
   subsequent row is written.  Note that this latter routine will not
   automatically update the NAXIS2 keyword which records the number of
   rows of data in the table, so this keyword must be explicitly
   updated by the application program after each row is written.
  \label{ffflus}
\end{description}

\begin{verbatim}
  int fits_flush_file / ffflus
      (fitsfile *fptr, > int *status)

  int fits_flush_buffer / ffflsh
      (fitsfile *fptr, 0, > int *status)

      (Note:  The second argument must be 0).
\end{verbatim}

\begin{description}
\item[10 ] Wrapper functions for global initialization and cleanup of the libcurl library used
   when accessing files with the HTTPS or FTPS protocols.  If an HTTPS/FTPS file transfer is to
   be performed, it is recommended that you call the init function once near the start of your 
   program before any file\_open calls, and before creating any threads. The cleanup function
   should be called after all HTTPS/FTPS file accessing is completed, and after all threads are 
   completed.  The functions return 0 upon successful initialization and cleanup.  These
   are NOT THREAD-SAFE.    
  \label{ffihtps}
\end{description}

\begin{verbatim}
  int fits_init_https / ffihtps
      ()
      
  int fits_cleanup_https / ffchtps
      ()
\end{verbatim}

\subsection{Download Utility Functions}

These routines do not need to be called for normal file accessing. They are
primarily intended to help with debugging and diagnosing issues which occur 
during file downloads.  These routines are NOT THREAD-SAFE.

\begin{description}
\item[1 ] Toggle the verbosity of the libcurl library diagnostic output when
   accessing files with the HTTPS or FTPS protocol.  `flag' = 1 turns the output
   on, 0 turns it off (the default).
  \label{ffvhtps}
\end{description}

\begin{verbatim}
   void fits_verbose_https / ffvhtps
        (int flag)
\end{verbatim}

\begin{description}
\item[2] If `flag' is set to 1, this will display (to stderr) a progress 
  bar during an https file download.  (This is not yet implemented
  for other file transfer protocols.)  `flag' = 0 by default.
 \label{ffshdwn}
\end{description}

\begin{verbatim}
   void fits_show_download_progress / ffshdwn
        (int flag)
\end{verbatim}

\begin{description}
\item[3] The timeout setting (in seconds) determines the maximum time
  allowed for a net download to complete.  If a download has not finished within
  the allowed time, the file transfer will terminate and the CFITSIO calling
  function will return with an error.  Use fits\_get\_timeout will see the 
  current timeout setting and fits\_set\_timeout to change the setting.  This
  adjustmant may be particularly useful when having trouble downloading large 
  files over slow connections.
 \label{ffgtmo}
\end{description}

\begin{verbatim}
   int fits_get_timeout / ffgtmo
       ()
       
   int fits_set_timeout / ffstmo
       (int seconds, > int *status)
\end{verbatim}

\section{HDU Access Routines}


\begin{description}
\item[1 ] Get the byte offsets in the FITS file to the start of the header
    and the start and end of the data in the CHDU. The difference
    between headstart and dataend equals the size of the CHDU.  If the
    CHDU is the last HDU in the file, then dataend is also equal to the
    size of the entire FITS file.  Null pointers may be input for any
   of the address parameters if their values are not needed. \label{ffghad}
\end{description}

\begin{verbatim}
  int fits_get_hduaddr / ffghad  (only supports files up to 2.1 GB in size)
       (fitsfile *fptr, > long *headstart, long *datastart, long *dataend,
        int *status)

  int fits_get_hduaddrll / ffghadll  (supports large files)
       (fitsfile *fptr, > LONGLONG *headstart, LONGLONG *datastart,
        LONGLONG *dataend, int *status)
\end{verbatim}

\begin{description}
\item[2 ] Create (append) a new empty HDU at the end of the FITS file.
    This is now  the CHDU but it is completely empty and has
    no header keywords.  It is recommended that fits\_create\_img or
 fits\_create\_tbl be used instead of this routine. \label{ffcrhd}
\end{description}

\begin{verbatim}
  int fits_create_hdu / ffcrhd
      (fitsfile *fptr, > int *status)
\end{verbatim}

\begin{description}
\item[3 ] Insert a new IMAGE extension immediately following the CHDU, or
    insert a new Primary Array at the beginning of the file.  Any
    following extensions in the file will be shifted down to make room
    for the new extension.  If the CHDU is the last HDU in the file
    then the new image extension will simply be appended to the end of
    the file.   One can force a new primary array to be inserted at the
    beginning of the FITS file by setting status = PREPEND\_PRIMARY prior
    to calling the routine.  In this case the old primary array will be
    converted to an IMAGE extension. The new extension (or primary
    array) will become the CHDU.  Refer to Chapter 9 for a list of
   pre-defined bitpix values.  \label{ffiimg}
\end{description}

\begin{verbatim}
  int fits_insert_img / ffiimg
      (fitsfile *fptr, int bitpix, int naxis, long *naxes, > int *status)

  int fits_insert_imgll / ffiimgll
      (fitsfile *fptr, int bitpix, int naxis, LONGLONG *naxes, > int *status)
\end{verbatim}

\begin{description}
\item[4 ] Insert a new ASCII or binary table extension immediately following the CHDU.
    Any following extensions will be shifted down to make room for the
    new extension.  If there are no other following extensions then the
    new table extension will simply be appended to the end of the
    file.   If the FITS file is currently empty then this routine will
    create a dummy primary array before appending the table to it. The
    new extension will become the CHDU.  The tunit and extname
    parameters are optional and a null pointer may be given if they are
    not defined.  When inserting an ASCII table with
    fits\_insert\_atbl, a null pointer  may given for the *tbcol
    parameter in which case each column of the table will be separated
    by a single space character. Similarly, if the input value of
    rowlen is  0, then CFITSIO will calculate the default rowlength
    based on the tbcol and ttype values.  Under normal circumstances,
    the nrows
    paramenter should have a value of 0; CFITSIO will automatically update
    the number of rows as data is written to the table.  When inserting a binary table
    with fits\_insert\_btbl, if there are following extensions in the
    file and if the table contains variable length array columns then
    pcount must specify the expected final size of the data heap,
   otherwise pcount must = 0. \label{ffitab} \label{ffibin}
\end{description}

\begin{verbatim}
  int fits_insert_atbl / ffitab
      (fitsfile *fptr, LONGLONG rowlen, LONGLONG nrows, int tfields, char *ttype[],
       long *tbcol, char *tform[], char *tunit[], char *extname, > int *status)

  int fits_insert_btbl / ffibin
      (fitsfile *fptr, LONGLONG nrows, int tfields, char **ttype,
      char **tform, char **tunit, char *extname, long pcount, > int *status)
\end{verbatim}

\begin{description}
\item[5 ] Modify the size, dimensions, and/or data type of the current
    primary array or image extension. If the new image, as specified
    by the input arguments, is larger than the current existing image
    in the FITS file then zero fill data will be inserted at the end
    of the current image and any following extensions will be moved
    further back in the file.  Similarly, if the new image is
    smaller than the current image then any following extensions
    will be shifted up towards the beginning of the FITS file
    and the image data will be truncated to the new size.
    This routine rewrites the BITPIX, NAXIS, and NAXISn keywords
   with the appropriate values for the new image. \label{ffrsim}
\end{description}

\begin{verbatim}
  int fits_resize_img / ffrsim
      (fitsfile *fptr, int bitpix, int naxis, long *naxes, > int *status)

  int fits_resize_imgll / ffrsimll
      (fitsfile *fptr, int bitpix, int naxis, LONGLONG *naxes, > int *status)
\end{verbatim}

\begin{description}
\item[6 ] Copy the data (and not the header) from the CHDU associated with infptr
    to the CHDU associated with outfptr. This will overwrite any data
    previously in the output CHDU.  This low level routine is used by
    fits\_copy\_hdu, but it may also be useful in certain application programs
    that want to copy the data from one FITS file to another but also
    want to modify the header keywords. The required FITS header keywords
    which define the structure of the HDU must be written to the
   output CHDU before calling this routine. \label{ffcpdt}
\end{description}

\begin{verbatim}
  int fits_copy_data / ffcpdt
      (fitsfile *infptr, fitsfile *outfptr, > int *status)
\end{verbatim}

\begin{description}
\item[7 ] Read or write a specified number of bytes starting at the specified byte
    offset from the start of the extension data unit.  These low
    level routine are intended mainly for accessing the data in
    non-standard, conforming extensions, and should not be used for standard
   IMAGE, TABLE, or BINTABLE extensions. \label{ffgextn}
\end{description}

\begin{verbatim}
  int fits_read_ext / ffgextn
      (fitsfile *fptr, LONGLONG offset, LONGLONG nbytes, void *buffer)
  int fits_write_ext / ffpextn
      (fitsfile *fptr, LONGLONG offset, LONGLONG nbytes, void *buffer)
\end{verbatim}

\begin{description}
\item[8 ] This routine forces CFITSIO to rescan the current header keywords that
    define the structure of the HDU (such as the NAXIS and BITPIX
    keywords) so that it reinitializes the internal buffers that
    describe the HDU structure.  This routine is useful for
    reinitializing the structure of an HDU if any of the required
    keywords (e.g., NAXISn) have been modified.  In practice it should
    rarely be necessary to call this routine because CFITSIO
   internally calls it in most situations. \label{ffrdef}
\end{description}

\begin{verbatim}
  int fits_set_hdustruc / ffrdef
      (fitsfile *fptr, > int *status)   (DEPRECATED)
\end{verbatim}

\section{Specialized Header Keyword Routines}


\subsection{Header Information Routines}


\begin{description}
\item[1 ] Reserve space in the CHU for MOREKEYS more header keywords.
    This routine may be called to allocate space for additional keywords
    at the time the header is created (prior to writing any data).
    CFITSIO can dynamically add more space to the header when needed,
    however it is more efficient to preallocate the required space
   if the size is known in advance. \label{ffhdef}
\end{description}

\begin{verbatim}
  int fits_set_hdrsize / ffhdef
      (fitsfile *fptr, int morekeys, > int *status)
\end{verbatim}

\begin{description}
\item[2 ] Return the number of keywords in the header (not counting the END
    keyword) and the current position
    in the header.  The position is the number of the keyword record that
    will be read next (or one greater than the position of the last keyword
    that was read). A value of 1 is returned if the pointer is
   positioned at the beginning of the header. \label{ffghps}
\end{description}

\begin{verbatim}
  int fits_get_hdrpos / ffghps
      (fitsfile *fptr, > int *keysexist, int *keynum, int *status)
\end{verbatim}


\subsection{Read and Write the Required Keywords}


\begin{description}
\item[1 ] Write the required extension header keywords into the CHU.
  These routines are not required, and instead the appropriate
  header may be constructed by writing each individual keyword in the
  proper sequence.

  The simpler fits\_write\_imghdr routine is equivalent to calling
  fits\_write\_grphdr with the default values of simple = TRUE, pcount
  = 0, gcount = 1, and extend = TRUE.  The PCOUNT, GCOUNT and EXTEND
  keywords are not required in the primary header and are only written
  if pcount is not equal to zero, gcount is not equal to zero or one,
  and if extend is TRUE, respectively.  When writing to an IMAGE
  extension, the SIMPLE and EXTEND parameters are ignored.  It is
  recommended that fits\_create\_image or fits\_create\_tbl be used
  instead of these routines to write the
  required header keywords. The general fits\_write\_exthdr routine
  may be used to write the header of any conforming FITS
 extension.  \label{ffphpr} \label{ffphps}
\end{description}

\begin{verbatim}
  int fits_write_imghdr / ffphps
      (fitsfile *fptr, int bitpix, int naxis, long *naxes, > int *status)

  int fits_write_imghdrll / ffphpsll
      (fitsfile *fptr, int bitpix, int naxis, LONGLONG *naxes, > int *status)

  int fits_write_grphdr / ffphpr
      (fitsfile *fptr, int simple, int bitpix, int naxis, long *naxes,
       LONGLONG pcount, LONGLONG gcount, int extend, > int *status)

  int fits_write_grphdrll / ffphprll
      (fitsfile *fptr, int simple, int bitpix, int naxis, LONGLONG *naxes,
       LONGLONG pcount, LONGLONG gcount, int extend, > int *status)

  int fits_write_exthdr /ffphext
      (fitsfile *fptr, char *xtension, int bitpix, int naxis, long *naxes,
       LONGLONG pcount, LONGLONG gcount, > int *status)

\end{verbatim}

\begin{description}
\item[2 ] Write the ASCII table header keywords into the CHU.  The optional
    TUNITn and EXTNAME keywords are written only if the input pointers
    are not null.  A null pointer may given for the
    *tbcol parameter in which case a single space will be inserted
    between each column of the table.  Similarly, if rowlen is
    given = 0, then CFITSIO will calculate the default rowlength based on
   the tbcol and ttype values. \label{ffphtb}
\end{description}

\begin{verbatim}
  int fits_write_atblhdr / ffphtb
      (fitsfile *fptr, LONGLONG rowlen, LONGLONG nrows, int tfields, char **ttype,
       long *tbcol, char **tform, char **tunit, char *extname, > int *status)
\end{verbatim}

\begin{description}
\item[3 ] Write the binary table header keywords into the CHU.   The optional
   TUNITn and EXTNAME keywords are written only if the input pointers
   are not null.  The pcount parameter, which specifies the
   size of the variable length array heap, should initially = 0;
   CFITSIO will automatically update the PCOUNT keyword value if any
   variable length array data is written to the heap.  The TFORM keyword
   value for variable length vector columns should have the form 'Pt(len)'
   or '1Pt(len)' where `t' is the data type code letter (A,I,J,E,D, etc.)
   and  `len' is an integer specifying the maximum length of the vectors
   in that column (len must be greater than or equal to the longest
   vector in the column).  If `len' is not specified when the table is
   created (e.g., the input TFORMn value is just '1Pt') then CFITSIO will
   scan the column when the table is first closed and will append the
   maximum length to the TFORM keyword value.  Note that if the table
   is subsequently modified to increase the maximum length of the vectors
   then the modifying program is responsible for also updating the TFORM
  keyword value.  \label{ffphbn}
\end{description}

\begin{verbatim}
  int fits_write_btblhdr / ffphbn
      (fitsfile *fptr, LONGLONG nrows, int tfields, char **ttype,
       char **tform, char **tunit, char *extname, LONGLONG pcount, > int *status)
\end{verbatim}

\begin{description}
\item[4 ] Read the required keywords from the CHDU (image or table).  When
    reading from an IMAGE extension the SIMPLE and EXTEND parameters are
    ignored.  A null pointer may be supplied for any of the returned
   parameters that are not needed. \label{ffghpr} \label{ffghtb} \label{ffghbn}
\end{description}

\begin{verbatim}
  int fits_read_imghdr / ffghpr
      (fitsfile *fptr, int maxdim, > int *simple, int *bitpix, int *naxis,
       long *naxes, long *pcount, long *gcount, int *extend, int *status)

  int fits_read_imghdrll / ffghprll
      (fitsfile *fptr, int maxdim, > int *simple, int *bitpix, int *naxis,
       LONGLONG *naxes, long *pcount, long *gcount, int *extend, int *status)

  int fits_read_atblhdr / ffghtb
      (fitsfile *fptr,int maxdim, > long *rowlen, long *nrows,
       int *tfields, char **ttype, LONGLONG *tbcol, char **tform, char **tunit,
       char *extname,  int *status)

  int fits_read_atblhdrll / ffghtbll
      (fitsfile *fptr,int maxdim, > LONGLONG *rowlen, LONGLONG *nrows,
       int *tfields, char **ttype, long *tbcol, char **tform, char **tunit,
       char *extname,  int *status)

  int fits_read_btblhdr / ffghbn
      (fitsfile *fptr, int maxdim, > long *nrows, int *tfields,
       char **ttype, char **tform, char **tunit, char *extname,
       long *pcount, int *status)

  int fits_read_btblhdrll / ffghbnll
      (fitsfile *fptr, int maxdim, > LONGLONG *nrows, int *tfields,
       char **ttype, char **tform, char **tunit, char *extname,
       long *pcount, int *status)
\end{verbatim}

\subsection{Write Keyword Routines}

These routines simply append a new keyword to the header and do not
check to see if a keyword with the same name already exists.  In
general it is preferable to use the fits\_update\_key routine to ensure
that the same keyword is not written more than once to the header.  See
Appendix B for the definition of the parameters used in these
routines.



\begin{description}
\item[1 ]  Write (append) a new keyword of the appropriate data type into the CHU.
     A null pointer may be entered for the comment parameter, which
     will cause the comment field of the keyword to be left blank.  The
     flt, dbl, cmp, and dblcmp versions of this routine have the added
     feature that if the 'decimals' parameter is negative, then the 'G'
     display format rather then the 'E' format will be used when
     constructing the keyword value, taking the absolute value of
     'decimals' for the precision.  This will suppress trailing zeros,
     and will use a fixed format rather than an exponential format,
    depending on the magnitude of the value. \label{ffpkyx}
\end{description}

\begin{verbatim}
  int fits_write_key_str / ffpkys
      (fitsfile *fptr, char *keyname, char *value, char *comment,
       > int *status)

  int fits_write_key_[log, lng] /  ffpky[lj]
      (fitsfile *fptr, char *keyname, DTYPE numval, char *comment,
       > int *status)

  int fits_write_key_[flt, dbl, fixflg, fixdbl] / ffpky[edfg]
      (fitsfile *fptr, char *keyname, DTYPE numval, int decimals,
      char *comment, > int *status)

  int fits_write_key_[cmp, dblcmp, fixcmp, fixdblcmp] / ffpk[yc,ym,fc,fm]
      (fitsfile *fptr, char *keyname, DTYPE *numval, int decimals,
      char *comment, > int *status)
\end{verbatim}

\begin{description}
\item[2 ] Write (append) a string valued keyword into the CHU which may be longer
    than 68 characters in length.  This uses the Long String Keyword
    convention that is described in the`Local FITS Conventions' section
    in Chapter 4.  Since this uses a non-standard FITS convention to
    encode the long keyword string, programs which use this routine
    should also call the fits\_write\_key\_longwarn routine to add some
    COMMENT keywords to warn users of the FITS file that this
    convention is being used.  The fits\_write\_key\_longwarn routine
    also writes a keyword called LONGSTRN to record the version of the
    longstring convention that has been used, in case a new convention
    is adopted at some point in the future.   If the LONGSTRN keyword
    is already present in the header, then fits\_write\_key\_longwarn
    will
   simply return without doing anything. \label{ffpkls} \label{ffplsw}
\end{description}

\begin{verbatim}
  int fits_write_key_longstr / ffpkls
      (fitsfile *fptr, char *keyname, char *longstr, char *comment,
       > int *status)

  int fits_write_key_longwarn / ffplsw
      (fitsfile *fptr, > int *status)
\end{verbatim}

\begin{description}
\item[3 ] Write (append) a numbered sequence of keywords into the CHU.  The
    starting index number (nstart) must be greater than 0. One may
    append the same comment to every keyword (and eliminate the need
    to have an array of identical comment strings, one for each keyword) by
    including the ampersand character as the last non-blank character in the
    (first) COMMENTS string parameter.  This same string
    will then be used for the comment field in all the keywords.
    One may also enter a null pointer for the comment parameter to
   leave the comment field of the keyword blank. \label{ffpknx}
\end{description}

\begin{verbatim}
  int fits_write_keys_str / ffpkns
      (fitsfile *fptr, char *keyroot, int nstart, int nkeys,
       char **value, char **comment, > int *status)

  int fits_write_keys_[log, lng] / ffpkn[lj]
      (fitsfile *fptr, char *keyroot, int nstart, int nkeys,
       DTYPE *numval, char **comment, int *status)

  int fits_write_keys_[flt, dbl, fixflg, fixdbl] / ffpkne[edfg]
      (fitsfile *fptr, char *keyroot, int nstart, int nkey,
       DTYPE *numval, int decimals, char **comment, > int *status)
\end{verbatim}

\begin{description}
\item[4 ]Copy an indexed keyword from one HDU to another, modifying
    the index number of the keyword name in the process.  For example,
    this routine could read the TLMIN3 keyword from the input HDU
    (by giving keyroot = `TLMIN' and innum = 3) and write it to the
    output HDU with the keyword name TLMIN4 (by setting outnum = 4).
    If the input keyword does not exist, then this routine simply
   returns without indicating an error. \label{ffcpky}
\end{description}

\begin{verbatim}
  int fits_copy_key / ffcpky
      (fitsfile *infptr, fitsfile *outfptr, int innum, int outnum,
       char *keyroot, > int *status)
\end{verbatim}

\begin{description}
\item[5 ]Write (append) a `triple precision' keyword into the CHU in F28.16 format.
    The floating point keyword value is constructed by concatenating the
    input integer value with the input double precision fraction value
    (which must have a value between 0.0 and 1.0). The ffgkyt routine should
    be used to read this keyword value, because the other keyword reading
   routines will not preserve the full precision of the value. \label{ffpkyt}
\end{description}

\begin{verbatim}
  int fits_write_key_triple / ffpkyt
      (fitsfile *fptr, char *keyname, long intval, double frac,
       char *comment, > int *status)
\end{verbatim}

\begin{description}
\item[6 ]Write keywords to the CHDU that are defined in an ASCII template file.
   The format of the template file is described under the fits\_parse\_template
  routine. \label{ffpktp}
\end{description}

\begin{verbatim}
  int fits_write_key_template / ffpktp
      (fitsfile *fptr, const char *filename, > int *status)
\end{verbatim}

\subsection{Insert Keyword Routines}

These insert routines are somewhat less efficient than the `update' or
`write' keyword routines  because the following keywords in the header
must be shifted down to make room for the inserted keyword.  See
Appendix B for the definition of the parameters used in these
routines.


\begin{description}
\item[1 ] Insert a new keyword record into the CHU at the specified position
    (i.e., immediately preceding the (keynum)th keyword in the header.)
  \label{ffirec}
\end{description}

\begin{verbatim}
  int fits_insert_record / ffirec
      (fitsfile *fptr, int keynum, char *card, > int *status)
\end{verbatim}

\begin{description}
\item[2 ] Insert a new keyword into the CHU.  The new keyword is inserted
    immediately following the last keyword that has been read from the
    header.  The `longstr' version has the same functionality as the
    `str' version except that it also supports the local long string
    keyword convention for strings longer than 68 characters.  A null
    pointer may be entered for the comment parameter which will cause
    the comment field to be left blank.  The flt, dbl, cmp, and dblcmp
    versions of this routine have the added
     feature that if the 'decimals' parameter is negative, then the 'G'
     display format rather then the 'E' format will be used when
     constructing the keyword value, taking the absolute value of
     'decimals' for the precision.  This will suppress trailing zeros,
     and will use a fixed format rather than an exponential format,
    depending on the magnitude of the value. \label{ffikyx}
\end{description}

\begin{verbatim}
  int fits_insert_card / ffikey
      (fitsfile *fptr, char *card, > int *status)

  int fits_insert_key_[str, longstr] / ffi[kys, kls]
      (fitsfile *fptr, char *keyname, char *value, char *comment,
       > int *status)

  int fits_insert_key_[log, lng] / ffiky[lj]
      (fitsfile *fptr, char *keyname, DTYPE numval, char *comment,
       > int *status)

  int fits_insert_key_[flt, fixflt, dbl, fixdbl] / ffiky[edfg]
      (fitsfile *fptr, char *keyname, DTYPE numval, int decimals,
       char *comment, > int *status)

  int fits_insert_key_[cmp, dblcmp, fixcmp, fixdblcmp] / ffik[yc,ym,fc,fm]
      (fitsfile *fptr, char *keyname, DTYPE *numval, int decimals,
       char *comment, > int *status)
\end{verbatim}

\begin{description}
\item[3 ] Insert a new keyword with an undefined, or null, value into the CHU.
   The value string of the keyword is left blank in this case. \label{ffikyu}
\end{description}

\begin{verbatim}
  int fits_insert_key_null / ffikyu
      (fitsfile *fptr, char *keyname, char *comment, > int *status)
\end{verbatim}


\subsection{Read Keyword Routines}

Wild card characters may be used when specifying the name of the
keyword to be read.


\begin{description}
\item[1 ] Read a keyword value (with the appropriate data type) and comment from
    the CHU.  If a NULL comment pointer is given on input, then the comment
    string will not be returned.  If the value of the keyword is not defined
    (i.e., the value field is blank) then an error status = VALUE\_UNDEFINED
    will be returned and the input value will not be changed (except that
    ffgkys will reset the value to a null string).
  \label{ffgkyx} \label{ffgkls}
\end{description}

\begin{verbatim}
  int fits_read_key_str / ffgkys
      (fitsfile *fptr, char *keyname, > char *value, char *comment,
       int *status);

  NOTE: after calling the following routine, programs must explicitly free
        the memory allocated for 'longstr' after it is no longer needed by
        calling fits_free_memory.

  int fits_read_key_longstr / ffgkls
      (fitsfile *fptr, char *keyname, > char **longstr, char *comment,
             int *status)

  int fits_free_memory / fffree
      (char *longstr, > int *status);

  int fits_read_key_[log, lng, flt, dbl, cmp, dblcmp] / ffgky[ljedcm]
      (fitsfile *fptr, char *keyname, > DTYPE *numval, char *comment,
       int *status)

  int fits_read_key_lnglng / ffgkyjj
      (fitsfile *fptr, char *keyname, > LONGLONG *numval, char *comment,
       int *status)
\end{verbatim}

\begin{description}
\item[2 ] Read a sequence of indexed keyword values (e.g., NAXIS1, NAXIS2, ...).
    The input starting index number (nstart) must be greater than 0.
    If the value of any of the keywords is not defined (i.e., the value
    field is blank) then an error status = VALUE\_UNDEFINED will be
    returned and the input value for the undefined keyword(s) will not
    be changed.  These routines do not support wild card characters in
    the root name.  If there are no indexed keywords in the header with
    the input root name then these routines do not return a non-zero
   status value and instead simply return nfound = 0. \label{ffgknx}
\end{description}

\begin{verbatim}
  int fits_read_keys_str / ffgkns
      (fitsfile *fptr, char *keyname, int nstart, int nkeys,
       > char **value, int *nfound,  int *status)

  int fits_read_keys_[log, lng, flt, dbl] / ffgkn[ljed]
      (fitsfile *fptr, char *keyname, int nstart, int nkeys,
       > DTYPE *numval, int *nfound, int *status)
\end{verbatim}

\begin{description}
\item[3 ] Read the value of a floating point keyword, returning the integer and
    fractional parts of the value in separate routine arguments.
    This routine may be used to read any keyword but is especially
    useful for reading the 'triple precision' keywords written by ffpkyt.
  \label{ffgkyt}
\end{description}

\begin{verbatim}
  int fits_read_key_triple / ffgkyt
      (fitsfile *fptr, char *keyname, > long *intval, double *frac,
       char *comment, int *status)
\end{verbatim}

\subsection{Modify Keyword Routines}

These routines modify the value of an existing keyword.  An error is
returned if the keyword does not exist.  Wild card characters may be
used when specifying the name of the keyword to be modified.  See