gjoseph92 / stackstac

Turn a STAC catalog into a dask-based xarray
https://stackstac.readthedocs.io
MIT License
232 stars 49 forks source link

Passing resampling #11

Open RichardScottOZ opened 3 years ago

RichardScottOZ commented 3 years ago

Without thinking I put resampling="bilinear" and got an error when I called .compute()

Traceback (most recent call last):
  File "carajas.py", line 92, in <module>
    band_medianNP = band_median.compute()
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/xarray/core/dataarray.py", line 899, in compute
    return new.load(**kwargs)
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/xarray/core/dataarray.py", line 873, in load
    ds = self._to_temp_dataset().load(**kwargs)
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/xarray/core/dataset.py", line 798, in load
    evaluated_data = da.compute(*lazy_data.values(), **kwargs)
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/dask/base.py", line 565, in compute
    results = schedule(dsk, keys, **kwargs)
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/dask/threaded.py", line 76, in get
    results = get_async(
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/dask/local.py", line 487, in get_async
    raise_exception(exc, tb)
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/dask/local.py", line 317, in reraise
    raise exc
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/dask/local.py", line 222, in execute_task
    result = _execute_task(task, data)
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/dask/core.py", line 121, in _execute_task
    return func(*(_execute_task(a, cache) for a in args))
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/dask/core.py", line 121, in <genexpr>
    return func(*(_execute_task(a, cache) for a in args))
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/dask/core.py", line 121, in _execute_task
    return func(*(_execute_task(a, cache) for a in args))
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/dask/optimization.py", line 963, in __call__
    return core.get(self.dsk, self.outkey, dict(zip(self.inkeys, args)))
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/dask/core.py", line 151, in get
    result = _execute_task(task, cache)
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/dask/core.py", line 121, in _execute_task
    return func(*(_execute_task(a, cache) for a in args))
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/stackstac/to_dask.py", line 151, in fetch_raster_window
    data = reader.read(current_window)
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/stackstac/rio_reader.py", line 393, in read
    reader = self.dataset
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/stackstac/rio_reader.py", line 389, in dataset
    self._dataset = self._open()
  File "/home/ubuntu/anaconda3/envs/richard/lib/python3.8/site-packages/stackstac/rio_reader.py", line 355, in _open
    vrt = WarpedVRT(
  File "rasterio/_warp.pyx", line 871, in rasterio._warp.WarpedVRTReaderBase.__init__
TypeError: an integer is required

so I presume you need to pass the integer from the enum? e.g. 1 in this case? Or I am not quite clear on what this keyword is expecting?

kylebarron commented 3 years ago

I think you need to pass the enum value from rasterio's Resampling enum

rmg55 commented 3 years ago

I am guessing the integer-resampling mapping is the same as rasterio. See the rasterio docs here: https://rasterio.readthedocs.io/en/latest/api/rasterio.enums.html#rasterio.enums.Resampling

kylebarron commented 3 years ago

I was on my phone before but yes, it should be a resampling enum https://github.com/gjoseph92/stackstac/blob/b652a07f9b2ae27235aea4db4ef0f1f594fd8941/stackstac/stack.py#L29

RichardScottOZ commented 3 years ago

Yes, my guess too when testing. We should put this in an example so people see it obviously. Or be fancy and the common text terms could map back via a dictionary?

gjoseph92 commented 3 years ago

Or be fancy and the common text terms could map back via a dictionary?

This seems worthwhile. Quite easy to add something like

    if isinstance(resampling, str):
        try:
            resampling = Resampling[resampling]
        except KeyError:
            raise ValueError(...) from None
kylebarron commented 3 years ago

Just curious, what's the point of raising the exception from None there? I would've assumed that would be identical to a bare raise ValueError

gjoseph92 commented 3 years ago

@kylebarron the from None just will give a shorter, cleaner traceback, eliminating the During handling of the above exception, another exception occurred message you'd otherwise see.

https://stefan.sofa-rockers.org/2020/10/28/raise-from/ https://stackoverflow.com/a/24752607

szwiep commented 1 year ago

I'd love to take this on as a first issue with stackstac!

gjoseph92 commented 1 year ago

@szwiep sorry I missed your comment, please feel free to take this on, that would be much appreciated!

szwiep commented 1 year ago

Hi all, finally put aside the time to get around to this! One question, how can I install from source/test my changes? I can't find documentation on this, so if you could point me in the right direction that'd be appreciated.