#!/bin/ksh
##################################################################
# hwrf_coupled_fcst.sh 
#######################
# Script to run the coupled model.
# Script for Coupled Forecast:  Vijay Tallapragada January 2007
# Modified for operations: Vijay Tallapragada April 2007 
# Upgrades to HWRF: Robert Tuleya, Young Kwon and Vijay Tallapragada 
#                   April 2008 
# Script modifications for 2009 HWRF implementation - Vijay Tallapragada
# Expanded capabilities for 3-way coupling and restart
# June 2009
# Jan 2011: Added east pac domain for ocean coupling (Biju Thomas)
##################################################################
set -x 

## SET DATE AND OTHER ENVIRONMENTAL VARIABLE FROM THE SUPER SCRIPT ##
##### GET THE INPUT VARIABLES ###############################
if [ $# -eq 6 ]
then
   export DOMAIN=${1}
   export YEAR=${2}
   export MONTH=${3}
   export DATE=${4}
   export CYCLE=${5}
   export CASE=${6}

   echo 'DOMAIN = ' ${DOMAIN}
   echo 'YEAR   = ' ${YEAR}
   echo 'MONTH  = ' ${MONTH}
   echo 'DATE   = ' ${DATE}
   echo 'CYCLE  = ' ${CYCLE}
   echo 'CASE   = ' ${CASE} 
else
   echo 'Usage: sh hwrf_coupled_fcst.sh DOMAIN YEAR MONTH DATE CYCLE CASE'
   echo 'NEED SIX ARGUMENTS'
   echo 'SCRIPT WILL EXIT'
   sh ${utilscript}/setup.sh
   err=911
   err_exit "NEED SIX ARGUMENTS - aborting"
fi

#############################################################
ndate=${utilexec}/ndate

export pdy=$YEAR$MONTH$DATE
export cyc=$CYCLE
export LCASE_NAME=`echo  $DOMAIN | tr '[A-Z]' '[a-z]' `
name=$LCASE_NAME
export CSTREAM=${DATA}/$DOMAIN.$YEAR$MONTH$DATE$CYCLE

echo 'pdy     = ' ${pdy}
echo 'cyc     = ' ${cyc}
echo 'name    = ' ${name}  
echo 'CSTREAM = ' ${CSTREAM} 

export mm=`echo $pdy | cut -c5-6`
export yymmdd=`echo $pdy | cut -c3-8`
export yyyy=`echo $pdy | cut -c1-4`

echo 'mm     = ' ${mm}
echo 'yymmdd = ' ${yymmdd}
echo 'yyyy   = ' ${yyyy}

if [[ "$RUN_PREP_HYBRID" == YES ]] ; then
    USEPH=T
else
    USEPH=F
fi


######### Region Specification #####################
work_d=$CSTREAM/OCEAN/ocn2D_spin
echo 'work_d = ' ${work_d} 

export region=`tail -1 $work_d/ocean_region_info.txt | awk '{print $1}'`
  tail -2 $work_d/ocean_region_info.txt | head -1

  case $region in
    east_atlantic) export region=eastatl;;
    west_united)   export region=united;;
    east_pacific)  export region=eastpac;;
  esac
echo "region chosen is " $region
####################################################
### SPECIFIC TO COUPLED FORECAST

export MP_SHARED_MEMORY=yes
export TARGET_CPU_LIST=-1
export MP_LABELIO=yes

##### OTHER DIRECTORIES REQUIRED FOR THE FORECAST

if [[ $CASE == MOVE ]]; then
fcst_d=$CSTREAM/COUPLED 
export C_PERIOD=540
export C_NSECS=43200
else
fcst_d=$CSTREAM/COUPLED_FCST 
fi
echo "Running CASE = ${CASE} in fcst_d = ${fcst_d}"

