#!/bin/sh
###############################################################################
####  UNIX Script Documentation Block                                         #
#                                                                             #
# Script name:         exrtofs_et_WPAb_run.sh.sms                             #
# Script description:                                                         #
#                                                                             #
# Author:        Dan Iredell       Org: NP23         Date: 2005-06-14         #
#                                                                             #
# Abstract: This script generates the analysis field and creates the initial  #
#           condition for the RTOFS_ET_WPAb ocean tracer model. Original      # 
#           version was scripted by Dan and tracer version was coded by       #
#           Hae-Cheol Kim Jan. 2012                                           #
#                                                                             #
# Sub-scripts called:                                                         #
#    GLBa0.08_2_RR_PDYm1.com : create the subregioned archives                #
#    GLBa0.08_2_RR_PDY.com : create the subregioned archives                  #
#    subregion_force.com : create the subregioned forcing field               # 
#                                                                             #
# Script history log:                                                         #
# 2005-06-14  Dan Iredell                                                     #
# 2012-01-05  Hae-Cheol Kim (modified for RTOFS_ET_WPAb)                      #
###############################################################################
set -xa

export PS4='$SECONDS + '

# --------------------------------------------------------------------------- #
# Set up working and nested environment

# workdir is $DATA
export workdir=$DATA

# make sure it is a clean dir -- until checkpoint restart is in effect

if [ "$(ls -A $workdir)" ]; then
  #echo "Take action $workdir is not Empty"
  #rm -rf $workdir
  mkdir -p $workdir/nest
else
    echo "$workdir is Empty"
fi

# end the makesure ........
cd $workdir

# --------------------------------------------------------------------------- #
# set up hycom environment

msg="RTOFS_ET_WPAb Analysis has begun on `hostname` at `date`"
postmsg "$jlogfile" "$msg"

# --------------------------------------------------------------------------- #
# 0.  Preparations
# --------------------------------------------------------------------------- #

# 0.a date and time stuff

  export nested=${nested:-0}
  if [ ${RUN_MODE} = 'analysis' ]
  then
    export srtdate=`echo $PDYm1 | cut -c1-8`
    export enddate=`echo $PDY | cut -c1-8`
  elif [ ${RUN_MODE} = 'forecast' ]
  then
    export srtdate=`echo $PDY | cut -c1-8`
    export enddate=`echo $PDYp1 | cut -c1-8`
  else
    echo 'ERROR: wrong RUN_MODE '${RUN_MODE}
    exit -2
  fi

  export inputgrid=${inputgrid:-ncep1_12}

# --------------------------------------------------------------------------- #
# 1  Set up the start time and end time for the forecast (daily initialization)
  sday=`sh $utilscript/date_normal2hycom.sh ${srtdate}00`
  eday=`sh $utilscript/date_normal2hycom.sh ${enddate}00`
  echo "  $sday $eday  " > limits

# 2. Copy the necessary input files for the model now/forecast

# 2.a Get topography (depth, grid and mask)
  ln -s -f $FIXofs/${RUN}_${modID}.regional.depth.a regional.depth.a
  ln -s -f $FIXofs/${RUN}_${modID}.regional.depth.b regional.depth.b 
  ln -s -f $FIXofs/${RUN}_${modID}.regional.grid.a regional.grid.a
  ln -s -f $FIXofs/${RUN}_${modID}.regional.grid.b regional.grid.b


  # Check to make sure all the topography files are copied:
  for fil in depth grid 
  do
     for type in a b
     do
        if [ -s regional.$fil.$type ]
        then
           echo "regional.${fil}.${type} copied" 
        else
           msg="ABNORMAL EXIT NOWCAST: NO FILE for regional.${fil}.${type}"
           postmsg "$jlogfile" "$msg"
           echo $msg
           err=2;
           export err;err_chk
        fi
     done
  done


# 2.b Get boundary conditions (relax files)

# new for WPAb:
# no horizontal relaxation to climatology but relaxation to nesting archives with rmu
# but still for surface salinity relaxation we need relax.[intf,saln,temp].[ab]
# Also, relax.trcr.[ab] is needed for surface relaxtion of tracer (for initialization with all zeros) 
# there is no lateral nudging in tracer (so, relax=0, trcrlx=0) 

    ln -s -f $FIXofs/${RUN}_${modID}_relax.intf.a $DATA/relax.intf.a
    ln -s -f $FIXofs/${RUN}_${modID}_relax.intf.b $DATA/relax.intf.b
    ln -s -f $FIXofs/${RUN}_${modID}_relax.temp.a $DATA/relax.temp.a 
    ln -s -f $FIXofs/${RUN}_${modID}_relax.temp.b $DATA/relax.temp.b 
    ln -s -f $FIXofs/${RUN}_${modID}_relax.saln.a $DATA/relax.saln.a 
    ln -s -f $FIXofs/${RUN}_${modID}_relax.saln.b $DATA/relax.saln.b 
    filreq='intf temp saln'

# Get parm files
  cp $PARMofs/${RUN}_${modID}_blkdat.input $DATA/blkdat.input
  cp $PARMofs/${RUN}_${modID}_ports.input $DATA/ports.input
  cp $PARMofs/${RUN}_${modID}_patch.input $DATA/patch.input

  for fil in blkdat ports patch
  do
     if [ -s ${fil}.input ]
     then
        echo "${fil}.input copied"
     else
        msg1="Missing Initialization File"
        msg2="ABNORMAL EXIT EPISODIC TRACER FORECAST: NO FILE for ${fil}.input"
        postmsg "$jlogfile" "$msg1" 
        err=2;
        export err;err_chk
     fi
  done

