Skip to content

Commit

Permalink
Merge pull request #334 from EddyCMWF/master
Browse files Browse the repository at this point in the history
variable encoding for to_netcdf
  • Loading branch information
iainrussell authored Mar 2, 2023
2 parents 134df11 + cfa24b8 commit 3951355
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
25 changes: 20 additions & 5 deletions cfgrib/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,22 @@ def selfcheck() -> None:
"-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."
"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
@click.option(
"--var-encoding-json",
"-v",
default=None,
help=(
"encoding options to apply to all data variables in the dataset. "
"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, var_encoding_json
): # type: (T.List[str], str, str, str, str, str, str) -> None
import xarray as xr

import cf2cdm
Expand Down Expand Up @@ -125,6 +134,12 @@ def to_netcdf(inpaths, outpath, cdm, engine, backend_kwargs_json, netcdf_kwargs_
else:
netcdf_kwargs = {}

if var_encoding_json is not None:
var_encoding = handle_json(var_encoding_json)
netcdf_kwargs.setdefault("encoding", {})
for var in ds.data_vars:
netcdf_kwargs["encoding"].setdefault(var, var_encoding)

ds.to_netcdf(outpath, **netcdf_kwargs)


Expand Down
1 change: 0 additions & 1 deletion cfgrib/xarray_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ def open_dataset(
errors: str = "warn",
extra_coords: T.Dict[str, str] = {},
) -> xr.Dataset:

store = CfGribDataStore(
filename_or_obj,
indexpath=indexpath,
Expand Down
20 changes: 20 additions & 0 deletions tests/test_60_main_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ def test_cfgrib_cli_to_netcdf_netcdf_kwargs(tmpdir: py.path.local) -> None:
assert res.output == ""


def test_cfgrib_cli_to_netcdf_var_encoding(tmpdir: py.path.local) -> None:
runner = click.testing.CliRunner()

var_encoding = '{"dtype": "float", "scale_factor": 0.1}'
res = runner.invoke(__main__.cfgrib_cli, ["to_netcdf", TEST_DATA, "-v", var_encoding])

assert res.exit_code == 0
assert res.output == ""

var_encoding_json = tmpdir.join("temp.json")
with open(var_encoding_json, "w") as f:
f.write(var_encoding)
res = runner.invoke(
__main__.cfgrib_cli, ["to_netcdf", TEST_DATA, "-v", str(var_encoding_json)]
)

assert res.exit_code == 0
assert res.output == ""


def test_cfgrib_cli_dump() -> None:
runner = click.testing.CliRunner()

Expand Down

0 comments on commit 3951355

Please sign in to comment.