#!/bin/sh
set -aeux

IM0=401       # Dimension of each face, for cube the total number of grid points in one layer is IM0xIM0x6
LM=50        # Number of vertical layers, should always be 38 for this version
NSUB=10    # Subdivision number, e.g. if NSUB 2, each face are divided
NMIN=6    # Is the number of faces, should always be 6 for cube


TMP_DIR=/scratchin/grupos/grpeta/projetos/tempo/oper/GEF_v1.0.0_oper/gef_trunk/PRP/gef_tmp
PRP_DIR=/scratchin/grupos/grpeta/projetos/tempo/oper/GEF_v1.0.0_oper/gef_trunk/PRP
ETA_RUN=/scratchout/grupos/grpeta/projetos/tempo/oper/GEF_v1.0.0_oper/2020082112
FInitBC=gfs0.25

INIM0=`cat ${PRP_DIR}/../configure | grep "IM0=" | cut -d= -f2`
INLM0=`cat ${PRP_DIR}/../configure | grep "LM=" | cut -d= -f2`
INSUB=`cat ${PRP_DIR}/../configure | grep "NSUB=" | cut -d= -f2`
INNMI=`cat ${PRP_DIR}/../configure | grep "NMIN=" | cut -d= -f2`


if [ ! -s ${PRP_DIR}/data_in/grid/topo.dat ] ; then


cd ${PRP_DIR}/data_in/grid/

${PRP_DIR}/exe/initCube.exe > ${ETA_RUN}/../err/err.initcube 2>&1

${PRP_DIR}/exe/metrics.exe > ${ETA_RUN}/../err/err.metrics 2>&1

export PBS_SERVER=eslogin13
qsub ${TMP_DIR}/run_topo.sh


LastArq="False"
while [ "${LastArq}" != "True" ] ; do

    tam=`du -b ${PRP_DIR}/data_in/grid/topo.dat | awk '{print $1}'` 

      if [ -s ${PRP_DIR}/data_in/grid/topo.dat ] ; then

         if (( ${tam} > 7795600 )) ; then

            ${TMP_DIR}/run_init.sh ${LM}
            LastArq="True" 
         
	 else
  
            sleep 200
          
	 fi
   
            else
 
          sleep 200			
      fi
	
done

else

cd ${TMP_DIR}		
./run_init.sh ${LM}

fi


