You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried using the context, but that resulted in an empty data array being returned (which I think is quite logical this the context will be closed at the time of plotting).
The example you provide does not seem to work (so passing a regular open file to open_rasterio):
...
File "/home/a001673/usr/src/rioxarray/rioxarray/_io.py", line 959, in open_rasterio
result = _prepare_dask(result, riods, filename, chunks)
File "/home/a001673/usr/src/rioxarray/rioxarray/_io.py", line 664, in _prepare_dask
mtime = os.path.getmtime(filename)
File "/home/a001673/mambaforge/envs/satpy/lib/python3.10/genericpath.py", line 55, in getmtime
return os.stat(filename).st_mtime
TypeError: stat: path should be string, bytes, os.PathLike or integer, not BufferedReader
Also rasterio seems to be having problems with opened jp2 files (tif works fine), so I'll be reporting an issue for that on rasterio when I get a minimal case.
I tried using the context, but that resulted in an empty data array being returned (which I think is quite logical this the context will be closed at the time of plotting).
Sounds like you may need to add a .load() call after open_rasterio.
Edited for brevity, "reproducible" code, and a possible solution:
Regarding @snowman2's suggestion to use with and fsspec, I've had trouble with rioxarray while using this pattern when reading from private Google Cloud Storage buckets. Interestingly it does not seem to happen for xarray.open_dataset(), and when using rioxarray.open_rasterio() it seems like file-like objects are being closed before subsequent operations can go back to retrieve data from them when needed. See below.
Using rioxarray to open geotiffs from a private Google Cloud Storage bucket, the resulting DataArray opens, and I can even access the coordinate values, but accessing the information that would show up in a plot is not possible:
Note that this solution fails when trying to pass any size for 'chunks' as a load argument, e.g., load_args = {'chunks': 2048}.
I can understand how the use of with leads to a file being closed and the ValueError("I/O operation on closed file.") error. It is strange to me that the same pattern works with xarray but not rioxarray, and that chunking does not seem to work.
Is this just something rioxarray currently does not support?
I think using .load() would go against what we generally try to do in Satpy which is to delay loading data until it is actually being computed.
From what I can tell, calling .load() is equivalent to the initial code snippet above:
withopen(filename, "rb") asof:
data=of.read()
Since the data is lazily loaded by rioxarray.open_rasterio, calling .load() is the only way to load in the data before closing the file handle in the current state of the code.
Ideas to improve this behavior are definitely welcome. It may be worth looking into how xarray handles this scenario as they may have found a solution for this.
I think using .load() would go against what we generally try to do in Satpy which is to delay loading data until it is actually being computed.
From what I can tell, calling .load() is equivalent to the initial code snippet above:
withopen(filename, "rb") asof:
data=of.read()
Yes, it is, however, the of.read() was just a means to get data into the MemoryFile to illustrate the problem.
In the real case, we want to keep things lazy as much as possible. I believe with your example here: #550 (comment) and the fix in #550 I'm good.
Ideas to improve this behavior are definitely welcome. It may be worth looking into how xarray handles this scenario as they may have found a solution for this.
Activity
mraspaud commentedon Jul 18, 2022
I suspect this is due to gdal's memory file object being disposed off before dask can make use of it...
mraspaud commentedon Jul 18, 2022
Just for clarification and some context: The opening of the file and reading to put the data in the MemoryFile is of course contrived, and just comes from the fact that I need to be able to pass open files to rioxarray. In my case, I want to use fsspec in the background (https://filesystem-spec.readthedocs.io/en/latest/) so as to be able to read remote files on the fly.
snowman2 commentedon Jul 18, 2022
See: rasterio/rasterio#2517 (comment) for an example of using MemoryFile using
with.@mraspaud, with this addition to rasterio 1.3 (recently released), I believe you should be able to use fsspec files directly (related #246):
mraspaud commentedon Jul 18, 2022
I tried using the context, but that resulted in an empty data array being returned (which I think is quite logical this the context will be closed at the time of plotting).
The example you provide does not seem to work (so passing a regular open file to
open_rasterio):Also rasterio seems to be having problems with opened jp2 files (tif works fine), so I'll be reporting an issue for that on rasterio when I get a minimal case.
mraspaud commentedon Jul 18, 2022
But thanks a lot for the heads up on @djhoese 's amazing contribution :)
snowman2 commentedon Jul 18, 2022
Haven't tested that feature with dask ... looks like we need a patch to handle that.
snowman2 commentedon Jul 18, 2022
Sounds like you may need to add a .load() call after
open_rasterio.djhoese commentedon Jul 18, 2022
I think using
.load()would go against what we generally try to do in Satpy which is to delay loading data until it is actually being computed.mraspaud commentedon Jul 18, 2022
Yes exactly.
jamespolly commentedon Jul 30, 2022
Edited for brevity, "reproducible" code, and a possible solution:
Regarding @snowman2's suggestion to use
withandfsspec, I've had trouble with rioxarray while using this pattern when reading from private Google Cloud Storage buckets. Interestingly it does not seem to happen forxarray.open_dataset(), and when usingrioxarray.open_rasterio()it seems like file-like objects are being closed before subsequent operations can go back to retrieve data from them when needed. See below.Using
rioxarrayto open geotiffs from a private Google Cloud Storage bucket, the resultingDataArrayopens, and I can even access the coordinate values, but accessing the information that would show up in a plot is not possible:Note the file successfully opens but when I try to do
da.plot()the following error is generated:As a work around to this I've found the following pattern does what I want:
Note that this solution fails when trying to pass any size for
'chunks'as a load argument, e.g.,load_args = {'chunks': 2048}.I can understand how the use of
withleads to a file being closed and theValueError("I/O operation on closed file.")error. It is strange to me that the same pattern works with xarray but not rioxarray, and that chunking does not seem to work.Is this just something rioxarray currently does not support?
snowman2 commentedon Aug 15, 2022
#558 should address this: #550 (comment)
snowman2 commentedon Aug 16, 2022
From what I can tell, calling
.load()is equivalent to the initial code snippet above:Since the data is lazily loaded by
rioxarray.open_rasterio, calling.load()is the only way to load in the data before closing the file handle in the current state of the code.Ideas to improve this behavior are definitely welcome. It may be worth looking into how xarray handles this scenario as they may have found a solution for this.
mraspaud commentedon Aug 19, 2022
Yes, it is, however, the
of.read()was just a means to get data into theMemoryFileto illustrate the problem.In the real case, we want to keep things lazy as much as possible. I believe with your example here: #550 (comment) and the fix in #550 I'm good.
Yes, I think they do, but I don't remember how.
snowman2 commentedon Dec 19, 2022
I think this is resolved. If something is missing still, please comment.
Force loading dataarray into memory