API limits and loops

library(meteospain)
library(sf)

The following APIs impose a period limit in the data retrieved, not allowing querying more than the predetermined period in each API.

AEMET API

AEMET API limit the daily data download to 15 days, and the monthly and yearly data to 36 months:

# aemet api has a limit for 15 days in daily:
get_meteo_from(
  'aemet',
  aemet_options(
    api_key = keyring::key_get('aemet'),
    resolution = 'daily',
    start_date = as.Date('1990-01-01'),
    end_date = as.Date('1990-12-31')
  )
)
#> Error in `api_function()`:
#> ✖ 404
#> ℹ AEMET API returned no data: El rango de fechas no puede ser superior a 15
#>   dias

# and monthly and yearly data to 36 months
get_meteo_from(
  'aemet',
  aemet_options(
    api_key = keyring::key_get('aemet'),
    resolution = 'yearly',
    start_date = as.Date('2005-01-01'),
    end_date = as.Date('2020-12-31'),
    stations = "0149X"
  )
)
#> Error in `api_function()`:
#> ✖ 404
#> ℹ AEMET API returned no data: El rango de las fechas no puede ser superior a 36
#>   mesess

This means that with one call to get_meteo_from to the AEMET service, one can only download 15 days of data, (or 36 months in monthly and yearly data).
If the period needed is bigger than that, one option is performing all the calls necessary and join the results:

res_1990_jan_1 <- get_meteo_from(
  'aemet',
  aemet_options(
    api_key = keyring::key_get('aemet'),
    resolution = 'daily',
    start_date = as.Date('1990-01-01'),
    end_date = as.Date('1990-01-15')
  )
)
#> ℹ © 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

res_1990_jan_2 <- get_meteo_from(
  'aemet',
  aemet_options(
    api_key = keyring::key_get('aemet'),
    resolution = 'daily',
    start_date = as.Date('1990-01-16'),
    end_date = as.Date('1990-01-31')
  )
)
#> ℹ © 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

res_1990_jan <- rbind(res_1990_jan_1, res_1990_jan_2)
res_1990_jan
#> Simple feature collection with 5066 features and 15 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -18.115 ymin: 27.72528 xmax: 4.215556 ymax: 43.65111
#> Geodetic CRS:  WGS 84
#> # A tibble: 5,066 × 16
#>    timestamp           service station_id station_name station_province altitude
#>  * <dttm>              <chr>   <chr>      <chr>        <chr>                 [m]
#>  1 1990-01-01 00:00:00 aemet   0002I      "VANDELLÒS … TARRAGONA              32
#>  2 1990-01-01 00:00:00 aemet   0016A      "REUS AEROP… TARRAGONA              71
#>  3 1990-01-01 00:00:00 aemet   0016B      "REUS (CENT… TARRAGONA             118
#>  4 1990-01-01 00:00:00 aemet   0076       "BARCELONA … BARCELONA               4
#>  5 1990-01-01 00:00:00 aemet   0149D      "MANRESA (L… BARCELONA             291
#>  6 1990-01-01 00:00:00 aemet   0158O      "MONTSERRAT" BARCELONA             738
#>  7 1990-01-01 00:00:00 aemet   0200E      "BARCELONA,… BARCELONA             408
#>  8 1990-01-01 00:00:00 aemet   0229I      "SABADELL A… BARCELONA             146
#>  9 1990-01-01 00:00:00 aemet   0294B      "LA BISBAL … GIRONA                 51
#> 10 1990-01-01 00:00:00 aemet   0321       "CAMPDEVANO… GIRONA                731
#> # ℹ 5,056 more rows
#> # ℹ 10 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 [°]>

While for short periods this can be easily done, when needing long periods (years, decades), this can be tedious and prone to error (or at least involves a lot of copy&paste and generate longer scripts).
To avoid this, we can use loops, both in a tidyverse way (purrr::map) or in a more classic approach (for). For both ways, the first thing to do is create the vectors of dates to retrieve:

