-
Notifications
You must be signed in to change notification settings - Fork 23
Add new recipe (19) as started by summer student: per-season trends #821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sadielbartholomew
merged 12 commits into
NCAS-CMS:main
from
sadielbartholomew:student-recipes-4
Nov 8, 2024
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9437776
Add George's original recipe for sea level seasonal pressure
75741e6
Add George recipe info & make fixes for original recipe 19
sadielbartholomew 53b981b
Update GP recipe 19 to find per-season max and min
sadielbartholomew 3aaaf40
Update GP recipe 19 to view interesting window of 1980+
sadielbartholomew 1543841
Update GP recipe 19 to consolidate per-season logic
sadielbartholomew 05c8b40
Finalise GP recipe 19 plot and functional code
sadielbartholomew 24e4f92
Lint recipe 19: black, isort, flake8
sadielbartholomew 9aeadc3
Flesh out literate prog. comments for recipe 19
sadielbartholomew bc20305
Final pre-review updates for recipe 19
sadielbartholomew d39a47d
Add recipe 19 to recipes listing with filter keywords
sadielbartholomew 87a1932
Update docs/source/recipes/plot_19_recipe.py for consistent data path
sadielbartholomew d99bfd5
Merge branch 'main' into student-recipes-4
sadielbartholomew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
""" | ||
Plotting per-season trends in global sea surface tempreature extrema | ||
==================================================================== | ||
|
||
In this recipe we find the area-based extrema of global sea surface | ||
temperature per month and, because it is very difficult to | ||
interpret for trends when in a monthly form, we calculate and plot | ||
on top of this the mean across each season for both the minima and the | ||
maxima. | ||
""" | ||
|
||
# %% | ||
# 1. Import cf-python, cf-plot and other required packages: | ||
import cfplot as cfp | ||
import matplotlib.pyplot as plt | ||
|
||
import cf | ||
|
||
# %% | ||
# 2. Read the dataset in extract the SST Field from the FieldList: | ||
f = cf.read("~/recipes_break/ERA5_monthly_averaged_SST.nc") | ||
sst = f[0] # this gives the sea surface temperature (SST) | ||
|
||
# %% | ||
# 3. Collapse the SST data by area extrema (extrema over spatial dimensions): | ||
am_max = sst.collapse("area: maximum") # equivalent to "X Y: maximum" | ||
am_min = sst.collapse("area: minimum") # equivalent to "X Y: minimum" | ||
|
||
# %% | ||
# 4. Reduce all timeseries down to just 1980+ since there are some data | ||
# quality issues before 1970 and also this window is about perfect size | ||
# for viewing the trends without the line plot becoming too cluttered: | ||
am_max = am_max.subspace(T=cf.ge(cf.dt("1980-01-01"))) | ||
am_min = am_min.subspace(T=cf.ge(cf.dt("1980-01-01"))) | ||
|
||
# %% | ||
# 5. Create a mapping which provides the queries we need to collapse on | ||
# the four seasons, along with our description of them, as a value, with | ||
# the key of the string encoding the colour we want to plot these | ||
# trendlines in. This structure will be iterated over to make our plot: | ||
colours_seasons_mapping = { | ||
"red": (cf.mam(), "Mean across MAM: March, April and May"), | ||
"blue": (cf.jja(), "Mean across JJA: June, July and August"), | ||
"green": (cf.son(), "Mean across SON: September, October and November"), | ||
"purple": (cf.djf(), "Mean across DJF: December, January and February"), | ||
} | ||
|
||
# %% | ||
# 6. Create and open the plot file: | ||
cfp.gopen( | ||
rows=2, columns=1, bottom=0.1, top=0.85, file="global_avg_sst_plot.png" | ||
) | ||
|
||
# %% | ||
# 7. Put maxima subplot at top since these values are higher, given | ||
# increasing x axis. Note we set limits manually with 'gset' only to | ||
# allow space so the legend doesn't overlap the data, which isn't | ||
# possible purely from positioning it anywhere within the default plot. | ||
# Otherwise cf-plot handles this for us. To plot the per-season means | ||
# of the maxima, we loop through the season query mapping and do a | ||
# "T: mean" collapse setting the season as the grouping: | ||
cfp.gpos(1) | ||
cfp.gset(xmin="1980-01-01", xmax="2022-12-01", ymin=304, ymax=312) | ||
for colour, season_query in colours_seasons_mapping.items(): | ||
query_on_season, season_description = season_query | ||
am_max_collapse = am_max.collapse("T: mean", group=query_on_season) | ||
cfp.lineplot( | ||
am_max_collapse, | ||
color=colour, | ||
markeredgecolor=colour, | ||
marker="o", | ||
label=season_description, | ||
title="Maxima per month or season", | ||
) | ||
cfp.lineplot( | ||
am_max, | ||
color="grey", | ||
xlabel="", | ||
label="All months", | ||
) | ||
|
||
# %% | ||
# 8. Create and add minima subplot below the maxima one. Just like for the | ||
# maxima case, we plot per-season means by looping through the season query | ||
# mapping and doing a "T: mean" collapse setting the season as the grouping: | ||
cfp.gpos(2) | ||
cfp.gset(xmin="1980-01-01", xmax="2022-12-01", ymin=269, ymax=272) | ||
for colour, season_query in colours_seasons_mapping.items(): | ||
query_on_season, season_description = season_query | ||
am_min_collapse = am_min.collapse("T: mean", group=query_on_season) | ||
cfp.lineplot( | ||
am_min_collapse, | ||
color=colour, | ||
markeredgecolor=colour, | ||
marker="o", | ||
xlabel="", | ||
title="Minima per month or season", | ||
) | ||
cfp.lineplot( | ||
am_min, | ||
color="grey", | ||
) | ||
|
||
# %% | ||
# 9. Add an overall title to the plot and close the file to save it: | ||
plt.suptitle( | ||
"Global mean sea surface temperature (SST) monthly\nminima and maxima " | ||
"showing seasonal means of these extrema", | ||
fontsize=18, | ||
) | ||
cfp.gclose() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.