#!/bin/ksh 
#
Run_Date=$1


#Get IC, BC and SST?

get_all="no"

ldm=`echo ${Run_Date} | cut -c7-8`
anomesdia=`echo ${Run_Date} | cut -c3-8`

mes=`echo ${Run_Date} | cut -c5-6`

if (( $mes >= 10 )) ; then
 mes_sst=`echo ${Run_Date} | cut -c5-6`
  else
 mes_sst=`echo ${Run_Date} | cut -c6-6`
fi

##### DEVE SER USADO O SEGUNDO MES DE JANEIRO ESCRITO NO ARQUIVO SST DE 24 TEMPOS 
if (( "$mes_sst" == "1" )) ; then
 mes_sst=13
fi

DATASST=`echo ${Run_Date} | cut -c 1-6`

ldm1=`../../../util/caldate.3.0 ${Run_Date} + 0d   "ld"`
ldm2=`../../../util/caldate.3.0 ${Run_Date} + 30d  "ld"`
ldm3=`../../../util/caldate.3.0 ${Run_Date} + 60d  "ld"`
ldm4=`../../../util/caldate.3.0 ${Run_Date} + 90d  "ld"`
ldm5=`../../../util/caldate.3.0 ${Run_Date} + 120d "ld"`
data_cfs=`../../../util/caldate.3.0 ${Run_Date} + 0d "yyyymmdd"`

let diai=${ldm1}-${ldm}
let Ndias=${diai}+${ldm2}+${ldm3}+${ldm4}+${ldm5}
let Nfct=${Ndias}*24
#Nfct=6
#
#INCLUI os diretorios de scripts e de saida 
Eta_home=/stornext/home/etaclim/sazonal/worketa
Eta_home1=/scratchin/grupos/eta/home/etaclim/sazonal/worketa
Eta_wrk=/scratchout/grupos/eta/home/etaclim/sazonal/worketa
#Configuracao Inicial da rodada

Lon=-60.0
Lat=-16.0
IM=227 
JM=351 
LM=38 
LSM=20
Res=40
resol=0.40
Fctexec=etafcst_zhao.x 
LabRod=Etasa 
Conf=227X351X38_oper_kieee
Fct=6
IntFct=1
FInitBC=T062L28_2
PT=25
InitBC=6
TInitBC=1
IntPhisAcum=6.0
TypRun=simulation
slope=.false.
soilmoist=.false.
vegflag=.true.
postout=latlonnopack
sstflg=.true.
qtdtypsolo=9
npr=360
npio_server=24
#
Eta_dprep=${Eta_home}/dprep
Eta_scr=${Eta_home1}/eta/${Conf}/scripts
Eta_run=${Eta_wrk}/eta/${Conf}/${Run_Date}${LabRod}${Res}km
Eta_ucl=${Eta_home1}/eta/${Conf}/ucl
Eta_grb=${Eta_home1}/eta/${Conf}/grb
Eta_Templ=${Eta_home}/eta/template
data_prep=${Eta_run}

# Make scr output dir
mkdir ${Eta_run}
mkdir ${Eta_run}/outjob
mkdir -p ${Eta_scr}/relatorios

if [ -s ${Eta_home1}/data/grib/sstoi_1x1_${data_cfs}_Eta ] ; then
echo "PREPARANDO A SST sstoi_1x1_${data_cfs}_Eta"
cat ${Eta_scr}/prepara_sst.f90_Templ | sed -e "s:file1:${Eta_home1}/data/grib/sstoi_1x1_${data_cfs}_Eta:g" | \
            sed -e "s:file2:${Eta_run}/sstoi_1x1_${data_cfs}_Eta:g" > ${Eta_run}/prepara_sst.f90

ftn -o ${Eta_run}/prepara_sst.x ${Eta_run}/prepara_sst.f90  
${Eta_run}/prepara_sst.x
chmod 755 ${Eta_run}/sstoi_1x1_${data_cfs}_Eta

else
echo "O ARQUIVO sstoi_1x1_${data_cfs}_Eta DA SST NAO EXISTE"
exit
fi

let nprp1=${npr}+${npio_server}

