Search code examples
pythonmetpy

Pythonic way to compute precipitable water?


I am using ERA5 reanalysis data from the NCAR RDA. I am using metpy dewpoint_from_relative_humidity() to compute dewpoint, then I convert it to a DataArray object and assign coordinates and dimensions that match the Dataset object I have the ERA5 data in. I would like to compute precipitable water using precipitable_water() in metpy but I am struggling to figure out the most efficient way to perform this calculation since it is designed to work on arrays (soundings) rather than grids of data. I have looked at xr.apply_ufunc() but I am unsure if this would be an appropriate application of that xarray feature or frankly even how I would execute that. I could write nested for loops/while loops but I don't feel this is the best way to do this. Does anyone have any suggestions? I tried the line below but it's telling me too many positional arguments, and I don't think this can be done since ds['TD'] is not an array.

ds['TD']  = xr.DataArray(mpcalc.dewpoint_from_relative_humidity(ds['T'],ds['R']),dims=['level','latitude','longitude'],coords=ds.coords)
ds['PW'] = xr.DataArray(mpcalc.precipitable_water(ds['TD'],ds.level,1000.0,700.0),dims=['level','latitude','longitude'],coords=ds.coords)

Solution

  • I'm going to guess you're running MetPy 1.0 (rc1). For that, the calling for precipitable_water() function has changed to use keyword-only arguments for bottom and top. I'd expect the following to work:

    pw = mpcalc.precipitable_water(ds['TD'], ds.level, bottom=1000.0, top=700.0)