The Earth Observation Prototype 6 (EOP6) for downscaling Snow Water Equivalent (SWE) is a prototype script designed to refine coarse resolution ERA5-Land SWE data to a higher spatial resolution using a random forest regression machine learning implementation in R. The script contains functionality to build a random forest training dataset, train a random forest regression model, and to perform SWE downscaling predictions. The random forest algorithm can be trained using SWE observations, open-source climate reanalysis data, and a variety of geographical data layers. The approach was designed and tested for the Upper Danube River Basin, but it can be applied anywhere when local predictor datasets and SWE observations are available.
This repository is part of the Deliverable 3.2 of SOS-Water - Water Resources System Safe Operating Space in a Changing Climate and Society (DOI:10.3030/101059264). Other code contributions to D3.2 can be found at the SOS-Water - WP3 Earth Observation repository.
Check out the project website at sos-water.eu for more information on the project.
This folder contains the necessary scripts to produce a random forest training dataset, random forest regression model, and to perform SWE downscaling predictions. Follow these steps to run the script:
-
Set the working directory:
Edit line 14 in the script:setwd("/path/to/working/directory") -
Provide a unique simulation name:
Specify the simulation name on line 20. This name will be used as a prefix for directories where all output data will be stored:simulation_name <- "my_simulation"
-
Provide a raster file or shapefile for the area of interest (AOI):
Define the AOI on line 23. Supported formats include GeoTIFF and ESRI Shapefile. -
Supply predictor rasters:
Ensure that all predictor rasters meet the following criteria:- Projected to a common coordinate reference system (e.g., EPSG:4326 (WGS84) or UTM).
- Resampled to a common spatial resolution.
- Provided in a supported format (GeoTIFF/NetCDF).
The required predictor rasters include:
- ERA5-Land Snow Water Equivalent [mm]
- Elevation [m]
- Aspect [°]
- Slope [°]
- Surface Roughness [m]
-
Prepare SWE observation data:
Supply a vector file and CSV file of SWE observations on lines 38–43. Combined the data needs to included time series and locations of weather stations. Due to the nature of each dataset being different the script should be adapted so that you eventually end up with the following columns:date(e.g.,YYYY-MM-DD)SWEvalue (numeric, in mm)coordinates(latitude, longitude)
-
Specify missing value. In line 45 specify
na_valueto be filtered out of training dataset.na_value <- -999
-
Specify user flags:
Configure the following flags on lines 58–60 to enable or disable specific functionalities:- Create a new training dataset (
TRUE/FALSE) - Train a random forest regression model (
TRUE/FALSE) - Perform SWE downscaling predictions (
TRUE/FALSE)
create_new_dataset <- TRUE # Flag to create a new dataset train_new_model <- TRUE # Flag to train a new RF model do_predictions <- TRUE # Flag to perform predictions
- Create a new training dataset (
-
Set the date range for predictions:
Specify the desired date range for predictions on lines 63–64 using the formatYYYY-MM-DD.start_date <- "2001-01-01" end_date <- "2001-12-31"
-
Run the script: At the bottom of the script, run the selection of functions.
- Use the
create_datasetfunction to build a training dataset. - Use the
train_rfrfunction to train a random forest regression model. - Use the
predict_swefunction to perform SWE downscaling predictions for the specified date range.
Detailed technical notes on the algorithms used are available at the SOS-Water - WP3 Earth Observation repository. The produced training dataset, random forest regression model, and downscaled SWE dataset for the Upper Danube River Basin (1990–2023) can be requested from t.schults@futurewater.nl.
The views and opinions expressed are those of the author(s) only and do not necessarily reflect those of the European Union or CINEA. Neither the European Union nor the granting authority can be held responsible for them.
![]() |
This project has received funding from the European Union’s Horizon Europe research and innovation programme under grant agreement No 101059264. |

