#!/bin/sh
###############################################################################
####  UNIX Script Documentation Block                                         #
#                                                                             #
# Script name:         exofs_atl_forecast.sh.sms                              #
# Script description:                                                         #
#                                                                             #
# Author:        Dan Iredell       Org: NP23         Date: 2005-06-14         #
#                                                                             #
# Abstract: This script generates the forecast field and creates the initial  #
#           condition for the OFS_ATL Ocean model                             #
#                                                                             #
# Sub-scripts called:                                                         #
#    seasforce.sh : create the forcing field                                 #
#                                                                             #
# Script history log:                                                         #
# 2005-06-14  Dan Iredell                                                     #
#                                                                             #
###############################################################################
set -xa

export PS4='$SECONDS + '

cd $DATA

msg="OFS_ATL Forecast has begun on `hostname` at `date`"
postmsg "$jlogfile" "$msg"

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

export mode=fcst
# 0.a date and time stuff

  export nested=${nested:-0}
  export srtdate=`echo $PDY | cut -c1-8`
  export enddate=${forecast_end:-$PDYp6}
  export inputgrid=${inputgrid:-ncep1_12}

# --------------------------------------------------------------------------- #
# 1  Set up the start time and end time for the forecast
  sday=`sh $utilscript/date_normal2hycom.sh $srtdate$cyc`
  eday=`sh $utilscript/date_normal2hycom.sh $enddate$cyc`
  echo "  $sday $eday false false  " > limits

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

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

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

  # 2.aa Get open boundary files

  if [ $nested -eq 0 ]
  then
  ln -s -f $PARMofs/ofs_${modID}.lowfreq.input lowfreq.input

  $USHofs/ofs_lowfreq_mean.sh
 
  # Check to make sure all the open boundary files are copied:
  if [ -s lowfreq.input ]   
  then                                                                    
      echo "LOWR files for open boundaries copied"                        
  else                                                                    
      msg="ABNORMAL EXIT FORECAST: FATAL ERROR--NO LOWFREQ FILE"             
      postmsg "$jlogfile" "$msg"
      echo $msg
      err=911;                                                            
      export err;err_chk                                                  
  fi
  fi

# 2.b Get boundary conditions (relax files)

  if [ $nested -eq 0 ]
  then

    ln -s -f $FIXofs/ofs_${modID}.$inputgrid.relax.intf.a relax.intf.a
    ln -s -f $FIXofs/ofs_${modID}.$inputgrid.relax.intf.b relax.intf.b
    ln -s -f $FIXofs/ofs_${modID}.$inputgrid.relax.rmu.a relax.rmu.a
    ln -s -f $FIXofs/ofs_${modID}.$inputgrid.relax.rmu.b relax.rmu.b
    ln -s -f $FIXofs/ofs_${modID}.$inputgrid.relax.temp.a relax.temp.a
    ln -s -f $FIXofs/ofs_${modID}.$inputgrid.relax.temp.b relax.temp.b
    ln -s -f $FIXofs/ofs_${modID}.$inputgrid.relax.saln.a relax.saln.a
    ln -s -f $FIXofs/ofs_${modID}.$inputgrid.relax.saln.b relax.saln.b
    filreq='intf rmu temp saln'

  else
    ln -s -f $FIXofs/ofs_${modID}.$inputgrid.relax.rmu.a relax.rmu.a
    ln -s -f $FIXofs/ofs_${modID}.$inputgrid.relax.rmu.b relax.rmu.b
    filreq='rmu'
# need files for nesting.. cp from yesterday to ./nest directory
    mkdir -p nest
    hrs=`${utilexec}/nhour ${enddate}${cyc} ${srtdate}${cyc}`
    let dys=hrs/24
    iday=0
    tday=$srtdate
    while [ $iday -lt $dys ]
    do
      yyyyddd=`$utilscript/date2jday.sh $tday`
      yyyy=`echo $yyyyddd | cut -c1-4`
      ddd0=`echo $yyyyddd | cut -c5-7`
      for hour in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
      do
         cp $COMINm1/${RUN}_${modID}.t${cyc}z.${yyyy}_${ddd0}_${hour}.a ./nest/archv_${yyyy}_${ddd0}_${hour}.a
         cp $COMINm1/${RUN}_${modID}.t${cyc}z.${yyyy}_${ddd0}_${hour}.b ./nest/archv_${yyyy}_${ddd0}_${hour}.b
      done
      let iday=iday+1
      tday=`$utilexec/ndate +24 ${tday}00 |cut -c1-8`
    done
