NCAR / geocat-comp

GeoCAT-comp provides implementations of computational functions for operating on geosciences data. Many of these functions originated in NCL and were translated into Python.
https://geocat-comp.readthedocs.io
Apache License 2.0
121 stars 54 forks source link

interp_hybrid_to_pressure improvements #439

Open anissa111 opened 11 months ago

anissa111 commented 11 months ago

Also, thanks to Kenton Wu for bringing these issues to our attention!

jhollowed commented 2 months ago

Adding this comment here so as not to create a new issue for this function. Line 515 of interp_hybrid_to_pressure() is

output = output.transpose(*dims).assign_coords(coords)

One of the entries in the dictionary coords will be the (key, value) pair {'plev' : new_levels}. If the user-supplied new_levels is not a Numpy array, but rather an xarray DataArray with dimension named lev_dim (an easy mistake to make by the caller, in my opinion), then line 515 fails with the error:

ValueError: cannot add coordinates with new dimensions to a DataArray

The source of the error is immediately clear, but is caused when lev_dim is not 'plev'.

I think that to avoid this, there should be an assertion early in the function ensuring that new_levels is a Numpy array and not a DataArray.

kafitzgerald commented 2 months ago

@jhollowed, thanks for noting this!

I believe it's mentioned in the docstring, but agree it'd be good to make this more robust.