Home

Awesome

<!-- README.md is generated from README.Rmd. Please edit that file -->

climaemet <img src="man/figures/logo.png" align="right" width="120"/>

<!-- badges: start -->

rOS-badge CRAN
status CRAN_time_from_release CRAN_latest_release_date CRAN
results r-universe R-CMD-check R-hub codecov DOI metacran
downloads license Project Status: Active – The project has reached a stable, usable
state and is being actively
developed.

<!-- badges: end -->

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:

# Install climaemet in R:
install.packages("climaemet",
  repos = c("https://ropenspain.r-universe.dev", "https://cloud.r-project.org")
)

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: 13 × 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 2024-11-20 02:00:00     0   249   7     2.7   307  41.7   288
#>  2 9434  -1.00 2024-11-20 03:00:00     0   249   7.9   6.1   287  41.7   285
#>  3 9434  -1.00 2024-11-20 04:00:00     0   249  10.8   5.5   290  41.7   290
#>  4 9434  -1.00 2024-11-20 05:00:00     0   249   6.4   4.1   302  41.7   303
#>  5 9434  -1.00 2024-11-20 06:00:00     0   249   5.4   3.8   282  41.7   275
#>  6 9434  -1.00 2024-11-20 07:00:00     0   249   5.6   4     318  41.7   293
#>  7 9434  -1.00 2024-11-20 08:00:00     0   249   6.9   4.1   308  41.7   305
#>  8 9434  -1.00 2024-11-20 09:00:00     0   249   6.5   4.5   301  41.7   298
#>  9 9434  -1.00 2024-11-20 10:00:00     0   249   6.9   4.8   306  41.7   308
#> 10 9434  -1.00 2024-11-20 11:00:00     0   249   7.1   5.3   297  41.7   313
#> 11 9434  -1.00 2024-11-20 12:00:00     0   249   6.9   3.5   318  41.7   310
#> 12 9434  -1.00 2024-11-20 13:00:00     0   249   8.8   5.9   318  41.7   323
#> 13 9434  -1.00 2024-11-20 14:00:00     0   249  11.1   4.6   324  41.7   305
#> # ℹ 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
B013X08304ESCORCA, LLUCILLES BALEARS4902.88583339.82333
B051A08316SÓLLER, PUERTOILLES BALEARS52.69138939.79556
B087XBANYALBUFARILLES BALEARS602.51277839.68917
B103BANDRATX - SANT ELMILLES BALEARS522.36888939.57944
B158XCALVIÀ, ES CAPDELLÀILLES BALEARS502.46638939.55139
B22808301PALMA, PUERTOILLES BALEARS32.62527839.55417

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.0041672024-11-20 02:00:0002497.02.730741.66056288ZARAGOZA AEROPUERTO986.6850.39.61016.910.510.513.18.17011.1013.0
9434-1.0041672024-11-20 03:00:0002497.96.128741.66056285ZARAGOZA AEROPUERTO986.7830.711.61016.810.012.012.09.28010.9012.9
9434-1.0041672024-11-20 04:00:00024910.85.529041.66056290ZARAGOZA AEROPUERTO986.6780.612.11016.711.612.512.98.86011.0012.8
9434-1.0041672024-11-20 05:00:0002496.44.130241.66056303ZARAGOZA AEROPUERTO986.6790.411.31016.811.811.812.68.36011.1012.7
9434-1.0041672024-11-20 06:00:0002495.43.828241.66056275ZARAGOZA AEROPUERTO986.3840.39.71016.610.310.311.87.78010.8012.6
9434-1.0041672024-11-20 07:00:0002495.64.031841.66056293ZARAGOZA AEROPUERTO986.7760.510.81016.910.311.711.77.613010.6012.6

## Get daily/annual climatology values for a station
data_daily <- aemet_daily_clim(station,
  start = "2022-01-01",
  end = "2022-06-30"
)

knitr::kable(head(data_daily))
fechaindicativonombreprovinciaaltitudtmedprectminhoratmintmaxhoratmaxdirvelmediarachahorarachasolpresMaxhoraPresMaxpresMinhoraPresMinhrMediahrMaxhoraHrMaxhrMinhoraHrMin
2022-01-019434ZARAGOZA, AEROPUERTOZARAGOZA2494.50,03.207:505.815:00241.75.617:100.01000.610997.5159810011:0098Varias
2022-01-029434ZARAGOZA, AEROPUERTOZARAGOZA2495.60,02.808:008.317:50242.26.719:201.71000.210997.11696100Varias8915:40
2022-01-039434ZARAGOZA, AEROPUERTOZARAGOZA2497.80,02.506:5013.015:10101.15.621:405.8997.600988.4248810010:506715:10
2022-01-049434ZARAGOZA, AEROPUERTOZARAGOZA24911.27,05.307:3017.214:20322.816.419:003.5988.400976.617879501:404713:50
2022-01-059434ZARAGOZA, AEROPUERTOZARAGOZA2497.00,04.223:599.914:20319.218.605:104.9987.910982.100678300:005314:00
2022-01-069434ZARAGOZA, AEROPUERTOZARAGOZA2495.60,02.906:208.215:20307.516.403:208.9991.424986.400497322:303814:10