# subsetting forcings 
  if [ ${RUN_MODE} = 'analysis' ]
  then
  csh $USHofs/${RUN}_${modID}_subregion_force.sh $COMbigIN anal
  else
  csh $USHofs/${RUN}_${modID}_subregion_force.sh $COMbigIN fcst1
  fi
 
# subretioning global archives 
  if [ ${RUN_MODE} = 'analysis' ]
  then
    csh $USHofs/${RUN}_${modID}_GLBa0.08_2_RR.sh $PDYm1 $COMbigINm1 n00
    csh $USHofs/${RUN}_${modID}_GLBa0.08_2_RR.sh $PDY $COMbigIN n00
  else
    # RUN_MODE=forecast
    csh $USHofs/${RUN}_${modID}_GLBa0.08_2_RR.sh $PDY $COMbigIN f24
    csh $USHofs/${RUN}_${modID}_GLBa0.08_2_RR.sh $PDYp1 $COMbigIN f24
  fi

# copy executable
  cp $EXECcode/${RUN}_${modID}_forecast .

# nest (relaxation mask for GLB archives at boundaries)
ln -s -f $FIXofs/${RUN}_${modID}_rmu.a $DATA/nest/rmu.a
ln -s -f $FIXofs/${RUN}_${modID}_rmu.b $DATA/nest/rmu.b

# copy fix files
ln -s $FIXofs/${RUN}_${modID}_forcing.kpar.a $DATA/forcing.kpar.a
ln -s $FIXofs/${RUN}_${modID}_forcing.kpar.b $DATA/forcing.kpar.b
ln -s $FIXofs/${RUN}_${modID}_forcing.offlux.a $DATA/forcing.offlux.a
ln -s $FIXofs/${RUN}_${modID}_forcing.offlux.b $DATA/forcing.offlux.b
ln -s $FIXofs/${RUN}_${modID}_forcing.rivers.a $DATA/forcing.rivers.a
ln -s $FIXofs/${RUN}_${modID}_forcing.rivers.b $DATA/forcing.rivers.b
ln -s $FIXofs/${RUN}_${modID}_relax.ssh.a $DATA/relax.ssh.a
ln -s $FIXofs/${RUN}_${modID}_relax.ssh.b $DATA/relax.ssh.b
ln -s $FIXofs/${RUN}_${modID}_veldf2.a $DATA/veldf2.a
ln -s $FIXofs/${RUN}_${modID}_veldf2.b $DATA/veldf2.b
ln -s $FIXofs/${RUN}_${modID}_thkdf4.a $DATA/thkdf4.a
ln -s $FIXofs/${RUN}_${modID}_thkdf4.b $DATA/thkdf4.b


  for fil in $filreq
  do
     for type in a b
     do
        if [ -s relax.${fil}.${type} ]
        then
           echo "relax.${fil}.${type} copied" 
        else
           msg="ABNORMAL EXIT NOWCAST: NO FILE for relax.${fil}.${type}"
           postmsg "$jlogfile" "$msg"
           echo $msg
           err=2;
           export err;err_chk
        fi
     done
  done


# 3. Get initial conditions (restart files)

       yesterday_dir=
       today_dir=$COMIN
       if [ ${RUN_MODE} = 'analysis' ] 
       then
         restart_dir=$COMINm1
       else 
         restart_dir=$COMIN
       fi

# 3.b After then, restart files will be picked up from ${yesterday_dir}
    ln -s -f ${restart_dir}/${RUN}_${modID}.t${cyc}z.next_restart.a restart_in.a
    ln -s -f ${restart_dir}/${RUN}_${modID}.t${cyc}z.next_restart.b restart_in.b


# 4. Prepare for the analysis run

  export OMP_NUM_THREADS=2
  export XLSMPOPTS=spins=0:yields=0
  
  touch ok
  rm -f ok
  date >> TRACK

  cmdtype='poe'

  export MP_LABELIO=YES
  export MP_STDOUTMODE=ordered
  export MEMORY_AFFINITY=MCM

  export MP_COREFILE_FORMAT=txt

 poe $EXECcode/${RUN}_${modID}_forecast -procs $NPROCS >> $pgmout 2>errfile

  export err=$?; err_chk

  date >> TRACK

# --------------------------------------------------------------------------- #
# 5.c  Check for errors

if [ -e ok ]; then
   modelstatus=0
else
   modelstatus=1
fi

# --------------------------------------------------------------------------- #
# 6.  Ending output
#
# Write restart files copying commands in CMD
#
if [ ${RUN_MODE} = 'analysis' ]
then
  cp restart_out.a $COMOUT/${RUN}_${modID}.t${cyc}z.next_restart.a 
  cp restart_out.b $COMOUT/${RUN}_${modID}.t${cyc}z.next_restart.b 
fi

   if [ $SENDCOM = YES ]
   then
      ${USHofs}/${RUN}_${modID}_tmp2com.sh
       echo "done" >$COMOUT/${RUN}_${modID}.t${cyc}z.log
       msg="6. Analysis job completed normally"
       postmsg "$jlogfile" "$msg"
       exit 0
   fi

#################################################
msg="THE RTOFS_ET_WPAb_ANALYSIS JOB HAS ENDED NORMALLY."
postmsg "$jlogfile" "$msg"
################## END OF SCRIPT #######################
