#!/bin/ksh 
########################################################### 
# Sea ice drift forecast model control script for
# Arctic and Antarctic.
# Produce Bulletins for OSO
# History: Sep 1997 - First implementation of new      
#        : Robert Grumbine, author.  30 June 1997.
#        : Modified 09 September 1997 by L. D. Burroughs
#        : June 1998 - Modified to collaborate with T-170 MRF  
#        : FEB 2000 - Convert to IBM SP."
#        : Jun 2001 - modified to increase output from 6 to 16 days 
#        : Oct 2004 - Modified for CCS Phase 2
#        :            It is originally Job 930
#        : Mar 2007 - Modified for 10m winds
########################################################### 

cd $DATA

########################################
set -x
msg="HAS BEGUN!"
postmsg "$jlogfile" "$msg"
###########################

export FILENV=$DATA/.assign.FORTRAN

#set MP_HOLDTIME to a larger number to speed up processing by 
# preventing CPUs from being rescheduled as frequently

export MP_HOLDTIME=2666666

#-----------------------------------------------------
# copy over the fix files
#-----------------------------------------------------
cp $FIXsice/seaice_quote seaice_quote   
cp $FIXsice/seaice_forecast.points seaice_forecast.points

pgm=seaice_sicedrft
export pgm; prep_step

ln -s seaice_forecast.points fort.47
ln -s seaice_quote fort.91

echo $PDY > alpha
ln -s alpha     fort.90

#-----------------------------------------------------
#get the ice line points
#-----------------------------------------------------
cp $FIXsice/seaice_com.ipasaiji.npoc.arctic niceline
cp $FIXsice/seaice_com.ipasaiji.npoc.antarc siceline
ln -s niceline   fort.48
ln -s siceline   fort.49

#-----------------------------------------------------
#units for the gfs data
#New Mar 2007: Construct averaged 10m winds to single file vs.
#  old usage of sigma files
#-----------------------------------------------------
#oldBASE=$COMIN/gfs.t00z.sf
#oldiunit=10
#oldfor hh in  00  12  24  36  48  60  72  84  96 108 120 132 144 156 168 180 192 \
#old          204 216 228 240 252 264 276 288 300 312 324 336 348 360 372 384
#olddo
#old  iunit=`expr $iunit + 1 `
#old  cp ${BASE}$hh fort.$iunit
#olddone

#Files are available every 3 hours through the 180
#  then 12 hours through 384 (no need for average, straight wgribbing)
#Note that due to resolution changes, we need to run averager even
#  when files are 12 hours apart

#Ensure that the output files don't already exist
for fn in u.averaged.$PDY v.averaged.$PDY
do
  if [ -f $fn ] ; then
    rm $fn
  fi
done
base=$COMIN/gfs.t00z.sfluxgrb

#Construct averages for 10m winds
for hr in 0 12 24 36 48 60 72 84 96 108 120 132 144 168
do
  h1=$hr;
  h2=`expr $h1 + 3`
  h3=`expr $h2 + 3`
  h4=`expr $h3 + 3`
  if [ $h1 -lt 10 ] ; then
    h1=0$h1;
  fi
  if [ $h2 -lt 10 ] ; then
    h2=0$h2;
  fi
  if [ $h3 -lt 10 ] ; then
    h3=0$h3;
  fi
  if [ $h4 -lt 10 ] ; then
    h4=0$h4;
  fi

  $EXECutil/wgrib ${base}f$h1 > index
  grep UGRD:kpds5=33:kpds6=105:kpds7=10 index | $EXECutil/wgrib -i ${base}f${h1} -nh -o tmpu.$h1.$PDY 
  grep VGRD:kpds5=34:kpds6=105:kpds7=10 index | $EXECutil/wgrib -i ${base}f${h1} -nh -o tmpv.$h1.$PDY 

  $EXECutil/wgrib ${base}f$h2 > index
  grep UGRD:kpds5=33:kpds6=105:kpds7=10 index | $EXECutil/wgrib -i ${base}f${h2} -nh -o tmpu.$h2.$PDY 
  grep VGRD:kpds5=34:kpds6=105:kpds7=10 index | $EXECutil/wgrib -i ${base}f${h2} -nh -o tmpv.$h2.$PDY 

  $EXECutil/wgrib ${base}f$h3 > index
  grep UGRD:kpds5=33:kpds6=105:kpds7=10 index | $EXECutil/wgrib -i ${base}f${h3} -nh -o tmpu.$h3.$PDY 
  grep VGRD:kpds5=34:kpds6=105:kpds7=10 index | $EXECutil/wgrib -i ${base}f${h3} -nh -o tmpv.$h3.$PDY 

  $EXECutil/wgrib ${base}f$h4 > index
  grep UGRD:kpds5=33:kpds6=105:kpds7=10 index | $EXECutil/wgrib -i ${base}f${h4} -nh -o tmpu.$h4.$PDY 
  grep VGRD:kpds5=34:kpds6=105:kpds7=10 index | $EXECutil/wgrib -i ${base}f${h4} -nh -o tmpv.$h4.$PDY 

  $EXECsice/seaice_preaverage u.averaged.$PDY tmpu.$h1.$PDY tmpu.$h2.$PDY tmpu.$h3.$PDY tmpu.$h4.$PDY
  $EXECsice/seaice_preaverage v.averaged.$PDY tmpv.$h1.$PDY tmpv.$h2.$PDY tmpv.$h3.$PDY tmpv.$h4.$PDY
