integrate: Integrate data over a range of depths

Description Usage Arguments Details Value Examples

View source: R/integrate.R

Description

Sum or average a given variable over a range of depths.

Usage

1
integrate(x, depth, fun = sum, from = 0, to = 100, ...)

Arguments

x

vector of the variable of interest.

depth

vector of depths at which x is measured.

fun

function used to perform the integration; usually sum() or mean().

from

depth at which to start the integration; by default 0.

to

depth at which to stop the integration; by default 100 m. Must be greater than from.

...

passed to approx(); allows to choose the method for the interpolation: "linear" (the default) results in integration by trapezoids, "constant" with f=0 or f=1 results in integration by rectangles (and is often not optimal).

Details

Integration is used to compute the stock of a quantity (Chlorophyll a, nutrients, etc.) in a given depth layer (when fun is sum()) of the average of a variable (temperature, density, etc.) in a layer (in which case, fun should be mean()). It is often used with discrete data coming from bottle samples and needs to be made continuous. Here, this is done by interpolating the data every meter, either using either linear or constant interpolation.

Value

The value of the variable, integrated over the depth range.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Stock of chlorophyll a inferred from fluorescence
plot(-depth ~ chla, data=d, type="l")

# near the surface
integrate(d$chla, d$depth, from=0, to=50)
integrate(d$chla, d$depth, from=0, to=50, method="constant")

# around the Deep Chlorophyll Maximum
DCM <- maxd(d$chla, depth=d$depth)
integrate(d$chla, d$depth, from=DCM-10, to=DCM+10)
# which amounts to this quantity
d_DCM <- subset(d, depth > DCM-10 & depth < DCM+10)
polygon(
  x=c(d_DCM$chla, rep(0, times=nrow(d_DCM))),
  y=-c(d_DCM$depth, rev(d_DCM$depth)),
  col="chartreuse3", border=NA
)
abline(h=-DCM, col="chartreuse4")

# For variables which do not represent a quantity, it is usually more
# meaningful to compute the mean.
# Average temperature in the top 50 m
plot(-depth ~ temp, data=d, type="l")
integrate(d$temp, d$depth, from=0, to=50, fun=mean)
# or in the surface, mixed layer
MLD <- mld(d$sigma, d$depth)
abline(h=-MLD, col="red")
integrate(d$temp, d$depth, from=0, to=MLD, fun=mean)

# This also work with discrete data, over a few irregularly spaced depths
# (like those coming from the analysis of water samples in bottles)
d <- data.frame(depth=c(10, 20, 50, 150), chla=c(0.1, 0.2, 0.4, 0.001))
plot(-depth ~ chla, data=d, type="b")
integrate(d$chla, d$depth, from=0, to=100, fun=sum)

jiho/castr documentation built on April 5, 2020, 2:12 p.m.