<jupyter_start><jupyter_text>Rename files<jupyter_code># for file in os.listdir(directory):
#     #print(file[0:58]+'20201215.tif')
#     os.rename(directory+file, directory+file[0:58]+'20201215.tif')<jupyter_output><empty_output><jupyter_text>Mosiac tifs<jupyter_code>#build vrt
os.chdir(directory)
os.system("gdalbuildvrt " + "Eastern_gm_mads_two_seasons_20210427_mosaic.vrt *.tif")

#build mosaic from .vrt
os.system("gdal_translate "\
       "-co BIGTIFF=YES "\
       "-co COMPRESS=DEFLATE "\
       "-co ZLEVEL=9 "\
       "-co PREDICTOR=1 "\
       "-co TILED=YES "\
       "-co BLOCKXSIZE=1024 "\
       "-co BLOCKYSIZE=1024 "\
       "-co NUM_THREADS=14 "\
       "--config GDAL_CACHEMAX 512 "
       + "Eastern_gm_mads_two_seasons_20210427_mosaic.vrt " + "Eastern_gm_mads_two_seasons_20210427_mosaic.tif")<jupyter_output><empty_output><jupyter_text>Clip mosaic to AEZ<jupyter_code># !pip install -e ../../production/dea_ml
# !pip install git+https://github.com/digitalearthafrica/deafrica-sandbox-notebooks.git@minty-fresh-sandbox#subdirectory=Tools
import xarray as xr
import numpy as np
import geopandas as gpd
from datacube.utils.cog import write_cog
import sys

from deafrica_tools.spatial import xr_rasterize
from deafrica_tools.dask import create_local_dask_cluster
create_local_dask_cluster()
# aez_region = 'data/Eastern.shp'
aez_region = 'data/WDPA_eastern.shp'
pred_tif = "results/classifications/20210427/Eastern_gm_mads_two_seasons_20210427_mosaic_clipped.tif"
chunks={'x':10000, 'y':10000}
aez = gpd.read_file(aez_region)
da = xr.open_rasterio(pred_tif)
aez = xr_rasterize(aez, da)
aez = 1-aez
da = da.chunk(chunks).where(aez.chunk(chunks)).astype(np.int8)
%%time
da = da.compute()
write_cog(da.squeeze(),
          pred_tif[:-4]+'_WDPA_masked.tif',
          overwrite=True)
# write_cog(aez.squeeze(),
#           'data/WDPA_eastern.tif',
#           overwrite=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13385416666666666, 'fraction_numerical': 0.046875, 'mean_word_length': 4.768768768768769, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '457807', 'license': 'Apache-2.0', 'n_tokens_mistral': 862, 'n_tokens_neox': 766, 'n_words': 106, 'path': 'testing/eastern_cropmask/mosaic.ipynb', 'repo_name': 'digitalearthafrica/crop-mask', 'size': '1920'}