/
func_plot_fields.py
195 lines (175 loc) · 7.08 KB
/
func_plot_fields.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
import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset
import requests
from lxml import etree
import ipywidgets as widgets
def write_in_widget():
global select
global path
global rootgrp
global oldvalue
value = select.value
if value == '.':
path = base_url
elif value == '..':
path = path.replace(oldvalue, '')
elif value.endswith('.nc'):
file = value
elif value == '':
pass
else:
path += value + '/'
oldvalue = value
try:
catalog_file = requests.get(path + 'catalog.xml')
tree = etree.fromstring(catalog_file.content)
except:
path = base_url
catalog_file = requests.get(path + 'catalog.xml')
tree = etree.fromstring(catalog_file.content)
options = ['', '.', '..']
if not value.endswith('.nc'):
for children in tree[2]:
if children.get('ID').endswith('/') or children.get('ID').endswith('.nc'):
options.append(children.get('name'))
select.options = options
else:
if value in loadedfiles[0]:
rootgrp = loadedfiles[1][loadedfiles[0].index(value)]
else:
rootgrp = Dataset(path + file)
loadedfiles[0].append(file)
loadedfiles[1].append(rootgrp)
datasets = rootgrp.variables
datasets_name = ['choose a dataset']
for i in datasets.keys():
datasets_name.append(i)
x_data.options = datasets_name
y_data.options = datasets_name
u_data.options = datasets_name
v_data.options = datasets_name
def f(select_loc, x_data_loc, y_data_loc, u_data_loc, v_data_loc):
write_in_widget()
if select_loc.endswith('.nc'):
widgets.interact(h)
def h():
widgets.interact(g,
variable=widgets.ToggleButtons(options=['u', 'v', 'norm'],
description='Variable:',
disabled=False,
button_style='',
),
vector=widgets.Checkbox(value=False,
description='Display vectors',
disabled=False
),
)
def g(variable, vector):
try:
plot_func(variable, vector, x_data, y_data, u_data, v_data)
plt.show()
except:
print('Invalid netCDF file or invalid dataset selection')
def plot_func(variable, vector, x_data, y_data, u_data, v_data):
fig_xsize = 15
fig_ysize = 10
font_size = 25
plt.figure(num=0, figsize=(fig_xsize, fig_ysize), dpi=60,
facecolor='w', edgecolor='w')
plt.rcParams.update({'font.size': font_size})
plt.gca().set_aspect('equal')
xaxis_name = 'i'
yaxis_name = 'j'
plt.xlabel(xaxis_name)
plt.ylabel(yaxis_name)
contourcolor = plt.cm.coolwarm
xlin = np.linspace(0, len(rootgrp.variables[x_data.value])-1,
len(rootgrp.variables[x_data.value]))
ylin = np.linspace(0, len(rootgrp.variables[y_data.value])-1,
len(rootgrp.variables[y_data.value]))
x, y = np.meshgrid(xlin, ylin)
try:
if variable == 'norm':
norm = np.sqrt(rootgrp.variables[u_data.value][0, 0, :, :]**2 +
rootgrp.variables[v_data.value][0, 0, :, :]**2)
mycontour = plt.contourf(norm, 50, cmap=contourcolor)
cbar = plt.colorbar(mycontour)
cbar.ax.set_ylabel('norm (m/s)')
elif variable == 'u':
varplot = rootgrp.variables[u_data.value][0, 0, :, :]
mycontour = plt.contourf(varplot, 50, cmap=contourcolor)
cbar = plt.colorbar(mycontour)
cbar.ax.set_ylabel(rootgrp.variables[variable].name +
" (" + rootgrp.variables[u_data.value].units + ")")
else:
varplot = rootgrp.variables[v_data.value][0, 0, :, :]
mycontour = plt.contourf(varplot, 50, cmap=contourcolor)
cbar = plt.colorbar(mycontour)
cbar.ax.set_ylabel(rootgrp.variables[variable].name +
" (" + rootgrp.variables[v_data.value].units + ")")
if vector:
plt.quiver(x, y, rootgrp.variables[u_data.value][0, 0, :, :],
rootgrp.variables[v_data.value][0, 0, :, :])
except:
if variable == 'norm':
norm = np.sqrt(rootgrp.variables[u_data.value][:, :]**2 +
rootgrp.variables[v_data.value][:, :]**2)
mycontour = plt.contourf(norm, 50, cmap=contourcolor)
cbar = plt.colorbar(mycontour)
cbar.ax.set_ylabel('norm (m/s)')
elif variable == 'u':
varplot = rootgrp.variables[u_data.value][:, :]
mycontour = plt.contourf(varplot, 50, cmap=contourcolor)
cbar = plt.colorbar(mycontour)
else:
varplot = rootgrp.variables[v_data.value][:, :]
mycontour = plt.contourf(varplot, 50, cmap=contourcolor)
cbar = plt.colorbar(mycontour)
if vector:
plt.quiver(x, y, rootgrp.variables[u_data.value][:, :],
rootgrp.variables[v_data.value][0, 0, :, :])
def plot_field():
global base_url
global path
global select
global x_data
global y_data
global u_data
global v_data
global loadedfiles
base_url = 'http://servdap.legi.grenoble-inp.fr:80/opendap/'
path = base_url
loadedfiles = [[], []]
select = widgets.Select(
options=['.', '..'],
value='.',
description='Select a file or folder',
disabled=False
)
x_data = widgets.Dropdown(
options=['choose a dataset'],
value='choose a dataset',
description='x',
disabled=False
)
y_data = widgets.Dropdown(
options=['choose a dataset'],
value='choose a dataset',
description='y',
disabled=False
)
u_data = widgets.Dropdown(
options=['choose a dataset'],
value='choose a dataset',
description='u',
disabled=False
)
v_data = widgets.Dropdown(
options=['choose a dataset'],
value='choose a dataset',
description='v',
disabled=False
)
widgets.interact(f, select_loc=select, x_data_loc=x_data,
y_data_loc=y_data, u_data_loc=u_data, v_data_loc=v_data)