ks905383 / xagg

Aggregating gridded data (xarray) to polygons
https://xagg.readthedocs.io/
GNU General Public License v3.0
82 stars 15 forks source link

Problem generating weights with DAYMET data #16

Open mewieczo opened 3 years ago

mewieczo commented 3 years ago

DAYMET data has lat and long that is 2 dimensional and x and y that are 1 dimensional. I dropped lat and long, and when running pixel overlap I get the following error:

ValueError: cannot reindex or align along dimension 'lon' because the index has duplicate values

Any suggestions or help is appreciated. Mike

rsignell-usgs commented 3 years ago

@ks905383: Could it be that xagg currently only works for gridded datasets that have 1D lon,lat coordinates?

The DAYMET data has 1D (x,y) Lambert Conformal Conic projected coordinates, so the (lon,lat) arrays are 2D.

ks905383 commented 3 years ago

Yeah - it's currently only tested / written for rectangular grids.

Expanding processing to work with non-rectangular grids should not be the most complicated thing in the world, and might be achievable mainly through expanding the functionality of create_raster_polygons. Maybe let's keep this open until that's done.

In the meantime, I'll see about putting in a helpful error message if a non-rectangular grid is detected, or one where the coordinates aren't the dimensions etc.

rsignell-usgs commented 3 years ago

@ks905383, okay, it's not just user error. Good! 😄 Do you know if the rasterstats package might work for the use case of @mewieczo?

rmcd-mscb commented 3 years ago

Here is an example of how I generated cells for Daymet grids. It might be of some help.

https://github.com/nhm-usgs/onhm-fetcher-parser/blob/master/pkg/CalcDaymetWeights.py

ks905383 commented 3 years ago

@ks905383, okay, it's not just user error. Good! 😄 Do you know if the rasterstats package might work for the use case of @mewieczo?

hm... it might. I personally haven't used rasterstats, but their method seems relatively robust (any pixel within the polygon boundary is included, either by center or by any part of the pixel), as long as the crs'es are well-defined? You won't get the area-averaging functionality of xagg, but depending on how much accuracy you need (i.e. how big are your pixels relative to your polygons, how bad is it if a pixel that only barely touches a polygon is included in the weighting) you might not need it anyways.

ks905383 commented 3 years ago

Here is an example of how I generated cells for Daymet grids. It might be of some help.

https://github.com/nhm-usgs/onhm-fetcher-parser/blob/master/pkg/CalcDaymetWeights.py

oh nice, thanks for that - I'll take a look

rmcd-mscb commented 2 years ago

@ks905383 would be interested in helping add capability for processing structured non-uniform grids to xagg Kevin. I've got code to generate lat_bnds and lon_bnds. Wondering if you would have time and interest to meet and discuss how it would make sense to you to add that capability? Cheers Kevin.

ks905383 commented 2 years ago

Hey @rmcd-mscb , apologies for the long radio silence here.

That actually sounds great - we could chat later this week or next week and see about integrating it?

rmcd-mscb commented 2 years ago

Thanks @ks905383 - I sent a calendar invite.

ks905383 commented 2 years ago

Hopefully, all major processing changes would only have to be in create_raster_polygons and get_bnds .

The remaining issues beyond that are largely semantic, in particular related to hardcoded calls to lat, lon. This may be fixable through implementing cf_xarray in xagg (Issue #1). Here's a quick (non-comprehensive) list of parts of the code where this might cause issues:

In xa.core

In xa.aux

rsignell-usgs commented 2 years ago

I was thinking about using xagg with DAYMET again, and thinking, "okay, it doesn't have 1D lon,lat coordinates, but it does have 1D x,y, coordinates" (lambert conformal projection, see this notebook), so I'm wondering if I could just:

I'm thinking the real limitation is the algorithm that needs 1D coordinates....

What would be the problems with this approach?