#!/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) ))
var_list=( uwnd vwnd uhpg vhpg tpot humi )

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
  d="${SCRATCH_DIR}/NC_F320/${ym}"
  f="abl_ERA5_L50Z10_GLO_TLAUTO_IGNONE_GF320reg_ana01h_${date}.nc"

  for v in ${var_list[@]}; do
  
    finp=${v}_${f}
    fout=${finp%.nc}_misstodis.nc
  
    if [ ! -s ${d}/${fout} ]; then
      if [ -s ${d}/${finp} ]; then
        while [ $(squeue -u $USER -h -t pending,running -r | wc -l) -eq 90 ]; do echo "sleeping..."; sleep 10; done
        sbatch --job-name=cdomiss_${date} --export=SCRATCH_DIR=${SCRATCH_DIR} ${DIR_SRC}/job_cdomiss.sbatch ${date} ${v}
      else
        echo "CDO_MISS ERROR: input files not found"; exit 1
      fi
    fi
  
  done
  
  while [[ ${date} -eq ${date_end} && $(squeue -u $USER -h -t pending,running -r -o '%20j' | grep cdomiss_${ym} | wc -l) -gt 0 ]]; do echo "sleeping..."; sleep 10; done
  
  if [ ${date} -eq ${date_end} ]; then
    file_nb=$( eval "ls ${d}/{$(tr ' ' ',' <<< ${var_list[*]})}_${f/${date}.nc/${ym}{${date_beg:6:2}..${date_end:6:2}}}_misstodis.nc 2>/dev/null" | wc -l )
    if [ $((nbday*${#var_list[@]})) -eq ${file_nb} ]; then
      echo "CDO_MISS OK"
    else
      echo "CDO_MISS ERROR"
      exit 1
    fi
  fi

done

exit 0
