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