done

while [ $hr -lt 384 ]
do
  hr=`expr $hr + 12`
  h1=$hr
  $EXECutil/wgrib ${base}f$h1 > index

  grep UGRD:kpds5=33:kpds6=105:kpds7=10 index | $EXECutil/wgrib -i ${base}f${h1} -nh -o tmpu.$h1.$PDY 
  $EXECsice/seaice_preaverage u.averaged.$PDY tmpu.$h1.$PDY

  grep VGRD:kpds5=34:kpds6=105:kpds7=10 index | $EXECutil/wgrib -i ${base}f${h1} -nh -o tmpv.$h1.$PDY 
  $EXECsice/seaice_preaverage v.averaged.$PDY tmpv.$h1.$PDY
done

rm tmp[uv].*.* index
ln u.averaged.$PDY fort.11
ln v.averaged.$PDY fort.12

#-----------------------------------------------------
#execute the model
#-----------------------------------------------------
msg="pgm sicedrft has BEGUN!"
postmsg "$jlogfile" "$msg"

echo 32 | $EXECsice/seaice_sicedrft >> $pgmout 2> errfile
err=$?; export err; err_chk

ln -s fort.60 fl.out
ln -s fort.61 ops.out
ln -s fort.62 ak.out
ln -s fort.63 global.tran
ln -s fort.64 alaska.tran

#-----------------------------------------------------
#Distribute the output
#-----------------------------------------------------
if [ $SENDCOM = "YES" ] ; then
  cp ops.out $COMOUT/global.$PDY
  cp ak.out $COMOUT/alaska.$PDY
  cp global.tran $COMOUT/global.tran.$PDY
  cp alaska.tran $COMOUT/alaska.tran.$PDY
  cp global.tran $pcom/global.tran.${cycle}
  cp alaska.tran $pcom/alaska.tran.${cycle}
  if [ "$SENDDBN" = 'YES' ] ; then
    $USHutil/make_ntc_bull.pl  WMOHD NONE KWBC NONE  \
       alaska.tran $pcom/alaska.tran.${cycle}
    $USHutil/make_ntc_bull.pl  WMONV NONE KWBC NONE  \
       global.tran $pcom/global.tran.${cyc}.$job
    $DBNROOT/bin/dbn_alert MODEL OMBICE $job $COMOUT/global.$PDY
    $DBNROOT/bin/dbn_alert MODEL OMBICE $job $COMOUT/alaska.$PDY
    $DBNROOT/bin/dbn_alert MODEL OMBICE_TRAN $job $COMOUT/global.tran.$PDY
    $DBNROOT/bin/dbn_alert MODEL OMBICE_TRAN $job $COMOUT/alaska.tran.$PDY
  fi
fi

#####################################################################
# GOOD RUN
set +x
echo "**************$job COMPLETED NORMALLY ON THE IBM SP"
echo "**************$job COMPLETED NORMALLY ON THE IBM SP"
echo "**************$job COMPLETED NORMALLY ON THE IBM SP"
set -x
#####################################################################

#################################

msg='HAS COMPLETED NORMALLY.'
echo $msg
postmsg "$jlogfile" "$msg"


#------------------------------------------------------------------
# End of script
#------------------------------------------------------------------

