def ds_like()

in bayesmark/xr_util.py [0:0]


def ds_like(ref, vars_, dims, fill=np.nan):
    """Produce a blank :class:`xarray:xarray.Dataset` copying some coordinates from another
    :class:`xarray:xarray.Dataset`.

    Parameters
    ----------
    ref : :class:`xarray:xarray.Dataset`
        The reference dataset we want to copy coordinates from.
    vars_ : typing.Iterable
        List of variable names we want in the new dataset.
    dims : list
        List of dimensions we want to copy over from `ref`. These are the dimensions of the output.
    fill : scalar
        Scalar value to fill the blank dataset. The `dtype` will be determined from the `fill` value.

    Returns
    -------
    ds : :class:`xarray:xarray.Dataset`
        A new dataset with variables `vars_` and dimensions `dims` where the coordinates have been copied from `ref`.
        All values are filled with `fill`.
    """
    size = [ref.sizes[dd] for dd in dims]

    # Use OrderedDict for good measure, probably not needed
    data = OrderedDict([(vv, (dims, np.full(size, fill))) for vv in vars_])
    coords = OrderedDict([(dd, ref.coords[dd].values) for dd in dims])
    ds = xr.Dataset(data, coords=coords)
    return ds