- 积分
- 7554
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2018-4-13
- 最后登录
- 1970-1-1
|
发表于 2023-5-6 10:09:24
|
显示全部楼层
工作中我们可能经常遇到批量绘图的情况,但是python运行时往往只是单线程运行,数据量、绘图量大的话CPU拉不满,绘图就可能比较慢。使用多进程运行就可以充分发挥CPU的多核性能了,这是我的小白代码,主要multiprocessing多线程用于绘制雷达图像,但在分配任务时,会有bug(数据个数不能被设置的进程数整除的话,余数的最后几个数据不会被处理)抛砖引玉,请大佬们多交流。
- import numpy as np
- import matplotlib
- matplotlib.use('agg')
- from matplotlib import pyplot as plt
- from multiprocessing import Process
- import os
- import cartopy.crs as ccrs
- import cartopy.io.shapereader as shapereader
- from pycwr.io import read_auto
- from pycwr.draw.RadarPlot import add_rings,GraphMap
- def get_filename(path,fileType):
- file_name =[]
- final_name = []
- for files in os.listdir(path): #root为目录路径 #dirs为路径下的子目录 #files为路径下的所有非子目录
- if fileType in files:
- file_name.append(files.replace(fileType,''))#生成不带‘.bin’后缀的文件名组成的列表
- final_name = [path +item +fileType for item in file_name]#生成‘.bin’后缀的文件名组成的绝对路径列表
- final_name.sort()
- return final_name #输出列表
- def generate_heat_array(sa_radar_file,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list):
-
- path1=[]
- path2=[]
- path3=[]
- path4=[]
- path5=[]
- path6=[]
- path7=[]
- path8=[]
- path9=[]
- path10=[]
- num = 0
- while num <= (len(sa_radar_file)/10-1):
- path1.append(sa_radar_file[10*num])
- path2.append(sa_radar_file[10*num+1])
- path3.append(sa_radar_file[10*num+2])
- path4.append(sa_radar_file[10*num+3])
- path5.append(sa_radar_file[10*num+4])
- path6.append(sa_radar_file[10*num+5])
- path7.append(sa_radar_file[10*num+6])
- path8.append(sa_radar_file[10*num+7])
- path9.append(sa_radar_file[10*num+8])
- path10.append(sa_radar_file[10*num+9])
- num = num+1
-
- #用multiprocessing实现多进程
- process1=Process(target=show_heat_map,args=(path1,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list))
- process2=Process(target=show_heat_map,args=(path2,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list))
- process3=Process(target=show_heat_map,args=(path3,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list))
- process4=Process(target=show_heat_map,args=(path4,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list))
- process5=Process(target=show_heat_map,args=(path5,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list))
- process6=Process(target=show_heat_map,args=(path6,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list))
- process7=Process(target=show_heat_map,args=(path7,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list))
- process8=Process(target=show_heat_map,args=(path8,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list))
- process9=Process(target=show_heat_map,args=(path9,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list))
- process10=Process(target=show_heat_map,args=(path10,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list))
- process1.start()
- process2.start()
- process3.start()
- process4.start()
- process5.start()
- process6.start()
- process7.start()
- process8.start()
- process9.start()
- process10.start()
- process1.join()
- process2.join()
- process3.join()
- process4.join()
- process5.join()
- process6.join()
- process7.join()
- process8.join()
- process9.join()
- process10.join()
- def show_heat_map(path,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list):
- # for key,color in zip(['dBZ'],['CN_ref']):#'V','CN_vel',
- for key in ['dBZ','V']:
- for level in [0,1,2,3,4,5]:#
- print(level,'begin')
- for nFiles in range(len(path)):
- # print(os.path.basename(path[nFiles]),'begin')
- PRD = read_auto(path[nFiles]) #新版本数据
- PRD.add_product_CR_lonlat(XLon=lon1d, YLat=lat1d)
- # XLon:np.ndarray, 1d, units:degrees
- # YLat:np.ndarray, 1d, units:degrees
- # level_height:常量,要计算的高度 units:meters
- fig = plt.figure(figsize=(6,6))
- ax = plt.axes(projection=proj , zorder = 5)
- # ax = fig.subplots(1, 1, subplot_kw={'projection': proj})
- ax.set_extent(extent, crs=proj)
- graph = GraphMap(PRD, proj)
- graph.plot_ppi_map(ax=ax, sweep_num=level, extend=extent,field_name=key) ## ,min_max=[-35,35],cmap = 'CN_vel' 0代表第一层, dBZ代表反射率产品,cmap
- # plot_lonlat_map(ax, grid_lon, grid_lat, PRD.product.CR_geo, transform=proj,)### bounds= np.arange(25,76,5),cmap = 'Greys'
- ### 设置经纬度间隔 修改Anaconda3\Lib\site-packages\pycwr\draw\RadarPlot.py
- ### 中def plot_lonlat_map 参数 parallels meridians
- add_rings(ax,rings=[50,100],color="red", linestyle='-', linewidth=0.6,)
- # ax.add_geometries(shapereader.Reader(mfl1).geometries(), crs=proj, facecolor='none',edgecolor='k',linewidth=1)
- for state, geos in zip(shapereader.Reader(mfl).records(), shapereader.Reader(mfl).geometries()):
- if state.attributes['NAME'] in name_list:
- ax.add_geometries([geos], crs=proj,facecolor='None', edgecolor='red',zorder = 5) # 重点区域上色
- # start_time = str(PRD.scan_info.start_time.values + np.timedelta64(8, 'h'))[5:19]
- end_time = str(PRD.scan_info.end_time.values + np.timedelta64(8, 'h'))[5:19]
- ax.set_title(end_time+' Z9856 '+str(level)+key, fontsize=16)
-
- plt.tight_layout()
- plt.savefig(fig_dir+os.path.basename(path[nFiles])+'_'+str(level)+'_'+key+'.png',dpi = 200)
- plt.clf()
- # plt.close()
- print(level,end_time,' Z9856 done')
- print(level,'done')
-
- if __name__=='__main__':
- #pdb.set_trace()
- #"""读取目标文件夹下的多个雷达基数据"""
- Dir = "G:\\data\\9856\\" #目标文件夹
- fig_dir = "D:\\9856_fig_all\\"
- fileType = '.bz2' #雷达基数据后缀名
- sa_radar_file = get_filename(Dir,fileType)
- # print(sa_radar_file)
- #"""----------------------------------------------------------------------"""
- mfl = 'G:/代码/地图文件/micaps_shp_utf8/County.shp'
- mfl1 = 'G:/代码/地图文件/micaps_shp_utf8/City.shp'
- name_list = ['碧江区','万山区','江口县','玉屏侗族自治县','石阡县','思南县',
- '印江土家族苗族自治县','德江县','沿河土家族自治县','松桃苗族自治县',]
- # extent = [108, 110, 27, 29]#限定绘图范围[108, 110, 27, 29]
- extent = [107.5, 109.5, 27, 29.5]
- proj = ccrs.PlateCarree()#创建投影,选择cartopy的platecarree投影
- lon1d = np.arange(extent[0], extent[1], 0.01) ##lon方向0.01等间距,lon_min-1on_max范围
- lat1d = np.arange(extent[2], extent[3], 0.01) ##lat方向0.01等间距,lat_min-lat_max度范围
- # grid_lon, grid_lat = np.meshgrid(lon1d, lat1d, indexing="ij")
- # for key,color in zip(['dBZ','ZDR','CC','PhiDP','KDP'],['CN_ref','pyart_RefDiff','pyart_RefDiff','pyart_RefDiff','pyart_RefDiff']):
- # return mfl,mfl1,extent,proj,lon1d,lat1d
- generate_heat_array(sa_radar_file,fig_dir,mfl,lon1d,lat1d,proj,extent,name_list)
- print('finish')
|
|