<!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>Grid Data Using the Barnes Algorithm — interpBarnes • 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="Grid Data Using the Barnes Algorithm — interpBarnes"><meta name="description" content="The algorithm follows that described by Koch et al. (1983), except
that interpBarnes adds (1) the ability to
blank out the grid where data are
sparse, using the trim argument, and (2) the ability to
pre-grid, with the pregrid argument."><meta property="og:description" content="The algorithm follows that described by Koch et al. (1983), except
that interpBarnes adds (1) the ability to
blank out the grid where data are
sparse, using the trim argument, and (2) the ability to
pre-grid, with the pregrid argument."></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>Grid Data Using the Barnes Algorithm</h1>
      <small class="dont-index">Source: <a href="https://github.com/dankelley/oce/blob/HEAD/R/misc.R" class="external-link"><code>R/misc.R</code></a></small>
      <div class="d-none name"><code>interpBarnes.Rd</code></div>
    </div>

    <div class="ref-description section level2">
    <p>The algorithm follows that described by Koch et al. (1983), except
that <code>interpBarnes</code> adds (1) the ability to
blank out the grid where data are
sparse, using the <code>trim</code> argument, and (2) the ability to
pre-grid, with the <code>pregrid</code> argument.</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">interpBarnes</span><span class="op">(</span></span>
<span>  <span class="va">x</span>,</span>
<span>  <span class="va">y</span>,</span>
<span>  <span class="va">z</span>,</span>
<span>  <span class="va">w</span>,</span>
<span>  <span class="va">xg</span>,</span>
<span>  <span class="va">yg</span>,</span>
<span>  <span class="va">xgl</span>,</span>
<span>  <span class="va">ygl</span>,</span>
<span>  <span class="va">xr</span>,</span>
<span>  <span class="va">yr</span>,</span>
<span>  gamma <span class="op">=</span> <span class="fl">0.5</span>,</span>
<span>  iterations <span class="op">=</span> <span class="fl">2</span>,</span>
<span>  trim <span class="op">=</span> <span class="fl">0</span>,</span>
<span>  pregrid <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-y">x, y<a class="anchor" aria-label="anchor" href="#arg-x-y"></a></dt>
<dd><p>a vector of x and y locations.</p></dd>


<dt id="arg-z">z<a class="anchor" aria-label="anchor" href="#arg-z"></a></dt>
<dd><p>a vector of z values, one at each (x,y) location.</p></dd>


<dt id="arg-w">w<a class="anchor" aria-label="anchor" href="#arg-w"></a></dt>
<dd><p>a optional vector of weights at the (x,y) location.  If not
supplied, then a weight of 1 is used for each point, which means equal
weighting.  Higher weights give data points more influence. If <code>pregrid</code>
is <code>TRUE</code>, then any supplied value of <code>w</code> is ignored, and instead
each of the pregriddd points is given equal weight.</p></dd>


<dt id="arg-xg-yg">xg, yg<a class="anchor" aria-label="anchor" href="#arg-xg-yg"></a></dt>
<dd><p>optional vectors defining the x and y grids.  If not supplied,
these values are inferred from the data, using e.g. <code>pretty(x, n=50)</code>.</p></dd>


<dt id="arg-xgl-ygl">xgl, ygl<a class="anchor" aria-label="anchor" href="#arg-xgl-ygl"></a></dt>
<dd><p>optional lengths of the x and y grids, to be constructed with
<code><a href="https://rdrr.io/r/base/seq.html" class="external-link">seq()</a></code> spanning the data range.  These values <code>xgl</code> are only
examined if <code>xg</code> and <code>yg</code> are not supplied.</p></dd>


<dt id="arg-xr-yr">xr, yr<a class="anchor" aria-label="anchor" href="#arg-xr-yr"></a></dt>
<dd><p>optional values defining the x and y radii of the weighting ellipse.
If not supplied, these are calculated as the span of x
and y over the square root of the number of data.</p></dd>


