LALPulsar 7.1.1.1-eeff03c
fits_header_list.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; /* FITS file pointer, defined in fitsio.h */
43 char card[FLEN_CARD]; /* Standard string lengths defined in fitsio.h */
44 int status = 0; /* CFITSIO status value MUST be initialized to zero! */
45 int single = 0, hdupos = 0, nkeys = 0, ii = 0;
46
47 int printhelp = ( argc == 2 && ( strcmp( argv[1], "-h" ) == 0 || strcmp( argv[1], "--help" ) == 0 ) );
48
49 if ( printhelp || argc != 2 ) {
50 fprintf( stderr, "Usage: %s filename[ext] \n", argv[0] );
51 fprintf( stderr, "\n" );
52 fprintf( stderr, "List the FITS header keywords in a single extension, or, if \n" );
53 fprintf( stderr, "ext is not given, list the keywords in all the extensions. \n" );
54 fprintf( stderr, "\n" );
55 fprintf( stderr, "Examples: \n" );
56 fprintf( stderr, " %s file.fits - list every header in the file \n", argv[0] );
57 fprintf( stderr, " %s file.fits[0] - list primary array header \n", argv[0] );
58 fprintf( stderr, " %s file.fits[2] - list header of 2nd extension \n", argv[0] );
59 fprintf( stderr, " %s file.fits+2 - same as above \n", argv[0] );
60 fprintf( stderr, " %s file.fits[GTI] - list header of GTI extension\n", argv[0] );
61 fprintf( stderr, "\n" );
62 fprintf( stderr, "Note that it may be necessary to enclose the input file\n" );
63 fprintf( stderr, "name in single quote characters on the Unix command line.\n" );
64 return ( 0 );
65 }
66
67#if defined(PAGER) && defined(HAVE_POPEN) && defined(HAVE_PCLOSE)
68 FILE *fout = popen( PAGER, "w" );
69 if ( fout == NULL ) {
70 fprintf( stderr, "Could not execute '%s'\n", PAGER );
71 return ( 1 );
72 }
73#else
74 FILE *fout = stdout;
75#endif
76
77 if ( !fits_open_file( &fptr, argv[1], READONLY, &status ) ) {
78 fits_get_hdu_num( fptr, &hdupos ); /* Get the current HDU position */
79
80 /* List only a single header if a specific extension was given */
81 if ( hdupos != 1 || strchr( argv[1], '[' ) ) {
82 single = 1;
83 }
84
85 for ( ; !status; hdupos++ ) { /* Main loop through each extension */
86 fits_get_hdrspace( fptr, &nkeys, NULL, &status ); /* get # of keywords */
87
88 fprintf( fout, "Header listing for HDU #%d:\n", hdupos );
89
90 for ( ii = 1; ii <= nkeys; ii++ ) { /* Read and print each keywords */
91
92 if ( fits_read_record( fptr, ii, card, &status ) ) {
93 break;
94 }
95 fprintf( fout, "%s\n", card );
96 }
97 fprintf( fout, "END\n\n" ); /* terminate listing with END */
98
99 if ( single ) {
100 break; /* quit if only listing a single header */
101 }
102
103 fits_movrel_hdu( fptr, 1, NULL, &status ); /* try to move to next HDU */
104 }
105
106 if ( status == END_OF_FILE ) {
107 status = 0; /* Reset after normal error */
108 }
109
110 fits_close_file( fptr, &status );
111 }
112
113#if defined(PAGER) && defined(HAVE_POPEN) && defined(HAVE_PCLOSE)
114 pclose( fout );
115#endif
116
117 if ( status ) {
118 fits_report_error( stderr, status ); /* print any error message */
119 }
120 return ( status );
121}
#define fprintf
int main(int argc, char *argv[])