## Focal LULC {#20_FocalLULC}

This step calculates focal statistics for the land use and land cover change (LULCC) data.
The resulting focal windows are used for the N-SDM model [@lulcc2024].
It uses a similar structure to the previous [Check LULCC](#11_CheckLULCC) step,
as it uses another conda environment
and this task also has a separate job script.
The configuration section for this step is as follows:


```{.yaml filename="src/config.yml" code-copy="false"}
# Focal LULC
FocalLULCC:
  InputDir: # keep empty to use FUTURE_EI_OUTPUT_DIR/LULCC_CH_OUTPUT_BASE_DIR
  OutputDir: # keep empty to use FUTURE_EI_OUTPUT_DIR/FOCAL_OUTPUT_BASE_DIR
  BaseName: ch_lulc_agg11_future_pixel  # Underscores will be split into folders
  RadiusList: [ 100, 200, 500, 1500, 3000 ]
  WindowType: circle
  FocalFunction: mean
  Overwrite: False # False -> skip if output exists, True -> overwrite
  Parallel: True
  NWorkers: 0  # 0 means use all available cores
```
This script recursively goes through the input directory and calculates the focal statistics for each scenario.
It creates the outputs in a similar structure, inside the output directory, named after the `BaseName`.
For each scenario, the focal statistics by `WindowType` and `FocalFunction`
are calculated for each radius in `RadiusList`.
For details, consult the docstring of the method
[`20_focal_statistics::simulated_lulc_to_predictors`](https://github.com/ethzplus/evoland-plus-HPC/blob/main/src/steps/20_FocalLULC/20_focal_statistics.R#L260-L323).

This step uses a conda environment with `raster~=3.6-26`, `terra~=1.7-71`
(only used for conversion), and further R packages.
The conda environment `focal_lulc` is set up by executing the setup script  
[`src/steps/20_FocalLULC/20_FocalLULC_setup.sh`](https://github.com/ethzplus/evoland-plus-HPC/tree/main/src/steps/20_FocalLULC/20_FocalPrep_setup.sh).

As for the previous steps, the job script
[`slurm_job.sh`](https://github.com/ethzplus/evoland-plus-HPC/tree/main/src/steps/20_FocalLULC/slurm_job.sh)
needs to be submitted to the HPC scheduler.

```{.bash}
sbatch src/steps/20_FocalLULC/slurm_job.sh
```

:::{#note-focal-output .callout-note}
#### Focal LULC output check
The `src/steps/20_FocalLULC/show_files.py` script checks whether the focal
window output files are complete.
It verifies the presence of expected files in the output directory structure,
calculates the percentage of completed files for each year,
and lists any missing files.
The script outputs a summary table and saves the missing file names to a
text file called `missing_files.txt`.
:::
