Skip to content

Commit ca50ee9

Browse files
add hovmoller section
1 parent 8cb845b commit ca50ee9

File tree

2 files changed

+707
-70
lines changed

2 files changed

+707
-70
lines changed

notebooks/.notebook_shadow_copies/Sec_05_Regridding.md

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,6 @@ plt.gca().coastlines()
123123
plt.show()
124124
```
125125

126-
```python
127-
# Consider a bonus task to measure the relative time taken.
128-
# import time... etc
129-
```
130-
131126
We can also regrid from latlon grids to LFRic style meshes using `GridToMeshESMFRegridder`.
132127

133128
```python
@@ -138,6 +133,11 @@ result_3 = g2m_regridder(grid_cube)
138133
result_3
139134
```
140135

136+
```python
137+
# Bonus task:
138+
# Use %%timeit to investigate how much time it takes to initialise a regridder vs applying the regridder.
139+
```
140+
141141
## Exercise 1: Comparing regridding methods
142142

143143
By default, regridding uses the area weighted `conservative` method. We can also use the bilinear regridding method.
@@ -209,10 +209,10 @@ lon_full = DimCoord(0, bounds=[[-180, 180]], standard_name="longitude", units="d
209209
**Step 3:** Create a single celled cube (i.e. `Cube([[0]])`) and attach the latitude and longitude coordinates to it.
210210

211211
```python
212-
lat_band_cube = Cube([[0, 0, 0, 0, 0, 0]])
212+
lat_band_cube = Cube(np.zeros((1,) + lat_bands.shape))
213213
lat_band_cube.add_dim_coord(lat_bands, 1)
214214
lat_band_cube.add_dim_coord(lon_full, 0)
215-
print(lat_band_cube)
215+
lat_band_cube
216216
```
217217

218218
**Step 4:** Create a regridder from `mesh_cube` to the single celled cube you created.
@@ -232,6 +232,9 @@ lat_band_mean_calculator_10 = MeshToGridESMFRegridder(mesh_cube, lat_band_cube,
232232
```python
233233
lat_band_mean_10 = lat_band_mean_calculator_10(mesh_cube)
234234
print(lat_band_mean_10.data)
235+
iqplt.pcolormesh(lat_band_mean_10)
236+
plt.gca().coastlines()
237+
plt.show()
235238
```
236239

237240
**Step 6:** Repeat step 4 and 5 for `resolution=100`.
@@ -242,6 +245,10 @@ Note the difference in value. Also note that it takes more time to initialise a
242245
lat_band_mean_calculator_100 = MeshToGridESMFRegridder(mesh_cube, lat_band_cube, resolution=100)
243246
lat_band_mean_100 = lat_band_mean_calculator_100(mesh_cube)
244247
print(lat_band_mean_100.data)
248+
249+
iqplt.pcolormesh(lat_band_mean_100)
250+
plt.gca().coastlines()
251+
plt.show()
245252
```
246253

247254
**Step 7:** Repeat steps 1 - 6 for latitude bounds `[[-90, 90]]`, longitude bounds `[[-40, 40]]` and resolutions 2 and 10.
@@ -265,8 +272,72 @@ lon_band_mean_10 = lon_band_mean_calculator_10(mesh_cube)
265272
print(lon_band_mean_10.data)
266273
```
267274

275+
## Exercise 3: Hovmoller plots
276+
277+
If we have data on aditional dimensions, we can use the same approach as exercise 2 to produce a Hovmoller diagram. That is, if we have data that varies along time we can take the area weighted mean over latitude bands and plot the data aginst longitude and time (or similarly, we can plot against latitude and time).
278+
279+
**Step 1:** Load a temperature cube using the `testdata_fetching` function `lfric_temp`. Extract a single pressure slice using the `cube.extract` method with a constraint `iris.Constraint(pressure=850)` as the argument (we choose this level because it has noticable details).
280+
281+
282+
from testdata_fetching import fric_temp
283+
mesh_temp = lfric_temp()
284+
285+
temp_slice = mesh_temp.extract(iris.Constraint(pressure=850))
286+
temp_slice
287+
288+
289+
**Step 2:** Create a target cube whose longitude coordinate is derived from the UM cube loaded from `um_orography` and whose latitude coordinate has bounds `[[-180, 180]]`. This can be done by slicing a cube derived from `um_orography` (using the slice `[:1]` so that this dimension isnt collapsed), removing the latitude coordinate and adding a latitude coordinate with bounds `[[-180, 180]]` (you can reuse the coordinate from exercise 2).
290+
291+
```python
292+
target_cube_lons = grid_cube[:1]
293+
target_cube_lons.remove_coord("latitude")
294+
target_cube_lons.add_dim_coord(lat_full, 0)
295+
target_cube_lons
296+
```
297+
298+
```python
299+
# We also can do the same thing for bands of constant latitude.
300+
301+
# target_cube_lats = grid_cube[:,:1]
302+
# target_cube_lats.remove_coord("longitude")
303+
# target_cube_lats.add_dim_coord(lon_full, 1)
304+
# target_cube_lats
305+
```
306+
307+
**Step 3:** Create a `MeshToGridESMFRegridder` regridder from the slice of the temperature cube onto the target cube. Set the resolution keyword to 2 (this should be sufficient since these are bands of constant longitude). Use this regridder to create a resulting cube.
308+
309+
```python
310+
um_lon_band_mean_calculator = MeshToGridESMFRegridder(temp_slice, target_cube_lons, resolution=2)
311+
um_lon_bound_means = um_lon_band_mean_calculator(temp_slice)
312+
um_lon_bound_means
313+
```
314+
315+
```python
316+
# um_lat_band_mean_calculator = MeshToGridESMFRegridder(temp_slice, target_cube, resolution=500)
317+
# um_lat_bound_means = um_lat_band_mean_calculator(temp_slice)
318+
# um_lat_bound_means
319+
```
320+
321+
**Step 4:** Plot the data in the resulting cube. This can be done with `iqplt.pcolormesh`. Note that the resulting cube will have an unnecessary dimension which will have to be sliced (using `[:, 0]`). Note that extra steps can be taken to format the dates for this plot.
322+
323+
```python
324+
import matplotlib.dates as mdates
325+
326+
iqplt.pcolormesh(um_lon_bound_means[:, 0, :])
327+
plt.gca().yaxis.set_major_formatter(mdates.DateFormatter("%D"))
328+
plt.show()
329+
```
330+
331+
```python
332+
# import matplotlib.dates as mdates
333+
# iqplt.pcolormesh(um_lat_bound_means[:, :, 0])
334+
# plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%D"))
335+
# plt.show()
336+
```
337+
268338
```python
269339
# Bonus Exercise:
340+
270341
# Create a regridder onto a single celled cube which represents the whole earth.
271342
# Use this regridder to compare how well bilinear regridding and area weighted
272343
# regridding preserve area weighted mean after round tripping.

0 commit comments

Comments
 (0)