#!/bin/ksh -x

# by LAMOSA 
#######!/bin/sh -x

# VARIAVEIS


cd ${Eta_run}
# Incluir compilador ------------------------------------------------
FC=`cat ${Eta_src}/configure/make.inc | cut -f1 -d# | grep FC | sed -e s:FC\=:\:g`

# head is used for symbolic links.  
# head="ftn" for HP workstations
# head="fort." for most (all?) others.

head="fort."
rm -f ${head}*
#Limpeza do diretorio run
rm -f ${Eta_run}/preproc.*
rm -f ${Eta_run}/sst12m.file
rm -f ${Eta_run}/cnst.file
rm -f ${Eta_run}/init.file
rm -f ${Eta_run}/bndy.newstyle
rm -f ${Eta_run}/bndy.file

EINTLM0D=`cat ${Eta_run}/ETAIN | grep TLM0D | cut -d= -f2`
EINTPH0D=`cat ${Eta_run}/ETAIN | grep TPH0D | cut -d= -f2`
EINIM=`cat ${Eta_run}/ETAIN | grep "IM=" | cut -d= -f2 |head -1`
EINJM=`cat ${Eta_run}/ETAIN | grep "JM=" | cut -d= -f2`
EINLM=`cat ${Eta_run}/ETAIN | grep "LM=" | cut -d= -f2`
EINDLMD=`cat  ${Eta_run}/ETAIN | grep DLMD | cut -d= -f2`
EINDPHD=`cat  ${Eta_run}/ETAIN | grep DPHD | cut -d= -f2`
CNFTLM0D=`cat ${Eta_scr}/CONF | grep Lon | awk '{print $2}'`
CNFTPH0D=`cat ${Eta_scr}/CONF | grep Lat | awk '{print $2}'`
CNFIM=`cat ${Eta_scr}/CONF | grep IM | awk '{print $2}'`
CNFJM=`cat ${Eta_scr}/CONF | grep JM |awk '{print $2}'`
CNFLM=`cat ${Eta_scr}/CONF | grep LM |awk '{print $2}'|head -1`
CNFDLMD=`cat  ${Eta_scr}/CONF | grep DLMD | awk '{print $2}'`
CNFDPHD=`cat  ${Eta_scr}/CONF | grep DPHD | awk '{print $2}'`

if [ "${EINTLM0D}" != "${CNFTLM0D}" ] || [ "${EINTPH0D}" != "${CNFTPH0D}" ]  \
|| [ "${EINIM}" != "${CNFIM}" ] || [ "${EINJM}" != "${CNFJM}" ]  \
|| [ "${EINLM}" != "${CNFLM}" ] || [ "${EINDLMD}" != "${CNFDLMD}" ]  \
|| [ "${EINDPHD}" != "${CNFDPHD}" ] 
then

echo Starting eta topography creation...

#### get the proper sea mask data uncompressed

if [ "${topo}" == "1km" ] ; then

data_topo=${Eta_static}/eta/static/topo

cp ${Eta_src}/prep/smask/smask_select.f ${Eta_run}/smask_select.f
cp ${Eta_exe}/select.x ${Eta_run}

# Pega o CPU time de ${Eta_run}/select.x

T1=`date +%s%N`
${Eta_run}/select.x
T2=`date +%s%N`

TEMPO_SELECT=`echo ${T2} - ${T1} | bc -l`
TEMPO_SELECT=`echo ${TEMPO_SELECT} / 1000000 | bc -l`
 

first=`head -1 tmp.smask`
last=`tail -1 tmp.smask`

tiles="01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 \
	 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36"

for tl in $tiles
do

if [ $tl -ge $first ]
then
if [ $tl -le $last ]
then
if [ ! -s smask.30s.$tl ]
then
echo "processing strip number " $tl
cp ${Eta_fix}/smasks_30s/smask.30s.${tl}.Z .
gzip -d  smask.30s.${tl}.Z
fi
fi
fi

done

# ***** T O P O ************

# Pega o CPU time de ${Eta_exe}/etatopo.exe ----------------------------------------------

