#!/bin/ksh
#
arguments=$*

if [ $1 ] ; then
  cd $1 2> /dev/null
  if [ $? -ne 0 ] ; then
    printf "\n Directory $1 is unavailable --- ABORT ---\n\n"
    exit 1
  fi
fi

ici=`pwd`
Um_checkcfg.ksh single
flag_cfg=$?

if [ $flag_cfg -eq 0 ] ; then
  set -x
  . ./configexp.cfg
  set +x
fi

. r.entry.dot

eval `cclargs_lite -D " " $0 \
\
 -BACKEND_mach       "${BACKEND_mach}"            ""      "[Backend machine name                ]" \
 -BACKEND_time_ntr   "${BACKEND_time_ntr:-600}"   "600"   "[Job resources: gemntr wallclock time]" \
 -BACKEND_time_mod   "${BACKEND_time_mod:-3600}"  "3600"  "[Job resources: gemdm  wallclock time]" \
 -BACKEND_cm         "${BACKEND_cm:-10G}"         "10G"   "[Job resources: gemdm  memory size   ]" \
 -BACKEND_execdir    "${BACKEND_execdir}"         ""      "[Working directory                   ]" \
 -BACKEND_listings   "${BACKEND_listings:-${HOME}/listings}" "" "[Directory for job listings    ]" \
 -BACKEND_class      "${BACKEND_class}"           ""      "[to control -cl option with soumet   ]" \
 -BACKEND_waste      "${BACKEND_waste:-0}"        "1"     "[Allow cpu waste on compute nodes    ]" \
 -BACKEND_norset     "${BACKEND_norset:-0}"       "1"     "[Make use of LL resources set        ]" \
 -BACKEND_nosubmit   "${BACKEND_nosubmit:-0}"     ""      "[to issu -nosubmit option with soumet]" \
\
 -GEM_exp         "${GEM_exp:-dbg1}"      "dbg1"  "[Current experiment name                    ]" \
 -GEM_anal        "${GEM_anal}"           ""      "[Full path of the analysis                  ]" \
 -GEM_inrep       "${GEM_inrep}"          ""      "[Full path of INIT & BCS for LAM (gemntr)   ]" \
 -GEM_model_input "${GEM_model_input}"    ""      "[Full path of input data for gemdm          ]" \
 -GEM_ovbin       "${GEM_ovbin}"          ""      "[Full path of main model executables        ]" \
 -GEM_anclima     "${GEM_anclima}"        ""      "[Full path of climatological analysis file  ]" \
 -GEM_climato     "${GEM_climato}"        ""      "[Full path of climatology file              ]" \
 -GEM_geophy      "${GEM_geophy}"         ""      "[Full path of geophysical file              ]" \
 -GEM_phy_intable "${GEM_phy_intable}"    ""      "[Full path of alternate physics_input_table ]" \
 -GEM_iaurep	  "${GEM_iaurep}"         ""      "[Full path of directory for increment files ]" \
 -GEM_busper      "${GEM_busper}"         ""      "[Full path of BUSPER rstr file for recycling]" \
 -GEM_RTinput     "${GEM_RTinput}"        ""      "[Full path of runtime forcing fields (phy)  ]" \
 -GEM_ozone       "${GEM_ozone}"          ""      "[Full path of climatology ozone file        ]" \
 -GEM_radtab      "${GEM_radtab}"         ""      "[Full path of radiation table file          ]" \
 -GEM_const       "${GEM_const}"          ""      "[Full path of geophysical constantes file   ]" \
 -GEM_cache       "${GEM_cache}"          ""      "[Full path of a cache directory for gemdm   ]" \
 -GEM_r_upl       "${GEM_r_upl:-1}"       "0"     "[To upload                                  ]" \
 -GEM_r_ntr       "${GEM_r_ntr:-1}"       "0"     "[To run GEMNTR                              ]" \
 -GEM_r_mod       "${GEM_r_mod:-1}"       "0"     "[To run GEMDM                               ]" \
 -GEM_split3df    "${GEM_split3df:-0}"    "1"     "[To split 3df files                         ]" \
 -GEM_d2z         "${GEM_d2z:-0}"         "1"     "[To re-assemble output files                ]" \
 -GEM_yyoutgrid   "${GEM_yyoutgrid:-'U'}" "U"     "[output grid for yin-yang: U, LAMS or GLB   ]" \
 -GEM_xfer        "${GEM_xfer}"           ""      "[WS:full-path-dir on which to rcp results   ]" \
 -GEM_xferl       "${GEM_xferl:-0}"       "1"     "[Transfer listings along with model output  ]" \
 -GEM_xcasc       "${GEM_xcasc:-1}"       "0"     "[Transfer sub-dir casc for LAM_casc         ]" \
 -GEM_prefix      "${GEM_prefix}"         ""      "[Prefix for output filenames                ]" \
 -GEM_dplusp      "${GEM_dplusp:-0}"      "1"     "[To assemble dynamics and physics output    ]" \
 -GEM_headscript_E      "${GEM_headscript_E}"      "" "[Script to execute before GEMNTR      ]" \
 -GEM_tailscript_E      "${GEM_tailscript_E}"      "" "[Script to execute after  GEMNTR      ]" \
 -GEM_tailscript_M_each "${GEM_tailscript_M_each}" "" "[Script to execute after  GEMDM - every restart]" \
 -GEM_tailscript_M_last "${GEM_tailscript_M_last}" "" "[Script to execute after  GEMDM - last  restart]" \
 -GEM_tailjob_M_each    "${GEM_tailjob_M_each}"    "" "[Job    to launch  after  GEMDM - every restart]" \
 -GEM_tailjob_M_last    "${GEM_tailjob_M_last}"    "" "[Job    to launch  after  GEMDM - last  restart]" \
 -GEM_tailscript_X_each "${GEM_tailscript_X_each}" "" "[Script to execute after  XFER  - every execute]" \
 -GEM_tailscript_X_last "${GEM_tailscript_X_last}" "" "[Script to execute after  XFER  - last  execute]" \
 -GEM_tailjob_X_each    "${GEM_tailjob_X_each}"    "" "[Job    to launch  after  XFER  - every execute]" \
 -GEM_tailjob_X_last    "${GEM_tailjob_X_last}"    "" "[Job    to launch  after  XFER  - last  execute]" \
 -GEM_checknml   "${GEM_checknml:-1}"    ""      "[Check namelists before submission          ]" \
 -GEM_ppid       "${GEM_ppid}"           ""      "[User choice PPID for job summission        ]" \
 -GEM_xchgdir    "${GEM_xchgdir}"        ""      "[Exchange directory for coupling            ]" \
 -GEM_cmclog     "${GEM_cmclog}"         ""      "[Full path for CMC log file                 ]" \
 -GEM_barrier    "${GEM_barrier:-0}"     "1"     "[Third party job (SM) will run the model    ]" \
 -GEM_timing     "${GEM_timing}"         ""      "[AIX HPM timing info                        ]" \
 -GEM_cpus_ntr   "${GEM_cpus_ntr:-1}"    ""      "[Number of Cpus to run gemntr               ]" \
 -GEM_ptopo      "${GEM_ptopo:-1x1x1}"   ""      "[MPI&OMP PEs topology (npex x npey x nomp)  ]" \
 -GEM_instances  "${GEM_instances}"      ""      "[Number of grids x number of domains        ]" \
 -GEM_in_block   "${GEM_in_block}"       ""      "[Input  file blocking (nblocx x nblocy)     ]" \
 -GEM_out_block  "${GEM_out_block}"      ""      "[Output file blocking (nblocx x nblocy)     ]" \
 -GEM_outfreq    "${GEM_outfreq}"        ""      "[Output post-processing frequencing         ]" \
 -GEM_smt        "${GEM_smt}"            ""      "[SMT controler (AIX) (smtdyn x smtphy)      ]" \
 -GEM_bind       "${GEM_bind:-0}"        "1"     "[Processor binding logical controler        ]" \
 -GEM_upload     "${GEM_upload}"         ""      "[Upload directory                           ]" \
 -GEM_ntsortie   "${GEM_ntsortie:-1}"    "1"     "[Numebr of CPUs for the sortie job          ]" \
 -GEM_onexferjob "${GEM_onexferjob:-0}"  "1"     "[Sequentializes all model output xfer jobs  ]" \
 -GEM_clean      "${GEM_clean:-1}"       "0"     "[Clean some directories after usage         ]" \
\
 -restart    "0"         "1"    "[Restart mode              ]" \
 -_job       ""          ""     "[OUTPUT: jobname           ]" \
 -_listing   ""          ""     "[OUTPUT: listing directory ]" \
 -_status    "ABORT"     ""     "[return status             ]" \
 ++ $arguments`

