# Deforestation + Fire intensity + Model fire output agreement mask
# Get the mask of where fire >2% of grid area annually
# Then do the fractional change annually 
# 1) For any year where fire exceeds 20% of the 1982-1999 average, mask in; mask all else out.
# 2) Multiply absolute change by mask to get absolute values for each grid in each year.
# 3) Mask in observed grid cells where plantation inception occurs before or after 2000
# 4) Compare resulting observed - planting dataset with fragmentation fires. 
# 5) Expand to include Thailand 

# FOR FIRE RECURRENCE MASK

cdo yearsum time_series_2000_2019.nc BA_Annual_Mean_2000_19.nc

ncap2 -s 'where (burned_area<1000000)  burned_area=0;' BA_Annual_Mean_2000_19.nc temp1.nc
ncap2 -s 'where (burned_area>0) burned_area=1;' temp1.nc 1km_bBA_mask.nc 

cdo timstd BA_Annual_Mean_1982_1999.nc temp1.nc
cdo mulc,2 temp1.nc temp2.nc
cdo add BA_Ann_1982_1999.nc temp2.nc Mean_STDx2_90s.nc

cdo sub BA_Annual_Mean_2000_19.nc Mean_STDx2_90s.nc temp1.nc
cdo mul temp1.nc 1km_bBA_mask.nc temp2.nc
ncap2 -s 'where (burned_area<0) burned_area=0;' temp2.nc temp3.nc
ncap2 -s 'where (burned_area>0) burned_area=1;' temp3.nc temp4.nc
cdo timsum temp4.nc BA_yrs_BA_gt_2xmean_plus_std.nc


# AGREEMENT MASK RECURRENCE

cdo mul Forest_Loss_Deforestation_AllGrids.nc BA_fracofstd_summed_where_BA_gt_2xmean_plus_std.nc Deforest_All_x_mean2std.nc
ncap2 -s 'where(Forest_Loss_Driver>0) Forest_Loss_Driver=1;' Deforest_All_x_mean2std.nc Deforest_All_x_mean2std_mask.nc
ncap2 -s 'where(Forest_Loss_Driver>0) Forest_Loss_Driver=50;' Deforest_All_x_mean2std_mask.nc Deforest_All_x_mean2std_mask_eq50.nc

ncap2 -s 'where(D_AREA_BURNT<0.01) D_AREA_BURNT=0;' dFRAC_BA_VS_CTRL_yrsum_mean.nc temp1.nc
ncap2 -s 'where(D_AREA_BURNT>0) D_AREA_BURNT=1;' temp1.nc temp2.nc
ncap2 -s 'where(D_AREA_BURNT>0) D_AREA_BURNT=100;' temp2.nc temp3.nc
cdo sub temp3.nc Deforest_All_x_mean2std_mask_eq50.nc temp4.nc
ncap2 -s 'where (D_AREA_BURNT>99) D_AREA_BURNT=0;' temp4.nc temp5.nc
cdo setctomiss,0 temp5.nc Agree_at50_Model_Deforest_BA2std.nc   

#  FOR FIRE INTENSITY MASK


cdo sub BA_Annual_Mean_2000_19.nc Mean_STDx2_90s.nc temp1.nc
ncap2 -s 'where(burned_area<0) burned_area=0;' temp1.nc tempa.nc
ncap2 -s 'where(burned_area>0) burned_area=1;' tempa.nc tempb.nc
cdo mul BA_Annual_Mean_2000_19.nc tempb.nc tempc.nc
cdo sub tempc.nc BA_Ann_1982_1999.nc tempd.nc
cdo timstd BA_Annual_Mean_1982_1999.nc tempx.nc
cdo sub tempd.nc tempx.nc tempe.nc
cdo div tempe.nc tempx.nc tempy.nc
cdo mul tempy.nc 1km_bBA_mask.nc temp2.nc
ncap2 -s 'where (burned_area<0) burned_area=0;' temp2.nc temp3.nc
cdo timsum temp3.nc temp4.nc
cdo divc,20 temp4.nc Accum_mean2std.nc
rm -rf temp*


# AGREEMENT  INTENSITY MASK

