#!/bin/ksh
set -aex

# produce SSTs for GFS components
# (1) interpolate MOM3 SSTs to 1x1 grid
# (2) merge MOM3 SSTs, observed climatology (1982-2001), and
#     initial SST anomalies decaying at a time scale of 90 days.
# input : sstMOM3$cplvdate.gr
# output: sstgrb.$VDATE
#
export NDATE=${NDATE:-/nwprod/util/exec/ndate}
export NHOUR=${NHOUR:-/nwprod/util/exec/nhour}

cpldate=$VDATE     ;# date of current coupling episode
fcpl=$FHCYC         ;# coupling frequency in hours

cplvdate=`$NDATE $fcpl $cpldate` ;# date of next coupling episode
if [ -f sst1x1fcst$cplvdate.gr ]; then
 rm sst1x1fcst$cplvdate.gr
fi
if [ -f sst1x1fcst$cplvdate.grb ]; then
 rm sst1x1fcst$cplvdate.grb
fi

yycur=`echo $cpldate | cut -c1-4`   ;#start year of this job
mmcur=`echo $cpldate | cut -c5-6`
ddcur=`echo $cpldate | cut -c7-8`

yynxt=`echo $cplvdate | cut -c1-4`   ;#start year of this job
mmnxt=`echo $cplvdate | cut -c5-6`
ddnxt=`echo $cplvdate | cut -c7-8`

yyini=`echo $start_date | cut -c1-4`   ;#start year of this job
mmini=`echo $start_date | cut -c5-6`
ddini=`echo $start_date | cut -c7-8`

ihrfcst=`$NHOUR $cplvdate $start_date`
iddf=`expr $ihrfcst \/ 24 `  ;#number of forecast days

# scSST: grib file of daily 1X1 SST climatology
scSST=${scSST:-FIXcfs/cfs_sst1x1grbclim19822001}

#soSST: grib file containing initial SST field
soSST=${soSST:-/gloptmp/dump/sstgrb.fnl.$($NDATE 24 $start_date)}

echo " &dateini iyrini=$yyini,imoini=$mmini,idayini=$ddini,iddf=$iddf /">sstparm
echo " &datecur iyrcur=$yycur,imocur=$mmcur,idaycur=$ddcur /">>sstparm
echo " &datenxt iyrnxt=$yynxt,imonxt=$mmnxt,idaynxt=$ddnxt /">>sstparm
echo " &files scSST='$scSST',soSST='$soSST' /">>sstparm

# merge fcst and observed SSTs
$coupO2Aexec <sstparm >> $pgmout 2>errfile
export err=$?;$DATA/err_chk
