#!/bin/bash

set +xv

date=$1
day_end=$( date -d "${date:4:2}/1 + 1 month - 1 day" +"%d" )
date_end=${date:0:6}${day_end}
ym=${date:0:6}
day_nb=$(( ($(date -d "${date_end}" +%s) - $(date -d "${ym}01" +"%s") )/(60*60*24) + 1 ))

var_list=( uwnd vwnd uhpg vhpg tpot humi )
d="/ec/res4/scratch/ar0s/ERA5/NC_F320/${ym}"
fabl="abl_ERA5_L50Z10_GLO_TLAUTO_IGNONE_GF320reg_ana01h_${date}.nc"

for v in ${var_list[@]}; do

  finp=${v}_${fabl}
  fout=${finp%.nc}_misstodis.nc

  if [ ! -s ${d}/${fout} ]; then
    while [ $(squeue -u $USER -h -t pending,running -r | wc -l) -eq 100 ]; do echo "sleeping..."; sleep 10; done 
    if [[ ${date} -eq ${date_end} && ${v} == "humi" ]]; then echo "wait..."; w="--wait"; fi
    sbatch ${w} --job-name=cdomiss_${date} /home/ar0s/PROD_ERA5_ABL/job_cdomiss.sbatch ${d}/${f}
    if [ -n "${w}" ]; then
      file_nb=$( eval "ls ${d}/{$(tr ' ' ',' <<< ${var_list[*]})}_${fabl/${date}/${ym}??} 2>/dev/null" | wc -l )
      if [ $((day_nb*var_nb)) -eq ${file_nb} ]; then echo "OK"; exit 0; else echo "ERROR"; exit 1; fi
    fi
  fi

done
