#!/bin/bash

echo -e "number of files: \n"
echo -e "exp\t\t\t6h.special\tday.toasurf\tmon.allevs\tmon.meridional\tmon.special\tmon.stdlevs\tmon.toasurf\traw"

# for set in HIPPO_set_1850_{1,2a,3,4}; do 
# for set in HIPPO_set_1420_2_prelim HIPPO_set_1780_2; do
# for set in HIPPO_set_1780_1; do
# for set in HIPPO_set_1400_1; do
# for set in HIPPO_set_1780_3; do
for set in HIPPO_set_1870_1; do

   case ${set} in
      HIPPO_set_1400_1) fyear=1400; lyear=1850 ;;
      HIPPO_set_1400_2) fyear=1400; lyear=1780 ;;
      HIPPO_set_1420_*) fyear=1420; lyear=1780 ;;
      HIPPO_set_1780_*) fyear=1780; lyear=1850 ;;
      HIPPO_set_1850_[1-5]) fyear=1850; lyear=2009 ;;
      HIPPO_set_1850_6) fyear=1850; lyear=1879 ;;
      HIPPO_set_1870_1) fyear=1870; lyear=1919 ;;
   esac

   nfiles=$(( (${lyear} - ${fyear} + 1) * 24  ))
   nfiles=$(( (${lyear} - ${fyear} + 1) * 12  ))
   echo "nfiles: ${nfiles}"

#   for dir in ${set}/HIPPO_r*i*p*f*; do
   for dir in ${set}/HIPPO_r*i*p*f1251*; do

      echo -e "$(basename ${dir}): $(eval ls ${dir}/means/{rerun_,}$(basename ${dir})_{${fyear}..${lyear}}_6h.special.grb 2>/dev/null | wc -w) \
	      $(eval ls ${dir}/means/{rerun_,}$(basename ${dir})_{${fyear}..${lyear}}_day.toasurf.grb 2>/dev/null | wc -w) \
	      $(eval ls ${dir}/means/{rerun_,}$(basename ${dir})_{${fyear}..${lyear}}_mon.alllevs.nc 2>/dev/null | wc -w) \
	      $(eval ls ${dir}/means/{rerun_,}$(basename ${dir})_{${fyear}..${lyear}}_mon.meridional.nc 2>/dev/null | wc -w) \
	      $(eval ls ${dir}/means/{rerun_,}$(basename ${dir})_{${fyear}..${lyear}}_mon.special.nc 2>/dev/null | wc -w) \
	      $(eval ls ${dir}/means/{rerun_,}$(basename ${dir})_{${fyear}..${lyear}}_mon.stdlevs.nc 2>/dev/null | wc -w) \
	      $(eval ls ${dir}/means/{rerun_,}$(basename ${dir})_{${fyear}..${lyear}}_mon.toasurf.nc 2>/dev/null | wc -w) \
         $(eval ls ${dir}/rawdata/{rerun_,}$(basename ${dir})_{${fyear}..${lyear}}??.01_echam 2>/dev/null | wc -w)"
	      # $(eval ls ${dir}/rawdata/$(basename ${dir})_{${fyear}..${lyear}}??.01_{echam,accw} 2>/dev/null | wc -w)" 

      # if [ $(eval ls ${dir}/rawdata/$(basename ${dir})_{${fyear}..${lyear}}??.01_{echam,accw} 2>/dev/null | wc -w) -ne ${nfiles} ]; then
        if [ $(eval ls ${dir}/rawdata/{rerun_,}$(basename ${dir})_{${fyear}..${lyear}}??.01_echam 2>/dev/null | wc -w) -ne ${nfiles} ] || [ $(eval ls ${dir}/rawdata/rerun_$(basename ${dir})_{${fyear}..${lyear}}??.01_echam 2>/dev/null | wc -w) -ne 0 ] ; then

   	missing=""
        rerun=""
        double=""

      for year in $(seq ${fyear} ${lyear}); do
         for mon in $(seq -w 1 12); do
               
            if [ ! -e ${dir}/rawdata/$(basename ${dir})_${year}${mon}.01_echam ] && [ ! -e ${dir}/rawdata/rerun_$(basename ${dir})_${year}${mon}.01_echam ]; then
               missing="${missing} ${year}-${mon}"
            fi

            if [ ! -e ${dir}/rawdata/$(basename ${dir})_${year}${mon}.01_echam ] && [ -e ${dir}/rawdata/rerun_$(basename ${dir})_${year}${mon}.01_echam ]; then
               rerun="${rerun} ${year}-${mon}"
            fi

            if [ -e ${dir}/rawdata/$(basename ${dir})_${year}${mon}.01_echam ] && [ -e ${dir}/rawdata/rerun_$(basename ${dir})_${year}${mon}.01_echam ]; then
               double="${double} ${year}-${mon}"
            fi

          done
       done

       echo "missing raw data :${missing}"
       echo "rerun only :${rerun}"
       echo "original + rerun :${double}"
    fi

   done
done

exit 0
