-
Notifications
You must be signed in to change notification settings - Fork 1
/
iraffits.c
2101 lines (1765 loc) · 56.4 KB
/
iraffits.c
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
/*------------------------------------------------------------------------*/
/* */
/* These routines have been modified by William Pence for use by CFITSIO */
/* The original files were provided by Doug Mink */
/*------------------------------------------------------------------------*/
/* File imhfile.c
* August 6, 1998
* By Doug Mink, based on Mike VanHilst's readiraf.c
* Module: imhfile.c (IRAF .imh image file reading and writing)
* Purpose: Read and write IRAF image files (and translate headers)
* 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: 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: 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
* 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 "fitsio2.h"
#include <stdio.h> /* define stderr, FD, and NULL */
#include <stdlib.h>
#include <stddef.h> /* stddef.h is apparently needed to define size_t */
#include <string.h>
#define FILE_NOT_OPENED 104
/* 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_PIXHDR 1024
#define MAXINT 2147483647 /* Biggest number that can fit in long */
static int isirafswapped(char *irafheader, int offset);
static int irafgeti4(char *irafheader, int offset);
static char *irafgetc2(char *irafheader, int offset, int nc);
static char *irafgetc(char *irafheader, int offset, int nc);
static char *iraf2str(char *irafstring, int nchar);
static char *irafrdhead(const char *filename, int *lihead);
static int irafrdimage (char **buffptr, size_t *buffsize,
size_t *filesize, int *status);
static int iraftofits (char *hdrname, char *irafheader, int nbiraf,
char **buffptr, size_t *nbfits, size_t *fitssize, int *status);
static char *same_path(char *pixname, const char *hdrname);
static int swaphead=0; /* =1 to swap data bytes of IRAF header values */
static int swapdata=0; /* =1 to swap bytes in IRAF data pixels */
static void irafswap(int bitpix, char *string, int nbytes);
static void irafswap2(char *string, int nbytes);
static void irafswap4(char *string, int nbytes);
static void irafswap8(char *string, int nbytes);
static int pix_version (char *irafheader);
static int irafncmp (char *irafheader, char *teststring, int nc);
static int machswap(void);
static int head_version (char *irafheader);
static int hgeti4(char* hstring, char* keyword, int* val);
static int hgets(char* hstring, char* keyword, int lstr, char* string);
static char* hgetc(char* hstring, char* keyword);
static char* ksearch(char* hstring, char* keyword);
static char *blsearch (char* hstring, char* keyword);
static char *strsrch (char* s1, char* s2);
static char *strnsrch ( char* s1,char* s2,int ls1);
static void hputi4(char* hstring,char* keyword, int ival);
static void hputs(char* hstring,char* keyword,char* cval);
static void hputcom(char* hstring,char* keyword,char* comment);
static void hputl(char* hstring,char* keyword,int lval);
static void hputc(char* hstring,char* keyword,char* cval);
static int getirafpixname (const char *hdrname, char *irafheader, char *pixfilename, int *status);
int iraf2mem(char *filename, char **buffptr, size_t *buffsize,
size_t *filesize, int *status);
void ffpmsg(const char *err_message);
/* CFITS_API is defined below for use on Windows systems. */
/* It is used to identify the public functions which should be exported. */
/* This has no effect on non-windows platforms where "WIN32" is not defined */
/* this is only needed to export the "fits_delete_iraf_file" symbol, which */
/* is called in fpackutil.c (and perhaps in other applications programs) */
#if defined (WIN32)
#if defined(cfitsio_EXPORTS)
#define CFITS_API __declspec(dllexport)
#else
#define CFITS_API //__declspec(dllimport)
#endif /* CFITS_API */
#else /* defined (WIN32) */
#define CFITS_API
#endif
int CFITS_API fits_delete_iraf_file(const char *filename, int *status);
/*--------------------------------------------------------------------------*/
int fits_delete_iraf_file(const char *filename, /* name of input file */
int *status) /* IO - error status */
/*
Delete the iraf .imh header file and the associated .pix data file
*/
{
char *irafheader;
int lenirafhead;
char pixfilename[SZ_IM2PIXFILE+1];
/* read IRAF header into dynamically created char array (free it later!) */
irafheader = irafrdhead(filename, &lenirafhead);
if (!irafheader)
{
return(*status = FILE_NOT_OPENED);
}
getirafpixname (filename, irafheader, pixfilename, status);
/* don't need the IRAF header any more */
free(irafheader);
if (*status > 0)
return(*status);
remove(filename);
remove(pixfilename);
return(*status);
}
/*--------------------------------------------------------------------------*/
int iraf2mem(char *filename, /* name of input file */
char **buffptr, /* O - memory pointer (initially NULL) */
size_t *buffsize, /* O - size of mem buffer, in bytes */
size_t *filesize, /* O - size of FITS file, in bytes */
int *status) /* IO - error status */
/*
Driver routine that reads an IRAF image into memory, also converting
it into FITS format.
*/
{
char *irafheader;
int lenirafhead;
*buffptr = NULL;
*buffsize = 0;
*filesize = 0;
/* read IRAF header into dynamically created char array (free it later!) */
irafheader = irafrdhead(filename, &lenirafhead);
if (!irafheader)
{
return(*status = FILE_NOT_OPENED);
}
/* convert IRAF header to FITS header in memory */
iraftofits(filename, irafheader, lenirafhead, buffptr, buffsize, filesize,
status);
/* don't need the IRAF header any more */
free(irafheader);
if (*status > 0)
return(*status);
*filesize = (((*filesize - 1) / 2880 ) + 1 ) * 2880; /* multiple of 2880 */
/* append the image data onto the FITS header */
irafrdimage(buffptr, buffsize, filesize, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
/* Subroutine: irafrdhead (was irafrhead in D. Mink's original code)
* Purpose: Open and read the iraf .imh file.
* 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.
*/
static char *irafrdhead (
const char *filename, /* Name of IRAF header file */
int *lihead) /* Length of IRAF image header in bytes (returned) */
{
FILE *fd;
int nbr;
char *irafheader;
char errmsg[FLEN_ERRMSG];
long nbhead;
int nihead;
*lihead = 0;
/* open the image header file */
fd = fopen (filename, "rb");
if (fd == NULL) {
ffpmsg("unable to open IRAF header file:");
ffpmsg(filename);
return (NULL);
}
/* Find size of image header file */
if (fseek(fd, 0, 2) != 0) /* move to end of the file */
{
ffpmsg("IRAFRHEAD: cannot seek in file:");
ffpmsg(filename);
return(NULL);
}
nbhead = ftell(fd); /* position = size of file */
if (nbhead < 0)
{
ffpmsg("IRAFRHEAD: cannot get pos. in file:");
ffpmsg(filename);
return(NULL);
}
if (fseek(fd, 0, 0) != 0) /* move back to beginning */
{
ffpmsg("IRAFRHEAD: cannot seek to beginning of file:");
ffpmsg(filename);
return(NULL);
}
/* allocate initial sized buffer */
nihead = nbhead + 5000;
irafheader = (char *) calloc (1, nihead);
if (irafheader == NULL) {
snprintf(errmsg, FLEN_ERRMSG,"IRAFRHEAD Cannot allocate %d-byte header",
nihead);
ffpmsg(errmsg);
ffpmsg(filename);
return (NULL);
}
*lihead = nihead;
/* Read IRAF header */
nbr = fread (irafheader, 1, nbhead, fd);
fclose (fd);
/* Reject if header less than minimum length */
if (nbr < LEN_PIXHDR) {
snprintf(errmsg, FLEN_ERRMSG,"IRAFRHEAD header file: %d / %d bytes read.",
nbr,LEN_PIXHDR);
ffpmsg(errmsg);
ffpmsg(filename);
free (irafheader);
return (NULL);
}
return (irafheader);
}
/*--------------------------------------------------------------------------*/
static int irafrdimage (
char **buffptr, /* FITS image header (filled) */
size_t *buffsize, /* allocated size of the buffer */
size_t *filesize, /* actual size of the FITS file */
int *status)
{
FILE *fd;
char *bang;
int nax = 1, naxis1 = 1, naxis2 = 1, naxis3 = 1, naxis4 = 1, npaxis1 = 1, npaxis2;
int bitpix, bytepix, i;
char *fitsheader, *image;
int nbr, nbimage, nbaxis, nbl, nbdiff;
char *pixheader;
char *linebuff;
int imhver, lpixhead = 0;
char pixname[SZ_IM2PIXFILE+1];
char errmsg[FLEN_ERRMSG];
size_t newfilesize;
fitsheader = *buffptr; /* pointer to start of header */
/* Convert pixel file name to character string */
hgets (fitsheader, "PIXFILE", SZ_IM2PIXFILE, pixname);
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");
/* Print error message and exit if pixel file is not found */
if (!fd) {
ffpmsg("IRAFRIMAGE: Cannot open IRAF pixel file:");
ffpmsg(pixname);
return (*status = FILE_NOT_OPENED);
}
/* Read pixel header */
pixheader = (char *) calloc (lpixhead, 1);
if (pixheader == NULL) {
ffpmsg("IRAFRIMAGE: Cannot alloc memory for pixel header");
ffpmsg(pixname);
fclose (fd);
return (*status = FILE_NOT_OPENED);
}
nbr = fread (pixheader, 1, lpixhead, fd);
/* Check size of pixel header */
if (nbr < lpixhead) {
snprintf(errmsg, FLEN_ERRMSG,"IRAF pixel file: %d / %d bytes read.",
nbr,LEN_PIXHDR);
ffpmsg(errmsg);
free (pixheader);
fclose (fd);
return (*status = FILE_NOT_OPENED);
}
/* check pixel header magic word */
imhver = pix_version (pixheader);
if (imhver < 1) {
ffpmsg("File not valid IRAF pixel file:");
ffpmsg(pixname);
free (pixheader);
fclose (fd);
return (*status = FILE_NOT_OPENED);
}
free (pixheader);
/* Find number of bytes to read */
hgeti4 (fitsheader,"NAXIS",&nax);
hgeti4 (fitsheader,"NAXIS1",&naxis1);
hgeti4 (fitsheader,"NPAXIS1",&npaxis1);
if (nax > 1) {
hgeti4 (fitsheader,"NAXIS2",&naxis2);
hgeti4 (fitsheader,"NPAXIS2",&npaxis2);
}
if (nax > 2)
hgeti4 (fitsheader,"NAXIS3",&naxis3);
if (nax > 3)
hgeti4 (fitsheader,"NAXIS4",&naxis4);
hgeti4 (fitsheader,"BITPIX",&bitpix);
if (bitpix < 0)
bytepix = -bitpix / 8;
else
bytepix = bitpix / 8;
nbimage = naxis1 * naxis2 * naxis3 * naxis4 * bytepix;
newfilesize = *filesize + nbimage; /* header + data */
newfilesize = (((newfilesize - 1) / 2880 ) + 1 ) * 2880;
if (newfilesize > *buffsize) /* need to allocate more memory? */
{
fitsheader = (char *) realloc (*buffptr, newfilesize);
if (fitsheader == NULL) {
snprintf(errmsg, FLEN_ERRMSG,"IRAFRIMAGE Cannot allocate %d-byte image buffer",
(int) (*filesize));
ffpmsg(errmsg);
ffpmsg(pixname);
fclose (fd);
return (*status = FILE_NOT_OPENED);
}
}
*buffptr = fitsheader;
*buffsize = newfilesize;
image = fitsheader + *filesize;
*filesize = newfilesize;
/* 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;
fseek (fd, nbdiff, 1);
linebuff = linebuff + nbaxis;
}
}
fclose (fd);
/* Check size of image */
if (nbr < nbimage) {
snprintf(errmsg, FLEN_ERRMSG,"IRAF pixel file: %d / %d bytes read.",
nbr,nbimage);
ffpmsg(errmsg);
ffpmsg(pixname);
return (*status = FILE_NOT_OPENED);
}
/* Byte-reverse image, if necessary */
if (swapdata)
irafswap (bitpix, image, nbimage);
return (*status);
}
/*--------------------------------------------------------------------------*/
/* Return IRAF image format version number from magic word in IRAF header*/
static int head_version (
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 */
static int pix_version (
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 */
static int irafncmp (
char *irafheader, /* IRAF image header from file */
char *teststring, /* C character string to compare */
int nc) /* Number of characters to compate */
{
char *line;
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 */
static int iraftofits (
char *hdrname, /* IRAF header file name (may be path) */
char *irafheader, /* IRAF image header */
int nbiraf, /* Number of bytes in IRAF header */
char **buffptr, /* pointer to the FITS header */
size_t *nbfits, /* allocated size of the FITS header buffer */
size_t *fitssize, /* Number of bytes in FITS header (returned) */
/* = number of bytes to the end of the END keyword */
int *status)
{
char *objname; /* object name from FITS file */
int lstr, i, j, k, ib, nax, nbits;
char *pixname, *newpixname, *bang, *chead;
char *fitsheader;
int nblock, nlines;
char *fhead, *fhead1, *fp, endline[81];
char irafchar;
char fitsline[81];
int pixtype;
int imhver, n, imu, pixoff, impixoff;
/* int immax, immin, imtime; */
int imndim, imlen, imphyslen, impixtype;
char errmsg[FLEN_ERRMSG];
/* 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) {
ffpmsg("File not valid IRAF image header");
ffpmsg(hdrname);
return(*status = FILE_NOT_OPENED);
}
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) {
snprintf(errmsg, FLEN_ERRMSG,"IRAF2FITS Cannot allocate %d-byte FITS header",
(int) (*nbfits));
ffpmsg(hdrname);
return (*status = FILE_NOT_OPENED);
}
fhead = fitsheader;
*buffptr = fitsheader;
(void)strncpy (fitsheader, endline, 80);
hputl (fitsheader, "SIMPLE", 1);
fhead = fhead + 80;
/* check if the IRAF file is in big endian (sun) format (= 0) or not. */
/* This is done by checking the 4 byte integer in the header that */
/* represents the iraf pixel type. This 4-byte word is guaranteed to */
/* have the least sig byte != 0 and the most sig byte = 0, so if the */
/* first byte of the word != 0, then the file in little endian format */
/* like on an Alpha machine. */
swaphead = isirafswapped(irafheader, impixtype);
if (imhver == 1)
swapdata = swaphead; /* vers 1 data has same swapness as header */
else
swapdata = irafgeti4 (irafheader, IM2_SWAPPED);
/* 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:
snprintf(errmsg,FLEN_ERRMSG,"Unsupported IRAF data type: %d", pixtype);
ffpmsg(errmsg);
ffpmsg(hdrname);
return (*status = FILE_NOT_OPENED);
}
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]");
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 header filename in header */
hputs (fitsheader,"IMHFILE",hdrname);
hputcom (fitsheader,"IMHFILE", "IRAF header file name");
fhead = fhead + 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);
if (newpixname) {
free (pixname);
pixname = newpixname;
}
}
if (strchr (pixname, '/') == NULL && strchr (pixname, '$') == NULL) {
newpixname = same_path (pixname, hdrname);
if (newpixname) {
free (pixname);
pixname = newpixname;
}
}
if ((bang = strchr (pixname, '!')) != NULL )
hputs (fitsheader,"PIXFILE",bang+1);
else
hputs (fitsheader,"PIXFILE",pixname);
free (pixname);
hputcom (fitsheader,"PIXFILE", "IRAF .pix pixel file");
fhead = fhead + 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;
/* Save flag as to whether to swap IRAF data for this file and machine */
if (swapdata)
hputl (fitsheader, "PIXSWAP", 1);
else
hputl (fitsheader, "PIXSWAP", 0);
hputcom (fitsheader,"PIXSWAP", "IRAF pixels, FITS byte orders differ if T");
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 (swaphead == 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);
*fitssize = fhead - fitsheader; /* no. of bytes to end of END keyword */
/* Pad rest of header with spaces */
strncpy (endline," ",3);
for (fp = fhead; fp < fhead1; fp = fp + 80) {
(void)strncpy (fp, endline,80);
}
return (*status);
}
/*--------------------------------------------------------------------------*/
/* get the IRAF pixel file name */
static int getirafpixname (
const char *hdrname, /* IRAF header file name (may be path) */
char *irafheader, /* IRAF image header */
char *pixfilename, /* IRAF pixel file name */
int *status)
{
int imhver;
char *pixname, *newpixname, *bang;
/* Check header magic word */
imhver = head_version (irafheader);
if (imhver < 1) {
ffpmsg("File not valid IRAF image header");
ffpmsg(hdrname);
return(*status = FILE_NOT_OPENED);
}
/* get 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);
if (newpixname) {
free (pixname);
pixname = newpixname;
}
}
if (strchr (pixname, '/') == NULL && strchr (pixname, '$') == NULL) {
newpixname = same_path (pixname, hdrname);
if (newpixname) {
free (pixname);
pixname = newpixname;
}
}
if ((bang = strchr (pixname, '!')) != NULL )
strcpy(pixfilename,bang+1);
else
strcpy(pixfilename,pixname);
free (pixname);
return (*status);
}
/*--------------------------------------------------------------------------*/
/* Put filename and header path together */
static char *same_path (
char *pixname, /* IRAF pixel file pathname */
const char *hdrname) /* IRAF image header file pathname */
{
int len;
char *newpixname;
/* WDP - 10/16/2007 - increased allocation to avoid possible overflow */
/* newpixname = (char *) calloc (SZ_IM2PIXFILE, sizeof (char)); */
newpixname = (char *) calloc (2*SZ_IM2PIXFILE+1, sizeof (char));
if (newpixname == NULL) {
ffpmsg("iraffits same_path: Cannot alloc memory for newpixname");
return (NULL);
}
/* 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';
(void)strncat (newpixname, &pixname[4], SZ_IM2PIXFILE);
}
/* 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--;