简体   繁体   中英

r ncdf4 cannot read character variable from netcdf

I am trying to read variable from a netcdf file that contain characters

library(ncdf4)

NCFile <- nc_open('MD02_2575.age')
ncvar_get(NCFile, 'Label')

I expect

'KIA33119' 'KIA24904' 'KIA33120' 'KIA24905' 'KIA33121' 'KIA24906' 'KIA25875' 'KIA24907' 'KIA24903' 'User    ' 'User    ' 'User    ' 'User    ' 'User    '

based on what I get when I read the file with Matlab, but with RI get

[1] "KKKKKKKKKUUUUU" "IIIIIIIIIsssss" "AAAAAAAAAeeeee" "323232222rrrrr" "343434544     " "191919899     " "102020700     " "940516573     "

I would like to use R. This looks like an encoding issue, does anyone have a suggestion how to get read in the variables?

Label is a 2D array with characters;

char Label(Label_Characters, Length);

Also ncdump is having difficulties in transforming this into a list of strings, for example: ncdump -v Label MD02_2575.age also gives the "incorrect" results:

 Label =
  "KKKKKKKKKUUUUU",
  "IIIIIIIIIsssss",
  "AAAAAAAAAeeeee",
  "323232222rrrrr",
  "343434544     ",
  "191919899     ",
  "102020700     ",
  "940516573     " ;

I wrote "incorrect", because from the NetCDF file itself it is not clear that (1) the individual characters need to be concatenated into strings, and (2) if that's the case, along which dimension the concatenation should be performed. Honestly, I'm surprised that ncdump , R and Matlab do this?

Just to illustrate the problem (using Python):

import netCDF4 as nc4

f = nc4.Dataset('MD02_2575.age')
v = f.variables['Label'][:,:]

label1 = v[:,0].tostring()  # Correct
label2 = v[0,:].tostring()  # Same as `ncdump`, `R`, ..

print(label1, label2)

Results in: 'KIA33119' 'KKKKKKKKKUUUUU'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM