Skip to main content
How to use the HDA API in R?

Let see how the Harmonized Data Access (HDA) API can be used in R.

David Bina avatar
Written by David Bina
Updated over a week ago

Context


In this article, we are going to see how to embed the functions from the Harmonized Data Access (HDA) API, in a R script.

First, you need to install the hda package:

  • You can find RStudio in the WEkEO JupyterHub, with the hda package pre-installed (select the Earth Observation Tools server)

  • Otherwise, you will find the installation procedure in this article

Setup to use the HDA in R


We will use the R package reticulate, built for calling Python functions inside a R code.

Install reticulate

Install the reticulate package from CRAN as follows:

install.packages("reticulate")

Python version

By default, reticulate uses the version of Python found in your PATH (e.g. Sys.which("python")).

The use_python() function enables to specify an alternate version, for example:

library(reticulate)
use_python("/usr/local/bin/python")

The use_virtualenv() and use_condaenv() functions allow to specify versions of Python in virtual or Conda environments, for example:

library(reticulate)
use_virtualenv("myenv")

πŸ“ŒNote: if you are in your personal environment, you can install the HDA package directly in R, by running: system("pip install hda")

πŸ’‘WEkEO Pro Tip: you don't need to change the Python version or environment if you are using the RStudio from the WEkEO JupyterHub.

Let's now see a practical example on how to use the HDA API in R! πŸ’ͺ

R script


Step 1. Import module

First let's import the hda package as follows:

library(reticulate)

# Import HDA Python module
hda <- import("hda")

Step 2. Credentials configuration

Afterwards, we must configure user's credentials. Two methods are available.

Method 1 (not regular users)

Configuration of the WEkEO credentials in the script:

# Configure user's credentials if .hdarc not existing
conf <- hda$Configuration(user = "username", password = "password")
hda_client <- hda$Client(config = conf)

Method 2 (regular users)

Create the .hdarc configuration file:

# Check if .hdarc is already existing in the path and create it if not
hdarc <- file.path("/Users/.hdarc")
if (!file.exists(hdarc)) {
USERNAME <- readline(prompt = "Enter your username: ")
PASSWORD <- readline(prompt = "Enter your password: ")

cat("sprintf("user: %s\n", USERNAME),
sprintf("password: %s\n", PASSWORD),
file = hdarc)
}

# Call the .hdarc file
hda_client <- hda$Client()

Step 3. Define query and start download

Once the credentials are defined, we can set the query (see how to get the query) and run the download in the directory of choice (here path = "data"):
​

# Select the parameters for the download
query <- list(
dataset_id = "EO:EEA:DAT:CLMS_HRVPP_VPP",
productType = "TPROD",
productGroupId = "s1",
start = "2020-01-01T00:00:00.000Z",
end = "2021-01-01T00:00:00.000Z",
bbox = c(-9.53592042, 42.46825465, -7.03631028, 43.99700636)
)

# Send the request
matches <- hda_client$search(query)
print(matches)

# Download data in the path "data"
path <- "data"
matches$download(path)

πŸ’‘WEkEO Pro Tip: as in Python, the download function can be customized by slicing the matches object, like:

matches[0]$download() # Will only download the first result

matches[-1]$download() # Will only download the last result

matches[:10]$download() # Will only download the first 10 results

And that's it!

Now you know how to download WEkEO data using the HDA API in R! πŸ™Œ

What's next?


We are user-driven and we implement users' suggestions, so feel free to contact us:

  • through a chat session available in the bottom right corner of the page

  • via e-mail to our support team (supportATwekeo.eu)

Did this answer your question?