#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exdgex_sfcupdate.sh.sms
# Script description:  Insert surface fields from NAM restart file into DGEX initial
#                      condition file.
#
# Author:        Eric Rogers       Org: NP22         Date: 1999-06-23
#
# Script history log:
# 2006-04-21  Eric Rogers - Extensive changes for WRF-NMM

set -x

#
# Get needed variables from exdgex_prelim.sh.sms
#
. $COMOUT/${RUN}_${REGION}.t${cyc}z.envir.sh

mkdir -p $DATA/sfcupdate
cd $DATA/sfcupdate
sh /nwprod/util/ush/setup.sh

# Set MPI environment variables for NCEP IBM
export MP_SHARED_MEMORY=yes
export XLFRTEOPTS="nlwidth=80"
export MP_LABELIO=yes

#--------------------------------------------------------------
# need to set these 6 variables.
#
#  INPUT_FILE is the file that contains the land states that
#  will be interpolated to the target grid.  it may either
#  be an edas file output from Eric's utility or
#  a wrf nmm binary file.  which one is communicated to
#  the program by setting INPUT_FILE_TYPE to "edas" or "nmm".
#
#  WRF_BINARY_FILE is the output grid file that will be updated by
#  this utility.  may be an arw or nmm file.  which one is
#  communicated to the program by setting DYN_CORE to
#  "edas" or "nmm". 
#
#  EXEC is the path/name of the coldstart executable.
#  WORK_DIR is the working directory.
#--------------------------------------------------------------

#get valid time of NAM 78-h forecast

echo $CYCLEdgex > startdate.dgex

YYYY=`cut -c 1-4 startdate.dgex`
MM=`cut -c 5-6 startdate.dgex`
DD=`cut -c 7-8 startdate.dgex`
HHSTART=`cut -c 9-10 startdate.dgex`

valyr=`echo $CYCLEdgex | cut -c1-4`
valmn=`echo $CYCLEdgex | cut -c5-6`
valdy=`echo $CYCLEdgex | cut -c7-8`
valhr=`echo $CYCLEdgex | cut -c9-10`

PDY=`echo $CYCLEnam | cut -c 1-8`
cycnam=`echo $CYCLEnam | cut -c 9-10`

cp $COMOUT/dgex_${REGION}.t${cyc}z.input_nemsio_precoldstart test_input_umo_regional.nemsio.d01

timeform=${valyr}"-"${valmn}"-"${valdy}"_"${valhr}":00:00"

INPUT_FILE="${GESDIRnam}/nam.t${cycnam}z.restart_nemsio.078h_00m_00.00s"
INPUT_FILE_TYPE="nems"
WRF_BINARY_FILE="test_input_umo_regional.nemsio.d01"
DYN_CORE="nems"
WORK_DIR="$DATA/sfcupdate"

cd $WORK_DIR

#----------------------------------------------------------------
# don't touch fort.81 namelist settings.
#----------------------------------------------------------------

