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

ValueError: Dimensions {'bounds'} do not exist. Expected one or more of ('lon', 'bnds') #120

Open
colinmaxime opened this issue Jul 9, 2021 · 1 comment

Comments

@colinmaxime
Copy link

I've been trying to use xESMF to regrid an existing dataset to a coarser grid taken from a different example file. I've tried many variations but I have not managed to solve the problem... The original dataset has a non-coordinate dimension called "bnds" which has a value of 2, whereas the new dataset doesn't have it since no variable of interest uses "bnds". Here is my python3 script:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
import xarray as xr
import xesmf as xe
from netCDF4 import Dataset
import h5py

import shutil
import subprocess as sub
import os
import glob
import sys

years_list = [2000,2001,2002,2003,2004,2005,2006,2007,2008,2009]

data_DIR_root = '/my_path1/'
output_DIR_root = '/my_path2/'

reference_file_for_regridding = '/my_path3/file_pluc_Newloc.nc'
reference_file_for_regridding_name_only = os.path.basename(reference_file_for_regridding)

data_DIR = data_DIR_root
list_of_files = glob.glob(data_DIR + 'gpm_3imerg_v06b_20*.nc')
if not os.path.exists('./xESMF_regrid/'):
os.makedirs('./xESMF_regrid/')

for file in list_of_files:
print( "WORKING ON FILE " + file)

file_name_only = os.path.basename(file)
file_name_only_no_ext = os.path.splitext(file_name_only)[0]

output_file = os.path.join(output_DIR_root, 'xESMF_regrid', '%s_xESMF_regridded.nc' % (file_name_only_no_ext) )
output_file_name_only = os.path.basename(output_file)


ds = xr.open_dataset(file)
dr = ds['precipitationCal']
ds.rename_dims({'bnds': 'bounds'})
print(ds)

ds_ref = xr.open_dataset(reference_file_for_regridding)
ds_ref_lat = ds_ref.lat
ds_ref_lon = ds_ref.lon
print(ds_ref_lat)

ds_out = xr.Dataset({'lat': (['lat'], ds_ref_lat),
                     'lon': (['lon'], ds_ref_lon),
                    }
                   )
print(ds_out)

regridder = xe.Regridder(ds, ds_out, 'conservative', periodic=True)
dr_out = regridder(dr)
dr_out.to_netcdf(output_file)

And I get the following error message:

[gadi-login-03.gadi.nci.org.au:2763401] shmem: posix: file name search - max attempts exceeded.cannot continue with posix.
[gadi-login-03.gadi.nci.org.au:2763702] shmem: posix: file name search - max attempts exceeded.cannot continue with posix.
Traceback (most recent call last):
File "regridding_dataset_with_xESMF_v1.py", line 63, in
regridder = xe.Regridder(ds, ds_out, 'conservative', periodic=True)
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-21.01/lib/python3.8/site-packages/xesmf/frontend.py", line 689, in init
grid_in, shape_in, input_dims = ds_to_ESMFgrid(
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-21.01/lib/python3.8/site-packages/xesmf/frontend.py", line 129, in ds_to_ESMFgrid
lon_b, lat_b = _get_lon_lat_bounds(ds)
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-21.01/lib/python3.8/site-packages/xesmf/frontend.py", line 75, in _get_lon_lat_bounds
lon_b = cfxr.bounds_to_vertices(lon_bnds, 'bounds', order=None)
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-21.01/lib/python3.8/site-packages/cf_xarray/helpers.py", line 53, in bounds_to_vertices
values = bounds.transpose(bnd_dim, *old_dims).data
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-21.01/lib/python3.8/site-packages/xarray/core/dataarray.py", line 2210, in transpose
dims = tuple(utils.infix_dims(dims, self.dims, missing_dims))
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-21.01/lib/python3.8/site-packages/xarray/core/utils.py", line 788, in infix_dims
existing_dims = drop_missing_dims(dims_supplied, dims_all, missing_dims)
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-21.01/lib/python3.8/site-packages/xarray/core/utils.py", line 879, in drop_missing_dims
raise ValueError(
ValueError: Dimensions {'bounds'} do not exist. Expected one or more of ('lon', 'bnds')

The file to regrid can be downloaded from the hyrax server (https://gpm1.gesdisc.eosdis.nasa.gov/opendap/hyrax/GPM_L3/GPM_3IMERGHH.06/2020/001/contents.html), using the opendap protocol, in netcdf4 format instead of the original HDF5.

The reference file from which to take the new grid can be found at https://wetransfer.com/downloads/f017ea89c61431310b3889e4897c56f120210709035420/a8b5f112e6b89266597bc5415865112020210709035729/6be6f6

aulemahal pushed a commit to Ouranosinc/xESMF that referenced this issue Oct 4, 2021
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/pre-commit/pre-commit-hooks: v3.3.0 → v4.0.1](pre-commit/pre-commit-hooks@v3.3.0...v4.0.1)
- https://github.com/ambv/blackhttps://github.com/psf/black
- [github.com/psf/black: 20.8b1 → 21.9b0](psf/black@20.8b1...21.9b0)
- https://gitlab.com/pycqa/flake8https://github.com/PyCQA/flake8
- [github.com/PyCQA/flake8: 3.8.4 → 3.9.2](PyCQA/flake8@3.8.4...3.9.2)
- https://github.com/pre-commit/mirrors-isorthttps://github.com/PyCQA/isort
- [github.com/PyCQA/isort: v5.6.4 → 5.9.3](PyCQA/isort@v5.6.4...5.9.3)
- https://github.com/prettier/pre-commithttps://github.com/pre-commit/mirrors-prettier
- [github.com/pre-commit/mirrors-prettier: 57f39166b5a5a504d6808b87ab98d41ebf095b46 → v2.4.1](pre-commit/mirrors-prettier@57f3916...v2.4.1)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@rabernat
Copy link

This version of the package is no longer maintained. Would you mind moving your issue to https://github.com/pangeo-data/xESMF?

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

2 participants