HARPgroup / cbp_wsm

1 stars 0 forks source link

Environment Variables and Scripting for HSPF/hsp2 #68

Open rburghol opened 2 years ago

rburghol commented 2 years ago

Overview

There are a set of "environment variables" that are available when running scripts related to the CBP modeling suite. These environment variables are enabled by using the command . hspf_config inside your script, or by running your script via the cbp helper script which also calls . hspf_config (see details of both scripts at https://github.com/HARPgroup/cbp_wsm/issues/69). The variables that are defined in hspf.config are then exported to any subsequent script that is called, so common locations are available to any programs that are executed from your command, which helps all scripts know where critical data, executables and libraries are.

Current list of Variables in hspf.config

The variables are as follows:

Using in a Script

This function has been tested in bash and csh scripts and runs in both with the same syntax . hspf_config

Annotated Excerpt from NLDAS

This example is from the NLDAS processing tools, which takes a blank wdm, populates it with meteorology data, then moves that wdm into the directory indicated by the environment variables loaded with the command . hspf_config. Sample steps are as follows:

Full Script: p6/gb604b/run/useful/copy_scenario

#!/bin/csh
# load env vars
. hspf_config

 set scen1 = $argv[1]
 set scen2 = $argv[2]

 cp $CBP_ROOT/config/control/land/${scen1}.con $CBP_ROOT/config/control/land/${scen2}.con
 cp $CBP_ROOT/config/control/river/${scen1}.con $CBP_ROOT/config/control/river/${scen2}.con
 cp $CBP_ROOT/config/control/script/${scen1}.con $CBP_ROOT/config/control/script/${scen2}.con

Full bash Script that Calls R script p532/run/export/wdm_flow_csv

#!/bin/bash
if [ $# -lt 2 ]; then
  echo "Usage: cbp wdm_flow_csv scenario landseg syear eyear"
    exit 1
fi
# load paths
. hspf_config
scenario=$1
basin=$2
syear=$3
eyear=$4
# check the directories
if [ ! -d "$CBP_EXPORT_DIR" ] ; then mkdir $CBP_EXPORT_DIR; fi
if [ ! -d "$CBP_EXPORT_DIR/$scenario" ] ; then mkdir $CBP_EXPORT_DIR/$scenario; fi
if [ ! -d "$CBP_EXPORT_DIR/$scenario/eos" ] ; then mkdir $CBP_EXPORT_DIR/$scenario/eos; fi

#segments=`cbp get_landsegs $basin`
segments=`cbp get_landsegs $basin`
for landseg in $segments; do
  echo "Rscript $CBP_ROOT/run/export/wdm_export_flow.R $scenario $landseg $syear $eyear $CBP_EXPORT_DIR $CBP_ROOT"
  Rscript $CBP_ROOT/run/export/wdm_export_flow.R $scenario $landseg $syear $eyear $CBP_EXPORT_DIR $CBP_ROOT
done