CFITSIO

Guidance last updated: 18 October 2026

CFITSIO is a library of C and Fortran subroutines for reading and writing data files in FITS (Flexible Image Transport System) data format. CFITSIO provides simple high-level routines for reading and writing FITS files that insulate the programmer from the internal complexities of the FITS format.

Usage

After connecting to Stanage (see Establishing a SSH connection), start an interactive session:

salloc

You can load a specific version of CFITSIO with the following command:

module load CFITSIO/4.6.2-GCCcore-14.3.0
module load CFITSIO/4.2.0-GCCcore-12.2.0

C example

Within an interactive session, load CFITSIO/4.6.2-GCCcore-14.3.0. Create a test.c file containing the following (source):

#include <string.h>
#include <stdio.h>
#include "fitsio.h"
int main(int argc, char *argv[])
{
        fitsfile *fptr;
        char card[FLEN_CARD];
        int status = 0, nkeys = 0, ii;  /* MUST initialize status */
        if (argc != 2) {
            fprintf(stderr, "Usage: %s file.fits\n", argv[0]);
            return 1;
        }
        fits_open_file(&fptr, argv[1], READONLY, &status);
        if (status) {
            fits_report_error(stderr, status);
            return status;
        }
        fits_get_hdrspace(fptr, &nkeys, NULL, &status);
        for (ii = 1; ii <= nkeys; ii++)  {
          fits_read_record(fptr, ii, card, &status); /* read keyword */
          printf("%s\n", card);
        }
        printf("END\n\n");  /* terminate listing with END */
        fits_close_file(fptr, &status);
        if (status)          /* print any error messages */
            fits_report_error(stderr, status);
        return(status);
}

Download the example FITS file to the same directory, then compile and run:

gcc test.c -o test -lcfitsio
./test IUElwp25637mxlo.fits > header.txt
head header.txt

The output should look like:

SIMPLE  =                    T / Standard FITS Format
BITPIX  =                    8 / 8 bits ASCII
NAXIS   =                    0 / No image data
EXTEND  =                    T / Extensions are present
TELESCOP= 'IUE     '           / International Ultraviolet Explorer
DATE    = '29/12/95'           / Date file was written
ORIGIN  = 'GSFC    '           / Institution generating the file
COMMENT *
COMMENT * CORE DATA ITEMS - COMMON SET
COMMENT *

Threaded example

For this example, request one task with two CPUs in an interactive session (salloc --ntasks=1 --cpus-per-task=2), then load CFITSIO/4.6.2-GCCcore-14.3.0. Download and unpack NASA’s threaded examples archive. From the unpacked threadtest directory, compile the parallel image reader and the OpenMP thread-count example:

gcc fits_threads.c -fopenmp -lcfitsio -o fits_threads
gcc threadhello.c -fopenmp -o threadhello
export OMP_NUM_THREADS=2
./threadhello
./fits_threads

threadhello should report two threads; their output order can vary. fits_threads reads test1.fits and test2.fits in parallel and prints 100 pixel values from each. The first rows begin:

7  7  7  6  6  6  6  6  5  4
...
22  22  22  21  21  21  21  21  20  19
...

Each thread must use its own independently opened FITS file handle, as in this example. See the CFITSIO threading requirements.

Installation notes

CFITSIO was installed using Easybuild, build details can be found in $EBROOTCFITSIO/easybuild with a given module loaded.

CFITSIO is built with --enable-reentrant to support concurrent calls from multiple threads.

Direct dependencies for latest version of CFITSIO