【发布时间】:2015-12-25 02:15:24
【问题描述】:
当我从 netCDF 文件重新分析(可变压力 (SLP),2014 年 1 月 1 日)中提取数据时,数据的分辨率非常高(9 公里网格),这使得生成的图像非常嘈杂。我想将数据放入较低分辨率的网格中(例如 1 度)。我正在尝试使用 meshgrid 和 gridata 功能,但缺乏经验无法使其工作。有谁知道如何解决?谢谢。
from netCDF4 import Dataset
import numpy as np
from scipy.interpolate import griddata
file = Dataset('slp_2014_01_01.nc', 'r')
# Printing variables
print ' '
print ' '
print '----------------------------------------------------------'
for i,variable in enumerate(file.variables):
print ' '+str(i),variable
if i == 2:
current_variable = variable
print ' '
print 'Variable: ', current_variable.upper()
print 'File name: ', file_name
lat = file.variables['lat'][:]
lon = file.variables['lon'][:]
slp = file.variables['slp'][:]
lon_i = np.linspace(lon[0], lon[len(REANALYSIS_lon)-1], num=len(lon)*2, endpoint=True, retstep=False)
lat_i = np.linspace(lat[0], lat[len(lat)-1], num=len(lat)*2, endpoint=True, retstep=False)
lon_grid, lat_grid = np.meshgrid(lon_i,lat_i)
temp_slp = np.asarray(slp).squeeze()
new_slp = temp_slp.reshape(temp_slp.size)
slp_grid = griddata((lon, lat), new_slp, (lon_grid, lat_grid),method='cubic')
如前所述,我尝试使用 meshgrid 和 datagrid 函数,但产生以下错误:
Traceback(最近一次调用最后一次):
文件“REANALYSIS_LOCAL.py”,第 346 行,在
经度、纬度、时间、var、variavel_atual=netCDF_builder_local(caminho_netcdf_local、nome_arquivo、dt)
文件“REANALYSIS_LOCAL.py”,第 143 行,在 netCDF_builder_local
slp_grid = griddata((lon, lat), new_slp, (lon_grid, lat_grid),method='cubic')
文件“/home/carlos/anaconda/lib/python2.7/site-packages/scipy/interpolate/ndgriddata.py”,第 182 行,在 griddata
点 = _ndim_coords_from_arrays(点)
文件“interpnd.pyx”,第 176 行,在 scipy.interpolate.interpnd._ndim_coords_from_arrays (scipy/interpolate/interpnd.c:4064)
文件“/home/carlos/anaconda/lib/python2.7/site-packages/numpy/lib/stride_tricks.py”,第 101 行,在 broadcast_arrays
“轴 %r 上的尺寸不兼容。” %(轴,))
ValueError:形状不匹配:两个或多个数组在轴 0 上的尺寸不兼容。
变量的维度是:
朗:(144,)
纬度:(73,)
lon_i: (288,)
lat_i: (146,)
lon_grid: (146, 288)
lat_grid: (146, 288)
new_slp: (10512,)
new_slp 中的值为: new_slp: [102485. 102485. 102485. ..., 100710. 100710. 100710.]
目的是增加变量(lon、lat 和 slp)中的值,因为再分析分辨率更高。然后,分辨率可能是最详细的(更多点)。
例如:变量lat有分:
原始维度变量 lat: (73,)
纬度: [ 90. 87.5 85. 82.5 80. 77.5 75. 72.5 70. 67.5 65. 62.5
60. 57.5 55. 52.5 50. 47.5 45. 42.5 40. 37.5 35. 32.5
30. 27.5 25. 22.5 20. 17.5 15. 12.5 10. 7.5 5. 2.5
0. -2.5 -5。 -7.5 -10。 -12.5 -15。 -17.5 -20。 -22.5 -25。 -27.5
-30。 -32.5 -35。 -37.5 -40。 -42.5 -45。 -47.5 -50。 -52.5 -55。 -57.5
-60。 -62.5 -65。 -67.5 -70。 -72.5 -75。 -77.5 -80。 -82.5 -85。 -87.5
-90。 ]
当我定义代码行时: lat_i = np.linspace(lat[0], lat[len(lat)-1], num=len(lat)*2, endpoint=True, retstep=False) 我加倍lat 变量 la_i(146,) 的值
纬度_i: [ 90. 88.75862069 87.51724138 86.27586207 85.03448276 83.79310345 82.55172414 81.31034483 80.06896552 78.8272069621 77.8> ...
-78.82758621 -80.06896552 -81.31034483 -82.55172414 -83.79310345 -85.03448276 -86.27586207 -87.51724138 -88.75862069 -90。 ]
我需要的想法与此代码中的相同,其中 x 是 lon,y 是 lat,slp 是 z。
从 scipy.interpolate 导入网格数据
将 numpy 导入为 np
将 matplotlib.pyplot 导入为 plt
x=np.linspace(1.,10.,20)
y=np.linspace(1.,10.,20)
z=z = np.random.random(20)
xi=np.linspace(1.,10.,40)
yi=np.linspace(1.,10.,40)
X,Y= np.meshgrid(xi,yi)
Z = griddata((x, y), z, (X, Y),method='nearest')
plt.contourf(X,Y,Z)
【问题讨论】:
标签: python-2.7 numpy scipy netcdf