From 36de35505c1ebf94a14ae38b16123abb2d44933a Mon Sep 17 00:00:00 2001 From: lee1043 Date: Tue, 29 Oct 2024 12:09:20 -0700 Subject: [PATCH 1/4] bug fix for units conversion --- .../mean_climate/lib/load_and_regrid.py | 1 + pcmdi_metrics/mean_climate/lib/plot_clim_maps.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pcmdi_metrics/mean_climate/lib/load_and_regrid.py b/pcmdi_metrics/mean_climate/lib/load_and_regrid.py index c3107df06..0a72185d7 100644 --- a/pcmdi_metrics/mean_climate/lib/load_and_regrid.py +++ b/pcmdi_metrics/mean_climate/lib/load_and_regrid.py @@ -51,6 +51,7 @@ def load_and_regrid( ) else: ds[varname_in_file] = ds[varname_in_file] * 86400 # Assumed as kg m-2 s-1 + ds.attrs["units"] = "mm/day" if calendar_qc: # calendar quality check diff --git a/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py b/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py index 8d0f0c6dd..98c099268 100644 --- a/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py +++ b/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py @@ -542,14 +542,18 @@ def _validate_season_input(season_to_plot, available_seasons): def _apply_variable_units_conversion(ds, data_var): """Apply unit conversion based on the variable type.""" + units = ds[data_var].attrs.get("units", "") + if data_var == "pr": - conversion_factor = 86400 # Convert kg/m²/s to mm/day - ds[data_var].attrs["units"] = "mm/day" - ds[data_var].attrs["long_name"] = "Precipitation" + if units not in ["mm/day", "mm d-1"]: + conversion_factor = 86400 # Convert kg/m²/s to mm/day + ds[data_var].attrs["units"] = "mm/day" + ds[data_var].attrs["long_name"] = "Precipitation" elif data_var == "psl" and ds[data_var].max() > 100000: - conversion_factor = 0.01 # Convert Pa to hPa - ds[data_var].attrs["units"] = "hPa" - ds[data_var].attrs["long_name"] = "Sea Level Pressure" + if units not in ["hPa"]: + conversion_factor = 0.01 # Convert Pa to hPa + ds[data_var].attrs["units"] = "hPa" + ds[data_var].attrs["long_name"] = "Sea Level Pressure" else: conversion_factor = 1 From 2c9e8fe9e213ba82356378625424152ecda164b0 Mon Sep 17 00:00:00 2001 From: lee1043 Date: Tue, 29 Oct 2024 12:23:31 -0700 Subject: [PATCH 2/4] bug fix --- pcmdi_metrics/mean_climate/lib/load_and_regrid.py | 2 +- pcmdi_metrics/mean_climate/lib/plot_clim_maps.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pcmdi_metrics/mean_climate/lib/load_and_regrid.py b/pcmdi_metrics/mean_climate/lib/load_and_regrid.py index 0a72185d7..bfb346d25 100644 --- a/pcmdi_metrics/mean_climate/lib/load_and_regrid.py +++ b/pcmdi_metrics/mean_climate/lib/load_and_regrid.py @@ -126,7 +126,7 @@ def load_and_regrid( # preserve units in regridded dataset try: - units = ds[varname].units + units = ds.attrs["units"] or ds[varname].units except Exception as e: print(e) units = "" diff --git a/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py b/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py index 98c099268..51d763e1c 100644 --- a/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py +++ b/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py @@ -543,6 +543,7 @@ def _validate_season_input(season_to_plot, available_seasons): def _apply_variable_units_conversion(ds, data_var): """Apply unit conversion based on the variable type.""" units = ds[data_var].attrs.get("units", "") + conversion_factor = 1 if data_var == "pr": if units not in ["mm/day", "mm d-1"]: @@ -554,8 +555,6 @@ def _apply_variable_units_conversion(ds, data_var): conversion_factor = 0.01 # Convert Pa to hPa ds[data_var].attrs["units"] = "hPa" ds[data_var].attrs["long_name"] = "Sea Level Pressure" - else: - conversion_factor = 1 # Store original attributes original_attrs = ds[data_var].attrs From bf6d88403433b3ae0419e203ce83783095af5a04 Mon Sep 17 00:00:00 2001 From: lee1043 Date: Tue, 29 Oct 2024 13:16:19 -0700 Subject: [PATCH 3/4] bug fix --- pcmdi_metrics/mean_climate/lib/plot_clim_maps.py | 4 ++-- pcmdi_metrics/mean_climate/mean_climate_driver.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py b/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py index 51d763e1c..78339c5f1 100644 --- a/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py +++ b/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py @@ -260,12 +260,12 @@ def plot_climatology_diff( # Add additional detailed information if plotting all seasons plt.gcf().text( 0.5, - 0.905, + 0.91, var_info_str, fontsize=9, color="grey", horizontalalignment="center", - verticalalignment="bottom", + verticalalignment="center", ) # Save the plot diff --git a/pcmdi_metrics/mean_climate/mean_climate_driver.py b/pcmdi_metrics/mean_climate/mean_climate_driver.py index 381e61523..250b2982a 100755 --- a/pcmdi_metrics/mean_climate/mean_climate_driver.py +++ b/pcmdi_metrics/mean_climate/mean_climate_driver.py @@ -431,6 +431,7 @@ output_dir=test_clims_plot_dir, output_filename=output_filename, dataname_test=f"{model}_{run}", + dataname_ref=ref_dataset_name, fig_title=f"Climatology ({season}, {region}): {varname}", ) print("plot map done") From 6198f4c7a1691cdba9a5afbd07b837fde9535570 Mon Sep 17 00:00:00 2001 From: lee1043 Date: Tue, 29 Oct 2024 14:09:12 -0700 Subject: [PATCH 4/4] adjust color levels for variables --- .../mean_climate/lib/plot_clim_maps.py | 104 ++++++++++++++++-- 1 file changed, 96 insertions(+), 8 deletions(-) diff --git a/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py b/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py index 78339c5f1..cb0099112 100644 --- a/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py +++ b/pcmdi_metrics/mean_climate/lib/plot_clim_maps.py @@ -74,7 +74,7 @@ def plot_climatology_diff( var_info_str = "" separator = ", " if long_name: - var_info_str += f"Variable: {_wrap_text(long_name)}" + var_info_str += f"Variable: {long_name}" if units: var_info_str += f"{separator}Units: {units}" if period is not None: @@ -261,7 +261,7 @@ def plot_climatology_diff( plt.gcf().text( 0.5, 0.91, - var_info_str, + _wrap_text(var_info_str, max_length=30), fontsize=9, color="grey", horizontalalignment="center", @@ -544,6 +544,7 @@ def _apply_variable_units_conversion(ds, data_var): """Apply unit conversion based on the variable type.""" units = ds[data_var].attrs.get("units", "") conversion_factor = 1 + conversion_adjust = 0 if data_var == "pr": if units not in ["mm/day", "mm d-1"]: @@ -555,12 +556,16 @@ def _apply_variable_units_conversion(ds, data_var): conversion_factor = 0.01 # Convert Pa to hPa ds[data_var].attrs["units"] = "hPa" ds[data_var].attrs["long_name"] = "Sea Level Pressure" + elif data_var in ["tas", "ts"] and ds[data_var].max() > 250: + if units not in ["deg C", "C"]: + conversion_adjust = -273.15 + ds[data_var].attrs["units"] = "deg C" # Store original attributes original_attrs = ds[data_var].attrs # Perform the operation - ds[data_var] = ds[data_var] * conversion_factor + ds[data_var] = ds[data_var] * conversion_factor + conversion_adjust # Re-assign the original attributes ds[data_var].attrs = original_attrs @@ -742,14 +747,30 @@ def _load_variable_setting(ds: xr.Dataset, data_var: str, level: int, diff=False "colormap_diff": "BrBG", } }, + "rlds": { + None: { + "levels": np.linspace(80, 500, 21), + "levels_diff": np.linspace(-50, 50, 21), + "colormap": cc.cm.rainbow, + "colormap_diff": "RdBu_r", + } + }, "rltcre": { None: { - "levels": np.linspace(0, 50, 21), + "levels": np.arange(0, 70, 5), "levels_diff": np.linspace(-30, 30, 13), "colormap": cc.cm.rainbow, "colormap_diff": "RdBu_r", } }, + "rlus": { + None: { + "levels": np.linspace(100, 500, 21), + "levels_diff": np.linspace(-40, 40, 21), + "colormap": cc.cm.rainbow, + "colormap_diff": "RdBu_r", + } + }, "rlut": { None: { "levels": [100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320], @@ -758,6 +779,56 @@ def _load_variable_setting(ds: xr.Dataset, data_var: str, level: int, diff=False "colormap_diff": "RdBu_r", } }, + "rlutcs": { + None: { + "levels": [100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320], + "levels_diff": np.linspace(-40, 40, 21), + "colormap": cc.cm.rainbow, + "colormap_diff": "RdBu_r", + } + }, + "rsds": { + None: { + "levels": np.linspace(50, 300, 26), + "levels_diff": np.linspace(-50, 50, 21), + "colormap": cc.cm.rainbow, + "colormap_diff": "RdBu_r", + } + }, + "rsdscs": { + None: { + "levels": np.linspace(0, 400, 21), + "levels_diff": np.linspace(-40, 40, 21), + "colormap": cc.cm.rainbow, + "colormap_diff": "RdBu_r", + } + }, + "rsdt": { + None: { + "levels": np.linspace(0, 450, 26), + "levels_diff": np.linspace(-1, 1, 21), + "colormap": cc.cm.rainbow, + "colormap_diff": "RdBu_r", + } + }, + "rsus": { + None: { + "levels": np.linspace(0, 300, 16), + "levels_diff": np.linspace(-60, 60, 13), + "colormap": cc.cm.rainbow, + "colormap_diff": "RdBu_r", + "colormap_ext": "max", + } + }, + "rsuscs": { + None: { + "levels": np.linspace(0, 300, 16), + "levels_diff": np.linspace(-60, 60, 13), + "colormap": cc.cm.rainbow, + "colormap_diff": "RdBu_r", + "colormap_ext": "max", + } + }, "rstscre": { None: { "levels": np.linspace(-50, 50, 21), @@ -775,6 +846,15 @@ def _load_variable_setting(ds: xr.Dataset, data_var: str, level: int, diff=False "colormap_ext": "max", } }, + "sfcWind": { + None: { + "levels": np.linspace(0, 10, 21), + "levels_diff": np.linspace(-6, 6, 13), + "colormap": cc.cm.rainbow, + "colormap_diff": "RdBu_r", + "colormap_ext": "max", + } + }, "ta": { 200: { "levels": np.arange(-70, -40, 2), @@ -789,6 +869,14 @@ def _load_variable_setting(ds: xr.Dataset, data_var: str, level: int, diff=False "colormap_diff": "RdBu_r", }, }, + "tas": { + None: { + "levels": np.arange(-40, 45, 5), + "levels_diff": [-15, -10, -5, -2, -1, -0.5, 0, 0.5, 1, 2, 5, 10, 15], + "colormap": cc.cm.rainbow, + "colormap_diff": "RdBu_r", + } + }, "tauu": { None: { "levels": np.linspace(-0.1, 0.1, 11), @@ -797,11 +885,11 @@ def _load_variable_setting(ds: xr.Dataset, data_var: str, level: int, diff=False "colormap_diff": "RdBu_r", } }, - "tas": { + "tauv": { None: { - "levels": np.arange(-40, 45, 5), - "levels_diff": [-15, -10, -5, -2, -1, -0.5, 0, 0.5, 1, 2, 5, 10, 15], - "colormap": cc.cm.rainbow, + "levels": np.linspace(-0.1, 0.1, 11), + "levels_diff": np.linspace(-0.1, 0.1, 11), + "colormap": "PiYG_r", "colormap_diff": "RdBu_r", } },