${Eta_scr}/get_global_data_CFS.ksh ${Run_Date}

Eta_scr_out=${Eta_scr}/output/${Run_Date}${LabRod}${Res}km
mkdir -p ${Eta_scr_out}

DLMD=`${Eta_scr}/def_eta.scr ${Res}|awk '{print $1}'`
DPHD=`${Eta_scr}/def_eta.scr ${Res}|awk '{print $2}'`

cd ${Eta_scr}

cat << EOF > ${Eta_scr_out}/SetVar
Run_Date=${Run_Date}
Eta_home=${Eta_home}
Eta_home1=${Eta_home1}
Eta_wrk=${Eta_wrk}
Eta_scr=\${Eta_home1}/eta/${Conf}/scripts
Eta_scr_out=${Eta_scr_out}
Eta_run=\${Eta_wrk}/eta/${Conf}/${Run_Date}${LabRod}${Res}km
Eta_binctl=\${Eta_wrk}/eta/${Conf}/binctl/${Run_Date}
Eta_posfig=\${Eta_wrk}/eta/${Conf}/posfig
Eta_grbctl=/stornext/online1/eta/etaclim/climatologia_nova/BMJ_ZHAO
Eta_indsev=\${Eta_wrk}/eta/${Conf}/indsev
Eta_ucl=${Eta_ucl}
Eta_fix=${Eta_home1}/eta/fix
Eta_wrk_fix=\${Eta_home1}/eta/${Conf}/fix
Eta_src=${Eta_home1}/eta/src
Eta_exe=\${Eta_home1}/eta/${Conf}/exe
dprep_bin=\${Eta_home}/dprep/bin
data_soilp=/scratchin/grupos/eta/static/newsoil
data_grib=${Eta_wrk}/data/${FInitBC}
data_prep=${data_prep}
data_topo=/scratchin/grupos/eta/static/topo
data_veg=/scratchin/grupos/eta/static/veg
Eta_datain=\${Eta_home1}/eta/${Conf}/datain
Eta_static=/scratchin/grupos/eta/static
Eta_conf=\${Eta_home}/eta/${Conf}/configuracao
Lon=${Lon} 
Lat=${Lat}
IM=${IM}
JM=${JM}
LM=${LM}
LSM=${LSM}
Res=${Res}
resol=${resol}
DLMD=${DLMD}
DPHD=${DPHD}
Fctexec=${Fctexec} 
LabRod=${LabRod} 
Conf=${Conf}
Fct=${Nfct}
IntFct=${IntFct}
FInitBC=${FInitBC}
InitBC=${InitBC}
TInitBC=${TInitBC}
IntPhisAcum=${IntPhisAcum}
TypRun=${TypRun}
slope=${slope}
vegflag=${vegflag}
qtdtypsolo=${qtdtypsolo}
soilmoist=${soilmoist}
sstflg=${sstflg}
postout=${postout}
IntFig=3
npr=${npr}
EOF

