intake / intake-xarray

Intake plugin for xarray
https://intake-xarray.readthedocs.io/
BSD 2-Clause "Simplified" License
74 stars 36 forks source link

DataArray object has no attribute 'to_zarr' #122

Closed itepifanio closed 2 years ago

itepifanio commented 2 years ago

I have a catalog that contains images in jpg format.

images:
    driver: xarray_image
    description: "Images in jpg format"
    args:
      urlpath: "s3://path/to/*.jpg"
      storage_options:
        key: "{{env(AWS_KEY)}}"
        secret: "{{env(AWS_SECRET)}}"
        client_kwargs:
          endpoint_url: "https://s3.endpoint.com"

But when I try to export using catalog.images.export('./catalogs') I'm facing the following error:

/tmp/ipykernel_34689/1348189355.py in <cell line: 1>()
----> 1 catalog.images.export('./catalogs')

~/miniconda3/envs/test/lib/python3.8/site-packages/intake/source/base.py in export(self, path, **kwargs)
    401         representation (``.yaml()``).
    402         """
--> 403         return self._export(path, **kwargs)
    404 
    405     def _export(self, path, **kwargs):

~/miniconda3/envs/test/lib/python3.8/site-packages/intake/source/base.py in _export(self, path, **kwargs)
    409         method = container_map[self.container]._persist
    410         # may need to create path - access file-system method
--> 411         out = method(self, path=path, **kwargs)
    412         out.description = self.description
    413         metadata = {'timestamp': time.time(),

~/miniconda3/envs/test/lib/python3.8/site-packages/intake_xarray/xarray_container.py in _persist(source, path, **kwargs)
    156         """
    157         from intake_xarray import ZarrSource
--> 158         source.to_dask().to_zarr(path, **kwargs)
    159         return ZarrSource(path)
    160 

~/miniconda3/envs/test/lib/python3.8/site-packages/xarray/core/common.py in __getattr__(self, name)
    237                 with suppress(KeyError):
    238                     return source[name]
--> 239         raise AttributeError(
    240             f"{type(self).__name__!r} object has no attribute {name!r}"
    241         )

AttributeError: 'DataArray' object has no attribute 'to_zarr'
aaronspring commented 2 years ago

There is only xr.Dataset.to_zarr not for xr.DataArray. I never use export so far. Maybe an easy fix would be to promote DataArrays to Datasets in this case.

itepifanio commented 2 years ago

There is only xr.Dataset.to_zarr not for xr.DataArray. I never use export so far. Maybe an easy fix would be to promote DataArrays to Datasets in this case.

@aaronspring I'm trying to get my remote data and store it locally, that's why I'm using the export feature.

To cast from DataArray to Dataset I would need to read the images like catalog.images.read() and then cast it, right? But I'm facing also an error when reading the images:

ValueError: could not broadcast input array from shape (1,338,500,3) into shape (1,375,500,3)

It seems to be related to this xarray error.

martindurant commented 2 years ago

There is only xr.Dataset.to_zarr not for xr.DataArray

Yep, clearly this would be a simple if/else correction in RemoteXArray._persist. If the source produces an xarray rather than a dataset, we can put it in a dataset, but must assign a name.

itepifanio commented 2 years ago

@martindurant @aaronspring thanks for the quick fix!

Do you know when these changes will be released?

martindurant commented 2 years ago

When one of us gets around to it :) If you keep pinging, we'll do it sooner!