forked from smft/pywrfplot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tzWRF.py
179 lines (160 loc) · 5.9 KB
/
tzWRF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding:utf-8 -*-
"""
@author Geir Arne Waagbø
@see http://code.google.com/p/pywrfplot/
"""
import pyplot as plt
from numpy import arange
from pywrfplotParams import *
from pywrfplotUtils import *
def tzCloudPlot(nest,plotMetar=False,offset=0):
nc = openWRF(nest)
Nx,Ny,Nz,_longs,_lats,_dx,_dy,x_nr,y_nr = getDimensions(nc)
heightground_t, heighthalf_tz = _getHeight(nc, Nx, Ny, Nz, x_nr, y_nr)
T_tz = np.zeros((Nz,Nt))
qcloud_tz = np.zeros((Nz,Nt))
qice_tz = np.zeros((Nz,Nt))
qsnow_tz = np.zeros((Nz,Nt))
qrain_tz = np.zeros((Nz,Nt))
for t in arange(Nt):
theta = nc.variables['T'][t,:,y_nr,x_nr] + T_base
P = nc.variables['P'][t,:,y_nr,x_nr] + nc.variables['PB'][t,:,y_nr,x_nr]
T_tz[:,t] = theta*(P/P_bot)**kappa # Temperatur i halvflatene (Kelvin)
rho = P[:]/(R*T_tz[:,t]) # regner om til g/m3
qcloud_tz[:,t] = 1000.0*nc.variables['QCLOUD'][t,:,y_nr,x_nr]*rho
qice_tz[:,t] = 1000.0*nc.variables['QICE'][t,:,y_nr,x_nr]*rho
qsnow_tz[:,t] = 1000.0*nc.variables['QSNOW'][t,:,y_nr,x_nr]*rho
qrain_tz[:,t] = 1000.0*nc.variables['QRAIN'][t,:,y_nr,x_nr]*rho
for s in [u'Snø','Regn']:
plt.figure()
plt.axis([-offset,Nt-1,0.0,z_max])
grid = np.reshape(np.tile(arange(Nt),Nz),(Nz,-1))
if (s==u"Snø"):
var = qsnow_tz
cm = cmap_blue
levs = tz_snow_levels
else:
var = qrain_tz
cm = cmap_green
levs = tz_rain_levels
plt.contourf(grid, heighthalf_tz, qcloud_tz, alpha=0.9,levels=tz_cloudwater_levels, cmap=cmap_red)#
plt.colorbar()
plt.contourf(grid, heighthalf_tz, var, alpha=0.6,levels=levs, cmap=cm)#
plt.colorbar()
cs = plt.contour(grid, heighthalf_tz, T_tz-T_zero, temp_int,colors='black',linestyles='solid')
plt.clabel(cs, inline=1, fmt='%1.0f', fontsize=12,colors='black')
plt.fill_between(arange(-offset,Nt),heightground_t[0],0,facecolor='lightgrey')
if plotMetar:
_metar()
print s + ' ($g/m^3$)'
plt.xlabel('Timer etter ' + date + 'T00:00Z')
plt.ylabel(u'Høyde [m]')
plt.yticks(np.arange(0,z_max,dz), fontsize='small')
plt.show()
plt.close()
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlim(0,Nt-1)
ax1.plot(np.sum(qcloud_tz,axis=0), color='black',label=u"skyvann")
ax1.plot(np.sum(qsnow_tz,axis=0), color='green',label=u"snø")
ax1.set_xlabel('Timer etter ' + date + 'T00:00Z')
ax1.set_ylabel(u'Skyvann og snø ($g/m^2$)')
ax2 = ax1.twinx()
ax2.plot(np.sum(qice_tz,axis=0), color='blue',label="is")
ax2.plot(np.sum(qrain_tz,axis=0), color='red',label="regn")
ax2.set_ylabel('Regn og is ($g/m^2$)')
ax1.set_xlim(0,Nt-1)
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
plt.show()
plt.close()
def _getHeight(nc,Nx,Ny,Nz,x_nr,y_nr):
# Calculate height above sea level for mass levels
# Note: geopotential defined at full levels (w-levels)
# Must interpolate to find geopotential at half levels (u-levels)
geopot = (nc.variables['PH'][:,0:Nz,y_nr,x_nr] + nc.variables['PHB'][:,0:Nz,y_nr,x_nr])
mu = (nc.variables['MU'][:,y_nr,x_nr]+nc.variables['MUB'][:,y_nr,x_nr])
znw = nc.variables['ZNW'][:,0:Nz] # full (w) levels
znu = nc.variables['ZNU'][:,0:Nz] # half (u,mass) levels
heighthalf = np.zeros((Nz,Nt))# height in meters
for t in arange(Nt):
pfull = mu[t]*znw[t,0:Nz]+P_top
phalf = mu[t]*znu[t,0:Nz]+P_top
for k in arange(Nz):
heighthalf[k,t]=interp(geopot[t,:],pfull[:],phalf[k])/g
heightground = geopot[:,0]/g
return heightground,heighthalf
filename = 'metar-engm.txt'
data = ['SG','-SG','SN','-SN','FZDZ','-FZDZ','FZRA','-FZRA']
fromday = 15
def _metar():
dl = _metardata()
min = -12.0
for item in dl:
itemnumber = (item[0]-fromday)*24 + item[1]
type = item[2]
if (type is not 'None'):
plt.fill_between([itemnumber-1,itemnumber],[150,150],[0,0],facecolor=_itemcolor(type))
def _itemcolor(s):
if (s=='-FZDZ' or s=='FZDZ'):
return 'pink'
if (s=='-FZRA'):
return 'red'
if (s=='FZRA'):
return 'darkred'
if (s=='SG' or s=='-SG'):
return 'lightblue'
if (s=='-SN'):
return 'blue'
if (s=='SN'):
return 'darkblue'
def _metardata():
datalist = list()
f = open(filename,'r')
s = f.readline()
while (s is not ''):
s = _strip(s)
if (s is not ''):
itemlist = list()
t = s.split()
itemlist.append(int(t[1][:2])) #day
h = int(t[1][2:4]) #hour
m = int(t[1][4:6])
if (m >= 30):
h = h+1
itemlist.append(h) #hour
j = 1
if (_hasdata(s)):
while (not _isdata(t[j])):
j = j+1
itemlist.append(t[j])
else:
itemlist.append('None')
while (t[j][0] is not 'M'):
j = j+1
itemlist.append(-int(t[j][1:3]))
#print str(itemlist)
datalist.append(itemlist)
s = f.readline()
f.close()
while True:
item = datalist.pop()
if (item[0] >= fromday):
datalist.append(item)
break
datalist.reverse()
#print str(datalist)
return datalist
def _strip(s):
s = s.strip()
index = s.find('TEMPO')
if (index!=-1):
s = s[0:index]
return s
def _hasdata(s):
for t in data:
if (s.find(t)!=-1):
return True
return False
def _isdata(s):
return s in data