#!/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}H00 + 1 day" +%s) - $(date -d "${ym}01H00" +"%s") )/(60*60*24) ))

p2D="/ec/res4/scratch/ar0s/ERA5/GRIB_N320/${ym}/surf2D_ERA5_L1_GLO_TLAUTO_IGNONE_GN320_ana01h_${date}.grib"
p3D="/ec/res4/scratch/ar0s/ERA5/GRIB_N320/${ym}/inst3D_ERA5_L107-137_GLO_TLAUTO_IGNONE_GN320_ana01h_${date}.grib"
dout="/ec/res4/scratch/ar0s/ERA5/NC_F320/${ym}" ; mkdir -p ${dout}

f2D=$(basename ${p2D/GN320/GF320reg}) ; f2D=${f2D%.grib}.nc
f3D=$(basename ${p3D/GN320/GF320reg}) ; f3D=${f3D%.grib}.nc
d2D=$(dirname $p2D) ; d3D=$(dirname $p3D)

if [ ! -s ${dout}/${f2D} ]; 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} ]; then echo "wait..."; w="--wait"; fi
  sbatch ${w} --job-name=cdogrid2D_${date} /home/ar0s/PROD_ERA5_ABL/job_cdogrid.sbatch ${p2D} ${dout}/${f2D}
  if [ -n "${w}" ]; then
    file_nb=$( eval "ls ${dout}/${f2D/${date}/${ym}??} 2>/dev/null" | wc -l )
    if [ ${day_nb} -eq ${file_nb} ]; then echo "OK"; else echo "ERROR"; exit 1; fi
  fi
fi

if [ ! -s ${dout}/${f3D} ]; 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} ]; then echo "wait..."; w="--wait"; fi
  sbatch ${w} --job-name=cdogrid3D_${date} /home/ar0s/PROD_ERA5_ABL/job_cdogrid.sbatch ${p3D} ${dout}/${f3D}
  if [ -n "${w}" ]; then
    file_nb=$( eval "ls ${dout}/${f3D/${date}/${ym}??} 2>/dev/null" | wc -l )
    if [ ${day_nb} -eq ${file_nb} ]; then echo "OK"; exit 0; else echo "ERROR"; exit 1; fi
  fi
fi
