更改python中NetCDF文件的维度和值

发布于 2025-02-12 23:43:49 字数 985 浏览 0 评论 0原文

我正在尝试更改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文件中?

你能给我一些建议吗?

I'm trying to change the dimension of values in netcdf file.

First I read a netcdf file and interpolated the data.

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 ?

Using the interpolation, I converted the values of dimension A to dimension B.

Let say the original dimension A is 100 and interpolated dimension B is 200.

After the changing dimension, how can I save this values and dimension into netcdf file?

Could you please give me some advise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2025-02-19 23:43:49

您可以打开NETCDF文件以进行编辑。请参阅:
https://unididata.github.ioio oio oio of创建popeningclating-a-netcdf-file

而不是而不是:

data = netcdf4.Dataset(file)

尝试:

data = netCDF4.Dataset(file,'r+',clobber=True).

You can open a NetCDF file for editing in place. See:
https://unidata.github.io/netcdf4-python/#creatingopeningclosing-a-netcdf-file

Rather than:

data = netcdf4.Dataset(file)

Try:

data = netCDF4.Dataset(file,'r+',clobber=True).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文