#!/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_postprocessing_inference%j.out
#SBATCH --error=serial_postprocessing_inference%j.err
##SBATCH --ntasks=112
#SBATCH --ntasks=112
#SBATCH --time=06: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 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=1990
START_YEAR_TRAINING=1991
END_YEAR_TRAINING=2014
START_YEAR_INFERENCE=1939
END_YEAR_INFERENCE=1989
# WINDOWS="W001 W002 W003 W004 W005 W006 W007 W008 W009 W010 \
# W011 W012 W013 W014 W015 W016 W017 W018 W019 W020 \
# W021 W022 W023 W024 W025 W026 W027 W028 W029 W030 \
# W031 W032 W033 W034"
WINDOWS="W001 W002 W003 W004 W005 W006 W008 W009 W010 \
W011 W012 W013 W014 W015 W017 W018 W019 W020 \
W021 W022 W023 W024 W026 W027 W028 W029 W030 \
W031 W032 W033 W034 W035 W036 W037 W038 W039"
WINDOW_TIMESTAMPS="20241218_110704"
PREPROCESSING_GOAL="global" 

# Set paths for saving/loading processed data
SCRIPT_DIR=$(dirname "$(realpath "$PWD")")
POSTPROCESSING_SCRIPT="$SCRIPT_DIR/source/postprocessing/postprocessing_global.py"

SAVE_DATA_PATH="/gpfs/scratch/bsc32/cerise/outputs/bsc032013/ml/lai/window_model/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"
PREDICTION_DATA_PATH="/gpfs/scratch/bsc32/cerise/outputs/bsc032013/ml/lai/window_model/global_39_output/"
REGION="global"  # Define the region
OUTPUT_DIR="/gpfs/scratch/bsc32/cerise/outputs/bsc032013/ml/lai/window_model/global_39_combined/"
# Run the Python script with arguments
python "$POSTPROCESSING_SCRIPT" --preprocessing_goal $PREPROCESSING_GOAL \
--start_year $START_YEAR --end_year $END_YEAR \
--start_year_training $START_YEAR_TRAINING --end_year_training $END_YEAR_TRAINING \
--start_year_inference $START_YEAR_INFERENCE --end_year_inference $END_YEAR_INFERENCE \
--save_data_path $SAVE_DATA_PATH --input_data_path $INPUT_DATA_PATH \
--predictions_dir $PREDICTION_DATA_PATH --regions_file $REGIONS_DATA_PATH  \
--windows $WINDOWS --output_dir $OUTPUT_DIR --region $REGION  \
--window_timestamps $WINDOW_TIMESTAMPS