Spaces:
Running

hyperFVC / workflow /scripts /lib /fvc_env.sh
lauransotomayor's picture
Mirror GitHub Pages presentation build (part 4)
d1ffd83 verified
Raw History Blame Contribute Delete
4.74 kB
#!/usr/bin/env bash
# =============================================================================
# fvc_env.sh — shared environment and path resolution for scripts/*.sh
# =============================================================================
# Source this at the top of any driver script, immediately after `set -euo
# pipefail`:
#
# source "$(dirname "${BASH_SOURCE[0]}")/lib/fvc_env.sh"
#
# It provides:
#
# FVC_REPO_ROOT absolute path to the repository checkout
# PYTHON_BIN the interpreter to run project code with
# FVC_ROOT_<NAME> every root from configs/paths.yaml (DATA, QGIS, UAS, ...)
# FVC_SITE_<NAME> every site from configs/paths.yaml (SITE1, SITE2, ...)
# fvc_layout resolve a layout template, e.g.
# fvc_layout predictors site=site1 grid=3072
# fvc_require_dir assert a directory exists, with an actionable message
#
# Roots come from configs/paths.yaml, so the shell scripts and the Python code
# read the same declarations. Override any of them per run:
#
# FVC_DATA_ROOT=/mnt/disk/calperumResearch bash scripts/<script>.sh
# =============================================================================
# Interpreter resolution must happen before Python can be used, so the
# candidates live here rather than in configs/paths.yaml.
# Order: explicit override, active conda/mamba env, known install paths, PATH.
_fvc_resolve_python() {
local candidate
for candidate in \
"${ENV_PYTHON:-}" \
"${PYTHON_BIN:-}" \
"${CONDA_PREFIX:+${CONDA_PREFIX}/bin/python}" \
"${HOME}/.local/share/mamba/envs/fvc_function/bin/python" \
"/opt/conda/bin/python" \
"/opt/conda/envs/fvc_function/bin/python"
do
if [[ -n "${candidate}" && -x "${candidate}" ]]; then
printf '%s\n' "${candidate}"
return 0
fi
done
if command -v python3 >/dev/null 2>&1; then
command -v python3
return 0
fi
if command -v python >/dev/null 2>&1; then
command -v python
return 0
fi
echo "fvc_env: no Python interpreter found. Activate the environment" >&2
echo " (mamba activate fvc_function) or set ENV_PYTHON=/path/to/python" >&2
return 1
}
FVC_REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
export FVC_REPO_ROOT
PYTHON_BIN="$(_fvc_resolve_python)" || exit 1
export PYTHON_BIN
# Kept for scripts that still reference ENV_PYTHON.
ENV_PYTHON="${ENV_PYTHON:-${PYTHON_BIN}}"
export ENV_PYTHON
_FVC_PATHS_PY="${FVC_REPO_ROOT}/utils/fvc_paths.py"
if [[ ! -f "${_FVC_PATHS_PY}" ]]; then
echo "fvc_env: missing ${_FVC_PATHS_PY}" >&2
exit 1
fi
# Resolve every root and site in a single interpreter start, rather than
# spawning Python once per lookup.
if ! _fvc_exports="$("${PYTHON_BIN}" "${_FVC_PATHS_PY}" --shell)"; then
echo "fvc_env: could not read configs/paths.yaml" >&2
echo " run: ${PYTHON_BIN} ${_FVC_PATHS_PY} --check" >&2
exit 1
fi
eval "${_fvc_exports}"
unset _fvc_exports
# Export the resolved names so child processes inherit them.
for _fvc_var in $(compgen -v FVC_ROOT_) $(compgen -v FVC_SITE_); do
export "${_fvc_var?}"
done
unset _fvc_var
# ---------------------------------------------------------------------------
# fvc_layout <template> [key=value ...]
#
# GRID_DIR="$(fvc_layout gridding site=site1 grid=3072)"
# OUT_DIR="$(fvc_layout outputs site=site1 density=medium)"
#
# Templates are declared under `layout:` in configs/paths.yaml.
# ---------------------------------------------------------------------------
fvc_layout() {
local template="$1"; shift
local args=(--layout "${template}")
local kv
for kv in "$@"; do
args+=(--set "${kv}")
done
"${PYTHON_BIN}" "${_FVC_PATHS_PY}" "${args[@]}"
}
# ---------------------------------------------------------------------------
# fvc_require_dir <path> [description]
#
# Fail early with a message that points at the config, instead of letting a
# missing external drive surface as a confusing error deep in a Python run.
# ---------------------------------------------------------------------------
fvc_require_dir() {
local path="$1"
local what="${2:-directory}"
if [[ ! -d "${path}" ]]; then
echo "fvc_env: missing ${what}: ${path}" >&2
echo " check the drive is mounted, then run:" >&2
echo " ${PYTHON_BIN} ${_FVC_PATHS_PY} --check" >&2
return 1
fi
}
# ---------------------------------------------------------------------------
# fvc_require_file <path> [description]
# ---------------------------------------------------------------------------
fvc_require_file() {
local path="$1"
local what="${2:-file}"
if [[ ! -f "${path}" ]]; then
echo "fvc_env: missing ${what}: ${path}" >&2
return 1
fi
}