fhs / go-netcdf

Go binding for the netCDF C library.
MIT License
37 stars 12 forks source link

Reading string variable data #20

Open guydillen opened 3 years ago

guydillen commented 3 years ago

Hi,

Hw to read string variable data?

This is my cdl file:

`netcdf test { dimensions: station = 9 ; ... variables: string station_name(station) ; ... // global attributes: ... data:

station_name = "W1", "W2", "W3", "W4", "W5", "W6", "W7", "W8", "W9" ;

...`

How can I read the string data?

Thanks.

fhs commented 3 years ago

Var.ReadBytes doesn't work? Maybe we need to wrap nc_get_vara_string? PRs are welcome.

guydillen commented 3 years ago

How should I use Var.ReadBytes: it returns an error and takes a byte slice/array?

// ReadBytes 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) ReadBytes(data []byte) error {
    if err := okData(v, CHAR, len(data)); err != nil {
        return err
    }
    return newError(C.nc_get_var_text(C.int(v.ds), C.int(v.id), (*C.char)(unsafe.Pointer(&data[0]))))
}
fhs commented 3 years ago

I think you need to create a ReadStrings methods that uses C.nc_get_var_string.

func (v Var) ReadStrings(data []string) error
SergeiMelman commented 3 years ago

I think It would be good for Attr as well :

func (a Attr) ReadStrings(data []string) error