atmos_d=$CSTREAM/ATMOS
bdy_d=$CSTREAM/ATMOS_BDY
export WORK_DIR=$fcst_d
export C_separate_stdout=$SEPAR3X
work_d=$CSTREAM/OCEAN/ocn2D_spin
pha3_d=$work_d/phase3-mcs
pha4_d=$work_d/phase4-mcs
ocn_clim=GDEM
ocn_clim=gdem
fixd_d=$FIXED_DATA


# MAKE THE COUPLED DIRECTORY AND COPY OCEAN AND ATMOSPHERIC FILES
# REQUIRED FOR THE RUN  

rm -f -r $fcst_d
mkdir -p $fcst_d  

MPI_PROG_DIR=${MPI_PROG_DIR:-$EXEChwrf}

if [[ $region == united ]] ; then
    ocean_exe=hwrf_ocean_united
elif [[ "$ATLEXT" == YES ]] ; then
    ocean_exe=hwrf_ocean_eastatl_ext
else
    ocean_exe=hwrf_ocean_eastatl
fi
if [[ $region == eastpac ]] ; then
 ocean_exe=hwrf_ocean_eastpac
fi

for final_exe in "$EXEChwrf/$ocean_exe" "$MPI_PROG_DIR/$ocean_exe" MISSING ; do
    if [[ -x "$final_exe" ]] ; then
        ocean_exe="$final_exe"
        break
    fi
done

if [[ "$final_exe" == MISSING ]] ; then
    sh ${utilscript}/setup.sh
    err=911
    err_exit "Looked for $ocean_exe in $EXEChwrf and $MPI_PROG_DIR but did not find it."
fi

cp "$final_exe" $fcst_d/ocean_${region}.xc

cp $EXEChwrf/hwrf_wrf               $fcst_d/wrf.exe
cp $EXEChwrf/hwrf_wm3c              $fcst_d/. 

# copy fixed files to working directory 
cp $FIXhwrf/hwrf_ETAMPNEW_DATA        ${fcst_d}/ETAMPNEW_DATA
cp $FIXhwrf/hwrf_ETAMPNEW_DATA_DBL    ${fcst_d}/ETAMPNEW_DATA_DBL
cp $FIXhwrf/hwrf_GENPARM.TBL          ${fcst_d}/GENPARM.TBL
cp $FIXhwrf/hwrf_LANDUSE.TBL          ${fcst_d}/LANDUSE.TBL
cp $FIXhwrf/hwrf_RRTM_DATA            ${fcst_d}/RRTM_DATA
cp $FIXhwrf/hwrf_RRTM_DATA_DBL        ${fcst_d}/RRTM_DATA_DBL
cp $FIXhwrf/hwrf_SOILPARM.TBL         ${fcst_d}/SOILPARM.TBL
cp $FIXhwrf/hwrf_VEGPARM.TBL          ${fcst_d}/VEGPARM.TBL
cp $FIXhwrf/hwrf_co2_trans            ${fcst_d}/co2_trans
cp $FIXhwrf/hwrf_eta_micro_lookup.dat ${fcst_d}/eta_micro_lookup.dat 
cp $FIXhwrf/hwrf_tr49t67              ${fcst_d}/tr49t67
cp $FIXhwrf/hwrf_tr49t85              ${fcst_d}/tr49t85
cp $FIXhwrf/hwrf_tr67t85              ${fcst_d}/tr67t85
cp $FIXhwrf/hwrf_track                ${fcst_d}/track
#

############### CREATE THE NAMELIST FOR 12hr RUN ######################################
export date_string=$YEAR$MONTH$DATE$CYCLE
export YMDH="$date_string"
if [ ${RESTART} == YES ]; then
i_restart=T
RSTMIN=360
RESTART_STATUS=false
else
i_restart=F
RSTMIN=0
RESTART_STATUS=false
fi

if [[ $CASE = "MOVE" ]] ; then
cd $fcst_d
echo "running CASE = ${CASE} in fcst_d = ${fcst_d}" 
$USHhwrf/hwrf_namelist.sh -h 12 -o $fcst_d/namelist_move.input
ln -s -f namelist_move.input                  $fcst_d/fort.12
cp $atmos_d/domain.center             $fcst_d/. 
cp $atmos_d/storm.center              $fcst_d/.

