-
Notifications
You must be signed in to change notification settings - Fork 12
/
datasets.go
88 lines (74 loc) · 2.17 KB
/
datasets.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
// 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.
package netcdf
// #cgo pkg-config: netcdf
// #include <stdlib.h>
// #include <netcdf.h>
import "C"
import (
"unsafe"
)
// Error represents an error returned by netCDF C library.
type Error C.int
func newError(n C.int) error {
if n == C.NC_NOERR {
return nil
}
return Error(n)
}
// Error returns a string representation of Error e.
func (e Error) Error() string {
return C.GoString(C.nc_strerror(C.int(e)))
}
// Dataset represents a netCDF dataset.
type Dataset C.int
// CreateFile creates a new netCDF dataset.
// Mode is a bitwise-or of FileMode values.
func CreateFile(path string, mode FileMode) (ds Dataset, err error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
var id C.int
err = newError(C.nc_create(cpath, C.int(mode), &id))
ds = Dataset(id)
return
}
// OpenFile opens an existing netCDF dataset file at path.
// Mode is a bitwise-or of FileMode values.
func OpenFile(path string, mode FileMode) (ds Dataset, err error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
var id C.int
err = newError(C.nc_open(cpath, C.int(mode), &id))
ds = Dataset(id)
return
}
// Close closes an open netCDF dataset.
func (ds Dataset) Close() (err error) {
return newError(C.nc_close(C.int(ds)))
}
// EndDef leaves define mode and enters data mode, so variable data
// can be read or written. Calling this method is not required
// for netCDF-4 files.
func (ds Dataset) EndDef() (err error) {
return newError(C.nc_enddef(C.int(ds)))
}
// NVars returns the number of variables defined for dataset f.
func (ds Dataset) NVars() (n int, err error) {
var cn C.int
err = newError(C.nc_inq_nvars(C.int(ds), &cn))
n = int(cn)
return
}
// NAttrs returns the number of global attributes defined for dataset f.
func (ds Dataset) NAttrs() (n int, err error) {
var cn C.int
err = newError(C.nc_inq_natts(C.int(ds), &cn))
n = int(cn)
return
}
// Version returns a string identifying the version of the netCDF library,
// and when it was built.
func Version() string {
return C.GoString(C.nc_inq_libvers())
}