首页 文章

高分辨率再分析数据

提问于
浏览
1

当我从netCDF文件重新分析(可变压力(SLP),01/01/2014)中提取数据时,数据的分辨率非常高(9km网格),这使得生成的图像非常嘈杂 . 我想将数据放入较低分辨率的网格(例如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 points = _ndim_coords_from_arrays(points)
文件"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上具有不兼容的尺寸 .

变量的维度是:
lon:(144,)
lat:(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有点:

原始尺寸变量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.82758621 77.5862069
...
-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 import griddata import numpy as np import matplotlib.pyplot as 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)

2 回答

  • 3

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

  • 0

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

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

    拥抱,卡洛斯 .

相关问题