LALPulsar 7.1.1.1-eeff03c
fits_table_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#include <math.h>
30
31#if defined(HAVE_LIBCFITSIO)
32// disable -Wstrict-prototypes flag for this header file as this causes
33// a build failure for cfitsio-3.440+
34#pragma GCC diagnostic ignored "-Wstrict-prototypes"
35#include <fitsio.h>
36#pragma GCC diagnostic pop
37#else
38#error CFITSIO library is not available
39#endif
40
41static int ndigits( int x )
42{
43 return floor( log10( abs( x ) ) ) + 1;
44}
45
46int main( int argc, char *argv[] )
47{
48 fitsfile *fptr = 0; /* FITS file pointer, defined in fitsio.h */
49 char *val = 0, value[1000], nullstr[] = "NAN";
50 char keyword[FLEN_KEYWORD], colname[1000][FLEN_VALUE];
51 int status = 0; /* CFITSIO status value MUST be initialized to zero! */
52 int hdunum = 0, hdutype = 0, ncols = 0, ii = 0, anynul = 0, typecode[1000], dispwidth[1000], nd = 0;
53 long jj = 0, nrows = 0, nvecelem[1000], kk = 0, repeat, width = 0;
54
55 int printhelp = ( argc == 2 && ( strcmp( argv[1], "-h" ) == 0 || strcmp( argv[1], "--help" ) == 0 ) );
56
57 char *argfile;
58 int printhdr;
59 if ( !printhelp && argc == 3 && strcmp( argv[1], "-n" ) == 0 ) {
60 printhdr = 0;
61 argfile = argv[2];
62 } else if ( !printhelp && argc == 2 ) {
63 printhdr = 1;
64 argfile = argv[1];
65 } else {
66 fprintf( stderr, "Usage: %s filename[ext][col filter][row filter] \n", argv[0] );
67 fprintf( stderr, "\n" );
68 fprintf( stderr, "List the contents of a FITS table \n" );
69 fprintf( stderr, "\n" );
70 fprintf( stderr, "Examples: \n" );
71 fprintf( stderr, " %s tab.fits[GTI] - list the GTI extension\n", argv[0] );
72 fprintf( stderr, " %s tab.fits[1][#row < 101] - list first 100 rows\n", argv[0] );
73 fprintf( stderr, " %s tab.fits[1][col X;Y] - list X and Y cols only\n", argv[0] );
74 fprintf( stderr, " %s tab.fits[1][col -PI] - list all but the PI col\n", argv[0] );
75 fprintf( stderr, " %s tab.fits[1][col -PI][#row < 101] - combined case\n", argv[0] );
76 fprintf( stderr, " %s -n ... - list without table header\n", argv[0] );
77 fprintf( stderr, "\n" );
78 fprintf( stderr, "Display formats can be modified with the TDISPn keywords.\n" );
79 return ( 0 );
80 }
81
82#if defined(PAGER) && defined(HAVE_POPEN) && defined(HAVE_PCLOSE)
83 FILE *fout = popen( PAGER, "w" );
84 if ( fout == NULL ) {
85 fprintf( stderr, "Could not execute '%s'\n", PAGER );
86 return ( 1 );
87 }
88#else
89 FILE *fout = stdout;
90#endif
91
92 if ( !fits_open_file( &fptr, argfile, READONLY, &status ) ) {
93 if ( fits_get_hdu_num( fptr, &hdunum ) == 1 )
94 /* This is the primary array; try to move to the */
95 /* first extension and see if it is a table */
96 {
97 fits_movabs_hdu( fptr, 2, &hdutype, &status );
98 } else {
99 fits_get_hdu_type( fptr, &hdutype, &status ); /* Get the HDU type */
100 }
101
102 if ( hdutype == IMAGE_HDU ) {
103 fprintf( stderr, "Error: this program only displays tables, not images\n" );
104 } else {
105 fits_get_num_rows( fptr, &nrows, &status );
106 fits_get_num_cols( fptr, &ncols, &status );
107
108 for ( ii = 1; ii <= ncols; ii++ ) {
109 fits_make_keyn( "TTYPE", ii, keyword, &status );
110 fits_read_key( fptr, TSTRING, keyword, colname[ii], NULL, &status );
111 fits_get_col_display_width( fptr, ii, &dispwidth[ii], &status );
112 fits_get_coltype( fptr, ii, &typecode[ii], &repeat, &width, &status );
113 if ( typecode[ii] != TSTRING && repeat > 1 ) {
114 nvecelem[ii] = repeat;
115 dispwidth[ii] += ndigits( nvecelem[ii] ) + 2;
116 } else {
117 nvecelem[ii] = 1;
118 }
119 if ( dispwidth[ii] < ( int )strlen( colname[ii] ) ) {
120 dispwidth[ii] = ( int )strlen( colname[ii] );
121 }
122 }
123
124 /* print column names as column headers */
125 if ( printhdr ) {
126 fprintf( fout, "##\n## " );
127 for ( ii = 1; ii <= ncols; ii++ ) {
128 if ( nvecelem[ii] > 1 ) {
129 for ( kk = 1; kk <= nvecelem[ii]; kk++ ) {
130 nd = ndigits( kk );
131 fprintf( fout, "%*s[%*li] ", dispwidth[ii] - nd - 2, colname[ii], nd, kk );
132 }
133 } else {
134 fprintf( fout, "%*s ", dispwidth[ii], colname[ii] );
135 }
136 }
137 fprintf( fout, "\n" ); /* terminate header line */
138 }
139
140 /* print each column, row by row (there are faster ways to do this) */
141 val = value;
142 for ( jj = 1; jj <= nrows && !status; jj++ ) {
143 fprintf( fout, " " );
144 for ( ii = 1; ii <= ncols && !status; ii++ ) {
145
146 /* read value as a string, regardless of intrinsic datatype */
147 for ( kk = 1; kk <= nvecelem[ii] && !status; kk++ ) {
148 if ( fits_read_col_str( fptr, ii, jj, kk, 1, nullstr, &val, &anynul, &status ) ) {
149 break; /* jump out of loop on error */
150 }
151 fprintf( fout, "%*s ", dispwidth[ii], value );
152 }
153
154 }
155 fprintf( fout, "\n" );
156 }
157 }
158 fits_close_file( fptr, &status );
159 }
160
161#if defined(PAGER) && defined(HAVE_POPEN) && defined(HAVE_PCLOSE)
162 pclose( fout );
163#endif
164
165 if ( status ) {
166 fits_report_error( stderr, status ); /* print any error message */
167 }
168 return ( status );
169}
#define fprintf
int main(int argc, char *argv[])
static int ndigits(int x)