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

mean clim patch for on-the-fly figure generations #1166

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion pcmdi_metrics/mean_climate/lib/load_and_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -125,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 = ""
Expand Down
127 changes: 109 additions & 18 deletions pcmdi_metrics/mean_climate/lib/plot_clim_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -260,12 +260,12 @@ def plot_climatology_diff(
# Add additional detailed information if plotting all seasons
plt.gcf().text(
0.5,
0.905,
var_info_str,
0.91,
_wrap_text(var_info_str, max_length=30),
fontsize=9,
color="grey",
horizontalalignment="center",
verticalalignment="bottom",
verticalalignment="center",
)

# Save the plot
Expand Down Expand Up @@ -542,22 +542,30 @@ 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
conversion_adjust = 0

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"
else:
conversion_factor = 1
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"
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
Expand Down Expand Up @@ -739,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],
Expand All @@ -755,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),
Expand All @@ -772,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),
Expand All @@ -786,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),
Expand All @@ -794,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",
}
},
Expand Down
1 change: 1 addition & 0 deletions pcmdi_metrics/mean_climate/mean_climate_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading