Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions regions/Ecuador/1.1. INAMHI-prepro.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1.1. INAMHI Preprocessing\n",
"This notebook converts raw stake mass-balance files from the Ecuadorian INAMHI network into cleaned CSV tables."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from scripts.config_EC import *\n",
"from scripts.helpers import seed_all\n",
"seed_all(42)\n",
"# TODO: implement preprocessing for INAMHI glacier mass-balance data"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
35 changes: 35 additions & 0 deletions regions/Ecuador/1.2. ERA5Land-prepro.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1.2. ERA5-Land Preprocessing\n",
"Download and format ERA5-Land climate variables for Ecuador."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from scripts.config_EC import path_ERA5_raw\n",
"# TODO: add ERA5-Land download and processing steps for Ecuador"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
36 changes: 36 additions & 0 deletions regions/Ecuador/3.2 Train-ML-model.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.2 Train ML Model\n",
"Assemble the Ecuador dataset, add features, and train an XGBoost model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from scripts.config_EC import *\n",
"from scripts.xgb_helpers import process_or_load_data\n",
"# TODO: load INAMHI mass balance data and call process_or_load_data"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
56 changes: 56 additions & 0 deletions regions/Ecuador/scripts/config_EC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# <------------------ INAMHI/Local Glacier Data: ------------------>
# Point data
path_PMB_INAMHI_raw = '../../../data/INAMHI/point/raw/'
path_PMB_INAMHI_csv = '../../../data/INAMHI/point/csv/'

# Glacier wide data
path_SMB_INAMHI_raw = '../../../data/INAMHI/glacier-wide/raw/'
path_SMB_INAMHI_csv = '../../../data/INAMHI/glacier-wide/csv/'

# Gridded data for MBM to use for making predictions over whole grid
path_glacier_grid = '../../../data/INAMHI/topo/gridded_topo_inputs/' # DEMs & topo

# Topo data
path_DEM = '../../../data/INAMHI/topo/DEM/' # DEMs
path_pcsr = '../../../data/INAMHI/topo/pcsr/' # Potential incoming clear sky solar radiation

path_distributed_MB = '../../../data/INAMHI/distributed_MB_grids/'
path_geodetic_MB = '../../../data/INAMHI/geodetic/'
path_glacier_ids = '../../../data/INAMHI/EC_glacier_ids.csv' # glacier ids for EC glaciers

# <------------------ OTHER PATHS: ------------------>
path_ERA5_raw = '../../../data/ERA5Land/raw/' # ERA5-Land
path_S2 = '../../../data/Sentinel/' # Sentinel-2
path_OGGM = '../../../data/OGGM/'
path_glogem = '../../../data/GloGEM' # glogem c_prec and t_off factors

# <------------------ OTHER USEFUL FUNCTIONS & ATTRIBUTES: ------------------>
vois_climate_long_name = {
't2m': 'Temperature',
'tp': 'Precipitation',
't2m_corr': 'Temperature corr.',
'tp_corr': 'Precipitation corr.',
'slhf': 'Surf. latent heat flux',
'sshf': 'Surf. sensible heat flux',
'ssrd': 'Surf. solar rad. down.',
'fal': 'Albedo',
'str': 'Surf. net thermal rad.',
'pcsr': 'Pot. in. clear sky solar rad.',
'u10': '10m E wind',
'v10': '10m N wind',
}

vois_units = {
't2m': 'C',
'tp': 'm w.e.',
't2m_corr': 'C',
'tp_corr': 'm w.e.',
'slhf': 'J m-2',
'sshf': 'J m-2',
'ssrd': 'J m-2',
'fal': '',
'str': 'J m-2',
'pcsr': 'J m-2',
'u10': 'm s-1',
'v10': 'm s-1',
}
85 changes: 85 additions & 0 deletions regions/Ecuador/scripts/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import torch
import numpy as np
import random as rd
import os
import gc
import shutil
from matplotlib.colors import to_hex
from matplotlib import pyplot as plt


def seed_all(seed=None):
"""Sets the random seed everywhere for reproducibility.
"""
if seed is None:
seed = 10 # Default seed value

# Python built-in random
rd.seed(seed)

# NumPy random
np.random.seed(seed)

# PyTorch seed
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # If using multiple GPUs

# Ensuring deterministic behavior in CuDNN
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False

# Setting CUBLAS environment variable (helps in newer versions)
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"


def free_up_cuda():
"""Frees up unused CUDA memory in PyTorch."""
gc.collect() # Run garbage collection
torch.cuda.empty_cache() # Free unused cached memory
torch.cuda.ipc_collect() # Collect inter-process memory


def get_cmap_hex(cmap, length):
"""
Function to get a get a list of colours as hex codes

:param cmap: name of colourmap
:type cmap: str

:return: list of hex codes
:rtype: list
"""
# Get cmap
rgb = plt.get_cmap(cmap)(np.linspace(0, 1, length))

# Convert to hex
hex_codes = [to_hex(rgb[i, :]) for i in range(rgb.shape[0])]

return hex_codes

def emptyfolder(path):
"""Removes all files and subdirectories in the given folder."""
if os.path.exists(path):
for item in os.listdir(path):
item_path = os.path.join(path, item)
try:
if os.path.isfile(item_path):
os.remove(item_path) # Remove file
elif os.path.isdir(item_path):
shutil.rmtree(item_path) # Remove folder and all contents
except Exception as e:
print(f"Error removing {item_path}: {e}")
else:
os.makedirs(path, exist_ok=True) # Ensure directory exists

# difference between two lists
def Diff(li1, li2):
li_dif = list(set(li1) - set(li2))
return li_dif

def format_rgi_code(X):
# Convert X to a string, and pad with leading zeros if its length is less than 5
Y = str(X).zfill(5)
# Return the final formatted string
return f"RGI60-11.{Y}"
Loading