Python:将函数应用于netCDF / numpy 3d数组中的每个条目

时间:2019-03-05 14:52:10

标签: python arrays numpy netcdf

https://stackoverflow.com/a/55001336/7474503的启发,我尝试在3d netCDF / numpy数组上应用一个函数。

/var/lib/mysql

很不幸,我收到一个 IndexError 索引不能是多维的

有人知道这可能是什么原因吗?我已经为from netCDF4 import Dataset import numpy as np my_example_nc_file = '/Users/Severin/Desktop/ecmwf_era5_17010100.nc' fh = Dataset(my_example_nc_file, mode='r') #fh becomes the file handle of the open netCDF file lons = fh.variables['lon'][:] lats = fh.variables['lat'][:] gph = fh.variables['GPH'][0,:,:,:] # Function to calculate the Pressure in hPa at point x/y at level z # p(z,j,i) = a(z) + b(z)*ps(j,i) # http://cfconventions.org/Data/cf-conventions/cf-conventions-1.0/build/apd.html # Assumption j,i are lat & lon (in this order) # lat=y=j & lon=x=i def calcP(z,x,y,a=a,b=b,ps=ps): p = (a[z]+b[z]*ps[x,y])/100 return p a = fh.variables['a'] b = fh.variables['b'] ps = fh.variables['ps'][0,:,:] p3d = np.fromfunction(calcP, (137,601,1200), a=a,b=b,ps=ps, dtype=float) fh.close() 函数尝试了不同形状的变量和变量的顺序。

有关我的变量的更多信息:

输出:

calcP

1 个答案:

答案 0 :(得分:1)

我认为您的ab应该是numpy数组,而不是netCDF4.Variable。 所以,

a = fh.variables['a'][:]
b = fh.variables['b'][:]

另外,我认为您可能需要在倒数第二行设置dtype=int