Description Usage Arguments Details Value Note Author(s) References See Also Examples
Automatic interpolation for hydrological ts, with optional plot (wrapper to some functions of the gstat and automap packages)
Originally it was thought as a way to make easier the computation of average precipitation over subcatchments (given as input in a shapefile map), based on values measured at several gauging stations, but nowadays it can also be used for interpolating any variable over a grid given by a raster map.
Available algorithms: inverse distance weighted (IDW), ordinary kriging (OK) and kriging with external drift (KED)
The (Block) Inverse Distance Weighted (IDW) interpolation is a wrapper to the idw function of the gstat package (so, it requires the gstat package).
The automatic kriging (OK or KED) is a wrapper to the autoKrige
function of the automap package (so, it requires the automap and gstat packages), which automatically selects the best variogram model from four different ones: spherical, exponential, gaussian and Matern with M. Stein's parameterization (for more details, see autoKrige
)
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 | hydrokrige(x.ts, x.gis, ...)
## Default S3 method:
hydrokrige(x.ts, x.gis, X= "x", Y= "y", sname, bname,
elevation, predictors, catchment.name = "all", type="cells",
formula, subcatchments, IDvar = NULL,
p4s=CRS(as.character(NA)), cell.size = 1000,
grid.type = "regular", nmin = 0, nmax = Inf, maxdist = Inf,
ColorRamp = "PCPAnomaly", plot = TRUE, col.nintv = 10,
col.at = "auto", main, stations.plot = FALSE, stations.offset,
arrow.plot = FALSE, arrow.offset, arrow.scale,
scalebar.plot = FALSE, sb.offset, sb.scale, verbose = TRUE,
allNA.action="error", ...)
## S3 method for class 'data.frame'
hydrokrige(x.ts, x.gis, X= "x", Y= "y", sname, bname,
elevation, predictors, catchment.name = "all", type = "block",
formula, subcatchments, IDvar= NULL,
p4s=CRS(as.character(NA)), cell.size = 1000,
grid.type = "regular", nmin = 0, nmax = Inf, maxdist = Inf,
ColorRamp = "PCPAnomaly", plot = FALSE, col.nintv = 10,
col.at = "auto", main, stations.plot = FALSE, stations.offset,
arrow.plot = FALSE, arrow.offset, arrow.scale,
scalebar.plot = FALSE, sb.offset, sb.scale,
verbose = TRUE, allNA.action="error",
dates=1, from, to, write2disk = TRUE,
out.fmt= "csv2",
fname = paste(ColorRamp, "by_Subcatch.csv", sep = ""), ...)
|
x.ts |
numeric or data.frame object, with measured values at several locations. -) The names of each value in When |
x.gis |
data.frame with the spatial information for each measurement point (e.g., gauging stations). -) The MINIMUM fields that have to be present in this file, and their corresponding column index are those described by: The ID of each measurement point, given in the field |
X |
character, field name in |
Y |
character, field name in |
sname |
character, field name in |
bname |
OPTIONAL. character, field name in |
elevation |
OPTIONAL. character, field name in |
predictors |
OPTIONAL. SpatialGridDataFrame-class object, with prediction/simulation locations (it is needed for KED). |
catchment.name |
name of the catchment that will be analysed. Possible values are: |
type |
Character, indicating the type of interpolation required by the user. |
formula |
OPTIONAL. Formula to be used in case of ordinary kriging or kriging with external drift. Requires the automap package. All the variables to be used within |
subcatchments |
OPTIONAL. Only required when |
IDvar |
See |
p4s |
OPTIONAL. a character with information about the projection of the GIS files, usually created by the |
cell.size |
OPTIONAL. Only required when |
grid.type |
OPTIONAL. Only required when |
nmin |
OPTIONAL. See |
nmax |
OPTIONAL. See |
maxdist |
OPTIONAL. See |
ColorRamp |
Function defining the colour ramp to be used for plotting the maps OR character representing the colours to be used in the plot. In the latter case, valid values are in: |
plot |
Logical, indicating if the interpolated values have to be plotted or not |
col.nintv |
integer, number of colours that have to be used for plotting the interpolated values |
col.at |
Specify at which interpolated values colours change.[MJ m-2 day-1] or [mm d-1]
|
main |
Character with the title to be used for the plot. |
stations.plot |
Logical, indicating if the gauging stations, defined by |
stations.offset |
2D list with the numeric coordinates in which the label with the amount of gauging stations have to be plotted. e.g., |
arrow.plot |
Logical, indicating if a North Arrow have to be plotted |
arrow.offset |
2D list with the numeric coordinates in which the north arrow has to be plotted. e.g., |
arrow.scale |
Scale (in the map units) to be used for plotting the north arrow, e.g., |
scalebar.plot |
Logical, indicating if a Scale Bar has to be plotted |
sb.offset |
2D list with the numeric coordinates in which the North Arrow has to be plotted. e.g., |
sb.scale |
Scale (in the map units) to be used for plotting the Scale Bar, e.g., |
verbose |
logical; if TRUE, progress messages are printed |
allNA.action |
Action to be executed when all the values in |
dates |
numeric, factor, or character object indicating how to obtain the ID (usually dates) that will be used to identify the interpolation carried out for each row of |
from |
Character indicating the starting date for the values stored in all the files that will be read. |
to |
Character indicating the ending date for the values stored in all the files that will be read. |
write2disk |
Logical. Indicates if we want to write the output into a CSV file. Default value is TRUE |
out.fmt |
OPTIONAL, only needed when |
fname |
OPTIONAL. Character with the filename of the output file. Only needed when |
... |
further arguments passed to or from other methods. In particular, these further arguments are passed to the function idw (gstat package) OR |
The type of interpolation (IDW, OK, KED) is obtained from the argument formula
:
-) When formula
is missing, an IDW interpolation, by calling the idw
function in the gstat package, with formula
= value~1
.
-) When formula
= value~1
, an OK interpolation, by calling the autoKrige
function, with formula
= value~1
.
-) When formula
= value~pred1 + pred2 + ...
, a KED interpolation, by calling the autoKrige
function, with the formula
specified by the user.
When type
is block or both, a block interpolation is carried out for each subcatchment defined by subcatchments
, so far, computing the average value over all the cells belonging to each subcatchment.
The automatic kriging is carried out by using a variogram generated automatically with the autofitVariogram
function of the automap package.
Cells |
When |
Block |
When |
list(Cells, Block) |
When |
IMPORTANT: It is you responsibility to check the validity of the fitted variogram !!.
Mauricio Zambrano-Bigiarini, mzb.devel@gmail
N.A.C. Cressie, 1993, Statistics for Spatial Data, Wiley.
Applied Spatial Data Analysis with R. Series: Use R. Bivand, Roger S., Pebesma, Edzer J., Gomez-Rubio, Virgilio. 2008. ISBN: 978-0-387-78170-9
Pebesma, E.J., 2004. Multivariable geostatistics in S: the gstat package. Computers & Geosciences, 30: 683-691
http://rspatial.r-forge.r-project.org/
krige, autoKrige, readShapePoly, spsample
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | ############
## Loading the monthly time series of precipitation within the Ebro River basin.
data(EbroPPtsMonthly)
## Loading the gis data
data(EbroPPgis)
## Loading the shapefile with the subcatchments
data(EbroCatchmentsCHE)
## Projection for the Subcatchments file
# European Datum 50, Zone 30N
require(sp)
p4s <- CRS("+proj=utm +zone=30 +ellps=intl +units=m +no_defs")
## Selecting the first day of 'EbroPPtsMonthly' for all the stations.
# The first column of 'EbroPPtsMonthly' has the dates
x.ts <- as.numeric(EbroPPtsMonthly[1, 2:ncol(EbroPPtsMonthly)])
## Setting the name of the gauging stations
names(x.ts) <- colnames(EbroPPtsMonthly[1,2:ncol(EbroPPtsMonthly)])
##################################################
## 1) IDW interpolation and plot
## Probably you will need to resize your window
## Not run:
x.idw <- hydrokrige(x.ts= x.ts, x.gis=EbroPPgis,
X="EAST_ED50", Y="NORTH_ED50", sname="ID", bname="CHE_BASIN_NAME",
type= "both",
subcatchments= EbroCatchmentsCHE,
cell.size= 3000,
ColorRamp= "Precipitation",
main= "IDW Precipitation on the Ebro")
## End(Not run)
##################################################
## 2) Ordinary Kriging interpolation and plot, in catchments defined by a shapefile
## Probably you will need to resize your window
## Not run:
# Computing OK, over of 3000x3000m, sampled withinthe subcatchments defined by 'subcatchments'
x.ok <- hydrokrige(x.ts= x.ts, x.gis=EbroPPgis,
X="EAST_ED50", Y="NORTH_ED50", sname="ID", bname="CHE_BASIN_NAME",
type= "both", formula=value~1,
subcatchments= EbroCatchmentsCHE,
p4s= p4s,
cell.size= 3000,
ColorRamp= "Precipitation",
main= "OK Precipitation on the Ebro", arrow.plot= TRUE,
arrow.offset= c(900000,4750000), arrow.scale= 20000,
scalebar.plot= TRUE,
sb.offset= c(400000,4480000), sb.scale= 100000)
# Getting the interpolated values in each polygon
# ('var1.pred' field in the output data.frame object)
x.ok.block <- slot(x.ok[["Block"]], "data")
# Getting the interpolated values in each cell (SpatialPixelsDataFrame object)
# (the pedicted values in each cell are stored in the 'var1.pred' field of the
# 'data' slot)
x.ok.cells <- x.ok[["Cells"]]
# Plotting the interpolated values in each cell
spplot(x.ok.cells, "var1.pred")
## End(Not run)
##################################################
## 3) Ordinary Kriging interpolation and plot, in an area defined by a raster map.
## The raster map may be any \link[sp]{SpatialGridDataFrame-class} object, read with
## the \code{\link[rgdal]{readGDAL}} function of the \pkg{rgdal} package or similar.
## Probably you will need to resize your window
#Loading the DEM
data(EbroDEM1000m)
#Giving a meaningful name to the predictor
EbroDEM1000m$ELEVATION <- EbroDEM1000m$band1
# Saving memory
EbroDEM1000m$band1 <- NULL
# Computing OK, over the spatial grid defined by the DEM
## Not run:
x.ok <- hydrokrige(x.ts= x.ts, x.gis=EbroPPgis,
X="EAST_ED50", Y="NORTH_ED50", sname="ID",
formula=value~1,
p4s= p4s,
predictors=EbroDEM1000m,
ColorRamp= "Precipitation",
main= "OK Precipitation on the Ebro",
arrow.plot= TRUE,
arrow.offset= c(900000,4750000), arrow.scale= 20000,
scalebar.plot= TRUE,
sb.offset= c(400000,4480000), sb.scale= 100000)
## End(Not run)
##################################################
## 4) Kriging with External Drift interpolation and plot
## Probably you will need to resize your window
#Loading the DEM
data(EbroDEM1000m)
#Giving a meaningful name to the predictor
EbroDEM1000m$ELEVATION <- EbroDEM1000m$band1
# Saving memory
EbroDEM1000m$band1 <- NULL
# Forcing the projection of the DEM to be the same of the 'subcatchments' argument
# (just because I know in advance they are the same)
proj4string(EbroDEM1000m) <- p4s
# Computing KED
## Not run:
x.ked <- hydrokrige(x.ts= x.ts, x.gis=EbroPPgis,
X="EAST_ED50", Y="NORTH_ED50", sname="ID",
bname="CHE_BASIN_NAME", elevation="ELEVATION",
type= "cells",
formula=value~ELEVATION,
subcatchments= EbroCatchmentsCHE,
predictors=EbroDEM1000m,
cell.size= 3000,
ColorRamp= "Precipitation",
main= "KED Precipitation on the Ebro",
arrow.plot= TRUE,
arrow.offset= c(900000,4750000), arrow.scale= 20000,
scalebar.plot= TRUE,
sb.offset= c(400000,4480000), sb.scale= 100000)
## End(Not run)
##################################################
## 5) Block IDW interpolation and plot of 'EbroPPtsMonthly' for 3 months
## Not run:
x.idw <- hydrokrige(x.ts= EbroPPtsMonthly, x.gis=EbroPPgis,
X="EAST_ED50", Y="NORTH_ED50", sname="ID",
bname="CHE_BASIN_NAME",
type= "cells", #'both'
subcatchments= EbroCatchmentsCHE,
cell.size= 3000,
ColorRamp= "Precipitation",
arrow.plot= TRUE,
arrow.offset= c(900000,4750000), arrow.scale= 20000,
scalebar.plot= TRUE,
sb.offset= c(400000,4480000), sb.scale= 100000,
dates=1,
from="1942-01-01", to="1942-03-01")
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.