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

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  
     8  	fits "github.com/astrogo/cfitsio"
     9  )
    10  
    11  func main() {
    12  	flag.Parse()
    13  	if flag.NArg() != 2 {
    14  		flag.Usage = func() {
    15  			const msg = `Usage:  go-cfitsio-fitscopy inputfile outputfile
    16  
    17  Copy an input file to an output file, optionally filtering
    18  the file in the process.  This seemingly simple program can
    19  apply powerful filters which transform the input file as
    20  it is being copied.  Filters may be used to extract a
    21  subimage from a larger image, select rows from a table,
    22  filter a table with a GTI time extension or a SAO region file,
    23  create or delete columns in a table, create an image by
    24  binning (histogramming) 2 table columns, and convert IRAF
    25  format *.imh or raw binary data files into FITS images.
    26  See the CFITSIO User's Guide for a complete description of
    27  the Extended File Name filtering syntax.
    28  
    29  Examples:
    30  
    31  go-cfitsio-fitscopy in.fit out.fit                   (simple file copy)
    32  go-cfitsio-fitscopy - -                              (stdin to stdout)
    33  go-cfitsio-fitscopy in.fit[11:50,21:60] out.fit      (copy a subimage)
    34  go-cfitsio-fitscopy iniraf.imh out.fit               (IRAF image to FITS)
    35  go-cfitsio-fitscopy in.dat[i512,512] out.fit         (raw array to FITS)
    36  go-cfitsio-fitscopy in.fit[events][pi>35] out.fit    (copy rows with pi>35)
    37  go-cfitsio-fitscopy in.fit[events][bin X,Y] out.fit  (bin an image) 
    38  go-cfitsio-fitscopy in.fit[events][col x=.9*y] out.fit        (new x column)
    39  go-cfitsio-fitscopy in.fit[events][gtifilter()] out.fit       (time filter)
    40  go-cfitsio-fitscopy in.fit[2][regfilter("pow.reg")] out.fit (spatial filter)
    41  
    42  Note that it may be necessary to enclose the input file name
    43  in single quote characters on the Unix command line.
    44  `
    45  			fmt.Fprintf(os.Stderr, "%v\n", msg)
    46  		}
    47  		flag.Usage()
    48  		os.Exit(1)
    49  	}
    50  
    51  	// open input file
    52  	in, err := fits.Open(flag.Arg(0), fits.ReadOnly)
    53  	if err != nil {
    54  		panic(err)
    55  	}
    56  	defer in.Close()
    57  
    58  	// create output file
    59  	out, err := fits.Create(flag.Arg(1))
    60  	if err != nil {
    61  		panic(err)
    62  	}
    63  	defer out.Close()
    64  
    65  	// copy every HDU until we get an error
    66  	for i := 1; err == nil; i++ {
    67  		err = in.SeekHDU(i, 0)
    68  		if err != nil {
    69  			break
    70  		}
    71  		err = fits.CopyHDU(&out, &in, 0)
    72  	}
    73  	if err != nil && err != fits.END_OF_FILE {
    74  		panic(err)
    75  	}
    76  }