# Processing of global plantation age and extent data
module load cdo noc ncl ncview

cdo remapbil,../Areas.nc Plantation_age_mean_0d1_fmtd.nc tempa.nc
ncap2 -s 'where(Age>20) Age=0.0 ;' tempa.nc Plantation_age_mean_0d1_fmtd_post2000.nc
#cdo setctomiss,0 temp1.nc Plantation_age_mean_0d1_fmtd_post2000.nc
ncap2 -s 'where(Age>0) Age=1.0 ;' Plantation_age_mean_0d1_fmtd_post2000.nc temp3.nc
cdo setrtoc,0.00000000000000000000000001,1,1 temp3.nc Plantation_age_mean_0d1_fmtd_post2000_mask.nc
rm -rf temp*


cdo remapbil,../Areas.nc Plantation_age_mean_0d1_fmtd.nc tempa.nc
ncap2 -s 'where(Age<20) Age=0.0 ;' tempa.nc Plantation_age_mean_0d1_fmtd_pre2000.nc
#cdo setctomiss,0 temp1.nc Plantation_age_mean_0d1_fmtd_pre2000.nc
ncap2 -s 'where(Age>0) Age=1.0 ;' Plantation_age_mean_0d1_fmtd_pre2000.nc temp3.nc
cdo setrtoc,0.00000000000000000000000001,1,1 temp3.nc Plantation_age_mean_0d1_fmtd_pre2000_mask.nc
rm -rf temp*
# THIS IS OK

cdo remapbil,../Areas.nc Plantation_age_mean_0d1_fmtd_post2000.nc tempx.nc 
ncap2 -s 'where(Age<0) Age=0;' tempx.nc Plantation_age_0.5deg_post2000.nc
#cdo setctomiss,0 temp1.nc Plantation_age_0.5deg_post2000.nc 
rm -rf temp*
cdo remapbil,../Areas.nc Plantation_age_mean_0d1_fmtd_pre2000.nc tempx.nc 
ncap2 -s 'where(Age<0) Age=0;' tempx.nc Plantation_age_0.5deg_pre2000.nc
#cdo setctomiss,0 temp1.nc Plantation_age_0.5deg_pre2000.nc 
rm -rf temp*



ncap2 -s 'where(Age>0) Age=1.0 ;' Plantation_age_0.5deg_pre2000.nc temp1.nc
cdo setrtoc,0.00000000000000000000000001,1,1 temp1.nc Plantation_age_0.5deg_pre2000_mask.nc
ncap2 -s 'where(Age>0) Age=1.0 ;' Plantation_age_0.5deg_post2000.nc temp2.nc
cdo setrtoc,0.00000000000000000000000001,1,1 temp2.nc Plantation_age_0.5deg_post2000_mask.nc
rm -rf temp*


cp -rf Plantation_age_0.5deg_post2000_mask.nc Plantation_age_0.5deg_pre2000_mask.nc Plantation_age_0.5deg_pre2000.nc  Plantation_age_0.5deg_post2000.nc ../MS_FIG/
cd ../MS_FIG/


#OK
ncap2 -s 'where(D_AREA_BURNT<-3) D_AREA_BURNT=-3;' dstd_BA_above_2.nc temp1.nc
ncap2 -s 'where(D_AREA_BURNT>3) D_AREA_BURNT=3;' temp1.nc temp2.nc
cdo setrtoc,-0.01,0,0 temp2.nc temp3.nc
cdo setrtoc,0,0.01,0  temp3.nc dstd_BA_above_2_limeq3.nc
rm -rf temp*
ncap2 -s 'where(D_AREA_BURNT>0.01) D_AREA_BURNT=1;' dstd_BA_above_2_limeq3.nc temp1.nc
ncap2 -s 'where(D_AREA_BURNT<-0.01) D_AREA_BURNT=1;' temp1.nc temp2.nc
cdo setrtoc,-0.01,0,0 temp2.nc temp3.nc
cdo setrtoc,0,0.01,0 temp3.nc dstd_BA_above_2_mask.nc
#OK


#DO DEFORESTATION ONLY MASK AND SEPARATE DEFORESTATION AND PLANTATION SINCE 2000 mask. 
#Deforestation Only: = red; size = severity
#Deforestation + plantation: blue --> shade=age; size = deforestation severity. 

