/
func_01emma_cyl.py
107 lines (94 loc) · 4.32 KB
/
func_01emma_cyl.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
from netCDF4 import Dataset
import requests
from lxml import etree
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widget
from IPython.display import display
global loadedfiles
loadedfiles = [[],[]]
def plot_func(variable, rootgrp, vector):
# Figure size
fig_xsize = 15
fig_ysize = 10
font_size = 25
fig = 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')
#axis names
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['lon'])-1, len(rootgrp.variables['lon']))
x, y = np.meshgrid(xlin, xlin)
if variable == 'norm':
norm = np.sqrt(rootgrp.variables['u'][0, 0, :, :]**2 +
rootgrp.variables['v'][0, 0, :, :]**2)
mycontour = plt.contourf(norm, 50, cmap=contourcolor)
cbar = plt.colorbar(mycontour)
cbar.ax.set_ylabel('norm (m/s)')
else:
varplot = rootgrp.variables[variable][0, 0, :, :]
mycontour = plt.contourf(varplot, 50, cmap=contourcolor)
cbar = plt.colorbar(mycontour)
cbar.ax.set_ylabel(rootgrp.variables[variable].name +
" (" + rootgrp.variables[variable].units + ")")
if vector:
plt.quiver(x, y, rootgrp.variables['u'][0, 0, :, :],
rootgrp.variables['v'][0, 0, :, :])
def get_folders(base_url):
catalog_file = requests.get(base_url + 'catalog.xml')
tree = etree.fromstring(catalog_file.content)
folders = []
for children in tree[2]:
if not '.' in children.get('name'):
folders.append(children.get('name'))
return folders
def create_plot():
base_url = 'http://servdap.legi.grenoble-inp.fr:80/opendap/coriolis/01EMMA_CYL/'
folders = get_folders(base_url)
interactive_plot = widget.interactive(g,
folder=widget.Select(options=folders,
description='Select a folder',
value='cc'
),
file=widget.IntText(value=1,
description='Select a file number:',
),
variable=widget.ToggleButtons(options=['u', 'v', 'norm'],
description='Variable:',
disabled=False,
button_style='',
),
vector=widget.Checkbox(value=False,
description='Display vectors',
disabled=False
),
);
output = interactive_plot.children[-1]
output.layout.height = '800px'
display(interactive_plot)
def g(folder, file, variable, vector):
url_base = 'http://servdap.legi.grenoble-inp.fr:80/opendap/coriolis/01EMMA_CYL/' + folder + '/'
catalog_file = requests.get(url_base + 'catalog.xml')
tree = etree.fromstring(catalog_file.content)
files = []
for children in tree[2]:
if '.nc' in children.get("name"):
files.append(children.get("name"))
print(str(len(files)) + ' files in folder ' + folder)
if file > len(files):
print('No file with number' + str(file))
else:
if files[file-1] in loadedfiles[0]:
rootgrp = loadedfiles[1][loadedfiles[0].index(files[file-1])]
else:
url = url_base + files[file-1]
rootgrp = Dataset(url)
loadedfiles[0].append(files[file-1])
loadedfiles[1].append(rootgrp)
plot_func(variable, rootgrp, vector)
plt.show()