#!/bin/csh

set prog = $0
set prog = $prog:t


set tmp_flsp = tmp.$$
#------------------------------------------------------------
#  check op sys
#------------------------------------------------------------
set op_sys = `uname -s | tr '[a-z]' '[A-Z]'`
set os = $op_sys
#------------------------------------------------------------
#  run_mz4 only handles linux, unix, OS X, or AIX op sys
#------------------------------------------------------------
if( $op_sys == LINUX || $op_sys == UNIX || $op_sys == DARWIN ) then
  set op_sys = LINUX
else if( $op_sys != AIX ) then
  echo "$prog only works for Linux, Unix, OS X, or AIX op systems"
  exit 1
endif

if( $op_sys == LINUX ) then
  limit stacksize unlimited >& /dev/null
  limit coredumpsize unlimited >& /dev/null
endif

set cfg_file = `pwd`
set cfg_file = $cfg_file:h
set cfg_file = $cfg_file/configure/MZ4.cfg
#------------------------------------------------------------
#  get configuration parameters and set mz4 root directory
#------------------------------------------------------------
if( ! -e $cfg_file ) then
  echo " "
  echo "*** Can't find configuration file $cfg_file ***"
  echo " "
  cat << EOM
  If you have run the configuration utility, cfg_mz4, then check

  (1) are you in the MZ4ROOT/run directory

  or

  (2) run the configuration utility, cfg_mz4, in MZ4ROOT/configuration;

  $prog terminating
EOM
  echo " "
  exit -1
else
  set cfgs = `cat $cfg_file`
  set MZ4ROOT = $cfgs[4]
  if( ! -d $MZ4ROOT ) then
    echo "Can't find mz4 "root" directory, $MZ4ROOT; $prog terminating"
    exit -1
  endif
endif

set pwd = `pwd`