# make scale (0-1, bins of 2)
ncap2 -s 'where(Forest_Loss_Driver<0.5) Forest_Loss_Driver=0;' Global_Forest_Loss_Driver.nc temp1.nc
ncap2 -s 'where(Forest_Loss_Driver>1.5) Forest_Loss_Driver=0;'  temp1.nc temp2.nc
cdo setrtoc,0.5,0.6,2 temp2.nc temp3.nc
cdo setrtoc,1.4,1.5,2 temp3.nc temp4.nc
cdo setrtoc,0.6,0.7,4 temp4.nc temp5.nc
cdo setrtoc,1.3,1.4,4 temp5.nc temp6.nc
cdo setrtoc,0.7,0.8,6 temp6.nc temp7.nc
cdo setrtoc,1.2,1.3,6 temp7.nc temp8.nc
cdo setrtoc,0.8,0.9,8 temp8.nc temp9.nc
cdo setrtoc,1.1,1.2,8 temp9.nc temp10.nc
cdo setrtoc,0.9,1.0,10 temp10.nc temp11.nc
cdo setrtoc,1.0,1.1,10 temp11.nc temp12.nc
cdo divc,10 temp12.nc deforest_scale_allGrid.nc
rm -rf temp*
# make scale (0-1, bins of 2)
cdo setrtoc,0.001,12,1 Plantation_age_0.5deg_post2000.nc temp1.nc
cdo setrtoc,12,14,2 temp1.nc temp2.nc
cdo setrtoc,14,16,3 temp2.nc temp3.nc
cdo setrtoc,16,18,4 temp3.nc temp4.nc
cdo setrtoc,18,21,5 temp4.nc temp5.nc
ncap2 -s 'where (Age>20) Age=0;' temp5.nc temp6.nc
cdo mulc,2 temp6.nc temp7.nc
cdo divc,10 temp7.nc temp8.nc
cdo setrtoc,1.000000000000001,3,1 temp8.nc Plantation_age_scale_post2000.nc
rm -rf temp*

PROBLEM FILE : Plantation_age_0.5deg_pre2000.nc


cdo setrtoc,20,22,1 Plantation_age_0.5deg_pre2000.nc temp1.nc
cdo setrtoc,22,24,2 temp1.nc temp2.nc
cdo setrtoc,24,26,3 temp2.nc temp3.nc
cdo setrtoc,26,28,4 temp3.nc temp4.nc
cdo setrtoc,28,30,5 temp4.nc temp5.nc
cdo setrtoc,30,32,6 temp5.nc temp6.nc
cdo setrtoc,32,34,7 temp6.nc temp7.nc
cdo setrtoc,34,36,8 temp7.nc temp8.nc
cdo setrtoc,36,39,9 temp8.nc temp9.nc
ncap2 -s 'where (Age>39.0000000001) Age=0;' temp9.nc temp10.nc
cdo divc,10 temp10.nc temp11.nc
cdo mulc,1.111111111 temp11.nc temp12.nc
cdo setrtoc,1.000000000000001,3,1 temp12.nc Plantation_age_scale_pre2000.nc
rm -rf temp*
## ABOVE NOW OK≥

#Mask out areas where model-data don't agree

# cdo mul Plantation_age_scale_post2000.nc dstd_BA_above_2_mask.nc Plantation_age_scale_post2000_dstd_mask.nc
# cdo mul Plantation_age_scale_pre2000.nc dstd_BA_above_2_mask.nc Plantation_age_scale_pre2000_dstd_mask.nc
# cdo mul deforest_scale_allGrid.nc dstd_BA_above_2_mask.nc deforest_scale_allGrid_dstd_mask.nc
# rm -rf temp*
#OK

# isolate deforestation-only from deforestation + plantation
cdo setgrid,../Global_plantation_pct_nc/grid.txt deforest_scale_allGrid.nc tempzz.nc
ncap2 -s 'where(Forest_Loss_Driver>0) Forest_Loss_Driver=1;' tempzz.nc temp1.nc
ncap2 -s 'where(Age>0) Age=10;' Plantation_age_scale_post2000.nc temp2.nc
ncap2 -s 'where(Age>0) Age=100;' Plantation_age_scale_pre2000.nc tempx.nc

#1= only deforest mask
#10=only post2000 plantation
#100=only pre2000 plantation
#11= deforest + plant post 2000
#101 = deforest + plant pre2000 (plant+ deforest mask )

