Skip to content
Extraits de code Groupes Projets
Figure_3.py 1,76 ko
Newer Older
  • Learn to ignore specific revisions
  • Pierre-Yves Barriat's avatar
    Pierre-Yves Barriat a validé
    #
    # Input data
    #
    # We are going to use a netCDF file for testing the following code. 
    # The test file islocated in a dedicated online repository:
    #    https://nextcloud.cism.ucl.ac.be/s/H7XFSi8J4E8dnRK
    #
    # Download the netCDF prmsl.2000.nc : 
    #    a classic (regular grid) pressure file 
    #
    
    from netCDF4 import Dataset
    import numpy as np
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature
    from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
    from cartopy.util import add_cyclic_point
    import matplotlib.pyplot as plt
    import matplotlib.ticker as mticker
    
    my_example_nc_file = 'prmsl.2000.nc'
    fh = Dataset(my_example_nc_file, mode='r')
    
    lons = fh.variables['lon'][:]
    lats = fh.variables['lat'][:]
    times = fh.variables['time'][:]
    times_unit = fh.variables['time'].units
    var = fh.variables['prmsl']
    var_units = fh.variables['prmsl'].units
    
    #fig = plt.subplots(figsize=(8, 6), dpi=102)
    plt.axis('off')
    
    ax = plt.axes(projection=ccrs.Robinson(central_longitude=10.0))
    ax.coastlines()
    
    # Plot Data
    data = np.squeeze(var[1,:,:])
    #cs = ax.pcolormesh(lons, lats, data, transform=ccrs.PlateCarree(), shading='auto')
    lon = np.arange(0, 360, 2)
    data, lon = add_cyclic_point(data, coord=lon)
    cs = ax.contourf(lon, lats, data, transform=ccrs.PlateCarree(), transform_first=False)
    
    # Add Grid Lines
    gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=0.5, color="grey")
    gl.xlocator = mticker.FixedLocator(np.arange(-170,191,36))
    gl.ylocator = mticker.FixedLocator(np.arange(-90,90,15))
    gl.rotate_labels = False
    gl.top_labels = False
    gl.xformatter = LONGITUDE_FORMATTER
    gl.yformatter = LATITUDE_FORMATTER
    
    # Add Colorbar
    cbar = plt.colorbar(cs,orientation="horizontal")
    cbar.set_label(var_units)
    
    # Add Title
    plt.title('Daily Pressure at Mean Sea Level')
    
    plt.show()