#!/bin/bash
# ./chkexpout.sh model expid

#set -xuve

model=$1
expid=$2
src1=/cfunas/exp/$model
src2=/cfunas/exp/$model/gribfiles

sdates=$(ls -1 $src1/$expid | grep [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
for s in $sdates; do
 mems=$(ls -1 $src1/$expid/$s | grep fc)
 for m in $mems; do
  if [[ -d $src1/$expid/$s/$m ]]; then
   out=""
   if [[ -d $src1/$expid/$s/$m/outputs ]]; then 
    a=$(ls -1 $src1/$expid/$s/$m/outputs | grep MMA | wc -l)
    o=$(ls -1 $src1/$expid/$s/$m/outputs | grep MMO | wc -l)
    sz=$(du -chs $src1/$expid/$s/$m/outputs | grep total | awk '{print $1}')
    mm="MMA=$a MMO=$o MM_SZ=$sz "
   fi
   res=""
   if [[ -d $src1/$expid/$s/$m/restarts ]]; then
    r=$(ls -1 $src1/$expid/$s/$m/restarts | grep REST | wc -l)
    sz=$(du -chs $src1/$expid/$s/$m/restarts | grep total | awk '{print $1}')
    rst="RST=$r RST_SZ=$sz "
   fi
   grb=""
   if [[ -d $src2/$expid/$s/$m/outputs ]]; then 
    gg=$(ls -1 $src2/$expid/$s/$m/outputs | grep ICMGG | wc -l)
    sh=$(ls -1 $src2/$expid/$s/$m/outputs | grep ICMSH | wc -l)
    sz=$(du -chs $src2/$expid/$s/$m/outputs | grep total | awk '{print $1}')
    grb="(additional) GG=$gg SH=$sh GRB_SZ=$sz"
   fi
   l=$(ls -1 $src1/$expid/$s/$m | grep logfiles | wc -l)
   sz=$(du -chs $src1/$expid/$s/$m | grep total | awk '{print $1}')
   echo "$expid $s $m ${mm}${rst}LOGS=$l TOTAL_SIZE=$sz $grb"
  fi
 done
done
du -chs $src1/$expid | grep $expid
du -chs $src2/$expid | grep $expid