# First, we prepare the date vectors, with the start and end dates.
start_dates <- seq(as.Date('1990-01-01'), as.Date('1990-07-01'), '15 days')
end_dates <- seq(as.Date('1990-01-15'), as.Date('1990-07-15'), '15 days')

# Both vectors must have the same length
length(start_dates) == length(end_dates)
#> [1] TRUE

# lets see them
data.frame(start_dates, end_dates)
#>    start_dates  end_dates
#> 1   1990-01-01 1990-01-15
#> 2   1990-01-16 1990-01-30
#> 3   1990-01-31 1990-02-14
#> 4   1990-02-15 1990-03-01
#> 5   1990-03-02 1990-03-16
#> 6   1990-03-17 1990-03-31
#> 7   1990-04-01 1990-04-15
#> 8   1990-04-16 1990-04-30
#> 9   1990-05-01 1990-05-15
#> 10  1990-05-16 1990-05-30
#> 11  1990-05-31 1990-06-14
#> 12  1990-06-15 1990-06-29
#> 13  1990-06-30 1990-07-14

tidyverse loop

We are gonna use purrr::map2, to iterate both date vectors at the same time and return a data frame with all the results directly:

# tidyverse map
res_tidyverse <-
  purrr::map2(
    .x = start_dates, .y = end_dates,
    .f = function(start_date, end_date) {
      res <- get_meteo_from(
        'aemet',
        aemet_options(
          api_key = keyring::key_get('aemet'),
          resolution = 'daily',
          start_date = start_date,
          end_date = end_date
        )
      )
      return(res)
    }
  ) |>
  purrr::list_rbind()
#> ℹ © 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
#> ℹ © 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
#> ℹ © 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
#> ℹ © 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
#> ℹ © 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
#> ℹ © 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
#> ℹ © 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
#> ℹ API request limit reached: Client error: (429) Too Many Requests (RFC 6585)
#> Trying again in 60 seconds
#> ℹ © 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
#> ℹ © 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
#> ℹ © 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
#> ℹ © 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
#> ℹ © 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

head(res_tidyverse)
#>    timestamp service station_id          station_name station_province altitude
#> 1 1990-01-01   aemet      0002I           VANDELLÒS          TARRAGONA   32 [m]
#> 2 1990-01-01   aemet      0016A       REUS AEROPUERTO        TARRAGONA   71 [m]
#> 3 1990-01-01   aemet      0016B REUS (CENTRE LECTURA)        TARRAGONA  118 [m]
#> 4 1990-01-01   aemet       0076  BARCELONA AEROPUERTO        BARCELONA    4 [m]
#> 5 1990-01-01   aemet      0149D    MANRESA (LA CULLA)        BARCELONA  291 [m]
#> 6 1990-01-01   aemet      0158O            MONTSERRAT        BARCELONA  738 [m]
#>   mean_temperature min_temperature max_temperature mean_relative_humidity
#> 1        11.2 [°C]        8.8 [°C]       13.7 [°C]                 80 [%]
#> 2         9.0 [°C]        5.4 [°C]       12.6 [°C]                 89 [%]
#> 3        10.1 [°C]        8.4 [°C]       11.8 [°C]                 82 [%]
#> 4         9.2 [°C]        4.0 [°C]       14.4 [°C]                 74 [%]
#> 5         5.0 [°C]       -1.5 [°C]       11.6 [°C]                 69 [%]
#> 6         7.0 [°C]        3.0 [°C]       11.0 [°C]                 NA [%]
#>   min_relative_humidity max_relative_humidity precipitation mean_wind_speed
#> 1                NA [%]                NA [%]     0 [L/m^2]       1.7 [m/s]
#> 2                NA [%]                NA [%]     0 [L/m^2]       0.6 [m/s]
#> 3                NA [%]                NA [%]     0 [L/m^2]       0.6 [m/s]
#> 4                NA [%]                NA [%]     0 [L/m^2]       3.6 [m/s]
#> 5                NA [%]                NA [%]     0 [L/m^2]       0.8 [m/s]
#> 6                NA [%]                NA [%]     0 [L/m^2]        NA [m/s]
#>   insolation                   geometry
#> 1    0.1 [h] POINT (0.8713889 40.95806)
#> 2    0.1 [h]    POINT (1.163611 41.145)
#> 3    0.2 [h]  POINT (1.108889 41.15417)
#> 4    6.7 [h]      POINT (2.07 41.29278)
#> 5    7.8 [h]     POINT (1.840278 41.72)
#> 6     NA [h]  POINT (1.839167 41.59444)

