#!/bin/bash -l

# This script is executed on the machine at which autosubmit is executed.
# and will be used if the initial conditions are in this same system.

# Because it can happen that the initial conditions as well and the execution happens in the local
# system we need to define these two variables:
LBC_DATA_IS_LOCAL=%SIMULATION.BOUNDARY_CONDITIONS.LOCAL%

if [ "x%HPCARCH%" == "xlocal" ]; then
  RUN_MACHINE_IS_LOCAL="True"
else
  RUN_MACHINE_IS_LOCAL="False"
fi

if [ "${LBC_DATA_IS_LOCAL}" != "True" ]; then
  # Get some variables provided by autosubmit.
  WORKDIR=%HPCROOTDIR%
  STARTDATE=%SDATE%
  HPCUSER=%HPCUSER%
  HPCHOST=%HPCHOST%
  MEMBER=%MEMBER%

  . ${WORKDIR}/proj/platforms/common/spack_utils.sh
  load_spack "%spack.init%" "%spack.root%" "%spack.url%" "%spack.branch%" "%spack.externals%" "%spack.compiler%" "%spack.disable_local_config%" "%spack.user_cache_path%" "%spack.user_config_path%" "%spack.upstreams%"

  # Load icon module
  spack load --first dwd-icon-tools

  # Set environment variable for eccodes-dwd definitions:
  source ${WORKDIR}/eccodes_defs.env


  # Define date directory, create it and go there
  COMMON_DATE_FOLDER=${WORKDIR}/${STARTDATE}/inidata
  COMMON_GRID_FOLDER=${WORKDIR}/inidata
  MEMBER_DIR=${WORKDIR}/${STARTDATE}/${MEMBER^}

  # Leave out the character part
  BOUNDARY_CONDITIONS_PARENT_FOLDER=%SIMULATION.BOUNDARY_CONDITIONS.PARENT_FOLDER%
  BOUNDARY_CONDITIONS_PATH=${BOUNDARY_CONDITIONS_PARENT_FOLDER}/${STARTDATE:0:8}00


  # Fetch tar.gz file of ICON-EU-EPS outputs necessary for the lateral boundary conditions
  FC_TAR_SOURCE=$(find ${BOUNDARY_CONDITIONS_PATH} -name "iefff.${MEMBER,}.tar.gz" | sort | tail -n 1)

  FC_TAR_FILE=$(basename "${FC_TAR_SOURCE}")


  if [ ! -f "${FC_TAR_SOURCE}" ]; then
    echo "ICON-EU FC file for date ${STARTDATE} not found!"
    exit 1
  fi

  # make a working directory for converting boundary conditions
  mkdir -p work/${MEMBER} || exit

  tar -zxvf ${FC_TAR_SOURCE} -C work

  DYNAMICS_GRID_FILENAME=%simulation.dynamics_grid_filename%
  BOUNDARY_GRID_FILE=%simulation.lateral_boundary_grid_filename%
  PARENT_GRID_FILE=%simulation.parent_grid_filename%

  # necessary for running fortran 
  ulimit -s unlimited
  OMP_NUM_THREADS=1

  # Loop through hours
  for ((tt=3; tt<=$((3 + %EXPERIMENT.CHUNKSIZE%)); tt++)) ; do
    th=$((tt % 24))
    td=$((tt / 24))
    EU_FC_TIME=$(printf "%02d" $td)$(printf "%02d" $th)

    tt2=$((tt-3))
    th=$((tt2 % 24))
    td=$((tt2 / 24))
    D2_FC_TIME=$(printf "%03d" $td)$(printf "%02d" $th)

    # Convert ICON-EU-EPS to latbc
    ${WORKDIR}/%python_environment.folder_name%/bin/python3 ${WORKDIR}/proj/templates/real-from-d2-ana/icon-remap-helper.py \
        --src-grid ${COMMON_GRID_FOLDER}/${PARENT_GRID_FILE} \
        --dst-grid ${COMMON_GRID_FOLDER}/${BOUNDARY_GRID_FILE} \
        --source   work/iefff${EU_FC_TIME}0000.${MEMBER,} \
        --dest work/${MEMBER} \
        --output-format grb

    # before 2022 wind field is only 3-hourly
    if [ ${STARTDATE} -lt 2022010100 ]; then
      if [ $((tt2 % 3)) -ne 0 ]; then
        grib_copy work/${MEMBER}/iefff${EU_FC_TIME}0000.grb work/${MEMBER}/iefff${EU_FC_TIME}0000.grb_t -w shortName!=wz
        mv work/${MEMBER}/iefff${EU_FC_TIME}0000.grb_t work/${MEMBER}/iefff${EU_FC_TIME}0000.grb
        grib_set -s stepRange=$((tt2 * 60)),dataTime=0,dataDate=${STARTDATE:0:8} ${COMMON_DATE_FOLDER}/WZ.grib  work/${MEMBER}/WZ.grib
        cat work/${MEMBER}/WZ.grib >> work/${MEMBER}/iefff${EU_FC_TIME}0000.grb
    fi
    fi

    # rename to the comfortable style for ICON
    mv work/${MEMBER}/iefff${EU_FC_TIME}0000.grb ${MEMBER_DIR}/latbc_${D2_FC_TIME}.${MEMBER^}.grib

  done

  # add HHL
  #grib_set -s stepRange=0,dataTime=2100,dataDate=${STARTDATE:0:8} ${COMMON_GRID_FOLDER}/HHL.grib 
  cat ${COMMON_DATE_FOLDER}/HHL.grib >> ${MEMBER_DIR}/latbc_00000.${MEMBER^}.grib

  # clean up the working directory
  rm -r work/${MEMBER}
  rm -r work/iefff*.${MEMBER,}


  # Fetch tar.gz file of ICON-EU-EPS outputs necessary for the lateral boundary conditions
  LBC_SOURCE=$(find ${MEMBER_DIR} -name "latbc_*00.${MEMBER^}.grib" | sort -n )

  if [ -z "${LBC_SOURCE}" ]; then
    echo "Failed to make boundary conditions file for date ${STARTDATE}!"
    exit 1
  fi

  # Check if we copy the initial conditions from the local system or the remote one
  # Create member folder and go there
  mkdir -p ${COMMON_DATE_FOLDER}
  cd ${COMMON_DATE_FOLDER} || exit

  # Save filenames to be used later by other scripts.
  echo "${LBC_SOURCE}" > ${MEMBER_DIR}/lbc_file.txt

  # Copy the first-guess and analysis files.
  #cp "${LBC_SOURCE}" "${LBC_FILE}"

  # Change permissions to read only.
  #chmod 440 ${MEMBER_DIR}/latbc_*.grib


fi