Skip to content

Commit efc0b52

Browse files
Merge pull request #900 from sadielbartholomew/docs-summer-student-2025-recipes
Add three new recipes (21 - 23) by 2025 summer student
2 parents bb8041c + 3908f20 commit efc0b52

28 files changed

+2647
-12
lines changed
39.3 KB
Loading

docs/source/conf.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,20 @@ def _get_date():
145145

146146
intersphinx_cache_limit = 5 # days to keep the cached inventories
147147
intersphinx_mapping = {
148-
"sphinx": ("https://www.sphinx-doc.org/en/master/", None),
148+
"sphinx": ("https://www.sphinx-doc.org/en/master", None),
149149
"python": ("https://docs.python.org/3", None),
150150
"numpy": ("https://numpy.org/doc/stable", None),
151-
"scipy": ("https://docs.scipy.org/doc/scipy/reference/", None),
151+
"scipy": ("https://docs.scipy.org/doc/scipy", None),
152152
# 'netCDF4': ("https://unidata.github.io/netcdf4-python", None),
153153
"cftime": ("https://unidata.github.io/cftime", None),
154154
"cfunits": ("https://ncas-cms.github.io/cfunits", None),
155155
"cfdm": ("https://ncas-cms.github.io/cfdm", None),
156-
"cfplot": ("https://ncas-cms.github.io/cf-plot/build/", None),
156+
"cfplot": ("https://ncas-cms.github.io/cf-plot", None),
157157
"dask": ("https://docs.dask.org/en/latest", None),
158-
"matplotlib": ("https://matplotlib.org/stable/", None),
158+
"matplotlib": ("https://matplotlib.org/stable", None),
159159
# REVIEW: h5: new intersphinx mapping
160160
"h5netcdf": ("https://h5netcdf.org", None),
161-
"zarr": ("https://zarr.readthedocs.io/en/stable/", None),
161+
"zarr": ("https://zarr.readthedocs.io/en/stable", None),
162162
}
163163

164164
# This extension is meant to help with the common pattern of having
@@ -387,16 +387,18 @@ def _get_date():
387387
"examples_dirs": "recipes", # path to recipe files
388388
"gallery_dirs": "recipes", # path to save gallery generated output
389389
"run_stale_examples": False,
390-
"reference_url": {"cf": None},
390+
# Below setting can be buggy: see:
391+
# https://github.com/sphinx-gallery/sphinx-gallery/issues/967
392+
#"reference_url": {"cf": None},
391393
"backreferences_dir": "gen_modules/backreferences",
392394
"doc_module": ("cf",),
393395
"inspect_global_variables": True,
394396
"within_subsection_order": FileNameSortKey,
395-
"default_thumb_file": "_static/logo.svg",
397+
"default_thumb_file": "_static/cf-recipe-placeholder-squarecrop.png",
396398
"image_scrapers": (
397399
"matplotlib",
398400
), # Ensures Matplotlib images are captured
399-
"plot_gallery": "True", # Enables plot rendering
401+
"plot_gallery": True, # Enables plot rendering
400402
"reset_modules": ("matplotlib",), # Helps with memory management
401403
"capture_repr": (),
402404
}
@@ -473,7 +475,6 @@ def _get_date():
473475

474476
import cf
475477

476-
link_release = re.search("(\d+\.\d+\.\d+)", release).groups()[0]
477478

478479

479480
def linkcode_resolve(domain, info):
@@ -483,7 +484,6 @@ def linkcode_resolve(domain, info):
483484
#
484485
# >> rm -fr build/.doctrees build/*/.doctrees build/*/*/.doctrees
485486
# =================================================================
486-
487487
online_source_code = True
488488

