Skip to content
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

Taylor Diagram #846

Merged
merged 8 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
clean up, extract ref dataset info from json to include it in taylor …
…diagram
  • Loading branch information
lee1043 committed May 3, 2022
commit 204122db18c83d952f44275c442b2e31866d18eb
107 changes: 87 additions & 20 deletions pcmdi_metrics/graphics/demo/mean_clim_plots_test_model.ipynb

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pcmdi_metrics/graphics/share/Metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, files):
self.df_dict,
self.var_list,
self.var_unit_list,
self.var_ref_dict,
self.regions,
self.stats,
) = read_mean_clim_json_files(files)
Expand Down Expand Up @@ -145,6 +146,7 @@ def merge(self, metrics_obj):

result.var_list = list(set(self.var_list + metrics_obj.var_list))
result.var_unit_list = list(set(self.var_unit_list + metrics_obj.var_unit_list))
result.var_ref_dict = {**self.var_ref_dict, **metrics_obj.var_ref_dict}
result.regions = list(set(self.regions + metrics_obj.regions))
result.stats = list(set(self.stats + metrics_obj.stats))

Expand Down
16 changes: 13 additions & 3 deletions pcmdi_metrics/graphics/share/read_json_mean_clim.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,37 @@ def read_mean_clim_json_files(
(Rows: models, Columns: variables (i.e., 2d array)
- `var_list`: list of string, all variables from JSON files
- `var_unit_list`: list of string, all variables and its units from JSON files
- `var_ref_list`: list of string, list for default reference dataset for each variable
- `regions`: list of string, regions
- `stats`: list of string, statistics
"""
# Read JSON and get unit for each variable
results_dict = {} # merged dict by reading all JSON files
var_list = []
var_unit_list = []
var_ref_dict = {}
regions_all = []

for json_file in json_list:
if debug:
print("json_file:", json_file)
with open(json_file) as fj:
dict_temp = json.load(fj)
var = dict_temp["Variable"]["id"]
dict_temp = json.load(fj) # e.g., load contents of precipitation json file
var = dict_temp["Variable"]["id"] # e.g., 'pr'
if "level" in list(dict_temp["Variable"].keys()):
var += "-" + str(int(dict_temp["Variable"]["level"] / 100.0)) # Pa to hPa
results_dict[var] = dict_temp
unit = extract_unit(var, results_dict[var])
var_unit = var + " [" + unit + "]"
var_list.append(var)
var_unit_list.append(var_unit)
var_ref_dict[var] = extract_ref(var, results_dict[var])
regions_all.extend(extract_region(var, results_dict[var]))
if stats is None:
stats = extract_stat(var, results_dict[var])
if debug:
print("var_unit_list:", var_unit_list)
print("var_ref_dict:", var_ref_dict)

if regions is None:
regions = list(set(regions_all)) # Remove duplicates
Expand Down Expand Up @@ -81,7 +85,7 @@ def read_mean_clim_json_files(
results_dict, var_list, region, stat, season, mip, debug
)

return df_dict, var_list, var_unit_list, regions, stats
return df_dict, var_list, var_unit_list, var_ref_dict, regions, stats


def extract_unit(var, results_dict_var):
Expand All @@ -90,6 +94,12 @@ def extract_unit(var, results_dict_var):
return units


def extract_ref(var, results_dict_var):
model_list = sorted(list(results_dict_var["RESULTS"].keys()))
ref = results_dict_var["RESULTS"][model_list[0]]["default"]["source"]
return ref


def extract_region(var, results_dict_var):
region_list, stat_list = extract_region_stat(var, results_dict_var)
return region_list
Expand Down
19 changes: 13 additions & 6 deletions pcmdi_metrics/graphics/taylor_diagram/taylor_diagram.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
def TaylorDiagram(
stddev, corrcoef, refstd, fig, colors,
normalize=True,
labels=None, markers=None, markersizes=None, zorders=None):
labels=None, markers=None, markersizes=None, zorders=None,
ref_label=None, smax=None):

"""Plot a Taylor diagram.

This code was adpated and revised from the ILAMB code by Nathan Collier found here:
This code was adpated from the ILAMB code by Nathan Collier found here:
https://github.com/rubisco-sfa/ILAMB/blob/master/src/ILAMB/Post.py#L80,
which was originally adapted from the code by Yannick Copin found here:
which was revised by Jiwoo Lee to enable more customization.

The original code was written by Yannick Copin that can be found here:
https://gist.github.com/ycopin/3342888

Parameters
Expand All @@ -32,6 +34,10 @@ def TaylorDiagram(
list of integer for marker size
zorders : list, optional
list of integer for zorder
ref_label : str, optional
label for reference data
smax : int or float, optional
maximum of axis range for (normalized) standard deviation

Return
------
Expand Down Expand Up @@ -59,7 +65,8 @@ def TaylorDiagram(
stddev = stddev / refstd
refstd = 1.
smin = 0
smax = max(2.0, 1.1 * stddev.max())
if smax is None:
smax = max(2.0, 1.1 * stddev.max())

# add the curvilinear grid
ghelper = FA.GridHelperCurveLinear(tr,
Expand Down Expand Up @@ -114,7 +121,7 @@ def TaylorDiagram(
ax.plot(np.arccos(corrcoef[i]), stddev[i], marker, color=colors[i], mew=0, ms=ms, label=label, zorder=zorder)

# Add reference point and stddev contour
l, = ax.plot([0], refstd, 'k*', ms=12, mew=0)
l, = ax.plot([0], refstd, 'k*', ms=12, mew=0, label=ref_label)
t = np.linspace(0, np.pi / 2)
r = np.zeros_like(t) + refstd
ax.plot(t, r, 'k--')
Expand Down