diff --git a/.gitignore b/.gitignore index b35caad6..ab70786a 100644 --- a/.gitignore +++ b/.gitignore @@ -116,3 +116,5 @@ pip-wheel-metadata/* tests/test_collections/*.nc .DS_Store dask-worker-space/ + +CMIP6-MRI-ESM2-0* diff --git a/intake_esm/cat.py b/intake_esm/cat.py index 0eb5d873..c1fe2ff5 100644 --- a/intake_esm/cat.py +++ b/intake_esm/cat.py @@ -140,7 +140,7 @@ def save( The name of the file to save the catalog to. directory: str The directory or cloud storage bucket to save the catalog to. - If None, use the current directory + If None, use the current directory. catalog_type: str The type of catalog to save. Whether to save the catalog table as a dictionary in the JSON file or as a separate CSV file. Valid options are 'dict' and 'file'. @@ -163,7 +163,13 @@ def save( raise ValueError( f'catalog_type must be either "dict" or "file". Received catalog_type={catalog_type}' ) - mapper = fsspec.get_mapper(f'{directory}' or '.', storage_options=storage_options) + + # Check if the directory is None, and if it is, set it to the current directory + if directory is None: + directory = os.getcwd() + + # Configure the fsspec mapper and associated filenames + mapper = fsspec.get_mapper(f'{directory}', storage_options=storage_options) fs = mapper.fs csv_file_name = f'{mapper.fs.protocol}://{mapper.root}/{name}.csv' json_file_name = f'{mapper.fs.protocol}://{mapper.root}/{name}.json' diff --git a/tests/test_core.py b/tests/test_core.py index 54c74ca8..90cd9459 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,4 +1,5 @@ import ast +import os import intake import pandas as pd @@ -176,24 +177,29 @@ def test_catalog_keys_info(): @pytest.mark.parametrize( - 'catalog_type, to_csv_kwargs, json_dump_kwargs', - [('file', {'compression': 'bz2'}, {}), ('file', {'compression': 'gzip'}, {}), ('dict', {}, {})], + 'catalog_type, to_csv_kwargs, json_dump_kwargs, directory', + [ + ('file', {'compression': 'bz2'}, {}, '.'), + ('file', {'compression': 'gzip'}, {}, None), + ('dict', {}, {}, None), + ], ) -def test_catalog_serialize(tmp_path, catalog_type, to_csv_kwargs, json_dump_kwargs): +def test_catalog_serialize(catalog_type, to_csv_kwargs, json_dump_kwargs, directory): cat = intake.open_esm_datastore(cdf_cat_sample_cmip6) - local_store = tmp_path cat_subset = cat.search( source_id='MRI-ESM2-0', ) name = 'CMIP6-MRI-ESM2-0' cat_subset.serialize( name=name, - directory=str(local_store), + directory=directory, catalog_type=catalog_type, to_csv_kwargs=to_csv_kwargs, json_dump_kwargs=json_dump_kwargs, ) - cat = intake.open_esm_datastore(f'{local_store}/{name}.json') + if directory is None: + directory = os.getcwd() + cat = intake.open_esm_datastore(f'{directory}/{name}.json') pd.testing.assert_frame_equal( cat_subset.df.reset_index(drop=True), cat.df.reset_index(drop=True) )