#!/bin/bash

# SLURM configuration for MN5
# information about the queue : https://www.bsc.es/supportkc/docs/MareNostrum5/slurm

# HOW to SUBMIT 
# sbatch -A, --account={account} -q, --qos={qos} {job_script} [args]

# Single job with a single node (default)
##BATCH --qos=gp_debug
##SBATCH --qos=acc_debug
#SBATCH --qos=gp_bsces
#SBATCH --output=serial_preprocessing_training%j.out
#SBATCH --error=serial_preprocessing_training%j.err
##SBATCH --ntasks=112
#SBATCH --ntasks=112
#SBATCH --time=02:00:00
#SBATCH --mail-type=end
##SBATCH --mail-user=amirpasha.mozaffari@bsc.es


# Load appropriate modules and activate conda environment
if [ "$BSC_MACHINE" = "mn5" ]; then

    export MODULEPATH=$MODULEPATH:/apps/GPP/EASYBUILD/modules/all
    module load anaconda/2023.07
    conda init
    source /apps/GPP/MINICONDA/24.1.2/etc/profile.d/conda.sh
    conda activate /gpfs/projects/bsc32/repository/apps/conda_envs/ai4land_gp_2025v1
    export PYTHONPATH=/gpfs/projects/bsc32/repository/apps/conda_envs/ai4land_gp_2025v1/lib/python3.11/site-packages:$PYTHONPATH

elif [ "$BSC_MACHINE" = "nord3v2" ]; then
    module load GDAL/3.3.2-foss-2019b-Python-3.7.4
    module load pycdo/1.5.3-foss-2019b-Python-3.7.4
    module load pynco/0.0.2-foss-2019b-Python-3.7.4
fi

# Launch training script 
# Set parameters
START_YEAR=2012
END_YEAR=2014
PREPROCESSING_GOAL="training" #training inference

# Set paths for saving/loading processed data
SCRIPT_DIR=$(dirname "$(realpath "$PWD")")
PREPROCESSING_SCRIPT="$SCRIPT_DIR/source/preprocessing/preprocessing_lai_monthly.py"
SAVE_DATA_PATH="/gpfs/scratch/bsc32/ai4land/saved_data/"
INPUT_DATA_PATH="/gpfs/scratch/bsc32/cerise/outputs/bsc032013/0.0089_deg/"
REGIONS_DATA_PATH="$(dirname "$(dirname "$PWD")")/inputs/regions_LAI_39.json"

echo $REGIONS_DATA_PATH
# Run the Python script with arguments

#Generate the list of regions dynamically
#For instance, if your regions are numbered from 1 to 32 and follow the pattern "W001", "W002", ..., "W032"
# 16 - 39
REGION_START=25
REGION_END=25
REGIONS=()
for region_num in $(seq -f "%03g" $REGION_START $REGION_END); do
    REGIONS+=( "W${region_num}" )
done
#REGIONS=("W025")

echo "The preprocessing for $PREPROCESSING_GOAL is started."
# Loop through each region
for REGION in "${REGIONS[@]}"; do
    echo "Processing region: $REGION"
    
    python $PREPROCESSING_SCRIPT \
        --preprocessing_goal $PREPROCESSING_GOAL \
        --start_year $START_YEAR --end_year $END_YEAR \
        --region $REGION \
        --save_data_path $SAVE_DATA_PATH \
        --input_data_path $INPUT_DATA_PATH \
        --region_data_path $REGIONS_DATA_PATH \
        --include_luh2 --include_hilda --overwrite --inspection \
        --dtype_ground_truth float32 --dtype_luh2 float32 \
        --dtype_hilda float32 --dtype_esa_cci float32 \
        --autoregressive --create_watermask
    
    echo "Finished processing region: $REGION"
done

echo "The preprocessing for $PREPROCESSING_GOAL in all regions is finished."
