#!/bin/csh
#---------------------------------------------------------------------------
# prep_helio: Set case parameters and generate boundary data.
#
# Usage: ./prep_helio [cr] [resolution]
#     [cr] is Carrington rotation -- 1624 < [cr] < 2008
#     [resolution] is either low or medium
#---------------------------------------------------------------------------

# Check parameters
if ($#argv < 2) then
  echo "prep_helio: Set case parameters and generate boundary data."
  echo ""
  echo "Usage: ./prep_helio [cr] [resolution]"
  echo "    [cr] is Carrington rotation -- 1624 < [cr] < 2008"
  echo "    [resolution] is either low or medium"
  exit 1
endif

if ($1 < 1625) then
  echo "prep_helio: Set case parameters and generate boundary data."
  echo ""
  echo "Usage: ./prep_helio [cr] [resolution]"
  echo "    [cr] is Carrington rotation -- 1624 < [cr] < 2008"
  echo "    [resolution] is either low or medium"
  exit 1
endif

if ($2 != 'low' && $2 != 'medium') then
  echo "prep_helio: Set case parameters and generate boundary data."
  echo ""
  echo "Usage: ./prep_helio [cr] [resolution]"
  echo "    [cr] is Carrington rotation -- 1624 < [cr] < 2008"
  echo "    [resolution] is either low or medium"
  exit 1
endif

# Set environment variables
pushd $CORHEL_DIR/tools/helio > /dev/null
source ./set.sh run
popd > /dev/null

set project_dir = $CORHEL_RUN/helio
set numo = mp3m

# Set parameters
set cr = $1
set resolution = $2

set dfast = 150
set tfast = 0.7
set dvexp = 2

# Set default parameters
set procs = 1

set x1l = 0.14
set x1r = 1.07
set x2l = 30
set x2r = 150
set x3l = 0
set x3r = 360

set vfast=650
set bfast=170

set nbrad = 2

# Set auxiliary parameters
if ($resolution == 'low') then
  set n1 = 128
  set n2 = 60
  set n3 = 90
else
  set n1 = 256
  set n2 = 120
  set n3 = 180
endif

set bnd=$cr'a2'

set grd = $n1'x'$n2'x'$n3
set code = $procs-$numo.$grd
set case = $bnd.$grd

# Make directories
test -d $project_dir || mkdir $project_dir
test -d $project_dir/case.$case || mkdir $project_dir/case.$case

cd $project_dir/case.$case

echo "Ambient Solar Wind - MAS coronal model"   > in
echo " &namdat"                         >> in   
echo "  ldir='$ENLIL_DATA',"            >> in   
echo "  ldata='$cr',"                   >> in   
echo "  lgrd='$grd',"                   >> in   
echo "  lbnd='$bnd',"                   >> in   
echo "  x1l=$x1l,"                      >> in   
echo "  x1r=$x1r,"                      >> in   
echo "  x2l=$x2l,"                      >> in   
echo "  x2r=$x2r,"                      >> in   
echo "  x3l=$x3l,"                      >> in   
echo "  x3r=$x3r,"                      >> in   
echo "  dfast=$dfast,"                  >> in   
echo "  tfast=$tfast,"                  >> in   
echo "  vfast=$vfast,"                  >> in   
echo "  bfast=$bfast,"                  >> in   
echo "  dvexp=$dvexp,"                  >> in   
echo "  nbrad=$nbrad,"                  >> in   
echo " /"                               >> in   

$ENLIL_BIN/code.$code/mas2bc < in

# If IDL is installed then display the boundary condition
set idl = `which idl`
if ($#idl == 1) then 
  echo ".r plot_amb3"                             > plot.idl
  echo "plot_amb3,'$project_dir','$bnd','$grd'"  >> plot.idl
  echo "exit"                                    >> plot.idl

  idl < plot.idl
  /bin/rm plot.idl

  display case.png
endif

exit 0
