Compatibility between services

library(meteospain)
library(sf)
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)

# 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:

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-01'), 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-01'), api_key = keyring::key_get('meteocat'))
)

meteogalicia_daily <- get_meteo_from(
  'meteogalicia',
  meteogalicia_options('daily', start_date = as.Date('2020-04-01'), 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-01'), 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
#> 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,
#> 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,
#> precipitation, mean_wind_speed, insolation, geometry, mean_relative_humidity,
#> min_relative_humidity, max_relative_humidity, mean_wind_direction)`
#> Joining with `by = join_by(timestamp, service, station_id, station_name,
#> station_province, altitude, mean_temperature, min_temperature, max_temperature,
#> precipitation, mean_wind_speed, geometry, mean_relative_humidity,
#> min_relative_humidity, max_relative_humidity, mean_wind_direction)`

april_2020_spain
#> Simple feature collection with 20412 features and 19 fields (with 150 geometries empty)
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -17.91528 ymin: 27.73583 xmax: 4.215556 ymax: 43.78611
#> Geodetic CRS:  WGS 84
#> # A tibble: 20,412 × 20
#>    timestamp           service station_id station_name station_province altitude
#>    <dttm>              <chr>   <chr>      <chr>        <chr>                 [m]
#>  1 2020-04-01 00:00:00 aemet   0016A      REUS AEROPU… TARRAGONA              71
#>  2 2020-04-01 00:00:00 aemet   0076       BARCELONA A… BARCELONA               4
#>  3 2020-04-01 00:00:00 aemet   0149X      MANRESA      BARCELONA             291
#>  4 2020-04-01 00:00:00 aemet   0200E      BARCELONA, … BARCELONA             408
#>  5 2020-04-01 00:00:00 aemet   0201D      BARCELONA    BARCELONA               6
#>  6 2020-04-01 00:00:00 aemet   0252D      ARENYS DE M… BARCELONA              74
#>  7 2020-04-01 00:00:00 aemet   0255B      SANTA SUSAN… BARCELONA              40
#>  8 2020-04-01 00:00:00 aemet   0324A      RIPOLL       GIRONA                675
#>  9 2020-04-01 00:00:00 aemet   0367       GIRONA AERO… GIRONA                143
#> 10 2020-04-01 00:00:00 aemet   0372C      PORQUERES    GIRONA                157
#> # ℹ 20,402 more rows
#> # ℹ 14 more variables: mean_temperature [°C], min_temperature [°C],
#> #   max_temperature [°C], precipitation [L/m^2], mean_wind_speed [m/s],
#> #   insolation [h], geometry <POINT [°]>, mean_relative_humidity [%],
#> #   min_relative_humidity [%], max_relative_humidity [%],
#> #   mean_wind_direction [°], global_solar_radiation [MJ/m^2],
#> #   solar_radiation [MJ/d/m^2], under_plastic <lgl>

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()