-
Notifications
You must be signed in to change notification settings - Fork 12
/
nc_uint64.go
302 lines (278 loc) · 9.6 KB
/
nc_uint64.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2014 The Go-NetCDF Authors. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
// These files are autogenerated from nc_double.go using generate.go
// DO NOT EDIT (except nc_double.go).
package netcdf
import (
"fmt"
"unsafe"
)
// #include <stdlib.h>
// #include <netcdf.h>
import "C"
// WriteUint64s writes data as the entire data for variable v.
func (v Var) WriteUint64s(data []uint64) error {
if err := okData(v, UINT64, len(data)); err != nil {
return err
}
return newError(C.nc_put_var_ulonglong(C.int(v.ds), C.int(v.id), (*C.ulonglong)(unsafe.Pointer(&data[0]))))
}
// ReadUint64s reads the entire variable v into data, which must have enough
// space for all the values (i.e. len(data) must be at least v.Len()).
func (v Var) ReadUint64s(data []uint64) error {
if err := okData(v, UINT64, len(data)); err != nil {
return err
}
return newError(C.nc_get_var_ulonglong(C.int(v.ds), C.int(v.id), (*C.ulonglong)(unsafe.Pointer(&data[0]))))
}
// WriteUint64s sets the value of attribute a to val.
func (a Attr) WriteUint64s(val []uint64) error {
// We don't need okData here because netcdf library doesn't know
// the length or type of the attribute yet.
cname := C.CString(a.name)
defer C.free(unsafe.Pointer(cname))
return newError(C.nc_put_att_ulonglong(C.int(a.v.ds), C.int(a.v.id), cname,
C.nc_type(UINT64), C.size_t(len(val)), (*C.ulonglong)(unsafe.Pointer(&val[0]))))
}
// ReadUint64s reads the entire attribute value into val.
func (a Attr) ReadUint64s(val []uint64) (err error) {
if err := okData(a, UINT64, len(val)); err != nil {
return err
}
cname := C.CString(a.name)
defer C.free(unsafe.Pointer(cname))
err = newError(C.nc_get_att_ulonglong(C.int(a.v.ds), C.int(a.v.id), cname,
(*C.ulonglong)(unsafe.Pointer(&val[0]))))
return
}
// ReadUint64At returns a value via index position
func (v Var) ReadUint64At(idx []uint64) (val uint64, err error) {
var dimPtr *C.size_t
if len(idx) > 0 {
dimPtr = (*C.size_t)(unsafe.Pointer(&idx[0]))
}
err = newError(C.nc_get_var1_ulonglong(C.int(v.ds), C.int(v.id),
dimPtr, (*C.ulonglong)(unsafe.Pointer(&val))))
return
}
// WriteUint64At sets a value via its index position
func (v Var) WriteUint64At(idx []uint64, val uint64) (err error) {
var dimPtr *C.size_t
if len(idx) > 0 {
dimPtr = (*C.size_t)(unsafe.Pointer(&idx[0]))
}
err = newError(C.nc_put_var1_ulonglong(C.int(v.ds), C.int(v.id),
dimPtr, (*C.ulonglong)(unsafe.Pointer(&val))))
return
}
// WriteUint64Slice writes data as a slice of variable v. The slice is specified by start and count:
// https://www.unidata.ucar.edu/software/netcdf/docs/programming_notes.html#specify_hyperslab.
func (v Var) WriteUint64Slice(data []uint64, start, count []uint64) error {
if err := okDataSlice(v, UINT64, len(data), start, count); err != nil {
return err
}
return newError(C.nc_put_vara_ulonglong(C.int(v.ds), C.int(v.id),
(*C.size_t)(unsafe.Pointer(&start[0])),
(*C.size_t)(unsafe.Pointer(&count[0])),
(*C.ulonglong)(unsafe.Pointer(&data[0])),
))
}
// ReadUint64Slice reads a slice of variable v into data, which must have enough
// space for all the values. The slice is specified by start and count:
// https://www.unidata.ucar.edu/software/netcdf/docs/programming_notes.html#specify_hyperslab.
func (v Var) ReadUint64Slice(data []uint64, start, count []uint64) error {
if err := okDataSlice(v, UINT64, len(data), start, count); err != nil {
return err
}
return newError(C.nc_get_vara_ulonglong(C.int(v.ds), C.int(v.id),
(*C.size_t)(unsafe.Pointer(&start[0])),
(*C.size_t)(unsafe.Pointer(&count[0])),
(*C.ulonglong)(unsafe.Pointer(&data[0])),
))
}
// WriteUint64StridedSlice writes data as a slice of variable v. The slice is specified by start, count and stride:
// https://www.unidata.ucar.edu/software/netcdf/docs/programming_notes.html#specify_hyperslab.
func (v Var) WriteUint64StridedSlice(data []uint64, start, count []uint64, stride []int64) error {
if err := okDataStride(v, UINT64, len(data), start, count, stride); err != nil {
return err
}
return newError(C.nc_put_vars_ulonglong(C.int(v.ds), C.int(v.id),
(*C.size_t)(unsafe.Pointer(&start[0])),
(*C.size_t)(unsafe.Pointer(&count[0])),
(*C.ptrdiff_t)(unsafe.Pointer(&stride[0])),
(*C.ulonglong)(unsafe.Pointer(&data[0])),
))
}
// ReadUint64StridedSlice reads a strided slice of variable v into data, which must have enough
// space for all the values. The slice is specified by start, count and stride:
// https://www.unidata.ucar.edu/software/netcdf/docs/programming_notes.html#specify_hyperslab.
func (v Var) ReadUint64StridedSlice(data []uint64, start, count []uint64, stride []int64) error {
if err := okDataStride(v, UINT64, len(data), start, count, stride); err != nil {
return err
}
return newError(C.nc_get_vars_ulonglong(C.int(v.ds), C.int(v.id),
(*C.size_t)(unsafe.Pointer(&start[0])),
(*C.size_t)(unsafe.Pointer(&count[0])),
(*C.ptrdiff_t)(unsafe.Pointer(&stride[0])),
(*C.ulonglong)(unsafe.Pointer(&data[0])),
))
}
// Uint64sReader is a interface that allows reading a sequence of values of fixed length.
type Uint64sReader interface {
Len() (n uint64, err error)
ReadUint64s(val []uint64) (err error)
}
// GetUint64s reads the entire data in r and returns it.
func GetUint64s(r Uint64sReader) (data []uint64, err error) {
n, err := r.Len()
if err != nil {
return
}
data = make([]uint64, n)
err = r.ReadUint64s(data)
return
}
// testReadUint64s writes somes data to v. N is v.Len().
// This function is only used for testing.
func testWriteUint64s(v Var, n uint64) error {
data := make([]uint64, n)
for i := 0; i < int(n); i++ {
data[i] = uint64(i + 10)
}
return v.WriteUint64s(data)
}
// testReadUint64s reads data from v and checks that it's the same as what
// was written by testWriteDouble. N is v.Len().
// This function is only used for testing.
func testReadUint64s(v Var, n uint64) error {
data := make([]uint64, n)
if err := v.ReadUint64s(data); err != nil {
return err
}
for i := 0; i < int(n); i++ {
if val := uint64(i + 10); data[i] != val {
return fmt.Errorf("data at position %d is %v; expected %v", i, data[i], val)
}
}
return nil
}
// testWriteUint64Slice writes somes data to v. N is v.LenDim().
// This function is only used for testing.
func testWriteUint64Slice(v Var, n []uint64) error {
if len(n) == 0 {
return nil // Don't test empty data.
}
start, count := make([]uint64, len(n)), make([]uint64, len(n))
for i, v := range n {
start[i] = v / 2
count[i] = v / 2
}
data := make([]uint64, product(count))
for i := 0; i < int(product(count)); i++ {
data[i] = uint64(i + 10)
}
return v.WriteUint64Slice(data, start, count)
}
// testReadUint64Slice reads data from v and checks that it's the same as what
// was written by testWriteDouble. N is v.LenDim().
// This function is only used for testing.
func testReadUint64Slice(v Var, n []uint64) error {
if len(n) == 0 {
return nil // Don't test empty data.
}
start, count := make([]uint64, len(n)), make([]uint64, len(n))
for i, v := range n {
start[i] = v / 2
count[i] = v / 2
}
data := make([]uint64, product(count))
if err := v.ReadUint64Slice(data, start, count); err != nil {
return err
}
for i := 0; i < int(product(count)); i++ {
if val := uint64(i + 10); data[i] != val {
return fmt.Errorf("strided slice data at position %d is %v; expected %v", i, data[i], val)
}
}
return nil
}
// testWriteUint64StridedSlice writes somes data to v. N is v.LenDim().
// This function is only used for testing.
func testWriteUint64StridedSlice(v Var, n []uint64) error {
if len(n) == 0 {
return nil // Don't test empty data.
}
start, count, stride := make([]uint64, len(n)), make([]uint64, len(n)), make([]int64, len(n))
for i, v := range n {
start[i] = 1
count[i] = (v - 1) / 2
stride[i] = 2
}
data := make([]uint64, product(count))
for i := 0; i < int(product(count)); i++ {
data[i] = uint64(i + 10)
}
return v.WriteUint64StridedSlice(data, start, count, stride)
}
// testReadUint64StridedSlice reads data from v and checks that it's the same as what
// was written by testWriteDouble. N is v.LenDim().
// This function is only used for testing.
func testReadUint64StridedSlice(v Var, n []uint64) error {
if len(n) == 0 {
return nil // Don't test empty data.
}
start, count, stride := make([]uint64, len(n)), make([]uint64, len(n)), make([]int64, len(n))
for i, v := range n {
start[i] = 1
count[i] = (v - 1) / 2
stride[i] = 2
}
data := make([]uint64, product(count))
if err := v.ReadUint64StridedSlice(data, start, count, stride); err != nil {
return err
}
for i := 0; i < int(product(count)); i++ {
if val := uint64(i + 10); data[i] != val {
return fmt.Errorf("strided slice data at position %d is %v; expected %v", i, data[i], val)
}
}
return nil
}
func testReadUint64At(v Var, n uint64) error {
data := make([]uint64, n)
if err := v.ReadUint64s(data); err != nil {
return err
}
for i := 0; i < int(n); i++ {
shape, _ := v.LenDims()
coords, _ := UnravelIndex(uint64(i), shape)
expected := data[i]
val, _ := v.ReadUint64At(coords)
if val != expected {
return fmt.Errorf("data at position %v is %v; expected %v", i, val, expected)
}
}
return nil
}
func testWriteUint64At(v Var, n uint64) error {
shape, _ := v.LenDims()
ndim := len(shape)
coord := make([]uint64, ndim)
for i := 0; i < ndim; i++ {
for k := 0; k < ndim; k++ {
coord[k] = uint64(i)
}
v.WriteUint64At(coord, uint64(i))
}
for i := 0; i < ndim; i++ {
for k := 0; k < ndim; k++ {
coord[k] = uint64(i)
}
val, _ := v.ReadUint64At(coord)
if val != uint64(i) {
return fmt.Errorf("data at position %v is %v; expected %v", coord, val, int(i))
}
}
return nil
}