Skip to content

Commit

Permalink
wip timeseries_to_catflow_precip
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexDo1 committed Apr 4, 2023
1 parent 6f6715d commit 6bfd5b3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
8 changes: 7 additions & 1 deletion in/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
35 changes: 34 additions & 1 deletion src/lib.R
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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
)
}
8 changes: 8 additions & 0 deletions src/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""))
Expand Down
31 changes: 30 additions & 1 deletion src/tool.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6bfd5b3

Please sign in to comment.