-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeltares_drought_functions.py
146 lines (122 loc) · 4.22 KB
/
deltares_drought_functions.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
import os
os.environ["USE_PYGEOS"] = "0"
import pandas as pd
import numpy as np
import geopandas as gpd
from shapely.geometry import Point
import time
from netCDF4 import Dataset
import matplotlib.pyplot as plt
def plot_forecast_timeseries(dataset, parameter, x, y):
"""
Creates a graph with forecast timeseries (all ensemble members and median) of a specific xy point locations
Args:
- dataset (netCDF4.Dataset): a netcdf dataset class in with the netCDF4 package
- parameter (str): one of the parameters provided by Deltares (qa, smdi, etdi)
- x (float): x coordinate (EPSG:4326)
- y (float): y coordinate (EPSG:4326)
Returns:
- fig (matplotlib figure): figure object with timeseries.
"""
timestamps = [
pd.to_datetime(time.ctime(t * 60)) for t in dataset.variables["time"][:]
]
lons = np.array(dataset.variables["x"])
lats = np.array(dataset.variables["y"])
fig, ax = plt.subplots(figsize=(16, 8))
ax.set_xlabel("Date")
ax.set_ylabel("index")
ax.grid()
for ensemble in range(51):
values = parameter[
:, ensemble, (np.abs(lons - x)).argmin(), (np.abs(lats - y)).argmin()
]
ax.plot(timestamps, values, color="grey", alpha=0.25)
medians = []
for t in range(107):
medians.append(
np.median(
parameter[
t, :, (np.abs(lons - x)).argmin(), (np.abs(lats - y)).argmin()
]
)
)
ax.plot(timestamps, medians, color="firebrick", linewidth=3)
return fig
def plot_forecast_spatial(
dataset,
parameter,
timestamp,
percentiles=[10, 50, 90],
vmin=-2,
vmax=2,
colormap="coolwarm",
):
"""
Creates a subplot with 3 spatial maps of a 3 percentile values of a certain dataset on a certain timestamp
Args:
- dataset (netCDF4.Dataset): a netcdf dataset class in with the netCDF4 package
- parameter (str): one of the parameters provided by Deltares (qa, smdi, etdi)
- timestamp (str): timestamp in 'yyyy-mm-dd' format
- percentiles (list): list with the three percentile values to plot
- vmin (float): minimum value to be used for colormap
- vmax (float): maximum value to be used for colormap
- colormap (str): colormap to be used for the maps
Returns:
- fig (matplotlib figure): figure object with spatial maps.
"""
timestamps = [
pd.to_datetime(time.ctime(t * 60)).strftime("%Y-%m-%d")
for t in dataset.variables["time"][:]
]
index = timestamps.index(timestamp)
fig, axs = plt.subplots(1, 3, figsize=(15, 3))
axs[0].imshow(
np.flip(
np.percentile(parameter[index, :, :, :], percentiles[0], axis=0), axis=0
),
vmin=vmin,
vmax=vmax,
cmap=colormap,
)
axs[0].set_title("{} - p{}".format(timestamp, percentiles[0]))
axs[0].grid()
axs[1].imshow(
np.flip(
np.percentile(parameter[index, :, :, :], percentiles[1], axis=0), axis=0
),
vmin=vmin,
vmax=vmax,
cmap=colormap,
)
axs[1].set_title("{} - p{}".format(timestamp, percentiles[1]))
axs[1].grid()
p90 = axs[2].imshow(
np.flip(
np.percentile(parameter[index, :, :, :], percentiles[2], axis=0), axis=0
),
vmin=vmin,
vmax=vmax,
cmap=colormap,
)
axs[2].set_title("{} - p{}".format(timestamp, percentiles[2]))
axs[2].grid()
cbar = fig.colorbar(p90, ax=axs, orientation="vertical", fraction=0.1)
cbar.set_label("Index values")
return fig
def pixels_to_gdf(dataset):
"""
Creates a geodataframe with points for each pixel center
Args:
- dataset (netCDF4.Dataset): a netcdf dataset class in with the netCDF4 package
Returns:
- gdf (geopandas.GeoDataFrame): geodataframe with points for each pixel center
"""
lats = np.abs(dataset.variables["y"])
lons = np.abs(dataset.variables["x"])
points = []
for x in lons:
for y in lats:
points.append(Point(x, y))
gdf = gpd.GeoDataFrame(geometry=points)
return gdf