T1=`date +%s%N`
${Eta_exe}/etatopo.exe > topo.out
T2=`date +%s%N`

TEMPO_ETATOPO=`echo ${T2} - ${T1} | bc -l`
TEMPO_ETATOPO=`echo ${TEMPO_ETATOPO} / 1000000 | bc -l`


err=$?
if [ $err -ne 0 ] ; then
echo "FALHA NA TOPOGRAFIA VERIFICAR O ARQUIVO /exe/etatopo.exe !!!!!!!!!!"
exit 1
fi

else

if [ "${topo}" == "90m" ] ; then

data_topo=${Eta_static}/eta/static/topo90m

cp ${Eta_src}/prep/smask/smask_select_3s.f ${Eta_run}/smask_select_3s.f
cp ${Eta_exe}/select_3s.x ${Eta_run}
# Pega o CPU time de ${Eta_run}/select.x
# by Lamosa PAD/CPTEC 2007

T1=`date +%s%N`
${Eta_run}/select_3s.x
T2=`date +%s%N`

TEMPO_SELECT=`echo ${T2} - ${T1} | bc -l`
TEMPO_SELECT=`echo ${TEMPO_SELECT} / 1000000 | bc -l`
 
# ***** T O P O ************

# Pega o CPU time de ${Eta_exe}/etatopo.exe ----------------------------------------------

T1=`date +%s%N`
${Eta_exe}/etatopo_3s.exe > topo.out
T2=`date +%s%N`

TEMPO_ETATOPO=`echo ${T2} - ${T1} | bc -l`
TEMPO_ETATOPO=`echo ${TEMPO_ETATOPO} / 1000000 | bc -l`


err=$?
if [ $err -ne 0 ] ; then
echo "FALHA NA TOPOGRAFIA VERIFICAR O ARQUIVO /exe/etatopo_3s.exe !!!!!!!!!!"
exit 1
fi
fi
fi

#rm ${Eta_wrk}/eta/${Conf}/saida_etatopo.txt 
# by Lamosa PAD/CPTEC 2007 -------------------------------------------------------------

unset F_UFMTADJUST F_UFMTIEEE
unset F_FF8 F_FF1
if [ "${vegflag}" = ".true." ] ; then
  cat > ./parmeta << EOF
       PARAMETER (IM=${EINIM},JM=${EINJM},IMJM=IM*JM-JM/2)
       PARAMETER (DLMD=${EINDLMD},
     &  DPHD=${EINDPHD})
       PARAMETER (TLM0D=${EINTLM0D},TPH0D=${EINTPH0D})
EOF

  F_UFMTIEEE=10,67
  export F_UFMTIEEE  
  F_UFMTADJUST=TYPE2
  export F_UFMTADJUST

  cp ${Eta_exe}/vegmsk.x ${Eta_run}

  ln -s -f   ${Eta_wrk_fix}/etatopo.dat    ${head}10
  ln -s -f   ${data_veg}/veg_americas_PROVEG_RADAM_urban_rj_sp.asc ${head}20
  ln -s -f   ${Eta_wrk_fix}/vegmask2d_umd_proveg_radam.bin ${head}31
  ln -s -f   ${Eta_wrk_fix}/vegmask1d_umd_proveg_radam.bin ${head}32
  ln -s -f   ${Eta_run}/vegsample.txt ${head}65

  #  Pega o CPU time de ${Eta_run}/vegmsk.x ------------------------------------------
  T1=`date +%s%N`
    ${Eta_run}/vegmsk.x > ${Eta_run}/veg.out
  T2=`date +%s%N`

  TEMPO_VEGMSK=`echo ${T2} - ${T1} | bc -l`
  TEMPO_VEGMSK=`echo ${TEMPO_VEGMSK} / 1000000 | bc -l`
  

  err=$?
  if [ $err -ne 0 ] ; then
    echo "FALHA NA VEGETACAO VERIFICAR O ARQUIVO Eta_run/vegmsk.x !!!!!!!!!"
    exit 1
  fi

fi

