library(meteospain)
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
library(purrr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
library(units)
#> udunits database from /usr/share/xml/udunits/udunits2.xml

# provide keys for aemet and meteocat if not done already
# keyring::key_set('aemet')
# keyring::key_set('meteocat')

meteospain aims to return stations data in a compatible format between services. This means:

  • Names of meteorological and metadata variables are standardized between services and temporal resolutions.
  • Units of numerical variables are standardized between services.
  • Same output format for all services, an sf object.

This ease combining data from different services. Let’s see an example.

April 2020 daily data

We are gonna download daily data for April, 2020 for all services providing this information, and combine them in one object:

Don’t forget to store the keys for AEMET and MeteoCat if not done already (see code above)

aemet_daily <- get_meteo_from(
    'aemet', aemet_options(
      'daily', start_date = as.Date('2020-04-16'), end_date = as.Date('2020-04-30'),
      api_key = keyring::key_get('aemet')
    )
)
#>  © AEMET. Autorizado el uso de la información y su reproducción citando a
#>   AEMET como autora de la misma.
#> https://www.aemet.es/es/nota_legal

meteocat_daily <- get_meteo_from(
  'meteocat',
  meteocat_options('daily', start_date = as.Date('2020-04-16'), api_key = keyring::key_get('meteocat'))
)
#>  Data provided by meteo.cat © Servei Meteorològic de Catalunya
#> https://www.meteo.cat/wpweb/avis-legal/#info

meteogalicia_daily <- get_meteo_from(
  'meteogalicia',
  meteogalicia_options('daily', start_date = as.Date('2020-04-16'), end_date = as.Date('2020-04-30'))
)
#>  A información divulgada a través deste servidor ofrécese gratuitamente aos
#>   cidadáns para que poida ser
#> utilizada libremente por eles, co único compromiso de mencionar expresamente a
#> MeteoGalicia e á
#> Consellería de Medio Ambiente, Territorio e Vivenda da Xunta de Galicia como
#> fonte da mesma cada vez
#> que as utilice para os usos distintos do particular e privado.
#> https://www.meteogalicia.gal/web/informacion/notaIndex.action

ria_daily <- get_meteo_from(
  'ria',
  ria_options('daily', start_date = as.Date('2020-04-16'), end_date = as.Date('2020-04-30'))
)
#> Some stations didn't return data for some dates:
#> 11-3
#> 11-8
#> 11-9
#> 14-3
#> 18-4
#> 18-9
#> 21-1
#> 21-104
#> 21-106
#> 21-107
#> 21-12
#> 23-10
#> 23-13
#> 23-9
#> 29-3
#> 29-5
#> 4-3
#> 4-9
#> 41-1
#> 41-14
#> 41-4
#> 41-6
#>  Data provided by Red de Información Agroclimática de Andalucía (RIA)
#> https://www.juntadeandalucia.es/agriculturaypesca/ifapa/riaweb/web/

Now we have all daily data for April, lets join them. We are gonna use the purrr package to do it in one pipe.
Here we convert the data to tibble before the join, that way we are not joining by the spatial data, but by timestamp and the stations metadata. After the join we convert back to sf.

april_2020_spain <- list(
  dplyr::as_tibble(aemet_daily),
  dplyr::as_tibble(meteocat_daily),
  dplyr::as_tibble(meteogalicia_daily),
  dplyr::as_tibble(ria_daily)
) |>
  purrr::reduce(dplyr::full_join) |>
  sf::st_as_sf()
#> Joining with `by = join_by(timestamp, service, station_id, station_name,
#> station_province, altitude, mean_temperature, min_temperature, max_temperature,
#> mean_relative_humidity, min_relative_humidity, max_relative_humidity,
#> precipitation, mean_wind_speed, geometry)`
#> Joining with `by = join_by(timestamp, service, station_id, station_name,
#> station_province, altitude, mean_temperature, min_temperature, max_temperature,
#> mean_relative_humidity, min_relative_humidity, max_relative_humidity,
#> precipitation, mean_wind_speed, insolation, geometry, mean_wind_direction)`
#> Joining with `by = join_by(timestamp, service, station_id, station_name,
#> station_province, altitude, mean_temperature, min_temperature, max_temperature,
#> mean_relative_humidity, min_relative_humidity, max_relative_humidity,
#> precipitation, mean_wind_speed, geometry, mean_wind_direction)`

april_2020_spain
#> Simple feature collection with 22167 features and 19 fields (with 120 geometries empty)
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -18.115 ymin: 27.66528 xmax: 4.323889 ymax: 43.78611
#> Geodetic CRS:  WGS 84
#> # A tibble: 22,167 × 20
#>    timestamp           service station_id station_name station_province altitude
#>    <dttm>              <chr>   <chr>      <chr>        <chr>                 [m]
#>  1 2020-04-16 00:00:00 aemet   0009X      "ALFORJA"    TARRAGONA             406
#>  2 2020-04-16 00:00:00 aemet   0016A      "REUS AEROP… TARRAGONA              71
#>  3 2020-04-16 00:00:00 aemet   0016B      "REUS (CENT… TARRAGONA             118
#>  4 2020-04-16 00:00:00 aemet   0034X      "VALLS"      TARRAGONA             233
#>  5 2020-04-16 00:00:00 aemet   0042Y      "TARRAGONA " TARRAGONA              55
#>  6 2020-04-16 00:00:00 aemet   0061X      "PONTONS"    BARCELONA             632
#>  7 2020-04-16 00:00:00 aemet   0066X      "VILAFRANCA… BARCELONA             177
#>  8 2020-04-16 00:00:00 aemet   0073X      "SITGES"     BARCELONA              58
#>  9 2020-04-16 00:00:00 aemet   0076       "BARCELONA … BARCELONA               4
#> 10 2020-04-16 00:00:00 aemet   0092X      "BERGA"      BARCELONA             682
#> # ℹ 22,157 more rows
#> # ℹ 14 more variables: mean_temperature [°C], min_temperature [°C],
#> #   max_temperature [°C], mean_relative_humidity [%],
#> #   min_relative_humidity [%], max_relative_humidity [%],
#> #   precipitation [L/m^2], mean_wind_speed [m/s], insolation [h],
#> #   geometry <POINT [°]>, mean_wind_direction [°],
#> #   global_solar_radiation [MJ/m^2], solar_radiation [MJ/d/m^2], …

We can visualize the data, only one day.

By service

april_2020_spain |>
  dplyr::filter(lubridate::day(timestamp) == 25) |>
  units::drop_units() |>
  ggplot(aes(colour = service)) +
  geom_sf() +
  scale_colour_viridis_d()

By one variable

april_2020_spain |>
  dplyr::filter(lubridate::day(timestamp) == 25) |>
  units::drop_units() |>
  ggplot(aes(colour = mean_temperature)) +
  geom_sf() +
  scale_colour_viridis_c()