#!/usr/bin/env bash

script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd ${script_dir}

if [ ! -d "${OPENGGCMDIR}" ]; then
  export OPENGGCMDIR=${script_dir}/../..
fi

#-----check that utils were built for same code path
has_git=$(which git &> /dev/null; echo "$?")
if [ "${has_git}" -eq 0 ]; then
  gitinfo="gitinfo.txt"
  echo "git status:" > ${gitinfo}
  echo "$(cd ${OPENGGCMDIR} && git status)" >> ${gitinfo}
  echo "" >> ${gitinfo}
  echo "git branch:" >> ${gitinfo}
  echo "$(cd ${OPENGGCMDIR} && git branch -v)" >> ${gitinfo}
  echo "" >> ${gitinfo}
  echo "git log:" >> ${gitinfo}
  echo "$(cd ${OPENGGCMDIR} && git log -n20)" >> ${gitinfo}
  echo "" >> ${gitinfo}
  echo "git diff:" >> ${gitinfo}
  echo "$(cd ${OPENGGCMDIR} && git diff -U0)" >> ${gitinfo}
fi

if [ "`uname`" == "Darwin" ]; then
  export GGCM_TEST_TARGET="macbook"
else
  export GGCM_TEST_TARGET="local"
fi

$OPENGGCMDIR/bin/script.runme $(pwd) $(basename $0) 0; exit

# WDC: runme derived from input.defines in master (b3cadd)

#---------------------------------------------------------------
#  precompilation actions and basic switches
#---------------------------------------------------------------
RUN              default  # run id, default sets to current dir (def=$V=`pwd`;chop($V);$V=`basename $V`;chop($V);$v=$V;)
OUTDIR           default  # The directory on the target machine where the OpenGGCM run directory will be created. (def=./)
TARGETDIR        default  # Another directory added after outdir if target is not localhost. Useful on fishercat if you do not want output in home. (def=./)
DOSMF            1        # Switch that determines whether or not a parameter database is created. Such database is not needed for the program to run, but later for post-processing. (def=on)
DOINP            1        # Switch that determinse whether or not the solar wind and IMF input file is generated. (def=on)
DOGRD            1        # Switch that determinse whether or not the grid is generated. Should always be 'on', unless there is a good reason to switch this off. (def=on)
KEEPTMP          0        # switch to keep tmp files for debugging (def=0)
GGCMDIR          default  # Full path of where the sources reside. (def= $ENV"OPENGGCMDIR")
TARGET           {$ENV{"GGCM_TEST_TARGET"}}  # The target for building and submitting the code (def=zaphod-myri)
TARGETHOST       localhost  # The host that the code will be built and submitted on. (def=none)
TUSER            dcramer  # The username to use on the target machine. (def= $ENV"USER")
TARGETPREP       default  # Place to add commands that need to be run on the target before starting the build process, e.g. "module load pgi". (def=none)
RUNTIME          00:10:00  # The amount of wallclock time to allocate for the job. (def=none)
CLASS            default  # (JR:workq) batch class (low,normal,high) (def=normal)
PRECISION        default  #  (def=single)
MHDCOORD         MHD      # Coordinate system used for the MHD equations, either MHD, which is GSE with x and y axis flipped, or SM (solar magnetic) in which the dipole axis is always along the z axis, but the solar wind input wobbles. (def=MHD)
MHD              default  # Use this MHD solver (fortran/...). (def=fortran)
SATOUT           default  # Include code to output satellite data. (def=true)
TIMELO           default  # (WDC:100,JR:500) Up to this time, the Harten-Zwas switch is disabled and we always use low order. (def=240.0)
CALCE_ASPECT_LOW  default  # switch to 1. order E-fluxes where cell aspect ratio is high (def=false)
LIMIT_ASPECT_LOW  true    # (WDC:true necessary to run 2/27/97 CIR to completion) switch to 1. order fluid fluxes where cell aspect ratio is high (def=false)
DO_LIMIT2        default  # Use the limit2 limiter. (def=false)
DO_LIMIT3        false    # (WDC:false,JR:false) Use the limit3 limiter. (def=true)
DO_LEGACY        false    # Use exactly the same MHD timestepping as in OpenGGCM 3.1. (def=false)
CONF_ARGS        default  # (JR:{ --enable-sse2 }) Additional arguments to pass to configure. (def=none)
CMDLINE_ARGS     default  # If the target file uses this, cmdline args for openggcm.exe (def=none)

