#!/bin/bash
#SBATCH -n 1
#SBATCH -t 10-12:00:00
#SBATCH -J retrieveORAP5
#SBATCH --mem=60000
#SBATCH -o retrieveORAP5-%j.log
 

set -evx
TMPDIR=/scratch/$(whoami)/$$
mkdir -p $TMPDIR
cd $TMPDIR

module load ECAC

DATASOURCE="ec:/RDX/prepIFS/g0tx" # Root of the path where to find ORAS4 on ECFS
YEARI=2000 # First year to download
YEARF=2013 # Last year to download
MEMBER_LST="0" # List of members to download
YEAR=$YEARI 

workdir=`pwd`
mkdir -p $workdir
cd $workdir

echo $YEAR
for year in `seq $YEARI $YEARF`; do
mkdir -p /esnas/reconstructions/ecmwf/orap5/restarts/$YEAR
  for mem in $MEMBER_LST ; do
    for file in $( ecaccess-file-dir ${DATASOURCE}/opa${mem}/restart/${YEAR}/g0tx_${YEAR}*_*_restart.tar.gz )
    do
      if [[ "$file" != "ECFS" ]];then
        echo $file
        if [[ ! -f /esnas/reconstructions/ecmwf/orap5/restarts/${YEAR}/$file ]]; then
          while [[ ! -f /esnas/reconstructions/ecmwf/orap5/restarts/${YEAR}/$file ]]
          do
            ecaccess-file-get -progress ${DATASOURCE}/opa${mem}/restart/${YEAR}/$file $file
            mv $file /esnas/reconstructions/ecmwf/orap5/restarts/${YEAR}/.
          done
        fi
      fi
    done
  done
  YEAR=$((YEAR+1))
done