if( $#argv > 0 ) then
#---------------------------------------
#  process arguments
#---------------------------------------
  set args1 = ($argv)
  set args = `echo $args1 | tr '[a-z]' '[A-Z]'`
#---------------------------------------
#  check for command help
#---------------------------------------
  if( $args[1] == HELP ) then
    set help_file = docs/README.run
    echo " "
    which less >& /dev/null
    if( ! $status ) then
      cat $help_file | less
    else
      which more >& /dev/null
      if( ! $status ) then
        cat $help_file | more
      else
        cat $help_file
      endif
    endif
    echo " "
    exit 0
  endif
#---------------------------------------
#  iterate over arguments
#---------------------------------------
  foreach argument ($args)
    foreach keyword (NP NT MODEL LOG NML DAT EXE DATA_DIR SITE NODES NBQ FULL_NODE BFILE PJNO)
      set sarg = $keyword
      if( $keyword != NBQ && $keyword != FULL_NODE ) then
        set sarg = "$sarg"=
      endif
      echo $argument | grep "$sarg" >& /dev/null
      if( ! $status ) then
        if( $keyword == NP ) then
          set nprocs = `echo $argument | cut -d= -f2`
        else if( $keyword == NT ) then
          set nthrds = `echo $argument | cut -d= -f2`
        else if( $keyword == MODEL ) then
          set model = `echo $argument | cut -d= -f2`
        else if( $keyword == LOG ) then
          set logfile = `echo $args1[1] | cut -d= -f2`
        else if( $keyword == NML ) then
          set namelist = `echo $args1[1] | cut -d= -f2`
        else if( $keyword == DAT ) then
          set datfile = `echo $args1[1] | cut -d= -f2`
        else if( $keyword == EXE ) then
          set exefile = `echo $args1[1] | cut -d= -f2`
        else if( $keyword == DATA_DIR ) then
          set data_dir = `echo $args1[1] | cut -d= -f2`
        else if( $keyword == SITE ) then
          set site = `echo $argument | cut -d= -f2`
        else if( $keyword == PJNO ) then
          set pjno = `echo $argument | cut -d= -f2`
        else if( $keyword == BFILE ) then
          set bfile = `echo $args1[1] | cut -d= -f2`
        else if( $keyword == NODES ) then
          set nodes = `echo $argument | cut -d= -f2`
        else if( $keyword == NBQ ) then
          set batch_override = ""
        else if( $keyword == FULL_NODE ) then
          set use_all_cpus = ""
        endif
        break
      else
        continue
      endif
    end
    shift args1
  end
endif

#------------------------------------------------------------
#  if model= in arguments then check setting
#------------------------------------------------------------
if( $?model ) then
  if( $model != HYB && $model != MPI && $model != OMP && $model != SNG ) then
    echo "$model is not in set {SNG,OMP,MPI,HYB}; $prog terminating"
    exit -1
  endif
else
#------------------------------------------------------------
#  if model= not in arguments then set default
#------------------------------------------------------------
  set model = $cfgs[6]
endif

if( $model == HYB ) then
  set exe_suffix="hyb"
else if( $model == MPI ) then
  set exe_suffix="mpi"
else if( $model == OMP ) then
  set exe_suffix="omp"
else
  set exe_suffix="sng"
endif

if( $?exefile ) then
#------------------------------------------------
#  executable specified via script argument
#------------------------------------------------
  set exe_flnm = $exefile:t
  if( $exe_flnm != $exefile:h ) then
    set exe_dir = $exefile:h
  endif
else
#------------------------------------------------
#  executable not specified in script argument
#------------------------------------------------
  set exe_flnm = mz4_$exe_suffix
endif
if( ! $?exe_dir ) then
  if( $?MZ4ROOT ) then
    set exe_dir = $MZ4ROOT/model/bin
  else
    set exe_dir = $pwd:h
    set exe_dir = $exe_dir/model/bin
  endif
endif

set exe = $exe_dir/$exe_flnm

#------------------------------------------------
#  make sure executable exits
#------------------------------------------------
if( ! -e $exe ) then
  echo "Can't find executable, $exe; $prog terminating"
  exit -1
endif

#-----------------------------------------------
#  setup the simulation namelist and data files
#------------------------------------------------
set usr_nml = 0
set suffix = (nml dat)
foreach file (1 2)
#-----------------------------------------------
#  if file not in arguments then set default
#------------------------------------------------
  if( $suffix[1] == nml ) then
    if( ! $?namelist ) then
      set flnm = $MZ4ROOT/run/mz4.tpl.$suffix[1]
    else
      set flnm = $namelist
      set usr_nml = 1
    endif
  else if( $suffix[1] == dat ) then
    if( ! $?datfile ) then
      set flnm = $MZ4ROOT/run/mz4.tpl.$suffix[1]
    else
      set flnm = $datfile
    endif
  endif
#------------------------------------------------
#  make sure file exits
#------------------------------------------------
  if( ! -e $flnm ) then
    echo "Can't find $suffix[1] file, $flnm; $prog terminating"
    exit -1
  endif
  if( -e mz4.$suffix[1] ) then
    rm -f mz4.$suffix[1] >& /dev/null
  endif
  if( $suffix[1] == dat ) then
    if( $flnm:h == $pwd ) then
      ln -s $flnm:t mz4.dat >& /dev/null
    else
      ln -s $flnm mz4.dat >& /dev/null
    endif
    if( $status ) then
      echo "Can't link $suffix[1] file, $flnm; $prog terminating"
      exit -1
    endif
  else if( $suffix[1] == nml ) then
    set namelist = $flnm
  endif
  shift suffix
end
#------------------------------------------------
#  if data_dir set via argument then check existence
#  otherwise check configuration parameter
#------------------------------------------------
if( ! $?data_dir ) then
  if( $cfgs[7] == NONE ) then
    echo "mozart input data directory not set in the configuration file; $prog terminating"
    echo "run cfg_mz4 and set the data_dir keyword"
    exit -1
  else
    set data_dir = $cfgs[7]
  endif
endif
if( ! -d $data_dir ) then
  echo "can't find mozart input data directory, $data_dir; $prog terminating"
  exit -1
endif
#------------------------------------------------
#  if nml argument check file for MZ_DATA_DIR token
#------------------------------------------------
set dir_replace = 1
if( $usr_nml ) then
  grep MZ_DATA_DIR $namelist >& /dev/null
  if( $status ) then
    set dir_replace = 0
  endif
endif

#------------------------------------------------
#  replace MZ_DATA_DIR holder with actual data directory
#------------------------------------------------
if( $dir_replace ) then
  set cmd_file = xtmp.$$
  set filep = `echo $data_dir | sed 's/\// /g'`
#------------------------------------------------
#  setup the sed command
#------------------------------------------------
  set sed_cmd = "s/MZ_DATA_DIR/\/$filep[1]"
  shift filep
  foreach token ($filep)
    set sed_cmd = $sed_cmd"\/$token"
  end
  set sed_cmd = $sed_cmd"/g"
#------------------------------------------------
#  put sed command in file
#------------------------------------------------
  echo $sed_cmd > $cmd_file
  sed -f $cmd_file $namelist > $tmp_flsp
  if( $status ) then
    rm -f $cmd_file >& /dev/null
    echo "failed to replace MZ_DATA_DIR place holder with actual directory; $prog terminating"
    exit -1
  endif
  rm -f $cmd_file >& /dev/null
  mv -f $tmp_flsp mz4.nml >& /dev/null
  if( $status ) then
    rm -f $tmp_flsp >& /dev/null
    echo "failed to mv $tmp_flsp to mz4.nml; $prog terminating"
    exit -1
  endif
  rm -f $tmp_flsp >& /dev/null
else
  rm -f mz4.nml
  cp $namelist mz4.nml >& /dev/null
  if( $status ) then
    echo "failed to cp $namelist to mz4.nml; $prog terminating"
    exit -1
  endif
endif
#------------------------------------------------------------
#  set the site if not a command arg
#------------------------------------------------------------
if( ! $?site ) then
  set site = $cfgs[5]
endif
#------------------------------------------------------------
#  do archiving?
#------------------------------------------------------------
if( $site == NCAR ) then
  which msrcp >& /dev/null
  set cstat = $status
  if( ! $cstat ) then
    set mss_rt = `grep -i hst_rt mz4.nml`
    set cstat = $status
    if( ! $cstat ) then
      set rtime = `echo $mss_rt | cut -f 2 -d "="`
      set rtime = `echo $rtime | cut -f 1 -d " "`
      set rtime = `echo $rtime | sed "s/'//g"`
      set rtime = `echo $rtime | sed 's/"//g'`
      if( $rtime != 0 ) then
        set do_archive = 1
      else
        set mss_rt = `grep -i rst_rt mz4.nml`
        set cstat = $status
        if( ! $cstat ) then
          set rtime = `echo $mss_rt | cut -f 2 -d "="`
          set rtime = `echo $rtime | cut -f 1 -d " "`
          set rtime = `echo $rtime | sed "s/'//g"`
          set rtime = `echo $rtime | sed 's/"//g'`
          if( $rtime != 0 ) then
            set do_archive = 1
          endif
        endif
      endif
    endif
  endif
endif
#------------------------------------------------------------
#  modify mz4.nml for archiving
#------------------------------------------------------------
if( $?do_archive ) then
  set arch_file = mz4.arch.$$
  set sed_cmd = "s/FILE_STUB/$arch_file/"
else
  set sed_cmd = "s/FILE_STUB/ /"
endif
echo $sed_cmd >! $cmd_file
sed -f $cmd_file mz4.nml > $tmp_flsp
if( $status ) then
  rm -f $cmd_file >& /dev/null
  rm -f $tmp_flsp >& /dev/null
  echo "failed to add archive file to mz4.nml; $prog terminating"
  exit -1
endif
rm -f $cmd_file >& /dev/null
mv -f $tmp_flsp mz4.nml >& /dev/null

#------------------------------------------------------------
#  set compiler and omp stack parameter if model == hyb || omp
#------------------------------------------------------------
set compiler = $cfgs[1]:t

if( $model == HYB || $model == OMP ) then
  if( $compiler == ifort ) then
    set stack_parm = "KMP_STACKSIZE 64m"
  else
    set stack_parm = "OMP_STACK_SIZE 64M"
  endif
endif


#------------------------------------------------------------
#  try to get cpu count per node
#------------------------------------------------------------
if( $os == LINUX ) then
  if( -e /proc/cpuinfo ) then
    set cpus_per_node = `grep -c -i processor /proc/cpuinfo`
  endif
else if( $os == AIX ) then
  which lsdev >& /dev/null
  if( ! $status ) then
    set cpus_per_node = `lsdev|grep -c proc`
  endif
else if( $os == DARWIN ) then
  which system_profiler >& /dev/null
  if( ! $status ) then
    set cpus_per_node = `system_profiler|grep -i 'total number'|sed 's/:/Z/'|cut -dZ -f2`
  endif
endif

#------------------------------------------------------------
#  check for batch queue; skip if nbq(no batch queue) arg set
#------------------------------------------------------------
if( ! $?batch_override ) then
  set batch_que = $cfgs[8]
  if( $batch_que == NONE ) then
    unset batch_que
  endif
endif

#------------------------------------------------------------
#  determine if run_mz4 is running in fore/back ground
#------------------------------------------------------------
set fgnd = ""
if( ! $?batch_que ) then
  if( $op_sys == LINUX ) then
    set ps = `ps -p $$ -o pgid,tpgid`
    if( $ps[3] != $ps[4] ) then
      unset fgnd
    endif
    unset ps
  else if( $op_sys == AIX ) then
    unset fgnd
  endif
endif

#------------------------------------------------------------
#  get model horizontal dimensions
#------------------------------------------------------------
if( $model != SNG ) then
  set flnm = "$MZ4ROOT/model/.make_mz_db/exe_db"
  if( -e $flnm ) then
    set model_dims = (`grep $exe_flnm $flnm`)
    if( ! $status ) then
      set plon = $model_dims[2]
      set plat = $model_dims[3]
      set p_plon = (`./.powers $plon`)
      set p_plat = (`./.powers $plat`)
      set p_proc_max = (`./.div_pwrs $p_plat 2 0`)
      set max_procs = `./.pwr2num $p_proc_max`
#     echo "plat factors are ($p_plat)"
#     echo "plon factors are ($p_plon)"
#     echo "max procs are ($p_proc_max)"
    endif
  endif
#------------------------------------------------------------
#  if have cpu count then factor it into powers of (2,3)
#------------------------------------------------------------
  if( $?cpus_per_node ) then
    set p_cpu_node = (`./.powers $cpus_per_node`)
    set p_thrd_node = ($p_cpu_node)
    if( $op_sys == AIX && $model == HYB && $?batch_que ) then
      @ p_thrd_node[1]++
    endif
#   echo "cpu, thread per node are $p_cpu_node, $p_thrd_node"
  endif
endif

#------------------------------------------------------------
#  force use_all if have queueing
#------------------------------------------------------------
if( $?batch_que ) then
  if( ! $?use_all_cpus ) then
    set use_all_cpus = ""
  endif
endif

#------------------------------------------------------------
#  set p_nodes and nodes
#------------------------------------------------------------
if( $?batch_que && ($model == HYB || $model == MPI) ) then
  if( $?nodes ) then
    set p_nodes = (`./.powers $nodes`)
  else
    set nodes   = 1
    set p_nodes = (0 0)
  endif
else
  set nodes   = 1
  set p_nodes = (0 0)
endif
#------------------------------------------------------------
#  multiple nodes; basic checks
#------------------------------------------------------------
if( $nodes > 1 ) then
  set p_tmp = `(./.div_pwrs $p_plat $p_nodes)`
  ./.valid_pwr $p_tmp
  if( ! $status ) then
    echo "can't evenly distribute $plat latitudes on $nodes nodes; $prog terminating"
    exit -1
  endif
else
  set p_tmp = (0 0)
endif

#------------------------------------------------------------
#  setup horizontal spatial domain decomposition
#------------------------------------------------------------
if( $?cpus_per_node ) then
#------------------------------------------------------------
#  in the following p_tmp is the power(2,3) representation
#  of the total threads per node,  This is just the number
#  of the cpus per node except for SMT where this is doubled
#------------------------------------------------------------
  if( $model == HYB ) then
    set p_tmp = ($p_thrd_node)
  else if( $model != SNG ) then
    set p_tmp = ($p_cpu_node)
  endif
  set pwr2 = $p_tmp[1]
  if( ! $?use_all_cpus ) then
    @ pwr2--
    set p_tmp[1] = `./.maxval $pwr2 0`
  endif
  switch ($model)
    case MPI
      set p_wrk = (`./.mpi_procs $p_tmp $p_plat $p_proc_max`)
      if( ! $?nprocs ) then
        ./.valid_pwr $p_wrk
        if( $status ) then
          set nprocs = `./.pwr2num $p_wrk`
        endif
      else
        set wrk = `./.pwr2num $p_wrk`
        if( $nprocs > $wrk ) then
          echo "requested mpi procs, $nprocs, > max procs, $wrk ; $prog terminating"
          exit -1
        else
          @ wrk = $plat % $nprocs
          if( $wrk != 0 ) then
            echo "can't evenly distribute $plat latitudes on $nprocs mpi procs; $prog terminating"
            exit -1
          endif
        endif
      endif
#------------------------------------------------------------
#  check for multiple nodes
#------------------------------------------------------------
      if( $nodes > 1 ) then
        @ wrk = $nodes * $nprocs
        if( $wrk > $max_procs ) then
          echo "total mpi procs, $wrk, > max procs, $max_procs ; $prog terminating"
          exit -1
        endif
      endif
      if( $?fgnd ) then
        @ wrk = $nodes * $nprocs
        echo "MPI model will use $wrk mpi procs on $nodes nodes(s)"
      endif
      breaksw
    case OMP
      if( ! $?nthrds ) then
        if( $op_sys == LINUX ) then
          set pwr2 = 2
        else
          set pwr2 = 3
        endif
        set pwr2 = `./.minval $p_tmp[1] $pwr2`
        set p_wrk = ($pwr2 0)
        set p_thrds = (`./.min_pwr $p_tmp $p_wrk`)
        set nthrds = `./.pwr2num $p_thrds`
      endif
      if( $?fgnd ) then
        echo " "
        echo "OMP model will use $nthrds threads"
      endif
      breaksw
    case HYB
      if( ! $?nprocs || ! $?nthrds ) then
        set p_wrk = ($p_plat)
        if( $nodes > 1 ) then
          set p_wrk = (`./.div_pwrs $p_plat $p_nodes`)
        endif
        set proc_thrd = (`./.hyb_proc_thrd $p_tmp $p_wrk`)
        set p_tmp = ($proc_thrd[1] $proc_thrd[2])
        ./.valid_pwr $p_tmp
        if( $status ) then
          set nprocs = `./.pwr2num $p_tmp`
          set p_tmp = ($proc_thrd[3] $proc_thrd[4])
          ./.valid_pwr $p_tmp
          if( $status ) then
            set nthrds = `./.pwr2num $p_tmp`
          endif
        else
          echo "can't partition horizontal grid( $plon x $plat ) for hybrid model; $prog terminating"
          exit -1
        endif
        if( $?nprocs && $?nthrds ) then
          set p_procs = ($proc_thrd[1] $proc_thrd[2])
          set p_thrds = ($proc_thrd[3] $proc_thrd[4])
          set p_tmp = `./.div_pwrs $p_plat $p_nodes`
          set p_tmp = `./.div_pwrs $p_tmp $p_procs`
          set lat_proc = `./.pwr2num $p_tmp`
          if( $?fgnd ) then
            echo " "
            echo "HYB model will use $nprocs procs each with $nthrds threads and $lat_proc latitudes on $nodes node(s)"
          endif
        endif
      endif
  endsw
else
#------------------------------------------------------------
#  couldn't get cpu information; assume 8 total cpus
#------------------------------------------------------------
  if( $model != SNG ) then
    cat << EOM

       Warning : $prog could not retrive cpu count per node for this machine
 
                 $prog will assume there are 8 cpus

EOM
    echo -n "Do you want to continue with $prog (y == yes) : "
    set ans = $<
    if( "$ans" != "y" ) then
      echo "$prog terminating"
      exit -1
    endif
    if( $model == HYB || $model == MPI ) then
      if( ! $?nprocs ) then
        if( $model == HYB ) then
          set nprocs = 2
        else
          set nprocs = 4
        endif
      endif
    endif
    if( $model == HYB || $model == OMP ) then
      if( ! $?nthrds ) then
        if( $model == HYB ) then
          set nthrds = 2
        else
          set nthrds = 4
        endif
      endif
    endif
  endif
endif

#------------------------------------------------
#  following only if no batch queue
#------------------------------------------------
if( ! $?batch_que ) then
  set runc = ""
#------------------------------------------------
#  check mpirun
#------------------------------------------------
  if( $model == HYB || $model == MPI ) then
    if( $cfgs[3] != NONE ) then
      set mpirun_cmd = $cfgs[3]:h
      set mpirun_cmd = "$mpirun_cmd/mpirun"
    else
      which mpirun >& /dev/null
      if( ! $status ) then
        set mpirun_cmd = `which mpirun`
      endif
    endif
    if( ! $?mpirun_cmd ) then
      echo "Can't find mpirun; $prog terminating"
      exit -1
    else if( ! -e $mpirun_cmd ) then
      echo "Can't find mpirun; $prog terminating"
      exit -1
    endif
  endif

#------------------------------------------------
#  setup run paramters
#------------------------------------------------
  switch( $model )
    case HYB:
      setenv OMP_NUM_THREADS $nthrds
      setenv $stack_parm
      set runc="$mpirun_cmd -np $nprocs"
      set exe_suffix="hyb"
      breaksw
    case MPI:
      set runc="$mpirun_cmd -np $nprocs"
      set exe_suffix="mpi"
      breaksw
    case OMP:
      setenv OMP_NUM_THREADS $nthrds
      setenv $stack_parm
      set exe_suffix="omp"
      breaksw
    case SNG:
      set exe_suffix="sng"
  endsw
#------------------------------------------------
#  check for logfile file
#------------------------------------------------
  if( $?logfile ) then
    if( "$logfile" == "" ) then
      set logfile = mz4_$exe_suffix.out
    endif
  else
    set logfile = mz4_$exe_suffix.out
  endif
#------------------------------------------------
#  make sure rest, hist, arch subdirectories exist
#------------------------------------------------
  foreach dir (rest hist arch)
    if( ! -d $dir ) then
      mkdir $dir >& /dev/null
      if( $status ) then
        echo "$dir directory doesn't exist and mkdir failed; $prog terminating"
        exit -1
      endif
    endif
  end
endif

#------------------------------------------------
#  make sim.params.nml file
#------------------------------------------------
set sim_flnm = "sim.params.nml"
if( -e $sim_flnm ) then
  rm -f $sim_flnm >& /dev/null
endif
echo " &simulation_params" > $sim_flnm
if( $model == HYB ) then
  set l_tiles = 8
else
  set l_tiles = 1
endif
echo "  longitude_tiles = $l_tiles" >> $sim_flnm
echo " /" >> $sim_flnm

if( $model != SNG ) then
#------------------------------------------------------------
#  check longitudes
#------------------------------------------------------------
  if( $?plon && $l_tiles != 1 ) then
    @ n = $plon % $l_tiles
    if( $n != 0 ) then
      cat << EOM
          
          model longitudes, $plon, are not evenly divisible by $l_tiles; $prog terminating
          $prog terminating
          
EOM
      exit -1
    endif
  endif
endif

set sub_cmd = `echo $cfgs[8] | tr '[a-z]' '[A-Z]'`
if( $sub_cmd == QSUB && ($model == HYB || $model == MPI) ) then
  if( $cfgs[3] == NONE ) then
    echo "mpirun, required by the batch queue qsub, can't be found; $prog terminating"
    exit -1
  endif
endif
#------------------------------------------------------------
#  if not batch job check for mpdboot
#------------------------------------------------------------
if( ! $?batch_que ) then
  if( $op_sys == LINUX ) then
    if( $model == HYB || $model == MPI ) then
      if( $cfgs[3] != NONE ) then
        set mpdboot_cmd = $cfgs[3]:h
        set mpdboot_cmd = "$mpdboot_cmd/mpdboot"
      else
        which mpdboot >& /dev/null
        if( ! $status ) then
          set mpdboot_cmd = `which mpdboot`
        endif
      endif
      if( ! $?mpdboot_cmd ) then
        echo "Can't find mpdboot; $prog terminating"
        exit -1
      else if( ! -e $mpdboot_cmd ) then
        echo "Can't find mpdboot; $prog terminating"
        exit -1
      endif
      $mpdboot_cmd >& /dev/null
      if( $status ) then
        echo "$mpdboot_cmd returned error code = $status; $prog terminating"
        exit -1
      endif
    endif
  endif
#------------------------------------------------
#  run the simulation
#------------------------------------------------
  if( $?logfile ) then
    if( $?fgnd ) then
      echo " "
      echo "Running $exe in foreground"
      echo " (a) log output to file $logfile"
      echo " "
      echo "(enter crtl-c to kill simulation)"
      echo " "
    endif
    if( -e $logfile ) then
      rm -f $logfile >& /dev/null
    endif
    $runc $exe mz4.nml< /dev/null >&! $logfile
  else
    $runc $exe mz4.nml < /dev/null
  endif
#------------------------------------------------
#  if archiving then do it
#------------------------------------------------
  if( $?do_archive ) then
    if( -e scrpts/NCAR/arch.scrpt ) then
      rm -f scrpts/NCAR/arch.scrpt
    endif
    cat << EOS > scrpts/NCAR/arch.scrpt
#!/bin/csh
set arch_out = arch/arch.out.$$
set arch_file = $arch_file
EOS
    cat scrpts/NCAR/arch.tpl.scrpt >> scrpts/NCAR/arch.scrpt
    chmod 700 scrpts/NCAR/arch.scrpt >& /dev/null   
    scrpts/NCAR/arch.scrpt
    if( $status ) then
      exit -1
    endif
  endif
else
#------------------------------------------------------------
#  setup and submit to the batch queue
#------------------------------------------------------------
#------------------------------------------------------------
#  check for template batch file override
#------------------------------------------------------------
  if( $?bfile ) then
    if( ! -e $bfile ) then
      echo "can't find batch queue file, $bfile; $prog terminating"
      exit -1
    endif
  endif
#------------------------------------------------------------
#  check the batch command
#------------------------------------------------------------
  set sub_cmd = $cfgs[8]
  if( $sub_cmd == NONE ) then
    echo "batch queue submission command not set in configuration; $prog terminating"
    exit -1
  endif
  if( $site == 'NCAR' ) then
    if( ! $?bfile ) then
#------------------------------------------------------------
#  no batch file input arg
#------------------------------------------------------------
      cat scrpts/NCAR/sim.tpl.scrpt > $tmp_flsp
    else
#------------------------------------------------------------
#  batch file input arg
#------------------------------------------------------------
      cat $bfile > $tmp_flsp
    endif
    if( ! $?pjno ) then
      set pjno = `grep -i $LOGNAME /etc/project.ncar | cut -f 1 -d "," | cut -f 2 -d ":"`
      if( $status ) then
        set pjno = "NONE"
      endif
    endif
#------------------------------------------------------------
#  edit batch file
#------------------------------------------------------------
    set tmp_flnm = `./.ncar_batch $nprocs $nthrds $nodes $pjno $tmp_flsp $exe_flnm $exe`
    chmod 600 $tmp_flnm >& /dev/null   
#------------------------------------------------------------
#  prepare archive script if archiving
#------------------------------------------------------------
    if( $?do_archive ) then
      if( -e scrpts/NCAR/arch.scrpt ) then
        rm -f scrpts/NCAR/arch.scrpt
      endif
      cat << EOS > scrpts/NCAR/arch.scrpt
#!/bin/csh
#BSUB -n 1
#BSUB -J mz4.arch
#BSUB -o mz4.arch.out
#BSUB -e mz4.arch.err
#BSUB -q share
#BSUB -W 0:03
#BSUB -P $pjno

EOS
      echo "set arch_out = arch/arch.out.$$" >> scrpts/NCAR/arch.scrpt
      echo "set arch_file = $arch_file" >> scrpts/NCAR/arch.scrpt
      echo "cd $TMPDIR/mozart/run/$exe_flnm" >> scrpts/NCAR/arch.scrpt
      cat scrpts/NCAR/arch.tpl.scrpt >> scrpts/NCAR/arch.scrpt
      cat >> $tmp_flnm << EOS

#----------------------------------------
# submit the archiver
#----------------------------------------
$sub_cmd < $pwd/scrpts/NCAR/arch.scrpt
EOS
#     echo "$sub_cmd < $pwd/scrpts/NCAR/arch.scrpt" >> $tmp_flnm
    endif
    if( $?fgnd ) then
      echo " "
      echo "submitting batch script file $tmp_flnm for execution"
      echo "via command: $sub_cmd"
      echo " "
      echo "history file output in $TMPDIR/mozart/run/$exe_flnm/hist"
      echo "restart file output in $TMPDIR/mozart/run/$exe_flnm/rest"
      echo " "
    endif
  else
#------------------------------------------------------------
#  all other batch queues; presently no auto parameter settings
#------------------------------------------------------------
    set tmp_flnm = "batch_file.$$"
    cat $bfile > $tmp_flnm
    chmod 600 $tmp_flnm >& /dev/null   
    if( $?fgnd ) then
      echo " "
      echo "submitting batch script file $tmp_flnm for execution"
      echo "via command: $sub_cmd"
      echo " "
    endif
  endif
#------------------------------------------------------------
#  submit to batch queue
#------------------------------------------------------------
  if( $sub_cmd == bsub ) then
    $sub_cmd < $tmp_flnm
  else
    $sub_cmd $tmp_flnm
  endif
endif

exit 0
