/
postprocessing_iot.py
135 lines (121 loc) · 6.17 KB
/
postprocessing_iot.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
from plastic_sources import RiverSources
from particles import BeachingParticles
from utilities import get_index_closest_point, get_dir
import numpy as np
import pandas as pd
def get_iot_lon_lat_range():
lon_range = [90., 128.]
lat_range = [-20., 6.]
return (lon_range, lat_range)
def get_iot_sources(original=False):
iot_lon, iot_lat = get_iot_lon_lat_range()
if not original:
global_sources = RiverSources.read_from_netcdf()
else:
global_sources = RiverSources.read_from_shapefile()
io_sources = global_sources.get_riversources_from_ocean_basin('io')
iot_sources = io_sources.get_riversources_in_lon_lat_range(iot_lon, iot_lat)
return iot_sources
def get_cki_box_lon_lat_range():
lon_range = [96.6, 97.1]
lat_range = [-12.3, -11.8]
return (lon_range, lat_range)
def get_christmas_box_lon_lat_range():
lon_range = [105.4, 105.9]
lat_range = [-10.7, -10.2]
return (lon_range, lat_range)
def get_christmas_plastic_measurements(input_path=get_dir('iot_measurements')):
time, lon, lat, n_plastic, kg_plastic = get_iot_plastic_measurements(input_path=input_path)
lon_range, lat_range = get_christmas_box_lon_lat_range()
l_lon = np.logical_and(lon_range[0]<=lon, lon<=lon_range[1])
l_lat = np.logical_and(lat_range[0]<=lat, lat<=lat_range[1])
l_ci = np.logical_and(l_lon, l_lat)
return time[l_ci], lon[l_ci], lat[l_ci], n_plastic[l_ci], kg_plastic[l_ci]
def get_cki_plastic_measurements(input_path=get_dir('iot_measurements')):
time, lon, lat, n_plastic, kg_plastic = get_iot_plastic_measurements(input_path=input_path)
lon_range, lat_range = get_cki_box_lon_lat_range()
l_lon = np.logical_and(lon_range[0]<=lon, lon<=lon_range[1])
l_lat = np.logical_and(lat_range[0]<=lat, lat<=lat_range[1])
l_cki = np.logical_and(l_lon, l_lat)
return time[l_cki], lon[l_cki], lat[l_cki], n_plastic[l_cki], kg_plastic[l_cki]
def get_iot_plastic_measurements(input_path=get_dir('iot_measurements')):
df = pd.read_excel(input_path, sheet_name='Event_details', skiprows=5)
time = np.array([d.to_pydatetime() for d in df['Date']])
lon = np.array(df['Longitude'])
lat = np.array(df['Latitude'])
n_plastic = np.array(df['Total'])
kg_plastic = np.array(df['Weight Kg'])
return time, lon, lat, n_plastic, kg_plastic
def get_l_particles_in_box(particles: BeachingParticles, iot_island):
if iot_island == 'cki':
box_lon, box_lat = get_cki_box_lon_lat_range()
elif iot_island == 'christmas':
box_lon, box_lat = get_christmas_box_lon_lat_range()
else:
raise ValueError(f'Unknown iot_island {iot_island}, valid options are: cki and christmas.')
l_in_box = []
for i in range(len(particles.pid)):
l_lon = np.logical_and(box_lon[0]<=particles.lon[i, :], particles.lon[i, :]<=box_lon[1])
l_lat = np.logical_and(box_lat[0]<=particles.lat[i, :], particles.lat[i, :]<=box_lat[1])
l_in_box.append(np.logical_and(l_lon, l_lat).any())
return np.array(l_in_box)
def get_particle_entry_time_box(particles: BeachingParticles, iot_island):
l_box = get_l_particles_in_box(particles, iot_island)
if iot_island == 'cki':
box_lon, box_lat = get_cki_box_lon_lat_range()
elif iot_island == 'christmas':
box_lon, box_lat = get_christmas_box_lon_lat_range()
else:
raise ValueError(f'Unknown iot_island {iot_island}, valid options are: cki and christmas.')
i_box = np.where(l_box)[0]
time_entry = []
for i in i_box:
l_lon = np.logical_and(box_lon[0]<=particles.lon[i, :], particles.lon[i, :]<=box_lon[1])
l_lat = np.logical_and(box_lat[0]<=particles.lat[i, :], particles.lat[i, :]<=box_lat[1])
i_first = np.where(np.logical_and(l_lon, l_lat))[0][0]
time_entry.append(particles.time[i_first])
return np.array(time_entry)
def get_particle_release_time_box(particles: BeachingParticles, iot_island):
l_box = get_l_particles_in_box(particles, iot_island)
_, t_release = particles._get_release_time_and_particle_indices()
time_release = particles.time[t_release[l_box]]
return time_release
def get_particle_release_locations_box(particles: BeachingParticles, iot_island):
l_box = get_l_particles_in_box(particles, iot_island)
lon0, lat0 = particles.get_initial_particle_lon_lat()
return (lon0[l_box], lat0[l_box])
def get_main_sources_lon_lat_n_particles(particles: BeachingParticles, iot_island):
lon0, lat0 = get_particle_release_locations_box(particles, iot_island)
coordinates0 = []
for i in range(len(lon0)):
coordinates0.append([lon0[i], lat0[i]])
coordinates0 = np.array(coordinates0)
coordinates0_unique, counts_unique = np.unique(coordinates0, axis=0, return_counts=True)
lon0_unique = coordinates0_unique[:, 0]
lat0_unique = coordinates0_unique[:, 1]
return (lon0_unique, lat0_unique, counts_unique)
def get_original_source_based_on_lon0_lat0(lon0, lat0):
iot_sources = get_iot_sources(original=True)
i_closest = get_index_closest_point(iot_sources.lon, iot_sources.lat, lon0, lat0, n_closest=6)
# iot_sources = get_iot_sources()
# iot_sources_org = get_iot_sources(original=True)
# i_sources = np.where(np.logical_and(iot_sources.lon == lon0, iot_sources.lat == lat0))
lon = iot_sources.lon[i_closest]
lat = iot_sources.lat[i_closest]
yearly_waste = np.sum(iot_sources.waste[i_closest], axis=1)
return lon, lat, yearly_waste
def get_n_particles_per_month_release_arrival(particles: BeachingParticles, iot_island):
release_time = get_particle_release_time_box(particles, iot_island)
release_months = np.array([x.month for x in release_time])
entry_time = get_particle_entry_time_box(particles, iot_island)
entry_months = np.array([x.month for x in entry_time])
months = np.arange(1,13,1)
n_release = []
n_entry = []
for month in months:
n_release.append(np.sum(release_months==month))
n_entry.append(np.sum(entry_months==month))
n_entry_per_release_month = np.zeros((len(months), len(months)))
for i in range(len(entry_months)):
n_entry_per_release_month[entry_months[i]-1, release_months[i]-1] += 1
return np.array(n_release), np.array(n_entry), n_entry_per_release_month