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
β οΈ Note that we're using the hda package, originally designed for Python, within R. This means we're not using a specific R client but leveraging the Python HDA package directly in R scripts to access the HDA API's functionalities seamlessly.
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 our contact webpage
via e-mail to our support team (supportATwekeo.eu)