JiaweiZhuang/xESMF

ValueError: {'longitude', 'latitude'} are not variables in the underlying object. {'longitude', 'latitude'} are dimensions with no index.

pramodadhikari opened this issue · 0 comments

I am trying to regrid from 4km to 9km grid using the conservative method. The bilinear method works fine but the conservative method shows me the value error as:
ValueError: {'longitude', 'latitude'} are not variables in the underlying object. {'longitude', 'latitude'} are dimensions with no index.

The original array did not have the lat_bounds and lon_bounds. I added them using:
`
lon_name = ds.cf['lon'].name
lat_name = ds.cf['lat'].name
ds = ds.cf.add_bounds([lon_name, lat_name])
lon_bnds = ds.cf.get_bounds('lon')
lat_bnds = ds.cf.get_bounds('lat')
lon_b = cfxr.bounds_to_vertices(lon_bnds, ds.cf.get_bounds_dim_name('lon'), order=None)
lat_b = cfxr.bounds_to_vertices(lat_bnds, ds.cf.get_bounds_dim_name('lat'), order=None)
del ds['lon_bounds'], ds['lat_bounds']
ds = ds.assign_coords({"lon_b": lon_b, "lat_b": lat_b})

`
Now the ds has:
Coordinates:
lat (lat) float64 24.08 24.12 24.17 ... 49.87 49.92
array([24.083333, 24.125 , 24.166667, ..., 49.833333, 49.875 , 49.916667])
lon (lon) float64 -125.0 -125.0 ... -66.54 -66.5
array([-125. , -124.958333, -124.916667, ..., -66.583333, -66.541667, -66.5 ])
lon_b (lon_vertices) float64 -125.0 -125.0 ... -66.52 -66.48
array([-125.02083333, -124.97916667, -124.9375 , ..., -66.5625 , -66.52083333, -66.47916667])
lat_b (lat_vertices) float64 24.06 24.1 24.15 ... 49.9 49.94
array([24.0625 , 24.10416667, 24.14583333 , ..., 49.8125 , 49.85416667, 49.89583333, 49.9375 ])

and
Data Variables:
Band1 (lat, lon) float32 ...

con_regridder = xe.Regridder(ds, ds_out, 'conservative')`
shows the following error:

KeyError Traceback (most recent call last)
File.../lib/python3.9/site-packages/xesmf/frontend.py:70, in _get_lon_lat_bounds(ds)
69 try:
---> 70 lon_bnds = ds.cf.get_bounds('longitude')
71 lat_bnds = ds.cf.get_bounds('latitude')

File .../lib/python3.9/site-packages/cf_xarray/accessor.py:2256, in CFDatasetAccessor.get_bounds(self, key)
2255 if not results:
-> 2256 raise KeyError(f"No results found for {key!r}.")
2258 return self._obj[results[0] if len(results) == 1 else results]

KeyError: "No results found for 'longitude'."

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
Cell In[366], line 1
----> 1 regridder = xe.Regridder(ds, ds_out, 'conservative')

File .../lib/python3.9/site-packages/xesmf/frontend.py:779, in Regridder.init(self, ds_in, ds_out, method, locstream_in, locstream_out, periodic, **kwargs)
777 grid_out, shape_out, _ = ds_to_ESMFlocstream(ds_out)
778 else:
--> 779 grid_out, shape_out, _ = ds_to_ESMFgrid(ds_out, need_bounds=need_bounds)
781 # Create the BaseRegridder
782 super().init(grid_in, grid_out, method, input_dims=input_dims, **kwargs)

File .../lib/python3.9/site-packages/xesmf/frontend.py:138, in ds_to_ESMFgrid(ds, need_bounds, periodic, append)
135 grid = Grid.from_xarray(lon.T, lat.T, periodic=periodic, mask=None)
137 if need_bounds:
--> 138 lon_b, lat_b = _get_lon_lat_bounds(ds)
139 lon_b, lat_b = as_2d_mesh(np.asarray(lon_b), np.asarray(lat_b))
140 add_corner(grid, lon_b.T, lat_b.T)

File .../lib/python3.9/site-packages/xesmf/frontend.py:78, in _get_lon_lat_bounds(ds)
76 lon_name = ds.cf['longitude'].name
77 lat_name = ds.cf['latitude'].name
---> 78 ds = ds.cf.add_bounds([lon_name, lat_name])
79 lon_bnds = ds.cf.get_bounds('longitude')
80 lat_bnds = ds.cf.get_bounds('latitude')

File .../lib/python3.9/site-packages/cf_xarray/accessor.py:2344, in CFDatasetAccessor.add_bounds(self, keys, dim, output_dim)
2342 if dims_no_idx:
2343 msg += f" {dims_no_idx!r} are dimensions with no index."
-> 2344 raise ValueError(msg)
2346 for var in variables:
2347 bname = f"{var}_bounds"

ValueError: {'longitude', 'latitude'} are not variables in the underlying object. {'longitude', 'latitude'} are dimensions with no index.