Search code examples
pythonmetpy

MetPy suface_based_cape_cin returning error with units


At my site, we're having an issue with MetPy returning a units error when trying to call surface_based_cape_cin

I am seeing the following error:

Traceback (most recent call last):
  File "Advanced_Sounding_3Dnetcdf2.py", line 202, in <module>
    sbcape, sbcin = mpcalc.surface_based_cape_cin(p1, T1, Td1)
  File "/gpfs/group/kal6112/default/sw/anaconda3/lib/python3.6/site-packages/metpy/xarray.py", line 677, in wrapper
    return func(*args, **kwargs)
  File "/gpfs/group/kal6112/default/sw/anaconda3/lib/python3.6/site-packages/metpy/units.py", line 320, in wrapper
    return func(*args, **kwargs)
  File "/gpfs/group/kal6112/default/sw/anaconda3/lib/python3.6/site-packages/metpy/calc/thermo.py", line 1851, in surface_based_cape_cin
    return cape_cin(p, t, td, profile)
  File "/gpfs/group/kal6112/default/sw/anaconda3/lib/python3.6/site-packages/metpy/xarray.py", line 677, in wrapper
    return func(*args, **kwargs)
  File "/gpfs/group/kal6112/default/sw/anaconda3/lib/python3.6/site-packages/metpy/units.py", line 319, in wrapper
    raise ValueError(msg)
ValueError: `cape_cin` given arguments with incorrect units: `temperature` requires "[temperature]" but given "none", `dewpt` requires "[temperature]" but given "none".

When I check the incoming values p1, T1, and Td1 they all have the correct units (hectopascal, degree_Celcius).

Just to be sure I added the following and checked the results prior to the call to surface_based_cape_cin:

p1 = units.hPa * phPa
T1 =  units.degC * TdegC
Td1 = units.degC * TddegC

I'm running the following version of MetPy

# Name                    Version                   Build  Channel
metpy                     0.12.2                     py_0    conda-forge

I don't recall having this prior to updating to this version but I can't be certain the problem I'm seeing arose after the update or not.

Thanks for any help you can provide.


Solution

  • This is definitely a bug in MetPy, likely due to more challenges with masked arrays and preserving units. I've opened a new issue. In the meanwhile as a work-around, it's probably best to just eliminate masked arrays with something like:

    p1 = p1.compressed() * p1.units
    T1 = T1.compressed() * T1.units
    Td1 = Td1.compressed() * Td1.units
    

    This will work so long as the data have no actual masked values or if all 3 arrays are masked in the same spot. If not, you'll need to do some more work to remove any of the levels where one of the values is masked.