From 046cf8fc27c5b3e309861ff72eec575d207f0c5f Mon Sep 17 00:00:00 2001 From: Natalia Hunt <> Date: Wed, 2 Oct 2024 17:06:07 +0100 Subject: [PATCH 01/10] Add Natalia's recipe to showcase plotting projections --- docs/source/recipes/plot_16_recipe.py | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 docs/source/recipes/plot_16_recipe.py diff --git a/docs/source/recipes/plot_16_recipe.py b/docs/source/recipes/plot_16_recipe.py new file mode 100644 index 0000000000..dc93d1406e --- /dev/null +++ b/docs/source/recipes/plot_16_recipe.py @@ -0,0 +1,59 @@ +""" +Plotting Contour Subplots with different Projections +============================================ +In this recipe, we will plot different projections for the same data to illustrate +visually the ones available in order to more visually decide which is suitable. +""" + +# %% +# 1. Import cf-python and cf-plot: + +import cfplot as cfp + +import cf + +#%% +# 2. Read the field in: +# Here I've used sample data ggap.nc (and later pressure=850), +# but you could use tas_A1.nc (with time=15) + +f=cf.read("~/cfplot_data/ggap.nc")[0] + +#%% +# 3. Create the file with subplots: +# If you are changing the number of subplots ensure the number of rows * number +# of columns = the number of subplots/projections +# Here we are doing 6 projections so 2x3 is fine + +cfp.gopen(rows=2, columns=3, bottom=0.2, file="projections.png") + +#%% +# 4. List the projection types being used: +# Here we are using Cylindrical/Default, North Pole Stereographic, +# South Pole Stereographic, Mollweide, Cropped Lambert Conformal and Robinson +# However you could also use other such as "rotated", "ortho" or +# "merc", "ukcp", "osgb", or "EuroPP" +# https://ncas-cms.github.io/cf-plot/build/user_guide.html#appendixc + +projtypes = ["cyl", "npstere", "spstere", "moll", "lcc", "robin"] + +#%% +# 5. We then use a for loop to cycle through all the different projection types: +# Only gpos has 1 added because it can only take 1 as its first value, otherwise there are +# errors. There are if statements for some projections (lcc, OSGB and EuroPP) as they have +# specific requirements for their contour. +# However, OSGB and EuroPP will require very different data anyway. + +for i, proj in enumerate(projtypes): + cfp.gpos(i+1) + if projtypes[i] == "lcc": + cfp.mapset(proj='lcc', lonmin=-50, lonmax=50, latmin=20, latmax=85) + if (projtypes[i]== "OSGB") or (projtypes[i] =="EuroPP"): + cfp.mapset(proj=projtypes[i], resolution='50m') + else: + cfp.mapset(proj=projtypes[i]) + if i ==len(projtypes)-1: + cfp.con(f.subspace(pressure=850), lines=False, title = projtypes[i], colorbar_position=[0.1, 0.1, 0.8, 0.02], colorbar_orientation='horizontal') #to see the marking lines need to be True, but this can be hard to read so if not needed use False + else: + cfp.con(f.subspace(pressure=850), lines = False, title = projtypes[i], colorbar = False) +cfp.gclose() \ No newline at end of file From 82021ee6f864c70d88c52fe1bbc78edcdc6e2f54 Mon Sep 17 00:00:00 2001 From: "Sadie L. Bartholomew" Date: Wed, 2 Oct 2024 17:10:16 +0100 Subject: [PATCH 02/10] Lint (black, isort, flake8) NH original recipe 16 --- docs/source/recipes/plot_16_recipe.py | 53 ++++++++++++++++----------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/docs/source/recipes/plot_16_recipe.py b/docs/source/recipes/plot_16_recipe.py index dc93d1406e..3d195a32cd 100644 --- a/docs/source/recipes/plot_16_recipe.py +++ b/docs/source/recipes/plot_16_recipe.py @@ -1,6 +1,7 @@ """ Plotting Contour Subplots with different Projections -============================================ +==================================================== + In this recipe, we will plot different projections for the same data to illustrate visually the ones available in order to more visually decide which is suitable. """ @@ -9,51 +10,61 @@ # 1. Import cf-python and cf-plot: import cfplot as cfp - import cf -#%% +# %% # 2. Read the field in: -# Here I've used sample data ggap.nc (and later pressure=850), +# Here I've used sample data ggap.nc (and later pressure=850), # but you could use tas_A1.nc (with time=15) -f=cf.read("~/cfplot_data/ggap.nc")[0] +f = cf.read("~/cfplot_data/ggap.nc")[0] -#%% +# %% # 3. Create the file with subplots: -# If you are changing the number of subplots ensure the number of rows * number +# If you are changing the number of subplots ensure the number of rows * number # of columns = the number of subplots/projections # Here we are doing 6 projections so 2x3 is fine cfp.gopen(rows=2, columns=3, bottom=0.2, file="projections.png") -#%% +# %% # 4. List the projection types being used: -# Here we are using Cylindrical/Default, North Pole Stereographic, +# Here we are using Cylindrical/Default, North Pole Stereographic, # South Pole Stereographic, Mollweide, Cropped Lambert Conformal and Robinson -# However you could also use other such as "rotated", "ortho" or +# However you could also use other such as "rotated", "ortho" or # "merc", "ukcp", "osgb", or "EuroPP" # https://ncas-cms.github.io/cf-plot/build/user_guide.html#appendixc -projtypes = ["cyl", "npstere", "spstere", "moll", "lcc", "robin"] +projtypes = ["cyl", "npstere", "spstere", "moll", "lcc", "robin"] -#%% +# %% # 5. We then use a for loop to cycle through all the different projection types: -# Only gpos has 1 added because it can only take 1 as its first value, otherwise there are +# Only gpos has 1 added because it can only take 1 as its first value, otherwise there are # errors. There are if statements for some projections (lcc, OSGB and EuroPP) as they have # specific requirements for their contour. # However, OSGB and EuroPP will require very different data anyway. for i, proj in enumerate(projtypes): - cfp.gpos(i+1) + cfp.gpos(i + 1) if projtypes[i] == "lcc": - cfp.mapset(proj='lcc', lonmin=-50, lonmax=50, latmin=20, latmax=85) - if (projtypes[i]== "OSGB") or (projtypes[i] =="EuroPP"): - cfp.mapset(proj=projtypes[i], resolution='50m') + cfp.mapset(proj="lcc", lonmin=-50, lonmax=50, latmin=20, latmax=85) + if (projtypes[i] == "OSGB") or (projtypes[i] == "EuroPP"): + cfp.mapset(proj=projtypes[i], resolution="50m") else: cfp.mapset(proj=projtypes[i]) - if i ==len(projtypes)-1: - cfp.con(f.subspace(pressure=850), lines=False, title = projtypes[i], colorbar_position=[0.1, 0.1, 0.8, 0.02], colorbar_orientation='horizontal') #to see the marking lines need to be True, but this can be hard to read so if not needed use False + if i == len(projtypes) - 1: + cfp.con( + f.subspace(pressure=850), + lines=False, + title=projtypes[i], + colorbar_position=[0.1, 0.1, 0.8, 0.02], + colorbar_orientation="horizontal", + ) # to see the marking lines need to be True, but this can be hard to read so if not needed use False else: - cfp.con(f.subspace(pressure=850), lines = False, title = projtypes[i], colorbar = False) -cfp.gclose() \ No newline at end of file + cfp.con( + f.subspace(pressure=850), + lines=False, + title=projtypes[i], + colorbar=False, + ) +cfp.gclose() From 4b255e1ded0f3cb09c20497dda530e9ab67ef96c Mon Sep 17 00:00:00 2001 From: "Sadie L. Bartholomew" Date: Wed, 2 Oct 2024 17:13:13 +0100 Subject: [PATCH 03/10] Line wrap comments in NH original recipe 16 --- docs/source/recipes/plot_16_recipe.py | 33 +++++++++++++++------------ 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/docs/source/recipes/plot_16_recipe.py b/docs/source/recipes/plot_16_recipe.py index 3d195a32cd..4762832598 100644 --- a/docs/source/recipes/plot_16_recipe.py +++ b/docs/source/recipes/plot_16_recipe.py @@ -1,49 +1,52 @@ """ -Plotting Contour Subplots with different Projections +Plotting contour subplots with different projections ==================================================== -In this recipe, we will plot different projections for the same data to illustrate -visually the ones available in order to more visually decide which is suitable. +In this recipe, we will plot different projections for the same +data to illustrate visually the ones available in order to more +visually decide which is suitable. + """ # %% # 1. Import cf-python and cf-plot: import cfplot as cfp + import cf # %% # 2. Read the field in: # Here I've used sample data ggap.nc (and later pressure=850), # but you could use tas_A1.nc (with time=15) - f = cf.read("~/cfplot_data/ggap.nc")[0] # %% # 3. Create the file with subplots: -# If you are changing the number of subplots ensure the number of rows * number -# of columns = the number of subplots/projections +# If you are changing the number of subplots ensure the number of +# rows * number of columns = the number of subplots/projections # Here we are doing 6 projections so 2x3 is fine - cfp.gopen(rows=2, columns=3, bottom=0.2, file="projections.png") # %% # 4. List the projection types being used: # Here we are using Cylindrical/Default, North Pole Stereographic, -# South Pole Stereographic, Mollweide, Cropped Lambert Conformal and Robinson +# South Pole Stereographic, Mollweide, Cropped Lambert Conformal +# and Robinson. # However you could also use other such as "rotated", "ortho" or # "merc", "ukcp", "osgb", or "EuroPP" # https://ncas-cms.github.io/cf-plot/build/user_guide.html#appendixc - projtypes = ["cyl", "npstere", "spstere", "moll", "lcc", "robin"] # %% -# 5. We then use a for loop to cycle through all the different projection types: -# Only gpos has 1 added because it can only take 1 as its first value, otherwise there are -# errors. There are if statements for some projections (lcc, OSGB and EuroPP) as they have +# 5. We then use a for loop to cycle through all the different projection +# types: +# Only gpos has 1 added because it can only take 1 as its first value, +# otherwise there are +# errors. There are if statements for some projections (lcc, OSGB and +# EuroPP) as they have # specific requirements for their contour. # However, OSGB and EuroPP will require very different data anyway. - for i, proj in enumerate(projtypes): cfp.gpos(i + 1) if projtypes[i] == "lcc": @@ -59,7 +62,9 @@ title=projtypes[i], colorbar_position=[0.1, 0.1, 0.8, 0.02], colorbar_orientation="horizontal", - ) # to see the marking lines need to be True, but this can be hard to read so if not needed use False + ) + # to see the marking lines need to be True, but this can be + # hard to read so if not needed use False else: cfp.con( f.subspace(pressure=850), From 5676a5a068d8d344cc00a87e7b356b295137fbbe Mon Sep 17 00:00:00 2001 From: "Sadie L. Bartholomew" Date: Wed, 2 Oct 2024 17:50:23 +0100 Subject: [PATCH 04/10] Temporary fix for possible cf-plot bug to NH original recipe 16 --- docs/source/recipes/plot_16_recipe.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/source/recipes/plot_16_recipe.py b/docs/source/recipes/plot_16_recipe.py index 4762832598..3d1095c7a8 100644 --- a/docs/source/recipes/plot_16_recipe.py +++ b/docs/source/recipes/plot_16_recipe.py @@ -19,7 +19,8 @@ # 2. Read the field in: # Here I've used sample data ggap.nc (and later pressure=850), # but you could use tas_A1.nc (with time=15) -f = cf.read("~/cfplot_data/ggap.nc")[0] +PATH = "~/git-repos/cf-plot/cfplot/test/cfplot_data" +f = cf.read(f"{PATH}/ggap.nc")[0] # %% # 3. Create the file with subplots: @@ -36,7 +37,9 @@ # However you could also use other such as "rotated", "ortho" or # "merc", "ukcp", "osgb", or "EuroPP" # https://ncas-cms.github.io/cf-plot/build/user_guide.html#appendixc -projtypes = ["cyl", "npstere", "spstere", "moll", "lcc", "robin"] +# TODO SB update second 'cyl' to 'lcc', replaced for now due to possible bug, +# see: https://github.com/NCAS-CMS/cf-plot/issues/75 +projtypes = ["cyl", "npstere", "spstere", "moll", "cyl", "robin"] # %% # 5. We then use a for loop to cycle through all the different projection From 26e6c4e168b85ab8529c2a1b044bb2dea4e8b40a Mon Sep 17 00:00:00 2001 From: "Sadie L. Bartholomew" Date: Wed, 2 Oct 2024 18:05:19 +0100 Subject: [PATCH 05/10] Feedback for Natalia: consolidating the Python code --- docs/source/recipes/plot_16_recipe.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/source/recipes/plot_16_recipe.py b/docs/source/recipes/plot_16_recipe.py index 3d1095c7a8..6c09eb0a38 100644 --- a/docs/source/recipes/plot_16_recipe.py +++ b/docs/source/recipes/plot_16_recipe.py @@ -52,17 +52,17 @@ # However, OSGB and EuroPP will require very different data anyway. for i, proj in enumerate(projtypes): cfp.gpos(i + 1) - if projtypes[i] == "lcc": + if proj == "lcc": cfp.mapset(proj="lcc", lonmin=-50, lonmax=50, latmin=20, latmax=85) - if (projtypes[i] == "OSGB") or (projtypes[i] == "EuroPP"): - cfp.mapset(proj=projtypes[i], resolution="50m") + if proj in ("OSGB", "EuroPP"): + cfp.mapset(proj=proj, resolution="50m") else: - cfp.mapset(proj=projtypes[i]) + cfp.mapset(proj=proj) if i == len(projtypes) - 1: cfp.con( f.subspace(pressure=850), lines=False, - title=projtypes[i], + title=proj, colorbar_position=[0.1, 0.1, 0.8, 0.02], colorbar_orientation="horizontal", ) @@ -72,7 +72,7 @@ cfp.con( f.subspace(pressure=850), lines=False, - title=projtypes[i], + title=proj, colorbar=False, ) cfp.gclose() From 67d253b661b0a9ea7f69c93ea726c49266740aa6 Mon Sep 17 00:00:00 2001 From: "Sadie L. Bartholomew" Date: Thu, 3 Oct 2024 15:06:27 +0100 Subject: [PATCH 06/10] Simplify code given revised proj choices in NH original recipe 16 --- docs/source/recipes/plot_16_recipe.py | 47 +++++++++------------------ 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/docs/source/recipes/plot_16_recipe.py b/docs/source/recipes/plot_16_recipe.py index 6c09eb0a38..4e36cfb349 100644 --- a/docs/source/recipes/plot_16_recipe.py +++ b/docs/source/recipes/plot_16_recipe.py @@ -17,47 +17,32 @@ # %% # 2. Read the field in: -# Here I've used sample data ggap.nc (and later pressure=850), -# but you could use tas_A1.nc (with time=15) PATH = "~/git-repos/cf-plot/cfplot/test/cfplot_data" f = cf.read(f"{PATH}/ggap.nc")[0] # %% -# 3. Create the file with subplots: -# If you are changing the number of subplots ensure the number of -# rows * number of columns = the number of subplots/projections -# Here we are doing 6 projections so 2x3 is fine +# 3. Create the file with subplots. If changing the number of subplots, +# ensure the number of rows * number of columns = the number of projections. +# Here we are doing 6 projections so 2 x 3 is fine: cfp.gopen(rows=2, columns=3, bottom=0.2, file="projections.png") # %% -# 4. List the projection types being used: -# Here we are using Cylindrical/Default, North Pole Stereographic, -# South Pole Stereographic, Mollweide, Cropped Lambert Conformal -# and Robinson. -# However you could also use other such as "rotated", "ortho" or -# "merc", "ukcp", "osgb", or "EuroPP" -# https://ncas-cms.github.io/cf-plot/build/user_guide.html#appendixc -# TODO SB update second 'cyl' to 'lcc', replaced for now due to possible bug, -# see: https://github.com/NCAS-CMS/cf-plot/issues/75 -projtypes = ["cyl", "npstere", "spstere", "moll", "cyl", "robin"] +# 4. List the projection types to use. Here we are using +# Cylindrical/Default, North Pole Stereographic, South Pole Stereographic, +# Mollweide, Mercator and Robinson. However there are several other choices +# possible, see: +# https://ncas-cms.github.io/cf-plot/build/user_guide.html#appendixc. Our +# chosen list is: +projtypes = ["cyl", "npstere", "spstere", "moll", "merc", "robin"] # %% -# 5. We then use a for loop to cycle through all the different projection -# types: -# Only gpos has 1 added because it can only take 1 as its first value, -# otherwise there are -# errors. There are if statements for some projections (lcc, OSGB and -# EuroPP) as they have -# specific requirements for their contour. -# However, OSGB and EuroPP will require very different data anyway. +# 5. Loop through the list of projection types and plot each as a sub-plot: for i, proj in enumerate(projtypes): + # gpos has 1 added to the index because it takes 1 as its first value cfp.gpos(i + 1) - if proj == "lcc": - cfp.mapset(proj="lcc", lonmin=-50, lonmax=50, latmin=20, latmax=85) - if proj in ("OSGB", "EuroPP"): - cfp.mapset(proj=proj, resolution="50m") - else: - cfp.mapset(proj=proj) + cfp.mapset(proj=proj) + + # For the final plot only, add a colour bar to cover all the sub-plots if i == len(projtypes) - 1: cfp.con( f.subspace(pressure=850), @@ -66,8 +51,6 @@ colorbar_position=[0.1, 0.1, 0.8, 0.02], colorbar_orientation="horizontal", ) - # to see the marking lines need to be True, but this can be - # hard to read so if not needed use False else: cfp.con( f.subspace(pressure=850), From d79fea35d63a5c2fdbbd47c1ce36f148e1ad3d1a Mon Sep 17 00:00:00 2001 From: "Sadie L. Bartholomew" Date: Thu, 3 Oct 2024 22:44:02 +0100 Subject: [PATCH 07/10] Standardise data path & simplify title for NH original recipe 16 --- docs/source/recipes/plot_16_recipe.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/docs/source/recipes/plot_16_recipe.py b/docs/source/recipes/plot_16_recipe.py index 4e36cfb349..c0d0d76a22 100644 --- a/docs/source/recipes/plot_16_recipe.py +++ b/docs/source/recipes/plot_16_recipe.py @@ -2,9 +2,8 @@ Plotting contour subplots with different projections ==================================================== -In this recipe, we will plot different projections for the same -data to illustrate visually the ones available in order to more -visually decide which is suitable. +In this recipe, we will plot the same data using different projections +as subplots to illustrate visually some available possibilities. """ @@ -17,8 +16,7 @@ # %% # 2. Read the field in: -PATH = "~/git-repos/cf-plot/cfplot/test/cfplot_data" -f = cf.read(f"{PATH}/ggap.nc")[0] +f = cf.read("~/recipes/ggap.nc")[0] # %% # 3. Create the file with subplots. If changing the number of subplots, From 8689f90e9c8c7c6f026312c072a4db8ce1c85e8d Mon Sep 17 00:00:00 2001 From: "Sadie L. Bartholomew" Date: Tue, 8 Oct 2024 12:07:45 +0100 Subject: [PATCH 08/10] Update recipe_list document to list Natalia recipe 16 --- docs/source/recipes/recipe_list.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/source/recipes/recipe_list.txt b/docs/source/recipes/recipe_list.txt index f39297f9cb..8ef6999007 100644 --- a/docs/source/recipes/recipe_list.txt +++ b/docs/source/recipes/recipe_list.txt @@ -27,4 +27,6 @@ plot_13_recipe.html#sphx-glr-recipes-plot-13-recipe-py plot_14_recipe.html#sphx-glr-recipes-plot-14-recipe-py
plot_15_recipe.html#sphx-glr-recipes-plot-15-recipe-py -
\ No newline at end of file +
+plot_16_recipe.html#sphx-glr-recipes-plot-16-recipe-py +
From c4297de7c36eae49b99d5eae4c1685988bf558f5 Mon Sep 17 00:00:00 2001 From: "Sadie L. Bartholomew" Date: Tue, 8 Oct 2024 12:09:47 +0100 Subject: [PATCH 09/10] Add new topic item 'subplot' to recipe filter keyword list --- docs/source/recipes/recipe_list.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/recipes/recipe_list.txt b/docs/source/recipes/recipe_list.txt index 8ef6999007..ea7e3e093f 100644 --- a/docs/source/recipes/recipe_list.txt +++ b/docs/source/recipes/recipe_list.txt @@ -13,7 +13,7 @@ plot_06_recipe.html#sphx-glr-recipes-plot-06-recipe-py plot_07_recipe.html#sphx-glr-recipes-plot-07-recipe-py
plot_08_recipe.html#sphx-glr-recipes-plot-08-recipe-py -
+
plot_09_recipe.html#sphx-glr-recipes-plot-09-recipe-py
plot_10_recipe.html#sphx-glr-recipes-plot-10-recipe-py @@ -27,6 +27,6 @@ plot_13_recipe.html#sphx-glr-recipes-plot-13-recipe-py plot_14_recipe.html#sphx-glr-recipes-plot-14-recipe-py
plot_15_recipe.html#sphx-glr-recipes-plot-15-recipe-py -
+
plot_16_recipe.html#sphx-glr-recipes-plot-16-recipe-py -
+
From a9efead0820ac4bc1e5e57e377246978884ac685 Mon Sep 17 00:00:00 2001 From: "Sadie L. Bartholomew" Date: Thu, 10 Oct 2024 15:36:28 +0100 Subject: [PATCH 10/10] Fix classes attached to recipe 16 filter keywords --- docs/source/recipes/recipe_list.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/recipes/recipe_list.txt b/docs/source/recipes/recipe_list.txt index ea7e3e093f..51d038845d 100644 --- a/docs/source/recipes/recipe_list.txt +++ b/docs/source/recipes/recipe_list.txt @@ -29,4 +29,4 @@ plot_14_recipe.html#sphx-glr-recipes-plot-14-recipe-py plot_15_recipe.html#sphx-glr-recipes-plot-15-recipe-py
plot_16_recipe.html#sphx-glr-recipes-plot-16-recipe-py -
+