#!/bin/ksh -x


# VARIAVEIS
cd ${Eta_scr}

Run_Date1=${Run_Date}
let Fctp1=${Fct}+1

dlmd=`${Eta_scr}/def_eta.scr ${Res}|awk '{print $1}'`
dphd=`${Eta_scr}/def_eta.scr ${Res}|awk '{print $2}'`
dt=`${Eta_scr}/def_eta.scr ${Res}|awk '{print $3}'`
nphs=`${Eta_scr}/def_eta.scr ${Res}|awk '{print $4}'`
ncnv=`${Eta_scr}/def_eta.scr ${Res}|awk '{print $5}'`
hydro=`${Eta_scr}/def_eta.scr ${Res}|awk '{print $6}'`
CInit_Lowercase=`echo ${CInit}|awk '{print tolower($1)}'`
BCond_Lowercase=`echo ${BCond}|awk '{print tolower($1)}'`
extprep=".ETAwrk"

let ninit=${Fct}/${InitBC}
let ninit=${ninit}+1

typeset -Z4 iyearf
typeset -Z2 imonth
typeset -Z2 iday
typeset -Z2 ih
iyear=`echo ${Run_Date} | cut -c1-4`
imonth=`echo ${Run_Date} | cut -c5-6`
iday=`echo ${Run_Date} | cut -c7-8`
ih=`echo ${Run_Date} | cut -c9-10`

cp ${Eta_ucl}/solar ${Eta_run}/solar
cp ${Eta_home1}/eta/fix/deta_files/deta_${LM} ${Eta_home1}/eta/${Conf}/fix/deta
cp ${Eta_home1}/eta/fix/rad_files/co2.${LM}_${PT}mb ${Eta_home1}/eta/${Conf}/fix/co2.dat

if [ "${postout}" = "latlonnopack" ] ; then

if [  ${Eta_ucl}/wgts1 ] ; then
cp ${Eta_ucl}/wgts1 ${Eta_run}/wgts1
fi

cp ${Eta_ucl}/cntrl.parm_NOPACK ${Eta_run}/cntrl.parm_NOPACK
cp ${Eta_grb}/cptec.table ${Eta_run}/
else
cp ${Eta_ucl}/cntrl.parm_GRIBIT ${Eta_run}/cntrl.parm_GRIBIT
fi


${Eta_scr_out}/process_grib.ksh

err=$?
if [ ${err} -ne 0 ] ; then
  echo "ERRO NO PREPROC!!"
  echo "Finalizando a rodada...."
  exit 1
fi


if [ "${TypRun}" = "forecast" ] ; then
typeset -Z6 fct
fct=0000
i=1
arq[${i}]=${CInit_Lowercase}_${Run_Date1}.${fct}${extprep}
echo "  INIT_IN(${i})='${data_prep}/${arq[${i}]}'">>./ICCC
let i=2
if ((${TInitBC}==0)) ; then
 Run_Date1=`${Eta_home1}/util/caldate.3.0 ${Run_Date} - ${HInitBC}hr 'yyyymmddhh'`
 let fct=${fct}+${InitBC}+${HInitBC}
else
 let fct=${fct}+${InitBC} 
fi
while ((${i}<=${ninit})) ; do
  arq[${i}]=${BCond_Lowercase}_${Run_Date1}.${fct}${extprep}
  echo "  INIT_IN(${i})='${data_prep}/${arq[${i}]}'">>./ICCC
let i=${i}+1
let fct=${fct}+${InitBC}
done
else
typeset -Z6 fct
typeset -Z6 icn
fct=0000
icn=0000
i=1
while ((${i}<=${ninit})) ; do
  arq[${i}]=${CInit_Lowercase}_${Run_Date}.${icn}${extprep}
  echo "  INIT_IN(${i})='${data_prep}/${arq[${i}]}'">>./ICCC
let i=${i}+1
let icn=${icn}+${InitBC}
if ((${TInitBC}==0)) ; then
 Run_Date1=`${Eta_home1}/util/caldate.3.0 ${Run_Date} - 12hr 'yyyymmddhh'`
if ((${InitBC}==06)) ; then
 fct=18
else
 fct=15
fi
fi
done
fi


typeset -Z6 fcthr
fcthr=0000
nfctout=0
echo "TSHDE="|awk '{printf ("%s",$1)}'>./FCTHR
while ((${fcthr}<=${Fct})) ; do
echo ${fcthr} |awk '{printf ("%s,",$1)}'>>./FCTHR
let rest=${fcthr}%10
if (((${fcthr}!=0000)  && (${rest}==0))) ; then
echo ${fcthr} |awk '{printf ("\n"," ")}'>>./FCTHR
fi
let fcthr=${fcthr}+${IntFct}
let nfctout=${nfctout}+1
done
echo ${fcthr} |awk '{printf ("\n"," ")}'>>./FCTHR


cat << EOF > ${Eta_run}/slope.nml
 &SLOPENML
  SLOPE=${slope}
 &END
EOF

cat << EOF > ${Eta_run}/GSPREC.nml
 &GSPRECNML
  GSPREC=${gsprec}
 &END
EOF

cat << EOF > ${Eta_run}/SSTFLAG
 &FLGSST
   LSST=${sstflg}
 &END
EOF

cat << EOF4 > ${Eta_run}/SOILMOIST
 &DANMOIST
  moistdan=${soilmoist}
 &END
EOF4

cat << EOF4 > ${Eta_run}/NEWGLOBALSOIL
 &GLOBALSOIL
  soilglobal=${newglobalsoil}
 &END
EOF4

cat << EOF5 > ${Eta_run}/MOISTPROP
 &DANPROP
  prop1=${prop1}
  prop2=${prop2}
  prop3=${prop3}
  prop4=${prop4}
 &END
EOF5

cat << EOF6 > ${Eta_run}/TYPSOLO
${qtdtypsolo}
EOF6

if [ "${topo}" = "1km" ] ; then
export data_topo=${Eta_static}/eta/static/topo
SEARES=30.
elif [ "${topo}" = "90m" ] ; then
export data_topo=${Eta_static}/eta/static/topo90m
SEARES=3.0
fi

let PTINP=${PT}*100
cat << EOF > ${Eta_run}/ETAIN
 &MODEL_GRIDS
  TLM0D=${Lon}
  TPH0D=${Lat}
  IM=${IM}
  JM=${JM}
  LM=${LM}
  PTINP=${PTINP}.
  DLMD=${dlmd}
  DPHD=${dphd}
  DT=${dt}
  IDTAD=2
  IMONTH=${imonth}
  IDATE=${iday}
  IYEAR=${iyear}
  ISTRTIM=${ih}
  NSOIL=4
  NINIT=${ninit}
  TBOCO=${InitBC}
EOF
cat ./ICCC >> ${Eta_run}/ETAIN 
cat << EOF1 >> ${Eta_run}/ETAIN
  INIT_GDSDIR='${data_prep}/gdsinfo${extprep}'
  INIT_OUT='${Eta_run}'
 &END

 &SFC_INIT
  TOPO_IN='${data_topo}/'
  TOPO_OUT='${Eta_wrk_fix}/etatopo.dat'
  SEARES=$SEARES
  FRACLK=0.3
  GRIBSOIL=.TRUE.
  SIGMA=.FALSE.
  REAN=.FALSE.
  REAN_SFC='${data_grib}/grb2d93031200'
 &END
EOF1

DIR_SMASKS_3S=${Eta_fix}/smasks_3s

cat << EOF1 > ${Eta_run}/seamaskdata.dat
 &lsmkdata
  froot='${DIR_SMASKS_3S}', !diretorio de busca das mascaras de oceano-terra (3s) no formato .bin
  res=1200.d0
 &end
EOF1

cat << EOF2 > ${Eta_run}/VEGETATION
 &VEG
  vegflag=${vegflag}
 &END
EOF2


cat << EOF > ${Eta_run}/fcstdata.meso
 &FCSTDATA                                                           
 TSTART=00.0,TEND=${Fct}.00,TCP=${Fctp1},RESTRT=.FALSE.,SINGLRST=.TRUE.,SUBPOST=.FALSE.,
 NMAP=${nfctout},
EOF

cat ./FCTHR >> ${Eta_run}/fcstdata.meso

cat ${Eta_ucl}/PressLevel >> ${Eta_run}/fcstdata.meso

cat << EOF >> ${Eta_run}/fcstdata.meso
 NPHS=${nphs},NCNVC=${ncnv},NRADSH=1,NRADLH=1,NTDDMP=1,                       
 TPREC=${IntPhisAcum},THEAT=${IntPhisAcum},TCLOD=${IntPhisAcum},
 TRDSW=${IntPhisAcum},TRDLW=${IntPhisAcum},TSRFC=${IntPhisAcum},
 NEST=.FALSE.,HYDRO=${hydro},SPLINE=.TRUE.
 &END
 &POSTLIST
  CHMO='chmod 755 ${Eta_run}/#'
  SUBMIT='qsub ${Eta_run}/#'
 &END                                                             
EOF

rm -f ./FCTHR
rm -f ./ICCC
# SST INDEX snowdepth hires_sst
cp ${Eta_fix}/sstgrb.index ${Eta_run}/sstgrb.index 
cp ${Eta_fix}/snowdepth.grb ${Eta_run}/snowdepth.grb
cp ${Eta_fix}/imssnow.grb ${Eta_run}/imssnow.grb
cp ${Eta_fix}/hires_sst ${Eta_run}/hires_sst

# NEW SST
sst_date=`${Eta_home1}/util/caldate.3.0 ${Run_Date} - 1d 'yyyymmdd'`
cp ${Eta_wrk}/data/grib/sst_Eta_${sst_date}.bin ${Eta_run}/sstgrb
#

cat << EOF > ${Eta_run}/run.preproc.nqs
#!/bin/ksh
#
cd ${Eta_run}
${Eta_run}/new_prep.sh
export err=$?
if [ ${err} -ne 0 ] ; then
  echo "ERRO NO PREPROC!!"
  echo "Finalizando a rodada...."
  exit 1
fi

# Submit Pos
${Eta_run}/SPos${Run_Date}.ksh > ${Eta_run}/SPos.out &

${Eta_run}/run.com_real_mpi
exit
EOF

chmod 755 ${Eta_run}/run.preproc.nqs
${Eta_run}/run.preproc.nqs
