coecms / xmhw

Xarray version of Marine Heatwaves code by Eric Olivier
https://xmhw.readthedocs.io/en/latest/
Apache License 2.0
22 stars 10 forks source link

PandasMultiIndex only accepts 1-dimensional variables #69

Closed croachutas closed 1 year ago

croachutas commented 1 year ago

Okay, just stumbled upon xmhw after sometime of beating round the bush trying to use the 'raw' python mhw package.

Anyway, I'm analyzing some SST output from a 30-year future climate run in ROMS for the NZ region (Moana project stuff from my time in NZ):

`<xarray.DataArray 'temp' (ocean_time: 11155, eta_rho: 467, xi_rho: 397)> [2068125845 values with dtype=float32] Coordinates: Vtransform (ocean_time) float64 ... hc (ocean_time) float32 ... s_rho float64 ... Cs_r (ocean_time) float32 ... h (ocean_time, eta_rho, xi_rho) float32 ... lon_rho (eta_rho, xi_rho) float64 ... lat_rho (eta_rho, xi_rho) float64 ...

  • ocean_time (ocean_time) object 2030-01-06 12:00:00 ... 2060-07-21 12:00:00 Dimensions without coordinates: eta_rho, xi_rho Attributes: long_name: time-averaged potential temperature units: Celsius time: ocean_time grid: grid location: face field: temperature, scalar, series`

When I try to define thresholds:

clim = threshold(ds,tdim='ocean_time')

I get an error:

`ValueError Traceback (most recent call last) Cell In[12], line 1 ----> 1 clim = threshold(ds,tdim='ocean_time')#,climatologyPeriod=[cftime.Datetime360Day(2031,1,1,12,0,0,0),cftime.Datetime360Day(2060,12,30,12,0,0,0)]) 2 print(clim.seas) 3 clim.seas.plot()

File ~/anaconda3/envs/analysis_tools/lib/python3.9/site-packages/xmhw/xmhw.py:213, in threshold(temp, tdim, climatologyPeriod, pctile, windowHalfWidth, smoothPercentile, smoothPercentileWidth, maxPadLength, coldSpells, tstep, anynans, skipna) 211 ds["seas"] = xr.concat(seas_results, dim='cell') 212 dims = [k for k in ts.cell.coords.keys()] --> 213 ds = ds.set_xindex(dims) 214 ds = ds.unstack(dim='cell') 215 ds.thresh.name = "threshold"

File ~/anaconda3/envs/analysis_tools/lib/python3.9/site-packages/xarray/core/dataset.py:4944, in Dataset.set_xindex(self, coord_names, index_cls, **options) 4938 raise ValueError( 4939 f"those coordinates already have an index: {indexed_coords}" 4940 ) 4942 coord_vars = {name: self._variables[name] for name in coord_names} -> 4944 index = index_cls.from_variables(coord_vars, options=options) 4946 new_coord_vars = index.create_variables(coord_vars) 4948 # special case for setting a pandas multi-index from level coordinates 4949 # TODO: remove it once we depreciate pandas multi-index dimension (tuple 4950 # elements) coordinate

File ~/anaconda3/envs/analysis_tools/lib/python3.9/site-packages/xarray/core/indexes.py:953, in PandasMultiIndex.from_variables(cls, variables, options) 946 @classmethod 947 def from_variables( 948 cls, (...) 951 options: Mapping[str, Any], 952 ) -> PandasMultiIndex: --> 953 _check_dim_compat(variables) 954 dim = next(iter(variables.values())).dims[0] 956 index = pd.MultiIndex.from_arrays( 957 [var.values for var in variables.values()], names=variables.keys() 958 )

File ~/anaconda3/envs/analysis_tools/lib/python3.9/site-packages/xarray/core/indexes.py:867, in _check_dim_compat(variables, all_dims) 862 """Check that all multi-index variable candidates are 1-dimensional and 863 either share the same (single) dimension or each have a different dimension. 864 865 """ 866 if any([var.ndim != 1 for var in variables.values()]): --> 867 raise ValueError("PandasMultiIndex only accepts 1-dimensional variables") 869 dims = {var.dims for var in variables.values()} 871 if all_dims == "equal" and len(dims) > 1: `

I'm using xarray v2023.9 and xmhw 0.9.3 both installed using anaconda

This looks an awful lot like #56 but per that discussion the underlying issue was suppose to be fixed.

croachutas commented 1 year ago

Okay, turns out I just needed to get rid of a no longer used depth coordinate...

croachutas commented 1 year ago

Problem solved.

paolap commented 1 year ago

Hi Christopher, I would have thought that the lat/lon on two dimensions would cause issues. I've been recently contacted by someone else who had a similar issue with 2D coordinates. I would be interested to get a sample of your data if you don't mind, I should be at IMAS tomorrow

croachutas commented 1 year ago

I'm down at desk 213.05 if ya wanna come grab the data.