Análise de dados de temperatura SSP

Author

Ana Festucci, Renan Alves Tavares

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
[1] 2

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).

######### PACOTES #############

library(dplyr)

Anexando pacote: 'dplyr'
Os seguintes objetos são mascarados por 'package:stats':

    filter, lag
Os seguintes objetos são mascarados por 'package:base':

    intersect, setdiff, setequal, union
library(lubridate)

Anexando pacote: 'lubridate'
Os seguintes objetos são mascarados por 'package:base':

    date, intersect, setdiff, union
library(waveslim)
Carregando pacotes exigidos: multitaper

waveslim: Wavelet Method for 1/2/3D Signals (version = 1.8.5)

Anexando pacote: 'waveslim'
O seguinte objeto é mascarado por 'package:lubridate':

    pm
library(ggplot2)
library(forecast)
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
######### TRATAMENTO #######
SSP_NASA <- read.csv("SSP_NASA.csv", skip = 10, sep = ",")
SSP_NASA <- SSP_NASA[-c(16284:16296), ]

# Renomear colunas
names(SSP_NASA)[names(SSP_NASA) == "T2M_MAX"] <- "TEMP.MAX.C"
names(SSP_NASA)[names(SSP_NASA) == "T2M_MIN"] <- "TEMP.MIN.C"
names(SSP_NASA)[names(SSP_NASA) == "YEAR"] <- "ANO"
names(SSP_NASA)[names(SSP_NASA) == "DOY"] <- "DIA"

# Converter colunas para numéricas
SSP_NASA$ANO <- as.numeric(SSP_NASA$ANO)
SSP_NASA$DIA <- as.numeric(SSP_NASA$DIA)
SSP_NASA$TEMP.MAX.C <- as.numeric(SSP_NASA$TEMP.MAX.C)
SSP_NASA$TEMP.MIN.C <- as.numeric(SSP_NASA$TEMP.MIN.C)

# Configura a linguagem para português (br)
Sys.setlocale("LC_TIME", "pt_BR.UTF-8")
[1] "pt_BR.UTF-8"
# Criar colunas Data e Mês
SSP_NASA <- SSP_NASA %>%
  mutate(
    Data = as.Date(DIA - 1, origin = paste0(ANO, "-01-01"))#,
    #MES = month(Data, label = TRUE, abbr = TRUE)
  )


######### ANOMALIAS #######

climatologia <- SSP_NASA %>%
  mutate(
    MES = month(Data),
    DIA_MES = day(Data)
  ) %>%
  group_by(MES, DIA_MES) %>%
  summarise(
    MEDIA_MAX = mean(TEMP.MAX.C, na.rm = TRUE),
    MEDIA_MIN = mean(TEMP.MIN.C, na.rm = TRUE),
    .groups = 'drop'
  )

climatologia <- climatologia %>%
  mutate(
    # Cria a nova variável concatenando as duas
    CHAVE_DATA = paste0(DIA_MES, "-", MES) # Ex: "25-12"
  ) 

SSP_NASA <- SSP_NASA %>%
  mutate(
    MES = month(Data),
    DIA_MES = day(Data),
    # Cria a nova variável concatenando as duas
    CHAVE_DATA = paste0(DIA_MES, "-", MES) # Ex: "25-12"
  )

# Agora o left_join vai funcionar perfeitamente
SSP_NASA <- SSP_NASA %>%
  left_join(climatologia, by = "CHAVE_DATA") %>%
  mutate(
    ANOMALIA_MAX = TEMP.MAX.C - MEDIA_MAX,
    ANOMALIA_MIN = TEMP.MIN.C - MEDIA_MIN
  )

######### ONDAS ######### 

percentil_90 <- SSP_NASA %>%
  summarise(
    P90_MAX = quantile(TEMP.MAX.C, 0.90, na.rm = TRUE),
    P90_MIN = quantile(TEMP.MIN.C, 0.90, na.rm = TRUE)
  )

SSP_NASA <- SSP_NASA %>%
  mutate(
    onda_calor = ifelse(
      TEMP.MAX.C > percentil_90$P90_MAX & 
        TEMP.MIN.C > percentil_90$P90_MIN, 1, 0)
  )

rle_onda           <- rle(SSP_NASA$onda_calor)

SSP_NASA$grupo     <- rep(seq_along(rle_onda$lengths), rle_onda$lengths)

SSP_NASA$duracao   <- rep(rle_onda$lengths, rle_onda$lengths)

ondas_de_calor     <- SSP_NASA %>% filter(onda_calor == 1 & duracao >= 3)

num_ondas_total    <- ondas_de_calor %>% distinct(grupo) %>% nrow()

ondas_inicio       <- ondas_de_calor %>% group_by(grupo) %>% slice(1) %>% ungroup()

cat("🔥 Número total de ondas de calor padrão detectadas:", num_ondas_total, "\n")
🔥 Número total de ondas de calor padrão detectadas: 38