knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
This is a small example that shows how to extract rainfall data for vector-polygons.
First we need to load the packages and set some paths
library(rainfallR) library(stars) library(ncdf4) library(tidyverse) library(scales) library(raster) library(sf)
Now we can set some paths
We will use the gridded rainfall data and a manually created polygon
os = Sys.info()["sysname"] if(os == "Linux"){ path_data = "/mnt/CEPH_PROJECTS/Proslide/PREC_GRIDS_updated/" su_path = "/mnt/CEPH_PROJECTS/Proslide/Envir_data/SlopeUnits/su_opt_16_TAA/su_16_TAA.shp" }else{ # the path to the data path_data = "\\\\projectdata.eurac.edu/projects/Proslide/PREC_GRIDS_updated/" # path to the slop units su_path = "\\\\projectdata.eurac.edu/projects/Proslide/Envir_data/SlopeUnits/su_opt_16_TAA/su_16_TAA.shp" }
rainfallR::get_rainfall
-function# set some parameters for the rainfallR day = as.Date("2016-08-16") # get the day as integer day_int = format(day, "%d") # we only want one raster to get the bounding box in order to draw s shape days_back = 1 # create the path to the NetCDF path_ncdf = rainfallR::get_nc_paths(path_data, day, days_back) %>% .[[1]] # read in one raster with stars r = read_ncdf(path_ncdf) # extract one raster r = r[,,,16]
set.seed(2) # read in the slope units and select a random one su = read_sf(su_path) # select a random one su = su %>% slice(sample(1:nrow(su), 1))
We now have:
the path to the NetCdfs
a date we want to extract the rainfall data
We can now query the rainfall data as a table with the rainfallR
-package
rainfall = rainfallR::ex_rainfall( data_path = path_data, spatial.obj = su, date = day, fun = c("mean", "max"), days_back = 3 ) # what have we got? print(rainfall)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.