cat << EOF > ${Eta_scr_out}/SetVar_csh
set Run_Date=${Run_Date}
set Eta_home=${Eta_home}
set Eta_home1=${Eta_home1}
set Eta_wrk=${Eta_wrk}
set Eta_scr=\${Eta_home1}/eta/${Conf}/scripts
set Eta_scr_out=${Eta_scr_out}
set Eta_run=\${Eta_wrk}/eta/${Conf}/${Run_Date}${LabRod}${Res}km
set Eta_binctl=\${Eta_wrk}/eta/${Conf}/binctl/${Run_Date}${LabRod}${Res}km
set Eta_grbctl=/stornext/online1/eta/etaclim/climatologia_nova/BMJ_ZHAO
set Eta_ucl=${Eta_ucl}
set Eta_fix=${Eta_home1}/eta/fix
set Eta_wrk_fix=\${Eta_home1}/eta/${Conf}/fix
set Eta_exe=\${Eta_home1}/eta/${Conf}/exe
set dprep_exe=${Eta_dprep}/exe
set Eta_src=${Eta_home1}/eta/src
set dprep_bin=${Eta_dprep}/bin
set data_soilp=/scratchin/grupos/eta/static/newsoil
set data_grib=${Eta_wrk}/data/${FInitBC}
set data_prep=${data_prep}
set data_topo=/scratchin/grupos/eta/static/topo
set data_veg=/scratchin/grupos/eta/static/veg
set Eta_datain=\${Eta_home1}/eta/${Conf}/datain
set Eta_static=/scratchin/grupos/eta/static
set Eta_conf=\${Eta_home}/eta/${Conf}/configuracao
set Lon=${Lon} 
set Lat=${Lat}
set IM=${IM}
set JM=${JM}
set LM=${LM}
set Res=${Res}
set DLMD=${DLMD}
set DPHD=${DPHD}
set Fctexec=${Fctexec} 
set LabRod=${LabRod} 
set Conf=${Conf}
set Fct=${Nfct}
set IntFct=${IntFct}
set FInitBC=${FInitBC}
set InitBC=${InitBC}
set TInitBC=${TInitBC}
set IntPhisAcum=${IntPhisAcum}
set TypRun=${TypRun}
set slope=${slope}
set vegflag=${vegflag}
set qtdtypsolo=${qtdtypsolo}
set soilmoist=${soilmoist}
set sstflg=${sstflg}
set postout=${postout}
EOF

cat << EOF > ${Eta_scr_out}/SetVarM
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
export Eta_binctl=\${Eta_wrk}/eta/${Conf}/binctl/${Run_Date}
export Eta_posfig=\${Eta_wrk}/eta/${Conf}/posfig
export Eta_grbctl=/stornext/online1/eta/etaclim/climatologia_nova/BMJ_ZHAO
export Eta_indsev=\${Eta_wrk}/eta/${Conf}/indsev
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 dprep_bin=\${Eta_home}/dprep/bin
export data_soilp=/scratchin/grupos/eta/static/newsoil
export data_grib=${Eta_wrk}/data/${FInitBC}
export data_prep=${data_prep}
export data_topo=/scratchin/grupos/eta/static/topo
export data_veg=/scratchin/grupos/eta/static/veg
export Eta_datain=\${Eta_home1}/eta/${Conf}/datain
export Eta_static=/scratchin/grupos/eta/static
export Eta_conf=\${Eta_home}/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 DLMD=${DLMD}
export DPHD=${DPHD}
export Fctexec=${Fctexec} 
export LabRod=${LabRod} 
export Conf=${Conf}
export Fct=${Nfct}
export Fcts=000
export IntFct=${IntFct}
export FInitBC=${FInitBC}
export InitBC=${InitBC}
export TInitBC=${TInitBC}
export IntPhisAcum=${IntPhisAcum}
export TypRun=${TypRun}
export slope=${slope}
export vegflag=${vegflag}
export sstflg=${sstflg}
export qtdtypsolo=${qtdtypsolo}
export soilmoist=${soilmoist}
export postout=${postout}
export IntFig=3
export npr=${npr}
export npio_server=${npio_server}
EOF

cat ${Eta_scr}/roda_mod.ksh_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarM" > ${Eta_scr}/output/${Run_Date}${LabRod}${Res}km/roda_mod.ksh
cat ${Eta_scr}/main.scr_Templ  | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarM" > ${Eta_scr}/output/${Run_Date}${LabRod}${Res}km/main_${Conf}.scr 
cat ${Eta_scr}/get_data.scr_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVar_csh" > ${Eta_scr}/output/${Run_Date}${LabRod}${Res}km/get_data.scr
cat ${Eta_scr}/process_grib.scr_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVar_csh" > ${Eta_scr}/output/${Run_Date}${LabRod}${Res}km/process_grib.scr
cat ${Eta_scr}/new_prep.sh_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVar" >  ${Eta_run}/new_prep.sh
cat ${Eta_scr}/run.com_real_mpi | sed -e "s:date_in:${Run_Date}:g" | sed -e "s:cam:${Eta_run}:g" | sed -e "s:nome:Eta${Res}${Run_Date}:g" | sed -e "s:nproc:${nprp1}:g" | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarM" > ${Eta_run}/run.com_real_mpi
cat ${Eta_scr}/outjob_special | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarM" > ${Eta_run}/outjob_special
cat ${Eta_scr}/outjob_special_prof | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarM" > ${Eta_run}/outjob_special_prof
cat ${Eta_scr}/outjob_special.ksh | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarM" | sed -e "s:dia:${anomesdia}:g" > ${Eta_run}/outjob_special.ksh
cat ${Eta_scr}/Finaliza_${postout}.ksh | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarM" > ${Eta_run}/Finaliza.ksh
cat ${Eta_scr}/SubmitPos.ksh | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarM" | sed -e "s:date_in:${Run_Date}:g" | sed -e "s:cam:${Eta_run}:g" > ${Eta_run}/SPos${Run_Date}.ksh
#cat ${Eta_scr}/Indice.ksh_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVar" > ${Eta_run}/Indice.ksh
#cat ${Eta_scr}/gera_figuras.ksh_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVar" > ${Eta_run}/gera_figuras.ksh

