climaemet

The goal of climaemet is to serve as an interface to download the climatic data of the Spanish Meteorological Agency (AEMET) directly from R using their API and create scientific graphs (climate charts, trend analysis of climate time series, temperature and precipitation anomalies maps, “warming stripes” graphics, climatograms, etc.).

Browse manual and vignettes at https://ropenspain.github.io/climaemet/.

AEMET Open Data

AEMET OpenData is a REST API developed by AEMET that allows the dissemination and reuse of the Agency’s meteorological and climatological information. To see more details visit: https://opendata.aemet.es/centrodedescargas/inicio

License for the original data

Information prepared by the Spanish Meteorological Agency (© AEMET). You can read about it here.

A summary for the usage of the data could be interpreted as:

People can use freely this data. You should mention AEMET as the collector of the original data in every situation except if you are using this data privately and individually. AEMET makes no warranty as to the accuracy or completeness of the data. All data are provided on an “as is” basis. AEMET is not responsible for any damage or loss derived from the interpretation or use of this data.

Installation

You can install the released version of climaemet from CRAN with:

install.packages("climaemet")

You can install the developing version of climaemet using the r-universe:

# Enable this universe
options(repos = c(
  ropenspain = "https://ropenspain.r-universe.dev",
  CRAN = "https://cloud.r-project.org"
))

install.packages("climaemet")

Alternatively, you can install the developing version of climaemet with:

library(remotes)
install_github("ropenspain/climaemet")

API key

To be able to download data from AEMET you will need a free API key which you can get here.

library(climaemet)

## Get api key from AEMET
browseURL("https://opendata.aemet.es/centrodedescargas/obtencionAPIKey")

## Use this function to register your API Key temporarly or permanently
aemet_api_key("MY API KEY")

Changes on v1.0.0!

Now the apikey parameter on the functions have been deprecated. You may need to set your API Key globally using aemet_api_key(). Note that you would need also to remove the apikey parameter on your old codes.

Now climaemet is tidy…

From v1.0.0 onward, climaemet provides its results in tibble format. 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.).

library(climaemet)

# See a tibble in action

aemet_last_obs("9434")
#> # A tibble: 23 x 25
#>    idema   lon fint                 prec   alt  vmax    vv    dv   lat  dmax
#>    <chr> <dbl> <dttm>              <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 9434  -1.00 2022-02-22 16:00:00     0   249  14.9  11.2   313  41.7   305
#>  2 9434  -1.00 2022-02-22 17:00:00     0   249  13.9   6.7   316  41.7   310
#>  3 9434  -1.00 2022-02-22 18:00:00     0   249   8.8   5.6   300  41.7   300
#>  4 9434  -1.00 2022-02-22 19:00:00     0   249   9.2   5.2   305  41.7   305
#>  5 9434  -1.00 2022-02-22 20:00:00     0   249   8.6   6.3   280  41.7   275
#>  6 9434  -1.00 2022-02-22 21:00:00     0   249  10.4   8.4   297  41.7   303
#>  7 9434  -1.00 2022-02-22 22:00:00     0   249  10.4   5.9   293  41.7   290
#>  8 9434  -1.00 2022-02-22 23:00:00     0   249   9     2.3   324  41.7   275
#>  9 9434  -1.00 2022-02-23 00:00:00     0   249   5     3.6   306  41.7   308
#> 10 9434  -1.00 2022-02-23 01:00:00     0   249   8     6.2   299  41.7   300
#> # ... with 13 more rows, and 15 more variables: ubi <chr>, pres <dbl>,
#> #   hr <dbl>, stdvv <dbl>, ts <dbl>, pres_nmar <dbl>, tamin <dbl>, ta <dbl>,
#> #   tamax <dbl>, tpr <dbl>, stddv <dbl>, inso <dbl>, tss5cm <dbl>,
#> #   pacutp <dbl>, tss20cm <dbl>

Examples

The package provides several functions to access the data of the API. Here you can find some examples:

## Get AEMET stations
stations <- aemet_stations() # Need to have the API Key registered

knitr::kable(head(stations))
indicativoindsinopnombreprovinciaaltitudlongitudlatitud
0252D08186ARENYS DE MARBARCELONA742.54000041.58750
007608181BARCELONA AEROPUERTOBARCELONA42.07000041.29278
0200EBARCELONA, FABRABARCELONA4082.12416741.41833
0201D08180BARCELONABARCELONA62.20000041.39056
0149X08174MANRESABARCELONA2911.84027841.72000
0229I08192SABADELL AEROPUERTOBARCELONA1462.10305641.52361
station <- "9434" # Zaragoza Aeropuerto

