#!/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
  p2D="${SCRATCH_DIR}/GRIB_${GRID}/${ym}/surf2D_L1_${BASE_NAME/_GF/_GN}_${date}.grib"
  p3D="${SCRATCH_DIR}/GRIB_${GRID}/${ym}/inst3D_${VLEVEL}_${BASE_NAME/_GF/_GN}_${date}.grib"
  dout="${SCRATCH_DIR}/NC_${GRID/N/F}/${ym}" ; mkdir -p ${dout}
  f2D="surf2D_L1_${BASE_NAME}_${date}.nc"
  f3D="inst3D_${VLEVEL}_${BASE_NAME}_${date}.nc"
  d2D=$(dirname $p2D) ; d3D=$(dirname $p3D)

  if [ ! -s ${dout}/${f2D} ]; then
  
    mkdir -pv ${dout}
  
    if [ -s ${p2D} ]; then
      while [ $(squeue -u $USER -h -t pending,running -r | wc -l) -ge 90 ]; do echo "sleeping..."; sleep 10; done
      sbatch --job-name=cdogrid2D_${date} --export=SCRATCH_DIR=${SCRATCH_DIR},GRID=${GRID} ${DIR_SRC}/job_cdogrid.sbatch ${p2D} ${dout}/${f2D}
    else
      echo "-CDO_GRID2D: ERROR: input files not found"; exit 1
    fi
  
  fi
  
  if [ ! -s ${dout}/${f3D} ]; then
  
    mkdir -pv ${dout}
 
    if [ -s ${p3D} ]; then
      while [ $(squeue -u $USER -h -t pending,running -r | wc -l) -ge 90 ]; do echo "sleeping..."; sleep 10; done
      sbatch --job-name=cdogrid3D_${date} --export=SCRATCH_DIR=${SCRATCH_DIR},GRID=${GRID} ${DIR_SRC}/job_cdogrid.sbatch ${p3D} ${dout}/${f3D}
    else
      echo "-CDO_GRID3D: ERROR: input files not found"; exit 1
    fi
  
  fi
  
  while [[ ${date} -eq ${date_end} && $(squeue -u $USER -h -t pending,running -r -o '%20j' | grep cdogrid.D_${ym} | wc -l) -gt 0 ]]; do echo "sleeping..."; sleep 10; done
  
  if [ ${date} -eq ${date_end} ]; then
    file_nb=$( eval "ls ${dout}/${f2D/${date}.nc/${ym}{${date_beg:6:2}..${date_end:6:2}}}.nc 2>/dev/null" | wc -l )
    if [ ${nbday} -eq ${file_nb} ]; then
      echo "-CDO_GRID2D: OK"
    else
      echo "-CDO_GRID2D: ERROR"; exit 1
    fi
    file_nb=$( eval "ls ${dout}/${f3D/${date}.nc/${ym}{${date_beg:6:2}..${date_end:6:2}}}.nc 2>/dev/null" | wc -l )
    if [ ${nbday} -eq ${file_nb} ]; then
      echo "-CDO_GRID3D: OK"
    else
      echo "-CDO_GRID3D: ERROR"; exit 1
    fi
  fi

done

exit 0
