#!/bin/bash
#
# Code to download all the instantaneous data from the
# ERCOFTAC KB WIKI
#
# Arnau Miro, Oriol Lehmkuhl

# 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/3
EXT=h5

# Some printing
printf "Requested dataset: ${DATASET} @ start instant=${START}, end instant=${END}, interval=${DT}\n"

# Create output directory
mkdir -p ${DEST}

# Loop all the requested instants
for i in $(seq $START $DT $END); do
	printf "Downloading instant ${i}...\n"
	# Build variables for the download
	FILE=${DATASET}_${i}.${EXT}
	URL_TO_DOWNLOAD=${URL}/${CASE}/${FILE}
	DEST_FILE=${DEST}/${FILE}
	# Download with curl
	curl ${URL_TO_DOWNLOAD} --progress-bar --output ${DEST_FILE}
#	printf " done!\n"
done
