2

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

Lena
  • 35
  • 4
  • What version of MetPy do you have installed? What's the output of `python -c 'import metpy; print(metpy.__version__)'`? – DopplerShift May 10 '22 at 01:56
  • Thanks for the question! I had the version 1.0 installed. With the newer one I don't have the problem anymore :D so thanks again! – Lena May 11 '22 at 02:07

1 Answers1

1

(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.

DopplerShift
  • 5,472
  • 1
  • 21
  • 20