How to use the xarray.Dataset function in xarray

To help you get started, we’ve selected a few xarray examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pySTEPS / pysteps / examples / xarray_pysteps.py View on Github external
def load_mch(filename, timestep, **importer_kwargs):
    R, quality, meta = importer(filename, **importer_kwargs)
    x1 = meta["x1"]
    y1 = meta["y1"]
    xsize = meta["xpixelsize"]
    ysize = meta["ypixelsize"]

    ds = xr.Dataset(
        {"precipitation": (["y", "x"], R[::-1, :])},
        coords={
            "x": (
                ["x"],
                np.arange(x1 + xsize // 2, x1 + xsize * R.shape[1], xsize),
            ),
            "y": (
                ["y"],
                np.arange(y1 + ysize // 2, y1 + ysize * R.shape[0], ysize),
            ),
            "time": (["time"], [timestep]),
        },
    )
    root = [
        "projection",
        "x1",
github dcs4cop / xcube / test / core / test_level.py View on Github external
def create_test_dataset(cls, shape, chunks=None):
        size = int(np.prod(shape))
        dims = ["time", "y", "x"]
        a_data = np.linspace(0, 1, size, dtype=np.float64).reshape(shape)
        a = xr.DataArray(a_data, dims=dims)
        b_data = np.linspace(-1, 0, size, dtype=np.float64).reshape(shape)
        b = xr.DataArray(b_data, dims=dims)
        if chunks:
            a.encoding.update(chunks=chunks, chunksizes=chunks)
            b.encoding.update(chunks=chunks, chunksizes=chunks)
            return xr.Dataset(dict(a=a, b=b)).chunk(chunks={dims[i]: chunks[i] for i in range(len(dims))})
        else:
            return xr.Dataset(dict(a=a, b=b))
github jcmgray / xyzpy / tests / test_gen / __init__.py View on Github external
def foo2_dataset(a, b):
    import numpy as np
    import xarray as xr
    x = np.tile(a + b, (2, 3))
    return xr.Dataset({'x': (['t1', 't2'], x)})
github CCI-Tools / cate / test / core / test_types.py View on Github external
def test_convert(self):
        self.assertEqual(DataFrameLike.convert(None), None)

        data = {'c1': [4, 5, 6], 'c2': [6, 7, 8]}
        xr_ds = xr.Dataset(data_vars=data)
        pd_ds = pd.DataFrame(data=data)
        gdf_ds = gpd.GeoDataFrame.from_features(read_test_features())
        proxy_gdf_ds = GeoDataFrame.from_features(read_test_features())
        self.assertIsInstance(DataFrameLike.convert(xr_ds), pd.DataFrame)
        self.assertIsInstance(DataFrameLike.convert(pd_ds), pd.DataFrame)
        self.assertIs(DataFrameLike.convert(pd_ds), pd_ds)
        self.assertIsInstance(DataFrameLike.convert(gdf_ds), gpd.GeoDataFrame)
        self.assertIs(DataFrameLike.convert(gdf_ds), gdf_ds)
        self.assertIsInstance(DataFrameLike.convert(proxy_gdf_ds), GeoDataFrame)
        self.assertIs(DataFrameLike.convert(proxy_gdf_ds), proxy_gdf_ds)

        with self.assertRaises(ValidationError):
            DataFrameLike.convert(42)
github uber / bayesmark / bayesmark / xr_util.py View on Github external
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`.
    """
    coords = OrderedDict([(dd, ref.coords[dd].values) for dd in dims])

    data = OrderedDict()
    for var_name, var_dims in vars_:
        assert set(var_dims).issubset(dims)
        size = [ref.sizes[dd] for dd in var_dims]
        data[var_name] = (var_dims, np.full(size, np.nan))
    ds = xr.Dataset(data, coords=coords)
    return ds
github crusaderky / xarray_extras / xarray_extras / recursive_diff.py View on Github external
@cast.register(xarray.Dataset)  # noqa:F811
def _(obj, brief_dims):
    """Single dispatch specialised variant of :func:`cast` for
    :class:`xarray.Dataset`.

    Map to a dict of DataArrays.
    """
    return {
        'attrs': obj.attrs,
        # There may be coords, index or not, that are not
        # used in any data variable.
        # See above on why indices are handled separately
        'index': {
            k: obj.coords[k].to_index()
            for k in obj.dims
        },
        'coords': {
github landlab / landlab / landlab / grid / radial.py View on Github external
def as_dataset(self, include="*", exclude=None):
        dataset = xr.Dataset(
            {
                "n_rings": self.number_of_rings,
                "nodes_in_first_ring": self.number_of_nodes_in_ring[0],
                "spacing": self.spacing_of_rings,
                "xy_of_center": (("dim",), list(self.xy_of_center)),
            },
            attrs={"grid_type": "radial"},
        )
        return dataset.update(
            super(RadialModelGrid, self).as_dataset(include=include, exclude=exclude)
        )
github atmtools / typhon / typhon / datasets / tovs.py View on Github external
)

        if "lon" in M.dtype.names:
            coords["lon"] = (("time", "scanpos"), M["lon"])
        if "lat" in M.dtype.names:
            coords["lat"] = (("time", "scanpos"), M["lat"])

        coords = {rename_dimensions.get(k, k):
                ([rename_dimensions.get(d, d) for d in v[0] if d not in skip_dimensions],
                 v[1],
                 {**(p[k][2] if k in p else {}),
                  **(add_to_all if k in M.dtype.names else {}),
                  **({origvar: k} if k in M.dtype.names else {})})
                for (k, v) in coords.items() if k not in skip_dimensions}

        ds = xarray.Dataset(
            {k:v for (k,v) in data_vars.items() if not k in coords.keys()},
            coords,
            {"title": "HIRS L1C"})

        for v in p.keys() & set(M.dtype.names):
            ds[p[v][0]].encoding = p[v][3]
            if ("bit" not in p[v][0] and
                ds[p[v][0]].dtype.kind not in "Mm" and
                hasattr(M[v], "mask")):
                ds[p[v][0]].values[M[v].mask] = numpy.nan #(
#                numpy.nan if M[v].dtype.kind.startswith('f')
#                else p[v][3]["_FillValue"])
        
        return ds
github MPAS-Dev / MPAS-Analysis / preprocess_observations / compute_woce_transects.py View on Github external
'units': 'deg C'}})

    salinity = xarray.DataArray.from_dict({'dims': ('nPoints',
                                                    'nz'),
                                           'data': salinity,
                                           'attrs':
                                               {'long_name': 'salinity',
                                                'units': 'PSU'}})

    potDensity = xarray.DataArray.from_dict(
        {'dims': ('nPoints', 'nz'),
         'data': potDensity,
         'attrs': {'long_name': 'potential denisty',
                   'units': 'kg m^{-3}'}})

    dsTransect = xarray.Dataset({'lon': longitude,
                                 'lat': latitude,
                                 'pressure': pressure,
                                 'z': z,
                                 'potentialTemperature': potTemp,
                                 'salinity': salinity,
                                 'potentialDensity': potDensity})

    write_netcdf(dsTransect, '{}/{}'.format(outDir, outFileName))
github noaa-oar-arl / MONET / monet / util / resample.py View on Github external
def resample_xesmf(source_da, target_da, cleanup=False, **kwargs):
    if has_xesmf:
        import xesmf as xe
        import xarray as xr
        regridder = xe.Regridder(source_da, target_da, **kwargs)
        if cleanup:
            regridder.clean_weight_file()
        if isinstance(source_da, xr.Dataset):
            das = {}
            for name, i in source_da.data_vars.items():
                das[name] = regridder(i)
            ds = xr.Dataset(das)
            ds.attrs = source_da.attrs
            return ds
        else:
            return regridder(source_da)