1 /*
2  This file was downloaded from the CFITSIO utilities web page:
3  http://heasarc.gsfc.nasa.gov/docs/software/fitsio/cexamples.html
4 
5  That page contains this text:
6  You may freely modify, reuse, and redistribute these programs as you wish.
7 
8  We assume it was originally written by the CFITSIO authors (primarily William
9  D. Pence).
10 
11  We (the Astrometry.net team) have modified it slightly.
12  # Licensed under a 3-clause BSD style license - see LICENSE
13  */
14 
15 #include <string.h>
16 #include <stdio.h>
17 #include "fitsio.h"
18 
main(int argc,char * argv[])19 int main(int argc, char *argv[])
20 {
21     fitsfile *fptr;         /* FITS file pointer, defined in fitsio.h */
22     char keyname[FLEN_KEYWORD], colname[FLEN_VALUE], coltype[FLEN_VALUE];
23     int status = 0;   /* CFITSIO status value MUST be initialized to zero! */
24     int single = 0, hdupos, hdutype, bitpix, naxis, ncols, ii;
25     long naxes[10], nrows;
26 
27     if (argc != 2) {
28         printf("Usage:  liststruc filename[ext] \n");
29         printf("\n");
30         printf("List the structure of a single extension, or, if ext is \n");
31         printf("not given, list the structure of the entire FITS file.  \n");
32         printf("\n");
33         printf("Note that it may be necessary to enclose the input file\n");
34         printf("name in single quote characters on the Unix command line.\n");
35         return(0);
36     }
37 
38     if (!fits_open_file(&fptr, argv[1], READONLY, &status))
39         {
40             fits_get_hdu_num(fptr, &hdupos);  /* Get the current HDU position */
41 
42             /* List only a single structure if a specific extension was given */
43             if (strchr(argv[1], '[') || strchr(argv[1], '+')) single++;
44 
45             for (; !status; hdupos++)   /* Main loop for each HDU */
46                 {
47                     fits_get_hdu_type(fptr, &hdutype, &status);  /* Get the HDU type */
48 
49                     printf("\nHDU #%d  ", hdupos);
50                     if (hdutype == IMAGE_HDU)   /* primary array or image HDU */
51                         {
52                             fits_get_img_param(fptr, 10, &bitpix, &naxis, naxes, &status);
53 
54                             printf("Array:  NAXIS = %d,  BITPIX = %d\n", naxis, bitpix);
55                             for (ii = 0; ii < naxis; ii++)
56                                 printf("   NAXIS%d = %ld\n",ii+1, naxes[ii]);
57                         }
58                     else  /* a table HDU */
59                         {
60                             fits_get_num_rows(fptr, &nrows, &status);
61                             fits_get_num_cols(fptr, &ncols, &status);
62 
63                             if (hdutype == ASCII_TBL)
64                                 printf("ASCII Table:  ");
65                             else
66                                 printf("Binary Table:  ");
67 
68                             printf("%d columns x %ld rows\n", ncols, nrows);
69                             printf(" COL NAME             FORMAT\n");
70 
71                             for (ii = 1; ii <= ncols; ii++)
72                                 {
73                                     fits_make_keyn("TTYPE", ii, keyname, &status); /* make keyword */
74                                     fits_read_key(fptr, TSTRING, keyname, colname, NULL, &status);
75                                     fits_make_keyn("TFORM", ii, keyname, &status); /* make keyword */
76                                     fits_read_key(fptr, TSTRING, keyname, coltype, NULL, &status);
77 
78                                     printf(" %3d %-16s %-16s\n", ii, colname, coltype);
79                                 }
80                         }
81 
82                     if (single)break;  /* quit if only listing a single HDU */
83 
84                     fits_movrel_hdu(fptr, 1, NULL, &status);  /* try move to next ext */
85                 }
86 
87             if (status == END_OF_FILE) status = 0; /* Reset normal error */
88             fits_close_file(fptr, &status);
89         }
90 
91     if (status) fits_report_error(stderr, status); /* print any error message */
92     return(status);
93 }
94 
95