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

#set -xuve

model=$1
expid=$2
src1=/cfunas/exp/$model
src2=/cfunas/exp/$model/restartfiles
src3=/cfunas/exp/$model/gribfiles
src4=/cfunas/exp/$model/cmorfiles

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
   l=$(ls -1 $src1/$expid/$s/$m | grep logfiles | wc -l)
   mm=""
   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
   rest=""
   # old option
   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}')
    rest="REST=$r REST_SZ=$sz"
   fi
   # old option
   if [[ -d $src2/$expid/$s/$m/restarts ]]; then
    r=$(ls -1 $src2/$expid/$s/$m/restarts | grep REST | wc -l)
    sz=$(du -chs $src2/$expid/$s/$m/restarts | grep total | awk '{print $1}')
    rest="REST=$r REST_SZ=$sz"
   fi
   grb=""
   if [[ -d $src3/$expid/$s/$m/outputs ]]; then 
    gg=$(ls -1 $src3/$expid/$s/$m/outputs | grep ICMGG | wc -l)
    sh=$(ls -1 $src3/$expid/$s/$m/outputs | grep ICMSH | wc -l)
    sz=$(du -chs $src3/$expid/$s/$m/outputs | grep total | awk '{print $1}')
    grb="GG=$gg SH=$sh GRB_SZ=$sz"
   fi
   cmor=""
   if [[ -d $src4/$expid/$s/$m/outputs ]]; then
    ca=$(ls -1 $src4/$expid/$s/$m/outputs | grep CMORA | wc -l)
    co=$(ls -1 $src4/$expid/$s/$m/outputs | grep CMORO | wc -l)
    sz=$(du -chs $src4/$expid/$s/$m/outputs | grep total | awk '{print $1}')
    cmor="CMORA=$ca CMORO=$co CMOR_SZ=$sz"
   fi
   echo "$expid $s $m LOGS=$l ${mm} ${rest} $grb $cmor"
  fi
 done
done
du -chs $src1/$expid | grep $expid
du -chs $src2/$expid | grep $expid
du -chs $src3/$expid | grep $expid
du -chs $src4/$expid | grep $expid

