<jupyter_start><jupyter_text>Averaging monthly climatic variablesThis notebook provides a short script that helps merge montly raster layers into and create a annual average. In this case this is used to average temperature, solar radiation, wind and precipication retrieved from [WorldClimV2](https://worldclim.org/version2).**Original code:** [<NAME>](https://github.com/kopegios) **Conceptualization & Methodological review :** [<NAME>](https://github.com/akorkovelos) **Updates, Modifications:** [<NAME>](https://github.com/akorkovelos) **Funding:** The World Bank (contract number: 7190531), [KTH](https://www.kth.se/en/itm/inst/energiteknik/forskning/desa/welcome-to-the-unit-of-energy-systems-analysis-kth-desa-1.197296)<jupyter_code>#Import necessary moduls and libraries

import os
import numpy as np
from glob import glob
import rasterio
# Here is an example for temperature
%time
print("Reading data...")

#Difine directories and name
data_dir = r"\\ug.kth.se\dfs\home\a\l\alekor\appdata\xp.V2\Desktop\Testing code\WorldClimTempMonthly"
avgoutputpath = r"\\ug.kth.se\dfs\home\a\l\alekor\appdata\xp.V2\Desktop\Testing code\WorldClimTempMonthly"
outputname = "tavg.tif"

file_list = glob(os.path.join(data_dir, '*.tif'))

def read_file(file):
    with rasterio.open(file) as src:
        return(src.read(1))

print ("Read all data as a list of numpy arrays..")
array_list = [read_file(x) for x in file_list]

print("Perform averaging...")
array_out = np.mean(array_list, axis=0)

print("Get metadata from one of the input files..")
with rasterio.open(file_list[0]) as src:
    meta = src.meta

meta.update(dtype=rasterio.float32)

print("Writing output file...")

with rasterio.open(avgoutputpath + "//" + outputname, 'w', **meta) as dst:
    dst.write(array_out.astype(rasterio.float32), 1)<jupyter_output>Wall time: 0 ns
Reading data...
Read all data as a list of numpy arrays..
Perform averaging...
Get metadata from one of the input files..
Writing output file...
{'fraction_non_alphanumeric': 0.13663967611336034, 'fraction_numerical': 0.013663967611336033, 'mean_word_length': 4.936936936936937, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 5, 'lorem ipsum': 0, 'www.': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '357589', 'license': 'MIT', 'n_tokens_mistral': 698, 'n_tokens_neox': 666, 'n_words': 177, 'path': 'agrodem_preprocessing/Downscaling/Downscaling_prepping/.ipynb_checkpoints/Averaging_Monthly_Datasets-checkpoint.ipynb', 'repo_name': 'babakkhavari/agrodem', 'size': '2019'}