summaryrefslogtreecommitdiffstats
path: root/tksao/wcssubs/fitsfile.c
blob: c832687bf043625342e1f0afcc3b60737584b65c (plain)
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
/*** File libwcs/fitsfile.c
 *** July 25, 2014
 *** By Jessica Mink, jmink@cfa.harvard.edu
 *** Harvard-Smithsonian Center for Astrophysics
 *** Copyright (C) 1996-2014
 *** Smithsonian Astrophysical Observatory, Cambridge, MA, USA

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Correspondence concerning WCSTools should be addressed as follows:
           Internet email: jmink@cfa.harvard.edu
           Postal address: Jessica Mink
                           Smithsonian Astrophysical Observatory
                           60 Garden St.
                           Cambridge, MA 02138 USA

 * Module:      fitsfile.c (FITS file reading and writing)
 * Purpose:     Read and write FITS image and table files
 * fitsropen (inpath)
 *		Open a FITS file for reading, returning a FILE pointer
 * fitsrhead (filename, lhead, nbhead)
 *		Read FITS header and return it
 * fitsrtail (filename, lhead, nbhead)
 *		Read appended FITS header and return it
 * fitsrsect (filename, nbhead, header, fd, x0, y0, nx, ny)
 *		Read section of a FITS image, having already read the header
 * fitsrimage (filename, nbhead, header)
 *		Read FITS image, having already ready the header
 * fitsrfull (filename, nbhead, header)
 *		Read a FITS image of any dimension
 * fitsrtopen (inpath, nk, kw, nrows, nchar, nbhead)
 *		Open a FITS table file for reading; return header information
 * fitsrthead (header, nk, kw, nrows, nchar, nbhead)
 *		Extract FITS table information from a FITS header
 * fitsrtline (fd, nbhead, lbuff, tbuff, irow, nbline, line)
 *		Read next line of FITS table file
 * ftgetr8 (entry, kw)
 *		Extract column from FITS table line as double
 * ftgetr4 (entry, kw)
 *		Extract column from FITS table line as float
 * ftgeti4 (entry, kw)
 *		Extract column from FITS table line as int
 * ftgeti2 (entry, kw)
 *		Extract column from FITS table line as short
 * ftgetc (entry, kw, string, maxchar)
 *		Extract column from FITS table line as a character string
 * fitswimage (filename, header, image)
 *		Write FITS header and image
 * fitswext (filename, header, image)
 *		Write FITS header and image as extension to existing FITS file
 * fitswhdu (fd, filename, header, image)
 *		Write FITS header and image as extension to file descriptor
 * fitscimage (filename, header, filename0)
 *		Write FITS header and copy FITS image
 * fitswhead (filename, header)
 *		Write FITS header and keep file open for further writing 
 * fitswexhead (filename, header)
 *		Write FITS header only to FITS extension without writing data
 * isfits (filename)
 *		Return 1 if file is a FITS file, else 0
 * fitsheadsize (header)
 *  		Return size of FITS header in bytes
 */

#include <stdlib.h>
#ifndef VMS
#include <unistd.h>
#endif
#include <stdio.h>
#include <fcntl.h>
#include <sys/file.h>
#include <errno.h>
#include <string.h>
#include "fitsfile.h"

static int verbose=0;		/* Print diagnostics */
static char fitserrmsg[80];
static int fitsinherit = 1;	/* Append primary header to extension header */
void
setfitsinherit (inh)
int inh;
{fitsinherit = inh; return;}

static off_t ibhead = 0;	/* Number of bytes read before header starts */

off_t
getfitsskip()
{return (ibhead);}

/* FITSRHEAD -- Read a FITS header */

char *
fitsrhead (filename, lhead, nbhead)

char	*filename;	/* Name of FITS image file */
int	*lhead;		/* Allocated length of FITS header in bytes (returned) */
int	*nbhead;	/* Number of bytes before start of data (returned) */
			/* This includes all skipped image extensions */

