Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
149 commits
Select commit Hold shift + click to select a range
eb12696
clean up, simplify, modernize the code
lee1043 Aug 12, 2024
ad9fa8e
bug fix
lee1043 Aug 12, 2024
db1270b
include period in the output file name as default
lee1043 Aug 12, 2024
fdd44b4
initial commit
lee1043 Sep 11, 2024
37658b0
clean up
lee1043 Sep 11, 2024
8f86abd
clean up
lee1043 Sep 11, 2024
ffa4f7d
clean up
lee1043 Sep 11, 2024
709b7e2
clean up
lee1043 Sep 11, 2024
42e3de8
clean up
lee1043 Sep 11, 2024
8d5af54
clean up
lee1043 Sep 11, 2024
2c0a5fe
clean up
lee1043 Sep 11, 2024
44dc275
clean up
lee1043 Sep 11, 2024
461d34f
clean up, bug fix
lee1043 Sep 11, 2024
ebc04d0
clean up
lee1043 Sep 12, 2024
ee16b19
clean up
lee1043 Sep 12, 2024
dd40546
clean up
lee1043 Sep 12, 2024
66bc090
clean up
lee1043 Sep 12, 2024
2754fd5
clean up
lee1043 Sep 12, 2024
7a85967
in progress
lee1043 Sep 15, 2024
3d76aa3
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Sep 15, 2024
d28c5d2
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Sep 17, 2024
9ebd5b9
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Sep 23, 2024
be1d12b
add dates comparing function
lee1043 Sep 23, 2024
0b5f6a3
compare given dates to actual dataset to find overlapping period
lee1043 Sep 23, 2024
22643f9
in progress. commit made for safe backup
lee1043 Sep 23, 2024
499ebd0
add new function: get_calendar
lee1043 Sep 23, 2024
cf39a4b
add new function: regenerate_time_axis
lee1043 Sep 23, 2024
1623c18
regenerate time axis of data if needed
lee1043 Sep 23, 2024
d530f95
clean up
lee1043 Sep 23, 2024
63e1ec6
in progress
lee1043 Sep 23, 2024
395f3db
in progress
lee1043 Sep 23, 2024
7eeef6f
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Sep 24, 2024
5f776c1
add new func
lee1043 Sep 24, 2024
a82eb95
Merge branch 'mean_clim_upgrade_lee1043_20240812' of github.com:PCMDI…
lee1043 Sep 24, 2024
0c881c8
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Sep 24, 2024
4fe0088
Merge branch 'mean_clim_upgrade_lee1043_20240812' of github.com:PCMDI…
lee1043 Sep 24, 2024
63eb9d6
in progress ... commit for back up the latest
lee1043 Sep 25, 2024
4c9d915
make extract_level function by reusing original code part from mean c…
lee1043 Sep 26, 2024
d1ca557
in progress. commit made for save progress as back up
lee1043 Sep 26, 2024
13dc5a6
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Sep 26, 2024
378ee88
in progress
lee1043 Sep 26, 2024
3dfcd87
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Sep 27, 2024
8a849f9
Merge branch 'mean_clim_upgrade_lee1043_20240812' of github.com:PCMDI…
lee1043 Sep 27, 2024
5294518
pre-commit fix
lee1043 Sep 27, 2024
3546f0e
consider cases that plev does not have units attrs
lee1043 Sep 27, 2024
862929b
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Sep 27, 2024
c7d003a
more sanity checks to make workflow to be more robust
lee1043 Sep 30, 2024
2445555
clean up
lee1043 Sep 30, 2024
c1e90b3
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Oct 4, 2024
23c253d
clean up, bug fix, in progress
lee1043 Oct 5, 2024
ee27995
bypass regridding if grids of current and target are identical
lee1043 Oct 8, 2024
0587008
bug fix, clean up, in progress
lee1043 Oct 8, 2024
8ea90c4
in progress
lee1043 Oct 8, 2024
cc7d7e0
bug fix
lee1043 Oct 8, 2024
c80a957
in progress
lee1043 Oct 8, 2024
7d76730
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Oct 24, 2024
e189fc0
clean up
lee1043 Nov 12, 2024
e98b0d2
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Nov 18, 2024
4fbfe1a
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Nov 19, 2024
fddcdc5
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Nov 26, 2024
60c2254
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Nov 26, 2024
a8df2a7
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Nov 28, 2024
6753f26
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Dec 12, 2024
7c818bc
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Dec 23, 2024
ee00de6
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Dec 23, 2024
8e72c50
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Dec 24, 2024
ab87486
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Jan 7, 2025
366988d
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Jan 7, 2025
7838e00
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Jan 13, 2025
eca7d89
update
lee1043 Jan 13, 2025
1516235
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
kristinchang3 Feb 5, 2025
efe6629
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Feb 11, 2025
baa3199
update
lee1043 Feb 11, 2025
cd8e519
pre-commit fix
lee1043 Feb 11, 2025
63328c1
update
lee1043 Feb 11, 2025
391e5dd
update
lee1043 Feb 14, 2025
b82e540
update
lee1043 Feb 14, 2025
6b629f2
update
lee1043 Feb 19, 2025
839cca6
add vertical bounds checker
lee1043 Feb 19, 2025
ab4e290
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Feb 19, 2025
48bff55
clean up
lee1043 Feb 20, 2025
4d3bea2
updated from the original script written by @gleckler1
lee1043 Feb 20, 2025
a0d834d
add code history title
lee1043 Feb 20, 2025
202179a
test for conflic resolve
lee1043 Feb 20, 2025
da653f2
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Feb 20, 2025
c3e9aca
clean up
lee1043 Feb 20, 2025
5b66292
clean up
lee1043 Feb 20, 2025
dc83651
clean up
lee1043 Feb 20, 2025
54b3de8
in progress
lee1043 Feb 20, 2025
2746c3d
remove vertical bounds generation
lee1043 Feb 20, 2025
32e515b
update xcdat version
lee1043 Feb 20, 2025
f662cdb
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Feb 26, 2025
e577577
rename netcdf for interpolated files from including gn to gr
lee1043 Feb 28, 2025
5d7b245
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Feb 28, 2025
f33f708
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Apr 8, 2025
22b462d
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Apr 30, 2025
511efc5
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Jul 2, 2025
d5ea9c1
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Jul 22, 2025
88e991d
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Aug 9, 2025
34f4bec
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Aug 22, 2025
62ef98f
bug fix
lee1043 Aug 25, 2025
4346804
test in progress
lee1043 Aug 25, 2025
fd13543
add quick QC for pr, removing negative values, replacing them to zero
lee1043 Aug 25, 2025
705bbf8
update
lee1043 Aug 25, 2025
3caa162
in progress
lee1043 Aug 26, 2025
eba872e
update
lee1043 Aug 28, 2025
252507b
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Aug 29, 2025
d5cb4dd
in progress
lee1043 Sep 2, 2025
fd81990
improve docstring
lee1043 Sep 3, 2025
76b871f
in progress
lee1043 Sep 3, 2025
559bd86
clean up
lee1043 Sep 4, 2025
15be541
in progress
lee1043 Sep 4, 2025
a18f466
update
lee1043 Sep 4, 2025
c81ed0d
clean up
lee1043 Sep 4, 2025
5d6061a
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Sep 9, 2025
5b58760
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Sep 11, 2025
20272f3
bug fix
lee1043 Sep 15, 2025
2e93acf
re-org
lee1043 Sep 17, 2025
d02e048
initial commit
lee1043 Sep 17, 2025
9ffc057
update
lee1043 Sep 17, 2025
a6e1319
in progress
lee1043 Sep 18, 2025
c31bc7b
in progress
lee1043 Sep 18, 2025
e8ffd4b
in progress
lee1043 Sep 22, 2025
4aafc5b
update
lee1043 Sep 22, 2025
4a217af
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Sep 22, 2025
452cd38
in progress
lee1043 Sep 22, 2025
2ccb53d
in progress
lee1043 Sep 22, 2025
657f690
minor bug fix
lee1043 Sep 30, 2025
5cbfa2f
in progress
lee1043 Sep 30, 2025
b55e98c
pre-commit fix
lee1043 Sep 30, 2025
864ccd5
pre-commit fix
lee1043 Oct 1, 2025
c8de473
resolve conflict
lee1043 Oct 7, 2025
f0d80e3
pre-commit fix
lee1043 Oct 7, 2025
23f9196
add capability to open yml file generated by pmp data curator
lee1043 Oct 8, 2025
f8a75bc
enable use of yml file for data curating
lee1043 Oct 8, 2025
c506b6f
update
lee1043 Oct 8, 2025
95b0255
clean up
lee1043 Oct 8, 2025
3ab2a5d
add new driver
lee1043 Oct 9, 2025
2236ffe
in progress
lee1043 Oct 9, 2025
ce60dd1
update
lee1043 Oct 10, 2025
232903e
update
lee1043 Oct 11, 2025
17246df
update
lee1043 Oct 11, 2025
ccc66c0
update
lee1043 Oct 11, 2025
9091494
clean up
lee1043 Oct 11, 2025
2ed63f2
update
lee1043 Oct 11, 2025
464a5d4
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Oct 15, 2025
5053d3f
enable yml file usage
lee1043 Oct 23, 2025
41d580f
accept list input from cmd
lee1043 Oct 29, 2025
527aedc
Merge branch 'main' into mean_clim_upgrade_lee1043_20240812
lee1043 Oct 30, 2025
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
2 changes: 1 addition & 1 deletion conda-env/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies:
- matplotlib >=3.7.1
- eofs >=2.0.0
- seaborn >=0.12.2
#- enso_metrics >=1.1.5
# - enso_metrics >=1.1.5
- xcdat >=0.10.0
- xmltodict >=0.13.0
- setuptools >=67.7.2
Expand Down
2 changes: 2 additions & 0 deletions doc/obs_info/catalogue/build_obs4MIPsClims-pcmdi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python

