#!/bin/bash
#
# nohup ./data_transfer.sh >& expid.log &
#

##################################
####  User Defined Variables  #### 
##################################

# User may change
MODEL=ecearth
dst1=/cfunas/exp/$MODEL # store logfiles, MM*
dst2=/cfunas/exp/$MODEL/restartfiles # store REST*
dst3=/cfunas/exp/$MODEL/gribfiles # store ICM*
dst4=/cfunas/exp/$MODEL/cmorfiles # store CMOR*
types="logfiles MM PP diags REST restart ICM CMOR"
remove=TRUE # remove output from source by default (if user want to kee the model output at HPC make it FALSE)

# User must fill-in
arch="" # choose platform (ithaca, mn-*, cfs-aux-4, ecmwf, ht-*, ar-*)
expid="" # supply expid: e.g. xxxx
sdates="" # supply list of start dates: e.g. 19601101 19651101 ...
members="" # supply list of members: e.g. fc0 fc1 fc2 ...

#####  End of User Defined Variables  ####

#################################
####  User Defined Funtions  #### 
#################################

function transfer1(){
 set -e
 dst=$1/$expid/$s/$m/$2
 if [[ $remove == TRUE ]]; then
  RSYNC="rsync -acv --remove-sent-files"
 else
  RSYNC="rsync -acv"
 fi
 for l in $list; do
  echo "tranfering file ... ${arch}:$l $dst"
  $RSYNC ${arch}:${l} $dst
  echo "done."
  echo
  echo
 done
 set +e
}

function transfer2(){
 dst=$1/$expid/$s/$m/$2
 for l in $list; do
  echo "tranfering file ... $src/$expid/$s/$m/$2/$l $dst/$l"
  ecaccess-file-get $src/$expid/$s/$m/$2/$l $dst/$l
  if [[ $? -eq 0 && $remove == TRUE ]]; then
   ecaccess-file-delete -force $src/$expid/$s/$m/$2/$l
  fi
  echo "done."
  echo
  echo
 done
}

####  End of the User Defined Functions  #### 

###################################
####  Main Part of the Script  ####
###################################

date
echo "platform is: $arch"
echo "expid is: $expid"
echo "start dates: $sdates"
echo "members per start date: $members"

for s in $sdates; do
 for m in $members; do

  rmtlist="ssh $arch ls -1"
  transfer="transfer1"

  case $arch in
   ithaca)
    src=/share/data/cfu/exp
   ;;
   mn-*)
    src=/gpfs/scratch/ecm86/ecm86010/exp
   ;;
   cfs-aux-4)
    src=/cfs/klemming/nobackup/a/asifsami/exp
   ;;
   ecmwf)
    export EcaccessWebtoolkitJava=/cfu/software/ecaccess-java
    export PATH=$EcaccessWebtoolkitJava/bin:${PATH}
    rmtlist="ecaccess-file-dir"
    transfer="transfer2"
    src=ec:/c3m/exp
   ;;
   ht-*)
    src=/work/pr1u1011/pr1u1011/pr1e1001/exp
   ;;
   ar-*)
    src=/work/pr1u1011/pr1u1011/shared/exp
   ;;
   *)
    echo "$arch is not a valid machine"
   ;;
  esac

  for typ in $types; do
   case $typ in
    logfiles)
     lswc=`$rmtlist $src/$expid/$s/$m/logfiles* | wc -l`
     if [[ $lswc -gt 0 ]]; then
      mkdir -p $dst1/$expid/$s/$m
      list=`$rmtlist $src/$expid/$s/$m/logfiles*`
      $transfer $dst1 ""
     fi
    ;;
    MM|PP|diags)
     lswc=`$rmtlist $src/$expid/$s/$m/outputs/${typ}* | wc -l`
     if [[ $lswc -gt 0 ]]; then
      mkdir -p $dst1/$expid/$s/$m/outputs
      list=`$rmtlist $src/$expid/$s/$m/outputs/${typ}*`
      $transfer $dst1 outputs
     fi
    ;;
    REST|restart)
     lswc=`$rmtlist $src/$expid/$s/$m/restarts/${typ}* | wc -l`
     if [[ $lswc -gt 0 ]]; then
      mkdir -p $dst2/$expid/$s/$m/restarts
      list=`$rmtlist $src/$expid/$s/$m/restarts/${typ}*`
      $transfer $dst2 restarts
     fi
    ;;
    ICM)
     lswc=`$rmtlist $src/$expid/$s/$m/outputs/ICM* | wc -l`
     if [[ $lswc -gt 0 ]]; then
      mkdir -p $dst3/$expid/$s/$m/outputs
      list=`$rmtlist $src/$expid/$s/$m/outputs/ICM*`
      $transfer $dst3 outputs
     fi
    ;;
    CMOR)
     lswc=`$rmtlist $src/$expid/$s/$m/outputs/CMOR* | wc -l`
     if [[ $lswc -gt 0 ]]; then
      mkdir -p $dst4/$expid/$s/$m/outputs
      list=`$rmtlist $src/$expid/$s/$m/outputs/CMOR*`
      $transfer $dst4 outputs
     fi
    ;;
    *)
     echo "$typ is not a valid data type"
    ;;
   esac
  done

 done
done
date

####  End of the Main Part of Script  ####
