- 积分
- 414
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2016-11-15
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2018-12-10 16:59:08
|
显示全部楼层
import threading
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import time
def data_process(month):
nc_obj = Dataset("E:\\data\\2015_" + month + ".nc")
month_dict={'Jan':1,'Feb':2,'Mar':3,'Apr':4,'May':5,'Jun':6,
'Jul':7,'Aug':8,'Sep':9,'Oct':10,'Nov':11,'Dec':12}
v = [month_dict[k] for k in month_dict.keys() if k==month][0]
list1 = [1,3,5,7,8,10,12]
list2 = [2]
list3 = [4,6,9,11]
lats_list = range(53)
lons_list = range(81)
if v in list1:
times_list = range(8*31)
if v in list2:
times_list = range(8*28)
if v in list3:
times_list = range(8*30)
arr1 = np.zeros( (53,81), dtype=np.float64 )
for lat in lats_list:
for lon in lons_list:
for t in times_list:
hcc = nc_obj.variables["hcc"][t,lat,lon]
lcc = nc_obj.variables["lcc"][t,lat,lon]
mcc = nc_obj.variables["mcc"][t,lat,lon]
count = 0
if lcc <= 0.05 and mcc <= 0.05 and hcc >= 0.05:
count += 1
arr1[lat][lon] = count/len(times_list)
m=Basemap(projection='tmerc',lat_0 = 23, lon_0 = 118,llcrnrlat=16.5,
llcrnrlon=108,urcrnrlat=23,urcrnrlon=118)
#加载中国地图
m.readshapefile('E:\\China_map\\region','region.shp',color='red')
#生成绘图背景格点场
ny=arr1.shape[0];nx=arr1.shape[1]
lons,lats=m.makegrid(nx,ny)
x,y=m(lons,lats)
yy=y[::-1]
#画经纬度网格
m.drawparallels(np.arange(23,16.5,-1.5),labels=[1,0,0,0])
m.drawmeridians(np.arange(108,118,1.5),labels=[0,0,0,1])
#画填色图
cm = plt.cm.get_cmap('Blues') #读取一个内置的色系
shades=m.contourf(x,yy,arr1,cmap=cm) #将数据填色绘出
m.colorbar(shades) #添加colorbar
#画等值线
#curve=m.contour(x,yy,arr1,colors='black')
#plt.clabel(curve,fmt='%1.0f',colors='black')
#添加标题
plt.title('2015_%d_hcc'%v)
#保存
plt.savefig('2015_hcc\\%d月.png'%v,dpi=300)
#关闭绘图功能
plt.close()
#单线程处理
#time1 = time.time()
#data_process("Jun")
#time2 = time.time()
#print(time2 - time1)
#print("\a")
#多线程处理
threads = []
t1 = threading.Thread(target=data_process,args=('Sep',))
threads.append(t1)
t2 = threading.Thread(target=data_process,args=('Nov',))
threads.append(t2)
time1 = time.time()
if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()
t.join()
print("\a")
time2 = time.time()
print(time2 - time1) |
|