LALPulsar 7.1.1.1-eeff03c
fits_overview.c
Go to the documentation of this file.
1//
2// From https://heasarc.gsfc.nasa.gov/docs/software/fitsio/cexamples.html:
3//
4// FITS Tools: Handy FITS Utilities that illustrate how to use CFITSIO
5// -------------------------------------------------------------------
6//
7// These are working programs written in ANSI C that illustrate how one can
8// easily read, write, and modify FITS files using the CFITSIO library. Most of
9// these programs are very short, containing only a few 10s of lines of
10// executable code or less, yet they perform quite useful operations on FITS
11// files. Copy the programs to your local machine, then compile, and link them
12// with the CFITSIO library. A short description of how to use each program can
13// be displayed by executing the program without any command line arguments.
14//
15// You may freely modify, reuse, and redistribute these programs as you wish. It
16// is often easier to use one of these programs as a template when writing a new
17// program, rather than coding the new program completely from scratch.
18//
19
20/**
21 * \file
22 * \ingroup lalpulsar_bin_FITSTools
23 */
24
25#include "config.h"
26
27#include <string.h>
28#include <stdio.h>
29
30#if defined(HAVE_LIBCFITSIO)
31// disable -Wstrict-prototypes flag for this header file as this causes
32// a build failure for cfitsio-3.440+
33#pragma GCC diagnostic ignored "-Wstrict-prototypes"
34#include <fitsio.h>
35#pragma GCC diagnostic pop
36#else
37#error CFITSIO library is not available
38#endif
39
40int main( int argc, char *argv[] )
41{
42 fitsfile *fptr = 0; /* FITS file pointer, defined in fitsio.h */
43 char keyname[FLEN_KEYWORD], colname[FLEN_VALUE], coltype[FLEN_VALUE];
44 int status = 0; /* CFITSIO status value MUST be initialized to zero! */
45 int single = 0, hdupos = 0, hdutype = 0, bitpix = 0, naxis = 0, ncols = 0, ii = 0;
46 long naxes[10], nrows = 0;
47
48 int printhelp = ( argc == 2 && ( strcmp( argv[1], "-h" ) == 0 || strcmp( argv[1], "--help" ) == 0 ) );
49
50 if ( printhelp || argc != 2 ) {
51 fprintf( stderr, "Usage: %s filename[ext] \n", argv[0] );
52 fprintf( stderr, "\n" );
53 fprintf( stderr, "List the structure of a single extension, or, if ext is \n" );
54 fprintf( stderr, "not given, list the structure of the entire FITS file. \n" );
55 fprintf( stderr, "\n" );
56 fprintf( stderr, "Note that it may be necessary to enclose the input file\n" );
57 fprintf( stderr, "name in single quote characters on the Unix command line.\n" );
58 return ( 0 );
59 }
60
61#if defined(PAGER) && defined(HAVE_POPEN) && defined(HAVE_PCLOSE)
62 FILE *fout = popen( PAGER, "w" );
63 if ( fout == NULL ) {
64 fprintf( stderr, "Could not execute '%s'\n", PAGER );
65 return ( 1 );
66 }
67#else
68 FILE *fout = stdout;
69#endif
70
71 if ( !fits_open_file( &fptr, argv[1], READONLY, &status ) ) {
72 fits_get_hdu_num( fptr, &hdupos ); /* Get the current HDU position */
73
74 /* List only a single structure if a specific extension was given */
75 if ( strchr( argv[1], '[' ) || strchr( argv[1], '+' ) ) {
76 single++;
77 }
78
79 for ( ; !status; hdupos++ ) { /* Main loop for each HDU */
80 fits_get_hdu_type( fptr, &hdutype, &status ); /* Get the HDU type */
81
82 fprintf( fout, "\nHDU #%d ", hdupos );
83 if ( hdutype == IMAGE_HDU ) { /* primary array or image HDU */
84 fits_get_img_param( fptr, 10, &bitpix, &naxis, naxes, &status );
85
86 fprintf( fout, "Array: NAXIS = %d, BITPIX = %d\n", naxis, bitpix );
87 for ( ii = 0; ii < naxis; ii++ ) {
88 fprintf( fout, " NAXIS%d = %ld\n", ii + 1, naxes[ii] );
89 }
90 } else { /* a table HDU */
91 fits_get_num_rows( fptr, &nrows, &status );
92 fits_get_num_cols( fptr, &ncols, &status );
93
94 if ( hdutype == ASCII_TBL ) {
95 fprintf( fout, "ASCII Table: " );
96 } else {
97 fprintf( fout, "Binary Table: " );
98 }
99
100 fprintf( fout, "%d columns x %ld rows\n", ncols, nrows );
101 fprintf( fout, " COL NAME FORMAT\n" );
102
103 for ( ii = 1; ii <= ncols; ii++ ) {
104 fits_make_keyn( "TTYPE", ii, keyname, &status ); /* make keyword */
105 fits_read_key( fptr, TSTRING, keyname, colname, NULL, &status );
106 fits_make_keyn( "TFORM", ii, keyname, &status ); /* make keyword */
107 fits_read_key( fptr, TSTRING, keyname, coltype, NULL, &status );
108
109 fprintf( fout, " %3d %-16s %-16s\n", ii, colname, coltype );
110 }
111 }
112
113 if ( single ) {
114 break; /* quit if only listing a single HDU */
115 }
116
117 fits_movrel_hdu( fptr, 1, NULL, &status ); /* try move to next ext */
118 }
119
120 if ( status == END_OF_FILE ) {
121 status = 0; /* Reset normal error */
122 }
123 fits_close_file( fptr, &status );
124 }
125
126#if defined(PAGER) && defined(HAVE_POPEN) && defined(HAVE_PCLOSE)
127 pclose( fout );
128#endif
129
130 if ( status ) {
131 fits_report_error( stderr, status ); /* print any error message */
132 }
133 return ( status );
134}
#define fprintf
int main(int argc, char *argv[])
Definition: fits_overview.c:40