---
title: "Extending climaemet"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Extending climaemet}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
resource_files:
   - vignettes/example-gif.gif
---

<!-- extending-climaemet.Rmd is generated from extending-climaemet.Rmd.orig. Please edit that file -->

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  dev = "ragg_png",
  dpi = 90,
  fig.path = "./",
  tidy = "styler",
  out.width = "100%"
)
```

**climaemet** provides several functions for accessing a selection of endpoints
of the [AEMET API tool](https://opendata.aemet.es/dist/index.html?). However,
this package does not cover in full all the capabilities of the API.

For that reason, we provide the `get_data_aemet()` function, that allows to
access any API endpoint freely. The drawback is that the user would need to
handle the results by him/herself.

```{r setup}
library(climaemet)
```

## Example: Normalized text

Some API endpoints, as `predicciones-normalizadas-texto`, provides the results
as plain text on natural language. These results are not parsed by
**climaemet**, but can be retrieved as this:

```{r text}

# endpoint, today forecast

today <- "/api/prediccion/nacional/hoy"

# Metadata
knitr::kable(get_metadata_aemet(today))

# Data
pred_today <- get_data_aemet(today)

```

```{r, results='asis', comment=''}

# Produce a result

clean <- gsub("\r", "\n", pred_today, fixed = TRUE)
clean <- gsub("\n\n\n", "\n", clean, fixed = TRUE)

cat(paste("---\n\n", clean, "\n---"))

```

## Example: Maps

AEMET also provides map data, usually on `image/gif` format. One way to get this
kind of data is as follows:

```{r maps, fig.cap="Example: Surface analysis map provided by AEMET"}

# Endpoint of a map
a_map <- "/api/mapasygraficos/analisis"

# Metadata
knitr::kable(get_metadata_aemet(a_map))

the_map <- get_data_aemet(a_map)


# Write as gif and include it
giffile <- "example-gif.gif"
writeBin(the_map, giffile)

# Display on the vignette
knitr::include_graphics(giffile)

```
