#!/bin/bash

####
# Code for preprocessing CMIP6 ESM data, please change dir names
# By Melnikova Irina, NIES (Japan), melnikova.irina@nies.go.jp
###


txtpath="outputtxt"
mkdir -p $txtpath
fpath="output"
models="NorESM2-LM ACCESS-ESM1-5 MPI-ESM1-2-LR CanESM5 CNRM-ESM2-1 UKESM1-0-LL IPSL-CM6A-LR MIROC-ES2L CESM2-WACCM"
for model in $models
do
	if [ $model = "IPSL-CM6A-LR" ];then
                center="IPSL"
                ensemble="r1i1p1f1"
		vars="nbp fgco2 tas fLuc gpp ra rh"
		scenarios="ssp119 ssp126 ssp245 ssp370 ssp434 ssp460 ssp534-over ssp585"
        elif [ $model = "CanESM5" ];then
                center="CCCma"
                ensemble="r1i1p1f1"
		vars="nbp fgco2 tas gpp ra rh"
		scenarios="ssp119  ssp126  ssp245  ssp370  ssp434  ssp460  ssp534-over  ssp585"
        elif [ $model = "CNRM-ESM2-1" ];then
                center="CNRM-CERFACS"
                ensemble="r1i1p1f2"
		vars="nbp fgco2 tas fLuc gpp ra rh"
		scenarios="ssp119  ssp126  ssp245  ssp370  ssp434  ssp460  ssp534-over  ssp585"
        elif [ $model = "UKESM1-0-LL" ];then
                center="MOHC"
                ensemblepi="r1i1p1f2"
                ensemble="r4i1p1f2"
		vars="nbp fgco2 tas fLuc gpp ra rh"
		scenarios="ssp119  ssp126  ssp245  ssp370  ssp434  ssp534-over  ssp585"
        elif [ $model = "CESM2-WACCM" ];then
                center="NCAR"
                ensemble="r1i1p1f1"
		vars="nbp fgco2 tas fLuc gpp ra rh"
		scenarios="ssp126  ssp245  ssp370  ssp534-over  ssp585"
        elif [ $model = "MIROC-ES2L" ];then
                center="MIROC"
                ensemble="r1i1p1f2"
		vars="nbp fgco2 tas gpp ra rh"
		scenarios="ssp119  ssp126  ssp245  ssp370  ssp534-over  ssp585"
        elif [ $model = "ACCESS-ESM1-5" ];then
                center="CSIRO"
                ensemble="r1i1p1f1"
		vars="nbp fgco2 tas gpp ra rh"
		scenarios="ssp126  ssp245  ssp370  ssp585"
        elif [ $model = "MPI-ESM1-2-LR" ];then
                center="MPI-M"
                ensemble="r1i1p1f1"
		vars="nbp fgco2 tas fLuc gpp ra rh"
		vars="fLuc"
		scenarios="ssp126  ssp245  ssp370  ssp585"
        elif [ $model = "NorESM2-LM" ];then
                center="NCC"
                ensemble="r1i1p1f1"
		vars="nbp fgco2 tas fLuc gpp ra rh"
		scenarios="ssp126 ssp245 ssp370 ssp534-over ssp585"
        fi
	scenarios="ssp534-over-noLu"
	for scenario in  ${scenarios}
	do
		for var in  ${vars}
		do
			inyear="${fpath}/${model}/${var}_${model}_${scenario}_${ensemble}_year.nc"
			inyeartwo="${fpath}/${model}/${var}_${model}_${scenario}-ext_${ensemble}_year.nc"
			mergefile="${fpath}/${model}/${var}_${model}_hist-${scenario}_${ensemble}_year.nc"
			deltayear="${fpath}/${model}/delta_${var}_${model}_hist-${scenario}_${ensemble}_year.nc"
			ssp5year="${fpath}/${model}/${var}_${model}_ssp585_${ensemble}_year.nc"
			histyear="${fpath}/${model}/${var}_${model}_historical_${ensemble}_year.nc"
			areao="${fpath}/${model}/${model}_area.nc"
			pimean="${fpath}/${model}/allmean_${var}_${model}_piControl_${ensemble}_year.nc"
			trendb="${fpath}/${model}/trendb_${var}_${model}_piControl_${ensemble}_year.nc"
			if [ $scenario = "ssp534-over-noLu" ];then
				inyear="${fpath}/${model}/${var}_${model}_${scenario}_${ensemble}_year.nc"
				inyeartwo="${fpath}/${model}/${var}_${model}_${scenario}-ext_${ensemble}_year.nc"
				mergefile="${fpath}/${model}/${var}_${model}_hist-${scenario}_${ensemble}_year.nc"
				deltayear="${fpath}/${model}/delta_${var}_${model}_hist-${scenario}_${ensemble}_year.nc"
				ssp5year="${fpath}/${model}/${var}_${model}_ssp585-noLu_${ensemble}_year.nc"
				histyear="${fpath}/${model}/${var}_${model}_hist-noLu_${ensemble}_year.nc"
				if [ ! -f $inyear ];then
					inyear="${fpath}/${model}/${var}_${model}_${scenario}_year.nc"
				fi
				if [ ! -f $inyeartwo ];then
					inyeartwo="${fpath}/${model}/${var}_${model}_${scenario}-ext_year.nc"
				fi
				if [ ! -f $ssp5year ];then
					ssp5year="${fpath}/${model}/${var}_${model}_ssp585-noLu_year.nc"
				fi
				if [ ! -f $histyear ];then
					histyear="${fpath}/${model}/${var}_${model}_hist-noLu_year.nc"
				fi
			fi
			if [ $model = "UKESM1-0-LL" ];then
				pimean="${fpath}/${model}/allmean_${var}_${model}_piControl_${ensemblepi}_year.nc"
				trendb="${fpath}/${model}/trendb_${var}_${model}_piControl_${ensemblepi}_year.nc"
			fi
			if [ $scenario = "ssp534-over" ] || [ $scenario = "ssp534-over-noLu" ];then
				if [ $model = "CNRM-ESM2-1" ] || [ $model = "MIROC-ES2L" ];then
					cdo selvar,${var} -setctomiss,-999 -mergetime $histyear $inyear $mergefile
				elif [ $model = "IPSL-CM6A-LR" ] &&  [ -f $inyeartwo ];then
					cdo selvar,${var} -mergetime $histyear -selyear,2015/2039 $ssp5year -selyear,2040/2100 $inyear $inyeartwo $mergefile
				elif [ $model = "IPSL-CM6A-LR" ] ||  [ $model = "CESM2-WACCM" ] ||  [ $model = "UKESM1-0-LL" ] || [ $model = "CanESM5" ] || [ $model = "NorESM2-LM" ];then
					cdo selvar,${var} -mergetime $histyear -seltimestep,1/25 $ssp5year $inyear $mergefile
				fi
			elif [ $model = "IPSL-CM6A-LR" ] &&  [ -f $inyeartwo ];then
				cdo selvar,${var} -mergetime $histyear -selyear,2015/2100 $inyear $inyeartwo $mergefile
			else
				cdo selvar,${var} -mergetime $histyear $inyear $mergefile
			fi
			echo cdo subtrend -setctomiss,-999 $mergefile $pimean $trendb $deltayear
			cdo subtrend $mergefile $pimean $trendb $deltayear
			if [ ${var} = "tas" ] || [ $var = "mrsos" ];then
				cdo -outputtab,year,value -fldmean ${deltayear} | awk 'FNR==1{ row=$2","$3;print row} FNR!=1{ row=$1","$2; print row}' > ${txtpath}/delta_${var}_${model}_${scenario}.csv
			elif [ ${var} = "fLuc" ];then
				cdo gridarea ${mergefile} ${areao}
				cdo -outputtab,year,value -divc,1e+12 -fldsum -mul ${mergefile} ${areao} | awk 'FNR==1{ row=$2","$3;print row  } FNR!=1{ row=$1","$2; print row}' > ${txtpath}/${var}_${model}_${scenario}.csv
			else	
				cdo gridarea ${mergefile} ${areao}
				cdo -outputtab,year,value -divc,1e+12 -fldsum -mul ${mergefile} ${areao} | awk 'FNR==1{ row=$2","$3;print row  } FNR!=1{ row=$1","$2; print row}' > ${txtpath}/${var}_${model}_${scenario}.csv
				cdo -outputtab,year,value -divc,1e+12 -fldsum -mul ${deltayear} ${areao} | awk 'FNR==1{ row=$2","$3;print row  } FNR!=1{ row=$1","$2; print row}' > ${txtpath}/delta_${var}_${model}_${scenario}.csv
			fi
		done #vars		
	done #scenarios
done #models
