#!/bin/ksh
##########################################################
# hwrf_relocate.sh 
###################################################################################
#
# This script creates hurricane componnent from 6 hours forecast fields
# (if the 6 hours forecast does not exist, the script will exist)
#
#                            April 2007, Qingfu Liu
#
####################################################################################
# April 2007 Revised for operations Vijay Tallapragada
# April 2008 Revised for 2008 HWRF by Qingfu Liu and Vijay Tallapragada
# Jun 2009 Revised for HWRF 2009 implementation, Vijay Tallapragada
##########################################################
set -x
if [ $INIT == NO ]; then
    echo 'Initialization not requested.  INIT = $INIT This script need not be run'
    sh ${utilscript}/setup.sh
    err=911
    err_exit 'Initialization not requested.  INIT = $INIT This script need not be run'
fi
########## GET THE INPUT VARIABLES #######################
if [ $# -eq 8 ]
    then
    export storm=${1}
    export YEAR=${2}
    export MONTH=${3}
    export DATE=${4}
    export CYCLE=${5}
    export storm_id=${6}
    export CLAT=${7}
    export CLON=${8}
    
    echo 'storm    = ' ${storm}
    echo 'YEAR     = ' ${YEAR}
    echo 'MONTH    = ' ${MONTH}
    echo 'DATE     = ' ${DATE}
    echo 'CYCLE    = ' ${CYCLE}
    echo 'storm_id = ' ${storm_id}
    echo 'CLAT     = ' ${CLAT}
    echo 'CLON     = ' ${CLON} 
else
    echo 'Usage: sh hwrf_relocate.sh storm YEAR MONTH DATE CYCLE'
    echo '                                 storm_id CLAT CLON'  
    echo 'NEED EIGHT ARGUMENTS'
    echo 'SCRIPT WILL EXIT'
    sh ${utilscript}/setup.sh
    err=911
    err_exit 'NEED EIGHT ARGUMENTS'
fi 
################################################################

storm_n=`echo $storm_id | cut -c 1-2 `
basin=`  echo $storm_id | cut -c 3-3 `

if [ ${basin} = 'L' ]; then
    basin=AL
fi
# added just in case lower case storm_id was used
if [ ${basin} = 'l' ]; then
    basin=AL
fi

if [ ${basin} = 'E' ]; then
    basin=EP
fi
# added just in case lower case storm_id was used
if [ ${basin} = 'e' ]; then
    basin=EP
fi

export cdate=$YEAR$MONTH$DATE$CYCLE
echo 'cdate = ' ${cdate}

# Control settings
ndate=${NDATE_PATH:-${utilexec}/ndate}
ncp=${CP_PATH:-/bin/cp}

gdate=`$ndate -06 $cdate`
echo 'gdate = ' ${gdate}
yyyy=` echo $cdate | cut -c 1-4 `
yy=`   echo $cdate | cut -c 3-4 `
im=`   echo $cdate | cut -c 5-6 `
id=`   echo $cdate | cut -c 7-8 `
ih=`   echo $cdate | cut -c 9-10 `

export VIT=$DATA/tmpvit

echo 'yyyy = ' ${yyyy}
echo 'yy   = ' ${yy}
echo 'im   = ' ${im}
echo 'ih   = ' ${ih}  

domdir=${DATA}/${1}.${cdate}/MESSAGES

# Set paths for runtime and save directories
tmpdir=${DATA}/3DVAR/hwrf_rel/$storm.$cdate
savdir=${DATA}/$storm.${cdate}/ATMOS
vitdir=$ATMOS_INPUT

# Specify fixed field and data directories.

testf_d1=${HISTDATA}/${stormid}.wrfout_d01_${yyyy}-${im}-${id}_${ih}:00:00
testf_d2=${HISTDATA}/${stormid}.wrfout_d02_${yyyy}-${im}-${id}_${ih}:00:00
testf_d3=${HISTDATA}/${stormid}.wrfout_d03_${yyyy}-${im}-${id}_${ih}:00:00

if [ ! -f $testf_d1 ]
    then
    echo "Previous six hour forecast is not located at $testf_d1"
    echo "This is a cold start."
    echo "This script will contnue from step 2 onwards"
else
# script will run to end of script 

    st_int=${STORM_INT}
    st_depth=${STORM_DEPTH}

  if [[ ${st_depth} = 'S' && ${st_int} -lt 12 ]]; then

    echo "Previous six hour forecast is located at $testf_d1"
    echo "storm is a shallow and weak storm"
    echo ${st_int}
    echo ${st_depth}
    echo "This is a cold start."
    echo "This script will contnue from step 2 onwards"

  elif [ ${st_int} -lt 12 ]; then

    echo "Previous six hour forecast is located at $testf_d1"
    echo "storm is a weak storm, max wind speed < 12m/s"
    echo ${st_int}
    echo ${st_depth}
    echo "This is a cold start."
    echo "This script will contnue from step 2 onwards"

  else 

    echo "Previous six hour forecast is located at $testf_d1"
    echo "This is not a cold start" 
    echo "The script hwrf_relocate.sh will continue" 

# Set up directories
    /bin/rm -rf $tmpdir
    mkdir -p $tmpdir
    cd $tmpdir

# create namelist file

    $USHhwrf/hwrf_namelist.sh -a -o ./namelist_analysis0.input
    ln -s -f   namelist_analysis0.input                ./fort.12

# Copy the domain center from previous six hour forecast cycle

    if [ $cyc -eq 00 ]; then
        ymdh=${PDYm1}${histcyc}
    else
        ymdh=${PDY}${histcyc}
    fi

    $ncp -f ${HISTDATA}/${stormid}.${ymdh}.domain.center domain.center
    $ncp -f ${DATA}/$storm.$cdate/MESSAGES/storm.center .

# 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
    echo "executable hwrf_swcorner_dynamic return code err = ${err}"  
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_swcorner_dynamic return code err = ${err}"
    fi

    . ./set_nest

    $USHhwrf/hwrf_namelist.sh -a -i2 "$istart" -j2 "$jstart" -o ./namelist.input

# copy the fixed file to $tmpdir 

    $ncp -f ${FIXhwrf}/hwrf_eta_micro_lookup.dat  ./eta_micro_lookup.dat 


# Copy executables to $tmpdir

    $ncp -f ${EXEChwrf}/hwrf_diffwrf_3dvar        ./hwrf_diffwrf_3dvar.exe
    $ncp -f ${EXEChwrf}/hwrf_bin_io               ./hwrf_bin_io.exe 
    $ncp -f ${EXEChwrf}/hwrf_merge_nest_4x_step12_3n  ./merge_nest_4x_step12_3n.x
    $ncp -f ${EXEChwrf}/hwrf_split1                ./wrf_split.x
    $ncp -f ${EXEChwrf}/hwrf_create_trak_guess    ./create_trak_guess.x
    $ncp -f ${EXEChwrf}/hwrf_pert_ct1              ./hwrf_pert_ct.x 

# Copy wrfnmm restart files
    if [ ${yyyy} -lt 2005 ]
        then
        grep -i $storm_id $vitdir/gblav.t${ih}z.syndata.tcvitals.tm00 > ./tcvitals.as
    else
        grep -i $storm_id $vitdir/gfs.t${ih}z.syndata.tcvitals.tm00   > ./tcvitals.as
    fi

    $ncp -f $VIT ./tmpvit
#zz    if [ $PARAFLAG == NO ]; then
#zz        sed -e "s/${yy}${im}${id}/$yyyy$im$id/g" tmpvit > tmpvit1
#zz        grep -i $storm_id ./tmpvit1   > ./tcvitals.as
#zz    else
        grep -i $storm_id ./tmpvit   > ./tcvitals.as
#zz    fi

    if [ ! -s tcvitals.as ]; then
        echo "tcvitals file is missing - check the path names and the status of GFS forecast run"
    fi

    st_int=${STORM_INT}

    $ncp -f $testf_d1     ./wrfout_d01
    $ncp -f $testf_d2     ./wrfout_d02

    if [ -f $testf_d3 ]; then 
       $ncp -f $testf_d3     ./wrfout_d03
    fi

    if [ $IO_FORM = 2 ]; then

        ./hwrf_diffwrf_3dvar.exe storm_relocate wrfout_d01 flnm3 old_hwrf_d01
        export err=$?
        if [ $err -ne 0 ]; then
            err_exit "executable ./hwrf_diffwrf_3dvar return code err = ${err}"
        fi

    else

        echo $tmpdir                 > itag4
        echo "wrfout_d01"           >> itag4
        echo "relocate"             >> itag4
        echo $tmpdir/old_hwrf_d01   >> itag4
        echo $tmpdir/domain.center  >> itag4

        sh ${utilscript}/setup.sh

        $USHhwrf/hwrf_run.sh -mpiwrap ./hwrf_bin_io.exe < itag4 > stdout_wrfges4 2>&1
        export err=$?
        if [ $err -ne 0 ]; then
            err_exit "executable hwrf_bin_io.exe return code err = ${err}"
        fi
    fi

    if [ $IO_FORM = 2 ]; then

        ./hwrf_diffwrf_3dvar.exe storm_relocate wrfout_d02 flnm3 old_hwrf_d02
        export err=$?
        if [ $err -ne 0 ]; then
            err_exit "executable ./hwrf_diffwrf_3dvar return code err = ${err}"
        fi

      if [ -f $testf_d3 ]; then 
        ./hwrf_diffwrf_3dvar.exe storm_relocate wrfout_d03 flnm3 old_hwrf_d03
        export err=$?
        if [ $err -ne 0 ]; then
            err_exit "executable ./hwrf_diffwrf_3dvar return code err = ${err}"
        fi
      fi
    else

        echo $tmpdir                > itag5
        echo "wrfout_d02"          >> itag5
        echo "relocate"            >> itag5
        echo $tmpdir/old_hwrf_d02  >> itag5
        echo $tmpdir/domain.center >> itag5

        sh ${utilscript}/setup.sh

        $USHhwrf/hwrf_run.sh -mpiwrap ./hwrf_bin_io.exe < itag5 > stdout_wrfges5 2>&1
        export err=$?
        if [ $err -ne 0 ]; then
            err_exit "executable hwrf_bin_io.exe return code err = ${err}"
        fi

      if [ -f $testf_d3 ]; then 
        echo $tmpdir                > itag6
        echo "wrfout_d03"          >> itag6
        echo "relocate"            >> itag6
        echo $tmpdir/old_hwrf_d03  >> itag6
        echo $tmpdir/domain.center >> itag6

        sh ${utilscript}/setup.sh

        $USHhwrf/hwrf_run.sh -mpiwrap ./hwrf_bin_io.exe < itag6 > stdout_wrfges6 2>&1
        export err=$?
        if [ $err -ne 0 ]; then
            err_exit "executable hwrf_bin_io.exe return code err = ${err}"
        fi
      fi

    fi
# correct data, regenerate inner nest data,test only

    /bin/rm -f $tmpdir/roughness
    /bin/rm -f $tmpdir/roughness1

    /bin/rm -f fort.*

    ln -s -f $tmpdir/tcvitals.as            fort.11
    ln -s -f $tmpdir/old_hwrf_d01           fort.26
    ln -s -f $tmpdir/old_hwrf_d02           fort.36
    ln -s -f $tmpdir/old_hwrf_d03           fort.46
    ln -s -f $tmpdir/data_4x_hwrf           fort.56
    ln -s -f $tmpdir/roughness1             fort.66

    ln -s -f $tmpdir/30_degree_data         fort.61

    export gesfhr=6
    export ibgs=0

# run executable merge_nest_4x_step1.x 
    sh ${utilscript}/setup.sh 
    export pgm=merge_nest_4x_step12_3n.x
    .  prep_step
    startmsg
    echo $gesfhr $st_int $ibgs $CLAT $CLON | $tmpdir/merge_nest_4x_step12_3n.x
    export err=$?; err_chk
    echo "executable merge_nest_4x_step12_3n.x return code err = ${err}"  
    if [ $err -ne 0 ]; then
        err_exit "executable merge_nest_4x_step12_3n.x return code err = ${err}"
    fi

    /bin/rm -f fort.*

    if [ ${st_int} -gt 10 ]; then
        $ncp -f ${HISTDATA}/${stormid}*atcfunix.${gdate}.combine $tmpdir/hdas_atcfunix
    else
        $ncp -f ${HISTDATA}/${stormid}*atcfunix.${gdate}.parent  $tmpdir/hdas_atcfunix
    fi

    ln -s -f $tmpdir/tcvitals.as                   fort.11
    ln -s -f $tmpdir/hdas_atcfunix                 fort.12
    ln -s -f $tmpdir/trak.fnl.all                  fort.30

    gesfhr6=`grep 'HWRF, 006,' $tmpdir/hdas_atcfunix | wc -l`
    if ( ! grep 'HWRF, 000,' $tmpdir/hdas_atcfunix ) ; then
        # Added so that HHS can give an error when track file was scrubbed,
        # but not give an error when 6hr position is missing.
        echo "There is no 0 hour (analysis time) position from previous cycle"
        echo "create_trak_guess.x can not continue with relocation"
    elif [ $gesfhr6 -lt 1 ]; then
        echo "There is no 6-hr Forecast Position from previous cycle"
        echo "create_trak_guess.x can not continue with relocation"
    else

# run executable create_trak_guess.x
        sh ${utilscript}/setup.sh 
        export pgm=create_trak_guess.x 
        . prep_step
        startmsg
        echo $storm_id $ih | $tmpdir/create_trak_guess.x
        export err=$?; err_chk 
        echo "executable create_trak_guess.x return code err = ${err}"  

        if [ $err -ne 0 ]; then
            err_exit "executable create_trak_guess.x return code err = ${err}"
        fi
        /bin/rm -f fort.*

        ln -s -f $tmpdir/tcvitals.as                   fort.11
        ln -s -f $tmpdir/data_4x_hwrf                  fort.26
        ln -s -f $tmpdir/trak.fnl.all                  fort.30
        ln -s -f $tmpdir/old_hwrf_d01                  fort.46 

        ln -s -f $tmpdir/wrf_env                       fort.56
        ln -s -f $tmpdir/rel_inform.$cdate             fort.52
        ln -s -f $tmpdir/vital_syn.$cdate              fort.55
        ln -s -f $tmpdir/storm_pert                    fort.71
        ln -s -f $tmpdir/storm_radius                  fort.85

        export gesfhr=6
        export ibgs=0

# run executable wrf_split.x 
        sh ${utilscript}/setup.sh
        export pgm=wrf_split.x
        . prep_step
        startmsg
        echo $gesfhr $ibgs $st_int | $tmpdir/wrf_split.x
        export err=$?; err_chk
        echo "executable wrf_split.x return code err = ${err}"  
        if [ $err -ne 0 ]; then
            err_exit "executable wrf_split.x return code err = ${err}"
        fi

        /bin/rm -f fort.*

        ln -s -f $tmpdir/hdas_atcfunix                 fort.12 
        ln -s -f $tmpdir/tcvitals.as                   fort.11
        ln -s -f $tmpdir/wrf_env                       fort.26
        ln -s -f $tmpdir/storm_pert                    fort.71
        ln -s -f $tmpdir/storm_pert_new                fort.58
        ln -s -f $tmpdir/storm_size_p                  fort.14
        ln -s -f $tmpdir/storm_sym                     fort.23

        ln -s -f $tmpdir/storm_pert_step1_1            fort.35

        export gesfhr=6

# run executable hwrf_pert_ct.x
        sh ${utilscript}/setup.sh
        export pgm=hwrf_pert_ct.x 
        . prep_step
        startmsg
        echo $gesfhr | $tmpdir/hwrf_pert_ct.x
        export err=$?; err_chk

        if [ $err -ne 0 ]; then
            err_exit "executable hwrf_pert_ct.x return code err = ${err}"
        fi

        /bin/rm -f fort.*
        echo "continue bogussing the storm in step2. This is not a cold start"
    fi
  fi
fi
#step2

# check dependencies

if [ ! -f $testf_d1 ]
then
# cold start waitting time
wait_time=2400
else
# warm start waitting time
wait_time=1200
fi
GFS_GHOST="$savdir/ghost_d03_0000-00-00_00:00:00"
if ( ! "$USHhwrf/hwrf_file_complete.pl" -v -o 10 -s 1000000000 -w $wait_time -S 20 "$GFS_GHOST" ) ; then
    sh ${utilscript}/setup.sh
    err_exit "timed out waiting for $GFS_GHOST"
    exit 2
fi

if [[ ! -d "$tmpdir" ]] ; then
   mkdir -p $tmpdir
fi
cd $tmpdir
# Copy the fixed file to $tmpdir 
#echo "continuing with cold start, bogusing"
$ncp -f ${EXEChwrf}/hwrf_pert_ct1              ./hwrf_pert_ct.x 
# Copy executable files to $tmpdir
$ncp ${EXEChwrf}/hwrf_diffwrf_3dvar        ./hwrf_diffwrf_3dvar.exe
$ncp ${EXEChwrf}/hwrf_bin_io               ./hwrf_bin_io.exe
$ncp ${EXEChwrf}/hwrf_create_nest_1x_10m   ./create_nest_1x_10m.x
$ncp ${EXEChwrf}/hwrf_split1                ./wrf_split.x
$ncp ${EXEChwrf}/hwrf_merge_nest_4x_step12_3n   ./merge_nest_4x_step12_3n.x
$ncp ${EXEChwrf}/hwrf_anl_bogus_10m        ./hwrf_anl_bogus_10m.x
$ncp ${EXEChwrf}/hwrf_anl_cs_10m           ./hwrf_anl_cs_10m.x
$ncp ${EXEChwrf}/hwrf_anl_4x_step2         ./hwrf_anl_4x_step2.x
$ncp ${EXEChwrf}/hwrf_inter_4to2           ./hwrf_inter_4to2.x
$ncp ${EXEChwrf}/hwrf_inter_4to6           ./hwrf_inter_4to6.x
$ncp ${EXEChwrf}/hwrf_create_trak_fnl      ./create_trak_fnl.x
$ncp -f ${FIXhwrf}/hwrf_eta_micro_lookup.dat  ./eta_micro_lookup.dat 

# Copy wrfnmm restart files
$ncp $savdir/wrfinput_d01                                ./wrfinput_d01
$ncp $savdir/wrfanl_d02_${yyyy}-${im}-${id}_${ih}:00:00  ./wrfinput_d02

#if [ -f $testf_d3 ]; then
  $ncp $savdir/ghost_d03_0000-00-00_00:00:00               ./wrfghost_d02
  $ncp $savdir/wrfanl_d03_${yyyy}-${im}-${id}_${ih}:00:00  ./wrfinput_d03
#else
#  $ncp $savdir/ghost_d03_0000-00-00_00:00:00               ./wrfghost_d02
#fi

if [ ${yyyy} -lt 2005 ]
    then
    grep -i $storm_id $vitdir/gblav.t${ih}z.syndata.tcvitals.tm00 > ./tcvitals.as
else
    grep -i $storm_id $vitdir/gfs.t${ih}z.syndata.tcvitals.tm00   > ./tcvitals.as 
fi

$ncp $VIT ./tmpvit
#zzif [ $PARAFLAG = NO ]; then
#zz    sed -e "s/${yy}${im}${id}/$yyyy$im$id/g" tmpvit > tmpvit1
#zz    grep -i $storm_id ./tmpvit1   > ./tcvitals.as
#zz    st_int=`cat ./tcvitals.as | cut -c66-67`
#zzelse
    grep -i $storm_id ./tmpvit   > ./tcvitals.as
    st_int=`cat ./tcvitals.as | cut -c68-69`
#zzfi

domain_test=$domdir/domain.center

if [ -f $domain_test ]; then
    $ncp $domdir/domain.center $tmpdir/domain.center
else
    echo "Domain Center at $domain_test does not exist"
    echo "Check the directory $domdir, provide domain.center"
    echo "And run this script again.  Now exiting"
    sh ${utilscript}/setup.sh
    err=911
    err_exit "Domain Center at $domain_test does not exist"
fi

 $ncp  $savdir/namelist_analysis0.input        ./namelist.input

if [ $IO_FORM = 2 ]; then

    ./hwrf_diffwrf_3dvar.exe storm_relocate wrfinput_d01 flnm3 new_gfs_d01
    export err=$?
    if [ $err -ne 0 ]; then
        err_exit "executable ./hwrf_diffwrf_3dvar return code err = ${err}"
    fi

else

    echo $tmpdir                > itag1
    echo "wrfinput_d01"        >> itag1
    echo "relocate"            >> itag1
    echo $tmpdir/new_gfs_d01   >> itag1
    echo $tmpdir/domain.center >> itag1

    sh ${utilscript}/setup.sh

    $USHhwrf/hwrf_run.sh -mpiwrap ./hwrf_bin_io.exe < itag1 > stdout_wrfges1 2>&1
    export err=$?
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_bin_io.exe returned error code $err"
    fi
fi

if [ $IO_FORM = 2 ]; then

    ./hwrf_diffwrf_3dvar.exe storm_relocate wrfinput_d02 flnm3 new_gfs_d02
    export err=$?
    if [ $err -ne 0 ]; then
        err_exit "executable ./hwrf_diffwrf_3dvar return code err = ${err}"
    fi

#    if [ -f $testf_d3 ]; then
          ./hwrf_diffwrf_3dvar.exe storm_relocate wrfinput_d03 flnm3 new_gfs_d03
      export err=$?
      if [ $err -ne 0 ]; then
        err_exit "executable ./hwrf_diffwrf_3dvar return code err = ${err}"
      fi
#    fi

else

    echo $tmpdir                > itag2
    echo "wrfinput_d02"        >> itag2
    echo "relocate"            >> itag2
    echo $tmpdir/new_gfs_d02   >> itag2
    echo $tmpdir/domain.center >> itag2

    sh ${utilscript}/setup.sh

    $USHhwrf/hwrf_run.sh -mpiwrap ./hwrf_bin_io.exe < itag2 > stdout_wrfges2 2>&1
    export err=$?
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_bin_io.exe returned error code $err"
    fi

    rm itag2

#    if [ -f $testf_d3 ]; then
    echo $tmpdir                > itag2
    echo "wrfinput_d03"        >> itag2
    echo "relocate"            >> itag2
    echo $tmpdir/new_gfs_d03   >> itag2
    echo $tmpdir/domain.center >> itag2

    sh ${utilscript}/setup.sh

    $USHhwrf/hwrf_run.sh -mpiwrap ./hwrf_bin_io.exe < itag2 > stdout_wrfges2 2>&1
    export err=$?
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_bin_io.exe returned error code $err"
    fi
#    fi 
fi

#$USHhwrf/hwrf_namelist.sh -g -o ./namelist.input
#sed s:USEPH:F:g $PARMhwrf/hwrf_namelist_ghost.input > ./namelist.input

$ncp  $savdir/namelist_ghost.input       ./namelist.input

if [ $IO_FORM = 2 ]; then

    ./hwrf_diffwrf_3dvar.exe storm_relocate wrfghost_d02 flnm3 new_ght_d02
    export err=$?
    if [ $err -ne 0 ]; then
        err_exit "executable ./hwrf_diffwrf_3dvar return code err = ${err}"
    fi

else

    echo $tmpdir                > itag3
    echo "wrfghost_d02"        >> itag3
    echo "relocate"            >> itag3
    echo $tmpdir/new_ght_d02   >> itag3
    echo $tmpdir/domain.center >> itag3

    sh ${utilscript}/setup.sh

    $USHhwrf/hwrf_run.sh -mpiwrap ./hwrf_bin_io.exe < itag3 > stdout_wrfges3 2>&1
    export err=$?
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_bin_io.exe returned error code $err"
    fi
fi

$ncp  $savdir/namelist_analysis0.input       ./namelist.input

# correct data, regenerate inner nest data

rm -f fort.*

ln -s -f $tmpdir/new_gfs_d02           fort.46
ln -s -f $tmpdir/new_gfs_d01           fort.26
ln -s -f $tmpdir/new_data_1x           fort.56
ln -s -f $tmpdir/new_data_d01          fort.57
export gesfhr=6

# run the executable create_nest_1x_10m.x
sh ${utilscript}/setup.sh
export pgm=create_nest_1x_10m.x
. prep_step
startmsg
echo $gesfhr | $tmpdir/create_nest_1x_10m.x
export err=$?; err_chk
echo "executable create_nest_1x_10m.x return code err = ${err}"  
if [ $err -ne 0 ]; then
    err_exit "executable create_nest_1x_10m.x returned error code $err"
fi

mv new_gfs_d01 new_gfs_d01_org
mv new_data_d01 new_gfs_d01

rm -f fort.*

COMATCF=$ATMOS_INPUT

if [[ "$FIND_GFS_VORTEX" != NO ]] ; then
    # Use HWRF scripts to find the GFS track, done in hwrf_gfs_track0 job:
    track0file="$savdir/gfs-anl-fix.atcfunix"
    
    # Wait for the file to exist.  In NCO mode, the file already should exist
    # since the job is run in parallel with the dummy job, but takes less
    # time.  Just in case, we wait up to 5 minutes here in NCO mode.  
    # In non-NCO mode, we wait a half hour to allow jobs to run out of order.
    naptime=1800
    if [[ "$PARAFLAG" == YES ]] ; then
        naptime=1800
    fi
    if ( ! "$USHhwrf/hwrf_file_complete.pl" -o 10 -s 0 -w $naptime -S 10 "$track0file" ) ; then

        # The file did not exist.
        if [[ "$PARAFLAG" == YES ]] ; then
            err_exit "timed out waiting for $track0file"
        else
            echo "Timed out waiting for $track0file: will use tcvitals for the vortex relocation instead."
            /bin/true > ./atcfunix # create an empty track file
        fi
    else
        if [[ ! -s "$track0file" ]] ; then
            # The track file exists but is empty.  That means we could not
            # find the GFS vortex.
            echo "Track file is empty; will use tcvitals instead: $track0file"
        fi
        cp "$track0file"  ./atcfunix
    fi
else
    # Use GFS track file
    grep -i $cdate ${COMATCF}/avn.t${CYCLE}z.cyclone.trackatcfunix > ./tmp11
    grep -i $basin ./tmp11 > ./tmp12
    grep -iE "AVNO|PRE1|PRD1" ./tmp12>   ./atcfunix
fi


ln -s -f $tmpdir/tcvitals.as            fort.11
ln -s -f $tmpdir/atcfunix               fort.12
ln -s -f $tmpdir/trak.fnl.all_gfs       fort.30

# run the executable create_trak_fnl.x
sh ${utilscript}/setup.sh 
export pgm=create_trak_fnl.x 
. prep_step
startmsg
echo $storm_id $yyyy | $tmpdir/create_trak_fnl.x
export err=$?; err_chk
echo "executable create_trak_fnl.x return code err = ${err}"  
if [ $err -ne 0 ]; then
    err_exit "executable create_trak_fnl.x returned error code $err"
fi

rm -f $tmpdir/roughness
rm -f $tmpdir/roughness2

rm -f fort.*

ln -s -f $tmpdir/tcvitals.as            fort.11
ln -s -f $tmpdir/new_gfs_d01            fort.26
ln -s -f $tmpdir/new_gfs_d02            fort.36
ln -s -f $tmpdir/new_gfs_d03            fort.46
ln -s -f $tmpdir/data_4x_gfs            fort.56
ln -s -f $tmpdir/roughness2             fort.66

export gesfhr=6
export ibgs=1

# run executable merge_nest_4x_step12_3n.x
sh ${utilscript}/setup.sh
export pgm=merge_nest_4x_step12_3n.x
. prep_step
startmsg
echo $gesfhr $st_int $ibgs $CLAT $CLON | $tmpdir/merge_nest_4x_step12_3n.x
##echo $gesfhr $st_int $ibgs | $tmpdir/merge_nest_4x_step12_3n.x
export err=$?; err_chk
echo "executable merge_nest_4x_step12_3n.x return code err = ${err}"  
if [ $err -ne 0 ]; then
    err_exit "executable merge_nest_4x_step12_3n.x returned error code $err"
fi

rm -f fort.*

ln -s -f $tmpdir/tcvitals.as            fort.11
ln -s -f $tmpdir/data_4x_gfs            fort.26
ln -s -f $tmpdir/trak.fnl.all_gfs       fort.30
ln -s -f $tmpdir/new_gfs_d01            fort.46 
ln -s -f $tmpdir/gfs_env                fort.56
ln -s -f $tmpdir/rel_inform_gfs.$cdate  fort.52
ln -s -f $tmpdir/vital_syn_gfs.$cdate   fort.55
ln -s -f $tmpdir/storm_pert_gfs         fort.71
ln -s -f $tmpdir/storm_radius_gfs       fort.85

export gesfhr=6
export ibgs=2

testf_d1=$tmpdir/storm_pert_new

if [ -f $testf_d1 ]; then
   export ibgs=1
fi

# run the executable wrf_split.x
sh ${utilscript}/setup.sh 
export pgm=wrf_split.x
. prep_step
startmsg
echo $gesfhr $ibgs $st_int | $tmpdir/wrf_split.x
export err=$?; err_chk 
echo "executable wrf_split.x return code err = ${err}"  
if [ $err -ne 0 ]; then
    err_exit "executable wrf_split.x returned error code $err"
fi

if [ -f $testf_d1 ]; then
    echo "This is not a cold start, previous six hour forecast is located at " $testf_d1

# correct data, regenerate inner nest data,test only

#    rm -f fort.*

#    ln -s -f $tmpdir/tcvitals.as            fort.11
#    ln -s -f $tmpdir/new_gfs_d02            fort.46
#    ln -s -f $tmpdir/new_gfs_d01            fort.47
#    ln -s -f $tmpdir/roughness              fort.66

#    export gesfhr=6
#    export ibgs=2

# run the executable merge_nest_4x_step2.x
#    sh ${utilscript}/setup.sh 
#    export pgm=merge_nest_4x_step2.x 
#    . prep_step
#    startmsg
#    echo $gesfhr $st_int $ibgs | $tmpdir/merge_nest_4x_step2.x
#    export err=$?; err_chk 
#    echo "executable merge_nest_4x_step2.x return code err = ${err}"  
#    if [ $err -ne 0 ]; then
#        err_exit "executable merge_nest_4x_step2.x returned error code $err"
#    fi

    rm -f fort.*

    ln -s -f $tmpdir/storm_size_p           fort.14
    ln -s -f $tmpdir/tcvitals.as            fort.11
    ln -s -f $tmpdir/hdas_atcfunix          fort.12
    ln -s -f $tmpdir/gfs_env                fort.26
    ln -s -f $tmpdir/wrf_env_new            fort.36
    ln -s -f $tmpdir/storm_pert_new         fort.71
    ln -s -f $tmpdir/roughness1             fort.46
    ln -s -f $tmpdir/new_data_4x            fort.56
    ln -s -f $tmpdir/storm_sym              fort.23

##  ln -s -f $tmpdir/storm_pert_step2_1     fort.61
##  ln -s -f $tmpdir/storm_pert_step2_2     fort.64

    export gesfhr=6

# run the executable hwrf_anl_4x_step2.x 
    sh ${utilscript}/setup.sh 
    export pgm=hwrf_anl_4x_step2.x 
    . prep_step
    startmsg
    echo $gesfhr | $tmpdir/hwrf_anl_4x_step2.x
    export err=$?; err_chk
    echo "executable hwrf_anl_4x_step2.x return code err = ${err}"  
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_anl_4x_step2.x returned error code $err"
    fi

    testf1=$tmpdir/flag_file
    if [ -f $testf1 ]; then

        cat $tmpdir/flag_file

        rm -f fort.*
        rm -f $tmpdir/new_data_4x


        ln -s -f $tmpdir/tcvitals.as           fort.11
        ln -s -f $tmpdir/wrf_env_new           fort.26


        ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47   fort.71
        ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47   fort.72
        ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47   fort.73
        ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47   fort.74
        ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47   fort.75
#        ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47   fort.76
#        ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47   fort.77
        ln -s -f $FIXhwrf/hwrf_storm_20       fort.76
        ln -s -f $FIXhwrf/hwrf_storm_20       fort.77
        ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47   fort.78

        ln -s -f $tmpdir/storm_radius          fort.85
        ln -s -f $tmpdir/new_data_4x           fort.56
        ln -s -f $tmpdir/roughness1            fort.46
        ln -s -f $tmpdir/test_data             fort.25
        ln -s -f $tmpdir/storm_sym             fort.23

##    ln -s -f $tmpdir/storm_per_step2_3     fort.64

        export gesfhr=6

# run the executable hwrf_anl_cs_10m.x
        sh ${utilscript}/setup.sh
        export pgm=hwrf_anl_cs_10m.x 
        . prep_step
        startmsg
        echo $gesfhr | $tmpdir/hwrf_anl_cs_10m.x
        export err=$?;  err_chk
        echo "executable hwrf_anl_cs_10m.x return code err = ${err}"  
        if [ $err -ne 0 ]; then
            err_exit "executable hwrf_anl_cs_10m.x returned error code $err"
        fi

    fi

    rm -f fort.*
    
    if [ -f $testf_d3 ]; then
       export iflag_ghost=0
    else
       export iflag_ghost=1
    fi
 
    ln -s -f $tmpdir/tcvitals.as             fort.11
    ln -s -f $tmpdir/new_data_4x             fort.26
    ln -s -f $tmpdir/new_ght_d02             fort.36
    ln -s -f $tmpdir/data_merge_2x           fort.56
    
    export gesfhr=6

# run the executable hwrf_inter_4to2.x
    sh ${utilscript}/setup.sh
    export pgm=hwrf_inter_4to2.x
    . prep_step 
    startmsg
    echo $gesfhr $iflag_ghost | $tmpdir/hwrf_inter_4to2.x
    export err=$?; err_chk 
    echo "executable hwrf_inter_4to2.x return code err = ${err}"
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_inter_4to2.x returned error code $err"
    fi
    
    rm -f fort.*
    
    ln -s -f $tmpdir/tcvitals.as             fort.11
    ln -s -f $tmpdir/new_data_4x             fort.36
    ln -s -f $tmpdir/new_gfs_d01             fort.46
##  ln -s -f $tmpdir/old_hwrf_d01            fort.26
    ln -s -f $tmpdir/new_gfs_d01            fort.26
    ln -s -f $tmpdir/storm_radius            fort.85 
    ln -s -f $tmpdir/data_merge_d01          fort.56
    
    export gesfhr=6

# run the executable hwrf_inter_4to6.x
    sh ${utilscript}/setup.sh 
    export pgm=hwrf_inter_4to6.x
    . prep_step
    startmsg
    echo $gesfhr | $tmpdir/hwrf_inter_4to6.x
    export err=$?; err_chk
    echo "executable hwrf_inter_4to6.x return code err = ${err}"   
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_inter_4to6.x returned error code $err"
    fi

else
    echo "This is a cold start"

    rm -f fort.*
    
    ln -s -f $tmpdir/tcvitals.as              fort.11
    ln -s -f $tmpdir/gfs_env                  fort.26
    ln -s -f $tmpdir/data_4x_gfs              fort.36
    ln -s -f $tmpdir/storm_pert_gfs           fort.61

    ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47      fort.71
    ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47      fort.72
    ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47      fort.73
    ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47      fort.74
    ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47      fort.75
#    ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47      fort.76
#    ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47      fort.77
    ln -s -f $FIXhwrf/hwrf_storm_20       fort.76
    ln -s -f $FIXhwrf/hwrf_storm_20       fort.77
    ln -s -f $FIXhwrf/hwrf_storm_cyn_axisy_47      fort.78

    ln -s -f $tmpdir/storm_radius_gfs         fort.85 
    ln -s -f $tmpdir/roughness2               fort.46
    ln -s -f $tmpdir/new_data_4x              fort.56
    ln -s -f $tmpdir/test_data                fort.25

    export gesfhr=6

# run executable hwrf_anl_bogus_10m.x 
    sh ${utilscript}/setup.sh 
    export pgm=hwrf_anl_bogus_10m.x 
    . prep_step
    startmsg
    echo $gesfhr | $tmpdir/hwrf_anl_bogus_10m.x
    export err=$?; err_chk
    echo "executable hwrf_anl_bogus_10m.x return code err = ${err}"  
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_anl_bogus_10m.x returned error code $err"
    fi

    rm -f fort.*

    ln -s -f $tmpdir/tcvitals.as              fort.11
    ln -s -f $tmpdir/new_data_4x              fort.26
    ln -s -f $tmpdir/new_ght_d02              fort.36
    ln -s -f $tmpdir/data_merge_2x            fort.56
    
    export gesfhr=6

# run the executable hwrf_inter_4to2.x
    sh ${utilscript}/setup.sh
    export pgm=hwrf_inter_4to2.x
    . prep_step
    startmsg
    echo $gesfhr | $tmpdir/hwrf_inter_4to2.x
    export err=$?; err_chk
    echo "executable hwrf_inter_4to2.x return code err = ${err}"
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_inter_4to2.x returned error code $err"
    fi

    rm -f fort.*
    rm -f $tmpdir/flag_file
    
    ln -s -f $tmpdir/tcvitals.as              fort.11
    ln -s -f $tmpdir/new_data_4x              fort.36
    ln -s -f $tmpdir/new_gfs_d01              fort.46
    ln -s -f $tmpdir/new_gfs_d01              fort.26
    ln -s -f $tmpdir/storm_radius_gfs         fort.85   
    ln -s -f $tmpdir/data_merge_d01           fort.56

###  ln -s -f $tmpdir/test_pert_step1          fort.64
    
    export gesfhr=6

# run executable hwrf_inter_4to6.x
    sh ${utilscript}/setup.sh
    export pgm=hwrf_inter_4to6.x
    . prep_step
    startmsg
    echo $gesfhr | $tmpdir/hwrf_inter_4to6.x
    export err=$?; err_chk
    echo "executable hwrf_inter_4to6.x return code err = ${err}"  
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_inter_4to6.x returned error code $err"
    fi
    
    rm -f fort.*
    
    $ncp  $tmpdir/storm_radius_gfs         $tmpdir/storm_radius
fi

rm -f itag1 itag3

$ncp  $savdir/namelist_analysis0.input        ./namelist.input

if [ $IO_FORM = 2 ]; then

    ./hwrf_diffwrf_3dvar.exe 3dvar_update wrfinput_d01 data_merge_d01
    export err=$?
    if [ $err -ne 0 ]; then
        err_exit "executable ./hwrf_diffwrf_3dvar return code err = ${err}"
    fi

else


    echo $tmpdir                  > itag1
    echo "wrfinput_d01"          >> itag1
    echo "update"                >> itag1
    echo $tmpdir/data_merge_d01  >> itag1
    echo $tmpdir/domain.center   >> itag1

    sh ${utilscript}/setup.sh

    $USHhwrf/hwrf_run.sh -mpiwrap ./hwrf_bin_io.exe < itag1 > stdout_wrfges7 2>&1
    export err=$?;err_chk
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_bin_io returned error code $err"
    fi
fi

$ncp  $savdir/namelist_ghost.input        ./namelist.input

if [ $IO_FORM = 2 ]; then

    ./hwrf_diffwrf_3dvar.exe 3dvar_update wrfghost_d02 data_merge_2x 
    export err=$?
    if [ $err -ne 0 ]; then
        err_exit "executable ./hwrf_diffwrf_3dvar return code err = ${err}"
    fi

else

    echo $tmpdir                 > itag3
    echo "wrfghost_d02"         >> itag3
    echo "update"               >> itag3
    echo $tmpdir/data_merge_2x  >> itag3
    echo $tmpdir/domain.center  >> itag3

    sh ${utilscript}/setup.sh

    $USHhwrf/hwrf_run.sh -mpiwrap ./hwrf_bin_io.exe < itag3 > stdout_wrfges8 2>&1
    export err=$?;err_chk
    if [ $err -ne 0 ]; then
        err_exit "executable hwrf_bin_io returned error code $err"
    fi
fi

    $ncp $tmpdir/tcvitals.as $savdir/tcvitals.$cdate

