#!/bin/ksh -x
#

export Run_Date=${1}
export ENS_Member=${2}
#
#INCLUI os diretorios de scripts e de saida 
#
source ${Eta_home1}/eta/${Conf}/scripts/ConfigRun
#

Eta_dprep=${Eta_home1}/eta/dprep
Eta_scr=${Eta_home1}/eta/${Conf}/scripts
Eta_run=${Eta_wrk}/eta/${Conf}/${Run_Date}${LabRod}${Res}km_${paramet}
Eta_ucl=${Eta_home1}/eta/${Conf}/ucl
Eta_Templ=${Eta_home}/eta/template

# Make scr output dir
mkdir ${Eta_run}
mkdir -p ${Eta_scr}/relatorios
Eta_scr_out=${Eta_scr}/output/${Run_Date}${LabRod}${Res}km_${paramet}
mkdir -p ${Eta_scr_out}
mkdir ${Eta_run}/outjob

####################################################
# Number of Model run cores
let nprp1=${npr}+${npio_server}
######################################################

###GSM PARA CAPTURAR QUAL A CONDICAO DO GLOBAL ESTA SENDO UTILIZADA TQ062 ou TQ0126
if [ -s ${Eta_ucl}/InputModelInf.txt_${FInitBC} ] ; then
cp ${Eta_ucl}/InputModelInf.txt_${FInitBC} ${Eta_run}/InputModelInf.txt
Cond_Glob=`cat ${Eta_run}/InputModelInf.txt | head -1 | tail -1 ` 
fi



cd ${Eta_scr}


cat << EOF > ${Eta_scr_out}/SetVarModelPrep
export Run_Date=${Run_Date}
export Eta_home=${Eta_home}
export Eta_home1=${Eta_home1}
export Eta_wrk=${Eta_wrk}
export Eta_scr=\${Eta_home1}/eta/${Conf}/scripts
export Eta_scr_out=${Eta_scr_out}
export Eta_run=\${Eta_wrk}/eta/${Conf}/${Run_Date}${LabRod}${Res}km_${paramet}
export Eta_ucl=${Eta_ucl}
export Eta_fix=\${Eta_home1}/eta/fix
export Eta_wrk_fix=${Eta_home1}/eta/${Conf}/fix
export Eta_src=${Eta_home1}/eta/src
export Eta_exe=\${Eta_home1}/eta/${Conf}/exe
export Eta_dprep_exe=${Eta_dprep}/exe
export data_grib=\${Eta_home1}/data/${FInitBC}
export Eta_grb=\${Eta_home1}/eta/${Conf}/grb
export data_prep=\${Eta_run}
export Eta_static=\${Eta_home1}
export data_topo=\${Eta_static}/eta/static/topo
export data_soilp=\${Eta_static}/eta/static/newsoil
export data_veg=\${Eta_static}/eta/static/veg
export Eta_datain=\${Eta_home1}/eta/${Conf}/datain
export Eta_conf=\${Eta_home1}/eta/${Conf}/configuracao
export Lon=${Lon} 
export Lat=${Lat}
export IM=${IM}
export JM=${JM}
export LM=${LM}
export LSM=${LSM}
export PT=${PT}
export Res=${Res}
export resol=${resol}
export Fctexec=${Fctexec} 
export paramet=${paramet}
export LabRod=${LabRod} 
export Conf=${Conf}
export gsprec=${gsprec}
export Fct=${Fct}
export postout=${postout}
export IntFct=${IntFct}
export CInit=${CInit}
export BCond=${BCond}
export InitBC=${InitBC}
export TInitBC=${TInitBC}
export HInitBC=${HInitBC}
export IntPhisAcum=${IntPhisAcum}
export Cond_Glob=${Cond_Glob}
export TypRun=${TypRun}
export slope=${slope}
export vegflag=${vegflag}
export sstflg=${sstflg}
export soilmoist=${soilmoist}
export newglobalsoil=${newglobalsoil}
export qtdtypsolo=${qtdtypsolo}
export topo=${topo}
export prop1=${prop1}
export prop2=${prop2}
export prop3=${prop3}
export prop4=${prop4}