#---------------------------------------------------------------
#------------- profiling and debugging -------------------------
#---------------------------------------------------------------
LOCALF77         gfortran # the local f77 compiler (def=pgf77 -fast)
MPIF77           default  # The name of the MPI F77 compiler on the target machine. (def=mpif77)
MPIF77_FLAGS     default  # Flags to pass to mpif77. (def= $mpif77opt )
MPIF90           default  # The name of the MPI F90 compiler on the target machine. (def=mpif90)
MPIF90_FLAGS     default  # Flags to pass to mpif90. (def=none)
MPICC            default  # The name of the MPI C compiler on the target machine. (def=mpicc)
MPICC_FLAGS      default  # Flags to pass to mpicc. (def=none)
MPIF77OPT        default  # mpif77 options, deprecated -- use mpif77flags. (def=none)
DEBUG_LEVEL      default  #  (def=0)
CHECK_MHDVAR     false    # (WDC:false,JR:false) Check mhd variables for sane values every step. (def=true)
CHECKSUMS        default  # Print out checksums to verify code changes. (def=false)
TIMEOUT_WARN     default  # Warn after a communication waits for XX secs (def=1800)
TIMEOUT_ERROR    default  # Kill the run after a communication waits for XX secs (def=3600)

#---------------------------------------------------------------
#------ the input source files
#---------------------------------------------------------------
GLOBU            default  # code input (utilities) (def="$ggcmdir/src/" . `$ggcmdir/bin/script.latest glob-util*for`)
GRIDA            default  # grid/x-grid generation (def="$ggcmdir/src/" . `$ggcmdir/bin/script.latest mhd-grida*for`)
GRIDB            default  # grid/partitioning (def="$ggcmdir/src/" . `$ggcmdir/bin/script.latest mhd-gridb*for`)
GRIDC            default  # grid/mapping coefficients (def="$ggcmdir/src/" . `$ggcmdir/bin/script.latest mhd-gridc*for`)
MHDSC            default  # constants and definitions (def="$ggcmdir/src/" . `$ggcmdir/bin/script.latest mhd-scon*for`)
COTR1            default  # coord. transf. and time routines (def="$ggcmdir/src/" . `$ggcmdir/bin/script.latest mhd-cotr*for`)
MINVA            default  # minimum variance program (def="$ggcmdir/src/" . `$ggcmdir/bin/script.latest sw-minvar*for`)
CTIM             none     # ctim version (1.0.10) or "none" for sigaa2 sigma computation (def="$ggcmdir/src/" . `$ggcmdir/bin/script.latest ctim-core.1*`)

#---------------------------------------------------------------
#------  grid definition
#---------------------------------------------------------------
NPX              2        # nx must be divisible by this (def=1)
NPY              1        # 2*ny must be divisible by this (def=1)
NPZ              1        # 2*nz must be divisible by this (def=1)
NX               140      #  (def=100)
NY               70       #  (def=32)
NZ               70       #  (def=32)
DY0              0.250    # (WDC:0.25,JR:0.25) minimum cell size in y (def=0.5)
DZ0              0.250    # (WDC:0.25,JR:0.25) minimum cell size in z (def=0.5)
XX1              -35.01   # sunward extension of box (def=-21.01)
XX2              5000.01  # anti-sunward extension of box (def=250.01)
YY2              48.01    # box size in +-y (def=36.01)
ZZ2              48.01    # box size in +-z (def=36.01)

#.... how the x-grid is defined 
#GGX1             default  #  (def= $v="-n   $NX     -x0 $XX1     -xn $XX2 " )
#GGX2             default  #  (def= $v="-x1  -26     -x3   -4     -dmm  16    -x5   80 "  )
#GGX3             default  #  (def= $v="-h0  2.0     -hn   20     -hm 1.00   -hmm 0.60 " )
#GGX4             default  #  (def= $v="-b1  0.15    -b2  0.025   -b3  0.3 " )
GGX1              $v="-n   $NX     -x0 $XX1     -xn $XX2 "
GGX2              $v="-x1  -22     -x3    -5      -dmm  20    -x5   120  "
GGX3              $v="-h0  3.0     -hn   600   -hm 1.5    -hmm 1.5 "
GGX4              $v="-b1  0.13    -b2  0.033  -b3  0.2 "
# old grid:
#     f = fak*(hm + (h0-hm)*tanhm(b1*(x-x1)) + (hn-hm)*tanhp(b2*(x-x5))
#    *  - (hm-hmm)*(#one-tanhm(b3*(x-(x3-dmm)))-tanhp(b3*(x-(x3+dmm)))) )
# new grid defined by dxfunc
#     fsm3: x=xx-0.5*(b+a); x=2.0*x/(b-a); if(x.gt.0.0) then; y=1.0+(x-1.0)**3; else; y=-1.0+(x+1.0)**3; endif; y=max(-1.0,min(1.0,y)); fsm3=0.5*(1.0+y);
DXFUNC           default  # (def=)
#DXFUNC           hm+(h0-hm)*tanhm(b1*(x-x1))+(hn-hm)*tanhp(b2*(x-x5))-(hm-hmm)*(1.0-tanhm(b3*(x-(x3-dmm)))-tanhp(b3*(x-(x3+dmm))))  #  (def=)
#DXFUNC           1.0+150.0*fsm3(x,4.0,400.0)+2.0*fsm3(x,-8.0,-30.0)  #  (def=)


