#!/bin/sh
###############################################################################
#                                                                             #
# Script Name: estofs_grib2.sh                                                #
#                                                                             #
# Technical Contact(s): Name: Yuji Funakoshi       Org: NOS/OCS/CSDL/MMAP     #
#                       Phone: 301-7132809 ext.113                            #
#                       E-Mail: yuji.funakoshi@noaa.gov                       #
#                                                                             #
# Abstract:                                                                   #
# This script is used to interpolate three water level results into           #
# NDFD conus and puerto rico grid during simulation                           #
#                                                                             #
# Usage: ./estofs_grib2.sh "conus" or "puertori" and "ifile" "jfile"          #
#                                                                             #
# Input Parameters:                                                           #
#                                                                             #
# Language:   Bourne Shell Script                                             #
#                                                                             #
# Target Computer: CIRRUS/STRATUS at NCEP                                     #
#                                                                             #
# Estimated Execution Time: < 20 minutes                                      #
#                                                                             #
# Suboutines/Functions Called:                                                #
# Name               Directory Location            Description                #
# estofs_grib2       /exec                         compute ndfd grid          #
#                                                  interpolation              #
#                                                                             #
# Input Files:                                                                #
# Name               Directory Location            Description                #
# grib2.ctl          /work                         grib2 control file         #
# YMDH.modID.cwl     /work                         CWL in fields              #
# YMDH.modID.htp     /work                         HTP in fields              #
# YMDH.modID.swl     /work                         SWL in fields              #
#                                                                             #
# Output Files:                                                               #
# Name               Directory Location            Description                #
# RUN.modID.cycle.conus.cwl.fcsth.grib2 /work      CWL in NDFD conus          #
# RUN.modID.cycle.conus.htp.fcsth.grib2 /work      HTP in NDFD conus          #
# RUN.modID.cycle.conus.swl.fcsth.grib2 /work      SWL in NDFD conus          #
# RUN.modID.cycle.puertori.cwl.fcsth.grib2 /work   CWL in NDFD Puerto Rico    #
# RUN.modID.cycle.puertori.htp.fcsth.grib2 /work   HTP in NDFD Puerto Rico    #
# RUN.modID.cycle.puertori.swl.fcsth.grib2 /work   SWL in NDFD Puerto Rico    #
#                                                                             #
# Libraries Used: see the makefile                                            #
#                                                                             #
# Author Name: Yuji Funakoshi                      Creation Date: Dec, 2011   #
#                                                                             #
# Revisions:                                                                  #
# Date               Author                        Description                #
#                                                                             #
# Remarks:                                                                    #
#                                                                             #
###############################################################################
set -x
# Start of estofs_grib2.sh script-------------------------------------------- #
# 1.  Set times, grid type, and loops

  export date=$PDY
  export YMDH=${PDY}${cyc}

  grid_type=$1
  ifile=1
  jfile=186

# --------------------------------------------------------------------------- #
# 2. Copy grid and mask files 

  grid_file=${RUN}.${modID}.fort.14

  if [ ${grid_type} == conus ] 
  then
    mask_file=$FIXestofs/${RUN}_${modID}_conus.mask
  else
    mask_file=$FIXestofs/${RUN}_${modID}_puertori.mask
  fi

# --------------------------------------------------------------------------- #
# 3. Create input file for combined water level

  rm -f grib2.ctl grib2.log
  area=${grid_type}
  wl_type=cwl
  echo $area >> grib2.ctl
  echo $wl_type >> grib2.ctl
  echo $YMDH >> grib2.ctl
  echo $grid_file >> grib2.ctl
  echo $mask_file >> grib2.ctl
  cwl_file=${YMDH}.${modID}.${wl_type}
  echo $cwl_file >> grib2.ctl

# 3.a Execute estofs_grib2 

  export pgm="estofs_grib2"
  prep_step
  startmsg
  echo "Interpolating CWL into $grid_type grid"
  $EXECestofs/estofs_grib2 < grib2.ctl >> grib2.log 2>errfile
  export err=$?; err_chk

  istep=$ifile

  while [ $istep -le $jfile ]
  do
    if [ $istep -ge 6 ]
    then
      icsth=$(($istep-6))
      if [ $icsth -le 9 ]
      then
        cp fort.100${icsth} ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f00${icsth}.grib2
        cat ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f00${icsth}.grib2 >>${RUN}.${modID}.${cycle}.${grid_type}.f00${icsth}.grib2
      elif [ $icsth -gt 9 -a $icsth -le 99 ]
      then
        cp fort.10${icsth} ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f0${icsth}.grib2
        cat ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f0${icsth}.grib2 >>${RUN}.${modID}.${cycle}.${grid_type}.f0${icsth}.grib2
      elif [ $icsth -gt 99 ]
      then
        cp fort.1${icsth} ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f${icsth}.grib2
        cat ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f${icsth}.grib2 >>${RUN}.${modID}.${cycle}.${grid_type}.f${icsth}.grib2
      fi
    fi
    istep=$(($istep+1))
  done
  rm -f fort.1*

