<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><title>Trim Beginning and Ending of a CTD cast — ctdTrim • oce</title><script src="../deps/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><link href="../deps/bootstrap-5.3.1/bootstrap.min.css" rel="stylesheet"><script src="../deps/bootstrap-5.3.1/bootstrap.bundle.min.js"></script><link href="../deps/font-awesome-6.4.2/css/all.min.css" rel="stylesheet"><link href="../deps/font-awesome-6.4.2/css/v4-shims.min.css" rel="stylesheet"><script src="../deps/headroom-0.11.0/headroom.min.js"></script><script src="../deps/headroom-0.11.0/jQuery.headroom.min.js"></script><script src="../deps/bootstrap-toc-1.0.1/bootstrap-toc.min.js"></script><script src="../deps/clipboard.js-2.0.11/clipboard.min.js"></script><script src="../deps/search-1.0.0/autocomplete.jquery.min.js"></script><script src="../deps/search-1.0.0/fuse.min.js"></script><script src="../deps/search-1.0.0/mark.min.js"></script><!-- pkgdown --><script src="../pkgdown.js"></script><meta property="og:title" content="Trim Beginning and Ending of a CTD cast — ctdTrim"><meta name="description" content='Often in CTD profiling, the goal is to isolate only the downcast, discarding measurements made in
the air, in an equilibration phase in which the device is held below the water surface, and then the
upcast phase that follows the downcast.  This is handled reasonably well by ctdTrim with
method="downcast", although it is almost always best to use plotScan() to
investigate the data, and then use the method="index" or method="scan" method based on
visual inspection of the data.'><meta property="og:description" content='Often in CTD profiling, the goal is to isolate only the downcast, discarding measurements made in
the air, in an equilibration phase in which the device is held below the water surface, and then the
upcast phase that follows the downcast.  This is handled reasonably well by ctdTrim with
method="downcast", although it is almost always best to use plotScan() to
investigate the data, and then use the method="index" or method="scan" method based on
visual inspection of the data.'></head><body>
    <a href="#main" class="visually-hidden-focusable">Skip to contents</a>


    <nav class="navbar navbar-expand-lg fixed-top bg-light" data-bs-theme="light" aria-label="Site navigation"><div class="container">

    <a class="navbar-brand me-2" href="../index.html">oce</a>

    <small class="nav-text text-muted me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="">1.8-4</small>


    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div id="navbar" class="collapse navbar-collapse ms-3">
      <ul class="navbar-nav me-auto"><li class="active nav-item"><a class="nav-link" href="../reference/index.html">Reference</a></li>
<li class="nav-item dropdown">
  <button class="nav-link dropdown-toggle" type="button" id="dropdown-articles" data-bs-toggle="dropdown" aria-expanded="false" aria-haspopup="true">Articles</button>
  <ul class="dropdown-menu" aria-labelledby="dropdown-articles"><li><a class="dropdown-item" href="../articles/A_oce.html">1. Introduction to Oce</a></li>
    <li><a class="dropdown-item" href="../articles/B_ctd.html">2. Analysis of CTD data</a></li>
    <li><a class="dropdown-item" href="../articles/C_adp.html">3. Analysis of acoustic-Doppler data</a></li>
    <li><a class="dropdown-item" href="../articles/D_map_projections.html">4. Using map projections</a></li>
    <li><a class="dropdown-item" href="../articles/E_flags.html">5. Dealing with data-quality flags</a></li>
    <li><a class="dropdown-item" href="../articles/F_subclassing.html">6. Subclassing oce objects</a></li>
    <li><a class="dropdown-item" href="../articles/G_altering_defaults.html">7. Altering oce Defaults</a></li>
    <li><a class="dropdown-item" href="../articles/H_tides.html">8. Tidal Analysis</a></li>
  </ul></li>
<li class="nav-item"><a class="nav-link" href="../news/index.html">Changelog</a></li>
      </ul><ul class="navbar-nav"><li class="nav-item"><form class="form-inline" role="search">
 <input class="form-control" type="search" name="search-input" id="search-input" autocomplete="off" aria-label="Search site" placeholder="Search for" data-search-index="../search.json"></form></li>
<li class="nav-item"><a class="external-link nav-link" href="https://github.com/dankelley/oce/" aria-label="GitHub"><span class="fa fab fa-github fa-lg"></span></a></li>
      </ul></div>


  </div>
