ks905383 / xagg

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

weightmap diagnostic requires lat/lon coordinate names #85

Closed thurber closed 1 month ago

thurber commented 1 month ago

I noticed when testing out an aggregation of raster data to US counties, that the aggregation works great even with coordinate names of x and y, but the weightmap diagnostic function throws an error unless the coordinates are renamed to lon and lat.

Traceback:


KeyError Traceback (most recent call last) ----> 1 weightmap.diag_fig(2020, tas)

venv/lib/python3.12/site-packages/xagg/classes.py in ?(self, poly_id, ds, fig, ax) 53 54 # Adjust grids between the input ds and the weightmap grid (in case subset to 55 # bbox was used) 56 with set_options(silent=True): ---> 57 ds = subset_find(ds,self.source_grid) 58 59 # Plot diagnostic figure 60 fig,ax=diag_fig(self,poly_id,ds,fig=fig,ax=ax)

venv/lib/python3.12/site-packages/xagg/auxfuncs.py in ?(ds0, ds1, silent) 409 if silent is None: 410 silent = get_options()['silent'] 411 412 if 'loc' not in ds0.sizes: --> 413 ds0 = ds0.stack(loc = ('lat','lon')) 414 was_stacked = True 415 else: 416 was_stacked = False

venv/lib/python3.12/site-packages/xarray/util/deprecation_helpers.py in ?(*args, *kwargs) 139 # Upgrade to DeprecationWarning in the future, when the renaming is complete. 140 PendingDeprecationWarning, 141 ) 142 kwargs["dim"] = kwargs.pop(old_name) --> 143 return func(args, **kwargs)

venv/lib/python3.12/site-packages/xarray/core/dataarray.py in ?(self, dim, create_index, index_cls, **dim_kwargs) 2903 See Also 2904 -------- 2905 DataArray.unstack 2906 """ -> 2907 ds = self._to_temp_dataset().stack( 2908 dim, 2909 create_index=create_index, 2910 index_cls=index_cls,

venv/lib/python3.12/site-packages/xarray/util/deprecation_helpers.py in ?(*args, *kwargs) 139 # Upgrade to DeprecationWarning in the future, when the renaming is complete. 140 PendingDeprecationWarning, 141 ) 142 kwargs["dim"] = kwargs.pop(old_name) --> 143 return func(args, **kwargs)

venv/lib/python3.12/site-packages/xarray/core/dataset.py in ?(self, dim, create_index, index_cls, **dim_kwargs) 5413 """ 5414 dim = either_dict_or_kwargs(dim, dim_kwargs, "stack") 5415 result = self 5416 for new_dim, dims in dim.items(): -> 5417 result = result._stack_once(dims, new_dim, index_cls, create_index) 5418 return result

venv/lib/python3.12/site-packages/xarray/core/dataset.py in ?(self, dims, new_dim, index_cls, create_index) 5343 new_coord_names = set(self._coord_names) 5344 if create_index or create_index is None: 5345 product_vars: dict[Any, Variable] = {} 5346 for dim in dims: -> 5347 idx, idx_vars = self._get_stack_index(dim, create_index=create_index) 5348 if idx is not None: 5349 product_vars.update(idx_vars) 5350

venv/lib/python3.12/site-packages/xarray/core/dataset.py in ?(self, dim, multi, create_index) 5299 if create_index and stack_index is None: 5300 if dim in self._variables: 5301 var = self.variables[dim] 5302 else: -> 5303 , _, var = _get_virtual_variable(self._variables, dim, self.sizes) 5304 # dummy index (only stack_coords will be used to construct the multi-index) 5305 stack_index = PandasIndex([0], dim) 5306 stack_coords = {dim: var}

venv/lib/python3.12/site-packages/xarray/core/dataset.py in ?(variables, key, dim_sizes) 214 raise KeyError(key) 215 216 split_key = key.split(".", 1) 217 if len(split_key) != 2: --> 218 raise KeyError(key) 219 220 ref_name, var_name = split_key 221 ref_var = variables[ref_name]

KeyError: 'lat'

Virtual environment:

Package Version


affine 2.4.0 appnope 0.1.4 asttokens 2.4.1 attrs 24.2.0 blosc2 2.7.1 Cartopy 0.24.1 certifi 2024.8.30 cf_xarray 0.9.5 cftime 1.6.4 click 8.1.7 click-plugins 1.1.1 cligj 0.7.2 comm 0.2.2 contourpy 1.3.0 cycler 0.12.1 debugpy 1.8.7 decorator 5.1.1 executing 2.1.0 fonttools 4.54.1 geopandas 1.0.1 ipykernel 6.29.5 ipython 8.28.0 jedi 0.19.1 jupyter_client 8.6.3 jupyter_core 5.7.2 kiwisolver 1.4.7 matplotlib 3.9.2 matplotlib-inline 0.1.7 msgpack 1.1.0 ndindex 1.9.2 nest-asyncio 1.6.0 netCDF4 1.7.1.post2 numexpr 2.10.1 numpy 2.1.2 packaging 24.1 pandas 2.2.3 parso 0.8.4 pexpect 4.9.0 pillow 11.0.0 pip 24.2 platformdirs 4.3.6 prompt_toolkit 3.0.48 psutil 6.0.0 ptyprocess 0.7.0 pure_eval 0.2.3 py-cpuinfo 9.0.0 Pygments 2.18.0 pyogrio 0.10.0 pyparsing 3.2.0 pyproj 3.7.0 pyshp 2.3.1 python-dateutil 2.9.0.post0 pytz 2024.2 pyzmq 26.2.0 rasterio 1.4.1 rioxarray 0.17.0 scipy 1.14.1 shapely 2.0.6 six 1.16.0 stack-data 0.6.3 tables 3.10.1 tornado 6.4.1 traitlets 5.14.3 typing_extensions 4.12.2 tzdata 2024.2 wcwidth 0.2.13 xagg 0.3.2.4 xarray 2024.9.0

This is related to JOSS review: https://github.com/openjournals/joss-reviews/issues/7239