# Calculating buoyancy frequency for argo/section objects using the apply() family

29 Feb 2016The most recent CRAN release of oce includes some nice new functionality for reading and converting `argo`

objects (see http://www.argo.ucsd.edu/ for more information about the fantastic Argo float program). One question that arose out of this increased functionality was how to calculate (also known as the buoyancy or Brunt-Väisälä frequency) for such objects.

## Buoyancy frequency

The definition of is:

where is the acceleration due to gravity, is the fluid density, and is the vertical coordinate. Essentially describes the vertical variation of fluid *density* (also known as “stratification”).

Calculating for regular `ctd`

objects is easily accomplished with the function `oce::swN2()`

. A caution: readers are encouraged to read the documentation carefully, as the details of the actual calculation can have important consequences when applied to real ocean data.

## for `station`

objects

For the case of a `station`

object (which is essentially a collection of `ctd`

stations), the most straightforward way to calculate is to use the `lapply()`

function to “apply” the `swN2()`

function to each of the stations in the object. An example:

The line with the `lapply()`

command takes the list of stations from the `section`

object, and evaluates each of the resulting `ctd`

objects using the `oceSetData()`

function to add the result of `swN2()`

back into the station `@data`

slot.

If we wanted to make a nice plot of the result, we could do:

where I’ve defined a custom colormap just for the fun of it.

## for `argo`

objects

In an `argo`

object, the default storage for the profiles is a matrix, rather than a list of `ctd`

objects. To calculate and make a plot, the simplest approach would be to use `as.section()`

to convert the `argo`

object to a `section`

class object and then do as above. However, having the field as a matrix allows for greater flexibility in plotting, e.g. using the `imagep()`

function, so one might want to calculated in a manner consistent with the default `argo`

storage format.

Let’s load some example data from the `argo`

dataset included in `oce`

:

Note that I’ve gridded the argo fields so the matrices are at consistent pressure levels. Now we create a function that can be applied to each of the matrix columns, to calculate from a single column of the density matrix:

Now we use the above function `N2`

to calculate buoyancy frequency and add it back to the original object, like:

Note that because of the difference between the “list” and “matrix” approach, the `oceSetData()`

occurs *outside* of the `apply()`

. Also note the second argument in the `apply()`

call, which specifies to apply the `N2()`

function along the 2nd dimension of the density matrix, i.e. along columns.

Now, lets make a sweet plot of the `N2`

field using `imagep()`

!

A thing of stratified beauty, if I do say so myself.