File: krigeInterp.R

package info (click to toggle)
futilities 270.73-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 804 kB
  • ctags: 5
  • sloc: makefile: 13
file content (105 lines) | stat: -rw-r--r-- 3,640 bytes parent folder | download | duplicates (2)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received A copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA

# Copyrights (C)
# for this R-port:
#   1999 - 2008, Diethelm Wuertz, Rmetrics Foundation, GPL
#   Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
#   www.rmetrics.org
# for the code accessed (or partly included) from other R-ports:
#   see R's copyright and license files
# for the code accessed (or partly included) from contributed R-ports
# and other sources
#   see Rmetrics's copyright file


################################################################################
# FUNCTION:                 BIVARIATE GRIDDED INTERPOLATION:
#  krigeInterp              Kriges irregularly distributed data points
################################################################################


krigeInterp <-
    function(x, y = NULL, z = NULL, gridPoints = 21,
    xo = seq(min(x), max(x), length = gridPoints),
    yo = seq(min(y), max(y), length = gridPoints), extrap = FALSE,
    polDegree = 6)
{
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Kriges Irregularly Distributed Data Points

    # Arguments:
    #   x, y, z - either three numeric vectors of equal length or if
    #       y and z are NULL, a list with entries x, y, a, or named
    #       data.frame with x in the first, y in the second, and z in
    #       the third column.
    #   gridPoints - number of grid points in x and y direction.
    #   xo, yo, a sequence of data points spanning the grid
    #   extrap - a logical, if TRUE then the data points are extrapolated.
    #   polDegree - polynomial degree, an integer ranging between 1 and 6.

    # Value:
    #   A list with three elements, $x and $y which are vectors of length
    #   'gridPoints' and $z which is a matrix of size 'gridPoints^2'.

    # Example:
    #   x = runif(999)-0.5; y = runif(999)-0.5; z = cos(2*pi*(x^2+y^2))
    #   require(spatial)
    #   ans = krigeInterp(x, y, z, extrap = FALSE)
    #   persp(ans, theta = -50, phi = 30, col = "steelblue")

    # Note:
    #   Requires Recommended R Package "spatial"

    # FUNCTION:

    if (!require(spatial, quietly = TRUE))
        stop("\n -- Package spatial not available -- \n\n")

    # Arguments:
    if (is.list(x)) x = matrix(unlist(x), ncol = 3)
    if (is.data.frame(x)) x = as.matrix.data.frame(x)
    if (is.matrix(x)) {
        z = x[, 3]
        y = x[, 2]
        x = x[, 1]
    }

    # Interpolate:
    krige = surf.gls(np = polDegree, covmod = expcov,
        x = x, y = y, z = z, d = 0.5, alpha = 1)
    ans = prmat(krige,
        xl = min(xo), xu = max(xo), yl = min(yo), yu = max(yo),
        n = gridPoints-1)

    # Extrapolate ?
    # - this should be done more efficiently
    if (!extrap) {
        E = akimaInterp(x = x, y = y, z = z, gridPoints = gridPoints,
            extrap = extrap)
        ans$z[is.na(E$z)] = NA
    }
    class(ans) = "gridData"

    # Return Value:
    ans
}


################################################################################