github.com/astrogo/cfitsio@v0.1.0/examples/go-cfitsio-listhead/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	cfitsio "github.com/astrogo/cfitsio"
    10  )
    11  
    12  func main() {
    13  	var single bool
    14  
    15  	flag.Usage = func() {
    16  		const msg = `Usage: go-cfitsio-listhead filename[ext]
    17  
    18  
    19  List the FITS header keywords in a single extension, or, if 
    20  ext is not given, list the keywords in all the extensions. 
    21  
    22  Examples:
    23     
    24     go-cfitsio-listhead file.fits      - list every header in the file 
    25     go-cfitsio-listhead file.fits[0]   - list primary array header 
    26     go-cfitsio-listhead file.fits[2]   - list header of 2nd extension 
    27     go-cfitsio-listhead file.fits+2    - same as above 
    28     go-cfitsio-listhead file.fits[GTI] - list header of GTI extension
    29  
    30  Note that it may be necessary to enclose the input file
    31  name in single quote characters on the Unix command line.
    32  `
    33  		fmt.Fprintf(os.Stderr, "%v\n", msg)
    34  		flag.PrintDefaults()
    35  	}
    36  
    37  	flag.Parse()
    38  	if flag.NArg() != 1 {
    39  		flag.Usage()
    40  		os.Exit(1)
    41  	}
    42  
    43  	fname := flag.Arg(0)
    44  	f, err := cfitsio.Open(fname, cfitsio.ReadOnly)
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  	defer f.Close()
    49  
    50  	ihdu := f.HDUNum() // get the current HDU position
    51  
    52  	// list only a single header if a specific extension was given
    53  	if ihdu != 0 || strings.Contains(fname, "[") {
    54  		single = true
    55  	}
    56  
    57  	for i := ihdu; i < len(f.HDUs()); i++ {
    58  		hdu := f.CHDU()
    59  		hdr := hdu.Header()
    60  		fmt.Printf("Header listing for HDU #%d:\n", i)
    61  
    62  		for _, n := range hdr.Keys() {
    63  			card := hdr.Get(n)
    64  			if card == nil {
    65  				panic(fmt.Errorf("could not retrieve card [%v]", n))
    66  			}
    67  			fmt.Printf(
    68  				"%-8s= %-29s / %s\n",
    69  				card.Name,
    70  				fmt.Sprintf("%v", card.Value),
    71  				card.Comment)
    72  		}
    73  		fmt.Printf("END\n\n")
    74  
    75  		// quit if only listing a single header
    76  		if single {
    77  			break
    78  		}
    79  		err = f.SeekHDU(1, 0)
    80  	}
    81  	if err != nil && err != cfitsio.END_OF_FILE {
    82  		panic(err)
    83  	}
    84  }