GGYZ1            default  #  (def= $v="-xn 2.0  -xm 0.5 " )

#---------------------------------------------------------------
#-------------- mirror dipole ----------------------------------
#---------------------------------------------------------------
XPOS_MIR         default  #  (def=-15.)
MIRDIP           default  #  (def=true)

#---------------------------------------------------------------
#-------------- resistivity ------------------------------------
#---------------------------------------------------------------
MAGDIFFU         nl1      # (WDC:const,JR:nl1) Resistivity model res1/nl1/const (def=nl1)
DIFFTHRESHOLD    0.60     # (WDC:0.75,JR:0.60) Resistivity threshold value (def=0.80)
DIFFCONSTANT     0.03     # (WDC:5e3,JR:0.03) Resistivity constant (def=0.05)
DIFFSPHERE       default  # No resistivity inside of this (def=6)
D_I              default  # ion skin depth (def=0.)

#---------------------------------------------------------------
#-------------- output unit normalizations ---------------------
#-------------- note: works only with diags=diagsc -------------
#---------------------------------------------------------------
BBNORM0          default  # Output units will be bbnorm0 T, ie nT by default  (def=1e-9)
VVNORM0          default  # Output units will be vvnorm0 m/s, ie km/s by default  (def=1e3)
RRNORM0          default  # Output units will be rrnorm0 1/m**3, ie 1/cm**3 by default  (def=1e6)
PPNORM0          default  # Output units will be ppnorm0 Pa, ie pPa by default (def=1e-12)
CCNORM0          default  # Output units will be ccnorm0 A/m**2, ie muA/m**2 by default (def=1e-6)
EENORM0          default  # Output units will be eenorm0 V/m, ie mV/m by default (def=1e-3)
RESNORM0         default  # Output units will be resnorm0 V*m/A, ie V*m/A by default (def=1.0)

#---------------------------------------------------------------
#---------- output control -------------------------------------
#---------------------------------------------------------------
DIAGS            diags2   # Diagnostics, choose diags2 or diagsc or fdiag2. (def=diags2)
OUTPUTMODE       combined # Diagnostics, choose "single" or "combined" for diags2, or one of "jr", "cdf", "xdmf_serial", "star", "ascii" for diagsc. (def=combined)
DIAGSC_SRV       default  # (WDC:cache) C diag: type of diag srver: nocache,cache (def=nocache)
HDF5_DIR         default  # Directory where to find HDF5 incl/lib (def=)
CDF_DIR          default  # Directory where to find cdf include/ and lib/ (def=)
DIAG_MAXFLD      default  # Number of 3d fields to cache (def=23)
NPDIAG           default  # Number of diag servers to create (def=1)
OUTTIMERUN       -1200000  # When to create runtime graphics. (def=-60)
OUTTIME2D        -6000    # When to output 2d MHD fields. (def=-60)
OUTTIMEIO        -2400    # When to output ionosphere fields. (def=-60)
OUTTIME3D        -3600000  # When to output 3d MHD fields. (def=-60)
OUTTIMEEX        default  # A:B:C.. increase output cadence by C between A and B (def=0)
OUTPLANEX        10000    # X-output planes. (def=0)
OUTPLANEY        10000    # Y-output planes. (def=0)
OUTPLANEZ        10000    # Z-output planes. (def=0)
ORBIT_DT         5        # output interval for direct orbit output. (def=10)
ORBITFILE        orbits.txt  # File containing orbit data. (def=none)
MODTTY           default  # Every how many time steps to write one output line. (def=1)

#---------------------------------------------------------------
#---------- checkpoint/restart ---------------------------------
#---------------------------------------------------------------
CHECKPOINT       default  # When to write checkpoint files (negative for cadence). (def=0)
RESTART          default  # Restart from this checkpoint number. Start up regularly if 0 (default). (def=0)

#---------------------------------------------------------------
#---------- time step control ----------------------------------
#---------------------------------------------------------------
RRMIN            default  # (WDC:0.10,JR:0.15) minimum permitted number density in cm**-3 (def=0.20)
SPEEDLIMIT       default  # maximum permitted Alfven speed in km/s  (def=1500.0)
THX              default  # (WDC:0.40,JR:0.40) cfl time step factor (def=0.35)
MODNEWSTEP       default  # (WDC:50) recalculate cfl limit every 'modnewstep' step (def=1)
DTMIN            default  # minimum allowed timestep (def=.0002)