## Get last observation values for a station
data_observation <- aemet_last_obs(station)

knitr::kable(head(data_observation))
idemalonfintprecaltvmaxvvdvlatdmaxubipreshrstdvvtspres_nmartamintatamaxtprstddvinsotss5cmpacutptss20cm
9434-1.0041672022-02-22 16:00:00024914.911.231341.66056305ZARAGOZA AEROPUERTO996.1201.420.71025.719.119.319.4-4.2760.015.1011.3
9434-1.0041672022-02-22 17:00:00024913.96.731641.66056310ZARAGOZA AEROPUERTO995.9171.417.61025.618.618.619.3-7.1960.014.8011.6
9434-1.0041672022-02-22 18:00:0002498.85.630041.66056300ZARAGOZA AEROPUERTO996.1180.914.71026.016.916.918.6-7.5735.814.0011.9
9434-1.0041672022-02-22 19:00:0002499.25.230541.66056305ZARAGOZA AEROPUERTO996.4210.513.31026.415.515.516.9-6.760.013.2012.1
9434-1.0041672022-02-22 20:00:0002498.66.328041.66056275ZARAGOZA AEROPUERTO996.6330.612.01026.813.613.615.5-2.550.012.6012.1
9434-1.0041672022-02-22 21:00:00024910.48.429741.66056303ZARAGOZA AEROPUERTO996.8390.912.01027.113.113.113.6-0.560.012.1012.1
## Get daily/annual climatology values for a station
data_daily <-
  aemet_daily_clim(station, start = "2020-01-01", end = "2020-12-31")

knitr::kable(head(data_daily))
fechaindicativonombreprovinciaaltitudtmedprectminhoratmintmaxhoratmaxdirvelmediarachahorarachasolpresMaxhoraPresMaxpresMinhoraPresMin
2020-01-019434ZARAGOZA, AEROPUERTOZARAGOZA2491.00,00.306:101.814:50281.75.604:400.01004.6101001.915
2020-01-029434ZARAGOZA, AEROPUERTOZARAGOZA2490.60,0-0.308:101.618:40291.13.600:400.01003.4101000.716
2020-01-039434ZARAGOZA, AEROPUERTOZARAGOZA2494.20,0-0.107:108.519:10304.48.321:402.41003.6101000.7Varias
2020-01-049434ZARAGOZA, AEROPUERTOZARAGOZA2498.60,03.606:4013.614:50335.012.512:308.21003.9101001.215
2020-01-059434ZARAGOZA, AEROPUERTOZARAGOZA2498.20,03.023:0013.315:20303.610.305:308.91001.900996.9Varias
2020-01-069434ZARAGOZA, AEROPUERTOZARAGOZA2492.30,01.423:593.200:00293.17.220:200.01001.722998.604

## Get monthly/annual climatology values for a station
data_monthly <- aemet_monthly_clim(station, year = 2000)
knitr::kable(head(data_monthly))
fechaindicativop_maxn_cubhrn_gran_foginsoq_maxnw_55q_marq_medtm_minta_maxts_minnt_30nv_0050n_desw_rachanp_100n_nubp_solnw_91ts_20np_001ta_minenp_300nv_1000evapp_mesn_llun_torts_10w_mednt_00ti_maxn_nietm_mestm_maxnv_0100ts_50q_minnp_010
2000-194348.0(15)877065.41003.5(11)51026.0994.1-0.116.3(31)6.2001232/22.2(22)0115604.93-5.8(12)650586114.9305.015161.004.38.705.6979.7(14)2
2000-294340.0(–)060007.71004.9(04)91027.1996.04.820.8(27)10.700833/21.4(17)02172011.80-1.5(06)790016380.00012.716413.4010.816.9010.6988.6(17)0
2000-394345.4(22)458007.81001.0(09)81020.3989.56.024.0(11)11.000832/20.0(15)01965014.97-0.8(03)8300206211.17015.718112.1012.017.9013.6976.5(28)4
2000-4943422.2(26)861006.2991.5(07)71008.0977.88.427.2(25)14.900110/20.0(02)12146016.7101.7(06)9900173049.114117.517011.7013.718.9015.2959.5(02)6
2000-5943423.4(05)661228.7991.4(30)NA1014.4984.713.233.0(30)18.8501NA32459NA23.5139.2(01)14301211667.511824.813021.5019.525.7020.8977.5(06)8
2000-6943412.2(09)2570011.3995.0(14)NA1017.1987.616.037.5(27)20.319018NA11075NA27.7611.0(08)16400295534.98429.221016.5023.029.8026.1977.9(09)5

