-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickviz_ADCP_MP.py
More file actions
168 lines (137 loc) · 6.85 KB
/
quickviz_ADCP_MP.py
File metadata and controls
168 lines (137 loc) · 6.85 KB
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
def plot_profile_3d_ADCP_MP(p, cfg={}, shift=None,df_MP=None,df=None):
'''
:param p: profile object
:param cfg: style -> string, vizualisation method, implemented methods : "contour", "gradient", "vector"
datatype -> string, determines what to plot. available types:
- "velocity" requires variable $component
- "custom" requires variable $cellattr
- "backscatter" (not implemented yet)
components -> list, a list of velocity components, that will be used. valid components: "x", "y", "z"
cellattr -> string, the attribute of cell the cells that will be plotted.
saveas -> string, path to output file, optional
title -> string, title of plot, optional
:param r3d: take as argument
:param shift: shift for mGFO
:return:
'''
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from outliers import get_cell_matrix, get_valuematrix_from_cellmatrix
from scipy.interpolate import griddata
if 'style' in cfg and cfg['style'] not in ["contour", "gradient", "vector", "stream"]:
raise NotImplementedError('plot_profile(): "{}" is not a valid plot style!'.format(cfg['style']))
# default values
cfg_datatype = cfg['datatype'] if 'datatype' in cfg else 'velocity'
cfg_components = cfg['components'] if 'components' in cfg else ['x', 'y']
cfg_style = cfg['style'] if 'style' in cfg else 'gradient'
# x
e_positions = [e.position for e in p.ensembles]
x = [0]
for i in range(1, len(e_positions)):
x.append(x[i - 1] + (e_positions[i] - e_positions[i - 1]).magn())
# ensemble with most cells, to steal the z positions of the cells
ncells = [len(e.cells) if hasattr(e, 'cells') else 0 for e in p.ensembles]
longestensemble = ncells.index(max(ncells))
z = [c.z_position for c in p.ensembles[longestensemble].cells]
for i in range(len(z)):
z[i] = z[i]+shift
# profile depth
z_depth = np.ma.masked_array([-e.depth for e in p.ensembles], [e.void for e in p.ensembles])
z_depths = -np.ma.masked_array([e.four_depths for e in p.ensembles],[[e.void, e.void, e.void, e.void] for e in p.ensembles])
# matrix with ProcesscedCell objects
cm = get_cell_matrix(p)
# fetch data to be displayed
data = []
if cfg_datatype == 'velocity':
for c in cfg_components:
if c in ['x', 'y', 'z']:
eval = '.velocity.' + c
data.append(get_valuematrix_from_cellmatrix(cm, eval, outputs=1, nvalues=1))
else:
raise NotImplementedError('plot_profile(): "{}" is not a valid component!'.format(c))
elif cfg_datatype == 'custom':
for ca in cfg['cellattr']:
data.append(get_valuematrix_from_cellmatrix(cm, ca, outputs=1, nvalues=1))
else:
raise NotImplementedError('plot_profile(): "{}" is not a valid datatype!'.format(cfg['datatype']))
# matrix to mask the profile
goodmat = get_valuematrix_from_cellmatrix(cm, eval, outputs=2, nvalues=1)
# prepare data depending on plot style
plot_data = np.zeros(shape=(data[0].shape[0], data[0].shape[1], 2))
for i in range(data[0].shape[0]):
for j in range(data[0].shape[1]):
if cfg_style != 'vector':
# merge data into a scalar by computing its magnitude
plot_data[i, j] = np.sqrt((np.array([d[i, j, 0] for d in data]) ** 2).sum())
else:
# re-order data for vector plotting
temp = np.array([d[i, j, 0] for d in data])
plot_data[i, j] = temp
# mask unusable matrix data (eg. parts not inside the river)
plot_data = np.ma.masked_array(plot_data, np.dstack((~goodmat, ~goodmat)))
cmap = plt.cm.jet
fig, ax = plt.subplots(nrows=2, ncols=1, sharex='col', figsize=(12, 8))
ax1, ax2 = ax
ax1.grid(linestyle='dotted')
ax2.grid(linestyle='dotted')
#ax1.axis([-20, 430, 21.5, 34.5])
#ax2.axis([-20, 430, 21.5, 34.5])
V_MIN = min(min(df_MP['Vnorm_RN']), np.min(plot_data[:, :, 0]))
V_MAX = max(max(df_MP['Vnorm_RN']), np.max(plot_data[:, :, 0]))
level = np.arange(V_MIN, V_MAX, 0.1).tolist()
im = ax1.contourf(x, z, plot_data[:, :, 0], cmap=cmap, vmin=V_MIN, vmax=V_MAX, levels=level)
plt.colorbar(im, ax=ax1)
im = ax2.tricontourf(df_MP['distance_MP'], df_MP['D_RN'], df_MP['Vnorm_RN'], cmap=cmap, vmin=V_MIN, vmax=V_MAX,levels=level)
plt.colorbar(im, ax=ax2)
ax1.set_ylabel('Hauteur [m]')
ax1.set_title(cfg['title'] if 'title' in cfg else "Vitesses ADCP")
ax2.set_xlabel('Distance depuis la rive [m]')
ax2.set_ylabel('Hauteur [m]')
ax2.set_title(cfg['title'] if 'title' in cfg else "Vitesses modèle physique")
plt.savefig('Vitesses_ADCP_MP')
# clear figure and axis.
plt.clf()
plt.cla()
# Courbe de densité
df_adcp = df.drop(columns=['X', 'Y', 'Z', 'U', 'V', 'W', 'Distance', 'Vitesse_MP_sur_grille_adcp'])
df_mp = df.drop(columns=['X', 'Y', 'Z', 'U', 'V', 'W', 'Magnitude', 'Distance'])
mot = 'ADCP'
nom1 = [mot]*len(df_adcp)
df_adcp = df_adcp.assign(Nom=nom1)
df_adcp['Magnitude'] = df_adcp['Magnitude'] / df_adcp['Magnitude'].mean()
mot='MP'
nom2 = [mot]*len(df_adcp)
df_mp = df_mp.assign(Nom=nom2)
df_mp['Vitesse_MP_sur_grille_adcp'] = df_mp['Vitesse_MP_sur_grille_adcp'] / df_mp['Vitesse_MP_sur_grille_adcp'].mean()
df_adcp = df_adcp.rename(columns = {'Magnitude' : 'Vitesses adimensionnées'})
df_mp = df_mp.rename(columns = {'Vitesse_MP_sur_grille_adcp' : 'Vitesses adimensionnées'})
df_tot = pd.concat([df_adcp, df_mp])
plt.grid(linestyle='dotted')
sns.set(font_scale=2)
ax = sns.kdeplot(data=df_tot, x='Vitesses adimensionnées', hue='Nom')
plt.xlabel('Vitesses adimensionnées', fontsize=15)
plt.ylabel('Densité', fontsize=13)
plt.savefig('Courbe_de_densité_de_Kernel-MP-ADCP')
plt.clf()
plt.cla()
#Profil de différence des vitesses
fig, ax = plt.subplots(figsize=(16, 8))
V_MIN = min(df['Diff_ADCP_MP'])
V_MAX = max(df['Diff_ADCP_MP'])
level = np.arange(V_MIN, V_MAX, 0.1).tolist()
im = ax.tricontourf(df['Distance'], df['Z'], df['Diff_ADCP_MP'], cmap=cmap, vmin=V_MIN, vmax=V_MAX,levels=level)
cbar = plt.colorbar(im, ax=ax)
cbar.ax.tick_params(labelsize=10)
ax.set_ylabel('Hauteur [m]', fontsize=12)
ax.set_xlabel('Distance depuis la rive [m]',fontsize=12)
ax.set_title('Profil de différences de vitesses ADCP-MP',fontsize=12)
ax.tick_params(axis='x', labelsize=10)
ax.tick_params(axis='y', labelsize=10)
ax.grid(linestyle='dotted')
cbar.set_label('Différence de vitesses [m/s]', fontsize=10)
plt.savefig('Profil_difference_de_vitesses_ADCP_MP')
# clear figure and axis.
plt.clf()
plt.cla()