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

## ----setup packages, warning = FALSE, message = FALSE-------------------------
library(climateBR)
library(dplyr)
library(sf)
library(ggplot2)
library(patchwork)


## ----import data, warning = FALSE, message = FALSE----------------------------
data("floods_rs")
data("mun_stations_distance")

tmp <- tempfile(fileext = ".rda")
on.exit(unlink(tmp), add = TRUE)

download.file(
  "https://github.com/kaiorb52/dados_municipais/raw/main/mun_24.rda",
  destfile = tmp,
  mode = "wb"
)

load(tmp)


## ----setup, warning = FALSE, message = FALSE----------------------------------
rain_sf <- floods_rs |>
  st_as_sf(coords = c("long", "lat"), crs = 4326) |>
  st_transform(crs = 29193)

mun_centroid <- mun_24 |>
  select(code_muni, geom) |>
  mutate(
    ponto = st_point_on_surface(geom)
  ) |>
  st_drop_geometry()

mun_grid_sf <- mun_centroid |>
  st_as_sf() |>
  st_transform(crs = 29193)


## ----warning = FALSE, message = FALSE-----------------------------------------
krig_result <- kriging_inmet(
  stations_df = rain_sf,
  mun_geo     = mun_grid_sf
)

## ----warning = FALSE, message = FALSE-----------------------------------------
mun_pred <- mun_centroid |>
  select(code_muni) |>
  bind_cols(
    krig_result |>
      st_drop_geometry() |>
      select(
        precip_krig     = var1.pred,
        precip_krig_var = var1.var
      )
  )

## ----warning = FALSE, message = FALSE-----------------------------------------
p1 <- mun_24 |>
  left_join(mun_pred, by = "code_muni") |>
  ggplot() +
  geom_sf(aes(fill = precip_krig), color = NA) +
  labs(title = "Ordinary Kriging") +
  scale_fill_distiller(
    palette = "RdYlGn",
    direction = 1,
    na.value = "grey80"
  ) +
  theme_void() +
  theme(
    legend.position = c(0.9, 0.1)
  )

## ----warning = FALSE, message = FALSE-----------------------------------------
mun_floods <- floods_rs |>
  left_join(
    mun_stations_distance |>
      filter(ano == 2024, i == 1),
    by = c("id_who" = "codigo_wmo")
  )

p2 <- mun_24 |>
  left_join(
    mun_floods |>
      select(code_muni, total_rainfall),
    by = "code_muni"
  ) |>
  ggplot() +
  geom_sf(aes(fill = total_rainfall), color = NA) +
  labs(title = "Nearest Station") +
  scale_fill_distiller(
    palette = "RdYlGn",
    direction = 1,
    na.value = "grey80"
  ) +
  theme_void() +
  theme(
    legend.position = c(0.9, 0.1)
  )

## ----fig.width=8, fig.height=6, echo=FALSE, warning = FALSE, message = FALSE----

p1 + p2


## ----fig.width=9, fig.height=5, echo=FALSE, warning = FALSE, message = FALSE----
(
mun_pred |> 
  ggplot(aes(x = precip_krig)) +
  geom_histogram(color = "black", fill = "steelblue4", alpha = 0.85) +
  coord_cartesian(xlim = c(0, 650), ylim = c(0, 3500)) +
  labs(title = "Ordinary kriging") +
  theme_minimal() +
mun_floods |> 
  #filter(total_rainfall >= 1) |> 
  ggplot(aes(x = total_rainfall)) +
  geom_histogram(color = "black", fill = "steelblue4", alpha = 0.85) +
  coord_cartesian(xlim = c(0, 650), ylim = c(0, 3500)) +
  labs(title = "Nearest Station") +
  theme_minimal()
)


