forked from SercoSPA/DESP-UserWorkflowService-Templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.py
More file actions
175 lines (137 loc) · 5.62 KB
/
Copy pathdisplay.py
File metadata and controls
175 lines (137 loc) · 5.62 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
from cartopy import crs, feature
from matplotlib import pyplot as plt
import numpy as np
import xarray as xr
VMAX = 200
PROJECTION = crs.EqualEarth()
CMAP = "Blues"
xr.set_options(keep_attrs=True)
def map(data, location=None, vmax=VMAX, vmin=None, projection=PROJECTION, cmap=CMAP, ax=None, figsize=None, extent=None, **kwargs):
if ax is None:
_, ax = plt.subplots(subplot_kw={"projection": projection}, figsize=figsize)
data.plot(ax=ax, cmap=cmap, vmax=vmax, vmin=vmin, transform=crs.PlateCarree())
if location is not None:
ax.plot(
location["longitude"],
location["latitude"],
"+",
edgecolor="red",
transform=crs.PlateCarree(),
)
ax.coastlines(linewidth=0.5)
ax.add_feature(feature.BORDERS, linewidth=0.5, edgecolor='dimgrey')
ax.set(**kwargs)
if extent:
ax.set_extent(extent)
return ax
def maps(data, vmax=VMAX, projection=PROJECTION, cmap=CMAP, axs_set=[]):
f, axs = plt.subplots(
1, len(data), subplot_kw={"projection": projection}, figsize=(16, 6)
)
if len(axs_set) < len(data):
axs_set.extend([{}] * (len(data) - len(axs_set)))
for ax, d, kwargs in zip(axs, data, axs_set):
map(d, vmax=vmax, projection=projection, cmap=cmap, ax=ax, **kwargs)
return axs
def compare(data, historic, time="time", ylim=[0, 1300]):
data_sum = (
data.resample({time: "D"})
.sum()
.assign_coords(dayofyear=data[time].dt.dayofyear)
.swap_dims({time: "dayofyear"})
.cumsum("dayofyear")
) * 1000
data_sum.attrs["units"] = "mm"
historic_sum = (
historic.resample({time: "D"})
.sum()
.assign_coords(
year=historic[time].dt.year, dayofyear=historic[time].dt.dayofyear
)
.set_index({time: ["year", "dayofyear"]})
.unstack(time)
.cumsum("dayofyear")
) * 1000
historic_sum.attrs["units"] = "mm"
historic_quantile = historic_sum.quantile([0.1, 0.9], dim="year")
historic_mean = historic_sum.mean("year")
_, ax = plt.subplots(figsize=(10, 6))
line1 = historic_quantile.isel(quantile=0).plot.line(x="dayofyear", c="green", alpha=0.8, linewidth=0.8, add_legend=True, ax=ax)
line2 = historic_quantile.isel(quantile=1).plot.line(x="dayofyear", c="orange", alpha=0.8, linewidth=0.8, add_legend=True, ax=ax)
line4 = historic_mean.plot.line(x="dayofyear", c="black", alpha=0.8, linewidth=0.8, add_legend=True, ax=ax)
line5 = data_sum.plot.line(x="dayofyear", c="red", linewidth=1.5, add_legend=True, ax=ax, label = 'something')
line3 = historic_sum.plot.line(x="dayofyear", c="black", alpha=0.05, linewidth=1, add_legend=False, ax=ax)
ax.fill_between(
historic_quantile.dayofyear,
historic_quantile.sel(quantile=0.9),
historic_quantile.sel(quantile=0.1),
alpha=0.5,
facecolor="gray"
)
month_starts = [1,32,61,92,122,153,183,214,245,275,306,336]
month_names = ['Jan', 'Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
ax.set(xlim=[-10, 366], ylim=ylim, title=None)
ax.set_yticks(ticks=np.arange(0,int(data_sum[-1].values), 200))
ax.set_xticks(month_starts)
ax.set_xticklabels(month_names)
ax.set_xlabel(None)
ax.set_ylabel(None)
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)
return ax
def albers_equal_area(da, ax=None, vmax=None, vmin=None, add_colorbar=True, cbar_kwargs=None, contour=False):
if ax is None:
fig, ax = plt.subplots(figsize=(15, 15), subplot_kw={"projection": crs.AlbersEqualArea()})
if contour == True:
da.plot.contourf(
ax=ax,
transform=crs.PlateCarree(),
cmap="coolwarm",
vmin=vmin,
vmax=vmax,
add_colorbar=add_colorbar,
cbar_kwargs=cbar_kwargs
)
else:
da.plot(
ax=ax,
transform=crs.PlateCarree(),
cmap="coolwarm",
vmin=vmin,
vmax=vmax,
add_colorbar=add_colorbar,
cbar_kwargs=cbar_kwargs
)
lat = da.latitude
lon = da.longitude
ax.set_extent([min(lon) + 3, max(lon) - 4, min(lat) + 4, max(lat) - 4], crs=crs.PlateCarree())
ax.set_title("")
ax.add_feature(feature.COASTLINE)
ax.add_feature(feature.LAKES.with_scale("10m"), color='forestgreen')
ax.add_feature(feature.RIVERS)
ax.add_feature(feature.COASTLINE, linewidth=0.5)
ax.add_feature(feature.BORDERS, linestyle=':', linewidth=0.5)
ax.add_feature(feature.OCEAN, facecolor='lightblue', zorder=2)
ax.gridlines(draw_labels=True)
def compare_map(da1, da2, title_0="", title_1="", contour=False):
if 'GRIB_units' in da1.attrs and da1.attrs['GRIB_units'] == 'K':
da1 = da1 - 273.15
da1.attrs["units"] = "°C"
if 'GRIB_units' in da2.attrs and da2.attrs['GRIB_units'] == 'K':
da2 = da2 - 273.15
da2.attrs["units"] = "°C"
fig, axes = plt.subplots(1, 2, figsize=(15, 15), subplot_kw={"projection": crs.AlbersEqualArea()})
vmax = max(da1.values.max(), da2.values.max())
vmin = max(da1.values.min(), da2.values.min())
cbar_kwargs = {
# 'label': '',
'shrink': 0.51, # % of the plot heigth
'aspect': 15, # height/width ratio
'pad': 0.1 # padding
}
albers_equal_area(da1, axes[0], vmax, vmin, add_colorbar=True, cbar_kwargs=cbar_kwargs, contour=contour)
albers_equal_area(da2, axes[1], vmax, vmin, add_colorbar=True, cbar_kwargs=cbar_kwargs, contour=contour)
axes[0].set_title(title_0)
axes[1].set_title(title_1)
plt.tight_layout()
plt.show()