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
|
/*** File imhfile.c
*** March 27, 2012
*** By Jessica Mink, jmink@cfa.harvard.edu
*** Harvard-Smithsonian Center for Astrophysics
*** Copyright (C) 1996-2012
*** 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: imhfile.c (IRAF .imh image file reading and writing)
* Purpose: Read and write IRAF image files (and translate headers)
* Subroutine: check_immagic (irafheader, teststring )
* Verify that file is valid IRAF imhdr or impix
* Subroutine: irafrhead (filename, lfhead, fitsheader, lihead)
* Read IRAF image header
* Subroutine: irafrimage (fitsheader)
* Read IRAF image pixels (call after irafrhead)
* Subroutine: same_path (pixname, hdrname)
* Put filename and header path together
* Subroutine: iraf2fits (hdrname, irafheader, nbiraf, nbfits)
* Convert IRAF image header to FITS image header
* Subroutine: irafwhead (hdrname, irafheader, fitsheader)
* Write IRAF header file
* Subroutine: irafwimage (hdrname, irafheader, fitsheader, image )
* Write IRAF image and header files
* Subroutine: fits2iraf (fitsheader, irafheader)
* Convert FITS image header to IRAF image header
* Subroutine: irafgeti4 (irafheader, offset)
* Get 4-byte integer from arbitrary part of IRAF header
* Subroutine: irafgetc2 (irafheader, offset)
* Get character string from arbitrary part of IRAF v.1 header
* Subroutine: irafgetc (irafheader, offset)
* Get character string from arbitrary part of IRAF header
* Subroutine: iraf2str (irafstring, nchar)
* Convert 2-byte/char IRAF string to 1-byte/char string
* Subroutine: str2iraf (string, irafstring, nchar)
* Convert 1-byte/char string to IRAF 2-byte/char string
* Subroutine: irafswap (bitpix,string,nbytes)
* Swap bytes in string in place, with FITS bits/pixel code
* Subroutine: irafswap2 (string,nbytes)
* Swap bytes in string in place
* Subroutine irafswap4 (string,nbytes)
* Reverse bytes of Integer*4 or Real*4 vector in place
* Subroutine irafswap8 (string,nbytes)
* Reverse bytes of Real*8 vector in place
* Subroutine irafsize (filename)
* Return length of file in bytes
* Subroutine isiraf (filename)
* Return 1 if IRAF .imh file, else 0
* Copyright: 2000 Smithsonian Astrophysical Observatory
* You may do anything you like with this file except remove
* this copyright. The Smithsonian Astrophysical Observatory
* makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without
* express or implied warranty.
*/
#include <stdio.h> /* define stderr, FD, and NULL */
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include "fitsfile.h"
/* Parameters from iraf/lib/imhdr.h for IRAF version 1 images */
#define SZ_IMPIXFILE 79 /* name of pixel storage file */
#define SZ_IMHDRFILE 79 /* length of header storage file */
#define SZ_IMTITLE 79 /* image title string */
#define LEN_IMHDR 2052 /* length of std header */
/* Parameters from iraf/lib/imhdr.h for IRAF version 2 images */
#define SZ_IM2PIXFILE 255 /* name of pixel storage file */
#define SZ_IM2HDRFILE 255 /* name of header storage file */
#define SZ_IM2TITLE 383 /* image title string */
#define LEN_IM2HDR 2046 /* length of std header */
/* Offsets into header in bytes for parameters in IRAF version 1 images */
#define IM_HDRLEN 12 /* Length of header in 4-byte ints */
#define IM_PIXTYPE 16 /* Datatype of the pixels */
#define IM_NDIM 20 /* Number of dimensions */
#define IM_LEN 24 /* Length (as stored) */
#define IM_PHYSLEN 52 /* Physical length (as stored) */
#define IM_PIXOFF 88 /* Offset of the pixels */
#define IM_CTIME 108 /* Time of image creation */
#define IM_MTIME 112 /* Time of last modification */
#define IM_LIMTIME 116 /* Time of min,max computation */
#define IM_MAX 120 /* Maximum pixel value */
#define IM_MIN 124 /* Maximum pixel value */
#define IM_PIXFILE 412 /* Name of pixel storage file */
#define IM_HDRFILE 572 /* Name of header storage file */
#define IM_TITLE 732 /* Image name string */
/* Offsets into header in bytes for parameters in IRAF version 2 images */
#define IM2_HDRLEN 6 /* Length of header in 4-byte ints */
#define IM2_PIXTYPE 10 /* Datatype of the pixels */
#define IM2_SWAPPED 14 /* Pixels are byte swapped */
#define IM2_NDIM 18 /* Number of dimensions */
#define IM2_LEN 22 /* Length (as stored) */
#define IM2_PHYSLEN 50 /* Physical length (as stored) */
#define IM2_PIXOFF 86 /* Offset of the pixels */
#define IM2_CTIME 106 /* Time of image creation */
#define IM2_MTIME 110 /* Time of last modification */
#define IM2_LIMTIME 114 /* Time of min,max computation */
#define IM2_MAX 118 /* Maximum pixel value */
#define IM2_MIN 122 /* Maximum pixel value */
#define IM2_PIXFILE 126 /* Name of pixel storage file */
#define IM2_HDRFILE 382 /* Name of header storage file */
#define IM2_TITLE 638 /* Image name string */
/* Codes from iraf/unix/hlib/iraf.h */
#define TY_CHAR 2
#define TY_SHORT 3
#define TY_INT 4
#define TY_LONG 5
#define TY_REAL 6
#define TY_DOUBLE 7
#define TY_COMPLEX 8
#define TY_POINTER 9
#define TY_STRUCT 10
#define TY_USHORT 11
#define TY_UBYTE 12
#define LEN_IRAFHDR 25000
#define LEN_PIXHDR 1024
#define LEN_FITSHDR 11520
int check_immagic();
int irafgeti4();
float irafgetr4();
char *irafgetc2();
char *irafgetc();
char *iraf2str();
static char *same_path();
static void irafputr4();
static void irafputi4();
static void irafputc2();
static void irafputc();
static void str2iraf();
static int headswap=-1; /* =1 to swap data bytes of foreign IRAF file */
static void irafswap();
static void irafswap2();
static void irafswap4();
static void irafswap8();
int head_version ();
int pix_version ();
int irafncmp ();
static int machswap();
static int irafsize();
#define SECONDS_1970_TO_1980 315532800L
/* Subroutine: irafrhead
* Purpose: Open and read the iraf .imh file, translating it to FITS, too.
* Returns: NULL if failure, else pointer to IRAF .imh image header
* Notes: The imhdr format is defined in iraf/lib/imhdr.h, some of
* which defines or mimicked, above.
*/
char *
irafrhead (filename, lihead)
char *filename; /* Name of IRAF header file */
int *lihead; /* Length of IRAF image header in bytes (returned) */
{
FILE *fd;
int nbr;
char *irafheader;
int nbhead, nbytes;
int imhver;
headswap = -1;
*lihead = 0;
/* open the image header file */
fd = fopen (filename, "rb");
if (fd == NULL) {
fprintf (stderr, "IRAFRHEAD: cannot open file %s to read\n", filename);
return (NULL);
}
/* Find size of image header file */
if ((nbhead = irafsize (fd)) <= 0) {
fprintf (stderr, "IRAFRHEAD: cannot read file %s, size = %d\n",
filename, nbhead);
return (NULL);
}
/* allocate initial sized buffer */
nbytes = nbhead + 5000;
irafheader = (char *) calloc (nbytes/4, 4);
if (irafheader == NULL) {
(void)fprintf(stderr, "IRAFRHEAD Cannot allocate %d-byte header\n",
nbytes);
return (NULL);
}
*lihead = nbytes;
/* Read IRAF header */
nbr = fread (irafheader, 1, nbhead, fd);
fclose (fd);
/* Reject if header less than minimum length */
if (nbr < LEN_PIXHDR) {
(void)fprintf(stderr, "IRAFRHEAD header file %s: %d / %d bytes read.\n",
filename,nbr,LEN_PIXHDR);
free (irafheader);
return (NULL);
}
/* Check header magic word */
imhver = head_version (irafheader);
if (imhver < 1) {
free (irafheader);
(void)fprintf(stderr, "IRAFRHEAD: %s is not a valid IRAF image header\n",
filename);
return(NULL);
}
/* check number of image dimensions
if (imhver == 2)
ndim = irafgeti4 (irafheader, IM2_NDIM])
else
ndim = irafgeti4 (irafheader, IM_NDIM])
if (ndim < 2) {
free (irafheader);
(void)fprintf(stderr, "File %s does not contain 2d image\n", filename);
return (NULL);
} */
return (irafheader);
}
char *
irafrimage (fitsheader)
char *fitsheader; /* FITS image header (filled) */
{
FILE *fd;
char *bang;
int naxis, naxis1, naxis2, naxis3, npaxis1, npaxis2,bitpix, bytepix, pixswap, i;
char *image;
int nbr, nbimage, nbaxis, nbl, nbdiff, lpname;
char *pixheader;
char *linebuff, *pixchar;
int imhver, lpixhead, len;
char pixname[SZ_IM2PIXFILE+1];
char newpixname[SZ_IM2HDRFILE+1];
/* Convert pixel file name to character string */
hgetm (fitsheader, "PIXFIL", SZ_IM2PIXFILE, pixname);
/* Drop trailing spaces */
lpname = strlen (pixname);
pixchar = pixname + lpname - 1;
while (*pixchar == ' ')
*pixchar = (char) 0;
hgeti4 (fitsheader, "PIXOFF", &lpixhead);
/* Open pixel file, ignoring machine name if present */
if ((bang = strchr (pixname, '!')) != NULL )
fd = fopen (bang + 1, "rb");
else
fd = fopen (pixname, "rb");
/* If not at pathname in header, try same directory as header file */
if (!fd) {
hgetm (fitsheader, "IMHFIL", SZ_IM2HDRFILE, newpixname);
len = strlen (newpixname);
newpixname[len-3] = 'p';
newpixname[len-2] = 'i';
newpixname[len-1] = 'x';
fd = fopen (newpixname, "rb");
}
/* Print error message and exit if pixel file is not found */
if (!fd) {
(void)fprintf(stderr,
"IRAFRIMAGE: Cannot open IRAF pixel file %s\n", pixname);
return (NULL);
}
/* Read pixel header */
pixheader = (char *) calloc (lpixhead/4, 4);
if (pixheader == NULL) {
(void)fprintf(stderr, "IRAFRIMAGE Cannot allocate %d-byte pixel header\n",
lpixhead);
return (NULL);
}
nbr = fread (pixheader, 1, lpixhead, fd);
/* Check size of pixel header */
if (nbr < lpixhead) {
(void)fprintf(stderr, "IRAF pixel file %s: %d / %d bytes read.\n",
pixname,nbr,LEN_PIXHDR);
free (pixheader);
fclose (fd);
return (NULL);
}
/* check pixel header magic word */
imhver = pix_version (pixheader);
if (imhver < 1) {
(void)fprintf(stderr, "File %s not valid IRAF pixel file.\n", pixname);
free (pixheader);
fclose (fd);
return(NULL);
}
free (pixheader);
/* Find number of bytes to read */
hgeti4 (fitsheader,"NAXIS",&naxis);
hgeti4 (fitsheader,"NAXIS1",&naxis1);
hgeti4 (fitsheader,"NAXIS2",&naxis2);
hgeti4 (fitsheader,"NPAXIS1",&npaxis1);
hgeti4 (fitsheader,"NPAXIS2",&npaxis2);
hgeti4 (fitsheader,"BITPIX",&bitpix);
if (bitpix < 0)
bytepix = -bitpix / 8;
else
bytepix = bitpix / 8;
/* If either dimension is one and image is 3-D, read all three dimensions */
if (naxis == 3 && ((naxis1 == 1) | (naxis2 == 1))) {
hgeti4 (fitsheader,"NAXIS3",&naxis3);
nbimage = naxis1 * naxis2 * naxis3 * bytepix;
}
else {
nbimage = naxis1 * naxis2 * bytepix;
naxis3 = 1;
}
if (bytepix > 4)
image = (char *) calloc (nbimage/8, 8);
else if (bytepix > 2)
image = (char *) calloc (nbimage/4, 4);
else if (bytepix > 1)
image = (char *) calloc (nbimage/2, 2);
else
image = (char *) calloc (nbimage, 1);
if (image == NULL) {
(void)fprintf(stderr, "IRAFRIMAGE Cannot allocate %d-byte image buffer\n",
nbimage);
return (NULL);
}
/* Read IRAF image all at once if physical and image dimensions are the same */
if (npaxis1 == naxis1)
nbr = fread (image, 1, nbimage, fd);
/* Read IRAF image one line at a time if physical and image dimensions differ */
else {
nbdiff = (npaxis1 - naxis1) * bytepix;
nbaxis = naxis1 * bytepix;
linebuff = image;
nbr = 0;
if (naxis2 == 1 && naxis3 > 1)
naxis2 = naxis3;
for (i = 0; i < naxis2; i++) {
nbl = fread (linebuff, 1, nbaxis, fd);
nbr = nbr + nbl;
(void) fseek (fd, nbdiff, SEEK_CUR);
linebuff = linebuff + nbaxis;
}
}
fclose (fd);
/* Check size of image */
if (nbr < nbimage) {
(void)fprintf(stderr, "IRAF pixel file %s: %d / %d bytes read.\n",
pixname,nbr,nbimage);
free (image);
return (NULL);
}
/* Byte-reverse image, if necessary */
pixswap = 0;
hgetl (fitsheader, "PIXSWAP", &pixswap);
if (pixswap)
irafswap (bitpix, image, nbimage);
return (image);
}
/* Return IRAF image format version number from magic word in IRAF header*/
int
head_version (irafheader)
char *irafheader; /* IRAF image header from file */
{
/* Check header file magic word */
if (irafncmp (irafheader, "imhdr", 5) != 0 ) {
if (strncmp (irafheader, "imhv2", 5) != 0)
return (0);
else
return (2);
}
else
return (1);
}
/* Return IRAF image format version number from magic word in IRAF pixel file */
int
pix_version (irafheader)
char *irafheader; /* IRAF image header from file */
{
/* Check pixel file header magic word */
if (irafncmp (irafheader, "impix", 5) != 0) {
if (strncmp (irafheader, "impv2", 5) != 0)
return (0);
else
return (2);
}
else
return (1);
}
/* Verify that file is valid IRAF imhdr or impix by checking first 5 chars
* Returns: 0 on success, 1 on failure */
int
irafncmp (irafheader, teststring, nc)
char *irafheader; /* IRAF image header from file */
char *teststring; /* C character string to compare */
int nc; /* Number of characters to compate */
{
char *line;
headswap = -1;
if ((line = iraf2str (irafheader, nc)) == NULL)
return (1);
if (strncmp (line, teststring, nc) == 0) {
free (line);
return (0);
}
else {
free (line);
return (1);
}
}
/* Convert IRAF image header to FITS image header, returning FITS header */
char *
iraf2fits (hdrname, irafheader, nbiraf, nbfits)
char *hdrname; /* IRAF header file name (may be path) */
char *irafheader; /* IRAF image header */
int nbiraf; /* Number of bytes in IRAF header */
int *nbfits; /* Number of bytes in FITS header (returned) */
{
char *objname; /* object name from FITS file */
int lstr, i, j, k, ib, nax, nbits, nl;
int lname = 0;
char *pixname, *newpixname, *bang, *chead;
char *fitsheader;
int nblock, nlines;
char *fhead, *fhead1, *fp, endline[81];
char irafchar;
char fitsline[81];
char *dstring;
int pixtype;
int imhver, n, imu, pixoff, impixoff, immax, immin, imtime;
int imndim, imlen, imphyslen, impixtype, pixswap, hpixswap, mtime;
float rmax, rmin;
headswap = -1;
/* Set up last line of FITS header */
(void)strncpy (endline,"END", 3);
for (i = 3; i < 80; i++)
endline[i] = ' ';
endline[80] = 0;
/* Check header magic word */
imhver = head_version (irafheader);
if (imhver < 1) {
(void)fprintf(stderr, "File %s not valid IRAF image header\n",
hdrname);
return(NULL);
}
if (imhver == 2) {
nlines = 24 + ((nbiraf - LEN_IM2HDR) / 81);
imndim = IM2_NDIM;
imlen = IM2_LEN;
imphyslen = IM2_PHYSLEN;
impixtype = IM2_PIXTYPE;
impixoff = IM2_PIXOFF;
imtime = IM2_MTIME;
immax = IM2_MAX;
immin = IM2_MIN;
}
else {
nlines = 24 + ((nbiraf - LEN_IMHDR) / 162);
imndim = IM_NDIM;
imlen = IM_LEN;
imphyslen = IM_PHYSLEN;
impixtype = IM_PIXTYPE;
impixoff = IM_PIXOFF;
imtime = IM_MTIME;
immax = IM_MAX;
immin = IM_MIN;
}
/* Initialize FITS header */
nblock = (nlines * 80) / 2880;
*nbfits = (nblock + 5) * 2880 + 4;
fitsheader = (char *) calloc (*nbfits, 1);
if (fitsheader == NULL) {
(void)fprintf(stderr, "IRAF2FITS Cannot allocate %d-byte FITS header\n",
*nbfits);
return (NULL);
}
hlength (fitsheader, *nbfits);
fhead = fitsheader;
(void)strncpy (fitsheader, endline, 80);
hputl (fitsheader, "SIMPLE", 1);
fhead = fhead + 80;
/* Set pixel size in FITS header */
pixtype = irafgeti4 (irafheader, impixtype);
switch (pixtype) {
case TY_CHAR:
nbits = 8;
break;
case TY_UBYTE:
nbits = 8;
break;
case TY_SHORT:
nbits = 16;
break;
case TY_USHORT:
nbits = -16;
break;
case TY_INT:
case TY_LONG:
nbits = 32;
break;
case TY_REAL:
nbits = -32;
break;
case TY_DOUBLE:
nbits = -64;
break;
default:
(void)fprintf(stderr,"Unsupported data type: %d\n", pixtype);
return (NULL);
}
hputi4 (fitsheader,"BITPIX",nbits);
hputcom (fitsheader,"BITPIX", "IRAF .imh pixel type");
fhead = fhead + 80;
/* Set image dimensions in FITS header */
nax = irafgeti4 (irafheader, imndim);
hputi4 (fitsheader,"NAXIS",nax);
hputcom (fitsheader,"NAXIS", "IRAF .imh naxis");
fhead = fhead + 80;
n = irafgeti4 (irafheader, imlen);
hputi4 (fitsheader, "NAXIS1", n);
hputcom (fitsheader,"NAXIS1", "IRAF .imh image naxis[1]");
fhead = fhead + 80;
if (nax > 1) {
n = irafgeti4 (irafheader, imlen+4);
hputi4 (fitsheader, "NAXIS2", n);
hputcom (fitsheader,"NAXIS2", "IRAF .imh image naxis[2]");
}
else
hputi4 (fitsheader, "NAXIS2", 1);
hputcom (fitsheader,"NAXIS2", "IRAF .imh naxis[2]");
fhead = fhead + 80;
if (nax > 2) {
n = irafgeti4 (irafheader, imlen+8);
hputi4 (fitsheader, "NAXIS3", n);
hputcom (fitsheader,"NAXIS3", "IRAF .imh image naxis[3]");
fhead = fhead + 80;
}
if (nax > 3) {
n = irafgeti4 (irafheader, imlen+12);
hputi4 (fitsheader, "NAXIS4", n);
hputcom (fitsheader,"NAXIS4", "IRAF .imh image naxis[4]");
fhead = fhead + 80;
}
/* Set object name in FITS header */
if (imhver == 2)
objname = irafgetc (irafheader, IM2_TITLE, SZ_IM2TITLE);
else
objname = irafgetc2 (irafheader, IM_TITLE, SZ_IMTITLE);
if ((lstr = strlen (objname)) < 8) {
for (i = lstr; i < 8; i++)
objname[i] = ' ';
objname[8] = 0;
}
hputs (fitsheader,"OBJECT",objname);
hputcom (fitsheader,"OBJECT", "IRAF .imh title");
free (objname);
fhead = fhead + 80;
/* Save physical axis lengths so image file can be read */
n = irafgeti4 (irafheader, imphyslen);
hputi4 (fitsheader, "NPAXIS1", n);
hputcom (fitsheader,"NPAXIS1", "IRAF .imh physical naxis[1]");
fhead = fhead + 80;
if (nax > 1) {
n = irafgeti4 (irafheader, imphyslen+4);
hputi4 (fitsheader, "NPAXIS2", n);
hputcom (fitsheader,"NPAXIS2", "IRAF .imh physical naxis[2]");
fhead = fhead + 80;
}
if (nax > 2) {
n = irafgeti4 (irafheader, imphyslen+8);
hputi4 (fitsheader, "NPAXIS3", n);
hputcom (fitsheader,"NPAXIS3", "IRAF .imh physical naxis[3]");
fhead = fhead + 80;
}
if (nax > 3) {
n = irafgeti4 (irafheader, imphyslen+12);
hputi4 (fitsheader, "NPAXIS4", n);
hputcom (fitsheader,"NPAXIS4", "IRAF .imh physical naxis[4]");
fhead = fhead + 80;
}
/* Save image minimum and maximum in header */
rmax = irafgetr4 (irafheader, immax);
rmin = irafgetr4 (irafheader, immin);
if (rmin != rmax) {
hputr4 (fitsheader, "IRAFMIN", &rmin);
fhead = fhead + 80;
hputcom (fitsheader,"IRAFMIN", "IRAF .imh minimum");
hputr4 (fitsheader, "IRAFMAX", &rmax);
hputcom (fitsheader,"IRAFMAX", "IRAF .imh maximum");
fhead = fhead + 80;
}
/* Save image header filename in header */
nl = hputm (fitsheader,"IMHFIL",hdrname);
if (nl > 0) {
lname = strlen (hdrname);
strcpy (fitsline, "IRAF header file name");
if (lname < 43)
hputcom (fitsheader,"IMHFIL_1", fitsline);
else if (lname > 67 && lname < 110)
hputcom (fitsheader,"IMHFIL_2", fitsline);
else if (lname > 134 && lname < 177)
hputcom (fitsheader,"IMHFIL_3", fitsline);
}
if (nl > 0) fhead = fhead + (nl * 80);
/* Save image pixel file pathname in header */
if (imhver == 2)
pixname = irafgetc (irafheader, IM2_PIXFILE, SZ_IM2PIXFILE);
else
pixname = irafgetc2 (irafheader, IM_PIXFILE, SZ_IMPIXFILE);
if (strncmp(pixname, "HDR", 3) == 0 ) {
newpixname = same_path (pixname, hdrname);
free (pixname);
pixname = newpixname;
}
if (strchr (pixname, '/') == NULL && strchr (pixname, '$') == NULL) {
newpixname = same_path (pixname, hdrname);
free (pixname);
pixname = newpixname;
}
if ((bang = strchr (pixname, '!')) != NULL )
nl = hputm (fitsheader,"PIXFIL",bang+1);
else
nl = hputm (fitsheader,"PIXFIL",pixname);
free (pixname);
if (nl > 0) {
strcpy (fitsline, "IRAF .pix pixel file");
if (lname < 43)
hputcom (fitsheader,"PIXFIL_1", fitsline);
else if (lname > 67 && lname < 110)
hputcom (fitsheader,"PIXFIL_2", fitsline);
else if (lname > 134 && lname < 177)
hputcom (fitsheader,"PIXFIL_3", fitsline);
}
if (nl > 0) fhead = fhead + (nl * 80);
/* Save image offset from star of pixel file */
pixoff = irafgeti4 (irafheader, impixoff);
pixoff = (pixoff - 1) * 2;
hputi4 (fitsheader, "PIXOFF", pixoff);
hputcom (fitsheader,"PIXOFF", "IRAF .pix pixel offset (Do not change!)");
fhead = fhead + 80;
/* Save IRAF file format version in header */
hputi4 (fitsheader,"IMHVER",imhver);
hputcom (fitsheader,"IMHVER", "IRAF .imh format version (1 or 2)");
fhead = fhead + 80;
/* Set flag if header numbers are byte-reversed on this machine */
if (machswap() != headswap)
hputl (fitsheader, "HEADSWAP", 1);
else
hputl (fitsheader, "HEADSWAP", 0);
hputcom (fitsheader,"HEADSWAP", "IRAF header, FITS byte orders differ if T");
fhead = fhead + 80;
/* Set flag if image pixels are byte-reversed on this machine */
if (imhver == 2) {
hpixswap = irafgeti4 (irafheader, IM2_SWAPPED);
if (headswap && !hpixswap)
pixswap = 1;
else if (!headswap && hpixswap)
pixswap = 1;
else
pixswap = 0;
}
else
pixswap = headswap;
if (machswap() != pixswap)
hputl (fitsheader, "PIXSWAP", 1);
else
hputl (fitsheader, "PIXSWAP", 0);
hputcom (fitsheader,"PIXSWAP", "IRAF pixels, FITS byte orders differ if T");
fhead = fhead + 80;
/* Read modification time */
mtime = irafgeti4 (irafheader, imtime);
if (mtime == 0)
dstring = lt2fd ();
else
dstring = tsi2fd (mtime);
hputs (fitsheader, "DATE-MOD", dstring);
hputcom (fitsheader,"DATE-MOD", "Date of latest file modification");
free (dstring);
fhead = fhead + 80;
/* Add user portion of IRAF header to FITS header */
fitsline[80] = 0;
if (imhver == 2) {
imu = LEN_IM2HDR;
chead = irafheader;
j = 0;
for (k = 0; k < 80; k++)
fitsline[k] = ' ';
for (i = imu; i < nbiraf; i++) {
irafchar = chead[i];
if (irafchar == 0)
break;
else if (irafchar == 10) {
(void)strncpy (fhead, fitsline, 80);
/* fprintf (stderr,"%80s\n",fitsline); */
if (strncmp (fitsline, "OBJECT ", 7) != 0) {
fhead = fhead + 80;
}
for (k = 0; k < 80; k++)
fitsline[k] = ' ';
j = 0;
}
else {
if (j > 80) {
if (strncmp (fitsline, "OBJECT ", 7) != 0) {
(void)strncpy (fhead, fitsline, 80);
/* fprintf (stderr,"%80s\n",fitsline); */
j = 9;
fhead = fhead + 80;
}
for (k = 0; k < 80; k++)
fitsline[k] = ' ';
}
if (irafchar > 32 && irafchar < 127)
fitsline[j] = irafchar;
j++;
}
}
}
else {
imu = LEN_IMHDR;
chead = irafheader;
if (headswap == 1)
ib = 0;
else
ib = 1;
for (k = 0; k < 80; k++)
fitsline[k] = ' ';
j = 0;
for (i = imu; i < nbiraf; i=i+2) {
irafchar = chead[i+ib];
if (irafchar == 0)
break;
else if (irafchar == 10) {
if (strncmp (fitsline, "OBJECT ", 7) != 0) {
(void)strncpy (fhead, fitsline, 80);
fhead = fhead + 80;
}
/* fprintf (stderr,"%80s\n",fitsline); */
j = 0;
for (k = 0; k < 80; k++)
fitsline[k] = ' ';
}
else {
if (j > 80) {
if (strncmp (fitsline, "OBJECT ", 7) != 0) {
(void)strncpy (fhead, fitsline, 80);
j = 9;
fhead = fhead + 80;
}
/* fprintf (stderr,"%80s\n",fitsline); */
for (k = 0; k < 80; k++)
fitsline[k] = ' ';
}
if (irafchar > 32 && irafchar < 127)
fitsline[j] = irafchar;
j++;
}
}
}
/* Add END to last line */
(void)strncpy (fhead, endline, 80);
/* Find end of last 2880-byte block of header */
fhead = ksearch (fitsheader, "END") + 80;
nblock = *nbfits / 2880;
fhead1 = fitsheader + (nblock * 2880);
/* Pad rest of header with spaces */
strncpy (endline," ",3);
for (fp = fhead; fp < fhead1; fp = fp + 80) {
(void)strncpy (fp, endline,80);
}
return (fitsheader);
}
int
irafwhead (hdrname, lhead, irafheader, fitsheader)
char *hdrname; /* Name of IRAF header file */
int lhead; /* Length of IRAF header */
char *irafheader; /* IRAF header */
char *fitsheader; /* FITS image header */
{
int fd;
int nbw, nbhead, lphead, pixswap;
/* Get rid of redundant header information */
hgeti4 (fitsheader, "PIXOFF", &lphead);
hgeti4 (fitsheader, "PIXSWAP", &pixswap);
/* Write IRAF header file */
/* Convert FITS header to IRAF header */
irafheader = fits2iraf (fitsheader, irafheader, lhead, &nbhead);
if (irafheader == NULL) {
fprintf (stderr, "IRAFWIMAGE: file %s header error\n", hdrname);
return (-1);
}
/* Open the output file */
if (!access (hdrname, 0)) {
fd = open (hdrname, O_WRONLY);
if (fd < 3) {
fprintf (stderr, "IRAFWIMAGE: file %s not writeable\n", hdrname);
return (0);
}
}
else {
fd = open (hdrname, O_RDWR+O_CREAT, 0666);
if (fd < 3) {
fprintf (stderr, "IRAFWIMAGE: cannot create file %s\n", hdrname);
return (0);
}
}
/* Write IRAF header to disk file */
nbw = write (fd, irafheader, nbhead);
(void) ftruncate (fd, nbhead);
close (fd);
if (nbw < nbhead) {
(void)fprintf(stderr, "IRAF header file %s: %d / %d bytes written.\n",
hdrname, nbw, nbhead);
return (-1);
}
return (nbw);
}
/* IRAFWIMAGE -- write IRAF .imh header file and .pix image file
* No matter what the input, this always writes in the local byte order */
int
irafwimage (hdrname, lhead, irafheader, fitsheader, image )
char *hdrname; /* Name of IRAF header file */
int lhead; /* Length of IRAF header */
char *irafheader; /* IRAF header */
char *fitsheader; /* FITS image header */
char *image; /* IRAF image */
{
int fd;
char *bang;
int nbw, bytepix, bitpix, naxis, naxis1, naxis2, nbimage, lphead;
char *pixn, *newpixname;
char pixname[SZ_IM2PIXFILE+1];
int imhver, pixswap;
hgeti4 (fitsheader, "IMHVER", &imhver);
if (!hgetm (fitsheader, "PIXFIL", SZ_IM2PIXFILE, pixname)) {
if (imhver == 2)
pixn = irafgetc (irafheader, IM2_PIXFILE, SZ_IM2PIXFILE);
else
pixn = irafgetc2 (irafheader, IM_PIXFILE, SZ_IMPIXFILE);
if (strncmp(pixn, "HDR", 3) == 0 ) {
newpixname = same_path (pixn, hdrname);
strcpy (pixname, newpixname);
free (newpixname);
}
else {
if ((bang = strchr (pixn, '!')) != NULL )
strcpy (pixname, bang+1);
else
strcpy (pixname, pixn);
}
free (pixn);
}
/* Find number of bytes to write */
hgeti4 (fitsheader,"NAXIS",&naxis);
hgeti4 (fitsheader,"NAXIS1",&naxis1);
hgeti4 (fitsheader,"NAXIS2",&naxis2);
hgeti4 (fitsheader,"BITPIX",&bitpix);
if (bitpix < 0)
bytepix = -bitpix / 8;
else
bytepix = bitpix / 8;
/* If either dimension is one and image is 3-D, read all three dimensions */
if (naxis == 3 && ((naxis1 == 1) | (naxis2 == 1))) {
int naxis3;
hgeti4 (fitsheader,"NAXIS3",&naxis3);
nbimage = naxis1 * naxis2 * naxis3 * bytepix;
}
else
nbimage = naxis1 * naxis2 * bytepix;
/* Read information about pixel file from header */
hgeti4 (fitsheader, "PIXOFF", &lphead);
hgeti4 (fitsheader, "PIXSWAP", &pixswap);
/* Write IRAF header file */
if (irafwhead (hdrname, lhead, irafheader, fitsheader))
return (0);
/* Open the output file */
if (!access (pixname, 0)) {
fd = open (pixname, O_WRONLY);
if (fd < 3) {
fprintf (stderr, "IRAFWIMAGE: file %s not writeable\n", pixname);
return (0);
}
}
else {
fd = open (pixname, O_RDWR+O_CREAT, 0666);
if (fd < 3) {
fprintf (stderr, "IRAFWIMAGE: cannot create file %s\n", pixname);
return (0);
}
}
/* Write header to IRAF pixel file */
if (imhver == 2)
irafputc ("impv2", irafheader, 0, 5);
else
irafputc2 ("impix", irafheader, 0, 5);
nbw = write (fd, irafheader, lphead);
/* Byte-reverse image, if necessary */
if (pixswap)
irafswap (bitpix, image, nbimage);
/* Write data to IRAF pixel file */
nbw = write (fd, image, nbimage);
close (fd);
return (nbw);
}
/* Put filename and header path together */
static char *
same_path (pixname, hdrname)
char *pixname; /* IRAF pixel file pathname */
char *hdrname; /* IRAF image header file pathname */
{
int len, plen;
char *newpixname;
newpixname = (char *) calloc (SZ_IM2PIXFILE, 1);
/* Pixel file is in same directory as header */
if (strncmp(pixname, "HDR$", 4) == 0 ) {
(void)strncpy (newpixname, hdrname, SZ_IM2PIXFILE);
/* find the end of the pathname */
len = strlen (newpixname);
#ifndef VMS
while( (len > 0) && (newpixname[len-1] != '/') )
#else
while( (len > 0) && (newpixname[len-1] != ']') && (newpixname[len-1] != ':') )
#endif
len--;
/* add name */
newpixname[len] = '\0';
plen = strlen (pixname) - 4;
if (len + plen > SZ_IM2PIXFILE)
(void)strncat (newpixname, &pixname[4], SZ_IM2PIXFILE - len);
else
(void)strncat (newpixname, &pixname[4], plen);
}
/* Bare pixel file with no path is assumed to be same as HDR$filename */
else if (strchr (pixname, '/') == NULL && strchr (pixname, '$') == NULL) {
(void)strncpy (newpixname, hdrname, SZ_IM2PIXFILE);
/* find the end of the pathname */
len = strlen (newpixname);
#ifndef VMS
while( (len > 0) && (newpixname[len-1] != '/') )
#else
while( (len > 0) && (newpixname[len-1] != ']') && (newpixname[len-1] != ':') )
#endif
len--;
/* add name */
newpixname[len] = '\0';
(void)strncat (newpixname, pixname, SZ_IM2PIXFILE);
}
/* Pixel file has same name as header file, but with .pix extension */
else if (strncmp (pixname, "HDR", 3) == 0) {
/* load entire header name string into name buffer */
(void)strncpy (newpixname, hdrname, SZ_IM2PIXFILE);
len = strlen (newpixname);
newpixname[len-3] = 'p';
newpixname[len-2] = 'i';
newpixname[len-1] = 'x';
}
return (newpixname);
}
/* Convert FITS image header to IRAF image header, returning IRAF header */
/* No matter what the input, this always writes in the local byte order */
char *
fits2iraf (fitsheader, irafheader, nbhead, nbiraf)
char *fitsheader; /* FITS image header */
char *irafheader; /* IRAF image header (returned updated) */
int nbhead; /* Length of IRAF header */
int *nbiraf; /* Length of returned IRAF header */
{
int i, n, pixoff, lhdrdir;
short *irafp, *irafs, *irafu;
char *iraf2u, *iraf2p, *filename, *hdrdir;
char *fitsend, *fitsp, pixfile[SZ_IM2PIXFILE], hdrfile[SZ_IM2HDRFILE];
char title[SZ_IM2TITLE], temp[80];
int nax, nlfits, imhver, nbits, pixtype, hdrlength, mtime;
int imndim, imlen, imphyslen, impixtype, imhlen, imtime, immax, immin;
float rmax, rmin;
hgeti4 (fitsheader, "IMHVER", &imhver);
hdel (fitsheader, "IMHVER");
hdel (fitsheader, "IMHVER");
hgetl (fitsheader, "HEADSWAP", &headswap);
hdel (fitsheader, "HEADSWAP");
hdel (fitsheader, "HEADSWAP");
if (imhver == 2) {
imhlen = IM2_HDRLEN;
imndim = IM2_NDIM;
imlen = IM2_LEN;
imtime = IM2_MTIME;
imphyslen = IM2_PHYSLEN;
impixtype = IM2_PIXTYPE;
immax = IM2_MAX;
immin = IM2_MIN;
}
else {
imhlen = IM_HDRLEN;
imndim = IM_NDIM;
imlen = IM_LEN;
imtime = IM_MTIME;
imphyslen = IM_PHYSLEN;
impixtype = IM_PIXTYPE;
immax = IM_MAX;
immin = IM_MIN;
}
/* Delete FITS header keyword not needed by IRAF */
hdel (fitsheader,"SIMPLE");
/* Set IRAF image data type */
hgeti4 (fitsheader,"BITPIX", &nbits);
switch (nbits) {
case 8:
pixtype = TY_CHAR;
break;
case -8:
pixtype = TY_UBYTE;
break;
case 16:
pixtype = TY_SHORT;
break;
case -16:
pixtype = TY_USHORT;
break;
case 32:
pixtype = TY_INT;
break;
case -32:
pixtype = TY_REAL;
break;
case -64:
pixtype = TY_DOUBLE;
break;
default:
(void)fprintf(stderr,"Unsupported data type: %d\n", nbits);
return (NULL);
}
irafputi4 (irafheader, impixtype, pixtype);
hdel (fitsheader,"BITPIX");
/* Set IRAF image dimensions */
hgeti4 (fitsheader,"NAXIS",&nax);
irafputi4 (irafheader, imndim, nax);
hdel (fitsheader,"NAXIS");
hgeti4 (fitsheader, "NAXIS1", &n);
irafputi4 (irafheader, imlen, n);
irafputi4 (irafheader, imphyslen, n);
hdel (fitsheader,"NAXIS1");
hgeti4 (fitsheader,"NAXIS2",&n);
irafputi4 (irafheader, imlen+4, n);
irafputi4 (irafheader, imphyslen+4, n);
hdel (fitsheader,"NAXIS2");
if (nax > 2) {
hgeti4 (fitsheader,"NAXIS3",&n);
irafputi4 (irafheader, imlen+8, n);
irafputi4 (irafheader, imphyslen+8, n);
hdel (fitsheader,"NAXIS3");
}
if (nax > 3) {
hgeti4 (fitsheader,"NAXIS4",&n);
irafputi4 (irafheader, imlen+12, n);
irafputi4 (irafheader, imphyslen+12, n);
hdel (fitsheader,"NAXIS4");
}
/* Set image pixel value limits */
rmin = 0.0;
hgetr4 (fitsheader, "IRAFMIN", &rmin);
rmax = 0.0;
hgetr4 (fitsheader, "IRAFMAX", &rmax);
if (rmin != rmax) {
irafputr4 (irafheader, immax, rmax);
irafputr4 (irafheader, immin, rmin);
}
hdel (fitsheader, "IRAFMIN");
hdel (fitsheader, "IRAFMAX");
/* Replace pixel file name, if it is in the FITS header */
if (hgetm (fitsheader, "PIXFIL", SZ_IM2PIXFILE, pixfile)) {
if (strchr (pixfile, '/')) {
if (hgetm (fitsheader, "IMHFIL", SZ_IM2HDRFILE, hdrfile)) {
hdrdir = strrchr (hdrfile, '/');
if (hdrdir != NULL) {
lhdrdir = hdrdir - hdrfile + 1;
if (!strncmp (pixfile, hdrfile, lhdrdir)) {
filename = pixfile + lhdrdir;
strcpy (temp, "HDR$");
strcat (temp,filename);
strcpy (pixfile, temp);
}
}
if (pixfile[0] != '/' && pixfile[0] != 'H') {
strcpy (temp, "HDR$");
strcat (temp,pixfile);
strcpy (pixfile, temp);
}
}
}
if (imhver == 2)
irafputc (pixfile, irafheader, IM2_PIXFILE, SZ_IM2PIXFILE);
else
irafputc2 (pixfile, irafheader, IM_PIXFILE, SZ_IMPIXFILE);
hdel (fitsheader,"PIXFIL_1");
hdel (fitsheader,"PIXFIL_2");
hdel (fitsheader,"PIXFIL_3");
hdel (fitsheader,"PIXFIL_4");
}
/* Replace header file name, if it is in the FITS header */
if (hgetm (fitsheader, "IMHFIL", SZ_IM2HDRFILE, pixfile)) {
if (!strchr (pixfile,'/') && !strchr (pixfile,'$')) {
strcpy (temp, "HDR$");
strcat (temp,pixfile);
strcpy (pixfile, temp);
}
if (imhver == 2)
irafputc (pixfile, irafheader, IM2_HDRFILE, SZ_IM2HDRFILE);
else
irafputc2 (pixfile, irafheader, IM_HDRFILE, SZ_IMHDRFILE);
hdel (fitsheader, "IMHFIL_1");
hdel (fitsheader, "IMHFIL_2");
hdel (fitsheader, "IMHFIL_3");
hdel (fitsheader, "IMHFIL_4");
}
/* Replace image title, if it is in the FITS header */
if (hgets (fitsheader, "OBJECT", SZ_IM2TITLE, title)) {
if (imhver == 2)
irafputc (title, irafheader, IM2_TITLE, SZ_IM2TITLE);
else
irafputc2 (title, irafheader, IM_TITLE, SZ_IMTITLE);
hdel (fitsheader, "OBJECT");
}
hgeti4 (fitsheader, "PIXOFF", &pixoff);
hdel (fitsheader, "PIXOFF");
hdel (fitsheader, "PIXOFF");
hdel (fitsheader, "PIXSWAP");
hdel (fitsheader, "PIXSWAP");
hdel (fitsheader, "DATE-MOD");
hdel (fitsheader, "DATE-MOD");
fitsend = ksearch (fitsheader,"END");
/* Find length of FITS header */
fitsend = ksearch (fitsheader,"END");
nlfits = ((fitsend - fitsheader) / 80);
/* Find new length of IRAF header */
if (imhver == 2)
*nbiraf = LEN_IM2HDR + (81 * nlfits);
else
*nbiraf = LEN_IMHDR + (162 * nlfits);
if (*nbiraf > nbhead)
irafheader = realloc (irafheader, *nbiraf);
/* Reset modification time */
mtime = lt2tsi ();
irafputi4 (irafheader, imtime, mtime);
/* Replace user portion of IRAF header with remaining FITS header */
if (imhver == 2) {
iraf2u = irafheader + LEN_IM2HDR;
iraf2p = iraf2u;
for (fitsp = fitsheader; fitsp < fitsend; fitsp = fitsp + 80) {
for (i = 0; i < 80; i++)
*iraf2p++ = fitsp[i];
*iraf2p++ = 10;
}
*iraf2p++ = 0;
*nbiraf = iraf2p - irafheader;
hdrlength = 1 + *nbiraf / 2;
}
else {
irafs = (short *)irafheader;
irafu = irafs + (LEN_IMHDR / 2);
irafp = irafu;
for (fitsp = fitsheader; fitsp < fitsend; fitsp = fitsp + 80) {
for (i = 0; i < 80; i++)
*irafp++ = (short) fitsp[i];
*irafp++ = 10;
}
*irafp++ = 0;
*irafp++ = 32;
*nbiraf = 2 * (irafp - irafs);
hdrlength = *nbiraf / 4;
}
/* Length of header file */
irafputi4 (irafheader, imhlen, hdrlength);
/* Offset in .pix file to first pixel data
hputi4 (fitsheader, "PIXOFF", pixoff); */
/* Return number of bytes in new IRAF header */
return (irafheader);
}
int
irafgeti4 (irafheader, offset)
char *irafheader; /* IRAF image header */
int offset; /* Number of bytes to skip before number */
{
char *ctemp, *cheader;
int temp;
cheader = irafheader;
ctemp = (char *) &temp;
/* If header swap flag not set, set it now */
if (headswap < 0) {
if (cheader[offset] > 0)
headswap = 1;
else
headswap = 0;
}
if (machswap() != headswap) {
ctemp[3] = cheader[offset];
ctemp[2] = cheader[offset+1];
ctemp[1] = cheader[offset+2];
ctemp[0] = cheader[offset+3];
}
else {
ctemp[0] = cheader[offset];
ctemp[1] = cheader[offset+1];
ctemp[2] = cheader[offset+2];
ctemp[3] = cheader[offset+3];
}
return (temp);
}
float
irafgetr4 (irafheader, offset)
char *irafheader; /* IRAF image header */
int offset; /* Number of bytes to skip before number */
{
char *ctemp, *cheader;
float temp;
cheader = irafheader;
ctemp = (char *) &temp;
/* If header swap flag not set, set it now */
if (headswap < 0) {
if (cheader[offset] > 0)
headswap = 1;
else
headswap = 0;
}
if (machswap() != headswap) {
ctemp[3] = cheader[offset];
ctemp[2] = cheader[offset+1];
ctemp[1] = cheader[offset+2];
ctemp[0] = cheader[offset+3];
}
else {
ctemp[0] = cheader[offset];
ctemp[1] = cheader[offset+1];
ctemp[2] = cheader[offset+2];
ctemp[3] = cheader[offset+3];
}
return (temp);
}
/* IRAFGETC2 -- Get character string from arbitrary part of v.1 IRAF header */
char *
irafgetc2 (irafheader, offset, nc)
char *irafheader; /* IRAF image header */
int offset; /* Number of bytes to skip before string */
int nc; /* Maximum number of characters in string */
{
char *irafstring, *string;
irafstring = irafgetc (irafheader, offset, 2*(nc+1));
string = iraf2str (irafstring, nc);
free (irafstring);
return (string);
}
/* IRAFGETC -- Get character string from arbitrary part of IRAF header */
char *
irafgetc (irafheader, offset, nc)
char *irafheader; /* IRAF image header */
int offset; /* Number of bytes to skip before string */
int nc; /* Maximum number of characters in string */
{
char *ctemp, *cheader;
int i;
cheader = irafheader;
ctemp = (char *) calloc (nc+1, 1);
if (ctemp == NULL) {
(void)fprintf(stderr, "IRAFGETC Cannot allocate %d-byte variable\n",
nc+1);
return (NULL);
}
for (i = 0; i < nc; i++) {
ctemp[i] = cheader[offset+i];
if (ctemp[i] > 0 && ctemp[i] < 32)
ctemp[i] = ' ';
}
return (ctemp);
}
/* Convert IRAF 2-byte/char string to 1-byte/char string */
char *
iraf2str (irafstring, nchar)
char *irafstring; /* IRAF 2-byte/character string */
int nchar; /* Number of characters in string */
{
char *string;
int i, j;
/* Set swap flag according to position of nulls in 2-byte characters */
if (headswap < 0) {
if (irafstring[0] != 0 && irafstring[1] == 0)
headswap = 1;
else if (irafstring[0] == 0 && irafstring[1] != 0)
headswap = 0;
else
return (NULL);
}
string = (char *) calloc (nchar+1, 1);
if (string == NULL) {
(void)fprintf(stderr, "IRAF2STR Cannot allocate %d-byte variable\n",
nchar+1);
return (NULL);
}
/* Swap bytes, if requested */
if (headswap)
j = 0;
else
j = 1;
/* Convert appropriate byte of input to output character */
for (i = 0; i < nchar; i++) {
string[i] = irafstring[j];
j = j + 2;
}
return (string);
}
/* IRAFPUTI4 -- Insert 4-byte integer into arbitrary part of IRAF header */
static void
irafputi4 (irafheader, offset, inum)
char *irafheader; /* IRAF image header */
int offset; /* Number of bytes to skip before number */
int inum; /* Number to put into header */
{
char *cn, *chead;
chead = irafheader;
cn = (char *) &inum;
if (headswap < 0)
headswap = 0;
if (headswap != machswap()) {
chead[offset+3] = cn[0];
chead[offset+2] = cn[1];
chead[offset+1] = cn[2];
chead[offset] = cn[3];
}
else {
chead[offset] = cn[0];
chead[offset+1] = cn[1];
chead[offset+2] = cn[2];
chead[offset+3] = cn[3];
}
return;
}
/* IRAFPUTR4 -- Insert 4-byte real number into arbitrary part of IRAF header */
static void
irafputr4 (irafheader, offset, rnum)
char *irafheader; /* IRAF image header */
int offset; /* Number of bytes to skip before number */
float rnum; /* Number to put into header */
{
char *cn, *chead;
chead = irafheader;
cn = (char *) &rnum;
if (headswap < 0)
headswap = 0;
if (headswap != machswap()) {
chead[offset+3] = cn[0];
chead[offset+2] = cn[1];
chead[offset+1] = cn[2];
chead[offset] = cn[3];
}
else {
chead[offset] = cn[0];
chead[offset+1] = cn[1];
chead[offset+2] = cn[2];
chead[offset+3] = cn[3];
}
return;
}
/* IRAFPUTC2 -- Insert character string into arbitrary part of v.1 IRAF header */
static void
irafputc2 (string, irafheader, offset, nc)
char *string; /* String to insert into header */
char *irafheader; /* IRAF image header */
int offset; /* Number of bytes to skip before string */
int nc; /* Maximum number of characters in string */
{
char *irafstring;
irafstring = (char *) calloc (2 * nc, 1);
if (irafstring == NULL) {
(void)fprintf(stderr, "IRAFPUTC2 Cannot allocate %d-byte variable\n",
2 * nc);
}
str2iraf (string, irafstring, nc);
irafputc (irafstring, irafheader, offset, 2*nc);
return;
}
/* IRAFPUTC -- Insert character string into arbitrary part of IRAF header */
static void
irafputc (string, irafheader, offset, nc)
char *string; /* String to insert into header */
char *irafheader; /* IRAF image header */
int offset; /* Number of bytes to skip before string */
int nc; /* Maximum number of characters in string */
{
char *chead;
int i;
chead = irafheader;
for (i = 0; i < nc; i++)
chead[offset+i] = string[i];
return;
}
/* STR2IRAF -- Convert 1-byte/char string to IRAF 2-byte/char string */
static void
str2iraf (string, irafstring, nchar)
char *string; /* 1-byte/character string */
char *irafstring; /* IRAF 2-byte/character string */
int nchar; /* Maximum number of characters in IRAF string */
{
int i, j, nc, nbytes;
nc = strlen (string);
/* Fill output string with zeroes */
nbytes = nchar * 2;
for (i = 0; i < nbytes; i++)
irafstring[i] = 0;
/* If swapped, start with first byte of 2-byte characters */
if (headswap)
j = 0;
else
j = 1;
/* Move input characters to appropriate bytes of output */
for (i = 0; i < nchar; i++) {
if (i > nc)
irafstring[j] = 0;
else
irafstring[j] = string[i];
j = j + 2;
}
return;
}
/* IRAFSWAP -- Reverse bytes of any type of vector in place */
static void
irafswap (bitpix, string, nbytes)
int bitpix; /* Number of bits per pixel */
/* 16 = short, -16 = unsigned short, 32 = int */
/* -32 = float, -64 = double */
char *string; /* Address of starting point of bytes to swap */
int nbytes; /* Number of bytes to swap */
{
switch (bitpix) {
case 16:
if (nbytes < 2) return;
irafswap2 (string,nbytes);
break;
case 32:
if (nbytes < 4) return;
irafswap4 (string,nbytes);
break;
case -16:
if (nbytes < 2) return;
irafswap2 (string,nbytes);
break;
case -32:
if (nbytes < 4) return;
irafswap4 (string,nbytes);
break;
case -64:
if (nbytes < 8) return;
irafswap8 (string,nbytes);
break;
}
return;
}
/* IRAFSWAP2 -- Swap bytes in string in place */
static void
irafswap2 (string,nbytes)
char *string; /* Address of starting point of bytes to swap */
int nbytes; /* Number of bytes to swap */
{
char *sbyte, temp, *slast;
slast = string + nbytes;
sbyte = string;
while (sbyte < slast) {
temp = sbyte[0];
sbyte[0] = sbyte[1];
sbyte[1] = temp;
sbyte= sbyte + 2;
}
return;
}
/* IRAFSWAP4 -- Reverse bytes of Integer*4 or Real*4 vector in place */
static void
irafswap4 (string,nbytes)
char *string; /* Address of Integer*4 or Real*4 vector */
int nbytes; /* Number of bytes to reverse */
{
char *sbyte, *slast;
char temp0, temp1, temp2, temp3;
slast = string + nbytes;
sbyte = string;
while (sbyte < slast) {
temp3 = sbyte[0];
temp2 = sbyte[1];
temp1 = sbyte[2];
temp0 = sbyte[3];
sbyte[0] = temp0;
sbyte[1] = temp1;
sbyte[2] = temp2;
sbyte[3] = temp3;
sbyte = sbyte + 4;
}
return;
}
/* IRAFSWAP8 -- Reverse bytes of Real*8 vector in place */
static void
irafswap8 (string,nbytes)
char *string; /* Address of Real*8 vector */
int nbytes; /* Number of bytes to reverse */
{
char *sbyte, *slast;
char temp[8];
slast = string + nbytes;
sbyte = string;
while (sbyte < slast) {
temp[7] = sbyte[0];
temp[6] = sbyte[1];
temp[5] = sbyte[2];
temp[4] = sbyte[3];
temp[3] = sbyte[4];
temp[2] = sbyte[5];
temp[1] = sbyte[6];
temp[0] = sbyte[7];
sbyte[0] = temp[0];
sbyte[1] = temp[1];
sbyte[2] = temp[2];
sbyte[3] = temp[3];
sbyte[4] = temp[4];
sbyte[5] = temp[5];
sbyte[6] = temp[6];
sbyte[7] = temp[7];
sbyte = sbyte + 8;
}
return;
}
/* Set flag if machine on which program is executing is not FITS byte order
* ( i.e., if it is an Alpha or PC instead of a Sun ) */
static int
machswap ()
{
char *ctest;
int itest;
itest = 1;
ctest = (char *)&itest;
if (*ctest)
return (1);
else
return (0);
}
/* ISIRAF -- return 1 if IRAF imh file, else 0 */
int
isiraf (filename)
char *filename; /* Name of file for which to find size */
{
if (strchr (filename, '='))
return (0);
else if (strsrch (filename, ".imh"))
return (1);
else
return (0);
}
/* IRAFSIZE -- return size of file in bytes */
static int
irafsize (diskfile)
FILE *diskfile; /* Descriptor of file for which to find size */
{
long filesize;
long offset;
offset = (long) 0;
/* Move to end of the file */
if (fseek (diskfile, offset, SEEK_END) == 0) {
/* Position is the size of the file */
filesize = ftell (diskfile);
/* Move file pointer back tot he start of the file */
fseek (diskfile, offset, SEEK_SET);
}
else
filesize = -1;
return (filesize);
}
/* Feb 15 1996 New file
* Apr 10 1996 Add more documentation
* Apr 17 1996 Print error message on open failure
* Jun 5 1996 Add byte swapping (reversal); use streams
* Jun 10 1996 Make fixes after running lint
* Jun 12 1996 Use IMSWAP subroutines instead of local ones
* Jul 3 1996 Go back to using local IRAFSWAP subroutines
* Jul 3 1996 Write to pixel file from FITS header
* Jul 10 1996 Allocate all headers
* Aug 13 1996 Add unistd.h to include list
* Aug 26 1996 Allow 1-d images; fix comments; fix arguments after lint
* Aug 26 1996 Add IRAF header lingth argument to IRAFWIMAGE and IRAFWHEAD
* Aug 28 1996 Clean up code in IRAF2FITS
* Aug 30 1996 Use write instead of fwrite
* Sep 4 1996 Fix write mode bug
* Oct 15 1996 Drop unused variables
* Oct 17 1996 Minor fix after lint; cast arguments to STR2IRAF
*
* May 15 1997 Fix returned header length in IRAF2FITS
* Dec 19 1997 Add IRAF version 2 .imh files
*
* Jan 2 1998 Allow uneven length of user parameter lines in IRAF headers
* Jan 6 1998 Fix output of imh2 headers; allow newlines in imh1 headers
* Jan 14 1998 Handle byte reversing correctly
* Apr 17 1998 Add new IRAF data types unsigned char and unsigned short
* Apr 30 1998 Fix error return if illegal data type after Allan Brighton
* May 15 1998 Delete header keywords used for IRAF binary values
* May 15 1998 Fix bug so FITS OBJECT is put into IRAF title
* May 26 1998 Fix bug in fits2iraf keeping track of end of header
* May 27 1998 Include fitsio.h instead of fitshead.h
* Jun 4 1998 Write comments into header for converted IRAF binary values
* Jun 4 1998 Pad FITS strings to 8 character minimum
* Jul 24 1998 Write header file length to IRAF header file
* Jul 27 1998 Print error messages to stderr for all failed malloc's
* Jul 27 1998 Fix bug padding FITS header with spaces in iraf2fits
* Jul 27 1998 Write modification time to IRAF header file
* Aug 6 1998 Change fitsio.h to fitsfile.h; imhio.c to imhfile.c
* Oct 1 1998 Set irafswap flag only once per file
* Oct 5 1998 Add subroutines irafsize() and isiraf()
* Nov 16 1998 Fix byte-swap checking
*
* Jan 27 1999 Read and write all of 3D image if one dimension is =1
* Jul 13 1999 Improve error messages; change irafsize() argument to fd
* Sep 22 1999 Don't copy OBJECT keyword from .imh file; use binary title
* Oct 14 1999 Set FITS header length
* Oct 20 1999 Allocate 5000 extra bytes for IRAF header
* Nov 2 1999 Fix getclocktime() to use only time.h subroutines
* Nov 2 1999 Add modification date and time to FITS header in iraf2fits()
* Nov 24 1999 Delete HEADSWAP, IMHVER, DATE-MOD from header before writing
* Nov 29 1999 Delete PIXSWAP, IRAF-MIN, IRAF-MAX from header before writing
*
* Jan 13 2000 Fix bug which dropped characters in iraf2fits()
* Feb 3 2000 Declare timezone long, not time_t; drop unused variable
* Mar 7 2000 Add more code to keep pixel file path short
* Mar 10 2000 Fix bugs when writing .imh file headers
* Mar 21 2000 Change computation of IRAF time tags to use only data structure
* Mar 22 2000 Move IRAF time tag computation to lt2tsi() in dateutil.c
* Mar 24 2000 Use Unix file update time if none in header
* Mar 27 2000 Use hputm() to save file paths up to 256 characters
* Mar 27 2000 Write filename comments after 1st keyword with short value
* Mar 27 2000 Allocate pixel file name in same_path to imh2 length
* Mar 29 2000 Add space after last linefeed of header in fits2iraf()
* Apr 28 2000 Dimension pixname in irafwimage()
* May 1 2000 Fix code for updating pixel file name with HDR$ in fits2iraf()
* Jun 2 2000 Drop unused variables in fits2iraf() after lint
* Jun 12 2000 If pixel filename has no / or $, use same path as header file
* Sep 6 2000 Use header directory if pixel file not found at its pathname
*
* Jan 11 2001 Print all messages to stderr
* Aug 24 2001 In isiraf(), return 0 if argument contains an equal sign
*
* Apr 8 2002 Fix bug in error message for unidentified nbits in fits2iraf()
*
* Feb 4 2003 Open catalog file rb instead of r (Martin Ploner, Bern)
* Oct 31 2003 Read image only in irafrimage() if physical dimension > image dim.
* Nov 3 2003 Set NAXISi to image, not physical dimensions in iraf2fits()
*
* Jun 13 2005 Drop trailing spaces on pixel file name
*
* Jun 20 2006 Initialize uninitialized variables
*
* Jan 4 2007 Change hputr4() calls to send pointer to value
* Jan 8 2007 Drop unused variable nbx in irafrimage()
* Jan 8 2007 Align header and image buffers properly by 4 and by BITPIX
*
* May 20 2011 Free newpixname, not pixname in irafwimage()
*
* Mar 27 2012 Fix pixname's appending to newpixname to avoid overflow
*/
|