#!/bin/bash

#----------------------------------------------------------
# Bash script to download all instantaneous data from the
# ERCOFTAC KB Wiki for the DNS-1/9 case
#
# File Created: March 10, 2026
# R. Montalà (UPC), O. Lehmkuhl (BSC) and I. Rodriguez (UPC)
#-----------------------------------------------------------

# Command line parameters
DATASET=$1
START=${2:-0}
END=${3:-925000}
DT=${4:-25000}
DEST=${5:-.}

# Variables
URL=https://kbwiki-data.s3-eu-west-2.amazonaws.com
CASE=DNS-1/9

# Print requested data
printf "Requested slice: ${DATASET} @ initial iteration=${START}, final iteration=${END}, iteration step=${DT}\n"

# Parse DATASET
AXIS=$(echo "$DATASET" | cut -d'/' -f1)
VALUE=$(echo "$DATASET" | cut -d'=' -f2)

# Build slice prefix
PREFIX="slice_${AXIS}slice_${VALUE}"

# Create output directory
mkdir -p "${DEST}/${PREFIX}"

# Loop all requested instants
for i in $(seq "$START" "$DT" $((END + DT))); do

    printf "Downloading iteration ${i}...\n"

    FILE="${PREFIX}/${PREFIX}-${i}-vtk.hdf"
    URL_TO_DOWNLOAD="${URL}/${CASE}/${FILE}"
    DEST_FILE="${DEST}/${FILE}"

    curl "${URL_TO_DOWNLOAD}" --progress-bar --output "${DEST_FILE}"

done