ks905383 / xagg

Aggregating gridded data (xarray) to polygons

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

work with xr.DataArray's

raybellwaves opened this issue · comments

In providing an xr.DataArray to xa.pixel_overlaps(da, gdf) you get the Traceback below.

Couple of ideas for fixes:

  • in the code parse it to an xr.Dataset
  • Don't use .keys() but use .dims() instead
AttributeError                            Traceback (most recent call last)
<ipython-input-74-f5cd39618cec> in <module>
----> 1 weightmap = xa.pixel_overlaps(da, gdf)

/opt/userenvs/ray.bell/main/lib/python3.9/site-packages/xagg/wrappers.py in pixel_overlaps(ds, gdf_in, weights, weights_target, subset_bbox)
     58     print('creating polygons for each pixel...')
     59     if subset_bbox:
---> 60         pix_agg = create_raster_polygons(ds,subset_bbox=gdf_in,weights=weights)
     61     else:
     62         pix_agg = create_raster_polygons(ds,subset_bbox=None,weights=weights)

/opt/userenvs/ray.bell/main/lib/python3.9/site-packages/xagg/core.py in create_raster_polygons(ds, mask, subset_bbox, weights, weights_target)
    148     # Standardize inputs
    149     ds = fix_ds(ds)
--> 150     ds = get_bnds(ds)
    151     #breakpoint()
    152     # Subset by shapefile bounding box, if desired

/opt/userenvs/ray.bell/main/lib/python3.9/site-packages/xagg/aux.py in get_bnds(ds, edges, wrap_around_thresh)
    196         # to [0,360], but it's not tested yet.
    197 
--> 198     if ('lat' not in ds.keys()) | ('lon' not in ds.keys()):
    199         raise KeyError('"lat"/"lon" not found in [ds]. Make sure the '+
    200                        'geographic dimensions follow this naming convention.')

/opt/userenvs/ray.bell/main/lib/python3.9/site-packages/xarray/core/common.py in __getattr__(self, name)
    237                 with suppress(KeyError):
    238                     return source[name]
--> 239         raise AttributeError(
    240             "{!r} object has no attribute {!r}".format(type(self).__name__, name)
    241         )

AttributeError: 'DataArray' object has no attribute 'keys'

Good point, especially since docs explicitly say DataArrays are fine to use.

Unfortunately, using .dims() won't work, since that mapping is different between Datasets and DataArrays, too.

As to your other suggestion, I think

if type(ds)==xr.core.dataarray.DataArray:
    ds = ds.to_dataset()

in xa.pixel_overlaps() should do it (since all the calls to .keys() show up in the functions that xa.pixel_overlaps() calls, and none in xa.aggregate())

Ok, should be fixed with #9 and will be included in the next release.