From 409c9457179246ef678e7102dd6f1e9933600429 Mon Sep 17 00:00:00 2001 From: Eddy Date: Thu, 24 Mar 2022 14:07:57 +0000 Subject: [PATCH 1/2] xarray.to+netcdf kwargs and tests --- cfgrib/__main__.py | 47 +++++++++++++++++++++++++--------- tests/test_60_main_commands.py | 24 +++++++++++++++++ 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/cfgrib/__main__.py b/cfgrib/__main__.py index fe7e529f..3e11ddff 100644 --- a/cfgrib/__main__.py +++ b/cfgrib/__main__.py @@ -17,6 +17,7 @@ # Alessandro Amici - B-Open - https://bopen.eu # +import json import os.path import typing as T @@ -25,6 +26,21 @@ # NOTE: imports are executed inside functions so missing dependencies don't break all commands +def handle_json(in_json): + """ + Handle input json which can be a a json format string, or path to a json format file. + Returns a dictionary of the json contents. + """ + try: + # Assume a json format string + out_json = json.loads(in_json) + except json.JSONDecodeError: + # Then a json file + with open(in_json, "r") as f: + out_json = json.load(f) + return out_json + + @click.group() def cfgrib_cli() -> None: pass @@ -57,10 +73,18 @@ def selfcheck() -> None: "the path to JSON file" ), ) -def to_netcdf(inpaths, outpath, cdm, engine, backend_kwargs_json): - # type: (T.List[str], str, str, str, str) -> None - import json - +@click.option( + "--netcdf-kwargs-json", + "-n", + default=None, + help=( + "kwargs used xarray.to_netcdf when creating the netCDF file." + "Can either be a JSON format string or " + "the path to JSON file." + ), +) +def to_netcdf(inpaths, outpath, cdm, engine, backend_kwargs_json, netcdf_kwargs_json): + # type: (T.List[str], str, str, str, str, str) -> None import xarray as xr import cf2cdm @@ -73,13 +97,7 @@ def to_netcdf(inpaths, outpath, cdm, engine, backend_kwargs_json): outpath = os.path.splitext(inpaths[0])[0] + ".nc" if backend_kwargs_json is not None: - try: - # Assume a json format string - backend_kwargs = json.loads(backend_kwargs_json) - except json.JSONDecodeError: - # Then a json file - with open(backend_kwargs_json, "r") as f: - backend_kwargs = json.load(f) + backend_kwargs = handle_json(backend_kwargs_json) else: backend_kwargs = {} @@ -97,7 +115,12 @@ def to_netcdf(inpaths, outpath, cdm, engine, backend_kwargs_json): coord_model = getattr(cf2cdm, cdm) ds = cf2cdm.translate_coords(ds, coord_model=coord_model) - ds.to_netcdf(outpath) + if netcdf_kwargs_json is not None: + netcdf_kwargs = handle_json(netcdf_kwargs_json) + else: + netcdf_kwargs = {} + + ds.to_netcdf(outpath, **netcdf_kwargs) @cfgrib_cli.command("dump") diff --git a/tests/test_60_main_commands.py b/tests/test_60_main_commands.py index da159e5d..b202ab88 100644 --- a/tests/test_60_main_commands.py +++ b/tests/test_60_main_commands.py @@ -32,6 +32,10 @@ def test_cfgrib_cli_to_netcdf(tmpdir: py.path.local) -> None: assert res.exit_code == 0 assert res.output == "" + +def test_cfgrib_cli_to_netcdf_backend_kwargs(tmpdir: py.path.local) -> None: + runner = click.testing.CliRunner() + backend_kwargs = '{"time_dims": ["time"]}' res = runner.invoke(__main__.cfgrib_cli, ["to_netcdf", TEST_DATA, "-b", backend_kwargs]) @@ -49,6 +53,26 @@ def test_cfgrib_cli_to_netcdf(tmpdir: py.path.local) -> None: assert res.output == "" +def test_cfgrib_cli_to_netcdf_netcdf_kwargs(tmpdir: py.path.local) -> None: + runner = click.testing.CliRunner() + + netcdf_kwargs = '{"engine": "scipy"}' + res = runner.invoke(__main__.cfgrib_cli, ["to_netcdf", TEST_DATA, "-n", netcdf_kwargs]) + + assert res.exit_code == 0 + assert res.output == "" + + netcdf_kwargs_json = tmpdir.join("temp.json") + with open(netcdf_kwargs_json, "w") as f: + f.write(netcdf_kwargs) + res = runner.invoke( + __main__.cfgrib_cli, ["to_netcdf", TEST_DATA, "-n", str(netcdf_kwargs_json)] + ) + + assert res.exit_code == 0 + assert res.output == "" + + def test_cfgrib_cli_dump() -> None: runner = click.testing.CliRunner() From fa8ad9adca91e4986b399a6e051497afd5d80fe9 Mon Sep 17 00:00:00 2001 From: Iain Russell Date: Mon, 4 Apr 2022 10:06:23 +0100 Subject: [PATCH 2/2] xarray.to+netcdf kwargs - add to changelog --- CHANGELOG.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4d0705b7..68708cc7 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,7 +5,8 @@ Changelog for cfgrib 0.9.10.2 (unreleased) --------------------- -- Nothing changed yet. +- added --netcdf_kwargs_json option to 'cfgrib to_netcdf' + See `#294 `_. 0.9.10.1 (2022-03-16)