knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
) 

Load the packages

library(tidyverse) # For much of the data preprocessing
library(grid)
library(rainfallR) # The access and processing of the rainfall data
library(here) # Project paths
library(gganimate) # For a little animation
library(iffitoR) # The landslide data
library(sf) # Handling of the spatial data
library(glue) # Concattening strings
library(purrr) # Mapping functions
library(crayon)  # Coloured consolde output 
library(raster) # Raster data handling

Get the slope units

# which os to automatically set the paths
os = Sys.info()["sysname"]

if(os == "Linux"){
  path_ncdf = "/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 if(os == "Windows"){
  path_ncdf = "\\\\projectdata.eurac.edu/projects/Proslide/PREC_GRIDS_updated/"
  su_path = "\\\\projectdata.eurac.edu/projects/Proslide/Envir_data/SlopeUnits/su_opt_16_TAA/su_16_TAA.shp"
}else{
  stop(call. = F, "what the hell are you working on...")
}

What have we got

# get vector of South Tyrol
st = iffitoR::get_shape_southtyrol() 

ggplot(st) +
  geom_sf() +
  theme_minimal()
su_orig = st_read(su_path)
# and verify directly if they are in the same crs
st_crs(su_orig) == st_crs(st)
st_reproj = st_transform(st,st_crs(su_orig))
st_crs(st_reproj) == st_crs(su_orig)

Clip the slopunits to South Tyrol

su = st_crop(su_orig, st_reproj)
# are the two bounding boxes equal?
st_bbox(su) == st_bbox(st_reproj) # for some reason the xmax is not the same
dim(su_orig) # how many were there
dim(su)

Get the rainfall for the slope Units

landsld = landsld %>% filter(str_detect(second_level, "translational|rotational")) %>% slice(1:50)

su_points_in_poly = rainfallR::slide_dates_in_polygon(poly = su_path
                                                      ,landsld = landsld)
su_points_in_poly %>% 
  dplyr::select(c(poly_id, date, month.int, second_level, slides_per_poly_date)) %>%  
  arrange(desc(slides_per_poly_date)) %>% 
  head()

Extract the rainfall data for each su with landslides for each day

What about the rainfall Events that happened in slope units and did not cause landslides?

Use the results from the new slide_dates_in_polygon-function

# filte out the slides before the start of the rainfall data
su_points_in_poly = su_points_in_poly[su_points_in_poly$date > as.Date("1980-01-01"), ]

# get the antecedent rainfall for all the dates in all the slope units with landslides
slopeunits_rainfall = rainfallR::get_rainfall_for_polygons(su_points_in_poly, nc_var = "precipitation", days_back = 14, data_path = path_ncdf)

What have we got back?

Within the get_rainfall_for_polygons-function:

# verfiy that each slope unit has 6 entries for all the six days before the slide
slopeunits_rainfall %>% 
  group_by(poly_id, PIFF_ID) %>% 
  summarise(n = n()) %>% 
  ungroup() %>% 
  count(n)

look at the antecedent rainfall

g =slopeunits_rainfall %>% 
  group_by(poly_id, PIFF_ID) %>% 
  mutate(unique_id = dplyr::cur_group_id()) %>% 
  ggplot() +
  geom_path(mapping = aes(x = days_before_event,
                  y = cumsum,
                  group = unique_id,
                  color = second_level)) +
  scale_x_reverse() +
  scale_color_manual(values = c("translational slide"="#D4f0f0", "rotational slide" = "#ffc8a2")) +
  theme_light() +
  labs(
    title = "Five day antecedent rainfall", 
   y = "cumulative sum [mm]",
   x = "Days before landslide"

  ) +
theme(
 panel.background = element_rect(fill="black"),
 plot.background = element_rect(fill="black"),
 panel.grid = element_blank(),
 plot.margin = margin(20, 20, 20, 20),
 axis.text = element_text(colour = "white")
)

g = ggplotGrob(g)
bg = g$grobs[[1]] 
round_bg = roundrectGrob(x=bg$x, y=bg$y, width=bg$width, height=bg$height,
                          r=unit(0.1, "snpc"),
                          just=bg$just, name=bg$name, gp=bg$gp, vp=bg$vp)
g$grobs[[1]] <- round_bg
plot(g)
table(slopeunits_rainfall$days_before_event)


RobinKohrs/rainfallR documentation built on Oct. 3, 2021, 1:42 a.m.