-
Notifications
You must be signed in to change notification settings - Fork 0
/
regions.py
227 lines (202 loc) · 7.09 KB
/
regions.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import pickle
import numpy as np
import xarray as xr
import math
import shapefile as shp
import geopandas as gp
import regionmask
from numpy import nan
from paths import countryfilepaths,contfilepaths,basinfilepaths,usafilepath
def pointlocs(re):
if re=='swus':
iloc=[135,200]
elif re=='yuma':
iloc=[130,198]
elif re=='sahara':
iloc=[120,10]
elif re=='sea':
iloc=[110,85]
elif re=='amazon':
iloc=[90,240]
elif re=='zambia':
iloc=[80,25]
return iloc
def rbin(re):
if re=='sea':
mt,mq=np.mgrid[280:320:250j,1e-2:3e-2:250j]
elif re=='swus':
mt,mq=np.mgrid[280:320:250j,5e-4:1.5e-2:250j]
return mt,mq
def rinfo(re):
if re=='sea':
[rloc,mgr]=pickle.load(open('/project/amp/miyawaki/plots/p004/hist_hotdays/cmip6/jja/fut-his/ssp245/mmm/defsea.t2m.95.ssp245.jja.pickle','rb'))
# [rloc,mgr]=pickle.load(open('/home/miyawaki/defsea.t2m.95.ssp245.jja.pickle','rb'))
rlat=mgr[0][:,0]
rlon=mgr[1][0,:]
elif re=='swus':
[rloc,mgr]=pickle.load(open('/project/amp/miyawaki/plots/p004/hist_hotdays/era5/jja/q2m/defswus.q2m.05.jja.pickle','rb'))
# [rloc,mgr]=pickle.load(open('/home/miyawaki/defswus.q2m.05.jja.pickle','rb'))
rlat=mgr[0][:,0]
rlon=mgr[1][0,:]
return rloc,rlat,rlon
def rlev(re,pc):
if pc=='':
if re=='sea':
levs=np.arange(0,250+5,5)
elif re=='swus':
levs=np.arange(0,100+1,5)
else:
if re=='sea':
levs=np.arange(0,250+5,5)
elif re=='swus':
levs=np.arange(0,100+5,5)
return levs
def rtlm(re,pc):
if pc=='':
if re=='sea':
tlim=[285,310]
elif re=='swus':
tlim=[285,315]
else:
if re=='sea':
tlim=[300,310]
elif re=='swus':
tlim=[300,315]
return tlim
def sellatlon(vn,gr,reg):
if reg=='nh':
sla=np.where(gr['lat']>0)[0]
vn=vn[sla,:]
elif reg=='sh':
sla=np.where(gr['lat']<0)[0]
vn=vn[sla,:]
return vn
# From Isla's shapefile_utils, changed to take list of level 0 files
def masklev0(listl0,dat4mask,mtype):
""" Generate a mask using information from a shapefile. Mask will have 1's
within the desired region, nan's everywhere else
Input:
listl0 = list of level 0 object names (e.g., country, continent)
dat4mask = the data that you're planning to mask
Output:
mask = the mask
"""
# setup of the grid for the mask from dat4mask
maskcoords = xr.Dataset({'lat' : (['lat'],dat4mask['lat'].values)}, {'lon' : (['lon'],dat4mask['lon'].values)})
mask = np.zeros([maskcoords.lat.size, maskcoords.lon.size])
for i in range(0,len(listl0),1):
if mtype=='country':
shpfile=countryfilepaths(listl0[i],lev=0)
elif mtype=='continent':
shpfile=contfilepaths(retname(listl0[i]))
elif mtype=='basincontinent':
shpfile=basinfilepaths(listl0[i],lev=1)
print("masking "+listl0[i])
# read in shapefile
lev0 = gp.read_file(shpfile)
maskt = regionmask.mask_geopandas(lev0, maskcoords["lon"], maskcoords["lat"])
maskt = np.where(np.isnan(maskt), 0, 1)
mask[:,:] = mask[:,:] + maskt[:,:]
# ensure unmasked region is set to 1, rest set to nan's
mask = np.where(mask == 0, nan, 1)
mask = xr.DataArray(mask, coords=maskcoords.coords)
return mask
# From Isla's shapefile_utils
def masklev1(parentlev,dat4mask,relist,mtype):
""" Generate a mask using information from a shapefile. Mask will have 1's
within the desired region, nan's everywhere else
Input:
parentlev = level 0 object name (e.g., country, continent)
dat4mask = the data that you're planning to mask
relist = list of subregions ('all' for all subregions)
mtype= mask type, e.g. country, basins
Output:
mask = the mask
"""
# setup of the grid for the mask from dat4mask
maskcoords = xr.Dataset({'lat' : (['lat'],dat4mask['lat'].values)}, {'lon' : (['lon'],dat4mask['lon'].values)})
mask = np.zeros([maskcoords.lat.size, maskcoords.lon.size])
if mtype=='country':
shpfile=gp.read_file(countryfilepaths(parentlev,lev=1))
elif mtype=='basins':
idname='HYBAS_ID'
shpfile=gp.read_file(basinfilepaths(parentlev,lev=2))
elif mtype=='state':
idname='NAME_1'
shpfile=gp.read_file(usafilepath())
for i,re in enumerate(relist):
print("masking "+str(re))
# read in shapefile
lev1 = shpfile[shpfile[idname]==re]
maskt = regionmask.mask_geopandas(lev1, maskcoords["lon"], maskcoords["lat"])
maskt = np.where(np.isnan(maskt), 0, 1)
mask[:,:] = mask[:,:] + maskt[:,:]
# ensure unmasked region is set to 1, rest set to nan's
mask = np.where(mask == 0, nan, 1)
mask = xr.DataArray(mask, coords=maskcoords.coords)
return mask
def settype(relb):
mtype={
'af' :'country',
'ic' :'country',
'fc' :'state',
'cp' :'state',
'sa' :'country',
'se' :'state',
'us' :'country',
}
return mtype[relb]
def regionsets(relb):
lc={
'af' :['botswana','zambia','zimbabwe'],
'ic' :['cambodia','laos','vietnam'],
'fc' :['Utah','Colorado','New Mexico','Arizona'],
'cp' :['Kansas','Nebraska','Missouri','Iowa'],
'sa' :['bolivia','brazil','paraguay'],
'se' :['Tennessee','North Carolina',
'Mississippi','Alabama','Georgia',
'South Carolina','Florida'],
'us' :['usa'],
}
return lc[relb]
def retname(re):
lc={
'af' :'Africa',
'an' :'Antarctica',
'ar' :'Arctic',
'as' :'Asia',
'au' :'Australia',
'eu' :'Europe',
'ic' :'Indochina',
'gr' :'Greenland',
'na' :'North America',
'oc' :'Oceania',
'sa' :'South America',
'se' :'Southeast US',
'si' :'Siberia',
'fc' :'Four Corners',
'cp' :'Central Plains',
'us' :'United States',
}
return lc[re]
def window(relb):
lonlat={
'af' :[-25,55,-40,40],
'ar' :[-180,50,50,80],
'as' :[50,150,0,60],
'au' :[90,180,-50,15],
'eu' :[-25,75,10,80],
'ic' :[90,120,0,30],
'gr' :[-75,-10,55,85],
'na' :[-140,-50,5,60],
'sa' :[-90,-30,-60,20],
'si' :[60,180,45,80],
}
return lonlat[relb]
def refigsize(relb):
fs={
'af' :(8,9),
'ic' :(9,8),
'sa' :(8,9),
}
return fs[relb]