# run the executable hwrf_swcorner_dynamic
sh ${utilscript}/setup.sh  
export pgm=hwrf_swcorner_dynamic 
. prep_step
startmsg
${EXEChwrf}/hwrf_swcorner_dynamic 
export err=$?; err_chk
msg="executable hwrf_swcorner_dynamic for CASE=${CASE} return err = ${err}"  
if [ $err -ne 0 ]; then
err_exit $msg
fi

. ./set_nest

$USHhwrf/hwrf_namelist.sh -h 12 -i2 "$istart" -j2 "$jstart" \
    -h 12 -o $fcst_d/namelist.input

cp $fcst_d/namelist.input $fcst_d/namelist_move12.input

else

############### CREATE THE NAMELIST FOR 126hr RUN ######################################
cd $fcst_d
ncompute=$(( NPROCS_a - IOSRV_PERGRP*IOSRV_GROUPS ))
case "$ncompute" in
    240) export NPROC_X=12 NPROC_Y=20 ;;
    *) /bin/true ;;
esac
echo "Running CASE = ${CASE} in fcst_d = ${fcst_d}" 
$USHhwrf/hwrf_namelist.sh -o $fcst_d/namelist.input
ln -s -f namelist.input                  $fcst_d/fort.12
cp $atmos_d/domain.center                 $fcst_d/.
cp $atmos_d/storm.center                  $fcst_d/.  

# run the executable hwrf_swcorner_dynamic
sh ${utilscript}/setup.sh  
export pgm=hwrf_swcorner_dynamic 
. prep_step
startmsg
${EXEChwrf}/hwrf_swcorner_dynamic 
export err=$?; err_chk
msg="executable hwrf_swcorner_dynamic for CASE=${CASE} return err = ${err}"  
if [ $err -ne 0 ]; then
err_exit $msg
fi
. ./set_nest

$USHhwrf/hwrf_namelist.sh -i2 "$istart" -j2 "$jstart" \
    -h 126 -o $fcst_d/namelist.input

cp $fcst_d/namelist.input $fcst_d/namelist_126.input

fi
#######################################################################################
/bin/cp $atmos_d/wrfinput*                                 $fcst_d/.
/bin/cp $bdy_d/wrfbdy*                                     $fcst_d/.
/bin/cp $atmos_d/wrfanl*                                   $fcst_d/.
/bin/cp $atmos_d/fort.65                                   $fcst_d/.
/bin/cp $atmos_d/geo*                                      $fcst_d/.

if [ $GWD == YES ]; then
# NOTE: there used to be a bunch of cp commands here to copy V2 HWRF
# gravity wave drag files.  Those are no longer created in V3 HWRF.
echo "HWRF configuration includes Gravity Wave Drag Parameterization"

else

echo "GWD not requested, using dummy gwd_surface data"

fi
#################################################################################
export XLSMPOPTS="parthds=1:spins=0:yields=0:stack=128000000"

cd $fcst_d
rm -f gfdl_initdata.${region}.${mm} RST.* PARAMETERS.inp

if [[ $CASE = "MOVE" ]]; then
  cp ${PARMhwrf}/hwrf_gfdl_${region}.parm_anal.coup   parameters.inp.shell
else
  cp ${PARMhwrf}/hwrf_gfdl_${region}.parm.coup        parameters.inp.shell
fi

sed -e "s/_date_/${yymmdd}${cyc}/g"  \
      -e "s/_name1_/${name}/g"     \
      -e "s/_name2_/${name}/g"     \
      -e "s/_rstph4file_/RST.final/g"  \
      parameters.inp.shell >PARAMETERS.inp
rm -f parameters.inp.shell
#

