ks905383 / xagg

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

dimension mismatch upon running pixel_overlaps #50

Closed ccallahan45 closed 10 months ago

ccallahan45 commented 1 year ago

Hello!

I attempted to run xa.pixel_overlaps using tropical cyclone wind data and a shapefile (GeoDataFrame) of US state boundaries. I received the following error: "ValueError: new dimensions ('lat', 'bnds') must be a superset of existing dimensions ('lat', 'bnd')".

Based on the traceback, this appears to be occurring when the latitude and longitude bounds are created. The creation of the "bnds_tmp" variable appears to concatenate a variable with the dimension "bnds" to a variable with the dimension "bnd."

Full error message is included here:

image

thanks!

mcruzloya commented 1 year ago

I'm running into a similar issue with a different dataset (temperature and precipitation data) when trying to aggregate over US counties with a shapefile. The same files used to work fine on a previous version of xagg.

oscarbau commented 1 year ago

Same issue here

ks905383 commented 1 year ago

Hey all - sorry for the delay. Could one of you post example files that cause this issue? I'm having trouble reproducing the bug locally. (and let me know what version of xagg you're using)

ccallahan45 commented 1 year ago

Here are a couple of files and a minimum working example that (at least for me) produced the bug. I'm using xagg version 0.3.1 installed from conda-forge. xagg_mwe.zip

simonmoulds commented 1 year ago

I think the problem may be lines 230-237 of xagg/aux.py:

            bnds_tmp = xr.DataArray(data=np.zeros((ds.dims[var],2))*np.nan,
                                    dims=[var,'bnds'],
                                    coords=[ds[var],np.arange(0,2)])

            # Assign all non-edge bounds as just half of the distance from the center
            # of each pixel to the center of the next pixel
            bnds_tmp[1:,:] = xr.concat([ds[var]-0.5*ds[var].diff(var),
                                          ds[var]+0.5*ds[var].diff(var)],dim='bnd').transpose(var,'bnd')

Note the 'bnds' dimension to create bnds_tmp, then referred to as 'bnd' when it's assigned.

ks905383 commented 1 year ago

Yeah that looks plausible, thanks @simonmoulds @ccallahan45 . The really weird thing is I still can't replicate the problem locally, even with @ccallahan45 's replication data.

Running xa.aux.get_bnds() with and without the change to bnds in 230-237 doesn't change anything for me (the following code block runs fine, for example). This is weird, because the second bnds_tmp[1:,:] = with bnd coordinates instead of bnds below should be failing on my machine, but something in xarray is letting that go through.

bnds_tmp[1:,:] = xr.concat([ds[var]-0.5*ds[var].diff(var),
                                      ds[var]+0.5*ds[var].diff(var)],dim='bnds').transpose(var,'bnds')
a = bnds_tmp.copy()

bnds_tmp[1:,:] = xr.concat([ds[var]-0.5*ds[var].diff(var),
                                      ds[var]+0.5*ds[var].diff(var)],dim='bnd').transpose(var,'bnd')

b = bnds_tmp.copy()

xr.testing.assert_allclose(a,b)

In any case, let me fix those lines, and hopefully that'll fix this issue.

ks905383 commented 1 year ago

Could someone for whom this error occurs locally test the new release #52 locally and make sure it works? Thanks!

simonmoulds commented 1 year ago

Thanks @ks905383 - this is now working for me.

helsharif commented 10 months ago

Hello, I was running into the same issue mentioned in this thread, and didn't realize that I didn't have the latest version of xagg installed. I used the installation instructions on the github page: conda install -c conda-forge xagg==0.3.1

But I see the version of xagg with the fix is version 0.3.1.1

I recommend to please update the github page installation instructions

ks905383 commented 10 months ago

Fixed, thanks for pointing that out.