Skip to content

Commit

Permalink
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
Browse files Browse the repository at this point in the history
  • Loading branch information
lee1043 authored Jan 13, 2025
2 parents 366988d + c5a0027 commit 7838e00
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 30 deletions.
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ keywords:
- climatology
- climate variability
license: BSD-3-Clause
version: '3.8.1'
date-released: '2025-01-06'
version: '3.8.2'
date-released: '2025-01-09'
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Release Notes and History

| <div style="width:300%">[Versions]</div> | Update summary |
| ------------- | ------------------------------------- |
| [v3.8.2] | Technical update
| [v3.8.1] | Technical update with new figure (modes of variability multi-panel plot)
| [v3.8] | New capability (**figure generation for ENSO**, xCDAT migration completed for **Monsoon Wang** with figure generation), major dependency update (`numpy` >= 2.0)
| [v3.7.2] | Technical update
Expand Down Expand Up @@ -173,6 +174,7 @@ Release Notes and History
</details>

[Versions]: https://github.com/PCMDI/pcmdi_metrics/releases
[v3.8.2]: https://github.com/PCMDI/pcmdi_metrics/releases/tag/v3.8.2
[v3.8.1]: https://github.com/PCMDI/pcmdi_metrics/releases/tag/v3.8.1
[v3.8]: https://github.com/PCMDI/pcmdi_metrics/releases/tag/v3.8
[v3.7.2]: https://github.com/PCMDI/pcmdi_metrics/releases/tag/v3.7.2
Expand Down
2 changes: 1 addition & 1 deletion conda-env/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies:
- cdp=1.7.0
- eofs=2.0.0
- seaborn=0.12.2
- enso_metrics=1.1.4
- enso_metrics=1.1.5
- xcdat=0.7.3
- xmltodict=0.13.0
- setuptools=67.7.2
Expand Down
2 changes: 1 addition & 1 deletion conda-env/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies:
- cdp=1.7.0
- eofs=2.0.0
- seaborn=0.12.2
- enso_metrics=1.1.4
- enso_metrics=1.1.5
- xcdat=0.7.3
- xmltodict=0.13.0
- setuptools=67.7.2
Expand Down
2 changes: 1 addition & 1 deletion pcmdi_metrics/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.8.1"
__version__ = "3.8.2"
6 changes: 3 additions & 3 deletions pcmdi_metrics/io/xcdat_openxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def fix_noncompliant_attr(ds: xr.Dataset) -> xr.Dataset:


def _xcdat_openxml(
xmlfile: str, data_var: str = None, decode_times: bool = True
xmlfile: str, data_var: str = None, decode_times: bool = True, chunks=None
) -> xr.Dataset:
"""Open input file (xml generated by cdscan)
Expand Down Expand Up @@ -142,11 +142,11 @@ def _xcdat_openxml(

if len(ncfile_list) > 1:
ds = xc.open_mfdataset(
ncfile_list, data_var=data_var, decode_times=decode_times
ncfile_list, data_var=data_var, decode_times=decode_times, chunks=chunks
)
else:
ds = xc.open_dataset(
ncfile_list[0], data_var=data_var, decode_times=decode_times
ncfile_list[0], data_var=data_var, decode_times=decode_times, chunks=chunks
)

return ds
57 changes: 46 additions & 11 deletions pcmdi_metrics/mean_climate/lib/calculate_climatology.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
from .plot_clim_maps import plot_climatology


# import dask


def calculate_climatology(
var: str,
infile: str,
Expand Down Expand Up @@ -279,20 +276,58 @@ def calculate_climatology(
# Add annual or seasonal climatology to the dictionary
ds_clim_dict[s] = ds_clim_s

if outfilename is not None:
out = os.path.join(outdir, outfilename)

out_season = out.replace(".nc", addf)

print("output file is", out_season)
d_clim_dict[s].to_netcdf(
out_season
) # global attributes are automatically saved as well

print("output file:", out_season)

# Plot climatology
if plot and s == "AC":
plot_climatology(
d_ac,
var,
season_to_plot="all",
output_filename=out_season.replace(".nc", ".png"),
)
# Check if variable is 4D
if is_4d_variable(d_ac, var):
# Plot 3 levels (hPa) for 4D variables for quick check
levels_to_plot = [200, 500, 850]
else:
levels_to_plot = [None]

# Plot climatology for each level
for level in levels_to_plot:
output_fig_path = out_season.replace(".nc", ".png")
if level is not None:
if var in output_fig_path:
output_fig_path = os.path.join(
outdir,
output_fig_path.split("/")[-1].replace(
var, f"{var}-{level}"
),
)
else:
output_fig_path = output_fig_path.replace(
".png", f"-{level}.png"
)

# plot climatology for each level
plot_climatology(
d_ac,
var,
level=level,
season_to_plot="all",
output_filename=output_fig_path,
period=f"{start_yr_str}-{end_yr_str}",
)

print("output figure:", output_fig_path)

ds.close()
return ds_clim_dict # Return the dictionary of all climatology datasets


def is_4d_variable(ds, data_var):
da = ds[data_var]
print("data_var, da.shape:", data_var, da.shape)
return len(da.shape) == 4
49 changes: 38 additions & 11 deletions pcmdi_metrics/mean_climate/lib/plot_clim_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,12 @@ def _load_variable_setting(
"colormap": cc.cm.rainbow,
"colormap_diff": "jet",
},
500: {
"levels": np.linspace(-45, 5, 21),
"levels_diff": None,
"colormap": cc.cm.rainbow,
"colormap_diff": "RdBu_r",
},
850: {
"levels": np.arange(-35, 40, 5),
"levels_diff": [-15, -10, -5, -2, -1, -0.5, 0, 0.5, 1, 2, 5, 10, 15],
Expand Down Expand Up @@ -936,6 +942,12 @@ def _load_variable_setting(
"colormap": "PiYG_r",
"colormap_diff": "RdBu_r",
},
500: {
"levels": np.arange(-40, 45, 5),
"levels_diff": np.linspace(-20, 20, 21),
"colormap": "PiYG_r",
"colormap_diff": "RdBu_r",
},
850: {
"levels": [
-25,
Expand Down Expand Up @@ -967,6 +979,12 @@ def _load_variable_setting(
"colormap": "PiYG_r",
"colormap_diff": "RdBu_r",
},
500: {
"levels": np.linspace(-10, 10, 11),
"levels_diff": np.linspace(-5, 5, 6),
"colormap": "PiYG_r",
"colormap_diff": "RdBu_r",
},
850: {
"levels": np.linspace(-10, 10, 11),
"levels_diff": np.linspace(-5, 5, 6),
Expand All @@ -984,30 +1002,39 @@ def _load_variable_setting(
},
}

# Check if the variable and level exist in the settings

in_dict = False
# Initialize
levels = None
levels_diff = None
cmap = None
cmap_diff = None
cmap_ext = None
cmap_ext_diff = None

# Check if the variable and level exist in the settings
if data_var in var_setting_dict:
if level in var_setting_dict[data_var]:
settings = var_setting_dict[data_var][level]
levels = settings["levels"]
levels_diff = settings["levels_diff"]
cmap = _get_colormap(settings["colormap"])
cmap_diff = _get_colormap(settings["colormap_diff"])
levels = settings.get("levels", None)
levels_diff = settings.get("levels_diff", None)
cmap = _get_colormap(settings.get("colormap", None))
cmap_diff = _get_colormap(settings.get("colormap_diff", None))
cmap_ext = settings.get("colormap_ext", "both")
cmap_ext_diff = "both"
in_dict = True

# Use default settings if not found
if not in_dict:
vmin = float(ds[data_var].min())
vmax = float(ds[data_var].max())
vmin = float(ds[data_var].min())
vmax = float(ds[data_var].max())
if levels is None:
levels = np.linspace(vmin, vmax, 21)
if levels_diff is None:
levels_diff = np.linspace(vmin / 2.0, vmax / 2.0, 21)
if cmap is None:
cmap = plt.get_cmap("jet")
if cmap_diff is None:
cmap_diff = plt.get_cmap("RdBu_r")
if cmap_ext is None:
cmap_ext = "both"
if cmap_ext_diff is None:
cmap_ext_diff = "both"

if diff:
Expand Down

0 comments on commit 7838e00

Please sign in to comment.