{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# MetPy calc\n", "\n", "#### Provides tools for unit-aware, meteorological calculations in the following categories:\n", "\n", "* Dry Thermodynamics\n", "* Moist Thermodynamics\n", "* Soundings\n", "* Dynamic/Kinematic\n", "* Boundary Layer/Turbulence\n", "* Mathematical Functions\n", "* Apparent Temperature\n", "* Standard Atmosphere\n", "* Other" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some methods require a vector or grid as input (e.g., calculations based on sounding profiles).\n", "\n", "Those that do not can still be passed dimensional arrays, and will return like-sized arrays, even if they appear to be scalar functions." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import metpy.calc as calc\n", "import metpy.constants as c\n", "from metpy.units import units\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".\n", "\n", "\n", "There are some very simple calculations, like potential temperature from pressure and temperature:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\\[290.9665329591883\\ kelvin\\]" ], "text/latex": [ "$290.9665329591883\\ \\mathrm{kelvin}$" ], "text/plain": [ "290.9665329591883 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "calc.potential_temperature(800. * units.hectopascal, 273. * units.kelvin)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Series example\n", "Given a list of model sigma coordinates in the vertical and the surface pressure, return the list of model pressure levels:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1007.94 ,\n", " 994.87 ,\n", " 976.94 ,\n", " 954.75 ,\n", " 927.81 ,\n", " 895.29 ,\n", " 856.8 ,\n", " 811.82 ,\n", " 760.56 ,\n", " 703.33 ,\n", " 641.13 ,\n", " 575.49 ,\n", " 508.22 ,\n", " 441.36 ,\n", " 376.84 ,\n", " 316.56 ,\n", " 261.56 ,\n", " 212.83 ,\n", " 170.39 ,\n", " 134.32 ,\n", " 104.14 ,\n", " 79.22 ,\n", " 58.75 ,\n", " 42.34 ,\n", " 29.17 ,\n", " 18.54 ,\n", " 10.23 ,\n", " 2.74 ]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sigma = [0.995, 0.9821, 0.9644, 0.9425, 0.9159, 0.8838, 0.8458, \n", " 0.8014, 0.7508, 0.6943, 0.6329, 0.5681, 0.5017, 0.4357, \n", " 0.372, 0.3125, 0.2582, 0.2101, 0.1682, 0.1326, 0.1028, \n", " 0.0782, 0.058, 0.0418, 0.0288, 0.0183, 0.0101, 0.0027] * units.dimensionless\n", "plevs = calc.sigma_to_pressure(sigma,1013.0 * units.millibar, 0.0 * units.millibar)\n", "list(np.around(plevs,decimals=2))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\\[\\begin{pmatrix}100793.5 & 99486.73 & 97693.72 & 95475.25 & 92780.67000000001 & 89528.94 & 85679.54 & 81181.82 & 76056.04000000001 & 70332.59000000001 & 64112.770000000004 & 57548.530000000006 & 50822.21 & 44136.409999999996 & 37683.6 & 31656.25 & 26155.66 & 21283.13 & 17038.66 & 13432.380000000001 & 10413.640000000001 & 7921.66 & 5875.400000000001 & 4234.339999999999 & 2917.44 & 1853.79 & 1023.1299999999999 & 273.51\\end{pmatrix} pascal\\]" ], "text/latex": [ "$\\begin{pmatrix}100793.5 & 99486.73 & 97693.72 & 95475.25 & 92780.67000000001 & 89528.94 & 85679.54 & 81181.82 & 76056.04000000001 & 70332.59000000001 & 64112.770000000004 & 57548.530000000006 & 50822.21 & 44136.409999999996 & 37683.6 & 31656.25 & 26155.66 & 21283.13 & 17038.66 & 13432.380000000001 & 10413.640000000001 & 7921.66 & 5875.400000000001 & 4234.339999999999 & 2917.44 & 1853.79 & 1023.1299999999999 & 273.51\\end{pmatrix}\\ \\mathrm{pascal}$" ], "text/plain": [ "array([100793.5 , 99486.73, 97693.72, 95475.25, 92780.67, 89528.94,\n", " 85679.54, 81181.82, 76056.04, 70332.59, 64112.77, 57548.53,\n", " 50822.21, 44136.41, 37683.6 , 31656.25, 26155.66, 21283.13,\n", " 17038.66, 13432.38, 10413.64, 7921.66, 5875.4 , 4234.34,\n", " 2917.44, 1853.79, 1023.13, 273.51]) " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plevs.to(units.pascal)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Nesting\n", "Functions can be nested.\n", "For example, there is not a function to calculate specific humidity directly from relative humidity, but there are functions for relative humidity to dewpoint, and then dewpoint to specific humidity:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/homes/kpegion/.conda/envs/aoes/lib/python3.6/site-packages/metpy/xarray.py:655: MetpyDeprecationWarning: The dewpoint_rh function was deprecated in version 0.12. This function has been renamed dewpoint_from_relative_humidity.\n", " return func(*args, **kwargs)\n" ] }, { "data": { "text/html": [ "\\[0.01746755638387136\\ dimensionless\\]" ], "text/latex": [ "$0.01746755638387136\\ dimensionless$" ], "text/plain": [ "0.01746755638387136 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "calc.specific_humidity_from_dewpoint(calc.dewpoint_rh(95 * units.degF, 50 * units.percent), 29.92 * units.inHg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More powerful functions\n", "Here is an example of calculating CAPE and CIN from profiles of temperature and dewpoint (e.g., from a radiosonde):" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1120.4139829861845 ,\n", " -188.78510536438793 )" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t_profile = [299.10, 300.08, 300.18, 300.28, 300.40, 300.50, 300.57, 300.59,\n", " 300.56, 300.52, 300.59, 300.26, 299.30, 298.96, 298.47, 297.90,\n", " 297.23, 296.46, 295.58, 294.68, 293.94, 293.46, 293.13, 292.65,\n", " 291.96, 291.15, 290.26, 289.34, 288.23, 286.91, 285.51, 284.15,\n", " 281.99, 280.00, 278.20, 276.52, 275.46, 273.96, 271.22, 269.24,\n", " 265.27, 265.24, 264.11, 262.99, 260.92, 259.68, 257.17, 256.84,\n", " 254.82, 252.79, 250.66, 248.49, 246.34, 244.41, 242.85, 240.85] * units.kelvin\n", "td_profile = [294.94, 295.25, 295.18, 295.12, 295.07, 295.01, 294.95, 294.88,\n", " 294.79, 294.66, 294.41, 293.35, 292.61, 292.12, 291.72, 291.28,\n", " 290.77, 290.63, 290.54, 290.38, 289.62, 287.89, 285.89, 284.3 ,\n", " 282.83, 281.19, 279.36, 277.59, 276.23, 275.34, 273.88, 270.3 ,\n", " 263.84, 256.68, 250.94, 247.86, 243.36, 240.34, 237.07, 239.96,\n", " 238.58, 235.17, 232.41, 231.57, 233.72, 240.21, 244.34, 243.2 ,\n", " 239.54, 236.16, 234.42, 235.03, 237.08, 235.84, 230.25, 224.31] * units.kelvin\n", "p_profile = [978.08, 976.92, 974.49, 971.84, 968.94, 965.78, 962.34, 958.58,\n", " 954.49, 950.03, 945.19, 939.94, 934.23, 928.06, 921.37, 914.15,\n", " 906.36, 897.97, 888.95, 879.27, 868.92, 857.86, 846.07, 833.54,\n", " 820.27, 806.24, 791.47, 775.96, 759.73, 742.82, 725.26, 707.1 ,\n", " 688.41, 669.24, 649.67, 629.79, 609.69, 589.47, 569.21, 549.02,\n", " 529.01, 509.26, 489.88, 471.01, 452.69, 434.94, 417.76, 401.11,\n", " 385.01, 369.42, 354.35, 339.77, 325.68, 312.07, 298.92, 286.22] * units.hectopascal\n", "calc.surface_based_cape_cin(p_profile, t_profile, td_profile)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dry Thermodynamics\n", "`add_height_to_pressure(pressure, height)` Calculate the pressure at a certain height above another pressure level.\n", "\n", "`add_pressure_to_height(height, pressure)` Calculate the height at a certain pressure above another height.\n", "\n", "`density(pressure, temperature, mixing[, …])` Calculate density.\n", "\n", "`dry_lapse(pressure, temperature[, ref_pressure])` Calculate the temperature at a level assuming only dry processes.\n", "\n", "`dry_static_energy(heights, temperature)` Calculate the dry static energy of parcels.\n", "\n", "`geopotential_to_height(geopot)` Compute height from a given geopotential.\n", "\n", "`height_to_geopotential(height)` Compute geopotential for a given height.\n", "\n", "`mean_pressure_weighted(pressure, *args, **kwargs)` Calculate pressure-weighted mean of an arbitrary variable through a layer.\n", "\n", "`potential_temperature(pressure, temperature)` Calculate the potential temperature.\n", "\n", "`sigma_to_pressure(sigma, psfc, ptop)` Calculate pressure from sigma values.\n", "\n", "`static_stability(pressure, temperature[, axis])` Calculate the static stability within a vertical profile.\n", "\n", "`temperature_from_potential_temperature(…)` Calculate the temperature from a given potential temperature.\n", "\n", "`thickness_hydrostatic(pressure, temperature, …)` Calculate the thickness of a layer via the hypsometric equation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Moist Thermodynamics\n", "`dewpoint(e)` Calculate the ambient dewpoint given the vapor pressure.\n", "\n", "`dewpoint_from_specific_humidity(…)` Calculate the dewpoint from specific humidity, temperature, and pressure.\n", "\n", "`dewpoint_rh(temperature, rh)` Calculate the ambient dewpoint given air temperature and relative humidity.\n", "\n", "`equivalent_potential_temperature(pressure, …)` Calculate equivalent potential temperature.\n", "\n", "`mixing_ratio(part_press, tot_press[, …])` Calculate the mixing ratio of a gas.\n", "\n", "`mixing_ratio_from_relative_humidity(…)` Calculate the mixing ratio from relative humidity, temperature, and pressure.\n", "\n", "`mixing_ratio_from_specific_humidity(…)` Calculate the mixing ratio from specific humidity.\n", "\n", "`moist_lapse(pressure, temperature[, …])` Calculate the temperature at a level assuming liquid saturation processes.\n", "\n", "`moist_static_energy(heights, temperature, …)` Calculate the moist static energy of parcels.\n", "\n", "`precipitable_water(dewpt, pressure[, …])` Calculate precipitable water through the depth of a sounding.\n", "\n", "`psychrometric_vapor_pressure_wet(…[, …])` Calculate the vapor pressure with wet bulb and dry bulb temperatures.\n", "\n", "`relative_humidity_from_dewpoint(temperature, …)` Calculate the relative humidity.\n", "\n", "`relative_humidity_from_mixing_ratio(…)` Calculate the relative humidity from mixing ratio, temperature, and pressure.\n", "\n", "`relative_humidity_from_specific_humidity(…)` Calculate the relative humidity from specific humidity, temperature, and pressure.\n", "\n", "`relative_humidity_wet_psychrometric(…)` Calculate the relative humidity with wet bulb and dry bulb temperatures.\n", "\n", "`saturation_equivalent_potential_temperature(…)` Calculate saturation equivalent potential temperature.\n", "\n", "`saturation_mixing_ratio(tot_press, temperature)` Calculate the saturation mixing ratio of water vapor.\n", "\n", "`saturation_vapor_pressure(temperature)` Calculate the saturation water vapor (partial) pressure.\n", "\n", "`specific_humidity_from_dewpoint(dewpoint, …)` Calculate the specific humidity from the dewpoint temperature and pressure.\n", "\n", "`specific_humidity_from_mixing_ratio(mixing_ratio)` Calculate the specific humidity from the mixing ratio.\n", "\n", "`thickness_hydrostatic_from_relative_humidity(…)` Calculate the thickness of a layer given pressure, temperature and relative humidity.\n", "\n", "`vapor_pressure(pressure, mixing)` Calculate water vapor (partial) pressure.\n", "\n", "`vertical_velocity(omega, pressure, temperature)` Calculate w from omega assuming hydrostatic conditions.\n", "\n", "`vertical_velocity_pressure(w, pressure, …)` Calculate omega from w assuming hydrostatic conditions.\n", "\n", "`virtual_potential_temperature(pressure, …)` Calculate virtual potential temperature.\n", "\n", "`virtual_temperature(temperature, mixing[, …])` Calculate virtual temperature.\n", "\n", "`wet_bulb_temperature(pressure, temperature, …)` Calculate the wet-bulb temperature using Normand’s rule." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Soundings\n", "`bulk_shear(pressure, u, v[, heights, …])` Calculate bulk shear through a layer.\n", "\n", "`bunkers_storm_motion(pressure, u, v, heights)` \n", "Calculate the Bunkers right-mover and left-mover storm motions and sfc-6km mean flow.\n", "\n", "`cape_cin(pressure, temperature, dewpt, …)` Calculate CAPE and CIN.\n", "\n", "`critical_angle(pressure, u, v, heights, …)` Calculate the critical angle.\n", "\n", "`el(pressure, temperature, dewpt[, …])` Calculate the equilibrium level.\n", "\n", "`lcl(pressure, temperature, dewpt[, …])` Calculate the lifted condensation level (LCL) using from the starting point.\n", "\n", "`lfc(pressure, temperature, dewpt[, …])` Calculate the level of free convection (LFC).\n", "\n", "`mixed_layer(p, *args, **kwargs)` Mix variable(s) over a layer, yielding a mass-weighted average.\n", "\n", "`mixed_parcel(p, temperature, dewpt[, …])` Calculate the properties of a parcel mixed from a layer.\n", "\n", "`most_unstable_cape_cin(pressure, …)` Calculate most unstable CAPE/CIN.\n", "\n", "`most_unstable_parcel(pressure, temperature, …)` Determine the most unstable parcel in a layer.\n", "\n", "`parcel_profile(pressure, temperature, dewpt)` Calculate the profile a parcel takes through the atmosphere.\n", "\n", "`parcel_profile_with_lcl(pressure, …)` Calculate the profile a parcel takes through the atmosphere.\n", "\n", "`significant_tornado(sbcape, …)` Calculate the significant tornado parameter (fixed layer).\n", "\n", "`storm_relative_helicity(u, v, heights, depth)` Calculate storm relative helicity.\n", "\n", "`supercell_composite(mucape, …)` Calculate the supercell composite parameter.\n", "\n", "`surface_based_cape_cin(pressure, …)` Calculate surface-based CAPE and CIN." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dynamic/Kinematic\n", "`absolute_momentum(u_wind, v_wind[, index])` Calculate cross-sectional absolute momentum (also called pseudoangular momentum).\n", "\n", "`absolute_vorticity(u, v, dx, dy, lats[, …])` Calculate the absolute vorticity of the horizontal wind.\n", "\n", "`advection(scalar, wind, deltas)` Calculate the advection of a scalar field by the wind.\n", "\n", "`ageostrophic_wind(heights, f, dx, dy, u, v)` Calculate the ageostrophic wind given from the heights or geopotential.\n", "\n", "`coriolis_parameter(latitude)` Calculate the coriolis parameter at each point.\n", "\n", "`divergence(u, v, dx, dy)` Calculate the horizontal divergence of a vector.\n", "\n", "`exner_function(pressure[, reference_pressure])` Calculate the Exner function.\n", "\n", "`frontogenesis(thta, u, v, dx, dy[, dim_order])` Calculate the 2D kinematic frontogenesis of a temperature field.\n", "\n", "`geostrophic_wind(heights, f, dx, dy)` Calculate the geostrophic wind given from the heights or geopotential.\n", "\n", "`inertial_advective_wind(u, v, u_geostrophic, …)` Calculate the inertial advective wind.\n", "\n", "`kinematic_flux(vel, b[, perturbation, axis])` Compute the kinematic flux from two time series.\n", "\n", "`montgomery_streamfunction(height, temperature)` Compute the Montgomery Streamfunction on isentropic surfaces.\n", "\n", "`potential_vorticity_baroclinic(…)` Calculate the baroclinic potential vorticity.\n", "\n", "`potential_vorticity_barotropic(heights, u, …)` Calculate the barotropic (Rossby) potential vorticity.\n", "\n", "`q_vector(u, v, temperature, pressure, dx, dy)` Calculate Q-vector at a given pressure level using the u, v winds and temperature.\n", "\n", "`shearing_deformation(u, v, dx, dy)` Calculate the shearing deformation of the horizontal wind.\n", "\n", "`stretching_deformation(u, v, dx, dy)` Calculate the stretching deformation of the horizontal wind.\n", "\n", "`total_deformation(u, v, dx, dy)` Calculate the horizontal total deformation of the horizontal wind.\n", "\n", "`vorticity(u, v, dx, dy)` Calculate the vertical vorticity of the horizontal wind.\n", "\n", "`wind_components(speed, wdir)` Calculate the U, V wind vector components from the speed and direction.\n", "\n", "`wind_direction(u, v[, convention])` Compute the wind direction from u and v-components.\n", "\n", "`wind_speed(u, v)` Compute the wind speed from u and v-components." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boundary Layer/Turbulence\n", "`brunt_vaisala_frequency(heights, …[, axis])` Calculate the Brunt-Vaisala frequency.\n", "\n", "`brunt_vaisala_frequency_squared(heights, …)` Calculate the square of the Brunt-Vaisala frequency.\n", "\n", "`brunt_vaisala_period(heights, …[, axis])` Calculate the Brunt-Vaisala period.\n", "\n", "`friction_velocity(u, w[, v, perturbation, axis])` Compute the friction velocity from the time series of velocity components.\n", "\n", "`tke(u, v, w[, perturbation, axis])` Compute turbulence kinetic energy." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mathematical Functions\n", "`cross_section_components(data_x, data_y[, index])` Obtain the tangential and normal components of a cross-section of a vector field.\n", "\n", "`first_derivative(f, **kwargs)` Calculate the first derivative of a grid of values.\n", "\n", "`gradient(f, **kwargs)` Calculate the gradient of a grid of values.\n", "\n", "`grid_deltas_from_dataarray(f)` Calculate the horizontal deltas between grid points of a DataArray.\n", "\n", "`laplacian(f, **kwargs)` Calculate the laplacian of a grid of values.\n", "\n", "`lat_lon_grid_deltas(longitude, latitude, …)` Calculate the delta between grid points that are in a latitude/longitude format.\n", "\n", "`normal_component(data_x, data_y[, index])` Obtain the normal component of a cross-section of a vector field.\n", "\n", "`second_derivative(f, **kwargs)` Calculate the second derivative of a grid of values.\n", "\n", "`tangential_component(data_x, data_y[, index])` Obtain the tangential component of a cross-section of a vector field.\n", "\n", "`unit_vectors_from_cross_section(cross[, index])` Calculate the unit tanget and unit normal vectors from a cross-section." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Apparent Temperature\n", "`apparent_temperature(temperature, rh, speed)` Calculate the current apparent temperature.\n", "\n", "`heat_index(temperature, rh[, mask_undefined])` Calculate the Heat Index from the current temperature and relative humidity.\n", "\n", "`windchill(temperature, speed[, …])` Calculate the Wind Chill Temperature Index (WCTI)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Standard Atmosphere\n", "`altimeter_to_sea_level_pressure(…)` Convert the altimeter setting to sea-level pressure.\n", "\n", "`altimeter_to_station_pressure(…)` Convert the altimeter measurement to station pressure.\n", "\n", "`height_to_pressure_std(height)` Convert height data to pressures using the U.S.\n", "\n", "`pressure_to_height_std(pressure)` Convert pressure data to heights using the U.S." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Other\n", "`angle_to_direction(input_angle[, full, level])` Convert the meteorological angle to directional text.\n", "\n", "`find_bounding_indices(arr, values, axis[, …])` Find the indices surrounding the values within arr along axis.\n", "\n", "`find_intersections(x, a, b[, direction, log_x])` Calculate the best estimate of intersection.\n", "\n", "`get_layer(pressure, *args, **kwargs)` Return an atmospheric layer from upper air data with the requested bottom and depth.\n", "\n", "`get_layer_heights(heights, depth, *args, …)` Return an atmospheric layer from upper air data with the requested bottom and depth.\n", "\n", "`get_perturbation(ts[, axis])` Compute the perturbation from the mean of a time series.\n", "\n", "`isentropic_interpolation(theta_levels, …)` Interpolate data in isobaric coordinates to isentropic coordinates.\n", "\n", "`nearest_intersection_idx(a, b)` Determine the index of the point just before two lines with common x values.\n", "\n", "`parse_angle(input_dir)` Calculate the meteorological angle from directional text.\n", "\n", "`reduce_point_density(points, radius[, priority])` Return a mask to reduce the density of points in irregularly-spaced data.\n", "\n", "`resample_nn_1d(a, centers)` Return one-dimensional nearest-neighbor indexes based on user-specified centers.\n", "\n", "`smooth_gaussian(scalar_grid, n)` Filter with normal distribution of weights.\n", "\n", "`smooth_n_point(scalar_grid[, n, passes])` Filter with normal distribution of weights." ] } ], "metadata": { "kernelspec": { "display_name": "Python (aoes)", "language": "python", "name": "aoes" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 4 }