# --------------------------------------------------------------------------- #
# 4. Create input file for harmonic tidal prediction 

  rm -f grib2.ctl grib2.log
  area=${grid_type}
  wl_type=htp
  echo $area >> grib2.ctl
  echo $wl_type >> grib2.ctl
  echo $YMDH >> grib2.ctl
  echo $grid_file >> grib2.ctl
  echo $mask_file >> grib2.ctl
  htp_file=${YMDH}.${modID}.${wl_type}
  echo $htp_file >> grib2.ctl

# 4.a Execute estofs_grib2
 

  export pgm="estofs_grib2"
  prep_step
  startmsg
  echo "Interpolating HTP into $grid_type grid"
  $EXECestofs/estofs_grib2 < grib2.ctl >> grib2.log 2>errfile
  export err=$?; err_chk

  istep=$ifile

  while [ $istep -le $jfile ]
  do
    if [ $istep -ge 6 ]
    then
      icsth=$(($istep-6))
      if [ $icsth -le 9 ]
      then
        cp fort.100${icsth} ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f00${icsth}.grib2
        cat ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f00${icsth}.grib2 >>${RUN}.${modID}.${cycle}.${grid_type}.f00${icsth}.grib2
      elif [ $icsth -gt 9 -a $icsth -le 99 ]
      then
        cp fort.10${icsth} ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f0${icsth}.grib2
        cat ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f0${icsth}.grib2 >>${RUN}.${modID}.${cycle}.${grid_type}.f0${icsth}.grib2
      elif [ $icsth -gt 99 ]
      then
        cp fort.1${icsth} ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f${icsth}.grib2
        cat ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f${icsth}.grib2 >>${RUN}.${modID}.${cycle}.${grid_type}.f${icsth}.grib2
      fi
    fi
    istep=$(($istep+1))
  done
  rm -f fort.1*

# --------------------------------------------------------------------------- #
# 5. Create input file for sub-tidal water level 

  rm -f grib2.ctl grib2.log
  area=${grid_type}
  wl_type=swl
  echo $area >> grib2.ctl
  echo $wl_type >> grib2.ctl
  echo $YMDH >> grib2.ctl
  echo $grid_file >> grib2.ctl
  echo $mask_file >> grib2.ctl
  swl_file=${YMDH}.${modID}.${wl_type}
  echo $swl_file >> grib2.ctl

# 5.a Execute estofs_grib2 

  export pgm="estofs_grib2"
  prep_step
  startmsg
  echo "Interpolating SWL into $grid_type grid"
  $EXECestofs/estofs_grib2 < grib2.ctl >> grib2.log 2>errfile
  export err=$?; err_chk

  istep=$ifile

  while [ $istep -le $jfile ]
  do
    if [ $istep -ge 6 ]
    then
      icsth=$(($istep-6))
      if [ $icsth -le 9 ]
      then
        cp fort.100${icsth} ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f00${icsth}.grib2
        cat ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f00${icsth}.grib2 >>${RUN}.${modID}.${cycle}.${grid_type}.f00${icsth}.grib2
      elif [ $icsth -gt 9 -a $icsth -le 99 ]
      then
        cp fort.10${icsth} ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f0${icsth}.grib2
        cat ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f0${icsth}.grib2 >>${RUN}.${modID}.${cycle}.${grid_type}.f0${icsth}.grib2
      elif [ $icsth -gt 99 ]
      then
        cp fort.1${icsth} ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f${icsth}.grib2
        cat ${RUN}.${modID}.${cycle}.${grid_type}.${wl_type}.f${icsth}.grib2 >>${RUN}.${modID}.${cycle}.${grid_type}.f${icsth}.grib2
      fi
    fi
    istep=$(($istep+1))
  done
  rm -f fort.1*

# End of estofs_grib2.sh script ---------------------------------------------- #