</nav><div class="container template-reference-topic">
<div class="row">
  <main id="main" class="col-md-9"><div class="page-header">

      <h1>Trim Beginning and Ending of a CTD cast</h1>
      <small class="dont-index">Source: <a href="https://github.com/dankelley/oce/blob/HEAD/R/ctd.R" class="external-link"><code>R/ctd.R</code></a></small>
      <div class="d-none name"><code>ctdTrim.Rd</code></div>
    </div>

    <div class="ref-description section level2">
    <p>Often in CTD profiling, the goal is to isolate only the downcast, discarding measurements made in
the air, in an equilibration phase in which the device is held below the water surface, and then the
upcast phase that follows the downcast.  This is handled reasonably well by <code>ctdTrim</code> with
<code>method="downcast"</code>, although it is almost always best to use <code><a href="plotScan.html">plotScan()</a></code> to
investigate the data, and then use the <code>method="index"</code> or <code>method="scan"</code> method based on
visual inspection of the data.</p>
    </div>

    <div class="section level2">
    <h2 id="ref-usage">Usage<a class="anchor" aria-label="anchor" href="#ref-usage"></a></h2>
    <div class="sourceCode"><pre class="sourceCode r"><code><span><span class="fu">ctdTrim</span><span class="op">(</span></span>
<span>  <span class="va">x</span>,</span>
<span>  <span class="va">method</span>,</span>
<span>  removeDepthInversions <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span>  parameters <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  indices <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span>  debug <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/options.html" class="external-link">getOption</a></span><span class="op">(</span><span class="st">"oceDebug"</span><span class="op">)</span></span>
<span><span class="op">)</span></span></code></pre></div>
    </div>

    <div class="section level2">
    <h2 id="arguments">Arguments<a class="anchor" aria-label="anchor" href="#arguments"></a></h2>


<dl><dt id="arg-x">x<a class="anchor" aria-label="anchor" href="#arg-x"></a></dt>
<dd><p>a <a href="ctd-class.html">ctd</a> object.</p></dd>


<dt id="arg-method">method<a class="anchor" aria-label="anchor" href="#arg-method"></a></dt>
<dd><p>A string (or a vector of two strings) specifying the trimming method, or a function to
be used to determine data indices to keep.  If <code>method</code> is not provided, <code>"downcast"</code> is
assumed. See “Details”.</p></dd>


