【问题标题】:hight resolution Reanalysis data高分辨率再分析数据
【发布时间】: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


    【解决方案1】:

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

    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')
    

    拥抱, 卡洛斯。

    【讨论】:

      【解决方案2】:

      根据您的最终目的,您可以使用 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 文件并处理它们,而不是每次运行脚本时都插值数据。

      【讨论】:

      • 谢谢你的回答......但我添加了更多关于我的目的/我需要做什么的细节。
      • 好的,首先你说你想要 1 度分辨率而不是 9 公里?但是随后您创建了新坐标 (lon_i, lat_i),其大小是初始数组的两倍,从而获得“更精细”的分辨率?!您仍然可以使用专门为此目的开发的 cdo 来执行此操作。
      • 我编辑了我的第一个答案以解释为什么您的代码似乎不起作用。
      猜你喜欢
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 2012-12-11
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多