-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_forward_model_v8.py
1821 lines (1448 loc) · 91.5 KB
/
main_forward_model_v8.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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 29 12:08:11 2021
v5: Implemented adjusted surface transport, permanent fouling
v6: remove suface transport for now, remove unnecessary parameters
v8: jan2023, version for review: more macro litter included
@author: kaandorp
"""
import numpy as np
import os
from glob import glob
import matplotlib.pyplot as plt
import xarray as xr
import pandas as pd
from datetime import datetime, timedelta
from h3.unstable import vect
from h3 import h3_to_geo, geo_to_h3
from scipy.sparse import coo_matrix
from sklearn.preprocessing import normalize
import cartopy.crs as ccrs
from tools import from_pickle, to_pickle, pcolorhex
from utils import get_settling_velocity
# from tools import pcolorhex
from cartopy import feature
import matplotlib.gridspec as gridspec
from scipy import special
from time import time
import cmocean
from scipy import sparse
import gc
import socket
import multiprocessing
from multiprocessing import set_start_method
from scipy.interpolate import interp1d
from argparse import ArgumentParser
from tools import get_colors_tableau
def Gaussian(x,mu,sigma):
return np.exp(-.5*((x-mu)/sigma)**2)
def draw_coastlines(ax,h3_indices,dict_coastlines,colors='r',**kwargs):
for i1,index_ in enumerate(h3_indices):
list_lon = dict_coastlines[index_]['lon_coast']
list_lat = dict_coastlines[index_]['lat_coast']
for lon_,lat_ in zip(list_lon,list_lat):
if isinstance(colors,np.ndarray):
ax.plot(lon_,lat_,'-',color=colors[i1],**kwargs)
else:
ax.plot(lon_,lat_,'-',color=colors,**kwargs)
def get_colors(inp, colormap, vmin=None, vmax=None):
norm = plt.Normalize(vmin, vmax)
return colormap(norm(inp))
def NB_model(k_arr,i_f,p,d_N=3):
'''
The fragmentation model by Charalambous (2015)
k_arr: the size classes
i_f: the fragmentation 'index' (denoted by f in the paper)
p: fragmentation probability
d_N: spatial dimensionality
'''
pmf_m = (special.gamma(k_arr+i_f) / (special.gamma(k_arr+1)*special.gamma(i_f)))*(p**k_arr)*(1-p)**i_f
pmf_N = 2**(d_N*k_arr) * pmf_m
return pmf_m,pmf_N
def geo_to_h3_depth(lat,lon,depth,res):
h3_index = vect.geo_to_h3(lat,lon,res)
if len(depth_layers) > 2:
for i1,(d_upper,d_lower,index_remove) in enumerate(zip(depth_layers[:-1],depth_layers[1:],index_remove_depth)):
mask = (depth >= d_upper) & (depth < d_lower)
if i1 == 0: #surface: don't change the h3 index
pass
elif i1 == len(depth_layers) - 2: #below the last depth layer: move particles to index 1 (removal due to sinking too deep)
h3_index[mask] = 1
print('%i particles removed below depth %f meters' % (mask.sum(),d_upper))
else:
h3_index[mask] -= index_remove #at other depth layers: remove the appropriate depth index
print('n particles in depth layer: %i' % mask.sum())
return h3_index
def get_fragmentation_fraction(lambda_f_week = 2e-4,units='m'):
l_0 = 160
k_arr = np.arange(10)
l_arr = l_0 / (2**k_arr)
pmf_m_0,pmf_N_0 = NB_model(k_arr,1,.4,d_N=2.5)
lambda_f = lambda_f_week*4 #f/week to f/month
m_next, N_next = NB_model(k_arr, lambda_f, .4, d_N=2.5)
macro_to_macro = 0
macro_to_micro = 0
for i1, (m_, l_) in enumerate(zip(pmf_m_0, l_arr)):
if i1 == 0:
i_start = None
i_end = None
else:
i_start = i1
i_end = -i1
m_arr_ = m_next[:i_end]
l_arr_ = l_arr[i_start:]
mask_macro = l_arr_ > 5
macro_to_macro += m_*(m_arr_[mask_macro].sum())
macro_to_micro += m_*(m_arr_[~mask_macro].sum())
return macro_to_macro,macro_to_micro
def h3_cell_volume():
h3_index_upper = h3_index_base + np.uint64(1e17)
dict_h3_normalize = pd.Series(index=map_h3_to_mat.index,dtype=np.float32)
for i1 in range(len(depth_layers)-1):
mask_select = (map_h3_to_mat.index < (h3_index_upper - index_remove_depth[i1])) & (map_h3_to_mat.index >= (h3_index_upper - index_remove_depth[i1+1]))
h3_orig = map_h3_to_mat[mask_select].index + index_remove_depth[i1]
areas = np.array([dict_coastlines[h3_orig[i1]]['area'] for i1 in range(len(h3_orig))]) *1e6 #m2
if i1 == len(depth_layers)-2: #in the lowest layer, calculate difference between local bathymetry and upper depth level (otherwise this results in np.inf)
bath_ = df_bath[h3_orig].values
height = bath_ - depth_layers[i1]
else:
height = depth_layers[i1+1] - depth_layers[i1]
# dict_h3_area[h3_orig] = areas
# dict_h3_volume[h3_orig] = areas*height
dict_h3_normalize[map_h3_to_mat[mask_select].index] = areas*height
#coastlines
mask_select = map_h3_to_mat.index > h3_index_upper
h3_orig = map_h3_to_mat[mask_select].index - np.uint64(1e17)
lengths = np.array([dict_coastlines[h3_orig[i1]]['length_coast_D1.27'] for i1 in range(len(h3_orig))]) *1e3
dict_h3_normalize[map_h3_to_mat[mask_select].index] = lengths
return dict_h3_normalize
def h3_cell_location():
h3_index_upper = h3_index_base + np.uint64(1e17)
df_h3_location = pd.DataFrame(index=map_h3_to_mat.index,columns=('lon','lat'),dtype=np.float32)
for i1 in range(len(depth_layers)-1):
mask_select = (map_h3_to_mat.index < (h3_index_upper - index_remove_depth[i1])) & (map_h3_to_mat.index >= (h3_index_upper - index_remove_depth[i1+1]))
h3_orig = map_h3_to_mat[mask_select].index + index_remove_depth[i1]
lons_ = []
lats_ = []
for i2 in range(len(h3_orig)):
(lat,lon) = h3_to_geo(hex(h3_orig[i2]))
lats_.append(lat)
lons_.append(lon)
df_h3_location.loc[map_h3_to_mat[mask_select].index,'lon'] = np.array(lons_)
df_h3_location.loc[map_h3_to_mat[mask_select].index,'lat'] = np.array(lats_)
#coastlines
mask_select = map_h3_to_mat.index > h3_index_upper
h3_orig = map_h3_to_mat[mask_select].index - np.uint64(1e17)
lons_ = []
lats_ = []
for i2 in range(len(h3_orig)):
(lat,lon) = h3_to_geo(hex(h3_orig[i2]))
lats_.append(lat)
lons_.append(lon)
df_h3_location.loc[map_h3_to_mat[mask_select].index,'lon'] = np.array(lons_)
df_h3_location.loc[map_h3_to_mat[mask_select].index,'lat'] = np.array(lats_)
return df_h3_location
def create_release_vector(type_='r',mu_log10_release=-.7,sigma_log10_release=.15):
weights = Gaussian(np.log10(l_arr),mu_log10_release,sigma_log10_release)
weights = weights / weights.sum()
if type_ == 'r':
file_release = os.path.join(data_folder,'00_data_files/input_h3_rivers.nc')
data_release = xr.open_dataset(file_release)
i_valid_h3 = np.in1d(data_release['h3_index'].values, map_mat_to_h3.values) #release locations present in transition matrix
h3_release = data_release['h3_index'].values[i_valid_h3]
tracer_release = (data_release['midpoint'].values[0,i_valid_h3] / 12) * 1e6 #tonnes/year to grams/month
release_per_month = {}
for i1 in range(12):
release_per_month[i1] = np.zeros([n_total,len(l_arr)])
for i2 in range(len(l_arr)):
np.add.at(release_per_month[i1][:,i2], map_h3_to_mat[h3_release].values, tracer_release*weights[i2])
elif type_ == 'f':
file_release = os.path.join(data_folder,'00_data_files/input_h3_fisheries_monthly.nc')
data_release = xr.open_dataset(file_release)
i_valid_h3 = np.in1d(data_release['h3_index'].values, map_mat_to_h3.values) #release locations present in transition matrix
h3_release = data_release['h3_index'].values[i_valid_h3]
release_per_month = {}
total_release = 0
# release_per_month = np.zeros([12,n_total])
for i1 in range(12):
release_per_month[i1] = np.zeros([n_total,len(l_arr)])
tracer_release = data_release['fishing_hours'].values[i1,i_valid_h3]
for i2 in range(len(l_arr)):
np.add.at(release_per_month[i1][:,i2], map_h3_to_mat[h3_release].values, tracer_release*weights[i2])
total_release += release_per_month[i1].sum()
# np.add.at(release_per_month[i1,:], map_h3_to_mat[h3_release].values, tracer_release)
for i1 in range(12):
#normalize to have a mean monthly input of 1
release_per_month[i1] /= (total_release / 12)
elif type_ == 'p':
file_release = os.path.join(data_folder,'00_data_files/input_h3_coastal_pop.nc')
data_release = xr.open_dataset(file_release)
i_valid_h3 = np.in1d(data_release['h3_index'].values, map_mat_to_h3.values) #release locations present in transition matrix
h3_release = data_release['h3_index'].values[i_valid_h3]
tracer_release = data_release['MPW_input'].values[3,i_valid_h3] #choose 2015?
release_per_month = {}
total_release = 0
for i1 in range(12):
release_per_month[i1] = np.zeros([n_total,len(l_arr)])
for i2 in range(len(l_arr)):
np.add.at(release_per_month[i1][:,i2], map_h3_to_mat[h3_release].values, tracer_release*weights[i2])
total_release += release_per_month[i1].sum()
for i1 in range(12):
#normalize to have a mean monthly input of 1
release_per_month[i1] /= (total_release / 12)
return release_per_month
def calculate_rise_velocity_Dietrich(l,rho_p=1010,rho_sw=1025):
w_b_arr = []
# d_star_arr = []
for r_tot in l:
# rho_sw = 1029
g = 9.81
sw_kin_visc = 1e-6
dn = r_tot # equivalent spherical diameter [m], calculated from Dietrich (1982) from A = pi/4 * dn**2
delta_rho = (rho_p - rho_sw) / rho_sw # normalised difference in density between total plastic+bf and seawater[-]
dstar = (abs(rho_p - rho_sw) * g * dn ** 3.) / (rho_sw * sw_kin_visc ** 2.) # [-]
if dstar > 5e9:
w_star = 265000
elif dstar < 0.05:
w_star = (dstar ** 2.) * 1.71E-4
else:
w_star = 10. ** (-3.76715 + (1.92944 * np.log10(dstar)) - (0.09815 * np.log10(dstar) ** 2.) - (
0.00575 * np.log10(dstar) ** 3.) + (0.00056 * np.log10(dstar) ** 4.))
if delta_rho > 0: # sinks
w_b = -(g * sw_kin_visc * w_star * delta_rho) ** (1. / 3.)
else: # rises
a_del_rho = delta_rho * -1.
w_b = (g * sw_kin_visc * w_star * a_del_rho) ** (1. / 3.) # m s-1
w_b_arr.append(w_b)
# d_star_arr.append(dstar)
return np.array(w_b_arr)
def print_time(t1,str_,div_by=1.):
units = ''
if div_by == 1:
units = 'seconds'
elif div_by == 60:
units = 'minutes'
print('time %s: %f %s' % (str_,((time()-t1)/div_by),units))
return time()
#%%
gc.enable()
def get_available_data(filename,map_h3_to_mat,plot_missing=False,index_add=0):
data = pd.read_csv(filename,index_col=0,parse_dates=['eventDate'])
date_cols = [col for col in data.columns if 'Date' in col]
for col in date_cols:
data[col] = pd.to_datetime(data[col])
mask_present_in_mat = ( np.isin(data['h%i'%h3_grid_res]+index_add,map_h3_to_mat.index) & ~(data['h%i'%h3_grid_res] == 0) )
data_return = data.loc[mask_present_in_mat,:].copy()
print('%f percent of data is located, n = %i' % ((mask_present_in_mat.sum()/len(mask_present_in_mat))*100,mask_present_in_mat.sum()) )
if plot_missing:
from cartopy.feature import LAND
fig = plt.figure(figsize=(13, 6))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(LAND, zorder=0,edgecolor='black')
h3_plot = np.unique(data.loc[~mask_present_in_mat,'h%i'%h3_grid_res])
colors = [plt.cm.tab10(1)]*len(h3_plot)
pcolorhex(ax,h3_plot,colors,alpha=.7)
ax.plot(data.loc[~mask_present_in_mat,'decimalLongitude'],data.loc[~mask_present_in_mat,'decimalLatitude'],'ko')
if 'l_min' in data_return.keys():
data_return.loc[np.isnan(data_return.loc[:,'l_min']),'l_min'] = 0
if 'l_max' in data_return.keys():
data_return.loc[np.isnan(data_return.loc[:,'l_max']),'l_max'] = np.inf
return data_return, mask_present_in_mat
if 'mbp13' in socket.gethostname(): # desktop
home_folder = '.'
data_folder = '.'
n_sizes = 2
elif 'node0' in socket.gethostname() or 'lorenz' in socket.gethostname(): #Lorenz
home_folder = '/nethome/kaand004/Git_repositories/Global_Analysis_Mikael'
data_folder = '/nethome/kaand004/Git_repositories/Global_Analysis_Mikael'
n_sizes = 15
elif 'science-bs' in socket.gethostname(): #gemini
home_folder = '/nethome/kaand004/Git_repositories/Global_Analysis_Mikael'
data_folder = '/scratch/kaand004'
n_sizes = 15
folder_figure = os.path.join(home_folder,'00_figures/17_inversion_01/')
if not os.path.exists(folder_figure):
os.mkdir(folder_figure)
#3: ~40,000 cells, 60km
#2: ~6,000 cells, 160km
h3_grid_res = 3
coastline_res = '50m'
use_Hinata = True
use_Stokes = False
depth_layers = np.array([0,5,50,500,np.inf])
particles_l = [0.0001,0.0004,0.0016,0.0064,0.0256,0.1024]
months = [1,2,3,4,5,6,7,8,9,10,11]
years = [2019]
year_start = 1980
dt_days = 30
l0 = 1.6384
# l0 = 0.2048
release = 'mix' #simple, 'r','f','p', or 'mix'
mass_count_ratio = 500. #grams/unit, 0.4096 meter objects
k_arr = np.arange(0,n_sizes) #11: 0.4mm to 0.4m
l_arr = l0 / (2**k_arr)
str_info = 'Year_' + '-'.join('%2.2i'%s_ for s_ in years) + '_Stokes%s_'%use_Stokes
#-----------------parameters------------------------
h3_index_base = np.uint64(5e17)
index_add_coast = np.uint64(1e17) #indices for coastal cells are the ocean surface cells + index_add_coast
index_remove_depth = np.array([int(0),int(1e17),int(2e17),int(3e17),int(4e17)])
# the dict in which all coastline data is stored
dict_coastlines = from_pickle(os.path.join(data_folder,'00_data_files/coastal_properties_h3_res%i_%s_Dc.pickle' % (h3_grid_res,coastline_res)))
h3_index_coastline_data = np.array([hex_ for hex_ in dict_coastlines.keys() if 'length_coast' in dict_coastlines[hex_].keys() ],dtype=np.uint64)
#TODO: monthly oceanic transitions below
filename_dict_transition = os.path.join(data_folder,'00_transition_files/transitions_h%i_%s_monthly_' % (h3_grid_res,coastline_res) + 'year_' + '-'.join('%2.2i'%s_ for s_ in years) + '_merge_months_False.pickle')
P = from_pickle(filename_dict_transition)
map_mat_to_h3 = P['map_mat_to_h3']
map_h3_to_mat = P['map_h3_to_mat']
# a map from the matrix entry to removal (-1), surface (0), and deeper layers (1,2,3,4) to make computations easier
# TODO: can be made variable for different depth layers
# map_mat_to_domain = pd.Series(index=map_mat_to_h3.index,dtype=int)
# map_mat_to_domain[map_mat_to_h3.values < 1e17] = -1
# map_mat_to_domain[map_mat_to_h3.values > 6e17] = 0
# map_mat_to_domain[(map_mat_to_h3.values < 6e17) & (map_mat_to_h3.values > 5e17)] = 1
# map_mat_to_domain[(map_mat_to_h3.values < 5e17) & (map_mat_to_h3.values > 4e17)] = 2
# map_mat_to_domain[(map_mat_to_h3.values < 4e17) & (map_mat_to_h3.values > 3e17)] = 3
# map_mat_to_domain[(map_mat_to_h3.values < 3e17) & (map_mat_to_h3.values > 2e17)] = 4
# not all coastline sections are present in the transition matrix: take the intersection
h3_index_coast = np.intersect1d(h3_index_coastline_data, map_mat_to_h3)
length_coast = np.array([dict_coastlines[hex_]['length_coast_D1.27'] for hex_ in h3_index_coast])
map_h3_to_coast_length = pd.Series(index=h3_index_coast, data=length_coast )
dict_variance = {'n_surf':0.28,'m_surf':0.40,'n_beach':0.2,'m_beach':0.14,'fish_frac':0.008,
'MDMAP':0.21,'OSPAR_100m':0.29,'OSPAR_1km':0.32,
'surf_nm-3_Egger':0.08,'surf_gm-3_Egger':0.09,
'depth_nm-3_Egger':0.04,'depth_gm-3_Egger':0.04,}
data_trawl_m, _ = get_available_data(os.path.join(data_folder,'00_data_files/plastic_data/converted_surface_m.csv'),map_h3_to_mat)
data_trawl_n, _ = get_available_data(os.path.join(data_folder,'00_data_files/plastic_data/converted_surface_n.csv'),map_h3_to_mat)
data_macro_n, _ = get_available_data(os.path.join(data_folder,'00_data_files/plastic_data/converted_surface_macro.csv'),map_h3_to_mat)
data_beach_inst, _ = get_available_data(os.path.join(data_folder,'00_data_files/plastic_data/converted_beach_inst.csv'),map_h3_to_mat,index_add=index_add_coast)
data_beach_OSPAR, _ = get_available_data(os.path.join(data_folder,'00_data_files/plastic_data/converted_agg_MDMAP_OSPAR.csv'),map_h3_to_mat,index_add=index_add_coast)
data_beach_mean, _ = get_available_data(os.path.join(data_folder,'00_data_files/plastic_data/converted_beach_mean.csv'),map_h3_to_mat,index_add=index_add_coast)
data_beach_mean['modelled_n'] = 0
data_beach_mean['modelled_sum'] = 0
data_beach_mean['modelled_sum2'] = 0
data_Egger_surf = pd.read_csv(os.path.join(data_folder,'00_data_files/plastic_data/converted_Egger2020_ERL_surface.csv'),index_col=0)
data_Egger_surf = pd.concat([data_Egger_surf,
pd.read_csv(os.path.join(data_folder,'00_data_files/plastic_data/converted_Egger2020_SR_surface.csv'),index_col=0)])
mask_present_in_mat = np.isin(data_Egger_surf['h%i'%h3_grid_res],map_h3_to_mat.index)
data_Egger_surf = data_Egger_surf.loc[mask_present_in_mat,:]
data_Egger_depth = pd.read_csv(os.path.join(data_folder,'00_data_files/plastic_data/converted_Egger2020SR_depth_frag.csv'),index_col=0)
data_Egger_depth['h%i'%h3_grid_res] = data_Egger_depth['h%i'%h3_grid_res] - index_remove_depth[np.digitize(data_Egger_depth['Depth'],depth_layers)-1]
mask_present_in_mat = np.isin(data_Egger_depth['h%i'%h3_grid_res],map_h3_to_mat.index)
data_Egger_depth = data_Egger_depth.loc[mask_present_in_mat,:]
data_Zhao_depth = pd.read_csv(os.path.join(data_folder,'00_data_files/plastic_data/converted_Zhao2022_depth.csv'),index_col=0)
data_Zhao_depth['h%i'%h3_grid_res] = data_Zhao_depth['h%i'%h3_grid_res] - index_remove_depth[np.digitize(data_Zhao_depth['Depth'],depth_layers)-1]
mask_present_in_mat = np.isin(data_Zhao_depth['h%i'%h3_grid_res],map_h3_to_mat.index)
data_Zhao_depth = data_Zhao_depth.loc[mask_present_in_mat,:]
data_Eriksen = pd.read_csv(os.path.join(data_folder,'00_data_files/plastic_data/converted_Eriksen_macro.csv'),index_col=0)
data_Eriksen['h%i'%h3_grid_res] = data_Eriksen['h%i'%h3_grid_res] - index_remove_depth[np.digitize(data_Eriksen['Depth'],depth_layers)-1]
mask_present_in_mat = np.isin(data_Eriksen['h%i'%h3_grid_res],map_h3_to_mat.index)
data_Eriksen = data_Eriksen.loc[mask_present_in_mat,:]
df_bath = from_pickle(os.path.join(data_folder,'00_data_files/bathymetry_h%i.pickle' % h3_grid_res))
df_pp = from_pickle(os.path.join(data_folder,'00_data_files/mean_pp_h%i_2019.pickle' % h3_grid_res)).astype(float)
df_pp = df_pp / np.quantile(df_pp.values[~np.isnan(np.array(df_pp.values,dtype=float))],.99) #normalize for outliers
df_pp[df_pp>1] = 1.
df_pp[df_pp<0] = 0.
dict_h3_normalize = h3_cell_volume()
dict_h3_location = h3_cell_location()
n_total = len(map_h3_to_mat)
lon_plot_dist = np.array([-140.,-112.,-140.,-100.])
lat_plot_dist = np.array([20.,23.,50.,15.])
depth_plot_dist = np.array([0,0,0,0])
mask_neuston_net = (l_arr > 3e-4) & (l_arr < 0.2)
mask_beach_cleanup = l_arr > 2.5e-2
# mask_beach_macro = l_arr > 0.4
mask_Eriksen = l_arr > 0.25
# mask_Egger = [(l_arr>=4e-4)&(l_arr<1.5e-3),(l_arr>1.5e-3)&(l_arr<5.0e-3),
# (l_arr>5e-3)&(l_arr<1.5e-2),(l_arr>1.5e-2)&(l_arr<5.2e-2)]
mask_Egger = [(l_arr>=5.6e-4)&(l_arr<1.13e-3),(l_arr>1.13e-3)&(l_arr<4.5e-3),
(l_arr>4.5e-3)&(l_arr<18e-3),(l_arr>18e-3)&(l_arr<36e-3)]
mask_Egger_sizes = [[5e-4,1.5e-3],[1.5e-3,5e-3],[5e-3,1.5e-2],[1.5e-2,5e-2]]
Egger_corr_n = [1.5029822975240785,
0.6564421139154354,
0.7997358204039169,
1.7005002991087623]
Egger_corr_m = [1.6803211760254935,
0.9670585583407109,
0.7636187765408871,
1.8350271441960349]
mask_Zhao = (l_arr >= 1e-4) & (l_arr < 5e-3)
parameter_names = [r'$S_{riv}$',r'$S_{pop}$',r'$S_{fis}$',
r'$\tau_{beach}$',r'$l_{coast,beach,max}$',r'$a_{resus.}$',r'$b_{resus.}$',
r'$f_{fouled}$',r'$f_{nb}$',r'$f_{pf}$',
r'$p_{land,remove}$',
r'$\lambda_{f}$',r'$p_f$',r'$d_N$',
r'$a_{L_{eff}}$',r'$b_{L_{eff}}$',
r'$\mu_{release,log10}$','PW_factor']
log_parameters = np.array([True,True,True,
True,False,False,False,
False,False,True,
True,
True,False,False,
False,False,
False,False])
# mu_parameters = np.array([-1.0,0.7,0.0,
# np.log10(100),125,260,7.1,
# .2,.2,.2,
# np.log10(0.01),
# -3.0,0.4,2.18,
# .64,-1.,
# -.4,0.05])
# sigma_parameters = np.array([0.16,0.08,0.22,
# .25,30,1.,0.1,
# .1,.1,.1,
# .3,
# 0.16,.001,.1,
# 0.06,0.15,
# .13,0.01]) #sigma is given for the log-transformed parameters
# mu_parameters = np.array([-9.89405350e-01, 6.98970004e-01, 3.57155523e-02,
# 2.00000000e+00, 4.70465152e+02, 2.60000000e+02, 7.10000000e+00,
# 2.50000000e-01, 2.50000000e-01, -8.92197672e-01,
# -2.69929574e+00,
# -2.66549661e+00, 4.00000000e-01, 2.18718327e+00,
# 6.37325580e-01, -9.48615760e-01,
# -3.92022000e-01, 5.00000000e-02])
# sigma_parameters = np.array([4.10814466e-01, 7.04270806e-02, 1.77824465e-01,
# 2.00686664e-01, 1.24409219e+02, 2.60000000e+00, 7.10000000e-02,
# 8.10000000e-02, 8.10000000e-02, 2.93321072e-01,
# 3.33333333e-01,
# 1.43804468e-01, 1.00000000e-03, 4.60050728e-02,
# 8.74484991e-02, 2.26095201e-01,
# 1.50947667e-01, 1.66666667e-02])
#2 sigma
mu_parameters = np.array([-9.89405350e-01, 6.98970004e-01, 3.57155523e-02,
2.00000000e+00, 4.70465152e+02, 2.60000000e+02, 7.10000000e+00,
2.50000000e-01, 2.50000000e-01, -8.92197672e-01,
-2.69929574e+00,
-2.66549661e+00, 4.20000000e-01, 2.18718327e+00,
6.37325580e-01, -9.48615760e-01,
-5.72947498e-01, 2.50000000e-02])
sigma_parameters = np.array([4.10814466e-01, 7.04270806e-02, 1.77824465e-01, 2.00686664e-01,
1.24409219e+02, 2.60000000e+00, 7.10000000e-02, 8.10000000e-02,
8.10000000e-02, 2.93321072e-01, 3.33333333e-01, 1.43804468e-01,
1.00000000e-02, 4.60050728e-02, 8.74484991e-02, 2.26095201e-01,
9.06391674e-02, 8.33333333e-03])
cap_0_1 = np.array([False,False,False,
False,False,False,False,
True,True,True,
True,
False,False,False,
False,False,
False,False])
def forward_model(parameters,final_run=False):
def draw_map(vec_,extent,gs,col_,base_title='k0, %3.3f-%3.3f m depth',stop_earlier=0,log_scale=True):
h3_index_upper = h3_index_base + np.uint64(1e17)
for i1 in range(len(depth_layers)-1-stop_earlier):
ax_ = plt.subplot(gs[i1,col_], projection=ccrs.PlateCarree())
mask_select = (map_h3_to_mat.index < (h3_index_upper - index_remove_depth[i1])) & (map_h3_to_mat.index >= (h3_index_upper - index_remove_depth[i1+1]))
h3_orig = map_h3_to_mat[mask_select].index + index_remove_depth[i1]
vec_select = vec_[mask_select]
if not log_scale:
colors = get_colors(vec_select,cmap,
vmin=vmin,vmax=vmax)
h3_plot = h3_orig
else:
colors = get_colors(np.log10(vec_select[vec_select>0]),cmap,
vmin=np.log10(vmin),vmax=np.log10(vmax))
h3_plot = h3_orig[vec_select > 0]
mask = (dict_h3_location.loc[h3_plot,'lon']>extent[0]) & (dict_h3_location.loc[h3_plot,'lon']<extent[1]) & (dict_h3_location.loc[h3_plot,'lat']>extent[2]) & (dict_h3_location.loc[h3_plot,'lat']<extent[3])
h3_plot = h3_plot[mask]
colors = colors[mask,:]
pcolorhex(ax_,h3_plot,colors,draw_edges=False,alpha=1.)
ax_.add_feature(feature.NaturalEarthFeature('physical','land',coastline_res),facecolor='grey',zorder=1000)
if i1 == 0:
mask_select = map_h3_to_mat.index > h3_index_upper # coastlines
h3_orig = map_h3_to_mat[mask_select].index - np.uint64(1e17)
vec_select = vec_[mask_select]
if not log_scale:
colors = get_colors(vec_select,cmap2,
vmin=vmin2,vmax=vmax2)
h3_plot = h3_orig
else:
colors = get_colors(np.log10(vec_select[vec_select>0]),cmap2,
vmin=np.log10(vmin2),vmax=np.log10(vmax2))
h3_plot = h3_orig[vec_select > 0]
mask = (dict_h3_location.loc[h3_plot,'lon']>extent[0]) & (dict_h3_location.loc[h3_plot,'lon']<extent[1]) & (dict_h3_location.loc[h3_plot,'lat']>extent[2]) & (dict_h3_location.loc[h3_plot,'lat']<extent[3])
h3_plot = h3_plot[mask]
colors = colors[mask,:]
# colors = get_colors(np.log10(vec_select[vec_select>0]),cmap2,
# vmin=np.log10(vmin2),vmax=np.log10(vmax2))
# h3_plot = h3_orig[vec_select > 0]
if vec_select.sum() > 0:
draw_coastlines(ax_,h3_plot,dict_coastlines,transform=ccrs.PlateCarree(),linewidth=7)
draw_coastlines(ax_,h3_plot,dict_coastlines,colors=colors,transform=ccrs.PlateCarree(),linewidth=6)
ax_.set_extent((extent[0]+1,extent[1]-1,extent[2]+1,extent[3]-1),ccrs.PlateCarree()) #reduce the margins to hide white cells
ax_.set_title(base_title % (depth_layers[i1],depth_layers[i1+1]))
t1 = time()
def to_scaled(p_unscaled,mu_params_,sigma_params_):
p_scaled = (p_unscaled - mu_params_) / sigma_params_
return p_scaled
def to_unscaled(p_scaled,mu_params_,sigma_params_):
p_unscaled = (p_scaled*sigma_params_) + mu_params_
return p_unscaled
def lin_max(length,l_max,max_val=1.):
if length.size > 1:
p = (1/l_max)*length
p[length > l_max] = max_val
elif length.size == 1:
p = min((1/l_max)*length,max_val)
else:
p = None
return p
#-------------get back the parameters
parameters_unscaled = to_unscaled(parameters,mu_parameters,sigma_parameters)
parameters_unscaled[log_parameters] =10**(parameters_unscaled[log_parameters])
parameters_unscaled[cap_0_1 & (parameters_unscaled < 0)] = 0.01
parameters_unscaled[cap_0_1 & (parameters_unscaled > 1)] = .99
for name_,p_ in zip(parameter_names,parameters_unscaled):
print('%s: %4.4e' % (name_,p_))
(r_riv,r_pop,r_fis,
tau_b_,l_max,a_resus,b_resus,
frac_fouled,frac_nb,frac_pf,
p_land_remove,
frag_if_month,p_f,d_N,
a_L_Leff,b_L_Leff,
mu_log10_release,PW_factor) = parameters_unscaled
sigma_L_eff = .13
pf_mode = 'permanent_fouled' # permanent_fouled_removal500
# unrealisic scenario: per time step more than the available tracer is removed (shouldnt happen)
# if 1-p_ocean_max_remove <= 0:
# print('max removal ocean outside of bounds (%f), setting to .99' % (p_ocean_max_remove))
# p_ocean_max_remove = .99
if 1-p_land_remove <= 0:
print('max removal land outside of bounds (%f), setting to .95' % (p_land_remove))
p_land_remove = .95
if b_resus < 0:
b_resus = 0
if l_max < 0:
l_max = .001
# fractions of fouled and neutrally bouyant particles shouldn't make up more than 100%
if frac_fouled + frac_nb + frac_pf >= 1:
print('adjusting fouled/nb/pf fractions to 98% in total')
sum_ = frac_fouled + frac_nb + frac_pf
frac_fouled = (frac_fouled / sum_) - 0.01
frac_nb = (frac_nb / sum_) - 0.01
frac_pf = (frac_pf / sum_) - 0.01
#t1 = print_time(t1,'init')
# p_ocean_min_remain = (1-p_ocean_max_remove)
p_land_remain = (1-p_land_remove)
release_vector = np.array([r_riv,r_pop,r_fis])
def calc_mass_per_item(l_arr,d_N):
l_calib = 0.0064 #from calculate_priors.py
m_calib = 0.0161
mass_per_item = np.zeros(len(l_arr))
mask_small = l_arr <= l_calib
mask_large = l_arr > l_calib
mass_per_item[mask_small] = ((l_arr[mask_small] / l_calib)**2.187)*m_calib
mass_per_item[mask_large] = ((l_arr[mask_large] / l_calib)**d_N)*m_calib
return mass_per_item
# mass_per_item = ((l_arr / 0.0064)**d_N)*0.02039 #from test_count_mass_ratio.py
mass_per_item = calc_mass_per_item(l_arr,d_N)
tau_b = tau_b_ * np.ones(len(l_arr)) #days
if not use_Hinata:
tau_r = 25 * np.ones(len(l_arr)) #days
else:
w_b = calculate_rise_velocity_Dietrich(l_arr)
tau_r = a_resus*w_b + b_resus
p_frag_m,p_frag_N = NB_model(k_arr,frag_if_month,p_f,d_N=d_N)
p_beach = 1-np.exp(-dt_days/tau_b)
p_resus = 1-np.exp(-dt_days/tau_r)
# sigma_log10_wb = 0.2
if release == 'rivers' or release == 'r':
vec_start = create_release_vector(type_='r')
elif release == 'coastlines' or release == 'p':
vec_start_r = create_release_vector(type_='r')
mass_calibrate = vec_start_r.sum() / 12
vec_start = create_release_vector(type_='p') #1/month
vec_start *= release_vector[1]*mass_calibrate
elif release == 'fisheries' or release == 'f':
vec_start_r = create_release_vector(type_='r')
mass_calibrate = vec_start_r.sum() / 12
vec_start = create_release_vector(type_='f') #1/month
vec_start *= release_vector[2]*mass_calibrate
elif release == 'mix': #mixed scenario
vec_start_r = create_release_vector(type_='r',mu_log10_release=mu_log10_release,sigma_log10_release=.3) #grams/month
vec_start_p = create_release_vector(type_='p',mu_log10_release=mu_log10_release,sigma_log10_release=.3)
vec_start_f = create_release_vector(type_='f',mu_log10_release=mu_log10_release,sigma_log10_release=.3)
mass_calibrate = vec_start_r[0].sum() #grams/month
vec_start = {}
vec_start_fN = {}
vec_start_N = {}
for i2 in range(12):
vec_start_r[i2] *= release_vector[0]
vec_start_p[i2] *= (release_vector[1]*release_vector[0]*mass_calibrate)
vec_start_f[i2] *= (release_vector[2]*release_vector[0]*mass_calibrate)
vec_start[i2] = vec_start_r[i2] + vec_start_p[i2] + vec_start_f[i2] #each column: in grams per month
vec_start_fN[i2] = vec_start_f[i2] / mass_per_item
vec_start_N[i2] = vec_start[i2] / mass_per_item
print('Releasing %3.3f kilotons of plastics from rivers per year' % (np.array([vec_start_r[i].sum() for i in range(12)]).sum()/1e9))
print('Compare to: 1000 kilotons (Meijer), 6 kilotons (Weiss)')
print('Total: %3.3f kilotons per year from rivers, pop, fisheries' % (np.array([vec_start[i].sum() for i in range(12)]).sum()/1e9))
#t1 = print_time(t1,'sources init.')
#-------------------------set-up matrix-------------------
i_r_ll,i_c_ll,_ = sparse.find(P['land_land'])
cols_land = np.unique(i_c_ll)
P_land_removed = coo_matrix( (np.ones(len(cols_land)),(2*np.ones(len(cols_land)),cols_land)),
shape=(n_total,n_total))# * (1-p_land_remain)
i_r_o,i_c_o,_ = sparse.find(P['water_water']+P['coast_water'])
# cols_ocean = np.unique(i_c_o)
i_r_ww,i_c_ww,_ = sparse.find(P['water_water'])
i_r_cw,i_c_cw,_ = sparse.find(P['coast_water'])
i_r_cl,i_c_cl,_ = sparse.find(P['coast_land'])
#t1 = print_time(t1,'mat init')
# -------------------sinking parameterization-------------------------
# pp_water_water = P['pp_water_water']
# pp_coast_water = P['pp_coast_water']
# pp_coast_land = P['pp_coast_land']
# P_ocean_removed = P['P_ocean_removed']
# -------------------beaching parameterization-------------------
p_coast_cl = lin_max(map_h3_to_coast_length[map_mat_to_h3[i_c_cl]],l_max)
p_coast_cw = lin_max(map_h3_to_coast_length[map_mat_to_h3[i_c_cw]],l_max)
P_cascade_m = {}
P_cascade_m[0] = P['cascade_water'] + P['cascade_land']*p_frag_m[0]
for i3 in range(1,len(k_arr)):
P_cascade_m[i3] = P['cascade_land']*p_frag_m[i3]
P_cascade_N = {}
P_cascade_N[0] = P['cascade_water'] + P['cascade_land']*p_frag_N[0]
for i3 in range(1,len(k_arr)):
P_cascade_N[i3] = P['cascade_land']*p_frag_N[i3]
#t1 = print_time(t1,'casc')
P_total_l = {}
for month_ in np.arange(12):
P_ocean_l = {}
P_total_l[month_] = {}
available_lengths = np.sort(np.array(list(P['ocean'][month_]['nonfouled'].keys())))
def l_to_leff(l_,n_pad=5):
# for the rise velocity, take a linear combination for different particle sizes (i.e. w_b's)
# as to try to solve the problem of too little mixing. A Gaussian function is used to weigh the samples
n_pad = 5
assert(available_lengths[1]/available_lengths[0] == 4.)
left_ = np.sort(np.array([available_lengths[0] / (4**n) for n in range(1,n_pad+1)]))
right_ = np.sort(np.array([available_lengths[-1] * (4**n) for n in range(1,n_pad+1)]))
l_arr_padded = np.append(left_,np.append(available_lengths,right_))
mu_L_eff = a_L_Leff*np.log10(l_) + b_L_Leff
weights_Leff_padded = Gaussian(np.log10(l_arr_padded),mu_L_eff,sigma_L_eff)
weights_Leff = weights_Leff_padded[n_pad:-n_pad].copy()
weights_Leff[0] += weights_Leff_padded[:n_pad].sum()
weights_Leff[-1] += weights_Leff_padded[-n_pad:].sum()
weights_Leff /= weights_Leff.sum()
l_use = available_lengths[weights_Leff > 0]
weight_use = weights_Leff[weights_Leff > 0]
if not len(weight_use)>0:
#exception where the the spread gets too small -> no weights
l_use = np.array([l_])
weight_use = np.array([1])
return l_use, weight_use
P_ocean_nb = P['ocean'][month_]['neutral'][1e-5] #the 1e-5 is the arbitrary small size denotation that was used in this case
for i1,l_ in enumerate(l_arr):
l_use,weight_use = l_to_leff(l_)
#------------------------------------------------------------------------
# non-fouled bouyant particles: create a combination of rise velocities
P_ocean_nonfouled = P['ocean'][month_]['nonfouled'][l_use[0]]*weight_use[0]
if len(weight_use) > 1:
for i2 in range(1,len(weight_use)):
P_ocean_nonfouled += P['ocean'][month_]['nonfouled'][l_use[i2]]*weight_use[i2]
#t1 = print_time(t1,'non-fouled')
#------------------------------------------------------------------------
# fouled bouyant particles: take the closest fouled case for transport
i_right = np.searchsorted(available_lengths,l_)
if i_right == 0:
i_right = 1
if i_right == len(available_lengths): #above max avail. length: assume same behavior for macroplastics
l_max = available_lengths.max()
P_ocean_pf = P['ocean'][month_][pf_mode][l_max]
P_ocean_fouled = P['ocean'][month_]['fouled'][l_max]
else:
l_left = available_lengths[i_right-1]
l_right = available_lengths[i_right]
w_2 = (np.log10(l_) - np.log10(l_left)) / (np.log10(l_right) - np.log10(l_left)) #0 at 0.0004, 1 at 0.102
assert(0<=w_2<=1)
P_ocean_fouled = P['ocean'][month_]['fouled'][l_left]*(1-w_2) + P['ocean'][month_]['fouled'][l_right]*(w_2)
P_ocean_pf = P['ocean'][month_][pf_mode][l_left]*(1-w_2) + P['ocean'][month_][pf_mode][l_right]*(w_2)
#t1 = print_time(t1,'fouled')
#------------------------------------------------------------------------
# A linear combination is made of fouled (oscill.), non-fouled, neutrally bouyant, and permanently fouled particles
P_ocean_l[l_] = P_ocean_nonfouled*(1-frac_fouled-frac_nb-frac_pf) + P_ocean_fouled*frac_fouled + P_ocean_nb*frac_nb + P_ocean_pf*frac_pf
#t1 = print_time(t1,'combin.')
#------------------------------------------------------------------------
# Surface transport can be adjusted, using bi-linear interpolation with wind and Stokes
# frac_Stokes = lin_max(l_,l_max_Stokes,max_val=.99)
# frac_wind = lin_max(l_,l_max_wind,max_val=.99)
# # frac_Stokes = .99
# # frac_wind = .99
# P_ocean_l[l_] = (P_ocean_l[l_]*(1-frac_Stokes) + P['ocean'][month_]['2D_Stokes'][1.0]*(frac_Stokes) )*(1 - frac_wind) + \
# (P['ocean'][month_]['2D_wind'][1.0]*(1-frac_Stokes) + P['ocean'][month_]['2D_Stokes_wind'][1.0]*(frac_Stokes) )*(frac_wind)
# # needs to be normalized again to retain mass balance in the deeper water layer matrix columns
# P_ocean_l[l_] = normalize(P_ocean_l[l_], norm='l1', axis=0)
#t1 = print_time(t1,'surf+norm')
p_beach_per_cell_cl = p_beach[i1]*p_coast_cl
p_beach_per_cell_cw = 1-(p_beach[i1]*p_coast_cw)
P_coast_land_beach = coo_matrix( (p_beach_per_cell_cl,(i_r_cl,i_c_cl)), shape=(n_total,n_total))
P_coast_water_beach = coo_matrix( (p_beach_per_cell_cw,(i_r_cw,i_c_cw)), shape=(n_total,n_total))
# P_coast_water_beach = P['coast_water']*(1-p_beach[i1])
# P_coast_land_beach = P['coast_land']*p_beach[i1]
P_land_coast_beach = P['land_coast']*p_resus[i1]
P_land_land_beach = P['land_land']*(1-p_resus[i1])
#t1 = print_time(t1,'beach')
#cells on land have permanent sinks
P_land_coast_sinks = P_land_coast_beach.multiply(p_land_remain)
P_land_land_sinks = P_land_land_beach.multiply(p_land_remain)
# sinking due to primary productivity
# A linear map is made from min pp (0 -> 1), to max pp (1 -> p_ocean_min_remain), such that no particles sink when there's no pp, and the prescribed max amount at pp=1
# P_water_water_remain = (p_ocean_min_remain - 1)*pp_water_water[month_] + P['water_water']
# P_coast_water_remain = (p_ocean_min_remain - 1)*pp_coast_water[month_] + P['coast_water']
# P_coast_land_remain = (p_ocean_min_remain - 1)*pp_coast_land[month_] + P['coast_land']
P_water_water_remain = P['water_water']
P_coast_water_remain = P['coast_water']
P_coast_land_remain = P['coast_land']
#t1 = print_time(t1,'pp')
#TO DO: size-dependent bottom hitting can be added here as well for fouled/non-fouled particles
P_water_water = P_water_water_remain
P_coast_water = P_coast_water_remain.multiply(P_coast_water_beach) #probabilities after fouling and beaching
P_coast_land = P_coast_land_remain.multiply(P_coast_land_beach)
# P_total_l[month_][l_] = (P_ocean_l[l_].multiply(P_water_water + P_coast_water)) + P_coast_land + \
# P_land_coast_sinks + P_land_land_sinks + P_land_removed*(1-p_land_remain) + P_ocean_removed[month_]*(1-p_ocean_min_remain) + P['removal']
P_total_l[month_][l_] = (P_ocean_l[l_].multiply(P_water_water + P_coast_water)) + P_coast_land + \
P_land_coast_sinks + P_land_land_sinks + P_land_removed*(1-p_land_remain) + P['removal']
#t1 = print_time(t1,'total')
colsum = np.array(np.sum(P_total_l[month_][l_],axis=0)).ravel()
leakiness = 1 - (colsum.sum()/len(colsum))
if leakiness > 0.0001: #check that mass conservation is not violated too much
print('leakiness bigger than 0.01 percent (m:%i l:%4.4f): %f' % (month_,l_,leakiness))
#-------------------------Iterate-------------------
# df_aggregated = pd.DataFrame(columns=['modelled_n','modelled_sum','modelled_sum2'])
df_results = pd.DataFrame(columns=['Year','Month',
'h3_cell_id','Modelled','Measured',
'Units','var_log10','l_min','l_max',
'ParentEventID','index_parent','Depth','detection_limit'])
extent = (-179,179,-80,80)
plot_map = False
do_plot_dist = False
plot_tracer_amount = False
plot_per = 120#120*2#12*10
i_start = 0
n_rows = 1
n_cols = 1
n_steps = (2020-1980)*12#12*11
vmin = 1e-9
vmax = 1e-3
cmap = plt.cm.viridis
vmin2 = 1e-5 #beaches
vmax2 = 1e1
cmap2 = cmocean.cm.turbid_r
tracer_matrix_mass = np.zeros([n_total,len(k_arr)],dtype=np.float32)
tracer_matrix_num = np.zeros([n_total,len(k_arr)],dtype=np.float32)
tracer_matrix_num_f = np.zeros([n_total,len(k_arr)],dtype=np.float32)
if plot_map:
fig = plt.figure(figsize=(20,9))
gs = gridspec.GridSpec(1, 4, bottom=.05, top=.95, wspace=.25, width_ratios=[30,30,1,1])
ax_1 = plt.subplot(gs[0,0])
ax_3 = plt.subplot(gs[0,2])
cplt1 = ax_1.scatter(np.random.random(1000),np.random.random(1000),c=np.linspace(np.log10(vmin),np.log10(vmax),1000),cmap=cmap)
cbar1 = plt.colorbar(cplt1,cax=ax_3)
cbar1.set_label('Tracer concentration [log$_{10}$(g m$^{-3}$)]')
ax_2 = plt.subplot(gs[0,1])
ax_4 = plt.subplot(gs[0,3])
cplt2 = ax_2.scatter(np.random.random(1000),np.random.random(1000),c=np.linspace(np.log10(vmin2),np.log10(vmax2),1000),cmap=cmap2)
cbar2 = plt.colorbar(cplt2,cax=ax_4)
cbar2.set_label('Tracer concentration [log$_{10}$(g m$^{-1}$)]')
fig.savefig(folder_figure+'colorbar.png')
fig = plt.figure(figsize=(25, 9))
gs = gridspec.GridSpec(n_rows, n_cols, bottom=.05, top=.95, wspace=.1)
for i4,l_ in enumerate(l_arr[0:n_cols]):
str_size = 'year %i' % (0)
# draw_map(tracer_matrix_mass.sum(axis=1)/dict_h3_normalize.values,extent,gs,i4,base_title=str_size+', %3.0f-%3.0f m depth',stop_earlier=(len(depth_layers)-n_rows-1) )
draw_map(tracer_matrix_mass[:,i4]/dict_h3_normalize.values,extent,gs,i4,base_title=str_size+', %3.0f-%3.0f m depth',stop_earlier=(len(depth_layers)-n_rows-1) )
if do_plot_dist:
ax_ = plt.subplot(gs[0,0], projection=ccrs.PlateCarree())
for i1,(lon_,lat_) in enumerate(zip(lon_plot_dist,lat_plot_dist)):
ax_.plot(lon_,lat_,'x',markersize=10,color=plt.cm.tab10(i1))
fig.savefig(folder_figure+'%3.3i.png' % (0))
plt.close('all')
gc.collect()
# gc.disable()
for i1 in range(n_steps): # time loop
t0 = time()
month_ = i1 % 12
year_current = year_start + i1//12
month_current = (i1 % 12) + 1 # from 0-11 to 1-12
date_current = datetime(year_current,month_current,1)
year_from_2015 = year_current - 2015
frac_PW = np.exp(PW_factor*year_from_2015)
# if release == 'simple' and i1 == 0:
# tracer_matrix_mass[:,0] += vec_start
# elif release == 'mix':
# tracer_matrix_mass[:,0] += vec_start[month_,:]
# tracer_matrix_num[:,0] += vec_start_N[month_,:]
# tracer_matrix_num_f[:,0] += vec_start_fN[month_,:]
# else:
# tracer_matrix_mass[:,0] += vec_start[month_,:]
if release == 'simple' and i1 == 0:
tracer_matrix_mass += vec_start[month_]