489489
if domain != "py":
@@ -547,6 +547,8 @@ def linkcode_resolve(domain, info):
547547
cfdm_version, fn, linespec
548548
)
549549
else:
550+
link_release = re.search("(\d+\.\d+\.\d+)", release).groups()[0]
551+
550552
# Point to on-line cf
551553
# code. E.g. https://github.com/NCAS-CMS/cf-python/blob/v3.0.1/cf/data/data.py#L4292
552554
url = "https://github.com/NCAS-CMS/cf-python/blob/v{0}/cf/{1}{2}".format(

docs/source/contributing.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,15 @@ ideas, code, and documentation to the cf library:
116116
* Bryan Lawrence
117117
* Charles Roberts
118118
* David Hassell
119-
* Evert Rol
119+
* Evert Rol
120+
* George Pulickan
120121
* Javier Dehesa
121122
* Jonathan Gregory
122123
* Klaus Zimmermann
123124
* Kristian Sebastián
124125
* Mark Rhodes-Smith
125126
* Matt Brown
127+
* Natalia Hunt
126128
* Michael Decker
127129
* Oliver Kotla
128130
* Sadie Bartholomew
69.4 KB
Loading
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
Calculating global mean temperature timeseries
3+
==============================================
4+
5+
In this recipe we will calculate and plot monthly and annual global mean temperature timeseries.
6+
"""
7+
8+
# %%
9+
# 1. Import cf-python and cf-plot:
10+
11+
import cfplot as cfp
12+
13+
import cf
14+
15+
# %%
16+
# 2. Read the field constructs:
17+
18+
f = cf.read("~/recipes/cru_ts4.06.1901.2021.tmp.dat.nc")
19+
print(f)
20+
21+
# %%
22+
# 3. Select near surface temperature by index and look at its contents:
23+
24+
temp = f[1]
25+
print(temp)
26+
27+
# %%
28+
# 4. Select latitude and longitude dimensions by identities, with two different techniques:
29+
30+
lon = temp.coordinate("long_name=longitude")
31+
lat = temp.coordinate("Y")
32+
33+
# %%
34+
# 5. Print the description of near surface temperature using the dump method to show properties of all constructs:
35+
36+
temp.dump()
37+
38+
# %%
39+
# 6. Latitude and longitude dimension coordinate cell bounds are absent, which are created and set:
40+
41+
a = lat.create_bounds()
42+
lat.set_bounds(a)
43+
lat.dump()
44+
45+
# %%
46+
47+
b = lon.create_bounds()
48+
lon.set_bounds(b)
49+
lon.dump()
50+
51+
# %%
52+
53+
print(b.array)
54+
55+
# %%
56+
# 7. Time dimension coordinate cell bounds are similarly created and set for cell sizes of one calendar month:
57+
58+
time = temp.coordinate("long_name=time")
59+
c = time.create_bounds(cellsize=cf.M())
60+
time.set_bounds(c)
61+
time.dump()
62+
63+
# %%
64+
# 8. Calculate and plot the area weighted mean surface temperature for each time:
65+
66+
global_avg = temp.collapse("area: mean", weights=True)
67+
cfp.lineplot(global_avg, color="red", title="Global mean surface temperature")
68+
69+
# %%
70+
# 9. Calculate and plot the annual global mean surface temperature:
71+
72+
annual_global_avg = global_avg.collapse("T: mean", group=cf.Y())
73+
cfp.lineplot(
74+
annual_global_avg,
75+
color="red",
76+
title="Annual global mean surface temperature",
77+
)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
Calculating and plotting the global average temperature anomalies
3+
=================================================================
4+
5+
In this recipe we will calculate and plot the global average temperature anomalies.
6+
"""
7+
8+
# %%
9+
# 1. Import cf-python and cf-plot:
10+
11+
import cfplot as cfp
12+
13+
import cf
14+
15+
# %%
16+
# 2. Read the field constructs:
17+
18+
f = cf.read("~/recipes/cru_ts4.06.1901.2021.tmp.dat.nc")
19+
print(f)
20+
21+
# %%
22+
# 3. Select near surface temperature by index and look at its contents:
23+
24+
temp = f[1]
25+
print(temp)
26+
27+
# %%
28+
# 4. Select latitude and longitude dimensions by identities, with two different techniques:
29+
30+
lon = temp.coordinate("long_name=longitude")
31+
lat = temp.coordinate("Y")
32+
33+
# %%
34+
# 5. Print the description of near surface temperature to show properties of all constructs:
35+
36+
temp.dump()
37+
38+
# %%
39+
# 6. Latitude and longitude dimension coordinate cell bounds are absent, which are created and set:
40+
41+
a = lat.create_bounds()
42+
lat.set_bounds(a)
43+
lat.dump()
44+
45+
# %%
46+
47+
b = lon.create_bounds()
48+
lon.set_bounds(b)
49+
lon.dump()
50+
51+
# %%
52+
53+
print(b.array)
54+
55+
# %%
56+
# 7. Time dimension coordinate cell bounds are similarly created and set for cell sizes of one calendar month:
57+
58+
time = temp.coordinate("long_name=time")
59+
c = time.create_bounds(cellsize=cf.M())
60+
time.set_bounds(c)
61+
time.dump()
62+
63+
# %%
64+
# 8. Calculate the area weighted mean surface temperature for each time using the collapse method:
65+
66+
global_avg = temp.collapse("area: mean", weights=True)
67+
68+
# %%
69+
# 9. Calculate the annual global mean surface temperature:
70+
71+
annual_global_avg = global_avg.collapse("T: mean", group=cf.Y())
72+
73+
# %%
74+
# 10. The temperature values are averaged for the climatological period of 1961-1990 by defining a subspace within these years using `cf.wi` query instance over subspace and doing a statistical collapse with the collapse method:
75+
76+
annual_global_avg_61_90 = annual_global_avg.subspace(
77+
T=cf.year(cf.wi(1961, 1990))
78+
)
79+
print(annual_global_avg_61_90)
80+
81+
# %%
82+
83+
temp_clim = annual_global_avg_61_90.collapse("T: mean")
84+
print(temp_clim)
85+
86+
# %%
87+
# 11. The temperature anomaly is then calculated by subtracting these climatological temperature values from the annual global average temperatures and plotted:
88+
89+
temp_anomaly = annual_global_avg - temp_clim
90+
cfp.lineplot(
91+
temp_anomaly,
92+
color="red",
93+
title="Global Average Temperature Anomaly (1901-2021)",
94+
ylabel="1961-1990 climatology difference ",
95+
yunits="degree Celcius",
96+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Plotting global mean temperatures spatially
3+
===========================================
4+
5+
In this recipe, we will plot the global mean temperature spatially.
6+
"""
7+
8+
# %%
9+
# 1. Import cf-python and cf-plot:
10+
11+
import cfplot as cfp
12+
13+
import cf
14+
15+
# %%
16+
# 2. Read the field constructs:
17+
18+
f = cf.read("~/recipes/cru_ts4.06.1901.2021.tmp.dat.nc")
19+
print(f)
20+
21+
# %%
22+
# 3. Select near surface temperature by index and look at its contents:
23+
24+
temp = f[1]
25+
print(temp)
26+
27+
# %%
28+
# 4. Average the monthly mean surface temperature values by the time axis using the collapse method:
29+
30+
global_avg = temp.collapse("mean", axes="long_name=time")
31+
32+
# %%
33+
# 5. Plot the global mean surface temperatures:
34+
35+
cfp.con(global_avg, lines=False, title="Global mean surface temperature")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Comparing two datasets with different resolutions using regridding
3+
==================================================================
4+
5+
In this recipe, we will regrid two different datasets with different resolutions. An example use case could be one where the observational dataset with a higher resolution needs to be regridded to that of the model dataset so that they can be compared with each other.
6+
"""
7+
8+
# %%
9+
# 1. Import cf-python:
10+
11+
import cf
12+
13+
# %%
14+
# 2. Read the field constructs:
15+
16+
obs = cf.read("~/recipes/cru_ts4.06.1901.2021.tmp.dat.nc", dask_chunks=None)
17+
print(obs)
18+
19+
# %%
20+
21+
model = cf.read(
22+
"~/recipes/tas_Amon_HadGEM3-GC3-1_hist-1p0_r3i1p1f2_gn_185001-201412.nc"
23+
)
24+
print(model)
25+
26+
# %%
27+
# 3. Select observation and model temperature fields by identity and index respectively, and look at their contents:
28+
29+
obs_temp = obs.select_field("long_name=near-surface temperature")
30+
print(obs_temp)
31+
32+
# %%
33+
34+
model_temp = model[0]
35+
print(model_temp)
36+
37+
# %%
38+
# 4. Regrid observational data to that of the model data and create a new low resolution observational data using bilinear interpolation:
39+
obs_temp_regrid = obs_temp.regrids(model_temp, method="linear")
40+
print(obs_temp_regrid)

0 commit comments

Comments
 (0)