# This script creates biome masks used to analyse biome-specific model output

cd DIFFS_AED_ALL


cdo mul CO2_FIRE.nc VEGET_COV_MAX.nc temp1.nc
cdo yearsum temp1.nc tempx.nc
cdo timmean tempx.nc tempy.nc

cdo mul tempy.nc Areas.nc tempz.nc
cdo mulc,1e-6 tempz.nc tempa.nc 
cdo divc,365 tempa.nc tempb.nc

cdo sellevel,2,3 tempb.nc temp2.nc
cdo vertsum temp2.nc trop.nc
cdo sellevel,4,5,6 tempb.nc temp3.nc
cdo vertsum temp3.nc tmperate.nc
cdo sellevel,7,8,9 tempb.nc temp4.nc
cdo vertsum temp4.nc bor.nc
cdo sellevel,10 tempb.nc temp11.nc
cdo vertsum temp11.nc C3.nc
cdo sellevel,11 tempb.nc temp12.nc
cdo vertsum temp12.nc C4.nc
cdo chname,CO2_FIRE,C3 C3.nc C3_1.nc
cdo chname,CO2_FIRE,C4 C4.nc C4_1.nc

cdo chname,CO2_FIRE,TROP trop.nc trop_1.nc
cdo chname,CO2_FIRE,TEMP tmperate.nc tmperate_1.nc
cdo chname,CO2_FIRE,BOREAL bor.nc bor_1.nc

cdo merge C3_1.nc C4_1.nc trop_1.nc tmperate_1.nc bor_1.nc All.nc


rm -rf temp* trop.nc tmperate.nc bor.nc trop_1.nc tmperate_1.nc bor_1.nc C3.nc C4.nc C3_1.nc C4_1.nc 

ncap2 -s 'where (C3<C4) C3=0 ;' All.nc temp1.nc
ncap2 -s 'where (C3<TROP) C3=0 ;' temp1.nc temp2.nc
ncap2 -s 'where (C3<TEMP) C3=0 ;' temp2.nc temp3.nc
ncap2 -s 'where (C3<BOREAL) C3=0 ;' temp3.nc temp4.nc
ncap2 -s 'where (C3>0) C3=1 ;' temp4.nc temp5.nc
cdo selvar,C3 temp5.nc C3_max.nc
rm -rf temp* 

ncap2 -s 'where (C4<C3) C4=0 ;' All.nc temp1.nc
ncap2 -s 'where (C4<TROP) C4=0 ;' temp1.nc temp2.nc
ncap2 -s 'where (C4<TEMP) C4=0 ;' temp2.nc temp3.nc
ncap2 -s 'where (C4<BOREAL) C4=0 ;' temp3.nc temp4.nc
ncap2 -s 'where (C4>0) C4=1 ;' temp4.nc temp5.nc
cdo selvar,C4 temp5.nc C4_max.nc
rm -rf temp* 


ncap2 -s 'where (TROP<C4) TROP=0 ;' All.nc temp1.nc
ncap2 -s 'where (TROP<C3) TROP=0 ;' temp1.nc temp2.nc
ncap2 -s 'where (TROP<TEMP) TROP=0 ;' temp2.nc temp3.nc
ncap2 -s 'where (TROP<BOREAL) TROP=0 ;' temp3.nc temp4.nc
ncap2 -s 'where (TROP>0) TROP=1 ;' temp4.nc temp5.nc
cdo selvar,TROP temp5.nc TROP_max.nc
rm -rf temp* 

ncap2 -s 'where (TEMP<C4) TEMP=0 ;' All.nc temp1.nc
ncap2 -s 'where (TEMP<C3) TEMP=0 ;' temp1.nc temp2.nc
ncap2 -s 'where (TEMP<TROP) TEMP=0 ;' temp2.nc temp3.nc
ncap2 -s 'where (TEMP<BOREAL) TEMP=0 ;' temp3.nc temp4.nc
ncap2 -s 'where (TEMP>0) TEMP=1 ;' temp4.nc temp5.nc
cdo selvar,TEMP temp5.nc TMPRATE_max.nc
rm -rf temp* 

ncap2 -s 'where (BOREAL<C4) BOREAL=0 ;' All.nc temp1.nc
ncap2 -s 'where (BOREAL<C3) BOREAL=0 ;' temp1.nc temp2.nc
ncap2 -s 'where (BOREAL<TEMP) BOREAL=0 ;' temp2.nc temp3.nc
ncap2 -s 'where (BOREAL<TROP) BOREAL=0 ;' temp3.nc temp4.nc
ncap2 -s 'where (BOREAL>0) BOREAL=1 ;' temp4.nc temp5.nc
cdo selvar,BOREAL temp5.nc BOREAL_max.nc
rm -rf temp* 


mv C3_max.nc C3_mask.nc
mv C4_max.nc C4_mask.nc
mv TROP_max.nc TROP_mask.nc
mv TMPRATE_max.nc TMPRATE_mask.nc
mv BOREAL_max.nc BOREAL_mask.nc

cdo setctomiss,0 C3_mask.nc C3_NaNmask.nc
cdo setctomiss,0 C4_mask.nc C4_NaNmask.nc
cdo setctomiss,0 TROP_mask.nc TROP_NaNmask.nc
cdo setctomiss,0 TMPRATE_mask.nc TMPRATE_NaNmask.nc
cdo setctomiss,0 BOREAL_mask.nc BOREAL_NaNmask.nc

mkdir FIRE_MASK
mv *_mask.nc *_NaNmask.nc All.nc FIRE_MASK/