<dt id="arg-gamma">gamma<a class="anchor" aria-label="anchor" href="#arg-gamma"></a></dt>
<dd><p>grid-focussing parameter.  At each successive iteration, <code>xr</code> and
<code>yr</code> are reduced by a factor of <code>sqrt(gamma)</code>.</p></dd>


<dt id="arg-iterations">iterations<a class="anchor" aria-label="anchor" href="#arg-iterations"></a></dt>
<dd><p>number of iterations.  Set this to 1 to perform just
one iteration, using the radii as described at <code>xr,yr</code> above.</p></dd>


<dt id="arg-trim">trim<a class="anchor" aria-label="anchor" href="#arg-trim"></a></dt>
<dd><p>a number between 0 and 1, indicating the quantile of data weight
to be used as a criterion for blanking out the gridded value (using
<code>NA</code>).  If 0, the whole <code>zg</code> grid is returned.  If &gt;0, any spots
on the grid where the data weight is less than the <code>trim</code>-th
<code><a href="https://rdrr.io/r/stats/quantile.html" class="external-link">quantile()</a></code> are set to <code>NA</code>.  See examples.</p></dd>


<dt id="arg-pregrid">pregrid<a class="anchor" aria-label="anchor" href="#arg-pregrid"></a></dt>
<dd><p>an indication of whether to pre-grid the data. If
<code>FALSE</code>, this is not done, i.e. conventional Barnes interpolation is
performed.  Otherwise, then the data are first averaged within grid cells
using <code><a href="binMean2D.html">binMean2D()</a></code>.  If <code>pregrid</code> is <code>TRUE</code> or
<code>4</code>, then this averaging is done within a grid that is 4 times finer
than the grid that will be used for the Barnes interpolation. Otherwise,
<code>pregrid</code> may be a single integer indicating the grid refinement (4
being the result if <code>TRUE</code> had been supplied), or a vector of two
integers, for the grid refinement in x and y. The purpose of using
<code>pregrid</code> is to speed processing on large datasets, and to remove
spatial bias (e.g. with a single station that is repeated frequently in an
otherwise seldom-sampled region).  A form of pregridding is done in the
World Ocean Atlas, for example.</p></dd>


<dt id="arg-debug">debug<a class="anchor" aria-label="anchor" href="#arg-debug"></a></dt>
<dd><p>a flag that turns on debugging.  Set to 0 for no debugging
information, to 1 for more, etc; the value is reduced by 1 for each
descendent function call.</p></dd>

</dl></div>
    <div class="section level2">
    <h2 id="value">Value<a class="anchor" aria-label="anchor" href="#value"></a></h2>
    <p>A list containing: <code>xg</code>, a vector holding the x-grid);
<code>yg</code>, a vector holding the y-grid; <code>zg</code>, a matrix holding the
gridded values; <code>wg</code>, a matrix holding the weights used in the
interpolation at its final iteration; and <code>zd</code>, a vector of the same
length as <code>x</code>, which holds the interpolated values at the data points.</p>
    </div>
    <div class="section level2">
    <h2 id="references">References<a class="anchor" aria-label="anchor" href="#references"></a></h2>
    <p>S. E.  Koch and M.  DesJardins and P. J. Kocin, 1983.  “An
interactive Barnes objective map analysis scheme for use with satellite and
conventional data,” <em>J.  Climate Appl.  Met.</em>, vol 22, p. 1487-1503.</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>See <code><a href="wind.html">wind()</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</p>
    </div>

    <div class="section level2">
    <h2 id="ref-examples">Examples<a class="anchor" aria-label="anchor" href="#ref-examples"></a></h2>
    <div class="sourceCode"><pre class="sourceCode r"><code><span class="r-in"><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="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># 1. contouring example, with wind-speed data from Koch et al. (1983)</span></span></span>