# and get last 00z file 
    yyyyddd=`$utilscript/date2jday.sh $tday`
    yyyy=`echo $yyyyddd | cut -c1-4`
    ddd0=`echo $yyyyddd | cut -c5-7`
    cp $COMINm1/${RUN}_${modID}.t${cyc}z.${yyyy}_${ddd0}_00.a ./nest/archv_${yyyy}_${ddd0}_00.a
    cp $COMINm1/${RUN}_${modID}.t${cyc}z.${yyyy}_${ddd0}_00.b ./nest/archv_${yyyy}_${ddd0}_00.b

  fi


  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 FORECAST: NO FILE for relax.${fil}.${type}"
           postmsg "$jlogfile" "$msg"
           echo $msg
           err=2;
           export err;err_chk
        fi
     done
  done


# 2.c Get initialization files 
  if [ $nested -eq 0 ]
  then
  cp $PARMofs/ofs_${modID}.fcst.blkdat.input blkdat.input
  cp $PARMofs/ofs_${modID}.ports.input ports.input
  cp $PARMofs/ofs_${modID}.$inputgrid.patch.input.$NPROCS patch.input
  cp $PARMofs/ofs_${modID}.angle.input angle.input
  cp $PARMofs/ofs_${modID}.tidalports_p.input tidalports_p.input
  cp $PARMofs/ofs_${modID}.tidalports_vel.input tidalports_vel.input
  filreq='blkdat ports patch angle tidalports_p tidalports_vel'
  else
  cp $PARMofs/ofs_${modID}.fcst.blkdat.input blkdat.input
  cp $PARMofs/ofs_${modID}.ports.input ports.input
  cp $PARMofs/ofs_${modID}.$inputgrid.patch.input.$NPROCS patch.input
  cp $PARMofs/ofs_${modID}.angle.input angle.input
  filreq='blkdat ports patch angle'
  fi

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

# 2.d Get initial conditions (restart files)
  rm -rf restart_in.a restart_in.b

  if [ $RESTART = YES ]
  then
     fcst_hour=`${utilexec}/nhour ${enddate}${cyc} ${PDY}${cyc}`
     while [ $fcst_hour -ge 0 ]
     do       
       if [ -s $GESdir/ofs_${modID}.t00z.restart_f${fcst_hour}.b ]
       then
         # find most recent forecast restart
         ln -s -f $GESdir/ofs_${modID}.t00z.restart_f${fcst_hour}.b restart_in.b
         ln -s -f $GESdir/ofs_${modID}.t00z.restart_f${fcst_hour}.a restart_in.a

       # replace limits file
         sday=`sh $utilscript/date_normal2hycom.sh \` ${utilexec}/ndate +$fcst_hour ${PDY}${cyc} \` `
         echo "  $sday $eday false false  " > limits
         break

       fi

       fcst_hour=`expr $fcst_hour - 1`
     done
     if [ $fcst_hour -lt 0 ]
     then
        msg="ABNORMAL EXIT FORECAST: NO FILE for restart_in.[ab]"
        postmsg "$jlogfile" "$msg"
        echo $msg
        err=2;
        export err;err_chk
     fi 

  else 
     if [ $mode = anal ]
     then
        restart_dir=$COMINm1
     else
        restart_dir=$COMIN
     fi

     ln -s -f $restart_dir/ofs_${modID}.t${cyc}z.next_restart.a restart_in.a
     ln -s -f $restart_dir/ofs_${modID}.t${cyc}z.next_restart.b restart_in.b
  
  fi

  if [ -s restart_in.a -a restart_in.b ]
  then
      echo "Initial restart files copied"
  else
      msg="ABNORMAL EXIT FORECAST: FATAL ERROR--NO RESTART FILE"
      postmsg "$jlogfile" "$msg"
      echo $msg
      err=911;
      export err;err_chk
  fi

# 2.e copy interpolator data
  intvl=${intvl:-3}
  psource=${psource:-1} # if =1, use master file for surface pressure, if =0, then pgrb file
  if [ $intvl -eq 3 ]
  then
    cp $FIXofs/ofs_${modID}.intp_pars.dat intp_pars.dat
  else

cat > intp_pars.dat << EOF
&intp_pars
avstep = 1.,      ! averaging fluxes (in hours)
mrffreq = 1.,     ! frequency of MRF fluxes (in hours)  = mrffreq for no averaging
flxflg = 5,       ! Type of HYCOM input (=4 => nhycom=7 and =5 => nhycom=8)
dbgn = 1,         ! debugging =0 - no dbg; =1,2,3 - add output
avg3 = 2,         ! if avg3 = 1, then averaged fluxes are converted to instantaneous fields
wslocal = 1       ! if  wslocal = 1, then wind stress are computed from wind velcoities
/
EOF

  fi

  if [[ -s intp_pars.dat ]]
  then
      echo "interpolator data copied"
  else
      msg="ABNORMAL EXIT FORECAST: NO FILE for intp_pars.dat"
      postmsg "$jlogfile" "$msg"
      echo $msg
      err=2;
      export err;err_chk
  fi

