---
title: "Get started with climaemet 1.0.0"
date: 2023-08-29
author: <a href="https://orcid.org/0000-0001-8457-4658">Diego Hernangómez <img alt="ORCID logo" src="https://info.orcid.org/wp-content/uploads/2019/11/orcid_24x24.png" width="16" height="16" /></a>
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Get started with climaemet 1.0.0}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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




Since the last release, this package has been integrated into
[rOpenSpain](https://ropenspain.es/), a community of **R** enthusiasts whose
ultimate goal is to create high-quality **R** packages for data mining public
Spanish open sources.

From version **1.0.0** onward, we have introduced some improvements and
(breaking) changes on the package, in order to provide a smoother interaction
with the AEMET API service.

## API Key

### Get your API Key

To be able to download data from AEMET you will need a free API key which you
can get at <https://opendata.aemet.es/centrodedescargas/obtencionAPIKey>

Once that you have your API Key, you can use any of the following methods:

#### a. Set API Key with `aemet_api_key()`

This is the recommended option. Just type:


```r
aemet_api_key("YOUR_API_KEY", install = TRUE)
```

Using `install = TRUE` ensures that the API key is stored on your local computer
and it would be reloaded every time you load the library. From now on you can
forget about API keys!

#### b. Use an environment variable

This is a temporary alternative. You can set your API key as an environment
variable


```r
Sys.setenv(AEMET_API_KEY = "YOUR_API_KEY")
```

Note that this is only valid for the current session. You would need to re-run
this command each time you restart your session.

#### c. Modify your `.Renviron` file

This stores your API key permanently on your machine. You can start editing your
`.Renviron` running this command:

``` r
usethis::edit_r_environ()
```

Now you can add the following line to you `.Renviron` file:

    AEMET_API_KEY = YOUR_API_KEY

## New features

### `tidyverse` format

From **v1.0.0** onward, **climaemet** provides its results in [tibble
format](https://tibble.tidyverse.org/). Also, the functions try to guess the
correct format of the fields (i.e. something as a Date/Hour now is an hour,
numbers are parsed as double, etc.).

See how a tibble is displayed:


```r
# See a tibble in action

aemet_last_obs("9434")
#> # A tibble: 23 × 25
#>    idema   lon fint                 prec   alt  vmax    vv    dv   lat  dmax ubi             pres    hr stdvv    ts
#>    <chr> <dbl> <dttm>              <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>          <dbl> <dbl> <dbl> <dbl>
#>  1 9434  -1.00 2023-08-28 15:00:00     0   249  19.9  11.8   317  41.7   313 ZARAGOZA  AER…  987.    42   1.6  31.3
#>  2 9434  -1.00 2023-08-28 16:00:00     0   249  16.4   9.6   317  41.7   320 ZARAGOZA  AER…  987.    40   1.7  28.7
#>  3 9434  -1.00 2023-08-28 17:00:00     0   249  15.2   8.8   323  41.7   330 ZARAGOZA  AER…  987.    39   1.8  27.5
#>  4 9434  -1.00 2023-08-28 18:00:00     0   249  13     7.3   323  41.7   330 ZARAGOZA  AER…  986.    41   1.4  25.5
#>  5 9434  -1.00 2023-08-28 19:00:00     0   249  11.5   5.8   326  41.7   303 ZARAGOZA  AER…  987.    47   1.5  23.6
#>  6 9434  -1.00 2023-08-28 20:00:00     0   249  10.6   5.8   322  41.7   330 ZARAGOZA  AER…  987.    52   1.2  22.2
#>  7 9434  -1.00 2023-08-28 21:00:00     0   249  12.8   8     323  41.7   325 ZARAGOZA  AER…  987.    57   1.5  21.5
#>  8 9434  -1.00 2023-08-28 22:00:00     0   249  11.7   7     325  41.7   335 ZARAGOZA  AER…  987.    59   1.3  20.9
#>  9 9434  -1.00 2023-08-28 23:00:00     0   249  11     6.7   298  41.7   310 ZARAGOZA  AER…  987.    60   1    20.5
#> 10 9434  -1.00 2023-08-29 00:00:00     0   249  14.2   8.3   290  41.7   303 ZARAGOZA  AER…  987.    60   1.3  20.2
#> # ℹ 13 more rows
#> # ℹ 10 more variables: pres_nmar <dbl>, tamin <dbl>, ta <dbl>, tamax <dbl>, tpr <dbl>, stddv <dbl>, inso <dbl>,
#> #   tss5cm <dbl>, pacutp <dbl>, tss20cm <dbl>
```

Note that when possible, data representing dates and numbers are converted to
the right format.

### Spatial objects: `sf`

Another major change in **v1.0.0** is the ability of return information on
spatial `sf` format, using `return_sf = TRUE`. The coordinate reference system
(CRS) used is **EPSG 4326**, that correspond to the **World Geodetic System
(WGS)** and return coordinates in latitude/longitude (unprojected coordinates):


```r
# You would need to install `sf` if not installed yet
# run install.packages("sf") for installation

# You would need to install `sf` if not installed yet
# run install.packages("sf") for installation

library(ggplot2)
library(dplyr)

all_stations <- aemet_daily_clim(
  start = "2021-01-08", end = "2021-01-08",
  return_sf = TRUE
)

ggplot(all_stations) +
  geom_sf(aes(colour = tmed), shape = 19, size = 2, alpha = 0.5) +
  labs(
    title = "Average temperature in Spain",
    subtitle = "8 Jan 2021",
    color = "Max temp.\n(celsius)",
    caption = "Source: AEMET"
  ) +
  scale_colour_gradientn(
    colours = hcl.colors(10, "RdBu", rev = TRUE, alpha = 0.5),
    guide = "legend"
  ) +
  guides(colour = guide_legend(n.breaks = 10)) +
  theme_bw() +
  theme(
    panel.border = element_blank(),
    plot.title = element_text(face = "bold"),
    plot.subtitle = element_text(face = "italic")
  )
```

![Example: Temperature in
Spain](../man/figures/README-spatial-1.png){width="100%"}

## Further enhancements

Other enhancements included on the **v1.0.0**:

-   All the functions are now vectorized.
-   New function `get_metadata_aemet()`.
-   New function `ggclimat_walter_lieth()`. This function is now the default for
    `climatogram_*` functions (experimental). Old behavior can be reproduced
    with options `ggplot2 = FALSE`.
-   Plot functions gains new parameters (`verbose` and `...`). Now it is
    possible to pass colors to the plotting functions.
-   New example datasets: `climaemet::climaemet_9434_climatogram`,
    `climaemet::climaemet_9434_temp`, `climaemet::climaemet_9434_wind`.
