ks905383 / xagg

Aggregating gridded data (xarray) to polygons

Home Page:https://xagg.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Option to return sum instead of mean?

bradyrx opened this issue · comments

Is it feasible with the current code structure to have an option to return the sum over a polygon instead of the mean? I.e., something like http://xarray.pydata.org/en/stable/generated/xarray.core.weighted.DataArrayWeighted.sum.html instead of http://xarray.pydata.org/en/stable/generated/xarray.core.weighted.DataArrayWeighted.mean.html.

The rasterstats zonal statistics module allows one to return mean, sum, etc: https://pythonhosted.org/rasterstats/manual.html#zonal-statistics.

I realize this might not be super straight-forward since you can't guarantee the preservation of the integral. Since your package looks at how any pixels overlap with the shapefile, you might be reusing some pixels. Or maybe not, since the idea is that you're calculating the percentage overlap here, so even a small shapefile would only grab a small percentage of a given pixel.

I'm thinking this is useful if one wants to convert from something like total precipitation on a raster grid to total/accumulated precipitation over a larger shapefile.

I realize this might not be super straight-forward since you can't guarantee the preservation of the integral. Since your package looks at how any pixels overlap with the shapefile, you might be reusing some pixels. Or maybe not, since the idea is that you're calculating the percentage overlap here, so even a small shapefile would only grab a small percentage of a given pixel.

I had considered doing this (I think I have a local branch that allows sum calculations somewhere), but I haven't stuck it into the main package for exactly this question. rasterstats (and other packages that are doing similar things, like regionmask's aggregating module) don't do area overlaps, so I wouldn't be as worried about that mass-preservation question, but here I'm worried that there's no good way to deal with that?