pythonarraysarray-differencemetpy

Temperature array difference for MetPy


I'm trying to use a metpy script to make Skew-T diagram. My source data uses the Dew Point Depression (dewpt) parameter and the original script uses the Dew Point (Td) parameter. Knowing the Temperature (T), I should change the script for Dew Point calculation, i.e. calculate

Td = T - dewpt

I suppose, the wrong strings are

Td = T - Dewpt
Td = df[Td.values] * units.degC

Unfortunately, I'm new to Python and don't understand how to do this correctly. I get various errors related to either the dimension of the arrays or the absence of the Values parameter of the variable. Tell me, please, how can I correctly denote a new variable Td and the difference of these two arrays (T and dewpt) in the code.

The code looks like this:

import matplotlib.pyplot as plt
import pandas as pd

import pathlib
from pathlib import Path

work_path=pathlib.Path.cwd()
print(work_path)


import metpy.calc as mpcalc
from metpy.cbook import get_test_data
from metpy.plots import add_metpy_logo, SkewT
from metpy.units import units

###########################################
# Upper air data can be obtained using the siphon package, but for this example we will use
# some of MetPy's sample data.

col_names = ['height', 'pressure', 'temperature', 'direction', 'speed', 'dewpt']

df = pd.read_fwf('Baranova_27Jul2022.txt', as_file_obj=False, encoding='latin-1',
                 skiprows=13, usecols=[1, 2, 3, 6, 7, 8], names=col_names)

# Drop any rows with all NaN values for T, Td, winds
df = df.dropna(subset=('temperature', 'dewpt', 'direction', 'speed'), how='all'
               ).reset_index(drop=True)

###########################################
# We will pull the data out of the example dataset into individual variables and
# assign units.

p = df['pressure'].values * units.hPa
T = df['temperature'].values * units.degC
Dewpt = df['dewpt'].values * units.degC
wind_speed = df['speed'].values * units.meter/units.second
wind_dir = df['direction'].values * units.degrees
u, v = mpcalc.wind_components(wind_speed, wind_dir)

Td = T - Dewpt
Td = df[Td.values] * units.degC

print(Td)

###########################################
# Create a new figure. The dimensions here give a good aspect ratio.

fig = plt.figure(figsize=(9, 9))
#add_metpy_logo(fig, 115, 100)
skew = SkewT(fig, rotation=45)

# Plot the data using normal plotting functions, in this case using
# log scaling in Y, as dictated by the typical meteorological plot.
skew.plot(p, T, 'r')
skew.plot(p, Td, 'g')
skew.plot_barbs(p, u, v)
skew.ax.set_ylim(1000, 100)
skew.ax.set_xlim(-40, 40)

# Set some better labels than the default
skew.ax.set_xlabel(f'Temperature ({T.units:~P})')
skew.ax.set_ylabel(f'Pressure ({p.units:~P})')

# Calculate LCL height and plot as black dot. Because `p`'s first value is
# ~1000 mb and its last value is ~250 mb, the `0` index is selected for
# `p`, `T`, and `Td` to lift the parcel from the surface. If `p` was inverted,
# i.e. start from low value, 250 mb, to a high value, 1000 mb, the `-1` index
# should be selected.
lcl_pressure, lcl_temperature = mpcalc.lcl(p[0], T[0], Td[0])
skew.plot(lcl_pressure, lcl_temperature, 'ko', markerfacecolor='black')

# Calculate full parcel profile and add to plot as black line
prof = mpcalc.parcel_profile(p, T[0], Td[0]).to('degC')
skew.plot(p, prof, 'k', linewidth=2)

# Shade areas of CAPE and CIN
skew.shade_cin(p, T, prof, Td)
skew.shade_cape(p, T, prof)

# An example of a slanted line at constant T -- in this case the 0
# isotherm
skew.ax.axvline(0, color='c', linestyle='--', linewidth=2)

# Add the relevant special lines
skew.plot_dry_adiabats()
skew.plot_moist_adiabats()
skew.plot_mixing_lines()

# Show the plot
plt.show()type here

Solution

  • Note that the correct unit for dewpoint depression is delta_degC, so the relevant middle portion of that code should be:

    T = df['temperature'].values * units.degC
    Dewpt = df['dewpt'].values * units.delta_degC
    
    Td = T - Dewpt
    

    At this point Td should be correct with units of "degC". See this portion of the MetPy User Guide for more information about working with temperature units.