#!/bin/bash

set +xv


# ARGUMENTS
date_beg=$1
date_end=$2
nbday=$(( ($(date -d "${date_end}H00 + 1 day" +%s) - $(date -d "${date_beg}H00" +"%s") )/(60*60*24) ))

for d in $(seq -f "%02g" 0 $((nbday-1))); do

  # TIME
  date=$(date -d "${date_beg}H00 + ${d} days" +"%C%y%m%d")
  ym=${date:0:6}

  # DIRECTORIES & FILES
  dout="${SCRATCH_DIR}/GRIB_${GRID}/${ym}"
  dreg="${SCRATCH_DIR}/NC_${GRID/N/F}/${ym}"
  f3D="inst3D_${VLEVEL}_${BASE_NAME/_GF/_GN}_${date}.grib"
  f2D="surf2D_L1_${BASE_NAME/_GF/_GN}_${date}.grib"
  n3D="inst3D_${VLEVEL}_${BASE_NAME}_${date}.nc"
  n2D="surf2D_L1_${BASE_NAME}_${date}.nc"

  # SUBMIT JOB
  if [ ! -s ${dout}/${f3D} ] && [ ! -s ${dreg}/${n3D} ]; then
    while [ $(squeue -u $USER -h -t pending,running -r | wc -l) -ge 90 ]; do echo "sleeping..."; sleep 10; done
    sbatch --job-name=mars_inst3D_${date} --export=SCRATCH_DIR=${SCRATCH_DIR},GRID=${GRID} ${DIR_SRC}/job_mars.sbatch ${DIR_SRC}/MARS/mars_inst3D_ana.req.base ${date}
  fi

  if [ ! -s ${dout}/${f2D} ] && [ ! -s ${dreg}/${n2D} ]; then
    while [ $(squeue -u $USER -h -t pending,running -r | wc -l) -ge 90 ]; do echo "sleeping..."; sleep 10; done
    sbatch --job-name=mars_surf2D_${date} --export=SCRATCH_DIR=${SCRATCH_DIR},GRID=${GRID} ${DIR_SRC}/job_mars.sbatch ${DIR_SRC}/MARS/mars_surf2D_ana.req.base ${date}
  fi

  # WAIT FOR THE LAST DATE TO FINISH
  while [[ ${date} -eq ${date_end} && $(squeue -u $USER -h -t pending,running -r -o '%20j' | grep mars_.*_${ym} | wc -l) -gt 0 ]]; do echo "sleeping..."; sleep 10; done

  # TEST ALL FILES ARE PRODUCED
  if [ ${date} -eq ${date_end} ]; then
    
    fnb=$( eval "ls ${dout}/${f3D/${date}.grib/${ym}{${date_beg:6:2}..${date_end:6:2}}}.grib 2>/dev/null" | wc -l )
    nnb=$( eval "ls ${dreg}/${n3D/${date}.nc/${ym}{${date_beg:6:2}..${date_end:6:2}}}.nc 2>/dev/null" | wc -l )
    if [[ ${nbday} -eq ${fnb} || ${nbday} -eq ${nnb} ]]; then echo "-MARS_INST3D: OK"; else echo "-MARS_INST3D: ERROR"; exit 1; fi

    fnb=$( eval "ls ${dout}/${f2D/${date}.grib/${ym}{${date_beg:6:2}..${date_end:6:2}}}.grib 2>/dev/null" | wc -l )
    nnb=$( eval "ls ${dreg}/${n2D/${date}.nc/${ym}{${date_beg:6:2}..${date_end:6:2}}}.nc 2>/dev/null" | wc -l )
    if [[ ${nbday} -eq ${fnb} || ${nbday} -eq ${nnb} ]]; then echo "-MARS_SURF2D: OK"; else echo "-MARS_SURF2D: ERROR"; exit 1; fi

  fi

done

exit 0
