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

grd=$1
ver=$2

src=ec:/c3m/ic/atmos/$grd/$ver
dst=/cfu/releases/ic/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
