LALPulsar 7.1.1.1-eeff03c
fits_header_getval.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
40#if defined(HAVE_LIBCFITSIO)
41
42#include <fitsio.h>
43
44// If fffree() is missing, use free() instead
45#if !defined(HAVE_FFFREE)
46#undef fits_free_memory
47#define fits_free_memory(ptr, status) free(ptr)
48
49// If ffree() is present but not declared, declare it
50#elif defined(HAVE_DECL_FFFREE) && !HAVE_DECL_FFFREE
51int fffree( void *, int * );
52#undef fits_free_memory
53#define fits_free_memory fffree
54
55#endif // ffree()
56
57#endif // defined(HAVE_LIBCFITSIO)
58
59int main( int argc, char *argv[] )
60{
61 fitsfile *fptr = 0; /* FITS file pointer, defined in fitsio.h */
62 char card[FLEN_CARD];
63 char value[FLEN_VALUE], comment[FLEN_COMMENT];
64 char *longvalue = 0;
65 int status = 0; /* CFITSIO status value MUST be initialized to zero! */
66
67 int printhelp = ( argc == 2 && ( strcmp( argv[1], "-h" ) == 0 || strcmp( argv[1], "--help" ) == 0 ) );
68
69 if ( printhelp || argc != 3 ) {
70 fprintf( stderr, "Usage: %s filename[ext] keyword\n", argv[0] );
71 fprintf( stderr, "\n" );
72 fprintf( stderr, "Print the current value of a header keyword.\n" );
73 fprintf( stderr, "\n" );
74 fprintf( stderr, "Examples: \n" );
75 fprintf( stderr, " %s file.fits dec - list the DEC keyword \n", argv[0] );
76 return ( 0 );
77 }
78
79 if ( !fits_open_file( &fptr, argv[1], READONLY, &status ) ) {
80 if ( fits_read_card( fptr, argv[2], card, &status ) ) {
81 fprintf( stderr, "Keyword does not exist\n" );
82 return ( 1 );
83 }
84
85 fits_parse_value( card, value, comment, &status );
86 if ( value[0] != '\'' ) {
87 printf( "%s\n", value );
88 } else {
89 fits_read_key_longstr( fptr, argv[2], &longvalue, comment, &status );
90 if ( longvalue ) {
91 printf( "%s\n", longvalue );
92 fits_free_memory( longvalue, &status );
93 }
94 }
95
96 fits_close_file( fptr, &status );
97 } /* open_file */
98
99 /* if error occured, print out error message */
100 if ( status ) {
101 fits_report_error( stderr, status );
102 }
103 return ( status );
104}
const char * comment
Definition: SearchTiming.c:94
#define fprintf
int main(int argc, char *argv[])