#---------------------------------------------------------------
#---------- Times, Solar Wind / IMF ----------------------------
#---------------------------------------------------------------
STARTTIME        1997:05:14:20:30:00.000  # UT when the run starts (def=2000:07:15:12:00:0.0)
ENDTIME          1997:05:14:20:30:19.000  # UT when the run ends (def=2000:07:16:12:00:0.0)
DIPOLTIME        1997:05:14:20:30:00.000  # UT time for dipole orientation (def=2000:07:16:00:00:0.0)
ALPHAFAK         1.10     # (WDC:1.10,JR:1.10) Density factor to account for SW alphas (def=1.00)
SWMON            ac       # Solar wind monitor sc (wi/ac/i8/ge/fi), use 'fi' if none. (def=fi)
SWFILE           default  # File with SW/IMF. This can also be "auto" or "minvar", in which case the input file is automatically generated. These options require that the following variables are set and that the corresponding raw data are available. (def= $v="./swdata"; )
SETIMFBX         default  # For automatic generation, specifies the IMF Bx if necessary (for "auto"). (def=none)
INPAVE           default  # (WDC:30,JR:30) averaging interval for input files, seconds (def=60)
IDATA            default  # Only for automatic input generation. Specifies the directory in which to find raw solar wind data. One file for each variable in standard ascii time series format (YY MM DD HH MI SE VAL). The following variables must be present: bx, by, bz, vx, vy, vz, rr, pp, xgse, ygse, zgse. (def= $v="./"; )
F107             150      # F10.7 solar flux (def=160)
MOX              200      # Xgse solar wind monitor position. (def=$v=0.0-$XX1)
MOY              0        # Ygse solar wind monitor position. (def=0)
MOZ              0        # Zgse solar wind monitor position. (def=0)
SWTURB1          default  # Add turbulence to solar wind input. (def=false)
SWTURB_VV        default  # SW turbulence velocity perturbation. (def=0.0)
SWTURB_RR        default  # SW turbulence density perturbation. (def=0.0)

#---------------------------------------------------------------
#--------------------- Dipole Rotation -------------------------
#---------------------------------------------------------------
DO_DIPROTATE     default  # Rotate the dipole, 0=off, 1=on (def=0)
MODDIPCOEFF      default  # (JR:200) update mapping coefficients every moddipcoeff steps (def=100)
MODDIPFIELD      default  # (JR:20) updipole field every "moddipfield" steps (def=1)

#---------------------------------------------------------------
#-------------- MI coupling and ionosphere ---------------------
#---------------------------------------------------------------
ISPHERE          default  # (JR:3.2) inner boundary radius (RE) (def=3.0)
IONODT           default  # do iono every ionodt secs (def=30.0)
IO_SHIELD_LAT    default  # ionosphere shielding latitude (boundary where pot=0) (def=50.0)
BND_IO_RR        default  # (WDC:1.0) density at inner boundary in cc (def=2.0)
BND_IO_TT        400      # (WDC:400,JR:400) temperature at inner bndry in eV (def=50)
NTHETA           default  # ionosphere grid latitude points (def=361)
NPHI             default  # ionosphere grid longitude points (def=121)
NIOX             default  # no of panels in theta for one hemisphere (def=40)
NIOY             default  # no of modes in phi (def=8)
NIOGO            default  # order of gauss legendre integration (def=4)
NJSLIDE          default  # fac sliding time averages (def=2)
SIGMATRANS       15000    # (WDC:15000,JR:15000) After which timestep begin transition to self-consistent sigmas (needs time to adjust). Use a very large value for constant conductivity. (def=5000000)
SIGMAFAK         default  # factor to multiply all ionosphere conductances. (def=1.0)
MAPE1            default  # MI coupling factor for mapped E (def=1.0)
FAC_FILT         2        # (WDC:2,JR:2) how often to spatially filter FAC (def=4)
SIG_FLOOR        default  # minimum sigmas (nightside not actually zero: stars and moon contribute EUV; need to set to avoid model problems) (def=0.5)
FAK_FAC_DYN      default  # factor for dynamo currents (only w/ctim)  (def=1.0)
SIG_SMOOTH       default  # spatially smoothing sigmas (def=2)
SIG_CONST        3.00     # (WDC:3.00,JR:3.00) constant sigma (S) for startup  (def=5.0)
SIGP_CONST       default  # constant sigmaP (Sp) for startup  (def=5.0)
SIGH_CONST       default  # constant sigmaH (Sh) for startup  (def=5.0)
SIG_H_BACK       default  # nightside P background conductance (def=0.3)
SIG_P_BACK       default  # nightside P background conductance (def=0.5)
SIGFAC_FAK       default  # fudge factor for discrete e-flux (def=1.0)
SIGFAC_FAK2      default  # fudge factor for discrete e-mean energy (def=1.0)
SIGDIF_FAK       default  # fudge factor for diffuse e-flux (def=1.0)
SIGDIF_FAK2      default  # fudge factor for diffuse e-mean energy (def=1.0)

