# Memory Error using xvec.zonal\_stats

**URL:** <https://discourse.pangeo.io/t/memory-error-using-xvec-zonal-stats/5151>\
**Category:** Science\
**Tags:** zarr\
**Created:** [May 19, 2025, 9:40pm UTC](https://discourse.pangeo.io/t/memory-error-using-xvec-zonal-stats/5151 "2025-05-19T21:40:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlummus](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@mlummus](https://discourse.pangeo.io/u/mlummus)\
**Post date:** [May 19, 2025, 9:40pm UTC](https://discourse.pangeo.io/t/memory-error-using-xvec-zonal-stats/5151/1 "2025-05-19T21:40:55Z")

</div>

Hello! I’m trying to use xvec zonal statistics to calculate the mean snow fraction for each month in a 20 year timespan in 934 hydrologic basin geometries. My workstation has **64 GB of memory**. The main dataset I am using is a 81 GB zarr file with coordinates of time, x, and y and a variable called snow\_fraction. The snow\_fraction is chunked as (252, 1000, 1000) and each chunk is 0.94 GB. The dataset is 96 chunks in 3 graph layers.

I then use xvec.zonal\_stats to find the mean snow fraction in each basin. The result is a dataset that has chunksize (geometry: 1, time: 252). This dramatically increases the number of graph layers to 8410.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/pangeo/original/2X/6/68c56c72afcccef90ec508c516379bfd937ea4ad.png)

I rechunk the aggregated mean into one big chunk (geometry: 934, time: 252). Not sure if this was necessary, but the chunks were so small and I thought it would be more efficient to increase chunk size.

Then I encode the dataset as recommended in the xvec reading and writing files tutorial and try to save. The final encoded dataset is 919 kiB. I get a memory error when I try to save it out as a .nc file.

```auto
encoded.to_netcdf(path = "F:/EnDI/EnDI_data/snow_data/snow_fraction.nc", mode = "w")

```

> MemoryError: Unable to allocate 2.75 MiB for an array with shape (16, 150, 300) and data type float32

I’m not sure why I am getting this memory error when the final dataset that I am trying to save is so small. Is there something else I should have done to make the zonal statistics step more memory safe?

[Here is a link to my notebook](https://github.com/mlummus/Cryo_Hydro_EnDI/blob/main/zonal_statistics_snow.ipynb)

---

<div class="post-metadata">

**Author:** ![mlummus](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@mlummus](https://discourse.pangeo.io/u/mlummus)\
**Post date:** [May 19, 2025, 9:45pm UTC](https://discourse.pangeo.io/t/memory-error-using-xvec-zonal-stats/5151/2 "2025-05-19T21:45:53Z")

</div>

This is the snow\_fraction dataset for those that are curious.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/pangeo/original/2X/d/da19827529f5c640b11ef779604ebcfc5006ec27.png)

Here are the xvec tutorials that I am working from: [zonal\_stats](https://xvec.readthedocs.io/en/stable/zonal_stats.html) and [reading and writing files](https://xvec.readthedocs.io/en/stable/io.html).
