knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(ggplot2)
library(purrr)
library(sf)
library(rainfallR)

Make the data

# get the shape of ST
st = iffitoR::get_shape_southtyrol() %>% st_transform(crs=32632)

# Get some random points
bb = st_bbox(st)
xs = sample(seq(bb[[1]], bb[[3]], length.out=100), 50)
ys = sample(seq(bb[[2]], bb[[4]], length.out=100), 50)

pts = map2_df(xs, ys, ~data.frame(x = .x, y=.y)) %>% 
  st_as_sf(coords = c("x", "y"), crs=32632)

pts_st = pts[st, ]

# make up some dates
pts_st[["date"]] = sample(seq(as.Date("2000-01-01"), as.Date("2020-01-01"), by="day"), nrow(pts_st))

Now that we have some data, lets query the rainfall

r_data = get_rainfall_point_data(
  pts_st,
  days_back = 10,
  daily_thresh = 1.1,
  n_dry = 2,
  ncores = 2,
  save = T,
  base_path = "~/rainfalldata"
)
# as we have all day information in the synthetic dataset...
pts_st[["date_info"]] = "day"

So lets try again to get the rainfall data

r_data = get_rainfall_point_data(
  pts_st,
  days_back = 10,
  daily_thresh = 1.1,
  n_dry = 2,
  ncores = 2,
  save = T,
  base_path = "~/rainfalldata"
)
pts_st[["id"]] = 1:nrow(pts_st)

r_data = get_rainfall_point_data(
  pts_st,
  days_back = 10,
  daily_thresh = 1.1,
  n_dry = 2,
  ncores = 2,
  save = T,
  base_path = "~/rainfalldata",
  id_landslide = "id"
)
pts_st[["lll"]] = 1:nrow(pts_st)

r_data = get_rainfall_point_data(
  pts_st,
  days_back = 10,
  daily_thresh = 1.1,
  n_dry = 2,
  ncores = 2,
  save = T,
  base_path = "~/rainfalldata",
  id_landslide = "lll"
)

What, no error...

print(head(r_data))

So we can plot the precpitation for each of these synthetic slide locations

ggplot(r_data) +
  geom_path(
    aes(
      x = days_before_event,
      y = precip,
      group = id
    )
  ) +
  scale_x_reverse() +
  theme_void() +
  labs(
    x = "Days before landslide",
    y = "Daily cumulated precipitation [ mm ]"
  ) +
  theme(
    axis.title = element_text(color="#37323e", size=14, family="Times", face="italic"),
    axis.title.y = element_text(color="#37323e", size=14, family="Times", face="italic", angle=90),
    axis.line.y = element_line(color="#37323e", size=1),
    axis.line.x = element_line(color="#37323e", size=1),
    axis.ticks = element_line(color="black"),
    axis.text = element_text(color="#37323e", family="Times", face="italic", margin = margin(rep(5, 4))),
    plot.margin = unit(rep(2,4), "cm"))


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