Search code examples
pythonmetpy

issue trying to use parcel profile


Novice user here.

I'm running into an issue when trying to use parcel_profile, it keeps kicking out an error saying the variable has no units associated with it:

Traceback (most recent call last): File "Advanced_Sounding_3Dnetcdf2.py", line 165, in prof = mpcalc.parcel_profile(p, T[0], Td[0]).to('degC')

The code snippet in question:

p = phPa * units.hPa
T = TdegC* units.degC
Td = TddegC* units.degC

lcl_pressure, lcl_temperature = mpcalc.lcl(p[0], T[0], Td[0])
skew.plot(lcl_pressure, lcl_temperature, 'ko', markerfacecolor='black')

print(p)

prof = mpcalc.parcel_profile(p, T[0], Td[0]).to('degC')

When I print 'p' it looks like the following:

[<Quantity(965.2435302734375, 'hectopascal')>
 <Quantity(959.7489624023438, 'hectopascal')>
 <Quantity(954.278564453125, 'hectopascal')>
...

The data is read in as:

file = 'cm1out_000001.nc'
figname = 'control'
figtime = '1'

f = Dataset(file, 'r')

[deleted for brevity]

th = f.variables['th'][0,0:zfl,yfl,xfl] #Read in potential temperature
p0=f.variables['prs0'][0,0:zfl,yfl,xfl] #Read in base state pressure
p = f.variables['prs'][0,0:zfl,yfl,xfl] #Read in pressure

[deleted for brevity]

phPA = p/100

[deleted for brevity]

p = phPa * units.hPa

Do I have another coding error in here or is something else amiss?

Thanks.


Solution

  • netCDF4-python gives you masked arrays by default, and those have some odd issues with Pint's unit support. Multiplying on the left with units should fix it:

    p = units.hPa * phPa
    T = units.degC * TdegC
    Td = units.degC * TddegC