_id
stringlengths
2
7
title
stringlengths
1
88
partition
stringclasses
3 values
text
stringlengths
31
13.1k
language
stringclasses
1 value
meta_information
dict
q23600
heat_index
train
def heat_index(temperature, rh, mask_undefined=True): r"""Calculate the Heat Index from the current temperature and relative humidity. The implementation uses the formula outlined in [Rothfusz1990]_. This equation is a multi-variable least-squares regression of the values obtained in [Steadman1979]_. Parameters ---------- temperature : `pint.Quantity` Air temperature rh : array_like The relative humidity expressed as a unitless ratio in the range [0, 1]. Can also pass a percentage if proper units are attached. Returns ------- `pint.Quantity` The corresponding Heat Index value(s) Other Parameters ---------------- mask_undefined : bool, optional A flag indicating whether a masked array should be returned with values where heat index is undefined masked. These are values where the temperature < 80F or relative humidity < 40 percent. Defaults to `True`. See Also -------- windchill """ delta = temperature.to(units.degF) - 0. * units.degF rh2 = rh * rh delta2 = delta * delta # Calculate the Heat Index -- constants converted for RH in [0, 1] hi = (-42.379 *
python
{ "resource": "" }
q23601
apparent_temperature
train
def apparent_temperature(temperature, rh, speed, face_level_winds=False): r"""Calculate the current apparent temperature. Calculates the current apparent temperature based on the wind chill or heat index as appropriate for the current conditions. Follows [NWS10201]_. Parameters ---------- temperature : `pint.Quantity` The air temperature rh : `pint.Quantity` The relative humidity expressed as a unitless ratio in the range [0, 1]. Can also pass a percentage if proper units are attached. speed : `pint.Quantity` The wind speed at 10m. If instead the winds are at face level, `face_level_winds` should be set to `True` and the 1.5 multiplicative correction will be applied automatically. face_level_winds : bool, optional A flag indicating whether the wind speeds were measured at facial level instead of 10m, thus requiring a correction. Defaults to `False`. Returns ------- `pint.Quantity` The corresponding apparent temperature value(s) See Also -------- heat_index, windchill """ is_not_scalar = isinstance(temperature.m, (list, tuple, np.ndarray)) temperature = atleast_1d(temperature) rh = atleast_1d(rh) speed = atleast_1d(speed) wind_chill_temperature = windchill(temperature, speed, face_level_winds=face_level_winds,
python
{ "resource": "" }
q23602
pressure_to_height_std
train
def pressure_to_height_std(pressure): r"""Convert pressure data to heights using the U.S. standard atmosphere. The implementation uses the formula outlined in [Hobbs1977]_ pg.60-61. Parameters ---------- pressure : `pint.Quantity` Atmospheric pressure Returns ------- `pint.Quantity` The corresponding height value(s) Notes
python
{ "resource": "" }
q23603
height_to_geopotential
train
def height_to_geopotential(height): r"""Compute geopotential for a given height. Parameters ---------- height : `pint.Quantity` Height above sea level (array_like) Returns ------- `pint.Quantity` The corresponding geopotential value(s) Examples -------- >>> from metpy.constants import g, G, me, Re >>> import metpy.calc >>> from metpy.units import units
python
{ "resource": "" }
q23604
geopotential_to_height
train
def geopotential_to_height(geopot): r"""Compute height from a given geopotential. Parameters ---------- geopotential : `pint.Quantity` Geopotential (array_like) Returns ------- `pint.Quantity` The corresponding height value(s) Examples -------- >>> from metpy.constants import g, G, me, Re >>> import metpy.calc >>> from metpy.units import units >>> height = np.linspace(0,10000, num = 11) * units.m >>> geopot = metpy.calc.height_to_geopotential(height) >>> geopot <Quantity([ 0. 9817.46806283 19631.85526579 29443.16305888 39251.39289118 49056.54621087 58858.62446525 68657.62910064 78453.56156253 88246.42329545 98036.21574306], 'meter ** 2 / second ** 2')> >>> height = metpy.calc.geopotential_to_height(geopot) >>>
python
{ "resource": "" }
q23605
height_to_pressure_std
train
def height_to_pressure_std(height): r"""Convert height data to pressures using the U.S. standard atmosphere. The implementation inverts the formula outlined in [Hobbs1977]_ pg.60-61. Parameters ---------- height : `pint.Quantity` Atmospheric height Returns ------- `pint.Quantity` The corresponding pressure value(s) Notes
python
{ "resource": "" }
q23606
coriolis_parameter
train
def coriolis_parameter(latitude): r"""Calculate the coriolis parameter at each point. The implementation uses the formula outlined in [Hobbs1977]_ pg.370-371. Parameters ---------- latitude : array_like Latitude at each point Returns ------- `pint.Quantity`
python
{ "resource": "" }
q23607
sigma_to_pressure
train
def sigma_to_pressure(sigma, psfc, ptop): r"""Calculate pressure from sigma values. Parameters ---------- sigma : ndarray The sigma levels to be converted to pressure levels. psfc : `pint.Quantity` The surface pressure value. ptop : `pint.Quantity` The pressure value at the top of the model domain. Returns ------- `pint.Quantity` The pressure values at the given sigma levels. Notes ----- Sigma definition adapted from [Philips1957]_. .. math:: p = \sigma * (p_{sfc} - p_{top}) + p_{top} * :math:`p` is pressure at a given `\sigma` level * :math:`\sigma` is non-dimensional, scaled pressure * :math:`p_{sfc}` is pressure at the surface or
python
{ "resource": "" }
q23608
_check_radians
train
def _check_radians(value, max_radians=2 * np.pi): """Input validation of values that could be in degrees instead of radians. Parameters ---------- value : `pint.Quantity` The input value to check. max_radians : float Maximum absolute value of radians before
python
{ "resource": "" }
q23609
remove_observations_below_value
train
def remove_observations_below_value(x, y, z, val=0): r"""Remove all x, y, and z where z is less than val. Will not destroy original values. Parameters ---------- x: array_like x coordinate. y: array_like y coordinate. z: array_like Observation value. val: float Value at which to threshold z. Returns ------- x, y, z List
python
{ "resource": "" }
q23610
remove_nan_observations
train
def remove_nan_observations(x, y, z): r"""Remove all x, y, and z where z is nan. Will not destroy original values. Parameters ---------- x: array_like x coordinate y: array_like y coordinate z: array_like observation value Returns
python
{ "resource": "" }
q23611
SkewXTick.gridOn
train
def gridOn(self): # noqa: N802 """Control whether the gridline is drawn for this tick.""" return (self._gridOn and (self._has_default_loc()
python
{ "resource": "" }
q23612
SkewXAxes._set_lim_and_transforms
train
def _set_lim_and_transforms(self): """Set limits and transforms. This is called once when the plot is created to set up all the transforms for the data, text and grids. """ # Get the standard transform setup from the Axes base class Axes._set_lim_and_transforms(self) # Need to put the skew in the middle, after the scale and limits, # but before the transAxes. This way, the skew is done in Axes # coordinates thus performing the transform around the proper origin # We keep the
python
{ "resource": "" }
q23613
bzip_blocks_decompress_all
train
def bzip_blocks_decompress_all(data): """Decompress all of the bzip2-ed blocks. Returns the decompressed data as a `bytearray`. """ frames = bytearray() offset = 0 while offset < len(data): size_bytes = data[offset:offset + 4] offset += 4 block_cmp_bytes = abs(Struct('>l').unpack(size_bytes)[0]) try: frames.extend(bz2.decompress(data[offset:offset + block_cmp_bytes])) offset += block_cmp_bytes except IOError: # If we've decompressed any frames, this is an error mid-stream, so warn, stop # trying to decompress and let processing proceed
python
{ "resource": "" }
q23614
nexrad_to_datetime
train
def nexrad_to_datetime(julian_date, ms_midnight): """Convert NEXRAD date time format to python `datetime.datetime`.""" # Subtracting one from julian_date is because epoch date is 1
python
{ "resource": "" }
q23615
remap_status
train
def remap_status(val): """Convert status integer value to appropriate bitmask.""" status = 0 bad = BAD_DATA if val & 0xF0 else 0 val &= 0x0F if val == 0: status = START_ELEVATION elif val == 1: status = 0 elif val == 2: status =
python
{ "resource": "" }
q23616
reduce_lists
train
def reduce_lists(d): """Replace single item lists in a dictionary with the single item.""" for field in d: old_data
python
{ "resource": "" }
q23617
float16
train
def float16(val): """Convert a 16-bit floating point value to a standard Python float.""" # Fraction is 10 LSB, Exponent middle 5, and Sign the MSB frac = val & 0x03ff exp = (val >> 10) & 0x1F sign = val >> 15 if exp:
python
{ "resource": "" }
q23618
date_elem
train
def date_elem(ind_days, ind_minutes): """Create a function to parse a datetime from the product-specific blocks."""
python
{ "resource": "" }
q23619
combine_elem
train
def combine_elem(ind1, ind2): """Create a function to combine two specified product-specific blocks into a single int.""" def inner(seq): shift = 2**16 if seq[ind1] < 0: seq[ind1]
python
{ "resource": "" }
q23620
relative_humidity_from_dewpoint
train
def relative_humidity_from_dewpoint(temperature, dewpt): r"""Calculate the relative humidity. Uses temperature and dewpoint in celsius to calculate relative humidity using the ratio of vapor pressure to saturation vapor pressures. Parameters ----------
python
{ "resource": "" }
q23621
exner_function
train
def exner_function(pressure, reference_pressure=mpconsts.P0): r"""Calculate the Exner function. .. math:: \Pi = \left( \frac{p}{p_0} \right)^\kappa This can be used to calculate potential temperature from temperature (and visa-versa), since .. math:: \Pi = \frac{T}{\theta} Parameters ---------- pressure : `pint.Quantity` The total atmospheric pressure reference_pressure : `pint.Quantity`, optional The reference pressure against which to
python
{ "resource": "" }
q23622
dry_lapse
train
def dry_lapse(pressure, temperature, ref_pressure=None): r"""Calculate the temperature at a level assuming only dry processes. This function lifts a parcel starting at `temperature`, conserving potential temperature. The starting pressure can be given by `ref_pressure`. Parameters ---------- pressure : `pint.Quantity` The atmospheric pressure level(s) of interest temperature : `pint.Quantity` The starting temperature ref_pressure : `pint.Quantity`, optional The reference pressure. If not given, it defaults to the first element of the pressure array. Returns ------- `pint.Quantity`
python
{ "resource": "" }
q23623
moist_lapse
train
def moist_lapse(pressure, temperature, ref_pressure=None): r"""Calculate the temperature at a level assuming liquid saturation processes. This function lifts a parcel starting at `temperature`. The starting pressure can be given by `ref_pressure`. Essentially, this function is calculating moist pseudo-adiabats. Parameters ---------- pressure : `pint.Quantity` The atmospheric pressure level(s) of interest temperature : `pint.Quantity` The starting temperature ref_pressure : `pint.Quantity`, optional The reference pressure. If not given, it defaults to the first element of the pressure array. Returns ------- `pint.Quantity` The temperature corresponding to the starting temperature and pressure levels. See Also -------- dry_lapse : Calculate parcel temperature assuming dry adiabatic processes parcel_profile : Calculate complete parcel profile Notes ----- This function is implemented by integrating the following differential equation: .. math:: \frac{dT}{dP} = \frac{1}{P} \frac{R_d T + L_v r_s} {C_{pd} + \frac{L_v^2 r_s \epsilon}{R_d T^2}} This equation comes from [Bakhshaii2013]_.
python
{ "resource": "" }
q23624
el
train
def el(pressure, temperature, dewpt, parcel_temperature_profile=None): r"""Calculate the equilibrium level. This works by finding the last intersection of the ideal parcel path and the measured environmental temperature. If there is one or fewer intersections, there is no equilibrium level. Parameters ---------- pressure : `pint.Quantity` The atmospheric pressure temperature : `pint.Quantity` The temperature at the levels given by `pressure` dewpt : `pint.Quantity` The dew point at the levels given by `pressure` parcel_temperature_profile: `pint.Quantity`, optional The parcel temperature profile from which to calculate the EL. Defaults to the
python
{ "resource": "" }
q23625
_parcel_profile_helper
train
def _parcel_profile_helper(pressure, temperature, dewpt): """Help calculate parcel profiles. Returns the temperature and pressure, above, below, and including the LCL. The other calculation functions decide what to do with the pieces. """ # Find the LCL press_lcl, temp_lcl = lcl(pressure[0], temperature, dewpt) press_lcl = press_lcl.to(pressure.units) # Find the dry adiabatic profile, *including* the LCL. We need >= the LCL in case the # LCL is included in the levels. It's slightly redundant in that case, but simplifies # the logic for removing it later. press_lower = concatenate((pressure[pressure >= press_lcl], press_lcl)) temp_lower = dry_lapse(press_lower, temperature) # If the pressure profile doesn't make it to the lcl, we can stop here if _greater_or_close(np.nanmin(pressure), press_lcl.m): return (press_lower[:-1], press_lcl, np.array([]) * press_lower.units,
python
{ "resource": "" }
q23626
_insert_lcl_level
train
def _insert_lcl_level(pressure, temperature, lcl_pressure): """Insert the LCL pressure into the profile.""" interp_temp = interpolate_1d(lcl_pressure, pressure, temperature) # Pressure needs to be increasing for searchsorted, so flip it and then convert # the index back to the original array loc
python
{ "resource": "" }
q23627
dewpoint_rh
train
def dewpoint_rh(temperature, rh): r"""Calculate the ambient dewpoint given air temperature and relative humidity. Parameters ---------- temperature : `pint.Quantity` Air temperature rh : `pint.Quantity` Relative humidity expressed as a ratio in the range 0 < rh <= 1 Returns ------- `pint.Quantity`
python
{ "resource": "" }
q23628
dewpoint
train
def dewpoint(e): r"""Calculate the ambient dewpoint given the vapor pressure. Parameters ---------- e : `pint.Quantity` Water vapor partial pressure Returns ------- `pint.Quantity` Dew point temperature See Also -------- dewpoint_rh, saturation_vapor_pressure, vapor_pressure Notes ----- This function inverts the [Bolton1980]_ formula for saturation vapor pressure to instead calculate the temperature. This
python
{ "resource": "" }
q23629
mixing_ratio
train
def mixing_ratio(part_press, tot_press, molecular_weight_ratio=mpconsts.epsilon): r"""Calculate the mixing ratio of a gas. This calculates mixing ratio given its partial pressure and the total pressure of the air. There are no required units for the input arrays, other than that they have the same units. Parameters ---------- part_press : `pint.Quantity` Partial pressure of the constituent gas tot_press : `pint.Quantity` Total air pressure molecular_weight_ratio : `pint.Quantity` or float, optional The ratio of the molecular weight of the constituent gas to that assumed for air. Defaults to the ratio for water vapor to dry air (:math:`\epsilon\approx0.622`). Returns -------
python
{ "resource": "" }
q23630
equivalent_potential_temperature
train
def equivalent_potential_temperature(pressure, temperature, dewpoint): r"""Calculate equivalent potential temperature. This calculation must be given an air parcel's pressure, temperature, and dewpoint. The implementation uses the formula outlined in [Bolton1980]_: First, the LCL temperature is calculated: .. math:: T_{L}=\frac{1}{\frac{1}{T_{D}-56}+\frac{ln(T_{K}/T_{D})}{800}}+56 Which is then used to calculate the potential temperature at the LCL: .. math:: \theta_{DL}=T_{K}\left(\frac{1000}{p-e}\right)^k \left(\frac{T_{K}}{T_{L}}\right)^{.28r} Both of these are used to calculate the final equivalent potential temperature: .. math:: \theta_{E}=\theta_{DL}\exp\left[\left(\frac{3036.}{T_{L}} -1.78\right)*r(1+.448r)\right] Parameters ---------- pressure: `pint.Quantity` Total atmospheric pressure temperature: `pint.Quantity` Temperature of parcel dewpoint: `pint.Quantity` Dewpoint of parcel Returns ------- `pint.Quantity` The equivalent potential temperature of the parcel Notes
python
{ "resource": "" }
q23631
saturation_equivalent_potential_temperature
train
def saturation_equivalent_potential_temperature(pressure, temperature): r"""Calculate saturation equivalent potential temperature. This calculation must be given an air parcel's pressure and temperature. The implementation uses the formula outlined in [Bolton1980]_ for the equivalent potential temperature, and assumes a saturated process. First, because we assume a saturated process, the temperature at the LCL is equivalent to the current temperature. Therefore the following equation .. math:: T_{L}=\frac{1}{\frac{1}{T_{D}-56}+\frac{ln(T_{K}/T_{D})}{800}}+56 reduces to .. math:: T_{L} = T_{K} Then the potential temperature at the temperature/LCL is calculated: .. math:: \theta_{DL}=T_{K}\left(\frac{1000}{p-e}\right)^k \left(\frac{T_{K}}{T_{L}}\right)^{.28r} However, because .. math:: T_{L} = T_{K} it follows that .. math:: \theta_{DL}=T_{K}\left(\frac{1000}{p-e}\right)^k Both of these are used to calculate the final equivalent potential temperature: .. math:: \theta_{E}=\theta_{DL}\exp\left[\left(\frac{3036.}{T_{K}} -1.78\right)*r(1+.448r)\right] Parameters ---------- pressure: `pint.Quantity` Total atmospheric pressure temperature: `pint.Quantity`
python
{ "resource": "" }
q23632
virtual_temperature
train
def virtual_temperature(temperature, mixing, molecular_weight_ratio=mpconsts.epsilon): r"""Calculate virtual temperature. This calculation must be given an air parcel's temperature and mixing ratio. The implementation uses the formula outlined in [Hobbs2006]_ pg.80. Parameters ---------- temperature: `pint.Quantity` The temperature mixing : `pint.Quantity` dimensionless mass mixing ratio molecular_weight_ratio : `pint.Quantity` or float, optional The ratio of the molecular weight of the constituent gas to that assumed for air. Defaults to the ratio for water vapor to dry air. (:math:`\epsilon\approx0.622`). Returns
python
{ "resource": "" }
q23633
virtual_potential_temperature
train
def virtual_potential_temperature(pressure, temperature, mixing, molecular_weight_ratio=mpconsts.epsilon): r"""Calculate virtual potential temperature. This calculation must be given an air parcel's pressure, temperature, and mixing ratio. The implementation uses the formula outlined in [Markowski2010]_ pg.13. Parameters ---------- pressure: `pint.Quantity` Total atmospheric pressure temperature: `pint.Quantity` The temperature mixing : `pint.Quantity` dimensionless mass mixing ratio molecular_weight_ratio : `pint.Quantity` or float, optional The ratio of the molecular weight of the constituent gas to that assumed for air. Defaults to the
python
{ "resource": "" }
q23634
density
train
def density(pressure, temperature, mixing, molecular_weight_ratio=mpconsts.epsilon): r"""Calculate density. This calculation must be given an air parcel's pressure, temperature, and mixing ratio. The implementation uses the formula outlined in [Hobbs2006]_ pg.67. Parameters ---------- temperature: `pint.Quantity` The temperature pressure: `pint.Quantity` Total atmospheric pressure mixing : `pint.Quantity` dimensionless mass mixing ratio molecular_weight_ratio : `pint.Quantity` or float, optional The ratio of the molecular weight of the constituent gas to that assumed for air. Defaults to the ratio for water vapor to dry air.
python
{ "resource": "" }
q23635
relative_humidity_wet_psychrometric
train
def relative_humidity_wet_psychrometric(dry_bulb_temperature, web_bulb_temperature, pressure, **kwargs): r"""Calculate the relative humidity with wet bulb and dry bulb temperatures. This uses a psychrometric relationship as outlined in [WMO8-2014]_, with coefficients from [Fan1987]_. Parameters ---------- dry_bulb_temperature: `pint.Quantity` Dry bulb temperature web_bulb_temperature: `pint.Quantity` Wet bulb temperature pressure: `pint.Quantity` Total atmospheric pressure Returns ------- `pint.Quantity` Relative humidity Notes ----- .. math:: RH =
python
{ "resource": "" }
q23636
psychrometric_vapor_pressure_wet
train
def psychrometric_vapor_pressure_wet(dry_bulb_temperature, wet_bulb_temperature, pressure, psychrometer_coefficient=6.21e-4 / units.kelvin): r"""Calculate the vapor pressure with wet bulb and dry bulb temperatures. This uses a psychrometric relationship as outlined in [WMO8-2014]_, with coefficients from [Fan1987]_. Parameters ---------- dry_bulb_temperature: `pint.Quantity` Dry bulb temperature wet_bulb_temperature: `pint.Quantity` Wet bulb temperature pressure: `pint.Quantity` Total atmospheric pressure psychrometer_coefficient: `pint.Quantity`, optional Psychrometer coefficient. Defaults to 6.21e-4 K^-1. Returns ------- `pint.Quantity` Vapor pressure Notes ----- .. math:: e' = e'_w(T_w) - A p (T - T_w)
python
{ "resource": "" }
q23637
cape_cin
train
def cape_cin(pressure, temperature, dewpt, parcel_profile): r"""Calculate CAPE and CIN. Calculate the convective available potential energy (CAPE) and convective inhibition (CIN) of a given upper air profile and parcel path. CIN is integrated between the surface and LFC, CAPE is integrated between the LFC and EL (or top of sounding). Intersection points of the measured temperature profile and parcel profile are linearly interpolated. Parameters ---------- pressure : `pint.Quantity` The atmospheric pressure level(s) of interest. The first entry should be the starting point pressure. temperature : `pint.Quantity` The atmospheric temperature corresponding to pressure. dewpt : `pint.Quantity` The atmospheric dew point corresponding to pressure. parcel_profile : `pint.Quantity` The temperature profile of the parcel Returns ------- `pint.Quantity` Convective available potential energy (CAPE). `pint.Quantity` Convective inhibition (CIN). Notes ----- Formula adopted from [Hobbs1977]_. .. math:: \text{CAPE} = -R_d \int_{LFC}^{EL} (T_{parcel} - T_{env}) d\text{ln}(p) .. math:: \text{CIN} = -R_d \int_{SFC}^{LFC} (T_{parcel} - T_{env}) d\text{ln}(p) * :math:`CAPE` Convective available potential energy * :math:`CIN` Convective inhibition * :math:`LFC` Pressure
python
{ "resource": "" }
q23638
_find_append_zero_crossings
train
def _find_append_zero_crossings(x, y): r""" Find and interpolate zero crossings. Estimate the zero crossings of an x,y series and add estimated crossings to series, returning a sorted array with no duplicate values. Parameters ---------- x : `pint.Quantity` x values of data y : `pint.Quantity` y values of data Returns ------- x : `pint.Quantity` x values of data y : `pint.Quantity` y values of data """ # Find and append crossings to the data crossings = find_intersections(x[1:], y[1:], np.zeros_like(y[1:]) * y.units) x =
python
{ "resource": "" }
q23639
most_unstable_parcel
train
def most_unstable_parcel(pressure, temperature, dewpoint, heights=None, bottom=None, depth=300 * units.hPa): """ Determine the most unstable parcel in a layer. Determines the most unstable parcel of air by calculating the equivalent potential temperature and finding its maximum in the specified layer. Parameters ---------- pressure: `pint.Quantity` Atmospheric pressure profile temperature: `pint.Quantity` Atmospheric temperature profile dewpoint: `pint.Quantity` Atmospheric dewpoint profile heights: `pint.Quantity`, optional Atmospheric height profile. Standard atmosphere assumed when None (the default). bottom: `pint.Quantity`, optional Bottom of the layer to consider for the calculation in pressure or height. Defaults to using the bottom pressure or height. depth: `pint.Quantity`, optional Depth of the layer to consider for the
python
{ "resource": "" }
q23640
surface_based_cape_cin
train
def surface_based_cape_cin(pressure, temperature, dewpoint): r"""Calculate surface-based CAPE and CIN. Calculate the convective available potential energy (CAPE) and convective inhibition (CIN) of a given upper air profile for a surface-based parcel. CIN is integrated between the surface and LFC, CAPE is integrated between the LFC and EL (or top of sounding). Intersection points of the measured temperature profile and parcel profile are linearly interpolated. Parameters ---------- pressure : `pint.Quantity` Atmospheric pressure profile. The first entry should be the starting (surface) observation. temperature : `pint.Quantity` Temperature profile
python
{ "resource": "" }
q23641
mixed_parcel
train
def mixed_parcel(p, temperature, dewpt, parcel_start_pressure=None, heights=None, bottom=None, depth=100 * units.hPa, interpolate=True): r"""Calculate the properties of a parcel mixed from a layer. Determines the properties of an air parcel that is the result of complete mixing of a given atmospheric layer. Parameters ---------- p : `pint.Quantity` Atmospheric pressure profile temperature : `pint.Quantity` Atmospheric temperature profile dewpt : `pint.Quantity` Atmospheric dewpoint profile parcel_start_pressure : `pint.Quantity`, optional Pressure at which the mixed parcel should begin (default None) heights: `pint.Quantity`, optional Atmospheric heights corresponding to the given pressures (default None) bottom : `pint.Quantity`, optional The bottom of the layer as a pressure or height above the surface pressure (default None) depth : `pint.Quantity`, optional The thickness of the layer as a pressure or height above the bottom of the layer (default 100 hPa) interpolate : bool, optional Interpolate the top and bottom points if they are not in the given data Returns ------- `pint.Quantity, pint.Quantity, pint.Quantity` The pressure, temperature, and dewpoint of the mixed parcel. """ # If a parcel starting pressure is not provided, use the surface if not parcel_start_pressure: parcel_start_pressure = p[0]
python
{ "resource": "" }
q23642
dry_static_energy
train
def dry_static_energy(heights, temperature): r"""Calculate the dry static energy of parcels. This function will calculate the dry static energy following the first two terms of equation 3.72 in [Hobbs2006]_. Notes ----- .. math::\text{dry static energy} = c_{pd} * T + gz * :math:`T` is temperature * :math:`z` is height Parameters ----------
python
{ "resource": "" }
q23643
moist_static_energy
train
def moist_static_energy(heights, temperature, specific_humidity): r"""Calculate the moist static energy of parcels. This function will calculate the moist static energy following equation 3.72 in [Hobbs2006]_. Notes ----- .. math::\text{moist static energy} = c_{pd} * T + gz + L_v q * :math:`T` is temperature
python
{ "resource": "" }
q23644
thickness_hydrostatic
train
def thickness_hydrostatic(pressure, temperature, **kwargs): r"""Calculate the thickness of a layer via the hypsometric equation. This thickness calculation uses the pressure and temperature profiles (and optionally mixing ratio) via the hypsometric equation with virtual temperature adjustment .. math:: Z_2 - Z_1 = -\frac{R_d}{g} \int_{p_1}^{p_2} T_v d\ln p, which is based off of Equation 3.24 in [Hobbs2006]_. This assumes a hydrostatic atmosphere. Layer bottom and depth specified in pressure. Parameters ---------- pressure : `pint.Quantity` Atmospheric pressure profile temperature : `pint.Quantity` Atmospheric temperature profile mixing : `pint.Quantity`, optional Profile of dimensionless mass mixing ratio. If none is given, virtual temperature is simply set to be the given temperature. molecular_weight_ratio : `pint.Quantity` or float, optional The ratio of the molecular weight of the constituent gas to that assumed for air. Defaults to the ratio for water vapor to dry air. (:math:`\epsilon\approx0.622`). bottom : `pint.Quantity`, optional The bottom of the layer in pressure. Defaults to the first observation. depth : `pint.Quantity`, optional The depth of the layer in hPa. Defaults to the full profile if bottom is not given, and 100 hPa if bottom is given. Returns ------- `pint.Quantity` The thickness of the layer in meters. See Also -------- thickness_hydrostatic_from_relative_humidity, pressure_to_height_std, virtual_temperature """ mixing = kwargs.pop('mixing', None) molecular_weight_ratio = kwargs.pop('molecular_weight_ratio', mpconsts.epsilon) bottom
python
{ "resource": "" }
q23645
thickness_hydrostatic_from_relative_humidity
train
def thickness_hydrostatic_from_relative_humidity(pressure, temperature, relative_humidity, **kwargs): r"""Calculate the thickness of a layer given pressure, temperature and relative humidity. Similar to ``thickness_hydrostatic``, this thickness calculation uses the pressure, temperature, and relative humidity profiles via the hypsometric equation with virtual temperature adjustment. .. math:: Z_2 - Z_1 = -\frac{R_d}{g} \int_{p_1}^{p_2} T_v d\ln p, which is based off of Equation 3.24 in [Hobbs2006]_. Virtual temperature is calculated from the profiles of temperature and relative humidity. This assumes a hydrostatic atmosphere. Layer bottom and depth specified in pressure. Parameters ---------- pressure : `pint.Quantity` Atmospheric pressure profile temperature : `pint.Quantity` Atmospheric temperature profile relative_humidity : `pint.Quantity` Atmospheric relative humidity profile. The relative humidity is expressed as a unitless ratio in the range [0, 1]. Can also pass a percentage if proper units are attached. bottom : `pint.Quantity`, optional The bottom of the layer in pressure. Defaults to the first observation. depth : `pint.Quantity`, optional The depth of the layer in hPa. Defaults to the
python
{ "resource": "" }
q23646
brunt_vaisala_frequency_squared
train
def brunt_vaisala_frequency_squared(heights, potential_temperature, axis=0): r"""Calculate the square of the Brunt-Vaisala frequency. Brunt-Vaisala frequency squared (a measure of atmospheric stability) is given by the formula: .. math:: N^2 = \frac{g}{\theta} \frac{d\theta}{dz} This formula is based off of Equations 3.75 and 3.77 in [Hobbs2006]_. Parameters ---------- heights : array-like One-dimensional profile of atmospheric height potential_temperature : array-like Atmospheric potential temperature axis : int, optional The axis corresponding to vertical in the potential temperature array, defaults to 0. Returns ------- array-like The square of the Brunt-Vaisala frequency.
python
{ "resource": "" }
q23647
brunt_vaisala_frequency
train
def brunt_vaisala_frequency(heights, potential_temperature, axis=0): r"""Calculate the Brunt-Vaisala frequency. This function will calculate the Brunt-Vaisala frequency as follows: .. math:: N = \left( \frac{g}{\theta} \frac{d\theta}{dz} \right)^\frac{1}{2} This formula based off of Equations 3.75 and 3.77 in [Hobbs2006]_. This function is a wrapper for `brunt_vaisala_frequency_squared` that filters out negative (unstable) quanties and takes the square root. Parameters ---------- heights : array-like One-dimensional profile of atmospheric height potential_temperature : array-like Atmospheric potential temperature axis : int, optional The axis corresponding to vertical in the potential temperature array, defaults to 0.
python
{ "resource": "" }
q23648
brunt_vaisala_period
train
def brunt_vaisala_period(heights, potential_temperature, axis=0): r"""Calculate the Brunt-Vaisala period. This function is a helper function for `brunt_vaisala_frequency` that calculates the period of oscilation as in Exercise 3.13 of [Hobbs2006]_: .. math:: \tau = \frac{2\pi}{N} Returns `NaN` when :math:`N^2 > 0`. Parameters ---------- heights : array-like One-dimensional profile of atmospheric height potential_temperature : array-like Atmospheric potential temperature axis : int, optional The axis corresponding to vertical in the potential temperature array, defaults to 0. Returns ------- array-like Brunt-Vaisala
python
{ "resource": "" }
q23649
wet_bulb_temperature
train
def wet_bulb_temperature(pressure, temperature, dewpoint): """Calculate the wet-bulb temperature using Normand's rule. This function calculates the wet-bulb temperature using the Normand method. The LCL is computed, and that parcel brought down to the starting pressure along a moist adiabat. The Normand method (and others) are described and compared by [Knox2017]_. Parameters ---------- pressure : `pint.Quantity` Initial atmospheric pressure temperature : `pint.Quantity` Initial atmospheric temperature dewpoint : `pint.Quantity` Initial atmospheric dewpoint Returns ------- array-like Wet-bulb temperature See Also -------- lcl, moist_lapse """ if not hasattr(pressure, 'shape'): pressure = atleast_1d(pressure) temperature = atleast_1d(temperature) dewpoint = atleast_1d(dewpoint) it = np.nditer([pressure, temperature, dewpoint, None], op_dtypes=['float', 'float', 'float', 'float'], flags=['buffered']) for press, temp, dewp, ret in it: press = press * pressure.units
python
{ "resource": "" }
q23650
static_stability
train
def static_stability(pressure, temperature, axis=0): r"""Calculate the static stability within a vertical profile. .. math:: \sigma = -\frac{RT}{p} \frac{\partial \ln \theta}{\partial p} This formuala is based on equation 4.3.6 in [Bluestein1992]_. Parameters ---------- pressure : array-like Profile of atmospheric pressure temperature : array-like Profile of temperature axis : int, optional The axis corresponding to vertical in the pressure and temperature arrays, defaults to 0. Returns ------- array-like
python
{ "resource": "" }
q23651
dewpoint_from_specific_humidity
train
def dewpoint_from_specific_humidity(specific_humidity, temperature, pressure): r"""Calculate the dewpoint from specific humidity, temperature, and pressure. Parameters ---------- specific_humidity: `pint.Quantity` Specific humidity of air temperature: `pint.Quantity` Air temperature pressure: `pint.Quantity`
python
{ "resource": "" }
q23652
vertical_velocity_pressure
train
def vertical_velocity_pressure(w, pressure, temperature, mixing=0): r"""Calculate omega from w assuming hydrostatic conditions. This function converts vertical velocity with respect to height :math:`\left(w = \frac{Dz}{Dt}\right)` to that with respect to pressure :math:`\left(\omega = \frac{Dp}{Dt}\right)` assuming hydrostatic conditions on the synoptic scale. By Equation 7.33 in [Hobbs2006]_, .. math: \omega \simeq -\rho g w Density (:math:`\rho`) is calculated using the :func:`density` function, from the given pressure and temperature. If `mixing` is given, the virtual temperature correction is used, otherwise, dry air is assumed. Parameters ---------- w: `pint.Quantity` Vertical velocity in terms of height pressure: `pint.Quantity`
python
{ "resource": "" }
q23653
vertical_velocity
train
def vertical_velocity(omega, pressure, temperature, mixing=0): r"""Calculate w from omega assuming hydrostatic conditions. This function converts vertical velocity with respect to pressure :math:`\left(\omega = \frac{Dp}{Dt}\right)` to that with respect to height :math:`\left(w = \frac{Dz}{Dt}\right)` assuming hydrostatic conditions on the synoptic scale. By Equation 7.33 in [Hobbs2006]_, .. math: \omega \simeq -\rho g w so that .. math w \simeq \frac{- \omega}{\rho g} Density (:math:`\rho`) is calculated using the :func:`density` function, from the given pressure and temperature. If `mixing` is given, the virtual temperature correction is used, otherwise, dry air is assumed. Parameters ---------- omega: `pint.Quantity` Vertical velocity in terms of pressure pressure: `pint.Quantity`
python
{ "resource": "" }
q23654
make_geo
train
def make_geo(attrs_dict, globe): """Handle geostationary projection.""" attr_mapping = [('satellite_height', 'perspective_point_height'), ('sweep_axis', 'sweep_angle_axis')] kwargs = CFProjection.build_projection_kwargs(attrs_dict, attr_mapping) # CartoPy can't handle central latitude for Geostationary (nor should it) # Just remove it if it's 0. if not kwargs.get('central_latitude'): kwargs.pop('central_latitude', None)
python
{ "resource": "" }
q23655
make_lcc
train
def make_lcc(attrs_dict, globe): """Handle Lambert conformal conic projection.""" attr_mapping = [('central_longitude', 'longitude_of_central_meridian'), ('standard_parallels', 'standard_parallel')] kwargs = CFProjection.build_projection_kwargs(attrs_dict, attr_mapping) if 'standard_parallels' in kwargs: try:
python
{ "resource": "" }
q23656
make_mercator
train
def make_mercator(attrs_dict, globe): """Handle Mercator projection.""" attr_mapping = [('latitude_true_scale', 'standard_parallel'), ('scale_factor', 'scale_factor_at_projection_origin')] kwargs = CFProjection.build_projection_kwargs(attrs_dict, attr_mapping)
python
{ "resource": "" }
q23657
make_stereo
train
def make_stereo(attrs_dict, globe): """Handle generic stereographic projection.""" attr_mapping = [('scale_factor',
python
{ "resource": "" }
q23658
CFProjection.build_projection_kwargs
train
def build_projection_kwargs(cls, source, mapping): """Handle mapping a dictionary of metadata to keyword arguments."""
python
{ "resource": "" }
q23659
CFProjection._map_arg_names
train
def _map_arg_names(source, mapping): """Map one set of keys to another.""" return
python
{ "resource": "" }
q23660
CFProjection.cartopy_globe
train
def cartopy_globe(self): """Initialize a `cartopy.crs.Globe` from the metadata.""" if 'earth_radius' in self._attrs: kwargs = {'ellipse': 'sphere', 'semimajor_axis': self._attrs['earth_radius'], 'semiminor_axis': self._attrs['earth_radius']} else: attr_mapping = [('semimajor_axis', 'semi_major_axis'), ('semiminor_axis', 'semi_minor_axis'), ('inverse_flattening', 'inverse_flattening')]
python
{ "resource": "" }
q23661
CFProjection.to_cartopy
train
def to_cartopy(self): """Convert to a CartoPy projection.""" globe = self.cartopy_globe proj_name = self._attrs['grid_mapping_name'] try: proj_handler = self.projection_registry[proj_name] except KeyError:
python
{ "resource": "" }
q23662
add_timestamp
train
def add_timestamp(ax, time=None, x=0.99, y=-0.04, ha='right', high_contrast=False, pretext='Created: ', time_format='%Y-%m-%dT%H:%M:%SZ', **kwargs): """Add a timestamp to a plot. Adds a timestamp to a plot, defaulting to the time of plot creation in ISO format. Parameters ---------- ax : `matplotlib.axes.Axes` The `Axes` instance used for plotting time : `datetime.datetime` Specific time to be plotted - datetime.utcnow will be use if not specified x : float Relative x position on the axes of the timestamp y : float Relative y position on the axes of the timestamp ha : str Horizontal alignment of the time stamp string high_contrast : bool Outline text for increased contrast pretext : str Text to appear before the timestamp, optional. Defaults
python
{ "resource": "" }
q23663
_add_logo
train
def _add_logo(fig, x=10, y=25, zorder=100, which='metpy', size='small', **kwargs): """Add the MetPy or Unidata logo to a figure. Adds an image to the figure. Parameters ---------- fig : `matplotlib.figure` The `figure` instance used for plotting x : int x position padding in pixels y : float y position padding in pixels zorder : int The zorder of the logo which : str Which logo to plot 'metpy' or 'unidata' size : str Size of logo to be used. Can be 'small' for 75 px square or 'large' for 150 px square. Returns ------- `matplotlib.image.FigureImage` The `matplotlib.image.FigureImage` instance created """ fname_suffix
python
{ "resource": "" }
q23664
add_metpy_logo
train
def add_metpy_logo(fig, x=10, y=25, zorder=100, size='small', **kwargs): """Add the MetPy logo to a figure. Adds an image of the MetPy logo to the figure. Parameters ---------- fig : `matplotlib.figure` The `figure` instance used for plotting x : int x position padding in pixels y : float y position padding in pixels zorder : int The zorder of the logo size : str Size of logo to be used. Can be 'small' for 75 px square or 'large' for
python
{ "resource": "" }
q23665
colored_line
train
def colored_line(x, y, c, **kwargs): """Create a multi-colored line. Takes a set of points and turns them into a collection of lines colored by another array. Parameters ---------- x : array-like x-axis coordinates y : array-like y-axis coordinates c : array-like values used for color-mapping kwargs : dict Other keyword arguments passed to :class:`matplotlib.collections.LineCollection` Returns ------- The created :class:`matplotlib.collections.LineCollection` instance. """ # Mask out any NaN values nan_mask = ~(np.isnan(x) | np.isnan(y) | np.isnan(c)) x = x[nan_mask] y = y[nan_mask] c = c[nan_mask] # Paste values end to end points = concatenate([x, y]) # Exploit numpy's strides to present a view of these points without copying. # Dimensions are (segment, start/end, x/y). Since x and
python
{ "resource": "" }
q23666
convert_gempak_color
train
def convert_gempak_color(c, style='psc'): """Convert GEMPAK color numbers into corresponding Matplotlib colors. Takes a sequence of GEMPAK color numbers and turns them into equivalent Matplotlib colors. Various GEMPAK quirks are respected, such as treating negative values as equivalent to 0. Parameters ---------- c : int or sequence of ints GEMPAK color number(s) style : str, optional The GEMPAK 'device' to use to interpret color numbers. May be 'psc' (the default; best for a white background) or 'xw' (best for a black background). Returns ------- List of strings of Matplotlib colors, or a single string if only one color requested. """ def normalize(x): """Transform input x to an int in range 0 to 31 consistent with GEMPAK color quirks.""" x = int(x) if x < 0 or x == 101: x = 0 else: x = x % 32 return x # Define GEMPAK colors (Matplotlib doesn't appear to like numbered variants) cols = ['white', # 0/32 'black', # 1 'red', # 2 'green', # 3 'blue', # 4 'yellow', # 5 'cyan', # 6 'magenta', # 7 '#CD6839', # 8 (sienna3) '#FF8247', # 9 (sienna1) '#FFA54F', # 10 (tan1) '#FFAEB9', # 11 (LightPink1) '#FF6A6A', # 12 (IndianRed1) '#EE2C2C', # 13 (firebrick2) '#8B0000',
python
{ "resource": "" }
q23667
process_msg3
train
def process_msg3(fname): """Handle information for message type 3.""" with open(fname, 'r') as infile: info = [] for lineno, line in enumerate(infile): parts = line.split(' ') try: var_name, desc, typ, units = parts[:4] size_hw = parts[-1] if '-' in size_hw: start, end = map(int, size_hw.split('-')) size = (end - start + 1) * 2 else: size = 2 assert size >= 2 fmt = fix_type(typ, size) var_name = fix_var_name(var_name) full_desc = fix_desc(desc, units)
python
{ "resource": "" }
q23668
process_msg18
train
def process_msg18(fname): """Handle information for message type 18.""" with open(fname, 'r') as infile: info = [] for lineno, line in enumerate(infile): parts = line.split(' ') try: if len(parts) == 8: parts = parts[:6] + [parts[6] + parts[7]] var_name, desc, typ, units, rng, prec, byte_range = parts start, end = map(int, byte_range.split('-')) size = end - start + 1 assert size >= 4 fmt = fix_type(typ, size, additional=[('See Note (5)', ('{size}s', 1172))]) if ' ' in var_name: warnings.warn('Space in {}'.format(var_name)) if not desc: warnings.warn('null description for {}'.format(var_name)) var_name = fix_var_name(var_name)
python
{ "resource": "" }
q23669
fix_type
train
def fix_type(typ, size, additional=None): """Fix up creating the appropriate struct type based on the information in the column.""" if additional is not None: my_types = types + additional else: my_types = types for t, info in my_types: if callable(t): matches = t(typ) else: matches = t == typ if matches: if callable(info): fmt_str, true_size = info(size)
python
{ "resource": "" }
q23670
fix_var_name
train
def fix_var_name(var_name): """Clean up and apply standard formatting to variable names.""" name = var_name.strip() for char in '(). /#,': name = name.replace(char, '_')
python
{ "resource": "" }
q23671
fix_desc
train
def fix_desc(desc, units=None): """Clean up description column.""" full_desc = desc.strip() if units and
python
{ "resource": "" }
q23672
write_file
train
def write_file(fname, info): """Write out the generated Python code.""" with open(fname, 'w') as outfile: # File header outfile.write('# Copyright (c) 2018 MetPy Developers.\n') outfile.write('# Distributed under the terms of the BSD 3-Clause License.\n') outfile.write('# SPDX-License-Identifier: BSD-3-Clause\n\n') outfile.write('# flake8: noqa\n') outfile.write('# Generated file -- do not modify\n') # Variable descriptions outfile.write('descriptions = {') outdata = ',\n '.join('"{name}": "{desc}"'.format(
python
{ "resource": "" }
q23673
pandas_dataframe_to_unit_arrays
train
def pandas_dataframe_to_unit_arrays(df, column_units=None): """Attach units to data in pandas dataframes and return united arrays. Parameters ---------- df : `pandas.DataFrame` Data in pandas dataframe. column_units : dict Dictionary of units to attach to columns of the dataframe. Overrides the units attribute if it is attached to the dataframe. Returns ------- Dictionary containing united arrays with keys corresponding to the dataframe column names. """ if not column_units: try: column_units = df.units except AttributeError:
python
{ "resource": "" }
q23674
concatenate
train
def concatenate(arrs, axis=0): r"""Concatenate multiple values into a new unitized object. This is essentially a unit-aware version of `numpy.concatenate`. All items must be able to be converted to the same units. If an item has no units, it will be given those of the rest of the collection, without conversion. The first units found in the arguments is used as the final output units. Parameters ---------- arrs : Sequence of arrays The items to be joined together axis : integer, optional
python
{ "resource": "" }
q23675
diff
train
def diff(x, **kwargs): """Calculate the n-th discrete difference along given axis. Wraps :func:`numpy.diff` to handle units. Parameters ---------- x : array-like Input data n : int, optional The number of times values are differenced. axis : int, optional The axis along which the difference is taken, default is the last axis. Returns ------- diff : ndarray The n-th differences. The shape of the output is the same as `a` except along `axis` where the dimension is smaller by `n`. The type of the output is the
python
{ "resource": "" }
q23676
atleast_1d
train
def atleast_1d(*arrs): r"""Convert inputs to arrays with at least one dimension. Scalars are converted to 1-dimensional arrays, whilst other higher-dimensional inputs are preserved. This is a thin wrapper around `numpy.atleast_1d` to preserve units. Parameters ---------- arrs : arbitrary positional arguments Input arrays to be converted if necessary Returns ------- `pint.Quantity` A single quantity or a list of quantities, matching the number of inputs. """ mags = [a.magnitude if hasattr(a, 'magnitude') else a for a in arrs] orig_units = [a.units if hasattr(a, 'units') else None for
python
{ "resource": "" }
q23677
_check_argument_units
train
def _check_argument_units(args, dimensionality): """Yield arguments with improper dimensionality.""" for arg, val in args.items(): # Get the needed dimensionality (for printing) as well as cached, parsed version # for this argument. try: need, parsed = dimensionality[arg] except KeyError: # Argument did not have units specified in decorator continue # See if the value passed in is appropriate try:
python
{ "resource": "" }
q23678
interpolate_to_slice
train
def interpolate_to_slice(data, points, interp_type='linear'): r"""Obtain an interpolated slice through data using xarray. Utilizing the interpolation functionality in `xarray`, this function takes a slice the given data (currently only regular grids are supported), which is given as an `xarray.DataArray` so that we can utilize its coordinate metadata. Parameters ---------- data: `xarray.DataArray` or `xarray.Dataset` Three- (or higher) dimensional field(s) to interpolate. The DataArray (or each DataArray in the Dataset) must have been parsed by MetPy and include both an x and y coordinate dimension. points: (N, 2) array_like A list
python
{ "resource": "" }
q23679
geodesic
train
def geodesic(crs, start, end, steps): r"""Construct a geodesic path between two points. This function acts as a wrapper for the geodesic construction available in `pyproj`. Parameters ---------- crs: `cartopy.crs` Cartopy Coordinate Reference System to use for the output start: (2, ) array_like A latitude-longitude pair designating the start point of the geodesic (units are degrees north and degrees east). end: (2, ) array_like A latitude-longitude pair designating the end point of the geodesic (units are degrees
python
{ "resource": "" }
q23680
cross_section
train
def cross_section(data, start, end, steps=100, interp_type='linear'): r"""Obtain an interpolated cross-sectional slice through gridded data. Utilizing the interpolation functionality in `xarray`, this function takes a vertical cross-sectional slice along a geodesic through the given data on a regular grid, which is given as an `xarray.DataArray` so that we can utilize its coordinate and projection metadata. Parameters ---------- data: `xarray.DataArray` or `xarray.Dataset` Three- (or higher) dimensional field(s) to interpolate. The DataArray (or each DataArray in the Dataset) must have been parsed by MetPy and include both an x and y coordinate dimension and the added `crs` coordinate. start: (2, ) array_like A latitude-longitude pair designating the start point of the cross section (units are degrees north and degrees east). end: (2, ) array_like A latitude-longitude pair designating the end point of the cross section (units are degrees north and degrees east). steps: int, optional The number of points along the geodesic between the start and the end point (including the end points) to use in the cross section. Defaults to 100. interp_type: str, optional The interpolation method, either 'linear' or 'nearest' (see `xarray.DataArray.interp()` for details). Defaults to 'linear'. Returns ------- `xarray.DataArray` or `xarray.Dataset` The interpolated cross section, with new index dimension along the cross-section. See Also -------- interpolate_to_slice, geodesic """ if isinstance(data, xr.Dataset): # Recursively apply to dataset return data.apply(cross_section, True, (start, end), steps=steps,
python
{ "resource": "" }
q23681
preprocess_xarray
train
def preprocess_xarray(func): """Decorate a function to convert all DataArray arguments to pint.Quantities. This uses the metpy xarray accessors to do the actual conversion. """ @functools.wraps(func) def wrapper(*args, **kwargs): args = tuple(a.metpy.unit_array if isinstance(a, xr.DataArray) else a for a in args) kwargs
python
{ "resource": "" }
q23682
check_matching_coordinates
train
def check_matching_coordinates(func): """Decorate a function to make sure all given DataArrays have matching coordinates.""" @functools.wraps(func) def wrapper(*args, **kwargs): data_arrays = ([a for a in args if isinstance(a, xr.DataArray)] + [a for a in kwargs.values() if isinstance(a, xr.DataArray)]) if len(data_arrays) > 1: first = data_arrays[0] for other in data_arrays[1:]:
python
{ "resource": "" }
q23683
_reassign_quantity_indexer
train
def _reassign_quantity_indexer(data, indexers): """Reassign a units.Quantity indexer to units of relevant coordinate.""" def _to_magnitude(val, unit): try: return val.to(unit).m except AttributeError: return val for coord_name in indexers: # Handle axis types for DataArrays if (isinstance(data, xr.DataArray) and coord_name not in data.dims and coord_name in readable_to_cf_axes): axis = coord_name coord_name = next(data.metpy.coordinates(axis)).name indexers[coord_name] = indexers[axis] del indexers[axis] # Handle slices of quantities if isinstance(indexers[coord_name], slice):
python
{ "resource": "" }
q23684
resample_nn_1d
train
def resample_nn_1d(a, centers): """Return one-dimensional nearest-neighbor indexes based on user-specified centers. Parameters ---------- a : array-like 1-dimensional array of numeric values from which to extract indexes of nearest-neighbors centers : array-like 1-dimensional array of numeric values representing a subset of values to approximate Returns
python
{ "resource": "" }
q23685
nearest_intersection_idx
train
def nearest_intersection_idx(a, b): """Determine the index of the point just before two lines with common x values. Parameters ---------- a : array-like 1-dimensional array of y-values for line 1 b : array-like 1-dimensional array of y-values for line 2 Returns
python
{ "resource": "" }
q23686
find_intersections
train
def find_intersections(x, a, b, direction='all'): """Calculate the best estimate of intersection. Calculates the best estimates of the intersection of two y-value data sets that share a common x-value set. Parameters ---------- x : array-like 1-dimensional array of numeric x-values a : array-like 1-dimensional array of y-values for line 1 b : array-like 1-dimensional array of y-values for line 2 direction : string, optional specifies direction of crossing. 'all', 'increasing' (a becoming greater than b), or 'decreasing' (b becoming greater than a). Defaults to 'all'. Returns ------- A tuple (x, y) of array-like with the x and y coordinates of the intersections of the lines. """ # Find the index of the points just before the intersection(s) nearest_idx = nearest_intersection_idx(a, b) next_idx = nearest_idx + 1 # Determine the sign of the change sign_change = np.sign(a[next_idx] - b[next_idx]) # x-values around each intersection _, x0 = _next_non_masked_element(x, nearest_idx) _, x1 = _next_non_masked_element(x, next_idx) # y-values around each intersection for the first line _, a0 = _next_non_masked_element(a, nearest_idx) _, a1 = _next_non_masked_element(a, next_idx) # y-values around each intersection for the second line _, b0 = _next_non_masked_element(b, nearest_idx) _, b1 = _next_non_masked_element(b, next_idx) # Calculate the x-intersection. This comes from finding the equations of the two lines, # one through (x0, a0) and (x1, a1) and the other through (x0, b0) and (x1, b1), # finding their intersection, and reducing with a bunch of algebra. delta_y0 = a0 - b0 delta_y1 = a1 - b1 intersect_x = (delta_y1 *
python
{ "resource": "" }
q23687
_next_non_masked_element
train
def _next_non_masked_element(a, idx): """Return the next non masked element of a masked array. If an array is masked, return the next non-masked element (if the given index is masked). If no other unmasked points are after the given masked point, returns none. Parameters ---------- a : array-like 1-dimensional array of numeric values idx : integer index of requested element Returns ------- Index of next non-masked element and next non-masked element """ try:
python
{ "resource": "" }
q23688
_delete_masked_points
train
def _delete_masked_points(*arrs): """Delete masked points from arrays. Takes arrays and removes masked points to help with calculations and plotting. Parameters ---------- arrs : one or more array-like source arrays Returns ------- arrs : one or more array-like arrays with masked elements
python
{ "resource": "" }
q23689
reduce_point_density
train
def reduce_point_density(points, radius, priority=None): r"""Return a mask to reduce the density of points in irregularly-spaced data. This function is used to down-sample a collection of scattered points (e.g. surface data), returning a mask that can be used to select the points from one or more arrays (e.g. arrays of temperature and dew point). The points selected can be controlled by providing an array of ``priority`` values (e.g. rainfall totals to ensure that stations with higher precipitation remain in the mask). Parameters ---------- points : (N, K) array-like N locations of the points in K dimensional space radius : float minimum radius allowed between points priority : (N, K) array-like, optional If given, this should have the same shape as ``points``; these values will be used to control selection priority for points. Returns ------- (N,) array-like of boolean values indicating whether points should be kept. This can be used directly to index numpy arrays to return only the desired points. Examples -------- >>> metpy.calc.reduce_point_density(np.array([1, 2, 3]), 1.) array([ True, False, True]) >>> metpy.calc.reduce_point_density(np.array([1, 2, 3]), 1., ... priority=np.array([0.1, 0.9, 0.3])) array([False, True, False]) """ # Handle 1D input if points.ndim < 2: points = points.reshape(-1, 1) # Make a kd-tree to speed searching of data. tree = cKDTree(points) # Need to
python
{ "resource": "" }
q23690
_get_bound_pressure_height
train
def _get_bound_pressure_height(pressure, bound, heights=None, interpolate=True): """Calculate the bounding pressure and height in a layer. Given pressure, optional heights, and a bound, return either the closest pressure/height or interpolated pressure/height. If no heights are provided, a standard atmosphere is assumed. Parameters ---------- pressure : `pint.Quantity` Atmospheric pressures bound : `pint.Quantity` Bound to retrieve (in pressure or height) heights : `pint.Quantity`, optional Atmospheric heights associated with the pressure levels. Defaults to using heights calculated from ``pressure`` assuming a standard atmosphere. interpolate : boolean, optional Interpolate the bound or return the nearest. Defaults to True. Returns ------- `pint.Quantity` The bound pressure and height. """ # Make sure pressure is monotonically decreasing sort_inds = np.argsort(pressure)[::-1] pressure = pressure[sort_inds] if heights is not None: heights = heights[sort_inds] # Bound is given in pressure if bound.dimensionality == {'[length]': -1.0, '[mass]': 1.0, '[time]': -2.0}: # If the bound is in the pressure data, we know the pressure bound exactly if bound in pressure: bound_pressure = bound # If we have heights, we know the exact height value, otherwise return standard # atmosphere height for the pressure if heights is not None: bound_height = heights[pressure == bound_pressure] else: bound_height = pressure_to_height_std(bound_pressure) # If bound is not in the data, return the nearest or interpolated values else: if interpolate: bound_pressure = bound # Use the user specified bound if heights is not None: # Interpolate heights from the height data bound_height = log_interpolate_1d(bound_pressure, pressure, heights) else:
python
{ "resource": "" }
q23691
get_layer_heights
train
def get_layer_heights(heights, depth, *args, **kwargs): """Return an atmospheric layer from upper air data with the requested bottom and depth. This function will subset an upper air dataset to contain only the specified layer using the heights only. Parameters ---------- heights : array-like Atmospheric heights depth : `pint.Quantity` The thickness of the layer *args : array-like Atmospheric variable(s) measured at the given pressures bottom : `pint.Quantity`, optional The bottom of the layer interpolate : bool, optional Interpolate the top and bottom points if they are not in the given data. Defaults to True. with_agl : bool, optional Returns the heights as above ground level by subtracting the minimum height in the provided heights. Defaults to False. Returns ------- `pint.Quantity, pint.Quantity` The height and data variables of the layer """ bottom = kwargs.pop('bottom', None) interpolate = kwargs.pop('interpolate', True) with_agl = kwargs.pop('with_agl', False) # Make sure pressure and datavars are the same length for datavar in args: if len(heights) != len(datavar): raise ValueError('Height and data variables must have the same length.') # If we want things in AGL, subtract the minimum height from all height values if with_agl: sfc_height = np.min(heights) heights = heights - sfc_height # If the bottom is not specified, make it the surface if bottom is None: bottom = heights[0] # Make heights and arguments base units heights = heights.to_base_units() bottom
python
{ "resource": "" }
q23692
get_layer
train
def get_layer(pressure, *args, **kwargs): r"""Return an atmospheric layer from upper air data with the requested bottom and depth. This function will subset an upper air dataset to contain only the specified layer. The bottom of the layer can be specified with a pressure or height above the surface pressure. The bottom defaults to the surface pressure. The depth of the layer can be specified in terms of pressure or height above the bottom of the layer. If the top and bottom of the layer are not in the data, they are interpolated by default. Parameters ---------- pressure : array-like Atmospheric pressure profile *args : array-like Atmospheric variable(s) measured at the given pressures heights: array-like, optional Atmospheric heights corresponding to the given pressures. Defaults to using heights calculated from ``p`` assuming a standard atmosphere. bottom : `pint.Quantity`, optional The bottom of the layer as a pressure or height above the surface pressure. Defaults to the highest pressure or lowest height given. depth : `pint.Quantity`, optional The thickness of the layer as a pressure or height above the bottom of the layer. Defaults to 100 hPa. interpolate : bool, optional Interpolate the top and bottom points if they are not in the given data. Defaults to True. Returns ------- `pint.Quantity, pint.Quantity` The pressure and data variables of the layer """ # Pop off keyword arguments heights = kwargs.pop('heights', None) bottom = kwargs.pop('bottom', None) depth = kwargs.pop('depth', 100 * units.hPa) interpolate = kwargs.pop('interpolate', True) # If we get the depth kwarg, but it's None, set it to the default as well if depth is None: depth = 100 * units.hPa # Make sure pressure and datavars are the same length for datavar in args: if len(pressure) != len(datavar): raise ValueError('Pressure and data variables must have the same length.') # If the bottom is not specified, make it the surface pressure if bottom is None: bottom = np.nanmax(pressure) * pressure.units bottom_pressure, bottom_height = _get_bound_pressure_height(pressure, bottom, heights=heights, interpolate=interpolate) #
python
{ "resource": "" }
q23693
interp
train
def interp(x, xp, *args, **kwargs): """Wrap interpolate_1d
python
{ "resource": "" }
q23694
find_bounding_indices
train
def find_bounding_indices(arr, values, axis, from_below=True): """Find the indices surrounding the values within arr along axis. Returns a set of above, below, good. Above and below are lists of arrays of indices. These lists are formulated such that they can be used directly to index into a numpy array and get the expected results (no extra slices or ellipsis necessary). `good` is a boolean array indicating the "columns" that actually had values to bound the desired value(s). Parameters ---------- arr : array-like Array to search for values values: array-like One or more values to search for in `arr` axis : int The dimension of `arr` along which to search. from_below : bool, optional Whether to search from "below" (i.e. low indices to high indices). If `False`, the search will instead proceed from high indices to low indices. Defaults to `True`. Returns ------- above : list of arrays List of broadcasted indices to the location above the desired value below : list of arrays List of broadcasted indices to the location below the desired value good : array Boolean array indicating where the search found proper bounds for the desired value """ # The shape of generated indices is the same as the input, but with the axis of interest # replaced by the number of values to search for. indices_shape = list(arr.shape)
python
{ "resource": "" }
q23695
log_interp
train
def log_interp(x, xp, *args, **kwargs): """Wrap log_interpolate_1d
python
{ "resource": "" }
q23696
_greater_or_close
train
def _greater_or_close(a, value, **kwargs): r"""Compare values for greater or close to boolean masks. Returns a boolean mask for values greater than or equal to a target within a specified absolute or relative tolerance (as in :func:`numpy.isclose`). Parameters ---------- a : array-like Array of values to be compared
python
{ "resource": "" }
q23697
_less_or_close
train
def _less_or_close(a, value, **kwargs): r"""Compare values for less or close to boolean masks. Returns a boolean mask for values less than or equal to a target within a specified absolute or relative tolerance (as in :func:`numpy.isclose`). Parameters ---------- a : array-like Array of values to be compared
python
{ "resource": "" }
q23698
grid_deltas_from_dataarray
train
def grid_deltas_from_dataarray(f): """Calculate the horizontal deltas between grid points of a DataArray. Calculate the signed delta distance between grid points of a DataArray in the horizontal directions, whether the grid is lat/lon or x/y. Parameters ---------- f : `xarray.DataArray` Parsed DataArray on a latitude/longitude grid, in (..., lat, lon) or (..., y, x) dimension order Returns ------- dx, dy: arrays of signed deltas between grid points in the x and y directions with dimensions matching those of `f`. See Also -------- lat_lon_grid_deltas """ if f.metpy.crs['grid_mapping_name'] == 'latitude_longitude': dx, dy = lat_lon_grid_deltas(f.metpy.x, f.metpy.y,
python
{ "resource": "" }
q23699
xarray_derivative_wrap
train
def xarray_derivative_wrap(func): """Decorate the derivative functions to make them work nicely with DataArrays. This will automatically determine if the coordinates can be pulled directly from the DataArray, or if a call to lat_lon_grid_deltas is needed. """ @functools.wraps(func) def wrapper(f, **kwargs): if 'x' in kwargs or 'delta' in kwargs: # Use the usual DataArray to pint.Quantity preprocessing wrapper return preprocess_xarray(func)(f, **kwargs) elif isinstance(f, xr.DataArray): # Get axis argument, defaulting to first dimension axis = f.metpy.find_axis_name(kwargs.get('axis', 0)) # Initialize new kwargs with the axis number new_kwargs = {'axis': f.get_axis_num(axis)} if f[axis].attrs.get('_metpy_axis') == 'T': # Time coordinate, need to convert to seconds from datetimes new_kwargs['x'] = f[axis].metpy.as_timestamp().metpy.unit_array elif CFConventionHandler.check_axis(f[axis], 'lon'): # Longitude coordinate, need to get grid deltas new_kwargs['delta'], _ = grid_deltas_from_dataarray(f) elif CFConventionHandler.check_axis(f[axis], 'lat'): # Latitude coordinate, need to get grid deltas
python
{ "resource": "" }