#!/bin/bash

if [[ $# -ne 2 ]]; then
 echo "Usage: ./means.sh EXPID yyyymm, where EXPID and yyyymm correspond to the strings of your input grib files: ICMSHEXPID+yyyymm and ICMGGEXPID+yyyymm"
 exit
fi
EXPID=$1
yyyymm=$2


       cdo sp2gpl ICMSH${EXPID}+${yyyymm} fcdata0
       cat fcdata0 ICMGG${EXPID}+${yyyymm}  > fcdata
       NYEAR=$(echo $yyyymm | cut -c1-4)
       NMONTH=$(echo $yyyymm | cut -c5-6)
       NMDAYS=$(cal $NMONTH $NYEAR | grep -v "[a-z]" | grep -v "^$" | wc -w)
       NSTP_DAY_R=$(( 24 / $NFRP ))
       NSTP_DAY=${NSTP_DAY_R%.*}
       echo write \"fcdata_[param].grb\"\; > rules_fcdata
       grib_filter rules_fcdata fcdata #fcdata_${param}.grb, 6h data

       cdo -r add fcdata_142.128.grb fcdata_143.128.grb fcdata_228.128.grb
       cdo -r mulc,1000 fcdata_228.128.grb fcdata_228.128.grb2
       mv fcdata_228.128.grb2 fcdata_228.128.grb
       rm fcdata_142.128.grb fcdata_143.128.grb
       for param in 146 147 169 175 176 177 179 228
       do
        cdo -r divc,3600.*${NFRP} fcdata_${param}.128.grb fcdata_${param}.128.grb2
        mv fcdata_${param}.128.grb2 fcdata_${param}.128.grb
       done
       #monthly 00GMT: extract good timesteps
       for param in 141 146 147 168 169 175 176 177 179 201 202 228 243 #212
       do
        cdo seltimestep,$(seq -s ',' 1 ${NSTP_DAY} $(( ${NMDAYS}*${NSTP_DAY}))) fcdata_${param}.128.grb  fcdata_${param}_mon0.grb
       done
       #split daily
       for param in 169 177 179 201 202 228
       do
        i=1
        while [[ $i -le $NMDAYS ]]
        do
         cdo seltimestep,$(seq -s ',' $(( $i * 4 - 3)) $(( $i * 4 ))) fcdata_${param}.128.grb  fcdata_${param}_d_$i.grb
         i=`expr $i + 1 `
        done
       done

       #mon0: conv2nc + mean
       for infcdata in `ls *mon0.grb`
       do
         cdo -R -r -t ecmwf -f nc splitvar $infcdata rg
         cdo -f nc -r -timmean rg* ${infcdata%???}nc
         rm -f rg*
       done
       #  cdo -f nc -r -timmean $infcdata ${infcdata%???}nc

       #monthly means with 6h
       for param in 151 164 165 166 167 168
       do
        cdo -R -r -t ecmwf -f nc splitvar fcdata_${param}.128.grb rg
        cdo -f nc -r -timmean rg* fcdata_${param}_mon.nc
        rm -f rg*
       done
       # cdo -f nc -r -timmean fcdata_${param}.128.grb ${fcdata%????}_${param}_mon.nc

       #daily: mean+conv2nc+concat
       for param in 169 177 179 201 202 228
       do
        for infcdata in `ls *${param}_d_*.grb`
        do
         cdo -R -r -t ecmwf -f nc splitvar $infcdata rg
         cdo -f nc -r -timmean rg* ${infcdata%???}nc #new
         rm -f rg*
        done
         i=1
         rm -f l
        while [[ $i -le $NMDAYS ]]
        do
         ls *${param}_d_${i}.nc >> l
         i=`expr $i + 1 `
        done
        ncrcat `cat l` fcdata_${param}_d.nc
       #before ncrcat *${param}_d_*.nc  fcdata_${param}_d.nc
       # ncrcat ${infcdata%???}nc  fcdata_${param}_d.nc
       done
       rm -f *_d_*
       #  cdo -f nc -r -timmean $infcdata ${infcdata%???}nc

       #6hour
       for param in 151 164 165 166 167 168
       do
        cdo -R -r -t ecmwf -f nc splitvar fcdata_${param}.128.grb rg
        cdo -f nc copy rg* fcdata_${param}_6h.nc
        rm -f rg*
       done
       # cdo -f nc copy fcdata_${param}.128.grb fcdata_${param}_6h.nc

       #concat all vars in one fcdata for mon,day,6h
       for infcdata in `ls *mon*.nc`
       do
        ncks -A $infcdata ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc
       done
       cdo showvar ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc
       ncrename -v LSP,TP ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc
       ncatted -O -a units,TP,m,c,"kg m-2 s-1" ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc
       for var in SSHF SLHF SSRD STR STRD TTR SSR TP
       do
        pst=`ncdump -h ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc | grep $var | head -n1 | cut -c 2`
        if [ ! -z $pst ]; then
         ncatted -O -a units,$var,m,c,"W m-2" ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc
        fi
        pst=''
       done

       for infcdata in `ls *d.nc`
       do
        ncks -A $infcdata ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc
       done
        cdo showvar ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc
        ncrename -v LSP,TP ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc
        ncatted -O -a units,TP,m,c,"kg m-2 s-1" ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc
        for var in SSRD STR STRD TTR TP
        do
         pst=`ncdump -h ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc | grep $var | head -n1 | cut -c 2`
         if [ ! -z $pst ]; then
          ncatted -O -a units,$var,m,c,"W m-2" ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc
         fi
         pst=''
        done

       for infcdata in `ls *6h*.nc`
       do
        ncks -A $infcdata ${EXPID}_6hr_${yyyymm}01-${yyyymm}${NMDAYS}.nc
       done
        for var in SSHF SLHF SSRD STR STRD TTR
        do
         pst=`ncdump -h ${EXPID}_6hr_${yyyymm}01-${yyyymm}${NMDAYS}.nc | grep $var | head -n1 | cut -c 2`
         if [ ! -z $pst ];then
          ncatted -O -a units,$var,m,c,"W m-2" ${EXPID}_6hr_${yyyymm}01-${yyyymm}${NMDAYS}.nc
         fi
         pst=''
        done

