My goal is to load different .asc files at each time step using a matrix<float> to assign values dynamically to a predefined grid. However, I encountered a "matrix index out of bounds" error during runtime.
Based on my testing I suspect the issue is due to the .asc files including a 6-line header (containing ncols, nrows, etc.), which is not automatically skipped when using matrix(file(...)).
What is the correct way to dynamically load .asc files? Or is there a better practice within GAMA to parse the data correctly?
Thank you for all your help.
the script is as follow:
model asc
/* Insert your model definition here */
global {
list<string> file_paths <- [
"E:/dv_distributed_1step.asc",
"E:/dv_distributed_2step.asc"
];
int my_step <- 0;
matrix<float> current_data;
float nodata <- -9999;
int val_color;
file grid_data1 <- file('E:/dv_distributed_1step1.asc');
geometry shape <- envelope(grid_data1);
//init
init {
current_data <- matrix(file(file_paths[my_step]));
ask flood_cell { do load_step; }
}
// reflex
reflex update_step when: my_step < length(file_paths) - 1 {
my_step <- my_step + 1;
current_data <- matrix(file(file_paths[my_step]));
ask flood_cell { do load_step; }
}
}
grid flood_cell width: 176 height: 371 {
float water <- 0.0;
action load_step {
if (grid_y < length(current_data) and grid_x < length(current_data[0])) {
float grid_value <- current_data[grid_y, grid_x];
val_color <- max([0, min([255, int(255 * (1 - (grid_value / 10)) )])]);
color <- [val_color, val_color, 255];
} else {
write "beyond: grid_x=" + grid_x + " grid_y=" + grid_y;
}
}
}
experiment flood_demo type: gui {
output {
display "Flood" type:2d{
grid flood_cell ;
}
}
}