region_mean: Interpolate vals into the resolution of 'gridInfo' and...

Description Usage Arguments Value Note Examples

View source: R/region_mean.R

Description

Interpolate vals into the resolution of gridInfo and calculate regional average

Usage

 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
region_mean(
  vals,
  grid,
  gridInfo = NULL,
  range = NULL,
  is_average = TRUE,
  mutliple_regions = TRUE,
  years = NULL,
  show = FALSE,
  ...
)

region_mean_raster2(
  x,
  grid,
  gridInfo = NULL,
  range,
  is_average = TRUE,
  mutliple_regions = TRUE,
  years = NULL,
  I_sel = NULL,
  p1 = 1,
  p2 = 30,
  nt = 1
)

Arguments

vals

matrix [ngrid, ntime]

grid

list(lon, lat), used to interpolate.

gridInfo

list(gridclip, I_sel), returned by gridPoly_OverlapFraction(). gridclip includes w, area-based weight of each pixel. w is not required with sum of 1. matrixStats::colWeightedMeans will deal with it automatically. w is only used to interpolated grids; I_sel is selected pixels of spatialGridDataFrame.

range

Interpolation range.

is_average

Boolean. If true, area-weighted average will be returned

mutliple_regions

not used. This parameter is only for China dataset. If mutliple_regions = TRUE, China will be divided into 3 groups, i.e. ALL, Eastern China (EC, lon >= 105), western China (WC).

years

years of vals. If not specified, it will be the colnames of vals.

show

whether plot result?

...

other parameters to plot.raster2, e.g. sp.layout

x

read from HW RDS file

I_sel

index of selected years

p1

Integer, starting index of reference period, if is.null(p1 or p2), reference period will be ignored. If I_sel provided, default reference period is 30 years.

p2

Integer, ending index of reference period .

nt

Integer, nt-year no overlapping average.

Value

Note

Currently, this function is only for China. Regional average is weighted by area, not w.

Examples

 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
## Not run: 
source('test/main_pkgs.R')
    
# polyline
read_polyline <- function(file, lwd = 0.5, ...){
    suppressWarnings(shp <- maptools::readShapeLines(file))
    proj4string(shp) <- sp2::prj84
    list("sp.lines", shp, first = FALSE, lwd = lwd, ...) # china poly    
}

# sp_arc_world    <- read_polyline("G:/ArcGIS/continent.shp") # sp_arc_world
# sp_arc_CH <- read_polyline(system.file("extdata/shp", "bou1_4l_sml.shp", 
#     package = "CMIP5tools"))
gridInfo  <- read_rds(file_grid_5.0deg)

init <- function(){
    x <- read_rds("../DATA/examples/historical_bcc-csm1-1_HW_index.RDS")
    HW <- filter_HWindex(x$HW, 2001, 2012)
    historical_bcc <- c(x[1:2], list(data = HW)) %>% structure(class = c("HWindex", "raster2"))
}

# ------------------------------------------------------------------------------

data("historical_bcc")
data("gridInfo_5deg")

range = c(70, 140, 15, 55)
gridInfo <- gridInfo_5deg
x  <- historical_bcc
HW <- x$data
vals <- HW$PR$`0.99`

at <- seq(0, 7)

d_mean <- region_mean(vals, x$grid, gridInfo, range, years = HW$year, show = TRUE, 
                      # sp.layout = sp_arc_CH, 
                      at = at)
ggplot(d_mean, aes(year, value, color = type_region)) + 
    geom_line()# + facet_wrap(~type_region)

# d <- melt(d_mean, "year")
# ggplot(d_mean[variable != "ALL"], aes(year, value, color = variable)) + geom_line()

# region_mean_cgrid
d1_cgrid <- region_mean_raster2(x$HW, x$grid, gridInfo, range, is_average = TRUE, p2 = NULL)
d2_cgrid <- region_mean_raster2(x$HW, x$grid, gridInfo, range, is_average = FALSE, p2 = NULL)


## End(Not run)

kongdd/CMIP5tools documentation built on Dec. 17, 2020, 11:03 a.m.