Parallelisation of Dataset.rio.clip #8608
Replies: 2 comments 1 reply
-
You can take a look at regionmask (e.g. mask_3D). (Disclaimer: I am the author). However, the You could use rasterio (see rasterio.features.rasterize) directly, but have to be careful with overlapping regions. (So you might be better off using the private internal regionmask function: |
Beta Was this translation helpful? Give feedback.
-
@vboussange not sure what you've tried so far. But so long as your dataset is not very large and therefore backed by a numpy array, you can use dask to parallelize your for-loop (https://tutorial.dask.org/03_dask.delayed.html#A-Typical-Workflow). Performance improvement will depend on your local resources (e.g. number of cores) as dask uses a single machine threaded scheduler by default (https://docs.dask.org/en/stable/scheduling.html). In any case, this pattern would work with any library if you choose to try other options for the logic within import dask
from dask.diagnostics import ProgressBar
@dask.delayed
def process_gdf(pol, dataset):
cropped_dataset = dataset.rio.clip([pol], dataset.rio.crs, all_touched=True)
numpy_array = np.stack([
cropped_dataset[var].values.reshape(-1)
for var in cropped_dataset.variables
if var not in cropped_dataset.coords
])
return numpy_array
lazy_results = []
for pol in polygons:
lazy_results.append(process_gdf(pol, dataset))
with ProgressBar():
env_pred = dask.compute(*lazy_results) |
Beta Was this translation helpful? Give feedback.
-
Hi all,
I am new to xarray, and would gladly appreciate your help.
I have a list of polygons, and an
xarray.Dataset
, call itdataset
. For each polygon, I want to extract thedataset
values which coordinates intersect with the polygon. For this task, I loop over each polygon, and usedataset.rio.clip
. Here is a MWE, which works nicely.However, because my list
polygons
can be (very) long, I would like to parallelise this code. Any ideas on how to do it? Does a vectorised function exist somewhere? My attempts withjoblib
ordask
have proven unsuccessful. Thanks in advance for your input!Beta Was this translation helpful? Give feedback.
All reactions