jbusecke / xMIP

Analysis ready CMIP6 data in python the easy way with pangeo tools.
https://cmip6-preprocessing.readthedocs.io/en/latest/?badge=latest
Apache License 2.0
197 stars 44 forks source link

Renaming dimensions in cmip6_rename results in the coordinate data being lost #343

Closed emmomp closed 5 months ago

emmomp commented 6 months ago

Problem: If a dimension is renamed in cmip6_rename, the current code drops the coordinate entirely, losing the coordinate values. Expected behaviour: The coordinate and dimension are both renamed.

See attached example for IPSL-CM6A-LR where old dimensions nvertex, olevel and axis_nbounds are renamed to vertex, lev and bnds but crucially deleted as coordinates. So now the returned dataset has no depth information for example.

Current relevent code is in the function

def _maybe_rename_dims(da, rdict):
   for di in da.dims:
     for target, candidates in rdict.items():
          if di in candidates:
               da = da.swap_dims({di: target})
               if di in da.coords:
                   da = da.drop_vars(di)
   return da

Where the final drop_vars is responsible. Can anyone explain the reason for this?

I have edited to have da = da.rename({di: target}).set_xindex(target) instead which seems to produce the desired result. Happy to make a pull request if there isn't a reason I'm missing for the current behaviour...

Screenshot 2024-03-14 at 13 58 29

jbusecke commented 6 months ago

Thanks for this @emmomp. PR would be very welcome for this! I do not believe there was a particular reason for this other than the fact that I quickly threw this together during a hackathon many years ago 😆.