Skip to content

vertices_to_bounds does not work without coordinate #602

@larsbuntemeyer

Description

@larsbuntemeyer

This is an example which worked before v0.10.7

import xarray as xr
import cf_xarray as cfxr

bounds = xr.DataArray([[0.0, 0.5], [0.5, 1.0]], dims=("lat", "bounds"))
cfxr.bounds_to_vertices(bounds, "bounds")

This results in

<xarray.DataArray (lat_vertices: 3)> Size: 24B
array([0. , 0.5, 1. ])
Dimensions without coordinates: lat_vertices

With the current main, i get:

ValueError: All core dimension orders must be aligned. Got orders: {}

I checked through the discussion in #579. I'm not sure if this was intentional to ensure correct ordering of bounds with respect to a corresponding coordinate. So this works:

import xarray as xr
import cf_xarray as cfxr

bounds = xr.DataArray(
    [[0.0, 0.5], [0.5, 1.0]], dims=("lat", "bounds"), coords={"lat": [0.25, 0.75]}
)
cfxr.bounds_to_vertices(bounds, bounds_dim="bounds")

and gives

<xarray.DataArray (lat_vertices: 3)> Size: 24B
array([0. , 0.5, 1. ])
Dimensions without coordinates: lat_vertices

full traceback

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[2], [line 7](vscode-notebook-cell:?execution_count=2&line=7)
      2 import cf_xarray as cfxr
      4 bounds = xr.DataArray(
      5     [[0.0, 0.5], [0.5, 1.0]], dims=("lat", "bounds")#, coords={"lat": [0.25, 0.[7](vscode-notebook-cell:?execution_count=2&line=7)5]}
      6 )
----> 7 cfxr.bounds_to_vertices(bounds, bounds_dim="bounds")

File ~/python/packages/cf-xarray/cf_xarray/helpers.py:184, in bounds_to_vertices(bounds, bounds_dim, core_dims, order)
    179 core_dim_coords = {
    180     dim: bounds.coords[dim].values for dim in core_dims if dim in bounds.coords
    181 }
    182 core_dim_orders = _get_core_dim_orders(core_dim_coords)
--> [184](https://file+.vscode-resource.vscode-cdn.net/Users/Buntemey/python/packages/cf-xarray/doc/examples/~/python/packages/cf-xarray/cf_xarray/helpers.py:184) return xr.apply_ufunc(
    185     _bounds_helper,
    186     bounds,
    187     input_core_dims=[core_dims + [bounds_dim]],
    188     dask="parallelized",
    189     kwargs={
    190         "n_core_dims": n_core_dims,
    191         "nbounds": nbounds,
    192         "order": order,
    193         "core_dim_orders": core_dim_orders,
    194     },
    195     output_core_dims=[output_core_dims],
    196     dask_gufunc_kwargs=dict(output_sizes=output_sizes),
    197     output_dtypes=[bounds.dtype],
    198 )

File ~/miniforge3/envs/cf_xarray/lib/python3.12/site-packages/xarray/computation/apply_ufunc.py:1267, in apply_ufunc(func, input_core_dims, output_core_dims, exclude_dims, vectorize, join, dataset_join, dataset_fill_value, keep_attrs, kwargs, dask, output_dtypes, output_sizes, meta, dask_gufunc_kwargs, on_missing_core_dim, *args)
   1265 # feed DataArray apply_variable_ufunc through apply_dataarray_vfunc
   1266 elif any(isinstance(a, DataArray) for a in args):
-> [1267](https://file+.vscode-resource.vscode-cdn.net/Users/Buntemey/python/packages/cf-xarray/doc/examples/~/miniforge3/envs/cf_xarray/lib/python3.12/site-packages/xarray/computation/apply_ufunc.py:1267)     return apply_dataarray_vfunc(
   1268         variables_vfunc,
   1269         *args,
   1270         signature=signature,
   1271         join=join,
   1272         exclude_dims=exclude_dims,
   1273         keep_attrs=keep_attrs,
   1274     )
   1275 # feed Variables directly through apply_variable_ufunc
   1276 elif any(isinstance(a, Variable) for a in args):