TMPDIR=${ici}/UM_MULTI_translation

if [ $flag_cfg -eq 0 ] ; then

/bin/rm -rf $TMPDIR ; mkdir -p $TMPDIR

cat > $TMPDIR/configexp.cfg <<EOF
BACKEND_mach=$BACKEND_mach
BACKEND_time_ntr=$BACKEND_time_ntr
BACKEND_time_mod=$BACKEND_time_mod
BACKEND_cm=$BACKEND_cm
BACKEND_execdir=$BACKEND_execdir
BACKEND_listings=$BACKEND_listings
BACKEND_class=$BACKEND_class
BACKEND_waste=$BACKEND_waste
BACKEND_nosubmit=$BACKEND_nosubmit
BACKEND_SMT=$BACKEND_SMT
BACKEND_norset=$BACKEND_norset
BACKEND_addenv=$BACKEND_addenv

GEM_exp=$GEM_exp
GEM_anal=$GEM_anal
GEM_inrep=$GEM_inrep
GEM_ovbin=$GEM_ovbin
GEM_anclima=$GEM_anclima
GEM_climato=$GEM_climato
GEM_geophy=$GEM_geophy
GEM_d2z=$GEM_d2z
GEM_yyoutgrid=$GEM_yyoutgrid
GEM_xfer=$GEM_xfer
GEM_xferl=$GEM_xferl
GEM_ntsortie=$GEM_ntsortie
GEM_xcasc=$GEM_xcasc
GEM_prefix=$GEM_prefix
GEM_dplusp=$GEM_dplusp
GEM_headscript_E=${GEM_headscript_E}
GEM_tailscript_E=${GEM_tailscript_E}
GEM_tailscript_M_each=${GEM_tailscript_M_each}
GEM_tailscript_M_last=${GEM_tailscript_M_last}
GEM_tailjob_M_each=${GEM_tailjob_M_each}
GEM_tailjob_M_last=${GEM_tailjob_M_last}
GEM_tailscript_X_each=${GEM_tailscript_X_each}
GEM_tailscript_X_last=${GEM_tailscript_X_last}
GEM_tailjob_X_each=${GEM_tailjob_X_each}
GEM_tailjob_X_last=${GEM_tailjob_X_last}
GEM_tailjob_X=$GEM_tailjob_X
GEM_checknml=$GEM_checknml
GEM_ppid=$GEM_ppid
GEM_xchgdir=$GEM_xchgdir
GEM_r_ntr=$GEM_r_ntr
GEM_r_mod=$GEM_r_mod
GEM_r_upl=$GEM_r_upl
GEM_split3df=$GEM_split3df
GEM_model_input=$GEM_model_input
GEM_phy_intable=$GEM_phy_intable
GEM_iaurep=$GEM_iaurep
GEM_busper=$GEM_busper
GEM_RTinput=$GEM_RTinput
GEM_ozone=$GEM_ozone
GEM_radtab=$GEM_radtab
GEM_const=$GEM_const
GEM_cache=$GEM_cache
GEM_cmclog=$GEM_cmclog
GEM_barrier=$GEM_barrier
GEM_timing=$GEM_timing
GEM_cpus_ntr=$GEM_cpus_ntr
GEM_ptopo=$GEM_ptopo
GEM_instances=$GEM_instances
GEM_in_block=$GEM_in_block
GEM_out_block=$GEM_out_block
GEM_outfreq=$GEM_outfreq
GEM_smt=$GEM_smt
GEM_bind=$GEM_bind
GEM_upload=$GEM_upload
GEM_onexferjob=$GEM_onexferjob
GEM_clean=$GEM_clean
EOF

cp gem_settings.nml outcfg.out coupleur_settings.nml $TMPDIR 2> /dev/null

cd $TMPDIR

# Check user namelists unless suppressed
if [[ $GEM_checknml -gt 0 ]] ; then
  printf "\n Running checknml\n"
  checknml -q gement gem_cfgs physics step grid 
  if [[ $? -ne 0 ]] ; then
    printf "\n ** Submission aborted due to namelist errors (suppress check with -GEM_checknml=0) **\n"
    exit 1
  fi
fi

printf "\n Um_newbatch.ksh\n"
Um_newbatch.ksh

printf "\n Um_lance_multi $TMPDIR -restart $restart \n\n"

. r.call.dot Um_lance_multi ./ -restart $restart

if [ -n "${_job}" ] ; then
  cp ${_job}* $ici
  _job=$ici/`basename $_job`
fi

fi

. r.return.dot