### NEWSOIL
if [ "${qtdtypsolo}" == "18" ] ; then
echo "newsoil" ${newsoil}
cat > ./parmetans << EOF22
       PARAMETER (IM=${EINIM},JM=${EINJM},IMJM=IM*JM-JM/2)
       PARAMETER (DLMD=${EINDLMD},
     &  DPHD=${EINDPHD})
       PARAMETER (TLM0D=${EINTLM0D},TPH0D=${EINTPH0D})
EOF22
cp ${Eta_src}/prep/newsoil/newsoil.f ${Eta_run}/newsoil.f
cp ${Eta_exe}/newsoil.x ${Eta_run}

  F_UFMTIEEE=10,67
  export F_UFMTIEEE  
  F_UFMTADJUST=TYPE2
  export F_UFMTADJUST
  ln -s -f   ${Eta_wrk_fix}/etatopo.dat    ${head}10
  ln -s -f   ${data_soilp}/mapa_suelos18_1km.dat ${head}33
  ln -s -f   ${Eta_wrk_fix}/SA_18SoilTypes_map_1km_2d.bin ${head}31
  ln -s -f   ${Eta_wrk_fix}/SA_18SoilTypes_map_1km_1d.bin ${head}32

  ${Eta_run}/newsoil.x > ${Eta_run}/soil.out
  err=$?
  if [ $err -ne 0 ] ; then
    echo "FALHA NO NEWSOIL VERIFICAR O ARQUIVO Eta_run/newsoil.x !!!!!!!!!"
    exit 1
  fi
chmod 775 ${Eta_wrk_fix}/SA_18SoilTypes_map_1km_2d.bin
chmod 775 ${Eta_wrk_fix}/SA_18SoilTypes_map_1km_1d.bin
cp ${Eta_wrk_fix}/SA_18SoilTypes_map_1km_2d.bin     ${Eta_run}/SoilMap_1km_2d.bin
fi

### NEWGLOBALSOIL
if [ "${qtdtypsolo}" == "15" ] ; then
cat > ./parmetagl << EOF23
       PARAMETER (IM=${EINIM},JM=${EINJM},IMJM=IM*JM-JM/2)
       PARAMETER (DLMD=${EINDLMD},
     &  DPHD=${EINDPHD})
       PARAMETER (TLM0D=${EINTLM0D},TPH0D=${EINTPH0D})
EOF23
cp ${Eta_src}/prep/newsoil/newglobalsoil.f ${Eta_run}/newglobalsoil.f
pgf90 -o ${Eta_run}/newglobalsoil.x ${Eta_run}/newglobalsoil.f

  F_UFMTIEEE=10,67
  export F_UFMTIEEE  
  F_UFMTADJUST=TYPE2
  export F_UFMTADJUST
  ln -s -f   ${Eta_wrk_fix}/etatopo.dat    ${head}10
  ln -s -f   ${data_soilp}/global_soil_map_1km.dat ${head}33
  ln -s -f   ${Eta_wrk_fix}/Global_15SoilTypes_map_1km_2d.bin ${head}31
  ln -s -f   ${Eta_wrk_fix}/Global_15SoilTypes_map_1km_1d.bin ${head}32


  ${Eta_run}/newglobalsoil.x > ${Eta_run}/newglobalsoil.out
  err=$?
  if [ $err -ne 0 ] ; then
    echo "FALHA NO NEWSOIL VERIFICAR O ARQUIVO Eta_run/newglobalsoil.x !!!!!!!!!"
    exit 1
  fi
chmod 775 ${Eta_wrk_fix}/Global_15SoilTypes_map_1km_2d.bin
chmod 775 ${Eta_wrk_fix}/Global_15SoilTypes_map_1km_1d.bin
cp ${Eta_wrk_fix}/Global_15SoilTypes_map_1km_2d.bin ${Eta_run}/SoilMap_1km_2d.bin
fi

cp ZEFF ${Eta_wrk_fix}


