#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exnam_coldstart_real.sh.sms 
# Script description:  Runs REAL to get GDAS firsr guess file in WRF-NMM input file
                       format to coldstart WRF-NNM NANM forecast if NDAS did not run
#
# Author:        Eric Rogers       Org: NP22         Date: 2004-07-02
#
# Script history log:
# 2006-02-01  Eric Rogers - Based on HIRESW script

set -x

msg="JOB $job FOR WRF-NMM HAS BEGUN"
postmsg "$jlogfile" "$msg"

mod=AVN
export DOMNAM=nam

#
# Get needed variables from exndas_prelim.sh.sms
#
. $GESDIR/${RUN}.t${cyc}z.envir.sh

cd $DATA

export MP_I_BINDPROC=NO
export MP_BINDPROC=NO

echo DATEXX$CYCLE > nmcdate

PDY=`cat nmcdate | cut -c7-14`
yy=`cat nmcdate | cut -c7-10`
mm=`cat nmcdate | cut -c11-12`
dd=`cat nmcdate | cut -c13-14`

### modify namelist file
ystart=`cat nmcdate | cut -c7-10`
mstart=`cat nmcdate | cut -c11-12`
dstart=`cat nmcdate | cut -c13-14`
hstart=`cat nmcdate | cut -c15-16`
cycstart=`cat nmcdate | cut -c7-16`
PDY=`cat nmcdate | cut -c7-14`

start=$ystart$mstart$dstart

end=`/nwprod/util/exec/ndate 0 $cycstart`

yend=`echo $end | cut -c1-4`
mend=`echo $end | cut -c5-6`
dend=`echo $end | cut -c7-8`
hend=`echo $end | cut -c9-10`

cp $GESDIR/ndas.t${cyc}z.input_nemsio_guess_${domain}nest_precoldstart test_input_umo_regional.nemsio.d01

INPUT_FILE="$GESDIR/ndas.t${cyc}z.nmm_b_restart_nemsio.tm03"

INPUT_FILE_TYPE="nems"

# file with the 14 GWD spaces
WRF_BINARY_FILE="test_input_umo_regional.nemsio.d01"

# dont touch this
DYN_CORE="nems"

# your working directory

if [ $domain = firewx ]
then
  FIXDIR=$COMIN
else
  FIXDIR=$FIXnam
fi

WORK_DIR=$DATA
cd $WORK_DIR

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

if [ $domain = firewx ]
then

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="${FIXDIR}/ndas.t${cyc}z.firewx_hpnt_latitudes.grb"
  lons_output_file="${FIXDIR}/ndas.t${cyc}z.firewx_hpnt_longitudes.grb"
  lsmask_output_file="${FIXDIR}/ndas.t${cyc}z.firewx_slmask.grb"
  orog_output_file="${FIXDIR}/ndas.t${cyc}z.firewx_elevtiles.grb"
  substrate_temp_output_file="${FIXDIR}/ndas.t${cyc}z.firewx_tbot.grb"
 /
 &optional_output_fields
  snow_free_albedo_output_file=""
  greenfrc_output_file=""
  mxsnow_alb_output_file=""
  slope_type_output_file=""
  soil_type_output_file=""
  veg_type_output_file="${FIXDIR}/ndas.t${cyc}z.firewx_vegtiles.grb"
  z0_output_file="${FIXDIR}/ndas.t${cyc}z.firewx_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_output = 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.
 /
!

else

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="${FIXDIR}/nam_${domain}nest_hpnt_latitudes.grb"
  lons_output_file="${FIXDIR}/nam_${domain}nest_hpnt_longitudes.grb"
  lsmask_output_file="${FIXDIR}/nam_${domain}nest_slmask.grb"
  orog_output_file="${FIXDIR}/nam_${domain}nest_elevtiles.grb"
  substrate_temp_output_file="${FIXDIR}/nam_${domain}nest_tbot.grb"
 /
 &optional_output_fields
  snow_free_albedo_output_file=""
  greenfrc_output_file=""
  mxsnow_alb_output_file=""
  slope_type_output_file=""
  soil_type_output_file=""
  veg_type_output_file="${FIXDIR}/nam_${domain}nest_vegtiles.grb"
  z0_output_file="${FIXDIR}/nam_${domain}nest_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_output = 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.
 /
!

fi

# Before running coldstart for fire weather nest, check to see if the fire weather nest has all water points
# by running wwgrib.pl to see if the max value of the land/sea mask is 0 or 1.
# If it is zero, skip nam_coldstart execution

if [ $domain = firewx ]
then

export EXECutil=/nwprod/util/exec

$USHnam/wwgrib.pl ${FIXDIR}/ndas.t${cyc}z.firewx_slmask.grb > landcheck.txt
landcheck=`cat landcheck.txt | awk '{print $11}'`

if [ $landcheck -eq 1 ]
then

export pgm=nam_coldstart
. prep_step

startmsg
$EXECnam/nam_coldstart >> $pgmout 2>errfile
export err=$?;err_chk

mv test_input_umo_regional.nemsio.d01 $GESDIR/ndas.t${cyc}z.input_nemsio_guess_${domain}nest_presfcupdate

else

mv test_input_umo_regional.nemsio.d01 $GESDIR/ndas.t${cyc}z.input_nemsio_guess_${domain}nest_presfcupdate

#end landcheck test
fi

else

export pgm=nam_coldstart
. prep_step

startmsg
$EXECnam/nam_coldstart >> $pgmout 2>errfile
export err=$?;err_chk

mv test_input_umo_regional.nemsio.d01 $GESDIR/ndas.t${cyc}z.input_nemsio_guess_${domain}nest_presfcupdate

#end firewx domain test
fi

msg="JOB $job FOR WRF-NMM HAS COMPLETED NORMALLY"
postmsg "$jlogfile" "$msg"

