#!/bin/bash
# Before downloading; the perturb it should be well prepared at ECMWF's c2a in the
# context of cfu's infrastructure (use "prepare_atm_perturb.cmd" over there first).
# nohup ./download_atm_perturb.sh T255L62 b0ga >& download.log &
#
set -xuve
date

grd=$1
ver=$2

src=ec:/c3m/perturb/atmos/$grd/$ver
dst=/cfu/releases/perturb/atmos/$grd/$ver

mkdir -p $dst
list=atm-${grd}-${ver}

if [[ ! -a $list ]]; then
   ecaccess-file-dir $src > $list
fi

for l in $(cat $list); do
 ecaccess-file-get $src/$l $dst/$l
 if [[ $? -ne 0 ]]; then
    exit 1
 fi
 cat $list | sed -e '1d' > ${list}.tmp
 mv ${list}.tmp $list
done
cat $list; rm $list

date
