-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmwe1.py
102 lines (64 loc) · 3.03 KB
/
mwe1.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
import os.path
import xarray as xr
import numpy as np
import boto3
import metpy.calc as mpcalc
from botocore import UNSIGNED
from botocore.config import Config
import sys
if (not os.path.isfile('gfs.t12z.pgrb2.0p50.f000')):
client = boto3.client('s3', config=Config(signature_version=UNSIGNED))
client.download_file('noaa-gfs-bdp-pds', 'gfs.20230824/12/atmos/gfs.t12z.pgrb2.0p25.f000', 'gfs.t12z.pgrb2.0p25.f000')
u850 = xr.open_dataset('gfs.t12z.pgrb2.0p50.f000', engine='cfgrib',backend_kwargs={'filter_by_keys':{'typeOfLevel': 'isobaricInhPa', 'shortName': 'u', 'level': 850}})
u = u850.u
v850 = xr.open_dataset('gfs.t12z.pgrb2.0p50.f000', engine='cfgrib', backend_kwargs={'filter_by_keys':{'typeOfLevel': 'isobaricInhPa', 'shortName': 'v', 'level': 850}})
v = v850.v
# Compute the 850 hPa relative vorticity.
vort850 = mpcalc.vorticity(u, v)
# Compute the 850 hPa divergence.
div850 = mpcalc.divergence(u, v)
mask = ((vort850.latitude <= 13.5) & (vort850.latitude >= 5.0) & (vort850.longitude <= 202.) & (vort850.longitude >= 191.))
vortmask = vort850.where(mask)
vortmask = vortmask.fillna(0.0)
divmask = div850.where(mask)
divmas = div850.fillna(0.0)
dx, dy = mpcalc.lat_lon_grid_deltas(vortmask.longitude, vortmask.latitude)
upsi = xr.zeros_like(vortmask)
#print(upsi.shape)
vpsi = xr.zeros_like(vortmask)
#print(vpsi.shape)
uchi = xr.zeros_like(divmask)
vchi = xr.zeros_like(divmask)
x_ll = list(vortmask.longitude.values).index(191.0)
x_ur = list(vortmask.longitude.values).index(202.0)
y_ll = list(vortmask.latitude.values).index(5.0)
y_ur = list(vortmask.latitude.values).index(13.5)
x_ll_subset = list(vortmask.longitude.values).index(180.0)
x_ur_subset = list(vortmask.longitude.values).index(220.0)
y_ll_subset = list(vortmask.latitude.values).index(0.0)
y_ur_subset = list(vortmask.latitude.values).index(30.0)
i = np.abs(x_ll_subset-x_ur_subset)
j = np.abs(y_ll_subset-y_ur_subset)
istart = np.linspace(x_ll_subset,x_ur_subset,num = i,endpoint=False,dtype=np.int32)
jstart = np.linspace(y_ur_subset,y_ll_subset,num=j,endpoint=False,dtype=np.int32)
x = np.abs(x_ll-x_ur)
y = np.abs(y_ll-y_ur)
xstart = np.linspace(x_ll,x_ur,num = x,endpoint=False,dtype=np.int32)
ystart = np.linspace(y_ll,y_ur,num=y,endpoint=False,dtype=np.int32)
xindex,yindex = np.meshgrid(xstart,ystart)
iindex = np.zeros((y,x))
jindex = np.zeros((y,x))
dx = dx.magnitude
dy = dy.magnitude
vortmask = vortmask.values
for i in range(x_ll_subset, x_ur_subset):
for j in range(y_ur_subset, y_ll_subset):
iindex[:,:] = i
jindex[:,:] = j
xdiff = (iindex-xindex)*dx[y_ur:y_ll,x_ll:x_ur]
ydiff = (jindex-yindex)*dy[y_ur:y_ll,x_ll:x_ur]
rsq = (xdiff*xdiff) + (ydiff*ydiff)
upsi[j,i] = np.where(rsq > 0, vortmask[y_ur:y_ll,x_ll:x_ur]*-1.0*(ydiff/rsq)*dx[y_ur:y_ll,x_ll:x_ur]*dy[y_ur:y_ll,x_ll:x_ur], 0.0).sum()
vpsi[j,i] = np.where(rsq > 0, vortmask[y_ur:y_ll,x_ll:x_ur]*-1.0*(xdiff/rsq)*dx[y_ur:y_ll,x_ll:x_ur]*dy[y_ur:y_ll,x_ll:x_ur], 0.0).sum()
upsi[:,:] = (1/(2*np.pi)) * upsi[:,:]
vpsi[:,:] = (1/(2*np.pi)) * vpsi[:,:]