for loop

We use base::for, iterating by the index of the dates vectors:

# base for loop
res_for <- data.frame()

for (index in seq_along(start_dates)) {
  temp_res <- get_meteo_from(
    'aemet',
    aemet_options(
      api_key = keyring::key_get('aemet'),
      resolution = 'daily',
      start_date = start_dates[index],
      end_date = end_dates[index]
    )
  )
  
  res_for <- rbind(res_for, temp_res)
}

head(res_for)
#> Simple feature collection with 6 features and 15 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 0.8713889 ymin: 40.95806 xmax: 2.07 ymax: 41.72
#> Geodetic CRS:  WGS 84
#> # A tibble: 6 × 16
#>   timestamp           service station_id station_name  station_province altitude
#>   <dttm>              <chr>   <chr>      <chr>         <chr>                 [m]
#> 1 1990-01-01 00:00:00 aemet   0002I      "VANDELLÒS  " TARRAGONA              32
#> 2 1990-01-01 00:00:00 aemet   0016A      "REUS AEROPU… TARRAGONA              71
#> 3 1990-01-01 00:00:00 aemet   0016B      "REUS (CENTR… TARRAGONA             118
#> 4 1990-01-01 00:00:00 aemet   0076       "BARCELONA A… BARCELONA               4
#> 5 1990-01-01 00:00:00 aemet   0149D      "MANRESA (LA… BARCELONA             291
#> 6 1990-01-01 00:00:00 aemet   0158O      "MONTSERRAT"  BARCELONA             738
#> # ℹ 10 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 [°]>

Both methods return identical results:

# both are identical
identical(res_tidyverse, res_for)
#> [1] FALSE

In a loop, no matter if a purrr::map or a for loop, each iteration will connect with the API, consuming connections from the user quota. Take this into consideration when creating loops for longer periods, as you can reach your API request limits for the day/month… (it depends on the service API).

MeteoCat API

When using MeteoCat in daily, monthly and yearly there are restrictions on the period that can be accessed.

daily

daily always returns the whole month the date selected is in, i.e. for start_date = as.Date('2020-04-10') it will return all days in April, 2020:

api_options <- meteocat_options(
  'daily', start_date = as.Date('2020-04-10'),
  api_key = keyring::key_get('meteocat')
)
april_2020 <- get_meteo_from('meteocat', api_options)
#> ℹ Data provided by meteo.cat © Servei Meteorològic de Catalunya
#> https://www.meteo.cat/wpweb/avis-legal/#info
unique(april_2020$timestamp)
#>  [1] "2020-04-01 UTC" "2020-04-02 UTC" "2020-04-03 UTC" "2020-04-04 UTC"
#>  [5] "2020-04-05 UTC" "2020-04-06 UTC" "2020-04-07 UTC" "2020-04-08 UTC"
#>  [9] "2020-04-09 UTC" "2020-04-10 UTC" "2020-04-11 UTC" "2020-04-12 UTC"
#> [13] "2020-04-13 UTC" "2020-04-14 UTC" "2020-04-15 UTC" "2020-04-16 UTC"
#> [17] "2020-04-17 UTC" "2020-04-18 UTC" "2020-04-19 UTC" "2020-04-20 UTC"
#> [21] "2020-04-21 UTC" "2020-04-22 UTC" "2020-04-23 UTC" "2020-04-24 UTC"
#> [25] "2020-04-25 UTC" "2020-04-26 UTC" "2020-04-27 UTC" "2020-04-28 UTC"
#> [29] "2020-04-29 UTC" "2020-04-30 UTC"