<dt id="arg-removedepthinversions">removeDepthInversions<a class="anchor" aria-label="anchor" href="#arg-removedepthinversions"></a></dt>
<dd><p>Logical value indicating whether to remove any levels at which depth is
less than, or equal to, a depth above.  (This is needed if the object is to be assembled into a
section, unless <code><a href="ctdDecimate.html">ctdDecimate()</a></code> will be used, which will remove the inversions.</p></dd>


<dt id="arg-parameters">parameters<a class="anchor" aria-label="anchor" href="#arg-parameters"></a></dt>
<dd><p>A list whose elements depend on the method; see “Details”.</p></dd>


<dt id="arg-indices">indices<a class="anchor" aria-label="anchor" href="#arg-indices"></a></dt>
<dd><p>Logical value indicating what to return. If <code>indices=FALSE</code> (the default),
then the return value is a subsetted <a href="ctd-class.html">ctd</a> object. If <code>indices=TRUE</code>,
then the return value is a logical vector that could be used to subset the data
with <code><a href="subset-ctd-method.html">subset,ctd-method()</a></code> or to set data-quality flags.</p></dd>


<dt id="arg-debug">debug<a class="anchor" aria-label="anchor" href="#arg-debug"></a></dt>
<dd><p>an integer specifying whether debugging information is
to be printed during the processing. This is a general parameter that
is used by many <code>oce</code> functions. Generally, setting <code>debug=0</code>
turns off the printing, while higher values suggest that more information
be printed. If one function calls another, it usually reduces the value of
<code>debug</code> first, so that a user can often obtain deeper debugging
by specifying higher <code>debug</code> values.</p></dd>

</dl></div>
    <div class="section level2">
    <h2 id="value">Value<a class="anchor" aria-label="anchor" href="#value"></a></h2>
    <p>Either a <a href="ctd-class.html">ctd</a> object or a logical vector of length matching
the data. In the first case, which is the default, the elements of the <code>data</code>
slot will have been trimmed, along with some elements of the <code>metadata</code> slot
(e.g. <code>metadata4flags</code> and, if present and of length matching
<code>data$pressure</code>, both <code>metadata$longitude</code> and <code>metadata$latitude</code>).  The
second case, achieved by setting <code>indices=FALSE</code>, may be helpful for advanced
users who wish to do things like construct data flags to be inserted into the
object.</p>
    </div>
    <div class="section level2">
    <h2 id="details">Details<a class="anchor" aria-label="anchor" href="#details"></a></h2>
    <p><code>ctdTrim</code> begins by examining the pressure differences between subsequent samples. If
these are all of the same value, then the input <code>ctd</code> object is returned, unaltered.
This handles the case of pressure-binned data. However, if the pressure difference
varies, a variety of approaches are taken to trimming the dataset.</p><ul><li><p>If <code>method[1]</code> is <code>"downcast"</code> then an attempt is made to keep only data for
which the CTD is descending.  This is done in stages, with variants based on <code>method[2]</code>, if
supplied.</p><ol><li><p>The pressure data are despiked with a smooth() filter with method "3R".
This removes wild spikes that arise from poor instrument connections, etc.</p></li>
<li><p><em>Step 2.</em> If no <code>parameters</code> are given, then any data with negative pressures
are deleted.  If there is a parameter named <code>pmin</code>, then that pressure (in decibars)
is used instead as the lower limit. This is a commonly-used setup, e.g.
<code>ctdTrim(ctd, parameters=list(pmin=1))</code> removes the top decibar (roughly 1m) from
the data.  Specifying <code>pmin</code> is a simple way to remove near-surface
data, such as a shallow equilibration phase, and if specified will cause <code>ctdTrim</code>
to skip step 4 below.</p></li>
<li><p>The maximum pressure is determined, and data acquired subsequent to
that point are deleted.  This removes the upcast and any subsequent data.</p></li>
<li><p>If the <code>pmin</code> parameter is not specified, an attempt is made to remove an initial
equilibrium phase by a regression of pressure on scan number.  There are three
variants to this, depending on the value of the second <code>method</code> element.
If <code>method</code> is <code>"A"</code> (or not given), the procedure is to
call <code><a href="https://rdrr.io/r/stats/nls.html" class="external-link">nls()</a></code> to fit a piecewise linear model of pressure as a function of scan,
in which pressure is constant for scan less than a critical value, and then
linearly varying for with scan. This is  meant to handle the common situation
in which the CTD is held at roughly constant depth (typically
a metre or so) to equilibrate, before it is lowered through the water column.
If <code>method</code> is <code>"B"</code>, the procedure is similar, except that the pressure
in the surface region is taken to be zero (this does not make
much sense, but it might help in some cases). Note that, prior to early 2016,
method <code>"B"</code> was called method <code>"C"</code>; the old <code>"B"</code> method was judged useless
and so it was removed.</p></li>
</ol></li>
<li><p>If <code>method="upcast"</code>, a sort of reverse of <code>"downcast"</code> is used. This
was added in late April 2017 and has not been well tested yet.</p></li>
<li><p>If <code>method="sbe"</code>, a method similar to that described
in the SBE Data Processing manual is used to remove the "soak"
period at the beginning of a cast (see Section 6 under subsection
"Loop Edit"). The method is based on the soak procedure whereby
the instrument sits at a fixed depth for a period of time, after
which it is raised toward the surface before beginning the actual
downcast. This enables equilibration of the sensors while still
permitting reasonably good near-surface data. Parameters for the
method can be passed using the <code>parameters</code> argument, which
include <code>minSoak</code> (the minimum depth for the soak) and
<code>maxSoak</code> the maximum depth of the soak. The method finds
the minimum pressure prior to the <code>maxSoak</code> value being
passed, each of which occurring after the scan in which the
<code>minSoak</code> value was reached. For the method to work, the
pre-cast pressure minimum must be less than the <code>minSoak</code>
value. The default values of <code>minSoak</code> and <code>maxSoak</code>
are 1 and 20 dbar, respectively.</p></li>
<li><p>If <code>method="index"</code> or <code>"scan"</code>, then each column of data is subsetted according to the
value of <code>parameters</code>. If the latter is a logical vector of length matching data column
length, then it is used directly for subsetting. If <code>parameters</code> is a numerical vector with
two elements, then the index or scan values that lie between <code>parameters[1]</code>
and <code>parameters[2]</code> (inclusive) are used for subsetting.  The
two-element method is probably the most useful, with the values being determined by visual
inspection of the results of <code><a href="plotScan.html">plotScan()</a></code>. While this may take a minute or two, the
analyst should bear in mind that a deep-water CTD profile might take 6 hours, corresponding to
ship-time costs exceeding a week of salary.</p></li>
<li><p>If <code>method="range"</code> then data are selected based on the value of the column named
<code>parameters$item</code>.  This may be by range or by critical value.  By range: select values
between <code>parameters$from</code> (the lower limit) and <code>parameters$to</code> (the upper limit) By
critical value: select if the named column exceeds the value.  For example, <code>ctd2 &lt;-
    ctdTrim(ctd, "range", parameters=list(item="scan", from=5))</code> starts at scan number 5 and
continues to the end, while
<code>ctdTrim(ctd,"range", parameters=list(item="scan", from=5, to=100))</code> also starts at scan 5,
but extends only to scan 100.</p></li>
<li><p>If <code>method</code> is a function, then it must return a vector of <code><a href="https://rdrr.io/r/base/logical.html" class="external-link">logical()</a></code>
values, computed based on two arguments: <code>data</code> (a
<code><a href="https://rdrr.io/r/base/list.html" class="external-link">list()</a></code>), and <code>parameters</code> as supplied to <code>ctdTrim</code>.  Both
<code>inferWaterDepth</code> and <code>removeInversions</code> are ignored in the function case. See
“Examples”.</p></li>
</ul></div>
    <div class="section level2">
    <h2 id="historical-note">Historical Note<a class="anchor" aria-label="anchor" href="#historical-note"></a></h2>



<p>The subsetting of <code>longitude</code> and <code>latitude</code> in the <code>metadata</code> slot was
introduced on 2022-12-13, for use with <a href="ctd-class.html">ctd</a> objects created using
<code><a href="as.ctd.html">as.ctd()</a></code> on <a href="rsk-class.html">rsk</a> objects created by using <code><a href="read.rsk.html">read.rsk()</a></code> on Ruskin
files that hold data from RBR CTD instruments linked with phone/tablet
devices equipped with GPS sensors.</p>
    </div>
    <div class="section level2">
    <h2 id="sample-of-usage">Sample of Usage<a class="anchor" aria-label="anchor" href="#sample-of-usage"></a></h2>


<div class="sourceCode"><pre><code><span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://dankelley.github.io/oce/">oce</a></span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/data.html" class="external-link">data</a></span><span class="op">(</span><span class="va">ctdRaw</span><span class="op">)</span></span>
<span><span class="co"># Example 1: focus on downcast</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html" class="external-link">plot</a></span><span class="op">(</span><span class="fu"><a href="../reference/ctdTrim.html">ctdTrim</a></span><span class="op">(</span><span class="va">ctdRaw</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="co"># Example 2: user-supplied function.</span></span>
<span><span class="va">trimByIndex</span><span class="op">&lt;-</span><span class="kw">function</span><span class="op">(</span><span class="va">data</span>, <span class="va">parameters</span><span class="op">)</span> <span class="op">{</span></span>
<span>    <span class="va">parameters</span><span class="op">[</span><span class="fl">1</span><span class="op">]</span> <span class="op">&lt;</span> <span class="va">data</span><span class="op">$</span><span class="va">scan</span> <span class="op">&amp;</span> <span class="va">data</span><span class="op">$</span><span class="va">scan</span> <span class="op">&lt;</span> <span class="va">parameters</span><span class="op">[</span><span class="fl">2</span><span class="op">]</span></span>
<span><span class="op">}</span></span>
<span><span class="va">trimmed</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/ctdTrim.html">ctdTrim</a></span><span class="op">(</span><span class="va">ctdRaw</span>, <span class="va">trimByIndex</span>, parameters<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">130</span>, <span class="fl">380</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html" class="external-link">plot</a></span><span class="op">(</span><span class="va">trimmed</span><span class="op">)</span></span></code></pre></div>

    </div>
    <div class="section level2">
    <h2 id="references">References<a class="anchor" aria-label="anchor" href="#references"></a></h2>
    <p>The Seabird CTD instrument is described at
<code>http://www.seabird.com/products/spec_sheets/19plusdata.htm</code>.</p>
<p>Seasoft V2: SBE Data Processing, SeaBird Scientific, 05/26/2016</p>
    </div>
    <div class="section level2">
    <h2 id="see-also">See also<a class="anchor" aria-label="anchor" href="#see-also"></a></h2>
    <div class="dont-index"><p>Other things related to ctd data:
<code><a href="CTD_BCD2014666_008_1_DN.ODF.gz.html">CTD_BCD2014666_008_1_DN.ODF.gz</a></code>,
<code><a href="sub-sub-ctd-method.html">[[,ctd-method</a></code>,
<code>[[&lt;-,ctd-method</code>,
<code><a href="as.ctd.html">as.ctd</a>()</code>,
<code><a href="cnvName2oceName.html">cnvName2oceName</a>()</code>,
<code><a href="ctd.html">ctd</a></code>,
<code><a href="ctd-class.html">ctd-class</a></code>,
<code><a href="ctd.cnv.gz.html">ctd.cnv.gz</a></code>,
<code><a href="ctdDecimate.html">ctdDecimate</a>()</code>,
<code><a href="ctdFindProfiles.html">ctdFindProfiles</a>()</code>,
<code><a href="ctdFindProfilesRBR.html">ctdFindProfilesRBR</a>()</code>,
<code><a href="ctdRaw.html">ctdRaw</a></code>,
<code><a href="ctdRepair.html">ctdRepair</a>()</code>,
<code><a href="ctd_aml_type1.csv.gz.html">ctd_aml_type1.csv.gz</a></code>,
<code><a href="ctd_aml_type3.csv.gz.html">ctd_aml_type3.csv.gz</a></code>,
<code><a href="d200321-001.ctd.gz.html">d200321-001.ctd.gz</a></code>,
<code><a href="d201211_0011.cnv.gz.html">d201211_0011.cnv.gz</a></code>,
<code><a href="handleFlags-ctd-method.html">handleFlags,ctd-method</a></code>,
<code><a href="initialize-ctd-method.html">initialize,ctd-method</a></code>,
<code><a href="initializeFlagScheme-ctd-method.html">initializeFlagScheme,ctd-method</a></code>,
<code><a href="oceNames2whpNames.html">oceNames2whpNames</a>()</code>,
<code><a href="oceUnits2whpUnits.html">oceUnits2whpUnits</a>()</code>,
<code><a href="plot-ctd-method.html">plot,ctd-method</a></code>,
<code><a href="plotProfile.html">plotProfile</a>()</code>,
<code><a href="plotScan.html">plotScan</a>()</code>,
<code><a href="plotTS.html">plotTS</a>()</code>,
<code><a href="read.ctd.html">read.ctd</a>()</code>,
<code><a href="read.ctd.aml.html">read.ctd.aml</a>()</code>,
<code><a href="read.ctd.itp.html">read.ctd.itp</a>()</code>,
<code><a href="read.ctd.odf.html">read.ctd.odf</a>()</code>,
<code><a href="read.ctd.odv.html">read.ctd.odv</a>()</code>,
<code><a href="read.ctd.saiv.html">read.ctd.saiv</a>()</code>,
<code><a href="read.ctd.sbe.html">read.ctd.sbe</a>()</code>,
<code><a href="read.ctd.ssda.html">read.ctd.ssda</a>()</code>,
<code><a href="read.ctd.woce.html">read.ctd.woce</a>()</code>,
<code><a href="read.ctd.woce.other.html">read.ctd.woce.other</a>()</code>,
<code><a href="setFlags-ctd-method.html">setFlags,ctd-method</a></code>,
<code><a href="subset-ctd-method.html">subset,ctd-method</a></code>,
<code><a href="summary-ctd-method.html">summary,ctd-method</a></code>,
<code><a href="woceNames2oceNames.html">woceNames2oceNames</a>()</code>,
<code><a href="woceUnit2oceUnit.html">woceUnit2oceUnit</a>()</code>,
<code><a href="write.ctd.html">write.ctd</a>()</code></p></div>
    </div>
    <div class="section level2">
    <h2 id="author">Author<a class="anchor" aria-label="anchor" href="#author"></a></h2>
    <p>Dan Kelley and Clark Richards</p>
    </div>

  </main><aside class="col-md-3"><nav id="toc" aria-label="Table of contents"><h2>On this page</h2>
    </nav></aside></div>


    <footer><div class="pkgdown-footer-left">
  <p>Developed by Dan Kelley, Clark Richards.</p>
</div>

<div class="pkgdown-footer-right">
  <p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.1.0.9000.</p>
</div>

    </footer></div>





  </body></html>

