File: makePredictionGrid.R

package info (click to toggle)
r-cran-fields 16.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,972 kB
  • sloc: fortran: 1,021; ansic: 288; sh: 35; makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,679 bytes parent folder | download | duplicates (3)
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
makePredictionGridList <- function(mKrigObject, nx, ny, np) {
  #
  # np used to add additional points so offGridWeights
  # has enough neighbors.
  #
  sDimension <- ncol(mKrigObject$x)
  xr <- range(mKrigObject$x[, 1])
  dx <- (xr[2] - xr[1]) / (nx - 2 * np)
  xg <- 0:(nx - 1) * dx +  (xr[1] - dx * (np - 1 / 2))
  predictionGridList <- list(x = xg)
  # add y grid if 2D
  if (sDimension == 2) {
    yr <- range(mKrigObject$x[, 2])
    dy <- (yr[2] - yr[1]) / (ny - 2 * np)
    yg <- 0:(ny - 1) * dy +  (yr[1] - dy * (np - 1 / 2))
    predictionGridList$y <- yg
  }
  return(predictionGridList)
}

checkPredictGrid <- function(predictionGridList) {
  testX <-
    sd(diff(predictionGridList$x)) / mean(diff(predictionGridList$x))
  if (testX > 1e-8) {
    stop("predictionGridList$x must be equally spaced")
  }
  dx <- predictionGridList$x[2] - predictionGridList$x[1]
  if (!is.null(predictionGridList$y)) {
    testY <-
      sd(diff(predictionGridList$x)) / mean(diff(predictionGridList$x))
    if (testY > 1e-8) {
      stop("predictionGridList$y must be equally spaced")
    }
    dy <- predictionGridList$y[2] - predictionGridList$y[1]
  }
}

makeSimulationGrid <- function(predictionGridList, gridRefinement) {
  dx <- predictionGridList$x[2] - predictionGridList$x[1]
  simulationGridList <- list(x = seq(
    min(predictionGridList$x),
    max(predictionGridList$x),
    dx / gridRefinement
  ))
  if (!is.null(predictionGridList$y)) {
    dy <- predictionGridList$y[2] - predictionGridList$y[1]
    simulationGridList$y <-
      seq(min(predictionGridList$y),
          max(predictionGridList$y),
          dy / gridRefinement)
  }
  return( simulationGridList )
}