【问题标题】:Change dimension name in netcdf file with python使用python更改netcdf文件中的维度名称
【发布时间】:2015-02-05 14:08:27
【问题描述】:

您好,我想编辑 NetCDF 文件中的一些信息,举个例子,假设您有一个带有下一个信息的文件的 ncdump:

NetCDF dimension information:
 Name: lon
    size: 144
    type: dtype('float64')
 Name: lat
    size: 73
    type: dtype('float64')
 Name: time
    size: 29220
    type: dtype('float64')
NetCDF variable information:
 Name: rlut
    dimensions: (u'time', u'lat', u'lon')
    type: dtype('float32')

我想将 'lon' 更改为 'longitude'。我试过了:

from netCDF4 import Dataset
path="Here goes the file path"
f=Dataset(path,'r+')
f.renameDimension(u'lon',u'longitude')
f.close()

但在此之后,当我尝试再次读取文件以执行不同的操作时,文件不再工作。

任何帮助我都会感谢你。

【问题讨论】:

  • 你能澄清一下“再次阅读文件以做不同的事情”的意思吗?这是使用不同的软件吗?预计这种行为会像您尝试的那样工作,如果没有,那就是一个错误。在此处查看单元测试:code.google.com/p/netcdf4-python/source/browse/trunk/test/…(另外,当我尝试使用 ncdump 时,它对我来说效果很好)
  • 当我说“再次读取文件以执行不同的操作”时,我的意思是此过程后的文件已损坏,因此无法对文件执行任何操作。
  • 然后我会在 github.com/unidata/netcdf4-python/issues 打开一个问题,如果可以的话,将你的数据文件发布到某个地方,因为它应该像你尝试的那样工作,并且在这里为我工作。

标签: python netcdf


【解决方案1】:

尽管这是一篇相当老的帖子,但添加它可能仍然很有价值:

Ajaramillo 指定的方法在我的情况下不起作用,因为它缺少重命名变量名称,而不仅仅是维度名称。这对我有用:

from netCDF4 import Dataset
path="Here goes the file path"
f=Dataset(path,'r+')
f.renameDimension(u'lon',u'longitude')
f.renameVariable(u'lon',u'longitude')
f.renameDimension(u'lat',u'latitude')
f.renameVariable(u'lat',u'latitude')
f.close()

【讨论】:

    【解决方案2】:

    感谢 N1B4 对使用 NCO 的建议,它是处理和编辑 NetCDF 文件的一个很好的选择。

    我想在这里为任何有兴趣使用 python 和 netcdf4 库修改 NetCDF 文件的人发布我的解决方案草图。这个想法是创建一个新的 NetCDF 文件,从现有文件中导入信息。

    #First import the netcdf4 library
    from netCDF4 import Dataset  # http://code.google.com/p/netcdf4-python/
    
    # Read en existing NetCDF file and create a new one
    # f is going to be the existing NetCDF file from where we want to import data
    # and g is going to be the new file.
    
    f=Dataset('pathtoexistingfile','r') # r is for read only
    g=Dataset('name of the new file','w') # w if for creating a file
                                          # if the file already exists it  
                                          # file will be deleted 
    
    
    # To copy the global attributes of the netCDF file  
    
    for attname in f.ncattrs():
        setattr(g,attname,getattr(f,attname))
    
    # To copy the dimension of the netCDF file
    
    for dimname,dim in f.dimensions.iteritems():
           # if you want to make changes in the dimensions of the new file
           # you should add your own conditions here before the creation of the dimension.
            g.createDimension(dimname,len(dim))
    
    
    # To copy the variables of the netCDF file
    
    for varname,ncvar in f.variables.iteritems():
           # if you want to make changes in the variables of the new file
           # you should add your own conditions here before the creation of the variable.
           var = g.createVariable(varname,ncvar.dtype,ncvar.dimensions)
           #Proceed to copy the variable attributes
           for attname in ncvar.ncattrs():  
              setattr(var,attname,getattr(ncvar,attname))
           #Finally copy the variable data to the new created variable
           var[:] = ncvar[:]
    
    
    f.close()
    g.close()
    

    我希望这对你有用。

    【讨论】:

      【解决方案3】:

      我认为您还需要修改对重命名维度的所有引用。例如。您的变量 rlut,其维度为“lon”,已重命名为“longitude”。不确定这是否可以通过就地编辑来完成。您可能需要使用以下命令创建文件的新副本:

      createVariable('rlut', 'f4', ('time', 'lat', 'longitude')
      

      【讨论】:

      • 我也试过这个选项,但是当我使用文件时出现错误。我想这是我正在使用的特定 netcdf 文件的问题。
      【解决方案4】:

      如果你不需要使用Python,我推荐NCO的ncrename函数:http://nco.sourceforge.net/nco.html#ncrename-netCDF-Renamer

      ncrename -d lon,longitude sample_file.nc  
      

      【讨论】:

      • 我尝试了这个选项,但是当我在此之后读取文件时,我收到错误 RuntimeError: NetCDF: Invalid dimension ID or name
      • 奇怪,有没有办法分享netcdf文件?
      • 我解决了创建新 NetCDF 文件并从旧文件导入属性的问题。谢谢
      猜你喜欢
      • 1970-01-01
      • 2021-03-23
      • 2013-09-22
      • 2015-03-03
      • 1970-01-01
      • 2013-12-11
      • 2022-01-03
      • 2017-02-19
      • 2017-11-17
      相关资源
      最近更新 更多