#!/bin/bash
#set -x

yrini="2015"
yrend="2100"

run="SSP"
suffix=""
run2="${run}${suffix}"
varlist="runoff precip subl"
#explist="1 2 3 4 5 6 7 8 9 10 11 12"
explist="2.6-1 2.6-2 2.6-3 4.5-1 4.5-2 4.5-3 7.0-1 7.0-2 7.0-3 8.5-1 8.5-2 8.5-3"
mkdir Annual/
mkdir Annual/${run2}/

for var in $varlist ; do
for exp in $explist ; do	
for year in `seq ${yrini} 1 ${yrend}` ; do

echo "${run}-${exp} ${var} ${year}"

#cdo timsum ${run2}/${var}/${var}.${run}-${exp}.${year}.MM.nc Annual/${run2}/${var}.${run}-${exp}.${year}.YY.nc

cdo timsum ${run2}/${var}/${var}.${run}${exp}.${year}.MM.nc Annual/${run2}/${var}.${run}${exp}.${year}.YY.nc

#echo "cdo timsum ${run}/${var}/${var}.${run}-${exp}.${year}.MM.nc Annual/${run}/${var}.${run}-${exp}.${year}.YY.nc"

done	

#ncrcat Annual/${run2}/${var}.${run}-${exp}.*.YY.nc Annual/${run2}/${var}.${run}-${exp}.${yrini}-${yrend}.YY.nc

ncrcat Annual/${run2}/${var}.${run}${exp}.*.YY.nc Annual/${run2}/${var}.${run}${exp}.${yrini}-${yrend}.YY.nc

mkdir Annual/${run2}/${var}
mv Annual/${run2}/${var}.${run}${exp}.${yrini}-${yrend}.YY.nc Annual/${run2}/${var}/
#mv Annual/${run2}/${var}.${run}-${exp}.${yrini}-${yrend}.YY.nc Annual/${run2}/${var}/
rm -rf Annual/${run2}/${var}.${run}${exp}.*.YY.nc
#rm -rf Annual/${run2}/${var}.${run}-${exp}.*.YY.nc

done
done
