#!/bin/bash
#======================================================
#             Run WPS 
#======================================================

echo
echo -e "\033[1;47m o  Running WPS in ${dirowps}\033[0m"
echo

# Perform calculation even if files are present? ( 0 - Do nothing 1 - Force calculation ; 2 - Calculate only if target files are absent )
export do_recalc=1

# Clean intermediate files ?
export do_clean_wrf=1

#---------------------------

#echo "Now in ${dirowps}"
#cd ${dirowps}

#echo "Using raw WPS data in: "${dir_wps}

#ln -sf ${dir_wps}/*exe . || { echo "Failed linking ${dir_wps}/*.exe from ${dirowps}. Bye."; exit 1; }

#mkdir -p geogrid metgrid
#cp ${dir_wps}/geogrid/GEOGRID.TBL geogrid
#cp ${dir_wps}/metgrid/METGRID.TBL metgrid

#---------------------------

if [[ $do_recalc -eq 1 ]] ; then
   echo 'Do_recalc is set to 1 in wrf-run-wps.sh : Doing all WRF-WPS calculations'
elif [[ $do_recalc -eq 2 ]] ; then
   echo 'Do_recalc is set to 2 in wrf-run-wps.sh : Not recalculating already existing WRF-WPS outputs'
elif [[ $do_recalc -eq 0 ]] ; then
   echo 'Do_recalc is set to 0 in wrf-run-wps.sh : Not running WPS'
   exit
else 
   echo 'do_recalc set to ' $do_recalc 'in wrf-run-wps.sh. Acceptable values are 1 or 2'
   exit 1
fi 


#---------------------------
# ungrib and metgrid
echo -e "\033[1;43m    ungrib and metgrid \033[0m"

if [[ $do_recalc -ge 1 ]] ; then

   # Generate the list of GRIB files to link
   dh=${h1}
   cdh=${y1}${m1}${da1}${h1}
   flist=""
   while [ ${cdh} -lt ${y2}${m2}${da2}${h2} ]; do
      cdh=`date -u -d "${y1}${m1}${da1} ${dh} hours" +%Y%m%d%H`
      cdhtiret=`date -u -d "${y1}${m1}${da1} ${dh} hours" +%Y-%m-%d-%H`
      cd=`date -u -d "${y1}${m1}${da1} ${dh} hours" +%Y%m%d`
      
      flist=${flist}" "${dgrb}/${cd}/PRES.${cdh}_0000
      (( dh +=6 ))
   done

   for lnk in ${flist}; do
      if [ -f ${lnk} ]; then
         echo "${lnk} exists"
      else
         echo "${lnk} not found. bye!"
         exit 1
      fi
   done

   ${dir_wps}/link_grib.csh ${flist} || { echo "Error link_grib.csh"; exit 1 ; }
   [ $? -ne 0 ] && { echo "Error running ${dir_wps}/link_grib.csh ${flist}"; exit 1 ; }


# Number of GRIB soil levels
   if [ $cdh -le 2015011406 ] ; then
      ln -sf ${dir_wps}/ungrib/Variable_Tables/Vtable.GFS.pre20150114 Vtable || exit 1
   else
      ln -sf ${dir_wps}/ungrib/Variable_Tables/Vtable.GFS Vtable || exit 1
   fi

   FILEname="FILE:${cdhtiret}"
   if ( [[ $do_recalc -gt 1 ]] \
      && [[ ! -f $FILEname ]] ) || [[ $do_recalc -eq 1 ]] ; then
      ./ungrib.exe >& ungrib.output || { echo "Error running ./ungrib.exe"; exit 1 ; }
   else
      echo "$FILEname exists -> not running ungrib.exe."
      echo "   Use do_recalc option in scripts/wrf-run-wps.sh to force run"
   fi

   # metgrid

   # For Thompson AA microphysics
   metemname="met_em.${metdom}.{cdhtiret}}:00:00.nc"
   ln -sf ${dir_wps}/QNWFA_QNIFA_Monthly_GFS .
   if ( [[ $do_recalc -gt 1 ]] \
          && [[ ! -f $metemname ]] ) || [[ $do_recalc -eq 1 ]] ; then

   echo -e "\033[1;38;31m  Running metgrid.exe... \033[0m"
   time ./metgrid.exe || { echo "Error running ./metgrid.exe" ; exit 1 ; }

   if [ ${do_clean_wrf} -ge 1 ]; then
          rm -f FILE:* PFILE:* GRIBFILE.*
   fi

else
       echo "   Not running ungrib & metgrid."
       echo "   Use do_recalc option to force recalculation in scripts/wrf-run-wps.sh"
fi


fi   

