#!/bin/ksh
###############################################################
# 
# script:  cfs_ocn.sh
#
#   This script created by shrinivas Moorthi on 02/18/2004
#   based on postmon64
#
# pgb
#
#  $1 ... Coupled run starting date
#  $2 ... Mom3 file processing begining date
#  $3 ... Mom3 file processing ending date
#
#  This script converts the MOM3 output file time-mean.yyyymmdd.nc
#  to daily 2x1 grib files from starting date to ending date.
#  It also outputs eta_daily files on 2.5x2 grid
#
if [ $# -ne 3 ] ; then
	$DATA/err_exit "`date` $0: argument error" 
fi
#
#
start_date=$1
bdate=$2
edate=$3
#
YMDH=$start_date
#
prog=pgb
. $DATA/prep_step

$DATA/startmsg
#
#
#
momoutdir=$DATA/MOM3_$YMDH
mkdir -p $momoutdir
cd $momoutdir
#
${NCP:-cp} $MASKDIR/cfs_gridsMOM3.dat gridsMOM3.dat
${NCP:-cp} $MASKDIR/cfs_maskMOM3.gr   maskMOM3.gr
#
oday=`echo $bdate | cut -c1-8`
date=$($NDATE $FHCYC $bdate)
until [[ $date -gt $edate ]] ; do
  ls -l $COM_YMDH/time_mean.$oday.nc
  ${NCP:-/bin/cp} $COM_YMDH/time_mean.$oday.nc .
  $mom3_daily_proc $oday            # converts mom3 daily file to 2x1 ieee file
  export err=$?;$DATA/err_chk
#  ls -l $momoutdir/ocndaily2x1_$oday.gr
#
#  rc=$?
#  if [[ $rc -ne 0 ]];then $PERR;exit 1;fi
#
  export idbug=1
  export infile=$momoutdir/ocndaily2x1_$oday.gr
  export outfile=$momoutdir/ocn_daily$date
  export year=`echo $date | cut -c1-4`
  export month=`echo $date | cut -c5-6`
  export day=`echo $date | cut -c7-8`
  export hour=0
  export idim=180
  export jdim=139
  export kdim=40
  export flatn=64.0
  export flats=-74.0
  export flonw=1.0
  export flone=359.0
  export dlat=1.0
  export dlon=2.0
  $grib_oceanexec          # converts ieee  ocn file to grib ocn file
  export err=$?;$DATA/err_chk
#  ls -l $outfile
#
#rc=$?
#  if [[ $rc -ne 0 ]];then $PERR;exit 1;fi
#
#
#
  $mom3_etadaily2p5 $oday
  export err=$?;$DATA/err_chk
#  ls -l $momoutdir/etadaily$oday.gr
  export infile=$momoutdir/etadaily$oday.gr
#  ls -l $infile
  export idbug=1
  export out00=$momoutdir/eta_daily${date}
  export out12=$momoutdir/eta_daily${oday}12
  year=`echo $date | cut -c1-4`
  month=`echo $date | cut -c5-6`
  day=`echo $date | cut -c7-8`
  hr=`echo $date | cut -c9-10`
  export idim=144
  export jdim=73
  export jdimi=55
  export flatn=62.5
  export flats=-72.5
  export dlat=2.5
  $grib_eta_daily
  export err=$?;$DATA/err_chk
#
#  rc=$?
#  if [[ $rc -ne 0 ]];then $PERR;exit 1;fi
#
  /bin/rm $momoutdir/ocndaily2x1_$oday.gr
  /bin/rm $momoutdir/etadaily$oday.gr
#
#
  if [ $SENDCOM = YES ] ; then
    for filename in $FILES_TO_SEND_COM
    do
      if [ $filename = ocn ] ; then
        if [ ! -s $COM_YMDH_DG/ocn_daily$date ] ; then
          $force_grib_date_y2k $momoutdir/ocn_daily$date $date
          $force_grib_date_y2k $momoutdir/eta_daily$date $date
          $force_grib_date_y2k $momoutdir/eta_daily${oday}12 ${oday}12

          ${NCP:-/bin/cp} $momoutdir/ocn_daily$date $COM_YMDH_DG/ocn_daily$date
          ${NCP:-/bin/cp} $momoutdir/eta_daily$date $COM_YMDH_DG/eta_daily$date
          ${NCP:-/bin/cp} $momoutdir/eta_daily${oday}12 $COM_YMDH_DG/eta_daily${oday}12
        fi
#
#       Cat the ocean height field to the pgb files
#
#                                                                    For the 00Z hour
        FH=$($NHOUR $date $YMDH)
        if [ -s $COM_YMDH_DG/pgbf$FH ] ; then
          cat $momoutdir/eta_daily$date >> $COM_YMDH_DG/pgbf$FH
        fi
        if [ -s $DATA/$YMDH/pgbf$FH ] ; then
          cat $momoutdir/eta_daily$date >> $DATA/$YMDH/pgbf$FH
        fi
#                                                                    For the 12Z hour
        FH=$($NHOUR ${oday}12 $YMDH)
        if [ -s $COM_YMDH_DG/pgbf$FH ] ; then
          cat $momoutdir/eta_daily${oday}12 >> $COM_YMDH_DG/pgbf$FH
        fi
        if [ -s $DATA/$YMDH/pgbf$FH ] ; then
          cat $momoutdir/eta_daily${oday}12 >> $DATA/$YMDH/pgbf$FH
        fi
      fi
    done
  fi
  oday=`echo $date | cut -c1-8`
  date=$($NDATE $FHCYC $date)
done
#
#
export err=$?; $DATA/err_chk

