forked from chrisba92/3d-rainfall-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaily-rainfall-resample.py
816 lines (605 loc) · 26.2 KB
/
daily-rainfall-resample.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
import pandas as pd
import numpy as np
import copy
from scipy import special
from scipy.linalg import cholesky
import matplotlib.pyplot as plt
import sys
# =============================================================================
# Functions in script
# =============================================================================
def rainfall_stat(data, timeres):
'''
Function to calculate selected statistics of the rainfall timeseries
=====
Input
=====
df : Pandas Dataframe, a data frame containing the rainfall intesities for each timestep - The index column must be a datetime format of sorts
timesres : int or float, temporal resolution of the timeseries, in minutes
'''
# Calculate statistics
rainstats = {}
# Calculate rain amount in every time step
IntSum = data.Int / 60 * timeres
data['IntSum'] = IntSum
# Annual precipitation
ap = data.IntSum.groupby(lambda x: x.year).sum()
# Save stats
rainstats['ap'] = [ap.mean(), ap.std()]
# Seasonal precipitation
test = data.IntSum.resample('M').sum().to_frame()
month = data.IntSum.resample('M').sum()
month_mean = month.groupby(lambda x: x.month).mean()
month_var = month.groupby(lambda x: x.month).var()
rainstats['month'] = [month_mean.values, month_var.values]
# Create a Seasonal Dictionary that will map months to seasons
SeasonDict = {11: 'Autumn', 12: 'Winter', 1: 'Winter', 2: 'Winter', 3: 'Spring', 4: 'Spring', 5: 'Spring', 6: 'Summer', 7: 'Summer', \
8: 'Summer', 9: 'Autumn', 10: 'Autumn'}
# Call the function with the groupby operation.
sp = test.IntSum.groupby([lambda x: x.year, lambda x: SeasonDict[x.month]]).sum()
sp = sp.to_frame()
labels = ['Winter', 'Spring', 'Summer', 'Autumn']
sp_var = []
for label in labels:
sp_var.append(sp.iloc[sp.index.get_level_values(1).str.contains(label)].var().values)
rainstats['sp_var'] = [sp_var]
# Winter
rainstats['spwi'] = [sp.iloc[sp.index.get_level_values(1).str.contains('Winter')].mean().values[0], sp.iloc[sp.index.get_level_values(1).str.contains('Winter')].std().values[0]]
# Autumn
rainstats['spau'] = [sp.iloc[sp.index.get_level_values(1).str.contains('Autumn')].mean().values[0], sp.iloc[sp.index.get_level_values(1).str.contains('Autumn')].std().values[0]]
# Summer
rainstats['spsu'] = [sp.iloc[sp.index.get_level_values(1).str.contains('Summer')].mean().values[0], sp.iloc[sp.index.get_level_values(1).str.contains('Summer')].std().values[0]]
# Spring
rainstats['spsp'] = [sp.iloc[sp.index.get_level_values(1).str.contains('Spring')].mean().values[0], sp.iloc[sp.index.get_level_values(1).str.contains('Spring')].std().values[0]]
return rainstats
def mult_exp(x, a, l):
r"""Mulit-exponential function
Parameters
----------
x : float
Uniform random number
a : float
Ratio of rain events in the class and total number of rain events
l : float
Inverse mean of rainfall depth in the class
"""
return a * (-(1/l) * np.log(1- x))
def is_pos_def(mat):
r"""Test if the input matrix is positive definite.
Parameters
----------
mat : array_like
Correlation matrix to be tested
Returns
----------
test_result : bool
Returns True of correlation matrix is positive definite
and False otherwise
"""
return np.all(np.linalg.eigvals(mat) > 0)
def normal2uniform(rnd):
r"""Convert standard normal distributed numbers (N(1, 0)) to uniform distributed numbers (U[0, 1]).
Parameters
----------
rnd : numpy ndarray
ndarray of standard nomral distributed numbers to be converted.
Returns
----------
z : numpy ndarray
Transformed random numbers
"""
z = np.zeros(rnd.shape)
for i, r in enumerate(rnd):
z[i,:] = 0.5 * special.erfc(-r / np.sqrt(2))
return z
def diagonalize(mat):
r"""Diagonalize non-positive indefinte matrix following
the procedure from ....
Parameters
----------
mat : numpy ndarray
Non-definite matrix that needs to be diagonlized into a
positive definite one
Returns
----------
cr_ new : new positive definite correlation matrix
"""
# Calculate eigenvalues and vectors
d, m = np.linalg.eig(mat)
d = np.diag(d)
# Replace negative eigenvalues
d[d<0] = 1e-7
# Perform diagonalzation
cr = m @ d @ m.T
# Normalize matrix
diag_cr = np.diag(cr)
diag_cr = np.reshape(diag_cr, (diag_cr.size, 1))
cr_new = cr / np.sqrt(diag_cr @ diag_cr.T)
return cr_new
def correlated_rnd(corr_mat, rnd):
r"""Creation of correlated random numbers using cholesky factorization
Parameters
----------
corr_mat : numpy ndarray
Correlation matrix used to create the correlated random numbers.
rnd : numpy ndarray
ndarray of random numbers
Returns
----------
z : ndarray of the correlated random numbers
"""
# Compute the (lower) Cholesky decomposition matrix
chol = cholesky(corr_mat, lower=True)
# Generate 3 series of normally distributed (Gaussian) numbers
ans = chol @ rnd
z = normal2uniform(ans)
return z
def execute_markov(m, n, rnd, markov_models, names):
r"""Feed correlated random numbers through a markov chain
Parameters
----------
m : int
Number of measuring rainfall stations.
n : int
Number of sequences to simulate.
rnd : numpy ndarray
Matrix containg correlated uniform random numbers.
markov_models : dcit
Dictonary containg the markov chains for each station.
names : array_like
list or np array containg the given name to the rainfall stations.
Returns
-------
seq : numpy ndarray
Matrix with the state at each timestep for each station.
"""
# Feed the random numbers through a markov process
seq = np.zeros((m, n))
seq[-1,0] = 1
for i in range(1, n):
pre_seq = seq[:,i-1]
probs_full = rnd[:,i]
for j in range(m):
trans = markov_models[names[j]]['trans']
if pre_seq[j] == 0:
pc = trans[0,0]
else:
pc = trans[1,0]
if probs_full[j] <= pc:
seq[j,i] = 0
else:
seq[j,i] = 1
return seq
def calc_occindex(occurence, m, occ_corr_org):
r"""
Parameters
----------
occurence : TYPE
DESCRIPTION.
m : TYPE
DESCRIPTION.
occ_corr_org : TYPE
DESCRIPTION.
Returns
-------
occ_index : TYPE
DESCRIPTION.
"""
# Initialize the occurence index array
occ_index = np.zeros(occurence.shape)
for i in range(m):
# Find all days with rain for the current station
ids = np.where(occurence[:,i]>0)[0]
# Look up the the current stations correlation with the other stations
c = copy.copy(occ_corr_org[i,:])
c = np.delete(c, i)
# Unit vector - needed for the calucations
u = np.ones(c.shape)
# Go through each rainy day and calculate occurence index
for id_ in ids:
o = copy.copy(occurence[id_,:])
o = np.delete(o, i)
km = np.dot(o,c) / np.dot(u, c)
occ_index[id_, i] = km
return occ_index
def determine_corrmat(corr_mat, org_corr_mat, corr_type='occurence', lr=0.1, n_sim=1000):
r"""Automatic determination of new correlation matrix
Parameters
----------
corr_mat : numpy ndarray
Initial guess of correlation matrix.
org_corr_mat : numpy ndarray
Target correlation matrix - the output using corr_mat should
be comparable with this.
lr : float, optional
Convergence critieon, low value will result in higher accuracy but
lower convergence speed. The default is 0.1.
n_sim : int, optional
Maximum number of iterations. The default is 1000.
Returns
-------
corr_mat : numpy ndarray
Converged, positive definite version of corr_mat
"""
fitness = []
rnd_ = np.random.normal(0.0, 1.0, size=(m, n))
last_avg = None
for p in range(n_sim):
# Test if the current correlation matrix is positive definite
if is_pos_def(corr_mat) == False:
# Diagonlize the matrix if it is not definite
corr_mat = diagonalize(corr_mat)
# Create correlated random, uniform, numbers
rnd = correlated_rnd(corr_mat, rnd_)
if corr_type=='occurence':
# Feed the random numbers through a markov process
rnd = execute_markov(m, n, rnd, markov_models, names)
# Get correlation
corr_temp = pd.DataFrame(data=rnd.T, columns=names).corr()
# Get the difference between original correlation matrix and new one
dif = org_corr_mat - corr_temp.values
# Add the difference to the correlation matrix
corr_mat = corr_mat + lr*dif
# Log the score of the solution scheme
fitness.append(np.sum(np.abs(dif)))
# After 10 runs, test if the scheme converged
if p>=10:
# Get the last 10 fitness scores
temp = np.array(fitness[-10:])
# Calculate the change between each iteration
change = np.abs(temp[1:] - temp[:-1])
# Get the average change
avg = np.mean(change)
# Test if scheme have converged
if last_avg is not None and np.isclose(avg, last_avg, atol=1e-4) and fitness[-1]<1:
print('\tsolution scheme converged!')
print(f'\tthe score ended up at {fitness[-1]:.3f}')
break
last_avg = avg
if p==n_sim-1:
print('\tmaximum number of iteration hit...')
print(f'\tcurrent score is {fitness[-1]}')
# Diagonlize the final matrix, if it is not positive definite
if is_pos_def(corr_mat) == False:
# Diagonlize the matrix if it is not definite
corr_mat = diagonalize(corr_mat)
return corr_mat
# =============================================================================
# Load and process the rainfall data
# =============================================================================
print('Loading the daily rainfall data...', end='')
# Set the rainfall file to be loaded - NB! Should be a file of daily rainfall amounts
filename = 'daily_rainfall.csv'
# Load the rainfall file into a pandas dataframe
df = pd.read_csv(filename,
index_col=0,
parse_dates=['Dates'])
# Extract the values to a np array
daily = df.values
# Store column names for later use
names = df.columns.values
# Filter out low values Anything lower than the resolution
daily[daily<0.3] = 0
# Get correlation matrix of the rainfall amounts
rainfall_corr_obs = df.corr()
# Transform into occurence array - 1 means wet day, 0 means dry day
occurence = copy.copy(daily)
occurence[occurence>0] = 1
# Determine the correlation matrix of the occurences
occ_df = pd.DataFrame(data=occurence, columns=names)
occ_corr_org = occ_df.corr().values
# Print out status of the data processing
print('Done!')
# =============================================================================
# Fit markov chain to each of the gauges
# =============================================================================
print('Fitting a markov chain to each of the rain gauges...', end='')
markov_models = {}
for j in range(daily.shape[1]):
name = names[j]
# Setup markov model
markov_models[name] = {}
markov_models[name][0] = [] # Dry state
markov_models[name][1] = [] # Wet state
# Get sequences for the current rain gauge
seq = occurence[:,j]
# Extract dependt occurences
for i in range(seq.size-1):
markov_models[name][seq[i]].append(seq[i+1])
# Create transistion matrix
trans = np.zeros((2,2))
trans[0,0] = np.sum(np.array(markov_models[name][0])==0) / len(markov_models[name][0])
trans[0,1] = np.sum(np.array(markov_models[name][0])==1) / len(markov_models[name][0])
trans[1,0] = np.sum(np.array(markov_models[name][1])==0) / len(markov_models[name][1])
trans[1,1] = np.sum(np.array(markov_models[name][1])==1) / len(markov_models[name][1])
# Save the model
markov_models[name]['trans'] = trans
print('Done!')
# =============================================================================
# Autodetermination of new correlation matrix
# =============================================================================
print('Determining new correlation matrix...')
# Set random seed
np.random.seed(1234)
# Total number of sequences to model
n = occurence.shape[0]
# Set number of stations
m = occurence.shape[1]
# Copy original occurence array - Using copy to avoid pointer issues
occ_corr = copy.copy(occ_corr_org)
occ_corr = determine_corrmat(occ_corr, occ_corr_org)
# Copy rainfall correlation array - Using copy to avoid pointer issues
rainfall_corr = copy.copy(rainfall_corr_obs.values)
rainfall_corr = determine_corrmat(rainfall_corr, rainfall_corr_obs.values, corr_type='rainfall')
#%%
# =============================================================================
# Build model for daily rainfall amounts
# =============================================================================
print('Creating model for daily rainfall amounts...')
# Calculate occurence indexs for each station
print('\tcalculating occurence index..')
occ_index = calc_occindex(occurence, m, occ_corr_org)
np.savetxt()
# Create season array
seasons = [[12, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11]]
season_name = ['DJF', 'MAM', 'JJA', 'SON']
# Initialze multip exponential model
print('\tcreating multi-exponential model...')
alpha_list = []
lambda_list = []
retbin_list = []
for i in range(occ_index.shape[1]):
n_class = 11
df_temp = pd.DataFrame(data=occ_index[:, i], columns=['occ_index'])
df_temp = df_temp.drop(df_temp[df[names[i]].values==0].index)
# Categorize the data into 6(4) different classes - FIGURE OUT A WAY TO AUTO DETERMINE THE NCLASS
if i>=0:
_, bins = pd.qcut(df_temp['occ_index'], n_class,
duplicates='drop', retbins=True)
n_class = bins.size-1
df_temp['class'] = pd.cut(df_temp['occ_index'], bins=bins, labels=np.arange(n_class), include_lowest=True)
# Save the class definition for the model
retbin_list.append(bins)
# Locate rainfall amount for each class and month
month_class_mean = {}
for k, (val, class_) in enumerate(zip(df_temp['occ_index'], df_temp['class'])):
if class_ not in month_class_mean: # If class haven't been added to the dict, initialize it
month_class_mean[class_] = {}
# Extract the month
month = df.index[k].month
if month not in month_class_mean[class_]: # Add the month to the dict, if it does not exist
month_class_mean[class_][month] = []
month_class_mean[class_][month].append(df.iloc[df_temp.index[k], i])
# Go through each class, and calculate the mean and save it for each season
result = {}
for key in range(n_class):
class_values = month_class_mean[key]
result[key] = {}
for m, season in enumerate(seasons):
result[key][season_name[m]] = []
temp = []
for s in season:
if s in class_values:
temp.extend(class_values[s])
season_average = np.mean(temp)
result[key][season_name[m]].append(season_average)
lambda_list.append(result)
# Build multi-exponential distribution
alpha = []
for class_ in range(n_class):
temp = []
for month in month_class_mean[class_]:
temp.append(len(month_class_mean[class_][month]))
alpha.append(np.sum(temp))
alpha = np.array(alpha) / np.sum(alpha)
alpha_list.append(alpha)
#break
from scipy.optimize import curve_fit
def func(x, a, b, c):
return a*x**2 + b*x + c
# Visualize the multi-expon fit
for season in season_name:
xdata = np.arange(1, bins.size)
ydata = []
for key in result:
ydata.append(result[key][season][0])
popt, _ = curve_fit(func, xdata, ydata)
y_plot = func(xdata, popt[0], popt[1], popt[2])
plt.figure()
plt.scatter(xdata, ydata)
plt.plot(xdata, y_plot)
#%%
# =============================================================================
# Let's create some rainfall!!
# =============================================================================
def mulGETS(result, i):
# Total number of sequences to model
n = occurence.shape[0]
# Set number of stations
m = occurence.shape[1]
# Create time array
time_array = pd.date_range(start=df.index[0], periods=n)
season_dict = {12 : 'DJF', 1 : 'DJF', 2 : 'DJF',
3 : 'MAM', 4 : 'MAM', 5 : 'MAM',
6: 'JJA', 7: 'JJA', 8: 'JJA',
9: 'SON', 10: 'SON', 11: 'SON'}
## Step 1 - Create, correlated, occurence array ##
# Generate standard normal random numbers
rnd_ = np.random.normal(0.0, 1.0, size=(m, n))
# Create correlated random, uniform, numbers
rnd = correlated_rnd(occ_corr, rnd_)
# Simulate occurences
seq = execute_markov(m, n, rnd, markov_models, names)
# Create correlated random, uniform, numbers
x_array = correlated_rnd(rainfall_corr, rnd_).T
## Step 2 - Calculate occurence index for each station ##
occ_index_new = calc_occindex(seq.T, m, occ_corr_org)
## Step 3 - Determine rainfall amounts! ##
rainfall = np.zeros((n,m))
#for i in range(occ_index_new.shape[1]):
# Unpack model parameters
bins = retbin_list[i] # Class definiton
#result = lambda_list[i] # Lambda values for the mutl exp function
alpha = alpha_list[i] # Alpha values for the mult exp function
# Categorize the data into the 4 different class'
df_temp = pd.DataFrame(data=occ_index_new[:, i], columns=['occ_index'])
df_temp = df_temp.drop(df_temp[seq.T[:,i]==0].index)
df_temp['class']= pd.cut(df_temp['occ_index'], bins=bins, labels=np.arange(bins.size-1), include_lowest=True)
for k, class_ in enumerate(df_temp['class']):
# Determine season for rainfall occurence
season = season_dict[time_array[k].month]
# Determine rainfall amount
x = x_array[df_temp.index[k], i]
temp_rain = []
for j in range(bins.size-1):
a = alpha[j]
l = 1 / result[j][season][0]
temp_rain.append(mult_exp(x, a, l))
rainfall[df_temp.index[k], i] = np.sum(temp_rain)
rainfall = pd.DataFrame(data=rainfall[:, i], index=time_array, columns=[names[i]])
return rainfall
def get_stats(df):
df_stat = df[names[i]].divide(24).to_frame().rename(columns={names[i] : 'Int'})
stats = rainfall_stat(df_stat, timeres=1440)
return stats
def convert2dict(x0, dim0, dim1):
temp = np.reshape(x0, (dim0, dim1))
result = {}
for i in range(dim0):
result[i] = {}
for j in range(dim1):
result[i][season_name[j]] = [temp[i,j]]
return result
def fun(x, *args):
result = convert2dict(x, args[0], args[1])
rainfall = mulGETS(result, args[3])
ap_sim = get_stats(rainfall)['ap'][0]
score = (np.abs(args[2] - ap_sim)) / args[2]
return score
# from scipy.optimize import minimize, differential_evolution
# for i in range(df.shape[1]):
# # Get target value for the optimizer
# ap_obs = get_stats(df)['ap'][0]
# # Extract lambda values to get inital guess of the optimizer
# x0 = np.zeros((4 * len(lambda_list[i]),))
# bounds = []
# for _ in range(4 * len(lambda_list[i])):
# bounds.append((0.01,10))
# j = 0
# for class_ in lambda_list[i]:
# for season in lambda_list[i][class_]:
# x0[j] = lambda_list[i][class_][season][0]
# j+=1
# #result_temp = convert2dict(x0, len(lambda_list[i]), 4)
# #score = fun(x0, (len(lambda_list[i]), 4, ap_obs, i))
# res = differential_evolution(fun, bounds=bounds, args=(len(lambda_list[i]), 4, ap_obs, i), disp=True, workers=3)
# break
# # Initialze some stuff!
# # Set random seed
# np.random.seed(1234)
# # Total number of sequences to model
# n = occurence.shape[0]
# # Set number of stations
# m = occurence.shape[1]
# # Create time array
# time_array = pd.date_range(start=df.index[0], periods=n)
# season_dict = {12 : 'DJF', 1 : 'DJF', 2 : 'DJF',
# 3 : 'MAM', 4 : 'MAM', 5 : 'MAM',
# 6: 'JJA', 7: 'JJA', 8: 'JJA',
# 9: 'SON', 10: 'SON', 11: 'SON'}
# ## Step 1 - Create, correlated, occurence array ##
# # Generate standard normal random numbers
# rnd_ = np.random.normal(0.0, 1.0, size=(m, n))
# # Create correlated random, uniform, numbers
# rnd = correlated_rnd(occ_corr, rnd_)
# # Simulate occurences
# seq = execute_markov(m, n, rnd, markov_models, names)
# # Create correlated random, uniform, numbers
# x_array = correlated_rnd(rainfall_corr, rnd_).T
# ## Step 2 - Calculate occurence index for each station ##
# occ_index_new = calc_occindex(seq.T, m, occ_corr_org)
# ## Step 3 - Determine rainfall amounts! ##
# rainfall = np.zeros((n,m))
# for i in range(occ_index_new.shape[1]):
# # Unpack model parameters
# bins = retbin_list[i] # Class definiton
# result = lambda_list[i] # Lambda values for the mutl exp function
# alpha = alpha_list[i] # Alpha values for the mult exp function
# # Categorize the data into the 4 different class'
# df_temp = pd.DataFrame(data=occ_index_new[:, i], columns=['occ_index'])
# df_temp = df_temp.drop(df_temp[seq.T[:,i]==0].index)
# df_temp['class']= pd.cut(df_temp['occ_index'], bins=bins, labels=np.arange(bins.size-1), include_lowest=True)
# for k, class_ in enumerate(df_temp['class']):
# # Determine season for rainfall occurence
# season = season_dict[time_array[k].month]
# # Determine rainfall amount
# x = x_array[df_temp.index[k], i]
# temp_rain = []
# for j in range(bins.size-1):
# a = alpha[j]
# l = 1 / result[j][season][0]
# temp_rain.append(mult_exp(x, a, l))
# rainfall[df_temp.index[k], i] = np.sum(temp_rain)
# # Save the simulated rainfall to a txt file, for pca.
# np.savetxt('mult_gets_rainfall.dat', rainfall)
# # =============================================================================
# # Perform diagnostics of the rainfall simulator!
# # =============================================================================
# ## Process the simulated rainfall ##
# # Add the simulated rainfall to a dataframe
# df_rainfall = pd.DataFrame(data=rainfall, index=time_array, columns=names)
# # Calculate the correlation of the rainfall amounts
# rainfall_corr_sim = df_rainfall.corr().values
# # Calculate the simulated occurence correlation
# df_occ_sim = pd.DataFrame(data=seq.T, columns=names)
# occ_sim_corr = df_occ_sim.corr().values
# ## Compare occurence correlation ##
# org = np.reshape(occ_corr_org, (occ_corr_org.size,))
# org[org==1] = np.nan
# sim = np.reshape(occ_sim_corr, (occ_sim_corr.size,))
# sim[sim==1] = np.nan
# fig, ax = plt.subplots()
# ax.scatter(org,sim)
# ax.set(xlim=(0.4, 1), ylim=(0.4, 1))
# diag_line, = ax.plot(ax.get_xlim(), ax.get_ylim(), ls="--", c=".3")
# ax.set(xlim=(0.4, 1), ylim=(0.4, 1))
# ax.set_xlabel('Observed correlation [-]')
# ax.set_ylabel('Simulated correlation [-]')
# ax.set_title('Occurence correlation')
# ax.grid('Major')
# # Save the figure
# fig.savefig('diagnostic_plots/occurence_correlation.png', dpi=300)
# ## Compare rainfall correlation ##
# org = np.reshape(rainfall_corr_obs.values, (rainfall_corr_obs.values.size,))
# org[org==1] = np.nan
# sim = np.reshape(rainfall_corr_sim, (rainfall_corr_sim.size,))
# sim[sim==1] = np.nan
# fig, ax = plt.subplots()
# ax.scatter(org,sim)
# ax.set(xlim=(0.4, 1), ylim=(0.4, 1))
# diag_line, = ax.plot(ax.get_xlim(), ax.get_ylim(), ls="--", c=".3")
# ax.set(xlim=(0.4, 1), ylim=(0.4, 1))
# ax.set_xlabel('Observed correlation [-]')
# ax.set_ylabel('Simulated correlation [-]')
# ax.set_title('Precip. amount correlation')
# ax.grid('Major')
# # Save the figure
# fig.savefig('diagnostic_plots/precip_correlation.png', dpi=300)
# # Calculate annual statistics and lowfrequency variablitiy
# def get_stats(df):
# df_stat = df[names[i]].divide(24).to_frame().rename(columns={names[i] : 'Int'})
# stats = rainfall_stat(df_stat, timeres=1440)
# return stats
# stat_obs = {}
# for i in range(names.size):
# stat_obs[names[i]] = get_stats(df)
# stat_sim = {}
# for _ in range(1):
# df_mulgets = pd.DataFrame(data=mulGETS(), index=time_array, columns=names)
# for i in range(1):
# if names[i] not in stat_sim:
# stat_sim[names[i]] = []
# stat_sim[names[i]].append(get_stats(df_mulgets))