Search code examples
pythonerror-codemetpy

How to fix an Metpy/mpcalc error: "InvalidSoundingError: Pressure does not decrease monotonically in your sounding."


I'm trying to plot a Skew-T of a sounding data and calculate the LFC,LCL using Python but it's giving me an error:

InvalidSoundingError: 
        Pressure does not decrease monotonically in your sounding.
        Using scipy.signal.medfilt may fix this.

I tried medfilt but it but I'm getting the same error.

Press = medfilt(Press)

I'm importing the data from a .csv file. The line that's giving me the error:

LFC_pressure, LFC_temperature = mpcalc.lfc(Press*units('hPa'), Temp*units.degC, Dewpt*units.degC)

Has anyone encountered this same issue? If so, what do you do?

Thank you


Solution

  • This error is caused by there being seemingly duplicate pressure levels in your sounding (likely caused by lack of precision when writing the data to the CSV). This is fixed in MetPy's development branch and will be included in the forthcoming 1.2.0 release.

    In the meanwhile, the simplest way to remove the duplicate levels is just only keep points where the difference in pressure from one level to the next is > 0:

    import numpy as np
    
    # Need concatenate with True to make sure we keep the bottom point
    non_dups = np.concatenate(([True], np.diff(Press) != 0))
    Press = Press[non_dups]
    Temp = Temp[non_dups]
    Dewpt = Dewpt[non_dups]