cat > ${WORK_DIR}/fort.81 << !
 &input_state_fields
  input_file="${INPUT_FILE}"
  input_file_type="${INPUT_FILE_TYPE}"
 /
 &output_grid_specs
  specs_from_output_file=.false.
  lats_output_file="$FIXdgex/dgex_${REGION}_hpnt_latitudes.grb"
  lons_output_file="$FIXdgex/dgex_${REGION}_hpnt_longitudes.grb"
  lsmask_output_file="$FIXdgex/dgex_${REGION}_slmask.grb"
  orog_output_file="$FIXdgex/dgex_${REGION}_elevtiles.grb"
  substrate_temp_output_file="$FIXdgex/dgex_${REGION}_tbot.grb"
 /
 &optional_output_fields
  snow_free_albedo_output_file=""
  greenfrc_output_file=""
  mxsnow_alb_output_file="${FIXdgex}/dgex_${REGION}_mxsnoalb.grb"
  slope_type_output_file=""
  soil_type_output_file="${FIXdgex}/dgex_${REGION}_soiltiles.grb"
  veg_type_output_file="${FIXdgex}/dgex_${REGION}_vegtiles.grb"
  z0_output_file="${FIXdgex}/dgex_${REGION}_z0clim.grb"
 /
 &soil_parameters
  soil_src_input = "statsgo"
  smclow_input  = 0.5
  smchigh_input = 3.0
  smcmax_input= 0.395, 0.421, 0.434, 0.476, 0.476, 0.439,
                0.404, 0.464, 0.465, 0.406, 0.468, 0.457,
                0.464, -9.99,  0.20, 0.421
  beta_input =  4.05,  4.26,  4.74,  5.33,  5.33,  5.25,
                6.77,  8.72,  8.17, 10.73, 10.39, 11.55,
                5.25, -9.99,  4.05,  4.26
  psis_input =  0.0350, 0.0363, 0.1413, 0.7586, 0.7586, 0.3548,
                0.1349, 0.6166, 0.2630, 0.0977, 0.3236, 0.4677,
                0.3548,  -9.99, 0.0350, 0.0363
  satdk_input = 1.7600e-4, 1.4078e-5, 5.2304e-6, 2.8089e-6, 2.8089e-6,
                3.3770e-6, 4.4518e-6, 2.0348e-6, 2.4464e-6, 7.2199e-6,
                1.3444e-6, 9.7394e-7, 3.3770e-6,     -9.99, 1.4078e-5,
                1.4078e-5
  soil_src_output = "statsgo"
  smclow_output  = 0.5
  smchigh_output = 3.0
  smcmax_output= 0.395, 0.421, 0.434, 0.476, 0.476, 0.439,
                 0.404, 0.464, 0.465, 0.406, 0.468, 0.457,
                 0.464, -9.99,  0.20, 0.421
  beta_output =  4.05,  4.26,  4.74,  5.33,  5.33,  5.25,
                 6.77,  8.72,  8.17, 10.73, 10.39, 11.55,
                 5.25, -9.99,  4.05,  4.26
  psis_output =  0.0350, 0.0363, 0.1413, 0.7586, 0.7586, 0.3548,
                 0.1349, 0.6166, 0.2630, 0.0977, 0.3236, 0.4677,
                 0.3548, -9.99,  0.0350, 0.0363
  satdk_input = 1.7600e-4, 1.4078e-5, 5.2304e-6, 2.8089e-6, 2.8089e-6,
                3.3770e-6, 4.4518e-6, 2.0348e-6, 2.4464e-6, 7.2199e-6,
                1.3444e-6, 9.7394e-7, 3.3770e-6,     -9.99, 1.4078e-5,
                1.4078e-5
 /
 &veg_parameters
  veg_src_input = "igbp"
  veg_src_output = "igbp"
  salp_output = 4.0
  snup_output = 0.080, 0.080, 0.080, 0.080, 0.080, 0.020,
                0.020, 0.060, 0.040, 0.020, 0.010, 0.020,
                0.020, 0.020, 0.013, 0.013, 0.010, 0.020,
                0.020, 0.020
 /
 &final_output
  output_file_type="${DYN_CORE}"
  output_file="${WRF_BINARY_FILE}"
 /
 &options
  landice_opt=3
 /
 &nam_options
  merge=.false.
/
!

# run the executable

export pgm=dgex_coldstart
#. prep_step

startmsg
$EXECdgex/dgex_coldstart >> $pgmout 2> errfile
export err=$?

# program returns a non-zero status then something is wrong, kill run

if [ $err -ne 0 ]
then
  msg=" ABNORMAL TERMINATION IN $pgm : kill DGEX"
  sh $utilscript/postmsg.sh "$jlogfile" "$msg"
  err_chk
else
  msg="$pgm completed normally"
  sh $utilscript/postmsg.sh "$jlogfile" "$msg"
  mv test_input_umo_regional.nemsio.d01 $COMOUT/dgex_${REGION}.t${cycnam}z.input_nemsio.tm00
fi

exit