# Code History
# ------------
# 2025-01-22 Peter J Glckler: Created
# 2025-02-13 Jiwoo Lee: Cleaned up
# 2025-02-18 Jiwoo Lee: default dict to record default reference dataset per variable
Expand Down
2 changes: 1 addition & 1 deletion pcmdi_metrics/io/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# init for pcmdi_metrics.io
from .xcdat_openxml import xcdat_open # noqa # isort:skip
from .xcdat_open import xcdat_open # noqa # isort:skip
from .string_constructor import StringConstructor, fill_template # noqa # isort:skip
from . import base # noqa
from . import cdp_io # noqa
Expand Down
39 changes: 39 additions & 0 deletions pcmdi_metrics/io/xcdat_dataset_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,45 @@ def get_grid(
return d[[lat_key, lon_key, lat_bnds_key, lon_bnds_key]]


def get_grid_shape(
d: Union[xr.Dataset, xr.DataArray],
) -> tuple:
"""
Get the shape of the grid in terms of longitude and latitude dimensions.

Parameters
----------
d : Union[xr.Dataset, xr.DataArray]
Input xarray dataset or data array containing longitude and latitude dimensions.

Returns
-------
tuple
A tuple representing the size of the longitude and latitude dimensions
in the form (longitude_size, latitude_size).

Notes
-----
The function retrieves the keys for longitude and latitude using the
`get_longitude_key` and `get_latitude_key` functions, then uses these keys to
obtain the sizes of the respective dimensions.

Examples
--------
>>> ds = xr.Dataset(...)
>>> get_grid_shape(ds)
(180, 90)

See Also
--------
get_longitude_key, get_latitude_key
"""
lon_key = get_longitude_key(d)
lat_key = get_latitude_key(d)

return tuple(d.sizes[dim] for dim in [lon_key, lat_key])


def get_calendar(d: Union[xr.Dataset, xr.DataArray]) -> str:
"""
Get the calendar type from an xarray Dataset or DataArray.
Expand Down
101 changes: 68 additions & 33 deletions pcmdi_metrics/io/xcdat_openxml.py → pcmdi_metrics/io/xcdat_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import xarray as xr
import xcdat as xc
import xmltodict
import yaml

from pcmdi_metrics.io.xcdat_dataset_io import get_calendar

Expand All @@ -22,7 +23,7 @@ def xcdat_open(
Parameters
----------
infile : Union[str, list]
list of string, or string, for path of file(s) to open using xcdat.
list of string, or string, for path of file(s) to open using xcdat. Can include wildcards if a string.
data_var : str, optional
key of the non-bounds data variable to keep in the Dataset, alongside any existing bounds data variables.
By default None, which loads all data variables.
Expand Down Expand Up @@ -51,43 +52,35 @@ def xcdat_open(
# Open an xml file
>>> ds = xcdat_open('mydata.xml')
"""
if isinstance(infile, list) or "*" in infile:

def _open_with_fallback(open_func, infile, data_var, decode_times, chunks):
"""Helper to try opening with decode_times, fall back if ValueError."""
try:
ds = xc.open_mfdataset(
ds = open_func(
infile, data_var=data_var, decode_times=decode_times, chunks=chunks
)
except (
ValueError
): # Could be due to non-cf-compliant calendar or other attribute
ds = xc.open_mfdataset(
infile, data_var=data_var, decode_times=False, chunks=chunks
)
except ValueError:
ds = open_func(infile, data_var=data_var, decode_times=False, chunks=chunks)
ds = fix_noncompliant_attr(ds)
return ds

if isinstance(infile, list) or "*" in infile:
ds = _open_with_fallback(
xc.open_mfdataset, infile, data_var, decode_times, chunks
)
else:
if infile.split(".")[-1].lower() == "xml":
try:
ds = _xcdat_openxml(
infile, data_var=data_var, decode_times=decode_times, chunks=chunks
)
except (
ValueError
): # Could be due to non-cf-compliant calendar or other attribute
ds = _xcdat_openxml(
infile, data_var=data_var, decode_times=False, chunks=chunks
)
ds = fix_noncompliant_attr(ds)
if infile.endswith(".xml"):
ds = _open_with_fallback(
_xcdat_openxml, infile, data_var, decode_times, chunks
)
elif infile.endswith(".yml") or infile.endswith(".yaml"):
ds = _open_with_fallback(
_xcdat_openyml, infile, data_var, decode_times, chunks
)
else:
try:
ds = xc.open_dataset(
infile, data_var=data_var, decode_times=decode_times, chunks=chunks
)
except (
ValueError
): # Could be due to non-cf-compliant calendar or other attribute
ds = xc.open_dataset(
infile, data_var=data_var, decode_times=False, chunks=chunks
)
ds = fix_noncompliant_attr(ds)
ds = _open_with_fallback(
xc.open_dataset, infile, data_var, decode_times, chunks
)

return ds.bounds.add_missing_bounds()

Expand Down Expand Up @@ -125,7 +118,8 @@ def _xcdat_openxml(
data_var: str, optional
key of the non-bounds data variable to keep in the Dataset, alongside any existing bounds data variables, by default None, which loads all data variables
decode_times : bool, optional
If True, attempt to decode times encoded in the standard NetCDF datetime format into cftime.datetime objects. Otherwise, leave them encoded as numbers. This keyword may not be supported by all the backends, by default True
If True, attempt to decode times encoded in the standard NetCDF datetime format into cftime.datetime objects.
Otherwise, leave them encoded as numbers. This keyword may not be supported by all the backends, by default True

Returns
-------
Expand All @@ -150,3 +144,44 @@ def _xcdat_openxml(
)

return ds


def _xcdat_openyml(
ymlfile: str, data_var: str = None, decode_times: bool = True, chunks=None
) -> xr.Dataset:
"""Open input file (yaml generated by PMP data curator)

Parameters
----------
infile: str
path of yaml file to open using xcdat
data_var: str, optional
key of the non-bounds data variable to keep in the Dataset, alongside any existing bounds data variables, by default None, which loads all data variables
decode_times : bool, optional
If True, attempt to decode times encoded in the standard NetCDF datetime format into cftime.datetime objects.
Otherwise, leave them encoded as numbers. This keyword may not be supported by all the backends, by default True

Returns
-------
xr.Dataset
xarray dataset opened via xcdat
"""
if not os.path.exists(ymlfile):
sys.exit(f"ERROR: File not exist: {ymlfile}")

# read yml file to python dictionary
with open(ymlfile, "r") as f:
yml_dict = yaml.safe_load(f)
directory_path = yml_dict["directory_path"]
netcdf_files = yml_dict["netcdf_files"]
files_to_open = [os.path.join(directory_path, f) for f in netcdf_files]

# check if files exist
for f in files_to_open:
if not os.path.isfile(f):
raise FileNotFoundError(f"File not found: {f}")

ds = xc.open_mfdataset(
files_to_open, data_var=data_var, decode_times=decode_times, chunks=chunks
)
return ds
4 changes: 2 additions & 2 deletions pcmdi_metrics/mean_climate/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .calculate_climatology import calculate_climatology # noqa
from .calculate_climatology import calculate_climatology, is_4d_variable # noqa
from .compute_metrics import compute_metrics # noqa
from .compute_statistics import ( # noqa
annual_mean,
Expand All @@ -19,7 +19,7 @@
from .plot_clim_maps import plot_climatology, plot_climatology_diff

from .create_mean_climate_parser import create_mean_climate_parser # noqa
from .load_and_regrid import load_and_regrid, extract_level # noqa
from .load_and_regrid import load_and_regrid, extract_level, extract_levels # noqa
from .mean_climate_metrics_to_json import mean_climate_metrics_to_json # noqa
from .pmp_parser import PMPParser # noqa

Expand Down
Loading
Loading