README.Rmd 4.29 KB
Newer Older
Raphael Saldanha's avatar
Raphael Saldanha committed
---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
```

Raphael Saldanha's avatar
Raphael Saldanha committed
# brclimr
Raphael Saldanha's avatar
Raphael Saldanha committed

<!-- badges: start -->
Raphael Saldanha's avatar
Raphael Saldanha committed

[![CRAN status](https://www.r-pkg.org/badges/version/brclimr)](https://CRAN.R-project.org/package=brclimr) [![](https://cranlogs.r-pkg.org/badges/brclimr)](https://cran.r-project.org/package=brclimr)
[![R-CMD-check](https://github.com/rfsaldanha/brclimr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/rfsaldanha/brclimr/actions/workflows/R-CMD-check.yaml)
Raphael Saldanha's avatar
Raphael Saldanha committed
<!-- badges: end -->

Raphael Saldanha's avatar
Raphael Saldanha committed
This R package retrieves zonal statistics from weather indicators that were calculated for each municipality in Brazil using data from the BR-DWGD and TerraClimate projects.
Raphael Saldanha's avatar
Raphael Saldanha committed
Zonal statistics such as mean, maximum, minimum, standard deviation, and sum were computed by taking into account the data cells that intersect the boundaries of each municipality and stored in Parquet files. This procedure was carried out for all Brazilian municipalities, and for all available dates, for every indicator available in the weather products (BR-DWGD and TerraClimate projects). This package queries on-line the already calculated statistics on the Parquet files and returns easy-to-use data.frames.
Raphael Saldanha's avatar
Raphael Saldanha committed

Raphael Saldanha's avatar
Raphael Saldanha committed
Details about the used methodology to calculate the zonal statistics are available at *Articles \> Methodology*.
Raphael Saldanha's avatar
Raphael Saldanha committed

Raphael Saldanha's avatar
Raphael Saldanha committed
## Installation

Raphael Saldanha's avatar
Raphael Saldanha committed
### Stable version

```{r eval=FALSE}
install.packages("brclimr")
```

Raphael Saldanha's avatar
Raphael Saldanha committed
```{r eval=FALSE}
remotes::install_github(repo = "rfsaldanha/brclimr")
```

Raphael Saldanha's avatar
Raphael Saldanha committed
### Development version

```{r eval=FALSE}
Raphael Saldanha's avatar
Raphael Saldanha committed
remotes::install_github(repo = "rfsaldanha/brclimr", ref = "duckdb")
Raphael Saldanha's avatar
Raphael Saldanha committed
Note: the stable version adopted a universal solution for querying Parquet files that works on all operating systems but it is slower. A development version is available for Linux and MacOS with a faster solution. More details on this [issue](https://github.com/rfsaldanha/brclimr/issues/1).
Raphael Saldanha's avatar
Raphael Saldanha committed
## Example
Raphael Saldanha's avatar
Raphael Saldanha committed
To fetch data for a specific product, indicator and statistic, use the `fetch_data` function. For example: Rio de Janeiro, RJ (IBGE code number 3304557), data product "brdwgd", average relative humidity, from 2010-10-15 to 2010-10-20.
Raphael Saldanha's avatar
Raphael Saldanha committed

```{r}
library(brclimr)

Raphael Saldanha's avatar
Raphael Saldanha committed
fetch_data(
Raphael Saldanha's avatar
Raphael Saldanha committed
    code_muni = 3304557,
    product = "brdwgd",
    indicator = "rh",
    statistics = "mean",
    date_start = as.Date("2010-10-15"),
    date_end = as.Date("2010-10-20")
  )
```

Raphael Saldanha's avatar
Raphael Saldanha committed
If you need to query several municipalities, indicators and zonal statistics, we recommend to download and locally query the parquet files using the `arrow` package. A list of URLs of the parquet files created for this project is available at *Articles \> Parquet files*.
Raphael Saldanha's avatar
Raphael Saldanha committed
A list with the indicators and zonal statistics available by product can be retrieved with the function `product_info`.
Raphael Saldanha's avatar
Raphael Saldanha committed

```{r eval=FALSE}
product_info("brdwgd")
product_info("terraclimate")
```

Raphael Saldanha's avatar
Raphael Saldanha committed
## Another example

```{r}
Raphael Saldanha's avatar
Raphael Saldanha committed
tmax <- fetch_data(
Raphael Saldanha's avatar
Raphael Saldanha committed
  code_muni = 3303401,
  product = "brdwgd",
  indicator = "tmax",
  statistics = "mean",
  date_start = as.Date("2010-01-01"),
  date_end = as.Date("2012-01-01")
)

Raphael Saldanha's avatar
Raphael Saldanha committed
tmin <- fetch_data(
Raphael Saldanha's avatar
Raphael Saldanha committed
  code_muni = 3303401,
  product = "brdwgd",
  indicator = "tmin",
  statistics = "mean",
  date_start = as.Date("2010-01-01"),
  date_end = as.Date("2012-01-01")
)

Raphael Saldanha's avatar
Raphael Saldanha committed
pr <- fetch_data(
Raphael Saldanha's avatar
Raphael Saldanha committed
  code_muni = 3303401,
  product = "brdwgd",
  indicator = "pr",
  statistics = "sum",
  date_start = as.Date("2010-01-01"),
  date_end = as.Date("2012-01-01")
)

tmax$name <- "Tmax_avg"
tmin$name <- "Tmin_avg"
```

```{r}
library(ggplot2)

ggplot(data = rbind(tmax, tmin), aes(x = date, y = value, color = name)) +
  geom_line() +
  scale_x_date(date_breaks = "2 months", date_labels =  "%m/%y") +
  ylim(0, NA) +
  labs(
    title = "Nova Friburgo, RJ",
    x = "Date", 
    y = "Temperature (average)",
    color = ""
  ) +
  theme_bw() +
  theme(legend.position = "bottom", legend.direction = "horizontal")
```

```{r}
ggplot(data = pr, aes(x = date, y = value)) +
  geom_line(color = "blue") +
  scale_x_date(date_breaks = "2 months", date_labels =  "%m/%y") +
  ylim(0, NA) +
  labs(
    title = "Nova Friburgo, RJ",
    x = "Date", 
    y = "Precipitation (sum)",
    color = ""
  ) +
  theme_bw() +
  theme(legend.position = "bottom", legend.direction = "horizontal")
```