Beispiel #1
0
def carte_xy(sim,
             var,
             lev=None,
             rep0='./images/',
             continent=None,
             season='year',
             xlim=None,
             ylim=None,
             xticks=None,
             yticks=None,
             xxc=None,
             yyc=None):

    repout = '{0}/{1}/{2}avg/'.format(rep0, sim, season)
    if not (os.path.exists(repout)):
        os.makedirs(repout)

    if lev is None:
        varname = var
    else:
        varname = '{0}{1}'.format(var, int(lev))

    # Get plot details
    coef = cc.xy_plotdetails[varname]['coef']
    units = cc.xy_plotdetails[varname]['units']
    cmap = cc.xy_plotdetails[varname]['cmap']
    bounds = cc.xy_plotdetails[varname]['bounds']
    firstwhite = cc.xy_plotdetails[varname]['firstwhite']
    ext = cc.xy_plotdetails[varname]['ext']
    title = cc.xy_plotdetails[varname]['title']

    # Ouverture du fichier
    data_nc = getfile(sim, var)
    fnc = nc.Dataset(data_nc)

    # Lecture des variables
    lat = fnc.variables['lat'][:]
    lon = fnc.variables['lon'][:]

    for taxis in ['time', 'time_counter']:
        try:
            time = fnc.variables[taxis]
            break
        except:
            pass
    dates = nc.num2date(time[:], units=time.units, calendar=time.calendar)

    data = fnc.variables[var]
    if len(data.shape) == 4:
        levname = 'pstd'
        levaxis = fnc.variables[levname][:]
        data = np.squeeze(data[:, levaxis == lev * 100., :, :]) * coef
    else:
        data = data[:, :, :] * coef

    # shifting grid from 0-360 to -180-180
    datanew, lonnew = shiftgrid(180, data, lon)

    # average over the chosen season
    inds = [d.month in seasons[season] for d in dates]
    datanew = np.average(datanew[inds], axis=0)

    # Mise en place de la carte
    proj = ccrs.PlateCarree(central_longitude=0, globe=None)

    # prepare colormap
    cmaplist = [cmap(i) for i in range(cmap.N)]
    if firstwhite:
        cmaplist[0] = (1, 1, 1, 0)
    cmap = cmap.from_list('Custom cmap', cmaplist[:-20], len(cmaplist) - 20)
    norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

    # Plot
    fig = plt.figure(figsize=(10, 10))
    fig, ax = plt.subplots(figsize=(12, 10), subplot_kw=dict(projection=proj))

    if not (xlim is None):
        ax.set_xlim(xlim)
    if not (ylim is None):
        ax.set_ylim(ylim)

    if not (xticks is None) and not (yticks is None):
        gl = ax.gridlines(xlocs=xticks,
                          ylocs=yticks,
                          linestyle='--',
                          lw=1,
                          color='dimgrey',
                          draw_labels=True)
        gl.xlabels_top = False
        gl.xformatter = LONGITUDE_FORMATTER
        #gl.xlines = False
        gl.ylabels_right = False
        gl.yformatter = LATITUDE_FORMATTER
        #gl.ylines = False

    cs = ax.contourf(lonnew,
                     lat,
                     datanew,
                     bounds,
                     cmap=cmap,
                     norm=norm,
                     extend=ext,
                     transform=ccrs.PlateCarree())

    cbar = fig.colorbar(cs,
                        shrink=0.9,
                        orientation='horizontal',
                        pad=0.1,
                        extend=ext,
                        boundaries=bounds,
                        ticks=bounds,
                        label=units)

    if continent == 'real':
        ax.coastlines('50m')
        ax.add_feature(cf.BORDERS)
    elif continent == 'ideal':
        # Add idealized continent
        ax.plot(xxc, yyc, color='k', linewidth=2, transform=ccrs.PlateCarree())

    plt.title('{0} ({1}) - {2}'.format(title, units, season))

    plt.savefig('{0}/xy_{1}.png'.format(repout, varname))

    print('{0}/xy_{1}.png'.format(repout, varname))
    plt.close()
Beispiel #2
0
var='va'
sim='WA_10a'
seasons = {'year': range(1,13),
           'JJAS': [6,7,8,9],
           }
if sim=='LC_Sym':
    t='time'
else:
    t='time_counter'
if sim=='LC_Sym':
    lv='plev'
else:
    lv='pstd'
#Lecture du fichier
data_nc=getfile(sim,var)
file = nc.Dataset(data_nc)


bounds1 =[ i for i in np.arange(-5,6,1)]
unit='(m/s)'
conv=1
ext='both'

#Lecture des variables
lat = file.variables['lat'][:]
lon = file.variables['lon'][:]
#time = file.variables['time'][:]
lev= file.variables[str(lv)][:]
dates = nc.num2date(file[str(t)][:],units=(file[str(t)]).units,calendar=(file[str(t)]).calendar)
data = (shiftgrid(180,file.variables[str(var)][:,:,:,:]*conv,nc.Dataset(data_nc).variables['lon'][:],start=False,cyclic=360.0))[0]
Beispiel #3
0
var5 = 'va'
var6 = 'wap'
sim = 'Simu_Base'
cont = 'real'
seasons = {
    'year': range(1, 13),
    'JJAS': [6, 7, 8, 9],
}
if sim == 'Simu_Base':
    t = 'time'
else:
    t = 'time_counter'

#Lecture des fichiers

data_nc1 = getfile(sim, var1)
file1 = nc.Dataset(data_nc1)
data_nc2 = getfile(sim, var2)
file2 = nc.Dataset(data_nc2)
data_nc3 = getfile(sim, var3)
file3 = nc.Dataset(data_nc3)
data_nc4 = getfile(sim, var4)
file4 = nc.Dataset(data_nc4)
data_nc5 = getfile(sim, var5)
file5 = nc.Dataset(data_nc5)
data_nc6 = getfile(sim, var6)
file6 = nc.Dataset(data_nc6)

#Lecture des variables

conv = 86400