Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regridding successful with "bilinear" method but throws error with "conservative" method #140

Open
alyssa-stansfield opened this issue Jun 3, 2024 · 1 comment

Comments

@alyssa-stansfield
Copy link

I am working on regridding precipitation on a regional WRF grid to the rectilinear GPM IMERG grid. The problem is when the regridding method is set to "bilinear", the regridding works, but when I change the method to "conservative", it fails with this error:

KeyError: "Dataset.cf does not understand the key 'longitude'. Use 'repr(Dataset.cf)' (or 'Dataset.cf' in a Jupyter environment) to see a list of key names that can be interpreted."

None of the variables are named "longitude" so I'm confused about what this error means and how just switching the regridding method raises it. Below is my script, where xe.Regridder fails.

import xarray as xr

import xesmf as xe

fin = xr.open_dataset("CONUS404_6hourly_2022083118_TCprecip_values.nc")
sf = fin.rename(name_dict={"XLAT":"lat", "XLONG":"lon"})
print (sf)

fout = xr.open_dataset("/glade/derecho/scratch/alyssas/FROM_CHEYENNE/IMERG/IMERG_6hourly_2022062212.nc")
fout.drop_vars(["PRECT"])

print (fout)

regridder = xe.Regridder(sf,fout,"conservative")

Below is the full error message: Traceback (most recent call last):
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/xarray/core/dataset.py", line 1393, in _construct_dataarray
variable = self._variables[name]
KeyError: 'longitude'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/cf_xarray/accessor.py", line 838, in _getitem
extravars = accessor.get_associated_variable_names(
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/cf_xarray/accessor.py", line 1722, in get_associated_variable_names
attrs_or_encoding = ChainMap(self._obj[name].attrs, self._obj[name].encoding)
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/xarray/core/dataset.py", line 1484, in getitem
return self._construct_dataarray(key)
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/xarray/core/dataset.py", line 1395, in _construct_dataarray
_, name, variable = _get_virtual_variable(self._variables, name, self.dims)
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/xarray/core/dataset.py", line 196, in _get_virtual_variable
raise KeyError(key)
KeyError: 'longitude'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/xesmf/frontend.py", line 69, in _get_lon_lat_bounds
lon_bnds = ds.cf.get_bounds('longitude')
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/cf_xarray/accessor.py", line 2285, in get_bounds
results = self[[key]].cf.bounds.get(key, [])
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/cf_xarray/accessor.py", line 2173, in getitem
return _getitem(self, key)
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/cf_xarray/accessor.py", line 881, in _getitem
raise KeyError(
KeyError: "Dataset.cf does not understand the key 'longitude'. Use 'repr(Dataset.cf)' (or 'Dataset.cf' in a Jupyter environment) to see a list of key names that can be interpreted."

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/xarray/core/dataset.py", line 1393, in _construct_dataarray
variable = self._variables[name]
KeyError: 'longitude'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/cf_xarray/accessor.py", line 838, in _getitem
extravars = accessor.get_associated_variable_names(
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/cf_xarray/accessor.py", line 1722, in get_associated_variable_names
attrs_or_encoding = ChainMap(self._obj[name].attrs, self._obj[name].encoding)
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/xarray/core/dataset.py", line 1484, in getitem
return self._construct_dataarray(key)
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/xarray/core/dataset.py", line 1395, in _construct_dataarray
_, name, variable = _get_virtual_variable(self._variables, name, self.dims)
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/xarray/core/dataset.py", line 196, in _get_virtual_variable
raise KeyError(key)
KeyError: 'longitude'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/glade/derecho/scratch/alyssas/CONUS404/precip_files/test.py", line 17, in
regridder = xe.Regridder(sf,fout,"conservative")
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/xesmf/frontend.py", line 779, in init
grid_in, shape_in, input_dims = ds_to_ESMFgrid(
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/xesmf/frontend.py", line 136, in ds_to_ESMFgrid
lon_b, lat_b = _get_lon_lat_bounds(ds)
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/xesmf/frontend.py", line 72, in _get_lon_lat_bounds
if ds.cf['longitude'].ndim > 1:
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/cf_xarray/accessor.py", line 2173, in getitem
return _getitem(self, key)
File "/glade/u/apps/opt/conda/envs/npl/lib/python3.10/site-packages/cf_xarray/accessor.py", line 881, in _getitem
raise KeyError(
KeyError: "Dataset.cf does not understand the key 'longitude'. Use 'repr(Dataset.cf)' (or 'Dataset.cf' in a Jupyter environment) to see a list of key names that can be interpreted."

@alyssa-stansfield
Copy link
Author

I've figured out this is because my input file (a WRF model output file) does not have any information about the grid corners, which is required for conservative regridding. Adding a function that automatically estimates the grid corners might be nice. I know the NCL regridding functions do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant