signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
def parse_cf(self, varname=None, coordinates=None):
from .plots.mapping import CFProjection<EOL>if varname is None:<EOL><INDENT>return self._dataset.apply(lambda da: self.parse_cf(da.name,<EOL>coordinates=coordinates))<EOL><DEDENT>var = self._dataset[varname]<EOL>if '<STR_LIT>' in var.attrs:<EOL><INDENT>proj_name = var.attrs['<STR_LIT>']<EOL>try:<EOL><INDENT>proj_var = ...
Parse Climate and Forecasting (CF) convention metadata.
f8456:c1:m1
@classmethod<EOL><INDENT>def check_axis(cls, var, *axes):<DEDENT>
for axis in axes:<EOL><INDENT>for criterion in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if (var.attrs.get(criterion, '<STR_LIT>') in<EOL>cls.criteria[criterion].get(axis, set())):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>if (axis in cls.criteria['<STR_LIT>'] and (<EOL>(<EOL>cls.criteria['<ST...
Check if var satisfies the criteria for any of the given axes.
f8456:c1:m2
def _fixup_coords(self, var):
for coord_name, data_array in var.coords.items():<EOL><INDENT>if (self.check_axis(data_array, '<STR_LIT:x>', '<STR_LIT:y>')<EOL>and not self.check_axis(data_array, '<STR_LIT>', '<STR_LIT>')):<EOL><INDENT>try:<EOL><INDENT>var.coords[coord_name].metpy.convert_units('<STR_LIT>')<EOL><DEDENT>except DimensionalityError: <E...
Clean up the units on the coordinate variables.
f8456:c1:m3
def _generate_coordinate_map(self, coords):
<EOL>coord_lists = {'<STR_LIT:T>': [], '<STR_LIT>': [], '<STR_LIT:Y>': [], '<STR_LIT:X>': []}<EOL>for coord_var in coords:<EOL><INDENT>axes_to_check = {<EOL>'<STR_LIT:T>': ('<STR_LIT:time>',),<EOL>'<STR_LIT>': ('<STR_LIT>',),<EOL>'<STR_LIT:Y>': ('<STR_LIT:y>', '<STR_LIT>'),<EOL>'<STR_LIT:X>': ('<STR_LIT:x>', '<STR_LIT>...
Generate a coordinate map via CF conventions and other methods.
f8456:c1:m4
@staticmethod<EOL><INDENT>def _fixup_coordinate_map(coord_map, var):<DEDENT>
for axis in coord_map:<EOL><INDENT>if not isinstance(coord_map[axis], xr.DataArray):<EOL><INDENT>coord_map[axis] = var[coord_map[axis]]<EOL><DEDENT><DEDENT>
Ensure sure we have coordinate variables in map, not coordinate names.
f8456:c1:m5
@staticmethod<EOL><INDENT>def _assign_axes(coord_map, var):<DEDENT>
for axis in coord_map:<EOL><INDENT>if coord_map[axis] is not None:<EOL><INDENT>coord_map[axis].attrs['<STR_LIT>'] = axis<EOL><DEDENT><DEDENT>
Assign axis attribute to coordinates in var according to coord_map.
f8456:c1:m6
def _resolve_axis_conflict(self, axis, coord_lists):
if axis in ('<STR_LIT:Y>', '<STR_LIT:X>'):<EOL><INDENT>projection_coords = [coord_var for coord_var in coord_lists[axis] if<EOL>self.check_axis(coord_var, '<STR_LIT:x>', '<STR_LIT:y>')]<EOL>if len(projection_coords) == <NUM_LIT:1>:<EOL><INDENT>coord_lists[axis] = projection_coords<EOL>return<EOL><DEDENT><DEDENT>dimensi...
Handle axis conflicts if they arise.
f8456:c1:m7
@property<EOL><INDENT>def loc(self):<DEDENT>
return self._LocIndexer(self._dataset)<EOL>
Make the LocIndexer available as a property.
f8456:c1:m8
def sel(self, indexers=None, method=None, tolerance=None, drop=False, **indexers_kwargs):
indexers = either_dict_or_kwargs(indexers, indexers_kwargs, '<STR_LIT>')<EOL>indexers = _reassign_quantity_indexer(self._dataset, indexers)<EOL>return self._dataset.sel(indexers, method=method, tolerance=tolerance, drop=drop)<EOL>
Wrap Dataset.sel to handle units.
f8456:c1:m9
def generate_grid(horiz_dim, bbox):
x_steps, y_steps = get_xy_steps(bbox, horiz_dim)<EOL>grid_x = np.linspace(bbox['<STR_LIT>'], bbox['<STR_LIT>'], x_steps)<EOL>grid_y = np.linspace(bbox['<STR_LIT>'], bbox['<STR_LIT>'], y_steps)<EOL>gx, gy = np.meshgrid(grid_x, grid_y)<EOL>return gx, gy<EOL>
r"""Generate a meshgrid based on bounding box and x & y resolution. Parameters ---------- horiz_dim: integer Horizontal resolution bbox: dictionary Dictionary containing coordinates for corners of study area. Returns ------- grid_x: (X, Y) ndarray X dimension meshgr...
f8457:m0
def generate_grid_coords(gx, gy):
return np.vstack([gx.ravel(), gy.ravel()]).T<EOL>
r"""Calculate x,y coordinates of each grid cell. Parameters ---------- gx: numeric x coordinates in meshgrid gy: numeric y coordinates in meshgrid Returns ------- (X, Y) ndarray List of coordinates in meshgrid
f8457:m1
def get_xy_range(bbox):
x_range = bbox['<STR_LIT>'] - bbox['<STR_LIT>']<EOL>y_range = bbox['<STR_LIT>'] - bbox['<STR_LIT>']<EOL>return x_range, y_range<EOL>
r"""Return x and y ranges in meters based on bounding box. bbox: dictionary dictionary containing coordinates for corners of study area Returns ------- x_range: float Range in meters in x dimension. y_range: float Range in meters in y dimension.
f8457:m2
def get_xy_steps(bbox, h_dim):
x_range, y_range = get_xy_range(bbox)<EOL>x_steps = np.ceil(x_range / h_dim)<EOL>y_steps = np.ceil(y_range / h_dim)<EOL>return int(x_steps), int(y_steps)<EOL>
r"""Return meshgrid spacing based on bounding box. bbox: dictionary Dictionary containing coordinates for corners of study area. h_dim: integer Horizontal resolution in meters. Returns ------- x_steps, (X, ) ndarray Number of grids in x dimension. y_steps: (Y, ) ndarray...
f8457:m3
def get_boundary_coords(x, y, spatial_pad=<NUM_LIT:0>):
west = np.min(x) - spatial_pad<EOL>east = np.max(x) + spatial_pad<EOL>north = np.max(y) + spatial_pad<EOL>south = np.min(y) - spatial_pad<EOL>return {'<STR_LIT>': west, '<STR_LIT>': south, '<STR_LIT>': east, '<STR_LIT>': north}<EOL>
r"""Return bounding box based on given x and y coordinates assuming northern hemisphere. x: numeric x coordinates. y: numeric y coordinates. spatial_pad: numeric Number of meters to add to the x and y dimensions to reduce edge effects. Returns ------- bbox: dict...
f8457:m4
@exporter.export<EOL>def natural_neighbor_to_grid(xp, yp, variable, grid_x, grid_y):
<EOL>points_obs = list(zip(xp, yp))<EOL>points_grid = generate_grid_coords(grid_x, grid_y)<EOL>img = natural_neighbor_to_points(points_obs, variable, points_grid)<EOL>return img.reshape(grid_x.shape)<EOL>
r"""Generate a natural neighbor interpolation of the given points to a regular grid. This assigns values to the given grid using the Liang and Hale [Liang2010]_. approach. Parameters ---------- xp: (N, ) ndarray x-coordinates of observations yp: (N, ) ndarray y-coordinates of o...
f8457:m5
@exporter.export<EOL>@deprecated('<STR_LIT>', addendum='<STR_LIT>',<EOL>pending=False)<EOL>def natural_neighbor(xp, yp, variable, grid_x, grid_y):
return natural_neighbor_to_grid(xp, yp, variable, grid_x, grid_y)<EOL>
Wrap natural_neighbor_to_grid for deprecated natural_neighbor function.
f8457:m6
@exporter.export<EOL>def inverse_distance_to_grid(xp, yp, variable, grid_x, grid_y, r, gamma=None, kappa=None,<EOL>min_neighbors=<NUM_LIT:3>, kind='<STR_LIT>'):
<EOL>points_obs = list(zip(xp, yp))<EOL>points_grid = generate_grid_coords(grid_x, grid_y)<EOL>img = inverse_distance_to_points(points_obs, variable, points_grid, r, gamma=gamma,<EOL>kappa=kappa, min_neighbors=min_neighbors, kind=kind)<EOL>return img.reshape(grid_x.shape)<EOL>
r"""Generate an inverse distance interpolation of the given points to a regular grid. Values are assigned to the given grid using inverse distance weighting based on either [Cressman1959]_ or [Barnes1964]_. The Barnes implementation used here based on [Koch1983]_. Parameters ---------- xp: (N, ) n...
f8457:m7
@exporter.export<EOL>@deprecated('<STR_LIT>', addendum='<STR_LIT>',<EOL>pending=False)<EOL>def inverse_distance(xp, yp, variable, grid_x, grid_y, r, gamma=None, kappa=None,<EOL>min_neighbors=<NUM_LIT:3>, kind='<STR_LIT>'):
return inverse_distance_to_grid(xp, yp, variable, grid_x, grid_y, r, gamma=gamma,<EOL>kappa=kappa, min_neighbors=min_neighbors, kind=kind)<EOL>
Wrap inverse_distance_to_grid for deprecated inverse_distance function.
f8457:m8
@exporter.export<EOL>def interpolate_to_grid(x, y, z, interp_type='<STR_LIT>', hres=<NUM_LIT>,<EOL>minimum_neighbors=<NUM_LIT:3>, gamma=<NUM_LIT>, kappa_star=<NUM_LIT>,<EOL>search_radius=None, rbf_func='<STR_LIT>', rbf_smooth=<NUM_LIT:0>,<EOL>boundary_coords=None):
<EOL>if boundary_coords is None:<EOL><INDENT>boundary_coords = get_boundary_coords(x, y)<EOL><DEDENT>grid_x, grid_y = generate_grid(hres, boundary_coords)<EOL>points_obs = np.array(list(zip(x, y)))<EOL>points_grid = generate_grid_coords(grid_x, grid_y)<EOL>img = interpolate_to_points(points_obs, z, points_grid, interp_...
r"""Interpolate given (x,y), observation (z) pairs to a grid based on given parameters. Parameters ---------- x: array_like x coordinate y: array_like y coordinate z: array_like observation value interp_type: str What type of interpolation to use. Available optio...
f8457:m9
@exporter.export<EOL>def interpolate_to_isosurface(level_var, interp_var, level, **kwargs):
<EOL>bottom_up_search = kwargs.pop('<STR_LIT>', True)<EOL>above, below, good = metpy.calc.find_bounding_indices(level_var, [level], axis=<NUM_LIT:0>,<EOL>from_below=bottom_up_search)<EOL>interp_level = (((level - level_var[above]) / (level_var[below] - level_var[above]))<EOL>* (interp_var[below] - interp_var[above])) +...
r"""Linear interpolation of a variable to a given vertical level from given values. This function assumes that highest vertical level (lowest pressure) is zeroth index. A classic use of this function would be to compute the potential temperature on the dynamic tropopause (2 PVU surface). Parameters ...
f8457:m10
@exporter.export<EOL>@deprecated('<STR_LIT>', addendum='<STR_LIT>',<EOL>pending=False)<EOL>def interpolate(x, y, z, interp_type='<STR_LIT>', hres=<NUM_LIT>,<EOL>minimum_neighbors=<NUM_LIT:3>, gamma=<NUM_LIT>, kappa_star=<NUM_LIT>,<EOL>search_radius=None, rbf_func='<STR_LIT>', rbf_smooth=<NUM_LIT:0>,<EOL>boundary_coords...
return interpolate_to_grid(x, y, z, interp_type=interp_type, hres=hres,<EOL>minimum_neighbors=minimum_neighbors, gamma=gamma,<EOL>kappa_star=kappa_star, search_radius=search_radius,<EOL>rbf_func=rbf_func, rbf_smooth=rbf_smooth,<EOL>boundary_coords=boundary_coords)<EOL>
Wrap interpolate_to_grid for deprecated interpolate function.
f8457:m11
def get_points_within_r(center_points, target_points, r):
tree = cKDTree(target_points)<EOL>indices = tree.query_ball_point(center_points, r)<EOL>return tree.data[indices].T<EOL>
r"""Get all target_points within a specified radius of a center point. All data must be in same coordinate system, or you will get undetermined results. Parameters ---------- center_points: (X, Y) ndarray location from which to grab surrounding points within r target_points: (X, Y) ndarray...
f8464:m0
def get_point_count_within_r(center_points, target_points, r):
tree = cKDTree(target_points)<EOL>indices = tree.query_ball_point(center_points, r)<EOL>return np.array([len(x) for x in indices])<EOL>
r"""Get count of target points within a specified radius from center points. All data must be in same coordinate system, or you will get undetermined results. Parameters ---------- center_points: (X, Y) ndarray locations from which to grab surrounding points within r target_points: (X, Y) ...
f8464:m1
def triangle_area(pt1, pt2, pt3):
a = <NUM_LIT:0.0><EOL>a += pt1[<NUM_LIT:0>] * pt2[<NUM_LIT:1>] - pt2[<NUM_LIT:0>] * pt1[<NUM_LIT:1>]<EOL>a += pt2[<NUM_LIT:0>] * pt3[<NUM_LIT:1>] - pt3[<NUM_LIT:0>] * pt2[<NUM_LIT:1>]<EOL>a += pt3[<NUM_LIT:0>] * pt1[<NUM_LIT:1>] - pt1[<NUM_LIT:0>] * pt3[<NUM_LIT:1>]<EOL>return abs(a) / <NUM_LIT:2><EOL>
r"""Return the area of a triangle. Parameters ---------- pt1: (X,Y) ndarray Starting vertex of a triangle pt2: (X,Y) ndarray Second vertex of a triangle pt3: (X,Y) ndarray Ending vertex of a triangle Returns ------- area: float Area of the given triangle...
f8464:m2
def dist_2(x0, y0, x1, y1):
d0 = x1 - x0<EOL>d1 = y1 - y0<EOL>return d0 * d0 + d1 * d1<EOL>
r"""Return the squared distance between two points. This is faster than calculating distance but should only be used with comparable ratios. Parameters ---------- x0: float Starting x coordinate y0: float Starting y coordinate x1: float Ending x coordinate y1: f...
f8464:m3
def distance(p0, p1):
return math.sqrt(dist_2(p0[<NUM_LIT:0>], p0[<NUM_LIT:1>], p1[<NUM_LIT:0>], p1[<NUM_LIT:1>]))<EOL>
r"""Return the distance between two points. Parameters ---------- p0: (X,Y) ndarray Starting coordinate p1: (X,Y) ndarray Ending coordinate Returns ------- d: float distance See Also -------- dist_2
f8464:m4
def circumcircle_radius_2(pt0, pt1, pt2):
a = distance(pt0, pt1)<EOL>b = distance(pt1, pt2)<EOL>c = distance(pt2, pt0)<EOL>t_area = triangle_area(pt0, pt1, pt2)<EOL>prod2 = a * b * c<EOL>if t_area > <NUM_LIT:0>:<EOL><INDENT>radius = prod2 * prod2 / (<NUM_LIT:16> * t_area * t_area)<EOL><DEDENT>else:<EOL><INDENT>radius = np.nan<EOL><DEDENT>return radius<EOL>
r"""Calculate and return the squared radius of a given triangle's circumcircle. This is faster than calculating radius but should only be used with comparable ratios. Parameters ---------- pt0: (x, y) Starting vertex of triangle pt1: (x, y) Second vertex of triangle pt2: (x, y)...
f8464:m5
def circumcircle_radius(pt0, pt1, pt2):
a = distance(pt0, pt1)<EOL>b = distance(pt1, pt2)<EOL>c = distance(pt2, pt0)<EOL>t_area = triangle_area(pt0, pt1, pt2)<EOL>if t_area > <NUM_LIT:0>:<EOL><INDENT>radius = (a * b * c) / (<NUM_LIT:4> * t_area)<EOL><DEDENT>else:<EOL><INDENT>radius = np.nan<EOL><DEDENT>return radius<EOL>
r"""Calculate and return the radius of a given triangle's circumcircle. Parameters ---------- pt0: (x, y) Starting vertex of triangle pt1: (x, y) Second vertex of triangle pt2: (x, y) Final vertex of a triangle Returns ------- r: float circumcircle radiu...
f8464:m6
def circumcenter(pt0, pt1, pt2):
a_x = pt0[<NUM_LIT:0>]<EOL>a_y = pt0[<NUM_LIT:1>]<EOL>b_x = pt1[<NUM_LIT:0>]<EOL>b_y = pt1[<NUM_LIT:1>]<EOL>c_x = pt2[<NUM_LIT:0>]<EOL>c_y = pt2[<NUM_LIT:1>]<EOL>bc_y_diff = b_y - c_y<EOL>ca_y_diff = c_y - a_y<EOL>ab_y_diff = a_y - b_y<EOL>cb_x_diff = c_x - b_x<EOL>ac_x_diff = a_x - c_x<EOL>ba_x_diff = b_x - a_x<EOL>d_...
r"""Calculate and return the circumcenter of a circumcircle generated by a given triangle. All three points must be unique or a division by zero error will be raised. Parameters ---------- pt0: (x, y) Starting vertex of triangle pt1: (x, y) Second vertex of triangle pt2: (x, y)...
f8464:m7
def find_natural_neighbors(tri, grid_points):
tree = cKDTree(grid_points)<EOL>in_triangulation = tri.find_simplex(tree.data) >= <NUM_LIT:0><EOL>triangle_info = {}<EOL>members = {key: [] for key in range(len(tree.data))}<EOL>for i, simplices in enumerate(tri.simplices):<EOL><INDENT>ps = tri.points[simplices]<EOL>cc = circumcenter(*ps)<EOL>r = circumcircle_radius(*p...
r"""Return the natural neighbor triangles for each given grid cell. These are determined by the properties of the given delaunay triangulation. A triangle is a natural neighbor of a grid cell if that triangles circumcenter is within the circumradius of the grid cell center. Parameters ---------- ...
f8464:m8
def find_nn_triangles_point(tri, cur_tri, point):
nn = []<EOL>candidates = set(tri.neighbors[cur_tri])<EOL>candidates |= set(tri.neighbors[tri.neighbors[cur_tri]].flat)<EOL>candidates.discard(-<NUM_LIT:1>)<EOL>for neighbor in candidates:<EOL><INDENT>triangle = tri.points[tri.simplices[neighbor]]<EOL>cur_x, cur_y = circumcenter(triangle[<NUM_LIT:0>], triangle[<NUM_LIT:...
r"""Return the natural neighbors of a triangle containing a point. This is based on the provided Delaunay Triangulation. Parameters ---------- tri: Object A Delaunay Triangulation cur_tri: int Simplex code for Delaunay Triangulation lookup of a given triangle that contains ...
f8464:m9
def find_local_boundary(tri, triangles):
edges = []<EOL>for triangle in triangles:<EOL><INDENT>for i in range(<NUM_LIT:3>):<EOL><INDENT>pt1 = tri.simplices[triangle][i]<EOL>pt2 = tri.simplices[triangle][(i + <NUM_LIT:1>) % <NUM_LIT:3>]<EOL>if (pt1, pt2) in edges:<EOL><INDENT>edges.remove((pt1, pt2))<EOL><DEDENT>elif (pt2, pt1) in edges:<EOL><INDENT>edges.remo...
r"""Find and return the outside edges of a collection of natural neighbor triangles. There is no guarantee that this boundary is convex, so ConvexHull is not sufficient in some situations. Parameters ---------- tri: Object A Delaunay Triangulation triangles: (N, ) array List of...
f8464:m10
def area(poly):
a = <NUM_LIT:0.0><EOL>n = len(poly)<EOL>for i in range(n):<EOL><INDENT>a += poly[i][<NUM_LIT:0>] * poly[(i + <NUM_LIT:1>) % n][<NUM_LIT:1>] - poly[(i + <NUM_LIT:1>) % n][<NUM_LIT:0>] * poly[i][<NUM_LIT:1>]<EOL><DEDENT>return abs(a) / <NUM_LIT><EOL>
r"""Find the area of a given polygon using the shoelace algorithm. Parameters ---------- poly: (2, N) ndarray 2-dimensional coordinates representing an ordered traversal around the edge a polygon. Returns ------- area: float
f8464:m11
def order_edges(edges):
edge = edges[<NUM_LIT:0>]<EOL>edges = edges[<NUM_LIT:1>:]<EOL>ordered_edges = [edge]<EOL>num_max = len(edges)<EOL>while len(edges) > <NUM_LIT:0> and num_max > <NUM_LIT:0>:<EOL><INDENT>match = edge[<NUM_LIT:1>]<EOL>for search_edge in edges:<EOL><INDENT>vertex = search_edge[<NUM_LIT:0>]<EOL>if match == vertex:<EOL><INDEN...
r"""Return an ordered traversal of the edges of a two-dimensional polygon. Parameters ---------- edges: (2, N) ndarray List of unordered line segments, where each line segment is represented by two unique vertex codes. Returns ------- ordered_edges: (2, N) ndarray
f8464:m12
@exporter.export<EOL>def interpolate_to_slice(data, points, interp_type='<STR_LIT>'):
try:<EOL><INDENT>x, y = data.metpy.coordinates('<STR_LIT:x>', '<STR_LIT:y>')<EOL><DEDENT>except AttributeError:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>data_sliced = data.interp({<EOL>x.name: xr.DataArray(points[:, <NUM_LIT:0>], dims='<STR_LIT:index>', attrs=x.attrs),<EOL>y...
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...
f8465:m0
@exporter.export<EOL>def geodesic(crs, start, end, steps):
import cartopy.crs as ccrs<EOL>from pyproj import Geod<EOL>g = Geod(crs.proj4_init)<EOL>geodesic = np.concatenate([<EOL>np.array(start[::-<NUM_LIT:1>])[None],<EOL>np.array(g.npts(start[<NUM_LIT:1>], start[<NUM_LIT:0>], end[<NUM_LIT:1>], end[<NUM_LIT:0>], steps - <NUM_LIT:2>)),<EOL>np.array(end[::-<NUM_LIT:1>])[None]<EO...
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 pai...
f8465:m1
@exporter.export<EOL>def cross_section(data, start, end, steps=<NUM_LIT:100>, interp_type='<STR_LIT>'):
if isinstance(data, xr.Dataset):<EOL><INDENT>return data.apply(cross_section, True, (start, end), steps=steps,<EOL>interp_type=interp_type)<EOL><DEDENT>elif data.ndim == <NUM_LIT:0>:<EOL><INDENT>return data<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>crs_data = data.metpy.cartopy_crs<EOL>x = data.metpy.x<EOL><DEDENT...
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 co...
f8465:m2
def cressman_point(sq_dist, values, radius):
weights = tools.cressman_weights(sq_dist, radius)<EOL>total_weights = np.sum(weights)<EOL>return sum(v * (w / total_weights) for (w, v) in zip(weights, values))<EOL>
r"""Generate a Cressman interpolation value for a point. The calculated value is based on the given distances and search radius. Parameters ---------- sq_dist: (N, ) ndarray Squared distance between observations and grid point values: (N, ) ndarray Observation values in same order ...
f8466:m0
def barnes_point(sq_dist, values, kappa, gamma=None):
if gamma is None:<EOL><INDENT>gamma = <NUM_LIT:1><EOL><DEDENT>weights = tools.barnes_weights(sq_dist, kappa, gamma)<EOL>total_weights = np.sum(weights)<EOL>return sum(v * (w / total_weights) for (w, v) in zip(weights, values))<EOL>
r"""Generate a single pass barnes interpolation value for a point. The calculated value is based on the given distances, kappa and gamma values. Parameters ---------- sq_dist: (N, ) ndarray Squared distance between observations and grid point values: (N, ) ndarray Observation value...
f8466:m1
def natural_neighbor_point(xp, yp, variable, grid_loc, tri, neighbors, triangle_info):
edges = geometry.find_local_boundary(tri, neighbors)<EOL>edge_vertices = [segment[<NUM_LIT:0>] for segment in geometry.order_edges(edges)]<EOL>num_vertices = len(edge_vertices)<EOL>p1 = edge_vertices[<NUM_LIT:0>]<EOL>p2 = edge_vertices[<NUM_LIT:1>]<EOL>c1 = geometry.circumcenter(grid_loc, tri.points[p1], tri.points[p2]...
r"""Generate a natural neighbor interpolation of the observations to the given point. This uses the Liang and Hale approach [Liang2010]_. The interpolation will fail if the grid point has no natural neighbors. Parameters ---------- xp: (N, ) ndarray x-coordinates of observations yp: (N...
f8466:m2
@exporter.export<EOL>def natural_neighbor_to_points(points, values, xi):
tri = Delaunay(points)<EOL>members, triangle_info = geometry.find_natural_neighbors(tri, xi)<EOL>img = np.empty(shape=(xi.shape[<NUM_LIT:0>]), dtype=values.dtype)<EOL>img.fill(np.nan)<EOL>for ind, (grid, neighbors) in enumerate(members.items()):<EOL><INDENT>if len(neighbors) > <NUM_LIT:0>:<EOL><INDENT>points_transposed...
r"""Generate a natural neighbor interpolation to the given points. This assigns values to the given interpolation points using the Liang and Hale [Liang2010]_. approach. Parameters ---------- points: array_like, shape (n, 2) Coordinates of the data points. values: array_like, shape (n,...
f8466:m3
@exporter.export<EOL>def inverse_distance_to_points(points, values, xi, r, gamma=None, kappa=None, min_neighbors=<NUM_LIT:3>,<EOL>kind='<STR_LIT>'):
obs_tree = cKDTree(points)<EOL>indices = obs_tree.query_ball_point(xi, r=r)<EOL>img = np.empty(shape=(xi.shape[<NUM_LIT:0>]), dtype=values.dtype)<EOL>img.fill(np.nan)<EOL>for idx, (matches, grid) in enumerate(zip(indices, xi)):<EOL><INDENT>if len(matches) >= min_neighbors:<EOL><INDENT>x1, y1 = obs_tree.data[matches].T<...
r"""Generate an inverse distance weighting interpolation to the given points. Values are assigned to the given interpolation points based on either [Cressman1959]_ or [Barnes1964]_. The Barnes implementation used here based on [Koch1983]_. Parameters ---------- points: array_like, shape (n, 2) ...
f8466:m4
@exporter.export<EOL>def interpolate_to_points(points, values, xi, interp_type='<STR_LIT>', minimum_neighbors=<NUM_LIT:3>,<EOL>gamma=<NUM_LIT>, kappa_star=<NUM_LIT>, search_radius=None, rbf_func='<STR_LIT>',<EOL>rbf_smooth=<NUM_LIT:0>):
<EOL>if interp_type in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>return griddata(points, values, xi, method=interp_type)<EOL><DEDENT>elif interp_type == '<STR_LIT>':<EOL><INDENT>return natural_neighbor_to_points(points, values, xi)<EOL><DEDENT>elif interp_type in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>ave_s...
r"""Interpolate unstructured point data to the given points. This function interpolates the given `values` valid at `points` to the points `xi`. This is modeled after `scipy.interpolate.griddata`, but acts as a generalization of it by including the following types of interpolation: - Linear - Near...
f8466:m5
@exporter.export<EOL>@preprocess_xarray<EOL>def interpolate_nans_1d(x, y, kind='<STR_LIT>'):
x_sort_args = np.argsort(x)<EOL>x = x[x_sort_args]<EOL>y = y[x_sort_args]<EOL>nans = np.isnan(y)<EOL>if kind == '<STR_LIT>':<EOL><INDENT>y[nans] = np.interp(x[nans], x[~nans], y[~nans])<EOL><DEDENT>elif kind == '<STR_LIT>':<EOL><INDENT>y[nans] = np.interp(np.log(x[nans]), np.log(x[~nans]), y[~nans])<EOL><DEDENT>else:<E...
Interpolate NaN values in y. Interpolate NaN values in the y dimension. Works with unsorted x values. Parameters ---------- x : array-like 1-dimensional array of numeric x-values y : array-like 1-dimensional array of numeric y-values kind : string specifies the kind of ...
f8468:m0
@exporter.export<EOL>@preprocess_xarray<EOL>@units.wraps(None, ('<STR_LIT>', '<STR_LIT>'))<EOL>def interpolate_1d(x, xp, *args, **kwargs):
<EOL>fill_value = kwargs.pop('<STR_LIT>', np.nan)<EOL>axis = kwargs.pop('<STR_LIT>', <NUM_LIT:0>)<EOL>x = np.asanyarray(x).reshape(-<NUM_LIT:1>)<EOL>ndim = xp.ndim<EOL>sort_args = np.argsort(xp, axis=axis)<EOL>sort_x = np.argsort(x)<EOL>sorter = broadcast_indices(xp, sort_args, ndim, axis)<EOL>xp = xp[sorter]<EOL>varia...
r"""Interpolates data with any shape over a specified axis. Interpolation over a specified axis for arrays of any shape. Parameters ---------- x : array-like 1-D array of desired interpolated values. xp : array-like The x-coordinates of the data points. args : array-like ...
f8468:m1
@exporter.export<EOL>@preprocess_xarray<EOL>@units.wraps(None, ('<STR_LIT>', '<STR_LIT>'))<EOL>def log_interpolate_1d(x, xp, *args, **kwargs):
<EOL>fill_value = kwargs.pop('<STR_LIT>', np.nan)<EOL>axis = kwargs.pop('<STR_LIT>', <NUM_LIT:0>)<EOL>log_x = np.log(x)<EOL>log_xp = np.log(xp)<EOL>return interpolate_1d(log_x, log_xp, *args, axis=axis, fill_value=fill_value)<EOL>
r"""Interpolates data with logarithmic x-scale over a specified axis. Interpolation on a logarithmic x-scale for interpolation values in pressure coordintates. Parameters ---------- x : array-like 1-D array of desired interpolated values. xp : array-like The x-coordinates of the d...
f8468:m2
def calc_kappa(spacing, kappa_star=<NUM_LIT>):
return kappa_star * (<NUM_LIT> * spacing / np.pi)**<NUM_LIT:2><EOL>
r"""Calculate the kappa parameter for barnes interpolation. Parameters ---------- spacing: float Average spacing between observations kappa_star: float Non-dimensional response parameter. Default 5.052. Returns ------- kappa: float
f8469:m0
@exporter.export<EOL>def remove_observations_below_value(x, y, z, val=<NUM_LIT:0>):
x_ = x[z >= val]<EOL>y_ = y[z >= val]<EOL>z_ = z[z >= val]<EOL>return x_, y_, z_<EOL>
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 -...
f8469:m1
@exporter.export<EOL>def remove_nan_observations(x, y, z):
x_ = x[~np.isnan(z)]<EOL>y_ = y[~np.isnan(z)]<EOL>z_ = z[~np.isnan(z)]<EOL>return x_, y_, z_<EOL>
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 ------- x, y, z List of coordinate observation pairs wit...
f8469:m2
@exporter.export<EOL>def remove_repeat_coordinates(x, y, z):
coords = []<EOL>variable = []<EOL>for (x_, y_, t_) in zip(x, y, z):<EOL><INDENT>if (x_, y_) not in coords:<EOL><INDENT>coords.append((x_, y_))<EOL>variable.append(t_)<EOL><DEDENT><DEDENT>coords = np.array(coords)<EOL>x_ = coords[:, <NUM_LIT:0>]<EOL>y_ = coords[:, <NUM_LIT:1>]<EOL>z_ = np.array(variable)<EOL>return x_, ...
r"""Remove all x, y, and z where (x,y) is repeated and keep the first occurrence only. Will not destroy original values. Parameters ---------- x: array_like x coordinate y: array_like y coordinate z: array_like observation value Returns ------- x, y, z ...
f8469:m3
def barnes_weights(sq_dist, kappa, gamma):
return np.exp(-<NUM_LIT:1.0> * sq_dist / (kappa * gamma))<EOL>
r"""Calculate the Barnes weights from squared distance values. Parameters ---------- sq_dist: (N, ) ndarray Squared distances from interpolation point associated with each observation in meters. kappa: float Response parameter for barnes interpolation. Default None. gamma: f...
f8469:m4
def cressman_weights(sq_dist, r):
return (r * r - sq_dist) / (r * r + sq_dist)<EOL>
r"""Calculate the Cressman weights from squared distance values. Parameters ---------- sq_dist: (N, ) ndarray Squared distances from interpolation point associated with each observation in meters. r: float Maximum distance an observation can be from an interpolation poin...
f8469:m5
def unit_calc(temp, press, dens, mixing, unitless_const):
pass<EOL>
r"""Stub calculation for testing unit checking.
f8472:m8
@classmethod<EOL><INDENT>def dontuse(cls):<DEDENT>
deprecation.warn_deprecated('<STR_LIT>', pending=True)<EOL>return False<EOL>
Don't use.
f8474:c0:m0
@classmethod<EOL><INDENT>@deprecation.deprecated('<STR_LIT>')<EOL>def really_dontuse(cls):<DEDENT>
return False<EOL>
Really, don't use.
f8474:c0:m1
def _is_x_first_dim(dim_order):
if dim_order is None:<EOL><INDENT>dim_order = '<STR_LIT>'<EOL><DEDENT>return dim_order == '<STR_LIT>'<EOL>
Determine whether x is the first dimension based on the value of dim_order.
f8475:m1
def _check_and_flip(arr):
if hasattr(arr, '<STR_LIT>'):<EOL><INDENT>if arr.ndim >= <NUM_LIT:2>:<EOL><INDENT>return arr.T<EOL><DEDENT>else:<EOL><INDENT>return arr<EOL><DEDENT><DEDENT>elif not is_string_like(arr) and iterable(arr):<EOL><INDENT>return tuple(_check_and_flip(a) for a in arr)<EOL><DEDENT>else:<EOL><INDENT>return arr<EOL><DEDENT>
Transpose array or list of arrays if they are 2D.
f8475:m2
def ensure_yx_order(func):
@functools.wraps(func)<EOL>def wrapper(*args, **kwargs):<EOL><INDENT>dim_order = kwargs.pop('<STR_LIT>', None)<EOL>x_first = _is_x_first_dim(dim_order)<EOL>if x_first:<EOL><INDENT>args = tuple(_check_and_flip(arr) for arr in args)<EOL>for k, v in kwargs:<EOL><INDENT>kwargs[k] = _check_and_flip(v)<EOL><DEDENT><DEDENT>re...
Wrap a function to ensure all array arguments are y, x ordered, based on kwarg.
f8475:m3
@exporter.export<EOL>@preprocess_xarray<EOL>@ensure_yx_order<EOL>def vorticity(u, v, dx, dy):
dudy = first_derivative(u, delta=dy, axis=-<NUM_LIT:2>)<EOL>dvdx = first_derivative(v, delta=dx, axis=-<NUM_LIT:1>)<EOL>return dvdx - dudy<EOL>
r"""Calculate the vertical vorticity of the horizontal wind. Parameters ---------- u : (M, N) ndarray x component of the wind v : (M, N) ndarray y component of the wind dx : float or ndarray The grid spacing(s) in the x-direction. If an array, there should be one item less t...
f8475:m4
@exporter.export<EOL>@preprocess_xarray<EOL>@ensure_yx_order<EOL>def divergence(u, v, dx, dy):
dudx = first_derivative(u, delta=dx, axis=-<NUM_LIT:1>)<EOL>dvdy = first_derivative(v, delta=dy, axis=-<NUM_LIT:2>)<EOL>return dudx + dvdy<EOL>
r"""Calculate the horizontal divergence of the horizontal wind. Parameters ---------- u : (M, N) ndarray x component of the wind v : (M, N) ndarray y component of the wind dx : float or ndarray The grid spacing(s) in the x-direction. If an array, there should be one item les...
f8475:m5
@exporter.export<EOL>@preprocess_xarray<EOL>@ensure_yx_order<EOL>def shearing_deformation(u, v, dx, dy):
dudy = first_derivative(u, delta=dy, axis=-<NUM_LIT:2>)<EOL>dvdx = first_derivative(v, delta=dx, axis=-<NUM_LIT:1>)<EOL>return dvdx + dudy<EOL>
r"""Calculate the shearing deformation of the horizontal wind. Parameters ---------- u : (M, N) ndarray x component of the wind v : (M, N) ndarray y component of the wind dx : float or ndarray The grid spacing(s) in the x-direction. If an array, there should be one item less...
f8475:m6
@exporter.export<EOL>@preprocess_xarray<EOL>@ensure_yx_order<EOL>def stretching_deformation(u, v, dx, dy):
dudx = first_derivative(u, delta=dx, axis=-<NUM_LIT:1>)<EOL>dvdy = first_derivative(v, delta=dy, axis=-<NUM_LIT:2>)<EOL>return dudx - dvdy<EOL>
r"""Calculate the stretching deformation of the horizontal wind. Parameters ---------- u : (M, N) ndarray x component of the wind v : (M, N) ndarray y component of the wind dx : float or ndarray The grid spacing(s) in the x-direction. If an array, there should be one item le...
f8475:m7
@exporter.export<EOL>@preprocess_xarray<EOL>@ensure_yx_order<EOL>def total_deformation(u, v, dx, dy):
dudy, dudx = gradient(u, deltas=(dy, dx), axes=(-<NUM_LIT:2>, -<NUM_LIT:1>))<EOL>dvdy, dvdx = gradient(v, deltas=(dy, dx), axes=(-<NUM_LIT:2>, -<NUM_LIT:1>))<EOL>return np.sqrt((dvdx + dudy)**<NUM_LIT:2> + (dudx - dvdy)**<NUM_LIT:2>)<EOL>
r"""Calculate the horizontal total deformation of the horizontal wind. Parameters ---------- u : (M, N) ndarray x component of the wind v : (M, N) ndarray y component of the wind dx : float or ndarray The grid spacing(s) in the x-direction. If an array, there should be one i...
f8475:m8
@exporter.export<EOL>@preprocess_xarray<EOL>@ensure_yx_order<EOL>def advection(scalar, wind, deltas):
<EOL>wind = _stack(wind)<EOL>if wind.ndim > scalar.ndim:<EOL><INDENT>wind = wind[::-<NUM_LIT:1>]<EOL><DEDENT>grad = _stack(gradient(scalar, deltas=deltas[::-<NUM_LIT:1>]))<EOL>grad, wind = atleast_2d(grad, wind)<EOL>return (-grad * wind).sum(axis=<NUM_LIT:0>)<EOL>
r"""Calculate the advection of a scalar field by the wind. The order of the dimensions of the arrays must match the order in which the wind components are given. For example, if the winds are given [u, v], then the scalar and wind arrays must be indexed as x,y (which puts x as the rows, not columns). ...
f8475:m9
@exporter.export<EOL>@preprocess_xarray<EOL>@ensure_yx_order<EOL>def frontogenesis(thta, u, v, dx, dy, dim_order='<STR_LIT>'):
<EOL>ddy_thta = first_derivative(thta, delta=dy, axis=-<NUM_LIT:2>)<EOL>ddx_thta = first_derivative(thta, delta=dx, axis=-<NUM_LIT:1>)<EOL>mag_thta = np.sqrt(ddx_thta**<NUM_LIT:2> + ddy_thta**<NUM_LIT:2>)<EOL>shrd = shearing_deformation(u, v, dx, dy, dim_order=dim_order)<EOL>strd = stretching_deformation(u, v, dx, dy, ...
r"""Calculate the 2D kinematic frontogenesis of a temperature field. The implementation is a form of the Petterssen Frontogenesis and uses the formula outlined in [Bluestein1993]_ pg.248-253. .. math:: F=\frac{1}{2}\left|\nabla \theta\right|[D cos(2\beta)-\delta] * :math:`F` is 2D kinematic frontogen...
f8475:m10
@exporter.export<EOL>@preprocess_xarray<EOL>@ensure_yx_order<EOL>def geostrophic_wind(heights, f, dx, dy):
if heights.dimensionality['<STR_LIT>'] == <NUM_LIT>:<EOL><INDENT>norm_factor = <NUM_LIT:1.> / f<EOL><DEDENT>else:<EOL><INDENT>norm_factor = mpconsts.g / f<EOL><DEDENT>dhdy = first_derivative(heights, delta=dy, axis=-<NUM_LIT:2>)<EOL>dhdx = first_derivative(heights, delta=dx, axis=-<NUM_LIT:1>)<EOL>return -norm_factor *...
r"""Calculate the geostrophic wind given from the heights or geopotential. Parameters ---------- heights : (M, N) ndarray The height field, with either leading dimensions of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``. f : array_like The cori...
f8475:m11
@exporter.export<EOL>@preprocess_xarray<EOL>@ensure_yx_order<EOL>def ageostrophic_wind(heights, f, dx, dy, u, v, dim_order='<STR_LIT>'):
u_geostrophic, v_geostrophic = geostrophic_wind(heights, f, dx, dy, dim_order=dim_order)<EOL>return u - u_geostrophic, v - v_geostrophic<EOL>
r"""Calculate the ageostrophic wind given from the heights or geopotential. Parameters ---------- heights : (M, N) ndarray The height field. f : array_like The coriolis parameter. This can be a scalar to be applied everywhere or an array of values. dx : float or ndarray ...
f8475:m12
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>')<EOL>def montgomery_streamfunction(height, temperature):
return (mpconsts.g * height) + (mpconsts.Cp_d * temperature)<EOL>
r"""Compute the Montgomery Streamfunction on isentropic surfaces. The Montgomery Streamfunction is the streamfunction of the geostrophic wind on an isentropic surface. This quantity is proportional to the geostrophic wind in isentropic coordinates, and its gradient can be interpreted similarly to the press...
f8475:m13
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>')<EOL>def storm_relative_helicity(u, v, heights, depth, bottom=<NUM_LIT:0> * units.m,<EOL>storm_u=<NUM_LIT:0> * units('<STR_LIT>'), storm_v=<NUM_LIT:0> * units('<STR_LIT>...
_, u, v = get_layer_heights(heights, depth, u, v, with_agl=True, bottom=bottom)<EOL>storm_relative_u = u - storm_u<EOL>storm_relative_v = v - storm_v<EOL>int_layers = (storm_relative_u[<NUM_LIT:1>:] * storm_relative_v[:-<NUM_LIT:1>]<EOL>- storm_relative_u[:-<NUM_LIT:1>] * storm_relative_v[<NUM_LIT:1>:])<EOL>positive_sr...
r"""Calculate storm relative helicity. Calculates storm relatively helicity following [Markowski2010] 230-231. .. math:: \int\limits_0^d (\bar v - c) \cdot \bar\omega_{h} \,dz This is applied to the data from a hodograph with the following summation: .. math:: \sum_{n = 1}^{N-1} [(u_{n+1} - c_{x})(v...
f8475:m14
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def absolute_vorticity(u, v, dx, dy, lats, dim_order='<STR_LIT>'):
f = coriolis_parameter(lats)<EOL>relative_vorticity = vorticity(u, v, dx, dy, dim_order=dim_order)<EOL>return relative_vorticity + f<EOL>
Calculate the absolute vorticity of the horizontal wind. Parameters ---------- u : (M, N) ndarray x component of the wind v : (M, N) ndarray y component of the wind dx : float or ndarray The grid spacing(s) in the x-direction. If an array, there should be one item less than ...
f8475:m15
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def potential_vorticity_baroclinic(potential_temperature, pressure, u, v, dx, dy, lats):
if ((np.shape(potential_temperature)[-<NUM_LIT:3>] < <NUM_LIT:3>) or (np.shape(pressure)[-<NUM_LIT:3>] < <NUM_LIT:3>)<EOL>or (np.shape(potential_temperature)[-<NUM_LIT:3>] != (np.shape(pressure)[-<NUM_LIT:3>]))):<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'.format(-<NUM_LIT:3>))<EOL><DEDENT>avor = absolute_...
r"""Calculate the baroclinic potential vorticity. .. math:: PV = -g \left(\frac{\partial u}{\partial p}\frac{\partial \theta}{\partial y} - \frac{\partial v}{\partial p}\frac{\partial \theta}{\partial x} + \frac{\partial \theta}{\partial p}(\zeta + f) \right) This formula is based ...
f8475:m16
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def potential_vorticity_barotropic(heights, u, v, dx, dy, lats, dim_order='<STR_LIT>'):
avor = absolute_vorticity(u, v, dx, dy, lats, dim_order=dim_order)<EOL>return (avor / heights).to('<STR_LIT>')<EOL>
r"""Calculate the barotropic (Rossby) potential vorticity. .. math:: PV = \frac{f + \zeta}{H} This formula is based on equation 7.27 [Hobbs2006]_. Parameters ---------- heights : (M, N) ndarray atmospheric heights u : (M, N) ndarray x component of the wind v : (M, N) ndarr...
f8475:m17
@exporter.export<EOL>@preprocess_xarray<EOL>def inertial_advective_wind(u, v, u_geostrophic, v_geostrophic, dx, dy, lats):
f = coriolis_parameter(lats)<EOL>dugdy, dugdx = gradient(u_geostrophic, deltas=(dy, dx), axes=(-<NUM_LIT:2>, -<NUM_LIT:1>))<EOL>dvgdy, dvgdx = gradient(v_geostrophic, deltas=(dy, dx), axes=(-<NUM_LIT:2>, -<NUM_LIT:1>))<EOL>u_component = -(u * dvgdx + v * dvgdy) / f<EOL>v_component = (u * dugdx + v * dugdy) / f<EOL>retu...
r"""Calculate the inertial advective wind. .. math:: \frac{\hat k}{f} \times (\vec V \cdot \nabla)\hat V_g .. math:: \frac{\hat k}{f} \times \left[ \left( u \frac{\partial u_g}{\partial x} + v \frac{\partial u_g}{\partial y} \right) \hat i + \left( u \frac{\partial v_g} {\partial x...
f8475:m18
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def q_vector(u, v, temperature, pressure, dx, dy, static_stability=<NUM_LIT:1>):
dudy, dudx = gradient(u, deltas=(dy, dx), axes=(-<NUM_LIT:2>, -<NUM_LIT:1>))<EOL>dvdy, dvdx = gradient(v, deltas=(dy, dx), axes=(-<NUM_LIT:2>, -<NUM_LIT:1>))<EOL>dtempdy, dtempdx = gradient(temperature, deltas=(dy, dx), axes=(-<NUM_LIT:2>, -<NUM_LIT:1>))<EOL>q1 = -mpconsts.Rd / (pressure * static_stability) * (dudx * d...
r"""Calculate Q-vector at a given pressure level using the u, v winds and temperature. .. math:: \vec{Q} = (Q_1, Q_2) = - \frac{R}{\sigma p}\left( \frac{\partial \vec{v}_g}{\partial x} \cdot \nabla_p T, \frac{\partial \vec{v}_g}{\...
f8475:m19
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>')<EOL>def relative_humidity_from_dewpoint(temperature, dewpt):
e = saturation_vapor_pressure(dewpt)<EOL>e_s = saturation_vapor_pressure(temperature)<EOL>return (e / e_s)<EOL>
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 ---------- temperature : `pint.Quantity` The temperature dew point : `pint.Quantity` The dew ...
f8476:m0
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>')<EOL>def exner_function(pressure, reference_pressure=mpconsts.P0):
return (pressure / reference_pressure).to('<STR_LIT>')**mpconsts.kappa<EOL>
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 atmospher...
f8476:m1
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>')<EOL>def potential_temperature(pressure, temperature):
return temperature / exner_function(pressure)<EOL>
r"""Calculate the potential temperature. Uses the Poisson equation to calculation the potential temperature given `pressure` and `temperature`. Parameters ---------- pressure : `pint.Quantity` The total atmospheric pressure temperature : `pint.Quantity` The temperature Ret...
f8476:m2
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>')<EOL>def temperature_from_potential_temperature(pressure, theta):
return theta * exner_function(pressure)<EOL>
r"""Calculate the temperature from a given potential temperature. Uses the inverse of the Poisson equation to calculate the temperature from a given potential temperature at a specific pressure level. Parameters ---------- pressure : `pint.Quantity` The total atmospheric pressure theta...
f8476:m3
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def dry_lapse(pressure, temperature, ref_pressure=None):
if ref_pressure is None:<EOL><INDENT>ref_pressure = pressure[<NUM_LIT:0>]<EOL><DEDENT>return temperature * (pressure / ref_pressure)**mpconsts.kappa<EOL>
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 le...
f8476:m4
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def moist_lapse(pressure, temperature, ref_pressure=None):
def dt(t, p):<EOL><INDENT>t = units.Quantity(t, temperature.units)<EOL>p = units.Quantity(p, pressure.units)<EOL>rs = saturation_mixing_ratio(p, t)<EOL>frac = ((mpconsts.Rd * t + mpconsts.Lv * rs)<EOL>/ (mpconsts.Cp_d + (mpconsts.Lv * mpconsts.Lv * rs * mpconsts.epsilon<EOL>/ (mpconsts.Rd * t * t)))).to('<STR_LIT>')<EO...
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.Q...
f8476:m5
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def lcl(pressure, temperature, dewpt, max_iters=<NUM_LIT:50>, eps=<NUM_LIT>):
def _lcl_iter(p, p0, w, t):<EOL><INDENT>td = dewpoint(vapor_pressure(units.Quantity(p, pressure.units), w))<EOL>return (p0 * (td / t) ** (<NUM_LIT:1.> / mpconsts.kappa)).m<EOL><DEDENT>w = mixing_ratio(saturation_vapor_pressure(dewpt), pressure)<EOL>fp = so.fixed_point(_lcl_iter, pressure.m, args=(pressure.m, w, tempera...
r"""Calculate the lifted condensation level (LCL) using from the starting point. The starting state for the parcel is defined by `temperature`, `dewpt`, and `pressure`. Parameters ---------- pressure : `pint.Quantity` The starting atmospheric pressure temperature : `pint.Quantity` ...
f8476:m6
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def lfc(pressure, temperature, dewpt, parcel_temperature_profile=None, dewpt_start=None):
<EOL>if parcel_temperature_profile is None:<EOL><INDENT>new_stuff = parcel_profile_with_lcl(pressure, temperature, dewpt)<EOL>pressure, temperature, _, parcel_temperature_profile = new_stuff<EOL>temperature = temperature.to('<STR_LIT>')<EOL>parcel_temperature_profile = parcel_temperature_profile.to('<STR_LIT>')<EOL><DE...
r"""Calculate the level of free convection (LFC). This works by finding the first intersection of the ideal parcel path and the measured parcel temperature. Parameters ---------- pressure : `pint.Quantity` The atmospheric pressure temperature : `pint.Quantity` The temperature a...
f8476:m7
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def el(pressure, temperature, dewpt, parcel_temperature_profile=None):
<EOL>if parcel_temperature_profile is None:<EOL><INDENT>new_stuff = parcel_profile_with_lcl(pressure, temperature, dewpt)<EOL>pressure, temperature, _, parcel_temperature_profile = new_stuff<EOL>temperature = temperature.to('<STR_LIT>')<EOL>parcel_temperature_profile = parcel_temperature_profile.to('<STR_LIT>')<EOL><DE...
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 ...
f8476:m8
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def parcel_profile(pressure, temperature, dewpt):
_, _, _, t_l, _, t_u = _parcel_profile_helper(pressure, temperature, dewpt)<EOL>return concatenate((t_l, t_u))<EOL>
r"""Calculate the profile a parcel takes through the atmosphere. The parcel starts at `temperature`, and `dewpt`, lifted up dry adiabatically to the LCL, and then moist adiabatically from there. `pressure` specifies the pressure levels for the profile. Parameters ---------- pressure : `pint.Qu...
f8476:m9
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def parcel_profile_with_lcl(pressure, temperature, dewpt):
p_l, p_lcl, p_u, t_l, t_lcl, t_u = _parcel_profile_helper(pressure, temperature[<NUM_LIT:0>],<EOL>dewpt[<NUM_LIT:0>])<EOL>new_press = concatenate((p_l, p_lcl, p_u))<EOL>prof_temp = concatenate((t_l, t_lcl, t_u))<EOL>new_temp = _insert_lcl_level(pressure, temperature, p_lcl)<EOL>new_dewp = _insert_lcl_level(pressure, de...
r"""Calculate the profile a parcel takes through the atmosphere. The parcel starts at `temperature`, and `dewpt`, lifted up dry adiabatically to the LCL, and then moist adiabatically from there. `pressure` specifies the pressure levels for the profile. This function returns a profile that includes the ...
f8476:m10
def _parcel_profile_helper(pressure, temperature, dewpt):
<EOL>press_lcl, temp_lcl = lcl(pressure[<NUM_LIT:0>], temperature, dewpt)<EOL>press_lcl = press_lcl.to(pressure.units)<EOL>press_lower = concatenate((pressure[pressure >= press_lcl], press_lcl))<EOL>temp_lower = dry_lapse(press_lower, temperature)<EOL>if _greater_or_close(np.nanmin(pressure), press_lcl.m):<EOL><INDENT>...
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.
f8476:m11
def _insert_lcl_level(pressure, temperature, lcl_pressure):
interp_temp = interpolate_1d(lcl_pressure, pressure, temperature)<EOL>loc = pressure.size - pressure[::-<NUM_LIT:1>].searchsorted(lcl_pressure)<EOL>return np.insert(temperature.m, loc, interp_temp.m) * temperature.units<EOL>
Insert the LCL pressure into the profile.
f8476:m12
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>')<EOL>def vapor_pressure(pressure, mixing):
return pressure * mixing / (mpconsts.epsilon + mixing)<EOL>
r"""Calculate water vapor (partial) pressure. Given total `pressure` and water vapor `mixing` ratio, calculates the partial pressure of water vapor. Parameters ---------- pressure : `pint.Quantity` total atmospheric pressure mixing : `pint.Quantity` dimensionless mass mixing ra...
f8476:m13
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>')<EOL>def saturation_vapor_pressure(temperature):
<EOL>return sat_pressure_0c * np.exp(<NUM_LIT> * (temperature - <NUM_LIT> * units.kelvin)<EOL>/ (temperature - <NUM_LIT> * units.kelvin))<EOL>
r"""Calculate the saturation water vapor (partial) pressure. Parameters ---------- temperature : `pint.Quantity` The temperature Returns ------- `pint.Quantity` The saturation water vapor (partial) pressure See Also -------- vapor_pressure, dewpoint Notes ...
f8476:m14
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>')<EOL>def dewpoint_rh(temperature, rh):
if np.any(rh > <NUM_LIT>):<EOL><INDENT>warnings.warn('<STR_LIT>')<EOL><DEDENT>return dewpoint(rh * saturation_vapor_pressure(temperature))<EOL>
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` T...
f8476:m15
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>')<EOL>def dewpoint(e):
val = np.log(e / sat_pressure_0c)<EOL>return <NUM_LIT:0.> * units.degC + <NUM_LIT> * units.delta_degC * val / (<NUM_LIT> - val)<EOL>
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 N...
f8476:m16
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def mixing_ratio(part_press, tot_press, molecular_weight_ratio=mpconsts.epsilon):
return (molecular_weight_ratio * part_press<EOL>/ (tot_press - part_press)).to('<STR_LIT>')<EOL>
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 pre...
f8476:m17
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>')<EOL>def saturation_mixing_ratio(tot_press, temperature):
return mixing_ratio(saturation_vapor_pressure(temperature), tot_press)<EOL>
r"""Calculate the saturation mixing ratio of water vapor. This calculation is given total pressure and the temperature. The implementation uses the formula outlined in [Hobbs1977]_ pg.73. Parameters ---------- tot_press: `pint.Quantity` Total atmospheric pressure temperature: `pint.Qua...
f8476:m18
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def equivalent_potential_temperature(pressure, temperature, dewpoint):
t = temperature.to('<STR_LIT>').magnitude<EOL>td = dewpoint.to('<STR_LIT>').magnitude<EOL>p = pressure.to('<STR_LIT>').magnitude<EOL>e = saturation_vapor_pressure(dewpoint).to('<STR_LIT>').magnitude<EOL>r = saturation_mixing_ratio(pressure, dewpoint).magnitude<EOL>t_l = <NUM_LIT> + <NUM_LIT:1.> / (<NUM_LIT:1.> / (td - ...
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}...
f8476:m19
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>')<EOL>def saturation_equivalent_potential_temperature(pressure, temperature):
t = temperature.to('<STR_LIT>').magnitude<EOL>p = pressure.to('<STR_LIT>').magnitude<EOL>e = saturation_vapor_pressure(temperature).to('<STR_LIT>').magnitude<EOL>r = saturation_mixing_ratio(pressure, temperature).magnitude<EOL>th_l = t * (<NUM_LIT:1000> / (p - e)) ** mpconsts.kappa<EOL>th_es = th_l * np.exp((<NUM_LIT> ...
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 satura...
f8476:m20
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def virtual_temperature(temperature, mixing, molecular_weight_ratio=mpconsts.epsilon):
return temperature * ((mixing + molecular_weight_ratio)<EOL>/ (molecular_weight_ratio * (<NUM_LIT:1> + mixing)))<EOL>
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` dimens...
f8476:m21
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def virtual_potential_temperature(pressure, temperature, mixing,<EOL>molecular_weight_ratio=mpconsts.epsilon):
pottemp = potential_temperature(pressure, temperature)<EOL>return virtual_temperature(pottemp, mixing, molecular_weight_ratio)<EOL>
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 temper...
f8476:m22
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def density(pressure, temperature, mixing, molecular_weight_ratio=mpconsts.epsilon):
virttemp = virtual_temperature(temperature, mixing, molecular_weight_ratio)<EOL>return (pressure / (mpconsts.Rd * virttemp)).to(units.kilogram / units.meter ** <NUM_LIT:3>)<EOL>
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 ...
f8476:m23
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def relative_humidity_wet_psychrometric(dry_bulb_temperature, web_bulb_temperature,<EOL>pressure, **kwargs):
return (psychrometric_vapor_pressure_wet(dry_bulb_temperature, web_bulb_temperature,<EOL>pressure, **kwargs)<EOL>/ saturation_vapor_pressure(dry_bulb_temperature))<EOL>
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: `pi...
f8476:m24
@exporter.export<EOL>@preprocess_xarray<EOL>@check_units('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>def psychrometric_vapor_pressure_wet(dry_bulb_temperature, wet_bulb_temperature, pressure,<EOL>psychrometer_coefficient=<NUM_LIT> / units.kelvin):
return (saturation_vapor_pressure(wet_bulb_temperature) - psychrometer_coefficient<EOL>* pressure * (dry_bulb_temperature - wet_bulb_temperature).to('<STR_LIT>'))<EOL>
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....
f8476:m25