#!/bin/csh
#---------------------------------------------------------------------------
# helio: Set run parameters and run heliospheric model.
#
# Usage: ./helio [cr] [resolution]
#     [cr] is Carrington rotation -- 1624 < [cr] < 2008
#     [resolution] is either low or medium
#---------------------------------------------------------------------------

# Check parameters
if ($#argv < 2) then
  echo "helio: Set run parameters and run heliospheric model."
  echo ""
  echo "Usage: ./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 "helio: Set run parameters and run heliospheric model."
  echo ""
  echo "Usage: ./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 "helio: Set run parameters and run heliospheric model."
  echo ""
  echo "Usage: ./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 project = .
set numo = mp3m
set par = par1

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

set gamma = 1.5

# Set default parameters
set procs = 1

# 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 run = $bnd.$grd.$procs-$numo.$par

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

# Create the input parameters file
cd $project_dir/run.$run

echo "Ambient Solar Wind - MAS coronal model" > in
echo " &namjob"                    >> in
#echo "  ldir='$project_dir',"     >> in
# Below is to fix the problem of having long pathname
echo "  ldir='..',"                >> in
echo "  lproj='$project',"         >> in
echo "  lgrd='$grd',"              >> in
echo "  lbnd='$bnd',"              >> in
echo "  lcode='$procs-$numo',"     >> in
echo "  lpar='$par',"              >> in
echo "  nslabs=$procs,"            >> in
echo " /"                          >> in
echo "gamma=1.5  vrot=27  cfl=0.4" >> in
echo " &namrun"                    >> in
echo "  vux1=1.496d11,"            >> in
echo "  vutime=86400d0,"           >> in
echo "  tstart=-6d0,"              >> in
echo "  tstop=30d0,"               >> in
echo "  ttfrom=0d0,"               >> in
echo "  ttto=27.2753d0,"           >> in
echo "  ttstep=27.2753d0,"         >> in
echo "  nestep=1,"                 >> in
echo "  x1sel=0.25,0.50,0.75,1.00,">> in
echo "  nsel=4,"                   >> in
echo "  ngeo=5,"                   >> in
echo "  ndstep=1,"                 >> in
echo "  gamma=$gamma,"             >> in
echo "  akcfl=0.4d0,"              >> in
echo "  vrot=0.00015276337d0,"     >> in
echo "  nbndin=1,"                 >> in
echo " /"                          >> in

#Create the scripts
if ($procs == 1) then
  echo "date"                                     > run
  echo "$ENLIL_BIN/code.$code/enlil < in"        >> run
  echo "date"                                    >> run
  chmod u=rwx run
else
  echo "date"                                     > run
  echo "$ENLIL_BIN/code.$code/enlil"             >> run
  echo "date"                                    >> run
  echo "date"                                     > run2
  echo "$ENLIL_BIN/code.$code/mertim"            >> run2
  echo "date"                                    >> run2
endif

./run

exit 0
