#!/bin/bash 
#-----------------------------------------------------------------------------#
# !SCRIPT: pre_processing
#
# !DESCRIPTION:
#     Script to prepare boundary and initials conditions for MONAN model.
#     
#     Performs the following tasks:
# 
#        o Creates topography, land use and static variables
#        o Ungrib GFS data
#        o Interpolates to model the grid
#        o Creates initial and boundary conditions
#        o Creates scripts to run the model and post-processing (CR: to be modified to phase 3 and 4)
#        o Integrates the MONAN model ((CR: to be modified to phase 3)
#        o Post-processing (netcdf for grib2, latlon regrid, crop) (CR: to be modified to phase 4)
#
#-----------------------------------------------------------------------------#

if [ $# -ne 4 -a $# -ne 1 ]
then
   echo ""
   echo "Instructions: execute the command below"
   echo ""
   echo "${0} EXP_NAME/OP RESOLUTION LABELI FCST"
   echo ""
   echo "EXP_NAME    :: Forcing: GFS"
   echo "OP          :: clean: remove all temporary files createed in the last run."
   echo "            :: Others options to be added later..."
   echo "RESOLUTION  :: number of points in resolution model grid, e.g: 1024002  (24 km)"
   echo "                                                                 40962  (120 km)"
   echo "LABELI      :: Initial date YYYYMMDDHH, e.g.: 2024010100"
   echo "FCST        :: Forecast hours, e.g.: 24 or 36, etc."
   echo ""
   echo "24 hour forecast example for 24km:"
   echo "${0} GFS 1024002 2024010100 24"
   echo "48 hour forecast example for 120km:"
   echo "${0} GFS   40962 2024010100 48"
   echo "Cleannig temp files example:"
   echo "${0} clean"
   echo ""

   exit
fi

# Set environment variables exports:
echo ""
echo -e "\033[1;32m==>\033[0m Moduling environment for MONAN model...\n"
. setenv.bash

if [ $# -eq 1 ]
then
   op=$(echo "${1}" | tr '[A-Z]' '[a-z]')
   if [ ${op} = "clean" ]
   then
      clean_pre_tmp_files
      exit
   else
      echo "Should type just \"clean\" for cleanning."
      echo "${0} clean"
      echo ""
      exit
   fi   
fi


# Standart directories variables:---------------------------------------
DIRHOMES=${DIR_SCRIPTS}/scripts_CD-CT; mkdir -p ${DIRHOMES}  
DIRHOMED=${DIR_DADOS}/scripts_CD-CT;   mkdir -p ${DIRHOMED}  
SCRIPTS=${DIRHOMES}/scripts;           mkdir -p ${SCRIPTS}
DATAIN=${DIRHOMED}/datain;             mkdir -p ${DATAIN}
DATAOUT=${DIRHOMED}/dataout;           mkdir -p ${DATAOUT}
SOURCES=${DIRHOMES}/sources;           mkdir -p ${SOURCES}
EXECS=${DIRHOMED}/execs;               mkdir -p ${EXECS}
#----------------------------------------------------------------------


# Input variables:--------------------------------------
EXP=${1};         #EXP=GFS
RES=${2};         #RES=1024002
YYYYMMDDHHi=${3}; #YYYYMMDDHHi=2024012000
FCST=${4};        #FCST=24
#-------------------------------------------------------


# Local variables--------------------------------------
# Calculating CIs and final forecast dates in model namelist format:
yyyymmddi=${YYYYMMDDHHi:0:8}
hhi=${YYYYMMDDHHi:8:2}
yyyymmddhhf=$(date +"%Y%m%d%H" -d "${yyyymmddi} ${hhi}:00 ${FCST} hours" )
final_date=${yyyymmddhhf:0:4}-${yyyymmddhhf:4:2}-${yyyymmddhhf:6:2}_${yyyymmddhhf:8:2}.00.00
#-------------------------------------------------------


echo -e  "${GREEN}==>${NC} Scripts_CD-CT last commit: \n"
git log -1 --name-only
git branch | head -1


# Untar the fixed files:
# x1.${RES}.graph.info.part.<Ncores> files can be found in datain/fixed
# *.TBL files also can be found in datain/fixed
# x1.${RES}.grid.nc also can be found in datain/fixed

echo -e  "${GREEN}==>${NC} copying and linking fixed input data... \n"
mkdir -p ${DATAIN}
if [ "$SERVER" = "egeon" ]; then
    ln -sf ${DIRDADOS}/WPS_GEOG ${DATAIN}
    rsync -rv --chmod=ugo=rw ${DIRDADOS}/fixed ${DATAIN}
else
    if [ ! -e "${DATAIN}/WPS_GEOG" ]; then
	if [ ! -f "WPS_GEOG.tar.gz" ]; then
	    if wget "https://dataserver.cptec.inpe.br/dataserver_dimnt/monan/MONAN-Model/monan_datain/WPS_GEOG.tar.gz"; then
	        echo "wget OK!"
            else
                echo "wget error!"
		echo "Link: https://dataserver.cptec.inpe.br/dataserver_dimnt/monan/MONAN-Model/monan_datain/WPS_GEOG.tar.gz"
                exit 1
            fi
	fi
	mkdir -p "${DATAIN}/WPS_GEOG"
        tar -xzvf WPS_GEOG.tar.gz -C "${DATAIN}/"
	rm WPS_GEOG.tar.gz
    fi
    if [ ! -e "${DATAIN}/fixed" ]; then
	if [ ! -f "fixed.tar.gz" ]; then
	    if wget "https://dataserver.cptec.inpe.br/dataserver_dimnt/monan/MONAN-Model/monan_datain/fixed.tar.gz"; then
	        echo "wget OK!"
            else
                echo "wget error"
		echo "Link: https://dataserver.cptec.inpe.br/dataserver_dimnt/monan/MONAN-Model/monan_datain/fixed.tar.gz"
                exit 1
            fi
	fi
	mkdir -p "${DATAIN}/fixed"
        tar -xzvf fixed.tar.gz -C "${DATAIN}/"
        rm fixed.tar.gz
    fi
fi


find /tmp -maxdepth 1 -type d -name 'rootfs*' -uid $(id -u) -exec rm -rf {} +



# Creating the x1.${RES}.static.nc file once, if does not exist yet:---------------
if [ ! -s ${DATAIN}/fixed/x1.${RES}.static.nc ]
then
   echo -e "${GREEN}==>${NC} Creating static.bash for submiting init_atmosphere to create x1.${RES}.static.nc...\n"
   time ./make_static.bash ${EXP} ${RES} ${YYYYMMDDHHi} ${FCST}
else
   echo -e "${GREEN}==>${NC} File x1.${RES}.static.nc already exist in ${DATAIN}/fixed.\n"
fi
#----------------------------------------------------------------------------------



# Degrib phase:---------------------------------------------------------------------
echo -e  "${GREEN}==>${NC} Submiting Degrib...\n"
time ./make_degrib.bash ${EXP} ${RES} ${YYYYMMDDHHi} ${FCST}
#----------------------------------------------------------------------------------



# Init Atmosphere phase:------------------------------------------------------------
echo -e  "${GREEN}==>${NC} Submiting Init Atmosphere...\n"
time ./make_initatmos.bash ${EXP} ${RES} ${YYYYMMDDHHi} ${FCST}
#----------------------------------------------------------------------------------