## Get recorded extreme values of temperature for a station
data_extremes <- aemet_extremes_clim(station, parameter = "T")
knitr::kable(head(data_extremes))
indicativonombreubicacioncodigotemMindiaMinanioMinmesMintemMaxdiaMaxanioMaxmesMaxtemMedBajaanioMedBajamesMedBajatemMedAltaanioMedAltamesMedAltatemMedMinanioMedMinmesMedMintemMedMaxanioMedMaxmesMedMax
9434ZARAGOZA, AEROPUERTOZARAGOZA023000-10441971220682016729195329720167-121957213520167
9434ZARAGOZA, AEROPUERTOZARAGOZA023000-1145196322552720197151956212119907-301956218019907
9434ZARAGOZA, AEROPUERTOZARAGOZA023000-639196422831919577711971214620017191973220719977
9434ZARAGOZA, AEROPUERTOZARAGOZA023000-243196723249201171041986217420147541970223720147
9434ZARAGOZA, AEROPUERTOZARAGOZA023000541967236529200171321984220820177851984227820177
9434ZARAGOZA, AEROPUERTOZARAGOZA023000521119712432292019718219532266200371261969233820037

We can also draw a “warming stripes” graph with the downloaded data from a weather station. These functions returns ggplot2 plots:

# Plot a climate stripes graph for a period of years for a station

library(ggplot2)

climatestripes_station("9434", start = 1980, end = 2020) +
  theme(plot.title = element_text(size = 10))

Furthermore, we can draw the well-known Walter & Lieth climatic diagram for a weather station and over a specified period of time:

# Plot of a Walter & Lieth climatic diagram (normal climatology values) for a station
climatogram_normal("9434", labels = "en")

Additionally, we may be interested in drawing the wind speed and direction over a period of time for the data downloaded from a weather station.:

# Plot a windrose showing the wind speed and direction for a station over a days period.
windrose_days(
  "9434",
  start = "2010-01-01",
  end = "2020-12-31",
  n_speeds = 5,
  speed_cuts = c(2.5, 5, 7.5, 10, 12.5, 15)
) +
  theme(plot.title = element_text(size = 10))

… and spatial!

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

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

library(ggplot2)
library(dplyr)

all_stations <- aemet_last_obs(return_sf = TRUE)
# Last hour
all_last <-
  all_stations %>% filter(fint == all_stations[["fint"]][1])

last_hour <- max(all_last$fint)


ggplot(all_last) +
  geom_sf(aes(colour = ta),
    shape = 19,
    size = 2,
    alpha = 0.5
  ) +
  labs(
    title = "Temperature in Spain",
    subtitle = last_hour,
    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")
  )

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Citation

Using climaemet for a paper you are writing?. Consider citing it:

Pizarro M, Hernangómez D, Fernández-Avilés G (2021). climaemet: Climate AEMET Tools. doi: 10.5281/zenodo.5205573 (URL: https://doi.org/10.5281/zenodo.5205573), <URL: https://hdl.handle.net/10261/250390>.

A BibTeX entry for LaTeX users is:

@Manual{,
  title = {climaemet: Climate AEMET Tools},
  author = {Manuel Pizarro and Diego Hernangómez and Gema Fernández-Avilés},
  abstract = {The goal of climaemet is to serve as an interface to download the climatic data of the Spanish Meteorological Agency (AEMET) directly from R using their API (https://opendata.aemet.es/) and create scientific graphs (climate charts, trend analysis of climate time series, temperature and precipitation anomalies maps, “warming stripes” graphics, climatograms, etc.).},
  year = {2021},
  month = {8},
  url = {https://hdl.handle.net/10261/250390},
  doi = {10.5281/zenodo.5205573},
  keywords = {Climate, Rcran,  Tools, Graphics, Interpolation, Maps},
}

Links

Download from CRAN at https://cran.r-project.org/package=climaemet

Browse source code at https://github.com/ropenspain/climaemet

Copy Link

Version

Down Chevron

Install

install.packages('climaemet')

Monthly Downloads

1,122

Version

1.0.1

License

GPL-3

Issues

Pull Requests

Stars

Forks

Last Published

February 24th, 2022

Functions in climaemet (1.0.1)