xarray-contrib / xwrf

A lightweight interface for working with the Weather Research and Forecasting (WRF) model output in Xarray.
https://xwrf.readthedocs.io/
Apache License 2.0
56 stars 16 forks source link

[Bug]: Destaggering WRF 10m Wind: List Index Out of Range Error #147

Closed mariandob closed 7 months ago

mariandob commented 7 months ago

What happened?

I tried to use the xwrf destaggering routine for a wrfout output variable (10m u-wind component), and received some internal error related to indexing a list. I didn't see this reported in a past or currently open bug report, so I wanted to mention it here.

Minimal Complete Verifiable Example

#Import packages
import xwrf
import xarray as xr

#Open WRF output file and run post-processing through xwrf
nc_file = xr.open_dataset('wrfout_d04_2017-12-07').xwrf.postprocess()

#Destagger the WRF grid for 10m u-wind component
u_wind_destaggered = nc_file['U10'].xwrf.destagger().variable

Relevant log output

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[5], line 1
----> 1 nc_file_2017_12_07['U10'].xwrf.destagger().variable

File /home/sbarc/students/mariandob/mambaforge/envs/swex/lib/python3.11/site-packages/xwrf/accessors.py:68, in WRFDataArrayAccessor.destagger(self, stagger_dim, unstaggered_dim_name, exclude_staggered_auxiliary_coords)
     31 def destagger(
     32     self,
     33     stagger_dim: str | None = None,
     34     unstaggered_dim_name: str | None = None,
     35     exclude_staggered_auxiliary_coords: bool = False,
     36 ) -> xr.DataArray:
     37     """
     38     Destagger a single WRF xarray.DataArray
     39 
   (...)
     66     dimension coordinates or obtained from the original dataset.
     67     """
---> 68     new_variable = _destag_variable(
     69         self.xarray_obj.variable, stagger_dim=stagger_dim, unstag_dim_name=unstaggered_dim_name
     70     )
     72     # Need to recalculate staggered coordinates, as they don't already exist independently
     73     # in a DataArray context
     74     new_coords = {}

File /home/sbarc/students/mariandob/mambaforge/envs/swex/lib/python3.11/site-packages/xwrf/destagger.py:55, in _destag_variable(datavar, stagger_dim, unstag_dim_name)
     49         raise NotImplementedError(
     50             'Expected a single destagger dimensions. Found multiple destagger dimensions: '
     51             f'{stagger_dim}'
     52         )
     54     # we need a string, not a list containing a string
---> 55     stagger_dim = stagger_dim[0]
     56 # Otherwise, we have a valid user provided stagger dimension
     57 
     58 # Destagger by mean of offset slices representing each side with respect to the stagger_dim
     59 stagger_dim_size = datavar.sizes[stagger_dim]

IndexError: list index out of range

Environment

System Information
------------------
xWRF commit : None
python      : 3.11.5 | packaged by conda-forge | (main, Aug 27 2023, 03:34:09) [GCC 12.3.0]
python-bits : 64
OS          : Linux
OS-release  : 3.10.0-1160.90.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')

Installed Python Packages
-------------------------
cf_xarray   : 0.8.6
dask        : 2023.11.0
donfig      : 0.8.1.post0
matplotlib  : 3.8.0
metpy       : 1.5.1
netCDF4     : 1.6.4
numpy       : 1.25.2
pandas      : 2.1.0
pint        : 0.22
pooch       : v1.7.0
pyproj      : 3.6.1
xarray      : 2023.11.0
xgcm        : 0.8.1
xwrf        : 0.0.2

Anything else we need to know?

The destaggering routine works with u-component of wind variable, but not with the u-component at 10m. Why might this be? Is it because the 10m wind components are already destaggered?

lpilz commented 7 months ago

Hi @mariandob, thanks for raising this issue. This seems to be a non-caught exception which is badly worded but in the end due to the U10 and V10 variables already being destaggered. You can check this by looking at the dimensions and seeing whether any dimension names end in _stag. I'll add some better worded exception to prevent any confusion in the future!