Skip to content

Commit

Permalink
update data_xr for supporting annual cycle climatology files
Browse files Browse the repository at this point in the history
  • Loading branch information
chengzhuzhang committed Mar 19, 2024
1 parent fc362f5 commit b327d93
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions e3sm_diags/driver/utils/climo_xr.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"MAM",
"JJA",
"SON",
"ANNUALCYCLE",
]
CLIMO_FREQS = get_args(CLIMO_FREQ)

Expand Down
29 changes: 26 additions & 3 deletions e3sm_diags/driver/utils/dataset_xr.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,10 @@ def _get_climo_dataset(self, season: str) -> xr.Dataset:
using other datasets.
"""
filepath = self._get_climo_filepath(season)
ds = self._open_climo_dataset(filepath)
if season == "ANNUALCYCLE":
ds = self._open_annual_cycle_climo_dataset(filepath)
else:
ds = self._open_climo_dataset(filepath)

if self.var in self.derived_vars_map:
ds = self._get_dataset_with_derived_climo_var(ds)
Expand All @@ -406,6 +409,18 @@ def _get_climo_dataset(self, season: str) -> xr.Dataset:

return ds

def _open_annual_cycle_climo_dataset(self, filepath: str) -> xr.Dataset:
"""Open 12 monthly mean climatology dataset.
Parameters
----------
filepath : str
The path to the climatology datasets.
"""
args = {"paths": filepath, "decode_times": False, "add_bounds": ["X", "Y"]}
ds = xc.open_mfdataset(**args)
return ds

def _open_climo_dataset(self, filepath: str) -> xr.Dataset:
"""Open a climatology dataset.
Expand Down Expand Up @@ -506,8 +521,16 @@ def _get_climo_filepath(self, season: str) -> str:
filename = self.parameter.ref_name
elif self.data_type == "test":
filename = self.parameter.test_name

filepath = self._find_climo_filepath(filename, season)
if season == "ANNUALCYCLE":
filepath = self._find_climo_filepath(filename, "01")
# find the path for 12 monthly mean files
if filepath:
filename_01 = filepath.split("/")[-1]
filepath = filepath.replace(
f"{filename_01}", f"{filename}_[0-1][0-9]_*_*climo.nc"
)
else:
filepath = self._find_climo_filepath(filename, season)

# If absolutely no filename was found, then raise an error.
if filepath is None:
Expand Down

0 comments on commit b327d93

Please sign in to comment.