EOF
cat << EOF > ${Eta_scr_out}/SetVarModel
export Run_Date=${Run_Date}
export Eta_home=${Eta_home}
export Eta_home1=${Eta_home1}
export Eta_wrk=${Eta_wrk}
export Eta_scr=\${Eta_home1}/eta/${Conf}/scripts
export Eta_scr_out=${Eta_scr_out}
export Eta_run=\${Eta_wrk}/eta/${Conf}/${Run_Date}${LabRod}${Res}km_${paramet}
export Eta_ucl=${Eta_ucl}
export Eta_fix=\${Eta_home1}/eta/fix
export Eta_wrk_fix=${Eta_home1}/eta/${Conf}/fix
export Eta_exe=\${Eta_home1}/eta/${Conf}/exe
export dprep_exe=${Eta_dprep}/exe
export data_prep=\${Eta_run}
export Lon=${Lon} 
export Lat=${Lat}
export IM=${IM}
export JM=${JM}
export LM=${LM}
export LSM=${LSM}
export PT=${PT}
export Res=${Res}
export resol=${resol}
export Fctexec=${Fctexec} 
export LabRod=${LabRod} 
export Conf=${Conf}
export Fctexec=${Fctexec} 
export npr=${npr}
export npio_server_groups=${npio_server_groups} 
export npio_server=${npio_server}
export Fct=${Fct}
export FInitBC=${FInitBC}
export InitBC=${InitBC}

EOF
cat << EOF > ${Eta_scr_out}/SetVarModelPos
export Run_Date=${Run_Date}
export Eta_home=${Eta_home}
export Eta_home1=${Eta_home1}
export Eta_wrk=${Eta_wrk}
export LabRod=${LabRod} 
export Conf=${Conf}
export Res=${Res}
export resol=${resol}
export Eta_scr=\${Eta_home1}/eta/${Conf}/scripts
export Eta_scr_out=${Eta_scr_out}
export Eta_run=\${Eta_wrk}/eta/${Conf}/${Run_Date}${LabRod}${Res}km_${paramet}
export Eta_binctl=\${Eta_wrk}/eta/${Conf}/binctl/${paramet}/${Run_Date}
export Eta_posfig=\${Eta_wrk}/eta/${Conf}/posfig
export Eta_grbctl=\${Eta_wrk}/eta/${Conf}/grbctl/${paramet}/${Run_Date}
export Eta_indsev=\${Eta_wrk}/eta/${Conf}/indsev/${paramet}/${Run_Date}
export Eta_exe=\${Eta_home1}/eta/${Conf}/exe
export Eta_ucl=${Eta_ucl}
export postout=${postout}
export Fct=${Fct}
export Fcts=000
export paramet=${paramet}
export IntFct=${IntFct}
export FInitBC=${FInitBC}
export InitBC=${InitBC}
export IntPhisAcum=${IntPhisAcum}

EOF


cat ${Eta_scr}/main.scr_Templ          | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/main_${Conf}.scr 
cat ${Eta_scr}/process_grib.ksh_Templ  | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/process_grib.ksh
cat ${Eta_scr}/new_prep.sh_Templ       | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" >  ${Eta_run}/new_prep.sh
cat ${Eta_scr}/run.com_real_mpi        | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModel"     > ${Eta_run}/run.com_real_mpi
cat ${Eta_scr}/outjob_special          | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPos"  > ${Eta_run}/outjob_special
cat ${Eta_scr}/outjob_special_prof     | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPos"  > ${Eta_run}/outjob_special_prof
cat ${Eta_scr}/outjob_special.ksh      | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPos"  > ${Eta_run}/outjob_special.ksh
cat ${Eta_scr}/Finaliza_${postout}.ksh | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPos"  > ${Eta_run}/Finaliza.ksh 
cat ${Eta_scr}/Indice.ksh_Templ        | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPos"  > ${Eta_run}/Indice.ksh
cat ${Eta_scr}/SubmitPos.ksh           | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPos"  > ${Eta_run}/SPos${Run_Date}.ksh

chmod 755 ${Eta_scr_out}/main_${Conf}.scr
chmod 755 ${Eta_scr_out}/process_grib.ksh
chmod 755 ${Eta_run}/new_prep.sh
chmod 755 ${Eta_run}/run.com_real_mpi
chmod 755 ${Eta_run}/outjob_special
chmod 755 ${Eta_run}/outjob_special_prof
chmod 755 ${Eta_run}/outjob_special.ksh
chmod 755 ${Eta_run}/Finaliza.ksh
chmod 755 ${Eta_run}/Indice.ksh
chmod 755 ${Eta_run}/SPos${Run_Date}.ksh