# Atualiza o arquivo CONF
echo "Lon "${Lon} >${Eta_scr}/CONF
echo "Lat "${Lat} >>${Eta_scr}/CONF
echo "IM "${IM} >>${Eta_scr}/CONF
echo "JM "${JM}  >>${Eta_scr}/CONF
echo "LM "${LM} >>${Eta_scr}/CONF
echo "DLMD "${EINDLMD} >>${Eta_scr}/CONF
echo "DPHD "${EINDPHD}  >>${Eta_scr}/CONF
echo "Res "${Res} >>${Eta_scr}/CONF
echo "Fctexec "${Fctexec} >>${Eta_scr}/CONF
echo "LabRod "${LabRod} >>${Eta_scr}/CONF
echo "Conf "${Conf} >>${Eta_scr}/CONF 
echo "Fct "${Fct} >>${Eta_scr}/CONF
echo "IntFct "${IntFct} >>${Eta_scr}/CONF
echo "FInitBC "${FInitBC} >>${Eta_scr}/CONF
echo "InitBC "${InitBC} >>${Eta_scr}/CONF
echo "TInitBC "${TInitBC} >>${Eta_scr}/CONF


unset F_UFMTADJUST F_UFMTIEEE
unset F_FF8 F_FF10 F_FF32 F_FF31 F_FF20 F_FF1 F_FF65
fi
# *****************

cp ${Eta_wrk_fix}/ZEFF ${Eta_run}/ZEFF

err=$?

if [ $err -eq 0 ]
then
echo " "
echo Starting eta initialization and bc file creation...

if [ -s ${Eta_run}/snowdepth.grb ]
then
echo "appear to have surface files"
else
echo "didnt find snow file...running get_sfcfields.com"
../scripts/get_sfcfields.com
fi

##################################
# Define o mapa de solo
 if [ "${qtdtypsolo}" = "18" ] ; then
  cp ${Eta_wrk_fix}/SA_18SoilTypes_map_1km_2d.bin     ${Eta_run}/SoilMap_1km_2d.bin 
 elif [ "${qtdtypsolo}" = "15" ] ; then
  cp ${Eta_wrk_fix}/Global_15SoilTypes_map_1km_2d.bin ${Eta_run}/SoilMap_1km_2d.bin 
 fi

#
##################################
#
# SOIL,VEG,SST and SNOW links
#
rm -f ${head}*
ln -s -f ${Eta_wrk_fix}/deta       deta
ln -s -f ${Eta_fix}/veg.eta.grb    veg.eta.grb
ln -s -f ${Eta_fix}/maxsnoalb.bin  maxsnoalb.bin
ln -s -f ${Eta_fix}/alb1_ieee      alb1_ieee
ln -s -f ${Eta_fix}/alb2_ieee      alb2_ieee
ln -s -f ${Eta_fix}/alb3_ieee      alb3_ieee
ln -s -f ${Eta_fix}/alb4_ieee      alb4_ieee
ln -s -f ${Eta_fix}/ivgtyp_1d_ieee ivgtyp_1d_ieee
ln -s -f ${Eta_fix}/isltyp_1d_ieee isltyp_1d_ieee
ln -s -f ${Eta_fix}/islope_1d_ieee islope_1d_ieee
ln -s -f ${Eta_datain}/hires_sst      hires_sst
ln -s -f ${Eta_datain}/sstgrb.index   sstgrb.index
ln -s -f ${Eta_fix}/rfusaflw_ieee  rfusaflw_ieee
ln -sf ${Eta_fix}/imsmask.ascii    imsmask.ascii 
ln -sf ${Eta_wrk_fix}/vegmask2d_umd_proveg_radam.bin     vegmask2d_umd_proveg_radam.bin


