---
title: "Climatology data with climatrends"
package: climatrends
author:
- name: Kauê de Sousa
  affiliation: Department of Agricultural Sciences, Inland Norway University, Hamar, Norway </br> Digital Inclusion, Bioversity International, Montpellier, France 
output: html_document
vignette: >
  %\VignetteIndexEntry{Climatology}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown_notangle}
bibliography: ["climatrends.bib"]
csl: citation_style.csl
---

We randomly selected 100 points in hexagonal within the coordinates 7$^\circ$ and 17$^\circ$ W, and 59 $^\circ$ and 63 $^\circ$ N, to illustrate one approch to get long term climate data in `climatrends`. We compute the temperature indices from 2000-01-01 to 2019-12-31 using the function `temperature()` with the method for objects of class 'sf'. The temperature data is fetched from the NASA Langley Research Center POWER Project funded through the NASA Earth Science Directorate Applied Science Program (<https://power.larc.nasa.gov/>), using the R package `nasapower` [@Sparks2018]. A new implementation of NASAPOWER API disabled calls for more than 366 days. As a turn around we split the years and run the call using a loop. This process can take a while. 


```r
library("climatrends")
library("sf")
library("nasapower")
library("ggplot2")
library("patchwork")
library("tidyverse")

# create a polygon within the coordinates 7, 17, 59, 63
e <- matrix(c(7, 59, 17, 59, 17, 63,
              7, 63, 7, 59),
            nrow = 5, ncol = 2, byrow = TRUE)

e <- st_polygon(list(e))
# sample 100 points in the hexagonal type
p <- st_sample(e, 100, type = 'hexagonal')
p <- st_as_sf(p, crs = 4326)

dayone <- paste0(2000:2019, "-01-01")
dayone <- as.Date(dayone, origin = "1970-01-01")

temp <- data.frame()

for (d in seq_along(dayone)) {
  temp_d <- temperature(p, 
                        day.one = dayone[d], 
                        last.day = dayone[d] + 365,
                        timeseries = TRUE,
                        intervals = 365)
  
  temp <- rbind(temp, temp_d)
  
}

```

We then select the indices CSDI (cold spell duration of night temperature), WSDI (warm spell duration of day temperature), and their associated indices the T10p (the 10th percentile of night temperature) and T90p (the 90th percentile of day temperature), in Figure 1. Plots are generated with `ggplot2` and `patchwork`.

```r
i <- c("CSDI","WSDI")

temp %>% 
  filter(index %in% i) %>% 
  group_by(index) %>% 
  mutate(ab = mean(value)) %>% 
  ggplot() +
  geom_line(aes(x = date, y = value, group = id)) + 
  geom_hline(aes(yintercept = ab), colour = "red", lwd = 0.7) +
  geom_smooth(aes(x = date, y = value), method = "loess", se = FALSE) +
  facet_wrap(. ~ index, scales = "free") +
  labs(x = "", y = "Index (days)") +
  theme(axis.text.y = element_text(size = 12, angle = 0, hjust = 1, 
                                   vjust = 0.5, face = "plain", colour = "black"),
        axis.text.x = element_text(size = 12, angle = 0, hjust = 0.5, 
                                   vjust = 1, face = "plain", colour = "black"),
        axis.title = element_text(size = 12, face = "plain"),
        panel.border = element_rect(colour = "black", fill=NA, size=0.5),
        plot.background = element_blank(),
        panel.background = element_blank(),
        strip.text.x = element_text(size = 12, colour = "black"),
        strip.text.y = element_text(size = 12, colour = "black"),
        strip.background = element_rect(colour = "black", fill="#FFFFFF")) ->
  gg1

gg1


i <- c("T10p","T90p")


temp %>% 
  filter(index %in% i) %>% 
  group_by(index) %>% 
  mutate(ab = mean(value)) %>% 
  ggplot() +
  geom_line(aes(x = date, y = value, group = id)) + 
  geom_hline(aes(yintercept = ab), colour = "red", lwd = 0.7) +
  geom_smooth(aes(x = date, y = value), method = "loess", se = FALSE) +
  facet_wrap(. ~ index, scales = "free") +
  labs(x = "Year", 
       y = expression(paste('Index (',~degree,'C)',sep=''))) +
  theme(axis.text.y = element_text(size = 12, angle = 0, hjust = 1, 
                                   vjust = 0.5, face = "plain", colour = "black"),
        axis.text.x = element_text(size = 12, angle = 0, hjust = 0.5, 
                                   vjust = 1, face = "plain", colour = "black"),
        axis.title = element_text(size = 12, face = "bold"),
        panel.border = element_rect(colour = "black", fill=NA, size=0.5),
        plot.background = element_blank(),
        panel.background = element_blank(),
        strip.text.x = element_text(size = 12, colour = "black"),
        strip.text.y = element_text(size = 12, colour = "black"),
        strip.background = element_rect(colour = "black", fill="#FFFFFF")) ->
  gg2

gg2


pl <- gg1 / gg2

```

The trends show a decrease in the cold spell duration (number of consecutive cold nights bellow the 10th percentile) and warm spell duration (number of consecutive warm days above the 90th percentile). However, the values of the percentiles show an increase over the time series. The T10p index shows a decrease around the year of 2010, but again rises up to the a value around the -10 $^\circ$C, meaning that the cold nights are becoming a bit warmer over the time. The T90p index also shows an increase in the temperature across the sampled area, with the average 90th percentile rising from ~ 16 $^\circ$C to ~ 18 $^\circ$C over the time series. 

# References