ncap2 -s 'where(burned_area<0) burned_area=0;' Accum_mean2std.nc temp1.nc
ncap2 -s 'where(burned_area>0) burned_area=1;' temp1.nc Accum_mean2std_gt100ofstd_mask.nc
ncap2 -s 'where(burned_area>0) burned_area=50;' Accum_mean2std_gt100ofstd_mask.nc Accum_mean2std_gt100ofstd_mask_eq50.nc
rm -rf temp*
ncap2 -s 'where(D_AREA_BURNT<0.01) D_AREA_BURNT=0;' dFRAC_BA_VS_CTRL_yrsum_mean.nc temp1.nc
ncap2 -s 'where(D_AREA_BURNT>0) D_AREA_BURNT=1;' temp1.nc temp2.nc
ncap2 -s 'where(D_AREA_BURNT>0) D_AREA_BURNT=100;' temp2.nc temp3.nc
cdo sub temp3.nc Accum_mean2std_gt100ofstd_mask_eq50.nc temp4.nc
ncap2 -s 'where (D_AREA_BURNT>99) D_AREA_BURNT=0;' temp4.nc temp5.nc
cdo setctomiss,0 temp5.nc Agree_at50_Model_Accum_2std_gt100ofstd.nc   
rm -rf temp*

# AGREEMENT MASK INTENSITY with levels
rm -rf temp*
ncap2 -s 'where(burned_area<0) burned_area=0;' Accum_mean2std.nc temp1.nc
cdo mul temp1.nc Forest_Loss_Deforestation_AllGrids.nc temp2.nc
# cdo setrtoc,0.000000000001,0.999999999999,1 temp1.nc temp2.nc
# cdo setrtoc,1.0000000000000000001,10,2 temp2.nc temp3.nc
# cdo setrtoc,10.0000000000000000001,100,3 temp3.nc temp4.nc
# cdo setrtoc,100.0000000000000000001,1000,4 temp4.nc temp5.nc
# ncap2 -s 'where(burned_area>1000) burned_area=5' temp5.nc temp6.nc

cdo setrtoc,-50,0,-1 Agree_at50_Model_Accum_2std_gt100ofstd.nc temp7.nc
cdo setrtoc,0,99,1 temp7.nc temp8.nc
cdo mul temp8.nc temp1.nc temp9.nc
cdo setctomiss,0 temp9.nc temp10.nc

######
# AGREEMENT MASK INTENSITY with levels with Deforestation mask!
rm -rf temp*
ncap2 -s 'where(burned_area<0) burned_area=0;' Accum_mean2std.nc temp1.nc
cdo mul temp1.nc Forest_Loss_Deforestation_AllGrids.nc temp2.nc
cdo setrtoc,-50,0,-1 Agree_at50_Model_Accum_2std_gt100ofstd.nc temp7.nc
cdo setrtoc,0,99,1 temp7.nc temp8.nc
cdo mul temp8.nc temp2.nc temp9.nc
cdo setctomiss,0 temp9.nc temp10.nc

######


cdo sellonlatbox,-89,-25,2,-26 Agree_at50_Model_Accum_2std_gt100ofstd.nc temp1.nc
cdo histcount,-50,0,0,99 temp1.nc temp2.nc
cdo fldsum temp2.nc histo_Brazil.nc
ncdump histo_Brazil.nc
rm -rf temp*

cdo sellonlatbox,-89,-25,2,-26 dstd_BA_above_2_xdeforest.nc temp1.nc
cdo histcount,-1000,0,0,1000 temp1.nc temp2.nc
cdo fldsum temp2.nc histo_defo_Brazil.nc
ncdump histo_defo_Brazil.nc
rm -rf temp*



cdo sellonlatbox,90,155,22,-12 Agree_at50_Model_Accum_2std_gt100ofstd.nc temp1.nc
cdo histcount,-50,0,0,99 temp1.nc temp2.nc
cdo fldsum temp2.nc histo_Indo.nc
rm -rf temp*

cdo sellonlatbox,90,155,22,-12 dstd_BA_above_2_xdeforest.nc temp1.nc
cdo histcount,-1000,0,0,1000 temp1.nc temp2.nc
cdo fldsum temp2.nc histo_defo_Indo.nc
rm -rf temp*

