knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(rainfallR)
library(tidyverse)
library(sf)
library(glue)
library(future)
path = "~/Desktop/dopt_fin.Rda"
load(path) # will create a variable called dopt

dopt[["eidee"]] = 1:nrow(dopt)

# find year with most date inforamtion
dopt %>%
  filter(!is.na(date)) %>%
  mutate(
    y = lubridate::year(date)
  ) %>%
  group_by(y) %>%
  summarise(n = n()) %>%
  arrange(desc(n))

dopt=st_as_sf(dopt, coords=c("x", "y"), crs=32632)
start = Sys.time()
res = get_ant_rainfall(
  dopt,
  id = "eidee",
  date_landslide = "date",
  vars = c(1,2,3),
  parallel = F,
  dates_for_vars = seq(as.Date("2008-01-01"), as.Date("2008-12-31"), by="day")
)
end = Sys.time()
took1 = (end - start) 
start = Sys.time()
res = get_ant_rainfall(
  dopt,
  id = "eidee",
  date_landslide = "date",
  vars = c(1,2,3),
  parallel = T,
  dates_for_vars = seq(as.Date("2008-01-01"), as.Date("2008-12-31"), by="day")
)
end = Sys.time()
took2 = (end - start)
data.frame("no_parrallel" = took1,
           "parrallel" = took2)
dopt_no_sf = dopt %>% st_drop_geometry()
dat = merge(res, dopt_no_sf) %>% 
  select(date_var, eidee, matches("p_.*"), PIFF_ID, date) 

# filters those dates where the date of the start of the variable extraction period
# is equal to the date of the landslide
dat %>% 
  mutate(
    trigger = case_when(date_var == date ~ TRUE,
                        TRUE ~ FALSE)
  ) %>% 
  pivot_longer(cols = matches("p_.*"), names_to="var", values_to="vals") -> df

df %>% 
  group_by(var, trigger) %>% 
  summarise(
    n = n()
  ) -> df_ann

ggplot(df) +
  geom_boxplot(aes(x = vals, y = var, fill = trigger)) +
  geom_label(
    data = df_ann,
    aes(
      x =  max(df$vals - 10),
      y = var,
      # group = var,
      fill = trigger,
      label = paste0("n: ", n)
    ),
    position = ggstance::position_dodgev(height=.75)
  )


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