-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_wind.py
More file actions
363 lines (308 loc) · 11.6 KB
/
map_wind.py
File metadata and controls
363 lines (308 loc) · 11.6 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python3
"""
@author: tylunel
Creation : 07/01/2021
"""
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
import metpy.calc as mcalc
from metpy.units import units
import pandas as pd
import tools
import shapefile
import global_variables as gv
from metpy.plots import StationPlot
import os
#########################################"""
model = 'irrswi1_d1'
domain_nb = 1
ilevel = 3 #0 is Halo, 1:2m, 2:6.1m, 3:10.5m, 10:49.3m, 20:141m, 30:304m, 40:600m, 50:1126m, 60:2070, 66:2930
# Datetime
wanted_date = '20210722-1200'
speed_plane = 'horiz' # 'horiz': horizontal normal wind, 'verti' for W
if speed_plane == 'verti':
vmax_cbar = 1
vmin_cbar = -vmax_cbar
cmap_name = 'seismic'
elif speed_plane == 'horiz':
vmax_cbar = 15
vmin_cbar = 0
cmap_name = 'BuPu'
zoom_on = 'urgell-paper' #None for no zoom, 'urgell', 'liaise'
add_smc_obs = True
save_plot = True
save_folder = f'./figures/winds/{model}/{speed_plane}/{ilevel}/zoom_{zoom_on}/'
barb_size_option = 'standard' # 'weak_winds' or 'standard'
isoalti_list = [600]
###########################################
if add_smc_obs:
alpha = 0.4
if zoom_on == 'marinada':
barb_length_coeff = 1.2
else:
barb_length_coeff = 1.1
else:
alpha = 0.9
if add_smc_obs and ilevel > 6:
raise ValueError(f"""Height of model level and of observation stations
are significantly different:
- SMC stations: 2-10m
- model: {gv.layers_height_MNH_LIAISE[ilevel]}m""")
barb_size_increments = gv.barb_size_increments
barb_size_description = gv.barb_size_description
prop = gv.zoom_domain_prop[zoom_on]
skip_barbs = prop['skip_barbs']
skip_barbs = 2
barb_length = prop['barb_length']
lat_range = prop['lat_range']
lon_range = prop['lon_range']
figsize = prop['figsize']
# OR: #locals().update(gv.zoom_domain_prop[zoom_on])
# for paper: hard-coded:
# skip_barbs = 3
#barb_length = 5.5
#barb_size_option = 'weak_winds'
plt.rcParams.update({'font.size': 11})
filename = tools.get_simu_filepath(model, wanted_date,
global_simu_folder=gv.global_simu_folder)
# load file, dataset and set parameters
ds1 = xr.open_dataset(filename,
decode_coords="coordinates",
)
# other calculations
ds1['DIV'] = mcalc.divergence(ds1['UT'], ds1['VT'])
fig1 = plt.figure(figsize=figsize)
#%% WIND SPEED COLORMAP
if speed_plane == 'horiz':
ws = mcalc.wind_speed(ds1['UT'] * units.meter_per_second,
ds1['VT'] * units.meter_per_second)
elif speed_plane == 'verti':
ws = ds1['WT']
# keep only layer of interest
ws_layer = ws.squeeze()[ilevel, :, :]
#ws_layer = ds1['ZS'].squeeze()[:, :]
plt.pcolormesh(ds1.longitude, ds1.latitude, ws_layer,
# cbar_kwargs={"orientation": "horizontal", "shrink": 0.7}
cmap=cmap_name,
vmin=vmin_cbar,
vmax=vmax_cbar,
alpha=0.8,
# vmin=0,
# vmax=1000
)
cbar = plt.colorbar()
cbar.set_label('Wind speed [m/s]')
#%% WIND BARBS
X = ds1.longitude
Y = ds1.latitude
U = ds1['UT'].squeeze()[ilevel, :,:]
V = ds1['VT'].squeeze()[ilevel, :,:]
plt.barbs(X[::skip_barbs, ::skip_barbs], Y[::skip_barbs, ::skip_barbs],
U[::skip_barbs, ::skip_barbs], V[::skip_barbs, ::skip_barbs],
pivot='middle',
length=barb_length, #length of barbs
sizes={
# 'spacing':1,
# 'height':1,
# 'width':1,
'emptybarb':0.01},
barb_increments=barb_size_increments[barb_size_option],
alpha=alpha,
)
plt.annotate(barb_size_description[barb_size_option],
fontsize=9,
xy=(0.1, 0.015),
xycoords='subfigure fraction'
)
#%% IRRIGATED, SEA and COUNTRIES BORDERS
if domain_nb == 2:
pgd = xr.open_dataset(
gv.global_simu_folder + \
'2.01_pgds_irr/PGD_400M_CovCor_v26_ivars.nc')
elif domain_nb == 1:
pgd = xr.open_dataset(
gv.global_simu_folder + \
'2.01_pgds_irr/PGD_2KM_CovCor_v26_ivars.nc')
#Irrigation borders
#from scipy.ndimage.filters import gaussian_filter
#sigma = 0.1 #default is 0.1
#irr_covers = gaussian_filter(pgd.COVER369.data, sigma)
irr_covers = pgd.COVER369.data
plt.contour(pgd.longitude.data,
pgd.latitude.data,
irr_covers,
levels=0, #+1 -> number of contour to plot
linestyles='solid',
linewidths=1.5,
colors='g'
# colors=['None'],
# hatches='-'
)
#Sea borders
sea_covers = pgd.COVER001.data
plt.contour(pgd.longitude.data,
pgd.latitude.data,
sea_covers,
levels=0, #+1 -> number of contour to plot
linestyles='solid',
linewidths=1.,
colors='k'
# colors=['None'],
# hatches='-'
)
#France borders
sf = shapefile.Reader("TM-WORLD-BORDERS/TM_WORLD_BORDERS-0.3.sph")
shapes=sf.shapes()
france = shapes[64].points
france_df = pd.DataFrame(france, columns=['lon', 'lat'])
france_S = france_df[france_df.lat < 43.35]
france_SW = france_S[france_S.lon < 2.95]
plt.plot(france_SW.lon, france_SW.lat,
color='k',
linewidth=1)
# --- ISOLINES FOR ALTI
# contour line of isoaltitude
isoalti = ds1['ZS']
plt.contour(isoalti.longitude.data,
isoalti.latitude.data,
isoalti,
levels=isoalti_list, #+1 -> number of contour to plot
linestyles=':',
linewidths=1.,
colors='k',
)
#%% POINTS SITES
points = ['cendrosa', 'elsplans',
# 'irta-corn',
# 'border_irrig_noirr',
# 'puig formigosa',
# 'tossal_baltasana',
# 'lleida',
# 'tossal_gros',
# 'tossal_torretes',
# 'coll_lilla',
#'moncayo', 'tres mojones',
# 'guara', 'caro', 'montserrat', 'joar',
]
sites = {key:gv.whole[key] for key in points}
for site in sites:
plt.scatter(sites[site]['lon'],
sites[site]['lat'],
color='r',
s=15 #size of markers
)
# print site name on fig:
try:
sitename = sites[site]['longname']
except KeyError:
sitename = site
plt.text(sites[site]['lon']+0.01,
sites[site]['lat']+0.01,
sitename,
fontsize=15)
#%% STATION PLOT
if add_smc_obs:
ax = plt.gca()
# --- with SMC data of liaise database ---
# stations_2m = ['VH', 'WK', 'V1', 'WB', 'VM', 'WX', 'WA', 'WC', 'V8', 'XI',
# 'XM', 'WL', 'UM', 'WI', 'VE']
# stations_10m = ['VK', 'C6', 'C7', 'C8', 'D1', 'XD', 'XR', 'XA', 'VP', 'VB','VQ']
# stations_unk = ['YJ', 'CW', 'MR', 'VM', 'WV', 'VD', 'YD', 'XX', 'YJ', ]
# stations_all = stations_2m + stations_10m + stations_unk
#
# for station in stations_all:
# # get data
# datafolder = gv.global_data_liaise + '/SMC/22_stations_liaise/'
# filename = f'LIAISE_{station}_SMC_MTO-1MN_L0_{wanted_date[:8]}_V01.nc'
# try:
# obs = xr.open_dataset(datafolder + filename)
# # find closest time
# obs['time_dist'] = np.abs(obs.time - pd.Timestamp(wanted_date).to_datetime64())
# obs_t = obs.where(obs['time_dist'] == obs['time_dist'].min(),
# drop=True).squeeze()
# except (FileNotFoundError, ValueError):
# continue
#
# obs_t['UT'], obs_t['VT'] = tools.calc_u_v(obs_t['WS'], obs_t['WD'])
#
# # plot station
# location = StationPlot(ax, obs['lon'], obs['lat'])
# location.plot_barb(obs_t['UT'], obs_t['VT'],
# pivot='middle',
# length=barb_length*barb_length_coeff, #length of barbs
# sizes={'emptybarb':0.1},
# barb_increments=barb_size_increments[barb_size_option]
# )
# --- with global SMC data ---
datafolder = gv.global_data_liaise + '/SMC/ALL_stations_july/'
for filename in os.listdir(datafolder):
# filename = 'C6.nc'
file_path = os.path.join(datafolder, filename)
if os.path.isfile(file_path):
try:
obs = xr.open_dataset(file_path)
# struggling with the datetime formats in the 3 next lines...
obs['datetime'] = [pd.Timestamp(str((elt.data))) for elt in obs['datetime']]
obs['datetime64'] = [elt.data.astype('int64') for elt in obs['datetime']]
obs['datetime64'] = obs['datetime64'].swap_dims({'datetime64' :'datetime'})
# compute distance to wanted datetime
obs['time_dist'] = np.abs(obs['datetime64'] - pd.Timestamp(wanted_date).to_datetime64().astype('int64'))
# keep the data closest to wanted datetime
obs_t = obs.where(obs['time_dist'] == obs['time_dist'].min(),
drop=True).squeeze()
# get height of wind measurement
wind_height = int((obs_t['obs_wind_height'].data))
obs_t['UT'], obs_t['VT'] = tools.calc_u_v(
obs_t[f'VV{wind_height}'], obs_t[f'DV{wind_height}'])
# --- plot station data ---
if (lon_range[0] < obs['lon'] < lon_range[1] and \
lat_range[0] < obs['lat'] < lat_range[1]):
# Create the station object and set the location of it
location = StationPlot(ax, obs['lon'], obs['lat'])
# plot the wind
location.plot_barb(
obs_t['UT'].data, obs_t['VT'].data,
pivot='tip', # 'tip' or 'middle'
length=barb_length*barb_length_coeff, #length of barbs
sizes={'emptybarb':0.1},
barb_increments=barb_size_increments[barb_size_option]
)
if wind_height != 10:
ax.text(obs['lon']+0.008, obs['lat']-0.016,
wind_height,
fontsize=7)
print(f'{filename} plotted')
except (FileNotFoundError, ValueError, IndexError, TypeError) as e:
print(f"Error for {obs['station_name']}:")
if hasattr(e, 'message'):
print(e.message)
else:
print(e)
continue
#%% FIGURE OPTIONS
if speed_plane == 'horiz':
level_agl = ws_layer.level
if speed_plane == 'verti':
level_agl = ws_layer.level_w
plt.xlabel('longitude')
plt.ylabel('latitude')
plot_title = '{0}m wind - {1} - {2}'.format(
np.round(level_agl, decimals=1),
pd.to_datetime(ws_layer.time.values).strftime('%Y-%m-%dT%H%M'),
model)
plt.title(plot_title)
if zoom_on is not None:
# plt.ylim([ws_layer.latitude.min(), ws_layer.latitude.max()])
# plt.xlim([ws_layer.longitude.min(), ws_layer.longitude.max()])
# else:
plt.ylim(lat_range)
plt.xlim(lon_range)
save_title = '{4} wind at {0}m on {1} for simu {2} zoomed on {3}'.format(
np.round(level_agl, decimals=1),
pd.to_datetime(ws_layer.time.values).strftime('%Y-%m-%dT%H%M'),
model,
zoom_on,
speed_plane)
if save_plot:
tools.save_figure(save_title, save_folder)