github.com/sbinet/go-cfitsio@v0.0.0-20140625105338-0307f985659e/utils.go (about) 1 package cfitsio 2 3 // #include "go-cfitsio.h" 4 // #include "go-cfitsio-utils.h" 5 import "C" 6 import ( 7 "reflect" 8 "strings" 9 "unsafe" 10 ) 11 12 // strIsContinued checks whether the last non-whitespace char in the string 13 // is an ampersand, which indicates the value string is to be continued on the 14 // next line 15 func strIsContinued(v string) bool { 16 vv := strings.Trim(v, " \n\t'") 17 if len(vv) == 0 { 18 return false 19 } 20 return vv[len(vv)-1] == '&' 21 } 22 23 func getLongValueString(f *File, key string) (string, error) { 24 c_key := C.CString(key) 25 defer C.free(unsafe.Pointer(c_key)) 26 c_status := C.int(0) 27 var c_value *C.char = nil 28 defer C.free(unsafe.Pointer(c_value)) 29 C.fits_read_key_longstr(f.c, c_key, &c_value, nil, &c_status) 30 if c_status > 0 { 31 return "", to_err(c_status) 32 } 33 return C.GoString(c_value), nil 34 } 35 36 // gotype2FITS returns the FITS format corresponding to a Go type 37 // it returns "" if there is no corresponding FITS format. 38 func gotype2FITS(v interface{}, hdu HDUType) string { 39 rt := reflect.TypeOf(v) 40 hdr := "" 41 switch rt.Kind() { 42 case reflect.Slice: 43 hdr = "Q" 44 rt = rt.Elem() 45 case reflect.Array: 46 hdr = "Q" 47 rt = rt.Elem() 48 default: 49 // no-op 50 } 51 52 t, ok := g_gotype2FITS[rt.Kind()] 53 if !ok { 54 return "" 55 } 56 fitsfmt, ok := t[hdu] 57 if !ok { 58 return "" 59 } 60 return hdr + fitsfmt 61 } 62 63 var g_gotype2FITS = map[reflect.Kind]map[HDUType]string{ 64 65 reflect.Bool: { 66 ASCII_TBL: "", 67 BINARY_TBL: "L", 68 }, 69 70 reflect.Int: { 71 ASCII_TBL: "I21", 72 BINARY_TBL: "K", 73 }, 74 75 reflect.Int8: { 76 ASCII_TBL: "I7", 77 BINARY_TBL: "S", 78 }, 79 80 reflect.Int16: { 81 ASCII_TBL: "I7", 82 BINARY_TBL: "I", 83 }, 84 85 reflect.Int32: { 86 ASCII_TBL: "I12", 87 BINARY_TBL: "J", 88 }, 89 90 reflect.Int64: { 91 ASCII_TBL: "I21", 92 BINARY_TBL: "K", 93 }, 94 95 reflect.Uint: { 96 ASCII_TBL: "I21", 97 BINARY_TBL: "V", 98 }, 99 100 reflect.Uint8: { 101 ASCII_TBL: "I7", 102 BINARY_TBL: "B", 103 }, 104 105 reflect.Uint16: { 106 ASCII_TBL: "I7", 107 BINARY_TBL: "U", 108 }, 109 110 reflect.Uint32: { 111 ASCII_TBL: "I12", 112 BINARY_TBL: "V", 113 }, 114 115 reflect.Uint64: { 116 ASCII_TBL: "I21", 117 BINARY_TBL: "V", 118 }, 119 120 reflect.Uintptr: { 121 ASCII_TBL: "", 122 BINARY_TBL: "", 123 }, 124 125 reflect.Float32: { 126 ASCII_TBL: "E26.17", // must write as float64 since we can only read as such 127 BINARY_TBL: "E", 128 }, 129 130 reflect.Float64: { 131 ASCII_TBL: "E26.17", 132 BINARY_TBL: "D", 133 }, 134 135 reflect.Complex64: { 136 ASCII_TBL: "", 137 BINARY_TBL: "C", 138 }, 139 140 reflect.Complex128: { 141 ASCII_TBL: "", 142 BINARY_TBL: "M", 143 }, 144 145 reflect.Array: { 146 ASCII_TBL: "", 147 BINARY_TBL: "", 148 }, 149 150 reflect.Chan: { 151 ASCII_TBL: "", 152 BINARY_TBL: "", 153 }, 154 155 reflect.Func: { 156 ASCII_TBL: "", 157 BINARY_TBL: "", 158 }, 159 160 reflect.Interface: { 161 ASCII_TBL: "", 162 BINARY_TBL: "", 163 }, 164 165 reflect.Map: { 166 ASCII_TBL: "", 167 BINARY_TBL: "", 168 }, 169 170 reflect.Ptr: { 171 ASCII_TBL: "", 172 BINARY_TBL: "", 173 }, 174 175 reflect.Slice: { 176 ASCII_TBL: "", 177 BINARY_TBL: "", 178 }, 179 180 reflect.String: { 181 ASCII_TBL: "A80", 182 BINARY_TBL: "80A", 183 }, 184 185 reflect.Struct: { 186 ASCII_TBL: "", 187 BINARY_TBL: "", 188 }, 189 } 190 191 // EOF