from intake.source import base
from ._version import get_versions
__version__ = get_versions()['version']
del get_versions


class NetCDFPlugin(base.Plugin):
    """Plugin for netcdf->xarray reader"""

    def __init__(self):
        super(NetCDFPlugin, self).__init__(
            name='netcdf',
            version=__version__,
            container='xarray',
            partition_access=True)

    def open(self, urlpath, chunks, **kwargs):
        """
        Create NetCDFSource instance

        Open any file types available to xarray.

        If `pynio`_ is installed, additional file-types such as GRIB(2) are
        supported. Use the keyword ``engine='pynio'``.

        .. pynio: https://github.com/NCAR/pynio

        If passing multiple files, kwargs should include ``concat_dim=``
        keyword, the dimension name to give the file index.

        Parameters
        ----------
        urlpath: str
            Path to source file(s).
        chunks: int or dict
            Chunks is used to load the new dataset into dask
            arrays. ``chunks={}`` loads the dataset with dask using a single
            chunk for all arrays.
        """
        from intake_xarray.netcdf import NetCDFSource
        base_kwargs, source_kwargs = self.separate_base_kwargs(kwargs)
        return NetCDFSource(
            urlpath=urlpath,
            chunks=chunks,
            xarray_kwargs=source_kwargs,
            metadata=base_kwargs['metadata'])


class ZarrPlugin(base.Plugin):
    """zarr>xarray reader"""

    def __init__(self):
        super(ZarrPlugin, self).__init__(
            name='zarr',
            version=__version__,
            container='xarray',
            partition_access=True
        )

    def open(self, urlpath, storage_options=None, **kwargs):
        """
        Parameters
        ----------
        urlpath: str
            Path to source. This can be a local directory or a remote data
            service (i.e., with a protocol specifier like ``'s3://``).
        storage_options: dict
            Parameters passed to the backend file-system
        kwargs:
            Further parameters are passed to xr.open_zarr
        """
        from intake_xarray.xzarr import ZarrSource
        base_kwargs, source_kwargs = self.separate_base_kwargs(kwargs)
        return ZarrSource(urlpath, storage_options, base_kwargs['metadata'],
                          **source_kwargs)


class DataSourceMixin:
    """Common behaviours for plugins in this repo"""

    def _get_schema(self):
        """Make schema object, which embeds xarray object and some details"""
        if self._ds is None:
            self._open_dataset()

        metadata = {
            'dims': dict(self._ds.dims),
            'data_vars': tuple(self._ds.data_vars.keys()),
            'coords': tuple(self._ds.coords.keys())
        }
        metadata.update(self._ds.attrs)
        return base.Schema(
            datashape=None,
            dtype=self._ds,
            shape=None,
            npartitions=None,
            extra_metadata=metadata)

    def read(self):
        """Return a version of the xarray with all the data in memory"""
        self._load_metadata()
        return self._ds.load()

    def read_chunked(self):
        """Return xarray object (which will have chunks)"""
        self._load_metadata()
        return self._ds

    def read_partition(self, i):
        """Fetch one chunk of data at tuple index i

        (not yet implemented)
        """
        raise NotImplementedError

    def to_dask(self):
        """Return xarray object where variables are dask arrays"""
        return self.read_chunked()

    def close(self):
        """Delete open file from memory"""
        self._ds.close()
        self._ds = None

@@ -1056,16 +1056,20 @@
  arrays.
+ Use
  %60%60chunk
@@ -1078,85 +1078,1316 @@
 %7B%7D%60%60
+ to
  load
-s the dataset with dask using a single%0A            chunk for all arrays.
+ without chunking.%0A        kwargs:%0A            Further parameters are passed to xr.open_dataset or%0A            xr.open_mfdataset .%0A        %22%22%22%0A        from intake_xarray.netcdf import NetCDFSource%0A        base_kwargs, source_kwargs = self.separate_base_kwargs(kwargs)%0A        return NetCDFSource(%0A            urlpath=urlpath,%0A            chunks=chunks,%0A            xarray_kwargs=source_kwargs,%0A            metadata=base_kwargs%5B'metadata'%5D)%0A%0A%0Aclass RasterIOPlugin(base.Plugin):%0A    %22%22%22Plugin for xarray reader via rasterIO%22%22%22%0A%0A    def __init__(self):%0A        super(RasterIOPlugin, self).__init__(%0A            name='rasterio',%0A            version=__version__,%0A            container='xarray',%0A            partition_access=True)%0A%0A    def open(self, urlpath, chunks, **kwargs):%0A        %22%22%22%0A        Create RasterIOSource instance%0A%0A        If %60rasterio%60_ is installed, additional file-types such as GeoTIFF are%0A        supported.%0A%0A        .. rasterio: https://rasterio.readthedocs.io%0A%0A        Parameters%0A        ----------%0A        urlpath: str%0A            Path to source file.%0A        chunks: int or dict%0A            Chunks is used to load the new dataset into dask%0A            arrays. Use %60%60chunks=%7B%7D%60%60 to load without chunking.%0A        kwargs:%0A            Further parameters are passed to xr.open_rasterio
 %0A   
@@ -2418,38 +2418,38 @@
 take_xarray.
-netcdf
+raster
  import 
 NetCDFSource
@@ -2428,38 +2428,40 @@
 y.raster import 
-NetCDF
+RasterIO
 Source%0A        b
@@ -2529,38 +2529,40 @@
 %0A        return 
-NetCDF
+RasterIO
 Source(%0A        

add rasterio opened