#!/bin/sh
#########################################################################
# Usage: ofs_seasforce3.sh                                         #
#                                                                       #
# Description: This script copies the GFS/GDAS flux files and           #
#              interpolate them to generates the seashore forcing files #
#              for the Ocean Forecast Model                             #
#                                                                       #
# History:                                                              #
#    06-16-2005  Dan Iredell                                            #
#    06-01-2006  Ilya Rivin: add sea level pressure forcing             #
#    09-30-2009  Dan Iredell - version for hwrf                         #
#    03-24-2010  Dan Iredell - overhaul for pressure from master        #
#########################################################################

set -x

echo "*** Started script $0"

if test $# -ne 2
then
  echo Usage: $0 startdate enddate
  exit
fi

sh ${utilscript}/setup.sh

date1=$1
date2=$2
sea_lev_pres=PRMSL

# 
# anal gets fluxes from gdas, which is hourly and pressure field from pgrb
# fcst get fluxes from gfs, is 3-hourly and pressure field from master
#

echo " GETGES_COM=${GETGES_COM}"

ihours=0
if [ $mode = 'anal' ]
then
  ihours=1
  global=gdas
fi
if [ $mode = 'fcst' ]
then
  ihours=3
  global=gfs
fi
if [ $ihours = 0 ]
then
  echo invalid mode $mode
  export err=40; err_chk
fi

nm=`$utilexec/nhour $date2 $date1`
let nm=nm/ihours+3
echo $nm > listflx.dat
rdate1=`$utilexec/ndate -$ihours $date1`
rdate2=`$utilexec/ndate $ihours $date2`
datei=$rdate1

while [[ $datei -le $rdate2 ]]
do
  pf1=`$USHofs/ofs_getges.sh -q -e $envir_getges -n $global -t mastgb -v $datei`
  ggrc=$?
  if [ $ggrc -ne 0 ]
  then
     echo prmsl not available in master at $datei, get from pgb file
     pf1=`$USHofs/ofs_getges.sh -q -e $envir_getges -n $global -t pgbges -v $datei`
  fi
  pgfile=`basename $pf1`
  pgfile=$datei.$pgfile
  cp $pf1 $pgfile
  ${utilexec}/grbindex $pgfile $pgfile.idx
  rec_number=`${utilexec}/wgrib -v ${pgfile} | grep ${sea_lev_pres} | cut -d: -f1`
  ${utilexec}/wgrib -d ${rec_number} -grib ${pgfile} -o dump0.grb
  ${utilexec}/grbindex dump0.grb dump0.idx

  sf=`$USHofs/ofs_getges.sh -q -e $envir_getges -n $global -t sfcflx -v $datei`
  flxfile=`basename $sf`
  flxfile=$datei.$flxfile
  cp $sf $flxfile
  ${utilexec}/grbindex $flxfile $flxfile.idx
          export GRBFILE=$flxfile
          export IDXFILE=$flxfile.idx
          $EXECofs/ofs_getkpds >>$pgmout 2>errfile
          export err=$?; err_chk
          atmgds='255 '`cat kpds.dat`
         export err=$?; err_chk

  ${utilexec}/copygb -g"$atmgds" -x -a -i0 dump0.grb $flxfile

 unset atmgds
 rm -r kpds.dat

  rm $flxfile.idx
  ${utilexec}/grbindex $flxfile $flxfile.idx

  echo $datei $flxfile $pgfile $sf $pf1 >>listflx.dat
  datei=`$utilexec/ndate $ihours $datei`
done

#################################################################
# Interpolate to OFS grid
#################################################################
#- UNIT  7  - FILE intp_pars.dat, COTROL RUN PARAMETRS
#- UNIT  8  - FILE regional.grid.b, DESCRIPTOR FOR HYCOM GRID
#-            (READ IN mod_geom.f90)
#- UNIT  9  - FILE regional.grid.a, HYCOM GRID
#-            (READ IN mod_geom.f90)
#- UNIT 33  - FILE listflx.dat, LIST OF DATES AND MRF FLUS FILES TO
#-            BE USED IN INTERPOLATION.
#- UNIT 59  - FILE regional.depth.a, HYCOM BATHIMETRY
#-            (READ IN mod_geom.f90)
#- UNIT 61  - FILE regional.mask.a, HYCOM mask
#-            (READ IN mod_geom.f90)
#- UNIT 81  - MRF GRIBBED FLUXES FILES WITH THE NAMES FROM THE LIST
#-            SPECIFIED IN listflx.dat.
#- UNIT 82  - THE SAME
#################################################################
pgm=ofs_gfs2ofs
. prep_step

msg=" `date`  -- $pgm for $adate started "
postmsg "$jlogfile" "$msg"

export XLFRTEOPTS="unit_vars=yes:buffering=disable_all"
export XLFUNIT_7=intp_pars.dat
export XLFUNIT_8=regional.grid.b
export XLFUNIT_9=regional.grid.a
export XLFUNIT_33=listflx.dat
export XLFUNIT_59=regional.depth.a
export XLFUNIT_61=regional.mask.a

$EXECofs/ofs_gfs2ofs >>$pgmout 2>errfile
export err=$?; err_chk

# End of generating the forcing files

echo "*** Finished script $0"
