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

workaround for file with variable and dimension having same name #4456

Closed
johnomotani opened this issue Sep 24, 2020 · 4 comments
Closed

workaround for file with variable and dimension having same name #4456

johnomotani opened this issue Sep 24, 2020 · 4 comments

Comments

@johnomotani
Copy link
Contributor

Adding a variable that's not a 1d "dimension coordinate" with the same name as a dimension is an error. This makes sense. However, if I have a .nc file that has such a variable, is there any workaround to get the badly-named variable into xarray short of altering the .nc file or loading it separately with netCDF4? I.e. to make the following work somehow

import xarray as xr
import netCDF4

f = netCDF4.Dataset()
f = netCDF4.Dataset("test.nc", "w")
f.createDimension("x", 2)
f.createDimension("y", 3)
f["y"] = np.ones([2,3])
f["y"][...] = 1.0
f.close()

ds = xr.open_dataset('test.nc')

rather than getting the current error MissingDimensionsError: 'y' has more than 1-dimension and the same name as one of its dimensions ('x', 'y'). xarray disallows such variables because they conflict with the coordinates used to label dimensions.

I think it might be nice to have something like a rename_vars argument to open_dataset(). Similar to how drop_vars ignores a list of variables, rename_vars could rename a dict of variables so the example above could do

ds = xr.open_dataset("test.nc", rename_vars={"y": "y_not_dimension"})

and get a Dataset with a dimension "y" and a variable "y_not_dimension".

@kmuehlbauer
Copy link
Contributor

Is there any news on that topic? Has something being implemented already, to solve that issue? I'm wondering if roundtripping might be possible at all. As those files are valid nc-files, shouldn't xarray be capable of handling them?

@johnomotani
Copy link
Contributor Author

I didn't find any solution in xarray - I think I ended up just dropping the conflicting variable...

@kmuehlbauer
Copy link
Contributor

Thanks @johnomotani me neither. I wonder if xarray could just invent the same as netcdf4 did, prepending the variables internally with some prefix. But this might only get confusing.

@dcherian
Copy link
Contributor

Closing as dupe of #2233. This should be fixed by the explicit indexes refactor.

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

3 participants