Skip to content

Commit

Permalink
Merge pull request #77 from Ouranosinc/cat-create
Browse files Browse the repository at this point in the history
simpler way to create a new catalog
  • Loading branch information
RondeauG committed Sep 20, 2022
2 parents 2ffdc62 + 9ee548d commit fc6922e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ New features and enhancements
* New function `catalog.unstack_id` to reverse-engineer IDs. (:pull:`63`).
* `generate_id` now accepts Datasets. (:pull:`63`).
* Add `rechunk` option to `properties_and_measures` (:pull:`76`).
* Add `create` argument to `ProjectCatalog` (:issue:`11`, :pull:`77`).

Breaking changes
^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion docs/notebooks
Submodule notebooks updated from dad3e4 to 05235c
38 changes: 37 additions & 1 deletion xscen/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,43 @@ def create(

return cls(str(meta_path))

def __init__(self, df, *args, **kwargs):
def __init__(
self,
df: Union[str, dict],
*args,
create: bool = False,
overwrite: bool = None,
project: dict = None,
**kwargs,
):
"""
Open or create a project catalog.
Parameters
----------
df : PathLike, dict
If string, this must be a path or URL to a catalog JSON file.
If dict, this must be a dict representation of an ESM catalog. See the notes below.
create: bool
If True, and if 'df' is a string, this will create an empty ProjectCatalog if none already exists.
project : dict-like
Metadata to create the catalog, if required.
overwrite : bool
If this and 'create' are True, this will overwrite any existing JSON and CSV file with an empty catalog.
Notes
----------
The dictionary in 'df' must have two keys: ‘esmcat’ and ‘df’.
The ‘esmcat’ key must be a dict representation of the ESM catalog. This should follow the template used by xscen.catalog.esm_col_data.
The ‘df’ key must be a Pandas DataFrame containing content that would otherwise be in the CSV file.
"""
if create:
if isinstance(df, (str, Path)) and (
not os.path.isfile(Path(df)) or overwrite
):
self.create(df, project=project, overwrite=overwrite)
super().__init__(df, *args, **kwargs)
self.check_valid()
self.drop_duplicates()
Expand Down

0 comments on commit fc6922e

Please sign in to comment.