diff --git a/ush/bash_utils.sh b/ush/bash_utils.sh index 0bfbfa51ce9..a7c45f4b976 100755 --- a/ush/bash_utils.sh +++ b/ush/bash_utils.sh @@ -36,7 +36,9 @@ function declare_from_tmpl() { # MEMDIR='mem001' YMD=${PDY} HH=${cyc} declare_from_tmpl -rx \ # COMOUT_ATMOS_HISTORY:COM_ATMOS_HISTORY_TMPL # - if [[ ${DEBUG_WORKFLOW:-"NO"} == "NO" ]]; then set +x; fi + if [[ ${DEBUG_WORKFLOW:-"NO"} == "NO" ]]; then + set +x + fi local opts="-g" local OPTIND=1 while getopts "rx" option; do @@ -110,6 +112,53 @@ function wait_for_file() { return 1 } +# This utility is to be used to create a COM structure in the DATAROOT +# It will replace the root path (up to $COMROOT) with $DATAROOT +# Use realpath --relative-to to get the relative path from $COMROOT to the target file +# and then prepend $DATAROOT to that path to get the new target path +function dataroot_com_path() { + # + # Generate a COM path in the DATAROOT based on an existing COM path. + # + # This function takes an existing COM path and generates a corresponding + # path in the DATAROOT by replacing the root directory with DATAROOT. + # + # Syntax: + # dataroot_com_path original_com_path + # + # original_com_path: The original COM path to be transformed. + # + # Example: + # # Declare COMOUT_ATMOS_ANALYSIS using template + # YMD=${PDY} HH=${cyc} declare_from_tmpl -rx \ + # COMOUT_ATMOS_ANALYSIS:COM_ATMOS_ANALYSIS_TMPL + # # Get the DATAROOT version of the COM path + # pCOMOUT_ATMOS_ANALYSIS=$(dataroot_com_path "${COMOUT_ATMOS_ANALYSIS}") + # echo "New COM path in DATAROOT: ${pCOMOUT_ATMOS_ANALYSIS}" + # + + set +x + if [[ $# -ne 1 ]]; then + echo "FATAL ERROR in dataroot_com_path: Incorrect number of arguments!" + echo "Usage: dataroot_com_path original_com_path" + exit 2 + fi + + local original_com_path=${1} + + if [[ -z "${COMROOT:-}" || -z "${DATAROOT:-}" ]]; then + echo "FATAL ERROR in dataroot_com_path: COMROOT and DATAROOT must be defined!" + exit 2 + fi + + local relative_path + relative_path=$(realpath --relative-to="${COMROOT}" "${original_com_path}") + local new_com_path="${DATAROOT}/${relative_path}" + + echo "${new_com_path}" + set_trace +} + # Initialize stacks for tick-tock profiling (initialize only once) if [[ -z ${_GW_TIMER_STACK+x} ]]; then declare -xa _GW_TIMER_STACK=() @@ -198,5 +247,6 @@ tock() { declare -xf declare_from_tmpl declare -xf wait_for_file +declare -xf dataroot_com_path declare -xf tick declare -xf tock