我有一个netcdf文件,其中几个值

import netCDF4

dset      = netCDF4.Dataset('test.nc')
dset[dset.variables['var'] < 0] = -1

最佳答案

如果要将数据保留在netCDF变量对象中,这应该可以工作:

import netCDF4

dset = netCDF4.Dataset('test.nc', 'r+')

dset['var'][:][dset['var'][:] < 0] = -1

dset.close() # if you want to write the variable back to disk

如果您不想写回磁盘,请继续获取numpy数组并对其切片/分配:
data = dset['sea_ice_cover'][:]  # data is a numpy array
data[data < 0] = -1

关于python - Python : Replacing Values in netcdf file using netCDF4,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31865410/

10-12 05:22