-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectivity_mixture_stats_spatial.py
169 lines (124 loc) · 5.97 KB
/
connectivity_mixture_stats_spatial.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
# %% Load the packages
import numpy as np
import xarray as xr
from tqdm import tqdm
import pandas as pd
import pickle
import concurrent.futures
import os
location = "Cape_Hatteras"
base_path = f"/storage/shared/oceanparcels/output_data/data_Claudio/NEMO_Ensemble/"
Latitude_limit = None
Longitude_limit = -40
print(f"Space mix. Latitude limit: {Latitude_limit}")
print(f"Space mix. Longitude limit: {Longitude_limit}")
# %% Spatial analysis
members = np.arange(1, 51)
N_subsets = 50
subset_particles = 150
def process_member(member, delta_r, location, subset_particles):
path = base_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(path)
pset = pset.isel(trajectory=np.random.choice(pset.trajectory, subset_particles, replace=False))
return pset
distributions = {}
for delta_r in [0.1, 1., 2.]:
for k in range(1, N_subsets+1):
psets = []
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(process_member, member, delta_r, location, subset_particles) for member in members]
for future in tqdm(concurrent.futures.as_completed(futures), total=len(members)):
pset = future.result()
psets.append(pset)
pset_members = xr.concat(psets, dim='trajectory')
obs_length = len(pset_members.obs)
print(f"Subset:{k} delta_r: {delta_r}. Number of particles: {len(pset_members.trajectory)}")
N_particles = len(pset_members.trajectory)
if Latitude_limit is not None:
lats = pset_members.lat.load().values
p_index, t_index = np.where(lats[:, :] > Latitude_limit)
elif Longitude_limit is not None:
lons = pset_members.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_members.z.load().values
depths = depths[subpolar_traj, drift_time]
distributions["member"] = k
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 = base_path + f"analysis/connectivity/mix_dr_{delta_r*100:03.0f}_{Latitude_limit}N/Distributions_mix_dr{delta_r*100:03.0f}_s{k:03d}.pkl"
elif Longitude_limit is not None:
save_path = base_path + f"analysis/connectivity/mix_dr_{delta_r*100:03.0f}_{abs(Longitude_limit)}W/Distributions_mix_dr{delta_r*100:03.0f}_s{k:03d}.pkl"
with open(save_path, "wb") as f:
pickle.dump(distributions, f)
else:
print(f"--EMPTY--")
# %% Make a dataframe with the statistics
N_subsets = 50
stats = {}
n_members = np.arange(1, N_subsets + 1)
counts = np.zeros(N_subsets)
median_time = np.zeros(N_subsets)
mean_time = np.zeros(N_subsets)
min_time = np.zeros(N_subsets)
std_time = np.zeros(N_subsets)
mean_depth = np.zeros(N_subsets)
median_depth = np.zeros(N_subsets)
std_depth = np.zeros(N_subsets)
for delta_r in [0.1, 1., 2.]:
for k in range(1, N_subsets+1):
if Latitude_limit is not None:
pkl_path = base_path + f"analysis/connectivity/mix_dr_{delta_r*100:03.0f}_{Latitude_limit}N/Distributions_mix_dr{delta_r*100:03.0f}_s{k:03d}.pkl"
elif Longitude_limit is not None:
pkl_path = base_path + f"analysis/connectivity/mix_dr_{delta_r*100:03.0f}_{abs(Longitude_limit)}W/Distributions_mix_dr{delta_r*100:03.0f}_s{k: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"]
print(f"Subset {k} has {len(trajectory)} trajectories, Latitude limit: {Latitude_limit}N")
median_time[k - 1] = np.median(drift_time)
mean_time[k - 1] = np.mean(drift_time)
min_time[k - 1] = np.min(drift_time)
std_time[k - 1] = np.std(drift_time)
counts[k - 1] = len(drift_time)
mean_depth[k - 1] = np.mean(depths)
median_depth[k - 1] = np.median(depths)
std_depth[k - 1] = np.std(depths)
else:
print(f"File {pkl_path} does not exist. Skipping subset {k}.")
median_time[k - 1] = np.nan
mean_time[k - 1] = np.nan
min_time[k - 1] = np.nan
std_time[k - 1] = np.nan
counts[k - 1] = 0
mean_depth[k - 1] = np.nan
median_depth[k - 1] = np.nan
std_depth[k - 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 = base_path + f"analysis/connectivity/Stats/Stats_mix_dr{delta_r*100:03.0f}_{Latitude_limit}N.csv"
elif Longitude_limit is not None:
save_csv_path = base_path + f"analysis/connectivity/Stats/Stats_mix_dr{delta_r*100:03.0f}_{abs(Longitude_limit)}W.csv"
stats_df.to_csv(save_csv_path)
print(f"Saved stats in {save_csv_path}")
# %%