def relvort(u, v, lats, lons): """ Compute the relative vertical vorticity of the wind Args: u ([type]): 2D arrays of u wind components, in meters per second v ([type]): 2D arrays of v wind components, in meters per second Returns: Returns in units of per second """ #Check if input is an xarray dataarray use_xarray = arr.check_xarray(u) #Compute the gradient of the wind dudy = calculate.compute_gradient(u, lats, lons)[1] dvdx = calculate.compute_gradient(v, lats, lons)[0] #Compute relative vorticity (dv/dx - du/dy) vort = np.subtract(dvdx, dudy) #Account for southern hemisphere tlons, tlats = np.meshgrid(lons, lats) vort[tlats < 0] = vort[tlats < 0] * -1 #Convert back to xarray dataset, if initially passed as one if use_xarray == True: vort = xr.DataArray(vort, coords=[lats, lons], dims=['lat', 'lon']) return vort
def divergence(u, v, lats, lons): """ Compute the horizontal divergence of a vector Args: var ([type]): 2D scalar field (e.g. temperature) u ([type]): 2D arrays of u & v wind components, in meters per second v ([type]): 2D arrays of u & v wind components, in meters per second Returns: Returns in units of per second """ #Check if input is an xarray dataarray use_xarray = arr.check_xarray(u) #Compute the gradient of the wind dudx = calculate.compute_gradient(u, lats, lons)[0] dvdy = calculate.compute_gradient(v, lats, lons)[1] #div = dudx + dvdy #dv/dx - du/dy div = np.add(dudx, dvdy) #Convert back to xarray dataset, if initially passed as one if use_xarray == True: div = xr.DataArray(div, coords=[lats, lons], dims=['lat', 'lon']) return div
def qvect(temp, hght, lev, lats, lons, smooth, static_stability=1): """ Compute the u and v components of the Q-vector Args: temp ([type]): 2D scalar temperature field (K) hght ([type]): 2D scalar geopotential height field (m) lev ([type]): Pressure level (hPa) lats ([type]): 1D arrays of lat lons ([type]): 1D arrays of lon smooth ([type]): integer representing sigma level of smoothing static_stability (int, optional): assumed to be 1, unless provided. Defaults to 1. Returns: Returns in units of meters per second """ #Check if input is an xarray dataarray use_xarray = arr.check_xarray(temp) #Smooth data hght = ndimage.gaussian_filter(hght, sigma=smooth, order=0) temp = ndimage.gaussian_filter(temp, sigma=smooth, order=0) #Convert pressure to Pa levPa = lev * 100.0 #Compute the geostrophic wind ug, vg = geo(hght, lats, lons) #Compute the constant out front const = (-1.0 * Rd) / (levPa * static_stability) #Compute gradient quantities dtdx, dtdy = calculate.compute_gradient(temp, lats, lons) dudx, dudy = calculate.compute_gradient(ug, lats, lons) dvdx, dvdy = calculate.compute_gradient(vg, lats, lons) #Compute u,v components of Q-vector Qu = const * ((dudx * dtdx) + (dvdx * dtdy)) Qv = const * ((dudy * dtdx) + (dvdy * dtdy)) #Convert back to xarray dataset, if initially passed as one if use_xarray == True: Qu = xr.DataArray(Qu, coords=[lats, lons], dims=['lat', 'lon']) Qv = xr.DataArray(Qv, coords=[lats, lons], dims=['lat', 'lon']) return Qu, Qv
def advection(var, u, v, lats, lons): """ Compute the magnitude of horizontal advection of a scalar quantity by the wind Args: var ([type]): 2D scalar field (e.g. temperature) u ([type]): 2D arrays of u & v wind components, in meters per second v ([type]): 2D arrays of u & v wind components, in meters per second Returns: Returns in units of (scalar unit) per second """ #Check if input is an xarray dataarray use_xarray = arr.check_xarray(var) #Compute the gradient of the variable ddx, ddy = calculate.compute_gradient(var, lats, lons) #Compute advection (-v dot grad var) #adv = -1 * ((ddx*u) + (ddy*v)) adv = np.add(np.multiply(ddx, u), np.multiply(ddy, v)) adv = np.multiply(-1.0, adv) #Convert back to xarray dataset, if initially passed as one if use_xarray == True: adv = xr.DataArray(adv, coords=[lats, lons], dims=['lat', 'lon']) return adv
def geo(hght, lats, lons): """ Compute the u and v components of the geostrophic wind Args: hght ([type]): 2D scalar geopotential height field (m) Returns: Returns in units of meters per second """ #Check if input is an xarray dataarray use_xarray = arr.check_xarray(hght) #Compute geopotential height gradient on pressure surface dzdx, dzdy = calculate.compute_gradient(hght, lats, lons) #2D array of Coriolis parameter for each lat/lon cor = coriolis(lats, lons) #Compute the geostrophic wind ug = (-1.0 * dzdy * g) / cor vg = (dzdx * g) / cor #Convert back to xarray dataset, if initially passed as one if use_xarray == True: ug = xr.DataArray(ug, coords=[lats, lons], dims=['lat', 'lon']) vg = xr.DataArray(vg, coords=[lats, lons], dims=['lat', 'lon']) return ug, vg