This means that if we want more than one month, we need to use loops in a similar way as described previously for AEMET:

start_dates <- seq(as.Date('2020-01-01'), as.Date('2020-04-01'), 'months')
# tidyverse map
meteocat_2020q1_tidyverse <-
  purrr::map(
    .x = start_dates,
    .f = function(start_date) {
      res <- get_meteo_from(
        'meteocat',
        meteocat_options(
          api_key = keyring::key_get('meteocat'),
          resolution = 'daily',
          start_date = start_date
        )
      )
      return(res)
    }
  ) |>
  purrr::list_rbind()
#> ℹ Data provided by meteo.cat © Servei Meteorològic de Catalunya
#> https://www.meteo.cat/wpweb/avis-legal/#info
#> ℹ Data provided by meteo.cat © Servei Meteorològic de Catalunya
#> https://www.meteo.cat/wpweb/avis-legal/#info
#> ℹ Data provided by meteo.cat © Servei Meteorològic de Catalunya
#> https://www.meteo.cat/wpweb/avis-legal/#info
#> ℹ Data provided by meteo.cat © Servei Meteorològic de Catalunya
#> https://www.meteo.cat/wpweb/avis-legal/#info

head(meteocat_2020q1_tidyverse)
#>    timestamp  service station_id                station_name station_province
#> 1 2020-01-01 meteocat         C6         Castellnou de Seana           Lleida
#> 2 2020-01-01 meteocat         C7                     Tàrrega           Lleida
#> 3 2020-01-01 meteocat         C8                     Cervera           Lleida
#> 4 2020-01-01 meteocat         C9            Mas de Barberans        Tarragona
#> 5 2020-01-01 meteocat         CC                        Orís        Barcelona
#> 6 2020-01-01 meteocat         CD la Seu d'Urgell - Bellestar           Lleida
#>   altitude mean_temperature min_temperature max_temperature
#> 1  264 [m]         3.9 [°C]        3.2 [°C]        4.5 [°C]
#> 2  427 [m]         2.5 [°C]        1.9 [°C]        3.3 [°C]
#> 3  554 [m]         1.6 [°C]        0.6 [°C]        2.3 [°C]
#> 4  240 [m]         3.4 [°C]        1.7 [°C]        6.5 [°C]
#> 5  626 [m]         2.2 [°C]       -2.5 [°C]       11.1 [°C]
#> 6  849 [m]         2.5 [°C]       -3.2 [°C]       11.9 [°C]
#>   mean_relative_humidity min_relative_humidity max_relative_humidity
#> 1                 93 [%]                87 [%]                97 [%]
#> 2                 97 [%]                93 [%]               100 [%]
#> 3                100 [%]               100 [%]               100 [%]
#> 4                 87 [%]                72 [%]                97 [%]
#> 5                 85 [%]                53 [%]                96 [%]
#> 6                 82 [%]                45 [%]                98 [%]
#>   precipitation mean_wind_direction mean_wind_speed global_solar_radiation
#> 1   0.0 [L/m^2]              NA [°]        NA [m/s]           1.6 [MJ/m^2]
#> 2   0.0 [L/m^2]              NA [°]        NA [m/s]           1.4 [MJ/m^2]
#> 3   0.1 [L/m^2]              NA [°]        NA [m/s]           1.6 [MJ/m^2]
#> 4   0.0 [L/m^2]              NA [°]        NA [m/s]           4.1 [MJ/m^2]
#> 5   0.0 [L/m^2]              NA [°]        NA [m/s]           8.1 [MJ/m^2]
#> 6   0.0 [L/m^2]              NA [°]        NA [m/s]           8.2 [MJ/m^2]
#>                   geometry
#> 1  POINT (0.95172 41.6566)
#> 2 POINT (1.16234 41.66695)
#> 3 POINT (1.29609 41.67555)
#> 4 POINT (0.39988 40.71825)
#> 5 POINT (2.20862 42.07398)
#> 6 POINT (1.43277 42.37083)

