- 积分
- 43
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2018-9-1
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 flyed 于 2021-4-1 10:07 编辑
- import time
- def windrose2polar(a):
- r = 360 - a + 90
- r[r>360] = r - 360
- return r
- def plotWindRole(fn, imageName, polType):
- #Read data (wind speed, weed direction, pm10)
- table = readtable(fn,delimiter=",",format='%3f')
- ws = table['WS']
- wd = table['WD']
- pm10 = table['VAL']
- N = len(ws)
- vmaxArray = []
- for row in pm10:
- vmaxArray.append(row)
- vmaxArray.sort()
- vmax = vmaxArray[int(len(vmaxArray)*0.99)]
-
- #Convert from windrose coordinate to polar coordinate
- rwd = windrose2polar(wd)
-
- #Degree to radians
- rwd = radians(rwd)
-
- #Calculate frequency of each wind direction bin
- wdbins = linspace(0.0, pi * 2, 9)
- dwdbins = degrees(wdbins)
- dwdbins = windrose2polar(dwdbins)
- rwdbins = radians(dwdbins)
- wdN = len(wdbins) - 1
- theta = ones(wdN + 1)
- for j in range(wdN):
- theta[j] = rwdbins[j]
- theta[wdN] = theta[0]
- wd = wd + 360./wdN/2
- wd[wd>360] = wd - 360
- wdhist = histogram(radians(wd), wdbins)[0].astype('float')
- wdhist = wdhist / N
- nwdhist = wdhist.aslist()
- nwdhist.append(nwdhist[0])
- nwdhist = array(nwdhist)
-
- #Polar coordinate to Cartesian coordinate
- rwdc, wsc = pol2cart(rwd, ws)
-
- #Get convexhull (minimum outer polygon of the wind points)
- poly = topo.convexhull(rwdc, wsc)
-
- #Get grid data
- dd = 0.5
- x = linspace(rwdc.min() - dd, rwdc.max() + dd, 5)
- y = linspace(wsc.min() - dd, wsc.max() + dd, 5)
- data = griddata((rwdc, wsc), pm10, xi=(x, y), method='idw', pointnum=5, convexhull=False)[0]
- #---------------------------------------
- #Plot figure
- pos = [0.13, 0.1, 0.775, 0.775]
-
- #Cartesian axes
- ax = axes(position=pos, aspect='equal')
- xaxis(visible=False)
- yaxis(location='right', visible=False)
- yaxis(location='left', shift = 50)
- ylabel('Wind speed (m/s)')
- levs = arange(0, vmax, vmax/7).astype(int)
- cg = contourf(x, y, data, levs, cmap='BlAqGrYeOrRe', visible=False)
- cg = cg.clip([poly])
- ax.add_graphic(cg)
- #ss = scatter(rwdc, wsc, s=6, fill=False, edgecolor='b', edgesize=1)
- #patch(poly)
- # xshift:离图的距离 shrink:colorBar的高度
- colorbar(cg, shrink=0.8, xshift=30, label=r'$\mu g/m^3'
- , labelloc='bottom')
- maxv = ws.max()+1
- xlim(-maxv, maxv)
- ylim(-maxv, maxv)
- ticks = ax.get_yticks()
- ax.set_yticklabels([abs(yy) for yy in ticks])
-
- #Polar axes
- axp = axes(position=pos, polar=True)
- plot(theta, nwdhist, color='k', linewidth=2)
- axp.set_rmax(1)
- axp.set_rlabel_position(25.)
- axp.set_rtick_locations([0.2,0.4,0.6,0.8])
- #axp.set_rticks(['2','4','6','8','10 m/s'])
- axp.set_rticks(['20%','40%','60%','80%'])
- axp.set_xtick_font(size=14)
- axp.set_xtick_locations(arange(0,360,22.5))
- axp.set_xticks(['E','NEE','NE','NNE','N','NNW','NW','NWW','W','SWW',
- 'SW','SSW','S','SSE','SE','SEE'])
- title(r'$Windrose \ with \ '+polType+r' \ concentrations, fontsize=18)
- savefig(imageName,640, 524)
- clf()
- time.sleep(2)
- baseDir = r"/mnt/d/pythonWorkspace/try/windrose/"
- targetDir = r"/mnt/f/tmp/windRoseImages/"
- for file in (os.listdir(baseDir)):
- if file.endswith(".txt"):
- imgName = file.replace(".txt", ".png")
- polType = imgName[imgName.rindex('-')+1:imgName.index('.')]
- if polType == 'pm25':
- polType = 'PM_{25}'
- if polType == 'o3':
- polType = 'O_{3}'
- if polType == 'pm10':
- polType = 'PM_{10}'
- polType = polType.upper()
- print(polType, baseDir, targetDir, imgName, file)
- plotWindRole(baseDir + file, targetDir + imgName, polType)
- print imgName
-
复制代码 如程序需求需要循环画风玫瑰图当在 clf()是否发生错误,此程序在windows下可正常运行,但在Linux中不行 linux中的报错信息为:AttributeError: 'NoneType' object has no attribute 'getFigureDock'
|
|