#!/bin/sh # ffbench - minimal CLI for the CONUS Flash-Flood Benchmark (curl required; jq optional). # Usage: ffbench {query|download|get|bundle|help} [flags] set -eu BASE="${FFBENCH_BASE:-https://huggingface.co/datasets/skyan1002/flash-flood-benchmark-data/resolve/main}" CACHE="${FFBENCH_CACHE:-${TMPDIR:-/tmp}/ffbench_manifest.jsonl}" have_jq() { command -v jq >/dev/null 2>&1; } get_manifest() { [ -f "$CACHE" ] || curl -sL "$BASE/manifest.jsonl" -o "$CACHE"; } LEVEL=l3; STATE=""; START=""; END=""; STRICT=""; SELECTED=""; RPMIN=""; KIND="all"; OUT="./ffbench_out"; CMD="${1:-help}"; [ $# -gt 0 ] && shift || true; ARG="" while [ $# -gt 0 ]; do case "$1" in --level) LEVEL=$2; shift 2;; --state) STATE=$2; shift 2;; --start) START=$2; shift 2;; --end) END=$2; shift 2;; --strict) STRICT=1; shift;; --selected) SELECTED=1; shift;; --rp-min) RPMIN=$2; shift 2;; --kind) KIND=$2; shift 2;; --out) OUT=$2; shift 2;; -*) echo "unknown flag $1" >&2; exit 2;; *) ARG=$1; shift;; esac; done build_filter() { f=".level==\"$(echo "$LEVEL"|tr a-z A-Z)\"" [ -n "$STATE" ] && f="$f and .primary_state_abbrev==\"$STATE\"" [ -n "$START" ] && f="$f and .begin_date>=\"$START\"" [ -n "$END" ] && f="$f and .begin_date<=\"$END\"" [ -n "$STRICT" ] && f="$f and .is_strict_flash_flood==true" [ -n "$SELECTED" ] && f="$f and .selected==true" [ -n "$RPMIN" ] && f="$f and .lp3_return_period_yr>=$RPMIN" echo "$f" } case "$CMD" in query) get_manifest; have_jq || { echo "query needs jq" >&2; exit 3; } jq -c "select(.record_type==\"record\" and $(build_filter)) | {testbed_id,ff_episode_id,primary_state_abbrev,begin_date,lp3_return_period_yr,flash_flood_class}" "$CACHE";; download) get_manifest; have_jq || { echo "download needs jq" >&2; exit 3; } mkdir -p "$OUT" kf='true'; [ "$KIND" != all ] && kf=".artifact_kind==\"$KIND\"" jq -r "select(.record_type==\"artifact\" and $(build_filter) and $kf).resolve_url" "$CACHE" \ | while read -r u; do (cd "$OUT" && curl -L -C - -O "$u"); done;; get) [ -n "$ARG" ] || { echo "get needs TESTBED_ID" >&2; exit 2; } get_manifest; mkdir -p "$OUT" if have_jq; then urls=$(jq -r "select(.testbed_id==\"$ARG\" and .record_type==\"artifact\").resolve_url" "$CACHE"); else urls=$(grep "\"testbed_id\":\"$ARG\"" "$CACHE" | grep '"record_type":"artifact"' | grep -o '"resolve_url":"[^"]*"' | cut -d'"' -f4); fi echo "$urls" | while read -r u; do [ -n "$u" ] && (cd "$OUT" && curl -L -C - -O "$u"); done;; bundle) case "$ARG" in selected26) z=testbeds_selected26.zip;; strict152) z=testbeds_strict152.zip;; *) echo "bundle: selected26|strict152" >&2; exit 2;; esac curl -L -C - -O "$BASE/bundles/$z";; help|*) sed -n '2,6p' "$0"; echo "commands: query|download|get TESTBED_ID|bundle {selected26|strict152}" echo "flags: --level l1|l2|l3 --state XX --start YYYY-MM-DD --end YYYY-MM-DD --strict --selected --rp-min N --kind streamflow|watershed|package_zip|all --out DIR";; esac