-
Notifications
You must be signed in to change notification settings - Fork 2
/
extreme_precipitation_functions.py
38 lines (37 loc) · 1.47 KB
/
extreme_precipitation_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
import pandas as pd
import numpy as np
def get_duration_and_size(lab, rettype='df'):
"""Returns either a Pandas DataFrame (default) or dictionary with keys that are event labels and values that are duration (days)."""
dur = dict()
# put eventID as key, values are dict: {duration: D, size: [s_0, ..., s_d]}
if isinstance(lab, np.ndarray):
labv = lab
else:
labv = lab.values # converts to numpy array
for t in range(lab.shape[0]):
tmp = labv[t,:,:] # temporary time slice
present_events = set(tmp[tmp != 0]) # uniq events in this time
if len(present_events) > 0:
for ev in present_events:
if ev in dur:
dur[ev]['duration'] += 1 # continuing
dur[ev]['size'].append(np.count_nonzero(tmp==ev))
else:
dur[ev] = dict() # new event
dur[ev]['duration'] = 1
dur[ev]['size'] = [np.count_nonzero(tmp==ev)]
if rettype == 'df':
# convert to dataFrame
df = pd.DataFrame.from_dict(dur, orient ='index')
# compute average size and include as a column
asiz = []
msiz = []
for s in df['size']:
asiz.append(np.mean(s))
msiz.append(np.max(s))
df['AvgSiz'] = asiz
df['MaxSiz'] = msiz
# get distribution of average size
return df
else:
return dur # AvgSiz and MaxSiz not included in output dict.