-
Notifications
You must be signed in to change notification settings - Fork 12
/
message.go
88 lines (66 loc) · 2.17 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package codes
import (
"math"
"runtime"
"github.com/amsokol/go-eccodes/debug"
"github.com/amsokol/go-eccodes/native"
)
type Message interface {
isOpen() bool
GetString(key string) (string, error)
GetLong(key string) (int64, error)
SetLong(key string, value int64) error
GetDouble(key string) (float64, error)
SetDouble(key string, value float64) error
Data() (latitudes []float64, longitudes []float64, values []float64, err error)
DataUnsafe() (latitudes *Float64ArrayUnsafe, longitudes *Float64ArrayUnsafe, values *Float64ArrayUnsafe, err error)
Close() error
}
type message struct {
handle native.Ccodes_handle
}
func newMessage(h native.Ccodes_handle) Message {
m := &message{handle: h}
runtime.SetFinalizer(m, messageFinalizer)
// set missing value to NaN
m.SetDouble(parameterMissingValue, math.NaN())
return m
}
func (m *message) isOpen() bool {
return m.handle != nil
}
func (m *message) GetString(key string) (string, error) {
return native.Ccodes_get_string(m.handle, key)
}
func (m *message) GetLong(key string) (int64, error) {
return native.Ccodes_get_long(m.handle, key)
}
func (m *message) SetLong(key string, value int64) error {
return native.Ccodes_set_long(m.handle, key, value)
}
func (m *message) GetDouble(key string) (float64, error) {
return native.Ccodes_get_double(m.handle, key)
}
func (m *message) SetDouble(key string, value float64) error {
return native.Ccodes_set_double(m.handle, key, value)
}
func (m *message) Data() (latitudes []float64, longitudes []float64, values []float64, err error) {
return native.Ccodes_grib_get_data(m.handle)
}
func (m *message) DataUnsafe() (latitudes *Float64ArrayUnsafe, longitudes *Float64ArrayUnsafe, values *Float64ArrayUnsafe, err error) {
lats, lons, vals, err := native.Ccodes_grib_get_data_unsafe(m.handle)
if err != nil {
return nil, nil, nil, err
}
return newFloat64ArrayUnsafe(lats), newFloat64ArrayUnsafe(lons), newFloat64ArrayUnsafe(vals), nil
}
func (m *message) Close() error {
defer func() { m.handle = nil }()
return native.Ccodes_handle_delete(m.handle)
}
func messageFinalizer(m *message) {
if m.isOpen() {
debug.MemoryLeakLogger.Print("message is not closed")
m.Close()
}
}