-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectivity_single_member_stats.py
273 lines (205 loc) · 10.1 KB
/
connectivity_single_member_stats.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
# %% Load the packages
import numpy as np
import xarray as xr
from tqdm import tqdm
import pandas as pd
import pickle
import os
location = "Cape_Hatteras"
path = f"/storage/shared/oceanparcels/output_data/data_Claudio/NEMO_Ensemble/"
Latitude_limit = 53
Longitude_limit = None
# %% Spatial analysis
distributions = {}
total_members = 50
for delta_r in [0.1, 1., 2.]:
for member in tqdm(range(1, total_members + 1)):
print(f"Member: {member:03d}, Delta_r: {delta_r}")
file_path = path + f"{location}/spatial_long/dr_{delta_r*100:03.0f}/{location}_dr{delta_r*100:03.0f}_m{member:03d}.zarr"
pset = xr.open_zarr(file_path)
N_particles = len(pset.trajectory)
if Latitude_limit is not None:
lats = pset.lat.load().values
p_index, t_index = np.where(lats[:, :] > Latitude_limit)
elif Longitude_limit is not None:
lons = pset.lon.load().values
p_index, t_index = np.where(lons[:, :] > Longitude_limit)
subpolar_traj = np.unique(p_index) # it's no longer a subpolar but I keep the name
drift_time = []
if len(subpolar_traj) > 0:
for i in subpolar_traj:
idx_t = np.where(p_index == i)[0][0]
drift_time.append(t_index[idx_t])
drift_time = np.array(drift_time)
depths = pset.z.load().values
depths = depths[subpolar_traj, drift_time]
distributions["member"] = member
distributions["drift_time"] = drift_time
distributions["depths"] = depths
distributions["trajectory"] = np.unique(p_index)
# SAVE DISTRIBUTIONS in a pickle file
if Latitude_limit is not None:
save_path = path + f"analysis/connectivity/dr_{delta_r*100:03.0f}_{Latitude_limit}N/Distributions_dr{delta_r*100:03.0f}_m{member:03d}.pkl"
elif Longitude_limit is not None:
save_path = path + f"analysis/connectivity/dr_{delta_r*100:03.0f}_{abs(Longitude_limit)}W/Distributions_dr{delta_r*100:03.0f}_m{member:03d}.pkl"
# save_path = f"/storage/shared/oceanparcels/output_data/data_Claudio/NEMO_Ensemble/analysis/connectivity/dr_{delta_r*100:03.0f}/Distributions_dr{delta_r*100:03.0f}_m{member:03d}.pkl"
with open(save_path, "wb") as f:
pickle.dump(distributions, f)
else:
print(f"--EMPTY--")
# %% Temporal analysis
distributions = {}
total_members = 50
for week in [4, 12, 20]:
for member in tqdm(range(1, total_members + 1)):
print(f"Member: {member:03d}, Week: {week}")
file_path = path + f"{location}/temporal_long/W_{week:01d}/{location}_W{week:01d}_m{member:03d}.zarr"
pset = xr.open_zarr(file_path)
N_particles = len(pset.trajectory)
if Latitude_limit is not None:
lats = pset.lat.load().values
p_index, t_index = np.where(lats[:, :] > Latitude_limit)
elif Longitude_limit is not None:
lons = pset.lon.load().values
p_index, t_index = np.where(lons[:, :] > Longitude_limit)
subpolar_traj = np.unique(p_index)
drift_time = []
if len(subpolar_traj) > 0:
for i in subpolar_traj:
idx_t = np.where(p_index == i)[0][0]
drift_time.append(t_index[idx_t])
drift_time = np.array(drift_time)
depths = pset.z.load().values
depths = depths[subpolar_traj, drift_time]
distributions["member"] = member
distributions["drift_time"] = drift_time
distributions["depths"] = depths
distributions["trajectory"] = np.unique(p_index)
# SAVE DISTRIBUTIONS in a pickle file
if Latitude_limit is not None:
save_path = path + f"analysis/connectivity/W_{week:02d}_{Latitude_limit}N/Distributions_W{week:02d}_m{member:03d}.pkl"
elif Longitude_limit is not None:
save_path = path + f"analysis/connectivity/W_{week:02d}_{abs(Longitude_limit)}W/Distributions_W{week:02d}_m{member:03d}.pkl"
with open(save_path, "wb") as f:
pickle.dump(distributions, f)
else:
print(f"--EMPTY--")
#%% Build the Pandas Dataframes from the pickle files
# ____________________Spatial__________________________
N_members = 50
stats = {}
n_members = np.arange(1, N_members + 1)
counts = np.zeros(N_members)
median_time = np.zeros(N_members)
mean_time = np.zeros(N_members)
min_time = np.zeros(N_members)
std_time = np.zeros(N_members)
mean_depth = np.zeros(N_members)
median_depth = np.zeros(N_members)
std_depth = np.zeros(N_members)
for delta_r in [0.1, 1., 2.]:
for member in range(1, N_members+1):
if Latitude_limit is not None:
pkl_path = path + f"analysis/connectivity/dr_{delta_r*100:03.0f}_{Latitude_limit}N/Distributions_dr{delta_r*100:03.0f}_m{member:03d}.pkl"
elif Longitude_limit is not None:
pkl_path = path + f"analysis/connectivity/dr_{delta_r*100:03.0f}_{abs(Longitude_limit)}W/Distributions_dr{delta_r*100:03.0f}_m{member:03d}.pkl"
if os.path.exists(pkl_path):
with open(pkl_path, "rb") as f:
distributions = pickle.load(f)
drift_time = distributions["drift_time"]
depths = distributions["depths"]
trajectory = distributions["trajectory"]
median_time[member - 1] = np.median(drift_time)
mean_time[member - 1] = np.mean(drift_time)
min_time[member - 1] = np.min(drift_time)
std_time[member - 1] = np.std(drift_time)
counts[member - 1] = len(drift_time)
mean_depth[member - 1] = np.mean(depths)
median_depth[member - 1] = np.median(depths)
std_depth[member - 1] = np.std(depths)
else:
print(f"File {pkl_path} does not exist. Skipping member {member}.")
median_time[member - 1] = np.nan
mean_time[member - 1] = np.nan
min_time[member - 1] = np.nan
std_time[member - 1] = np.nan
counts[member - 1] = 0
mean_depth[member - 1] = np.nan
median_depth[member - 1] = np.nan
std_depth[member - 1] = np.nan
stats["subset"] = n_members
stats["counts"] = counts
stats["median_time"] = median_time
stats["mean_time"] = mean_time
stats["min_time"] = min_time
stats["std_time"] = std_time
stats["mean_depth"] = mean_depth
stats["median_depth"] = median_depth
stats["std_depth"] = std_depth
stats_df = pd.DataFrame(stats)
if Latitude_limit is not None:
save_csv_path = path + f"analysis/connectivity/Stats/Stats_dr{delta_r*100:03.0f}_{Latitude_limit}N.csv"
elif Longitude_limit is not None:
save_csv_path = path + f"analysis/connectivity/Stats/Stats_dr{delta_r*100:03.0f}_{abs(Longitude_limit)}W.csv"
stats_df.to_csv(save_csv_path)
print(f"Saved {save_csv_path}")
#%% ___________________Temporal__________________________
N_members = 50
stats = {}
n_members = np.arange(1, N_members + 1)
for week in [12, 20]:
counts = np.zeros(N_members)
median_time = np.zeros(N_members)
mean_time = np.zeros(N_members)
min_time = np.zeros(N_members)
std_time = np.zeros(N_members)
mean_depth = np.zeros(N_members)
median_depth = np.zeros(N_members)
std_depth = np.zeros(N_members)
for member in range(1, N_members + 1):
if Latitude_limit is not None:
pkl_path = path + f"analysis/connectivity/W_{week:02d}_{Latitude_limit}N/Distributions_W{week:02d}_m{member:03d}.pkl"
elif Longitude_limit is not None:
pkl_path = path + f"analysis/connectivity/W_{week:02d}_{abs(Longitude_limit)}W/Distributions_W{week:02d}_m{member:03d}.pkl"
if os.path.exists(pkl_path):
with open(pkl_path, "rb") as f:
distributions = pickle.load(f)
drift_time = distributions["drift_time"]
print(f"Member {member} week {week}. Number of particles: {len(drift_time)}")
depths = distributions["depths"]
trajectory = distributions["trajectory"]
median_time[member - 1] = np.median(drift_time)
mean_time[member - 1] = np.mean(drift_time)
min_time[member - 1] = np.min(drift_time)
std_time[member - 1] = np.std(drift_time)
counts[member - 1] = len(drift_time)
mean_depth[member - 1] = np.mean(depths)
median_depth[member - 1] = np.median(depths)
std_depth[member - 1] = np.std(depths)
else:
print(f"File {pkl_path} does not exist. Skipping member {member}.")
median_time[member - 1] = np.nan
mean_time[member - 1] = np.nan
min_time[member - 1] = np.nan
std_time[member - 1] = np.nan
counts[member - 1] = 0
mean_depth[member - 1] = np.nan
median_depth[member - 1] = np.nan
std_depth[member - 1] = np.nan
stats["subset"] = n_members
stats["counts"] = counts
stats["median_time"] = median_time
stats["mean_time"] = mean_time
stats["min_time"] = min_time
stats["std_time"] = std_time
stats["mean_depth"] = mean_depth
stats["median_depth"] = median_depth
stats["std_depth"] = std_depth
stats_df = pd.DataFrame(stats)
if Latitude_limit is not None:
save_csv_path = path + f"analysis/connectivity/Stats/Stats_W{week:02d}_{Latitude_limit}N.csv"
elif Longitude_limit is not None:
save_csv_path = path + f"analysis/connectivity/Stats/Stats_W{week:02d}_{abs(Longitude_limit)}W.csv"
stats_df.to_csv(save_csv_path)
print(f"Saved {save_csv_path}")
# %%