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

UI adjustments #121

Merged
merged 10 commits into from Dec 22, 2022
6 changes: 4 additions & 2 deletions xscen/catalog.py
Expand Up @@ -26,9 +26,9 @@
from intake.source.utils import reverse_format
from intake_esm.cat import ESMCatalogModel

from .config import CONFIG, parse_config, recursive_update
from .config import CONFIG, args_as_str, parse_config, recursive_update
from .io import get_engine
from .utils import CV
from .utils import CV # noqa

logger = logging.getLogger(__name__)
# Monkey patch for attribute names in the output of to_dataset_dict
Expand Down Expand Up @@ -162,6 +162,8 @@ def __init__(self, *args, check_valid=False, drop_duplicates=False, **kwargs):
kwargs["read_csv_kwargs"] = recursive_update(
csv_kwargs.copy(), kwargs.get("read_csv_kwargs", {})
)
args = args_as_str(args)

super().__init__(*args, **kwargs)
if check_valid:
self.check_valid()
Expand Down
14 changes: 14 additions & 0 deletions xscen/config.py
Expand Up @@ -49,6 +49,7 @@
from copy import deepcopy
from functools import wraps
from pathlib import Path
from typing import Any, Tuple

import xarray as xr
import xclim as xc
Expand All @@ -59,8 +60,10 @@

__all__ = [
"CONFIG",
"args_as_str",
"load_config",
"parse_config",
"recursive_update",
]


Expand Down Expand Up @@ -111,6 +114,17 @@ def recursive_update(d, other):
return d


def args_as_str(*args: Tuple[Any, ...]) -> Tuple[str, ...]:
RondeauG marked this conversation as resolved.
Show resolved Hide resolved
"""Return arguments as strings."""
new_args = []
for i, arg in enumerate(*args):
if isinstance(arg, Path):
new_args.append(str(arg))
else:
new_args.append(arg)
return tuple(new_args)


def load_config(*files, reset=False, verbose=False):
"""Load configuration from given files (in order, the last has priority).

Expand Down
11 changes: 9 additions & 2 deletions xscen/extract.py
@@ -1,6 +1,7 @@
# noqa: D100
import datetime
import logging
import os
import re
from copy import deepcopy
from pathlib import Path
Expand Down Expand Up @@ -501,7 +502,9 @@ def resample(

@parse_config
def search_data_catalogs(
data_catalogs: Union[list, DataCatalog],
data_catalogs: Union[
Union[str, os.PathLike], List[Union[str, os.PathLike]], DataCatalog
],
variables_and_freqs: dict,
*,
other_search_criteria: Optional[dict] = None,
Expand All @@ -519,7 +522,7 @@ def search_data_catalogs(

Parameters
----------
data_catalogs : Union[list, DataCatalog]
data_catalogs : Union[Union[str, os.PathLike], List[Union[str, os.PathLike]], DataCatalog]
DataCatalog (or multiple, in a list) or paths to JSON/CSV data catalogs. They must use the same columns and aggregation options.
variables_and_freqs : dict
Variables and freqs to search for, following a 'variable: xr-freq-compatible-str' format.
Expand Down Expand Up @@ -578,6 +581,10 @@ def search_data_catalogs(
"registry": registry_from_module(load_xclim_module(conversion_yaml))
}

# Cast paths to single item list
if isinstance(data_catalogs, (str, Path)):
data_catalogs = [data_catalogs]

# Prepare a unique catalog to search from, with the DerivedCat added if required
if isinstance(data_catalogs, DataCatalog):
catalog = DataCatalog(
Expand Down