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

simpler way to create a new catalog #77

Merged
merged 8 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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