The vapor_utils module#

The vapor_utils module comes with functions that are useful for any Vapor user. The following can be used to calculate magnitude of one or more vector variables once input variables have been selected and a new magnitude variable has been defined in the gui. See below for all available functions.

from vapor_utils import *
magnitude = Mag(U, V, W)

The vapor_utils module contains: StaggeredToUnstaggeredGrid - resample staggered grid DerivFinDiff - calculate derivative using 6th order finite differences DerivVarFinDiff - calculate a derivative of one 3D variable with respect to another variable. CurlFinDiff - calculate curl using finite differences DivFinDiff - calculate divergence using finite differences GradFinDiff - calculate gradient using finite differences. Interp3d - interpolate a 3D variable to a vertical level surface of another variable. VectorRotate - rotate and scale vector field for lat-lon grid.

vapor_utils.CurlFinDiff(M, N, P, dx, dy, dz, order=6)#

Function that calculates the Curl of a vector field on Cartesian grids

This function computes the curl of a 3D vector field defined by the vector component arrays M, N, and P using 2nd, 4th, or 6th order finite differences.

If F is defined as

M(x,y,z)i + N(x,y,z)j + P(x,y,z)

then curl F is given by:

(dP/dy - dN/dz)i + (dM/dz - dP/dx)j + (dN/dx - dM/dy)k

Parameters#

Mnumpy.ndarray

A three-dimensional Numpy array giving the x component of the vector

Nnumpy.ndarray

A three-dimensional Numpy array giving the y component of the vector

Pnumpy.ndarray

A three-dimensional Numpy array giving the z component of the vector

dxfloat

The differential step size along the fastest varying axis

dyfloat

The differential step size along the second fastest varying axis

dzfloat

The differential step size along the third fastest varying axis

orderint, optional

The accuracy order of finite difference method. The default is 6. Valid values are 2, 4, 6.

Calling sequence#

>>> wx,wy,wz = CurlFinDiff(M,N,P,dx,dy,dz,order=6)

Returns#

wx,wy,wznumpy.ndarray

The i,j,k components of the curl, respectively

vapor_utils.DerivFinDiff(a, axis, dx, order=6)#

Function that calculates first-order derivatives on Cartesian grids.

This function computes the partial derivative of a multidimensional array using 2nd, 4th, or 6th order finite differences.

Parameters#

anumpy.ndarray

A two or three-dimensional Numpy array

axisint

The axis along which the derivative should be taken. The slowest varying axis is 0. The next slowest is 1.

dxfloat

The differential step size

orderint, optional

The accuracy order of finite difference method. The default is 6. Valid values are 2, 4, 6.

Calling sequence#

>>> deriv = DerivFinDiff(a,axis,delta, order=6)

Returns#

da_dxnumpy.ndarray

The derivative of a with respect to dx along axis

vapor_utils.DerivVarFinDiff(a, var, axis, order=6)#

Function that calculates first-order derivatives on Cartesian grids with respect to another variable

This function computes the partial derivative of a multidimensional array using 2nd, 4th, or 6th order finite differences with respect to a second multidimensional array of the same shape.

Parameters#

anumpy.ndarray

A two or three-dimensional Numpy array

varnumpy.ndarray

A two or three-dimensional Numpy array of the same shape as a

axisint

The axis along which the derivative should be taken. The slowest varying axis is 0. The next slowest is 1.

orderint, optional

The accuracy order of finite difference method. The default is 6. Valid values are 2, 4, 6.

Calling sequence#

>>> deriv = DerivFinDiff(a,var,delta, order=6)

Returns#

da_varnumpy.ndarray

The derivative of a with respect to var along axis

vapor_utils.DivFinDiff(M, N, P, dx, dy, dz, order=6)#

Function that calculates the Divergence of a vector field on Cartesian grids

This function computes the divergence of a 3D vector field defined by the vector component arrays M, N, and P using 2nd, 4th, or 6th order finite differences.

If F is defined as

M(x,y,z)i + N(x,y,z)j + P(x,y,z)

then div F is given by:

dM/dx + dN/dy + dP/dz

Parameters#

Mnumpy.ndarray

A three-dimensional Numpy array giving the x component of the vector