ln -s -f ${Eta_wrk_fix}/deta       ${head}16
ln -s -f ${Eta_fix}/maxsnoalb.bin  ${head}20
ln -s -f ${Eta_fix}/alb1_ieee      ${head}21
ln -s -f ${Eta_fix}/alb2_ieee      ${head}22
ln -s -f ${Eta_fix}/alb3_ieee      ${head}23
ln -s -f ${Eta_fix}/alb4_ieee      ${head}24
ln -s -f ${Eta_fix}/ivgtyp_1d_ieee ${head}30
ln -s -f ${Eta_fix}/isltyp_1d_ieee ${head}31
ln -s -f ${Eta_fix}/islope_1d_ieee ${head}32
ln -s -f ${Eta_datain}/sfcanl      ${head}33
ln -s -f ${Eta_run}/sstgrb         ${head}39
ln -s -f ${Eta_datain}/sstgrb.index   ${head}40
ln -s -f ${Eta_fix}/rfusaflw_ieee  ${head}42
ln -s -f ${Eta_fix}/imsmask.ascii    ${head}43
ln -s -f ${Eta_wrk_fix}/vegmask2d_umd_proveg_radam.bin     ${head}45
ln -s -f ${Eta_run}/SoilMap_1km_2d.bin                 ${head}49
ln -s -f ${Eta_run}/initial_soil_moist_1km_2d.bin          ${head}48
#
####################################
set F_UFMTIEEE=45,47,62,1,71
set F_UFMTADJUST1=TYPE2
set F_UFMTADJUST45=TYPE2
set F_UFMTADJUST47=TYPE2
set F_UFMTADJUST62=TYPE2
set F_UFMTADJUST71=TYPE2

ln -s -f veg.bin ${head}71

  # pega CPU time de ${Eta_exe}/initbc.exe -----------------------------------------
  
  T1=`date +%s%N`
  ${Eta_exe}/initbc.exe > initbc.out
  T2=`date +%s%N`

  TEMPO_INITBC=`echo ${T2} - ${T1} | bc -l`
  TEMPO_INITBC=`echo ${TEMPO_INITBC} / 1000000 | bc -l`


  err=$? 
  echo value of err is $err 
  if [ $err -ne 0 ] ; then
    echo "FALHA!!!!!!! VERIFICAR NO ARQUIVO /exe/initbc.exe"
    exit 1
  fi
fi


# Monta relatorio com os tempos gastos ############################################
#
# Formato: Tempos parciais e tempos finais
#          Tempos em segundos
# 
# by Lamosa PAD/CPTEC 2007 ########################################################

TOTAL=0
if [ ${TEMPO_SELECT}x != "x" ] ; then
  TOTAL=`echo "$TOTAL + ${TEMPO_SELECT}" | bc -l`
  TEMPO_SELECT=`echo "${TEMPO_SELECT} / 1000" | bc -l`
fi
if [ ${TEMPO_ETATOPO}x != "x" ] ; then
  TOTAL=`echo "$TOTAL + ${TEMPO_ETATOPO}" | bc -l`
  TEMPO_TOPO=`echo "${TEMPO_TOPO} / 1000" | bc -l`
fi
if [ ${TEMPO_VEGMSK}x != "x" ] ; then
  TOTAL=`echo "$TOTAL + ${TEMPO_VEGMSK}" | bc -l`
  TEMPO_VEGMSK=`echo "${TEMPO_VEGMSK} / 1000" | bc -l`
fi
if [ ${TEMPO_INITBC}x != "x" ] ; then
  TOTAL=`echo "$TOTAL + ${TEMPO_INITBC}" | bc -l`
  TEMPO_INITBC=`echo "${TEMPO_INITBC} / 1000" | bc -l`
fi

TOTAL=`echo "${TOTAL} / 1000" | bc -l`

cat << EOF > ${Eta_home1}/eta/${Conf}/scripts/relatorios/relatorio_${Run_Date}_${paramet}.txt 
Modelo ETA: Relatorio de tempos ############################################################

Tempos de pre-processamento ------------------------------
Tempo de select.x  = ${TEMPO_SELECT}s
Tempo de etatopo.x = ${TEMPO_ETATOPO}s
Tempo de vegmsk.x  = ${TEMPO_VEGMSK}s
Tempo de initbc.x  = ${TEMPO_INITBC}s

Tempo total de pre-processamento = ${TOTAL}s
# --------------------------------------------------------
EOF
# ########################################################

exit 0
