[英]Change dimension and values of netcdf file in Python
我正在尝试更改 netcdf 文件中值的维度。
首先,我读取了一个 netcdf 文件并插入了数据。
import numpy as np
import netCDF4
from scipy.interpolate import interp1d
def interpolation(a,b,c):
f = interp1d(a,b,kind='linear')
return f(c)
file = 'directory/test.nc'
data = netcdf4.Dataset(file)
lon = data.variables['lon'] # size = 10
lat = data.variables['lat'] # size = 10
lev = data.variables['lev'] # size = 100
values = data.variables['values'] # size = (100,10,10)
new_lev = np.linspace(0,1,200) # new vertical grid size = 200
new_values = np.full(len(new_lev), len(lat), len(lon)) # size = (200,10,10)
### interpolation ###
for loop_lat in range(len(lat)):
for loop_lon in range(len(lon)):
new_values[:, loop_lat, loop_lon] = interpolation(lev, values[:,loop_lat,loop_lon], new_lev)
## how can I save these new_lev and new_values in the netcdf file ?
使用插值,我将维度 A 的值转换为维度 B。
假设原始维度 A 为 100,插值维度 B 为 200。
更改维度后,如何将此值和维度保存到 netcdf 文件中?
你能给我一些建议吗?
您可以打开 NetCDF 文件进行就地编辑。 看:
https://unidata.github.io/netcdf4-python/#creatingopeningclosing-a-netcdf-file
而不是:
data = netcdf4.Dataset(file)
尝试:
data = netCDF4.Dataset(file,'r+',clobber=True).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.