Lena
Lena

Reputation: 47

Python value error: operands could not be broadcast together with shapes (732,25,17,33) (25,732,17,33) - coordinates of xarray switched?

I am trying to calculate the dewpoint temperature with the metpy function metpy.calc.dewpoint_from_specific_humidity(pressure, temperature, specific humidity) from ERA5 gridded data, however I get an error:

ds = xarray.open_dataset('/dir/file.nc')
Td = metpy.calc.dewpoint_from_specific_humidity(ds.pressure_level, ds.temperature, ds.specific_humidity)

ValueError: operands could not be broadcast together with shapes (732,25,17,33) (25,732,17,33) 

The shapes are the coordinates of the xarray (time:732, pressure level:25, latitude:17, longitude:33). It seems like the order is switched and the function can´t calculate because of the switched dimensions. Is there a way to fix this?

It would be nice if someone could help me!

Best, Lena

Upvotes: 2

Views: 171

Answers (1)

DopplerShift
DopplerShift

Reputation: 5853

(Posting the answer for completeness.) I'm not sure why the data have the dimensions flipped, but with earlier versions of MetPy, we relied strictly on NumPy broadcasting to handle matching up the arrays--which doesn't understand the dimensions, it can only expand the arrays to try to match. With newer versions of MetPy (I thought 1.0, but certainly > 1.0), it uses Xarray's broadcasting which can leverage the named dimensions to align the arrays for operations.

Upvotes: 1

Related Questions