if [ ${region} = 'united' ]; then
ln -s -f ${fixd_d}/gfdl_ocean_topo_and_mask.${region}                 fort.66
# ln -s -f ${fixd_d}/gfdl_initdata.${ocn_clim}.${region}.${mm}          fort.13
pha3loop=$CSTREAM/OCEAN/ocn2D_spin/phase3-mcs/gfdl_initdata.${region}.${mm}
if [[ ! -s "$pha3loop" ]] ; then
   sh ${utilscript}/setup.sh
   err=911
   err_exit "PHASE 3 LOOP CURRENT IS MISSING: $pha3loop"
fi
ln -s -f "$pha3loop" fort.13
else
if [[ "$ATLEXT" == YES ]] ; then
ln -s -f ${fixd_d}/gfdl_initdata.gdem.united.${mm}                    fort.12
ln -s -f ${fixd_d}/gfdl_initdata.${region}.${mm}                      fort.13
if [[ "$PARAFLAG" == YES ]] ; then
    # We must temporarily point to the development fix area for this
    # gfdl file since nwprod has not yet been updated:
    ln -s -f ${FIXhwrf}/gfdl_ocean_topo_and_mask.${region}_extn           fort.66
else
    ln -s -f ${fixd_d}/gfdl_ocean_topo_and_mask.${regcode}_extn           fort.66
fi
else
ln -s -f ${fixd_d}/gfdl_Hdeepgsu.${region}                            fort.66
ln -s -f ${fixd_d}/gfdl_initdata.${region}.${mm}                      fort.13
fi
fi

if [ ${region} = 'eastpac' ]; then
    ln -s -f ${pha3_d}/temp_salin_levitus.eastpac                    fort.13
    ln -s -f ${pha3_d}/eastpac_ocean_model_info                      fort.66
fi

ln -s -f $pha4_d/RST.final                                            RST.final
ln -s -f $work_d/sst.gfs.dat                                          fort.41
ln -s -f $work_d/mask.gfs.dat                                         fort.42
ln -s -f $work_d/lonlat.gfs                                           fort.43

ln -s -f $CSTREAM/OCEAN/ocn2D_spin/track   track

# 
# NOTE THAT THE EXECUTABLE IS SIMPLY REPLACED WITH DMITRY'S SCRIPT 

#################################################################################
###INSERT WAWE WATCH STUFF HERE
#################################################################################

if [ ${WAVEWATCH} == YES ]; then
cd $fcst_d
cp -p ${FIXhwrf}/hwrf_cpat.moddef ./mod_def.ww3
cp -p ${PARMhwrf}/hwrf_cpat.inp.tmpl ./ww3_shel.inp.tmpl
cp -p ${EXEChwrf}/hwrf_ww3_shel ./ww3_shel
# use stream editor to modify values in the input template and save it as
# ww3_shel.inp
# get two files - one for wind and another for ice in order to run the 
# wave model (fixed files)

ww3_start="${PDY} ${CYC}0000"
if [[ $CASE == MOVE ]]; then
    endtime=$( $ndate 12 $date_string )
else
    endtime=$( $ndate 126 $date_string )
fi
ww3_end=$( echo $endtime | cut -c 1-8 )\ $(echo $endtime | cut -c9-10 )0000
sed -e "s/START_TIME/${ww3_start}/g" \
    -e "s/END_TIME/${ww3_end}/g" \
    ./ww3_shel.inp.tmpl > ./ww3_shel.inp

cp -p ${FIXhwrf}/hwrf_cpat.wind ./wind.ww3
cp -p ${FIXhwrf}/hwrf_cpat.ice ./ice.ww3

echo "Integration of HWRF includes 3-way coupled system with Ocean Model $OCEAN and Wave Watch III"

${USHhwrf}/hwrf_c_execute.sh
#################################################################################

else

echo "Integration of HWRF includes 2-way coupled system with Ocean Model $OCEAN"

${USHhwrf}/hwrf_c_execute.sh  

fi

# end of script 
