美文网首页花间独酌
如何在Python中阅读和绘制NetCDF MERRA-2数据

如何在Python中阅读和绘制NetCDF MERRA-2数据

作者: 榴莲气象 | 来源:发表于2019-06-06 15:58 被阅读30次

    本文将展示如何使用Python从现代时代回顾性研究和应用版本2(MERRA-2)中读取和绘制NetCDF4数据。

    例:

    示例数据: MERRA-2 2010年1月每月0.5 x 0.625度2米气温(M2TMNXSLV_V5.12.4)。

    **示例图片: **

    merra2_t2m:merra2_t2m

    预计完成以下程序的时间: 20分钟

    先决条件:

    Python和免费软件包:numpymatplotlibbasemapnetCDF4。Matplotlib和底图仅用于绘图。该脚本使用Python 2.7进行了测试。

    程序:

    用户必须注册Earthdata才能访问数据。要注册,请按照以下步骤操作:data-access

    1. 注册Earthdata后,请访问https://disc.gsfc.nasa.gov/

    2. 搜索“M2TMNXSLV_5.12.4”并单击数据集链接。

    3. 进入数据集页面后,可以通过“数据访问”框中的多种方式访问​​数据。对于此HowTo,我们将通过单击“在线存档”使用https服务。

    4. 运行howto_readMERRA2netCDF_python.py脚本

    howto_readMERRA2netCDF_python.py
    
    # this script will read-in and plot 3-dimensional NetCDF data in python
    from netCDF4 import Dataset
    
    # Read in NetCDF4 file. Assign directory path if necessary.
    data = Dataset('MERRA2_300.tavgM_2d_slv_Nx.201001.nc4', mode='r')
    
    # Uncomment 'print data' line to print MERRA2 metadata. This line will print attribute and variable information.
    # from the 'variables(dimensions)' list, choose which variable(s) to read in below.
    # print data
    
    
    # Read in 'T2M' 2-meter air temperature variable. Varible names can be printed by uncommenting 'print data' above.
    lons = data.variables['lon'][:]
    lats = data.variables['lat'][:]
    T2M = data.variables['T2M'][:,:,:]
    
    # If using MERRA-2 data with multiple time indices, line 19 will subset the first time dimension.
    # Changing T2M[0,:,:] to T2M[10,:,:] will subset to the 11th time index.
    T2M = T2M[0,:,:]
    
    #========= Start Plotting Data ===============================================
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.cm as cm
    from mpl_toolkits.basemap import Basemap
    
    map = Basemap(resolution='l', projection='eck4', lat_0=0, lon_0=0)
    
    lon, lat = np.meshgrid(lons, lats)
    xi, yi = map(lon, lat)
    
    
    # Plot Data
    cs = map.pcolor(xi,yi,np.squeeze(T2M), vmin=np.min(T2M), vmax=np.max(T2M), cmap=cm.jet)
    cs.set_edgecolor('face')
    
    # Add Grid Lines
    map.drawparallels(np.arange(-90., 90., 15.), labels=[1,0,0,0], fontsize=5)
    map.drawmeridians(np.arange(-180., 180., 30.), labels=[0,0,0,1], fontsize=4)
    
    # Add Coastlines, States, and Country Boundaries
    map.drawcoastlines()
    map.drawstates()
    map.drawcountries()
    
    # Add Colorbar
    cbar = map.colorbar(cs, location='bottom', pad="10%")
    cbar.set_label('K')
    cbar.ax.tick_params(labelsize=10)
    
    # Add Title
    plt.title('MERRA-2 2-meter air temperature (2010-01)')
    
    # Save figure as PDF
    plt.savefig('MERRA2_2m_airTemp_TEST.pdf', format='pdf', dpi=360)
    

    相关文章

      网友评论

        本文标题:如何在Python中阅读和绘制NetCDF MERRA-2数据

        本文链接:https://www.haomeiwen.com/subject/uuduxctx.html