{
    int fd;
    char *header;	/* FITS image header (filled) */
    int extend;
    int nbytes,naxis, i;
    int ntry,nbr,irec,nrec, nbh, ipos, npos, nbprim, lprim, lext;
    int nax1, nax2, nax3, nax4, nbpix, ibpix, nblock, nbskip;
    char fitsbuf[2884];
    char *headend;	/* Pointer to last line of header */
    char *headnext;	/* Pointer to next line of header to be added */
    int hdu;		/* header/data unit counter */
    int extnum;		/* desired header data number
			   (0=primary -1=first with data -2=use EXTNAME) */
    char extname[32];	/* FITS extension name */
    char extnam[32];	/* Desired FITS extension name */
    char *ext;		/* FITS extension name or number in header, if any */
    char *pheader;	/* Primary header (naxis is 0) */
    char cext = 0;
    char *rbrac;	/* Pointer to right bracket if present in file name */
    char *mwcs;		/* Pointer to WCS name separated by % */
    char *newhead;	/* New larger header */
    int nbh0;		/* Length of old too small header */
    char *pheadend;
    int inherit = 1;	/* Value of INHERIT keyword in FITS extension header */
    int extfound = 0;	/* Set to one if desired FITS extension is found */
    int npcount;

    pheader = NULL;
    lprim = 0;
    header = NULL;

    /* Check for FITS WCS specification and ignore for file opening */
    mwcs = strchr (filename, '%');
    if (mwcs != NULL)
	*mwcs = (char) 0;

    /* Check for FITS extension and ignore for file opening */
    rbrac = NULL;
    ext = strchr (filename, ',');
    if (ext == NULL) {
	ext = strchr (filename, '[');
	if (ext != NULL) {
	    rbrac = strchr (filename, ']');
	    if (rbrac != NULL)
		*rbrac = (char) 0;
	    }
	}
    if (ext != NULL) {
	cext = *ext;
	*ext = (char) 0;
	}

    /* Open the image file and read the header */
    if (strncasecmp (filename,"stdin",5)) {
	fd = -1;
	fd = fitsropen (filename);
	}
#ifndef VMS
    else {
	fd = STDIN_FILENO;
	extnum = -1;
	}
#endif

    if (ext != NULL) {
	if (isnum (ext+1))
	    extnum = atoi (ext+1);
	else {
	    extnum = -2;
	    strcpy (extnam, ext+1);
	    }
	}
    else
	extnum = -1;

    /* Repair the damage done to the file-name string during parsing */
    if (ext != NULL)
	*ext = cext;
    if (rbrac != NULL)
	*rbrac = ']';
    if (mwcs != NULL)
	*mwcs = '%';

    if (fd < 0) {
	fprintf (stderr,"FITSRHEAD:  cannot read file %s\n", filename);
	return (NULL);
	}

    nbytes = FITSBLOCK;
    *nbhead = 0;
    headend = NULL;
    nbh = FITSBLOCK * 20 + 4;
    header = (char *) calloc ((unsigned int) nbh, 1);
    (void) hlength (header, nbh);
    headnext = header;
    nrec = 1;
    hdu = 0;
    ibhead = 0;

    /* Read FITS header from input file one FITS block at a time */
    irec = 0;
    ibhead = 0;
    while (irec < 500) {
	nbytes = FITSBLOCK;
	for (ntry = 0; ntry < 10; ntry++) {
	    for (i = 0; i < 2884; i++) fitsbuf[i] = 0;
	    nbr = read (fd, fitsbuf, nbytes);
	    if (verbose)
		fprintf (stderr,"FITSRHEAD: %d header bytes read\n",nbr);

	    /* Short records allowed only if they have the last header line */
	    if (nbr < nbytes) {
		headend = ksearch (fitsbuf,"END");
		if (headend == NULL) {
		    if (ntry < 9) {
			if (verbose)
			    fprintf (stderr,"FITSRHEAD: %d / %d bytes read %d\n",
				     nbr,nbytes,ntry);
			}
		    else {
			snprintf(fitserrmsg,79,"FITSRHEAD: '%d / %d bytes of header read from %s\n"
				,nbr,nbytes,filename);
#ifndef VMS
			if (fd != STDIN_FILENO)
#endif
			    (void)close (fd);
			free (header);
			/* if (pheader != NULL)
			    return (pheader); */
    			if (extnum != -1 && !extfound) {
			    *ext = (char) 0;
			    if (extnum < 0) {
	    			snprintf (fitserrmsg,79,
				    "FITSRHEAD: Extension %s not found in file %s",
				    extnam, filename);
				}
			    else {
	    			snprintf (fitserrmsg,79,
				    "FITSRHEAD: Extension %d not found in file %s",
				    extnum, filename);
				}
			    *ext = cext;
			    }
			else if (hdu > 0) {
	    		    snprintf (fitserrmsg,79,
				"FITSRHEAD: No extensions found in file %s", filename);
			    hdu = 0;
			    if (pheader != NULL) {
				*lhead = nbprim;
				*nbhead = nbprim;
				return (pheader);
				}
			    break;
			    }
			else {
	    		    snprintf (fitserrmsg,79,
				"FITSRHEAD: No header found in file %s", filename);
			    }
			return (NULL);
			}
		    }
		else
		    break;
		}
	    else
		break;
	    }

	/* Replace control characters and nulls with spaces */
	for (i = 0; i < 2880; i++)
	    if (fitsbuf[i] < 32 || i > nbr) fitsbuf[i] = 32;
	if (nbr < 2880)
	    nbr = 2880;

	/* Move current FITS record into header string */
	strncpy (headnext, fitsbuf, nbr);
	*nbhead = *nbhead + nbr;
	nrec = nrec + 1;
	*(headnext+nbr+1) = 0;
	ibhead = ibhead + 2880;
	if (verbose)
	    fprintf (stderr,"FITSRHEAD: %d bytes in header\n",ibhead);

	/* Check to see if this is the final record in this header */
	headend = ksearch (fitsbuf,"END");
	if (headend == NULL) {

	    /* Double size of header buffer if too small */
	    if (nrec * FITSBLOCK > nbh) {
		nbh0 = nbh - 4;
		nbh = (nrec * 2 * FITSBLOCK) + 4;
		newhead = (char *) calloc (1,(unsigned int) nbh);
		if (newhead) {
		    for (i = 0; i < nbh0; i++)
			newhead[i] = header[i];
		    free (header);
		    newhead[nbh-3] = (char) 0;
		    header = newhead;
		    (void) hlength (header, nbh);
		    headnext = header + ((nrec-1) * FITSBLOCK);
		    }
		else {
		    fprintf (stderr,"FITSRHEAD: %d bytes cannot be allocated for header\n",nbh);
		    exit (1);
		    }
		}
	    else
		headnext = headnext + FITSBLOCK;
	    }

	else {
	    naxis = 0;
	    hgeti4 (header,"NAXIS",&naxis);

	    /* If header has no data, save it for appending to desired header */
	    if (naxis < 1) {
		nbprim = nrec * FITSBLOCK;
		headend = ksearch (header,"END");
		lprim = headend + 80 - header;
		pheader = (char *) calloc ((unsigned int) nbprim, 1);
		for (i = 0; i < lprim; i++)
		    pheader[i] = header[i];
		for (i = lprim; i < nbprim; i++)
		    pheader[i] = ' ';
		}

	    /* If header has no data, start with the next record */
	    if (naxis < 1 && extnum == -1) {
		extend = 0;
		hgetl (header,"EXTEND",&extend);
		if (naxis == 0 && extend) {
		    headnext = header;
		    *headend = ' ';
		    headend = NULL;
		    nrec = 1;
		    hdu = hdu + 1;
		    }
		else {
		    break;
		    }
		}

	    /* If this is the desired header data unit, keep it */
	    else if (extnum != -1) {
		if (extnum > -1 && hdu == extnum) {
		    extfound = 1;
		    break;
		    }
		else if (extnum < 0) {
		    extname[0] = 0;
		    hgets (header, "EXTNAME", 32, extname);
		    if (!strcmp (extnam,extname)) {
			extfound = 1;
			break;
			}
		    }

		/* If this is not desired header data unit, skip over data */
		hdu = hdu + 1;
		nblock = 0;
		ibhead = 0;
		if (naxis > 0) {
		    ibpix = 0;
		    hgeti4 (header,"BITPIX",&ibpix);
		    if (ibpix < 0) {
			nbpix = -ibpix / 8;
			}
		    else {
			nbpix = ibpix / 8;
			}
		    nax1 = 1;
		    hgeti4 (header,"NAXIS1",&nax1);
		    nax2 = 1;
		    if (naxis > 1) {
			hgeti4 (header,"NAXIS2",&nax2);
			}
		    nax3 = 1;
		    if (naxis > 2) {
			hgeti4 (header,"NAXIS3",&nax3);
			}
		    nax4 = 1;
		    if (naxis > 3) {
			hgeti4 (header,"NAXIS4",&nax4);
			}
		    nbskip = nax1 * nax2 * nax3 * nax4 * nbpix;
		    nblock = nbskip / 2880;
		    if (nblock*2880 < nbskip) {
			nblock = nblock + 1;
			}
		    npcount = 0;
		    hgeti4 (header,"PCOUNT", &npcount);
		    if (npcount > 0) {
			nbskip = nbskip + npcount;
			nblock = nbskip / 2880;
			if (nblock*2880 < nbskip)
			    nblock = nblock + 1;
			}
		    }
		else {
		    nblock = 0;
		    }
		*nbhead = *nbhead + (nblock * 2880);

		/* Set file pointer to beginning of next header/data unit */
		if (nblock > 0) {
#ifndef VMS
		    if (fd != STDIN_FILENO) {
			ipos = lseek (fd, *nbhead, SEEK_SET);
			npos = *nbhead;
			}
		    else {
#else
			{
#endif
			ipos = 0;
			for (i = 0; i < nblock; i++) {
			    nbytes = FITSBLOCK;
			    nbr = read (fd, fitsbuf, nbytes);
			    if (nbr < nbytes) {
				ipos = ipos + nbr;
				break;
				}
			    else {
				ipos = ipos + nbytes;
				}
			    }
			npos = nblock * 2880;
			}
		    if (ipos < npos) {
			snprintf (fitserrmsg,79,"FITSRHEAD: %d / %d bytes skipped\n",
				 ipos,npos);
			extfound = 0;
			break;
			}
		    }
		headnext = header;
		headend = NULL;
		nrec = 1;
		}
	    else {
		break;
		}
	    }
	}

#ifndef VMS
    if (fd != STDIN_FILENO)
	(void)close (fd);
#endif

/* Print error message and return null if extension not found */
    if (extnum != -1 && !extfound) {
	if (extnum < 0)
	    fprintf (stderr, "FITSRHEAD: Extension %s not found in file %s\n",extnam, filename);
	else
	    fprintf (stderr, "FITSRHEAD: Extension %d not found in file %s\n",extnum, filename);
	if (pheader != NULL) {
	    free (pheader);
	    pheader = NULL;
	    }
	return (NULL);
	}

    /* Allocate an extra block for good measure */
    *lhead = (nrec + 1) * FITSBLOCK;
    if (*lhead > nbh) {
	newhead = (char *) calloc (1,(unsigned int) *lhead);
	for (i = 0; i < nbh; i++)
	    newhead[i] = header[i];
	free (header);
	header = newhead;
	(void) hlength (header, *lhead);
	}
    else
	*lhead = nbh;

    /* If INHERIT keyword is FALSE, never append primary header */
    if (hgetl (header, "INHERIT", &inherit)) {
	if (!inherit && fitsinherit)
	    fitsinherit = 0;
	}

    /* Append primary data header to extension header */
    if (pheader != NULL && extnum != 0 && fitsinherit && hdu > 0) {
	extname[0] = 0;
	hgets (header, "XTENSION", 32, extname);
	if (!strcmp (extname,"IMAGE")) {
	    strncpy (header, "SIMPLE  ", 8);
	    hputl (header, "SIMPLE", 1);
	    }
	headend = blsearch (header,"END");
	if (headend == NULL)
	    headend = ksearch (header, "END");
	lext = headend - header;

	/* Update primary header for inclusion at end of extension header */
	hchange (pheader, "SIMPLE", "ROOTHEAD");
	hchange (pheader, "NEXTEND", "NUMEXT");
	hdel (pheader, "BITPIX");
	hdel (pheader, "NAXIS");
	hdel (pheader, "EXTEND");
	hputl (pheader, "ROOTEND",1);
	pheadend = ksearch (pheader,"END");
	lprim = pheadend + 320 - pheader;
	if (lext + lprim > nbh) {
	    nrec = (lext + lprim) / FITSBLOCK;
	    if (FITSBLOCK*nrec < lext+lprim)
		nrec = nrec + 1;
	    *lhead = (nrec+1) * FITSBLOCK;
	    newhead = (char *) calloc (1,(unsigned int) *lhead);
	    for (i = 0; i < nbh; i++)
		newhead[i] = header[i];
	    free (header);
	    header = newhead;
	    headend = header + lext;
	    (void) hlength (header, *lhead);
	    }
	hputs (header,"COMMENT","-------------------------------------------");
	hputs (header,"COMMENT","Information from Primary Header");
	hputs (header,"COMMENT","-------------------------------------------");
	headend = blsearch (header,"END");
	if (headend == NULL)
	    headend = ksearch (header, "END");
	pheader[lprim] = 0;
	strncpy (headend, pheader, lprim);
	if (pheader != NULL) {
	    free (pheader);
	    pheader = NULL;
	    }
	}

    ibhead = *nbhead - ibhead;

    return (header);
}


/* FITSRTAIL -- Read FITS header appended to graphics file */

char *
fitsrtail (filename, lhead, nbhead)

char	*filename;	/* Name of image file */
int	*lhead;		/* Allocated length of FITS header in bytes (returned) */
int	*nbhead;	/* Number of bytes before start of data (returned) */
			/* This includes all skipped image extensions */

{
    int fd;
    char *header;	/* FITS image header (filled) */
    int nbytes, i, ndiff;
    int nbr, irec;
    off_t offset;
    char *mwcs;		/* Pointer to WCS name separated by % */
    char *headstart;
    char *newhead;

    header = NULL;

    /* Check for FITS WCS specification and ignore for file opening */
    mwcs = strchr (filename, '%');
    if (mwcs != NULL)
	*mwcs = (char) 0;

    /* Open the image file and read the header */
    if (strncasecmp (filename,"stdin",5)) {
	fd = -1;
	fd = fitsropen (filename);
	}
#ifndef VMS
    else {
	fd = STDIN_FILENO;
	}
#endif

    /* Repair the damage done to the file-name string during parsing */
    if (mwcs != NULL)
	*mwcs = '%';

    if (fd < 0) {
	fprintf (stderr,"FITSRTAIL:  cannot read file %s\n", filename);
	return (NULL);
	}

    nbytes = FITSBLOCK;
    *nbhead = 0;
    *lhead = 0;

    /* Read FITS header from end of input file one FITS block at a time */
    irec = 0;
    while (irec < 100) {
	nbytes = FITSBLOCK * (irec + 2);
	header = (char *) calloc ((unsigned int) nbytes, 1);
	offset = lseek (fd, -nbytes, SEEK_END);
	if (offset < 0) {
	    free (header);
	    header = NULL;
	    nbytes = 0;
	    break;
	    }
	for (i = 0; i < nbytes; i++) header[i] = 0;
	nbr = read (fd, header, nbytes);

	/* Check for SIMPLE at start of header */
	for (i = 0; i < nbr; i++)
	    if (header[i] < 32) header[i] = 32;
	if ((headstart = ksearch (header,"SIMPLE"))) {
	    if (headstart != header) {
		ndiff = headstart - header;
		newhead = (char *) calloc ((unsigned int) nbytes, 1);
		for (i = 0; i < nbytes-ndiff; i++)
		    newhead[i] = headstart[i];
		free (header);
		header = newhead;
		}
	    *lhead = nbytes;
	    *nbhead = nbytes;
	    break;
	    }
	free (header);
	}
    (void) hlength (header, nbytes);

#ifndef VMS
    if (fd != STDIN_FILENO)
	(void)close (fd);
#endif

    return (header);
}


/* FITSRSECT -- Read a piece of a FITS image, having already read the header */

char *
fitsrsect (filename, header, nbhead, x0, y0, nx, ny, nlog)

char	*filename;	/* Name of FITS image file */
char	*header;	/* FITS header for image (previously read) */
int	nbhead;		/* Actual length of image header(s) in bytes */
int	x0, y0;		/* FITS image coordinate of first pixel */
int	nx;		/* Number of columns to read (less than NAXIS1) */
int	ny;		/* Number of rows to read (less than NAXIS2) */
int	nlog;		/* Note progress mod this rows */
{
    int fd;		/* File descriptor */
    int nbimage, naxis1, naxis2, bytepix, nbread;
    int bitpix, naxis, nblocks, nbytes, nbr;
    int x1, y1, nbline, nyleft;
    off_t impos, nblin;
    char *image, *imline, *imlast;
    int ilog = 0;
    int row;

    /* Open the image file and read the header */
    if (strncasecmp (filename,"stdin", 5)) {
	fd = -1;

	fd = fitsropen (filename);
	if (fd < 0) {
	    snprintf (fitserrmsg,79, "FITSRSECT:  cannot read file %s\n", filename);
	    return (NULL);
	    }

	/* Skip over FITS header and whatever else needs to be skipped */
	if (lseek (fd, nbhead, SEEK_SET) < 0) {
	    (void)close (fd);
	    snprintf (fitserrmsg,79, "FITSRSECT:  cannot skip header of file %s\n",
		     filename);
	    return (NULL);
	    }
	}
#ifndef VMS
    else
	fd = STDIN_FILENO;
#endif

    /* Compute size of image in bytes using relevant header parameters */
    naxis = 1;
    hgeti4 (header,"NAXIS",&naxis);
    naxis1 = 1;
    hgeti4 (header,"NAXIS1",&naxis1);
    naxis2 = 1;
    hgeti4 (header,"NAXIS2",&naxis2);
    bitpix = 0;
    hgeti4 (header,"BITPIX",&bitpix);
    if (bitpix == 0) {
	/* snprintf (fitserrmsg,79, "FITSRSECT:  BITPIX is 0; image not read\n"); */
	(void)close (fd);
	return (NULL);
	}
    bytepix = bitpix / 8;
    if (bytepix < 0) bytepix = -bytepix;

    /* Keep X coordinates within image limits */
    if (x0 < 1)
	x0 = 1;
    else if (x0 > naxis1)
	x0 = naxis1;
    x1 = x0 + nx - 1;
    if (x1 < 1)
	x1 = 1;
    else if (x1 > naxis1)
	x1 = naxis1;
    nx = x1 - x0 + 1;

    /* Keep Y coordinates within image limits */
    if (y0 < 1)
	y0 = 1;
    else if (y0 > naxis2)
	y0 = naxis2;
    y1 = y0 + ny - 1;
    if (y1 < 1)
	y1 = 1;
    else if (y1 > naxis2)
	y1 = naxis2;
    ny = y1 - y0 + 1;

    /* Number of bytes in output image */
    nbline = nx * bytepix;
    nbimage = nbline * ny;

    /* Set number of bytes to integral number of 2880-byte blocks */
    nblocks = nbimage / FITSBLOCK;
    if (nblocks * FITSBLOCK < nbimage)
	nblocks = nblocks + 1;
    nbytes = nblocks * FITSBLOCK;

    /* Allocate image section to be read */
    image = (char *) malloc (nbytes);
    nyleft = ny;
    imline = image;
    nbr = 0;

    /* Computer pointer to first byte of input image to read */
    nblin = naxis1 * bytepix;
    impos = ((y0 - 1) * nblin) + ((x0 - 1) * bytepix);
    row = y0 - 1;

    /* Read image section one line at a time */
    while (nyleft-- > 0) {
	if (lseek (fd, impos, SEEK_CUR) >= 0) {
	    nbread = read (fd, imline, nbline);
	    nbr = nbr + nbread;
	    impos = nblin - nbread;
	    imline = imline + nbline;
	    row++;
	    if (++ilog == nlog) {
		ilog = 0;
		fprintf (stderr, "Row %5d extracted   ", row);
                (void) putc (13,stderr);
		}
	    }
	}
    if (nlog)
	fprintf (stderr, "\n");

    /* Fill rest of image with zeroes */
    imline = image + nbimage;
    imlast = image + nbytes;
    while (imline++ < imlast)
	*imline = (char) 0;

    /* Byte-reverse image, if necessary */
    if (imswapped ())
	imswap (bitpix, image, nbytes);

    return (image);
}


/* FITSRIMAGE -- Read a FITS image */

char *
fitsrimage (filename, nbhead, header)

char	*filename;	/* Name of FITS image file */
int	nbhead;		/* Actual length of image header(s) in bytes */
char	*header;	/* FITS header for image (previously read) */
{
    int fd;
    int nbimage, naxis1, naxis2, bytepix, nbread;
    int bitpix, naxis, nblocks, nbytes, nbleft, nbr;
    int simple;
    char *image, *imleft;

    /* Open the image file and read the header */
    if (strncasecmp (filename,"stdin", 5)) {
	fd = -1;

	fd = fitsropen (filename);
	if (fd < 0) {
	    snprintf (fitserrmsg,79, "FITSRIMAGE:  cannot read file %s\n", filename);
	    return (NULL);
	    }

	/* Skip over FITS header and whatever else needs to be skipped */
	if (lseek (fd, nbhead, SEEK_SET) < 0) {
	    (void)close (fd);
	    snprintf (fitserrmsg,79, "FITSRIMAGE:  cannot skip header of file %s\n",
		     filename);
	    return (NULL);
	    }
	}
#ifndef VMS
    else
	fd = STDIN_FILENO;
#endif

    /* If SIMPLE=F in header, simply put post-header part of file in buffer */
    hgetl (header, "SIMPLE", &simple);
    if (!simple) {
	nbytes = getfilesize (filename) - nbhead;
	if ((image = (char *) malloc (nbytes + 1)) == NULL) {
	    /* snprintf (fitserrmsg,79, "FITSRIMAGE:  %d-byte image buffer cannot be allocated\n"); */
	    (void)close (fd);
	    return (NULL);
	    }
	hputi4 (header, "NBDATA", nbytes);
	nbread = read (fd, image, nbytes);
	return (image);
	}

    /* Compute size of image in bytes using relevant header parameters */
    naxis = 1;
    hgeti4 (header,"NAXIS",&naxis);
    naxis1 = 1;
    hgeti4 (header,"NAXIS1",&naxis1);
    naxis2 = 1;
    hgeti4 (header,"NAXIS2",&naxis2);
    bitpix = 0;
    hgeti4 (header,"BITPIX",&bitpix);
    if (bitpix == 0) {
	/* snprintf (fitserrmsg,79, "FITSRIMAGE:  BITPIX is 0; image not read\n"); */
	(void)close (fd);
	return (NULL);
	}
    bytepix = bitpix / 8;
    if (bytepix < 0) bytepix = -bytepix;

    /* If either dimension is one and image is 3-D, read all three dimensions */
    if (naxis == 3 && (naxis1 ==1 || naxis2 == 1)) {
	int naxis3;
	hgeti4 (header,"NAXIS3",&naxis3);
	nbimage = naxis1 * naxis2 * naxis3 * bytepix;
	}
    else
	nbimage = naxis1 * naxis2 * bytepix;

    /* Set number of bytes to integral number of 2880-byte blocks */
    nblocks = nbimage / FITSBLOCK;
    if (nblocks * FITSBLOCK < nbimage)
	nblocks = nblocks + 1;
    nbytes = nblocks * FITSBLOCK;

    /* Allocate and read image */
    image = (char *) malloc (nbytes);
    nbleft = nbytes;
    imleft = image;
    nbr = 0;
    while (nbleft > 0) {
	nbread = read (fd, imleft, nbleft);
	nbr = nbr + nbread;
#ifndef VMS
	if (fd == STDIN_FILENO && nbread < nbleft && nbread > 0) {
	    nbleft = nbleft - nbread;
	    imleft = imleft + nbread;
	    }
	else
#endif
	    nbleft = 0;
	}
#ifndef VMS
    if (fd != STDIN_FILENO)
	(void)close (fd);
#endif
    if (nbr < nbimage) {
	snprintf (fitserrmsg,79, "FITSRIMAGE:  %d of %d bytes read from file %s\n",
		 nbr, nbimage, filename);
	return (NULL);
	}

    /* Byte-reverse image, if necessary */
    if (imswapped ())
	imswap (bitpix, image, nbytes);

    return (image);
}


/* FITSRFULL -- Read a FITS image of any dimension */

char *
fitsrfull (filename, nbhead, header)

char	*filename;	/* Name of FITS image file */
int	nbhead;		/* Actual length of image header(s) in bytes */
char	*header;	/* FITS header for image (previously read) */
{
    int fd;
    int nbimage, naxisi, iaxis, bytepix, nbread;
    int bitpix, naxis, nblocks, nbytes, nbleft, nbr, simple;
    char keyword[16];
    char *image, *imleft;

    /* Open the image file and read the header */
    if (strncasecmp (filename,"stdin", 5)) {
	fd = -1;

	fd = fitsropen (filename);
	if (fd < 0) {
	    snprintf (fitserrmsg,79, "FITSRFULL:  cannot read file %s\n", filename);
	    return (NULL);
	    }

	/* Skip over FITS header and whatever else needs to be skipped */
	if (lseek (fd, nbhead, SEEK_SET) < 0) {
	    (void)close (fd);
	    snprintf (fitserrmsg,79, "FITSRFULL:  cannot skip header of file %s\n",
		     filename);
	    return (NULL);
	    }
	}
#ifndef VMS
    else
	fd = STDIN_FILENO;
#endif

    /* If SIMPLE=F in header, simply put post-header part of file in buffer */
    hgetl (header, "SIMPLE", &simple);
    if (!simple) {
	nbytes = getfilesize (filename) - nbhead;
	if ((image = (char *) malloc (nbytes + 1)) == NULL) {
	    snprintf (fitserrmsg,79, "FITSRFULL:  %d-byte image buffer cannot be allocated\n",nbytes+1);
	    (void)close (fd);
	    return (NULL);
	    }
	hputi4 (header, "NBDATA", nbytes);
	nbread = read (fd, image, nbytes);
	return (image);
	}

    /* Find number of bytes per pixel */
    bitpix = 0;
    hgeti4 (header,"BITPIX",&bitpix);
    if (bitpix == 0) {
	snprintf (fitserrmsg,79, "FITSRFULL:  BITPIX is 0; image not read\n");
	(void)close (fd);
	return (NULL);
	}
    bytepix = bitpix / 8;
    if (bytepix < 0) bytepix = -bytepix;
    nbimage = bytepix;

    /* Compute size of image in bytes using relevant header parameters */
    naxis = 1;
    hgeti4 (header,"NAXIS",&naxis);
    for (iaxis = 1; iaxis <= naxis; iaxis++) {
	sprintf (keyword, "NAXIS%d", iaxis);
	naxisi = 1;
	hgeti4 (header,keyword,&naxisi);
	nbimage = nbimage * naxisi;
	}

    /* Set number of bytes to integral number of 2880-byte blocks */
    nblocks = nbimage / FITSBLOCK;
    if (nblocks * FITSBLOCK < nbimage)
	nblocks = nblocks + 1;
    nbytes = nblocks * FITSBLOCK;

    /* Allocate and read image */
    image = (char *) malloc (nbytes);
    nbleft = nbytes;
    imleft = image;
    nbr = 0;
    while (nbleft > 0) {
	nbread = read (fd, imleft, nbleft);
	nbr = nbr + nbread;
#ifndef VMS
	if (fd == STDIN_FILENO && nbread < nbleft && nbread > 0) {
	    nbleft = nbleft - nbread;
	    imleft = imleft + nbread;
	    }
	else
#endif
	    nbleft = 0;
	}
#ifndef VMS
    if (fd != STDIN_FILENO)
	(void)close (fd);
#endif
    if (nbr < nbimage) {
	snprintf (fitserrmsg,79, "FITSRFULL:  %d of %d image bytes read from file %s\n",
		 nbr, nbimage, filename);
	return (NULL);
	}

    /* Byte-reverse image, if necessary */
    if (imswapped ())
	imswap (bitpix, image, nbytes);

    return (image);
}


/* FITSROPEN -- Open a FITS file, returning the file descriptor */

int
fitsropen (inpath)

char	*inpath;	/* Pathname for FITS tables file to read */

{
    int ntry;
    int fd;		/* file descriptor for FITS tables file (returned) */
    char *ext;		/* extension name or number */
    char cext = 0;
    char *rbrac;
    char *mwcs;		/* Pointer to WCS name separated by % */

/* Check for FITS WCS specification and ignore for file opening */
    mwcs = strchr (inpath, '%');

/* Check for FITS extension and ignore for file opening */
    ext = strchr (inpath, ',');
    rbrac = NULL;
    if (ext == NULL) {
	ext = strchr (inpath, '[');
	if (ext != NULL) {
	    rbrac = strchr (inpath, ']');
	    }
	}

/* Open input file */
    for (ntry = 0; ntry < 3; ntry++) {
	if (ext != NULL) {
	    cext = *ext;
	    *ext = 0;
	    }
	if (rbrac != NULL)
	    *rbrac = (char) 0;
	if (mwcs != NULL)
	    *mwcs = (char) 0;
	fd = open (inpath, O_RDONLY);
	if (ext != NULL)
	    *ext = cext;
	if (rbrac != NULL)
	    *rbrac = ']';
	if (mwcs != NULL)
	    *mwcs = '%';
	if (fd >= 0)
	    break;
	else if (ntry == 2) {
	    snprintf (fitserrmsg,79, "FITSROPEN:  cannot read file %s\n", inpath);
	    return (-1);
	    }
	}

    if (verbose)
	fprintf (stderr,"FITSROPEN:  input file %s opened\n",inpath);

    return (fd);
}


static int offset1=0;
static int offset2=0;

/* FITSRTOPEN -- Open FITS table file and fill structure with
 *		 pointers to selected keywords
 *		 Return file descriptor (-1 if unsuccessful)
 */

int
fitsrtopen (inpath, nk, kw, nrows, nchar, nbhead)

char	*inpath;	/* Pathname for FITS tables file to read */
int	*nk;		/* Number of keywords to use */
struct Keyword	**kw;	/* Structure for desired entries */
int	*nrows;		/* Number of rows in table (returned) */
int	*nchar;		/* Number of characters in one table row (returned) */
int	*nbhead;	/* Number of characters before table starts */

{
    char temp[16];
    int fd;
    int	lhead;		/* Maximum length in bytes of FITS header */
    char *header;	/* Header for FITS tables file to read */

/* Read FITS header from input file */
    header = fitsrhead (inpath, &lhead, nbhead);
    if (!header) {
	snprintf (fitserrmsg,79,"FITSRTOPEN:  %s is not a FITS file\n",inpath);
	return (0);
	}

/* Make sure this file is really a FITS table file */
    temp[0] = 0;
    (void) hgets (header,"XTENSION",16,temp);
    if (strlen (temp) == 0) {
	snprintf (fitserrmsg,79,
		  "FITSRTOPEN:  %s is not a FITS table file\n",inpath);
	free ((void *) header);
	return (0);
	}

/* If it is a FITS file, get table information from the header */
    else if (!strcmp (temp, "TABLE") || !strcmp (temp, "BINTABLE")) {
	if (fitsrthead (header, nk, kw, nrows, nchar)) {
	    snprintf (fitserrmsg,79,
		      "FITSRTOPEN: Cannot read FITS table from %s\n",inpath);
	    free ((void *) header);
	    return (-1);
	    }
	else {
	    fd = fitsropen (inpath);
	    offset1 = 0;
	    offset2 = 0;
	    free ((void *) header);
	    return (fd);
	    }
	}

/* If it is another FITS extension note it and return */
    else {
	snprintf (fitserrmsg,79,
		  "FITSRTOPEN:  %s is a %s extension, not table\n",
		  inpath, temp);
	free ((void *) header);
	return (0);
	}
}

static struct Keyword *pw;	/* Structure for all entries */
static int *lpnam;		/* length of name for each field */
static int bfields = 0;

/* FITSRTHEAD -- From FITS table header, read pointers to selected keywords */

int
fitsrthead (header, nk, kw, nrows, nchar)

char	*header;	/* Header for FITS tables file to read */
int	*nk;		/* Number of keywords to use */
struct Keyword	**kw;	/* Structure for desired entries */
int	*nrows;		/* Number of rows in table (returned) */
int	*nchar;		/* Number of characters in one table row (returned) */

{
    struct Keyword *rw;	/* Structure for desired entries */
    int nfields;
    int ifield, ik, i, ikf, ltform, kl;
    char *h0, *h1, *tf1, *tf2;
    char tname[12];
    char temp[16];
    char tform[16];
    int tverb;
    int bintable = 0;

    h0 = header;

/* Make sure this is really a FITS table file header */
    temp[0] = 0;
    hgets (header,"XTENSION",16,temp);
    if (strlen (temp) == 0) {
	snprintf (fitserrmsg,79, "FITSRTHEAD:  Not a FITS table header\n");
	return (-1);
	}
    else if (!strcmp (temp, "BINTABLE")) {
	bintable = 1;
	}
    else if (strcmp (temp, "TABLE")) {
	snprintf (fitserrmsg,79, "FITSRTHEAD:  %s extension, not TABLE\n",temp);
	return (-1);
	}

/* Get table size from FITS header */
    *nchar = 0;
    hgeti4 (header,"NAXIS1",nchar);
    *nrows = 0;
    hgeti4 (header,"NAXIS2", nrows);
    if (*nrows <= 0 || *nchar <= 0) {
	snprintf (fitserrmsg,79, "FITSRTHEAD: cannot read %d x %d table\n",
		 *nrows,*nchar);
	return (-1);
	}

/* Set up table for access to individual fields */
    nfields = 0;
    hgeti4 (header,"TFIELDS",&nfields);
    if (verbose)
	fprintf (stderr, "FITSRTHEAD: %d fields per table entry\n", nfields);
    if (nfields > bfields) {
	if (bfields > 0)
	    free ((void *)pw);
	pw = (struct Keyword *) calloc (nfields, sizeof(struct Keyword));
	if (pw == NULL) {
	    snprintf (fitserrmsg,79,"FITSRTHEAD: cannot allocate table structure\n");
	    return (-1);
	    }
	if (bfields > 0)
	    free ((void *)lpnam);
	lpnam = (int *) calloc (nfields, sizeof(int));
	if (lpnam == NULL) {
	    snprintf (fitserrmsg,79,"FITSRTHEAD: cannot allocate length structure\n");
	    return (-1);
	    }
	bfields = nfields;
	}

    tverb = verbose;
    verbose = 0;
    ikf = 0;

    for (ifield = 0; ifield < nfields; ifield++) {

    /* Name of field */
	for (i = 0; i < 12; i++) tname[i] = 0;
	sprintf (tname, "TTYPE%d", ifield+1);;
	temp[0] = 0;
	h1 = ksearch (h0,tname);
	h0 = h1;
	hgets (h0,tname,16,temp);
	strcpy (pw[ifield].kname,temp);
	pw[ifield].lname = strlen (pw[ifield].kname);

    /* Sequence of field on line */
	pw[ifield].kn = ifield + 1;

    /* First column of field */
	if (bintable)
	    pw[ifield].kf = ikf;
	else {
	    for (i = 0; i < 12; i++) tname[i] = 0;
	    sprintf (tname, "TBCOL%d", ifield+1);
	    pw[ifield].kf = 0;
	    hgeti4 (h0,tname, &pw[ifield].kf);
	    }

    /* Length of field */
	for (i = 0; i < 12; i++) tname[i] = 0;
	sprintf (tname, "TFORM%d", ifield+1);;
	tform[0] = 0;
	hgets (h0,tname,16,tform);
	strcpy (pw[ifield].kform, tform);
	ltform = strlen (tform);
	if (tform[ltform-1] == 'A') {
	    pw[ifield].kform[0] = 'A';
	    for (i = 0; i < ltform-1; i++)
		pw[ifield].kform[i+1] = tform[i];
	    pw[ifield].kform[ltform] = (char) 0;
	    tf1 = pw[ifield].kform + 1;
	    kl = atof (tf1);
	    }
	else if (!strcmp (tform,"I"))
	    kl = 2;
	else if (!strcmp (tform, "J"))
	    kl = 4;
	else if (!strcmp (tform, "E"))
	    kl = 4;
	else if (!strcmp (tform, "D"))
	    kl = 8;
	else {
	    tf1 = tform + 1;
	    tf2 = strchr (tform,'.');
	    if (tf2 != NULL)
		*tf2 = ' ';
	    kl = atoi (tf1);
	    }
	pw[ifield].kl = kl;
	ikf = ikf + kl;
	}

/* Set up table for access to desired fields */
    verbose = tverb;
    if (verbose)
	fprintf (stderr, "FITSRTHEAD: %d keywords read\n", *nk);

/* If nk = 0, allocate and return structures for all table fields */
    if (*nk <= 0) {
	*kw = pw;
	*nk = nfields;
	return (0);
	}
    else
	rw = *kw;

/* Find each desired keyword in the header */
    for (ik = 0; ik < *nk; ik++) {
	if (rw[ik].kn <= 0) {
	    for (ifield = 0; ifield < nfields; ifield++) {
		if (rw[ik].lname != pw[ifield].lname)
		    continue;
		if (strcmp (pw[ifield].kname, rw[ik].kname) == 0) {
		    break;
		    }
		}
	    }
	else
	    ifield = rw[ik].kn - 1;

/* Set pointer, lentth, and name in returned array of structures */
	rw[ik].kn = ifield + 1;
	rw[ik].kf = pw[ifield].kf - 1;
	rw[ik].kl = pw[ifield].kl;
	strcpy (rw[ik].kform, pw[ifield].kform);
	strcpy (rw[ik].kname, pw[ifield].kname);
	}

    return (0);
}


int
fitsrtline (fd, nbhead, lbuff, tbuff, irow, nbline, line)

int	fd;		/* File descriptor for FITS file */
int	nbhead;		/* Number of bytes in FITS header */
int	lbuff;		/* Number of bytes in table buffer */
char	*tbuff;		/* FITS table buffer */
int	irow;		/* Number of table row to read */
int	nbline;		/* Number of bytes to read for this line */
char	*line;		/* One line of FITS table (returned) */

{
    int nbuff, nlbuff;
    int nbr = 0;
    int offset, offend, ntry, ioff;
    char *tbuff1;

    offset = nbhead + (nbline * irow);
    offend = offset + nbline - 1;

/* Read a new buffer of the FITS table into memory if needed */
    if (offset < offset1 || offend > offset2) {
	nlbuff = lbuff / nbline;
	nbuff = nlbuff * nbline;
	for (ntry = 0; ntry < 3; ntry++) {
	    ioff = lseek (fd, offset, SEEK_SET);
	    if (ioff < offset) {
		if (ntry == 2)
		    return (0);
		else
		    continue;
		}
	    nbr = read (fd, tbuff, nbuff);
	    if (nbr < nbline) {
		if (verbose)
		    fprintf (stderr, "FITSRTLINE: %d / %d bytes read %d\n",
				nbr,nbuff,ntry);
		if (ntry == 2)
		    return (nbr);
		}
	    else
		break;
	    }
	offset1 = offset;
	offset2 = offset + nbr - 1;
	strncpy (line, tbuff, nbline);
	return (nbline);
	}
    else {
	tbuff1 = tbuff + (offset - offset1);
	strncpy (line, tbuff1, nbline);
	return (nbline);
	}
}


void
fitsrtlset ()
{
    offset1 = 0;
    offset2 = 0;
    return;
}


/* FTGETI2 -- Extract n'th column from FITS table line as short */

short
ftgeti2 (entry, kw)

char	*entry;		/* Row or entry from table */
struct Keyword *kw;	/* Table column information from FITS header */
{
    char temp[30];
    short i;
    int j;
    float r;
    double d;

    if (ftgetc (entry, kw, temp, 30)) {
	if (!strcmp (kw->kform, "I"))
	    moveb (temp, (char *) &i, 2, 0, 0);
	else if (!strcmp (kw->kform, "J")) {
	    moveb (temp, (char *) &j, 4, 0, 0);
	    i = (short) j;
	    }
	else if (!strcmp (kw->kform, "E")) {
	    moveb (temp, (char *) &r, 4, 0, 0);
	    i = (short) r;
	    }
	else if (!strcmp (kw->kform, "D")) {
	    moveb (temp, (char *) &d, 8, 0, 0);
	    i = (short) d;
	    }
	else
	    i = (short) atof (temp);
	return (i);
	}
    else
	return ((short) 0);
}


/* FTGETI4 -- Extract n'th column from FITS table line as int */

int
ftgeti4 (entry, kw)

char	*entry;		/* Row or entry from table */
struct Keyword *kw;	/* Table column information from FITS header */
{
    char temp[30];
    short i;
    int j;
    float r;
    double d;

    if (ftgetc (entry, kw, temp, 30)) {
	if (!strcmp (kw->kform, "I")) {
	    moveb (temp, (char *) &i, 2, 0, 0);
	    j = (int) i;
	    }
	else if (!strcmp (kw->kform, "J"))
	    moveb (temp, (char *) &j, 4, 0, 0);
	else if (!strcmp (kw->kform, "E")) {
	    moveb (temp, (char *) &r, 4, 0, 0);
	    j = (int) r;
	    }
	else if (!strcmp (kw->kform, "D")) {
	    moveb (temp, (char *) &d, 8, 0, 0);
	    j = (int) d;
	    }
	else
	    j = (int) atof (temp);
	return (j);
	}
    else
	return (0);
}


/* FTGETR4 -- Extract n'th column from FITS table line as float */

float
ftgetr4 (entry, kw)

char	*entry;		/* Row or entry from table */
struct Keyword *kw;	/* Table column information from FITS header */
{
    char temp[30];
    short i;
    int j;
    float r;
    double d;

    if (ftgetc (entry, kw, temp, 30)) {
	if (!strcmp (kw->kform, "I")) {
	    moveb (temp, (char *) &i, 2, 0, 0);
	    r = (float) i;
	    }
	else if (!strcmp (kw->kform, "J")) {
	    moveb (temp, (char *) &j, 4, 0, 0);
	    r = (float) j;
	    }
	else if (!strcmp (kw->kform, "E"))
	    moveb (temp, (char *) &r, 4, 0, 0);
	else if (!strcmp (kw->kform, "D")) {
	    moveb (temp, (char *) &d, 8, 0, 0);
	    r = (float) d;
	    }
	else
	    r = (float) atof (temp);
	return (r);
	}
    else
	return ((float) 0.0);
}


/* FTGETR8 -- Extract n'th column from FITS table line as double */

double
ftgetr8 (entry, kw)

char	*entry;		/* Row or entry from table */
struct Keyword *kw;	/* Table column information from FITS header */
{
    char temp[30];
    short i;
    int j;
    float r;
    double d;

    if (ftgetc (entry, kw, temp, 30)) {
	if (!strcmp (kw->kform, "I")) {
	    moveb (temp, (char *) &i, 2, 0, 0);
	    d = (double) i;
	    }
	else if (!strcmp (kw->kform, "J")) {
	    moveb (temp, (char *) &j, 4, 0, 0);
	    d = (double) j;
	    }
	else if (!strcmp (kw->kform, "E")) {
	    moveb (temp, (char *) &r, 4, 0, 0);
	    d = (double) r;
	    }
	else if (!strcmp (kw->kform, "D"))
	    moveb (temp, (char *) &d, 8, 0, 0);
	else
	    d = atof (temp);
	return (d);
	}
    else
	return ((double) 0.0);
}


/* FTGETC -- Extract n'th column from FITS table line as character string */

int
ftgetc (entry, kw, string, maxchar)

char	*entry;		/* Row or entry from table */
struct Keyword *kw;	/* Table column information from FITS header */
char	*string;	/* Returned string */
int	maxchar;	/* Maximum number of characters in returned string */
{
    int length = maxchar;

    if (kw->kl < length)
	length = kw->kl;
    if (length > 0) {
	strncpy (string, entry+kw->kf, length);
	string[length] = 0;
	return ( 1 );
	}
    else
	return ( 0 );
}

extern int errno;


/*FITSWIMAGE -- Write FITS header and image */

int
fitswimage (filename, header, image)

char	*filename;	/* Name of FITS image file */
char	*header;	/* FITS image header */
char	*image;		/* FITS image pixels */

{
    int fd;

    /* Open the output file */
    if (strcasecmp (filename,"stdout") ) {

	if (!access (filename, 0)) {
	    fd = open (filename, O_WRONLY);
	    if (fd < 3) {
		snprintf (fitserrmsg,79, "FITSWIMAGE:  file %s not writeable\n", filename);
		return (0);
		}
	    }
	else {
	    fd = open (filename, O_RDWR+O_CREAT, 0666);
	    if (fd < 3) {
		snprintf (fitserrmsg,79, "FITSWIMAGE:  cannot create file %s\n", filename);
		return (0);
		}
	    }
	}
#ifndef VMS
    else
	fd = STDOUT_FILENO;
#endif

    return (fitswhdu (fd, filename, header, image));
}


/*FITSWEXT -- Write FITS header and image as extension to a file */

int
fitswext (filename, header, image)

char	*filename;	/* Name of IFTS image file */
char	*header;	/* FITS image header */
char	*image;		/* FITS image pixels */

{
    int fd;

    /* Open the output file */
    if (strcasecmp (filename,"stdout") ) {

	if (!access (filename, 0)) {
	    fd = open (filename, O_WRONLY);
	    if (fd < 3) {
		snprintf (fitserrmsg,79, "FITSWEXT:  file %s not writeable\n",
			 filename);
		return (0);
		}
	    }
	else {
	    fd = open (filename, O_APPEND, 0666);
	    if (fd < 3) {
		snprintf (fitserrmsg,79, "FITSWEXT:  cannot append to file %s\n",
			 filename);
		return (0);
		}
	    }
	}
#ifndef VMS
    else
	fd = STDOUT_FILENO;
#endif

    return (fitswhdu (fd, filename, header, image));
}


/* FITSWHDU -- Write FITS head and image as extension */

int
fitswhdu (fd, filename, header, image)

int	fd;		/* File descriptor */
char	*filename;	/* Name of IFTS image file */
char	*header;	/* FITS image header */
char	*image;		/* FITS image pixels */
{
    int nbhead, nbimage, nblocks, bytepix, i, nbhw;
    int bitpix, naxis, iaxis, naxisi, nbytes, nbw, nbpad, nbwp, simple;
    char *endhead, *padding;
    double bzero, bscale;
    char keyword[32];

    /* Change BITPIX=-16 files to BITPIX=16 with BZERO and BSCALE */
    bitpix = 0;
    hgeti4 (header,"BITPIX",&bitpix);
    if (bitpix == -16) {
	if (!hgetr8 (header, "BZERO", &bzero) &&
	    !hgetr8 (header, "BSCALE", &bscale)) {
	    bitpix = 16;
	    hputi4 (header, "BITPIX", bitpix);
	    hputr8 (header, "BZERO", 32768.0);
	    hputr8 (header, "BSCALE", 1.0);
	    }
	}

    /* Write header to file */
    endhead = ksearch (header,"END") + 80;
    nbhead = endhead - header;
    nbhw = write (fd, header, nbhead);
    if (nbhw < nbhead) {
	snprintf (fitserrmsg,79, "FITSWHDU:  wrote %d / %d bytes of header to file %s\n",
		 nbhw, nbhead, filename);
	(void)close (fd);
	return (0);
	}

    /* Write extra spaces to make an integral number of 2880-byte blocks */
    nblocks = nbhead / FITSBLOCK;
    if (nblocks * FITSBLOCK < nbhead)
	nblocks = nblocks + 1;
    nbytes = nblocks * FITSBLOCK;
    nbpad = nbytes - nbhead;
    padding = (char *)calloc (1, nbpad);
    for (i = 0; i < nbpad; i++)
	padding[i] = ' ';
    nbwp = write (fd, padding, nbpad);
    if (nbwp < nbpad) {
	snprintf (fitserrmsg,79, "FITSWHDU:  wrote %d / %d bytes of header padding to file %s\n",
		 nbwp, nbpad, filename);
	(void)close (fd);
	return (0);
	}
    nbhw = nbhw + nbwp;
    free (padding);

    /* Return if file has no data */
    if (bitpix == 0 || image == NULL) {
	/* snprintf (fitserrmsg,79, "FITSWHDU:  BITPIX is 0; image not written\n"); */
	(void)close (fd);
	return (0);
	}

    /* If SIMPLE=F in header, just write whatever is in the buffer */
    hgetl (header, "SIMPLE", &simple);
    if (!simple) {
	hgeti4 (header, "NBDATA", &nbytes);
	nbimage = nbytes;
	}

    else {

	/* Compute size of pixel in bytes */
	bytepix = bitpix / 8;
	if (bytepix < 0) bytepix = -bytepix;
	nbimage = bytepix;

	/* Compute size of image in bytes using relevant header parameters */
	naxis = 1;
	hgeti4 (header,"NAXIS",&naxis);
	for (iaxis = 1; iaxis <= naxis; iaxis++) {
	    sprintf (keyword, "NAXIS%d", iaxis);
	    naxisi = 1;
	    hgeti4 (header,keyword,&naxisi);
	    nbimage = nbimage * naxisi;
	    }

	/* Number of bytes to write is an integral number of FITS blocks */
	nblocks = nbimage / FITSBLOCK;
	if (nblocks * FITSBLOCK < nbimage)
	    nblocks = nblocks + 1;
	nbytes = nblocks * FITSBLOCK;

	/* Byte-reverse image before writing, if necessary */
	if (imswapped ())
	    imswap (bitpix, image, nbimage);
	}

    /* Write image to file */
    nbw = write (fd, image, nbimage);
    if (nbw < nbimage) {
	snprintf (fitserrmsg,79, "FITSWHDU:  wrote %d / %d bytes of image to file %s\n",
		 nbw, nbimage, filename);
	return (0);
	}

    /* Write extra zeroes to make an integral number of 2880-byte blocks */
    nbpad = nbytes - nbimage;
    if (nbpad > 0) {
	padding = (char *)calloc (1, nbpad);
	nbwp = write (fd, padding, nbpad);
	if (nbwp < nbpad) {
	    snprintf (fitserrmsg,79, "FITSWHDU:  wrote %d / %d bytes of image padding to file %s\n",
		 nbwp, nbpad, filename);
	    (void)close (fd);
	    return (0);
	    }
	free (padding);
	}
    else
	nbwp = 0;

    (void)close (fd);

    /* Byte-reverse image after writing, if necessary */
    if (imswapped ())
	imswap (bitpix, image, nbimage);

    nbw = nbw + nbwp + nbhw;
    return (nbw);
}


/*FITSCIMAGE -- Write FITS header and copy FITS image
		Return number of bytes in output image, 0 if failure */

int
fitscimage (filename, header, filename0)

char	*filename;	/* Name of output FITS image file */
char	*header;	/* FITS image header */
char	*filename0;	/* Name of input FITS image file */

{
    int fdout, fdin;
    int nbhead, nbimage, nblocks, bytepix;
    int bitpix, naxis, naxis1, naxis2, nbytes, nbw, nbpad, nbwp;
    char *endhead, *lasthead, *padding;
    char *image;	/* FITS image pixels */
    char *oldhead;	/* Input file image header */
    int nbhead0;	/* Length of input file image header */
    int lhead0;
    int nbbuff, nbuff, ibuff, nbr, nbdata;

    /* Compute size of image in bytes using relevant header parameters */
    naxis = 1;
    hgeti4 (header, "NAXIS", &naxis);
    naxis1 = 1;
    hgeti4 (header, "NAXIS1", &naxis1);
    naxis2 = 1;
    hgeti4 (header, "NAXIS2", &naxis2);
    hgeti4 (header, "BITPIX", &bitpix);
    bytepix = bitpix / 8;
    if (bytepix < 0) bytepix = -bytepix;

    /* If either dimension is one and image is 3-D, read all three dimensions */
    if (naxis == 3 && (naxis1 ==1 || naxis2 == 1)) {
	int naxis3;
	hgeti4 (header,"NAXIS3",&naxis3);
	nbimage = naxis1 * naxis2 * naxis3 * bytepix;
	}
    else
	nbimage = naxis1 * naxis2 * bytepix;

    nblocks = nbimage / FITSBLOCK;
    if (nblocks * FITSBLOCK < nbimage)
	nblocks = nblocks + 1;
    nbytes = nblocks * FITSBLOCK;

    /* Allocate image buffer */
    nbbuff = FITSBLOCK * 100;
    if (nbytes < nbbuff)
	nbbuff = nbytes;
    image = (char *) calloc (1, nbbuff);
    nbuff = nbytes / nbbuff;
    if (nbytes > nbuff * nbbuff)
	nbuff = nbuff + 1;

    /* Read input file header */
    if ((oldhead = fitsrhead (filename0, &lhead0, &nbhead0)) == NULL) {
	snprintf (fitserrmsg, 79,"FITSCIMAGE: header of input file %s cannot be read\n",
		 filename0);
	return (0);
	}

    /* Find size of output header */
    nbhead = fitsheadsize (header);

    /* If overwriting, be more careful if new header is longer than old */
    if (!strcmp (filename, filename0) && nbhead > nbhead0) {
	if ((image = fitsrimage (filename0, nbhead0, oldhead)) == NULL) {
	    snprintf (fitserrmsg,79, "FITSCIMAGE:  cannot read image from file %s\n",
		     filename0);
	    free (oldhead);
	    return (0);
	    }
	return (fitswimage (filename, header, image));
	}
    free (oldhead);

    /* Open the input file and skip over the header */
    if (strcasecmp (filename0,"stdin")) {
	fdin = -1;
	fdin = fitsropen (filename0);
	if (fdin < 0) {
	    snprintf (fitserrmsg, 79,"FITSCIMAGE:  cannot read file %s\n", filename0);
	    return (0);
	    }

	/* Skip over FITS header */
	if (lseek (fdin, nbhead0, SEEK_SET) < 0) {
	    (void)close (fdin);
	    snprintf (fitserrmsg,79, "FITSCIMAGE:  cannot skip header of file %s\n",
		     filename0);
	    return (0);
	    }
	}
#ifndef VMS
    else
	fdin = STDIN_FILENO;
#endif

    /* Open the output file */
    if (!access (filename, 0)) {
	fdout = open (filename, O_WRONLY);
	if (fdout < 3) {
	    snprintf (fitserrmsg,79, "FITSCIMAGE:  file %s not writeable\n", filename);
	    return (0);
	    }
	}
    else {
	fdout = open (filename, O_RDWR+O_CREAT, 0666);
	if (fdout < 3) {
	    snprintf (fitserrmsg,79, "FITSCHEAD:  cannot create file %s\n", filename);
	    return (0);
	    }
	}

    /* Pad header with spaces */
    endhead = ksearch (header,"END") + 80;
    lasthead = header + nbhead;
    while (endhead < lasthead)
	*(endhead++) = ' ';

    /* Write header to file */
    nbw = write (fdout, header, nbhead);
    if (nbw < nbhead) {
	snprintf (fitserrmsg, 79,"FITSCIMAGE:  wrote %d / %d bytes of header to file %s\n",
		 nbw, nbytes, filename);
	(void)close (fdout);
	(void)close (fdin);
	return (0);
	}

    /* Return if no data */
    if (bitpix == 0) {
	(void)close (fdout);
	(void)close (fdin);
	return (nbhead);
	}

    nbdata = 0;
    for (ibuff = 0; ibuff < nbuff; ibuff++) {
	nbr = read (fdin, image, nbbuff);
	if (nbr > 0) {
	    nbw = write (fdout, image, nbr);
	    nbdata = nbdata + nbw;
	    }
	}

    /* Write extra to make integral number of 2880-byte blocks */
    nblocks = nbdata / FITSBLOCK;
    if (nblocks * FITSBLOCK < nbdata)
	nblocks = nblocks + 1;
    nbytes = nblocks * FITSBLOCK;
    nbpad = nbytes - nbdata;
    padding = (char *)calloc (1,nbpad);
    nbwp = write (fdout, padding, nbpad);
    nbw = nbdata + nbwp;
    free (padding);

    (void)close (fdout);
    (void)close (fdin);

    if (nbw < nbimage) {
	snprintf (fitserrmsg, 79, "FITSWIMAGE:  wrote %d / %d bytes of image to file %s\n",
		 nbw, nbimage, filename);
	return (0);
	}
    else
	return (nbw);
}


/* FITSWHEAD -- Write FITS header and keep file open for further writing */

int
fitswhead (filename, header)

char	*filename;	/* Name of IFTS image file */
char	*header;	/* FITS image header */

{
    int fd;
    int nbhead, nblocks;
    int nbytes, nbw;
    char *endhead, *lasthead;

    /* Open the output file */
    if (!access (filename, 0)) {
	fd = open (filename, O_WRONLY);
	if (fd < 3) {
	    snprintf (fitserrmsg, 79, "FITSWHEAD:  file %s not writeable\n", filename);
	    return (0);
	    }
	}
    else {
	fd = open (filename, O_RDWR+O_CREAT, 0666);
	if (fd < 3) {
	    snprintf (fitserrmsg, 79, "FITSWHEAD:  cannot create file %s\n", filename);
	    return (0);
	    }
	}

    /* Write header to file */
    endhead = ksearch (header,"END") + 80;
    nbhead = endhead - header;
    nblocks = nbhead / FITSBLOCK;
    if (nblocks * FITSBLOCK < nbhead)
	nblocks = nblocks + 1;
    nbytes = nblocks * FITSBLOCK;

    /* Pad header with spaces */
    lasthead = header + nbytes;
    while (endhead < lasthead)
	*(endhead++) = ' ';
    
    nbw = write (fd, header, nbytes);
    if (nbw < nbytes) {
	fprintf (stderr, "FITSWHEAD:  wrote %d / %d bytes of header to file %s\n",
		 nbw, nbytes, filename);
	(void)close (fd);
	return (0);
	}
    return (fd);
}


/* FITSWEXHEAD -- Write FITS header in place */

int
fitswexhead (filename, header)

char	*filename;	/* Name of FITS image file with ,extension */
char	*header;	/* FITS image header */

{
    int fd;
    int nbhead, lhead;
    int nbw, nbnew, nbold;
    char *endhead, *lasthead, *oldheader;
    char *ext, cext;

    /* Compare size of existing header to size of new header */
    fitsinherit = 0;
    oldheader = fitsrhead (filename, &lhead, &nbhead);
    if (oldheader == NULL) {
	snprintf (fitserrmsg, 79, "FITSWEXHEAD:  file %s cannot be read\n", filename);
	return (-1);
	}
    nbold = fitsheadsize (oldheader);
    nbnew = fitsheadsize (header);

    /* Return if the new header is bigger than the old header */
    if (nbnew > nbold) {
	snprintf (fitserrmsg, 79, "FITSWEXHEAD:  old header %d bytes, new header %d bytes\n", nbold,nbnew);
	free (oldheader);
	oldheader = NULL;
	return (-1);
	}

    /* Add blank lines if new header is smaller than the old header */
    else if (nbnew < nbold) {
	strcpy (oldheader, header);
	endhead = ksearch (oldheader,"END");
	lasthead = oldheader + nbold;
	while (endhead < lasthead)
	    *(endhead++) = ' ';
	strncpy (lasthead-80, "END", 3);
	}

    /* Pad header with spaces */
    else {
	endhead = ksearch (header,"END") + 80;
	lasthead = header + nbnew;
	while (endhead < lasthead)
	    *(endhead++) = ' ';
	strncpy (oldheader, header, nbnew);
	}

    /* Check for FITS extension and ignore for file opening */
    ext = strchr (filename, ',');
    if (ext == NULL)
	ext = strchr (filename, '[');
    if (ext != NULL) {
	cext = *ext;
	*ext = (char) 0;
	}

    /* Open the output file */
    fd = open (filename, O_WRONLY);
    if (ext != NULL)
	*ext = cext;
    if (fd < 3) {
	snprintf (fitserrmsg, 79, "FITSWEXHEAD:  file %s not writeable\n", filename);
	return (-1);
	}

    /* Skip to appropriate place in file */
    (void) lseek (fd, ibhead, SEEK_SET);

    /* Write header to file */
    nbw = write (fd, oldheader, nbold);
    (void)close (fd);
    free (oldheader);
    oldheader = NULL;
    if (nbw < nbold) {
	fprintf (stderr, "FITSWHEAD:  wrote %d / %d bytes of header to file %s\n",
		 nbw, nbold, filename);
	return (-1);
	}
    return (0);
}


/* ISFITS -- Return 1 if FITS file, else 0 */
int
isfits (filename)

char    *filename;      /* Name of file for which to find size */
{
    int diskfile;
    char keyword[16];
    char *comma;
    int nbr;

    /* First check to see if this is an assignment */
    if (strchr (filename, '='))
	return (0);

    /* Check for stdin (input from pipe) */
    else if (!strcasecmp (filename,"stdin"))
	return (1);

    /* Then check file extension
    else if (strsrch (filename, ".fit") ||
	strsrch (filename, ".fits") ||
	strsrch (filename, ".fts"))
	return (1); */

    /* If no FITS file extension, try opening the file */
    else {
	if ((comma = strchr (filename,',')))
	    *comma = (char) 0;
	if ((diskfile = open (filename, O_RDONLY)) < 0) {
	    if (comma)
		*comma = ',';
	    return (0);
	    }
	else {
	    nbr = read (diskfile, keyword, 8);
	    if (comma)
		*comma = ',';
	    close (diskfile);
	    if (nbr < 8)
		return (0);
	    else if (!strncmp (keyword, "SIMPLE", 6))
		return (1);
	    else
		return (0);
	    }
	}
}


/* FITSHEADSIZE -- Find size of FITS header */

int
fitsheadsize (header)

char	*header;	/* FITS header */
{
    char *endhead;
    int nbhead, nblocks;

    endhead = ksearch (header,"END") + 80;
    nbhead = endhead - header;
    nblocks = nbhead / FITSBLOCK;
    if (nblocks * FITSBLOCK < nbhead)
        nblocks = nblocks + 1;
    return (nblocks * FITSBLOCK);
}


/* Print error message */
void
fitserr ()
{   fprintf (stderr, "%s\n",fitserrmsg);
    return; }


/* MOVEB -- Copy nbytes bytes from source+offs to dest+offd (any data type) */

void
moveb (source, dest, nbytes, offs, offd)

char *source;	/* Pointer to source */
char *dest;	/* Pointer to destination */
int nbytes;	/* Number of bytes to move */
int offs;	/* Offset in bytes in source from which to start copying */
int offd;	/* Offset in bytes in destination to which to start copying */
{
char *from, *last, *to;
        from = source + offs;
        to = dest + offd;
        last = from + nbytes;
        while (from < last) *(to++) = *(from++);
        return;
}

/*
 * Feb  8 1996	New subroutines
 * Apr 10 1996	Add subroutine list at start of file
 * Apr 17 1996	Print error message to stderr
 * May  2 1996	Write using stream IO
 * May 14 1996	If FITSRTOPEN NK is zero, return all keywords in header
 * May 17 1996	Make header internal to FITSRTOPEN
 * Jun  3 1996	Use stream I/O for input as well as output
 * Jun 10 1996	Remove unused variables after running lint
 * Jun 12 1996	Deal with byte-swapped images
 * Jul 11 1996	Rewrite code to separate header and data reading
 * Aug  6 1996  Fixed small defects after lint
 * Aug  6 1996  Drop unused NBHEAD argument from FITSRTHEAD
 * Aug 13 1996	If filename is stdin, read from standard input instead of file
 * Aug 30 1996	Use write for output, not fwrite
 * Sep  4 1996	Fix mode when file is created
 * Oct 15 1996	Drop column argument from FGET* subroutines
 * Oct 15 1996	Drop unused variable 
 * Dec 17 1996	Add option to skip bytes in file before reading the header
 * Dec 27 1996	Turn nonprinting header characters into spaces
 *
 * Oct  9 1997	Add FITS extension support as filename,extension
 * Dec 15 1997	Fix minor bugs after lint
 *
 * Feb 23 1998	Do not append primary header if getting header for ext. 0
 * Feb 23 1998	Accept either bracketed or comma extension
 * Feb 24 1998	Add SIMPLE keyword to start of extracted extension
 * Apr 30 1998	Fix error return if not table file after Allan Brighton
 * May  4 1998	Fix error in argument sequence in HGETS call
 * May 27 1998	Include fitsio.h and imio.h
 * Jun  1 1998	Add VMS fixes from Harry Payne at STScI
 * Jun  3 1998	Fix bug reading EXTNAME
 * Jun 11 1998	Initialize all header parameters before reading them
 * Jul 13 1998	Clarify argument definitions
 * Aug  6 1998	Rename fitsio.c to fitsfile.c to avoid conflict with CFITSIO
 * Aug 13 1998	Add FITSWHEAD to write only header
 * Sep 25 1998	Allow STDIN or stdin for standard input reading
 * Oct  5 1998	Add isfits() to decide whether a file is FITS
 * Oct  9 1998	Assume stdin and STDIN to be FITS files in isfits()
 * Nov 30 1998	Fix bug found by Andreas Wicenec when reading large headers
 * Dec  8 1998	Fix bug introduced by previous bug fix
 *
 * Jan  4 1999	Do not print error message if BITPIX is 0
 * Jan 27 1999	Read and write all of 3D images if one dimension is 1
 * Jan 27 1999	Pad out data to integral number of 2880-byte blocks
 * Apr 29 1999	Write BITPIX=-16 files as BITPIX=16 with BSCALE and BZERO
 * Apr 30 1999	Add % as alternative to , to denote sub-images
 * May 25 1999	Set buffer offsets to 0 when FITS table file is opened
 * Jul 14 1999	Do not try to write image data if BITPIX is 0
 * Sep 27 1999	Add STDOUT as output filename option in fitswimage()
 * Oct  6 1999	Set header length global variable hget.lhead0 in fitsrhead()
 * Oct 14 1999	Update header length as it is changed in fitsrhead()
 * Oct 20 1999	Change | in if statements to ||
 * Oct 25 1999	Change most malloc() calls to calloc()
 * Nov 24 1999	Add fitscimage()
 *
 * Feb 23 2000	Fix problem with some error returns in fitscimage()
 * Mar 17 2000	Drop unused variables after lint
 * Jul 20 2000	Drop BITPIX and NAXIS from primary header if extension printerd
 * Jul 20 2000	Start primary part of header with ROOTHEAD keyword
 * Jul 28 2000	Add loop to deal with buffered stdin
 *
 * Jan 11 2001	Print all messages to stderr
 * Jan 12 2001	Add extension back onto filename after fitsropen() (Guy Rixon)
 * Jan 18 2001	Drop EXTEND keyword when extracting an extension
 * Jan 18 2001	Add fitswext() to append HDU and fitswhdu() to do actual writing
 * Jan 22 2001	Ignore WCS name or letter following a : in file name in fitsrhead()
 * Jan 30 2001	Fix FITSCIMAGE so it doesn't overwrite data when overwriting a file
 * Feb 20 2001	Ignore WCS name or letter following a : in file name in fitsropen()
 * Feb 23 2001	Initialize rbrac in fitsropen()
 * Mar  8 2001	Use % instead of : for WCS specification in file name
 * Mar  9 2001	Fix bug so primary header is always appended to secondary header
 * Mar  9 2001	Change NEXTEND to NUMEXT in appended primary header
 * Mar 20 2001	Declare fitsheadsize() in fitschead()
 * Apr 24 2001	When matching column names, use longest length
 * Jun 27 2001	In fitsrthead(), allocate pw and lpnam only if more space needed
 * Aug 24 2001	In isfits(), return 0 if argument contains an equal sign
 *
 * Jan 28 2002	In fitsrhead(), allow stdin to include extension and/or WCS selection
 * Jun 18 2002	Save error messages as fitserrmsg and use fitserr() to print them
 * Oct 21 2002	Add fitsrsect() to read a section of an image
 *
 * Feb  4 2003	Open catalog file rb instead of r (Martin Ploner, Bern)
 * Apr  2 2003	Drop unused variable in fitsrsect()
 * Jul 11 2003	Use strcasecmp() to check for stdout and stdin
 * Aug  1 2003	If no other header, return root header from fitsrhead()
 * Aug 20 2003	Add fitsrfull() to read n-dimensional FITS images
 * Aug 21 2003	Modify fitswimage() to always write n-dimensional FITS images
 * Nov 18 2003	Fix minor bug in fitswhdu()
 * Dec  3 2003	Remove unused variable lasthead in fitswhdu()
 *
 * May  3 2004	Do not always append primary header to extension header
 * May  3 2004	Add ibhead as position of header read in file
 * May 19 2004	Do not reset ext if NULL in fitswexhead()
 * Jul  1 2004	Initialize INHERIT to 1
 * Aug 30 2004	Move fitsheadsize() declaration to fitsfile.h
 * Aug 31 2004	If SIMPLE=F, put whatever is in file after header in image
 *
 * Mar 17 2005	Use unbuffered I/O in isfits() for robustness
 * Jun 27 2005	Drop unused variable nblocks in fitswexhead()
 * Aug  8 2005	Fix space-padding bug in fitswexhead() found by Armin Rest
 * Sep 30 2005	Fix fitsrsect() to position relatively, not absolutely
 * Oct 28 2005	Add error message if desired FITS extension is not found
 * Oct 28 2005	Fix initialization problem found by Sergey Koposov
 *
 * Feb 23 2006	Add fitsrtail() to read appended FITS headers
 * Feb 27 2006	Add file name to header-reading error messages
 * May  3 2006	Remove declarations of unused variables
 * Jun 20 2006	Initialize uninitialized variables
 * Nov  2 2006	Change all realloc() calls to calloc()
 *
 * Jan  5 2007	In fitsrtail(), change control characters in header to spaces
 * Apr 30 2007	Improve error reporting in FITSRFULL
 * Nov 28 2007	Add support to BINTABLE in ftget*() and fitsrthead()
 * Dec 20 2007	Add data heap numerated by PCOUNT when skipping HDU in fitsrhead()
 * Dec 20 2007	Return NULL pointer if fitsrhead() cannot find requested HDU
 *
 * Apr  7 2008	Drop comma from name when reading file in isfits()
 * Jun 27 2008	Do not append primary data header if it is the only header
 * Nov 21 2008	In fitswhead(), print message if too few bytes written
 *
 * Sep 18 2009	In fitswexhead() write to error string instead of stderr
 * Sep 22 2009	In fitsrthead(), fix lengths for ASCII numeric table entries
 * Sep 25 2009	Add subroutine moveb() and fix calls to it
 * Sep 25 2009	Fix several small errors found by Jessicalas Burke
 *
 * Mar 29 2010	In fitswhead(), always pad blocks to 2880 bytes with spaces
 * Mar 31 2010	In fitsrhead(), fix bug reading long primary headers
 *
 * Sep 15 2011	In fitsrsect() declare impos and nblin off_t
 * Sep 15 2011	In fitsrtail() declare offset off_t
 * Sep 15 2011	Declare global variable ibhead off_t
 *
 * Jul 25 2014	Fix bug when reallocating buffer for long headers
 */