#!/bin/bash
#
#PBS -N SMCr50init 
#PBS -q shared
#PBS -l select=1:ncpus=1
#PBS -l walltime=00:08:00
#PBS -j oe

# #PBS -q parallel
# #PBS -l select=1:ncpus=32

##-----------------------------------------------------------------------------
#
# Description:  Cray XC40 script to prepare forcing files for smc24816 model
# 
# First Created:    Jian-Guo Li   2 Feb 2009
# Last modified:    Jian-Guo Li  20 Nov 2015
#------------------------------------------------------------------------------

######################### Set up directories ##################################
 RUNDIR='/home/d02/frjl/WW3Run'
 WW3DIR='/data/d02/frjl/WW3Vn5'
 WINDIR='/data/d02/frjl/Wind25km'
 WW3rst='/data/d02/frjl/G50SMCrst'
 SCRDIR="/scratch/d02/frjl"

######################### Set date ############################################
 cd $RUNDIR 
#obsdate=`cut -c1-13  date_glb`
 obsdate=`cut -c1-8   date_glb`
  yymmdd=`cut -c3-8   date_glb`
 echo " obsdate set as $obsdate"
 echo " yymmdd set as $yymmdd"

##  Year Month Day Hour for wind namelist input
    Year=`cut -c1-4  date_glb`
   Month=`cut -c5-6  date_glb`
     Day=`cut -c7-8  date_glb`
    Hour='00'
#   Hour=`cut -c10-11 date_glb`
#   Hour=`expr $mdhr - 6 `


################# Set mandatory WW3 environment variables #####################
 export WW3_STRT_INP=ww3_strt
 export WW3_SHEL_INP=ww3_shel
 export WW3_GRID_INP=ww3_grid
 export WW3_MOD_DEF=mod_def
 export WW3_WIND=wind
 export WW3_ICE=ice
 export WW3_RESTART=restart
 export WW3_LOG=log
 export WW3_NEST=nest
 export WW3_OUT_GRD=out_grd
 export WW3_OUT_PNT=out_pnt

############### Change to scratch directory for this run ######################
 cd $SCRDIR
 CC=$?
 if test $CC -ne 0
 then
  mkdir $SCRDIR
  cd    $SCRDIR
 fi

 echo "Clearing working dir `pwd`"
 rm -f $SCRDIR/*

####### Copy executable and input files to scratch dir #######################
echo "Copy executables and input files ..."
cp $WW3DIR/exearc/ww3_shel        ./
cp $WW3DIR/exearc/ww3_strt        ./
cp $WW3DIR/exearc/ww3_outp        ./
cp $WW3DIR/exearc/ww3_outf        ./
cp $WW3DIR/exearc/ff2ww           ./
cp $WW3DIR/inps50/mod_def.ww3     ./

ls -l

###################### Select input files ####################################
##  Insert the correct dates in the ww3_shel.inp file.
##  Use the mid-run date $obsdate as a reference point.
  START_TIME=`rose date  -s-00h -f"%Y%m%d %H%M00"  ${obsdate}T000006`
##  One day run
    END_TIME=`rose date  -s+24h -f"%Y%m%d %H%M00"  ${obsdate}T000006`
RESTART_TIME="${END_TIME}"

echo "START TIME IS   ${START_TIME}"
echo "END TIME IS     ${END_TIME}"
echo "RESTART TIME IS ${RESTART_TIME}"
cat  $WW3DIR/inps50/ww3_shel.inp.template  \
     | sed "s/<START_TIME>/${START_TIME}/g" \
     | sed "s/<END_TIME>/${END_TIME}/g" \
     | sed "s/<RESTART_TIME>/${RESTART_TIME}/g" > ww3_shel.inp

##  Restart file from saved copy
  rstfile=restart`rose date -s -0h -f"%Y%m%d%H"  ${obsdate}T000006`

if [[ ! -f $WW3rst/${rstfile} ]]; then
     echo "WARNING! No restart file available for $restfile SMC6W run." 
      cp  $WW3DIR/inps50/ww3_strt.inp   ./
else
   echo " Restart file to be used is"
   ls -t  $WW3rst/${rstfile}
   cp     $WW3rst/${rstfile}  ./restart.ww3
fi

##  Get a copy of date_glb in working directory for mrun
 echo ${obsdate} >              ./date_glb

# 5. Insert the correct dates in the ww3_outp.inp file.
echo "START TIME IS   ${START_TIME}"
cat  $WW3DIR/inps50/ww3_outp.inp.template  \
     | sed "s/<START_TIME>/${START_TIME}/g" > ww3_outp.inp

# 6. Insert the correct dates in the ww3_outf.inp file.
cat  $WW3DIR/inps50/ww3_outf.inp.template  \
     | sed "s/<START_TIME>/${START_TIME}/g" > ww3_outf.inp

 ls -lt

##  Use saved 25km wind forcing with date stamp.  23 Aug 2010
 DATAWND=$WINDIR/$Year$Month$Day
 DATEMRK=$Year$Month$Day

#if test $Hour -lt 12
#   then
 WINDFILE1=$DATAWND/${DATEMRK}_qwqu00.marine0_10m
 WINDFILE2=$DATAWND/${DATEMRK}_qwqu06.marine0_10m
#else
#WINDFILE1=$DATAWND/qwqu12.marine0_10m_$DATEMRK
#WINDFILE2=$DATAWND/qwqu18.marine0_10m_$DATEMRK
 WINDFILE3=$DATAWND/${DATEMRK}_qwqu12.marine0_10m
 WINDFILE4=$DATAWND/${DATEMRK}_qwqu18.marine0_10m
#fi

## Use saved ice file in the same directory 
  ICEFILE=$DATAWND/qwgl.daily.ice

################ Create input parameter files ff2ww ###########################
# -0.41670 -79.16664  0.83333333   0.55555556
# 1024  688
# 1024  720
# 0.17578125  -78.63281250  0.35156250  0.23437500 
cat >> f2wndinput << ENDHK
WND
 512    368
 0.3515625  -82.265625   0.7031250   0.4687500
 0.0   90.0
 3225  3226
 $START_TIME 3600 25
$WINDFILE1
$WINDFILE2
$WINDFILE3
$WINDFILE4
ENDHK

cat >> f2iceinput << ENDHK
ICE
 512    368
 0.3515625  -82.265625   0.7031250   0.4687500
 0.0   90.0
 31    -1
 -1    -1   3600  1
$ICEFILE
ENDHK

################ Convert wind and ice files to ww format ######################
 echo " *** ice *** "

 ./ff2ww  f2iceinput  ice.ww3

#
  CC=$?
  if test $CC -ne 0
  then
    echo " *** ICE Crash !!! *** "
  else
##  Save converted ice.ww3 file
  echo " The converted ice.ww3 file ..."
  ls -l ice.ww3
  fi

 
 echo " *** wind *** "
 
 ./ff2ww  f2wndinput  wind.ww3

#
  CC=$?
  if test $CC -ne 0
  then
    echo " *** WND Crash !!! *** "
  else
##  Save converted wind.ww3 file
  echo " The converted wind.ww3 file ..."
  ls -l wind.ww3
  fi


#######################################################################
# Appended script to run the NAEWW3, adapted from Chris' run.sh
#               JGLi  29 Feb 2008
# Grabs the relavent wind file from the operational suite
# and creates an ww3_shel.inp file with the right dates in.
#
# Submit GLBWW3 run script into queue:
 cd $RUNDIR 
 qsub   smcrv50kmrun
 echo " smcrv50kmrun submit-ed. "

 echo " *** END *** "

exit 0

