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

xarray.to_netcdf kwargs and tests #294

Merged
merged 2 commits into from
Apr 4, 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
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/ecmwf/cfgrib/pull/294/>`_.


0.9.10.1 (2022-03-16)
Expand Down
47 changes: 35 additions & 12 deletions cfgrib/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# Alessandro Amici - B-Open - https://bopen.eu
#

import json
import os.path
import typing as T

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 = {}

Expand All @@ -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")
Expand Down
24 changes: 24 additions & 0 deletions tests/test_60_main_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand All @@ -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()

Expand Down