# base for loop
meteocat_2020q1_for <- data.frame()

for (index in seq_along(start_dates)) {
  temp_res <- get_meteo_from(
    'meteocat',
    meteocat_options(
      api_key = keyring::key_get('meteocat'),
      resolution = 'daily',
      start_date = start_dates[index]
    )
  )
  
  meteocat_2020q1_for <- rbind(meteocat_2020q1_for, temp_res)
}

head(meteocat_2020q1_for)
#> Simple feature collection with 6 features and 16 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 0.39988 ymin: 40.71825 xmax: 2.20862 ymax: 42.37083
#> Geodetic CRS:  WGS 84
#> # A tibble: 6 × 17
#>   timestamp           service  station_id station_name station_province altitude
#>   <dttm>              <chr>    <chr>      <chr>        <chr>                 [m]
#> 1 2020-01-01 00:00:00 meteocat C6         Castellnou … Lleida                264
#> 2 2020-01-01 00:00:00 meteocat C7         Tàrrega      Lleida                427
#> 3 2020-01-01 00:00:00 meteocat C8         Cervera      Lleida                554
#> 4 2020-01-01 00:00:00 meteocat C9         Mas de Barb… Tarragona             240
#> 5 2020-01-01 00:00:00 meteocat CC         Orís         Barcelona             626
#> 6 2020-01-01 00:00:00 meteocat CD         la Seu d'Ur… Lleida                849
#> # ℹ 11 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_direction [°], mean_wind_speed [m/s],
#> #   global_solar_radiation [MJ/m^2], geometry <POINT [°]>

# both are identical
identical(meteocat_2020q1_tidyverse, meteocat_2020q1_for)
#> [1] FALSE

monthly

monthly always returns the whole year the date selected is in, i.e. for start_date = as.Date('2020-04-10') it will return all months in 2020:

api_options <- meteocat_options(
  'monthly', start_date = as.Date('2020-04-10'),
  api_key = keyring::key_get('meteocat')
)
year_2020 <- get_meteo_from('meteocat', api_options)
#> ℹ Data provided by meteo.cat © Servei Meteorològic de Catalunya
#> https://www.meteo.cat/wpweb/avis-legal/#info
unique(year_2020$timestamp)
#>  [1] "2020-01-01 UTC" "2020-02-01 UTC" "2020-03-01 UTC" "2020-04-01 UTC"
#>  [5] "2020-05-01 UTC" "2020-06-01 UTC" "2020-07-01 UTC" "2020-08-01 UTC"
#>  [9] "2020-09-01 UTC" "2020-10-01 UTC" "2020-11-01 UTC" "2020-12-01 UTC"

Which means that if we need more than one year of monthly data, we need to use loops again:

start_dates <- seq(as.Date('2019-01-01'), as.Date('2020-01-01'), 'years')
# tidyverse map
meteocat_2019_20_tidyverse <-
  purrr::map(
    .x = start_dates,
    .f = function(start_date) {
      res <- get_meteo_from(
        'meteocat',
        meteocat_options(
          api_key = keyring::key_get('meteocat'),
          resolution = 'monthly',
          start_date = start_date
        )
      )
      return(res)
    }
  ) |>
  purrr::list_rbind()
