#!/bin/csh
#---------------------------------------------------------------------------
# run_corhel_batch: Run CORHEL in batch mode.
#---------------------------------------------------------------------------

# Set environment variables
setenv CORHEL_ROOT "`pwd`"
setenv CORHEL_DIR "$CORHEL_ROOT"
setenv CORHEL_BIN "$CORHEL_ROOT/bin"


# Check system platform
if (`uname` == 'Linux') then
  set system = "linux"
else if (`uname` == 'Darwin') then
  set system = "macosx"
else
  $CORHEL_BIN/error_msg 1
  exit 1
endif


# Check and set parameters
echo ""
echo "Check input parameters ..."
if ($#argv < 4) then
  $CORHEL_BIN/error_msg 0
  exit 1
endif

set observ = 'kittpeak'
set mmmax = 9
set ebmin = 0.25
set ebmax = 0.45
set epoint = 0.35
set plotlist = "'vr','vt','vp','br','bt','bp','rho','t','p'"
set tmax = 120
set rl = 30
set tpltxint = $tmax
set dtmax = 0.05
set dtmin = 0.005
set lund = 1.e3
set visc = 0.01
set density = 2.0
set temp = 1.8e6

set id = 1
while ($id <= $#argv)
  switch ($argv[$id])
    case -cr:
      @ id++
      set cr = $argv[$id]
      breaksw
    case -res:
      @ id++
      set res = $argv[$id]
      breaksw
    case -observ:
      @ id++
      set observ = $argv[$id]
      breaksw
    case -mmmax:
      @ id++
      set mmmax = $argv[$id]
      breaksw
    case -ebmin:
      @ id++
      set ebmin = $argv[$id]
      breaksw
    case -ebmax:
      @ id++
      set ebmax = $argv[$id]
      breaksw
    case -epoint:
      @ id++
      set epoint = $argv[$id]
      breaksw
    case -plotlist:
      @ id++
      set plotlist = $argv[$id]
      set plot = `echo $plotlist | awk '{ split ($0, a, ",");\
                  for (i = 1; i <= 15; i++) print a[i] }'`

      foreach str ($plot)
        if ($str == "vr") then
          set vr = $str
        else if ($str == "vt") then
          set vt = $str
        else if ($str == "vp") then
          set vp = $str
        else if ($str == "br") then
          set br = $str
        else if ($str == "bt") then
          set bt = $str
        else if ($str == "bp") then
          set bp = $str
        else if ($str == "rho") then
          set rho = $str
        else if ($str == "t") then
          set t = $str
        else if ($str == "p") then
          set p = $str
        else if ($str == "jr") then
          set jr = $str
        else if ($str == "jt") then
          set jt = $str
        else if ($str == "jp") then
          set jp = $str
        else if ($str == "ar") then
          set ar = $str
        else if ($str == "at") then
          set at = $str
        else if ($str == "ap") then
          set ap = $str
        else
          $CORHEL_BIN/error_msg 4 $str
          exit 1
        endif
      end

      set plotlist = ""
      foreach str ($plot)
        if ($str == $plot[$#plot]) then
          set plotlist = "${plotlist}'${str}'"
        else
          set plotlist = "${plotlist}'${str}',"
        endif
      end
      breaksw
    case -tmax:
      @ id++
      set tmax = $argv[$id]
      breaksw
    case -rl:
      @ id++
      set rl = $argv[$id]
      breaksw
    case -tpltxint:
      @ id++
      set tpltxint = $argv[$id]
      breaksw
    case -dtmax:
      @ id++
      set dtmax = $argv[$id]
      breaksw
    case -dtmin:
      @ id++
      set dtmin = $argv[$id]
      breaksw
    case -lund:
      @ id++
      set lund = $argv[$id]
      breaksw
    case -visc:
      @ id++
      set visc = $argv[$id]
      breaksw
    case -rho:
      @ id ++
      set density = $argv[$id]
      breaksw
    case -temp:
      @ id ++
      set temp = $argv[$id]
      breaksw
    default:
      $CORHEL_BIN/error_msg 5 $argv[$id]
      exit 1
  endsw
  @ id++
end

if ($?cr == 0) then
  $CORHEL_BIN/error_msg 2
  exit 1
else if ($cr < 1625) then
  set cr = 1625
else if ($cr > 2007 && $cr <= 2010) then
  set cr = 2007
endif

if ($?res == 0) then
  $CORHEL_BIN/error_msg 3
  exit 1
else if ($res != 'low' && $res != 'medium') then
  set res = 'low'
endif

if ($observ != 'kittpeak' && $observ != 'wilcox') then
  set observ = 'kittpeak'
endif

if ($mmmax < 0) then
  set mmmax = 0
else if ($mmmax > 20) then
  set mmmax = 20
endif

if ($tmax < 1) then
  set tmax = 1
else if ($tmax > 200) then
  set tmax = 200
endif

if ($rl < 1) then
  set rl = 1
else if ($rl > 30) then
  set rl = 30
endif

if ($tpltxint < 1) then
  set tpltxint = 1
else if ($tpltxint > $tmax) then
  set tpltxint = $tmax
endif

echo "Input parameters:"
echo "  Carrington rotation   : $cr"
echo "  Resolution            : $res"
echo "  Observatory           : $observ"
echo "  Max mode              : $mmmax"
echo "  Extrapolation band min: $ebmin"
echo "  Extrapolation band max: $ebmax"
echo "  Extrapolation point   : $epoint"
echo "  Plotlist              : $plotlist"
echo "  Maximum time          : $tmax"
echo "  Outer radial boundary : $rl"
echo "  Output interval       : $tpltxint"
echo "  dt Max                : $dtmax"
echo "  dt Min                : $dtmin"
echo "  Lundquist number      : $lund"
echo "  Viscosity             : $visc"
echo "  Density               : $density"
echo "  Temperature           : $temp"
echo ""


$CORHEL_BIN/corhel $cr $res
if ($status != 0) then
  echo "Fail: $CORHEL_BIN/corhel"
  exit 1
endif

$CORHEL_BIN/prep_corona $cr $res $observ $mmmax $ebmin $ebmax $epoint
if ($status != 0) then
  echo "Fail: $CORHEL_BIN/prep_corona"
  exit 1
endif

$CORHEL_BIN/create_imas $cr $res $mmmax $plotlist $tmax $rl $tpltxint\
$dtmax $dtmin $lund $visc $density $temp
if ($status != 0) then
  echo "Fail: $CORHEL_BIN/create_imas"
  exit 1
endif

$CORHEL_BIN/corona $cr $res
if ($status != 0) then
  echo "Fail: $CORHEL_BIN/corona"
  exit 1
endif

$CORHEL_BIN/prep_helio $cr $res $tmax $tpltxint
if ($status != 0) then
  echo "Fail: $CORHEL_BIN/prep_helio"
  exit 1
endif

$CORHEL_BIN/helio_cr $cr $res
if ($status != 0) then
  echo "Fail: $CORHEL_BIN/helio_cr"
  exit 1
endif

exit 0
