#dir=/mnt/c/Users/quant/Desktop/temp/BCSD/PTV
dir=/work/users/hmo/remoclic/Quan/DATA
model=MPI-ESM-MR
sed -e 's/imodel/'$model'/g' biascorrect.R > tmp_biascorrect.R
mkdir $model
mv tmp_biascorrect.R $model
cd $model
#cdo splitmon $dir/pr_APH_historical_SRF.nc APH_
cdo splitmon $dir/APH_historical.nc APH_
cdo splitmon $dir/pr_$model\_RegCM4_historical.nc $model\.his_
cdo splitmon $dir/pr_$model\_RegCM4_rcp45.nc $model\.fu45_
cdo splitmon $dir/pr_$model\_RegCM4_rcp85.nc $model\.fu85_
Rscript tmp_biascorrect.R

mon='1 2 3 4 5 6 7 8 9 10 11 12'

for imon in $mon; do
	cdo splityear pr.RCP45.$model\.$imon\.nc pr.RCP45.$model\.$imon\.
	cdo splityear pr.RCP85.$model\.$imon\.nc pr.RCP85.$model\.$imon\.
done

mkdir tmp
for iyear in $(seq 2006 2099);do
	for imon in $mon;do
		cdo splitmon pr.RCP45.$model\.$imon\.$iyear\.nc tmp/pr.RCP45.$model\.$iyear.
		cdo splitmon pr.RCP85.$model\.$imon\.$iyear\.nc tmp/pr.RCP85.$model\.$iyear.
	done
done

cdo mergetime tmp/pr.RCP45.$model\.200* tmp/tmp45.10.nc
cdo mergetime tmp/pr.RCP45.$model\.201* tmp/tmp45.11.nc
cdo mergetime tmp/pr.RCP45.$model\.202* tmp/tmp45.12.nc
cdo mergetime tmp/pr.RCP45.$model\.203* tmp/tmp45.13.nc
cdo mergetime tmp/pr.RCP45.$model\.204* tmp/tmp45.14.nc
cdo mergetime tmp/pr.RCP45.$model\.205* tmp/tmp45.15.nc
cdo mergetime tmp/pr.RCP45.$model\.206* tmp/tmp45.16.nc
cdo mergetime tmp/pr.RCP45.$model\.207* tmp/tmp45.17.nc
cdo mergetime tmp/pr.RCP45.$model\.208* tmp/tmp45.18.nc
cdo mergetime tmp/pr.RCP45.$model\.209* tmp/tmp45.19.nc

cdo mergetime tmp/tmp45* bc.pr_$model\_RegCM4_rcp45.nc

cdo mergetime tmp/pr.RCP85.$model\.200* tmp/tmp85.10.nc
cdo mergetime tmp/pr.RCP85.$model\.201* tmp/tmp85.11.nc
cdo mergetime tmp/pr.RCP85.$model\.202* tmp/tmp85.12.nc
cdo mergetime tmp/pr.RCP85.$model\.203* tmp/tmp85.13.nc
cdo mergetime tmp/pr.RCP85.$model\.204* tmp/tmp85.14.nc
cdo mergetime tmp/pr.RCP85.$model\.205* tmp/tmp85.15.nc
cdo mergetime tmp/pr.RCP85.$model\.206* tmp/tmp85.16.nc
cdo mergetime tmp/pr.RCP85.$model\.207* tmp/tmp85.17.nc
cdo mergetime tmp/pr.RCP85.$model\.208* tmp/tmp85.18.nc
cdo mergetime tmp/pr.RCP85.$model\.209* tmp/tmp85.19.nc

cdo mergetime tmp/tmp85* bc.pr_$model\_RegCM4_rcp85.nc
mv *RegCM4* ../

