#!/bin/bash


####
# Code for preprocessing CMIP6 ESM data, please change dir names
# By Melnikova Irina, NIES (Japan), melnikova.irina@nies.go.jp
###
scenarios="piControl historical ssp119 ssp126 ssp245 ssp370 ssp434 ssp460 ssp534-over ssp585"
run="COU"
ipath="data"
fpath="output"
txtpath="ouputtxt"
mkdir -p ${fpath}
mkdir -p ${txtpath}
models="NorESM2-LM ACCESS-ESM1-5 CanESM5 CNRM-ESM2-1 UKESM1-0-LL IPSL-CM6A-LR MIROC-ES2L CESM2-WACCM MPI-ESM1-2-LR"
for model in $models
do
	mkdir -p ${fpath}/${model}
	for scenario in  ${scenarios}
	do
        if [ $scenario = "historical" ] || [ $scenario = "piControl" ];then
                mip="CMIP"
        else
                mip="ScenarioMIP"
        fi

	if [ $scenario = "hist-bgc" ] || [ $scenario = "ssp585-bgc" ] || [ $scenario = "ssp534-over-bgc" ];then
		run="BGC"
	elif [ $scenario = "historical" ] || [ $scenario = "ssp585" ] || [ $scenario = "ssp534-over" ];then
		run="COU"
	fi
        if [ $model = "IPSL-CM6A-LR" ];then
                center="IPSL"
                ensembles="r1i1p1f1"
        elif [ $model = "CanESM5" ] && [ $run = "COU" ];then
                center="CCCma"
                ensembles="r1i1p1f1"
        elif [ $model = "CanESM5" ] && [ $run = "BGC" ];then
                center="CCCma"
                ensembles="r1i1p2f1"
        elif [ $model = "CNRM-ESM2-1" ] && [ $run = "COU" ];then
                center="CNRM-CERFACS"
                ensembles="r1i1p1f2"
        elif [ $model = "CNRM-ESM2-1" ] && [ $run = "BGC" ];then
                center="CNRM-CERFACS"
                ensembles="r1i1p1f2"
        elif [ $model = "UKESM1-0-LL" ];then
                center="MOHC"
                ensembles="r1i1p1f2"
                ensembles="r4i1p1f2"
        elif [ $model = "CESM2-WACCM" ];then
                center="NCAR"
                #ensembles="r2i1p1f1 r1i1p1f1"
                ensembles="r1i1p1f1"
        elif [ $model = "MIROC-ES2L" ];then
                center="MIROC"
                ensembles="r1i1p1f2"
	elif [ $model = "ACCESS-ESM1-5" ];then
                center="CSIRO"
                ensembles="r1i1p1f1"
	elif [ $model = "MPI-ESM1-2-LR" ];then
                center="MPI-M"
                ensembles="r1i1p1f1"
	elif [ $model = "NorESM2-LM" ];then
                center="NCC"
                ensembles="r1i1p1f1"
        fi
		for ensemble in $ensembles
		do
			for realm in Lmon Amon Omon Emon
			do
			if [ ${realm} = "Emon" ];then
                        	vars="fLuc"
                        elif [ ${realm} = "Lmon" ];then
				vars="gpp ra rh nbp"
				vars="lai mrsos"
                        elif [ ${realm} = "Omon" ];then
				vars="fgco2"
                        elif [ ${realm} = "Amon" ];then
				vars="tas"
			fi
			for var in ${vars}
			do
			for grid in gn #$grids 
			do
				ffolder="${ipath}/${mip}/${center}/${model}/${scenario}/${grid}/"
				fin="${ipath}/${mip}/${center}/${model}/${scenario}/${grid}/${var}*"				
				mergefile="${fpath}/${model}/${var}_${model}_${scenario}_${ensemble}_all.nc"
				mergeyear="${fpath}/${model}/${var}_${model}_${scenario}_${ensemble}_year.nc"
				allmean="${fpath}/${model}/allmean_${var}_${model}_${scenario}_year.nc"
				trenda="${fpath}/${model}/trenda_${var}_${model}_${scenario}_year.nc"
				trendb="${fpath}/${model}/trendb_${var}_${model}_${scenario}_year.nc"
				areao="${fpath}/${model}/${var}_${model}_${scenario}_area.nc"
				cdo setcalendar,standard -mergetime ${fin} ${mergefile}
				if [ ${var} = "tas" ] || [ ${var} = "pr" ] || [ ${var} = "mrsos" ];then
					cdo yearmonmean -selvar,${var} ${mergefile} ${mergeyear}
				elif [ ${var} = "lai" ];then
					cdo yearmax -selvar,${var} ${mergefile} ${mergeyear}
				else	
					cdo yearsum  -mulc,86400  -muldpm -selvar,${var} ${mergefile} ${mergeyear}
				fi
				cdo gridarea  ${mergeyear} $areao
        			if [ $scenario = "piControl" ];then
					cdo timmean -selvar,${var} ${mergeyear} ${allmean}
					cdo trend $mergeyear $trenda $trendb
				fi
			done #grid
			done #vars
			done #realms		
			done #ensembles		
	done #scenarios
done #models