if [ "${postout}" = "latlonnopack" ] ; then
cp ${Eta_ucl}/cntrl.parm_NOPACK ${Eta_run}/cntrl.parm_NOPACK
cp ${Eta_grb}/* ${Eta_run}/.
cat ${Eta_run}/gribeta_Templ | sed "/VARIAVEIS/r ${Eta_scr_out}/SetVarM" > ${Eta_run}/gribeta 
cp ${Eta_ucl}/wgts1 ${Eta_run}/.
else
cp ${Eta_ucl}/cntrl.parm ${Eta_run}/cntrl.parm
fi

cp ${Eta_ucl}/solar ${Eta_run}

echo "${mes_sst}" > ${Eta_run}/tempo_init_sst.txt
chmod 755 ${Eta_run}/tempo_init_sst.txt

######cp ${Eta_ucl}/InputModelInf_${FInitBC}.txt ${data_prep}/InputModelInf.txt
echo ${Run_Date} | cut -c3-10 > ${data_prep}/adate.txt

cp ${Eta_home1}/eta/fix/deta_files/deta_${LM} ${Eta_home1}/eta/${Conf}/fix/deta
cp ${Eta_home1}/eta/fix/rad_files/co2.${LM}_50mb ${Eta_home1}/eta/${Conf}/fix/co2.dat
#cp ${Eta_home1}/eta/fix/rad_files/co2.${LM}_${PT}mb ${Eta_home1}/eta/${Conf}/fix/co2.dat
cp ${Eta_run}/sstoi_1x1_${data_cfs}_Eta ${Eta_run}/sst_1x1.dat

chmod 755 ${Eta_scr_out}/main_${Conf}.scr
chmod 755 ${Eta_scr_out}/get_data.scr
chmod 755 ${Eta_scr_out}/process_grib.scr
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}/gribeta 
chmod 755 ${Eta_run}/SPos${Run_Date}.ksh
#chmod 755 ${Eta_run}/Indice.ksh
#chmod 755 ${Eta_run}/gera_figuras.ksh

# Get ic and cc from ftp site 
if [ "${get_all}" = "yes" ] ; then
  ${Eta_scr_out}/get_data.scr 00 ${Fct} ${FInitBC} ${InitBC} ${data_rod}
fi

cat << EOF > ${Eta_scr_out}/${LabRod}${Run_Date}
#!/bin/ksh -x
#PBS -N Eta${Res}${Run_Date}
#PBS -lselect=1:ncpus=1
#PBS -A CPTEC
#PBS -o ${Eta_run}/Etap${Res}${Run_Date}
#PBS -j oe
#PBS -q pesq.q

cd ${Eta_scr_out}
${Eta_scr_out}/main_${Conf}.scr  ${Run_Date} ${Fct} ${FInitBC} ${InitBC} ${TInitBC} ${Res} ${Lon} ${Lat} ${IM} ${JM} ${LM} ${LabRod}
EOF

export PBS_SERVER=aux20-eth4

# Submit model
qsub ${Eta_scr_out}/${LabRod}${Run_Date}

# Submit Pos
qsub ${Eta_run}/SPos${Run_Date}.ksh
#############################################################################

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

exit 