#---------------------------------------------------------------
#-------------- memory management ------------------------------
#---------------------------------------------------------------
BIGDIM           default  # Size of global global memory array. Should not matter because this is determined self-consistently in the pre-compilation process. (def=12000000)

#---------------------------------------------------------------
#-------------- CTIM specific inputs ---------------------------
#---------------------------------------------------------------
CTIM_IN          default  # (def="$ggcmdir/ctim-indata")
CTIM_START_TIME  default  # CTIM start time in seconds (def=300.0)
CTIM_DELTA_T     default  # CTIM calling frequency in seconds (def=60.0)
CTIM_POTFAK      default  # factor for driving ctim potential (def=0.6)
CTIM_F_PRSXXXX   default  # This is either an ascii file that contains everything included in the 'prs', 'mmm', and 'vel' files (in which case ctim_f_mmmxxxx and ctim_f_velxxxx are not used), or the binary 'prs' file. (def="$CTIM_IN/inp_ctim.fields.20000921000.asc")
CTIM_F_MMMXXXX   default  #  (def="$CTIM_IN/inp_ctim.mmm20000921000")
CTIM_F_VELXXXX   default  #  (def="$CTIM_IN/inp_ctim.vel20000921000")
CTIM_F_BMAGFLD   default  #  (def="$CTIM_IN/inp_ctim.bmagfld")
CTIM_F_SUBSTORM  default  #  (def="$CTIM_IN/inp_ctim.substorm")
CTIM_F_CORMAG    default  #  (def="$CTIM_IN/inp_ctim.cormag")
CTIM_F_DATAM     default  #  (def="$CTIM_IN/inp_ctim.datam")
CTIM_F_LEVPI     default  #  (def="$CTIM_IN/inp_ctim.levpi")
CTIM_F_GWATTS    default  #  (def="$CTIM_IN/inp_ctim.gwatts")
CTIM_F_CPCPD     default  #  (def="$CTIM_IN/inp_ctim.cpcpd")
CTIM_F_IONPROF   default  #  (def="$CTIM_IN/inp_ctim.ionprof")
CTIM_F_PROF2     default  #  (def="$CTIM_IN/inp_ctim.prof2")
CTIM_F_DMSP      default  #  (def="$CTIM_IN/inp_ctim.dmsp")
CTIM_F_HOLT      default  #  (def="$CTIM_IN/inp_ctim.holt")
CTIM_F_DMSPMOD   default  #  (def="$CTIM_IN/inp_ctim.dmspmod")
CTIM_F_RICE91    default  #  (def="$CTIM_IN/inp_ctim.rice91")
CTIM_F_RICEPROF  default  #  (def="$CTIM_IN/inp_ctim.riceprof")
CTIM_F_HPROF     default  #  (def="$CTIM_IN/inp_ctim.hprof")
CTIM_F_HOUGH     default  #  (def="$CTIM_IN/inp_ctim.hough")
CTIM_F_INSHAUN   default  #  (def="$CTIM_IN/inp_ctim.d1d2")
CTIM_I10         default  # I10 is the smothing frequency. (def=5)
CTIM_I5          default  #  i5 is mwt calling frequency. (def=1)
CTIM_I8          default  #  i8 is shaun frequency. (def=6)
CTIM_WINDMX      default  # maximum wind (def=1000.0)
CTIM_TMPMIN      default  # minimum temperature (def=130.0)
CTIM_AMPL22      default  # amplitudes of the tidal model (def=608.0)
CTIM_AMPL23      default  #  (def=500.0)
CTIM_AMPL24      default  #  (def=138.0)
CTIM_AMPL25      default  #  (def=138.0)
CTIM_AMPL11      default  #  (def=200.0)
CTIM_PHASE22     default  # Phases of tidal model. (def=9.6)
CTIM_PHASE23     default  #  (def=4.6)
CTIM_PHASE24     default  #  (def=1.2)
CTIM_PHASE25     default  #  (def=2.2)
CTIM_PHASE11     default  #  (def=12.0)
CTIM_KP          default  # kp index (def=3)
CTIM_XX          default  # global setting??? (def=91)
CTIM_AMP         default  # ratio of E foster field to real E field. (def=1.3)
CTIM_SWITCH_1    default  # ctim boolean switches (def=1)
CTIM_SWITCH_2    default  #  (def=1)
CTIM_SWITCH_3    default  #  (def=0)
CTIM_SWITCH_4    default  #  (def=1)
CTIM_SWITCH_5    default  #  (def=0)
CTIM_SWITCH_6    default  #  (def=0)
CTIM_SWITCH_7    default  #  (def=1)
CTIM_SWITCH_8    default  #  (def=1)
CTIM_SWITCH_9    default  #  (def=1)
CTIM_SWITCH_10   default  #  (def=1)
CTIM_SWITCH_11   default  #  (def=0)
CTIM_SWITCH_12   default  #  (def=0)
CTIM_SWITCH_13   default  #  (def=1)
CTIM_SWITCH_14   default  #  (def=1)
CTIM_SWITCH_15   default  #  (def=1)
CTIM_SWITCH_16   default  #  (def=1)
CTIM_SWITCH_17   default  #  (def=0)
CTIM_SWITCH_18   default  #  (def=0)
CTIM_SWITCH_19   default  #  (def=0)
CTIM_SWITCH_20   default  #  (def=0)
CTIM_SWITCH_21   default  #  (def=0)
CTIM_SWITCH_22   default  #  (def=0)
CTIM_SWITCH_23   default  #  (def=0)
CTIM_SWITCH_24   default  #  (def=1)
CTIM_SWITCH_25   default  #  (def=1)
CTIM_SWITCH_26   default  #  (def=1)
CTIM_SWITCH_27   default  #  (def=1)
CTIM_SWITCH_28   default  #  (def=1)
CTIM_SWITCH_30   default  #  (def=0)
CTIM_SWITCH_31   default  #  (def=0)
CTIM_SWITCH_32   default  #  (def=1)
CTIM_SWITCH_33   default  #  (def=0)
CTIM_SWITCH_34   default  #  (def=0)
CTIM_SWITCH_35   default  #  (def=0)
CTIM_SWITCH_37   default  #  (def=1)