Nnumpy.ndarray

A three-dimensional Numpy array giving the y component of the vector

Pnumpy.ndarray

A three-dimensional Numpy array giving the z component of the vector

dxfloat

The differential step size along the fastest varying axis

dyfloat

The differential step size along the second fastest varying axis

dzfloat

The differential step size along the third fastest varying axis

orderint, optional

The accuracy order of finite difference method. The default is 6. Valid values are 2, 4, 6.

Calling sequence#

>>> a = DivFinDiff(M,N,P,dx,dy,dz,order=6)

Returns#

wx,wy,wznumpy.ndarray

The i,j,k components of the curl, respectively

vapor_utils.GradFinDif(A, dx, dy, dz, order=6)#

Function that calculates the Gradient of a scalar field on Cartesian grids

This function computes the gradient of a scalar field A:

dA/dx*i + dA/dy*j + dA/dz*k

Parameters#

Anumpy.ndarray

A three-dimensional Numpy array

dxfloat

The differential step size along the fastest varying axis

dyfloat

The differential step size along the second fastest varying axis

dzfloat

The differential step size along the third fastest varying axis

orderint, optional

The accuracy order of finite difference method. The default is 6. Valid values are 2, 4, 6.

Calling sequence#

>>> da_dx,da_dy,da_dz = GradFinDif(A,dx,dy,dz,order=6)

Returns#

da_dx,da_dy,da_dz: numpy.ndarray

The partial derivatives of A with respect to x,y,z, respectively

vapor_utils.Interp3d(A, PR, val)#

Method that vertically interpolates one 3D variable to a level determined by another variable. The second variable (PR) is typically pressure. The second variable must decrease as a function of z (elevation). The returned value is a 2D variable having values interpolated to the surface defined by PR = val Sweep array from bottom to top

vapor_utils.Mag(*argv)#

Return the magnitude of one or more vectors

This method computes the vector magnitude of the Numpy arrays in args. Each array in args must have the same number of dimensions. The arrays may be a mixture of staggered and unstaggered arrays. I.e. for any axis the dimension length may differ by no more than one. Staggered arrays are downsampled along the staggered axis to have the same dimension length as the unstaggered array. Thus all arrays are resampled as necessary to have the same shape prior to computing the array magnitude.

Parameters#

*argvtuple of numpy.ndarray

A a list of two or three-dimensional Numpy arrays

Returns#

anumpy.ndarray

The vector magnitude

vapor_utils.StaggeredToUnstaggeredGrid(a, axis)#

Resample a numpy array on a staggered grid to an unstaggered grid

This function is useful for resampling data sampled on an Arakawa C-grid to an Arakawa A-grid. E.g. resampling the velocity grid to the mass grid. It simply down samples the specified axis specified by axis by one grid point, locating the new grid points in the returned array at the midpoints of the samples in the original array, a

Parameters#

anumpy.ndarray

A two or three dimensional Numpy array

axis : int

An integer in the range 0..n, where n is a.ndim - 1, specifying which axis should be downsampled. Zero is the slowest varying dimension.

Returns#

aprime: numpy.ndarray:

The resampled array

vapor_utils.VectorRotate(angleRad, latDeg, u, v)#

Rotate and scale vectors u,v for integration on lon-lat grid. Calling sequence: rotfield=VectorRotate(angleRad, latDeg, u,v) Where: angleRad: 2D var, rotation from East in radians latDeg: 2D var, latitude in degrees u,v: 3D vars, x,y components of a vector field rotfield is a 2-tuple of 3-dimensional float32 arrays, representing rotation of u,v, returned by this operator.

vapor_utils.mag2d(a1, a2)#

Calculate the magnitude of a 2-vector. Calling sequence: MAG = mag2d(A,B) Where: A, and B are float32 np arrays. Result MAG is a float32 np array containing the square root of the sum of the squares of A and B.

vapor_utils.mag3d(a1, a2, a3)#

Calculate the magnitude of a 3-vector. Calling sequence: MAG = mag3d(A,B,C) Where: A, B, and C are float32 np arrays. Result MAG is a float32 np array containing the square root of the sum of the squares of A, B, and C.