#!/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)
#SBATCH --qos=gp_debug
##SBATCH --qos=gp_bsces
#SBATCH --output=serial_%j.out
#SBATCH --error=serial_%j.err
#SBATCH --ntasks=112
#SBATCH --time=02:00:00
#SBATCH --mail-type=end
#SBATCH --mail-user=amirpasha.mozaffari@bsc.es

# SLURM configuration for NORD3
# more information here : https://earth.bsc.es/wiki/doku.php?id=computing:nord3-v2

# HOW to SUBMIT 
# sbatch -A, --account={account} -q, --qos={qos} {job_script} [args]
# sbatch -A bsc32 -q bsc_es launch_remap_NR3.sh

# Single job with a single node (default)
##SBATCH --qos=bsc_es
##SBATCH --output=serial_%j.out
##SBATCH --error=serial_%j.err
##SBATCH --ntasks=10
##SBATCH --time=00:30:00
##SBATCH --mail-type=end
##SBATCH --mail-user=amirpasha.mozaffari@bsc.es
##SBATCH --constraint=highmem
##SBATCH --exclusive

conda deactivate 

if [ "$BSC_MACHINE" = mn5 ]; then
    module load miniconda
    conda init
    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 hdf5/1.14.1-2
    module load pnetcdf/1.12.3
    module load netcdf/2023-06-14
    #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
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

idir="/gpfs/projects/bsc32/cerise/KIT/hildap_vGLOB-1.0-f_netcdf"
odir="/gpfs/projects/bsc32/cerise/outputs/bsc032013/0.0089_deg/hilda"
tmpdir="/gpfs/projects/bsc32/cerise/outputs/bsc032013/tmp"
resolution=0.0089         # Resolution for output
output_width=40320
output_height=20159
xmin=-180.0044643
ymin=-89.9955357
xmax=179.9955357
ymax=89.9955357

years=$(seq 1920 1991)

for year in ${years}; do
  echo "Processing year: ${year} from idir"
  ifile="${idir}/hildaplus_GLOB-1-0-f_states.nc"
  ofile="${odir}/hildaplus_GLOB-1-0-f_states_${year}_${resolution}deg.nc"

  # Call the script with arguments
  ./regrid_hildaplus.sh $ifile $ofile $tmpdir $resolution $year $output_width $output_height $xmin $ymin $xmax $ymax

done 
echo "All years are processed"