#---------------------------------------------------------------

#---------------------------------------------------------------
#-------- Inner magnetosphere specific inputs ------------------
#--------           (RCM, CRCM)               ------------------
#---------------------------------------------------------------
RCMCODE          rice     #  rice, crcm (def=none)

#---------------------------------------------------------------
# --- variables for file tmp.rcm.in -- These are RCM (rice) specific
#---------------------------------------------------------------
RCM_ITIMEI       default  # start time (def=0)
RCM_ITIMEF       default  # end time (def=345600)
RCM_IRDR         default  # record # to read in (def=1)
RCM_IRDW         default  # record # to write out (def=2)
RCM_IDEBUG       default  # do disk printout (def=0)
RCM_IDT          default  # basic time step in program (def=2)
RCM_IDT1         60       # (WDC:60) [sec] how often to call RCM (def=120)
RCM_IDT2         default  # [sec] t-step for writing formatted output (def="$RCM_IDT1")
RCM_IDT3         60000    # (WDC:60000) t-step for invoking ADD & ZAP (def=60)
RCM_IMIN         default  # i-value of poleward bndy (def=1)
RCM_IPOT         5        # (WDC:5) which potential solver to use (def=4)
RCM_IWIND        default  # 0 is no neutral winds (def=0)
RCM_IVOPTN       default  # 1 is euler time derivs in vcalc, 2--runge-kutta (def=1)
RCM_IBND         default  # (should be rcm_ibnd_type?) type of bndy (1-eq.p, 2-iono) (def=4)
RCM_IPCP_TYPE    default  # type of bndy (1-eq.p, 2-iono) (def=13)
RCM_NSMTHI       default  # How much to smooth cond in I (def=0)
RCM_NSMTHJ       default  # How much to smooth cond in J (def=0)
RCM_ICOND        default  # (1:active conductances, 2:use Hardy statistical model, 3:?) (def=3)
RCM_IFLOOR       default  # floor for eflux? (def=.TRUE.)
RCM_ICORRECT     default  # correct eflux? (def=.TRUE.)
RCM_DSTMIN       default  # min allowed dist. between adjacent pnts (def=1.5)
RCM_DSTMAX       default  # max allowed dist. between adjacent pnts (def=3.1)
RCM_RHOMAX       default  #  (def=0.5)
RCM_VMFACT       default  #  (def=0.1)
RCM_EPSLON       default  # (should be epslon_edge?) in MODULE rcm_mod_edgpt (def=0.0001)
RCM_DLIM         default  # (should be fmrwif_dlim?) min tail thickness for FMRWIF (def=0.25)
RCM_CMAX         default  # in rcm_mod_balgn (def=3.0)
RCM_EETA_CUTOFF  default  # as a fraction (def=0.05)
RCM_TOL_GMRES    default  # Potential solver GMRESM tolerance (def=1.0e-5)
RCM_ITYPE_BF     2        # (WDC:2) 1 is interpolate for HV, 2--friction code (def=3)
RCM_I_ADVECT     default  # 1-interpolate, 2-CLAWPACK/inter, 3-CLAWPACK (def=3)
RCM_I_ETA_BC     default  # 1-time-dep. from file, 2-constant for run (def=2)
RCM_KILL_FUDGE   .FALSE.  # (WDC:.FALSE.) .true. means no loss (def=.TRUE.)
RCM_I_BIRK       default  # compute field-aligned current density (def=1)
RCM_DKTIME       default  # decay time (def=.FALSE.)
RCM_SUNSPOT_NUMBER  default  #  (def=96)