# 2.f Get parameter data for seasforce

  cp $FIXofs/ofs_${modID}.ismus_msk384x190.dat ismus_msk384x190.dat  
  cp $FIXofs/ofs_${modID}.ismus_msk512x256.dat ismus_msk512x256.dat  
  cp $FIXofs/ofs_${modID}.ismus_msk768x384.dat ismus_msk768x384.dat  
  cp $FIXofs/ofs_${modID}.ismus_msk1152x576.dat ismus_msk1152x576.dat  
  cp $FIXofs/ofs_${modID}.ismus_msk1760x880.dat ismus_msk1760x880.dat

  for rel in 384x190 512x256 768x384 1152x576 1760x880
  do
     if [ -s ismus_msk${rel}.dat ]
     then
        echo "File ismus_msk${rel}.dat copied"
     else
        msg="ABNORMAL EXIT FORECAST: NO FILE for ismus_msk${rel}.dat"
        postmsg "$jlogfile" "$msg"
        echo $msg
        err=2;
        export err;err_chk
     fi
  done

# 2.g Get river forcing files

  ln -s -f $FIXofs/ofs_${modID}.$inputgrid.forcing.rivers.a forcing.rivers.a
  ln -s -f $FIXofs/ofs_${modID}.$inputgrid.forcing.rivers.b forcing.rivers.b

  if [ -s forcing.rivers.a -a forcing.rivers.b ]
  then
      echo "River Forcing Files copied"
  else
      msg="ABNORMAL EXIT FORECAST: NO FILE for forcing.rivers.a or .b "
      postmsg "$jlogfile" "$msg"
      echo $msg
      err=911;
      export err;err_chk
  fi

# 3. Create Forcing files

# 3.a call seasforce3 to get the forcing files 

  intvl=3
  $USHofs/ofs_seasforce3.sh $srtdate$cyc $enddate$cyc ## $intvl $psource

# 3.b correct air temperature over ice.

  $USHofs/ofs_correct_forcing.sh 

# 4. Prepare for the forecast 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 $EXECofs/ofs_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

  if [ $modelstatus = 0 ]
  then
     if [ $SENDCOM = YES ]
     then

      # Copy probes to /com
       for prb in `ls probe_out_*`
       do
         if [ -s $COMOUT/${RUN}_${modID}.t${cyc}z.${prb} ]
         then
            cat $prb >> $COMOUT/${RUN}_${modID}.t${cyc}z.${prb}
         else
            cp -p $prb $COMOUT/${RUN}_${modID}.t${cyc}z.${prb}
         fi
       done

      # copy lowfreq.out to /com
       cp -p lowfreq.out $COMOUT/${RUN}_${modID}.t${cyc}z.fcst.lowfreq.out
       cp lowfreq.input $COMOUT/${RUN}_${modID}.t${cyc}z.fcst.lowfreq.input
       cp lowfreq_mean.txt $COMOUT/${RUN}_${modID}.t${cyc}z.fcst.lowfreq_mean.txt              

      # copy list of forcing files to /com
       cp -p  listflx.dat $COMOUT/${RUN}_${modID}.t${cyc}z.fcst.listflx.dat
       echo "done" >$COMOUT/${RUN}_${modID}.t${cyc}z.fcst.log
       msg="6. Forecast job completed normally"
       postmsg "$jlogfile" "$msg"
       exit 0
     fi
  else
# failed .. if by neg dp then can reduce baclin to get run through
# suggest new number....
    baclin=`grep \'baclin\' blkdat.input|cut -f1 -d"'"`
    batrop=`grep \'batrop\' blkdat.input|cut -f1 -d"'"`
    baclini=`echo $baclin|cut -d. -f1`
    case $baclini in
     240) newbaclin=120.0;;
      80) newbaclin=60.0;;
      60) newbaclin=40.0;;
      40) newbaclin=30.0;;
      30) newbaclin=20.0;;
      *) newbaclin="a lower number";;
    esac
    msg="ABNORMAL EXIT FORECAST: Try modifying baclin from $baclin to ${newbaclin} and rerunning "
    postmsg "$jlogfile" "$msg"
    echo $msg
    err=13;
    export err;err_chk
  fi

#################################################
msg='THE OFS_ATL_FORECAST JOB HAS ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

################## END OF SCRIPT #######################

