#!/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_inference%j.out
##SBATCH --error=serial_inference%j.err
###SBATCH --ntasks=112
##SBATCH --ntasks=112
##SBATCH --time=24:00:00
##SBATCH --mail-type=end
###SBATCH --mail-user=amirpasha.mozaffari@bsc.es

#SBATCH -A bsc32
# number of the nodes
#SBATCH -N 1
# how many processes per single node
#SBATCH --ntasks-per-node 16
# how many core per process
#SBATCH -c 5
# how many gpus
#SBATCH --gres=gpu:4
##SBATCH -J testtask
##SBATCH -o testtask-%j.out
#SBATCH --output=serial_inference%j.out
#SBATCH --error=serial_inference%j.err
#SBATCH --time=10
#SBATCH --exclusive
#SBATCH --qos acc_debug


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

    export MODULEPATH=$MODULEPATH:/apps/GPP/EASYBUILD/modules/all
    # module load GDAL/3.8.5-foss-2023b
    # module load NCO/5.2.4-foss-2023b
    # module load miniconda
    # module load hdf5/1.14.1-2
    # module load pnetcdf/1.12.3
    # module load netcdf/2023-06-14
    # module load miniconda
    conda init
    source /apps/GPP/MINICONDA/24.1.2/etc/profile.d/conda.sh
    #conda activate /gpfs/projects/bsc32/repository/apps/conda_envs/LAND_may2024_v1
    #export PYTHONPATH=/gpfs/projects/bsc32/repository/apps/conda_envs/LAND_may2024_v1/lib/python3.12/site-packages:$PYTHONPATH
    conda activate /home/bsc/bsc032013/.conda/envs/cerise_g_202412
    export PYTHONPATH=/home/bsc/bsc032013/.conda/envs/cerise_g_202412/lib/python3.12/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=1939
END_YEAR=1999
PREPROCESSING_GOAL="inference" #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/cerise/outputs/bsc032013/ml/lai/window_model/monthly_prediction/global_39"
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=3
REGION_END=4
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."
