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

Plotting accessor error for datasets with Dimension name matches coordinate names #8106

Closed
4 tasks done
blaylockbk opened this issue Aug 23, 2023 · 3 comments · Fixed by #8126
Closed
4 tasks done

Comments

@blaylockbk
Copy link

What happened?

I am working with NetCDF files produced from the Model Evaluation Tools (MET).

This feature in xarray v2023.8.0

allows reading of datasets where a dimension name is associated with a multidimensional variable

lets me read the file where the dimension names lat and lon match the coordinate names lat lon (see dtcenter/MET#1246). This is wonderful! Thank you for this 🎉🙌🏻

import urllib
import xarray as xr

# download a sample MET file
urllib.request.urlretrieve(
    "https://github.com/dtcenter/MET/raw/main_v11.1/data/poly/NCEP_masks/WEST_mask.nc",
    "WEST_mask.nc",
)

# open MET file
ds = xr.open_dataset(
    "WEST_mask.nc",
    engine="netcdf4",
)
ds

image

However, one issue I've run into is that the plotting accessor doesn't seem to work for this case

ds.WEST.plot()

OUT:
ValueError: coordinate 'lat' is a DataArray dimension, but it has shape (110, 147) rather than expected shape 110 matching the dimension size

To overcome this, I had to rename the dims

ds.rename_dims({"lat": "y", "lon": "x"}).WEST.plot()

image

What did you expect to happen?

I expected the plot accessor to work without needing to rename the dimensions.

Minimal Complete Verifiable Example

import urllib
import xarray as xr

# download a sample MET file
urllib.request.urlretrieve(
    "https://github.com/dtcenter/MET/raw/main_v11.1/data/poly/NCEP_masks/WEST_mask.nc",
    "WEST_mask.nc",
)

ds = xr.open_dataset(
    "WEST_mask.nc",
    engine="netcdf4",
)
ds.WEST.plot()

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.

Relevant log output

No response

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.11.4 | packaged by conda-forge | (main, Jun 10 2023, 18:08:17) [GCC 12.2.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-1160.53.1.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.2
libnetcdf: 4.9.1

xarray: 2023.8.0
pandas: 2.0.3
numpy: 1.25.2
scipy: 1.11.2
netCDF4: 1.6.3
pydap: None
h5netcdf: 1.2.0
h5py: 3.8.0
Nio: None
zarr: None
cftime: 1.6.2
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: None
dask: 2023.8.1
distributed: 2023.8.1
matplotlib: 3.7.2
cartopy: 0.22.0
seaborn: 0.12.2
numbagg: None
fsspec: 2023.6.0
cupy: None
pint: 0.22
sparse: None
flox: None
numpy_groupies: None
setuptools: 68.1.2
pip: 23.2.1
conda: None
pytest: 7.4.0
mypy: None
IPython: 8.7.0
sphinx: 4.5.0
/p/home/blaylock/miniconda3/envs/flight/lib/python3.11/site-packages/_distutils_hack/init.py:33: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")

@blaylockbk blaylockbk added bug needs triage Issue that has not been reviewed by xarray team member labels Aug 23, 2023
@benbovy benbovy added topic-plotting and removed needs triage Issue that has not been reviewed by xarray team member labels Aug 25, 2023
dcherian added a commit to dcherian/xarray that referenced this issue Aug 31, 2023
@dcherian
Copy link
Contributor

Thanks looks like I only tested Variable creation and not DataArray.

See #8126. This capability is new and mostly untested. It would help a lot if you could try some analysis with #8126 installed and let us know how it goes.

@blaylockbk
Copy link
Author

blaylockbk commented Sep 8, 2023

Thanks @dcherian! #8126 worked for me in the example I shared in the initial posting and also for my other MET output files.

Of course, my initial example was a poor sample, because the 'lon' coordinates cross over the prime meridian 😂

import urllib
import xarray as xr

print("xarray version:", xr.__version__)

# download a sample MET file
urllib.request.urlretrieve(
    "https://github.com/dtcenter/MET/raw/main_v11.1/data/poly/NCEP_masks/WEST_mask.nc",
    "WEST_mask.nc",
)

# open MET file
ds = xr.open_dataset(
    "WEST_mask.nc",
    engine="netcdf4",
)
ds.WEST.plot()

xarray version: 0.10.1.dev2530+gba555427
image

So, had to use Cartopy anyways to plot this one right...

import cartopy.crs as ccrs

ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=-100))
ax.pcolormesh(ds.lon, ds.lat, ds.WEST, transform=ccrs.PlateCarree())
ax.coastlines()

image

But for my other MET output files, this now produces the expected result...

(This image is data from the ECMWF-open-data run through some of MET's tools)

ds = xr.open_dataset("path/to/my/MET/file.nc")
ds["OBS_VGRD_P500_FULL"].plot()

image

Note: the fact that the longitude is a range of 180 to 540 is a weird MET artifact that I need to fix on my end, its not an xarray issue.

@dcherian
Copy link
Contributor

dcherian commented Sep 9, 2023

Great, are you able to do all the analytical calculations you want without errors?

dcherian added a commit that referenced this issue Sep 22, 2023
* Allow creating DataArrays with nD coordinate variables

Closes #2233
Closes #8106

* more test

more test#	make_aggs.bash

* Fix  test

* Apply suggestions from code review

Co-authored-by: Michael Niklas  <[email protected]>

* Update  test

---------

Co-authored-by: Michael Niklas <[email protected]>
Co-authored-by: Maximilian Roos <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants