Esempio n. 1
0
def dT2m_pcolormesh(ax,
                    x,
                    y,
                    z,
                    cmap=utl.linearized_ncl_cmap('hotcold_18lev'),
                    vmin=-16,
                    vmax=16,
                    transform=ccrs.PlateCarree(),
                    **kwargs):
    img = ax.pcolormesh(x,
                        y,
                        z,
                        cmap=cmap,
                        transform=transform,
                        vmin=vmin,
                        vmax=vmax,
                        **kwargs)
    return img
Esempio n. 2
0
def ulj_contourf(ax,
                 x,
                 y,
                 z,
                 cmap=utl.linearized_ncl_cmap('MPL_Oranges'),
                 levels=np.arange(30, 80, 2),
                 alpha=0.8,
                 transform=ccrs.PlateCarree(),
                 **kwargs):
    z[z < levels[0]] = np.nan
    img = ax.contourf(x,
                      y,
                      z,
                      cmap=cmap,
                      levels=levels,
                      transform=transform,
                      extend='max',
                      alpha=alpha,
                      **kwargs)
    return img
Esempio n. 3
0
def dT2m_contour(ax,
                 x,
                 y,
                 z,
                 cmap=utl.linearized_ncl_cmap('BlRe'),
                 levels=[-16, -12, -10, -8, -6, 6, 8, 10, 12, 16],
                 vmin=-16,
                 vmax=16,
                 alpha=0.5,
                 transform=ccrs.PlateCarree(),
                 **kwargs):
    img = ax.contour(x,
                     y,
                     z,
                     levels=levels,
                     cmap=cmap,
                     transform=transform,
                     alpha=alpha,
                     vmin=vmin,
                     vmax=vmax,
                     **kwargs)
    return img
