#!/bin/bash
# One fine day, download JRA-55 for fun :)
HOST='ds.data.jma.go.jp'
USER='jra03024'	
PASSWD='L0YutltW'
WRKDIR='/work/users/longtt/JRA55'
# Parameter for download
#case='anl_'
case='fcst_'
#para="column125 p125 surf125"
#para="surf125"
para="phy2m125"
styr=1958
enyr=2012
stmn=1
enmn=12
# Create download.sh file
#set -xv
cat << END2 > download.sh
ftp -n $HOST << SCRIPTEND
user jra03024 L0YutltW
bin
prompt off
END2
# Put loop paragraph to download.sh
for p in $para
do
  cd $WRKDIR
  mkdir -p ${case}${p}
  echo "cd JRA-55/Hist/Monthly/${case}${p}" >> download.sh
  echo "lcd $WRKDIR/${case}${p}" >> download.sh
  for (( y=$styr ; y<=$enyr ; y++))
  do
    for (( m=$stmn ; m<=$enmn ; m++))
    do
      mon=$m
      if [[ $mon -lt 10 ]] ; then
        mon=0$mon
      fi
      echo "get ${case}${p}.${y}${mon}" >> download.sh
    done
  done
done
echo "bye" >> download.sh
echo "SCRIPTEND" >> download.sh
# Run download.sh
chmod +x download.sh
./download.sh
