#!/usr/bin/ksh

PS4='+ gfdl_pre_ocean_eastpac_phase3.sh line $LINENO: '

set +x
#####################################################################
echo "   ----------------------------------------------------------"
echo "   gfdl_pre_ocean_eastpac_phase3.sh - Script to run ocean model"
echo "                               in eastern Pacific for GFDL runs"
echo "   ----------------------------------------------------------"
echo "   History: MAY 2004 - Script written by Tim Marchok"
echo "                       (timothy.marchok@noaa.gov, 609-452-6534)"
#####################################################################
set -x

DATA=${DATA}/eastpac
if [ ! -d ${DATA} ]; then mkdir -p ${DATA}; fi
cd $DATA

sh $utilscript/setup.sh

export YYYY=` echo $PDY | cut -c1-4`
export YY=`   echo $PDY | cut -c3-4`
export MM=`   echo $PDY | cut -c5-6`
export DD=`   echo $PDY | cut -c7-8`
export HH=${cyc}
export ymdh=${PDY}${cyc}
export yyyymmdd=${PDY}

if [ ${PARAFLAG} = 'YES' ]
then
  if [ ${FORAENV} = 'GFS' ]
  then
    gfsdir=/com/gfs/prod/gfs.${PDY}
    hurdir=${RST_PHASE3_DIR}
    # NOTE: We ALWAYS want to use the 00z GFS data for our
    #       "production" reruns, since this is what NCO 
    #       uses for their runs, and we want to match them.
    sfcanlfile=gfs.t00z.sfcanl
    siganlfile=gfs.t00z.sanl
  else
    gfsdir=${PARA_DAT_DIR}
    hurdir=${RST_PHASE3_DIR}
    sfcanlfile=${PARA_SFCANL_FILE}
    siganlfile=${PARA_SIGANL_FILE}
  fi
else
  gfsdir=${gfsdir:-/com/gfs/${envir}/gfs.${PDY}}
  hurdir=/com/hur/${envir}/hur.${PDY}00
  mkdir -m 775 -p $hurdir
  sfcanlfile=gfs.t${cyc}z.sfcanl
  siganlfile=gfs.t${cyc}z.sanl
fi

#------------------------------------------------------------
# Copy the GFS sst, mask and latlon data files that were 
# produced in the exgfdl_ocean_spinup job and are currently
# in ${hurdir}....

cp ${hurdir}/sst.gfs.${yyyymmdd}.dat    sst.gfs.dat
cp ${hurdir}/mask.gfs.${yyyymmdd}.dat   mask.gfs.dat
cp ${hurdir}/lonlat.gfs                 lonlat.gfs


#------------------------------------------------------------
# Now run the ocean model.  

region_id=eastpac

set +x
echo " "
echo "+++ Running ocean model for ${region_id} region at `date`...."
echo " "
set -x

# Make appropriate modifications to the parameters.inp file
cp ${PARMhur}/gfdl_${region_id}.parm.3 parameters.inp.shell

sed -e "s/_date_/${YYMMDD}${cyc}/g"  \
    -e "s/_name1_/${stormenv}/g"     \
    -e "s/_name2_/${stormenv}/g"     \
    parameters.inp.shell >parameters.inp

rm parameters.inp.shell
rm RST.*

# Do the phase 3 ocean spin-up.  Note that there is a different
# executable depending on the region (region_id) variable.

ln -s -f ${DATA}/parameters.inp                                        fort.10
ln -s -f ${DATA}/temp_salin_levitus.eastpac.${ATCFNAME}.${PDY}${cyc}   fort.13
ln -s -f ${DATA}/nullfile                                              fort.15
ln -s -f ${DATA}/sst.gfs.dat                                           fort.21
ln -s -f ${DATA}/mask.gfs.dat                                          fort.22
ln -s -f ${DATA}/lonlat.gfs                                            fort.23
ln -s -f ${COMIN}/westbd.${ATCFNAME}.${PDY}${cyc}                      fort.44
ln -s -f ${FIXhur}/gfdl_raw_temp_salin.eastpac.${MM}                   fort.45
ln -s -f ${DATA}/eastpac_ocean_model_info.${ATCFNAME}.${PDY}${cyc}     fort.66

export pgm=gfdl_ocean_${region_id}
. prep_step
startmsg

$EXEChur/gfdl_ocean_${region_id} >gfdl_ocean.${region_id}.out
export err=$?;err_chk

mv RST.* $DATA/RST.phase3.${region_id}
mv ${DATA}/RST.phase3.${region_id}  ${COMIN}/rst.phase3.${ATCFNAME}.${PDY}${cyc}.${region_id}
mv ${DATA}/eastpac_ocean_model_info.${ATCFNAME}.${PDY}${cyc}    ${COMIN}/.
mv ${DATA}/temp_salin_levitus.eastpac.${ATCFNAME}.${PDY}${cyc}  ${COMIN}/.

set +x
echo " "
echo "+++ End of gfdl_pre_ocean_eastpac_phase3.sh at `date`...."
echo " "
set -x
