subsetGrid: Select an arbitrary subset from a grid or multigrid along one...

View source: R/subsetGrid.R

subsetGridR Documentation

Select an arbitrary subset from a grid or multigrid along one or more of its dimensions

Description

Create a new grid/multigrid that is a subset of the input grid along the selected dimensions

Usage

subsetGrid(
  grid,
  var = NULL,
  cluster = NULL,
  runtime = NULL,
  members = NULL,
  years = NULL,
  season = NULL,
  lonLim = NULL,
  latLim = NULL,
  outside = FALSE,
  station.id = NULL,
  drop = TRUE
)

Arguments

grid

The input grid or station data to be subset. This is either a grid (or station data), as returned e.g. by loadeR::loadGridData (or loadeR::loadStationData), a multigrid, as returned by makeMultiGrid, or other types of multimember grids (possibly multimember grids) as returned e.g. by loadeR.ECOMS::loadECOMS.

var

Character vector indicating the variables(s) to be extracted. (Used for multigrid subsetting). See details.

cluster

An integer indicating the cluster to be subset. For Lamb WTs subsetting, see lambWT.

runtime

An integer vector indicating the position of the runtimes to be subset.

members

An integer vector indicating the position of the members to be subset.

years

The years to be selected. Note that this can be either a continuous or discontinuous series of years, the latter option often used in a cross-validation framework. See details for year-crossing seasons. Default to NULL (no subsetting is performed on the time dimension).

season

An integer vector indicating the months to be subset.

lonLim

Vector of length = 2, with minimum and maximum longitude coordinates, in decimal degrees, of the bounding box defining the subset. For single-point subsets, a numeric value with the longitude coordinate. If NULL (default), no subsetting is performed on the longitude dimension

latLim

Same as lonLim argument, but for latitude.

outside

if TRUE subset coordinates outside the grid extent are allowed. Default is FALSE.

station.id

Station ID (check $Metadata$station_id).

drop

Logical (default is TRUE). Drop or keep dimensions of length 1.

Details

The attribute subset will be added to the different slots corresponding to the subset dimensions, taking the value of the subroutine called in each case (e.g.: attribute subset will have the value subsetSpatial in the xyCoords slot after spatial subsetting...).

Time slicing

In case of year-crossing seasons (e.g. boreal winter (DJF), season = c(12,1,2)), the season is assigned to the years of January and February (i.e., winter of year 2000 corresponds to Dec 1999, Jan 2000 and Feb 2000). Thus, the years argument must be introduced accordingly (See e.g. getYearsAsINDEX function for details). Hence, when subsetting along season, some data might be lost when using year-crossing seasons. For example, assume a dataset encompassing a full-year season (i.e., season=1:12) for the period 1981-2010 (i.e., years=1981:2010). When performing a subset on boreal winter (DJF, season = c(12,1,2)), the first available winter will be “winter 1982”, encompassing Dec 1981 and Jan and Feb 1982. Thus, all data corresponding to Jan and Feb 1981 are discarded from the subset (i.e., only complete “winters” will be returned). Similarly, December 2010 will be lost (because it belongs to winter 2011, beyond the temporal extent of the dataset), and the last data provided will correspond to winter 2009. To override this default behaviour and retaining all January, February and December records strictly within the period 1981-2010, the non-standard season=c(1,2,12) can be specified (although this is rarely needed).

Spatial slicing

Spatial subset definition is done via the lonLim and latLim arguments, in the same way as for instance the loadGridData function, from package loadeR, with the exception that several checks are undertaken to ensure that the subset is actually within the current extent of the input grid. It is also possible to make single-point selections from a grid, just by specifying a single coordinate instead of a range as the argument value. For instance lonLim = c(-10,10) and latLim = c(35,45) indicate a rectangular window centered in the Iberian Peninsula), and single grid-cell values (for instance lonLim = -3.21 and latLim = 41.087 for retrieving the data in the closest grid point to the point coordinate -3.21E, 41.087N. In the last two cases, the function operates by finding the nearest (euclidean distance) grid-points to the coordinates introduced.

Extracting grids from multigrids

One or several variables from a multigrid object can be extracted. Note that argument var is insensitive to the order of the variables, i.e.: variables will be always returned in the same order they are in the original multigrid.

Value

A new grid object that is a logical subset of the input grid along the specified dimensions.

Author(s)

J. Bedia, M. Iturbide, J. A. Fernandez

See Also

Other subsetting: getTemporalIntersection(), intersectGrid(), intersectGrid.spatial(), intersectGrid.time(), limitArea(), subsetCluster(), subsetDimension(), subsetMembers(), subsetRuntime(), subsetSeason(), subsetSpatial(), subsetStation(), subsetVar(), subsetYears()

Examples

## Not run: 
require(climate4R.datasets)
# Example 1 - Spatial / member subset
data("CFS_Iberia_tas")
# Selection of a smaller domain over the Iberian Peninsula and members 3 and 7
sub <- subsetGrid(CFS_Iberia_tas,
                  members = c(3,7),
                  lonLim = c(-10,-5),
                  latLim = c(36,43))
require(visualizeR)                  
spatialPlot(climatology(sub), tol = 0.005, contour = TRUE,
                backdrop.theme = "coastline", rev.colors = TRUE)
                
## Example 2 - Subsetting a multimember multigrid by variables
# Multimember multigrid creation
data("CFS_Iberia_pr", "CFS_Iberia_hus850")
mm.mf <- makeMultiGrid(CFS_Iberia_tas, CFS_Iberia_pr, CFS_Iberia_hus850)
# Extracting just minimum temperature
sub1 <- subsetGrid(mm.mf, var = "tas", members = 1:4)
spatialPlot(climatology(sub1, by.member = TRUE), backdrop.theme = "coastline",
     rev.colors = TRUE)
# Extracting precipitation and maximum temperature
# (Note that the grid variables are NOT re-ordered)
sub2 <- subsetGrid(mm.mf, var = c("pr", "tas"))
getShape(sub2)
getVarNames(sub2)

## Example 3 - Subsetting stations by their codes
data("VALUE_Iberia_pr")
getStationID(VALUE_Iberia_pr)
central_ib <- subsetGrid(VALUE_Iberia_pr, station.id = c("000229", "000231", "000232"))
getStationID(central_iberia)
central_iberia$Metadata
VALUE_Iberia_pr$Metadata

## End(Not run)

SantanderMetGroup/transformeR documentation built on Nov. 25, 2024, 1:25 p.m.