Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python/netcdf4中的字符串处理_Python_Netcdf - Fatal编程技术网

python/netcdf4中的字符串处理

python/netcdf4中的字符串处理,python,netcdf,Python,Netcdf,我是netCDF4新手,但我有以下几点: import netCDF4 nc = netCDF4.Dataset('test.nc','w',format='NETCDF4_CLASSIC') lat = nc.createVariable('lat','d') lon = nc.createVariable('lon','d') place = nc.createVariable('place','c') lat[:]=17.002 lon[:]=-81.501 #place[:]='test

我是netCDF4新手,但我有以下几点:

import netCDF4
nc = netCDF4.Dataset('test.nc','w',format='NETCDF4_CLASSIC')
lat = nc.createVariable('lat','d')
lon = nc.createVariable('lon','d')
place = nc.createVariable('place','c')
lat[:]=17.002
lon[:]=-81.501
#place[:]='test'
nc.close()
我想为变量“place”指定一个大于1个字符的值。如何在不使用属性(例如place.name='test')的情况下执行此操作?

关键是使用函数将字符串数组转换为字符数组

import netCDF4
import numpy as np

str_out = netCDF4.stringtochar(np.array(['test'], 'S4'))

nc = netCDF4.Dataset('./test.nc', 'w', format='NETCDF4_CLASSIC')
nc.createDimension('lat', 1)
nc.createDimension('lon', 1)
nc.createDimension('nchar', 4)

lat = nc.createVariable('lat', 'f4', ('lat',))
lon = nc.createVariable('lon', 'f4', ('lon',))
place = nc.createVariable('place', 'S1', ('nchar'))

lat[:] = 17.002
lon[:] = -81.501
place[:] = str_out

nc.close()
您可以使用

>>> ncks test.nc 
...
lat[0]=17.002 
lon[0]=-81.501 
nchar[0] place[0--3]='test'
请注意,通过删除“NETCDF4_CLASSIC”的格式,您可以通过另一种方式完成此操作:

str_out = np.array(['test'], dtype='object')

nc = netCDF4.Dataset('./test.nc', 'w')
nc.createDimension('lat', 1)
nc.createDimension('lon', 1)
nc.createDimension('str_dim', 1)

lat = nc.createVariable('lat', 'f4', ('lat',))
lon = nc.createVariable('lon', 'f4', ('lon',))
# Now we can use the data type of 'str' 
place = nc.createVariable('place', str, ('str_dim',))

lat[:] = 17.002
lon[:] = -81.501
place[:] = str_out

nc.close()

>>> ncks test.nc 
...
lat[0]=17.002 
lon[0]=-81.501 
str_dim[0] place[0]=test 

对于较长的文本,根据N1B4的有用答案,您可以执行以下操作:

from __future__ import print_function                                                                                                                        
import netCDF4 as nc
import numpy as np

message = """This message is spread over two lines.
I won't even bother counting the characters."""

fname = './test.nc'

# Write the message
with nc.Dataset(fname, 'w') as ds:

    ds.createDimension('nchar', len(message))
    data = ds.createVariable('message', 'S1', ('nchar'))
    data[:] = nc.stringtochar(np.array([message], 'S'))

# Read the message
with nc.Dataset(fname, 'r') as ds:

    reconstructed = ds.variables['message'][...].tostring().decode()

print(reconstructed)