#> ℹ Data provided by meteo.cat © Servei Meteorològic de Catalunya
#> https://www.meteo.cat/wpweb/avis-legal/#info
#> ℹ Data provided by meteo.cat © Servei Meteorològic de Catalunya
#> https://www.meteo.cat/wpweb/avis-legal/#info

head(meteocat_2019_20_tidyverse)
#>    timestamp  service station_id                station_name station_province
#> 1 2019-01-01 meteocat         C6         Castellnou de Seana           Lleida
#> 2 2019-01-01 meteocat         C7                     Tàrrega           Lleida
#> 3 2019-01-01 meteocat         C8                     Cervera           Lleida
#> 4 2019-01-01 meteocat         C9            Mas de Barberans        Tarragona
#> 5 2019-01-01 meteocat         CC                        Orís        Barcelona
#> 6 2019-01-01 meteocat         CD la Seu d'Urgell - Bellestar           Lleida
#>   altitude mean_temperature min_temperature_absolute min_temperature_mean
#> 1  264 [m]         2.9 [°C]                -7.0 [°C]            -1.8 [°C]
#> 2  427 [m]         2.9 [°C]                -5.0 [°C]            -0.5 [°C]
#> 3  554 [m]         2.5 [°C]                -5.6 [°C]            -0.7 [°C]
#> 4  240 [m]         9.4 [°C]                 0.2 [°C]             5.1 [°C]
#> 5  626 [m]         2.3 [°C]                -8.3 [°C]            -2.7 [°C]
#> 6  849 [m]         2.9 [°C]                -7.1 [°C]            -3.0 [°C]
#>   max_temperature_absolute max_temperature_mean mean_relative_humidity
#> 1                17.9 [°C]             8.5 [°C]                 84 [%]
#> 2                15.9 [°C]             7.4 [°C]                 85 [%]
#> 3                14.0 [°C]             6.5 [°C]                 85 [%]
#> 4                18.0 [°C]            14.0 [°C]                 55 [%]
#> 5                15.7 [°C]             9.9 [°C]                 73 [%]
#> 6                17.9 [°C]            10.7 [°C]                 56 [%]
#>   min_relative_humidity_absolute min_relative_humidity_mean
#> 1                         23 [%]                     64 [%]
#> 2                         28 [%]                     68 [%]
#> 3                         27 [%]                     70 [%]
#> 4                         17 [%]                     38 [%]
#> 5                          8 [%]                     43 [%]
#> 6                          4 [%]                     28 [%]
#>   max_relative_humidity_absolute max_relative_humidity_mean precipitation
#> 1                        100 [%]                     97 [%]   8.4 [L/m^2]
#> 2                        100 [%]                     96 [%]  14.1 [L/m^2]
#> 3                        100 [%]                     95 [%]  10.4 [L/m^2]
#> 4                         97 [%]                     76 [%]   4.0 [L/m^2]
#> 5                         96 [%]                     90 [%]   8.0 [L/m^2]
#> 6                         98 [%]                     78 [%]  34.9 [L/m^2]
#>   mean_wind_direction mean_wind_speed global_solar_radiation
#> 1              NA [°]        NA [m/s]           6.4 [MJ/m^2]
#> 2              NA [°]        NA [m/s]           6.4 [MJ/m^2]
#> 3              NA [°]        NA [m/s]           6.7 [MJ/m^2]
#> 4              NA [°]        NA [m/s]           9.3 [MJ/m^2]
#> 5              NA [°]        NA [m/s]           8.0 [MJ/m^2]
#> 6              NA [°]        NA [m/s]           8.4 [MJ/m^2]
#>                   geometry
#> 1  POINT (0.95172 41.6566)
#> 2 POINT (1.16234 41.66695)
#> 3 POINT (1.29609 41.67555)
#> 4 POINT (0.39988 40.71825)
#> 5 POINT (2.20862 42.07398)
#> 6 POINT (1.43277 42.37083)

# base for loop
meteocat_2019_20_for <- data.frame()