## Get monthly/annual climatology values for a station
data_monthly <- aemet_monthly_clim(station, year = 2022)
knitr::kable(head(data_monthly))
fechaindicativop_maxn_cubhrn_gran_foginsoq_maxnw_55q_marq_medtm_minta_maxts_minnt_30nv_0050n_desw_rachanp_100n_nubp_solnw_91np_001ta_minw_recenp_300nv_1000p_mesn_llun_torw_mednt_00ti_maxn_nietm_mestm_maxnv_0100q_minnp_010evap
2022-0194347.0(04)468037.61005.7(29)71028.5996.71.017.2(04)11.2002131/24.2(31)067902-3.2(23)35365037.44015165.806.311.60976.6(04)11021
2022-0294340.4(13)257027.81002.2(08)91025.4994.35.020.5(02)9.300730/23.9(01)01974020.3(10)40873000.84017012.4010.415.80982.3(14)01632
2022-0394346.2(11)1969003.8997.2(22)71019.3988.47.419.5(01)11.800031/18.6(17)012320133.0(09)388920033.613116010.5011.114.80971.2(30)81367
2022-04943411.8(27)956008.5996.0(29)91014.1983.98.825.8(16)14.200528/22.2(08)11664090.5(04)469950031.011020010.2014.119.30965.4(23)72357
2022-0594345.6(03)6450010.7994.8(26)51016.2986.614.835.3(21)19.91401131/19.2(26)014740511.3(03)3991180013.85218018.3021.628.20972.7(29)43486
2022-0694346.4(16)0380011.5992.3(12)121013.1984.119.541.2(15)24.0260628/22.5(11)024760414.4(28)383132009.45516024.8026.733.90975.2(19)33560


## 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-10441971220682016729195329720168-121957213520167
9434ZARAGOZA, AEROPUERTOZARAGOZA023000-1145196322552720197151956212119908-301956218019907
9434ZARAGOZA, AEROPUERTOZARAGOZA023000-639196422871320237711971214720238191973221120237
9434ZARAGOZA, AEROPUERTOZARAGOZA023000-243196723249201171041986217420148541970224020237
9434ZARAGOZA, AEROPUERTOZARAGOZA023000541967236529200171321984221620228851984228220227
9434ZARAGOZA, AEROPUERTOZARAGOZA023000521119712432292019718219532267202281261969233920227

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)

# Example data
temp_data <- climaemet::climaemet_9434_temp

ggstripes(temp_data, plot_title = "Zaragoza Airport") +
  labs(subtitle = "(1950-2020)")
<img src="man/figures/README-climatestripes-1.png" width="100%" />

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 for a station

# Example data
wl_data <- climaemet::climaemet_9434_climatogram

ggclimat_walter_lieth(wl_data,
  alt = "249", per = "1981-2010",
  est = "Zaragoza Airport"
)
<img src="man/figures/README-climatogram-1.png" width="100%" />

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

# Example data
wind_data <- climaemet::climaemet_9434_wind

speed <- wind_data$velmedia
direction <- wind_data$dir

ggwindrose(
  speed = speed, direction = direction,
  speed_cuts = seq(0, 16, 4), legend_title = "Wind speed (m/s)",
  calm_wind = 0, n_col = 1, plot_title = "Zaragoza Airport"
) +
  labs(subtitle = "2000-2020", caption = "Source: AEMET")
<img src="man/figures/README-windrose-1.png" width="100%" />

… 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_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.95) +
  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),
    breaks = c(-10, -5, 0, 5, 10, 15, 20),
    guide = "legend"
  ) +
  theme_bw() +
  theme(
    panel.border = element_blank(),
    plot.title = element_text(face = "bold"),
    plot.subtitle = element_text(face = "italic")
  )
<img src="man/figures/README-spatial-1.png" width="100%" />

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:

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

A BibTeX entry for LaTeX users is:

@Manual{R-climaemet,
  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