#!/bin/bash
export SC=//home/kshakya/data1/SCRIPTS/Retro_NoahMP_5km/


for file in `ls -1 Retro.*.nc`; do
    yyyy=`echo $file | cut -b7-10`
    mm=`echo $file | cut -b11-12`
    echo "$yyyy $mm"
    cp $file $file.mod
#    cdo -s -L setgrid,$SC/5kmgrid -settaxis,${yyyy}-${mm}-01,00:00:00 -setreftime,${yyyy}-${mm}-01,00:00:00 temp.nc $file
    ncks -A $SC/weights.nc $file.mod
    ncwa -a lev_2 -d lev_2,0,2 -w SoilDepth -v SoilMoist_inst $file.mod -O $file.mod
    ncks -x -v SoilMoist_inst $file -O $file
    ncks -x -v lev_2 $file.mod -O $file.mod                                       
    ncks -A $file.mod $file
    rm $file.mod
    ncap2 -s "Evap_tavg=Evap_tavg*86400; Rainf_f_tavg=Rainf_f_tavg*86400; SoilMoist_inst=SoilMoist_inst*1000 " $file -O $file
    ncatted -a units,Rainf_f_tavg,m,c,"mm day-1" -a units,Evap_tavg,m,c,"mm day-1" -a units,SoilMoist_inst,m,c,"kg m2-1" $file
done
