small tools package¶
DocStrings¶
-
class
dypy.small_tools.
CrossSection
(variables, coo, pressure, int2p=False, int2z=False, flip=False, pollon=-170, pollat=43, nbre=1000, order=1, version='rotated')[source]¶ Create a cross-section
return the distance, the pressure levels, and the variables.
Parameters: - variables (dictionary) – A dictionary of the variables with the following structure: {‘name’: np.array}. If version is rotated: need to contain as least rlon, rlat if version is regular: need to contain at least lon, lat
- coo (list or numpy.ndarray) – If coo is a list of coordinates, it is used of the starting and end points: [(startlon, startlat), (endlon, endlat)] If coo is a numpy.ndarray it is used as the cross-section points. coo need then to be similar to : np.array([[10, 45], [11, 46], [12, 47]])
- pressure (np.array) – pressure coordinate as 1D array
- int2p (bool, optional (default: False)) – True if variables need to be interpolated on pressure levels, requires p in the variables, the levels are given by pressure
- int2z (bool, optional (default: False)) – True if variables need to be interpolated on heights levels, requires z in the variables, the levels are given by pressure may require `flip`=True
- flip (bool, optional (default: False)) – True if variables need to be flip vertically, the first dimension of 3D array will be flipped
- pollon (float, optional (default: -170)) – pole_longitude of the rotated grid
- pollat (float, optional (default: 43)) – pole_latitude of the rotated grid
- nbre (int, optional (default: 10000)) – nbre of points along the cross-section
- order ({1, 2, 3, 4}, optional (default: 1)) – order of interpolation see for details
- version (string (defautl rotated)) – type of grid used as input (rotated or regular)
-
dypy.small_tools.
interpolate
(data, grid, interplevels)[source]¶ interpolate data on grid for given interplevels
Interpolate the data array at every given levels (interplevels) using the grid array as reference.
The grid array need to be increasing along the first axis. Therefore ERA-Interim pressure must be flip: p[::-1, …], but not COSMO pressure since its level 0 is located at the top of the atmosphere.
Parameters: - data (array (nz, nlat, nlon)) – data to interpolate
- grid (array (nz, nlat, nlon)) – grid use to perform the interpolation
- interplevels (list, array) – list of the new vertical levels, in the same unit as the grid
Returns: interpolated array
Return type: array (len(interplevels), nlat, nlon)
Examples
>>> print(qv.shape) (60, 181, 361) >>> print(p.shape) (60, 181, 361) >>> levels = np.arange(200, 1050, 50) (17,) >>> qv_int = interpolate(qv, p, levels) >>> print(qv_int.shape) (17, 181, 361)
-
dypy.small_tools.
rotate_points
(pole_longitude, pole_latitude, lon, lat, direction='n2r')[source]¶ Rotate lon, lat from/to a rotated system
Parameters: - pole_longitude (float) – longitudinal coordinate of the rotated pole
- pole_latitude (float) – latitudinal coordinate of the rotated pole
- lon (array (1d)) – longitudinal coordinates to rotate
- lat (array (1d)) – latitudinal coordinates to rotate
- direction (string, optional) – direction of the rotation; n2r: from non-rotated to rotated (default) r2n: from rotated to non-rotated
Returns: - rlon (array)
- rlat (array)
-
dypy.small_tools.
dewpoint
(p, qv)[source]¶ Calculate the dew point temperature
following (eq.8): Lawrence, M.G., 2005. The Relationship between Relative Humidity and the Dewpoint Temperature in Moist Air: A Simple Conversion and Applications. Bulletin of the American Meteorological Society 86, 225–233. doi:10.1175/BAMS-86-2-225
Parameters: - p (float, array) – pressure in Pa
- qv (float, array) – specific humidity in kg/kg
Returns: dewpoint (in °C)
Return type: float, array
-
dypy.small_tools.
esat
(t)[source]¶ Calculate the saturation vapor pressure for t in °C
Following eq. 6 of Lawrence, M.G., 2005. The Relationship between Relative Humidity and the Dewpoint Temperature in Moist Air: A Simple Conversion and Applications. Bulletin of the American Meteorological Society 86, 225–233. doi:10.1175/BAMS-86-2-225
Parameters: t (float, numpy.array) – temperature in K Examples
>>> esat(0) 610.94000000000005
-
dypy.small_tools.
moist_lapse
(t, p)[source]¶ Calculates moist adiabatic lapse rate
Note: We calculate dT/dp, not dT/dz See formula 3.16 in Rogers&Yau for dT/dz, but this must be combined with the dry adiabatic lapse rate (gamma = g/cp) and the inverse of the hydrostatic equation (dz/dp = -RT/pg)
Parameters: - t (float, array) – temperature in degree Celsius
- p (float, array) – temperature in Pa
Returns: moist adiabatic lapse rate
Return type: float, array
-
dypy.small_tools.
equivalent_pot_temp
(t, p, qv)[source]¶ Return the equivalent potential temperature in K
computation of equivalent potential temperature according to Bolton (1980) except constant 0.1998 (4.805 according to Bolton).
Parameters: - t (np.array, np.float) – Temperature in K
- p (np.array, np.float) – Pressure in hPa
- qv (np.array, np.float) – Specific humidity in kg/kg
Returns: THE (equivalent potential temperature in K)
Return type: (np.array, np.float)
Examples
>>> t = 16 + 273 >>> qv = 0.01156 >>> p = 850 >>> equivalent_pot_temp(t, p, qv) 337.59612858187029
-
dypy.small_tools.
mask_polygon
(polygon, array, lon, lat)[source]¶ Mask value outside polygon, work only for regular grid
Parameters: - polygon (array (X x 2)) – coordinates of the polygon
- array (array (nlon x nlat)) – array to mask
- lon (array (nlon x nlat)) – regular coordinates of the array
- lat (array (nlon x nlat)) – regular coordinates of the array
Returns: masked array
Return type: MaskedArray (nlon x nlat)
Examples
>>> import numpy as np >>> polygon = np.array([[5, 5], >>> [10, 5], >>> [10, 10], >>> [5, 10], >>> [5, 5]]) >>> array = np.zeros((20, 20)) >>> lon = np.arange(0, 20, dtype=float) >>> lat = np.arange(0, 20, dtype=float) >>> lons, lats = np.meshgrid(lon, lat) >>> clipped = mask_polygon(polygon, array, lons, lats)
-
dypy.small_tools.
potential_temperature
(t, p, p0=1000)[source]¶ Calculate the potential temperature
Parameters: - t (array) – temperature in K
- p (array) – pressure in hPa
- p0 (float, optional) – pressure at sea level in hPa
Returns: potential temperature
Return type: array
-
dypy.small_tools.
great_circle_distance
(lon1, lat1, lon2, lat2)[source]¶ Calculate the great circle distance between two points
based on : https://gist.github.com/gabesmed/1826175
Parameters: - lon1 (float) – longitude of the starting point
- lat1 (float) – latitude of the starting point
- lon2 (float) – longitude of the ending point
- lat2 (float) – latitude of the ending point
Returns: distance (km)
Return type: float
Examples
>>> great_circle_distance(0, 55, 8, 45.5) 1199.3240879770135