Skip to content

Commit f0afbbd

Browse files
committed
Add Exercise_01
1 parent 0f9101f commit f0afbbd

File tree

4 files changed

+1210
-0
lines changed

4 files changed

+1210
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
text_representation:
5+
extension: .md
6+
format_name: markdown
7+
format_version: '1.3'
8+
jupytext_version: 1.14.4
9+
kernelspec:
10+
display_name: Python 3 (ipykernel)
11+
language: python
12+
name: python3
13+
---
14+
15+
<!-- #region tags=[] -->
16+
# LFRic Iris data manipulation and visualisation practical
17+
18+
Let's apply what we've learned about data processing and visualisation of LFRic data in Iris and PyVista with the two exercises. \
19+
20+
The aim is to use the prompts to write the code yourself, but we have also provided a separate notebook containing the answers if you are stuck \
21+
22+
All the information needed to write the code for this practical can be found in the notebooks in the first part of this practical \
23+
24+
Note: this is delivered in Jupyter labs, but sometime the PyVista and GeoVista plotting is laggy in labs. If you prefer you can run in ipython.
25+
26+
27+
<!-- #endregion -->
28+
29+
<!-- #region tags=[] -->
30+
## Exercise 1 - Regrid LFRic to UM and plot data
31+
32+
In this exercise you will take LFRic data, regrid it to UM data and then plot the differences
33+
<!-- #endregion -->
34+
35+
**Step 1** To begin, we need to import the neccesary packages that we will need for this exercise.
36+
37+
```python
38+
%matplotlib inline
39+
import pyvista as pv
40+
import geovista as gv
41+
import geovista.theme
42+
import iris.quickplot as qplt
43+
import iris
44+
from geovista import GeoPlotter
45+
from esmf_regrid.experimental.unstructured_scheme import MeshToGridESMFRegridder, GridToMeshESMFRegridder
46+
from iris.experimental.ugrid.load import PARSE_UGRID_ON_LOAD
47+
pv.rcParams["use_ipyvtk"] = True
48+
iris.FUTURE.datum_support = True # avoids some warnings
49+
```
50+
51+
The pv_conversions script contains two functions which convert LFRic cubes to pyvista objects. Load these two functions:
52+
53+
```python
54+
from pv_conversions import pv_from_lfric_cube
55+
```
56+
57+
**Step 2** Use iris.load_cube to load in the lfric data above, and select the diagnostic 'surface_air_pressure'. Print the cube - is it a mesh or grid? \
58+
(hint: you will need to use PARSE_UGRID_ON_LOAD.context() )
59+
60+
```python tags=[]
61+
# Define the location of the data and file names
62+
data_path =
63+
lfric_path = data_path + '20210324T0000Z_lf_ugrid.nc'
64+
um_path = data_path + '20210324T0000Z_um_latlon.nc'
65+
66+
with PARSE_UGRID_ON_LOAD.context():
67+
lfric_rho = iris.load_cube(lfric_path, 'surface_air_pressure')
68+
print(lfric_rho)
69+
```
70+
71+
<!-- #region tags=[] -->
72+
**Step 4** Select the first timestep from the data so we have a 2D cube
73+
<!-- #endregion -->
74+
75+
```python tags=[]
76+
77+
```
78+
79+
**Step 3** Transform the LFRic cube into a pyvista object using one of the functions from pv_conversions
80+
81+
```python
82+
83+
```
84+
85+
**Step 5** Use GeoPlotter to plot the data using PyVista. You will need to use plotter.show() to display the plot. \
86+
87+
You can now observe Surface Air Pressue on a 3D globe.
88+
89+
```python tags=[]
90+
91+
```
92+
93+
**Step 6** Now, lets regrid some LFRic data onto a lat/lon grid \
94+
95+
Use iris.load_cube to load the reference grid and print the cube. You can use the equivelent UM data loaded above for this.
96+
97+
```python
98+
99+
```
100+
101+
**Step 7** Create a regridder by using MeshToGridESMRegridder. Make sure the mesh and grid are the correct way round, consult the regridding section of the notebooks for help.
102+
103+
```python
104+
105+
```
106+
107+
**Step 8** Now, regrid the LFRic data using the regridder you created. Print the result - is your LFRic data regridded?
108+
109+
```python
110+
111+
```
112+
113+
**Step 9** Using qplt.pcolormesh plot the regridded LFRic data
114+
115+
```python tags=[]
116+
117+
```
118+
119+
**Step 10** We can use the UM data loaded as reference for the regridding to compare to the reggrided LFRic data.\
120+
Now select the first timestep of the UM data loaded in step 6
121+
122+
```python
123+
124+
```
125+
126+
**Step 11** Now calulate the difference between the LFRic regridded data and native UM data
127+
128+
```python
129+
130+
```
131+
132+
**Step 12** Now plot the original UM data and the regridded LFRic data and the difference side by side. \
133+
(hint: use plt.subplot(1,3,1) and try adding a title and coastlines to the plot)
134+
135+
```python
136+
137+
```
138+
139+
```python
140+
141+
```
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
text_representation:
5+
extension: .md
6+
format_name: markdown
7+
format_version: '1.3'
8+
jupytext_version: 1.14.4
9+
kernelspec:
10+
display_name: Python 3 (ipykernel)
11+
language: python
12+
name: python3
13+
---
14+
15+
<!-- #region tags=[] -->
16+
# LFRic Iris data manipulation and visualisation practical
17+
18+
Let's apply what we've learned about data processing and visualisation of LFRic data in Iris and PyVista with the two exercises. \
19+
20+
The aim is to use the prompts to write the code yourself, but we have also provided a separate notebook containing the answers if you are stuck \
21+
22+
All the information needed to write the code for this practical can be found in the notebooks in the first part of this practical \
23+
24+
Note: this is delivered in Jupyter labs, but sometime the PyVista and GeoVista plotting is laggy in labs. If you prefer you can run in ipython.
25+
26+
27+
<!-- #endregion -->
28+
29+
<!-- #region tags=[] -->
30+
## Exercise 1 - Regrid LFRic to UM and plot data
31+
32+
In this exercise you will take LFRic data, regrid it to UM data and then plot the differences
33+
<!-- #endregion -->
34+
35+
**Step 1** To begin, we need to import the neccesary packages that we will need for this exercise.
36+
37+
```python tags=[]
38+
%matplotlib inline
39+
import pyvista as pv
40+
import geovista as gv
41+
import geovista.theme
42+
import iris.quickplot as qplt
43+
import iris
44+
from geovista import GeoPlotter
45+
from esmf_regrid.experimental.unstructured_scheme import MeshToGridESMFRegridder, GridToMeshESMFRegridder
46+
from iris.experimental.ugrid.load import PARSE_UGRID_ON_LOAD
47+
pv.rcParams["use_ipyvtk"] = True
48+
iris.FUTURE.datum_support = True # avoids some warnings
49+
50+
```
51+
52+
The pv_conversions script contains two functions which convert LFRic cubes to pyvista objects. Load these two functions:
53+
54+
```python tags=[]
55+
from pv_conversions import pv_from_lfric_cube
56+
```
57+
58+
**Step 2** Use iris.load_cube to load in the lfric data above, and select the diagnostic 'surface_air_pressure'. Print the cube - is it a mesh or grid? \
59+
(hint: you will need to use PARSE_UGRID_ON_LOAD.context() )
60+
61+
```python tags=[]
62+
# Define the location of the data and file names
63+
data_path = '/scratch/bfock/example_data_iris-mesh-tutorial'
64+
data_path = '/scratch/bfock/example_data_u-ct674/'
65+
66+
lfric_path = data_path + '20210324T0000Z_lf_ugrid.nc'
67+
um_path = data_path + '20210324T0000Z_um_latlon.nc'
68+
69+
with PARSE_UGRID_ON_LOAD.context():
70+
lfric_rho = iris.load_cube(lfric_path, 'surface_air_pressure')
71+
print(lfric_rho)
72+
```
73+
74+
<!-- #region tags=[] -->
75+
**Step 4** Select the first timestep from the data so we have a 2D cube
76+
<!-- #endregion -->
77+
78+
```python tags=[]
79+
lfric_rho_t0 = lfric_rho[0]
80+
```
81+
82+
**Step 3** Transform the LFRic cube into a pyvista object using one of the functions from pv_conversions
83+
84+
```python tags=[]
85+
lfric_pv = pv_from_lfric_cube(lfric_rho_t0)
86+
```
87+
88+
**Step 5** Use GeoPlotter to plot the data using PyVista. You will need to use plotter.show() to display the plot. \
89+
90+
You can now observe Surface Air Pressue on a 3D globe.
91+
92+
```python tags=[]
93+
# Plot the data using pyvista
94+
plotter = GeoPlotter()
95+
_ = plotter.add_mesh(lfric_pv)
96+
plotter.show()
97+
```
98+
99+
**Step 6** Now, lets regrid some LFRic data onto a lat/lon grid \
100+
101+
Use iris.load_cube to load the reference grid and print the cube. You can use the equivelent UM data loaded above for this.
102+
103+
```python tags=[]
104+
um_rho = iris.load_cube(um_path, 'surface_air_pressure')
105+
print(um_rho)
106+
```
107+
108+
**Step 7** Create a regridder by using MeshToGridESMRegridder. Make sure the mesh and grid are the correct way round, consult the regridding section of the notebooks for help.
109+
110+
```python tags=[]
111+
# Create the regrider
112+
regridder = MeshToGridESMFRegridder(lfric_rho_t0, um_rho)
113+
```
114+
115+
**Step 8** Now, regrid the LFRic data using the regridder you created. Print the result - is your LFRic data regridded?
116+
117+
```python tags=[]
118+
result = regridder(lfric_rho_t0)
119+
print(result)
120+
```
121+
122+
**Step 9** Using qplt.pcolormesh plot the regridded LFRic data
123+
124+
```python tags=[]
125+
qplt.pcolormesh(result)
126+
```
127+
128+
**Step 10** We can use the UM data loaded as reference for the regridding to compare to the reggrided LFRic data.\
129+
Now select the first timestep of the UM data loaded in step 6
130+
131+
```python tags=[]
132+
um_rho_t0 = um_rho[0]
133+
```
134+
135+
**Step 11** Now calulate the difference between the LFRic regridded data and native UM data
136+
137+
```python tags=[]
138+
difference = result - um_rho_t0
139+
```
140+
141+
**Step 12** Now plot the original UM data and the regridded LFRic data and the difference side by side. \
142+
(hint: use plt.subplot(1,3,1) and try adding a title and coastlines to the plot)
143+
144+
```python
145+
qplt.plt.figure(figsize=(20,12))
146+
147+
qplt.plt.subplot(1, 3, 1)
148+
qplt.pcolormesh(result)
149+
qplt.plt.title("LFRic data")
150+
qplt.plt.gca().coastlines()
151+
152+
qplt.plt.subplot(1, 3, 2)
153+
qplt.pcolormesh(um_rho_t0)
154+
qplt.plt.title("UM data")
155+
qplt.plt.gca().coastlines()
156+
157+
qplt.plt.subplot(1, 3, 3)
158+
qplt.pcolormesh(difference, cmap="bwr")
159+
qplt.plt.title('Difference')
160+
qplt.plt.gca().coastlines()
161+
162+
qplt.plt.show()
163+
```
164+
165+
```python
166+
167+
```

0 commit comments

Comments
 (0)