for (index in seq_along(start_dates)) {
  temp_res <- get_meteo_from(
    'meteocat',
    meteocat_options(
      api_key = keyring::key_get('meteocat'),
      resolution = 'monthly',
      start_date = start_dates[index]
    )
  )
  
  meteocat_2019_20_for <- rbind(meteocat_2019_20_for, temp_res)
}
head(meteocat_2019_20_for)
#> Simple feature collection with 6 features and 20 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 0.39988 ymin: 40.71825 xmax: 2.20862 ymax: 42.37083
#> Geodetic CRS:  WGS 84
#> # A tibble: 6 × 21
#>   timestamp           service  station_id station_name station_province altitude
#>   <dttm>              <chr>    <chr>      <chr>        <chr>                 [m]
#> 1 2019-01-01 00:00:00 meteocat C6         Castellnou … Lleida                264
#> 2 2019-01-01 00:00:00 meteocat C7         Tàrrega      Lleida                427
#> 3 2019-01-01 00:00:00 meteocat C8         Cervera      Lleida                554
#> 4 2019-01-01 00:00:00 meteocat C9         Mas de Barb… Tarragona             240
#> 5 2019-01-01 00:00:00 meteocat CC         Orís         Barcelona             626
#> 6 2019-01-01 00:00:00 meteocat CD         la Seu d'Ur… Lleida                849
#> # ℹ 15 more variables: mean_temperature [°C], min_temperature_absolute [°C],
#> #   min_temperature_mean [°C], max_temperature_absolute [°C],
#> #   max_temperature_mean [°C], mean_relative_humidity [%],
#> #   min_relative_humidity_absolute [%], min_relative_humidity_mean [%],
#> #   max_relative_humidity_absolute [%], max_relative_humidity_mean [%],
#> #   precipitation [L/m^2], mean_wind_direction [°], mean_wind_speed [m/s],
#> #   global_solar_radiation [MJ/m^2], geometry <POINT [°]>

# both are identical
identical(meteocat_2019_20_tidyverse, meteocat_2019_20_for)
#> [1] FALSE

yearly

yearly always returns all available years and start_date argument is ignored, i.e. using start_date = as.Date('2020-04-10') will return all years, independently of the date supplied:

api_options <- meteocat_options(
  'yearly', start_date = as.Date('2020-04-10'),
  api_key = keyring::key_get('meteocat')
)
all_years <- get_meteo_from('meteocat', api_options)
#> ℹ Data provided by meteo.cat © Servei Meteorològic de Catalunya
#> https://www.meteo.cat/wpweb/avis-legal/#info
unique(all_years$timestamp)
#>  [1] "1989-10-17 UTC" "1990-10-17 UTC" "1991-10-17 UTC" "1992-10-17 UTC"
#>  [5] "1993-10-17 UTC" "1994-10-17 UTC" "1995-10-17 UTC" "1996-10-17 UTC"
#>  [9] "1997-10-17 UTC" "1998-10-17 UTC" "1999-10-17 UTC" "2000-10-17 UTC"
#> [13] "2001-10-17 UTC" "2002-10-17 UTC" "2003-10-17 UTC" "2004-10-17 UTC"
#> [17] "2005-10-17 UTC" "2006-10-17 UTC" "2007-10-17 UTC" "2008-10-17 UTC"
#> [21] "2009-10-17 UTC" "2010-10-17 UTC" "2011-10-17 UTC" "2012-10-17 UTC"
#> [25] "2013-10-17 UTC" "2014-10-17 UTC" "2015-10-17 UTC" "2016-10-17 UTC"
#> [29] "2017-10-17 UTC" "2018-10-17 UTC" "2019-10-17 UTC" "2020-10-17 UTC"
#> [33] "2021-10-17 UTC" "2022-10-17 UTC" "2023-10-17 UTC" "2024-10-17 UTC"

This means that with yearly we always get all the data available, so there is no need of loops.