Esempio n. 4
0
def draw_dT_2m(dT_2m=None,
               T_type='Tmx',
               map_extent=(50, 150, 0, 65),
               regrid_shape=20,
               add_china=True,
               city=True,
               south_China_sea=True,
               output_dir=None,
               Global=False):
    # set font
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 步骤一(替换sans-serif字体)
    plt.rcParams['axes.unicode_minus'] = False  # 步骤二(解决坐标轴负数的负号显示问题)

    # set figure
    fig = plt.figure(figsize=(16, 9))

    if (Global == True):
        plotcrs = ccrs.Robinson(central_longitude=115.)
    else:
        plotcrs = ccrs.AlbersEqualArea(
            central_latitude=(map_extent[2] + map_extent[3]) / 2.,
            central_longitude=(map_extent[0] + map_extent[1]) / 2.,
            standard_parallels=[30., 60.])

    ax = plt.axes([0.01, 0.1, .98, .84], projection=plotcrs)
    datacrs = ccrs.PlateCarree()
    map_extent2 = utl.adjust_map_ratio(ax,
                                       map_extent=map_extent,
                                       datacrs=datacrs)

    # define return plots
    plots = {}
    if dT_2m is not None:
        x, y = np.meshgrid(dT_2m['lon'], dT_2m['lat'])
        z = np.squeeze(dT_2m['data'])
        cmap = utl.linearized_ncl_cmap('hotcold_18lev')
        plots['T_2m'] = ax.pcolormesh(x,
                                      y,
                                      z,
                                      cmap=cmap,
                                      zorder=1,
                                      transform=datacrs,
                                      alpha=1,
                                      vmin=-16,
                                      vmax=16)

        z = gaussian_filter(z, 5)
        cmap = utl.linearized_ncl_cmap('BlRe')
        clevs = [-16, -12, -10, -8, -6, 6, 8, 10, 12, 16]
        plots['T_2m_contour'] = ax.contour(x,
                                           y,
                                           z,
                                           levels=clevs,
                                           cmap=cmap,
                                           zorder=3,
                                           transform=datacrs,
                                           alpha=0.5,
                                           vmin=-16,
                                           vmax=16)
        clev_colors = []
        for iclev in clevs:
            per_color = utl.get_part_clev_and_cmap(cmap=cmap,
                                                   clev_range=[-16, 16],
                                                   clev_slt=iclev)
            clev_colors.append(np.squeeze(per_color[:]))

        cl = plt.clabel(plots['T_2m_contour'],
                        inline=1,
                        fontsize=15,
                        fmt='%i',
                        colors=clev_colors)
        for t in cl:
            t.set_path_effects([
                path_effects.Stroke(linewidth=3, foreground='#D9D9D9'),
                path_effects.Normal()
            ])
        '''
        z=gaussian_filter(z,5)
        plots['T_2m_zero'] = ax.contour(
            x, y, z, levels=[0], colors='#232B99',
            linewidths=2, transform=datacrs, zorder=2)
        cl_zero=plt.clabel(plots['T_2m_zero'], inline=1, fontsize=15, fmt='%i',colors='#232B99')
        for t in cl_zero:
            t.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'),
                       path_effects.Normal()])

        plots['T_2m_35'] = ax.contour(
            x, y, z, levels=[35,37,40], colors=['#FF8F00','#FF6200','#FF0000'],
            linewidths=2, transform=datacrs, zorder=2)
        cl_35=plt.clabel(plots['T_2m_35'], inline=1, fontsize=15, fmt='%i',colors='#FF0000')
        for t in cl_35:
            t.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'),
                       path_effects.Normal()])
        '''

    plt.title('[' + dT_2m.attrs['model'] + ']' + ' ' + dT_2m.attrs['title'],
              loc='left',
              fontsize=30)

    ax.add_feature(cfeature.OCEAN)
    utl.add_china_map_2cartopy_public(ax,
                                      name='coastline',
                                      edgecolor='gray',
                                      lw=0.8,
                                      zorder=4,
                                      alpha=0.5)
    if add_china:
        utl.add_china_map_2cartopy_public(ax,
                                          name='province',
                                          edgecolor='gray',
                                          lw=0.5,
                                          zorder=4)
        utl.add_china_map_2cartopy_public(ax,
                                          name='nation',
                                          edgecolor='black',
                                          lw=0.8,
                                          zorder=4)
        utl.add_china_map_2cartopy_public(ax,
                                          name='river',
                                          edgecolor='#74b9ff',
                                          lw=0.8,
                                          zorder=4,
                                          alpha=0.5)

    # grid lines
    gl = ax.gridlines(crs=datacrs,
                      linewidth=2,
                      color='gray',
                      alpha=0.5,
                      linestyle='--',
                      zorder=5)
    gl.xlocator = mpl.ticker.FixedLocator(np.arange(0, 360, 15))
    gl.ylocator = mpl.ticker.FixedLocator(np.arange(-90, 90, 15))

    utl.add_cartopy_background(ax, name='RD')

    l, b, w, h = ax.get_position().bounds

    #forecast information
    bax = plt.axes([l, b + h - 0.1, .25, .1], facecolor='#FFFFFFCC')
    bax.set_yticks([])
    bax.set_xticks([])
    bax.axis([0, 10, 0, 10])

    initTime = pd.to_datetime(
        str(dT_2m.coords['forecast_reference_time'].values)).replace(
            tzinfo=None).to_pydatetime()
    fcst_time = initTime + timedelta(
        hours=dT_2m.coords['forecast_period'].values[0])
    #发布时间
    if (sys.platform[0:3] == 'lin'):
        locale.setlocale(locale.LC_CTYPE, 'zh_CN.utf8')
    if (sys.platform[0:3] == 'win'):
        locale.setlocale(locale.LC_CTYPE, 'chinese')
    plt.text(2.5, 7.5, '起报时间: ' + initTime.strftime("%Y年%m月%d日%H时"), size=15)
    plt.text(2.5, 5, '预报时间: ' + fcst_time.strftime("%Y年%m月%d日%H时"), size=15)
    plt.text(2.5,
             2.5,
             '预报时效: ' + str(int(dT_2m.coords['forecast_period'].values[0])) +
             '小时',
             size=15)
    plt.text(2.5, 0.5, 'www.nmc.cn', size=15)

    # add color bar
    if (dT_2m != None):
        cax = plt.axes([l, b - 0.04, w, .02])
        cb = plt.colorbar(
            plots['T_2m'],
            cax=cax,
            orientation='horizontal',
            extend='both',
            ticks=[-16, -12, -10, -8, -6, -4, 0, 4, 6, 8, 10, 12, 16])
        cb.ax.tick_params(labelsize='x-large')
        cb.set_label(u'°C', size=20)

    # add south China sea
    if south_China_sea:
        utl.add_south_China_sea(pos=[l + w - 0.091, b, .1, .2])

    small_city = False
    #if(map_extent2[1]-map_extent2[0] < 25):
    #    small_city=True
    #if city:
    utl.add_city_and_number_on_map(ax,
                                   data=dT_2m,
                                   map_extent=map_extent2,
                                   transform=datacrs,
                                   zorder=6,
                                   size=13,
                                   small_city=small_city)

    utl.add_logo_extra_in_axes(pos=[l - 0.02, b + h - 0.1, .1, .1],
                               which='nmc',
                               size='Xlarge')

    # show figure
    if (output_dir != None):
        plt.savefig(output_dir + '最低温度_预报_' + '起报时间_' +
                    initTime.strftime("%Y年%m月%d日%H时") + '预报时效_' +
                    str(dT_2m.coords['forecast_period'].values[0]) + '小时' +
                    '.png',
                    dpi=200,
                    bbox_inches='tight')
        plt.close()
    if (output_dir == None):
        plt.show()