|
| 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 | +## Exercise 2 - Regrid UM data to LFRic and plot using PyVista |
| 30 | + |
| 31 | + |
| 32 | +Now you can do a similar exercise compared to the previous Exercise 1, but regrid UM data onto a LFRic mesh and plot the data using PyVista |
| 33 | + |
| 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 | +from pv_conversions import pv_from_um_cube |
| 56 | +``` |
| 57 | + |
| 58 | +**Step 2** Lets chose a different diagnostic, 'surface_temperature' and load both the UM data, as well LFRic data to use as reference grid. |
| 59 | + |
| 60 | +```python |
| 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 | + |
| 69 | +um_temp = iris.load_cube(um_path, 'surface_temperature') |
| 70 | +um_temp_t0 = um_temp[0] |
| 71 | +``` |
| 72 | + |
| 73 | +**Step 3** Initialise the regridder 'GridToMeshESMFRegridder' \ |
| 74 | +Then use the regridder to regrid the new UM cube created earlier. Print your result and notice the mesh characterisics. |
| 75 | + |
| 76 | +```python |
| 77 | +g2m_regridder = GridToMeshESMFRegridder(um_temp_t0, lfric_rho) |
| 78 | +regridded_um = g2m_regridder(um_temp_t0) |
| 79 | +print(regridded_um) |
| 80 | +``` |
| 81 | + |
| 82 | +**Step 4** Plot the regridded UM data with PyVista. \ |
| 83 | +(hint: before you can do this you will need to convert you mesh to polydata using pv_from_lfric_cube) |
| 84 | + |
| 85 | +```python |
| 86 | +pv = pv_from_lfric_cube(regridded_um) |
| 87 | +pv.plot() |
| 88 | +``` |
| 89 | + |
| 90 | +**Step 5** Plot the native UM data with PyVista. \ |
| 91 | +(hint: before you can do this you will need to convert you mesh to polydata using pv_from_um_cube) |
| 92 | + |
| 93 | +```python |
| 94 | +um_pv = pv_from_um_cube(um_temp_t0) |
| 95 | +um_pv.plot() |
| 96 | +``` |
| 97 | + |
| 98 | +**Step 6** Now we can plot this data side by side \ |
| 99 | +(hints: start by using plotter = GeoPlotter(shape=(1,2)), then create your subplots, add your coastlines, add a base layer, and add you mesh) \ |
| 100 | +note: PyVista and GeoVista can be slow in jupyter labs, but try and move the plots, look at the poles - you might notice the polar sigularity problem of the lat-lon grid. |
| 101 | + |
| 102 | +```python |
| 103 | +plotter = GeoPlotter(shape=(1, 2)) |
| 104 | + |
| 105 | +plotter.subplot(0, 0) |
| 106 | +plotter.add_coastlines() |
| 107 | +plotter.add_base_layer(texture=gv.natural_earth_1()) |
| 108 | +plotter.add_mesh(pv, show_edges=True) |
| 109 | + |
| 110 | +plotter.subplot(0, 1) |
| 111 | +plotter.add_coastlines() |
| 112 | +#plotter.add_base_layer(texture=gv.natural_earth_1()) |
| 113 | +plotter.add_mesh(um_pv, show_edges=True) |
| 114 | + |
| 115 | +# Make left+right move together |
| 116 | +plotter.link_views() |
| 117 | + |
| 118 | +# Try to rationalise the global view a little. |
| 119 | +plotter.view_xz() |
| 120 | + |
| 121 | +plotter.show() |
| 122 | +``` |
| 123 | + |
| 124 | +**Step 7** In notebook Section 3, we see how to use the plotter.camera_position = viewpoint functionality. Try this out with the surface temperature data. |
| 125 | + |
| 126 | +```python |
| 127 | +viewpoint = [ |
| 128 | + (0.9550352379408845, 0.9378277371075855, 0.9637172962958191), |
| 129 | + (0.0, 0.0, 0.0), |
| 130 | + (-0.3202752464164225, -0.5004192729867466, 0.8043657860428399) |
| 131 | +] |
| 132 | + |
| 133 | +plotter.camera_position = viewpoint |
| 134 | + |
| 135 | +plotter.show() |
| 136 | +``` |
| 137 | + |
| 138 | +**Finished!?** These two exercises were just the beginning. If you have time try adding some cells below and extract a zonal mean, or try to select a region of data. |
0 commit comments