ks905383 / xagg

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

weights on same grid as data seems to still require xesmf #86

Closed thurber closed 1 month ago

thurber commented 1 month ago

When trying to test a randomly weighted grid of temperature data aggregated to US counties, I encounter an error stating that xesmf is required for data and weights on different grids, but the grids are the same. See traceback below. When I compare the grid sizes manually, it is unclear to me how the error is able to trigger; perhaps the preprocessing is affecting it somehow.

Data:

image

Code:

# Get overlap between pixels and polygons
weightmap = xa.pixel_overlaps(
    tas[['band_data']],
    county_shapes,
    weights=tas.weight
)

# Aggregate data in [ds] onto polygons
aggregated = xa.aggregate(tas, weightmap)

Traceback:


ImportError Traceback (most recent call last) Cell In[133], line 2 1 # Get overlap between pixels and polygons ----> 2 weightmap = xa.pixel_overlaps( 3 tas[['band_data']], 4 county_shapes, 5 weights=tas.weight 6 ) 8 # Aggregate data in [ds] onto polygons 9 aggregated = xa.aggregate(tas, weightmap)

File venv/lib/python3.12/site-packages/xagg/wrappers.py#line=88, in pixel_overlaps(ds, gdf_in, weights, weights_target, subset_bbox, impl, silent) 87 print('creating polygons for each pixel...') 88 if subset_bbox: ---> 89 pix_agg = create_raster_polygons(ds,subset_bbox=gdf_in,weights=weights,silent=silent) 90 if not silent: 91 if pix_agg['gdf_pixels'].empty:

File venv/lib/python3.12/site-packages/xagg/core.py#line=309, in create_raster_polygons(ds, mask, subset_bbox, weights, weights_target, wrap_around_thresh, silent) 307 warnings.warn('[subset_bbox] is not a geodataframe; no mask by polygon bounding box used.') 309 # Process weights --> 310 ds,winf = process_weights(ds,weights,target=weights_target) 312 # Mask 313 if mask is not None:

File venv/lib/python3.12/site-packages/xagg/core.py#line=172, in process_weights(ds, weights, target, silent) 167 if ((not ((ds.sizes['lat'] == weights.sizes['lat']) and (ds.sizes['lon'] == weights.sizes['lon']))) or 168 (not (np.allclose(ds.lat,weights.lat) and np.allclose(ds.lon,weights.lon)))): 169 # Import xesmf here to allow the code to work without it (it 170 # often has dependency issues and isn't necessary for many 171 # features of xagg) 172 if not _has_xesmf: --> 173 raise ImportError('If the weights grid and the ds grid are different, '+ 174 'xesmf is needed for xagg to regrid them to match; however, '+ 175 'xesmf is not installed. Either install xesmf or '+ 176 'manually regrid them to match each other.') 177 if target == 'ds': 178 if not silent:

ImportError: If the weights grid and the ds grid are different, xesmf is needed for xagg to regrid them to match; however, xesmf is not installed. Either install xesmf or manually regrid them to match each other.

Testing the above logical statement manually:

image

Related to JOSS review: https://github.com/openjournals/joss-reviews/issues/7239

ks905383 commented 1 month ago

Yeah fair - that definitely shouldn't be happening. Could you share the dataset that you ran into this with?

thurber commented 1 month ago

xagg_joss_review.zip

Here's a sample with a netcdf with a band_data temperature variable and a random weight variable, and a geojson with counties in WA state.