#!/bin/csh
#---------------------------------------------------------------------------
# create_imas: Create an input file for coronal run.
#
# Usage      : create_imas <Carrington rotation> <resolution> <max mode>
#                          <output parameters> <max time>
#                          <outer radial boundary> <output interval>
#                          <dt max> <dt min> <Lundquist number> <viscosity>
#                          <density> <temperature>
#---------------------------------------------------------------------------

# Set parameters
set cr = $1
set res = $2
set mmmax = $3
set plotlist = $4
set tmax = $5
set rl = $6
@ rl -= 1
set tpltxint = $7
set dtmax = $8
set dtmin = $9
set lund = $10
set visc = $11
set density = $12
set temp = $13

# Create modestring from mmmax value
set id = 0
set modestring = "${id}"
while ($id < $mmmax)
  @ id++
  set modestring = "${modestring},${id}"
end

# Split plotlist if it has more than 9 plots
set plot = `echo $plotlist | awk '{ split ($0, a, ",");\
                  for (i = 1; i <= 15; i++) print a[i] }'`
if ($#plot > 9) then
  set plotlist = ""
  set id = 1
  while ($id <= 9)
    if ($id == 9) then
      set plotlist = "${plotlist}${plot[9]}"
    else
      set plotlist = "${plotlist}${plot[$id]},"
    endif
    @ id++
  end

  set plotlist2 = ""
  while ($id <= $#plot)
    if ($id == $#plot) then
      set plotlist2 = "${plotlist2}${plot[$#plot]}"
    else                    
      set plotlist2 = "${plotlist2}${plot[$id]},"
    endif
    @ id++
  end
endif


# Set environment variables
setenv CORHEL_DIR "$CORHEL_ROOT"
setenv CORHEL_BIN "$CORHEL_ROOT/bin"
setenv CORHEL_RUN "$CORHEL_ROOT/run/cr$cr-$res"


# Create imas file
cd $CORHEL_RUN/corona

echo " &invars"                                                         >> imas
echo " option='streamer'"                                               >> imas
echo " fldtype='potential' bingauss=.true. bnfile='br.m9.filt3.dat'"    >> imas
echo " eqtype='parker' np1d=130 onedfile='parker1.8.pw' rhor0=$density" >> imas
echo " bcr0type='1dchar' bcr1type='1dchar'"                             >> imas
echo " tmax=$tmax ntmax=40000 dtmax=$dtmax dtmin=$dtmin"                >> imas
echo " ifideal=0 slund=$lund visc=$visc"                                >> imas
echo " rsifile=' '"                                                     >> imas
echo " rl=$rl. g0=.823 ifrho=1 iftemp=1 ifvdgv=1 ifpc=1"                >> imas
echo " rfrac=.4,1. drratio=60.,4. nfrmesh=5"                            >> imas
echo " tfrac=.5,1. dtratio=.5,2. nftmesh=5"                             >> imas
echo " mmodes=$modestring"                                              >> imas
echo " tpltxint=$tpltxint. ihistint=1 ifprec=1 trsdump=20."             >> imas
echo " upwindv=1. cfl=.4 isitype=1"                                     >> imas
echo " dformat='hdf'"                                                   >> imas
echo " plotlist=$plotlist"                                              >> imas
if ($#plot > 9) then
  echo " $plotlist2"                                                    >> imas
endif
echo " tnode=0.,10.0 vnode=0.,0. ishearprof=2"                          >> imas
echo " arotate=1. brotate=0. crotate=0."                                >> imas
echo " ihst=23 jhst=33 khst=1"                                          >> imas
echo " parchar=.false. ubzero=.true. nfiltub=3"                         >> imas
echo " he_frac=0. radloss=0. tcond=0. ifaw=0"                           >> imas
echo " tnode_ch=0.,500. q0phys_ch=0.,0."                                >> imas
echo " tbc0=$temp tbc1=0."                                              >> imas
echo " upwinda=0. upwindar=1. upwindat=1. upwindap=1."                  >> imas
echo " upwindphi=1. strmax=.4"                                          >> imas
echo " fdphi=.false."                                                   >> imas
echo " si_aggressive=.true."                                            >> imas
echo " /"                                                               >> imas

exit 0