####################################################
# sst
cat ${Eta_scr}/get_sst_data.ksh_Templ  | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/get_sst.ksh
chmod 755 ${Eta_scr_out}/get_sst.ksh
${Eta_scr_out}/get_sst.ksh

####################################################
# sst, ic and bc for the specified model driver

case "${CInit}" in
        "cfsr") cat ${Eta_scr}/get_cfsr_data.scr_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/get_data.ksh
                chmod 755 ${Eta_scr_out}/get_data.ksh
		${Eta_scr_out}/get_data.ksh;;

         "cfs") cat ${Eta_scr}/get_cfs_data_Templ.ksh | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/get_data.ksh
	        chmod 755 ${Eta_scr_out}/get_data.ksh
		${Eta_scr_out}/get_data.ksh ${ENS_Member} 000 000;;

		
  "gfs2gr0.25") cat ${Eta_scr}/get_gfs0.25_data.ksh_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/get_data.ksh
                cat ${Eta_scr}/gfs2gr0.25_deco.sh_Templ   | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/gfs2_deco.sh
		chmod 755 ${Eta_scr_out}/get_data.ksh
	        chmod 755 ${Eta_scr_out}/gfs2_deco.sh
		${Eta_scr_out}/get_data.ksh 000 000
		${Eta_scr_out}/gfs2_deco.sh 000 000;;		

		
    "cpteta40") cat ${Eta_scr}/get_cpteta_data.ksh_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/get_data.ksh
                chmod 755 ${Eta_scr_out}/get_data.ksh
		${Eta_scr_out}/get_data.ksh;;

		
    "cpteta15") cat ${Eta_scr}/get_cpteta_data.ksh_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/get_data.ksh
                chmod 755 ${Eta_scr_out}/get_data.ksh
		${Eta_scr_out}/get_data.ksh

esac

case "${BCond}" in
        "cfsr") cat ${Eta_scr}/get_cfsr_data.scr_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/get_data.ksh
                chmod 755 ${Eta_scr_out}/get_data.ksh
		${Eta_scr_out}/get_data.ksh ;;

         "cfs") cat ${Eta_scr}/get_cfs_data_Templ.ksh | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/get_data.ksh
	        chmod 755 ${Eta_scr_out}/get_data.ksh
		${Eta_scr_out}/get_data.ksh ${ENS_Member} ${InitBC} ${Fct};;

		
  "gfs2gr0.25") cat ${Eta_scr}/get_gfs0.25_data.ksh_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/get_data.ksh
                cat ${Eta_scr}/gfs2gr0.25_deco.sh_Templ   | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/gfs2_deco.sh
		chmod 755 ${Eta_scr_out}/get_data.ksh 
	        chmod 755 ${Eta_scr_out}/gfs2_deco.sh
		${Eta_scr_out}/get_data.ksh ${InitBC} ${Fct}
		${Eta_scr_out}/gfs2_deco.sh ${InitBC} ${Fct};;		

		
    "cpteta40") cat ${Eta_scr}/get_cpteta_data.ksh_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/get_data.ksh
                chmod 755 ${Eta_scr_out}/get_data.ksh
		${Eta_scr_out}/get_data.ksh;;

		
    "cpteta15") cat ${Eta_scr}/get_cpteta_data.ksh_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarModelPrep" > ${Eta_scr_out}/get_data.ksh
                chmod 755 ${Eta_scr_out}/get_data.ksh
		${Eta_scr_out}/get_data.ksh

esac

echo ${Run_Date} | cut -c3-10 > ${Eta_run}/adate.txt


cat << EOF > ${Eta_scr_out}/${LabRod}${Run_Date}
#!/bin/ksh -x

cd ${Eta_scr_out}
${Eta_scr_out}/main_${Conf}.scr  
EOF

# Submit model
chmod 755 ${Eta_scr_out}/${LabRod}${Run_Date}
${Eta_scr_out}/${LabRod}${Run_Date}

#############################################################################

# Make symbolic link to an output dir
cd ${Eta_scr_out}
rm -f dirout
ln -s ${Eta_run} dirout

exit 

