高分辨率再分析数据

hight resolution Reanalysis data

当我从 netCDF 文件再分析(可变压力 (SLP),01/01/2014)中提取数据时,数据的分辨率非常高(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 函数,但产生了以下错误:

回溯(最近调用最后): 文件 "REANALYSIS_LOCAL.py",第 346 行,在
中 lon,lat,time,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 中 "incompatible dimensions on axis %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)的值,因为 Reanalysis 分辨率更高。然后,解决方案可能最详细(更多点)。

例如:变量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,)

lat _i: [ 90. 88.75862069 87.51724138 86.27586207 85.03448276 83.79310345 82.55172414 81.31034483 80.06896552 78.8275.8621 78.8275.8621[24] 70 ...
-78.82758621 -80.06896552 -81.31034483 -82.55172414 -83.79310345 -85.03448276 -86.27586207 -87.51724138 -88.75862069 -90。 ]

我需要的想法在这段代码中可用,其中 x 是经度,y 是纬度,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)

根据您的最终目的,您可以使用 cdo 重新网格化整个文件

cdo remapbil,r360x180 infile outfile

或者像这样绘制原始文件中的每隔一个或第三个值:

plt.pcolormesh(lon[::2,::2],lat[::2,::2],var1[::2,::2])

您显示的错误消息只是说维度不多,只需在错误出现之前打印变量的形状并尝试使其正常工作。

为什么您的代码不起作用? 您选择的方法需要输入坐标作为数据点的 lon,lat 对,而不是网格坐标。如果您有形状为 10000 的数据点,则您的坐标必须具有形状 (10000,2),而不是 (100,100)。 但是由于 griddata 适用于非结构化数据,因此对于您的目的而言效率不高,我建议使用 scipy.interpolate.RegularGridInterpolator

之类的东西

但是无论如何,如果您需要多次使用插值数据,我建议使用 cdo 创建新的 netCDF 文件并处理它们,而不是每次都插值数据您 运行 您的脚本。

感谢您的帮助。真的,我的问题是关于尺寸的。我正在学习使用海洋学数据。所以,我用这段代码解决了这个问题。

lonbounds = [25,59]
latbounds = [-10,-33]

#longitude lower and upper index
lonli = np.argmin(np.abs(lon - lonbounds[0]))
lonui = np.argmin(np.abs(lon - lonbounds[1]))  

#latitude lower and upper index
latli = np.argmin(np.abs(lat - latbounds[0]))
latui = np.argmin(np.abs(lat - latbounds[1])) 

#limiting of the interest region/data
lon_f = file.variables['lon'][lonli:lonui]  
lat_f = file.variables['lat'][latli:latui] 
slp_f = file.variables['slp'][0,latli:latui,lonli:lonui] 

#creating a matrix with the filtered data (area to be searched) for use in gridData function of python
lon_f_grid, lat_f_grid = np.meshgrid(lon_f,lat_f)

#adjusting the data (size 1) for use in gridData function of python
lon_f1 = lon_f_grid.reshape(lon_f_grid.size)
lat_f1 = lat_f_grid.reshape(lat_f_grid.size)
slp_f1 = slp_f.reshape(slp_f.size)

#increasing the resolution of data (1000 points) of longitude and latitude for the data to be more refined
lon_r = np.linspace(lon_f[0], lon_f[len(lon_f)-1], num=1000, endpoint=True, retstep=False)
lat_r = np.linspace(lat_f[0], lat_f[len(lat_f)-1], num=1000, endpoint=True, retstep=False)

#creating a matrix with the filtered data (area to be searched) and higher resolution for use in gridData function of python
lon_r_grid, lat_r_grid = np.meshgrid(lon_r,lat_r)

#applying gridata that can be generated since pressure (SLP) with higher resolution.
slp_r = griddata((lon_f1,lat_f1),slp_f1,(lon_r_grid,lat_r_grid),method='cubic')

拥抱, 卡洛斯