#!/bin/sh

########################################
# Preliminary data setup step
########################################

set -xa
# #### 06/25/2003 ###################
# SET SHELL PROCESSING VARIABLES
# ###################################
export PS4='$SECONDS + '
date

####################################
#
# obtain unique process id (pid) and make temp directories
#
####################################
echo cyc is ${cyc:?}
echo envir is ${envir:?}
echo seaice_drift_ver is ${seaice_drift_ver:?} #ensure code_ver is defined
export pid=$$
export cycle=t${cyc}z

if [ "$envir" = 'prod' ] || [ "$envir" = 'para' ] || [ "$envir" = 'test' ] ; then
  #NCO
  export BASEDIR=${DATAROOT:-/lfs/f1/ops/$envir/tmp/}
else
  #developer
  export BASEDIR=/lfs/h2/emc/ptmp/wx21rg/
fi

#Change to:
export DATA=$BASEDIR/${jobid}

#change from 
#export DATA=$BASEDIR/${job}.${pid}
mkdir -p $DATA
cd $DATA
export TMPDIR=$DATA
echo zzz basedir data tmpdir: $BASEDIR $DATA $TMPDIR

####################################
# File To Log Msgs
####################################
if [ "$envir" = 'prod' ] || [ "$envir" = 'para' ] || [ "$envir" = 'test' ] ; then
  #NCO
  if [ "$envir" = 'prod' ] ; then
    export jlogfile=""
  else
    export jlogfile=""
  fi
else
  #developer
  export jlogfile=$HOME/jlogfile
fi


####################################
# Determine Job Output Name on System
####################################
export outid="LL$job"
export jobid="${outid}.o${pid}"
export pgmout="OUTPUT.${pid}"
export pgmerr=errfile

####################################
# variables that control processing
# Determine variable settings before testing and set them accordingly
#    SENDDBN=YES        file will be sent to OSO
#    SENDCOM=YES        copy files from TMPDIR to $COMOUT
#    SENDECF=YES        Flag Events on ECF
####################################
export SENDCOM=YES
export SENDECF=YES

if [ "$envir" = "prod" ] ; then
  export SENDDBN=YES
  export SENDDBN_GB2=YES
  export SENDDBN_SUFFIX=
else
  if [ $envir = "para" ] ; then
    export SENDDBN=YES
    export SENDDBN_GB2=YES
    export SENDDBN_SUFFIX=_PARA
  else
    export SENDDBN=NO
    export SENDDBN_GB2=NO
    export SENDDBN_SUFFIX=_TEST
  fi
fi

export NET=seaice_drift
export RUN=seaice_drift

#################################################
# Specify job's execution areas (directory paths)
#################################################

#Path to home directory:
if [ "$envir" = 'prod' ] || [ "$envir" = 'para' ] || [ "$envir" = 'test' ] ; then
  #NCO
  export HOMEbase=${HOMEseaice_drift}
else
  #dev
  #export HOMEbase=${HOMEbase:-/u/robert.grumbine/rgdev/drift}
  export HOMEbase=/u/robert.grumbine/rgdev/seaice_drift.${seaice_drift_ver}/

fi

export HOMEsice=$HOMEbase
export EXECsice=$HOMEsice/exec
export FIXsice=$HOMEsice/fix
export PARMsice=$HOMEsice/parm
export USHsice=$HOMEsice/ush

###################################
# Set up the UTILITIES
###################################
#Requires prod_util module to be established
export utilscript=$UTILROOT/ush
export utilexec=$UTILROOT/exec
export utilparm=$UTILROOT/parm

##############################
# Run setpdy and initialize PDY variables
##############################
#export PDY=20121022
sh $utilscript/setpdy.sh
. ./PDY

##############################################
# Define COM directories
##############################################
if [ "$envir" = 'prod' ] || [ "$envir" = 'para' ] || [ "$envir" = 'test' ] ; then
  export COMbase=`compath.py ${NET}/${seaice_drift_ver?"version number missing"}`
  err=$? err_chk
  export COMIN=${COMIN:-$(compath.py naefs/${naefs_ver}/gefs.${PDY}/$cyc/pgrb2a_bc)} 

  err=$? err_chk
  export COMINice_analy=${COMINice_analy:-$(compath.py seaice_analysis/${seaice_analysis_ver}/seaice_analysis.${PDYm1})}
  err=$? err_chk

else
  #Developer
  export COMbase=${HOME}/noscrub/com/${NET}/${envir}/
  export pcom=${HOME}/noscrub/pcom/$RUN/

  # For NRT runs
  #development: export COMIN=${COMIN:-$(compath.py -e prod /naefs/${naefs_ver}/gefs.${PDY})/$cyc/pgrb2ap5_bc}

  export COMIN=${COMIN:-$(compath.py -e prod /naefs/${naefs_ver}/gefs.${PDY})/$cyc/pgrb2a_bc}
  #export COMINice_analy=${COMROOT}/seaice_analysis/${seaice_analysis_ver}/seaice_analysis.${PDYm1}
  export COMINice_analy=$(compath.py -e prod /seaice_analysis/${seaice_analysis_ver}/seaice_analysis.${PDYm1})
  echo zzz cominice_analy = $COMINice_analy

  #for python:
  export PYTHONPATH=$PYTHONPATH:$HOME/rgdev/ice_scoring/sidfex/:$HOME/rgdev/mmablib/py/

fi

if [ ! -d $COMIN ] ; then
  err_exit Could not find the input directory $COMIN exiting
fi
export COMOUT=$COMbase/${RUN}.${PDY}
export pcom=${COMOUT}/wmo

mkdir -m 775 -p $COMOUT
mkdir -m 775 -p $pcom

msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

env

##############################################
# Execute the script
set -xe

sh $HOMEsice/scripts/exseaice_drift.sh
export err=$?
err_chk
##############################################

cat $pgmout

msg="ENDED NORMALLY."
postmsg "$jlogfile" "$msg"

##############################
date

cd $BASEDIR
##############################################
# Remove temp directories
##############################################
if [ "$KEEPDATA" != "YES" ] ; then
  rm -rf $DATA
fi