#!/bin/bash
#

#
# We require a certain nomenclature for the daily files to be processed :
# ModelName_Period_Sampling_Variable.nc :
# ModelName : Name of the model or institution which produced the file.
# Period : 19890101_20131231 in our case as this is the period of the forcing.
# Sampling : 1D as daily output have been requested.
# Variable : The name of the variable in the file.
#
#

VAR="ESoil Evap LAI LWnet PotEvap Qh Qle Qs Qsb RadT RadTmin RadTmax Rainf Snowf SWE SWnet SnowDepth SoilWet SubSnow TVeg"
VAR="Dis ESoil Evap LAI LWnet PotEvap Qh Qle Qs Qsb RadT RadTmax RadTmin Rainf SWE SWnet SnowDepth Snowf SoilWet SubSnow TVeg"

EXP="WFDE5_CRU_GPCC ETHZ_Avg IPSL_Avg IPSL_Alt"

for e in ${EXP} ; do
    echo "==> Experiment ${e}"
    if [ ! -d  ../GFDL/${e} ] ; then
	mkdir ../GFDL/${e}
	mkdir ../GFDL/${e}/TS_DA
	for v in ${VAR} ; do
	    echo $e $v
	    nu=$(ncdump -h ${e}/${v}_ALMA.nc | grep ${v} | grep units | grep -o "kg/" | wc -l)
	    if [ ${nu} -gt 0 ] ; then
		# Change units for water mass fluxes
		ncatted -h -a units,${v},o,c,"kg m-2 s-1" ${e}/${v}_ALMA.nc ../GFDL/${e}/TS_DA/GFDL_${e}_19890101_20131231_1D_${v}.nc
	    else
		cp ${e}/${v}_ALMA.nc ../GFDL/${e}/TS_DA/GFDL_${e}_19890101_20131231_1D_${v}.nc
	    fi
	done
    fi
done
