github.com/astrogo/cfitsio@v0.1.0/examples/go-cfitsio-imlist/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.Usage = func() { 13 const msg = `Usage: go-cfitsio-imlist filename[ext][section filter] 14 15 List the the pixel values in a FITS image 16 17 Example: 18 imlist image.fits - list the whole image 19 imlist image.fits[100:110,400:410] - list a section 20 imlist table.fits[2][bin (x,y) = 32] - list the pixels in 21 an image constructed from a 2D histogram of X and Y 22 columns in a table with a binning factor = 32 23 ` 24 fmt.Fprintf(os.Stderr, "%v\n", msg) 25 flag.PrintDefaults() 26 } 27 28 flag.Parse() 29 if flag.NArg() != 1 { 30 flag.Usage() 31 os.Exit(1) 32 } 33 34 fname := flag.Arg(0) 35 f, err := fits.Open(fname, fits.ReadOnly) 36 if err != nil { 37 panic(err) 38 } 39 defer f.Close() 40 41 hdu := f.CHDU() 42 hdr := hdu.Header() 43 44 if len(hdr.Axes()) > 2 || len(hdr.Axes()) == 0 { 45 fmt.Printf("Error: only 1D or 2D images are supported\n") 46 os.Exit(1) 47 } 48 49 axes := hdr.Axes() 50 51 var data []float64 52 err = hdu.Data(&data) 53 if err != nil { 54 panic(err) 55 } 56 57 // default output format string 58 hdformat := " %15d" 59 format := " %15.5f" 60 if hdr.Bitpix() > 0 { 61 hdformat = " %7d" 62 format = " %7.0f" 63 } 64 // column header 65 fmt.Printf("\n ") 66 row := int(axes[0]) 67 for ii := 0; ii < row; ii++ { 68 fmt.Printf(hdformat, ii) 69 } 70 fmt.Printf("\n") 71 72 // loop over all rows 73 for jj := 0; jj < int(axes[1]); jj++ { 74 fmt.Printf(" %4d ", jj) 75 for ii := 0; ii < row; ii++ { 76 fmt.Printf(format, data[row*jj+ii]) 77 } 78 79 fmt.Printf("\n") 80 } 81 if err != nil && err != fits.END_OF_FILE { 82 panic(err) 83 } 84 }