<span class="r-in"><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">wind</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="va">u</span> <span class="op">&lt;-</span> <span class="fu">interpBarnes</span><span class="op">(</span><span class="va">wind</span><span class="op">$</span><span class="va">x</span>, <span class="va">wind</span><span class="op">$</span><span class="va">y</span>, <span class="va">wind</span><span class="op">$</span><span class="va">z</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/graphics/contour.html" class="external-link">contour</a></span><span class="op">(</span><span class="va">u</span><span class="op">$</span><span class="va">xg</span>, <span class="va">u</span><span class="op">$</span><span class="va">yg</span>, <span class="va">u</span><span class="op">$</span><span class="va">zg</span>, labcex <span class="op">=</span> <span class="fl">1</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/graphics/text.html" class="external-link">text</a></span><span class="op">(</span><span class="va">wind</span><span class="op">$</span><span class="va">x</span>, <span class="va">wind</span><span class="op">$</span><span class="va">y</span>, <span class="va">wind</span><span class="op">$</span><span class="va">z</span>, cex <span class="op">=</span> <span class="fl">0.7</span>, col <span class="op">=</span> <span class="st">"blue"</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/graphics/title.html" class="external-link">title</a></span><span class="op">(</span><span class="st">"Numbers are the data"</span><span class="op">)</span></span></span>
<span class="r-plt img"><img src="interpBarnes-1.png" alt="" width="700" height="700"></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># 2. As 1, but blank out spots where data are sparse</span></span></span>
<span class="r-in"><span><span class="va">u</span> <span class="op">&lt;-</span> <span class="fu">interpBarnes</span><span class="op">(</span><span class="va">wind</span><span class="op">$</span><span class="va">x</span>, <span class="va">wind</span><span class="op">$</span><span class="va">y</span>, <span class="va">wind</span><span class="op">$</span><span class="va">z</span>, trim <span class="op">=</span> <span class="fl">0.1</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/graphics/contour.html" class="external-link">contour</a></span><span class="op">(</span><span class="va">u</span><span class="op">$</span><span class="va">xg</span>, <span class="va">u</span><span class="op">$</span><span class="va">yg</span>, <span class="va">u</span><span class="op">$</span><span class="va">zg</span>, level <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/seq.html" class="external-link">seq</a></span><span class="op">(</span><span class="fl">0</span>, <span class="fl">30</span>, <span class="fl">1</span><span class="op">)</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/graphics/points.html" class="external-link">points</a></span><span class="op">(</span><span class="va">wind</span><span class="op">$</span><span class="va">x</span>, <span class="va">wind</span><span class="op">$</span><span class="va">y</span>, cex <span class="op">=</span> <span class="fl">1.5</span>, pch <span class="op">=</span> <span class="fl">20</span>, col <span class="op">=</span> <span class="st">"blue"</span><span class="op">)</span></span></span>
<span class="r-plt img"><img src="interpBarnes-2.png" alt="" width="700" height="700"></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># 3. As 1, but interpolate back to points, and display the percent mismatch</span></span></span>
<span class="r-in"><span><span class="va">u</span> <span class="op">&lt;-</span> <span class="fu">interpBarnes</span><span class="op">(</span><span class="va">wind</span><span class="op">$</span><span class="va">x</span>, <span class="va">wind</span><span class="op">$</span><span class="va">y</span>, <span class="va">wind</span><span class="op">$</span><span class="va">z</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/graphics/contour.html" class="external-link">contour</a></span><span class="op">(</span><span class="va">u</span><span class="op">$</span><span class="va">xg</span>, <span class="va">u</span><span class="op">$</span><span class="va">yg</span>, <span class="va">u</span><span class="op">$</span><span class="va">zg</span>, labcex <span class="op">=</span> <span class="fl">1</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="va">mismatch</span> <span class="op">&lt;-</span> <span class="fl">100</span> <span class="op">*</span> <span class="op">(</span><span class="va">wind</span><span class="op">$</span><span class="va">z</span> <span class="op">-</span> <span class="va">u</span><span class="op">$</span><span class="va">zd</span><span class="op">)</span> <span class="op">/</span> <span class="va">wind</span><span class="op">$</span><span class="va">z</span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/graphics/text.html" class="external-link">text</a></span><span class="op">(</span><span class="va">wind</span><span class="op">$</span><span class="va">x</span>, <span class="va">wind</span><span class="op">$</span><span class="va">y</span>, <span class="fu"><a href="https://rdrr.io/r/base/Round.html" class="external-link">round</a></span><span class="op">(</span><span class="va">mismatch</span><span class="op">)</span>, col <span class="op">=</span> <span class="st">"blue"</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/graphics/title.html" class="external-link">title</a></span><span class="op">(</span><span class="st">"Numbers are percent mismatch between grid and data"</span><span class="op">)</span></span></span>
<span class="r-plt img"><img src="interpBarnes-3.png" alt="" width="700" height="700"></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># 4. As 3, but contour the mismatch</span></span></span>
<span class="r-in"><span><span class="va">mismatchGrid</span> <span class="op">&lt;-</span> <span class="fu">interpBarnes</span><span class="op">(</span><span class="va">wind</span><span class="op">$</span><span class="va">x</span>, <span class="va">wind</span><span class="op">$</span><span class="va">y</span>, <span class="va">mismatch</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/graphics/contour.html" class="external-link">contour</a></span><span class="op">(</span><span class="va">mismatchGrid</span><span class="op">$</span><span class="va">xg</span>, <span class="va">mismatchGrid</span><span class="op">$</span><span class="va">yg</span>, <span class="va">mismatchGrid</span><span class="op">$</span><span class="va">zg</span>, labcex <span class="op">=</span> <span class="fl">1</span><span class="op">)</span></span></span>
<span class="r-plt img"><img src="interpBarnes-4.png" alt="" width="700" height="700"></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># 5. One-dimensional example, smoothing a salinity profile</span></span></span>
<span class="r-in"><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">ctd</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="va">p</span> <span class="op">&lt;-</span> <span class="va">ctd</span><span class="op">[[</span><span class="st">"pressure"</span><span class="op">]</span><span class="op">]</span></span></span>
<span class="r-in"><span><span class="va">y</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html" class="external-link">rep</a></span><span class="op">(</span><span class="fl">1</span>, <span class="fu"><a href="https://rdrr.io/r/base/length.html" class="external-link">length</a></span><span class="op">(</span><span class="va">p</span><span class="op">)</span><span class="op">)</span> <span class="co"># fake y data, with arbitrary value</span></span></span>
<span class="r-in"><span><span class="va">S</span> <span class="op">&lt;-</span> <span class="va">ctd</span><span class="op">[[</span><span class="st">"salinity"</span><span class="op">]</span><span class="op">]</span></span></span>
<span class="r-in"><span><span class="va">pg</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/pretty.html" class="external-link">pretty</a></span><span class="op">(</span><span class="va">p</span>, n <span class="op">=</span> <span class="fl">100</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="va">g</span> <span class="op">&lt;-</span> <span class="fu">interpBarnes</span><span class="op">(</span><span class="va">p</span>, <span class="va">y</span>, <span class="va">S</span>, xg <span class="op">=</span> <span class="va">pg</span>, xr <span class="op">=</span> <span class="fl">1</span><span class="op">)</span></span></span>
<span class="r-in"><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">S</span>, <span class="va">p</span>, cex <span class="op">=</span> <span class="fl">0.5</span>, col <span class="op">=</span> <span class="st">"blue"</span>, ylim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rev.html" class="external-link">rev</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/range.html" class="external-link">range</a></span><span class="op">(</span><span class="va">p</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/graphics/lines.html" class="external-link">lines</a></span><span class="op">(</span><span class="va">g</span><span class="op">$</span><span class="va">zg</span>, <span class="va">g</span><span class="op">$</span><span class="va">xg</span>, col <span class="op">=</span> <span class="st">"red"</span><span class="op">)</span></span></span>
<span class="r-plt img"><img src="interpBarnes-5.png" alt="" width="700" height="700"></span>
</code></pre></div>
    </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>

