diff --git a/in/parameters.json b/in/parameters.json index 9515ad5..8206839 100644 --- a/in/parameters.json +++ b/in/parameters.json @@ -93,5 +93,11 @@ "precid": 1, "climid": 1, "windid": [1, 1, 1, 1] - } + }, + "timeseries_to_catflow_precip": { + "raindat": "./in/raindat.csv", + "start.time": "01.01.2004 00:00:00", + "time.unit": "h", + "faktor.p": 1 + } } \ No newline at end of file diff --git a/src/lib.R b/src/lib.R index e2acbcc..627b2ff 100644 --- a/src/lib.R +++ b/src/lib.R @@ -1,8 +1,13 @@ # source catlib.R source("catlib.R") -# implement "workflow scripts" + lib_preprocess_catflow <- function(params) { + ### + # CATFLOW preprocessing workflow, creates all the files + # necessary to start a CATFLOW run. + ### + # (1) make geometry from .tif files catlib_make_geometry_representative_hillslope(params) @@ -33,3 +38,31 @@ lib_preprocess_catflow <- function(params) { # (8) complete the CATFLOW file structure catlib_complete_file_structure(params) } + + +lib_timeseries_to_catflow_precip <- function(data, start.time, time.unit, faktor.p) { + ### + # Convert precipitation time series data into the + # format required by CATFLOW. + # + # Always give the precipitation data (data) in mm/h. + ### + + # Convert the time column to a datetime object + data[, 1] <- as.POSIXct(data$time, format = "%Y-%m-%d %H:%M:%S") + + if (start.time) { + # Create a new column with the hours since the given start.time + data$hours <- as.numeric(difftime(data[, 1], start.time, units = "hours")) + } else { + # Create a new column with the hours since the start of the time series + data$hours <- as.numeric(difftime(data[, 1], min(data$time), units = "hours")) + } + + catlib_write_precip( + raindat = data, + start.time = start.time, + time.unit = time.unit, + faktor.p = faktor.p + ) +} diff --git a/src/run.R b/src/run.R index bb53a76..22d1d89 100644 --- a/src/run.R +++ b/src/run.R @@ -51,6 +51,14 @@ if (toolname == "make_geometry") { } else if (toolname == "preprocess_catflow") { lib_preprocess_catflow(params) +} else if (toolname == "timeseries_to_catflow_precip") { + lib_timeseries_to_catflow_precip( + data = params$data, + start.time = params$start.time, + time.unit = params$time.unit, + faktor.p = params$faktor.p + ) + } else { # in any other case, the tool was invalid or not configured print(paste("[", Sys.time(), "] Either no TOOL_RUN environment variable available, or '", toolname, "' is not valid.\n", sep = "")) diff --git a/src/tool.yml b/src/tool.yml index 954b825..682fa26 100644 --- a/src/tool.yml +++ b/src/tool.yml @@ -604,4 +604,33 @@ tools: Wind direction ID: either a vector of numbers representing different wind direction sectors which are to be assigned to all surface nodes, or a matrix of numbers with the same number of rows as xsi and as - many columns as there are wind direction sectors (default: 4 sectors) + many columns as there are wind direction sectors (default: 4 sectors). + timeseries_to_catflow_precip: + title: Convert precipitation timeseries to CATFLOW specific format + description: | + CATFLOW requires a precipitation record from which the rainfall intensities + are interpolated between discrete time steps; rainfall intensities are thus + only needed at those time steps when they are changing. + This tool converts a precipitation timeseries to CATFLOW specific format. + version: 0.1 + parameters: + data: + type: file + description: | + Rainfall data: data frame with two columns; first column must be + timestamps ("%Y-%m-%d %H:%M:%S"), the second column contains the + precipitation data. + start.time: + type: string + description: Start date for the simulation ("%Y-%m-%d %H:%M:%S") + optional: true + time.unit: + type: string + description: Time units of the precipitation record + default: "h" + faktor.p: + type: float + description: | + Conversion factor for rain rate to m/s (only needed if rate is NOT + given as mm per 'time.unit') + optional: true