You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The reason this is done in two steps is because initialising a regridder is potentially quite expensive if the grids or meshes involved are large. Once initialised, a regridder can regrid many source cubes (defined on the same source grid/mesh) onto the same target. We can demonstrate this by regridding a different cube using the same regridder.
@@ -73,8 +73,8 @@ mesh_temp
73
73
```python
74
74
# Regrid the new mesh cube using the same regridder.
75
75
# Note how the time coordinate is also transposed in the result.
76
-
result_2= regridder(mesh_temp)
77
-
result_2
76
+
regridded_temperature= regridder(mesh_temp)
77
+
regridded_temperature
78
78
```
79
79
80
80
We can save time in future runs by saving and loading a regridder with `save_regridder` and `load_regridder`.
We can then plot the difference between the UM data and the data regridded from LFRic. Since all our data is now on a latlon grid we can subtract to find the difference between the regridded LFRic data and equivalent UM data and plot this with matplotlib as normal.
113
113
114
114
```python
115
-
temp_diff = result_2 - grid_temp
115
+
temp_diff = regridded_temperature - grid_temp
116
+
temp_diff.long_name ="Difference in temperature"
116
117
117
118
# We choose a colormap that makes it clear where the differences are.
# - demonstrate that the data in the grid file was probably a result of regridding from the mesh file.
180
227
```
181
228
229
+
<!-- #region -->
182
230
## Exercise 2: Zonal means
183
231
184
232
For a latlon cube, a common operation is to collapse over longitude by taking an average. This is not possible for an LFRic style mesh cube since there is no independent longitude dimension to collapse. While it is possible to regrid to a latlon cube and then collapse, this introduces an additional step to the process. Instead, it is possible to simplify this into a single step by considering this as a regridding operation where the target cube contains multiple latitude bands.
@@ -188,6 +236,8 @@ Calculating zonal means can be done as a regridding operation where the zone is
188
236
189
237
**Step 1:** Define a latitude coordinate whose bounds are `[[-90, -60], [-60, -30], [-30, 0], [0, 30], [30, 60], [60, 90]]`. Remember to set the standard name to be `"latitude"` and the units to be `"degrees"`
190
238
239
+
<details><summary>Sample code solution : <b>click to reveal</b></summary>
**Step 2:** Define a longitude coordinate whose bounds are `[[-180, 180]]`. Remember to set the standard name to be `"longitude"` and the units to be `"degrees"`
201
263
264
+
<details><summary>Sample code solution : <b>click to reveal</b></summary>
**Step 3:** Create a six celled cube (i.e. `Cube([[0, 0, 0, 0, 0, 0]])`) and attach the latitude and longitude coordinates to it.
207
278
279
+
<details><summary>Sample code solution : <b>click to reveal</b></summary>
280
+
281
+
```python
282
+
lat_band_cube = Cube([[0, 0, 0, 0, 0, 0]])
283
+
lat_band_cube.add_dim_coord(lat_bands, 1)
284
+
lat_band_cube.add_dim_coord(lon_full, 0)
285
+
lat_band_cube
286
+
```
287
+
</details>
288
+
<!-- #endregion -->
289
+
208
290
```python
209
291
lat_band_cube = Cube([[0, 0, 0, 0, 0, 0]])
210
292
lat_band_cube.add_dim_coord(lat_bands, 1)
211
293
lat_band_cube.add_dim_coord(lon_full, 0)
212
294
lat_band_cube
213
295
```
214
296
297
+
<!-- #region -->
215
298
**Step 4:** Create a regridder from `mesh_cube` to the single celled cube you created.
216
299
217
300
*Note:* ESMF represents all lines as sections of great circles rather than lines of constant latitude. This means that `MeshToGridESMFRegridder` would fail to properly handle such a large cell. We can solve this problem by using the `resolution` keyword. By providing a `resolution`, we divide each cell into as many sub-cells each bounded by the same latitude bounds.
@@ -220,12 +303,33 @@ If we initialise a regridder with `MeshToGridESMFRegridder(src_mesh, tgt_grid, r
220
303
221
304
Initialise a `MeshToGridESMFRegridder` with `mesh_cube` and your single celled cube as its arguments and with a `resolution=10` keyword.
222
305
306
+
<details><summary>Sample code solution : <b>click to reveal</b></summary>
**Step 6:** Repeat step 4 and 5 for `resolution=100`.
238
343
239
344
Note the difference in value. Also note that it takes more time to initialise a regridder with higher resolution. Higher resolutions ought to be more accurate but there is a tradeoff between performance and accuracy.
240
345
346
+
<details><summary>Sample code solution : <b>click to reveal</b></summary>
**Step 7:** Repeat steps 1 - 6 for latitude bounds `[[-90, 90]]`, longitude bounds `[[-40, 40]]` and resolutions 2 and 10.
252
372
253
373
*Note:* Unlike lines of constant latitude, lines of constant longitude are already great circle arcs.This might suggest that the `resolution` argument is unnnecessary, however these arcs are 180 degrees which ESMF is unable to represent so we still need a `resolution` of at least 2. In this case, an increase in resolution will not affect the accuracy since a resolution of 2 will already have maximum accuracy. Note how the results are the equal.
254
374
375
+
<details><summary>Sample code solution : <b>click to reveal</b></summary>
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 latitude and time (or similarly, we can plot against longitude and time).
275
417
276
418
**Step 1:** Load a cube with humidity data using the `testdata_fetching` function `lfric_rh_alltimes_3d`.
277
419
420
+
<details><summary>Sample code solution : <b>click to reveal</b></summary>
421
+
278
422
```python
279
423
from testdata_fetching import lfric_rh_alltimes_3d
280
424
281
425
humidity_cube = lfric_rh_alltimes_3d()
282
426
humidity_cube
283
427
```
428
+
</details>
429
+
<!-- #endregion -->
284
430
431
+
```python
432
+
from testdata_fetching import lfric_rh_alltimes_3d
433
+
434
+
humidity_cube = lfric_rh_alltimes_3d()
435
+
humidity_cube
436
+
```
437
+
438
+
<!-- #region -->
285
439
**Step 2:** Create a target cube whose latitude coordinate is derived from the UM cube loaded from `um_orography` and whose longitude 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 longitude coordinate and adding a longitude coordinate with bounds `[[-180, 180]]` (you can reuse the coordinate from exercise 2).
286
440
441
+
<details><summary>Sample code solution : <b>click to reveal</b></summary>
442
+
443
+
```python
444
+
target_cube_lats = grid_cube[:,:1]
445
+
target_cube_lats.remove_coord("longitude")
446
+
target_cube_lats.add_dim_coord(lon_full, 1)
447
+
target_cube_lats
448
+
```
449
+
</details>
450
+
<!-- #endregion -->
451
+
287
452
```python
288
453
target_cube_lats = grid_cube[:,:1]
289
454
target_cube_lats.remove_coord("longitude")
@@ -300,8 +465,19 @@ target_cube_lats
300
465
# target_cube_lons
301
466
```
302
467
468
+
<!-- #region -->
303
469
**Step 3:** Create a `MeshToGridESMFRegridder` regridder from the slice of the humidity cube onto the target cube. Set the resolution keyword to 500 (this should be good balance of accuracy and performance). Use this regridder to create a resulting cube.
304
470
471
+
<details><summary>Sample code solution : <b>click to reveal</b></summary>
**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
499
500
+
<details><summary>Sample code solution : <b>click to reveal</b></summary>
501
+
502
+
```python
503
+
import matplotlib.dates as mdates
504
+
# We use a colormap which highlights fine details.
0 commit comments