# --- other variables
RCM_DEBUG        default  #  (def=false)
RCM_DEBUG_LEVEL  default  # RCM debug level (def=0)
RCM_FB_PRECON    0        # (WDC:5400) time for first call to rcm (def=0.0)
RCM_IONO         default  # ionosphere altitude km (def=120.0)

# --- variables which influence rcm boundary location
RCM_XX1          default  # sunward boundary of rcm domain (def=-10.0)
RCM_XX2          default  # anti-sunward boundary of rcm domain (def=20.0)
RCM_YY1          default  # +- y boundary of rcm domain (def=20.0)
RCM_ZZ1          default  # +- z boundary of rcm domain (def=20.0)
RCM_MIN_B        default  # if positive, consider fieldlines with |B| < rcm_min_b to be open for the purposes of the rcmcode (def=30.0)
RCM_MAX_BETA     default  # maximum allowed beta in equitorial plane for feedback. negative values represent infinity. (def=-1.)

# --- variables which influence (MHD) feedback
RCM_PP_RR_FEEDBACK      default  # pressure and density feedback (def=yes)
RCM_FB_STRENGTH         0.25     # (WDC:0.25) Strength factor for rcm feedback nudging (def=1.0)
RCM_FB_KICKIN_TIME      7200     # (WDC:7200) at this time feedback starts to kick in the case where rcm_pp_rr_feedback is yes (def=300.0)
RCM_FB_RAMP_UP          3600     # (WDC:3600) ramp up the rcm feedback over this many seconds (def=900.0)
RCM_FEEDBACK_BND        10.0     # (WDC:10.0) If positive, feedback only to this radial L shell (def=-1.0)
RCM_FB_MIN_B            default  # if positive, don't feedback on fieldlines where |B| < rcm_fb_min_b (def=100.)
RCM_FB_B_CEIL           default  # partially limit feedback up to |B| = rcm_fb_b_ceil (def=400.)
RCM_PLASMASPHERE        default  # Use a primitive model for the plasmasphere. (def=.TRUE.)
RCM_PLSFAC              default  # multiply plasmaspheric density by this factor (if rcm_plasmasphere is .TRUE.) (def=0.01)
RCM_SERIAL_FB_TRACER    default  # Use serial feedback tracer, (even if parallel_tracer is true) (def=false)
RCM_SERIAL_TRACER       default  # Use serial forward tracer, (even if parallel_tracer is true) (def=false)
RCM_MAKE_NEW_FAC        default  # (WDC:no) overwrite MHD FAC with RCM FAC if RCM FAC is not zero (def=false)
RCM_MAKE_NEW_PRECIP     default  # overwrite MHD diffuse precip with RCM precip (def=false)
RCM_RFLMAX              80.0     # (WDC:80.0) max allowed field line length (def=100.0)
RCM_RMAX                default  # field line is considered open if farther than this (def=15.0)
RCM_FLSTEPSIZE          0.05     # (WDC:0.05) stepsize to take when calculating fieldline -- old default was 0.01 which is just too small. (def=0.1)
RCM_RMIN_DIP_MHD_BLEND  {"$ISPHERE"}  # (WDC: {"$ISPHERE"}) lower boundary for dipole-mhd blend -- below this is only dipole (def=4.5)
RCM_RMAX_DIP_MHD_BLEND  3.6      # (WDC:3.6) upper boundary for dipole-mhd blend -- beyond this is only mhd (def=6.5)
DO_RCM_INPUT_DIAG       default  # Write the fieldline tracer output to a file as a diagnostic (def=false)
RCM_DIAG                1        # (WDC:1) write diagnostics for rcm feedback in the output files (mostly for debugging purposes) (def=0)

#---------------------------------------------------------------
#------  Fieldline Tracer Module -------------------------------
#---------------------------------------------------------------
PARALLEL_TRACER  default  # Use parallel fieldline tracing module (def=false)
NPAR_TRACE       default  # Use npar_trace processes for tracing. Only used if parallel_tracer is true (def=0)
PAR_TRACE_DEBUG  default  # print verbose debugging messages for parallel tracer (def=false)
FL_USE_COTR      default  # use cotr to get dipole field instead of coordinate independent (analytic) formula for dipole (def=false)
FL_HUEN          default  # Use (2nd order) huen method for fieldline tracing (instead of 1st order Euler) (def=.false.)

#---------------------------------------------------------------
#---- Comprehensive Ring Current Model (CRCM) specific inputs --
# WDC: not in input.defines, so no idea of defaults
#---------------------------------------------------------------
crcm_write_n_timesteps       1.0       # at how many crcm_delta_t intervals to write down the CRCM flux, etc...

