#!/bin/bash
#===============================================================================
#
#          FILE:  generate_initial_conditions.sh
# 
#         USAGE:  ./generate_initial_conditions.sh 
# 
#   DESCRIPTION:  
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  Kai Keller (kai.keller@bsc.es), 
#       COMPANY:  
#       VERSION:  0.9
#       CREATED:  23/08/03 18:22:32 EEST
#      REVISION:  ---
#===============================================================================

set -e

export PATH=/users/lrb_465000454_fdb/mars/versions/current/bin:$PATH;
export ECCODES_DEFINITION_PATH=/pfs/lustrep3/projappl/project_465000454/models/ifs-nemo/DE_CY48R1.0_climateDT_20231214/make/lumi-cray/source/eccodes/definitions;
export LD_LIBRARY_PATH=/users/lrb_465000454_fdb/mars/versions/current/lib64:$LD_LIBRARY_PATH;
export METKIT_RAW_PARAM=1

_inidate=-1
_expid=-1
_datadir=-1

_usage_() {
  echo "usage: $(basename $0) -t <step> -i <idate> -s <sdate> -e <expid> -X <expdir> -R <restartdir> -N <nemotool> [-h]"
  echo "  -i <idate>  : [required] 'idate'  date of the new ICs (format: YYYYMMDDHH)"
  echo "  -e <expid>  : [required] 'expid'  experiment id"
  echo "  -d <expdir> : [required] 'expdir' experiment directory (where the IFS output is stored)"
  echo "  -h          : prints this message"
}


while getopts "i:e:d:h" opt; do
  case ${opt} in
    i ) 
      _inidate="$OPTARG"
      ;;  
    e ) 
      _expid="$OPTARG"
      ;;  
    d ) 
      _datadir="$OPTARG"
      ;;  
    h ) 
      _usage_
      exit 0
      ;;  
    \? )
      echo "Invalid flag '${opt}'" 1>&2
      exit 1
      ;;  
    : ) 
      echo "Invalid option $OPTARG requires an argument" 1>&2
      exit 1
      ;;  
  esac
done
shift $((OPTIND -1))

err=0
errors=()

if [ $_expid == -1 ]; then
  errors+=('please provide an experiment id (-e <expid>)')
  err=$((err+1))
fi

if [ $_inidate == -1 ]; then
  errors+=('please provide a new date for the output IC (-i <yyyymmdd>)')
  err=$((err+1))
fi

if [ $_datadir == -1 ]; then
  errors+=('please provide the experiment directory (-e <path>)')
  err=$((err+1))
fi

if [ $err -gt 0 ]; then
  echo "ERRORS: $err"
  for i in `seq 0 $((err-1))`; do
    echo "[$i]: ${errors[$i]}"
  done
  echo "---"
  _usage_
  exit 1
fi

expid=$_expid

inidata_in=$_datadir

inidata_out=${INIDATA_OUTDIR:-$PWD/out}

mkdir -p $inidata_out

inidate=$_inidate

wam_sfcwind_in=${inidata_in}/sfcwindin
wam_cdwave_in=${inidata_in}/cdwavein
wam_uwave_in=${inidata_in}/uwavein
wam_specwave_in=${inidata_in}/specwavein

wam_sfcwind_out=${inidata_out}/sfcwindin.new
wam_cdwave_out=${inidata_out}/cdwavein.new
wam_uwave_out=${inidata_out}/uwavein.new
wam_specwave_out=${inidata_out}/specwavein.new

ifs_shinit_in=${inidata_in}/ICMSH${expid}INIT
ifs_gginit_in=${inidata_in}/ICMGG${expid}INIT
ifs_uainit_in=${inidata_in}/ICMGG${expid}INIUA

ifs_shinit_out=${inidata_out}/ICMSH${expid}INIT.new
ifs_gginit_out=${inidata_out}/ICMGG${expid}INIT.new
ifs_uainit_out=${inidata_out}/ICMGG${expid}INIUA.new


if ! [ -f "$wam_sfcwind_in" ]; then
  echo "(error) cannot find '$wam_sfcwind_in'"
  exit 1
fi
if ! [ -f "$wam_cdwave_in" ]; then
  echo "(error) cannot find '$wam_cdwave_in'"
  exit 1
fi
if ! [ -f "$wam_uwave_in" ]; then
  echo "(error) cannot find '$wam_uwave_in'"
  exit 1
fi
if ! [ -f "$wam_specwave_in" ]; then
  echo "(error) cannot find '$wam_specwave_in'"
  exit 1
fi
if ! [ -f "$ifs_shinit_in" ]; then
  echo "(error) cannot find '$ifs_shinit_in'"
  exit 1
fi
if ! [ -f "$ifs_gginit_in" ]; then
  echo "(error) cannot find '$ifs_gginit_in'"
  exit 1
fi
if ! [ -f "$ifs_uainit_in" ]; then
  echo "(error) cannot find '$ifs_uainit_in'"
  exit 1
fi
if ! [ -d "$inidata_out" ]; then
  echo "(error) cannot find outdir '$inidata_out'"
  exit 1
fi


tabs 20
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo -e "\tCONFIGURATION"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo -n "expid  ->" ; echo "$expid" | fold -s -w 120  | sed -e "s|^|\t|g" 
echo -n "inidate  ->" ; echo "$inidate" | fold -s -w 120  | sed -e "s|^|\t|g"
echo -n "datadir ->" ; echo "$inidata_in" | fold -s -w 120  | sed -e "s|^|\t|g"
echo -n "outdir ->" ; echo "$inidata_out" | fold -s -w 120  | sed -e "s|^|\t|g"
tabs 4
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -


echo -n "setting new date..."

grib_set -r -s dataDate=$inidate $wam_sfcwind_in $wam_sfcwind_out
grib_set -r -s dataDate=$inidate $wam_cdwave_in $wam_cdwave_out
grib_set -r -s dataDate=$inidate $wam_uwave_in $wam_uwave_out
grib_set -r -s dataDate=$inidate $wam_specwave_in $wam_specwave_out
                 
grib_set -r -s dataDate=$inidate $ifs_shinit_in $ifs_shinit_out
grib_set -r -s dataDate=$inidate $ifs_gginit_in $ifs_gginit_out
grib_set -r -s dataDate=$inidate $ifs_uainit_in $ifs_uainit_out

echo " [done]"

