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

variable encoding for to_netcdf #334

Merged
merged 1 commit into from
Mar 2, 2023
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
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