cdo enssum tempx.nc temp2.nc temp1.nc temp3.nc

# where value = 2 is both forest and plantation so mask these cells 
cdo setrtoc,0.5,1.5,1 temp3.nc temp4.nc
cdo setrtoc,1.0000001,1000000,0 temp4.nc temp5.nc
ncap2 -s 'where (Age<1) Age=0;' temp5.nc Only_Deforest_Mask.nc
rm -rf temp4.nc temp5.nc

cdo setrtoc,9.5,10.5,1 temp3.nc temp4.nc
cdo setrtoc,1.0000001,1000000,0 temp4.nc temp5.nc
ncap2 -s 'where (Age<1) Age=0;' temp5.nc post2000_Only_Plant_Mask.nc
rm -rf temp4.nc temp5.nc

cdo setrtoc,99.5,100.5,1 temp3.nc temp4.nc
cdo setrtoc,1.0000001,1000000,0 temp4.nc temp5.nc
ncap2 -s 'where (Age<1) Age=0;' temp5.nc pre2000_Only_Plant_Mask.nc
rm -rf temp4.nc temp5.nc

cdo setrtoc,10.5,11.5,1 temp3.nc temp4.nc
cdo setrtoc,1.0000001,1000000,0 temp4.nc temp5.nc
ncap2 -s 'where (Age<1) Age=0;' temp5.nc post2000_Plant_plus_Def_scale_Masked.nc
rm -rf temp4.nc temp5.nc

cdo setrtoc,100.5,101.5,1 temp3.nc temp4.nc
cdo setrtoc,1.0000001,1000000,0 temp4.nc temp5.nc
ncap2 -s 'where (Age<1) Age=0;' temp5.nc pre2000_Plant_plus_Def_scale_Masked.nc
rm -rf temp*
cdo add post2000_Plant_plus_Def_scale_Masked.nc pre2000_Plant_plus_Def_scale_Masked.nc temp1.nc
ncap2 -s 'where (Age>0) Age=1;' temp1.nc Plant_plus_Deforest_Mask.nc
rm -rf temp1.nc
#OK --> nonzero

cdo mul deforest_scale_allGrid_dstd_mask.nc Only_Deforest_Mask.nc Defo_only_Defo_scale_Masked.nc
cdo mul deforest_scale_allGrid_dstd_mask.nc Plant_plus_Deforest_Mask.nc Def_plus_Plant_scale_Masked.nc


#### PROBLEM IS HERE!! THESE FILES RETURN AN ERROR.

cdo mul post2000_Plant_plus_Def_scale_Masked.nc Plantation_age_scale_post2000_dstd_mask.nc post2000_Plant_plus_Def_scale_Masked_1.nc
cdo mul Plantation_age_scale_post2000_dstd_mask.nc post2000_Only_Plant_Mask.nc post2000_Plant_only_Plant_scale_Masked_1.nc
cdo mul Plantation_age_scale_pre2000_dstd_mask.nc pre2000_Plant_plus_Def_scale_Masked.nc pre2000_Plant_plus_Def_scale_Masked_1.nc
cdo mul Plantation_age_scale_pre2000_dstd_mask.nc pre2000_Only_Plant_Mask.nc pre2000_Plant_only_Plant_scale_Masked_1.nc

cdo setctomiss,0 Defo_only_Defo_scale_Masked.nc temp1.nc
cdo setctomiss,0 Def_plus_Plant_scale_Masked.nc temp2.nc 

cdo setctomiss,0 post2000_Plant_plus_Def_scale_Masked_1.nc temp3.nc
cdo setctomiss,0 post2000_Plant_only_Plant_scale_Masked_1.nc temp4.nc
cdo setctomiss,0 pre2000_Plant_plus_Def_scale_Masked_1.nc temp5.nc
cdo setctomiss,0 pre2000_Plant_only_Plant_scale_Masked_1.nc temp6.nc
# stopped here
cdo chname,Forest_Loss_Driver,Deforest_scale_def_only temp1.nc tempx.nc
#cdo setgrid,../Global_plantation_pct_nc/grid.txt tempx.nc tempa.nc
#ncwa -a time tempx.nc tempbla.nc
cdo chname,Forest_Loss_Driver,Deforest_scale_defplant temp2.nc tempy.nc
#cdo setgrid,../Global_plantation_pct_nc/grid.txt tempy.nc tempb.nc
#ncwa -a time tempy.nc tempbla2.nc

