#!/bin/sh
#
# ofs_windforcing.sh 
#    computes the parameterized winds based on gfs winds and tcvitals.
#    calls :
#       ofs_fwind - produces a set of wind-speed & wind-stress components from tcvitals
#       ofs_winds2hycom - horizontal interpolation and rotation of the winds
#       ofs_correct_wind - modifies GFS surface wind and wind stress in forcing.tau[en]wd1.[ab]   
#
set -x

echo "*** Started script $0"

# 1. run fwind

# determine ranges of region
# min and max lat
aline=`grep ulat  regional.grid.b`
ltmn=`echo $aline|cut -d" " -f4`
ltmx=`echo $aline|cut -d" " -f5`
iltmn=`echo $ltmn|cut -d. -f1`
iltmx=`echo $ltmx|cut -d. -f1`
let latmin=iltmn-1
let latmax=iltmx+1

# min and max lon
aline=`grep ulon  regional.grid.b`
lnmn=`echo $aline|cut -d" " -f4`
lnmx=`echo $aline|cut -d" " -f5`
ilnmn=`echo $lnmn|cut -d. -f1`
ilnmx=`echo $lnmx|cut -d. -f1`
let lonmin=ilnmn-1
let lonmax=ilnmx+1

# create track for this hurricane (modified for non-real time tests)
#grep ${STORMID} ${VITAL_DATA}/syndat_tcvitals.${YYYY} | $PRE_SORTVIT/gfdl_pre_ocean_sortvit.sh > track
#echo \
#"000  000 000000    000000 0000 000  000 000  00  000 0000  000 00  00 0000 0000 0000 0000 0000 0000 0000 0000"\
#>> track
##   yymmdd=`echo $YYYYMMDDHH | cut -c3-8`
##   hh=`echo $YYYYMMDDHH | cut -c9-10`
##   row=`grep -n "${yymmdd} ${hh}" track.$$ | tail -1 | cut -d: -f1`
##   head -$row track.$$ > track


   grep ${STORMID} ${VITAL_DATA}/syndat_tcvitals.${YYYY} | $PRE_SORTVIT/gfdl_pre_ocean_sortvit.sh > track.$$
   mv track.$$ track

   echo \
   "000  000 000000    000000 0000 000  000 000  00  000 0000  000 00  00 0000 0000 0000 0000 0000 0000 0000 0000"\
   >> track

# adjust fwind time of focus to line up with gfs flux files (so need tcvitals from 2 cycles previous)
# need better way to set fdate!

spinlength=${spinlength:-6}
let fdateoffset=spinlength*-1
fdate=`${utilexec}/ndate $fdateoffset $YYYYMMDDHH`
fdate=`echo $fdate | cut -c3-10`
# need to change for partial days
diffhours=`${utilexec}/nhour ${enddate} ${srtdate}`
nday=`expr $diffhours / 24`
typeset -Z3 residual=`expr $diffhours % 24 \* 1000 / 24`
ndays=${nday}.${residual}
echo $ndays
datfile=$STORM.xy.dat
binfile=fwind4d.$STORM.bin

cat > fwind.in <<EOF
track
$fdate
$intvl
$ndays
$datfile
$binfile
$latmin
$latmax
$lonmin
$lonmax
EOF

$EXEChwrf/hwrf_ofs_fwind < fwind.in >> $pgmout 2> errfile
fwindrc=$?
if [ $fwindrc -ne 0 ]
then
  export err=$fwindrc;err_chk
else
  echo done with ofs_fwind
fi


# 2. run wind2hycom

binfname=frc4hycom.bin

cat > wind2hycom.in << EOF
$binfname
$binfile
$datfile
EOF

$EXEChwrf/hwrf_ofs_wind2hycom < wind2hycom.in >> $pgmout 2> errfile
wind2hycomrc=$?
if [ $wind2hycomrc -ne 0 ]
then
  export err=$wind2hycomrc;err_chk
else
  echo done with ofs_wind2hycom
fi


# 3. run correctwind

# wind speed old and renamed files
export A1="forcing.wndspd.a"
export oA1="forcing.wdspd1.a"
export B1="forcing.wndspd.b"
export oB1="forcing.wdspd1.b"

# tau-x old and renamed files
export A2="forcing.tauewd.a";
export oA2="forcing.tauew1.a";
export B2="forcing.tauewd.b"
export oB2="forcing.tauew1.b"

# tau-y old and renamed files
export A3="forcing.taunwd.a";
export oA3="forcing.taunw1.a";
export B3="forcing.taunwd.b"
export oB3="forcing.taunw1.b"

# specs of B1
blines1=`cat ${B1} | wc -l`
nframes1=`grep wndspd ${B1} | wc -l`
ihead1=`expr ${blines1} \- ${nframes1} `
labela1=`grep span ${B1} | head -1 |cut -c1-10`

#specs of B2
blines2=`cat ${B2} | wc -l`
nframes2=`grep tauewd ${B2} | wc -l`
ihead2=`expr ${blines2} \- ${nframes2} `
labela2=`grep span ${B2} | head -1 | cut -c1-10`

#specs of B3
blines3=`cat ${B3} | wc -l`
nframes3=`grep taunwd ${B3} | wc -l`
ihead3=`expr ${blines3} \- ${nframes3} `
labela3=`grep span ${B3} | head -1 | cut -c1-10`

# number of records in datfile
nrcds=`head -1 ${datfile} | cut -c1-5`

cat > correct_wind.in <<EOF
$binfname
$oB1,$oB2,$oB3
$oA1,$oA2,$oA3
$B1,$B2,$B3
$ihead1
$labela1,$labela2,$labela3
$nframes1
$nrcds
EOF

$EXEChwrf/hwrf_ofs_correct_wind < correct_wind.in >> $pgmout 2> errfile
correctwindrc=$?
if [ $correctwindrc -ne 0 ]
then
  export err=$correctwindrc;err_chk
else
  echo done with ofs_correct_wind
fi

echo "*** Finished script $0"