crcm_species                   2       # 1=electrons; 2=protons
crcm_time_init_cond     172800.0       # [sec] until that time artificial potential Volland-Stern will be used (or mixed - see crcm_mixed_potential)
crcm_start_time              0.0       # [sec] when to actually start CRCM (no need to change it)
crcm_delta_t               120.0       # [sec] how often to call CRCM
crcm_write_time              1.0       # [sec] the time interval defined approximately as (crcm_write_time) x (crcm_delta_t) at which to write down the CRCM flux, etc...
crcm_iono                  120.0       # [km] ionosphere in km in CRCM
crcm_pp_rr_feedback          yes       # {yes,no} pressure and density feedback
crcm_fb_kickin_time        300.0       # [sec] at this time feedback starts to kick in in case crcm_pp_rr_feedback=yes

# --- at least one of the two 'make_new_fac' options should be 'no'. Otherwise the 'rc' option will simply add/overwrite
crcm_make_new_fac_w_pc       yes       # {yes,no} FAC feedback overwrite MHD FAC with MHD integrated parallel FAC
crcm_make_new_fac_w_rc        no       # {yes,no} FAC feedback overwrite MHD FAC with CRCM FAC if CRCM FAC is not zero
crcm_new_fac_smooth            2       # [integer] average the new FAC (CRCM's/integrated + MHD's) over this number of grid cells - basic smoothing. Zero does nothing.
crcm_intgrd_fac_smooth         4       # [integer] average the integrated parallel FAC over this number of grid cells - basic smoothing. Matters if crcm_make_new_fac_pc=yes
crcm_crcm_fac_smooth           4       # [integer] average the CRCM FAC over this number of grid cells - basic smoothing. Matters if crcm_make_new_fac_rc=yes
crcm_mixed_potential         yes       # {yes,no} create mixed half MHD half VS potential for the specified crcm_time_init_cond (> 0)
crcm_mixed_pot_factor        1.0       # factor multiplier for the mixed potential (if yes)
crcm_lowbndry_condition      yes       # set lower boundary density, temperature, potential for CRCM
crcm_fac_lowbnd_lat         54.0       # [deg] lower boundary condition latitude if crcm_lowbndry_condition is yes
crcm_makefac_lowbnd_lat     63.0       # [deg] lower boundary condition (merging FACs control) latitude if crcm_lowbndry_condition is yes
crcm_null_isphere_vars       yes       # set CRCM feedback pressure and density zero inside ISPHERE (otherwise stability issues?)

# --- Factoring of the Input/Output stuff for CRCM and MHD------
crcm_mhd_pot_factor          1.0       # factor the mhd potential before input in crcm if wanted
crcm_mhd_dens_factor         1.0       # factor the mhd density before input in crcm if wanted
crcm_mhd_temp_factor         1.0       # factor the mhd temperature before input in crcm if wanted
crcm_to_mhd_pres_factor      1.0       # factor the crcm pressure before feedback in mhd if wanted
crcm_to_mhd_dens_factor      1.0       # factor the crcm density before feedback in mhd if wanted
crcm_pp_rr_scale_length      0.5       # [RE] scale the pressure and density from 0 at ishpere to 1 at isphere+crcm_pp_rr_scale_length with a sin^2 to reduce the gradient jump before passing them to the MHD grid
crcm_pp_out_smooth             0       # [integer] smooth CRCM output pressure before passing it to the MHD grid. No smoothing if zero
crcm_rr_out_smooth             0       # [integer] smooth CRCM output density before passing it to the MHD grid. No smoothing if zero

# --- Field-line tracer stuff for CRCM -------------------------
crcm_max_lat_grid_limit   69.458       # [degrees] max latutide on the crcm ionosphere grid - 69.458 degrees is default - do not go less that that. Go only higher than that.
crcm_rflmax               1000.0       # [RE] max allowed field line length
crcm_rmax                    7.0       # [RE] field line is considered open if farther than this - this basically controls how far is the outer CRCM boudary in the equatorial plane
crcm_rmin_dip_mhd_blend      2.5       # [RE] lower boundary for dipole-mhd blend - below this is only dipole
crcm_rmax_dip_mhd_blend      8.0       # [RE] upper boundary for dipole-mhd blend - beyond this is only mhd

# --- the next two (new) variables are for CTIM, but we have put them here, because their values affect CRCM - CRCM and CTIM have a conflict running together depending on their respective start times and delta_times (AEV). The new variables are used in mhd-misig.for
#ctim_start_time            300.0       # [seconds] CTIM start time
#ctim_delta_t                20.0       # [seconds] CTIM calling frequency

# --- debug
crcm_debug                    no       # [yes,no] write more output for debug

# --- bruteforce workaround for dtmin errors
#dtmin                  0.0000002       # [sec] change the default dtmin in mhd-corea.for in case dtmin goes below 0.0002 and the run crashes - hopefully this will help the code the "hop over" the "bad" CFL condition