cdo chname,Age,post2000_Plant_age_scale_defplant temp3.nc temp7.nc
cdo chname,Age,post2000_Plant_age_scale_plant_only temp4.nc temp8.nc
cdo chname,Age,pre2000_Plant_age_scale_defplant temp5.nc temp9.nc
cdo chname,Age,pre2000_Plant_age_scale_plant_only temp6.nc temp10.nc
#ncwa -a time_counter dstd_BA_above_2_limeq3.nc temp11.nc
cdo chname,D_AREA_BURNT,BA_FRAC_EXCESS_STD dstd_BA_above_2_limeq3.nc temp12.nc 

cdo merge temp7.nc temp8.nc temp9.nc temp10.nc  temp12.nc tempx.nc tempy.nc All_Data.nc



cdo sellonlatbox,90,155,22,-12 All_Data.nc All_Data_INDO.nc
cdo sellonlatbox,-89,-25,2,-26 All_Data.nc All_Data_AMAZON.nc


rm -rf Only_Deforest_Mask.nc post2000_Only_Plant_Mask.nc pre2000_Only_Plant_Mask.nc post2000_Plant_plus_Def_scale_Masked.nc pre2000_Plant_plus_Def_scale_Masked.nc Plant_plus_Deforest_Mask.nc Defo_only_Defo_scale_Masked.nc Defo_only_Defo_scale_Masked.nc Def_plus_Plant_scale_Masked.nc
# OK


cdo sellonlatbox,97,139,7,-9.5 dstd_BA_above_2_limeq3.nc temp1.nc
ncap2 -s 'where(D_AREA_BURNT<0) D_AREA_BURNT=0;' temp1.nc temp2.nc




###############
cdo setgrid,../Global_plantation_pct_nc/grid.txt deforest_scale_allGrid.nc tempyy.nc
ncap2 -s 'where(Forest_Loss_Driver>0) Forest_Loss_Driver=1;' tempyy.nc tempz.nc
# ncap2 -s 'where(Age>0) Age=10;' Plantation_age_scale_post2000.nc tempy.nc
# ncap2 -s 'where(Age>0) Age=100;' Plantation_age_scale_pre2000.nc tempx.nc
# cdo add tempz.nc tempy.nc tempx.nc

ncwa -a time tempz.nc tempy.nc
cdo sellonlatbox,97,118,7,-5.5 tempy.nc tempu.nc # mask of deforestation + plantation

cdo sellonlatbox,97,118,7,-5.5 dstd_BA_above_2_limeq3.nc temp1.nc
cdo setrtoc,0.000000000001,100,1 temp1.nc temp3.nc # agreement mask 1
ncap2 -s 'where (D_AREA_BURNT<0) D_AREA_BURNT=0;' temp3.nc tempc.nc # agreement mask 2
ncap2 -s 'where (D_AREA_BURNT<0) D_AREA_BURNT=1;' temp3.nc tempd.nc
ncap2 -s 'where (D_AREA_BURNT<0) D_AREA_BURNT=0;' tempd.nc tempe.nc
cdo setctomiss,0 tempe.nc tempf.nc # fire anomaly mask
cdo histcount,-100,0,0,100 temp1.nc tempa.nc
cdo fldsum tempa.nc tempb.nc # histcount of agree/disagree
#

cdo mul tempu.nc tempe.nc temp4.nc
 # mask out non-agreeing cells
cdo setrtoc,0.0000000000001,100000,1 temp4.nc temp5.nc
cdo histcount,-100,0,0.0000001,100 temp5.nc temp6.nc
cdo fldsum temp6.nc temp7.nc

cdo mul tempu.nc tempc.nc temp4.nc
 # mask out non-agreeing cells
cdo setrtoc,0.0000000000001,100000,1 temp4.nc temp5.nc
cdo histcount,-100,0,0.0000001,100 temp5.nc temp6.nc
cdo fldsum temp6.nc temp8.nc




Agree: 79 
Disagree: 57 = --> 58.1% Agree
Deforest/Plantation: = 81 = 60% of all grids
Deforest/Plantation IN AGREE GRIDS = 67% of all agreeing grids.  
