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
# - demonstrate that the data in the grid file was probably a result of regridding from the mesh file.
182
227
```
183
228
229
+
<!-- #region -->
184
230
## Exercise 2: Zonal means
185
231
186
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.
@@ -190,6 +236,19 @@ Calculating zonal means can be done as a regridding operation where the zone is
190
236
191
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"`
192
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"`
203
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.
209
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
+
210
290
```python
211
291
lat_band_cube = Cube([[0, 0, 0, 0, 0, 0]])
212
292
lat_band_cube.add_dim_coord(lat_bands, 1)
213
293
lat_band_cube.add_dim_coord(lon_full, 0)
214
294
lat_band_cube
215
295
```
216
296
297
+
<!-- #region -->
217
298
**Step 4:** Create a regridder from `mesh_cube` to the single celled cube you created.
218
299
219
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.
@@ -222,24 +303,60 @@ If we initialise a regridder with `MeshToGridESMFRegridder(src_mesh, tgt_grid, r
222
303
223
304
Initialise a `MeshToGridESMFRegridder` with `mesh_cube` and your single celled cube as its arguments and with a `resolution=10` keyword.
224
305
306
+
<details><summary>Sample code solution : <b>click to reveal</b></summary>
**Step 6:** Repeat step 4 and 5 for `resolution=100`.
240
343
241
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.
242
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.
254
372
255
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.
256
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).
277
417
278
418
**Step 1:** Load a cube with humidity data using the `testdata_fetching` function `lfric_rh_alltimes_3d`.
279
419
420
+
<details><summary>Sample code solution : <b>click to reveal</b></summary>
421
+
280
422
```python
281
423
from testdata_fetching import lfric_rh_alltimes_3d
282
424
283
425
humidity_cube = lfric_rh_alltimes_3d()
284
426
humidity_cube
285
427
```
428
+
</details>
429
+
<!-- #endregion -->
286
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 -->
287
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).
288
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
+
289
452
```python
290
453
target_cube_lats = grid_cube[:,:1]
291
454
target_cube_lats.remove_coord("longitude")
@@ -302,8 +465,19 @@ target_cube_lats
302
465
# target_cube_lons
303
466
```
304
467
468
+
<!-- #region -->
305
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.
306
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.
324
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