#!/bin/sh
###############################################################################
#                                                                             #
# Script Name: estofs_surface_forcing.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 program is used to read GFS operational products of grib2 files        #
# to generated surface wind forcing file.                                     #
# The wind vector is rotated to earth coordinates.                            #
# The missing variables are filled with a missing value of -99999.0.          #
#                                                                             #
# Usage: ./estofs_surface_forcing.sh $ymdh                                    #
#                                                                             #
# Input Parameters:                                                           #
#                                                                             #
# Language:   Bourne Shell Script                                             #
#                                                                             #
# Target Computer: CIRRUS/STRATUS at NCEP                                     #
#                                                                             #
# Estimated Execution Time: < 30 minutes                                      #
#                                                                             #
# Suboutines/Functions Called:                                                #
# Name               Directory Location            Description                #
# estofs_surface     /exec                         create GFS surface forcing #
# _forcing                                                                    #                                         
#                                                                             #
# Input Files:                                                                #
# Name               Directory Location            Description                #
# surface_forcing.ctl/work                         surface_frocing control    #
#                                                  file                       #
#                                                                             #
# Output Files:                                                               #
# Name               Directory Location            Description                #
# YMDH.modID.for.22  /work                         Surface forcing input      #
#                                                                             #
# Libraries Used: see the makefile                                            #
#                                                                             #
# Authour by: Yuji Funakoshi                       Creation Date: Dec, 2011   #
#                                                                             #
# Revisions:                                                                  #
# Date               Author                        Description                #
#                                                                             #
# Remarks:                                                                    #
#                                                                             #
###############################################################################
set -x
# Start of estofs_surface_forcing.sh script --------------------------------- #
# 1.  Set times

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

  ymdh=$1

# 1.a Create temporary directory for GFS files 

  rm -rf gfsinc_$1 
  mkdir gfsinc_$1 
  cd gfsinc_$1
  $USHutil/setup.sh

# --------------------------------------------------------------------------- #
# 2.  Prepare GFS interpolation
# 2.a Define GFS variables

  npp=3
  varname[1]='PRMSL'
  varname[2]='UGRD'
  varname[3]='VGRD'
  lev[1]=':mean sea level:'
  lev[2]=':10 m above ground:'
  lev[3]=':10 m above ground:'

# 2.b Run getges.sh for nowcast or copy from GFS directory for forecast

  if [ $ymdh -le $YMDH ]
  then
    export pgm="getges.sh"
    $GETGES -t pg2ges -v $ymdh -n gfs > getges.out 2> getges.err
    export err=$?; err_chk

    spec_file=`cat getges.out | awk '{ print $1 }'`
    rm -f getges.out getges.err
    specfile_ready=yes

  else

    fcsth=`$NHOUR $ymdh $YMDH`
    spec_file=$COMGFS/gfs.${cycle}.pgrb2f$fcsth
    file_size=`ls -l $spec_file | awk '{print $5}'`

    if [ -f $COMGFS/gfs.${cycle}.pgrb2f$fcsth ] && [ $file_size -gt 40000000 ]
    then
      specfile_ready=yes
    else
      specfile_ready=no
    fi
  fi

# --------------------------------------------------------------------------- #
# 3.  Copy GFS grib2 files and extract vairables 

  if [ -f $spec_file ] && [ $specfile_ready = yes ]
  then
    cp $spec_file swnd.$ymdh
    count=0
    while (( count < $npp ))
    do
      (( count = count + 1 ))
      $WGRIB2 swnd.$ymdh -s | grep "${varname[count]}${lev[count]}" | $WGRIB2 -i swnd.$ymdh -spread tmp.txt
      if [ -s tmp.txt ]
      then
        cat tmp.txt > ${ymdh}.${modID}.${varname[count]}.gfs
      fi	  
    done
    rm -f tmp.txt
   fi

# --------------------------------------------------------------------------- #
# 4.  Execute estofs_surface_forcing.sh 
# 4.a Preparing input files

  cd ..
  rm -f surface_forcing.ctl surface_forcing.log
  gfs_file=$FIXestofs/${RUN}_${modID}_gfs.tmpl
  grid_file=${RUN}.${modID}.fort.14
  prmsl_file=gfsinc_$1/${ymdh}.${modID}.PRMSL.gfs
  ugrd_file=gfsinc_$1/${ymdh}.${modID}.UGRD.gfs
  vgrd_file=gfsinc_$1/${ymdh}.${modID}.VGRD.gfs

  echo $gfs_file >> surface_forcing.ctl
  echo $grid_file >> surface_forcing.ctl
  echo $prmsl_file >> surface_forcing.ctl
  echo $ugrd_file >> surface_forcing.ctl
  echo $vgrd_file >> surface_forcing.ctl

# 4.b Executing estofs_surface_forcing.sh 

  export pgm="estofs_surface_forcing"
  prep_step; startmsg
  $EXECestofs/estofs_surface_forcing < surface_forcing.ctl > surface_forcing.log 2>errfile
  export err=$?; err_chk

# --------------------------------------------------------------------------- #
# 5.  Clean up the directory 

  rm -rf gfsinc_$ymdh

# End of estofs_surface foricng.sh script ----------------------------------- #
