API Reference
This section provides a detailed overview of the MATLAB functions, scripts, and structure of the CryoGrid Optimization Automatization project.
Project Architecture (UML Diagram)
The above diagram summarizes how the main script (main_optimization_parallel.m) interacts with the
helper functions and CryoGrid modules.
Main Script
main_optimization_parallel.m
Feature Description :
Role : Entry point for the entire workflow. Handles setup, optimization, and output generation.
Inputs : Sensor ID, paths to (
data/forcing) folders, CryoGrid input Excel file and path, optimization settings.Outputs : Results in (
CryoGridCommunity_results/) (Excel files, plots, MATLAB .mat summaries).Key : Dependencies (
run_program_parallel.m), (objectiveFcn_parallel.m) and all helped functions in the folder.Notes: This is the only script you typically edit to configure a new simulation.
Workflow Functions (src/functions)
Function |
Description |
Key Inputs |
Key Outputs |
|---|---|---|---|
|
Supervises parallel worker execution, calls CryoGrid simulation, and collects results. |
Sensor ID, paths, parameters |
Results from each parallel simulation |
|
Find the sensor’s CSV daily_mean file in the folder by the sensor’s ID. |
Sensor ID, daily_mean folder path |
Sensor’s measure file |
|
Function which recover the sensor data in the PAPPROG_data.xlsx file. |
Sensor ID, PAPROG Excel file |
Structured sensor data |
|
Find the sensor’s .mat forcing file in the folder by the sensor’s ID. |
Sensor ID, forcing data folder path |
Sensor’s forcing file |
|
Find the parameter row in the CryoGrid Excel file. |
CryoGrid excel file, parameters name |
Parameters rows in the CryoGrid file |
|
Replace the new parameter value in the CryoGrid Excel file. |
Parameters row |
Updated CryoGrid Excel file |
|
Replace only the initial temperature value un the CryoGrid Excel file, because it’s different that other parameters. |
Intial temperature first row |
Updated CryoGrid Excel file |
|
Detects snow cover periods from daily temperature measurements. |
CSV file (daily temps) |
Snow cover periods (indices, plots) |
|
Create the basics optimization performance plot. |
Bayesian optimization results |
Basic optimization performance plot |
|
Merge all the optimization workers results files. |
PResults folder |
Complete optimization result file |
|
Objective function used by Bayesian optimization ( |
Parameters (albedo, z0, snow_fraction) |
Simulation score |
|
Function that allow to launch CryoGrid script in a loop. |
Excel metadata, sensor ID |
CryoGrid results |
|
Function which extract the simulated CryoGrid temperature results at the sensor depth. |
CryoGrid result file, sensor depth, altitude |
Simulated temperature and date vector |
|
Extract sensor measure dates and temperature values. |
CSV file (daily temps) |
Sensor observed dates and temperature |
|
Calculates the difference mean temperature by each year mean (Avoids bias if it miss a season in the measure. |
Simulated and observed temperature vectors |
Mean difference temperature |
|
Truncates the observed and simulated data to the same size. |
Simulated and observed date and temp vectors |
Simulated and observed date and temp vector with the same size |
|
Calculates seasonal performance metrics (RMSE, bias, seasonal fit, etc …). |
Simulated and observed temperature vectors |
Score, all global and seasonal statistics indicators per season |
|
Calculates a performance model score by key statistics indicators values. |
Stats and trends from following functions |
Performance score and key statistics indicators values |
|
Calculates the key statistics indicators values (RMSE, Bias, R2…). |
Simulated and observed temperature vectors |
Structured key statistics indicators values |
|
Calcules the key trends indicators values (slope, Sperman correlation, …). |
Simulated and observed temperature vectors |
Structured key trend indicators values |
|
Calculates the model performance during the five days after the snow periods. |
Snow periods |
Model performance score after snow period |
|
Calculates the model performance during the snow periods. |
Snow periods |
Model performance score during snow period |
|
extracts air temperature from forcing file. It allows to updated the initial temperature profil |
Forcing file |
Air temperature vector from forcing file |
Optimization Logic
Component |
Description |
|---|---|
Bayesian Optimization |
Global parameter search using |
Parallelization |
MATLAB Parallel Computing Toolbox used to run simulations concurrently. |
Lock Mechanism |
|
CryoGrid Modules (CryoGrid/)
Folder |
Description |
|---|---|
|
Core CryoGrid model source docs ans script. |
|
Output directory containing simulation results, plots, and Excel summaries. |
|
CryoGrid input Excel file updated dynamically by scripts. |
|
Stores default physical constants and parameter ranges. |
Data Structure
Folder |
Description |
|---|---|
|
Contains daily temperature CSV files. |
|
Excel file with sensor metadata (altitude, slope, massif ID, etc.). |
|
|
Temperature profile initialization logic
In the CG_single.xlsx file, you have to put an initial temperature profile that is the supposed
existing temperature column at the near-surface measurement site and that is therefore the inherited
ground temperature conditions.
Visualization of ‘CG_single.xlsx’ part with the initial temperature profil
In this optimization logic, it is first initialize at 0°C. We then try to assume the inherited temperature
profile. All layers are initialized at the same temperature for simplicity, and because these are
near-surface sensors, the programme focuses solely on near-surface temperature. To do so, every time there is a better
simulation (a better score), this profile T_ini is updated with this logic :
calculation of
T_mean_ref: the forcing data mean air temperature at the sensor location between 1991, January 1st and 2020, December 31th that is supposed to be representative of the climate condition at the measurement sitecalculation of
T_mean_sensor: the forcing data mean annual air temperature between the first day of the measurement time serie and the 365th day of the time serie and that is representative of the climate during the measurement periodcalculation of
T_mean_cryo: the mean annual near-surface temperature simulated with CryoGrid between the first and the 365th day of the simulated time serie
–> The new initial temperature profile is : T_ini = T_mean_cryo - abs(T_mean_sensor - T_mean_ref)
Score logic
To the oprimization be performant, the objective function have to score the simulation performance compared to the reference sensor temperature measures.
The score is build like this :
10 points based on the RMSE value
20 points based on the MAE value
20 points based on BIAS value
20 points based on R^2 value
10 points based on mean difference temperature value
10 points based on slope difference value
10 points based on Spearman’s rho value
10 points based on Directional agreement value
Total score on 110 points, normalized to 100 points.
This evaluation is done for all the season, and the final score is a ponderation of the each season score based on the season weights.
If you want to modify the score repartition, you have to do it in the score_model.m function. The season
weights are defined in the initial configuration on the main script.
Extending the Code
Adding a New Sensor
Add the new daily CSV file to (
data/Daily_mean/).Add metadata line to (
PAPROG_Data_set.xlsx), with a new sensor ID.Generate a forcing .mat file and place it in (
forcing/Forcing_Data/).Update informations in (
main_optimization_parallel).
Adding a New Parameter to Optimize
Adapte the parameter updating. Search where this parameter is between the (
CG_single.xlsx) or (CONSTANTS_excel.xlsx) files. It’s important to know in which file this parameter is. First because the actual optimized parameters are all in the (CG_single.xlsx), so the (find_parameter_rows) adn (replace_parameters) functions take (CG_single.xlsx) in argument and search in this file. If your new parameter is in the (CONSTANTS_excel.xlsx) file, you should add a step in (run_program_parallel) with the seraching functions with this new argument. Second, the parameter remplacement is made by dettecting the line with the parameter name, and change the value in the same line in the next column. But if the configuration is different you should modifiy or create a special function.Update Bayesian optimization variable definitions in the main.