# 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

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.

```>>> wx,wy,wz = CurlFinDiff(M,N,P,dx,dy,dz,order=6)
```
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.

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.

```>>> deriv = DerivFinDiff(a,axis,delta, order=6)
```
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.

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.

```>>> deriv = DerivFinDiff(a,var,delta, order=6)
```
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

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.

```>>> a = DivFinDiff(M,N,P,dx,dy,dz,order=6)
```
wx,wy,wznumpy.ndarray

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

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

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.

```>>> da_dx,da_dy,da_dz = GradFinDif(A,dx,dy,dz,order=6)
```
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.

*argvtuple of numpy.ndarray

A a list of two or three-dimensional Numpy arrays

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

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.

aprime: numpy.ndarray:

The resampled array