File ~/miniforge3/envs/cf_xarray/lib/python3.12/site-packages/xarray/computation/apply_ufunc.py:310, in apply_dataarray_vfunc(func, signature, join, exclude_dims, keep_attrs, *args)
    305 result_coords, result_indexes = build_output_coords_and_indexes(
    306     args, signature, exclude_dims, combine_attrs=keep_attrs
    307 )
    309 data_vars = [getattr(a, "variable", a) for a in args]
--> [310](https://file+.vscode-resource.vscode-cdn.net/Users/Buntemey/python/packages/cf-xarray/doc/examples/~/miniforge3/envs/cf_xarray/lib/python3.12/site-packages/xarray/computation/apply_ufunc.py:310) result_var = func(*data_vars)
    312 out: tuple[DataArray, ...] | DataArray
    313 if signature.num_outputs > 1:

File ~/miniforge3/envs/cf_xarray/lib/python3.12/site-packages/xarray/computation/apply_ufunc.py:818, in apply_variable_ufunc(func, signature, exclude_dims, dask, output_dtypes, vectorize, keep_attrs, dask_gufunc_kwargs, *args)
    813 elif vectorize:
    814     func = _vectorize(
    815         func, signature, output_dtypes=output_dtypes, exclude_dims=exclude_dims
    816     )
--> [818](https://file+.vscode-resource.vscode-cdn.net/Users/Buntemey/python/packages/cf-xarray/doc/examples/~/miniforge3/envs/cf_xarray/lib/python3.12/site-packages/xarray/computation/apply_ufunc.py:818) result_data = func(*input_data)
    820 if signature.num_outputs == 1:
    821     result_data = (result_data,)

File ~/python/packages/cf-xarray/cf_xarray/helpers.py:284, in _bounds_helper(values, n_core_dims, nbounds, order, core_dim_orders)
    281         vertex_vals = np.block([[bot_left, bot_right], [top_left, top_right]])
    282 elif n_core_dims == 1 and nbounds == 2:
    283     # Middle points case (1D lat/lon)
--> [284](https://file+.vscode-resource.vscode-cdn.net/Users/Buntemey/python/packages/cf-xarray/doc/examples/~/python/packages/cf-xarray/cf_xarray/helpers.py:284)     vertex_vals = _get_ordered_vertices(values, core_dim_orders)
    286 return vertex_vals

File ~/python/packages/cf-xarray/cf_xarray/helpers.py:330, in _get_ordered_vertices(bounds, core_dim_orders)
    289 def _get_ordered_vertices(
    290     bounds: np.ndarray, core_dim_orders: dict[str, str]
    291 ) -> np.ndarray:
    292     """
    293     Convert a bounds array of shape (..., N, 2) or (N, 2) into a 1D array of vertices.
    294 
   (...)    328         Array of vertices with shape (..., N+1) or (N+1,).
    329     """
--> [330](https://file+.vscode-resource.vscode-cdn.net/Users/Buntemey/python/packages/cf-xarray/doc/examples/~/python/packages/cf-xarray/cf_xarray/helpers.py:330)     order = _get_order_of_core_dims(core_dim_orders)
    332     if _is_bounds_monotonic(bounds):
    333         vertices = np.concatenate((bounds[..., :, 0], bounds[..., -1:, 1]), axis=-1)

File ~/python/packages/cf-xarray/cf_xarray/helpers.py:416, in _get_order_of_core_dims(core_dim_orders)
    413 orders = set(core_dim_orders.values())
    415 if len(orders) != 1:
--> [416](https://file+.vscode-resource.vscode-cdn.net/Users/Buntemey/python/packages/cf-xarray/doc/examples/~/python/packages/cf-xarray/cf_xarray/helpers.py:416)     raise ValueError(
    417         f"All core dimension orders must be aligned. Got orders: {core_dim_orders}"
    418     )
    420 order = next(iter(orders))
    422 return order

ValueError: All core dimension orders must be aligned. Got orders: {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions