Skip to content

Add debugging functions. #152

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion cf_xarray/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
from .accessor import CFAccessor # noqa
from .helpers import bounds_to_vertices, vertices_to_bounds # noqa
from .helpers import ( # noqa
bounds_to_vertices,
create_dataset_like,
to_dict,
vertices_to_bounds,
)
54 changes: 53 additions & 1 deletion cf_xarray/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
import xarray as xr
from xarray import DataArray
from xarray import DataArray, Dataset


def bounds_to_vertices(
Expand Down Expand Up @@ -119,3 +119,55 @@ def vertices_to_bounds(
f"vertices format not understood. Got {vertices.dims} with shape {vertices.shape}."
)
return xr.DataArray(bnd_vals, dims=out_dims[: vertices.ndim + 1])


def create_dataset_like(ds: Dataset) -> Dataset:
"""Returns a dataset that looks like ``ds`` with dummy data but
attrs and encoding preserved."""
ndims = len(ds.dims)
sizes = range(2, ndims + 2)
dims = dict(zip(ds.dims.keys(), sizes))

coords = {
k: (k, np.arange(dims[k]), ds[k].attrs)
for k, v in dims.items()
if k in ds.coords
}
newds = Dataset(coords=coords, attrs=ds.attrs)
for var in ds.variables:
if var in newds:
continue
old = ds[var]
newshape = list(dims[dim] for dim in old.dims)
newds[var] = (
(old.dims),
np.arange(np.prod(newshape)).reshape(newshape),
old.attrs,
)
newds[var].encoding = ds[var].encoding

newds = newds.set_coords(ds.coords.keys())
return newds


def to_dict(ds: Dataset) -> dict:
"""
Returns Dataset.to_dict() with 'data' rewritten to a string with
an appropriate call to np.arange. Use this with output from
``create_dataset_like``.

See Also
--------
create_dataset_like
Dataset.to_dict
"""
asdict = ds.to_dict()
for kind in ["data_vars", "coords"]:
for var in asdict[kind]:
if var in asdict["dims"]:
continue
array = np.asarray(asdict[kind][var]["data"])
asdict[kind][var][
"data"
] = f"np.arange(np.prod({array.shape})).reshape({array.shape})"
return asdict