How to use the HDA API Python Client in R?

Let see how the HDA API Client python can be used within 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 API Python Client, in a R script.

But first, you need to install the HDA API Python Client: the latest version is already available in the JupyterHub WEkEO server, in the miniwekeolab environment. Otherwise, here you will find the procedure.

Set up


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 you to specify an alternate version, for example:

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

The use_virtualenv() and use_condaenv() functions enable you 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")

Let's now see a practical example on how to use the HDA API.

R script


Step 1. Import module

As we mentioned above, we will need the reticulate R package to be imported:

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/edimedio/.hdarc")
if (!file.exists(hdarc)) {
USERNAME <- readline(prompt = "Enter your username: ")
PASSWORD <- readline(prompt = "Enter your password: ")

cat("url: https://wekeo-broker.apps.mercator.dpi.wekeo.eu/databroker\n",
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 and run the download in the directory of choice (here path = "/data"):

# Select the parameters for the download
query <- list(
datasetId = "EO:EEA:DAT:CLMS_HRVPP_VPP",
boundingBoxValues = list(
list(
name = "bbox",
bbox = list(
1.1249375000000001,
42.5133734277223,
10.968140625000002,
48.4222938065848
)
)
),
dateRangeSelectValues = list(
list(
name = "temporal_interval",
start = "2019-01-01T00:00:00.000Z",
end = "2020-01-01T00:00:00.000Z"
)
),
stringChoiceValues = list(
list(
name = "productType",
value = "TPROD"
),
list(
name = "productGroupId",
value = "s1")
)
)

# 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! You can now use the HDA API in R! πŸ™Œ

What's next?


These articles might be of interest for you:

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?