【问题标题】:String handling in python/netcdf4python/netcdf4中的字符串处理
【发布时间】:2016-09-01 23:16:51
【问题描述】:

我是 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')如何做到这一点?

【问题讨论】:

    标签: python netcdf


    【解决方案1】:

    关键是使用netCDF4.stringtochar函数将字符串数组转换为字符数组。

    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 
    

    【讨论】:

    • 如果你的 str 数据在 Pandas DataFrame 中,你可能需要place[:] = df['name'].to_numpy(),即将.to_numpy() 添加到 Pandas 源中。
    【解决方案2】:

    对于较长的文本,基于 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)
    

    【讨论】:

      猜你喜欢
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      相关资源
      最近更新 更多