def draw_cumulated_precip( rain=None, map_extent=(50, 150, 0, 65), add_china=True,city=True,south_China_sea=True,add_background=True, output_dir=None,Global=False): initTime = pd.to_datetime(rain.attrs['initTime']).replace(tzinfo=None).to_pydatetime() fcstTime1 = initTime + timedelta(hours=int(rain.attrs['fhour1']-rain.attrs['t_gap'])) fcstTime2 = initTime + timedelta(hours=int(rain.attrs['fhour2'])) title = '[{}] {}到{}时效累积降水'.format(rain.attrs['model'],int(rain.attrs['fhour1']-rain.attrs['t_gap']),int(rain.attrs['fhour2'])) forcast_info = '起报时间: {0:%Y}年{0:%m}月{0:%d}日{0:%H}时\n起始时间: {1:%Y}年{1:%m}月{1:%d}日{1:%H}时\n终止时间: {2:%Y}年{2:%m}月{2:%d}日{2:%H}时\nwww.nmc.cn'.format(initTime, fcstTime1, fcstTime2) fig, ax, map_extent = GF.pallete_set.Horizontal_Pallete((18, 9),map_extent=map_extent, title=title, forcast_info=forcast_info,info_zorder=4, add_china=add_china, add_city=city, add_background=add_background, south_China_sea=south_China_sea) if rain is not None: x, y = np.meshgrid(rain['lon'], rain['lat']) z=np.squeeze(rain['data'].values) z[z<0.1]=np.nan cmap,norm=dk_ctables.cm_qpf_nws(atime=24) plots={} cmap.set_under(color=[0,0,0,0],alpha=0.0) plots['rain'] = ax.pcolormesh( x,y,z, norm=norm, cmap=cmap, zorder=1,transform=ccrs.PlateCarree(),alpha=0.5) l, b, w, h = ax.get_position().bounds # add color bar cax=plt.axes([l,b-0.04,w,.02]) cb = plt.colorbar(plots['rain'], cax=cax, orientation='horizontal') cb.ax.tick_params(labelsize='x-large') cb.set_label('Precipitation (mm)',size=20) if(output_dir is not None): plt.savefig(output_dir+ '起报时间_{0:%Y}年{0:%m}月{0:%d}日{0:%H}时_起始时间_{1:%Y}年{1:%m}月{1:%d}日{1:%H}时_终止时间_{1:%Y}年{1:%m}月{1:%d}日{1:%H}时'.format(initTime, fcstTime1, fcstTime2)+'.png', dpi=200,bbox_inches='tight') plt.close() else: plt.show()
def draw_cumulated_precip( rain=None, 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=(9,14)) camera = Camera(fig) nframe=rain['data'].shape[0] 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.]) datacrs = ccrs.PlateCarree() ax = plt.axes([0.01,0.1,.98,.84], projection=plotcrs) map_extent2=utl.adjust_map_ratio(ax,map_extent=map_extent,datacrs=datacrs) # define return plots plots = {} # draw mean sea level pressure if rain is not None: x, y = np.meshgrid(rain['lon'], rain['lat']) z=np.squeeze(rain['data'].values) z[z<0.1]=np.nan znan=np.zeros(shape=x.shape) znan[:]=np.nan # cmap,norm=dk_ctables.cm_qpf_nws(atime=None, # pos=np.concatenate(( # np.array([0, 0.1, 0.5, 1]), np.arange(2.5, 25, 2.5), # np.arange(25, 50, 5), np.arange(50, 150, 10), # np.arange(150, 800, 50)))) cmap,norm=dk_ctables.cm_qpf_nws(atime=24) cmap.set_under(color=[0,0,0,0],alpha=0.0) plots['rain'] = ax.pcolormesh( x,y,znan, norm=norm, cmap=cmap, zorder=1,transform=datacrs,alpha=0.5) #additional information plt.title('['+rain.attrs['model']+'] '+ str(int(rain.coords['forecast_period'].values[0]))+'至'+str(int(rain.coords['forecast_period'].values[-1]))+'时效累积降水预报', loc='left', fontsize=30) ax.add_feature(cfeature.OCEAN) utl.add_china_map_2cartopy_public( ax, name='coastline', edgecolor='gray', lw=0.8, zorder=3,alpha=0.5) if add_china: utl.add_china_map_2cartopy_public( ax, name='province', edgecolor='gray', lw=0.5, zorder=3) utl.add_china_map_2cartopy_public( ax, name='nation', edgecolor='black', lw=0.8, zorder=3) utl.add_china_map_2cartopy_public( ax, name='river', edgecolor='#74b9ff', lw=0.8, zorder=3,alpha=0.5) # grid lines gl = ax.gridlines( crs=datacrs, linewidth=2, color='gray', alpha=0.5, linestyle='--', zorder=1) 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 initTime = pd.to_datetime( str(rain.coords['forecast_reference_time'].values)).replace(tzinfo=None).to_pydatetime() #发布时间 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') # add color bar cax=plt.axes([l,b-0.04,w,.02]) cb = plt.colorbar(plots['rain'], cax=cax, orientation='horizontal') cb.ax.tick_params(labelsize='x-large') cb.set_label('Precipitation (mm)',size=20) fcst_time=initTime+timedelta(hours=rain.coords['forecast_period'].values[0]) bax=plt.axes([l,b+h-0.07,.45,.07]) bax.set_yticks([]) bax.set_xticks([]) bax.axis([0, 10, 0, 10]) fcst_time1=initTime+timedelta(hours=rain.coords['forecast_period'].values[0]-6) bax.text(2.5, 7.5,'起始时间: '+fcst_time1.strftime("%Y年%m月%d日%H时"),size=15) bax.text(2.5, 0.5,'www.nmc.cn',size=15) valid_fhour=bax.text(2.5, 5,'截至时间: ',size=15) txt_fhour=bax.text(2.5, 2.5,'预报时效: ',size=15) utl.add_logo_extra_in_axes(pos=[l-0.0,b+h-0.085,.1,.1],which='nmc', size='Xlarge') # 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_on_map(ax,map_extent=map_extent2,transform=datacrs,zorder=2,size=13,small_city=small_city) fcst_time2=initTime+timedelta(hours=rain.coords['forecast_period'].values[-1]) valid_fhour.set_text('截至时间: '+fcst_time2.strftime("%Y年%m月%d日%H时")) txt_fhour.set_text('预报时效: '+str(int(rain.coords['forecast_period'].values[-1]))+'小时') ax.pcolormesh( x,y,np.squeeze(z[-1,:,:]), norm=norm, cmap=cmap, zorder=1,transform=datacrs,alpha=0.5) # plt.draw() plt.savefig(output_dir+ '起报时间_'+initTime.strftime("%Y年%m月%d日%H时")+ '预报时效_'+str(int(rain.coords['forecast_period'].values[-1]))+'小时_'+rain.attrs['model']+'.png', dpi=200,bbox_inches='tight')
def draw_cumulated_precip_evo( rain=None, 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)) 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.]) datacrs = ccrs.PlateCarree() ax = plt.axes([0.01,0.1,.98,.84], projection=plotcrs) map_extent2=utl.adjust_map_ratio(ax,map_extent=map_extent,datacrs=datacrs) # define return plots plots = {} # draw mean sea level pressure if rain is not None: x, y = np.meshgrid(rain['lon'], rain['lat']) z=np.squeeze(rain['data'].values) z[z<0.1]=np.nan znan=np.zeros(shape=x.shape) znan[:]=np.nan cmap,norm=dk_ctables.cm_qpf_nws(atime=24) cmap.set_under(color=[0,0,0,0],alpha=0.0) plots['rain'] = ax.pcolormesh( x,y,znan, norm=norm, cmap=cmap, zorder=1,transform=datacrs,alpha=0.5) #additional information plt.title('['+rain.attrs['model']+'] '+ str(int(rain.coords['forecast_period'].values[0])-rain.attrs['t_gap'])+ '至'+str(int(rain.coords['forecast_period'].values[-1]))+'时效累积降水预报', loc='left', fontsize=25) ax.add_feature(cfeature.OCEAN) utl.add_china_map_2cartopy_public( ax, name='coastline', edgecolor='gray', lw=0.8, zorder=3,alpha=0.5) if add_china: utl.add_china_map_2cartopy_public( ax, name='province', edgecolor='gray', lw=0.5, zorder=3) utl.add_china_map_2cartopy_public( ax, name='nation', edgecolor='black', lw=0.8, zorder=3) utl.add_china_map_2cartopy_public( ax, name='river', edgecolor='#74b9ff', lw=0.8, zorder=3,alpha=0.5) # grid lines gl = ax.gridlines( crs=datacrs, linewidth=2, color='gray', alpha=0.5, linestyle='--', zorder=1) 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 initTime = pd.to_datetime( str(rain.coords['forecast_reference_time'].values)).replace(tzinfo=None).to_pydatetime() #发布时间 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') # add color bar cax=plt.axes([l,b-0.04,w,.02]) cb = plt.colorbar(plots['rain'], cax=cax, orientation='horizontal') cb.ax.tick_params(labelsize='x-large') cb.set_label('Precipitation (mm)',size=20) fcst_time=initTime+timedelta(hours=rain.coords['forecast_period'].values[0]) bax=plt.axes([l,b+h-0.1,.25,.1]) bax.set_yticks([]) bax.set_xticks([]) bax.axis([0, 10, 0, 10]) fcst_time1=initTime+timedelta(hours=rain.coords['forecast_period'].values[0]-6) bax.text(2.5, 7.5,'起始时间: '+fcst_time1.strftime("%Y年%m月%d日%H时"),size=15) bax.text(2.5, 0.5,'www.nmc.cn',size=15) valid_fhour=bax.text(2.5, 5,'截至时间: ',size=15) txt_fhour=bax.text(2.5, 2.5,'预报时效: ',size=15) utl.add_logo_extra_in_axes(pos=[l-0.02,b+h-0.1,.1,.1],which='nmc', size='Xlarge') # 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_on_map(ax,map_extent=map_extent2,transform=datacrs,zorder=2,size=13,small_city=small_city) def update(frame_number): fcst_time2=initTime+timedelta(hours=rain.coords['forecast_period'].values[frame_number]) valid_fhour.set_text('截至时间: '+fcst_time2.strftime("%Y年%m月%d日%H时")) txt_fhour.set_text('预报时效: '+str(int(rain.coords['forecast_period'].values[frame_number]))+'小时') return ax.pcolormesh( x,y,np.squeeze(z[frame_number,:,:]), norm=norm, cmap=cmap, zorder=1,transform=datacrs,alpha=0.5) nframes=rain['data'].shape[0] animation1 = FuncAnimation(fig, update, frames=nframes,interval=1000) # ffmpegpath = os.path.abspath(r"C:\Users\HEYGY\Desktop\ffmpeg-20200824-3477feb-win64-static\bin\ffmpeg.exe") # import matplotlib # matplotlib.rcParams["animation.ffmpeg_path"] = ffmpegpath # writer = animation.FFMpegWriter() # show figure plt.subplots_adjust(top=1, bottom=0, right=0.93, left=0, hspace=0, wspace=0) plt.margins(0, 0) if(output_dir != None): animation1.save(output_dir+'累积降水演变_'+ '起报时间_'+initTime.strftime("%Y年%m月%d日%H时")+ '预报时效_'+str(int(rain.coords['forecast_period'].values[-1]))+'小时_'+ '['+rain.attrs['model']+'] '+'.gif',writer='pillow') if(output_dir == None): #animation.save('rain.gif', fps=75, writer='imagemagick') plt.show()
def draw_gh_rain(gh=None, rain=None, 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 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.]) datacrs = ccrs.PlateCarree() ax = plt.axes([0.01,0.1,.98,.84], projection=plotcrs) map_extent2=utl.adjust_map_ratio(ax,map_extent=map_extent,datacrs=datacrs) # define return plots plots = {} # draw mean sea level pressure if rain is not None: x, y = np.meshgrid(rain['lon'], rain['lat']) z=np.squeeze(rain['data'].values) z[z<0.1]=np.nan cmap,norm=dk_ctables.cm_qpf_nws(atime=rain.attrs['atime']) cmap.set_under(color=[0,0,0,0],alpha=0.0) plots['rain'] = ax.pcolormesh( x,y,z, norm=norm, cmap=cmap, zorder=1,transform=datacrs,alpha=0.5) # draw -hPa geopotential height if gh is not None: x, y = np.meshgrid(gh['lon'], gh['lat']) clevs_gh = np.append(np.append(np.arange(0, 480, 4),np.append(np.arange(480, 584, 8), np.arange(580, 604, 4))), np.arange(604, 2000, 8)) plots['gh'] = ax.contour( x, y, np.squeeze(gh['data']), clevs_gh, colors='black', linewidths=2, transform=datacrs, zorder=3) plt.clabel(plots['gh'], inline=2, fontsize=20, fmt='%.0f',colors='black') #additional information plt.title('['+gh.attrs['model']+'] '+ str(int(gh['level'].values[0]))+'hPa 位势高度场, '+ str(int(rain.attrs['atime']))+'小时降水', loc='left', fontsize=30) ax.add_feature(cfeature.OCEAN) utl.add_china_map_2cartopy_public( ax, name='coastline', edgecolor='gray', lw=0.8, zorder=3,alpha=0.5) if add_china: utl.add_china_map_2cartopy_public( ax, name='province', edgecolor='gray', lw=0.5, zorder=3) utl.add_china_map_2cartopy_public( ax, name='nation', edgecolor='black', lw=0.8, zorder=3) utl.add_china_map_2cartopy_public( ax, name='river', edgecolor='#74b9ff', lw=0.8, zorder=3,alpha=0.5) # grid lines gl = ax.gridlines( crs=datacrs, linewidth=2, color='gray', alpha=0.5, linestyle='--', zorder=1) 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(gh.coords['forecast_reference_time'].values)).replace(tzinfo=None).to_pydatetime() fcst_time=initTime+timedelta(hours=gh.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(gh.coords['forecast_period'].values[0]))+'小时'+'(降水'+str(int(gh.coords['forecast_period'].values[0]+12))+'小时)',size=15) plt.text(2.5, 0.5,'www.nmc.cn',size=15) # add color bar if(rain != None): cax=plt.axes([l,b-0.04,w,.02]) cb = plt.colorbar(plots['rain'], cax=cax, orientation='horizontal') cb.ax.tick_params(labelsize='x-large') cb.set_label(str(int(rain.attrs['atime']))+'h precipitation (mm)',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_on_map(ax,map_extent=map_extent2,transform=datacrs,zorder=2,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(int(gh.coords['forecast_period'].values[0]))+'小时'+'.png', dpi=200) if(output_dir == None): plt.show()