def to_zarr(self, path, **kwargs):
        """
        Write contents to a zarr group.

        Parameters
        ----------
        path: str
            Path to which to save.
        **kwargs:
            Keyword arguments for :py:func:`xarray.Dataset.to_zarr()`

        References
        ----------
        http://xarray.pydata.org/en/stable/generated/xarray.Dataset.to_zarr.html
        """

        # Check parameters
        _check_instance({"path": path}, "str")

        # to_zarr doesn't like coordinates attribute
        dataset = _rename_coord_attrs(self.dataset)

        # Compute
        compute = kwargs.pop("compute", None)
        print("Writing dataset to [{}].".format(path))
        if compute is None or compute is False:
            delayed_obj = dataset.to_zarr(path, compute=False, **kwargs)
            with _ProgressBar():
                delayed_obj.compute()
        else:
            dataset.to_zarr(path, compute=compute, **kwargs)
Exemple #2
0
def save_ds_info(ds, info, path):
    """
    Save ds and info to path.nc and path.obj, respectively 
    
    Parameters
    ----------
    ds: xarray.Dataset
    info: oceanspy.open_dataset._info   
    path: str
        Path to which to save ds and info
    """

    import pickle as _pickle
    import os as _os

    # Create paths
    if not _os.path.exists(_os.path.dirname(path)):
        _os.makedirs(_os.path.dirname(path))
    ds_path = path + '.nc'
    info_path = path + '.obj'

    # Save ds
    from dask.diagnostics import ProgressBar as _ProgressBar
    print('Saving ds to', ds_path)
    delayed_obj = ds.to_netcdf(ds_path, compute=False)
    with _ProgressBar():
        results = delayed_obj.compute()

    # Save info
    info.to_obj(info_path)
Exemple #3
0
    def to_netcdf(self, path, **kwargs):
        """
        Write dataset contents to a netCDF file.
        
        Parameters
        ----------
        path: str
            Path to which to save this dataset.
        **kwargs:
            Keyword arguments for xarray.DataSet.to_netcdf()
        
        References
        ----------
        http://xarray.pydata.org/en/stable/generated/xarray.Dataset.to_netcdf.html
        """
        # Check parameters
        if not isinstance(path, str):
            raise TypeError('`path` must be str')

        # to_netcdf doesn't like coordinates attribute
        dataset = self.dataset
        for var in dataset.variables:
            attrs = dataset[var].attrs
            coordinates = attrs.pop('coordinates', None)
            dataset[var].attrs = attrs
            if coordinates is not None:
                dataset[var].attrs['_coordinates'] = coordinates

        compute = kwargs.pop('compute', None)
        print('Writing dataset to [{}].'.format(path))
        if compute is None or compute is False:
            delayed_obj = dataset.to_netcdf(path, compute=False, **kwargs)
            with _ProgressBar():
                results = delayed_obj.compute()
        else:
            dataset.to_netcdf(path, compute=compute, **kwargs)