#!/bin/ksh 

#SBATCH -J ext
# #SBATCH --qos=long
#SBATCH --qos=long
#SBATCH -o /scratch/ms/pt/ptb/LOGS/%x.%j.%N.out
#SBATCH -e /scratch/ms/pt/ptb/LOGS/%x.%j.%N.out
#SBATCH --mail-type=FAIL

## Arguments
# $1 - type, ei, ea, od 
# $2 - dstart 
# $3 - dend 
# $4 - expver
# $5 - dtype (DAY or MON) extraction 

set -exu

SBASE='/home/ms/pt/ptb/work/extract_forcing/' # base directory with scripts
DSTORAGE="ec:/ptb/OSM_FORCING/"             # default to archive files 
#DSTORAGE="$SCRATCH/tmp/OSM_FORCING/"

export PATH="${PATH}:$SBASE"
echo "HOSTNAME - $HOSTNAME $1 $2 $3"

ttype=$1 #ei


if [[ $ttype == ea ]]; then

  # sbatch main_extract.ksh ea 197901 201712 1 MON # for monthly 
  # TL639 hourly, 152MB per day 
  # TL639 hourly, 5.1 Gb per month 
  dstart=$2 #197901  20120101 
  dend=$3 # 201612   20160101
  EXPVER=${4:-xxx}
  dtype=${5:-"DAY"}
  FREQ=1
  CLASS=ea
  STREAM=oper
  LAPSE=false
  FCLEN=0
  EFOR=false
  export LEXTRACT_ML=true
  export LEXTRACT_SFC=true
  export LEXTRACT_LNSP=true
  export FORCINGLEV=0 
fi

##=======================================
##
if [[ $ttype == ei ]]; then
  # nohup ./main_extract.ksh ei 197901 201812 1>out.ei 2>&1 &
  # sbatch main_extract.ksh ei 197901 201812 
  # 280 MB per month (37 year 121 GB)
  dstart=$2 #197901
  dend=$3 #201612
  dtype="MON"
  EXPVER=1
  FREQ=3
  CLASS=ei
  STREAM=oper
  LAPSE=false
  FCLEN=0
  EFOR=false
  export LEXTRACT_ML=true
  export LEXTRACT_LNSP=true
  export LEXTRACT_SFC=true
  export FORCINGLEV=0 
fi



dd=$dstart
while [[ $dd -le $dend ]]; 
do
  if [[ $EFOR = true ]]; then
    edd=${dd}
    ndd=$( dateincr -h ${dd} +$dincr)
  else
    if [[ $dtype = MON ]]; then
      edd=${dd}00
      ndd=$( dateincr ${dd}01 +33 | cut -c1-6 )
    else
      edd=$dd
      ndd=$( dateincr ${dd} +1)
    fi
  fi 
  time driver_extract.ksh -e $edd -i $EXPVER -f $FREQ -c $CLASS -s $STREAM -l $LAPSE -x $DSTORAGE \
      -b $EFOR -z $FCLEN
  if [[ $? -ne 0 ]] ;then 
    echo "Error extracting"
    exit -1
  fi 
  dd=$ndd
done
