1 /******************************************************************************/
2 /* Peter Kirchgessner */
3 /* e-mail: pkirchg@aol.com */
4 /* WWW : http://members.aol.com/pkirchg */
5 /******************************************************************************/
8 /* Package : FITS reading/writing library */
9 /* Modul-Name : fitsrw.c */
10 /* Description : Support of reading/writing FITS-files */
11 /* Function(s) : fits_new_filestruct - (local) initialize file structure*/
12 /* fits_new_hdulist - (local) initialize hdulist struct*/
13 /* fits_delete_filestruct - (local) delete file structure */
14 /* fits_delete_recordlist - (local) delete record list */
15 /* fits_delete_hdulist - (local) delete hdu list */
16 /* fits_nan_32 - (local) check IEEE NaN values */
17 /* fits_nan_64 - (local) check IEEE NaN values */
18 /* fits_get_error - get error message */
19 /* fits_set_error - (local) set error message */
20 /* fits_drop_error - (local) remove an error message */
21 /* fits_open - open a FITS file */
22 /* fits_close - close a FITS file */
23 /* fits_add_hdu - add a HDU to a FITS file */
24 /* fits_add_card - add a card to the HDU */
25 /* fits_print_header - print a single FITS header */
26 /* fits_read_header - (local) read in FITS header */
27 /* fits_write_header - write a FITS header */
28 /* fits_decode_header - (local) decode a header */
29 /* fits_eval_pixrange - (local) evaluate range of pixels */
30 /* fits_decode_card - decode a card */
31 /* fits_search_card - search a card in a record list */
32 /* fits_image_info - get information about image */
33 /* fits_seek_image - position to an image */
34 /* fits_read_pixel - read pixel values from file */
35 /* fits_to_pgmraw - convert FITS-file to PGM-file */
36 /* pgmraw_to_fits - convert PGM-file to FITS-file */
38 /* Author : P. Kirchgessner */
39 /* Date of Gen. : 12-Apr-97 */
40 /* Last modified : 20-Dec-97 */
44 /* #MOD-0001, nn, 20-Dec-97, Initialize some variables */
47 /******************************************************************************/
49 /* - NOST, Definition of the Flexible Image Transport System (FITS), */
50 /* September 29, 1995, Standard, NOST 100-1.1 */
51 /* (ftp://nssdc.gsfc.nasa.gov/pub/fits/fits_standard_ps.Z) */
52 /* - The FITS IMAGE Extension. A Proposal. J.D. Ponz, R.W. Thompson, */
53 /* J.R. Munoz, Feb. 7, 1992 */
54 /* (ftp://www.cv.nrao.edu/fits/documents/standards/image.ps.gz) */
56 /******************************************************************************/
59 /* Identifikation: "@(#) <product> <ver> <dd-mmm-yy>" */
60 static char ident
[] = "@(#) libfits.c 0.11 20-Dec-97 (%I%)";
62 /******************************************************************************/
63 /* FITS reading/writing library */
64 /* Copyright (C) 1997 Peter Kirchgessner */
65 /* (email: pkirchg@aol.com, WWW: http://members.aol.com/pkirchg) */
66 /* The library was developed for a FITS-plug-in to GIMP, the GNU Image */
67 /* Manipulation Program. But it is completely independant to that. If someone */
68 /* finds it useful for other purposes, try to keep it independant from your */
70 /******************************************************************************/
72 /* This program is free software; you can redistribute it and/or modify */
73 /* it under the terms of the GNU General Public License as published by */
74 /* the Free Software Foundation; either version 2 of the License, or */
75 /* (at your option) any later version. */
77 /* This program is distributed in the hope that it will be useful, */
78 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
79 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
80 /* GNU General Public License for more details. */
82 /* You should have received a copy of the GNU General Public License */
83 /* along with this program; if not, write to the Free Software */
84 /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
85 /******************************************************************************/
94 /* Declaration of local funtions */
96 static FITS_FILE
*fits_new_filestruct (void);
97 static FITS_HDU_LIST
*fits_new_hdulist (void);
98 static void fits_delete_filestruct (FITS_FILE
*ff
);
99 static void fits_delete_recordlist (FITS_RECORD_LIST
*rl
);
100 static void fits_delete_hdulist (FITS_HDU_LIST
*hl
);
101 int fits_nan_32 (unsigned char *value
);
102 int fits_nan_64 (unsigned char *value
);
103 static void fits_set_error (const char *errmsg
);
104 static void fits_drop_error (void);
105 static FITS_RECORD_LIST
*fits_read_header (FILE *fp
, int *nrec
);
106 static FITS_HDU_LIST
*fits_decode_header (FITS_RECORD_LIST
*hdr
,
107 long hdr_offset
, long dat_offset
);
108 static int fits_eval_pixrange (FILE *fp
, FITS_HDU_LIST
*hdu
);
111 /* Error handling like a FIFO */
112 #define FITS_MAX_ERROR 16
113 #define FITS_ERROR_LENGTH 256
114 static int fits_n_error
= 0;
115 static char fits_error
[FITS_MAX_ERROR
][FITS_ERROR_LENGTH
];
117 /* What byte ordering for IEEE-format we are running on ? */
118 int fits_ieee32_intel
= 0;
119 int fits_ieee32_motorola
= 0;
120 int fits_ieee64_intel
= 0;
121 int fits_ieee64_motorola
= 0;
124 #define FITS_RETURN(msg, val) { fits_set_error (msg); return (val); }
125 #define FITS_VRETURN(msg) { fits_set_error (msg); return; }
127 /* Get pixel values from memory. p must be an (unsigned char *) */
128 #define FITS_GETBITPIX16(p,val) val = ((p[0] << 8) | (p[1]))
129 #define FITS_GETBITPIX32(p,val) val = \
130 ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])
132 /* Get floating point values from memory. p must be an (unsigned char *). */
133 /* The floating point values must directly correspond */
134 /* to machine representation. Otherwise it does not work. */
135 #define FITS_GETBITPIXM32(p,val) \
136 { if (fits_ieee32_intel) {unsigned char uc[4]; \
137 uc[0] = p[3]; uc[1] = p[2]; uc[2] = p[1]; uc[3] = p[0]; \
138 val = *(FITS_BITPIXM32 *)uc; } \
139 else if (fits_ieee32_motorola) { val = *(FITS_BITPIXM32 *)p; } \
140 else if (fits_ieee64_motorola) {FITS_BITPIXM64 m64; \
141 unsigned char *uc= (unsigned char *)&m64; \
142 uc[0]=p[0]; uc[1]=p[1]; uc[2]=p[2]; uc[3]=p[3]; uc[4]=uc[5]=uc[6]=uc[7]=0; \
143 val = (FITS_BITPIXM32)m64; } \
144 else if (fits_ieee64_intel) {FITS_BITPIXM64 i64; \
145 unsigned char *uc= (unsigned char *)&i64; \
146 uc[0]=uc[1]=uc[2]=uc[3]=0; uc[7]=p[0]; uc[6]=p[1]; uc[5]=p[2]; uc[4]=p[3]; \
147 val = (FITS_BITPIXM32)i64;}\
150 #define FITS_GETBITPIXM64(p,val) \
151 { if (fits_ieee64_intel) {unsigned char uc[8]; \
152 uc[0] = p[7]; uc[1] = p[6]; uc[2] = p[5]; uc[3] = p[4]; \
153 uc[4] = p[3]; uc[5] = p[2]; uc[6] = p[1]; uc[7] = p[0]; \
154 val = *(FITS_BITPIXM64 *)uc; } else val = *(FITS_BITPIXM64 *)p; }
156 #define FITS_WRITE_BOOLCARD(fp,key,value) \
158 snprintf (card, sizeof( card ), "%-8.8s= %20s%50s", key, value ? "T" : "F", " "); \
159 fwrite (card, 1, 80, fp); }
161 #define FITS_WRITE_LONGCARD(fp,key,value) \
163 snprintf (card, sizeof( card ), "%-8.8s= %20ld%50s", key, (long)value, " "); \
164 fwrite (card, 1, 80, fp); }
166 #define FITS_WRITE_DOUBLECARD(fp,key,value) \
167 {char card[81], dbl[21], *istr; \
168 snprintf (dbl, sizeof( dbl ), "%20f", (double)value); istr = strstr (dbl, "e"); \
169 if (istr) *istr = 'E'; \
170 snprintf (card, sizeof( card ), "%-8.8s= %20.20s%50s", key, dbl, " "); \
171 fwrite (card, 1, 80, fp); }
173 #define FITS_WRITE_STRINGCARD(fp,key,value) \
174 {char card[81]; int k;\
175 snprintf (card, sizeof( card ), "%-8.8s= \'%s", key, value); \
176 for (k = strlen (card); k < 81; k++) card[k] = ' '; \
177 k = strlen (key); if (k < 8) card[19] = '\''; else card[11+k] = '\''; \
178 fwrite (card, 1, 80, fp); }
180 #define FITS_WRITE_CARD(fp,value) \
182 snprintf (card, sizeof( card ), "%-80.80s", value); \
183 fwrite (card, 1, 80, fp); }
186 /*****************************************************************************/
189 /* Function : fits_new_filestruct - (local) initialize file structure */
194 /* Returns a pointer to an initialized fits file structure. */
195 /* On failure, a NULL-pointer is returned. */
198 /*****************************************************************************/
200 static FITS_FILE
*fits_new_filestruct (void)
204 ff
= (FITS_FILE
*)malloc (sizeof (FITS_FILE
));
205 if (ff
== NULL
) return (NULL
);
207 memset ((char *)ff
, 0, sizeof (*ff
));
212 /*****************************************************************************/
215 /* Function : fits_new_hdulist - (local) initialize hdulist structure */
220 /* Returns a pointer to an initialized hdulist structure. */
221 /* On failure, a NULL-pointer is returned. */
224 /*****************************************************************************/
226 static FITS_HDU_LIST
*fits_new_hdulist (void)
230 hdl
= (FITS_HDU_LIST
*)malloc (sizeof (FITS_HDU_LIST
));
231 if (hdl
== NULL
) return (NULL
);
233 memset ((char *)hdl
, 0, sizeof (*hdl
));
234 hdl
->pixmin
= hdl
->pixmax
= hdl
->datamin
= hdl
->datamax
= 0.0;
235 hdl
->bzero
= hdl
->bscale
= 0.0;
241 /*****************************************************************************/
244 /* Function : fits_delete_filestruct - (local) delete file structure */
247 /* FITS_FILE *ff [I] : pointer to fits file structure */
248 /* ( mode : I=input, O=output, I/O=input/output ) */
250 /* Frees all memory allocated by the file structure. */
253 /*****************************************************************************/
255 static void fits_delete_filestruct (FITS_FILE
*ff
)
258 if (ff
== NULL
) return;
260 fits_delete_hdulist (ff
->hdu_list
);
268 /*****************************************************************************/
271 /* Function : fits_delete_recordlist - (local) delete record list */
274 /* FITS_RECORD_LIST *rl [I] : record list to delete */
275 /* ( mode : I=input, O=output, I/O=input/output ) */
278 /*****************************************************************************/
280 static void fits_delete_recordlist (FITS_RECORD_LIST
*rl
)
282 {FITS_RECORD_LIST
*next
;
286 next
= rl
->next_record
;
287 rl
->next_record
= NULL
;
294 /*****************************************************************************/
297 /* Function : fits_delete_hdulist - (local) delete hdu list */
300 /* FITS_HDU_LIST *hl [I] : hdu list to delete */
301 /* ( mode : I=input, O=output, I/O=input/output ) */
304 /*****************************************************************************/
306 static void fits_delete_hdulist (FITS_HDU_LIST
*hl
)
308 {FITS_HDU_LIST
*next
;
312 fits_delete_recordlist (hl
->header_record_list
);
321 /*****************************************************************************/
324 /* Function : fits_nan_32 - (local) check for IEEE NaN values (32 bit) */
327 /* unsigned char *v [I] : value to check */
328 /* ( mode : I=input, O=output, I/O=input/output ) */
330 /* The function returns 1 if the value is a NaN. Otherwise 0 is returned. */
331 /* The byte sequence at v must start with the sign/eponent byte. */
334 /*****************************************************************************/
336 int fits_nan_32 (unsigned char *v
)
338 {register unsigned long k
;
340 k
= (v
[0] << 24) | (v
[1] << 16) | (v
[2] << 8) | v
[3];
341 k
&= 0x7fffffff; /* Dont care about the sign bit */
343 /* See NOST Definition of the Flexible Image Transport System (FITS), */
344 /* Appendix F, IEEE special formats. */
345 return ( ((k
>= 0x7f7fffff) && (k
<= 0x7fffffff))
346 || ((k
>= 0x00000001) && (k
<= 0x00800000)));
350 /*****************************************************************************/
353 /* Function : fits_nan_64 - (local) check for IEEE NaN values (64 bit) */
356 /* unsigned char *v [I] : value to check */
357 /* ( mode : I=input, O=output, I/O=input/output ) */
359 /* The function returns 1 if the value is a NaN. Otherwise 0 is returned. */
360 /* The byte sequence at v must start with the sign/eponent byte. */
361 /* (currently we ignore the low order 4 bytes of the mantissa. Therefore */
362 /* this function is the same as for 32 bits). */
365 /*****************************************************************************/
367 int fits_nan_64 (unsigned char *v
)
369 {register unsigned long k
;
371 k
= (v
[0] << 24) | (v
[1] << 16) | (v
[2] << 8) | v
[3];
372 k
&= 0x7fffffff; /* Dont care about the sign bit */
374 /* See NOST Definition of the Flexible Image Transport System (FITS), */
375 /* Appendix F, IEEE special formats. */
376 return ( ((k
>= 0x7f7fffff) && (k
<= 0x7fffffff))
377 || ((k
>= 0x00000001) && (k
<= 0x00800000)));
381 /*****************************************************************************/
384 /* Function : fits_get_error - get an error message */
389 /* If an error message has been set, a pointer to the message is returned. */
390 /* Otherwise a NULL pointer is returned. */
391 /* An inquired error message is removed from the error FIFO. */
394 /*****************************************************************************/
396 char *fits_get_error (void)
398 {static char errmsg
[FITS_ERROR_LENGTH
];
401 if (fits_n_error
<= 0) return (NULL
);
402 strcpy (errmsg
, fits_error
[0]);
404 for (k
= 1; k
< fits_n_error
; k
++)
405 strcpy (fits_error
[k
-1], fits_error
[k
]);
413 /*****************************************************************************/
416 /* Function : fits_set_error - (local) set an error message */
419 /* char *errmsg [I] : Error message to set */
420 /* ( mode : I=input, O=output, I/O=input/output ) */
422 /* Places the error message in the FIFO. If the FIFO is full, */
423 /* the message is discarded. */
426 /*****************************************************************************/
428 static void fits_set_error (const char *errmsg
)
431 if (fits_n_error
< FITS_MAX_ERROR
)
433 strncpy (fits_error
[fits_n_error
], errmsg
, FITS_ERROR_LENGTH
);
434 fits_error
[fits_n_error
++][FITS_ERROR_LENGTH
-1] = '\0';
439 /*****************************************************************************/
442 /* Function : fits_drop_error - (local) remove an error message */
447 /* Removes the last error message from the error message FIFO */
450 /*****************************************************************************/
452 static void fits_drop_error (void)
455 if (fits_n_error
> 0) fits_n_error
--;
459 /*****************************************************************************/
462 /* Function : fits_open - open a FITS file */
465 /* char *filename [I] : name of file to open */
466 /* char *openmode [I] : mode to open the file ("r", "w") */
467 /* ( mode : I=input, O=output, I/O=input/output ) */
469 /* On success, a FITS_FILE-pointer is returned. On failure, a NULL- */
470 /* pointer is returned. */
471 /* The functions scans through the file loading each header and analyzing */
475 /*****************************************************************************/
478 FITS_FILE
*fits_open (const char* filename
, const char *openmode
)
480 {int reading
, writing
, n_rec
, n_hdr
;
481 long fpos_header
, fpos_data
;
484 FITS_RECORD_LIST
*hdrlist
;
485 FITS_HDU_LIST
*hdulist
, *last_hdulist
;
487 if ((filename
== NULL
) || (*filename
== '\0') || (openmode
== NULL
))
488 FITS_RETURN ("fits_open: Invalid parameters", NULL
);
494 /* Check the IEEE-format we are running on */
497 unsigned char *op32
= (unsigned char *)&one32
;
498 unsigned char *op64
= (unsigned char *)&one64
;
500 if (sizeof (float) == 4)
502 fits_ieee32_intel
= (op32
[3] == 0x3f);
503 fits_ieee32_motorola
= (op32
[0] == 0x3f);
505 if (sizeof (double) == 8)
507 fits_ieee64_intel
= (op64
[7] == 0x3f);
508 fits_ieee64_motorola
= (op64
[0] == 0x3f);
512 reading
= (strcmp (openmode
, "r") == 0);
513 writing
= (strcmp (openmode
, "w") == 0);
514 if ((!reading
) && (!writing
))
515 FITS_RETURN ("fits_open: Invalid openmode", NULL
);
517 fp
= fopen (filename
, reading
? "rb" : "wb");
518 if (fp
== NULL
) FITS_RETURN ("fits_open: fopen() failed", NULL
);
520 ff
= fits_new_filestruct ();
524 FITS_RETURN ("fits_open: No more memory", NULL
);
528 ff
->openmode
= *openmode
;
530 if (writing
) return (ff
);
532 for (n_hdr
= 0; ; n_hdr
++) /* Read through all HDUs */
534 fpos_header
= ftell (fp
); /* Save file position of header */
535 hdrlist
= fits_read_header (fp
, &n_rec
);
539 if (n_hdr
> 0) /* At least one header must be present. */
540 fits_drop_error (); /* If we got a header already, drop the error */
543 fpos_data
= ftell (fp
); /* Save file position of data */
545 /* Decode the header */
546 hdulist
= fits_decode_header (hdrlist
, fpos_header
, fpos_data
);
549 fits_delete_recordlist (hdrlist
);
553 ff
->n_pic
+= hdulist
->numpic
;
555 if (hdulist
->used
.blank_value
) ff
->blank_used
= 1;
556 if (hdulist
->used
.nan_value
) ff
->nan_used
= 1;
559 ff
->hdu_list
= hdulist
;
561 last_hdulist
->next_hdu
= hdulist
;
562 last_hdulist
= hdulist
;
563 /* Evaluate the range of pixel data */
564 fits_eval_pixrange (fp
, hdulist
);
566 /* Reposition to start of next header */
567 if (fseek (fp
, hdulist
->data_offset
+hdulist
->data_size
, SEEK_SET
) < 0)
575 /*****************************************************************************/
578 /* Function : fits_close - close a FITS file */
581 /* FITS_FILE *ff [I] : FITS file pointer */
582 /* ( mode : I=input, O=output, I/O=input/output ) */
585 /*****************************************************************************/
587 void fits_close (FITS_FILE
*ff
)
590 if (ff
== NULL
) FITS_VRETURN ("fits_close: Invalid parameter");
594 fits_delete_filestruct (ff
);
598 /*****************************************************************************/
601 /* Function : fits_add_hdu - add a HDU to the file */
604 /* FITS_FILE *ff [I] : FITS file pointer */
605 /* ( mode : I=input, O=output, I/O=input/output ) */
607 /* Adds a new HDU to the list kept in ff. A pointer to the new HDU is */
608 /* returned. On failure, a NULL-pointer is returned. */
611 /*****************************************************************************/
613 FITS_HDU_LIST
*fits_add_hdu (FITS_FILE
*ff
)
615 {FITS_HDU_LIST
*newhdu
, *hdu
;
617 if (ff
->openmode
!= 'w')
618 FITS_RETURN ("fits_add_hdu: file not open for writing", NULL
);
620 newhdu
= fits_new_hdulist ();
621 if (newhdu
== NULL
) return (NULL
);
623 if (ff
->hdu_list
== NULL
)
625 ff
->hdu_list
= newhdu
;
630 while (hdu
->next_hdu
!= NULL
)
632 hdu
->next_hdu
= newhdu
;
639 /*****************************************************************************/
642 /* Function : fits_add_card - add a card to the HDU */
645 /* FITS_HDU_LIST *hdulist [I] : HDU listr */
646 /* char *card [I] : card to add */
647 /* ( mode : I=input, O=output, I/O=input/output ) */
649 /* The card must follow the standards of FITS. The card must not use a */
650 /* keyword that is written using *hdulist itself. On success 0 is returned. */
651 /* On failure -1 is returned. */
654 /*****************************************************************************/
656 int fits_add_card (FITS_HDU_LIST
*hdulist
, char *card
)
660 if (hdulist
->naddcards
>= FITS_NADD_CARDS
) return (-1);
663 if (k
< FITS_CARD_SIZE
)
665 memset (&(hdulist
->addcards
[hdulist
->naddcards
][k
]), ' ', FITS_CARD_SIZE
-k
);
666 memcpy (hdulist
->addcards
[(hdulist
->naddcards
)++], card
, k
);
670 memcpy (hdulist
->addcards
[(hdulist
->naddcards
)++], card
, FITS_CARD_SIZE
);
676 /*****************************************************************************/
679 /* Function : fits_print_header - print the internal representation */
680 /* of a single header */
682 /* FITS_HDU_LIST *hdr [I] : pointer to the header */
683 /* ( mode : I=input, O=output, I/O=input/output ) */
686 /*****************************************************************************/
688 void fits_print_header (FITS_HDU_LIST
*hdr
)
692 if (hdr
->used
.simple
)
693 printf ("Content of SIMPLE-header:\n");
695 printf ("Content of XTENSION-header %s:\n", hdr
->xtension
);
696 printf ("header_offset : %ld\n", hdr
->header_offset
);
697 printf ("data_offset : %ld\n", hdr
->data_offset
);
698 printf ("data_size : %ld\n", hdr
->data_size
);
699 printf ("used data_size: %ld\n", hdr
->udata_size
);
700 printf ("bytes p.pixel : %d\n", hdr
->bpp
);
701 printf ("pixmin : %f\n", hdr
->pixmin
);
702 printf ("pixmax : %f\n", hdr
->pixmax
);
704 printf ("naxis : %d\n", hdr
->naxis
);
705 for (k
= 1; k
<= hdr
->naxis
; k
++)
706 printf ("naxis%-3d : %d\n", k
, hdr
->naxisn
[k
-1]);
708 printf ("bitpix : %d\n", hdr
->bitpix
);
711 printf ("blank : %ld\n", hdr
->blank
);
713 printf ("blank : not used\n");
715 if (hdr
->used
.datamin
)
716 printf ("datamin : %f\n", hdr
->datamin
);
718 printf ("datamin : not used\n");
719 if (hdr
->used
.datamax
)
720 printf ("datamax : %f\n", hdr
->datamax
);
722 printf ("datamax : not used\n");
724 if (hdr
->used
.gcount
)
725 printf ("gcount : %ld\n", hdr
->gcount
);
727 printf ("gcount : not used\n");
728 if (hdr
->used
.pcount
)
729 printf ("pcount : %ld\n", hdr
->pcount
);
731 printf ("pcount : not used\n");
733 if (hdr
->used
.bscale
)
734 printf ("bscale : %f\n", hdr
->bscale
);
736 printf ("bscale : not used\n");
738 printf ("bzero : %f\n", hdr
->bzero
);
740 printf ("bzero : not used\n");
744 /*****************************************************************************/
747 /* Function : fits_read_header - (local) read FITS header */
750 /* FILE *fp [I] : file pointer */
751 /* int *nrec [O] : number of records read */
752 /* ( mode : I=input, O=output, I/O=input/output ) */
754 /* Reads in all header records up to the record that keeps the END-card. */
755 /* A pointer to the record list is returned on success. */
756 /* On failure, a NULL-pointer is returned. */
759 /*****************************************************************************/
761 static FITS_RECORD_LIST
*fits_read_header (FILE *fp
, int *nrec
)
763 {unsigned char record
[FITS_RECORD_SIZE
];
764 FITS_RECORD_LIST
*start_list
= NULL
, *cu_record
= NULL
, *new_record
;
766 int k
, simple
, xtension
;
770 k
= fread (record
, 1, FITS_RECORD_SIZE
, fp
);
771 if (k
!= FITS_RECORD_SIZE
)
772 FITS_RETURN ("fits_read_header: Error in read of first record", NULL
);
774 simple
= (strncmp (record
, "SIMPLE ", 8) == 0);
775 xtension
= (strncmp (record
, "XTENSION", 8) == 0);
776 if ((!simple
) && (!xtension
))
777 FITS_RETURN ("fits_read_header: Missing keyword SIMPLE or XTENSION", NULL
);
781 fdat
= fits_decode_card (record
, typ_fbool
);
782 if (fdat
&& !fdat
->fbool
)
783 fits_set_error ("fits_read_header (warning): keyword SIMPLE does not have\
787 for (;;) /* Process all header records */
789 new_record
= (FITS_RECORD_LIST
*)malloc (sizeof (FITS_RECORD_LIST
));
790 if (new_record
== NULL
)
792 fits_delete_recordlist (start_list
);
793 FITS_RETURN ("fits_read_header: Not enough memory", NULL
);
795 memcpy (new_record
->data
, record
, FITS_RECORD_SIZE
);
796 new_record
->next_record
= NULL
;
799 if (start_list
== NULL
) /* Add new record to the list */
800 start_list
= new_record
;
802 cu_record
->next_record
= new_record
;
804 cu_record
= new_record
;
805 /* Was this the last record ? */
806 if (fits_search_card (cu_record
, "END") != NULL
) break;
808 k
= fread (record
, 1, FITS_RECORD_SIZE
, fp
);
809 if (k
!= FITS_RECORD_SIZE
)
810 FITS_RETURN ("fits_read_header: Error in read of record", NULL
);
816 /*****************************************************************************/
819 /* Function : fits_write_header - write a FITS header */
822 /* FITS_FILE *ff [I] : FITS-file pointer */
823 /* FITS_HDU_LIST [I] : pointer to header */
824 /* ( mode : I=input, O=output, I/O=input/output ) */
826 /* Writes a header to the file. On success, 0 is returned. On failure, */
827 /* -1 is returned. */
830 /*****************************************************************************/
832 int fits_write_header (FITS_FILE
*ff
, FITS_HDU_LIST
*hdulist
)
837 if (ff
->openmode
!= 'w')
838 FITS_RETURN ("fits_write_header: file not open for writing", -1);
842 if (hdulist
->used
.simple
)
844 FITS_WRITE_BOOLCARD (ff
->fp
, "SIMPLE", 1);
847 else if (hdulist
->used
.xtension
)
849 FITS_WRITE_STRINGCARD (ff
->fp
, "XTENSION", hdulist
->xtension
);
853 FITS_WRITE_LONGCARD (ff
->fp
, "BITPIX", hdulist
->bitpix
);
856 FITS_WRITE_LONGCARD (ff
->fp
, "NAXIS", hdulist
->naxis
);
859 for (r
= 0; r
< hdulist
->naxis
; r
++)
861 snprintf (naxisn
, sizeof( naxisn
), "NAXIS%d", r
+1);
862 FITS_WRITE_LONGCARD (ff
->fp
, naxisn
, hdulist
->naxisn
[r
]);
866 if (hdulist
->used
.extend
)
868 FITS_WRITE_BOOLCARD (ff
->fp
, "EXTEND", hdulist
->extend
);
872 if (hdulist
->used
.groups
)
874 FITS_WRITE_BOOLCARD (ff
->fp
, "GROUPS", hdulist
->groups
);
878 if (hdulist
->used
.pcount
)
880 FITS_WRITE_LONGCARD (ff
->fp
, "PCOUNT", hdulist
->pcount
);
883 if (hdulist
->used
.gcount
)
885 FITS_WRITE_LONGCARD (ff
->fp
, "GCOUNT", hdulist
->gcount
);
889 if (hdulist
->used
.bzero
)
891 FITS_WRITE_DOUBLECARD (ff
->fp
, "BZERO", hdulist
->bzero
);
894 if (hdulist
->used
.bscale
)
896 FITS_WRITE_DOUBLECARD (ff
->fp
, "BSCALE", hdulist
->bscale
);
900 if (hdulist
->used
.datamin
)
902 FITS_WRITE_DOUBLECARD (ff
->fp
, "DATAMIN", hdulist
->datamin
);
905 if (hdulist
->used
.datamax
)
907 FITS_WRITE_DOUBLECARD (ff
->fp
, "DATAMAX", hdulist
->datamax
);
911 if (hdulist
->used
.blank
)
913 FITS_WRITE_LONGCARD (ff
->fp
, "BLANK", hdulist
->blank
);
917 /* Write additional cards */
918 if (hdulist
->naddcards
> 0)
920 fwrite (hdulist
->addcards
, FITS_CARD_SIZE
, hdulist
->naddcards
, ff
->fp
);
921 numcards
+= hdulist
->naddcards
;
924 FITS_WRITE_CARD (ff
->fp
, "END");
927 r
= (numcards
*FITS_CARD_SIZE
) % FITS_RECORD_SIZE
;
928 if (r
) /* Must the record be filled up ? */
930 while (r
++ < FITS_RECORD_SIZE
)
934 return (ferror (ff
->fp
) ? -1 : 0);
938 /*****************************************************************************/
941 /* Function : fits_decode_header - (local) decode a header */
944 /* FITS_RECORD_LIST *hdr [I] : the header record list */
945 /* long hdr_offset [I] : fileposition of header */
946 /* long dat_offset [I] : fileposition of data (end of header) */
947 /* ( mode : I=input, O=output, I/O=input/output ) */
949 /* The function decodes the mostly used data within the header and generates */
950 /* a FITS_HDU_LIST-entry. On failure, a NULL-pointer is returned. */
953 /*****************************************************************************/
955 static FITS_HDU_LIST
*fits_decode_header (FITS_RECORD_LIST
*hdr
,
956 long hdr_offset
, long dat_offset
)
958 {FITS_HDU_LIST
*hdulist
;
960 char errmsg
[80], key
[9];
961 int k
, bpp
, random_groups
;
962 long mul_axis
, data_size
, bitpix_supported
;
964 #define FITS_DECODE_CARD(mhdr,mkey,mfdat,mtyp) \
965 {strcpy (key, mkey); \
966 mfdat = fits_decode_card (fits_search_card (mhdr, mkey), mtyp); \
967 if (mfdat == NULL) goto err_missing; }
969 #define FITS_TRY_CARD(mhdr,mhdu,mkey,mvar,mtyp,unionvar) \
970 {FITS_DATA *mfdat = fits_decode_card (fits_search_card (mhdr,mkey), mtyp); \
971 mhdu->used.mvar = (mfdat != NULL); \
972 if (mhdu->used.mvar) mhdu->mvar = mfdat->unionvar; }
974 hdulist
= fits_new_hdulist ();
976 FITS_RETURN ("fits_decode_header: Not enough memory", NULL
);
978 /* Initialize the header data */
979 hdulist
->header_offset
= hdr_offset
;
980 hdulist
->data_offset
= dat_offset
;
982 hdulist
->used
.simple
= (strncmp (hdr
->data
, "SIMPLE ", 8) == 0);
983 hdulist
->used
.xtension
= (strncmp (hdr
->data
, "XTENSION", 8) == 0);
984 if (hdulist
->used
.xtension
)
986 fdat
= fits_decode_card (fits_search_card (hdr
, "XTENSION"), typ_fstring
);
987 strcpy (hdulist
->xtension
, fdat
->fstring
);
990 FITS_DECODE_CARD (hdr
, "NAXIS", fdat
, typ_flong
);
991 hdulist
->naxis
= fdat
->flong
;
993 FITS_DECODE_CARD (hdr
, "BITPIX", fdat
, typ_flong
);
994 bpp
= hdulist
->bitpix
= (int)fdat
->flong
;
995 if ( (bpp
!= 8) && (bpp
!= 16) && (bpp
!= 32)
996 && (bpp
!= -32) && (bpp
!= -64))
998 strcpy (errmsg
, "fits_decode_header: Invalid BITPIX-value");
1001 if (bpp
< 0) bpp
= -bpp
;
1005 FITS_TRY_CARD (hdr
, hdulist
, "GCOUNT", gcount
, typ_flong
, flong
);
1006 FITS_TRY_CARD (hdr
, hdulist
, "PCOUNT", pcount
, typ_flong
, flong
);
1008 FITS_TRY_CARD (hdr
, hdulist
, "GROUPS", groups
, typ_fbool
, fbool
);
1009 random_groups
= hdulist
->used
.groups
&& hdulist
->groups
;
1011 FITS_TRY_CARD (hdr
, hdulist
, "EXTEND", extend
, typ_fbool
, fbool
);
1013 if (hdulist
->used
.xtension
) /* Extension requires GCOUNT and PCOUNT */
1015 if ((!hdulist
->used
.gcount
) || (!hdulist
->used
.pcount
))
1017 strcpy (errmsg
, "fits_decode_header: Missing GCOUNT/PCOUNT for XTENSION");
1024 /* Find all NAXISx-cards */
1025 for (k
= 1; k
<= FITS_MAX_AXIS
; k
++)
1028 snprintf (naxisn
, sizeof( naxisn
), "NAXIS%-3d", k
);
1029 fdat
= fits_decode_card (fits_search_card (hdr
, naxisn
), typ_flong
);
1032 k
--; /* Save the last NAXISk read */
1035 hdulist
->naxisn
[k
-1] = (int)fdat
->flong
;
1036 if (hdulist
->naxisn
[k
-1] < 0)
1038 strcpy (errmsg
, "fits_decode_header: Negative value in NAXISn");
1041 if ((k
== 1) && (random_groups
))
1043 if (hdulist
->naxisn
[0] != 0)
1045 strcpy (errmsg
, "fits_decode_header: Random groups with NAXIS1 != 0");
1050 mul_axis
*= hdulist
->naxisn
[k
-1];
1053 if ((hdulist
->naxis
> 0) && (k
< hdulist
->naxis
))
1055 strcpy (errmsg
, "fits_decode_card: Not enough NAXISn-cards");
1059 /* If we have only one dimension, just set the second to size one. */
1060 /* So we dont have to check for naxis < 2 in some places. */
1061 if (hdulist
->naxis
< 2)
1062 hdulist
->naxisn
[1] = 1;
1063 if (hdulist
->naxis
< 1)
1066 hdulist
->naxisn
[0] = 1;
1069 if (hdulist
->used
.xtension
)
1070 data_size
= bpp
*hdulist
->gcount
*(hdulist
->pcount
+ mul_axis
);
1072 data_size
= bpp
*mul_axis
;
1073 hdulist
->udata_size
= data_size
; /* Used data size without padding */
1075 /* Datasize must be a multiple of the FITS logical record size */
1076 data_size
= (data_size
+ FITS_RECORD_SIZE
- 1) / FITS_RECORD_SIZE
;
1077 data_size
*= FITS_RECORD_SIZE
;
1078 hdulist
->data_size
= data_size
;
1081 FITS_TRY_CARD (hdr
, hdulist
, "BLANK", blank
, typ_flong
, flong
);
1083 FITS_TRY_CARD (hdr
, hdulist
, "DATAMIN", datamin
, typ_fdouble
, fdouble
);
1084 FITS_TRY_CARD (hdr
, hdulist
, "DATAMAX", datamax
, typ_fdouble
, fdouble
);
1086 FITS_TRY_CARD (hdr
, hdulist
, "BZERO", bzero
, typ_fdouble
, fdouble
);
1087 FITS_TRY_CARD (hdr
, hdulist
, "BSCALE", bscale
, typ_fdouble
, fdouble
);
1089 /* Evaluate number of interpretable images for this HDU */
1090 hdulist
->numpic
= 0;
1092 /* We must support this format */
1093 bitpix_supported
= (hdulist
->bitpix
> 0)
1094 || ( (hdulist
->bitpix
== -64)
1095 && (fits_ieee64_intel
|| fits_ieee64_motorola
))
1096 || ( (hdulist
->bitpix
== -32)
1097 && ( fits_ieee32_intel
|| fits_ieee32_motorola
1098 || fits_ieee64_intel
|| fits_ieee64_motorola
));
1100 if (bitpix_supported
)
1102 if (hdulist
->used
.simple
)
1104 if (hdulist
->naxis
> 0)
1106 hdulist
->numpic
= 1;
1107 for (k
= 3; k
<= hdulist
->naxis
; k
++)
1108 hdulist
->numpic
*= hdulist
->naxisn
[k
-1];
1111 else if ( hdulist
->used
.xtension
1112 && (strncmp (hdulist
->xtension
, "IMAGE", 5) == 0))
1114 if (hdulist
->naxis
> 0)
1116 hdulist
->numpic
= 1;
1117 for (k
= 3; k
<= hdulist
->naxis
; k
++)
1118 hdulist
->numpic
*= hdulist
->naxisn
[k
-1];
1124 snprintf (msg
, sizeof( msg
), "fits_decode_header: IEEE floating point format required for\
1125 BITPIX=%d\nis not supported on this machine", hdulist
->bitpix
);
1126 fits_set_error (msg
);
1129 hdulist
->header_record_list
= hdr
; /* Add header records to the list */
1133 snprintf (errmsg
, sizeof(errmsg
), "fits_decode_header: missing/invalid %.50s card", key
);
1136 fits_delete_hdulist (hdulist
);
1137 fits_set_error (errmsg
);
1140 #undef FITS_DECODE_CARD
1144 /*****************************************************************************/
1147 /* Function : fits_eval_pixrange - (local) evaluate range of pixel data */
1150 /* FILE *fp [I] : file pointer */
1151 /* FITS_HDU_LIST *hdu [I] : pointer to header */
1152 /* ( mode : I=input, O=output, I/O=input/output ) */
1154 /* The Function sets the values hdu->pixmin and hdu->pixmax. On success 0 */
1155 /* is returned. On failure, -1 is returned. */
1158 /*****************************************************************************/
1160 static int fits_eval_pixrange (FILE *fp
, FITS_HDU_LIST
*hdu
)
1162 {register unsigned int maxelem
;
1163 #define FITSNPIX 4096
1164 unsigned char pixdat
[FITSNPIX
];
1165 unsigned int nelem
, bpp
;
1166 int blank_found
= 0, nan_found
= 0;
1168 if (fseek (fp
, hdu
->data_offset
, SEEK_SET
) < 0)
1169 FITS_RETURN ("fits_eval_pixrange: cant position file", -1);
1171 bpp
= hdu
->bpp
; /* Number of bytes per pixel */
1172 nelem
= hdu
->udata_size
/ bpp
; /* Number of data elements */
1174 switch (hdu
->bitpix
)
1177 register FITS_BITPIX8 pixval
;
1178 register unsigned char *ptr
;
1179 FITS_BITPIX8 minval
= 255, maxval
= 0;
1180 FITS_BITPIX8 blankval
;
1184 maxelem
= sizeof (pixdat
)/bpp
;
1185 if (nelem
< maxelem
) maxelem
= nelem
;
1187 if (fread ((char *)pixdat
, bpp
, maxelem
, fp
) != maxelem
)
1188 FITS_RETURN ("fits_eval_pixrange: error on read bitpix 8 data", -1);
1191 if (hdu
->used
.blank
)
1193 blankval
= (FITS_BITPIX8
)hdu
->blank
;
1194 while (maxelem
-- > 0)
1196 pixval
= (FITS_BITPIX8
)*(ptr
++);
1197 if (pixval
!= blankval
)
1199 if (pixval
< minval
) minval
= pixval
;
1200 else if (pixval
> maxval
) maxval
= pixval
;
1202 else blank_found
= 1;
1207 while (maxelem
-- > 0)
1209 pixval
= (FITS_BITPIX8
)*(ptr
++);
1210 if (pixval
< minval
) minval
= pixval
;
1211 else if (pixval
> maxval
) maxval
= pixval
;
1215 hdu
->pixmin
= minval
;
1216 hdu
->pixmax
= maxval
;
1220 register FITS_BITPIX16 pixval
;
1221 register unsigned char *ptr
;
1222 FITS_BITPIX16 minval
= 0x7fff, maxval
= ~0x7fff;
1226 maxelem
= sizeof (pixdat
)/bpp
;
1227 if (nelem
< maxelem
) maxelem
= nelem
;
1229 if (fread ((char *)pixdat
, bpp
, maxelem
, fp
) != maxelem
)
1230 FITS_RETURN ("fits_eval_pixrange: error on read bitpix 16 data", -1);
1233 if (hdu
->used
.blank
)
1234 {FITS_BITPIX16 blankval
= (FITS_BITPIX16
)hdu
->blank
;
1236 while (maxelem
-- > 0)
1238 FITS_GETBITPIX16 (ptr
, pixval
);
1240 if (pixval
!= blankval
)
1242 if (pixval
< minval
) minval
= pixval
;
1243 else if (pixval
> maxval
) maxval
= pixval
;
1245 else blank_found
= 1;
1250 while (maxelem
-- > 0)
1252 FITS_GETBITPIX16 (ptr
, pixval
);
1254 if (pixval
< minval
) minval
= pixval
;
1255 else if (pixval
> maxval
) maxval
= pixval
;
1259 hdu
->pixmin
= minval
;
1260 hdu
->pixmax
= maxval
;
1265 register FITS_BITPIX32 pixval
;
1266 register unsigned char *ptr
;
1267 FITS_BITPIX32 minval
= 0x7fffffff, maxval
= ~0x7fffffff;
1271 maxelem
= sizeof (pixdat
)/bpp
;
1272 if (nelem
< maxelem
) maxelem
= nelem
;
1274 if (fread ((char *)pixdat
, bpp
, maxelem
, fp
) != maxelem
)
1275 FITS_RETURN ("fits_eval_pixrange: error on read bitpix 32 data", -1);
1278 if (hdu
->used
.blank
)
1279 {FITS_BITPIX32 blankval
= (FITS_BITPIX32
)hdu
->blank
;
1281 while (maxelem
-- > 0)
1283 FITS_GETBITPIX32 (ptr
, pixval
);
1285 if (pixval
!= blankval
)
1287 if (pixval
< minval
) minval
= pixval
;
1288 else if (pixval
> maxval
) maxval
= pixval
;
1290 else blank_found
= 1;
1295 while (maxelem
-- > 0)
1297 FITS_GETBITPIX32 (ptr
, pixval
);
1299 if (pixval
< minval
) minval
= pixval
;
1300 else if (pixval
> maxval
) maxval
= pixval
;
1304 hdu
->pixmin
= minval
;
1305 hdu
->pixmax
= maxval
;
1309 register FITS_BITPIXM32 pixval
;
1310 register unsigned char *ptr
;
1311 FITS_BITPIXM32 minval
, maxval
;
1322 maxelem
= sizeof (pixdat
)/bpp
;
1323 if (nelem
< maxelem
) maxelem
= nelem
;
1325 if (fread ((char *)pixdat
, bpp
, maxelem
, fp
) != maxelem
)
1326 FITS_RETURN ("fits_eval_pixrange: error on read bitpix -32 data", -1);
1329 while (maxelem
-- > 0)
1331 if (!fits_nan_32 (ptr
))
1333 FITS_GETBITPIXM32 (ptr
, pixval
);
1338 minval
= maxval
= pixval
;
1340 else if (pixval
< minval
) { minval
= pixval
; }
1341 else if (pixval
> maxval
) { maxval
= pixval
; }
1346 hdu
->pixmin
= minval
;
1347 hdu
->pixmax
= maxval
;
1351 register FITS_BITPIXM64 pixval
;
1352 register unsigned char *ptr
;
1353 FITS_BITPIXM64 minval
, maxval
;
1363 maxelem
= sizeof (pixdat
)/bpp
;
1364 if (nelem
< maxelem
) maxelem
= nelem
;
1366 if (fread ((char *)pixdat
, bpp
, maxelem
, fp
) != maxelem
)
1367 FITS_RETURN ("fits_eval_pixrange: error on read bitpix -64 data", -1);
1370 while (maxelem
-- > 0)
1372 if (!fits_nan_64 (ptr
))
1374 FITS_GETBITPIXM64 (ptr
, pixval
);
1379 minval
= maxval
= pixval
;
1381 else if (pixval
< minval
) { minval
= pixval
; }
1382 else if (pixval
> maxval
) { maxval
= pixval
; }
1387 hdu
->pixmin
= minval
;
1388 hdu
->pixmax
= maxval
;
1391 if (nan_found
) hdu
->used
.nan_value
= 1;
1392 if (blank_found
) hdu
->used
.blank_value
= 1;
1398 /*****************************************************************************/
1401 /* Function : fits_decode_card - decode a card */
1404 /* const char *card [I] : pointer to card image */
1405 /* FITS_DATA_TYPES data_type [I] : datatype to decode */
1406 /* ( mode : I=input, O=output, I/O=input/output ) */
1408 /* Decodes a card and returns a pointer to the union, keeping the data. */
1409 /* If card is NULL or on failure, a NULL-pointer is returned. */
1410 /* If the card does not have the value indicator, an error is generated, */
1411 /* but its tried to decode the card. The data is only valid up to the next */
1412 /* call of the function. */
1415 /*****************************************************************************/
1417 FITS_DATA
*fits_decode_card (const char *card
, FITS_DATA_TYPES data_type
)
1419 {static FITS_DATA data
;
1422 char l_card
[FITS_CARD_SIZE
+1], msg
[256];
1423 char *cp
= ident
, *dst
, *end
;
1425 if (card
== NULL
) return (NULL
);
1427 memcpy (l_card
, card
, FITS_CARD_SIZE
);
1428 l_card
[FITS_CARD_SIZE
] = '\0';
1430 if (strncmp (card
+8, "= ", 2) != 0)
1432 snprintf (msg
, sizeof( msg
), "fits_decode_card (warning): Missing value indicator\
1433 '= ' for %8.8s", l_card
);
1434 fits_set_error (msg
);
1440 data
.bitpix8
= (FITS_BITPIX8
)(l_card
[10]);
1444 if (sscanf (l_card
+10, "%ld", &l_long
) != 1)
1445 FITS_RETURN ("fits_decode_card: error decoding typ_bitpix16", NULL
);
1446 data
.bitpix16
= (FITS_BITPIX16
)l_long
;
1450 if (sscanf (l_card
+10, "%ld", &l_long
) != 1)
1451 FITS_RETURN ("fits_decode_card: error decoding typ_bitpix32", NULL
);
1452 data
.bitpix32
= (FITS_BITPIX32
)l_long
;
1456 if (sscanf (l_card
+10, "%lf", &l_double
) != 1)
1457 FITS_RETURN ("fits_decode_card: error decoding typ_bitpixm32", NULL
);
1458 data
.bitpixm32
= (FITS_BITPIXM32
)l_double
;
1462 if (sscanf (l_card
+10, "%lf", &l_double
) != 1)
1463 FITS_RETURN ("fits_decode_card: error decoding typ_bitpixm64", NULL
);
1464 data
.bitpixm64
= (FITS_BITPIXM64
)l_double
;
1469 while (*cp
== ' ') cp
++;
1470 if (*cp
== 'T') data
.fbool
= 1;
1471 else if (*cp
== 'F') data
.fbool
= 0;
1472 else FITS_RETURN ("fits_decode_card: error decoding typ_fbool", NULL
);
1476 if (sscanf (l_card
+10, "%ld", &l_long
) != 1)
1477 FITS_RETURN ("fits_decode_card: error decoding typ_flong", NULL
);
1478 data
.flong
= (FITS_BITPIX32
)l_long
;
1482 if (sscanf (l_card
+10, "%lf", &l_double
) != 1)
1483 FITS_RETURN ("fits_decode_card: error decoding typ_fdouble", NULL
);
1484 data
.fdouble
= (FITS_BITPIXM32
)l_double
;
1490 FITS_RETURN ("fits_decode_card: missing \' decoding typ_fstring", NULL
);
1494 end
= l_card
+FITS_CARD_SIZE
-1;
1495 for (;;) /* Search for trailing quote */
1497 if (*cp
!= '\'') /* All characters but quote are used. */
1501 else /* Maybe there is a quote in the string */
1503 if (cp
>= end
) break; /* End of card ? finished */
1504 if (*(cp
+1) != '\'') break;
1507 if (cp
>= end
) break;
1517 /*****************************************************************************/
1520 /* Function : fits_search_card - search a card in the record list */
1523 /* FITS_RECORD_LIST *rl [I] : record list to search */
1524 /* char *keyword [I] : keyword identifying the card */
1525 /* ( mode : I=input, O=output, I/O=input/output ) */
1527 /* A card is searched in the reord list. Only the first eight characters of */
1528 /* keyword are significant. If keyword is less than 8 characters, its filled */
1530 /* If the card is found, a pointer to the card is returned. */
1531 /* The pointer does not point to a null-terminated string. Only the next */
1532 /* 80 bytes are allowed to be read. */
1533 /* On failure a NULL-pointer is returned. */
1536 /*****************************************************************************/
1538 char *fits_search_card (FITS_RECORD_LIST
*rl
, const char *keyword
)
1544 key_len
= strlen (keyword
);
1545 if (key_len
> 8) key_len
= 8;
1547 FITS_RETURN ("fits_search_card: Invalid parameter", NULL
);
1550 memcpy (key
, keyword
, key_len
);
1554 card
= (char *)rl
->data
;
1555 for (k
= 0; k
< FITS_RECORD_SIZE
/ FITS_CARD_SIZE
; k
++)
1557 if (strncmp (card
, key
, 8) == 0) return (card
);
1558 card
+= FITS_CARD_SIZE
;
1560 rl
= rl
->next_record
;
1566 /*****************************************************************************/
1569 /* Function : fits_image_info - get information about an image */
1572 /* FITS_FILE *ff [I] : FITS file structure */
1573 /* int picind [I] : Index of picture in file (1,2,...) */
1574 /* int *hdupicind [O] : Index of picture in HDU (1,2,...) */
1575 /* ( mode : I=input, O=output, I/O=input/output ) */
1577 /* The function returns on success a pointer to a FITS_HDU_LIST. hdupicind */
1578 /* then gives the index of the image within the HDU. */
1579 /* On failure, NULL is returned. */
1582 /*****************************************************************************/
1584 FITS_HDU_LIST
*fits_image_info (FITS_FILE
*ff
, int picind
, int *hdupicind
)
1586 {FITS_HDU_LIST
*hdulist
;
1587 int firstpic
, lastpic
;
1590 FITS_RETURN ("fits_image_info: ff is NULL", NULL
);
1592 if (ff
->openmode
!= 'r')
1593 FITS_RETURN ("fits_image_info: file not open for reading", NULL
);
1595 if ((picind
< 1) || (picind
> ff
->n_pic
))
1596 FITS_RETURN ("fits_image_info: picind out of range", NULL
);
1599 for (hdulist
= ff
->hdu_list
; hdulist
!= NULL
; hdulist
= hdulist
->next_hdu
)
1601 if (hdulist
->numpic
<= 0) continue;
1602 lastpic
= firstpic
+hdulist
->numpic
-1;
1603 if (picind
<= lastpic
) /* Found image in current HDU ? */
1606 firstpic
= lastpic
+1;
1608 *hdupicind
= picind
- firstpic
+ 1;
1613 /*****************************************************************************/
1616 /* Function : fits_seek_image - position to a specific image */
1619 /* FITS_FILE *ff [I] : FITS file structure */
1620 /* int picind [I] : Index of picture to seek (1,2,...) */
1621 /* ( mode : I=input, O=output, I/O=input/output ) */
1623 /* The function positions the file pointer to a specified image. */
1624 /* The function returns on success a pointer to a FITS_HDU_LIST. This pointer*/
1625 /* must also be used when reading data from the image. */
1626 /* On failure, NULL is returned. */
1629 /*****************************************************************************/
1631 FITS_HDU_LIST
*fits_seek_image (FITS_FILE
*ff
, int picind
)
1633 {FITS_HDU_LIST
*hdulist
;
1635 long offset
, pic_size
;
1637 hdulist
= fits_image_info (ff
, picind
, &hdupicind
);
1638 if (hdulist
== NULL
) return (NULL
);
1640 pic_size
= hdulist
->bpp
* hdulist
->naxisn
[0] * hdulist
->naxisn
[1];
1641 offset
= hdulist
->data_offset
+ (hdupicind
-1)*pic_size
;
1642 if (fseek (ff
->fp
, offset
, SEEK_SET
) < 0)
1643 FITS_RETURN ("fits_seek_image: Unable to position to image", NULL
);
1649 /*****************************************************************************/
1652 /* Function : fits_read_pixel - read pixel values from a file */
1655 /* FITS_FILE *ff [I] : FITS file structure */
1656 /* FITS_HDU_LIST *hdulist [I] : pointer to hdulist that describes image */
1657 /* int npix [I] : number of pixel values to read */
1658 /* FITS_PIX_TRANSFORM *trans [I]: pixel transformation */
1659 /* void *buf [O] : buffer where to place transformed pixels */
1660 /* ( mode : I=input, O=output, I/O=input/output ) */
1662 /* The function reads npix pixel values from the file, transforms them */
1663 /* checking for blank/NaN pixels and stores the transformed values in buf. */
1664 /* The number of transformed pixels is returned. If the returned value is */
1665 /* less than npix (or even -1), an error has occured. */
1666 /* hdulist must be a pointer returned by fits_seek_image(). Before starting */
1667 /* to read an image, fits_seek_image() must be called. Even for successive */
1671 /*****************************************************************************/
1673 int fits_read_pixel (FITS_FILE
*ff
, FITS_HDU_LIST
*hdulist
, int npix
,
1674 FITS_PIX_TRANSFORM
*trans
, void *buf
)
1676 {double offs
, scale
;
1677 double datadiff
, pixdiff
;
1678 unsigned char pixbuffer
[4096], *pix
, *cdata
;
1679 unsigned char creplace
;
1681 long tdata
, tmin
, tmax
;
1683 FITS_BITPIX8 bp8
, bp8blank
;
1684 FITS_BITPIX16 bp16
, bp16blank
;
1685 FITS_BITPIX32 bp32
, bp32blank
;
1686 FITS_BITPIXM32 bpm32
;
1687 FITS_BITPIXM64 bpm64
;
1693 if (ff
->openmode
!= 'r') return (-1); /* Not open for reading */
1694 if (trans
->dsttyp
!= 'c') return (-1); /* Currently we only return chars */
1695 if (npix
<= 0) return (npix
);
1697 datadiff
= trans
->datamax
- trans
->datamin
;
1698 pixdiff
= trans
->pixmax
- trans
->pixmin
;
1700 offs
= trans
->datamin
- trans
->pixmin
*datadiff
/pixdiff
;
1701 scale
= datadiff
/ pixdiff
;
1703 tmin
= (long)trans
->datamin
;
1704 tmax
= (long)trans
->datamax
;
1705 if (tmin
< 0) tmin
= 0; else if (tmin
> 255) tmin
= 255;
1706 if (tmax
< 0) tmax
= 0; else if (tmax
> 255) tmax
= 255;
1708 cdata
= (unsigned char *)buf
;
1709 creplace
= (unsigned char)trans
->replacement
;
1711 switch (hdulist
->bitpix
)
1714 while (npix
> 0) /* For all pixels to read */
1716 maxelem
= sizeof (pixbuffer
) / hdulist
->bpp
;
1717 if (maxelem
> npix
) maxelem
= npix
;
1718 if (fread ((char *)pixbuffer
, hdulist
->bpp
, maxelem
, ff
->fp
) != (unsigned) maxelem
)
1723 if (hdulist
->used
.blank
)
1725 bp8blank
= (FITS_BITPIX8
)hdulist
->blank
;
1728 bp8
= (FITS_BITPIX8
)*(pix
++);
1729 if (bp8
== bp8blank
) /* Is it a blank pixel ? */
1730 *(cdata
++) = creplace
;
1731 else /* Do transform */
1733 tdata
= (long)(bp8
* scale
+ offs
);
1734 if (tdata
< tmin
) tdata
= tmin
;
1735 else if (tdata
> tmax
) tdata
= tmax
;
1736 *(cdata
++) = (unsigned char)tdata
;
1745 bp8
= (FITS_BITPIX8
)*(pix
++);
1746 tdata
= (long)(bp8
* scale
+ offs
);
1747 if (tdata
< tmin
) tdata
= tmin
;
1748 else if (tdata
> tmax
) tdata
= tmax
;
1749 *(cdata
++) = (unsigned char)tdata
;
1757 while (npix
> 0) /* For all pixels to read */
1759 maxelem
= sizeof (pixbuffer
) / hdulist
->bpp
;
1760 if (maxelem
> npix
) maxelem
= npix
;
1761 if (fread ((char *)pixbuffer
, hdulist
->bpp
, maxelem
, ff
->fp
) != (unsigned) maxelem
)
1766 if (hdulist
->used
.blank
)
1768 bp16blank
= (FITS_BITPIX16
)hdulist
->blank
;
1771 FITS_GETBITPIX16 (pix
, bp16
);
1772 if (bp16
== bp16blank
)
1773 *(cdata
++) = creplace
;
1776 tdata
= (long)(bp16
* scale
+ offs
);
1777 if (tdata
< tmin
) tdata
= tmin
;
1778 else if (tdata
> tmax
) tdata
= tmax
;
1779 *(cdata
++) = (unsigned char)tdata
;
1789 FITS_GETBITPIX16 (pix
, bp16
);
1790 tdata
= (long)(bp16
* scale
+ offs
);
1791 if (tdata
< tmin
) tdata
= tmin
;
1792 else if (tdata
> tmax
) tdata
= tmax
;
1793 *(cdata
++) = (unsigned char)tdata
;
1802 while (npix
> 0) /* For all pixels to read */
1804 maxelem
= sizeof (pixbuffer
) / hdulist
->bpp
;
1805 if (maxelem
> npix
) maxelem
= npix
;
1806 if (fread ((char *)pixbuffer
, hdulist
->bpp
, maxelem
, ff
->fp
) != (unsigned) maxelem
)
1811 if (hdulist
->used
.blank
)
1813 bp32blank
= (FITS_BITPIX32
)hdulist
->blank
;
1816 FITS_GETBITPIX32 (pix
, bp32
);
1817 if (bp32
== bp32blank
)
1818 *(cdata
++) = creplace
;
1821 tdata
= (long)(bp32
* scale
+ offs
);
1822 if (tdata
< tmin
) tdata
= tmin
;
1823 else if (tdata
> tmax
) tdata
= tmax
;
1824 *(cdata
++) = (unsigned char)tdata
;
1834 FITS_GETBITPIX32 (pix
, bp32
);
1835 tdata
= (long)(bp32
* scale
+ offs
);
1836 if (tdata
< tmin
) tdata
= tmin
;
1837 else if (tdata
> tmax
) tdata
= tmax
;
1838 *(cdata
++) = (unsigned char)tdata
;
1847 while (npix
> 0) /* For all pixels to read */
1849 maxelem
= sizeof (pixbuffer
) / hdulist
->bpp
;
1850 if (maxelem
> npix
) maxelem
= npix
;
1851 if (fread ((char *)pixbuffer
, hdulist
->bpp
, maxelem
, ff
->fp
) != (unsigned) maxelem
)
1858 if (fits_nan_32 (pix
)) /* An IEEE special value ? */
1859 *(cdata
++) = creplace
;
1860 else /* Do transform */
1862 FITS_GETBITPIXM32 (pix
, bpm32
);
1863 tdata
= (long)(bpm32
* scale
+ offs
);
1864 if (tdata
< tmin
) tdata
= tmin
;
1865 else if (tdata
> tmax
) tdata
= tmax
;
1866 *(cdata
++) = (unsigned char)tdata
;
1875 while (npix
> 0) /* For all pixels to read */
1877 maxelem
= sizeof (pixbuffer
) / hdulist
->bpp
;
1878 if (maxelem
> npix
) maxelem
= npix
;
1879 if (fread ((char *)pixbuffer
, hdulist
->bpp
, maxelem
, ff
->fp
) != (unsigned) maxelem
)
1886 if (fits_nan_64 (pix
))
1887 *(cdata
++) = creplace
;
1890 FITS_GETBITPIXM64 (pix
, bpm64
);
1891 tdata
= (long)(bpm64
* scale
+ offs
);
1892 if (tdata
< tmin
) tdata
= tmin
;
1893 else if (tdata
> tmax
) tdata
= tmax
;
1894 *(cdata
++) = (unsigned char)tdata
;
1902 return (transcount
);
1907 #ifndef FITS_NO_DEMO
1908 /*****************************************************************************/
1911 /* Function : fits_to_pgmraw - convert FITS-file to raw PGM-file */
1914 /* char *fitsfile [I] : name of fitsfile */
1915 /* char *pgmfile [I] : name of pgmfile */
1916 /* ( mode : I=input, O=output, I/O=input/output ) */
1918 /* The function converts a FITS-file to a raw PGM-file. The PGM-file will */
1919 /* be upside down, because the orientation for storing the image is */
1920 /* different. On success, 0 is returned. On failure, -1 is returned. */
1923 /*****************************************************************************/
1925 int fits_to_pgmraw (char *fitsfile
, char *pgmfile
)
1927 {FITS_FILE
*fitsin
= NULL
;
1928 FILE *pgmout
= NULL
;
1930 FITS_PIX_TRANSFORM trans
;
1931 int retval
= -1, nbytes
, maxbytes
;
1934 fitsin
= fits_open (fitsfile
, "r"); /* Open FITS-file for reading */
1935 if (fitsin
== NULL
) goto err_return
;
1937 if (fitsin
->n_pic
< 1) goto err_return
; /* Any picture in it ? */
1939 hdu
= fits_seek_image (fitsin
, 1); /* Position to the first image */
1940 if (hdu
== NULL
) goto err_return
;
1941 if (hdu
->naxis
< 2) goto err_return
; /* Enough dimensions ? */
1943 pgmout
= fopen (pgmfile
, "wb");
1944 if (pgmout
== NULL
) goto err_return
;
1946 /* Write PGM header with width/height */
1947 fprintf (pgmout
, "P5\n%d %d\n255\n", hdu
->naxisn
[0], hdu
->naxisn
[1]);
1949 /* Set up transformation for FITS pixel values to 0...255 */
1950 /* It maps trans.pixmin to trans.datamin and trans.pixmax to trans.datamax. */
1951 /* Values out of range [datamin, datamax] are clamped */
1952 trans
.pixmin
= hdu
->pixmin
;
1953 trans
.pixmax
= hdu
->pixmax
;
1954 trans
.datamin
= 0.0;
1955 trans
.datamax
= 255.0;
1956 trans
.replacement
= 0.0; /* Blank/NaN replacement value */
1957 trans
.dsttyp
= 'c'; /* Output type is character */
1959 nbytes
= hdu
->naxisn
[0]*hdu
->naxisn
[1];
1962 maxbytes
= sizeof (buffer
);
1963 if (maxbytes
> nbytes
) maxbytes
= nbytes
;
1965 /* Read pixels and transform them */
1966 if (fits_read_pixel (fitsin
, hdu
, maxbytes
, &trans
, buffer
) != maxbytes
)
1969 if (fwrite (buffer
, 1, maxbytes
, pgmout
) != maxbytes
)
1978 if (fitsin
) fits_close (fitsin
);
1979 if (pgmout
) fclose (pgmout
);
1985 /*****************************************************************************/
1988 /* Function : pgmraw to fits - convert raw PGM-file to FITS-file */
1991 /* char *pgmfile [I] : name of pgmfile */
1992 /* char *fitsfile [I] : name of fitsfile */
1993 /* ( mode : I=input, O=output, I/O=input/output ) */
1995 /* The function converts a raw PGM-file to a FITS-file. The FITS-file will */
1996 /* be upside down, because the orientation for storing the image is */
1997 /* different. On success, 0 is returned. On failure, -1 is returned. */
2000 /*****************************************************************************/
2002 int pgmraw_to_fits (char *pgmfile
, char *fitsfile
)
2004 {FITS_FILE
*fitsout
= NULL
;
2008 int width
, height
, numbytes
, maxbytes
;
2011 fitsout
= fits_open (fitsfile
, "w");
2012 if (fitsout
== NULL
) goto err_return
;
2014 pgmin
= fopen (pgmfile
, "r");
2015 if (pgmin
== NULL
) goto err_return
;
2017 /* Read signature of PGM file */
2018 if (fgets (buffer
, sizeof (buffer
), pgmin
) == NULL
) goto err_return
;
2019 if ((buffer
[0] != 'P') || (buffer
[1] != '5')) goto err_return
;
2021 /* Skip comments upto width/height */
2024 if (fgets (buffer
, sizeof (buffer
), pgmin
) == NULL
) goto err_return
;
2025 } while (buffer
[0] == '#');
2027 if (sscanf (buffer
, "%d%d", &width
, &height
) != 2) goto err_return
;
2028 if ((width
< 1) || (height
< 1)) goto err_return
;
2030 /* Skip comments upto maxval */
2033 if (fgets (buffer
, sizeof (buffer
), pgmin
) == NULL
) goto err_return
;
2034 } while (buffer
[0] == '#');
2037 hdu
= fits_add_hdu (fitsout
); /* Create a HDU for the FITS file */
2038 if (hdu
== NULL
) goto err_return
;
2040 hdu
->used
.simple
= 1; /* Set proper values */
2043 hdu
->naxisn
[0] = width
;
2044 hdu
->naxisn
[1] = height
;
2045 hdu
->used
.datamin
= 1;
2047 hdu
->used
.datamax
= 1;
2048 hdu
->datamax
= 255.0;
2049 hdu
->used
.bzero
= 1;
2051 hdu
->used
.bscale
= 1;
2054 fits_add_card (hdu
, "");
2055 fits_add_card (hdu
, "HISTORY THIS FITS FILE WAS GENERATED BY FITSRW\
2056 USING PGMRAW_TO_FITS");
2058 /* Write the header. Blocking is done automatically */
2059 if (fits_write_header (fitsout
, hdu
) < 0) goto err_return
;
2061 /* The primary array plus blocking must be written manually */
2062 numbytes
= width
* height
;
2064 while (numbytes
> 0)
2066 maxbytes
= sizeof (buffer
);
2067 if (maxbytes
> numbytes
) maxbytes
= numbytes
;
2069 if (fread (buffer
, 1, maxbytes
, pgmin
) != maxbytes
) goto err_return
;
2070 if (fwrite (buffer
, 1, maxbytes
, fitsout
->fp
) != maxbytes
) goto err_return
;
2072 numbytes
-= maxbytes
;
2076 numbytes
= (width
* height
) % FITS_RECORD_SIZE
;
2079 while (numbytes
++ < FITS_RECORD_SIZE
)
2080 if (putc (0, fitsout
->fp
) == EOF
) goto err_return
;
2086 if (fitsout
) fits_close (fitsout
);
2087 if (pgmin
) fclose (pgmin
);