#var="BH R Rx T2m Tm Tmtb Tx Txtb Um Vx"
#var="BH  NRai  P  R  Rx  SSH  T2m  Tm  Tmtb  Tx  Txtb  Um  Utb  Vtb  Vx"
#var="NRai P"
var="R"
dir="1_SL_BASE  2_SL_IMHEN  3_SL_Daily"
odir="4_Combine"

rm -rf stlist.tmp combine.log
for ivar in ${var};do
    rm -rf ${odir}/${ivar}
    mkdir ${odir}/${ivar}
    for idir in ${dir};do
#        nst=` ls ./${idir}/Converted/${ivar}/ |sed s/_P-//g | wc -l`
#        echo ${ivar} ${nst} > stlist_${idir%%_*}.tmp
        for ist in `ls ./${idir}/Converted/${ivar}/*|sed s:./${idir}/Converted/${ivar}/${ivar}_::g|sed s/.txt//g`;do
            echo ${ist} >> stlist_${idir%%_*}.tmp
        done
    done
    for ick in `cat stlist_1.tmp`;do
        sed -i /${ick}/d stlist_2.tmp
	sed -i /${ick}/d stlist_3.tmp
    done

    for ick in `cat stlist_2.tmp`;do
        sed -i /${ick}/d stlist_3.tmp
    done
        
    cat stlist_2.tmp >> stlist_1.tmp
    cat stlist_3.tmp >> stlist_1.tmp
    nst=` cat stlist_1.tmp | wc -l `
    echo ${ivar} ${nst} > stlist.tmp
    echo ${ivar} ${nst} >> combine.log
    cat stlist_1.tmp >> stlist.tmp
    ./a.out
    rm -rf aa stlist_1.tmp stlist_2.tmp stlist_3.tmp
done
